diff options
Diffstat (limited to 'tests/server/getpart.c')
-rw-r--r-- | tests/server/getpart.c | 105 |
1 files changed, 67 insertions, 38 deletions
diff --git a/tests/server/getpart.c b/tests/server/getpart.c index a3b5f2fb..f773dd05 100644 --- a/tests/server/getpart.c +++ b/tests/server/getpart.c @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et al. + * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -19,10 +19,7 @@ * KIND, either express or implied. * ***************************************************************************/ - -#define CURL_NO_OLDIES - -#include "setup.h" +#include "server_setup.h" #include "getpart.h" @@ -31,7 +28,7 @@ versions instead */ #include "curlx.h" /* from the private lib dir */ -/* just to please base64.h we create a fake struct */ +/* just to please curl_base64.h we create a fake struct */ struct SessionHandle { int fake; }; @@ -49,7 +46,7 @@ struct SessionHandle { #ifdef DEBUG_GETPART #define show(x) printf x #else -#define show(x) +#define show(x) Curl_nop_stmt #endif #if defined(_MSC_VER) && defined(_DLL) @@ -61,6 +58,9 @@ curl_free_callback Curl_cfree = (curl_free_callback)free; curl_realloc_callback Curl_crealloc = (curl_realloc_callback)realloc; curl_strdup_callback Curl_cstrdup = (curl_strdup_callback)strdup; curl_calloc_callback Curl_ccalloc = (curl_calloc_callback)calloc; +#if defined(WIN32) && defined(UNICODE) +curl_wcsdup_callback Curl_cwcsdup = (curl_wcsdup_callback)_wcsdup; +#endif #if defined(_MSC_VER) && defined(_DLL) # pragma warning(default:4232) /* MSVC extension, dllimport identity */ @@ -152,44 +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 */ - src_len = Curl_base64_decode(src_buf, &buf64.as_uchar); - 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; @@ -201,8 +184,42 @@ 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; + unsigned char *buf64 = NULL; + size_t src_len = 0; + + if(!*len) + return GPE_OK; + + /* base64 decode the given buffer */ + error = (int) Curl_base64_decode(*buf, &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. + */ + 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; } @@ -306,6 +323,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; } } @@ -316,6 +340,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; } } @@ -403,15 +434,13 @@ int getpart(char **outbuf, size_t *outlen, } /* while */ - if(buffer) - free(buffer); + free(buffer); if(error != GPE_OK) { if(error == GPE_END_OF_FILE) error = GPE_OK; else { - if(*outbuf) - free(*outbuf); + free(*outbuf); *outbuf = NULL; *outlen = 0; } |