From 9a1755264f2e8e373c8fe867c88cf7df83211d66 Mon Sep 17 00:00:00 2001 From: Steve Holme Date: Sat, 2 Nov 2013 17:00:00 +0000 Subject: [PATCH] getpart: Fixed base64 encoded parts following commit e17c1b25bc33eb --- tests/server/getpart.c | 88 ++++++++++++++++++++++++++++-------------- 1 file changed, 59 insertions(+), 29 deletions(-) diff --git a/tests/server/getpart.c b/tests/server/getpart.c index b00e8521e..654587358 100644 --- a/tests/server/getpart.c +++ b/tests/server/getpart.c @@ -152,46 +152,27 @@ static int appenddata(char **dst_buf, /* dest buffer */ char *src_buf, /* source buffer */ int src_b64) /* != 0 if source is base64 encoded */ { - size_t need_alloc, src_len; - union { - unsigned char *as_uchar; - char *as_char; - } buf64; + size_t need_alloc = 0; + size_t src_len = strlen(src_buf); - src_len = strlen(src_buf); if(!src_len) return GPE_OK; - buf64.as_char = NULL; + need_alloc = src_len + *dst_len + 1; if(src_b64) { - /* base64 decode the given buffer */ - int error = (int) Curl_base64_decode(src_buf, &buf64.as_uchar, &src_len); - if(error) - return GPE_OUT_OF_MEMORY; - src_buf = buf64.as_char; - if(!src_len || !src_buf) { - /* - ** currently there is no way to tell apart an OOM condition in - ** Curl_base64_decode() from zero length decoded data. For now, - ** let's just assume it is an OOM condition, currently we have - ** no input for this function that decodes to zero length data. - */ - if(buf64.as_char) - free(buf64.as_char); - return GPE_OUT_OF_MEMORY; - } - } + if(src_buf[src_len - 1] == '\r') + src_len--; - need_alloc = src_len + *dst_len + 1; + if(src_buf[src_len - 1] == '\n') + src_len--; + } /* enlarge destination buffer if required */ if(need_alloc > *dst_alloc) { size_t newsize = need_alloc * 2; char *newptr = realloc(*dst_buf, newsize); if(!newptr) { - if(buf64.as_char) - free(buf64.as_char); return GPE_OUT_OF_MEMORY; } *dst_alloc = newsize; @@ -203,8 +184,43 @@ static int appenddata(char **dst_buf, /* dest buffer */ *dst_len += src_len; *(*dst_buf + *dst_len) = '\0'; - if(buf64.as_char) - free(buf64.as_char); + return GPE_OK; +} + +static int decodedata(char **buf, /* dest buffer */ + size_t *len) /* dest buffer data length */ +{ + int error = 0; + char *buf64 = NULL; + size_t src_len = 0; + + if(!*len) + return GPE_OK; + + /* base64 decode the given buffer */ + error = (int) Curl_base64_decode(*buf, (unsigned char **) &buf64, &src_len); + if(error) + return GPE_OUT_OF_MEMORY; + + if(!src_len) { + /* + ** currently there is no way to tell apart an OOM condition in + ** Curl_base64_decode() from zero length decoded data. For now, + ** let's just assume it is an OOM condition, currently we have + ** no input for this function that decodes to zero length data. + */ + if(buf64) + free(buf64); + + return GPE_OUT_OF_MEMORY; + } + + /* memcpy to support binary blobs */ + memcpy(*buf, buf64, src_len); + *len = src_len; + *(*buf + src_len) = '\0'; + + free(buf64); return GPE_OK; } @@ -308,6 +324,13 @@ int getpart(char **outbuf, size_t *outlen, if(in_wanted_part) { /* end of wanted part */ in_wanted_part = 0; + + /* Do we need to base64 decode the data? */ + if(base64) { + error = decodedata(outbuf, outlen); + if(error) + return error; + } break; } } @@ -318,6 +341,13 @@ int getpart(char **outbuf, size_t *outlen, if(in_wanted_part) { /* end of wanted part */ in_wanted_part = 0; + + /* Do we need to base64 decode the data? */ + if(base64) { + error = decodedata(outbuf, outlen); + if(error) + return error; + } break; } }