Replace mh_xmalloc() with mh_xcalloc()
[mmh] / sbr / fmt_rfc2047.c
index 5bc7850..e9db55e 100644 (file)
@@ -38,7 +38,7 @@ static signed char index_64[128] = {
 #define char64(c) (((unsigned char) (c) > 127) ? -1 : index_64[(unsigned char) (c)])
 
 static int
-unqp (unsigned char byte1, unsigned char byte2)
+unqp(unsigned char byte1, unsigned char byte2)
 {
        if (hexindex[byte1] == -1 || hexindex[byte2] == -1)
                return -1;
@@ -57,7 +57,7 @@ unqp (unsigned char byte1, unsigned char byte2)
 #define ADDCHR(C) do { *q++ = (C); dstlen--; if (!dstlen) goto buffull; } while (0)
 
 int
-decode_rfc2047 (char *str, char *dst, size_t dstlen)
+decode_rfc2047(char *str, char *dst, size_t dstlen)
 {
        char *p, *q, *pp;
        char *startofmime, *endofmime;
@@ -81,7 +81,7 @@ decode_rfc2047 (char *str, char *dst, size_t dstlen)
        ** Do a quick and dirty check for the '=' character.
        ** This should quickly eliminate many cases.
        */
-       if (!strchr (str, '='))
+       if (!strchr(str, '='))
                return 0;
 
        for (p = str, q = dst; *p; p++) {
@@ -125,22 +125,25 @@ decode_rfc2047 (char *str, char *dst, size_t dstlen)
 
                        if (!*pp)
                                continue;
+                       *pp = '\0';
 
                        /* Check if character set can be handled natively */
-                       if (!check_charset(startofmime, pp - startofmime)) {
+                       if (!is_native_charset(startofmime)) {
 #ifdef HAVE_ICONV
                                /* .. it can't. We'll use iconv then. */
-                               *pp = '\0';
                                cd = iconv_open(get_charset(), startofmime);
                                fromutf8 = !mh_strcasecmp(startofmime, "UTF-8");
                                *pp = '?';
-                               if (cd == (iconv_t)-1) continue;
+                               if (cd == (iconv_t)-1)
+                                       continue;
                                use_iconv = 1;
 #else
+                               *pp = '?';
                                continue;
 #endif
                        }
 
+                       *pp = '?';
                        startofmime = pp + 1;
 
                        /* Check for valid encoding type */
@@ -206,7 +209,7 @@ decode_rfc2047 (char *str, char *dst, size_t dstlen)
                        if (use_iconv) {
                                saveq = q;
                                savedstlen = dstlen;
-                               q = convbuf = (char *) mh_xmalloc(endofmime - startofmime);
+                               q = convbuf = (char *) mh_xcalloc(endofmime - startofmime, sizeof(char));
                        }
 /*
 ** ADDCHR2 is for adding characters when q is or might be convbuf:
@@ -222,7 +225,7 @@ decode_rfc2047 (char *str, char *dst, size_t dstlen)
                        if (quoted_printable) {
                                for (pp = startofmime; pp < endofmime; pp++) {
                                        if (*pp == '=') {
-                                               c = unqp (pp[1], pp[2]);
+                                               c = unqp(pp[1], pp[2]);
                                                if (c == -1)
                                                        continue;
                                                if (c != 0)
@@ -243,14 +246,16 @@ decode_rfc2047 (char *str, char *dst, size_t dstlen)
                                while (pp < endofmime) {
                                        /* 6 + 2 bits */
                                        while ((pp < endofmime) &&
-                                                  ((c1 = char64(*pp)) == -1)) {
+                                                       ((c1 = char64(*pp))
+                                                       == -1)) {
                                                pp++;
                                        }
                                        if (pp < endofmime) {
                                                pp++;
                                        }
                                        while ((pp < endofmime) &&
-                                                  ((c2 = char64(*pp)) == -1)) {
+                                                       ((c2 = char64(*pp))
+                                                       == -1)) {
                                                pp++;
                                        }
                                        if (pp < endofmime && c1 != -1 && c2 != -1) {
@@ -259,7 +264,8 @@ decode_rfc2047 (char *str, char *dst, size_t dstlen)
                                        }
                                        /* 4 + 4 bits */
                                        while ((pp < endofmime) &&
-                                                  ((c3 = char64(*pp)) == -1)) {
+                                                       ((c3 = char64(*pp))
+                                                       == -1)) {
                                                pp++;
                                        }
                                        if (pp < endofmime && c2 != -1 && c3 != -1) {
@@ -268,7 +274,8 @@ decode_rfc2047 (char *str, char *dst, size_t dstlen)
                                        }
                                        /* 2 + 6 bits */
                                        while ((pp < endofmime) &&
-                                                  ((c4 = char64(*pp)) == -1)) {
+                                                       ((c4 = char64(*pp))
+                                                       == -1)) {
                                                pp++;
                                        }
                                        if (pp < endofmime && c3 != -1 && c4 != -1) {
@@ -302,7 +309,7 @@ decode_rfc2047 (char *str, char *dst, size_t dstlen)
                                                ** which won't work if either
                                                ** encoding was stateful
                                                */
-                                               iconv (cd, 0, 0, &saveq, &savedstlen);
+                                               iconv(cd, 0, 0, &saveq, &savedstlen);
                                                if (!savedstlen)
                                                        break;
                                                *saveq++ = '?';