This is important to allow use of reformime in CGI scripts
that handle uploads, since web-browsers typically POST using binary
rather than base64, 7bit, or 8bit.

Signed-off-by: Joshua Judson Rosen <[email protected]>
---
 mailutils/reformime.c |   43 +++++++++++++++++++++++++++++++++++++++----
 1 file changed, 39 insertions(+), 4 deletions(-)

diff --git a/mailutils/reformime.c b/mailutils/reformime.c
index 8e7d455..507fc06 100644
--- a/mailutils/reformime.c
+++ b/mailutils/reformime.c
@@ -138,6 +138,9 @@ static int parse(const char *boundary, char **argv)
                } else {
                        /* No, process one non-multipart section */
                        char *end;
+                       char end2[4] = ""; /* buffer for an EOL-string */
+                       size_t end_bufsz;
+                       ssize_t end_len, end2_len;
                        pid_t pid = pid;
                        FILE *fp;
 
@@ -181,24 +184,56 @@ static int parse(const char *boundary, char **argv)
 
                        /* write to fp */
                        end = NULL;
+                       end_bufsz = 0;
+                       end2_len = 0;
                        if (0 == strcasecmp(encoding, "base64")) {
                                read_base64(stdin, fp, '-');
                        } else
                        if (0 != strcasecmp(encoding, "7bit")
                         && 0 != strcasecmp(encoding, "8bit")
+                        && 0 != strcasecmp(encoding, "binary")
                        ) {
-                               /* quoted-printable, binary, user-defined are 
unsupported so far */
+                               /* quoted-printable and user-defined are 
unsupported so far */
                                bb_error_msg_and_die("encoding '%s' not 
supported", encoding);
                        } else {
-                               /* plain 7bit or 8bit */
-                               while ((end = xmalloc_fgets(stdin)) != NULL) {
+                               /* plain 7bit, 8bit, or binary */
+                               while ((end_len = getline(&end, &end_bufsz, 
stdin)) > 0) {
                                        if ('-' == end[0]
                                         && '-' == end[1]
                                         && strncmp(end + 2, boundary, 
boundary_len) == 0
                                        ) {
                                                break;
                                        }
-                                       fputs(end, fp);
+
+                                       if (end2_len) {
+                                               /* If we got here, this line 
isn't actually
+                                                  the boundary-marker; so 
flush out any EOL
+                                                  bytes that are buffered from 
the last line;
+                                                  we want to discard the 
necessary EOL between
+                                                  the data and the boundary, 
but only that EOL.
+                                                */
+                                               fwrite(end2, sizeof (char), 
end2_len, fp);
+                                               end2_len = 0;
+                                       }
+
+                                       /* Buffer any EOL bytes at the end of 
this line
+                                          until the next iteration--if the 
next line is
+                                          the boundary-marker, then the only 
reason that
+                                          this EOL is here is to put the 
boundary-marker
+                                          on its own line, and we should 
therefor discard
+                                          this EOL:
+                                        */
+                                       if (end_len >= 1 && end[end_len-1] == 
'\n') {
+                                               end2_len++;
+
+                                               if (end_len >= 2 && 
end[end_len-2] == '\r') {
+                                                       end2_len++;
+                                               }
+                                       }
+                                       strncpy (end2, end + end_len - 
end2_len, sizeof end2);
+
+                                       fwrite(end, sizeof (char), end_len - 
end2_len, fp);
+
                                }
                        }
                        fclose(fp);
-- 
1.7.10.4

_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to