]> diplodocus.org Git - nmh/blob - sbr/encode_rfc2047.c
823fd824940becd9995e0b5fc5d7e55bb66e0f7f
[nmh] / sbr / encode_rfc2047.c
1 /* encode_rfc2047.c -- encode message headers using RFC 2047 encoding.
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include "h/mh.h"
9 #include "check_charset.h"
10 #include "error.h"
11 #include "h/mhparse.h"
12 #include "h/addrsbr.h"
13 #include "h/utils.h"
14 #include "base64.h"
15 #include "unquote.h"
16
17 /*
18 * List of headers that contain addresses and as a result require special
19 * handling
20 */
21
22 static char *address_headers[] = {
23 "To",
24 "From",
25 "cc",
26 "Bcc",
27 "Reply-To",
28 "Sender",
29 "Resent-To",
30 "Resent-From",
31 "Resent-cc",
32 "Resent-Bcc",
33 "Resent-Reply-To",
34 "Resent-Sender",
35 NULL,
36 };
37
38 /*
39 * Macros we use for parsing headers
40 */
41
42 #define is_fws(c) (c == '\t' || c == ' ' || c == '\n')
43
44 #define qphrasevalid(c) ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || \
45 (c >= 'a' && c <= 'z') || \
46 c == '!' || c == '*' || c == '+' || c == '-' || \
47 c == '/' || c == '=' || c == '_')
48 #define qpspecial(c) (c < ' ' || c == '=' || c == '?' || c == '_')
49
50 #define base64len(n) ((((n) + 2) / 3 ) * 4) /* String len to base64 len */
51 #define strbase64(n) ((n) / 4 * 3) /* Chars that fit in base64 */
52
53 #define ENCODELINELIMIT 76
54
55 static void unfold_header(char **, int);
56 static int field_encode_address(const char *, char **, int, const char *);
57 static int field_encode_quoted(const char *, char **, const char *, int,
58 int, int);
59 static int field_encode_base64(const char *, char **, const char *);
60 static int scanstring(const char *, int *, int *, int *);
61 static int utf8len(const char *);
62 static int pref_encoding(int, int, int);
63
64 /*
65 * Encode a message header using RFC 2047 encoding. We make the assumption
66 * that all characters < 128 are ASCII and as a consequence don't need any
67 * encoding.
68 */
69
70 int
71 encode_rfc2047(const char *name, char **value, int encoding,
72 const char *charset)
73 {
74 int i, asciicount = 0, eightbitcount = 0, qpspecialcount = 0;
75 char *p;
76
77 /*
78 * First, check to see if we even need to encode the header
79 */
80
81 for (p = *value; *p != '\0'; p++) {
82 if (isascii((unsigned char) *p)) {
83 asciicount++;
84 if (qpspecial((unsigned char) *p))
85 qpspecialcount++;
86 } else
87 eightbitcount++;
88 }
89
90 if (eightbitcount == 0)
91 return 0;
92
93 /*
94 * Some rules from RFC 2047:
95 *
96 * - Encoded words cannot be more than 75 characters long
97 * - Multiple "long" encoded words must be on new lines.
98 *
99 * Also, we're not permitted to encode email addresses, so
100 * we need to actually _parse_ email addresses and only encode
101 * the right bits.
102 */
103
104 /*
105 * If charset was NULL, then get the value from the locale. But
106 * we reject it if it returns US-ASCII
107 */
108
109 if (charset == NULL)
110 charset = write_charset_8bit();
111
112 if (strcasecmp(charset, "US-ASCII") == 0) {
113 inform("Cannot use US-ASCII with 8 bit characters in header");
114 return 1;
115 }
116
117 /*
118 * If we have an address header, then we need to parse the addresses
119 * and only encode the names or comments. Otherwise, handle it normally.
120 */
121
122 for (i = 0; address_headers[i]; i++) {
123 if (strcasecmp(name, address_headers[i]) == 0)
124 return field_encode_address(name, value, encoding, charset);
125 }
126
127 /*
128 * On the encoding we choose, and the specifics of encoding:
129 *
130 * - If a specified encoding is passed in, we use that.
131 * - Otherwise, pick which encoding is shorter.
132 *
133 * We don't quite handle continuation right here, but it should be
134 * pretty close.
135 */
136
137 if (encoding == CE_UNKNOWN)
138 encoding = pref_encoding(asciicount, qpspecialcount, eightbitcount);
139
140 unfold_header(value, asciicount + eightbitcount);
141
142 switch (encoding) {
143
144 case CE_BASE64:
145 return field_encode_base64(name, value, charset);
146
147 case CE_QUOTED:
148 return field_encode_quoted(name, value, charset, asciicount,
149 eightbitcount + qpspecialcount, 0);
150
151 default:
152 inform("Internal error: unknown RFC-2047 encoding type");
153 return 1;
154 }
155 }
156
157 /*
158 * Encode our specified header (or field) using quoted-printable
159 */
160
161 static int
162 field_encode_quoted(const char *name, char **value, const char *charset,
163 int ascii, int encoded, int phraserules)
164 {
165 int prefixlen = name ? strlen(name) + 2: 0, outlen = 0, column;
166 int charsetlen = strlen(charset), utf8;
167 char *output = NULL, *p, *q = NULL;
168
169 /*
170 * Right now we just encode the whole thing. Maybe later on we'll
171 * only encode things on a per-atom basis.
172 */
173
174 p = *value;
175
176 column = prefixlen + 2; /* Header name plus ": " */
177
178 utf8 = strcasecmp(charset, "UTF-8") == 0;
179
180 bool newline = true;
181 while (*p != '\0') {
182 /*
183 * Start a new line, if it's time
184 */
185 if (newline) {
186 /*
187 * If it's the start of the header, we don't need to pad it
188 *
189 * The length of the output string is ...
190 * =?charset?Q?...?= so that's 7+strlen(charset) + 2 for \n NUL
191 *
192 * plus 1 for every ASCII character and 3 for every eight bit
193 * or special character (eight bit characters are written as =XX).
194 *
195 */
196
197 int tokenlen;
198
199 outlen += 9 + charsetlen + ascii + 3 * encoded;
200
201 /*
202 * If output is set, then we're continuing the header. Otherwise
203 * do the initial allocation.
204 */
205
206 if (output) {
207 int curlen = q - output, i;
208 outlen += prefixlen + 1; /* Header plus \n ": " */
209 output = mh_xrealloc(output, outlen);
210 q = output + curlen;
211 *q++ = '?';
212 *q++ = '=';
213 *q++ = '\n';
214 for (i = 0; i < prefixlen; i++)
215 *q++ = ' ';
216 } else {
217 /*
218 * A bit of a hack here; the header can contain multiple
219 * spaces (probably at least one) until we get to the
220 * actual text. Copy until we get to a non-space.
221 */
222 output = mh_xmalloc(outlen);
223 q = output;
224 while (is_fws(*p))
225 *q++ = *p++;
226 }
227
228 tokenlen = snprintf(q, outlen - (q - output), "=?%s?Q?", charset);
229 q += tokenlen;
230 column = prefixlen + tokenlen;
231 newline = false;
232 }
233
234 /*
235 * Process each character, encoding if necessary
236 *
237 * Note that we have a different set of rules if we're processing
238 * RFC 5322 'phrase' (something you'd see in an address header).
239 */
240
241 column++;
242
243 if (*p == ' ') {
244 *q++ = '_';
245 ascii--;
246 } else if (isascii((unsigned char) *p) &&
247 (phraserules ? qphrasevalid((unsigned char) *p) :
248 !qpspecial((unsigned char) *p))) {
249 *q++ = *p;
250 ascii--;
251 } else {
252 snprintf(q, outlen - (q - output), "=%02X", (unsigned char) *p);
253 q += 3;
254 column += 2; /* column already incremented by 1 above */
255 encoded--;
256 }
257
258 p++;
259
260 /*
261 * We're not allowed more than ENCODELINELIMIT characters per line,
262 * so reserve some room for the final ?=.
263 *
264 * If prefixlen == 0, we haven't been passed in a header name, so
265 * don't ever wrap the field (we're likely doing an address).
266 */
267
268 if (prefixlen == 0)
269 continue;
270
271 if (column >= ENCODELINELIMIT - 2) {
272 newline = true;
273 } else if (utf8) {
274 /*
275 * Okay, this is a bit weird, but to explain a bit more ...
276 *
277 * RFC 2047 prohibits the splitting of multibyte characters
278 * across encoded words. Right now we only handle the case
279 * of UTF-8, the most common multibyte encoding.
280 *
281 * p is now pointing at the next input character. If we're
282 * using UTF-8 _and_ we'd go over ENCODELINELIMIT given the
283 * length of the complete character, then trigger a newline
284 * now. Note that we check the length * 3 since we have to
285 * allow for the encoded output.
286 */
287 if (column + (utf8len(p) * 3) > ENCODELINELIMIT - 2) {
288 newline = true;
289 }
290 }
291 }
292
293 if (q == NULL) {
294 /* This should never happen, but just in case. Found by
295 clang static analyzer. */
296 inform("null output encoding for %s, continuing...", *value);
297 return 1;
298 }
299 *q++ = '?';
300 *q++ = '=';
301
302 if (prefixlen)
303 *q++ = '\n';
304
305 *q = '\0';
306
307 free(*value);
308
309 *value = output;
310
311 return 0;
312 }
313
314 /*
315 * Encode our specified header (or field) using base64.
316 *
317 * This is a little easier since every character gets encoded, we can
318 * calculate the line wrap up front.
319 */
320
321 static int
322 field_encode_base64(const char *name, char **value, const char *charset)
323 {
324 int prefixlen = name ? strlen(name) + 2 : 0, charsetlen = strlen(charset);
325 int outlen = 0, numencode, curlen;
326 char *output = NULL, *p = *value, *q = NULL, *linestart = NULL;
327
328 /*
329 * Skip over any leading white space.
330 */
331
332 while (*p == ' ' || *p == '\t')
333 p++;
334
335 /*
336 * If we had a zero-length prefix, then just encode the whole field
337 * as-is, without line wrapping. Note that in addition to the encoding
338 *
339 * The added length we need is =? + charset + ?B? ... ?=
340 *
341 * That's 7 + strlen(charset) + 2 (for \n NUL).
342 */
343
344 while (prefixlen && ((base64len(strlen(p)) + 7 + charsetlen +
345 prefixlen) > ENCODELINELIMIT)) {
346
347 /*
348 * Our very first time, don't pad the line in the front
349 *
350 * Note ENCODELINELIMIT is + 2 because of \n \0
351 */
352
353
354 if (! output) {
355 outlen += ENCODELINELIMIT + 2;
356 output = q = mh_xmalloc(outlen);
357 linestart = q - prefixlen; /* Yes, this is intentional */
358 } else {
359 int curstart = linestart - output;
360 curlen = q - output;
361
362 outlen += ENCODELINELIMIT + 2;
363 output = mh_xrealloc(output, outlen);
364 q = output + curlen;
365 linestart = output + curstart;
366 }
367
368 /*
369 * We should have enough space now, so prepend the encoding markers
370 * and character set information. The leading space is intentional.
371 */
372
373 q += snprintf(q, outlen - (q - output), " =?%s?B?", charset);
374
375 /*
376 * Find out how much room we have left on the line and see how
377 * many characters we can stuff in. The start of our line
378 * is marked by "linestart", so use that to figure out how
379 * many characters are left out of ENCODELINELIMIT. Reserve
380 * 2 characters for the end markers and calculate how many
381 * characters we can fit into that space given the base64
382 * encoding expansion.
383 */
384
385 numencode = strbase64(ENCODELINELIMIT - (q - linestart) - 2);
386
387 if (numencode <= 0) {
388 inform("Internal error: tried to encode %d characters "
389 "in base64", numencode);
390 return 1;
391 }
392
393 /*
394 * RFC 2047 prohibits spanning multibyte characters across tokens.
395 * Right now we only check for UTF-8.
396 *
397 * So note the key here ... we want to make sure the character BEYOND
398 * our last character is not a continuation byte. If it's the start
399 * of a new multibyte character or a single-byte character, that's ok.
400 */
401
402 if (strcasecmp(charset, "UTF-8") == 0) {
403 /*
404 * p points to the start of our current buffer, so p + numencode
405 * is one past the last character to encode
406 */
407
408 while (numencode > 0 && ((*(p + numencode) & 0xc0) == 0x80))
409 numencode--;
410
411 if (numencode == 0) {
412 inform("Internal error: could not find start of "
413 "UTF-8 character when base64 encoding header");
414 return 1;
415 }
416 }
417
418 if (writeBase64raw((unsigned char *) p, numencode,
419 (unsigned char *) q) != OK) {
420 inform("Internal error: base64 encoding of header failed");
421 return 1;
422 }
423
424 p += numencode;
425 q += base64len(numencode);
426
427 /*
428 * This will point us at the beginning of the new line (trust me).
429 */
430
431 linestart = q + 3;
432
433 /*
434 * What's going on here? Well, we know we're continuing to the next
435 * line, so we want to add continuation padding. We also add the
436 * trailing marker for the RFC 2047 token at this time as well.
437 * This uses a trick of snprintf(); we tell it to print a zero-length
438 * string, but pad it out to prefixlen - 1 characters; that ends
439 * up always printing out the requested number of spaces. We use
440 * prefixlen - 1 because we always add a space on the starting
441 * token marker; this makes things work out correctly for the first
442 * line, which should have a space between the ':' and the start
443 * of the token.
444 *
445 * It's okay if you don't follow all of that.
446 */
447
448 q += snprintf(q, outlen - (q - output), "?=\n%*s", prefixlen - 1, "");
449 }
450
451 /*
452 * We're here if there is either no prefix, or we can fit it in less
453 * than ENCODELINELIMIT characters. Encode the whole thing.
454 */
455
456 outlen += prefixlen + 9 + charsetlen + base64len(strlen(p));
457 curlen = q - output;
458
459 output = mh_xrealloc(output, outlen);
460 q = output + curlen;
461
462 q += snprintf(q, outlen - (q - output), "%s=?%s?B?",
463 prefixlen ? " " : "", charset);
464
465 if (writeBase64raw((unsigned char *) p, strlen(p),
466 (unsigned char *) q) != OK) {
467 inform("Internal error: base64 encoding of header failed");
468 return 1;
469 }
470
471 strcat(q, "?=");
472
473 if (prefixlen)
474 strcat(q, "\n");
475
476 free(*value);
477
478 *value = output;
479
480 return 0;
481 }
482
483 /*
484 * Calculate the length of a UTF-8 character.
485 *
486 * If it's not a UTF-8 character (or we're in the middle of a multibyte
487 * character) then simply return 0.
488 */
489
490 static int
491 utf8len(const char *p)
492 {
493 int len = 1;
494
495 if (*p == '\0')
496 return 0;
497
498 if (isascii((unsigned char) *p) || (((unsigned char) *p) & 0xc0) == 0x80)
499 return 0;
500
501 p++;
502 while ((((unsigned char) *p++) & 0xc0) == 0x80)
503 len++;
504
505 return len;
506 }
507
508 /*
509 * "Unfold" a header, making it a single line (without continuation)
510 *
511 * We cheat a bit here; we never make the string longer, so using the
512 * original length here is fine.
513 */
514
515 static void
516 unfold_header(char **value, int len)
517 {
518 char *str = mh_xmalloc(len + 1);
519 char *p = str, *q = *value;
520
521 while (*q != '\0') {
522 if (*q == '\n') {
523 /*
524 * When we get a newline, skip to the next non-whitespace
525 * character and add a space to replace all of the whitespace
526 *
527 * This has the side effect of stripping off the final newline
528 * for the header; we put it back in the encoding routine.
529 */
530 while (is_fws(*q))
531 q++;
532 if (*q == '\0')
533 break;
534
535 *p++ = ' ';
536 } else {
537 *p++ = *q++;
538 }
539 }
540
541 *p = '\0';
542
543 free(*value);
544 *value = str;
545 }
546
547 /*
548 * Decode a header containing addresses. This means we have to parse
549 * each address and only encode the display-name or comment field.
550 */
551
552 static int
553 field_encode_address(const char *name, char **value, int encoding,
554 const char *charset)
555 {
556 int prefixlen = strlen(name) + 2, column = prefixlen, groupflag;
557 int asciichars;
558 int specialchars;
559 int eightbitchars;
560 bool reformat = false;
561 bool errflag = false;
562 size_t len;
563 char *mp, *cp = NULL, *output = NULL;
564 char *tmpbuf = NULL;
565 size_t tmpbufsize = 0;
566 struct mailname *mn;
567 char errbuf[BUFSIZ];
568
569 /*
570 * Because these are addresses, we need to handle them individually.
571 *
572 * Break them down and process them one by one. This means we have to
573 * rewrite the whole header, but that's unavoidable.
574 */
575
576 /*
577 * The output headers always have to start with a space first; this
578 * is just the way the API works right now.
579 */
580
581 output = add(" ", output);
582
583 for (groupflag = 0; (mp = getname(*value)); ) {
584 if ((mn = getm(mp, NULL, 0, errbuf, sizeof(errbuf))) == NULL) {
585 inform("%s: %s", errbuf, mp);
586 errflag = true;
587 continue;
588 }
589
590 reformat = false;
591
592 /*
593 * We only care if the phrase (m_pers) or any trailing comment
594 * (m_note) have 8-bit characters. If doing q-p, we also need
595 * to encode anything marked as qspecial(). Unquote it first
596 * so the specialchars count is right.
597 */
598
599 if (! mn->m_pers)
600 goto check_note;
601
602 if ((len = strlen(mn->m_pers)) + 1 > tmpbufsize) {
603 tmpbuf = mh_xrealloc(tmpbuf, tmpbufsize = len + 1);
604 }
605
606 unquote_string(mn->m_pers, tmpbuf);
607
608 if (scanstring(tmpbuf, &asciichars, &eightbitchars,
609 &specialchars)) {
610 /*
611 * If we have 8-bit characters, encode it.
612 */
613
614 if (encoding == CE_UNKNOWN)
615 encoding = pref_encoding(asciichars, specialchars,
616 eightbitchars);
617
618 /*
619 * This is okay, because the output of unquote_string will be either
620 * equal or shorter than the original.
621 */
622
623 strcpy(mn->m_pers, tmpbuf);
624
625 switch (encoding) {
626
627 case CE_BASE64:
628 if (field_encode_base64(NULL, &mn->m_pers, charset)) {
629 errflag = true;
630 goto out;
631 }
632 break;
633
634 case CE_QUOTED:
635 if (field_encode_quoted(NULL, &mn->m_pers, charset, asciichars,
636 eightbitchars + specialchars, 1)) {
637 errflag = true;
638 goto out;
639 }
640 break;
641
642 default:
643 inform("Internal error: unknown RFC-2047 encoding type");
644 errflag = true;
645 goto out;
646 }
647
648 reformat = true;
649 }
650
651 check_note:
652
653 /*
654 * The "note" field is generally a comment at the end of the address,
655 * at least as how it's implemented here. Notes are always surrounded
656 * by parenthesis (since they're comments). Strip them out and
657 * then put them back when we format the final field, but they do
658 * not get encoded.
659 */
660
661 if (! mn->m_note)
662 goto do_reformat;
663
664 if ((len = strlen(mn->m_note)) + 1 > tmpbufsize) {
665 tmpbuf = mh_xrealloc(tmpbuf, tmpbufsize = len + 1);
666 }
667
668 if (mn->m_note[0] != '(' || mn->m_note[len - 1] != ')') {
669 inform("Internal error: Invalid note field \"%s\"",
670 mn->m_note);
671 errflag = true;
672 goto out;
673 }
674
675 strncpy(tmpbuf, mn->m_note + 1, len - 1);
676 tmpbuf[len - 2] = '\0';
677
678 if (scanstring(tmpbuf, &asciichars, &eightbitchars,
679 &specialchars)) {
680 /*
681 * If we have 8-bit characters, encode it.
682 */
683
684 if (encoding == CE_UNKNOWN)
685 encoding = pref_encoding(asciichars, specialchars,
686 eightbitchars);
687
688 switch (encoding) {
689
690 case CE_BASE64:
691 if (field_encode_base64(NULL, &tmpbuf, charset)) {
692 errflag = true;
693 goto out;
694 }
695 break;
696
697 case CE_QUOTED:
698 if (field_encode_quoted(NULL, &tmpbuf, charset, asciichars,
699 eightbitchars + specialchars, 1)) {
700 errflag = true;
701 goto out;
702 }
703 break;
704
705 default:
706 inform("Internal error: unknown RFC-2047 encoding type");
707 errflag = true;
708 goto out;
709 }
710
711 reformat = true;
712
713 /*
714 * Make sure the size of tmpbuf is correct (it always gets
715 * reallocated in the above functions).
716 */
717
718 tmpbufsize = strlen(tmpbuf) + 1;
719
720 /*
721 * Put the note field back surrounded by parenthesis.
722 */
723
724 mn->m_note = mh_xrealloc(mn->m_note, tmpbufsize + 2);
725
726 snprintf(mn->m_note, tmpbufsize + 2, "(%s)", tmpbuf);
727 }
728
729 do_reformat:
730
731 /*
732 * So, some explanation is in order.
733 *
734 * We know we need to rewrite at least one address in the header,
735 * otherwise we wouldn't be here. If we had to reformat this
736 * particular address, then run it through adrformat(). Otherwise
737 * we can use m_text directly.
738 */
739
740 /*
741 * If we were in a group but are no longer, make sure we add a
742 * semicolon (which needs to be FIRST, as it needs to be at the end
743 * of the last address).
744 */
745
746 if (groupflag && ! mn->m_ingrp) {
747 output = add(";", output);
748 column++;
749 }
750
751 groupflag = mn->m_ingrp;
752
753 if (mn->m_gname) {
754 cp = mh_xstrdup(mn->m_gname);
755 }
756
757 if (reformat) {
758 cp = add(adrformat(mn), cp);
759 } else {
760 cp = add(mn->m_text, cp);
761 }
762
763 len = strlen(cp);
764
765 /*
766 * If we're not at the beginning of the line, add a command and
767 * either a space or a newline.
768 */
769
770 if (column != prefixlen) {
771 if (len + column + 2 > OUTPUTLINELEN) {
772
773 if ((size_t) (prefixlen + 3) < tmpbufsize)
774 tmpbuf = mh_xrealloc(tmpbuf, tmpbufsize = prefixlen + 3);
775
776 snprintf(tmpbuf, tmpbufsize, ",\n%*s", column = prefixlen, "");
777 output = add(tmpbuf, output);
778 } else {
779 output = add(", ", output);
780 column += 2;
781 }
782 }
783
784 /*
785 * Finally add the address
786 */
787
788 output = add(cp, output);
789 column += len;
790 free(cp);
791 cp = NULL;
792 }
793
794 /*
795 * Just in case we're at the end of a list
796 */
797
798 if (groupflag) {
799 output = add(";", output);
800 }
801
802 output = add("\n", output);
803
804 free(*value);
805 *value = output;
806 output = NULL;
807
808 out:
809 free(tmpbuf);
810 free(output);
811
812 return errflag;
813 }
814
815 /*
816 * Scan a string, check for characters that need to be encoded
817 */
818
819 static int
820 scanstring(const char *string, int *asciilen, int *eightbitchars,
821 int *specialchars)
822 {
823 *asciilen = 0;
824 *eightbitchars = 0;
825 *specialchars = 0;
826
827 for (; *string != '\0'; string++) {
828 if ((isascii((unsigned char) *string))) {
829 (*asciilen)++;
830 /*
831 * So, a space is not a valid phrase character, but we're counting
832 * an exception here, because in q-p a space can be directly
833 * encoded as an underscore.
834 */
835 if (!qphrasevalid((unsigned char) *string) && *string != ' ')
836 (*specialchars)++;
837 } else {
838 (*eightbitchars)++;
839 }
840 }
841
842 return *eightbitchars > 0;
843 }
844
845 /*
846 * This function is to be used to decide which encoding algorithm we should
847 * use if one is not given. Basically, we pick whichever one is the shorter
848 * of the two.
849 *
850 * Arguments are:
851 *
852 * ascii - Number of ASCII characters in to-be-encoded string.
853 * specials - Number of ASCII characters in to-be-encoded string that
854 * still require encoding under quoted-printable. Note that
855 * these are included in the "ascii" total.
856 * eightbit - Eight-bit characters in the to-be-encoded string.
857 *
858 * Returns one of CE_BASE64 or CE_QUOTED.
859 */
860
861 static int
862 pref_encoding(int ascii, int specials, int eightbits)
863 {
864 /*
865 * The length of the q-p encoding is:
866 *
867 * ascii - specials + (specials + eightbits) * 3.
868 *
869 * The length of the base64 encoding is:
870 *
871 * base64len(ascii + eightbits) (See macro for details)
872 */
873
874 return base64len(ascii + eightbits) < (ascii - specials +
875 (specials + eightbits) * 3) ? CE_BASE64 : CE_QUOTED;
876 }