diff options
Diffstat (limited to 'src/mime.c')
-rw-r--r-- | src/mime.c | 50 |
1 files changed, 19 insertions, 31 deletions
@@ -14,14 +14,9 @@ | |||
14 | /*=========================================================================*\ | 14 | /*=========================================================================*\ |
15 | * Don't want to trust escape character constants | 15 | * Don't want to trust escape character constants |
16 | \*=========================================================================*/ | 16 | \*=========================================================================*/ |
17 | #define CR 0x0D | ||
18 | #define LF 0x0A | ||
19 | #define HT 0x09 | ||
20 | #define SP 0x20 | ||
21 | |||
22 | typedef unsigned char UC; | 17 | typedef unsigned char UC; |
23 | static const char CRLF[] = {CR, LF, 0}; | 18 | static const char CRLF[] = "\r\n"; |
24 | static const char EQCRLF[] = {'=', CR, LF, 0}; | 19 | static const char EQCRLF[] = "=\r\n"; |
25 | 20 | ||
26 | /*=========================================================================*\ | 21 | /*=========================================================================*\ |
27 | * Internal function prototypes. | 22 | * Internal function prototypes. |
@@ -121,9 +116,9 @@ static int mime_global_wrp(lua_State *L) | |||
121 | luaL_buffinit(L, &buffer); | 116 | luaL_buffinit(L, &buffer); |
122 | while (input < last) { | 117 | while (input < last) { |
123 | switch (*input) { | 118 | switch (*input) { |
124 | case CR: | 119 | case '\r': |
125 | break; | 120 | break; |
126 | case LF: | 121 | case '\n': |
127 | luaL_addstring(&buffer, CRLF); | 122 | luaL_addstring(&buffer, CRLF); |
128 | left = length; | 123 | left = length; |
129 | break; | 124 | break; |
@@ -327,11 +322,10 @@ static int mime_global_unb64(lua_State *L) | |||
327 | * all (except CRLF in text) can be =XX | 322 | * all (except CRLF in text) can be =XX |
328 | * CLRL in not text must be =XX=XX | 323 | * CLRL in not text must be =XX=XX |
329 | * 33 through 60 inclusive can be plain | 324 | * 33 through 60 inclusive can be plain |
330 | * 62 through 120 inclusive can be plain | 325 | * 62 through 126 inclusive can be plain |
331 | * 9 and 32 can be plain, unless in the end of a line, where must be =XX | 326 | * 9 and 32 can be plain, unless in the end of a line, where must be =XX |
332 | * encoded lines must be no longer than 76 not counting CRLF | 327 | * encoded lines must be no longer than 76 not counting CRLF |
333 | * soft line-break are =CRLF | 328 | * soft line-break are =CRLF |
334 | * !"#$@[\]^`{|}~ should be =XX for EBCDIC compatibility | ||
335 | * To encode one byte, we need to see the next two. | 329 | * To encode one byte, we need to see the next two. |
336 | * Worst case is when we see a space, and wonder if a CRLF is comming | 330 | * Worst case is when we see a space, and wonder if a CRLF is comming |
337 | \*-------------------------------------------------------------------------*/ | 331 | \*-------------------------------------------------------------------------*/ |
@@ -344,16 +338,10 @@ static void qpsetup(UC *qpclass, UC *qpunbase) | |||
344 | int i; | 338 | int i; |
345 | for (i = 0; i < 256; i++) qpclass[i] = QP_QUOTED; | 339 | for (i = 0; i < 256; i++) qpclass[i] = QP_QUOTED; |
346 | for (i = 33; i <= 60; i++) qpclass[i] = QP_PLAIN; | 340 | for (i = 33; i <= 60; i++) qpclass[i] = QP_PLAIN; |
347 | for (i = 62; i <= 120; i++) qpclass[i] = QP_PLAIN; | 341 | for (i = 62; i <= 126; i++) qpclass[i] = QP_PLAIN; |
348 | qpclass[HT] = QP_IF_LAST; qpclass[SP] = QP_IF_LAST; | 342 | qpclass['\t'] = QP_IF_LAST; |
349 | qpclass['!'] = QP_QUOTED; qpclass['"'] = QP_QUOTED; | 343 | qpclass[' '] = QP_IF_LAST; |
350 | qpclass['#'] = QP_QUOTED; qpclass['$'] = QP_QUOTED; | 344 | qpclass['\r'] = QP_CR; |
351 | qpclass['@'] = QP_QUOTED; qpclass['['] = QP_QUOTED; | ||
352 | qpclass['\\'] = QP_QUOTED; qpclass[']'] = QP_QUOTED; | ||
353 | qpclass['^'] = QP_QUOTED; qpclass['`'] = QP_QUOTED; | ||
354 | qpclass['{'] = QP_QUOTED; qpclass['|'] = QP_QUOTED; | ||
355 | qpclass['}'] = QP_QUOTED; qpclass['~'] = QP_QUOTED; | ||
356 | qpclass['}'] = QP_QUOTED; qpclass[CR] = QP_CR; | ||
357 | for (i = 0; i < 256; i++) qpunbase[i] = 255; | 345 | for (i = 0; i < 256; i++) qpunbase[i] = 255; |
358 | qpunbase['0'] = 0; qpunbase['1'] = 1; qpunbase['2'] = 2; | 346 | qpunbase['0'] = 0; qpunbase['1'] = 1; qpunbase['2'] = 2; |
359 | qpunbase['3'] = 3; qpunbase['4'] = 4; qpunbase['5'] = 5; | 347 | qpunbase['3'] = 3; qpunbase['4'] = 4; qpunbase['5'] = 5; |
@@ -377,7 +365,7 @@ static void qpquote(UC c, luaL_Buffer *buffer) | |||
377 | 365 | ||
378 | /*-------------------------------------------------------------------------*\ | 366 | /*-------------------------------------------------------------------------*\ |
379 | * Accumulate characters until we are sure about how to deal with them. | 367 | * Accumulate characters until we are sure about how to deal with them. |
380 | * Once we are sure, output the to the buffer, in the correct form. | 368 | * Once we are sure, output to the buffer, in the correct form. |
381 | \*-------------------------------------------------------------------------*/ | 369 | \*-------------------------------------------------------------------------*/ |
382 | static size_t qpencode(UC c, UC *input, size_t size, | 370 | static size_t qpencode(UC c, UC *input, size_t size, |
383 | const char *marker, luaL_Buffer *buffer) | 371 | const char *marker, luaL_Buffer *buffer) |
@@ -389,7 +377,7 @@ static size_t qpencode(UC c, UC *input, size_t size, | |||
389 | /* might be the CR of a CRLF sequence */ | 377 | /* might be the CR of a CRLF sequence */ |
390 | case QP_CR: | 378 | case QP_CR: |
391 | if (size < 2) return size; | 379 | if (size < 2) return size; |
392 | if (input[1] == LF) { | 380 | if (input[1] == '\n') { |
393 | luaL_addstring(buffer, marker); | 381 | luaL_addstring(buffer, marker); |
394 | return 0; | 382 | return 0; |
395 | } else qpquote(input[0], buffer); | 383 | } else qpquote(input[0], buffer); |
@@ -398,7 +386,7 @@ static size_t qpencode(UC c, UC *input, size_t size, | |||
398 | case QP_IF_LAST: | 386 | case QP_IF_LAST: |
399 | if (size < 3) return size; | 387 | if (size < 3) return size; |
400 | /* if it is the last, quote it and we are done */ | 388 | /* if it is the last, quote it and we are done */ |
401 | if (input[1] == CR && input[2] == LF) { | 389 | if (input[1] == '\r' && input[2] == '\n') { |
402 | qpquote(input[0], buffer); | 390 | qpquote(input[0], buffer); |
403 | luaL_addstring(buffer, marker); | 391 | luaL_addstring(buffer, marker); |
404 | return 0; | 392 | return 0; |
@@ -492,19 +480,19 @@ static size_t qpdecode(UC c, UC *input, size_t size, | |||
492 | case '=': | 480 | case '=': |
493 | if (size < 3) return size; | 481 | if (size < 3) return size; |
494 | /* eliminate soft line break */ | 482 | /* eliminate soft line break */ |
495 | if (input[1] == CR && input[2] == LF) return 0; | 483 | if (input[1] == '\r' && input[2] == '\n') return 0; |
496 | /* decode quoted representation */ | 484 | /* decode quoted representation */ |
497 | c = qpunbase[input[1]]; d = qpunbase[input[2]]; | 485 | c = qpunbase[input[1]]; d = qpunbase[input[2]]; |
498 | /* if it is an invalid, do not decode */ | 486 | /* if it is an invalid, do not decode */ |
499 | if (c > 15 || d > 15) luaL_addlstring(buffer, (char *)input, 3); | 487 | if (c > 15 || d > 15) luaL_addlstring(buffer, (char *)input, 3); |
500 | else luaL_putchar(buffer, (c << 4) + d); | 488 | else luaL_putchar(buffer, (c << 4) + d); |
501 | return 0; | 489 | return 0; |
502 | case CR: | 490 | case '\r': |
503 | if (size < 2) return size; | 491 | if (size < 2) return size; |
504 | if (input[1] == LF) luaL_addlstring(buffer, (char *)input, 2); | 492 | if (input[1] == '\n') luaL_addlstring(buffer, (char *)input, 2); |
505 | return 0; | 493 | return 0; |
506 | default: | 494 | default: |
507 | if (input[0] == HT || (input[0] > 31 && input[0] < 127)) | 495 | if (input[0] == '\t' || (input[0] > 31 && input[0] < 127)) |
508 | luaL_putchar(buffer, input[0]); | 496 | luaL_putchar(buffer, input[0]); |
509 | return 0; | 497 | return 0; |
510 | } | 498 | } |
@@ -582,9 +570,9 @@ static int mime_global_qpwrp(lua_State *L) | |||
582 | luaL_buffinit(L, &buffer); | 570 | luaL_buffinit(L, &buffer); |
583 | while (input < last) { | 571 | while (input < last) { |
584 | switch (*input) { | 572 | switch (*input) { |
585 | case CR: | 573 | case '\r': |
586 | break; | 574 | break; |
587 | case LF: | 575 | case '\n': |
588 | left = length; | 576 | left = length; |
589 | luaL_addstring(&buffer, CRLF); | 577 | luaL_addstring(&buffer, CRLF); |
590 | break; | 578 | break; |
@@ -623,7 +611,7 @@ static int mime_global_qpwrp(lua_State *L) | |||
623 | * c is the current character being processed | 611 | * c is the current character being processed |
624 | * last is the previous character | 612 | * last is the previous character |
625 | \*-------------------------------------------------------------------------*/ | 613 | \*-------------------------------------------------------------------------*/ |
626 | #define eolcandidate(c) (c == CR || c == LF) | 614 | #define eolcandidate(c) (c == '\r' || c == '\n') |
627 | static int eolprocess(int c, int last, const char *marker, | 615 | static int eolprocess(int c, int last, const char *marker, |
628 | luaL_Buffer *buffer) | 616 | luaL_Buffer *buffer) |
629 | { | 617 | { |