| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
| 2 | * |
| 3 | * ***** BEGIN LICENSE BLOCK ***** |
| 4 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
| 5 | * |
| 6 | * The contents of this file are subject to the Mozilla Public License Version |
| 7 | * 1.1 (the "License"); you may not use this file except in compliance with |
| 8 | * the License. You may obtain a copy of the License at |
| 9 | * http://www.mozilla.org/MPL/ |
| 10 | * |
| 11 | * Software distributed under the License is distributed on an "AS IS" basis, |
| 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 13 | * for the specific language governing rights and limitations under the |
| 14 | * License. |
| 15 | * |
| 16 | * The Original Code is mozilla.org code. |
| 17 | * |
| 18 | * The Initial Developer of the Original Code is |
| 19 | * Netscape Communications Corporation. |
| 20 | * Portions created by the Initial Developer are Copyright (C) 1999 |
| 21 | * the Initial Developer. All Rights Reserved. |
| 22 | * |
| 23 | * Contributor(s): |
| 24 | * |
| 25 | * Alternatively, the contents of this file may be used under the terms of |
| 26 | * either the GNU General Public License Version 2 or later (the "GPL"), or |
| 27 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 28 | * in which case the provisions of the GPL or the LGPL are applicable instead |
| 29 | * of those above. If you wish to allow use of your version of this file only |
| 30 | * under the terms of either the GPL or the LGPL, and not to allow others to |
| 31 | * use your version of this file under the terms of the MPL, indicate your |
| 32 | * decision by deleting the provisions above and replace them with the notice |
| 33 | * and other provisions required by the GPL or the LGPL. If you do not delete |
| 34 | * the provisions above, a recipient may use your version of this file under |
| 35 | * the terms of any one of the MPL, the GPL or the LGPL. |
| 36 | * |
| 37 | * ***** END LICENSE BLOCK ***** */ |
| 38 | package org.ietf.ldap.util; |
| 39 | |
| 40 | /** Implements a plaintext -> Base64 encoder. |
| 41 | */ |
| 42 | public final class MimeBase64Decoder extends MimeEncoder { |
| 43 | |
| 44 | static final long serialVersionUID = 797397585345375903L; |
| 45 | private byte token[] = new byte[4]; // input buffer |
| 46 | private byte bytes[] = new byte[3]; // output buffer |
| 47 | private int token_length = 0; // input buffer length |
| 48 | |
| 49 | static private final byte NUL = 127; // must be out of range 0-64 |
| 50 | static private final byte EOF = 126; // must be out of range 0-64 |
| 51 | |
| 52 | static private final byte map[] = { |
| 53 | NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, // 000-007 |
| 54 | NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, // 010-017 |
| 55 | NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, // 020-027 |
| 56 | NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, // 030-037 |
| 57 | NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, // 040-047 !"#$%&' |
| 58 | NUL, NUL, NUL, 62, NUL, NUL, NUL, 63, // 050-057 ()*+,-./ |
| 59 | 52, 53, 54, 55, 56, 57, 58, 59, // 060-067 01234567 |
| 60 | 60, 61, NUL, NUL, NUL, EOF, NUL, NUL, // 070-077 89:;<=>? |
| 61 | |
| 62 | NUL, 0, 1, 2, 3, 4, 5, 6, // 100-107 @ABCDEFG |
| 63 | 7, 8, 9, 10, 11, 12, 13, 14, // 110-117 HIJKLMNO |
| 64 | 15, 16, 17, 18, 19, 20, 21, 22, // 120-127 PQRSTUVW |
| 65 | 23, 24, 25, NUL, NUL, NUL, NUL, NUL, // 130-137 XYZ[\]^_ |
| 66 | NUL, 26, 27, 28, 29, 30, 31, 32, // 140-147 `abcdefg |
| 67 | 33, 34, 35, 36, 37, 38, 39, 40, // 150-157 hijklmno |
| 68 | 41, 42, 43, 44, 45, 46, 47, 48, // 160-167 pqrstuvw |
| 69 | 49, 50, 51, NUL, NUL, NUL, NUL, NUL, // 170-177 xyz{|}~ |
| 70 | |
| 71 | NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, // 200-207 |
| 72 | NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, // 210-217 |
| 73 | NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, // 220-227 |
| 74 | NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, // 230-237 |
| 75 | NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, // 240-247 |
| 76 | NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, // 250-257 |
| 77 | NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, // 260-267 |
| 78 | NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, // 270-277 |
| 79 | |
| 80 | NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, // 300-307 |
| 81 | NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, // 310-317 |
| 82 | NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, // 320-327 |
| 83 | NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, // 330-337 |
| 84 | NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, // 340-347 |
| 85 | NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, // 350-357 |
| 86 | NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, // 360-367 |
| 87 | NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, // 370-377 |
| 88 | }; |
| 89 | |
| 90 | |
| 91 | // Fast routine that assumes full 4-char tokens with no '=' in them. |
| 92 | // |
| 93 | private final void decode_token(ByteBuf out) { |
| 94 | int num = ((map[token[0]] << 18) | |
| 95 | (map[token[1]] << 12) | |
| 96 | (map[token[2]] << 6) | |
| 97 | (map[token[3]])); |
| 98 | |
| 99 | bytes[0] = (byte) (0xFF & (num >> 16)); |
| 100 | bytes[1] = (byte) (0xFF & (num >> 8)); |
| 101 | bytes[2] = (byte) (0xFF & num); |
| 102 | |
| 103 | out.append(bytes); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | // Hairier routine that deals with the final token, which can have fewer |
| 108 | // than four characters, and that might be padded with '='. |
| 109 | // |
| 110 | private final void decode_final_token(ByteBuf out) { |
| 111 | |
| 112 | byte b0 = map[token[0]]; |
| 113 | byte b1 = map[token[1]]; |
| 114 | byte b2 = map[token[2]]; |
| 115 | byte b3 = map[token[3]]; |
| 116 | |
| 117 | int eq_count = 0; |
| 118 | |
| 119 | if (b0 == EOF) { b0 = 0; eq_count++; } |
| 120 | if (b1 == EOF) { b1 = 0; eq_count++; } |
| 121 | if (b2 == EOF) { b2 = 0; eq_count++; } |
| 122 | if (b3 == EOF) { b3 = 0; eq_count++; } |
| 123 | |
| 124 | int num = ((b0 << 18) | (b1 << 12) | (b2 << 6) | (b3)); |
| 125 | |
| 126 | // eq_count will be 0, 1, or 2. |
| 127 | // No "=" padding means 4 bytes mapped to 3, the normal case, |
| 128 | // not handled in this routine. |
| 129 | // "xxx=" means 3 bytes mapped to 2. |
| 130 | // "xx==" means 2 bytes mapped to 1. |
| 131 | // "x===" can't happen, because "x" would then be encoding |
| 132 | // only 6 bits, not 8, the minimum possible. |
| 133 | |
| 134 | out.append((byte) (num >> 16)); // byte 1, count = 0 or 1 or 2 |
| 135 | if (eq_count <= 1) { |
| 136 | out.append((byte) ((num >> 8) & 0xFF)); // byte 2, count = 0 or 1 |
| 137 | if (eq_count == 0) { |
| 138 | out.append((byte) (num & 0xFF)); // byte 3, count = 0 |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | |
| 144 | /** Given a sequence of input bytes using the Base64 encoding, produces a |
| 145 | sequence of unencoded output bytes. Note that some (small) amount of |
| 146 | buffering may be necessary, if the input byte stream didn't fall on an |
| 147 | appropriate boundary. If there are bytes in `out' already, the new |
| 148 | bytes are appended, so the caller should do `out.setLength(0)' first |
| 149 | if that's desired. |
| 150 | */ |
| 151 | public final void translate(ByteBuf in, ByteBuf out) { |
| 152 | |
| 153 | if (token == null) // already saw eof marker? |
| 154 | return; |
| 155 | |
| 156 | byte inb[] = in.toBytes(); |
| 157 | int in_length = in.length(); |
| 158 | |
| 159 | for (int i = 0; i < in_length; i++) { |
| 160 | byte b = inb[i]; |
| 161 | int x=(b&0xff); |
| 162 | byte t = map[x]; |
| 163 | |
| 164 | if (t != NUL) |
| 165 | token[token_length++] = b; |
| 166 | |
| 167 | if (t == EOF) { |
| 168 | eof(out); |
| 169 | return; |
| 170 | } else if (token_length == 4) { |
| 171 | decode_token(out); |
| 172 | token_length = 0; |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | |
| 178 | /** Tell the Base64 decoder that no more input data will be forthcoming. |
| 179 | This may result in output, as a result of flushing the internal buffer. |
| 180 | This object must not be used again after calling eof(). If there are |
| 181 | bytes in `out' already, the new bytes are appended, so the caller should |
| 182 | do `out.setLength(0)' first if that's desired. |
| 183 | */ |
| 184 | public final void eof(ByteBuf out) { |
| 185 | if (token != null && token_length != 0) { |
| 186 | while (token_length < 4) |
| 187 | token[token_length++] = (byte)'='; |
| 188 | decode_final_token(out); |
| 189 | } |
| 190 | token_length = 0; |
| 191 | token = new byte[4]; |
| 192 | bytes = new byte[3]; |
| 193 | } |
| 194 | } |