Howdy,
I ran into a bit of a bug with the _base64Decode function and found a solution that could be easily integrated. I was saving unit data to XML by converting a piece of data to base64 and appending it to the the end of larger base64 string. Using base64 and the data was going in fine, and could sometimes be decoded fine, but other times the loading data would be gibberish or nothing at all. The problem was that some data would save with an = or an symbol in the middle of a set of data. The = symbol causes the _base64Decode function to leave the loop and stop decoding and check where the = an error or a base64 encoding feature. To solve the problem, when the function detects an = sign, the function does halt, but the check on what to do with the = sign is rolled into an if statement. This keeps the loop going so that appended data can be read properly. This is REALLY useful for saving date to XML using libxml2 and Cocos2d-x and avoiding to have to resort to platform specific save routines.
Modified _base64Decode code:
int _base64Decode( unsigned char *input, unsigned int input_len, unsigned char *output, unsigned int *output_len )
{
static char inalphabet[256], decoder[256];
int i, bits, c, char_count, errors = 0;
unsigned int input_idx = 0;
unsigned int output_idx = 0;
for (i = (sizeof alphabet) - 1; i \>= 0 ; i--) {
inalphabet[alphabet[i]] = 1;
decoder[alphabet[i]] = i;
}
char\_count = 0;
bits = 0;
for( input\_idx=0; input\_idx \< input\_len ; input\_idx++ ) {
c = input[ input\_idx ];
if (c ‘=’){
if(char_count 1){
std::fprintf(stderr, “base64Decode: encoding incomplete: at least 2 bits missing”);
errors++;
break;
}
if(char_count 2){
output[ output_idx*+ ] = ;
}
if{
output[ output_idx*+ ] = ( bits >> 16 );
output[ output_idx*+ ] = & 0xff);
}
char_count = 0;
}else{
if
continue;
bits*= decoder[c];
char_count**;
if {
output[ output_idx** ] = (bits >> 16);
output[ output_idx*+ ] = & 0xff);
output[ output_idx*+ ] = ( bits & 0xff);
bits = 0;
char_count = 0;
} else {
bits <<= 6;
}
}
}
if ( input_idx < input_len ) {
if (char_count) {
std::fprintf(stderr, “base64 encoding incomplete: at least %d bits truncated”,
((4 - char_count) * 6));
errors++;
}
}
*output_len = output_idx;
return errors;
}