I am trying to get my decoder code to work. I am using the example 64-bit encoded string from wikipedia, trying to reproduce the text they encoded.
#include <stdio.h>
//Convert raw binary character to the cb64 index
unsigned char get_cb64(unsigned char c){
if(c>='A' && c<='Z'){return c-'A';}
if(c>='a' && c<='z'){return c-'G';}
if(c>='0' && c<='9'){return c+4;}
if(c=='+'){return '>';}
if(c=='/'){return '?';}
else{return 0;}
}
void main(int argc, char** argv)
{
unsigned char* str = "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=";
//convert each binary character to its cb64 index
int size = 360;
int num_bytes = 8;
unsigned char str_cb64[size + 1];
int cb64_idx;
int i;
for(i=0; i < size; i++){
str_cb64[i]=get_cb64(str[i]);
}
str_cb64[size] = 0;
//convert blocks of 4 6 bit chars to 3 8 bit chars
int end_size = size*6/8;
unsigned char ascii_out[end_size];
int out_idx = 0;
int in_idx = 0;
while(in_idx < end_size/4){
ascii_out[out_idx] = str_cb64[in_idx+0] << 2 | str_cb64[in_idx+1] >> 4;
ascii_out[out_idx+1] = str_cb64[in_idx+1] << 4 | str_cb64[in_idx+2] >> 2;
ascii_out[out_idx+2] = str_cb64[in_idx+2] << 6 | str_cb64[in_idx+3];
out_idx += 3;
in_idx += 4;
}
for(i=0; i < end_size; i++){printf("%d\n",ascii_out[i]);}
}
To inspect, the code here prints the ascii value of each decoded character, which SHOULD be between 48 and 122, but there are values from (0, 255). I tested the conversion from the raw binary to the cb64 index, and that seems to work fine. The problem is in my shifting code. Any idea why it isn't working? I double checked the shifts and they look like they are coded correctly.
Thanks!