l8w8jwt
Loading...
Searching...
No Matches
Functions
base64.h File Reference

Base-64 encode and decode strings/bytes. More...

#include "version.h"
#include <stdint.h>
#include <string.h>
Include dependency graph for base64.h:

Go to the source code of this file.

Functions

L8W8JWT_API int l8w8jwt_base64_encode (int url, const uint8_t *data, size_t data_length, char **out, size_t *out_length)
 
L8W8JWT_API int l8w8jwt_base64_decode (int url, const char *data, size_t data_length, uint8_t **out, size_t *out_length)
 

Detailed Description

Base-64 encode and decode strings/bytes.

Author
Raphael Beck
Warning
The caller is responsible for freeing the returned buffers!

Pass true as first parameter if you want to use base64url encoding instead of base64.

See also
https://en.wikipedia.org/wiki/Base64#URL_applications

Function Documentation

◆ l8w8jwt_base64_decode()

L8W8JWT_API int l8w8jwt_base64_decode ( int  url,
const char *  data,
size_t  data_length,
uint8_t **  out,
size_t *  out_length 
)

Decodes a base-64 encoded string to an array of bytes.

Note
The returned bytes buffer is NUL-terminated to allow usage as a C string.
The NUL terminator is NOT included in the out_length.
DO NOT FORGET to free the output buffer once you're done using it!
Parameters
urlDecode using base64url instead of base64? Set to 0 for false; anything else for true.
dataThe base-64 encoded string to decode (obtained via l8w8jwt_base64_encode).
data_lengthThe length of the string to decode.
outOutput where the decoded bytes should be written into (will be malloc'ed, so make sure to free() this as soon as you're done using it!).
out_lengthPointer to a size_t variable into which to write the output buffer's length.
Returns
Return code as defined in retcodes.h

◆ l8w8jwt_base64_encode()

L8W8JWT_API int l8w8jwt_base64_encode ( int  url,
const uint8_t *  data,
size_t  data_length,
char **  out,
size_t *  out_length 
)

Encodes a byte array to a base-64 string.

If you're encoding a string, don't include the NUL terminator (pass strlen(data) instead of the array's size to the data_length parameter).

Note
The output buffer is NUL-terminated to make it easier to use as a C string.
The NUL terminator is NOT included in the out_length.
DO NOT FORGET to free the output buffer once you're done using it!
Parameters
urlbase64url encode instead of base64? Set to 0 for false; anything else for true.
dataThe data (array of bytes) to base-64 encode.
data_lengthThe length of the input data array (in case of a C string: array size - 1 in order to omit the NUL terminator).
outOutput where the base-64 encoded string should be written into (will be malloc'ed, so make sure to free() this as soon as you're done using it!).
out_lengthPointer to a size_t variable containing the length of the output buffer minus the NUL terminator.
Returns
Return code as defined in retcodes.h