* please see the file GPL in the top level source directory.
*/
-#include <lib-lib/mem.h>
-#include <lib-lib/str.h>
-#include <lib-lib/ascii.h>
+#include <lib-lib/lib-lib.h>
#include <lib-mime/mime.h>
#include "charset.h"
#include "thread.h"
-#include <assert.h>
-#include <ctype.h>
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
/* If you are debugging this file, comment out the following line. */
/*#define NDEBUG*/
cd = mutt_iconv_open(to, from, 0);
- if (cd == (iconv_t)(-1))
+ if (cd == MUTT_ICONV_ERROR)
return -1;
obl = 4 * flen + 1;
p_delete(&res);
}
- mutt_canonical_charset(buf, sizeof(buf), tocode);
+ charset_canonicalize(buf, sizeof(buf), tocode);
m_strreplace(&tocode, buf);
}
ssize_t ibl = dlen;
iconv_t cd = mutt_iconv_open(tocode, fromcode, 0);
- assert (cd != (iconv_t)(-1));
+ assert (cd != MUTT_ICONV_ERROR);
ob = buf1;
if (fromcode) {
cd = mutt_iconv_open(tocode, fromcode, 0);
- assert (cd != (iconv_t) (-1));
+ assert (cd != MUTT_ICONV_ERROR);
ib = d, ibl = dlen, ob = buf1, obl = sizeof(buf1) - m_strlen(tocode);
n1 = my_iconv(cd, &ib, &ibl, &ob, &obl);
n2 = my_iconv(cd, 0, 0, &ob, &obl);
}
/* Hack to avoid labelling 8-bit data as us-ascii. */
- if (!icode && mutt_is_us_ascii(tocode))
+ if (!icode && charset_is_us_ascii(tocode))
tocode = "unknown-8bit";
/* Adjust t0 for maximum length of line. */
}
-void _rfc2047_encode_string(char **pd, int encode_specials, int col)
+static void _rfc2047_encode_string(char **pd, int encode_specials, int col)
{
char *e;
ssize_t elen;
/* Decoding functions */
/****************************************************************************/
-/* decode one word into d[len] */
-static int rfc2047_decode_word(char *d, size_t len, const char *s)
+/* decode one word into d[len] =?cst?[QB]?....?= */
+static int
+rfc2047_decode_word(char *d, size_t len, const char *p, const char *end)
{
- const char *p, *eotoken;
- char *charset = NULL;
- int enc = 0, count = 0;
- char *d0;
+ char charset[STRING] = "";
+ const char *t;
+ char *q, *d0 = NULL;
+ int enc = 0;
- /* =?[QB]?cset?.?= */
- for (p = s; (eotoken = strchr(p, '?')); p = eotoken + 1) {
- switch (++count) {
- const char *t;
- char *q;
+ p += 2; /* =? */
- case 2:
- /* ignore language specification a la RFC 2231 */
- t = memchr(p, '*', eotoken - p) ?: eotoken;
- charset = p_dupstr(p, t - p);
- break;
+ t = strchr(p, '?');
+ if (!t)
+ return -1;
+ m_strncpy(charset, sizeof(charset), p, t - p);
- case 3:
- switch (*p) {
- case 'q': case 'Q':
- enc = ENCQUOTEDPRINTABLE;
- break;
+ switch (t[1]) {
+ case 'q': case 'Q':
+ enc = ENCQUOTEDPRINTABLE;
+ break;
- case 'b': case 'B':
- enc = ENCBASE64;
- break;
+ case 'b': case 'B':
+ enc = ENCBASE64;
+ break;
- default:
- p_delete(&charset);
- return -1;
+ default:
+ return -1;
+ }
+
+ if (t[2] != '?')
+ return -1;
+
+ p = t + 3; /* skip ?[QB]? */
+ d0 = q = p_new(char, end - p + 1); /* it's enough space to decode */
+
+ if (enc == ENCQUOTEDPRINTABLE) {
+ while (p < end - 2) {
+ if (*p == '=' && hexval(p[1]) >= 0 && hexval(p[2]) >= 0) {
+ *q++ = (hexval (p[1]) << 4) | hexval (p[2]);
+ p += 3;
+ } else
+ if (*p == '_') {
+ *q++ = ' ';
+ p++;
+ } else {
+ *q++ = *p++;
}
- break;
+ }
+ } else { /* enc == ENCBASE64 */
+ int c, b = 0, k = 0;
- case 4:
- d0 = q = p_new(char, m_strlen(s) + 1);
-
- if (enc == ENCQUOTEDPRINTABLE) {
- while (p < eotoken) {
- if (*p == '=' && hexval(p[1]) >= 0 && hexval(p[2]) >= 0) {
- *q++ = (hexval (p[1]) << 4) | hexval (p[2]);
- p += 3;
- } else
- if (*p == '_') {
- *q++ = ' ';
- p++;
- } else {
- *q++ = *p++;
- }
- }
- *q = 0;
- } else { /* enc == ENCBASE64 */
- int c, b = 0, k = 0;
-
- while (p < eotoken) {
- if (*p == '=')
- break;
-
- c = base64val(*p++);
- if (c < 0)
- continue;
-
- if (k + 6 >= 8) {
- k -= 2;
- *q++ = b | (c >> k);
- b = c << (8 - k);
- } else {
- b |= c << (k + 2);
- k += 6;
- }
- }
- *q = 0;
+ while (p < end - 2) {
+ if (*p == '=')
+ break;
+
+ c = base64val(*p++);
+ if (c < 0)
+ continue;
+
+ if (k + 6 >= 8) {
+ k -= 2;
+ *q++ = b | (c >> k);
+ b = c << (8 - k);
+ } else {
+ b |= c << (k + 2);
+ k += 6;
}
- break;
}
}
+ *q = '\0';
- if (charset)
+ if (*charset)
mutt_convert_string(&d0, charset, Charset, M_ICONV_HOOK_FROM);
m_strcpy(d, len, d0);
- p_delete(&charset);
p_delete(&d0);
return 0;
}
continue;
s += 3;
- while (0x20 <= *s && *s < 0x7f && *s != '?') {
+ while (0x20 <= *s && *s < 0x7f && (*s != '?' || s[1] != '=')) {
s++;
}
}
}
- rfc2047_decode_word(d, dlen, p);
+ rfc2047_decode_word(d, dlen, p, q);
found_encoded = 1;
s = q;
while (*d && dlen)