From b2c413ed12a1c049271b273b3b9f618450e47709 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Wilfried=20G=C3=B6esgens?= Date: Sun, 30 Sep 2007 21:20:23 +0000 Subject: [PATCH] * check the buffersize while base64 encoding; and adjust it if needed. * double-pointer the output param, so we can realoc it. --- webcit/messages.c | 5 ++-- webcit/sieve.c | 9 ++++--- webcit/tools.c | 61 +++++++++++++++++++++++++++++++++++++++++------ webcit/webcit.h | 2 +- 4 files changed, 64 insertions(+), 13 deletions(-) diff --git a/webcit/messages.c b/webcit/messages.c index fbf3914d2..abe91b05e 100644 --- a/webcit/messages.c +++ b/webcit/messages.c @@ -2755,6 +2755,7 @@ void post_mime_to_server(void) { struct wc_attachment *att; char *encoded; size_t encoded_length; + size_t encoded_strlen; /** RFC2045 requires this, and some clients look for it... */ serv_puts("MIME-Version: 1.0"); @@ -2794,7 +2795,7 @@ void post_mime_to_server(void) { encoded_length = ((att->length * 150) / 100); encoded = malloc(encoded_length); if (encoded == NULL) break; - CtdlEncodeBase64(encoded, att->data, att->length, 1); + encoded_strlen = CtdlEncodeBase64(&encoded, att->data, att->length, &encoded_length, 1); serv_printf("--%s", boundary); serv_printf("Content-type: %s", att->content_type); @@ -2802,7 +2803,7 @@ void post_mime_to_server(void) { "filename=\"%s\"", att->filename); serv_puts("Content-transfer-encoding: base64"); serv_puts(""); - serv_write(encoded, strlen(encoded)); + serv_write(encoded, encoded_strlen); serv_puts(""); serv_puts(""); free(encoded); diff --git a/webcit/sieve.c b/webcit/sieve.c index 3110acd7c..d2adb28d4 100644 --- a/webcit/sieve.c +++ b/webcit/sieve.c @@ -425,9 +425,12 @@ void parse_fields_from_rule_editor(void) { char buf[256]; char fname[256]; char rule[2048]; - char encoded_rule[4096]; + char encoded_rule; char my_addresses[4096]; + long encoded_len; + encoded_len = 4096; + encoded_rule = (char*) malloc (encoded_len); /* Enumerate my email addresses in case they are needed for a vacation rule */ my_addresses[0] = 0; serv_puts("GVEA"); @@ -506,7 +509,7 @@ void parse_fields_from_rule_editor(void) { redirect, automsg, final ); - CtdlEncodeBase64(encoded_rule, rule, strlen(rule)+1, 0); + CtdlEncodeBase64(&encoded_rule, rule, strlen(rule)+1, &encoded_len, 0); serv_printf("# WEBCIT_RULE|%d|%s|", i, encoded_rule); output_sieve_rule(hfield, compare, htext, sizecomp, sizeval, action, fileinto, redirect, automsg, final, my_addresses); @@ -518,7 +521,7 @@ void parse_fields_from_rule_editor(void) { serv_puts("stop;"); serv_puts("000"); - + free(encoded_rule); } diff --git a/webcit/tools.c b/webcit/tools.c index 6ee7f3cbe..c0a6b49c7 100644 --- a/webcit/tools.c +++ b/webcit/tools.c @@ -67,9 +67,9 @@ int num_tokens(char *source, char tok) */ long extract_token(char *dest, const char *source, int parmnum, char separator, int maxlen) { - const char *s; /* source */ - int len = 0; /* running total length of extracted string */ - int current_token = 0; /* token currently being processed */ + const char *s; //* source * / + int len = 0; //* running total length of extracted string * / + int current_token = 0; //* token currently being processed * / s = source; @@ -77,6 +77,8 @@ long extract_token(char *dest, const char *source, int parmnum, char separator, return(-1); } +// cit_backtrace(); +// lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source); dest[0] = 0; if (s == NULL) { @@ -87,17 +89,27 @@ long extract_token(char *dest, const char *source, int parmnum, char separator, if (*s == separator) { ++current_token; } - else if ( (current_token == parmnum) && (len < maxlen) ) { + if ( (current_token == parmnum) && + (*s != separator) && + (len < maxlen) ) { dest[len] = *s; ++len; } + else if ((current_token > parmnum) || (len >= maxlen)) { + break; + } ++s; } - dest[len] = 0; - if (current_token < parmnum) return(-1); + dest[len] = '\0'; + if (current_token < parmnum) { +// lprintf (CTDL_DEBUG,"test *destlen) + { + int newlen; + char *newbuf; + newlen = *destlen + *destlen / 2; + newbuf = (char*) malloc(newlen); + memcpy(newbuf, dest, *destlen); + *pdest = dest = newbuf; + *destlen = newlen; + } dest[dpos++] = ogroup[i]; dest[dpos] = 0; } thisline += 4; if ( (linebreaks) && (thisline > 70) ) { + if (dpos + 3 > *destlen) + { + int newlen; + char *newbuf; + newlen = *destlen + *destlen / 2; + newbuf = (char*) malloc(newlen); + memcpy(newbuf, dest, *destlen); + *pdest = dest = newbuf; + *destlen = newlen; + } dest[dpos++] = '\r'; dest[dpos++] = '\n'; dest[dpos] = 0; @@ -455,11 +491,22 @@ void CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen, int line } } if ( (linebreaks) && (thisline > 70) ) { + if (dpos + 3 > *destlen) + { + int newlen; + char *newbuf; + newlen = *destlen + 5; + newbuf = (char*) malloc(newlen); + memcpy(newbuf, dest, *destlen); + *pdest = dest = newbuf; + *destlen = newlen; + } dest[dpos++] = '\r'; dest[dpos++] = '\n'; dest[dpos] = 0; thisline = 0; } + return dpos; } diff --git a/webcit/webcit.h b/webcit/webcit.h index c76709cd4..0ae981222 100644 --- a/webcit/webcit.h +++ b/webcit/webcit.h @@ -634,7 +634,7 @@ void do_tasks_view(void); void free_calendar_buffer(void); void calendar_summary_view(void); int load_msg_ptrs(char *servcmd, int with_headers); -void CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen, int linebreaks); +size_t CtdlEncodeBase64(char **dest, const char *source, size_t sourcelen, size_t *destlen, int linebreaks); int CtdlDecodeBase64(char *dest, const char *source, size_t length); void free_attachments(struct wcsession *sess); void free_march_list(struct wcsession *wcf); -- 2.30.2