library/src/utility/BP-StringUtilities.cc
// decodes a buffer which has been encoded with base64
decodes a buffer which has been encoded with base64
decodes a buffer which has been encoded with base64
P_BP_BASE64_DECODED_DATA BP_Base64Decode(char *src, size_t len)
{
	if(!src)
		return NULL;
	if(!len)
		return NULL;
	unsigned char base64_table[64];
	memcpy(&base64_table, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", bpstrlen("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"));
	unsigned char dtable[256] = {0};
	unsigned char *out = NULL;
	unsigned char *pos = NULL;
	unsigned char in[4] = {0};
	unsigned char block[4] = {0};
	unsigned char tmp = 0x00;
	size_t i     = 0;
	size_t count = 0;
	size_t olen  = 0;
	memset(dtable, 0x80, 256);
	for (i = 0; i < sizeof(base64_table); i++)
	{
		dtable[base64_table[i]] = i;
	}
	dtable['='] = 0;
	count = 0;
	for (i = 0; i < len; i++)
	{
		if (dtable[src[i]] != 0x80)
			count++;
	}
	if (count % 4)
		return NULL;
	olen = count / 4 * 3;
	pos = out = (unsigned char *) bpcalloc(count, 1);
	if (out == NULL)
		return NULL;
	count = 0;
	for (i = 0; i < len; i++)
	{
		tmp = dtable[src[i]];
		if (tmp == 0x80)
				continue;
		in[count] = src[i];
		block[count] = tmp;
		count++;
		if (count == 4)
		{
				*pos++ = (block[0] << 2) | (block[1] >> 4);
				*pos++ = (block[1] << 4) | (block[2] >> 2);
				*pos++ = (block[2] << 6) | block[3];
				count = 0;
		}
	}
	if (pos > out)
	{
		if (in[2] == '=')
			pos -= 2;
		else if (in[3] == '=')
			pos--;
	}
	P_BP_BASE64_DECODED_DATA decoded = (P_BP_BASE64_DECODED_DATA) bpcalloc(sizeof(BP_BASE64_DECODED_DATA), 1);
	decoded->buff        = out;
	decoded->buff_length = pos - out;
	return decoded;
}
P_BP_BASE64_DECODED_DATA BP_Base64Decode ( char * src , size_t len ) { if ( ! src ) return NULL ; if ( ! len ) return NULL ; unsigned char base64_table [ 64 ] ; memcpy ( & base64_table , " " , bpstrlen ( " " ) ) ; unsigned char dtable [ 256 ] = { 0 } ; unsigned char * out = NULL ; unsigned char * pos = NULL ; unsigned char in [ 4 ] = { 0 } ; unsigned char block [ 4 ] = { 0 } ; unsigned char tmp = 0x00 ; size_t i = 0 ; size_t count = 0 ; size_t olen = 0 ; memset ( dtable , 0x80 , 256 ) ; for ( i = 0 ; i < sizeof ( base64_table ) ; i ++ ) { dtable [ base64_table [ i ] ] = i ; } dtable [ ' ' ] = 0 ; count = 0 ; for ( i = 0 ; i < len ; i ++ ) { if ( dtable [ src [ i ] ] != 0x80 ) count ++ ; } if ( count % 4 ) return NULL ; olen = count / 4 * 3 ; pos = out = ( unsigned char * ) bpcalloc ( count , 1 ) ; if ( out == NULL ) return NULL ; count = 0 ; for ( i = 0 ; i < len ; i ++ ) { tmp = dtable [ src [ i ] ] ; if ( tmp == 0x80 ) continue ; in [ count ] = src [ i ] ; block [ count ] = tmp ; count ++ ; if ( count == 4 ) { * pos ++ = ( block [ 0 ] << 2 ) | ( block [ 1 ] >> 4 ) ; * pos ++ = ( block [ 1 ] << 4 ) | ( block [ 2 ] >> 2 ) ; * pos ++ = ( block [ 2 ] << 6 ) | block [ 3 ] ; count = 0 ; } } if ( pos > out ) { if ( in [ 2 ] == ' ' ) pos -= 2 ; else if ( in [ 3 ] == ' ' ) pos -- ; } P_BP_BASE64_DECODED_DATA decoded = ( P_BP_BASE64_DECODED_DATA ) bpcalloc ( sizeof ( BP_BASE64_DECODED_DATA ) , 1 ) ; decoded -> buff = out ; decoded -> buff_length = pos - out ; return decoded ; }
decodes a buffer which has been encoded with base64
decodes a buffer which has been encoded with base64
// set the standard character lookup table // set counters // set decode table to 0x80 in all places // set decode table // create decoded data structure // set buffer and position in the new structure // return the usable structure