Giter Club home page Giter Club logo

Comments (7)

aklomp avatar aklomp commented on July 3, 2024

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.

from base64.

sandeshk1 avatar sandeshk1 commented on July 3, 2024

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.

from base64.

aklomp avatar aklomp commented on July 3, 2024

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.

from base64.

mayeut avatar mayeut commented on July 3, 2024

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

from base64.

sandeshk1 avatar sandeshk1 commented on July 3, 2024

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 ?

from base64.

aklomp avatar aklomp commented on July 3, 2024

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.

from base64.

sandeshk1 avatar sandeshk1 commented on July 3, 2024

Sorry for the delayed response.

Thanks for your suggestion on the usage.

from base64.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.