aklomp / base64

Fast Base64 stream encoder/decoder in C99, with SIMD acceleration
BSD 2-Clause "Simplified" License
866 stars 162 forks source link

Unable to do base64_encode on an image data #74

Closed sandeshk1 closed 3 years ago

sandeshk1 commented 3 years ago

I tried to do base64 encoding on an image data

    char* out;
    size_t outlen;

    base64_encode((const char*)color.get_data(), color.get_data_size(), out, &outlen, 0);

color.get_data() returns pointer to the frame data. color.get_data_size() returns the number of bytes in the frame handle.

aklomp commented 3 years ago

Did you allocate the memory for the output buffer in char* out? The caller is responsible for supplying a memory buffer of "sufficient" size. Sufficient here means at least 4/3 of the input size, because that's the size that the Base64 encoding will expand to.

sandeshk1 commented 3 years ago

My bad I did not allocate memory. Thanks for the quick reply.

Would I able to do base64 encode using this library and then decode the encoded data using python base64 decode ?

Is there any python wrappers provided in this library?

I have a python application which will receive the encoded data from a c++ application hence wanted to confirm on this.

aklomp commented 3 years ago

Would I able to do base64 encode using this library and then decode the encoded data using python base64 decode ?

Yes, Base64 encoding is standardized and is implementation independent.

Is there any python wrappers provided in this library?

No, this is a pure C library.

mayeut commented 3 years ago

Is there any python wrappers provided in this library?

No, this is a pure C library.

While this library does not provide a python wrapper, I do maintain one: https://github.com/mayeut/pybase64

sandeshk1 commented 3 years ago

Did you allocate the memory for the output buffer in char* out? The caller is responsible for supplying a memory buffer of "sufficient" size. Sufficient here means at least 4/3 of the input size, because that's the size that the Base64 encoding will expand to.

Should this memory allocation be enough ?

char* out = (char*)malloc(get_data_size()*sizeof(char)*(4/3));

// get_data_size() returns the size of the original frame handle which I am trying to encode

Is it responsibility of the user to free the allocated memory buffer ?

aklomp commented 3 years ago

4/3 of the original size should be enough, but it's probably safer to add 4 bytes or so for the optional base64 end markers or if you expect to add a null terminator at some point.

Also, your code as written has a bug. (4/3) will evaluate to 1 because it's using integer division. sizeof (char) is guaranteed to be 1 per the C standard, so it doesn't add much to use it in the expression. You could do something like this (untested):

const size_t size = get_data_size();
uint8_t *out;

// Allocate memory for the output buffer.
if ((out = malloc(size * 4 / 3 + 4)) == NULL) {

        // Handle the malloc error
        perror("malloc");
        return false;
}

// Do something with the buffer.
...

// Free the memory.
free(out);

return true;

Just a quick example of course. Yes, the user is responsible for calling free() on any memory they allocate. That's the drawback of C's manual memory handling.

sandeshk1 commented 3 years ago

Sorry for the delayed response.

Thanks for your suggestion on the usage.