aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteve Holme <steve_holme@hotmail.com>2013-12-01 13:47:11 +0000
committerSteve Holme <steve_holme@hotmail.com>2013-12-01 13:59:47 +0000
commitf3ee587775c88a263a05fe7b5ee1c9380ff9f1a5 (patch)
tree8c8d097ec1b22771c0525272035ab3e4b95493ae
parentc92c30edbde3b7f7562c2bcb4a06626781c55f1d (diff)
base64: Post extended extended validation tidy up
Reduced the separate processing of the last quantum to be performed in the main decoding loop and renamed some variables for consistency.
-rw-r--r--lib/base64.c47
1 files changed, 19 insertions, 28 deletions
diff --git a/lib/base64.c b/lib/base64.c
index d0b49261a..af01f3a69 100644
--- a/lib/base64.c
+++ b/lib/base64.c
@@ -68,9 +68,13 @@ static size_t decodeQuantum(unsigned char *dest, const char *src)
}
}
- dest[2] = curlx_ultouc(x & 0xFFUL);
+ if(padding < 2)
+ dest[2] = curlx_ultouc(x & 0xFFUL);
+
x >>= 8;
- dest[1] = curlx_ultouc(x & 0xFFUL);
+ if(padding < 1)
+ dest[1] = curlx_ultouc(x & 0xFFUL);
+
x >>= 8;
dest[0] = curlx_ultouc(x & 0xFFUL);
@@ -94,23 +98,22 @@ static size_t decodeQuantum(unsigned char *dest, const char *src)
CURLcode Curl_base64_decode(const char *src,
unsigned char **outptr, size_t *outlen)
{
- size_t srcLen = 0;
+ size_t srclen = 0;
size_t length = 0;
- size_t equalsTerm = 0;
+ size_t padding = 0;
size_t i;
size_t result;
size_t numQuantums;
- unsigned char lastQuantum[3];
size_t rawlen = 0;
unsigned char *pos;
unsigned char *newstr;
*outptr = NULL;
*outlen = 0;
- srcLen = strlen(src);
+ srclen = strlen(src);
/* Check the length of the input string is valid */
- if(!srcLen || srcLen % 4)
+ if(!srclen || srclen % 4)
return CURLE_BAD_CONTENT_ENCODING;
/* Find the position of any = padding characters */
@@ -119,20 +122,20 @@ CURLcode Curl_base64_decode(const char *src,
/* A maximum of two = padding characters is allowed */
if(src[length] == '=') {
- equalsTerm++;
- if(src[length+equalsTerm] == '=')
- equalsTerm++;
+ padding++;
+ if(src[length + 1] == '=')
+ padding++;
}
/* Check the = padding characters weren't part way through the input */
- if(length + equalsTerm != srcLen)
+ if(length + padding != srclen)
return CURLE_BAD_CONTENT_ENCODING;
/* Calculate the number of quantums */
- numQuantums = srcLen / 4;
+ numQuantums = srclen / 4;
/* Calculate the size of the decoded string */
- rawlen = (numQuantums * 3) - equalsTerm;
+ rawlen = (numQuantums * 3) - padding;
/* Allocate our buffer including room for a zero terminator */
newstr = malloc(rawlen + 1);
@@ -141,9 +144,8 @@ CURLcode Curl_base64_decode(const char *src,
pos = newstr;
- /* Decode all but the last quantum (which may not decode to a
- multiple of 3 bytes) */
- for(i = 0; i < numQuantums - 1; i++) {
+ /* Decode the quantums */
+ for(i = 0; i < numQuantums; i++) {
result = decodeQuantum(pos, src);
if(!result) {
Curl_safefree(newstr);
@@ -155,19 +157,8 @@ CURLcode Curl_base64_decode(const char *src,
src += 4;
}
- /* Decode the last quantum */
- result = decodeQuantum(lastQuantum, src);
- if(!result) {
- Curl_safefree(newstr);
-
- return CURLE_BAD_CONTENT_ENCODING;
- }
-
- for(i = 0; i < 3 - equalsTerm; i++)
- pos[i] = lastQuantum[i];
-
/* Zero terminate */
- pos[i] = '\0';
+ *pos = '\0';
/* Return the decoded data */
*outptr = newstr;