2
* $Id: UTF7imap.java,v 1.2 2003/10/19 16:16:50 dog Exp $
3
* Copyright (C) 2003 The Free Software Foundation
5
* This file is part of GNU inetlib, a library.
7
* GNU inetlib is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* GNU inetlib is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this library; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
* As a special exception, if you link this library with other files to
22
* produce an executable, this library does not by itself cause the
23
* resulting executable to be covered by the GNU General Public License.
24
* This exception does not however invalidate any other reasons why the
25
* executable file might be covered by the GNU General Public License.
28
package gnu.inet.imap;
30
import java.io.ByteArrayOutputStream;
31
import java.io.IOException;
34
* Encodes and decodes text according to the IMAP4rev1 mailbox name
37
* @author <a href="mailto:dog@gnu.org">Chris Burdess</a>
38
* @version $Revision: 1.2 $ $Date: 2003/10/19 16:16:50 $
40
public final class UTF7imap
43
private static final String US_ASCII = "US-ASCII";
45
private static final byte[] src = {
46
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,
47
0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54,
48
0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x61, 0x62, 0x63, 0x64,
49
0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e,
50
0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
51
0x79, 0x7a, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
52
0x38, 0x39, 0x2b, 0x2c
55
private static final byte[] dst;
58
dst = new byte[0x100];
59
for (int i = 0; i < 0xff; i++)
61
for (int i = 0; i < src.length; i++)
62
dst[src[i]] = (byte) i;
71
* Encode the specified byte array using the modified BASE64 algorithm
72
* specified by UTF-7, with further IMAP4rev1 modifications.
74
* @param bs the source byte array
76
static byte[] encode(byte[]bs)
78
int si = 0, ti = 0; // source/target array indices
79
byte[]bt = new byte[((bs.length + 2) / 3) * 4]; // target byte array
80
for (; si < bs.length; si += 3)
82
int buflen = bs.length - si;
88
bt[ti++] = src[b >>> 2 & 0x3f];
89
bt[ti++] = src[(b << 4 & 0x30) + (i >>> 4 & 0xf)];
93
byte b1 = bs[si], b2 = bs[si + 1];
95
bt[ti++] = src[b1 >>> 2 & 0x3f];
96
bt[ti++] = src[(b1 << 4 & 0x30) + (b2 >>> 4 & 0xf)];
97
bt[ti++] = src[(b2 << 2 & 0x3c) + (i >>> 6 & 0x3)];
101
byte b1 = bs[si], b2 = bs[si + 1], b3 = bs[si + 2];
102
bt[ti++] = src[b1 >>> 2 & 0x3f];
103
bt[ti++] = src[(b1 << 4 & 0x30) + (b2 >>> 4 & 0xf)];
104
bt[ti++] = src[(b2 << 2 & 0x3c) + (b3 >>> 6 & 0x3)];
105
bt[ti++] = src[b3 & 0x3f];
112
* Decode the specified byte array using the modified BASE64 algorithm
113
* specified by UTF-7, with further IMAP4rev1 modifications.
115
* @param bs the source byte array
117
static int[] decode(byte[]bs)
119
int[] buffer = new int[bs.length];
122
int len = bs.length - si;
125
byte b0 = dst[bs[si++] & 0xff];
126
byte b2 = dst[bs[si++] & 0xff];
127
buffer[buflen++] = (b0 << 2 & 0xfc | b2 >>> 4 & 0x3);
131
b2 = dst[bs[si++] & 0xff];
132
buffer[buflen++] = (b0 << 4 & 0xf0 | b2 >>> 2 & 0xf);
136
b2 = dst[bs[si++] & 0xff];
137
buffer[buflen++] = (b0 << 6 & 0xc0 | b2 & 0x3f);
140
len = bs.length - si;
142
int[] bt = new int[buflen];
143
System.arraycopy(buffer, 0, bt, 0, buflen);
148
* Encodes the specified name using the UTF-7.imap encoding.
149
* See IMAP4rev1 spec, section 5.1.3
151
public static String encode(String name)
155
StringBuffer buffer = null;
156
ByteArrayOutputStream encoderSink = null;
157
char[] chars = name.toCharArray();
158
boolean encoding = false;
159
for (int i = 0; i < chars.length; i++)
166
buffer = new StringBuffer();
167
for (int j = 0; j < i; j++)
168
buffer.append(chars[j]);
173
if (c < 0x1f || c > 0x7f)
178
buffer = new StringBuffer();
179
for (int j = 0; j < i; j++)
180
buffer.append(chars[j]);
181
encoderSink = new ByteArrayOutputStream();
189
encoderSink.write(((int) c) / 0x100);
190
encoderSink.write(((int) c) % 0x100);
195
byte[]encoded = encode(encoderSink.toByteArray());
196
buffer.append(new String(encoded, US_ASCII));
202
else if (buffer != null)
208
byte[]encoded = encode(encoderSink.toByteArray());
209
buffer.append(new String(encoded, US_ASCII));
213
return buffer.toString();
217
// This should never happen
218
throw new RuntimeException(e.getMessage());
224
* Decodes the specified name using the UTF-7.imap decoding.
225
* See IMAP4rev1 spec, section 5.1.3
227
public static String decode(String name)
229
StringBuffer buffer = null;
230
ByteArrayOutputStream decoderSink = null;
231
char[] chars = name.toCharArray();
232
boolean encoded = false;
233
for (int i = 0; i < chars.length; i++)
240
buffer = new StringBuffer();
241
decoderSink = new ByteArrayOutputStream();
242
for (int j = 0; j < i; j++)
243
buffer.append(chars[j]);
248
else if (c == '-' && encoded)
250
if (decoderSink.size() == 0)
254
int[] decoded = decode(decoderSink.toByteArray());
255
for (int j = 0; j < decoded.length - 1; j += 2)
257
int hibyte = decoded[j];
258
int lobyte = decoded[j + 1];
259
int d = (hibyte * 0x100) | lobyte;
260
buffer.append((char) d);
266
decoderSink.write((byte) c);
267
else if (buffer != null)
271
return buffer.toString();
275
public static void main(String[]args)
277
boolean decode = false;
278
for (int i = 0; i < args.length; i++)
280
if (args[i].equals("-d"))
285
System.out.println(args[i] + " = " + decode(args[i]));
287
System.out.println(args[i] + " = " + encode(args[i]));