Debian Codegraph @

faxmail/MIMEState.c++

raw
1 /* $Id$ */
2 /*
3 * Copyright (c) 1990-1996 Sam Leffler
4 * Copyright (c) 1991-1996 Silicon Graphics, Inc.
5 * HylaFAX is a trademark of Silicon Graphics
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
14 *
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 */
26 /*
27 * MIME Decoder Support.
28 */
29 #include "MIMEState.h"
30 #include "MsgFmt.h"
31 #include "StackBuffer.h"
32
33 #include <string.h>
34 #include <ctype.h>
35
36 MIMEState::MIMEState(const char* t, const char* st) : type(t), subtype(st)
37 {
38 parent = NULL;
39 lastPart = false;
40 encode = ENC_7BIT;
41 charset = CS_USASCII;
42 blen = (u_int) -1; // NB: should insure no matches
43 lineno = 1;
44 }
45 MIMEState::MIMEState(MIMEState& other)
46 : parent(&other)
47 , boundary(other.boundary)
48 {
49 if (other.type == "multipart" && other.subtype == "digest")
50 type = "message", subtype = "rfc822";
51 else
52 type = "text", subtype = "plain";
53 lastPart = false;
54 encode = other.encode;
55 charset = other.charset;
56 blen = other.blen;
57 lineno = other.lineno;
58 }
59 MIMEState::MIMEState(MIMEState& other, const char* t, const char* st)
60 : parent(&other)
61 , type(t)
62 , subtype(st)
63 , boundary(other.boundary)
64 {
65 lastPart = false;
66 encode = other.encode;
67 charset = other.charset;
68 blen = other.blen;
69 lineno = other.lineno;
70 }
71 MIMEState::~MIMEState()
72 {
73 if (parent) {
74 parent->lineno = lineno;
75 }
76 }
77
78 void
79 MIMEState::trace(FILE* fd)
80 {
81 static const char* enames[] = {
82 "7bit",
83 "quoted-printable",
84 "base64",
85 "8bit",
86 "binary",
87 "x-uuencode"
88 };
89 static const char* cnames[] = {
90 "us-ascii",
91 "iso-8859-1",
92 "iso-8859-2",
93 "iso-8859-3",
94 "iso-8859-4",
95 "iso-8859-5",
96 "iso-8859-6",
97 "iso-8859-7",
98 "iso-8859-8",
99 "iso-8859-9",
100 };
101 fprintf(fd, "MIME part (line %u): %s/%s charset=%s encoding=%s\n"
102 , lineno
103 , (const char*) type
104 , (const char*) subtype
105 , cnames[charset]
106 , enames[encode]
107 );
108 }
109
110 bool
111 MIMEState::parseToken(const char*& cp, const char delimeter, fxStr& result)
112 {
113 while (*cp && isspace(*cp))
114 cp++;
115 const char* bp = cp;
116 while (*cp && !isspace(*cp) && *cp != delimeter)
117 cp++;
118 if (cp - bp > 0) {
119 result = fxStr(bp, cp-bp);
120 while (isspace(*cp)) // remove trailing ws
121 cp++;
122 return (true);
123 } else
124 return (false);
125 }
126
127 /*
128 * Extract MIME-related information from a set of
129 * message headers and fillin the MIME state block.
130 */
131 bool
132 MIMEState::parse(const MsgFmt& msg, fxStr& emsg)
133 {
134 const fxStr* s = msg.findHeader("Content-Type");
135 if (s) { // type/subtype [; parameter]*
136 const char* cp = &(*s)[0];
137 if (parseToken(cp, '/', type)
138 && *cp++ == '/'
139 && parseToken(cp, ';', subtype)) {
140 if (*cp == ';') // parse optional parameters
141 parseParameters(cp+1);
142 type.lowercase();
143 subtype.lowercase();
144 } else {
145 emsg = _("Syntax error parsing MIME Content-Type: ") | *s;
146 type = "text"; // reset on parsing error
147 return (false);
148 }
149 }
150 s = msg.findHeader("Content-Transfer-Encoding");
151 if (s)
152 setEncoding(&(*s)[0]);
153 s = msg.findHeader("Content-Description");
154 if (s)
155 desc = *s;
156 s = msg.findHeader("Content-ID");
157 if (s)
158 cid = *s;
159 return (true);
160 }
161
162 /*
163 * Parse optional MIME parameters used by the decoder.
164 */
165 void
166 MIMEState::parseParameters(const char* cp)
167 {
168 for (;;) {
169 while (*cp && isspace(*cp))
170 cp++;
171 const char* bp = cp;
172 while (*cp && !isspace(*cp) && *cp != '=')
173 cp++;
174 if (*cp == '\0') // end of string
175 break;
176 fxStr param(bp, cp-bp);
177 if (*cp != '=') { // param<ws>=
178 while (isspace(*cp++))
179 ;
180 if (*cp != '=') // syntax error
181 break;
182 }
183 for (cp++; isspace(*cp); cp++) // skip ws after =
184 ;
185 const char* ev;
186 if (*cp == '"') { // quoted value
187 for (ev = ++cp; *ev && *ev != '"'; ev++)
188 ;
189 if (*ev != '"') // missing close quote
190 break;
191 } else {
192 for (ev = cp; *ev && !isspace(*ev) && *ev != ';'; ev++)
193 ;
194 }
195
196 (void) setParameter(param, fxStr(cp, ev-cp));
197
198 if (*ev != '\0' && *ev != ';') { // scan to trailing ;
199 do {
200 ev++;
201 } while (*ev && *ev != ';');
202 }
203 if (*ev != ';')
204 break;
205 cp = ++ev;
206 }
207 }
208
209 /*
210 * Set a MIME parameter used by the decoder.
211 */
212 bool
213 MIMEState::setParameter(const fxStr& p, const fxStr& value)
214 {
215 fxStr param(p);
216 param.lowercase();
217 if (param.length() == 7 && param == "charset") {
218 setCharset(value); // character set
219 } else if (param.length() == 8 && param == "boundary") {
220 setBoundary(value); // part boundary marker
221 } else
222 return (false);
223 return (true);
224 }
225
226 /*
227 * Set the content-transfer-encoding.
228 */
229 void
230 MIMEState::setEncoding(const char* cp)
231 {
232 if (strcasecmp(cp, "7bit") == 0)
233 encode = ENC_7BIT;
234 else if (strcasecmp(cp, "quoted-printable") == 0)
235 encode = ENC_QPRINT;
236 else if (strcasecmp(cp, "base64") == 0)
237 encode = ENC_BASE64;
238 else if (strcasecmp(cp, "8bit") == 0)
239 encode = ENC_8BIT;
240 else if (strcasecmp(cp, "binary") == 0)
241 encode = ENC_BINARY;
242 else if (strcasecmp(cp, "x-uuencode") == 0)
243 encode = ENC_UUENCODE;
244 }
245
246 /*
247 * Set the body part boundary.
248 */
249 void
250 MIMEState::setBoundary(const char* s)
251 {
252 boundary = s;
253 boundary.insert("--"); // NB: insert "--" to simplify parsing
254 boundary.append("--"); // NB: append "--" to simplify parsing
255 blen = boundary.length()-2; // optimized for checking
256 }
257
258 /*
259 * Set the text character set.
260 */
261 void
262 MIMEState::setCharset(const char* cp)
263 {
264 if (strcasecmp(cp, "us-ascii") == 0)
265 charset = CS_USASCII;
266 else if (strcasecmp(cp, "iso-8859-1") == 0)
267 charset = CS_ISO8859_1;
268 else if (strcasecmp(cp, "iso-8859-2") == 0)
269 charset = CS_ISO8859_2;
270 else if (strcasecmp(cp, "iso-8859-3") == 0)
271 charset = CS_ISO8859_3;
272 else if (strcasecmp(cp, "iso-8859-4") == 0)
273 charset = CS_ISO8859_4;
274 else if (strcasecmp(cp, "iso-8859-5") == 0)
275 charset = CS_ISO8859_5;
276 else if (strcasecmp(cp, "iso-8859-6") == 0)
277 charset = CS_ISO8859_6;
278 else if (strcasecmp(cp, "iso-8859-7") == 0)
279 charset = CS_ISO8859_7;
280 else if (strcasecmp(cp, "iso-8859-8") == 0)
281 charset = CS_ISO8859_8;
282 else if (strcasecmp(cp, "iso-8859-9") == 0)
283 charset = CS_ISO8859_9;
284 }
285
286 extern void fxFatal(const char* fmt ...);
287
288 /*
289 * Return a line of data according to the current
290 * setting of the content-transfer-encoding and
291 * the body part boundary. If the boundary marker
292 * or EOF is reached false is returned, otherwise
293 * this method returns true and the decoded line.
294 * If the data is encoded with a text-oriented
295 * scheme (7bit, 8bit, binary, or quoted-printable)
296 * then the trailing newline character is returned
297 * in the buffer. Otherwise any trailing newline
298 * is discarded and only the decoded data is returned.
299 */
300 bool
301 MIMEState::getLine(FILE* fd, fxStackBuffer& buf)
302 {
303 buf.reset();
304 switch (encode) {
305 case ENC_7BIT:
306 for (;;) {
307 int c = getc(fd);
308 if (c == EOF) {
309 return (buf.getLength() > 0);
310 }
311 c &= 0xff;
312 if (c == '\n') { // check for boundary marker
313 lineno++;
314 u_int cc = buf.getLength();
315 if (cc >= blen && buf[0] == '-') {
316 if (cc == blen && strneq(buf, boundary, blen)) {
317 return (false);
318 }
319 if (cc == blen+2 && strneq(buf, boundary, blen+2)) {
320 lastPart = true;
321 return (false);
322 }
323 }
324 buf.put('\n');
325 return (true);
326 }
327 buf.put(c);
328 }
329 /*NOTREACHED*/
330 case ENC_8BIT:
331 case ENC_BINARY:
332 for (;;) {
333 int c = getc(fd);
334 if (c == EOF) {
335 return (buf.getLength() > 0);
336 }
337 c &= 0xff;
338 if (c == '\n') { // check for boundary marker
339 lineno++;
340 u_int cc = buf.getLength();
341 if (cc >= blen && buf[0] == '-') {
342 if (cc == blen && strneq(buf, boundary, blen)) {
343 return (false);
344 }
345 if (cc == blen+2 && strneq(buf, boundary, blen+2)) {
346 lastPart = true;
347 return (false);
348 }
349 }
350 buf.put('\n');
351 return (true);
352 }
353 buf.put(c);
354 }
355 /*NOTREACHED*/
356 case ENC_QPRINT: return getQuotedPrintableLine(fd, buf);
357 case ENC_BASE64: return getBase64Line(fd, buf);
358 case ENC_UUENCODE: return getUUDecodeLine(fd, buf);
359 }
360 fxFatal(_("Internal error, unsupported Content-Transfer-Encoding %u"), encode);
361 /*NOTREACHED*/
362 return (false);
363 }
364
365 static int
366 hex(char c)
367 {
368 // NB: don't check if c is in set [0-9a-fA-F]
369 return (isdigit(c) ? c-'0' : isupper(c) ? 10+c-'A' : 10+c-'a');
370 }
371
372 static void
373 copyQP(fxStackBuffer& buf, const char line[], u_int cc)
374 {
375 // copy to buf & convert =XX escapes
376 for (u_int i = 0; i < cc; i++) {
377 if (line[i] == '=' && cc-i >= 2) {
378 int v1 = hex(line[++i]);
379 int v2 = hex(line[++i]);
380 buf.put((v1<<4) + v2);
381 } else
382 buf.put(line[i]);
383 }
384 }
385
386 /*
387 * Return a decoded line of quoted-printable text.
388 */
389 bool
390 MIMEState::getQuotedPrintableLine(FILE* fd, fxStackBuffer& buf)
391 {
392 char line[80]; // spec says never more than 76
393 u_int cc = 0; // chars in current line
394 for (;;) {
395 int c = getc(fd);
396 if (c == EOF) {
397 copyQP(buf, line, cc);
398 return (buf.getLength() > 0);
399 }
400 c &= 0xff;
401 if (c == '\n') { // check for boundary marker
402 lineno++;
403 if (cc > 0 && line[cc-1] == '=') { // soft line break
404 copyQP (buf, line, cc-1); // everything up to ``="''
405 cc = 0;
406 continue;
407 }
408 if (cc >= blen && line[0] == '-') {
409 if (cc == blen && strneq(line, boundary, blen))
410 return (false);
411 if (cc == blen+2 && strneq(line, boundary, blen+2)) {
412 lastPart = true;
413 return (false);
414 }
415 }
416 copyQP(buf, line, cc);
417 buf.put('\n');
418 return (true);
419 }
420 if (cc < sizeof (line)-1)
421 line[cc++] = c;
422 }
423 /*NOTREACHED*/
424 }
425
426 static void
427 copyBase64(fxStackBuffer& buf, const char line[], u_int cc)
428 {
429 static const int base64[128] = {
430 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
431 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
432 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,
433 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,
434 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,
435 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,
436 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
437 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1
438 };
439 u_int i = 0;
440 while (i < cc) {
441 int c1;
442 do {
443 c1 = base64[(u_int)line[i++]];
444 } while (c1 == -1 && i < cc);
445 if (c1 != -1) {
446 int c2;
447 do {
448 c2 = base64[(u_int)line[i++]];
449 } while (c2 == -1 && i < cc);
450 if (c2 != -1) {
451 buf.put((c1<<2) | ((c2&0x30)>>4));
452 int c3;
453 do {
454 c3 = base64[(u_int)line[i++]];
455 } while (c3 == -1 && i < cc);
456 if (c3 != -1) {
457 buf.put(((c2&0x0f)<<4) | ((c3&0x3c)>>2));
458 int c4;
459 do {
460 c4 = base64[(u_int)line[i++]];
461 } while (c4 == -1 && i < cc);
462 if (c4 != -1)
463 buf.put((c3&0x3)<<6 | c4);
464 }
465 }
466 }
467 }
468 }
469
470 /*
471 * Return a decoded line of base64 data.
472 */
473 bool
474 MIMEState::getBase64Line(FILE* fd, fxStackBuffer& buf)
475 {
476 char line[80]; // spec says never more than 76
477 u_int cc = 0; // chars in current line
478 for (;;) {
479 int c = getc(fd);
480 if (c == EOF) {
481 copyBase64(buf, line, cc);
482 return (buf.getLength() > 0);
483 }
484 c &= 0x7f;
485 if (c == '\n') { // check for boundary marker
486 lineno++;
487 if (cc >= blen && line[0] == '-') {
488 if (cc == blen && strneq(line, boundary, blen))
489 return (false);
490 if (cc == blen+2 && strneq(line, boundary, blen+2)) {
491 lastPart = true;
492 return (false);
493 }
494 }
495 copyBase64(buf, line, cc);
496 return (true);
497 }
498 if (cc < sizeof (line)-1)
499 line[cc++] = c;
500 }
501 /*NOTREACHED*/
502 }
503
504 inline int DEC(char c) { return ((c - ' ') & 077); }
505
506 static void
507 copyUUDecode(fxStackBuffer& buf, const char line[], u_int)
508 {
509 const char* cp = line;
510 int n = DEC(*cp);
511 if (n > 0) {
512 // XXX check n against passed in byte count for line
513 int c;
514 for (cp++; n >= 3; cp += 4, n -= 3) {
515 c = (DEC(cp[0])<<2) | (DEC(cp[1])>>4); buf.put(c);
516 c = (DEC(cp[1])<<4) | (DEC(cp[2])>>2); buf.put(c);
517 c = (DEC(cp[2])<<6) | DEC(cp[3]); buf.put(c);
518 }
519 if (n >= 1)
520 c = (DEC(cp[0])<<2) | (DEC(cp[1])>>4), buf.put(c);
521 if (n >= 2)
522 c = (DEC(cp[1])<<4) | (DEC(cp[2])>>2), buf.put(c);
523 if (n >= 3)
524 c = (DEC(cp[2])<<6) | DEC(cp[3]), buf.put(c);
525 }
526 }
527
528 /*
529 * Return a decoded line of uuencode'd data.
530 */
531 bool
532 MIMEState::getUUDecodeLine(FILE* fd, fxStackBuffer& buf)
533 {
534 char line[80]; // spec says never more than 62
535 u_int cc = 0; // chars in current line
536 for (;;) {
537 int c = getc(fd);
538 if (c == EOF) {
539 copyUUDecode(buf, line, cc);
540 return (buf.getLength() > 0);
541 }
542 c &= 0x7f;
543 if (c == '\n') {
544 lineno++;
545 if (cc >= blen && line[0] == '-') { // check for boundary marker
546 if (cc == blen && strneq(line, boundary, blen))
547 return (false);
548 if (cc == blen+2 && strneq(line, boundary, blen+2)) {
549 lastPart = true;
550 return (false);
551 }
552 } else if (cc >= 6 && strneq(line, "begin ", 6)) {
553 return (getUUDecodeLine(fd, buf));
554 } else if (cc == 3 && streq(line, "end")) {
555 // consume to boundary marker
556 while (getUUDecodeLine(fd, buf))
557 ;
558 return (false);
559 }
560 copyUUDecode(buf, line, cc);
561 return (true);
562 }
563 if (cc < sizeof (line)-1)
564 line[cc++] = c;
565 }
566 /*NOTREACHED*/
567 }