#include "SecBase64P.h"
#include <assert.h>
#include <string.h>
#ifndef B64_DOCUMENTATION_SKIP_SECTION
# define NUM_PLAIN_DATA_BYTES (3)
# define NUM_ENCODED_DATA_BYTES (4)
#endif
#if defined(_MSC_VER) && \
_MSC_VER < 1000
# pragma warning(disable : 4705)
#endif
static const char b64_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const signed char b64_indexes[] =
{
-1, -1, -1, -1, -1, -1, -1, -1
, -1, -1, -1, -1, -1, -1, -1, -1
, -1, -1, -1, -1, -1, -1, -1, -1
, -1, -1, -1, -1, -1, -1, -1, -1
, -1, -1, -1, -1, -1, -1, -1, -1
, -1, -1, -1, 62, -1, -1, -1, 63
, 52, 53, 54, 55, 56, 57, 58, 59
, 60, 61, -1, -1, -1, -1, -1, -1
, -1, 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, -1, -1, -1, -1, -1
, -1, 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, -1, -1, -1, -1, -1
, -1, -1, -1, -1, -1, -1, -1, -1
, -1, -1, -1, -1, -1, -1, -1, -1
, -1, -1, -1, -1, -1, -1, -1, -1
, -1, -1, -1, -1, -1, -1, -1, -1
, -1, -1, -1, -1, -1, -1, -1, -1
, -1, -1, -1, -1, -1, -1, -1, -1
, -1, -1, -1, -1, -1, -1, -1, -1
, -1, -1, -1, -1, -1, -1, -1, -1
, -1, -1, -1, -1, -1, -1, -1, -1
, -1, -1, -1, -1, -1, -1, -1, -1
, -1, -1, -1, -1, -1, -1, -1, -1
, -1, -1, -1, -1, -1, -1, -1, -1
, -1, -1, -1, -1, -1, -1, -1, -1
, -1, -1, -1, -1, -1, -1, -1, -1
, -1, -1, -1, -1, -1, -1, -1, -1
, -1, -1, -1, -1, -1, -1, -1, -1
};
static size_t SecBase64Encode_( unsigned char const *src
, size_t srcSize
, char *const dest
, size_t destLen
, unsigned lineLen
, SecBase64Result *rc)
{
size_t total = ((srcSize + (NUM_PLAIN_DATA_BYTES - 1)) / NUM_PLAIN_DATA_BYTES) * NUM_ENCODED_DATA_BYTES;
assert(NULL != rc);
*rc = kSecB64_R_OK;
if(lineLen > 0)
{
size_t numLines = (total + (lineLen - 1)) / lineLen;
total += 2 * (numLines - 1);
}
if(NULL == dest)
{
return total;
}
else if(destLen < total)
{
*rc = kSecB64_R_INSUFFICIENT_BUFFER;
return 0;
}
else
{
char *p = dest;
char *end = dest + destLen;
size_t len = 0;
for(; NUM_PLAIN_DATA_BYTES <= srcSize; srcSize -= NUM_PLAIN_DATA_BYTES)
{
char characters[NUM_ENCODED_DATA_BYTES];
characters[0] = (char)((src[0] & 0xfc) >> 2);
characters[1] = (char)(((src[0] & 0x03) << 4) + ((src[1] & 0xf0) >> 4));
characters[2] = (char)(((src[1] & 0x0f) << 2) + ((src[2] & 0xc0) >> 6));
characters[3] = (char)(src[2] & 0x3f);
#ifndef __WATCOMC__
assert(characters[0] >= 0 && characters[0] < 64);
assert(characters[1] >= 0 && characters[1] < 64);
assert(characters[2] >= 0 && characters[2] < 64);
assert(characters[3] >= 0 && characters[3] < 64);
#endif
src += NUM_PLAIN_DATA_BYTES;
*p++ = b64_chars[(unsigned char)characters[0]];
assert(NULL != strchr(b64_chars, *(p-1)));
++len;
assert(len != lineLen);
*p++ = b64_chars[(unsigned char)characters[1]];
assert(NULL != strchr(b64_chars, *(p-1)));
++len;
assert(len != lineLen);
*p++ = b64_chars[(unsigned char)characters[2]];
assert(NULL != strchr(b64_chars, *(p-1)));
++len;
assert(len != lineLen);
*p++ = b64_chars[(unsigned char)characters[3]];
assert(NULL != strchr(b64_chars, *(p-1)));
if( ++len == lineLen &&
p != end)
{
*p++ = '\r';
*p++ = '\n';
len = 0;
}
}
if(0 != srcSize)
{
unsigned char dummy[NUM_PLAIN_DATA_BYTES];
size_t i;
for(i = 0; i < srcSize; ++i)
{
dummy[i] = *src++;
}
for(; i < NUM_PLAIN_DATA_BYTES; ++i)
{
dummy[i] = '\0';
}
SecBase64Encode_(&dummy[0], NUM_PLAIN_DATA_BYTES, p, NUM_ENCODED_DATA_BYTES * (1 + 2), 0, rc);
for(p += 1 + srcSize; srcSize++ < NUM_PLAIN_DATA_BYTES; )
{
*p++ = '=';
}
}
return total;
}
}
static size_t SecBase64Decode_( char const *src
, size_t srcLen
, unsigned char *dest
, size_t destSize
, unsigned flags
, char const **badChar
, SecBase64Result *rc)
{
const size_t wholeChunks = (srcLen / NUM_ENCODED_DATA_BYTES);
const size_t remainderBytes = (srcLen % NUM_ENCODED_DATA_BYTES);
size_t maxTotal = (wholeChunks + (0 != remainderBytes)) * NUM_PLAIN_DATA_BYTES;
unsigned char *dest_ = dest;
((void)remainderBytes);
assert(NULL != badChar);
assert(NULL != rc);
*badChar = NULL;
*rc = kSecB64_R_OK;
if(NULL == dest)
{
return maxTotal;
}
else if(destSize < maxTotal)
{
*rc = kSecB64_R_INSUFFICIENT_BUFFER;
return 0;
}
else
{
char const *begin = src;
char const *const end = begin + srcLen;
size_t currIndex = 0;
size_t numPads = 0;
signed char indexes[NUM_ENCODED_DATA_BYTES];
for(; begin != end; ++begin)
{
const char ch = *begin;
if('=' == ch)
{
assert(currIndex < NUM_ENCODED_DATA_BYTES);
indexes[currIndex++] = '\0';
++numPads;
}
else
{
signed char ix = b64_indexes[(unsigned char)ch];
if(-1 == ix)
{
switch(ch)
{
case ' ':
case '\t':
case '\b':
case '\v':
if(kSecB64_F_STOP_ON_UNEXPECTED_WS & flags)
{
*rc = kSecB64_R_DATA_ERROR;
*badChar = begin;
return 0;
}
else
{
}
case '\r':
case '\n':
continue;
default:
if(kSecB64_F_STOP_ON_UNKNOWN_CHAR & flags)
{
*rc = kSecB64_R_DATA_ERROR;
*badChar = begin;
return 0;
}
else
{
continue;
}
}
}
else
{
numPads = 0;
assert(currIndex < NUM_ENCODED_DATA_BYTES);
indexes[currIndex++] = ix;
}
}
if(NUM_ENCODED_DATA_BYTES == currIndex)
{
unsigned char bytes[NUM_PLAIN_DATA_BYTES];
bytes[0] = (unsigned char)((indexes[0] << 2) + ((indexes[1] & 0x30) >> 4));
currIndex = 0;
*dest++ = bytes[0];
if(2 != numPads)
{
bytes[1] = (unsigned char)(((indexes[1] & 0xf) << 4) + ((indexes[2] & 0x3c) >> 2));
*dest++ = bytes[1];
if(1 != numPads)
{
bytes[2] = (unsigned char)(((indexes[2] & 0x3) << 6) + indexes[3]);
*dest++ = bytes[2];
}
}
if(0 != numPads)
{
break;
}
}
}
return (size_t)(dest - dest_);
}
}
size_t SecBase64Encode(void const *src, size_t srcSize, char *dest, size_t destLen)
{
SecBase64Result rc_;
return SecBase64Encode_((unsigned char const*)src, srcSize, dest, destLen, 0, &rc_);
}
size_t SecBase64Encode2( void const *src
, size_t srcSize
, char *dest
, size_t destLen
, unsigned flags
, int lineLen
, SecBase64Result *rc )
{
SecBase64Result rc_;
if(NULL == rc)
{
rc = &rc_;
}
switch(kSecB64_F_LINE_LEN_MASK & flags)
{
case kSecB64_F_LINE_LEN_USE_PARAM:
if(lineLen >= 0)
{
break;
}
case kSecB64_F_LINE_LEN_64:
lineLen = 64;
break;
case kSecB64_F_LINE_LEN_76:
lineLen = 76;
break;
default:
assert(!"Bad line length flag specified to SecBase64Encode2()");
case kSecB64_F_LINE_LEN_INFINITE:
lineLen = 0;
break;
}
assert(0 == (lineLen % 4));
return SecBase64Encode_((unsigned char const*)src, srcSize, dest, destLen, (unsigned)lineLen, rc);
}
size_t SecBase64Decode(char const *src, size_t srcLen, void *dest, size_t destSize)
{
char const *badChar_;
SecBase64Result rc_;
return SecBase64Decode_(src, srcLen, (unsigned char*)dest, destSize, kSecB64_F_STOP_ON_NOTHING, &badChar_, &rc_);
}
size_t SecBase64Decode2( char const *src
, size_t srcLen
, void *dest
, size_t destSize
, unsigned flags
, char const **badChar
, SecBase64Result *rc )
{
char const *badChar_;
SecBase64Result rc_;
if(NULL == badChar)
{
badChar = &badChar_;
}
if(NULL == rc)
{
rc = &rc_;
}
return SecBase64Decode_(src, srcLen, (unsigned char*)dest, destSize, flags, badChar, rc);
}