* check the buffersize while base64 encoding; and adjust it if needed.
authorWilfried Göesgens <willi@citadel.org>
Sun, 30 Sep 2007 21:20:23 +0000 (21:20 +0000)
committerWilfried Göesgens <willi@citadel.org>
Sun, 30 Sep 2007 21:20:23 +0000 (21:20 +0000)
* double-pointer the output param, so we can realoc it.

webcit/messages.c
webcit/sieve.c
webcit/tools.c
webcit/webcit.h

index fbf3914d2fa2e34706f694eee4d8ccf1b6474587..abe91b05e77848873b194289ddebbca617a6d6da 100644 (file)
@@ -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);
index 3110acd7cb2d20ce3d3034ed294fcd043c55583f..d2adb28d4adbe5cf656cda93cea00fcd48450155 100644 (file)
@@ -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);
 }
 
 
index 6ee7f3cbe0e1e2acdf201e7a53fcda87f74610cb..c0a6b49c7126a4bcc1e36aedfc19c2584cc7ee05 100644 (file)
@@ -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 <!: %s\n", dest);
+               return(-1);
+       }
+//     lprintf (CTDL_DEBUG,"test <: %d; %s\n", len, dest);
        return(len);
 }
+//*/
 
 
 /**
@@ -385,14 +397,18 @@ void sleeeeeeeeeep(int seconds)
  * \param dest encrypted string
  * \param source the string to encrypt
  * \param sourcelen the length of the source data (may contain string terminators)
+ * \return the length of the encoded string.
  */
 
-void CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen, int linebreaks)
+size_t CtdlEncodeBase64(char **pdest, const char *source, size_t sourcelen, size_t *destlen, int linebreaks)
 {
        int i, hiteof = FALSE;
        int spos = 0;
        int dpos = 0;
        int thisline = 0;
+       char *dest;
+
+       dest = *pdest;
 
        /**  Fill dtable with character encodings.  */
 
@@ -442,11 +458,31 @@ void CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen, int line
                                }
                        }
                        for (i = 0; i < 4; i++) {
+                               if (dpos > *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;
 }
 
 
index c76709cd4be084058fd1171a8a256955afd08a2b..0ae981222f96f3a59d8bb16ba84a2b5dc9ca82b5 100644 (file)
@@ -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);