Replace mh_xmalloc() with mh_xcalloc()
[mmh] / sbr / fmt_rfc2047.c
index 92e168b..e9db55e 100644 (file)
@@ -1,10 +1,10 @@
 /*
- * fmt_rfc2047.c -- decode RFC-2047 header format
- *
- * This code is Copyright (c) 2002, by the authors of nmh.  See the
- * COPYRIGHT file in the root directory of the nmh distribution for
- * complete copyright information.
- */
+** fmt_rfc2047.c -- decode RFC-2047 header format
+**
+** This code is Copyright (c) 2002, by the authors of nmh.  See the
+** COPYRIGHT file in the root directory of the nmh distribution for
+** complete copyright information.
+*/
 
 #include <h/mh.h>
 #include <h/utils.h>
@@ -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;
@@ -50,14 +50,14 @@ unqp (unsigned char byte1, unsigned char byte2)
 
 
 /*
- * Decode the string as a RFC-2047 header field
- */
+** Decode the string as a RFC-2047 header field
+*/
 
 /* Add character to the destination buffer, and bomb out if it fills up */
 #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;
@@ -78,10 +78,10 @@ decode_rfc2047 (char *str, char *dst, size_t dstlen)
                return 0;
 
        /*
-        * Do a quick and dirty check for the '=' character.
-        * This should quickly eliminate many cases.
-        */
-       if (!strchr (str, '='))
+       ** Do a quick and dirty check for the '=' character.
+       ** This should quickly eliminate many cases.
+       */
+       if (!strchr(str, '='))
                return 0;
 
        for (p = str, q = dst; *p; p++) {
@@ -94,9 +94,9 @@ decode_rfc2047 (char *str, char *dst, size_t dstlen)
                }
 #endif
                /*
-                * If we had an '=' character pending from
-                * last iteration, then add it first.
-                */
+               ** If we had an '=' character pending from
+               ** last iteration, then add it first.
+               */
                if (equals_pending) {
                        ADDCHR('=');
                        equals_pending = 0;
@@ -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 */
@@ -158,11 +161,11 @@ decode_rfc2047 (char *str, char *dst, size_t dstlen)
                        startofmime++;
 
                        /*
-                        * Scan ahead for the ending ?=
-                        *
-                        * While doing this, we will also check if encoded
-                        * word has any embedded linear whitespace.
-                        */
+                       ** Scan ahead for the ending ?=
+                       **
+                       ** While doing this, we will also check if encoded
+                       ** word has any embedded linear whitespace.
+                       */
                        endofmime = NULL;
                        for (pp = startofmime; *pp && *(pp+1); pp++) {
                                if (is_lws(*pp)) {
@@ -176,17 +179,18 @@ decode_rfc2047 (char *str, char *dst, size_t dstlen)
                                continue;
 
                        /*
-                        * We've found an encoded word, so we can drop
-                        * the '=' that was pending
-                        */
+                       ** We've found an encoded word, so we can drop
+                       ** the '=' that was pending
+                       */
                        equals_pending = 0;
 
                        /*
-                        * If we are between two encoded words separated only by
-                        * linear whitespace, then we ignore the whitespace.
-                        * We will roll back the buffer the number of whitespace
-                        * characters we've seen since last encoded word.
-                        */
+                       ** If we are between two encoded words separated
+                       ** only by linear whitespace, then we ignore
+                       ** the whitespace.  We will roll back the buffer
+                       ** the number of whitespace characters we've seen
+                       ** since last encoded word.
+                       */
                        if (between_encodings) {
                                q -= whitespace;
                                dstlen += whitespace;
@@ -194,9 +198,9 @@ decode_rfc2047 (char *str, char *dst, size_t dstlen)
 
 #ifdef HAVE_ICONV
                        /*
-                        * empty encoded text. This ensures that we don't
-                        * malloc 0 bytes but skip on to the end
-                        */
+                       ** empty encoded text. This ensures that we don't
+                       ** malloc 0 bytes but skip on to the end
+                       */
                        if (endofmime == startofmime && use_iconv) {
                                use_iconv = 0;
                                iconv_close(cd);
@@ -205,12 +209,13 @@ 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:
- * in this case on buffer-full we want to run iconv before returning.
- * I apologise for the dreadful name.
- */
+/*
+** ADDCHR2 is for adding characters when q is or might be convbuf:
+** in this case on buffer-full we want to run iconv before returning.
+** I apologise for the dreadful name.
+*/
 # define ADDCHR2(C) do { *q++ = (C); dstlen--; if (!dstlen) goto iconvbuffull; } while (0)
 #else
 # define ADDCHR2(C) ADDCHR(C)
@@ -220,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)
@@ -241,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) {
@@ -257,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) {
@@ -266,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) {
@@ -278,10 +287,11 @@ decode_rfc2047 (char *str, char *dst, size_t dstlen)
 
 #ifdef HAVE_ICONV
                iconvbuffull:
-                       /* NB that the string at convbuf is not necessarily
-                        * NUL terminated here:
-                        * q points to the first byte after the valid part.
-                        */
+                       /*
+                       ** NB that the string at convbuf is not necessarily
+                       ** NUL terminated here:
+                       ** q points to the first byte after the valid part.
+                       */
                        /* Convert to native character set */
                        if (use_iconv) {
                                size_t inbytes = q - convbuf;
@@ -292,10 +302,14 @@ decode_rfc2047 (char *str, char *dst, size_t dstlen)
                                                (size_t)-1) {
                                                if (errno != EILSEQ)
                                                        break;
-                                               /* character couldn't be converted. we output a `?'
-                                                * and try to carry on which won't work if
-                                                * either encoding was stateful */
-                                               iconv (cd, 0, 0, &saveq, &savedstlen);
+                                               /*
+                                               ** character couldn't be
+                                               ** converted. we output a
+                                               ** `?'  and try to carry on
+                                               ** which won't work if either
+                                               ** encoding was stateful
+                                               */
+                                               iconv(cd, 0, 0, &saveq, &savedstlen);
                                                if (!savedstlen)
                                                        break;
                                                *saveq++ = '?';
@@ -313,10 +327,13 @@ decode_rfc2047 (char *str, char *dst, size_t dstlen)
                                        }
                                }
                                q = saveq;
-                               /* Stop now if (1) we hit the end of the buffer trying to do
-                                * MIME decoding and have just iconv-converted a partial string
-                                * or (2) our iconv-conversion hit the end of the buffer.
-                                */
+                               /*
+                               ** Stop now if (1) we hit the end of the
+                               ** buffer trying to do MIME decoding and
+                               ** have just iconv-converted a partial
+                               ** string or (2) our iconv-conversion hit
+                               ** the end of the buffer.
+                               */
                                if (!dstlen || !savedstlen)
                                        goto buffull;
                                dstlen = savedstlen;
@@ -325,9 +342,9 @@ decode_rfc2047 (char *str, char *dst, size_t dstlen)
 #endif
 
                        /*
-                        * Now that we are done decoding this particular
-                        * encoded word, advance string to trailing '='.
-                        */
+                       ** Now that we are done decoding this particular
+                       ** encoded word, advance string to trailing '='.
+                       */
                        p = endofmime + 1;
 
                        encoding_found = 1;  /* we found (at least 1) encoded word */
@@ -347,7 +364,10 @@ decode_rfc2047 (char *str, char *dst, size_t dstlen)
        return encoding_found;
 
   buffull:
-       /* q is currently just off the end of the buffer, so rewind to NUL terminate */
+       /*
+       ** q is currently just off the end of the buffer,
+       ** so rewind to NUL terminate
+       */
        q--;
        *q = '\0';
        return encoding_found;