fixed quoted-printable encoder
Showing
2 changed files
with
45 additions
and
8 deletions
1 | 2002-11-28 Frederic Gobry <frederic.gobry@smartdata.ch> | ||
2 | |||
3 | * mailbox/filter_trans.c (qp_encode): fixed quoted-printable | ||
4 | encoding. | ||
5 | |||
1 | 2002-11-27 Frederic Gobry <frederic.gobry@smartdata.ch> | 6 | 2002-11-27 Frederic Gobry <frederic.gobry@smartdata.ch> |
2 | 7 | ||
3 | * mailbox/filter_trans.c (trans_read): applied patch from Sergey | 8 | * mailbox/filter_trans.c (trans_read): applied patch from Sergey | ... | ... |
... | @@ -456,30 +456,55 @@ qp_encode (const char *iptr, size_t isize, char *optr, size_t osize, | ... | @@ -456,30 +456,55 @@ qp_encode (const char *iptr, size_t isize, char *optr, size_t osize, |
456 | int c; | 456 | int c; |
457 | size_t consumed = 0; | 457 | size_t consumed = 0; |
458 | 458 | ||
459 | (void)osize; | ||
460 | *nbytes = 0; | 459 | *nbytes = 0; |
461 | while (consumed < isize && (*nbytes + 4) < isize) | 460 | |
461 | /* Strategy: check if we have enough room in the output buffer only | ||
462 | once the required size has been computed. If there is not enough, | ||
463 | return and hope that the caller will free up the output buffer a | ||
464 | bit. */ | ||
465 | |||
466 | while (consumed < isize) | ||
462 | { | 467 | { |
463 | if (*line_len == QP_LINE_MAX) | 468 | if (*line_len == QP_LINE_MAX) |
464 | { | 469 | { |
470 | /* to cut a qp line requires two bytes */ | ||
471 | if ((*nbytes) + 2 > osize) | ||
472 | return consumed; | ||
473 | |||
465 | *optr++ = '='; | 474 | *optr++ = '='; |
466 | *optr++ = '\n'; | 475 | *optr++ = '\n'; |
467 | (*nbytes) += 2; | 476 | (*nbytes) += 2; |
468 | *line_len = 0; | 477 | *line_len = 0; |
469 | } | 478 | } |
470 | 479 | ||
471 | c = *iptr++; | 480 | /* candidate byte to convert */ |
472 | consumed++; | 481 | c = *iptr; |
473 | if (((c >= 32) && (c <= 60)) || ((c >= 62) && (c <= 126)) || (c == 9)) | 482 | |
483 | if (((c >= 32) && (c <= 60)) || | ||
484 | ((c >= 62) && (c <= 126)) || | ||
485 | (c == 9)) | ||
474 | { | 486 | { |
487 | /* a non-quoted character uses up one byte */ | ||
488 | if (*nbytes + 1 > osize) | ||
489 | return consumed; | ||
490 | |||
475 | *optr++ = c; | 491 | *optr++ = c; |
476 | (*nbytes)++; | 492 | (*nbytes)++; |
477 | (*line_len)++; | 493 | (*line_len)++; |
494 | |||
495 | iptr++; | ||
496 | consumed++; | ||
478 | } | 497 | } |
479 | else | 498 | else |
480 | { | 499 | { |
500 | /* can we store the quoted character in the remaining of the | ||
501 | line ? */ | ||
481 | if (*line_len >= (QP_LINE_MAX - 3)) | 502 | if (*line_len >= (QP_LINE_MAX - 3)) |
482 | { | 503 | { |
504 | /* check if we have enough room to store the padding */ | ||
505 | if (*nbytes + *line_len - QP_LINE_MAX + 3 > osize) | ||
506 | return consumed; | ||
507 | |||
483 | /* add spaces. */ | 508 | /* add spaces. */ |
484 | while (*line_len < QP_LINE_MAX) | 509 | while (*line_len < QP_LINE_MAX) |
485 | { | 510 | { |
... | @@ -487,16 +512,23 @@ qp_encode (const char *iptr, size_t isize, char *optr, size_t osize, | ... | @@ -487,16 +512,23 @@ qp_encode (const char *iptr, size_t isize, char *optr, size_t osize, |
487 | (*nbytes)++; | 512 | (*nbytes)++; |
488 | (*line_len)++; | 513 | (*line_len)++; |
489 | } | 514 | } |
490 | consumed--; | ||
491 | iptr--; | ||
492 | } | 515 | } |
493 | else | 516 | else |
494 | { | 517 | { |
518 | /* a quoted character uses up three bytes */ | ||
519 | if ((*nbytes) + 3 > osize) | ||
520 | return consumed; | ||
521 | |||
495 | *optr++ = '='; | 522 | *optr++ = '='; |
523 | *optr++ = _hexdigits[(c >> 4) & 0xf]; | ||
496 | *optr++ = _hexdigits[c & 0xf]; | 524 | *optr++ = _hexdigits[c & 0xf]; |
497 | *optr++ = _hexdigits[(c/16) & 0xf]; | 525 | |
498 | (*nbytes) += 3; | 526 | (*nbytes) += 3; |
499 | (*line_len) += 3; | 527 | (*line_len) += 3; |
528 | |||
529 | /* we've actuall used up one byte of input */ | ||
530 | iptr ++; | ||
531 | consumed ++; | ||
500 | } | 532 | } |
501 | } | 533 | } |
502 | } | 534 | } | ... | ... |
-
Please register or sign in to post a comment