LeFroys
LeFroys

Reputation: 19

Decode base64/UTF-16LE using C

My problem is trying to decode a base64 / UTF-16LE string. The result of this decoding is always the first character. If you decode the string to utf-8 / ascii / windows1252, then the result is correct.

Can anyone have a ready-made solution?

The standard code from the internet that I use:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
    b64_decode_string("SABlAGwAbABvAA==");
    
    return 0;
}


char base64decode_lut[] = {
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
    52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
    64,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
    15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
    64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
    41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
};
 

void base64decode(char *src, char *dest, int len)
{
    int i=0, slen=strlen(src);
    for(i=0;i<slen&&i<len;i+=4,src+=4)
    { 
        char c1=base64decode_lut[*src], c2=base64decode_lut[*(src+1)], c3=base64decode_lut[*(src+2)], c4=base64decode_lut[*(src+3)];

        *(dest++)=(c1&0x3F)<<0x2|(c2&0x30)>>0x4;
        *(dest++)=(c3!=64)?((c2&0xF)<<0x4|(c3&0x3C)>>0x2):'\0';
        *(dest++)=(c4!=64)?((c3&0x3)<<0x6|c4&0x3F):'\0';
    }
    *dest='\0'; // Append terminator
}
 
 
int b64_decode_string(char *source)
{
    int dest_size;
    int res;
    char *dest;

    dest_size = strlen(source);
    dest = (char *)malloc(dest_size);

    memset(dest,0,dest_size);
    base64decode(source, dest, dest_size);
    printf("Decode: %s", dest);
}

Upvotes: 0

Views: 338

Answers (2)

user3629249
user3629249

Reputation: 16540

The reason only the first character was decoded is because the second character starts with a 0x00 byte, so the call to strlen() (which is only for ascii strings) returned a very small number (like 1 or 2) rather than the actual length of the string.

suggest becoming familiar with the wchar_t type and the functions to handle wide characters, like: wprintf() and the 'wide' modifier for strings: -L and the header file: wchar.h

how to handle wide character strings

Upvotes: 2

ryyker
ryyker

Reputation: 23218

You are not working with C strings here, so stay away from using string functions such as strlen();

And consider using unsigned char as the type for the array

unsigned char base64decode{} = {...

In this case, the number of array elements can be determined by the statement:

size_t len = sizeof(base64decode_lut)/sizeof(base64decode_lut[0]);

Upvotes: 0

Related Questions