40
40
unsigned char* base64Decode(char const* in, unsigned& resultSize,
41
41
Boolean trimTrailingZeros) {
42
static Boolean haveInitedBase64DecodeTable = False;
43
if (!haveInitedBase64DecodeTable) {
42
if (in == NULL) return NULL; // sanity check
43
return base64Decode(in, strlen(in), resultSize, trimTrailingZeros);
46
unsigned char* base64Decode(char const* in, unsigned inSize,
48
Boolean trimTrailingZeros) {
49
static Boolean haveInitializedBase64DecodeTable = False;
50
if (!haveInitializedBase64DecodeTable) {
44
51
initBase64DecodeTable();
45
haveInitedBase64DecodeTable = True;
52
haveInitializedBase64DecodeTable = True;
48
55
unsigned char* out = (unsigned char*)strDupSize(in); // ensures we have enough space
50
int const jMax = strlen(in) - 3;
51
// in case "in" is not a multiple of 4 bytes (although it should be)
58
int const jMax = inSize - 3;
59
// in case "inSize" is not a multiple of 4 (although it should be)
52
60
for (int j = 0; j < jMax; j += 4) {
53
61
char inTmp[4], outTmp[4];
54
62
for (int i = 0; i < 4; ++i) {
55
63
inTmp[i] = in[i+j];
64
if (inTmp[i] == '=') ++paddingCount;
56
65
outTmp[i] = base64DecodeTable[(unsigned char)inTmp[i]];
57
if ((outTmp[i]&0x80) != 0) outTmp[i] = 0; // pretend the input was 'A'
66
if ((outTmp[i]&0x80) != 0) outTmp[i] = 0; // this happens only if there was an invalid character; pretend that it was 'A'
60
69
out[k++] = (outTmp[0]<<2) | (outTmp[1]>>4);