X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=rfc2047e.c;h=e6f8f32bcc4913b5d0d1800dd69197f273f11699;hb=da989f7b8294e342572ec5f27f1a6f3f2b1fe56f;hp=d42e30461c087548991fbc0dcfbda04c0c7c3950;hpb=9ef647242d5bd8185aaa5337527dc03ffc8a6611;p=~andy%2Ffetchmail diff --git a/rfc2047e.c b/rfc2047e.c index d42e3046..e6f8f32b 100644 --- a/rfc2047e.c +++ b/rfc2047e.c @@ -17,7 +17,9 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif #include "fetchmail.h" #include @@ -52,7 +54,7 @@ static char *encode_words(char *const *words, int nwords, const char *charset) l += strlen(words[i]) * 3; /* worst case, encode everything */ l += (strlen(charset) + 8) * (l/60 + 1); - out = v = xmalloc(l); + out = v = (char *)xmalloc(l); t = stpcpy(out, "=?"); t = stpcpy(t, charset); t = stpcpy(t, "?Q?"); @@ -67,7 +69,7 @@ static char *encode_words(char *const *words, int nwords, const char *charset) } if (*u == ' ') { *t++ = '_'; continue; } if (strchr(encchars, *u)) { *t++ = *u; continue; } - sprintf(t, "=%02X", (unsigned char)*u); + sprintf(t, "=%02X", (unsigned int)((unsigned char)*u)); t += 3; } } @@ -75,14 +77,23 @@ static char *encode_words(char *const *words, int nwords, const char *charset) return out; } +/** RFC-2047 encode string with given charset. Only the Q encoding + * (quoted-printable) supported at this time. + * WARNING: this code returns a static buffer! + */ char *rfc2047e(const char *string, const char *charset) { - char *t, *out; + static char *out; + char *t; const char *r; int count, minlen, idx, i; char **words = NULL; size_t l; assert(strlen(charset) < 40); + if (out) { + free(out); + out = NULL; + } /* phase 1: split original into words */ /* 1a: count, 1b: copy */ @@ -95,22 +106,22 @@ char *rfc2047e(const char *string, const char *charset) { count++; r += strspn(r, ws); } - words = xmalloc(sizeof(char *) * (count + 1)); + words = (char **)xmalloc(sizeof(char *) * (count + 1)); idx = 0; r = string; while (*r) { l = strcspn(r, ws); - words[idx] = xmalloc(l+1); + words[idx] = (char *)xmalloc(l+1); memcpy(words[idx], r, l); - words[idx][l] = 0; + words[idx][l] = '\0'; idx++; r += l; if (!*r) break; l = strspn(r, ws); - words[idx] = xmalloc(l+1); + words[idx] = (char *)xmalloc(l+1); memcpy(words[idx], r, l); - words[idx][l] = 0; + words[idx][l] = '\0'; idx++; r += l; } @@ -136,18 +147,19 @@ char *rfc2047e(const char *string, const char *charset) { free(words[idx]); words[idx] = tmp; for (i = idx + 1; i <= end; i++) - words[i][0] = 0; + words[i][0] = '\0'; idx = end + 2; } - for (idx = l = 0; idx < count; idx++) { + l = 0; + for (idx = 0; idx < count; idx++) { l += strlen(words[idx]); } /* phase 3: limit lengths */ minlen = strlen(charset) + 7; /* allocate ample memory */ - out = xmalloc(l + (l / (72 - minlen) + 1) * (minlen + 2) + 1); + out = (char *)xmalloc(l + (l / (72 - minlen) + 1) * (minlen + 2) + 1); if (count) t = stpcpy(out, words[0]); @@ -164,13 +176,16 @@ char *rfc2047e(const char *string, const char *charset) { if (i + 1 < count) m += strcspn(words[i+1], "\r\n"); if (l + m > 74) - l = 0, t = stpcpy(t, "\r\n"); + t = stpcpy(t, "\r\n"); t = stpcpy(t, words[i]); if (i + 1 < count) { t = stpcpy(t, words[i+1]); } tmp = strrchr(out, '\n'); - if (!tmp) tmp = out; else tmp++; + if (tmp == NULL) + tmp = out; + else + tmp++; l = strlen(tmp); }