Loading...
Searching...
No Matches
Base64 encoder decoder

Base64 encoder and decoder. More...

Detailed Description

Base64 encoder and decoder.

Encoding and decoding functions for base64

Author
Martin Landsmann Marti.nosp@m.n.La.nosp@m.ndsma.nosp@m.nn@H.nosp@m.AW-Ha.nosp@m.mbur.nosp@m.g.de

Macros

#define BASE64_SUCCESS   (0)
 return value for success
 
#define BASE64_ERROR_BUFFER_OUT   (-1)
 error value for invalid output buffer pointer
 
#define BASE64_ERROR_BUFFER_OUT_SIZE   (-2)
 error value for invalid output buffer size
 
#define BASE64_ERROR_DATA_IN   (-3)
 error value for invalid input buffer
 
#define BASE64_ERROR_DATA_IN_SIZE   (-4)
 error value for invalid input buffer size
 
#define BASE64_ERROR_OVERFLOW   (-5)
 error value for buffer overflow
 

Functions

static size_t base64_estimate_decode_size (size_t base64_in_size)
 Estimates the amount of bytes needed for decoding base64_in_size characters from base64.
 
static size_t base64_estimate_encode_size (size_t data_in_size)
 Estimates the length of the resulting string after encoding data_in_size bytes into base64.
 
static bool base64_can_estimate_encode_size (size_t data_in_size)
 Checks if the given size of data can return a valid estimate for the size without an integer overflow.
 
int base64_encode (const void *data_in, size_t data_in_size, void *base64_out, size_t *base64_out_size)
 Encodes a given datum to base64 and save the result to the given destination.
 
int base64url_encode (const void *data_in, size_t data_in_size, void *base64_out, size_t *base64_out_size)
 Encodes a given datum to base64 with URL and Filename Safe Alphabet and save the result to the given destination.
 
int base64_decode (const void *base64_in, size_t base64_in_size, void *data_out, size_t *data_out_size)
 Decodes a given base64 string and save the result to the given destination.
 

Macro Definition Documentation

◆ BASE64_ERROR_BUFFER_OUT

#define BASE64_ERROR_BUFFER_OUT   (-1)

error value for invalid output buffer pointer

Definition at line 27 of file base64.h.

◆ BASE64_ERROR_BUFFER_OUT_SIZE

#define BASE64_ERROR_BUFFER_OUT_SIZE   (-2)

error value for invalid output buffer size

Definition at line 28 of file base64.h.

◆ BASE64_ERROR_DATA_IN

#define BASE64_ERROR_DATA_IN   (-3)

error value for invalid input buffer

Definition at line 29 of file base64.h.

◆ BASE64_ERROR_DATA_IN_SIZE

#define BASE64_ERROR_DATA_IN_SIZE   (-4)

error value for invalid input buffer size

Definition at line 30 of file base64.h.

◆ BASE64_ERROR_OVERFLOW

#define BASE64_ERROR_OVERFLOW   (-5)

error value for buffer overflow

Definition at line 31 of file base64.h.

◆ BASE64_SUCCESS

#define BASE64_SUCCESS   (0)

return value for success

Definition at line 26 of file base64.h.

Function Documentation

◆ base64_can_estimate_encode_size()

static bool base64_can_estimate_encode_size ( size_t data_in_size)
inlinestatic

Checks if the given size of data can return a valid estimate for the size without an integer overflow.

Parameters
[in]data_in_sizeAmount of bytes to be encoded
Return values
trueif the given size can be encoded to base64
falseif the given size would overflow

Definition at line 71 of file base64.h.

◆ base64_decode()

int base64_decode ( const void * base64_in,
size_t base64_in_size,
void * data_out,
size_t * data_out_size )

Decodes a given base64 string and save the result to the given destination.

Parameters
[in]base64_inPointer to the datum to decode
[in]base64_in_sizeThe size of base64_in
[out]data_outPointer to store the decoded base64 string (may be tainted on error)
[in,out]data_out_sizeThe size of data_out. This value is overwritten with the estimated size used for the decoded string on BASE64_ERROR_BUFFER_OUT_SIZE. This value is overwritten with the actual used size for the decoded string on BASE64_SUCCESS.
Return values
BASE64_SUCCESSon success
BASE64_ERROR_BUFFER_OUT_SIZEon insufficient size for decoding to data_out
BASE64_ERROR_BUFFER_OUTif data_out equals NULL but the size for data_out_size is sufficient
BASE64_ERROR_DATA_INif base64_in equals NULL
BASE64_ERROR_DATA_IN_SIZEif base64_in_size has remainder 1 (mod 4)

◆ base64_encode()

int base64_encode ( const void * data_in,
size_t data_in_size,
void * base64_out,
size_t * base64_out_size )

Encodes a given datum to base64 and save the result to the given destination.

Parameters
[in]data_inPointer to the datum to encode
[in]data_in_sizeThe size of data_in
[out]base64_outPointer to store the encoded base64 string (may be tainted on error)
[in,out]base64_out_sizePointer to the variable containing the size of base64_out. This value is overwritten with the estimated size used for the encoded base64 string on BASE64_ERROR_BUFFER_OUT_SIZE. This value is overwritten with the actual used size for the encoded base64 string on BASE64_SUCCESS.
Return values
BASE64_SUCCESSon success
BASE64_ERROR_BUFFER_OUT_SIZEon insufficient size for encoding to base64_out
BASE64_ERROR_BUFFER_OUTif base64_out equals NULL but the base64_out_size is sufficient
BASE64_ERROR_DATA_INif data_in equals NULL
BASE64_ERROR_OVERFLOWif data_in_size is too large to be encoded to base64

◆ base64_estimate_decode_size()

static size_t base64_estimate_decode_size ( size_t base64_in_size)
inlinestatic

Estimates the amount of bytes needed for decoding base64_in_size characters from base64.

Parameters
[in]base64_in_sizeSize of the string to be decoded
Returns
Amount of bytes estimated to be used after decoding

Definition at line 41 of file base64.h.

◆ base64_estimate_encode_size()

static size_t base64_estimate_encode_size ( size_t data_in_size)
inlinestatic

Estimates the length of the resulting string after encoding data_in_size bytes into base64.

Precondition
data_in_size must be smaller than ~75% of the SIZE_MAX to prevent integer overflow in the calculation of the return value.
Parameters
[in]data_in_sizeAmount of bytes to be encoded
Returns
Amount of characters the output string is estimated to have

Definition at line 57 of file base64.h.

◆ base64url_encode()

int base64url_encode ( const void * data_in,
size_t data_in_size,
void * base64_out,
size_t * base64_out_size )

Encodes a given datum to base64 with URL and Filename Safe Alphabet and save the result to the given destination.

See also
RFC 4648, section 5
Note
Requires the use of the base64url module.
Parameters
[in]data_inPointer to the datum to encode
[in]data_in_sizeThe size of data_in
[out]base64_outPointer to store the encoded base64 string (may be tainted on error)
[in,out]base64_out_sizePointer to the variable containing the size of base64_out. This value is overwritten with the estimated size used for the encoded base64 string on BASE64_ERROR_BUFFER_OUT_SIZE. This value is overwritten with the actual used size for the encoded base64 string on BASE64_SUCCESS.
Return values
BASE64_SUCCESSon success
BASE64_ERROR_BUFFER_OUT_SIZEon insufficient size for encoding to base64_out
BASE64_ERROR_BUFFER_OUTif base64_out equals NULL but the base64_out_size is sufficient
BASE64_ERROR_DATA_INif data_in equals NULL
BASE64_ERROR_OVERFLOWif data_in_size is too large to be encoded to base64