0
|
1 /* ml_exthdr.c - supporting RFC-2047 (Non-ASCII Mail Headers) */
|
|
2 /* by takada@seraph.ntt.jp */
|
|
3 /* arranged by MH-plus project */
|
|
4
|
|
5 #ifdef MIME_HEADERS
|
|
6
|
|
7 #include "../h/mh.h"
|
|
8 #include <stdio.h>
|
|
9 #include <ctype.h>
|
|
10
|
|
11 #define MAXWIDTH 75
|
|
12
|
|
13 static char b64_to_alpha[] =
|
|
14 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
15
|
|
16 static unsigned char alpha_to_b64[256] = {
|
|
17 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
18 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
19 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,62, 0, 0, 0,63,
|
|
20 52,53,54,55, 56,57,58,59, 60,61, 0, 0, 0, 0, 0, 0,
|
|
21 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
|
|
22 15,16,17,18, 19,20,21,22, 23,24,25, 0, 0, 0, 0, 0,
|
|
23 0,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
|
|
24 41,42,43,44, 45,46,47,48, 49,50,51, 0, 0, 0, 0, 0,
|
|
25 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
26 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
27 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
28 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
29 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
30 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
31 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
32 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
33 };
|
|
34
|
|
35 static unsigned char alpha_to_qpr[256] = {
|
|
36 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
37 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
38 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
39 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0,
|
|
40 0,10,11,12, 13,14,15, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
41 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
42 0,10,11,12, 13,14,15, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
43 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
44 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
45 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
46 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
47 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
48 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
49 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
50 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
51 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
52 };
|
|
53
|
|
54 static char *text_headers[] = {
|
|
55 /* unstructured headers */
|
|
56 "Subject",
|
|
57 "Comments",
|
|
58 "Content-Description",
|
|
59 NULL
|
|
60 };
|
|
61
|
|
62 static int structured = 1;
|
|
63 static int nameoutput = 1;
|
|
64 static char *mm_charset = NULL;
|
|
65 #ifdef JAPAN
|
|
66 extern int japan_environ;
|
|
67 #endif /* JAPAN */
|
|
68
|
|
69 static int ml_to_mmh();
|
|
70 static int mmh_to_ml();
|
|
71 static int bin_to_b64();
|
|
72 static int bin_to_qpr();
|
|
73 static int b64_to_bin();
|
|
74 static int qpr_to_bin();
|
|
75
|
|
76 /*
|
|
77 * Encode multilingual string into RFC-2047 format
|
|
78 */
|
|
79 char *
|
|
80 exthdr_encode(input, output, width, component)
|
|
81 char *input, *output, *component;
|
|
82 int width;
|
|
83 {
|
|
84 char **p;
|
|
85
|
|
86 structured = 1; /* almost headers are structured. */
|
|
87 if (uprf(component, "X-"))
|
|
88 /* user defined headers are assumed to be unstructured. */
|
|
89 structured = 0;
|
|
90 else {
|
|
91 for (p = text_headers; *p; p++)
|
|
92 if (uleq(component, *p)) {
|
|
93 structured = 0;
|
|
94 break;
|
|
95 }
|
|
96 }
|
|
97 nameoutput = strlen(component);
|
|
98 if (nameoutput < 1)
|
|
99 nameoutput = 1;
|
|
100 if (nameoutput > 36)
|
|
101 nameoutput = 36; /* avoid endless loop */
|
|
102 (void) ml_to_mmh(input, output, width);
|
|
103 return(output);
|
|
104 }
|
|
105
|
|
106 #define ASCII 0
|
|
107 #define JISX0208 1
|
|
108
|
|
109 #define DSGNT_ASCII() \
|
|
110 {\
|
|
111 if (kanji_pos != ASCII) {\
|
|
112 *bp++ = '\033'; *bp++ = '('; *bp++ = 'B';\
|
|
113 kanji_pos = ASCII;\
|
|
114 }\
|
|
115 }
|
|
116
|
|
117 #define DSGNT_JISX0208() \
|
|
118 {\
|
|
119 if (kanji_pos != JISX0208) {\
|
|
120 *bp++ = '\033'; *bp++ = '$'; *bp++ = 'B';\
|
|
121 kanji_pos = JISX0208;\
|
|
122 }\
|
|
123 }
|
|
124
|
|
125 #define isspecials(c) \
|
|
126 ((c) == '(' || (c) == ')' || (c) == '<' || (c) == '>' || (c) == '@' \
|
|
127 || (c) == ',' || (c) == ';' || (c) == ':' || (c) == '\\' || (c) == '"' \
|
|
128 || (c) == '.' || (c) == '[' || (c) == ']')
|
|
129
|
|
130
|
|
131 #ifdef JAPAN
|
|
132 #define PUT_ENCODED_WORD()\
|
|
133 {\
|
|
134 int x;\
|
|
135 *bp = '\0';\
|
|
136 if (japan_environ) {\
|
|
137 op = copy("=?ISO-2022-JP?B?", op); width += 16;\
|
|
138 x = bin_to_b64(buf, op); op += x; width += x;\
|
|
139 } else {\
|
|
140 op = copy("=?", op); width += 2;\
|
|
141 op = copy(mm_charset, op); width += strlen(mm_charset);\
|
|
142 op = copy("?Q?", op); width += 3;\
|
|
143 x = bin_to_qpr(buf, op); op += x; width += x;\
|
|
144 }\
|
|
145 op = copy("?=", op); width += 2;\
|
|
146 mmcontain = 1;\
|
|
147 bp = buf;\
|
|
148 }
|
|
149 #else /* JAPAN */
|
|
150 #define PUT_ENCODED_WORD()\
|
|
151 {\
|
|
152 int x;\
|
|
153 *bp = '\0';\
|
|
154 op = copy("=?", op); width += 2;\
|
|
155 op = copy(mm_charset, op); width += strlen(mm_charset);\
|
|
156 op = copy("?Q?", op); width += 3;\
|
|
157 x = bin_to_qpr(buf, op); op += x; width += x;\
|
|
158 op = copy("?=", op); width += 2;\
|
|
159 mmcontain = 1;\
|
|
160 bp = buf;\
|
|
161 }
|
|
162 #endif /* JAPAN */
|
|
163
|
|
164 #define qpr_len(c) \
|
|
165 ((c == ' ' || isalnum(c) \
|
|
166 || c == '!' || c == '*' || c == '+' || c == '-' || c == '/' \
|
|
167 || (!structured && !isspace(c) && !iscntrl(c) && !((c) & 0x80) \
|
|
168 && c != '=' && c != '?' && c != '_')) \
|
|
169 ? 1 : 3)
|
|
170
|
|
171 static
|
|
172 int encoded_length(buf, bp, kanji_pos, p)
|
|
173 char *buf, *bp, *p;
|
|
174 int kanji_pos;
|
|
175 {
|
|
176 int len;
|
|
177 char *cp, c;
|
|
178
|
|
179 #ifdef JAPAN
|
|
180 if (japan_environ) {
|
|
181 len = bp - buf;
|
|
182 if (p) {
|
|
183 if (ml_ismlptr(p)) {
|
|
184 if (kanji_pos != JISX0208)
|
|
185 len += 3+2+3;
|
|
186 else
|
|
187 len += 2+3;
|
|
188 } else
|
|
189 len++;
|
|
190 }
|
|
191 return 16 /* strlen("=?ISO-2022-JP?B?") */ + ((len+2)/3)*4 + 2;
|
|
192 }
|
|
193 #endif /* JAPAN */
|
|
194
|
|
195 len = 2 + strlen(mm_charset) + 3 + 2;
|
|
196 cp = buf;
|
|
197 while (cp < bp) {
|
|
198 c = *cp++;
|
|
199 len += qpr_len(c);
|
|
200 }
|
|
201 if (p) {
|
|
202 c = *p;
|
|
203 len += qpr_len(c);
|
|
204 }
|
|
205 return len;
|
|
206 }
|
|
207
|
|
208
|
|
209 static
|
|
210 int parse_specials(encodep, ctextp, dpp, epp, ppp)
|
|
211 int *encodep, *ctextp;
|
|
212 char **dpp, **epp, **ppp;
|
|
213 {
|
|
214 char *pp = *ppp, *xp;
|
|
215 unsigned char c, pair = '\0';
|
|
216
|
|
217 if (*pp == '"') {
|
|
218 /* encoded-word never appear within a quoted-string */
|
|
219 while ((c = *++pp)) {
|
|
220 if (c == '"') {
|
|
221 pp++;
|
|
222 break;
|
|
223 }
|
|
224 if (c == '\\' && *++pp)
|
|
225 pp++;
|
|
226 #ifdef JAPAN
|
|
227 else if (japan_environ) {
|
|
228 if (ml_ismlptr(pp)) {
|
|
229 pp++;
|
|
230 *encodep = 1;
|
|
231 }
|
|
232 }
|
|
233 #endif /* JAPAN */
|
|
234 else if (*pp & 0x80)
|
|
235 *encodep = 1;
|
|
236 }
|
|
237 *ppp = pp;
|
|
238 return 0;
|
|
239 }
|
|
240
|
|
241 if (*pp == '[') {
|
|
242 /* encoded-word never appear within a domain-literal */
|
|
243 while ((c = *++pp)) {
|
|
244 if (c == ']') {
|
|
245 pp++;
|
|
246 break;
|
|
247 }
|
|
248 if (c == '\\' && *++pp)
|
|
249 pp++;
|
|
250 #ifdef JAPAN
|
|
251 else if (japan_environ) {
|
|
252 if (ml_ismlptr(pp)) {
|
|
253 pp++;
|
|
254 *encodep = 1;
|
|
255 }
|
|
256 }
|
|
257 #endif /* JAPAN */
|
|
258 else if (*pp & 0x80)
|
|
259 *encodep = 1;
|
|
260 }
|
|
261 *ppp = pp;
|
|
262 return 0;
|
|
263 }
|
|
264
|
|
265 if (*encodep)
|
|
266 *epp = *dpp = pp;
|
|
267 if (((c = *pp) == '<' || c == '(') && *epp < pp)
|
|
268 return 1;
|
|
269
|
|
270 switch (c) {
|
|
271 case '(':
|
|
272 while (*pp == '(') {
|
|
273 pp++;
|
|
274 (*ctextp)++;
|
|
275 }
|
|
276 xp = pp;
|
|
277 while ((c = *pp) && !isspace(c) && c != '(' && c != ')') {
|
|
278 #ifdef JAPAN
|
|
279 if (japan_environ ? ml_ismlptr(pp) : (*pp & 0x80)) {
|
|
280 #else /* JAPAN */
|
|
281 if (*pp & 0x80) {
|
|
282 #endif /* JAPAN */
|
|
283 pp = xp;
|
|
284 break;
|
|
285 }
|
|
286 pp++;
|
|
287 if (c == '\\' && *pp)
|
|
288 pp++;
|
|
289 }
|
|
290 if (c != ')') {
|
|
291 *ppp = pp;
|
|
292 return 1;
|
|
293 }
|
|
294 /* else fall */
|
|
295
|
|
296 case ')':
|
|
297 do {
|
|
298 if (*ctextp > 0)
|
|
299 (*ctextp)--;
|
|
300 } while (*++pp == ')');
|
|
301 *ppp = pp;
|
|
302 if (*ctextp || (*pp != ',' && *pp != '>' && *pp != ':' && *pp != ';'))
|
|
303 return 1;
|
|
304 *encodep = 0;
|
|
305 pp--; /* tricky */
|
|
306 /* fall */
|
|
307
|
|
308 case '.':
|
|
309 if (*encodep) {
|
|
310 (*ppp)++;
|
|
311 return 1;
|
|
312 }
|
|
313 /* else fall */
|
|
314
|
|
315 case '<':
|
|
316 case '@':
|
|
317 xp = pp;
|
|
318 while ((c = *++pp)) {
|
|
319 #ifdef JAPAN
|
|
320 if (japan_environ ? ml_ismlptr(pp) : (*pp & 0x80)) {
|
|
321 #else /* JAPAN */
|
|
322 if (*pp & 0x80) {
|
|
323 #endif /* JAPAN */
|
|
324 pp = xp + 1;
|
|
325 break;
|
|
326 }
|
|
327 if (c == '\\' && *++pp)
|
|
328 pp++;
|
|
329 else if (c == pair)
|
|
330 pair = '\0';
|
|
331 else if (!pair) {
|
|
332 if (c == '"')
|
|
333 pair = '"';
|
|
334 else if (c == '[')
|
|
335 pair = ']';
|
|
336 else if (c == '.' || c == '@'
|
|
337 || (**epp == '<' && (c == ',' || c == ':')))
|
|
338 xp = pp;
|
|
339 else if (isspecials(c)) {
|
|
340 break;
|
|
341 }
|
|
342 }
|
|
343 }
|
|
344 if (*pp == '>') {
|
|
345 /* fall */
|
|
346 case '>':
|
|
347 case ':':
|
|
348 case ']':
|
|
349 pp++;
|
|
350 }
|
|
351 if (*pp == ';') {
|
|
352 /* fall */
|
|
353 case ';':
|
|
354 pp++;
|
|
355 }
|
|
356 if (*pp == ',') {
|
|
357 /* fall */
|
|
358 case ',':
|
|
359 pp++;
|
|
360 }
|
|
361 *ppp = pp;
|
|
362 return 1;
|
|
363 }
|
|
364 /* NOT REACHABLE */
|
|
365 return 0;
|
|
366 }
|
|
367
|
|
368 static int
|
|
369 ml_to_mmh(ibuf, obuf, width)
|
|
370 char *ibuf, *obuf;
|
|
371 int width;
|
|
372 {
|
|
373 int kanji_pos, encode, mmcontain, ctext, extra;
|
|
374 char *ip = ibuf;
|
|
375 char *op = obuf;
|
|
376 char buf[BUFSIZ], *bp = buf;
|
|
377 char *cp, *dp, *ep, *pp, *saveip;
|
|
378 unsigned char c1, c2;
|
|
379
|
|
380 #ifdef JAPAN
|
|
381 if (japan_environ)
|
|
382 mm_charset = "ISO-2022-JP";
|
|
383 else
|
|
384 #endif /* JAPAN */
|
|
385 if (!(mm_charset = getenv("MM_CHARSET")))
|
|
386 mm_charset = m_find("MM-Charset");
|
|
387 if (!mm_charset || !*mm_charset)
|
|
388 mm_charset = "UNKNOWN-8BIT";
|
|
389 kanji_pos = ASCII;
|
|
390 mmcontain = ctext = 0;
|
|
391 while (*ip) {
|
|
392 pp = ip;
|
|
393 while (isspace(*pp & 0xff))
|
|
394 pp++;
|
|
395 cp = dp = pp;
|
|
396 for (;;) {
|
|
397 ep = pp;
|
|
398 encode = 0;
|
|
399 if (structured) {
|
|
400 while ((c1 = *pp) && !isspace(c1)) {
|
|
401 if (c1 == '\\') {
|
|
402 if (*++pp)
|
|
403 pp++;
|
|
404 } else if (ctext
|
|
405 ? (c1 == '(' || c1 == ')') : isspecials(c1)) {
|
|
406 if (parse_specials(&encode, &ctext, &dp, &ep, &pp))
|
|
407 goto start_encode;
|
|
408 }
|
|
409 #ifdef JAPAN
|
|
410 else if (japan_environ) {
|
|
411 if (ml_ismlptr(pp)) {
|
|
412 pp++;
|
|
413 encode = 1;
|
|
414 }
|
|
415 }
|
|
416 #endif /* JAPAN */
|
|
417 else if (c1 & 0x80)
|
|
418 encode = 1;
|
|
419 pp++;
|
|
420 }
|
|
421 } else { /* unstructured */
|
|
422 while ((c1 = *pp) && !isspace(c1)) {
|
|
423 #ifdef JAPAN
|
|
424 if (japan_environ) {
|
|
425 if (ml_ismlptr(pp)) {
|
|
426 pp++;
|
|
427 encode = 1;
|
|
428 }
|
|
429 } else
|
|
430 #endif /* JAPAN */
|
|
431 if (c1 & 0x80)
|
|
432 encode = 1;
|
|
433 pp++;
|
|
434 }
|
|
435 }
|
|
436 if (!encode)
|
|
437 break;
|
|
438 dp = pp;
|
|
439 while (isspace(*pp & 0xff))
|
|
440 pp++;
|
|
441 }
|
|
442 /*
|
|
443 * ip -- cp : white spaces
|
|
444 * cp -- dp : to encode (some blocks)
|
|
445 * dp -- ep : white spaces
|
|
446 * ep -- pp : not to encode (one block)
|
|
447 */
|
|
448 start_encode:
|
|
449 if (cp == dp)
|
|
450 cp = dp = ip; /* make cp < dp whenever ip < cp */
|
|
451
|
|
452 /* lines containing encoded-word have MAXWIDTH limit (cf. RFC-2047) */
|
|
453 if (mmcontain && width + (cp - ip) > MAXWIDTH) {
|
|
454 ip = cp;
|
|
455 sprintf(op, "\n%*s", width = nameoutput, "");
|
|
456 op += nameoutput + 1;
|
|
457 mmcontain = 0;
|
|
458 } else {
|
|
459 char *xp = op;
|
|
460 while (ip < cp) {
|
|
461 if ((*op++ = *ip++) == '\n')
|
|
462 mmcontain = width = 0;
|
|
463 else
|
|
464 width++;
|
|
465 }
|
|
466 if (cp < dp) {
|
|
467 if (xp == op && op > obuf && *(op-1) != '(') {
|
|
468 *op++ = ' '; width++;
|
|
469 }
|
|
470 if (width
|
|
471 + encoded_length(buf, bp, kanji_pos, cp) > MAXWIDTH) {
|
|
472 sprintf(xp, "\n%*s", width = nameoutput, "");
|
|
473 op = xp + nameoutput + 1;
|
|
474 mmcontain = 0;
|
|
475 }
|
|
476 }
|
|
477 }
|
|
478
|
|
479 saveip = ip;
|
|
480 extra = 0;
|
|
481 redo_encode:
|
|
482 while (ip < dp) {
|
|
483 if (*ip == '\n') {
|
|
484 ip++;
|
|
485 } else
|
|
486 #ifdef JAPAN
|
|
487 if (japan_environ && (*ip & 0x80)) {
|
|
488 if (ml_ismlptr(ip)) {
|
|
489 if (width + encoded_length(buf, bp, kanji_pos, ip)
|
|
490 + extra > MAXWIDTH) {
|
|
491 DSGNT_ASCII();
|
|
492 if (bp > buf)
|
|
493 PUT_ENCODED_WORD();
|
|
494 sprintf(op, "\n%*s", width = nameoutput, "");
|
|
495 op += nameoutput + 1;
|
|
496 saveip = ip;
|
|
497 }
|
|
498 DSGNT_JISX0208();
|
|
499 *bp++ = *ip++ & 0x7f;
|
|
500 *bp++ = *ip++ & 0x7f;
|
|
501 } else {
|
|
502 ip++; /* skip */
|
|
503 }
|
|
504 } else
|
|
505 #endif /* JAPAN */
|
|
506 {
|
|
507 DSGNT_ASCII();
|
|
508 if (width + encoded_length(buf, bp, kanji_pos, ip)
|
|
509 + extra > MAXWIDTH) {
|
|
510 if (bp > buf)
|
|
511 PUT_ENCODED_WORD();
|
|
512 sprintf(op, "\n%*s", width = nameoutput, "");
|
|
513 op += nameoutput + 1;
|
|
514 saveip = ip;
|
|
515 }
|
|
516 *bp++ = *ip++;
|
|
517 }
|
|
518 }
|
|
519 DSGNT_ASCII();
|
|
520 if (bp > buf) {
|
|
521 if (dp == ep && !extra
|
|
522 && (*ep == ')' || *ep == ',' || *ep == ':'
|
|
523 || *ep == '>' || *ep == ';') /* kinsoku :-) */
|
|
524 && width + encoded_length(buf, bp, kanji_pos, NULL)
|
|
525 + (extra = (*ep != ')' ? 1 : 0) + (pp - ip)) > MAXWIDTH) {
|
|
526 ip = saveip;
|
|
527 bp = buf;
|
|
528 kanji_pos = ASCII;
|
|
529 goto redo_encode; /* redo with new "extra" */
|
|
530 }
|
|
531 PUT_ENCODED_WORD();
|
|
532 }
|
|
533
|
|
534 if (mmcontain && width + (ep - ip) > MAXWIDTH) {
|
|
535 ip = ep;
|
|
536 if (ep < pp) {
|
|
537 sprintf(op, "\n%*s", width = nameoutput, "");
|
|
538 op += nameoutput + 1;
|
|
539 mmcontain = 0;
|
|
540 } else if (dp < ep)
|
|
541 *op++ = *(ep - 1); /* end of string */
|
|
542 } else {
|
|
543 char *xp = op;
|
|
544 while (ip < ep) {
|
|
545 if ((*op++ = *ip++) == '\n')
|
|
546 mmcontain = width = 0;
|
|
547 else
|
|
548 width++;
|
|
549 }
|
|
550 if (ep < pp) {
|
|
551 if (xp == op && cp < dp && *ep != ')') {
|
|
552 *op++ = ' '; width++;
|
|
553 }
|
|
554 if ((mmcontain && width + (pp - ip) > MAXWIDTH) ||
|
|
555 ((*ep == '(' || *ep == '<') /* kinsoku :-) */
|
|
556 && *ep == *(pp-1) && *pp && !isspace(*pp & 0xff)
|
|
557 && width + (pp - ip) + (*(pp-1) != '(' ? 1 : 0)
|
|
558 + encoded_length(buf, bp, kanji_pos, pp) > MAXWIDTH)) {
|
|
559 sprintf(xp, "\n%*s", width = nameoutput, "");
|
|
560 op = xp + nameoutput + 1;
|
|
561 mmcontain = 0;
|
|
562 }
|
|
563 }
|
|
564 }
|
|
565
|
|
566 while (ip < pp) {
|
|
567 if ((*op = *ip++) & 0x80)
|
|
568 continue; /* skip */
|
|
569 op++; width++;
|
|
570 }
|
|
571 }
|
|
572 *op = '\0';
|
|
573 return (op - obuf);
|
|
574 }
|
|
575
|
|
576 static int
|
|
577 bin_to_b64(in, out)
|
|
578 char *in, *out;
|
|
579 {
|
|
580 char *oldout = out;
|
|
581 char c1, c2, c3;
|
|
582
|
|
583 while (c1 = *in++) {
|
|
584 *out++ = b64_to_alpha[c1 >> 2];
|
|
585 c2 = *in++;
|
|
586 *out++ = b64_to_alpha[((c1 & 0x03) << 4) | ((c2 & 0xf0) >> 4)];
|
|
587 if (c2) {
|
|
588 c3 = *in++;
|
|
589 *out++ = b64_to_alpha[((c2 & 0x0f) << 2) | ((c3 & 0xc0) >> 6)];
|
|
590 if (c3) {
|
|
591 *out++ = b64_to_alpha[c3 & 0x3f];
|
|
592 } else {
|
|
593 *out++ = '=';
|
|
594 break;
|
|
595 }
|
|
596 } else {
|
|
597 *out++ = '=';
|
|
598 *out++ = '=';
|
|
599 break;
|
|
600 }
|
|
601 }
|
|
602 *out = '\0';
|
|
603 return(out - oldout);
|
|
604 }
|
|
605
|
|
606 static int
|
|
607 bin_to_qpr(in, out)
|
|
608 char *in, *out;
|
|
609 {
|
|
610 char *oldout = out;
|
|
611 unsigned char c1;
|
|
612
|
|
613 while (c1 = *in++) {
|
|
614 if (c1 == ' ')
|
|
615 *out++ = '_';
|
|
616 else if (isalnum(c1) || c1 == '!' || c1 == '*' || c1 == '+'
|
|
617 || c1 == '-' || c1 == '/')
|
|
618 *out++ = c1;
|
|
619 else if (!structured && !isspace(c1) && !iscntrl(c1) && !((c1) & 0x80)
|
|
620 && c1 != '=' && c1 != '?' && c1 != '_')
|
|
621 *out++ = c1;
|
|
622 else {
|
|
623 sprintf(out, "=%02X", c1);
|
|
624 out += 3;
|
|
625 }
|
|
626 }
|
|
627 *out = '\0';
|
|
628 return(out - oldout);
|
|
629 }
|
|
630
|
|
631
|
|
632 /*
|
|
633 * Decode multilingual string from RFC-2047 format
|
|
634 */
|
|
635 char *
|
|
636 exthdr_decode(input, output)
|
|
637 char *input, *output;
|
|
638 {
|
|
639 char *cp = input;
|
|
640 char *op = output;
|
|
641 char *first, *last;
|
|
642 int in, out;
|
|
643
|
|
644 last = cp;
|
|
645 first = op;
|
|
646 while (*cp) {
|
|
647 char *tmp = cp;
|
|
648 if (uprf(cp, "=?") &&
|
|
649
|
|
650 /* NOTE: The first "?=" of "=?ISO-2022-JP?Q?=1B$B ... ?="
|
|
651 is not the end of encoded-word. */
|
|
652 (tmp = index(tmp+2, '?')) &&
|
|
653 (tmp = index(tmp+1, '?')) &&
|
|
654 ((in = stringdex("?=", tmp + 1) + tmp + 1 - cp) >= 0) &&
|
|
655 ((out = mmh_to_ml(cp, cp+in+1, op)) >= 0)) {
|
|
656 cp += in + 2; op += out;
|
|
657 last = cp;
|
|
658 if (!*cp) {
|
|
659 break;
|
|
660 } else if (isspace(*cp & 0xff) && *(cp+1))
|
|
661 while (*++cp && isspace(*cp & 0xff)) ;
|
|
662 } else {
|
|
663 #ifdef JAPAN
|
|
664 if (first < op) {
|
|
665 *op = '\0';
|
|
666 (void) ml_conv(first);
|
|
667 op = first + strlen(first);
|
|
668 }
|
|
669 #endif /* JAPAN */
|
|
670 if (*last == '\n' && last+1 < cp) {
|
|
671 *op++ = ' ';
|
|
672 last = cp;
|
|
673 }
|
|
674 while (last < cp)
|
|
675 *op++ = *last++;
|
|
676 *op++ = *cp++;
|
|
677 last = cp;
|
|
678 first = op;
|
|
679 }
|
|
680 }
|
|
681 #ifdef JAPAN
|
|
682 if (first < op) {
|
|
683 *op = '\0';
|
|
684 (void) ml_conv(first);
|
|
685 op = first + strlen(first);
|
|
686 }
|
|
687 #endif /* JAPAN */
|
|
688 while (last < cp)
|
|
689 *op++ = *last++;
|
|
690 *op = '\0';
|
|
691 return(output);
|
|
692 }
|
|
693
|
|
694 static int
|
|
695 mmh_to_ml(bp, ep, op)
|
|
696 char *bp, *ep, *op;
|
|
697 {
|
|
698 char *ip, *cp, *tmp, buf[BUFSIZ], encode;
|
|
699 int result;
|
|
700
|
|
701 #ifdef JAPAN
|
|
702 if (japan_environ)
|
|
703 mm_charset = "iso-2022-jp";
|
|
704 else
|
|
705 #endif /* JAPAN */
|
|
706 mm_charset = getenv("MM_CHARSET");
|
|
707 ip = bp;
|
|
708 if ((ip > ep) || strncmp(ip, "=?", 2)) return(-1);
|
|
709 ip += 2;
|
|
710 if ((ip > ep) || ((cp = index(ip, '?')) == NULL)) return(-1);
|
|
711 if (cp - ip >= BUFSIZ) return(-1);
|
|
712 strncpy(buf, ip, cp-ip); buf[cp-ip] = 0;
|
|
713 if ((tmp = index(buf, '*')) != NULL) {
|
|
714 /* =?ISO-2022-JP*ja-JP?B?......?= is valid. (cf. RFC-2231) */
|
|
715 *tmp++ = '\0';
|
|
716 for (;;) {
|
|
717 int i;
|
|
718 if (!isalpha(*tmp)) return(-1);
|
|
719 for (i = 1; i < 8 && isalpha(*(tmp+i)); i++) ;
|
|
720 tmp += i;
|
|
721 if (*tmp == '\0') break;
|
|
722 if (*tmp++ != '-') return(-1);
|
|
723 }
|
|
724 }
|
|
725 if (!uleq(buf, "us-ascii") && (!mm_charset || !uleq(buf, mm_charset)))
|
|
726 return(-1);
|
|
727 ip = cp + 1;
|
|
728 if (ip > ep) return(-1);
|
|
729 if ((*ip == 'B') || (*ip == 'b'))
|
|
730 encode = 'B';
|
|
731 else if ((*ip == 'Q') || (*ip == 'q'))
|
|
732 encode = 'Q';
|
|
733 else
|
|
734 return(-1);
|
|
735 ip++;
|
|
736 if ((ip > ep) || (*ip != '?')) return(-1);
|
|
737 ip++;
|
|
738 if ((ip > ep) || ((cp = index(ip, '?')) == NULL)) return(-1);
|
|
739 if (cp - ip >= BUFSIZ) return(-1);
|
|
740 strncpy(buf, ip, cp-ip); buf[cp-ip] = 0;
|
|
741 ip = cp + 1;
|
|
742 if ((ip != ep) || (*ip != '=')) return(-1);
|
|
743 if (encode == 'B') {
|
|
744 (void) b64_to_bin(buf, op);
|
|
745 } else {
|
|
746 (void) qpr_to_bin(buf, op);
|
|
747 }
|
|
748 return(strlen(op));
|
|
749 }
|
|
750
|
|
751 static int
|
|
752 b64_to_bin(in, out)
|
|
753 char *in, *out;
|
|
754 {
|
|
755 char *oldout = out;
|
|
756 unsigned char c1, c2, c3, c4;
|
|
757
|
|
758 while (c1 = *in++) {
|
|
759 if ((c2 = *in++) && (c3 = *in++) && (c4 = *in++)) {
|
|
760 if ((c1 == '=') || (c2 == '=')) {
|
|
761 break;
|
|
762 } else {
|
|
763 c1 = alpha_to_b64[c1];
|
|
764 c2 = alpha_to_b64[c2];
|
|
765 }
|
|
766 *out++ = (c1 << 2) | ((c2 & 0x30) >> 4);
|
|
767 if (c3 == '=') {
|
|
768 break;
|
|
769 } else {
|
|
770 c3 = alpha_to_b64[c3];
|
|
771 }
|
|
772 *out++ = ((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2);
|
|
773 if (c4 == '=') {
|
|
774 break;
|
|
775 } else {
|
|
776 c4 = alpha_to_b64[c4];
|
|
777 }
|
|
778 *out++ = ((c3 & 0x03) << 6) | c4 ;
|
|
779 } else {
|
|
780 break;
|
|
781 }
|
|
782 }
|
|
783 *out = '\0';
|
|
784 return(out - oldout);
|
|
785 }
|
|
786
|
|
787 static int
|
|
788 qpr_to_bin(in, out)
|
|
789 char *in, *out;
|
|
790 {
|
|
791 char *oldout = out;
|
|
792 unsigned char c1, c2, c3;
|
|
793
|
|
794 while (c1 = *in++) {
|
|
795 if ((c1 == '=') && (c2 = *in++) && (c3 = *in++)) {
|
|
796 c2 = alpha_to_qpr[c2];
|
|
797 c3 = alpha_to_qpr[c3];
|
|
798 *out++ = (c2 << 4) | c3 ;
|
|
799 } else if (c1 == '_') {
|
|
800 *out++ = ' ';
|
|
801 } else {
|
|
802 *out++ = c1;
|
|
803 }
|
|
804 }
|
|
805 *out = '\0';
|
|
806 return(out - oldout);
|
|
807 }
|
|
808
|
|
809 #endif /* MIME_HEADERS */
|