library/src/utility/BP-StringUtilities.cc
// encodes a buffer with base64 (must be freed)
encodes a buffer with base64 (must be freed)
encodes a buffer with base64 ( must be freed )
char * BP_Base64Encode(unsigned char *src, size_t len)
{
	unsigned char base64_table[64];
	memcpy(&base64_table, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", bpstrlen("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"));
	if(!src)
		return NULL;
	if(!len)
		return NULL;
	unsigned char *out = NULL;
	unsigned char *pos = NULL;
	unsigned char *end = NULL;
	unsigned char *in  = NULL;
	size_t olen = 0;
	int line_len = 0;
	olen = len * 4 / 3 + 4; 
	olen += olen / 72; 
	olen++; 
	out = (unsigned char *) bpcalloc(olen, 1);
	if (out == NULL)
		return NULL;
	end = src + len;
	in = src;
	pos = out;
	line_len = 0;
	while ( (end - in) >= 3)
	{
		*pos++ = base64_table[in[0] >> 2];
		*pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
		*pos++ = base64_table[((in[1] & 0x0f) << 2) | (in[2] >> 6)];
		*pos++ = base64_table[in[2]   & 0x3f];
		in       += 3;
		line_len += 4;
	}
	if (end - in)
	{
		*pos++ = base64_table[in[0] >> 2];
		if (end - in == 1)
		{
			*pos++ = base64_table[(in[0] & 0x03) << 4];
			*pos++ = '=';
		}
		else
		{
			*pos++ = base64_table[(  (in[0] & 0x03) << 4) |  (in[1] >> 4 )  ];
			*pos++ = base64_table[  (in[1] & 0x0f) <<   2  ];
		}
		*pos++ = '=';
	}
	*pos = '\0';
	return (char *) out;
}
char * BP_Base64Encode ( unsigned char * src , size_t len ) { unsigned char base64_table [ 64 ] ; memcpy ( & base64_table , " " , bpstrlen ( " " ) ) ; if ( ! src ) return NULL ; if ( ! len ) return NULL ; unsigned char * out = NULL ; unsigned char * pos = NULL ; unsigned char * end = NULL ; unsigned char * in = NULL ; size_t olen = 0 ; int line_len = 0 ; olen = len * 4 / 3 + 4 ; olen += olen / 72 ; olen ++ ; out = ( unsigned char * ) bpcalloc ( olen , 1 ) ; if ( out == NULL ) return NULL ; end = src + len ; in = src ; pos = out ; line_len = 0 ; while ( ( end - in ) >= 3 ) { * pos ++ = base64_table [ in [ 0 ] >> 2 ] ; * pos ++ = base64_table [ ( ( in [ 0 ] & 0x03 ) << 4 ) | ( in [ 1 ] >> 4 ) ] ; * pos ++ = base64_table [ ( ( in [ 1 ] & 0x0f ) << 2 ) | ( in [ 2 ] >> 6 ) ] ; * pos ++ = base64_table [ in [ 2 ] & 0x3f ] ; in += 3 ; line_len += 4 ; } if ( end - in ) { * pos ++ = base64_table [ in [ 0 ] >> 2 ] ; if ( end - in == 1 ) { * pos ++ = base64_table [ ( in [ 0 ] & 0x03 ) << 4 ] ; * pos ++ = ' ' ; } else { * pos ++ = base64_table [ ( ( in [ 0 ] & 0x03 ) << 4 ) | ( in [ 1 ] >> 4 ) ] ; * pos ++ = base64_table [ ( in [ 1 ] & 0x0f ) << 2 ] ; } * pos ++ = ' ' ; } * pos = ' \0 ' ; return ( char * ) out ; }
encodes a buffer with base64 (must be freed)
encodes a buffer with base64 ( must be freed )
// set the standard character lookup table // check for valid data // -- begin /* 3-byte blocks to 4-byte */ /* line feeds */ /* nul termination */ // create the return / output buffer // set a pointer to the end of the data // set pointer to the beginning of the data // set position as the beginning of the string // set the current line length to zero // loop from the beginning to the end of the string // this is the meat of the base64 operations /*
		if (line_len >= 72) {
			*pos++ = '\n';
			line_len = 0;
		}
		*/ // null truncate the output string (since pos is a pointer to outs end) // return pointer to the data