#include "../curl_setup.h"
#include <curl/curl.h>
#include "warnless.h"
#include "base64.h"
#ifdef BUILDING_LIBCURL
#include "../curl_memory.h"
#endif
#include "../memdebug.h"
const char Curl_base64encdec[]=
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const char base64url[]=
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
static const unsigned char decodetable[] =
{ 62, 255, 255, 255, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255,
255, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255, 255, 26, 27, 28,
29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51 };
CURLcode curlx_base64_decode(const char *src,
unsigned char **outptr, size_t *outlen)
{
size_t srclen = 0;
size_t padding = 0;
size_t i;
size_t numQuantums;
size_t fullQuantums;
size_t rawlen = 0;
unsigned char *pos;
unsigned char *newstr;
unsigned char lookup[256];
*outptr = NULL;
*outlen = 0;
srclen = strlen(src);
if(!srclen || srclen % 4)
return CURLE_BAD_CONTENT_ENCODING;
while(src[srclen - 1 - padding] == '=') {
padding++;
if(padding > 2)
return CURLE_BAD_CONTENT_ENCODING;
}
numQuantums = srclen / 4;
fullQuantums = numQuantums - (padding ? 1 : 0);
rawlen = (numQuantums * 3) - padding;
newstr = malloc(rawlen + 1);
if(!newstr)
return CURLE_OUT_OF_MEMORY;
pos = newstr;
memset(lookup, 0xff, sizeof(lookup));
memcpy(&lookup['+'], decodetable, sizeof(decodetable));
for(i = 0; i < fullQuantums; i++) {
unsigned char val;
unsigned int x = 0;
int j;
for(j = 0; j < 4; j++) {
val = lookup[(unsigned char)*src++];
if(val == 0xff)
goto bad;
x = (x << 6) | val;
}
pos[2] = x & 0xff;
pos[1] = (x >> 8) & 0xff;
pos[0] = (x >> 16) & 0xff;
pos += 3;
}
if(padding) {
unsigned char val;
unsigned int x = 0;
int j;
size_t padc = 0;
for(j = 0; j < 4; j++) {
if(*src == '=') {
x <<= 6;
src++;
if(++padc > padding)
goto bad;
}
else {
val = lookup[(unsigned char)*src++];
if(val == 0xff)
goto bad;
x = (x << 6) | val;
}
}
if(padding == 1)
pos[1] = (x >> 8) & 0xff;
pos[0] = (x >> 16) & 0xff;
pos += 3 - padding;
}
*pos = '\0';
*outptr = newstr;
*outlen = rawlen;
return CURLE_OK;
bad:
free(newstr);
return CURLE_BAD_CONTENT_ENCODING;
}
static CURLcode base64_encode(const char *table64,
unsigned char padbyte,
const char *inputbuff, size_t insize,
char **outptr, size_t *outlen)
{
char *output;
char *base64data;
const unsigned char *in = (const unsigned char *)inputbuff;
*outptr = NULL;
*outlen = 0;
if(!insize)
return CURLE_OK;
DEBUGASSERT(insize <= CURL_MAX_BASE64_INPUT);
if(insize > CURL_MAX_BASE64_INPUT)
return CURLE_TOO_LARGE;
base64data = output = malloc((insize + 2) / 3 * 4 + 1);
if(!output)
return CURLE_OUT_OF_MEMORY;
while(insize >= 3) {
*output++ = table64[ in[0] >> 2 ];
*output++ = table64[ ((in[0] & 0x03) << 4) | (in[1] >> 4) ];
*output++ = table64[ ((in[1] & 0x0F) << 2) | ((in[2] & 0xC0) >> 6) ];
*output++ = table64[ in[2] & 0x3F ];
insize -= 3;
in += 3;
}
if(insize) {
*output++ = table64[ in[0] >> 2 ];
if(insize == 1) {
*output++ = table64[ ((in[0] & 0x03) << 4) ];
if(padbyte) {
*output++ = padbyte;
*output++ = padbyte;
}
}
else {
*output++ = table64[ ((in[0] & 0x03) << 4) | ((in[1] & 0xF0) >> 4) ];
*output++ = table64[ ((in[1] & 0x0F) << 2) ];
if(padbyte)
*output++ = padbyte;
}
}
*output = '\0';
*outptr = base64data;
*outlen = (size_t)(output - base64data);
return CURLE_OK;
}
CURLcode curlx_base64_encode(const char *inputbuff, size_t insize,
char **outptr, size_t *outlen)
{
return base64_encode(Curl_base64encdec, '=',
inputbuff, insize, outptr, outlen);
}
CURLcode curlx_base64url_encode(const char *inputbuff, size_t insize,
char **outptr, size_t *outlen)
{
return base64_encode(base64url, 0, inputbuff, insize, outptr, outlen);
}