IoTivity-Lite
oc_base64.h File Reference
#include "oc_export.h"
#include "util/oc_compiler.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

Enumerations

enum  oc_base64_encoding_t { OC_BASE64_ENCODING_STD , OC_BASE64_ENCODING_URL }
 

Functions

int oc_base64_decode (uint8_t *str, size_t len)
 In place decoding of base 64 string. More...
 
int oc_base64_decode_v1 (oc_base64_encoding_t encoding, bool padding, const uint8_t *input, size_t input_size, uint8_t *output_buffer, size_t output_buffer_size)
 Decode a base64 array to a byte array. More...
 
int oc_base64_decoded_output_size (const uint8_t *input, size_t input_size, bool padding)
 Calculate the size of the output buffer required to store a decoded base64 array. More...
 
int oc_base64_encode (const uint8_t *input, size_t input_size, uint8_t *output_buffer, size_t output_buffer_size)
 Encode byte buffer to base64 string. More...
 
int oc_base64_encode_v1 (oc_base64_encoding_t encoding, bool padding, const uint8_t *input, size_t input_size, uint8_t *output_buffer, size_t output_buffer_size)
 Encode byte buffer to base64 string. More...
 
size_t oc_base64_encoded_output_size (size_t size, bool padding)
 Calculate the size of the output buffer required to encode a byte array to base64. More...
 

Function Documentation

◆ oc_base64_decode()

int oc_base64_decode ( uint8_t *  str,
size_t  len 
)

In place decoding of base 64 string.

Size of a base 64 input string will always be larger than the resulting byte array. Unused trailing bytes will be set to zero. Use the return value to know the size of the output array.

Example: output_len = oc_base64_decode(b64_buf, strlen(b64_buf)); if (output_len < 0) { //handle error }

Parameters
[in,out]strbase 64 encoded string that will be decoded in place.
[in]lensize of the base 64 encoded string.
Returns
  • The size the the decoded byte array
  • '-1' if unable to decode string. This should only happen if the string is not a properly encoded base64 string.

◆ oc_base64_decode_v1()

int oc_base64_decode_v1 ( oc_base64_encoding_t  encoding,
bool  padding,
const uint8_t *  input,
size_t  input_size,
uint8_t *  output_buffer,
size_t  output_buffer_size 
)

Decode a base64 array to a byte array.

Parameters
encodingtype of encoding to use
paddingtrue if the input array is padded with '=' characters at the end
inputpointer to base64 encoded string
input_sizesize of base64 encoded string
[out]output_bufferbuffer to hold the decoded byte array
output_buffer_sizesize of the output_buffer
Returns
-1 on failure
>=0 the size of the decoded byte array on success

◆ oc_base64_decoded_output_size()

int oc_base64_decoded_output_size ( const uint8_t *  input,
size_t  input_size,
bool  padding 
)

Calculate the size of the output buffer required to store a decoded base64 array.

Parameters
inputinput base64-encoded array
input_sizesize of the input base64-encoded array
paddingtrue if the input array is padded with '=' characters at the end
Returns
-1 if the input array is not a valid base64-encoded array
size of the output buffer required to decode the input array

◆ oc_base64_encode()

int oc_base64_encode ( const uint8_t *  input,
size_t  input_size,
uint8_t *  output_buffer,
size_t  output_buffer_size 
)

Encode byte buffer to base64 string.

The base64 encoder does not NUL terminate its output. User the return value to add '\0' to the end of the string.

Output is uint8_t casting is needed to use value as a string.

Example:

// calculate the space required for the output size_t b64_buf_size = (sizeof(input) / 3) * 4; if (sizeof(input) % 3 != 0) { b64_buf_size += 4; } // one extra byte for terminating NUL character. b64_buf_size++; // allocate space char *b64_buf = (char *)calloc(1, b64_buf_size); int output_len = oc_base64_encode(input, sizeof(input), (uint8_t *)b64_buf, b64_buf_size); if (output_len < 0) { //handle error } // append NUL character to end of string. b64Buf[output_len] = '\0';

Parameters
[in]inputpointer to input byte array to be encoded
[in]input_sizesize of input byte array
[out]output_bufferbuffer to hold the base 64 encoded string
[in]output_buffer_sizesize of the output_buffer
Returns
  • the size of the base64 encoded string
  • -1 if the output buffer provided was not large enough

◆ oc_base64_encode_v1()

int oc_base64_encode_v1 ( oc_base64_encoding_t  encoding,
bool  padding,
const uint8_t *  input,
size_t  input_size,
uint8_t *  output_buffer,
size_t  output_buffer_size 
)

Encode byte buffer to base64 string.

The base64 encoder does not NUL terminate its output. User the return value to add '\0' to the end of the string.

Parameters
encodingtype of encoding to use
paddingtrue if padding should be used
inputpointer to input byte array to be encoded
input_sizesize of input byte array
[out]output_bufferbuffer to hold the base 64 encoded string
output_buffer_sizesize of the output_buffer
Returns
  • the size of the base64 encoded string
  • -1 if the output buffer provided was not large enough

◆ oc_base64_encoded_output_size()

size_t oc_base64_encoded_output_size ( size_t  size,
bool  padding 
)

Calculate the size of the output buffer required to encode a byte array to base64.

Parameters
sizesize of the input byte array
paddingtrue if padding should be used
Returns
size of the output buffer required to encode the input byte array