I already used a lot of different functions to decode Base64 encoded char array in C. Right now, I am using this function to decode Base64 char array.
unsigned int b64_decode(const unsigned char* in, unsigned int in_len, unsigned char* out) {
unsigned int i=0, j=0, k=0, s[4];
for (i=0;i<in_len;i++) {
s[j++]=b64_int(*(in+i));
if (j==4) {
out[k+0] = ((s[0]&255)<<2)+((s[1]&0x30)>>4);
if (s[2]!=64) {
out[k+1] = ((s[1]&0x0F)<<4)+((s[2]&0x3C)>>2);
if ((s[3]!=64)) {
out[k+2] = ((s[2]&0x03)<<6)+(s[3]); k+=3;
} else {
k+=2;
}
} else {
k+=1;
}
j=0;
}
}
return k;
}
The problem is, that for a Base64 string like this QKSRjAEAAAAB3bW3rVpoJOA8bsb3eEuEEDiq, I am getting a string with only five characters, even the decoded text should be much longer. I tried to debug the code and it is because the result of the bit shift for the sixth character is 0, which unexpectedly ends the string. Online Base64 decoders decoded the mentioned string without problems.
I then want to convert the decoded string to hex. I am doing that like this:
for (int i = 0, j = 0; i < size; ++i, j += 2) {
sprintf(data_hex + j, "%02x", decoded_data[i] & 0xff);
}
Example of expected input: QKSRjAEAAAAB3bW3rVpoJOA8bsb3eEuEEDiq
Example of expected output: 40a4918c0100000001ddb5b7ad5a6824e03c6ec6f7784b841038aa
Thank you for your response.