summaryrefslogtreecommitdiffstats
path: root/Utilities/cmcurl/lib/base64.c
diff options
context:
space:
mode:
Diffstat (limited to 'Utilities/cmcurl/lib/base64.c')
-rw-r--r--Utilities/cmcurl/lib/base64.c80
1 files changed, 37 insertions, 43 deletions
diff --git a/Utilities/cmcurl/lib/base64.c b/Utilities/cmcurl/lib/base64.c
index 960a1ca..52654c2 100644
--- a/Utilities/cmcurl/lib/base64.c
+++ b/Utilities/cmcurl/lib/base64.c
@@ -18,6 +18,8 @@
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
+ * SPDX-License-Identifier: curl
+ *
***************************************************************************/
/* Base64 encoding/decoding */
@@ -41,10 +43,11 @@
#include "memdebug.h"
/* ---- Base64 Encoding/Decoding Table --- */
+/* Padding character string starts at offset 64. */
static const char base64[]=
- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
-/* The Base 64 encoding with an URL and filename safe alphabet, RFC 4648
+/* The Base 64 encoding with a URL and filename safe alphabet, RFC 4648
section 5 */
static const char base64url[]=
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
@@ -52,25 +55,18 @@ static const char base64url[]=
static size_t decodeQuantum(unsigned char *dest, const char *src)
{
size_t padding = 0;
- const char *s, *p;
+ const char *s;
unsigned long i, x = 0;
for(i = 0, s = src; i < 4; i++, s++) {
if(*s == '=') {
- x = (x << 6);
+ x <<= 6;
padding++;
}
else {
- unsigned long v = 0;
- p = base64;
-
- while(*p && (*p != *s)) {
- v++;
- p++;
- }
-
- if(*p == *s)
- x = (x << 6) + v;
+ const char *p = strchr(base64, *s);
+ if(p)
+ x = (x << 6) + curlx_uztoul(p - base64);
else
return 0;
}
@@ -107,11 +103,11 @@ CURLcode Curl_base64_decode(const char *src,
unsigned char **outptr, size_t *outlen)
{
size_t srclen = 0;
- size_t length = 0;
size_t padding = 0;
size_t i;
size_t numQuantums;
size_t rawlen = 0;
+ const char *padptr;
unsigned char *pos;
unsigned char *newstr;
@@ -124,19 +120,17 @@ CURLcode Curl_base64_decode(const char *src,
return CURLE_BAD_CONTENT_ENCODING;
/* Find the position of any = padding characters */
- while((src[length] != '=') && src[length])
- length++;
-
- /* A maximum of two = padding characters is allowed */
- if(src[length] == '=') {
+ padptr = strchr(src, '=');
+ if(padptr) {
padding++;
- if(src[length + 1] == '=')
+ /* A maximum of two = padding characters is allowed */
+ if(padptr[1] == '=')
padding++;
- }
- /* Check the = padding characters weren't part way through the input */
- if(length + padding != srclen)
- return CURLE_BAD_CONTENT_ENCODING;
+ /* Check the = padding characters weren't part way through the input */
+ if(padptr + padding != src + srclen)
+ return CURLE_BAD_CONTENT_ENCODING;
+ }
/* Calculate the number of quantums */
numQuantums = srclen / 4;
@@ -144,7 +138,7 @@ CURLcode Curl_base64_decode(const char *src,
/* Calculate the size of the decoded string */
rawlen = (numQuantums * 3) - padding;
- /* Allocate our buffer including room for a zero terminator */
+ /* Allocate our buffer including room for a null-terminator */
newstr = malloc(rawlen + 1);
if(!newstr)
return CURLE_OUT_OF_MEMORY;
@@ -185,6 +179,7 @@ static CURLcode base64_encode(const char *table64,
char *output;
char *base64data;
const char *indata = inputbuff;
+ const char *padstr = &table64[64]; /* Point to padding string. */
*outptr = NULL;
*outlen = 0;
@@ -222,27 +217,30 @@ static CURLcode base64_encode(const char *table64,
switch(inputparts) {
case 1: /* only one byte read */
- msnprintf(output, 5, "%c%c==",
- table64[obuf[0]],
- table64[obuf[1]]);
+ i = msnprintf(output, 5, "%c%c%s%s",
+ table64[obuf[0]],
+ table64[obuf[1]],
+ padstr,
+ padstr);
break;
case 2: /* two bytes read */
- msnprintf(output, 5, "%c%c%c=",
- table64[obuf[0]],
- table64[obuf[1]],
- table64[obuf[2]]);
+ i = msnprintf(output, 5, "%c%c%c%s",
+ table64[obuf[0]],
+ table64[obuf[1]],
+ table64[obuf[2]],
+ padstr);
break;
default:
- msnprintf(output, 5, "%c%c%c%c",
- table64[obuf[0]],
- table64[obuf[1]],
- table64[obuf[2]],
- table64[obuf[3]]);
+ i = msnprintf(output, 5, "%c%c%c%c",
+ table64[obuf[0]],
+ table64[obuf[1]],
+ table64[obuf[2]],
+ table64[obuf[3]]);
break;
}
- output += 4;
+ output += i;
}
/* Zero terminate */
@@ -270,8 +268,6 @@ static CURLcode base64_encode(const char *table64,
* Returns CURLE_OK on success, otherwise specific error code. Function
* output shall not be considered valid unless CURLE_OK is returned.
*
- * When encoded data length is 0, returns NULL in *outptr.
- *
* @unittest: 1302
*/
CURLcode Curl_base64_encode(const char *inputbuff, size_t insize,
@@ -293,8 +289,6 @@ CURLcode Curl_base64_encode(const char *inputbuff, size_t insize,
* Returns CURLE_OK on success, otherwise specific error code. Function
* output shall not be considered valid unless CURLE_OK is returned.
*
- * When encoded data length is 0, returns NULL in *outptr.
- *
* @unittest: 1302
*/
CURLcode Curl_base64url_encode(const char *inputbuff, size_t insize,