Debian Codegraph @

java-sdk/ietfldap/org/ietf/ldap/util/MimeBase64Encoder.java

raw symbols SCIP
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
41 /** Implements a plaintext -> Base64 encoder.
42 */
43 public final class MimeBase64Encoder extends MimeEncoder {
44
45 static final long serialVersionUID = 8781620079813078315L;
46 private int buf = 0; // a 24-bit quantity
47 private int buf_bytes = 0; // how many octets are set in it
48 private byte line[] = new byte[74]; // output buffer
49 private int line_length = 0; // output buffer fill pointer
50
51 static private final byte crlf[] = "\r\n".getBytes();
52
53 static private final char map[] = {
54 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // 0-7
55 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 8-15
56 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 16-23
57 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', // 24-31
58 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', // 32-39
59 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', // 40-47
60 'w', 'x', 'y', 'z', '0', '1', '2', '3', // 48-55
61 '4', '5', '6', '7', '8', '9', '+', '/', // 56-63
62 };
63
64 private final void encode_token() {
65 int i = line_length;
66 line[i] = (byte)map[0x3F & (buf >> 18)]; // sextet 1 (octet 1)
67 line[i+1] = (byte)map[0x3F & (buf >> 12)]; // sextet 2 (octet 1 and 2)
68 line[i+2] = (byte)map[0x3F & (buf >> 6)]; // sextet 3 (octet 2 and 3)
69 line[i+3] = (byte)map[0x3F & buf]; // sextet 4 (octet 3)
70 line_length += 4;
71 buf = 0;
72 buf_bytes = 0;
73 }
74
75 private final void encode_partial_token() {
76 int i = line_length;
77 line[i] = (byte)map[0x3F & (buf >> 18)]; // sextet 1 (octet 1)
78 line[i+1] = (byte)map[0x3F & (buf >> 12)]; // sextet 2 (octet 1 and 2)
79
80 if (buf_bytes == 1)
81 line[i+2] = (byte)'=';
82 else
83 line[i+2] = (byte)map[0x3F & (buf >> 6)]; // sextet 3 (octet 2 and 3)
84
85 if (buf_bytes <= 2)
86 line[i+3] = (byte)'=';
87 else
88 line[i+3] = (byte)map[0x3F & buf]; // sextet 4 (octet 3)
89 line_length += 4;
90 buf = 0;
91 buf_bytes = 0;
92 }
93
94 private final void flush_line(ByteBuf out) {
95 out.append(line, 0, line_length);
96 line_length = 0;
97 }
98
99 /** Given a sequence of input bytes, produces a sequence of output bytes
100 using the base64 encoding. If there are bytes in `out' already, the
101 new bytes are appended, so the caller should do `out.setLength(0)'
102 first if that's desired.
103 */
104 public final void translate(ByteBuf in, ByteBuf out) {
105
106 byte inb[] = in.toBytes();
107 int in_length = in.length();
108
109 for (int i = 0; i < in_length; i++) {
110 if (buf_bytes == 0)
111 buf = (buf & 0x00FFFF) | (inb[i] << 16);
112 else if (buf_bytes == 1)
113 buf = (buf & 0xFF00FF) | ((inb[i] << 8) & 0x00FFFF);
114 else
115 buf = (buf & 0xFFFF00) | (inb[i] & 0x0000FF);
116
117 if ((++buf_bytes) == 3) {
118 encode_token();
119 if (line_length >= 72) {
120 flush_line(out);
121 }
122 }
123
124 if (i == (in_length-1)) {
125 if ((buf_bytes > 0) && (buf_bytes < 3))
126 encode_partial_token();
127 if (line_length > 0)
128 flush_line(out);
129 }
130 }
131
132 for (int i=0; i<line.length; i++)
133 line[i] = 0;
134 }
135
136 /** Tell the base64 encoder that no more input data will be forthcoming.
137 This may result in output, as a result of flushing the internal buffer.
138 This object must not be used again after calling eof(). If there are
139 bytes in `out' already, the new bytes are appended, so the caller should
140 do `out.setLength(0)' first if that's desired.
141 */
142 public final void eof(ByteBuf out) {
143 if (buf_bytes != 0)
144 encode_partial_token();
145 flush_line(out);
146 for (int i=0; i<line.length; i++)
147 line[i] = 0;
148 }
149 }