Hi guys, I think there is an issue with the base64 decode function: from http://web.mit.edu/freebsd/head/contrib/wpa/src/utils/base64.c unsigned char * base64_decode(const unsigned char *src, size_t len, size_t *out_len) { ...... for (i = 0; i < sizeof(base64_table) - 1; i++) dtable[base64_table[i]] = (unsigned char) i; ..... the problem is the for loop which initializes the dtable. It does not initialize the last element from the table ('/') which causes the char to be interpreted as a garbage and ignored. Please have a closer look. Best regards, Ivan
Hi Ivan, I was wondering if you've got a test case that shows this "off-by-one"? As when I read the code, I do not believe there is an "off-by-one" error. The base64_table: static const unsigned char base64_table[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; Is defined as size 65, yet contains 64 elements and the last trailing NULL. So sizeof(base64_table) will return 65, yet the for loop is creating the reverse base64 lookup in dtable, so we only want to do this for elements in base64_table. Which is sizeof(base64_table) -1, or strlen(base64_table). This is my understanding of the code and I am unable to encode/decode an error. I would be interested to hear your thoughts. Regards Tim
Hi Tim, I have modified the source code a little bit before using it in my project. As it turns out I have changed the line static const unsigned char base64_table[65] = .... to static const unsigned char base64_encode_table[] = which allocates indeed one byte less :). Sorry for the time lost :D However I don't find it sexy, to allocate 65 bytes and initialize only 64 of them and rely on the fact that static variables usually land in the .bss. The code will work by initializing 64 elements and removing the " -1 " from the for loop. Best regards, Ivan