Commit 0a1c0c33 0a1c0c336d1e4884d9c2440595b49923a113cd05 by Sergey Poznyakoff

(base64_encode): Split into two functions

(B_encode): New function for RFC 2047 encoding
1 parent d83990fc
...@@ -450,10 +450,10 @@ base64_decode (const char *iptr, size_t isize, char *optr, size_t osize, ...@@ -450,10 +450,10 @@ base64_decode (const char *iptr, size_t isize, char *optr, size_t osize,
450 return consumed; 450 return consumed;
451 } 451 }
452 452
453 #define BASE64_LINE_MAX 77
454 static int 453 static int
455 base64_encode (const char *iptr, size_t isize, char *optr, size_t osize, 454 base64_encode_internal (const char *iptr, size_t isize,
456 size_t *nbytes, int *line_len) 455 char *optr, size_t osize,
456 size_t *nbytes, int *line_len, int line_max)
457 { 457 {
458 size_t consumed = 0; 458 size_t consumed = 0;
459 int pad = 0; 459 int pad = 0;
...@@ -466,7 +466,7 @@ base64_encode (const char *iptr, size_t isize, char *optr, size_t osize, ...@@ -466,7 +466,7 @@ base64_encode (const char *iptr, size_t isize, char *optr, size_t osize,
466 pad = 1; 466 pad = 1;
467 while (((consumed + 3) <= isize && (*nbytes + 4) <= osize) || pad) 467 while (((consumed + 3) <= isize && (*nbytes + 4) <= osize) || pad)
468 { 468 {
469 if (*line_len == 76) 469 if (line_max && *line_len == line_max)
470 { 470 {
471 *optr++ = '\n'; 471 *optr++ = '\n';
472 (*nbytes)++; 472 (*nbytes)++;
...@@ -488,6 +488,15 @@ base64_encode (const char *iptr, size_t isize, char *optr, size_t osize, ...@@ -488,6 +488,15 @@ base64_encode (const char *iptr, size_t isize, char *optr, size_t osize,
488 } 488 }
489 489
490 static int 490 static int
491 base64_encode (const char *iptr, size_t isize,
492 char *optr, size_t osize,
493 size_t *nbytes, int *line_len)
494 {
495 return base64_encode_internal (iptr, isize, optr, osize,
496 nbytes, line_len, 76);
497 }
498
499 static int
491 base64_init (mu_filter_t filter) 500 base64_init (mu_filter_t filter)
492 { 501 {
493 struct _trans_stream *ts; 502 struct _trans_stream *ts;
...@@ -519,10 +528,42 @@ static struct _mu_filter_record _base64_filter = ...@@ -519,10 +528,42 @@ static struct _mu_filter_record _base64_filter =
519 NULL 528 NULL
520 }; 529 };
521 530
531 static int
532 B_encode (const char *iptr, size_t isize,
533 char *optr, size_t osize,
534 size_t *nbytes, int *line_len)
535 {
536 return base64_encode_internal (iptr, isize, optr, osize,
537 nbytes, line_len, 0);
538 }
539
540 static int
541 B_init (mu_filter_t filter)
542 {
543 struct _trans_stream *ts;
544 ts = calloc (sizeof (*ts), 1);
545 if (ts == NULL)
546 return ENOMEM;
547
548 ts->min_size = 4;
549 ts->s_buf = calloc (ts->min_size, 1);
550 if (ts->s_buf == NULL)
551 {
552 free (ts);
553 return ENOMEM;
554 }
555 ts->transcoder = (filter->type == MU_FILTER_DECODE) ? base64_decode : B_encode;
556
557 filter->_read = trans_read;
558 filter->_destroy = trans_destroy;
559 filter->data = ts;
560 return 0;
561 }
562
522 static struct _mu_filter_record _B_filter = 563 static struct _mu_filter_record _B_filter =
523 { 564 {
524 "B", 565 "B",
525 base64_init, 566 B_init,
526 NULL, 567 NULL,
527 NULL, 568 NULL,
528 NULL 569 NULL
......