~ubuntu-branches/ubuntu/karmic/libgnuinet-java/karmic

« back to all changes in this revision

Viewing changes to source/gnu/inet/imap/UTF7imap.java

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2004-04-14 12:42:10 UTC
  • Revision ID: james.westby@ubuntu.com-20040414124210-osc3q0wzthgme27p
Tags: upstream-0.0.cvs20031116
ImportĀ upstreamĀ versionĀ 0.0.cvs20031116

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Id: UTF7imap.java,v 1.2 2003/10/19 16:16:50 dog Exp $
 
3
 * Copyright (C) 2003 The Free Software Foundation
 
4
 * 
 
5
 * This file is part of GNU inetlib, a library.
 
6
 * 
 
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.
 
11
 * 
 
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.
 
16
 * 
 
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
 
20
 *
 
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.
 
26
 */
 
27
 
 
28
package gnu.inet.imap;
 
29
 
 
30
import java.io.ByteArrayOutputStream;
 
31
import java.io.IOException;
 
32
 
 
33
/**
 
34
 * Encodes and decodes text according to the IMAP4rev1 mailbox name
 
35
 * encoding scheme.
 
36
 *
 
37
 * @author <a href="mailto:dog@gnu.org">Chris Burdess</a>
 
38
 * @version $Revision: 1.2 $ $Date: 2003/10/19 16:16:50 $
 
39
 */
 
40
public final class UTF7imap
 
41
{
 
42
 
 
43
  private static final String US_ASCII = "US-ASCII";
 
44
 
 
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
 
53
  };
 
54
 
 
55
  private static final byte[] dst;
 
56
  static
 
57
  {
 
58
    dst = new byte[0x100];
 
59
    for (int i = 0; i < 0xff; i++)
 
60
      dst[i] = -1;
 
61
    for (int i = 0; i < src.length; i++)
 
62
      dst[src[i]] = (byte) i;
 
63
 
 
64
  }
 
65
 
 
66
  private UTF7imap()
 
67
  {
 
68
  }
 
69
 
 
70
  /**
 
71
   * Encode the specified byte array using the modified BASE64 algorithm
 
72
   * specified by UTF-7, with further IMAP4rev1 modifications.
 
73
   *
 
74
   * @param bs the source byte array
 
75
   */
 
76
  static byte[] encode(byte[]bs)
 
77
  {
 
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)
 
81
    {
 
82
      int buflen = bs.length - si;
 
83
      if (buflen == 1)
 
84
      {
 
85
        byte b = bs[si];
 
86
        int i = 0;
 
87
        boolean flag = false;
 
88
        bt[ti++] = src[b >>> 2 & 0x3f];
 
89
        bt[ti++] = src[(b << 4 & 0x30) + (i >>> 4 & 0xf)];
 
90
      }
 
91
      else if (buflen == 2)
 
92
      {
 
93
        byte b1 = bs[si], b2 = bs[si + 1];
 
94
        int i = 0;
 
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)];
 
98
      }
 
99
      else if (buflen == 3)
 
100
      {
 
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];
 
106
      }
 
107
    }
 
108
    return bt;
 
109
  }
 
110
 
 
111
  /**
 
112
   * Decode the specified byte array using the modified BASE64 algorithm
 
113
   * specified by UTF-7, with further IMAP4rev1 modifications.
 
114
   *
 
115
   * @param bs the source byte array
 
116
   */
 
117
  static int[] decode(byte[]bs)
 
118
  {
 
119
    int[] buffer = new int[bs.length];
 
120
    int buflen = 0;
 
121
    int si = 0;
 
122
    int len = bs.length - si;
 
123
    while (len > 0)
 
124
    {
 
125
      byte b0 = dst[bs[si++] & 0xff];
 
126
      byte b2 = dst[bs[si++] & 0xff];
 
127
      buffer[buflen++] = (b0 << 2 & 0xfc | b2 >>> 4 & 0x3);
 
128
      if (len > 2)
 
129
      {
 
130
        b0 = b2;
 
131
        b2 = dst[bs[si++] & 0xff];
 
132
        buffer[buflen++] = (b0 << 4 & 0xf0 | b2 >>> 2 & 0xf);
 
133
        if (len > 3)
 
134
        {
 
135
          b0 = b2;
 
136
          b2 = dst[bs[si++] & 0xff];
 
137
          buffer[buflen++] = (b0 << 6 & 0xc0 | b2 & 0x3f);
 
138
        }
 
139
      }
 
140
      len = bs.length - si;
 
141
    }
 
142
    int[] bt = new int[buflen];
 
143
    System.arraycopy(buffer, 0, bt, 0, buflen);
 
144
    return bt;
 
145
  }
 
146
 
 
147
  /**
 
148
   * Encodes the specified name using the UTF-7.imap encoding.
 
149
   * See IMAP4rev1 spec, section 5.1.3
 
150
   */
 
151
  public static String encode(String name)
 
152
  {
 
153
    try
 
154
    {
 
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++)
 
160
      {
 
161
        char c = chars[i];
 
162
        if (c == '&')
 
163
        {
 
164
          if (buffer == null)
 
165
          {
 
166
            buffer = new StringBuffer();
 
167
            for (int j = 0; j < i; j++)
 
168
              buffer.append(chars[j]);
 
169
          }
 
170
          buffer.append('&');
 
171
          buffer.append('-');
 
172
        }
 
173
        if (c < 0x1f || c > 0x7f)
 
174
        {
 
175
          // needs encoding
 
176
          if (buffer == null)
 
177
          {
 
178
            buffer = new StringBuffer();
 
179
            for (int j = 0; j < i; j++)
 
180
              buffer.append(chars[j]);
 
181
            encoderSink = new ByteArrayOutputStream();
 
182
          }
 
183
          if (!encoding)
 
184
          {
 
185
            encoderSink.reset();
 
186
            buffer.append('&');
 
187
            encoding = true;
 
188
          }
 
189
          encoderSink.write(((int) c) / 0x100);
 
190
          encoderSink.write(((int) c) % 0x100);
 
191
        }
 
192
        else if (encoding)
 
193
        {
 
194
          encoderSink.flush();
 
195
          byte[]encoded = encode(encoderSink.toByteArray());
 
196
          buffer.append(new String(encoded, US_ASCII));
 
197
          buffer.append('-');
 
198
          encoding = false;
 
199
          if (c != '-')
 
200
            buffer.append(c);
 
201
        }
 
202
        else if (buffer != null)
 
203
          buffer.append(c);
 
204
      }
 
205
      if (encoding)
 
206
      {
 
207
        encoderSink.flush();
 
208
        byte[]encoded = encode(encoderSink.toByteArray());
 
209
        buffer.append(new String(encoded, US_ASCII));
 
210
        buffer.append('-');
 
211
      }
 
212
      if (buffer != null)
 
213
        return buffer.toString();
 
214
    }
 
215
    catch(IOException e)
 
216
    {
 
217
      // This should never happen
 
218
      throw new RuntimeException(e.getMessage());
 
219
    }
 
220
    return name;
 
221
  }
 
222
 
 
223
  /**
 
224
   * Decodes the specified name using the UTF-7.imap decoding.
 
225
   * See IMAP4rev1 spec, section 5.1.3
 
226
   */
 
227
  public static String decode(String name)
 
228
  {
 
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++)
 
234
    {
 
235
      char c = chars[i];
 
236
      if (c == '&')
 
237
      {
 
238
        if (buffer == null)
 
239
        {
 
240
          buffer = new StringBuffer();
 
241
          decoderSink = new ByteArrayOutputStream();
 
242
          for (int j = 0; j < i; j++)
 
243
            buffer.append(chars[j]);
 
244
        }
 
245
        decoderSink.reset();
 
246
        encoded = true;
 
247
      }
 
248
      else if (c == '-' && encoded)
 
249
      {
 
250
        if (decoderSink.size() == 0)
 
251
          buffer.append('&');
 
252
        else
 
253
        {
 
254
          int[] decoded = decode(decoderSink.toByteArray());
 
255
          for (int j = 0; j < decoded.length - 1; j += 2)
 
256
          {
 
257
            int hibyte = decoded[j];
 
258
            int lobyte = decoded[j + 1];
 
259
            int d = (hibyte * 0x100) | lobyte;
 
260
            buffer.append((char) d);
 
261
          }
 
262
        }
 
263
        encoded = false;
 
264
      }
 
265
      else if (encoded)
 
266
        decoderSink.write((byte) c);
 
267
      else if (buffer != null)
 
268
        buffer.append(c);
 
269
    }
 
270
    if (buffer != null)
 
271
      return buffer.toString();
 
272
    return name;
 
273
  }
 
274
 
 
275
  public static void main(String[]args)
 
276
  {
 
277
    boolean decode = false;
 
278
    for (int i = 0; i < args.length; i++)
 
279
    {
 
280
      if (args[i].equals("-d"))
 
281
        decode = true;
 
282
      else
 
283
      {
 
284
        if (decode)
 
285
          System.out.println(args[i] + " = " + decode(args[i]));
 
286
        else
 
287
          System.out.println(args[i] + " = " + encode(args[i]));
 
288
      }
 
289
    }
 
290
  }
 
291
 
 
292
}