+/*
+ * Copyright (c) 1996-2012 by the citadel.org team
+ *
+ * This program is open source software. You can redistribute it and/or
+ * modify it under the terms of the GNU General Public License, version 3.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
#include "webcit.h"
#ifdef HAVE_ICONV
*/
-inline char *FindNextEnd (char *bptr)
+static inline char *FindNextEnd (char *bptr)
{
char * end;
/* Find the next ?Q? */
* Handle subjects with RFC2047 encoding such as:
* =?koi8-r?B?78bP0s3Mxc7JxSDXz9rE1dvO2c3JINvB0sHNySDP?=
*/
-void utf8ify_rfc822_string(char *buf) {
+void utf8ify_rfc822_string(char **buf) {
char *start, *end, *next, *nextend, *ptr;
char newbuf[1024];
char charset[128];
char encoding[16];
char istr[1024];
iconv_t ic = (iconv_t)(-1) ;
- char *ibuf; /**< Buffer of characters to be converted */
- char *obuf; /**< Buffer for converted characters */
- size_t ibuflen; /**< Length of input buffer */
- size_t obuflen; /**< Length of output buffer */
- char *isav; /**< Saved pointer to input buffer */
- char *osav; /**< Saved pointer to output buffer */
+ char *ibuf; /* Buffer of characters to be converted */
+ char *obuf; /* Buffer for converted characters */
+ size_t ibuflen; /* Length of input buffer */
+ size_t obuflen; /* Length of output buffer */
+ char *isav; /* Saved pointer to input buffer */
+ char *osav; /* Saved pointer to output buffer */
int passes = 0;
int i, len, delta;
int illegal_non_rfc2047_encoding = 0;
* handle it anyway by converting from a user-specified default
* charset to UTF-8 if we see any nonprintable characters.
*/
- len = strlen(buf);
+ len = strlen(*buf);
for (i=0; i<len; ++i) {
- if ((buf[i] < 32) || (buf[i] > 126)) {
+ if (((*buf)[i] < 32) || ((*buf)[i] > 126)) {
illegal_non_rfc2047_encoding = 1;
- i = len; ///< take a shortcut, it won't be more than one.
+ i = len; /*< take a shortcut, it won't be more than one. */
}
}
if (illegal_non_rfc2047_encoding) {
if (ic != (iconv_t)(-1) ) {
ibuf = malloc(1024);
isav = ibuf;
- safestrncpy(ibuf, buf, 1024);
+ safestrncpy(ibuf, *buf, 1023);
ibuflen = strlen(ibuf);
obuflen = 1024;
obuf = (char *) malloc(obuflen);
osav = obuf;
iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
- osav[1024-obuflen] = 0;
- strcpy(buf, osav);
- free(osav);
+ osav[1023-obuflen] = 0;
+ free(*buf);
+ *buf = osav;
iconv_close(ic);
free(isav);
}
/* pre evaluate the first pair */
nextend = end = NULL;
- len = strlen(buf);
- start = strstr(buf, "=?");
+ len = strlen(*buf);
+ start = strstr(*buf, "=?");
if (start != NULL)
end = FindNextEnd (start);
/* now terminate the gab at the end */
delta = (next - end) - 2;
len -= delta;
- buf[len] = '\0';
+ (*buf)[len] = '\0';
/* move next to its new location. */
next -= delta;
/* Now we handle foreign character sets properly encoded
* in RFC2047 format.
*/
- while (start=strstr(buf, "=?"), end=FindNextEnd((start != NULL)? start : buf),
+ while (start=strstr((*buf), "=?"), end=FindNextEnd((start != NULL)? start : (*buf)),
((start != NULL) && (end != NULL) && (end > start)) )
{
extract_token(charset, start, 1, '?', sizeof charset);
ibuf = malloc(1024);
isav = ibuf;
- if (!strcasecmp(encoding, "B")) { /**< base64 */
+ if (!strcasecmp(encoding, "B")) { /* base64 */
ibuflen = CtdlDecodeBase64(ibuf, istr, strlen(istr));
}
- else if (!strcasecmp(encoding, "Q")) { /**< quoted-printable */
+ else if (!strcasecmp(encoding, "Q")) { /* quoted-printable */
size_t len;
long pos;
ibuflen = CtdlDecodeQuotedPrintable(ibuf, istr, len);
}
else {
- strcpy(ibuf, istr); /**< unknown encoding */
+ strcpy(ibuf, istr); /* unknown encoding */
ibuflen = strlen(istr);
}
remove_token(end, 0, '?');
strcpy(end, &end[1]);
- snprintf(newbuf, sizeof newbuf, "%s%s%s", buf, osav, end);
- strcpy(buf, newbuf);
+ snprintf(newbuf, sizeof newbuf, "%s%s%s", *buf, osav, end);
+ strcpy(*buf, newbuf);
+
free(osav);
iconv_close(ic);
}
remove_token(end, 0, '?');
strcpy(end, &end[1]);
- snprintf(newbuf, sizeof newbuf, "%s(unreadable)%s", buf, end);
- strcpy(buf, newbuf);
+ snprintf(newbuf, sizeof newbuf, "%s(unreadable)%s", *buf, end);
+ strcpy(*buf, newbuf);
}
free(isav);
}
#else
-inline void utf8ify_rfc822_string(char *a){};
+inline void utf8ify_rfc822_string(char **a){};
#endif
-/**
- * \brief RFC2047-encode a header field if necessary.
- * If no non-ASCII characters are found, the string
- * will be copied verbatim without encoding.
+/*
+ * RFC2047-encode a header field if necessary.
+ * If no non-ASCII characters are found, the string will be copied verbatim without encoding.
+ * Returns encoded length; -1 if non success.
*
- * \param target Target buffer.
- * \param maxlen Maximum size of target buffer.
- * \param source Source string to be encoded.
- * \param SourceLen Length of the source string
- * \returns encoded length; -1 if non success.
+ * target Target buffer.
+ * maxlen Maximum size of target buffer.
+ * source Source string to be encoded.
+ * SourceLen Length of the source string
*/
int webcit_rfc2047encode(char *target, int maxlen, char *source, long SourceLen)
{