Virgil IoT KIT
Functions
base64.h File Reference

base64 functions More...

#include <stdbool.h>

Go to the source code of this file.

Functions

int base64decode_len (const char *in, int inlen)
 Get Base64 Decoded len. More...
 
bool base64decode (const char *in, int inlen, unsigned char *out, int *outlen)
 Base64 Decode string. More...
 
int base64encode_len (int len)
 Get Base64 Encoded len. More...
 
bool base64encode (const unsigned char *in, int inlen, char *out, int *outlen)
 Base64 Encode data. More...
 

Detailed Description

base64 functions

This header contains base64 encoders/decoders.

You can see an example :

const char *origin = "Test vector";
char *encoded = NULL;
unsigned char *decoded = NULL;
int size_origin = 0;
int size_encoded = 0;
int size_decoded = 0;
bool res = false;
// Encoding length
size_origin = strlen(origin);
size_encoded = base64encode_len(size_origin);
assert(size_encoded > 0);
// Prepare encoding buffer
encoded = malloc(size_encoded);
assert(encoded);
// Encode
res = base64encode((const unsigned char *)origin, size_origin, encoded, &size_encoded);
assert(res && size_encoded);
// Decoded length
size_decoded = base64decode_len(encoded, size_encoded);
assert(size_decoded > 0);
// Prepare decoding buffer
decoded = malloc(size_decoded);
assert(decoded);
// Decode
res = base64decode(encoded, size_encoded, decoded, &size_decoded);
assert(res && size_decoded);
// Compare results
assert(size_decoded == size_origin && !memcmp(origin, decoded, size_origin));
// Free buffers
free(decoded);
free(encoded);

This product includes software developed by the Apache Group for use in the Apache HTTP server project (http://www.apache.org/)

Function Documentation

◆ base64decode()

bool base64decode ( const char *  in,
int  inlen,
unsigned char *  out,
int *  outlen 
)

Base64 Decode string.

This function decodes the given Base64 encoded string into raw binary data.

Note
This function an do in-place decoding. So, the same buffer can be used for input as well as output.
Parameters
[in]inPointer to Base64 encoded string.
[in]inlenLength of the Base64 encoded string.
[out]outPointer to the output buffer that will be populated by the function.
[in,out]outlenHolds the length of the output buffer and is populated with the length of the decoded data by this function.
Returns
-1 on failure
0 on success

◆ base64decode_len()

int base64decode_len ( const char *  in,
int  inlen 
)

Get Base64 Decoded len.

Gives the length of the data that will be obtained after decoding the given base64 encoded string

Parameters
[in]inPointer to Base64 encoded string.
[in]inlenLength of the Base64 encoded string.
Returns
Length of the data that will be obtained after decoding

◆ base64encode()

bool base64encode ( const unsigned char *  in,
int  inlen,
char *  out,
int *  outlen 
)

Base64 Encode data.

This function encodes the given raw binary data into a Base64 encoded string.

Parameters
[in]inPointer to raw binary data
[in]inlenLength if the binary data (in bytes)
[out]outPointer to the output buffer that will be populated by the function
[in/out]outlen Holds the length of the output buffer and is populated with the length of the encoded data by this function. The required length of the buffer can be obtained using base64encode_len().
Returns
-1 on failure
0 on success

◆ base64encode_len()

int base64encode_len ( int  len)

Get Base64 Encoded len.

Gives the length of the string that will be obtained after encoding the given binary data

Parameters
[in]Lengthof the input binary data.
Returns
Length of the string that will be obtained after encoding
base64encode
bool base64encode(const unsigned char *in, int inlen, char *out, int *outlen)
Base64 Encode data.
base64encode_len
int base64encode_len(int len)
Get Base64 Encoded len.
base64decode
bool base64decode(const char *in, int inlen, unsigned char *out, int *outlen)
Base64 Decode string.
base64decode_len
int base64decode_len(const char *in, int inlen)
Get Base64 Decoded len.