|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| package org.apache.geronimo.mail.util; |
|
19 |
| |
|
20 |
| import java.io.EOFException; |
|
21 |
| import java.io.IOException; |
|
22 |
| import java.io.InputStream; |
|
23 |
| import java.io.OutputStream; |
|
24 |
| import java.io.PrintStream; |
|
25 |
| import java.io.PushbackInputStream; |
|
26 |
| import java.io.UnsupportedEncodingException; |
|
27 |
| |
|
28 |
| public class QuotedPrintableEncoder implements Encoder { |
|
29 |
| |
|
30 |
| static protected final byte[] encodingTable = |
|
31 |
| { |
|
32 |
| (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7', |
|
33 |
| (byte)'8', (byte)'9', (byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F' |
|
34 |
| }; |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| static protected final byte[] decodingTable = new byte[128]; |
|
40 |
| |
|
41 |
| static { |
|
42 |
| |
|
43 |
1
| for (int i = 0; i < encodingTable.length; i++)
|
|
44 |
| { |
|
45 |
16
| decodingTable[encodingTable[i]] = (byte)i;
|
|
46 |
| } |
|
47 |
| } |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| static private final int DEFAULT_CHARS_PER_LINE = 76; |
|
52 |
| |
|
53 |
| |
|
54 |
| protected OutputStream out; |
|
55 |
| |
|
56 |
| protected int bytesWritten = 0; |
|
57 |
| |
|
58 |
| protected int lineCount = 0; |
|
59 |
| |
|
60 |
| protected int lineLength; |
|
61 |
| |
|
62 |
| protected int deferredWhitespace = 0; |
|
63 |
| |
|
64 |
| protected int cachedCharacter = -1; |
|
65 |
| |
|
66 |
| |
|
67 |
| protected boolean lastCR = false; |
|
68 |
| |
|
69 |
| protected boolean lastWhitespace = false; |
|
70 |
| |
|
71 |
19
| public QuotedPrintableEncoder() {
|
|
72 |
19
| this(null, DEFAULT_CHARS_PER_LINE);
|
|
73 |
| } |
|
74 |
| |
|
75 |
0
| public QuotedPrintableEncoder(OutputStream out) {
|
|
76 |
0
| this(out, DEFAULT_CHARS_PER_LINE);
|
|
77 |
| } |
|
78 |
| |
|
79 |
22
| public QuotedPrintableEncoder(OutputStream out, int lineLength) {
|
|
80 |
22
| this.out = out;
|
|
81 |
22
| this.lineLength = lineLength;
|
|
82 |
| } |
|
83 |
| |
|
84 |
765
| private void checkDeferred(int ch) throws IOException {
|
|
85 |
| |
|
86 |
765
| if (lastWhitespace) {
|
|
87 |
| |
|
88 |
0
| if (ch == '\r' || ch == '\n') {
|
|
89 |
0
| writeEncodedCharacter(' ');
|
|
90 |
| } |
|
91 |
| else { |
|
92 |
| |
|
93 |
0
| writeCharacter(' ');
|
|
94 |
| } |
|
95 |
| |
|
96 |
0
| lastWhitespace = false;
|
|
97 |
| } |
|
98 |
| |
|
99 |
765
| else if (lastCR) {
|
|
100 |
| |
|
101 |
0
| if (ch != '\n') {
|
|
102 |
0
| writeEOL();
|
|
103 |
| } |
|
104 |
| |
|
105 |
0
| lastCR = false;
|
|
106 |
| } |
|
107 |
| } |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
3
| public int encode(byte[] data, int off, int length) throws IOException {
|
|
120 |
3
| int endOffset = off + length;
|
|
121 |
| |
|
122 |
3
| while (off < endOffset) {
|
|
123 |
| |
|
124 |
765
| byte ch = data[off++];
|
|
125 |
| |
|
126 |
| |
|
127 |
765
| encode(ch);
|
|
128 |
| } |
|
129 |
| |
|
130 |
3
| return bytesWritten;
|
|
131 |
| } |
|
132 |
| |
|
133 |
| |
|
134 |
765
| public void encode(int ch) throws IOException {
|
|
135 |
| |
|
136 |
765
| ch = ch &0xFF;
|
|
137 |
| |
|
138 |
| |
|
139 |
765
| checkDeferred(ch);
|
|
140 |
| |
|
141 |
765
| switch (ch) {
|
|
142 |
| |
|
143 |
| |
|
144 |
0
| case ' ':
|
|
145 |
| { |
|
146 |
| |
|
147 |
| |
|
148 |
0
| lastWhitespace = true;
|
|
149 |
| |
|
150 |
0
| lastCR = false;
|
|
151 |
0
| break;
|
|
152 |
| } |
|
153 |
| |
|
154 |
| |
|
155 |
0
| case '\r':
|
|
156 |
| { |
|
157 |
| |
|
158 |
0
| lastCR = true;
|
|
159 |
0
| break;
|
|
160 |
| } |
|
161 |
| |
|
162 |
| |
|
163 |
0
| case '\n':
|
|
164 |
| { |
|
165 |
| |
|
166 |
0
| writeEOL();
|
|
167 |
0
| break;
|
|
168 |
| } |
|
169 |
| |
|
170 |
| |
|
171 |
| |
|
172 |
3
| case '=':
|
|
173 |
| { |
|
174 |
3
| writeEncodedCharacter(ch);
|
|
175 |
3
| break;
|
|
176 |
| } |
|
177 |
| |
|
178 |
| |
|
179 |
762
| default:
|
|
180 |
| { |
|
181 |
762
| if (ch < 32 || ch >= 127) {
|
|
182 |
519
| writeEncodedCharacter(ch);
|
|
183 |
| } |
|
184 |
| else { |
|
185 |
243
| writeCharacter(ch);
|
|
186 |
| } |
|
187 |
762
| break;
|
|
188 |
| } |
|
189 |
| } |
|
190 |
| } |
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
| |
|
197 |
| |
|
198 |
| |
|
199 |
| |
|
200 |
| |
|
201 |
| |
|
202 |
0
| public int encode(byte[] data, int off, int length, String specials) throws IOException {
|
|
203 |
0
| int endOffset = off + length;
|
|
204 |
| |
|
205 |
0
| while (off < endOffset) {
|
|
206 |
| |
|
207 |
0
| byte ch = data[off++];
|
|
208 |
| |
|
209 |
| |
|
210 |
0
| encode(ch, specials);
|
|
211 |
| } |
|
212 |
| |
|
213 |
0
| return bytesWritten;
|
|
214 |
| } |
|
215 |
| |
|
216 |
| |
|
217 |
| |
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
| |
|
222 |
| |
|
223 |
| |
|
224 |
| |
|
225 |
| |
|
226 |
18
| public int encode(PushbackInputStream in, StringBuffer out, String specials, int limit) throws IOException {
|
|
227 |
18
| int count = 0;
|
|
228 |
| |
|
229 |
138
| while (count < limit) {
|
|
230 |
138
| int ch = in.read();
|
|
231 |
| |
|
232 |
138
| if (ch == -1) {
|
|
233 |
18
| return count;
|
|
234 |
| } |
|
235 |
| |
|
236 |
120
| ch = ch &0xFF;
|
|
237 |
| |
|
238 |
| |
|
239 |
| |
|
240 |
120
| if (ch == ' ') {
|
|
241 |
| |
|
242 |
17
| out.append('_');
|
|
243 |
17
| count++;
|
|
244 |
| } |
|
245 |
| |
|
246 |
103
| else if (ch < 32 || ch >= 127 || specials.indexOf(ch) != -1) {
|
|
247 |
| |
|
248 |
| |
|
249 |
34
| if (count + 3 > limit) {
|
|
250 |
0
| in.unread(ch);
|
|
251 |
0
| return count;
|
|
252 |
| } |
|
253 |
34
| out.append('=');
|
|
254 |
34
| out.append((char)encodingTable[ch >> 4]);
|
|
255 |
34
| out.append((char)encodingTable[ch & 0x0F]);
|
|
256 |
34
| count += 3;
|
|
257 |
| } |
|
258 |
| else { |
|
259 |
| |
|
260 |
69
| out.append((char)ch);
|
|
261 |
69
| count++;
|
|
262 |
| } |
|
263 |
| } |
|
264 |
0
| return count;
|
|
265 |
| } |
|
266 |
| |
|
267 |
| |
|
268 |
| |
|
269 |
| |
|
270 |
| |
|
271 |
| |
|
272 |
| |
|
273 |
| |
|
274 |
| |
|
275 |
| |
|
276 |
| |
|
277 |
| |
|
278 |
| |
|
279 |
0
| public void encode(int ch, String specials) throws IOException {
|
|
280 |
| |
|
281 |
0
| ch = ch &0xFF;
|
|
282 |
| |
|
283 |
| |
|
284 |
| |
|
285 |
0
| if (ch == ' ') {
|
|
286 |
| |
|
287 |
0
| writeCharacter('_');
|
|
288 |
| } |
|
289 |
| |
|
290 |
0
| else if (ch < 32 || ch >= 127 || specials.indexOf(ch) != -1) {
|
|
291 |
0
| writeEncodedCharacter(ch);
|
|
292 |
| } |
|
293 |
| else { |
|
294 |
| |
|
295 |
0
| writeCharacter(ch);
|
|
296 |
| } |
|
297 |
| } |
|
298 |
| |
|
299 |
| |
|
300 |
| |
|
301 |
| |
|
302 |
| |
|
303 |
| |
|
304 |
| |
|
305 |
| |
|
306 |
| |
|
307 |
| |
|
308 |
| |
|
309 |
| |
|
310 |
0
| public int encode(byte[] data, int off, int length, OutputStream out) throws IOException {
|
|
311 |
| |
|
312 |
0
| this.out = out;
|
|
313 |
0
| bytesWritten = 0;
|
|
314 |
| |
|
315 |
| |
|
316 |
0
| return encode(data, off, length);
|
|
317 |
| } |
|
318 |
| |
|
319 |
| |
|
320 |
| |
|
321 |
| |
|
322 |
| |
|
323 |
| |
|
324 |
| |
|
325 |
| |
|
326 |
| |
|
327 |
| |
|
328 |
| |
|
329 |
| |
|
330 |
| |
|
331 |
0
| public int decode(byte[] data, int off, int length, OutputStream out) throws IOException {
|
|
332 |
| |
|
333 |
0
| this.out = out;
|
|
334 |
| |
|
335 |
0
| int endOffset = off + length;
|
|
336 |
0
| int bytesWritten = 0;
|
|
337 |
| |
|
338 |
0
| while (off < endOffset) {
|
|
339 |
0
| byte ch = data[off++];
|
|
340 |
| |
|
341 |
| |
|
342 |
| |
|
343 |
0
| if (ch == ' ') {
|
|
344 |
0
| int trailingSpaces = 1;
|
|
345 |
| |
|
346 |
0
| while (off < endOffset && data[off] == ' ') {
|
|
347 |
| |
|
348 |
0
| off++;
|
|
349 |
0
| trailingSpaces++;
|
|
350 |
| } |
|
351 |
| |
|
352 |
0
| if (off >= endOffset || data[off] == '\r' || data[off] == '\n') {
|
|
353 |
| |
|
354 |
0
| continue;
|
|
355 |
| } |
|
356 |
| else { |
|
357 |
| |
|
358 |
0
| bytesWritten += trailingSpaces;
|
|
359 |
| |
|
360 |
0
| while (trailingSpaces-- > 0) {
|
|
361 |
0
| out.write(' ');
|
|
362 |
| } |
|
363 |
| } |
|
364 |
| } |
|
365 |
0
| else if (ch == '=') {
|
|
366 |
| |
|
367 |
| |
|
368 |
0
| if (off + 1 >= endOffset) {
|
|
369 |
0
| throw new IOException("Invalid quoted printable encoding");
|
|
370 |
| } |
|
371 |
| |
|
372 |
0
| byte b1 = data[off++];
|
|
373 |
0
| byte b2 = data[off++];
|
|
374 |
| |
|
375 |
| |
|
376 |
0
| if (b1 == '\r') {
|
|
377 |
0
| if (b2 != '\n') {
|
|
378 |
0
| throw new IOException("Invalid quoted printable encoding");
|
|
379 |
| } |
|
380 |
| |
|
381 |
| |
|
382 |
| } |
|
383 |
| else { |
|
384 |
| |
|
385 |
0
| b1 = decodingTable[b1];
|
|
386 |
0
| b2 = decodingTable[b2];
|
|
387 |
0
| out.write((b1 << 4) | b2);
|
|
388 |
| |
|
389 |
0
| bytesWritten++;
|
|
390 |
| } |
|
391 |
| } |
|
392 |
| else { |
|
393 |
| |
|
394 |
0
| out.write(ch);
|
|
395 |
0
| bytesWritten++;
|
|
396 |
| } |
|
397 |
| } |
|
398 |
| |
|
399 |
0
| return bytesWritten;
|
|
400 |
| } |
|
401 |
| |
|
402 |
| |
|
403 |
| |
|
404 |
| |
|
405 |
| |
|
406 |
| |
|
407 |
| |
|
408 |
| |
|
409 |
| |
|
410 |
| |
|
411 |
7
| public int decodeWord(byte[] data, OutputStream out) throws IOException {
|
|
412 |
7
| return decodeWord(data, 0, data.length, out);
|
|
413 |
| } |
|
414 |
| |
|
415 |
| |
|
416 |
| |
|
417 |
| |
|
418 |
| |
|
419 |
| |
|
420 |
| |
|
421 |
| |
|
422 |
| |
|
423 |
| |
|
424 |
| |
|
425 |
| |
|
426 |
| |
|
427 |
7
| public int decodeWord(byte[] data, int off, int length, OutputStream out) throws IOException {
|
|
428 |
| |
|
429 |
7
| this.out = out;
|
|
430 |
| |
|
431 |
7
| int endOffset = off + length;
|
|
432 |
7
| int bytesWritten = 0;
|
|
433 |
| |
|
434 |
7
| while (off < endOffset) {
|
|
435 |
96
| byte ch = data[off++];
|
|
436 |
| |
|
437 |
| |
|
438 |
96
| if (ch == '_') {
|
|
439 |
15
| out.write(' ');
|
|
440 |
| } |
|
441 |
81
| else if (ch == '=') {
|
|
442 |
| |
|
443 |
| |
|
444 |
30
| if (off + 1 >= endOffset) {
|
|
445 |
0
| throw new IOException("Invalid quoted printable encoding");
|
|
446 |
| } |
|
447 |
| |
|
448 |
30
| byte b1 = data[off++];
|
|
449 |
30
| byte b2 = data[off++];
|
|
450 |
| |
|
451 |
| |
|
452 |
30
| if (b1 == '\r') {
|
|
453 |
0
| if (b2 != '\n') {
|
|
454 |
0
| throw new IOException("Invalid quoted printable encoding");
|
|
455 |
| } |
|
456 |
| |
|
457 |
| |
|
458 |
| } |
|
459 |
| else { |
|
460 |
| |
|
461 |
30
| byte c1 = decodingTable[b1];
|
|
462 |
30
| byte c2 = decodingTable[b2];
|
|
463 |
30
| out.write((c1 << 4) | c2);
|
|
464 |
| |
|
465 |
30
| bytesWritten++;
|
|
466 |
| } |
|
467 |
| } |
|
468 |
| else { |
|
469 |
| |
|
470 |
51
| out.write(ch);
|
|
471 |
51
| bytesWritten++;
|
|
472 |
| } |
|
473 |
| } |
|
474 |
| |
|
475 |
7
| return bytesWritten;
|
|
476 |
| } |
|
477 |
| |
|
478 |
| |
|
479 |
| |
|
480 |
| |
|
481 |
| |
|
482 |
| |
|
483 |
| |
|
484 |
| |
|
485 |
| |
|
486 |
| |
|
487 |
| |
|
488 |
0
| public int decode(String data, OutputStream out) throws IOException {
|
|
489 |
0
| try {
|
|
490 |
| |
|
491 |
0
| byte[] bytes = data.getBytes("US-ASCII");
|
|
492 |
0
| return decode(bytes, 0, bytes.length, out);
|
|
493 |
| } catch (UnsupportedEncodingException e) { |
|
494 |
0
| throw new IOException("Invalid UUEncoding");
|
|
495 |
| } |
|
496 |
| } |
|
497 |
| |
|
498 |
765
| private void checkLineLength(int required) throws IOException {
|
|
499 |
| |
|
500 |
765
| if ((lineCount + required) > lineLength ) {
|
|
501 |
23
| out.write('=');
|
|
502 |
23
| out.write('\r');
|
|
503 |
23
| out.write('\n');
|
|
504 |
23
| bytesWritten += 3;
|
|
505 |
23
| lineCount = 0;
|
|
506 |
| } |
|
507 |
| } |
|
508 |
| |
|
509 |
| |
|
510 |
522
| public void writeEncodedCharacter(int ch) throws IOException {
|
|
511 |
| |
|
512 |
522
| checkLineLength(3);
|
|
513 |
522
| out.write('=');
|
|
514 |
522
| out.write(encodingTable[ch >> 4]);
|
|
515 |
522
| out.write(encodingTable[ch & 0x0F]);
|
|
516 |
522
| lineCount += 3;
|
|
517 |
522
| bytesWritten += 3;
|
|
518 |
| } |
|
519 |
| |
|
520 |
| |
|
521 |
243
| public void writeCharacter(int ch) throws IOException {
|
|
522 |
| |
|
523 |
243
| checkLineLength(1);
|
|
524 |
243
| out.write(ch);
|
|
525 |
243
| lineCount++;
|
|
526 |
243
| bytesWritten++;
|
|
527 |
| } |
|
528 |
| |
|
529 |
| |
|
530 |
0
| public void writeEOL() throws IOException {
|
|
531 |
0
| out.write('\r');
|
|
532 |
0
| out.write('\n');
|
|
533 |
0
| lineCount = 0;
|
|
534 |
0
| bytesWritten += 3;
|
|
535 |
| } |
|
536 |
| |
|
537 |
| |
|
538 |
788
| public int decode(InputStream in) throws IOException {
|
|
539 |
| |
|
540 |
| |
|
541 |
| |
|
542 |
788
| if (deferredWhitespace > 0) {
|
|
543 |
0
| deferredWhitespace--;
|
|
544 |
0
| return ' ';
|
|
545 |
| } |
|
546 |
| |
|
547 |
| |
|
548 |
| |
|
549 |
788
| if (cachedCharacter != -1) {
|
|
550 |
0
| int result = cachedCharacter;
|
|
551 |
0
| cachedCharacter = -1;
|
|
552 |
0
| return result;
|
|
553 |
| } |
|
554 |
| |
|
555 |
788
| int ch = in.read();
|
|
556 |
| |
|
557 |
| |
|
558 |
788
| if (ch == -1) {
|
|
559 |
0
| return -1;
|
|
560 |
| } |
|
561 |
| |
|
562 |
| |
|
563 |
| |
|
564 |
788
| if (ch == ' ') {
|
|
565 |
| |
|
566 |
0
| while ((ch = in.read()) == ' ') {
|
|
567 |
0
| deferredWhitespace++;
|
|
568 |
| } |
|
569 |
| |
|
570 |
| |
|
571 |
0
| if (ch == -1 || ch == '\r' || ch == '\n') {
|
|
572 |
| |
|
573 |
0
| deferredWhitespace = 0;
|
|
574 |
| |
|
575 |
0
| return ch;
|
|
576 |
| } |
|
577 |
| else { |
|
578 |
| |
|
579 |
0
| cachedCharacter = ch;
|
|
580 |
| |
|
581 |
0
| return ' ';
|
|
582 |
| } |
|
583 |
| } |
|
584 |
788
| else if (ch == '=') {
|
|
585 |
545
| int b1 = in.read();
|
|
586 |
| |
|
587 |
545
| if (b1 == -1) {
|
|
588 |
0
| throw new IOException("Truncated quoted printable data");
|
|
589 |
| } |
|
590 |
545
| int b2 = in.read();
|
|
591 |
| |
|
592 |
545
| if (b2 == -1) {
|
|
593 |
0
| throw new IOException("Truncated quoted printable data");
|
|
594 |
| } |
|
595 |
| |
|
596 |
| |
|
597 |
545
| if (b1 == '\r') {
|
|
598 |
23
| if (b2 != '\n') {
|
|
599 |
0
| throw new IOException("Invalid quoted printable encoding");
|
|
600 |
| } |
|
601 |
| |
|
602 |
| |
|
603 |
23
| return decode(in);
|
|
604 |
| } |
|
605 |
| else { |
|
606 |
| |
|
607 |
522
| b1 = decodingTable[b1];
|
|
608 |
522
| b2 = decodingTable[b2];
|
|
609 |
522
| return (b1 << 4) | b2;
|
|
610 |
| } |
|
611 |
| } |
|
612 |
| else { |
|
613 |
243
| return ch;
|
|
614 |
| } |
|
615 |
| } |
|
616 |
| |
|
617 |
| |
|
618 |
| |
|
619 |
| |
|
620 |
| |
|
621 |
| |
|
622 |
| |
|
623 |
| |
|
624 |
| |
|
625 |
| |
|
626 |
| |
|
627 |
| |
|
628 |
| |
|
629 |
| |
|
630 |
9
| public void encodeWord(InputStream in, String charset, String specials, OutputStream out, boolean fold) throws IOException
|
|
631 |
| { |
|
632 |
| |
|
633 |
| |
|
634 |
9
| PushbackInputStream inStream = new PushbackInputStream(in);
|
|
635 |
9
| PrintStream writer = new PrintStream(out);
|
|
636 |
| |
|
637 |
| |
|
638 |
9
| int limit = 76 - 7 - charset.length();
|
|
639 |
9
| boolean firstLine = true;
|
|
640 |
9
| StringBuffer encodedString = new StringBuffer(76);
|
|
641 |
| |
|
642 |
9
| while (true) {
|
|
643 |
| |
|
644 |
| |
|
645 |
18
| encode(inStream, encodedString, specials, limit);
|
|
646 |
| |
|
647 |
18
| if (encodedString.length() == 0) {
|
|
648 |
9
| break;
|
|
649 |
| } |
|
650 |
| |
|
651 |
| |
|
652 |
9
| if (!firstLine) {
|
|
653 |
0
| if (fold) {
|
|
654 |
0
| writer.print("\r\n");
|
|
655 |
| } |
|
656 |
| else { |
|
657 |
0
| writer.print(" ");
|
|
658 |
| } |
|
659 |
| } |
|
660 |
| |
|
661 |
| |
|
662 |
9
| writer.print("=?");
|
|
663 |
9
| writer.print(charset);
|
|
664 |
9
| writer.print("?Q?");
|
|
665 |
| |
|
666 |
9
| writer.print(encodedString.toString());
|
|
667 |
| |
|
668 |
9
| writer.print("?=");
|
|
669 |
9
| writer.flush();
|
|
670 |
| |
|
671 |
| |
|
672 |
9
| encodedString.setLength(0);
|
|
673 |
| } |
|
674 |
| } |
|
675 |
| } |
|
676 |
| |
|
677 |
| |
|
678 |
| |