cURL / Mailing Lists / curl-library / Single Mail


Re: external access to the curl base64 functions

From: Andrew Francis <>
Date: Tue, 8 Oct 2002 10:04:39 +0800

"Daniel Stenberg" <> wrote:
> > I think it would be a good idea to make them available to the
> > using libcurl.
> But... exporting them to others will require a sensible API that people
> and understands. The current one really has more or less ended up as it is
> accident, as can be seen by how they work right now:

I don't actually think they're too bad :P But if the interface were to be
done from scratch, I would favor something like the below.
s/malloc/curl_malloc/, if curl_malloc is still on the table.

The major difference between the current interface and the below is:

- user chooses whether to allocate memory themselves, or let curl_base64_*
do it for them

- the functions are expected to do error checking (make sure there's enough
space to store the output, ensure that input to base64_decode is valid
base64, etc).

I'll go ahead and implement these sometime this week if you like the idea.

char *curl_base64_encode(unsigned char *orig, int origlen, char *dest, int

Takes origlen bytes from orig and produces the base64 encoded string, null
terminated, at dest.

dest may be NULL, in which case memory will be obtained from malloc().

If destlen is not NULL, then then the length of the base64 string (not
including null terminator) will be written to *destlen before the function

If dest and destlen are both non-NULL, then *destlen going in will be the
size of the buffer, and the function will not write more than that number of
bytes to *dest.

The function returns a pointer to the base64 string (either equal to dest,
or if dest is NULL, then malloc()ed memory). Returning NULL indicates

unsigned char *curl_base64_decode(char *encoded, int encodedlen, unsigned
char *dest, int *destlen)

This behaves identically, except it takes base64 as input and produces raw
data. It will fail on input that is not valid base64. If encodedlen is not a
positive number, then the function will attempt to detect the end of the
input itself (equals and \0).

Examples of usage:
  /* we have input, of length inputlen, and want to get the raw data */
  #define BUF_SIZE 4000
  unsigned char *rawdata = malloc(BUF_SIZE);
  int rawsize = BUF_SIZE;
  if(!curl_base64_decode(input, inputlen, rawdata, &rawsize)) {
    fprintf(stderr, "there was an error decoding. (probably input too large,
or not valid base64)\n");
  } else {
    /* push the raw data out to stdout */
    fwrite(rawdata, 1, rawsize, stdout);

  /* this time, we don't malloc ourselves */
  int rawsize;
  char *rawdata;
  rawdata = curl_base64_decode(input, inputlen, NULL, &rawsize);
  if(rawdata) {
    printf("raw data is %d bytes\n", rawsize);
  } else {
    printf("there was an error, probably out-of-memory, or invalid


Andrew Francis
Software Developer
Family Health Network
This email is sponsored by:ThinkGeek
Welcome to geek heaven.
Received on 2002-10-08