KNX IoT
KNX IoT Point API stack implementation
oc_base64.h File Reference

Base64 encoding and decoding functions. More...

#include <stddef.h>
#include <stdint.h>

Go to the source code of this file.

Functions

int oc_base64_decode (uint8_t *str, size_t len)
 In place decoding of base 64 string. More...
 
int oc_base64_encode (const uint8_t *input, size_t input_len, uint8_t *output_buffer, size_t output_buffer_len)
 encode byte buffer to base64 string. More...
 

Detailed Description

Base64 encoding and decoding functions.

Definition in file oc_base64.h.

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_encode()

int oc_base64_encode ( const uint8_t *  input,
size_t  input_len,
uint8_t *  output_buffer,
size_t  output_buffer_len 
)

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_lensize of input byte array
[out]output_bufferbuffer to hold the base 64 encoded string
[in]output_buffer_lensize of the output_buffer
Returns
  • the size of the base64 encoded string
  • -1 if the output buffer provided was not large enough