libcontrac
A library for contact tracing
Files | Macros | Functions
Utilities

Static utility functions. More...

Files

file  utils.c
 Static utitlity functions.
 
file  utils.h
 Static utility functions.
 

Macros

#define MAX(a, b)
 
#define MIN(a, b)
 

Functions

size_t base64_encode_size (size_t binary_input)
 
size_t base64_decode_size (size_t base64_input)
 
void base64_encode_binary_to_base64 (unsigned char const *input, size_t input_size, unsigned char *output, size_t *output_size)
 
void base64_decode_base64_to_binary (unsigned char const *input, size_t input_size, unsigned char *output, size_t *output_size)
 
uint32_t epoch_to_day_number (time_t epoch)
 
uint8_t epoch_to_time_interval_number (time_t epoch)
 

Detailed Description

Static utility functions.

Provides various static utitlity functions. In particular:

base64 encoding and decoding functionality. Time conversion: from epoch to day numbers and time interval numbers.

Macro Definition Documentation

◆ MAX

#define MAX (   a,
 
)
Value:
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })

◆ MIN

#define MIN (   a,
 
)
Value:
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; })

Function Documentation

◆ base64_decode_base64_to_binary()

void base64_decode_base64_to_binary ( unsigned char const *  input,
size_t  input_size,
unsigned char *  output,
size_t *  output_size 
)

Decodes a base64 string into the original binary data it represents.

Decodes the base64 string provided into binary, storing the result in the output buffer provided. The output buffer must be pre-allocated with enough space to store the result. The size needed can be found by calling the base64_decode_size() function.

If the output buffer is too small (based on the size provided) then the binary output may be only partially written.

Parameters
inputThe base64 string to encode. This doesn't need to be zero terminated.
input_sizeThe size of the input buffer to be converted.
outputA pre-allocated buffer to store the result.
output_sizeThe size of the allocated buffer, which will be updated to the number of bytes written to the buffer.

◆ base64_decode_size()

size_t base64_decode_size ( size_t  base64_input)

Returns the amount of space needed to store the binary equivalent of a base64 string.

When converting from base64 it's often useful to know how much space will be needed to store the result, for example so that a buffer of the correct size can be allocated for it.

This function returns the size needed for a buffer that will be large enough to store the result. The returned value may be larger than the size actually needed.

Parameters
base64_inputThe length of a base64 string that would be decoded.
Returns
The buffer size needed to store the binary version of the same data.

◆ base64_encode_binary_to_base64()

void base64_encode_binary_to_base64 ( unsigned char const *  input,
size_t  input_size,
unsigned char *  output,
size_t *  output_size 
)

Encodes binary data to base64 format string.

Encodes the binary data provided into a base64 string, storing the result in the output buffer provided. The output buffer must be pre-allocated with enough space to store the result. The size needed can be found by calling the base64_encode_size() function.

If the output buffer is too small (based on the size provided) then the base64 string may be only partially written.

Parameters
inputThe binary data to encode. This doesn't need to be zero terminated.
input_sizeThe size of the input buffer to be converted.
outputA pre-allocated buffer to store the result.
output_sizeThe size of the allocated buffer, which will be updated to the number of bytes written to the buffer.

◆ base64_encode_size()

size_t base64_encode_size ( size_t  binary_input)

Returns the amount of space needed to store the base64 equivalent of a binary input.

When converting to base64 it's often useful to know how much space will be needed to store the result, for example so that a buffer of the correct size can be allocated for it.

This function returns the size needed for a buffer that will be large enough to store the result, including a terminating null character. The returned value may be larger than the size actually needed.

Parameters
binary_inputThe length of binary input that would be encoded.
Returns
The buffer size needed to store the base64 version of the same data.

◆ epoch_to_day_number()

uint32_t epoch_to_day_number ( time_t  epoch)

Converts a time in epoch format into a day number.

Returns the day number for the given epoch time. The epoch time represents the number of seconds since 00:00:00 UTC on 01/01/1970.

The day number is calculated as: (Number of Seconds since Epoch) / (60 * 60 * 24)

Parameters
epochThe epoch time in seconds.
Returns
The day number for this epoch time.

◆ epoch_to_time_interval_number()

uint8_t epoch_to_time_interval_number ( time_t  epoch)

Converts a time in epoch format into a time interval number.

Returns the time interval number for the given epoch time. The epoch time represents the number of seconds since 00:00:00 UTC on 01/01/1970.

The time interval number is calculated as: (Seconds Since Start of DayNumber) / (60 * 10)

and must fall in the interval [0, 143].

Parameters
epochThe epoch time in seconds.
Returns
The time interval number for this epoch time.