l8w8jwt
Loading...
Searching...
No Matches
Macros | Functions
util.h File Reference

Useful utility functions. More...

#include <stdlib.h>
#include <stddef.h>
#include "version.h"
Include dependency graph for util.h:

Go to the source code of this file.

Macros

#define L8W8JWT_PLATFORM_MALLOC_ALT   0
 
#define L8W8JWT_PLATFORM_CALLOC_ALT   0
 
#define L8W8JWT_PLATFORM_REALLOC_ALT   0
 
#define l8w8jwt_malloc   malloc
 
#define l8w8jwt_calloc   calloc
 
#define l8w8jwt_realloc   realloc
 

Functions

L8W8JWT_API int l8w8jwt_hexstr2bin (const char *hexstr, size_t hexstr_length, unsigned char *output, size_t output_size, size_t *output_length)
 
L8W8JWT_API int l8w8jwt_strncmpic (const char *str1, const char *str2, size_t n)
 
L8W8JWT_API int l8w8jwt_memcmp (const void *mem1, const void *mem2, size_t n)
 

Detailed Description

Useful utility functions.

Author
Raphael Beck

Macro Definition Documentation

◆ L8W8JWT_PLATFORM_CALLOC_ALT

#define L8W8JWT_PLATFORM_CALLOC_ALT   0

Set this pre-processor definition to 1 if you need to provide custom implementation of calloc

◆ L8W8JWT_PLATFORM_MALLOC_ALT

#define L8W8JWT_PLATFORM_MALLOC_ALT   0

Set this pre-processor definition to 1 if you need to provide custom implementation of malloc

◆ L8W8JWT_PLATFORM_REALLOC_ALT

#define L8W8JWT_PLATFORM_REALLOC_ALT   0

Set this pre-processor definition to 1 if you need to provide custom implementation of realloc

Function Documentation

◆ l8w8jwt_hexstr2bin()

L8W8JWT_API int l8w8jwt_hexstr2bin ( const char *  hexstr,
size_t  hexstr_length,
unsigned char *  output,
size_t  output_size,
size_t *  output_length 
)

Converts a hex-encoded string to a binary array.

A NUL-terminator is appended at the end of the output buffer, so make sure to allocate at least (hexstr_length / 2) + 1 bytes!

Parameters
hexstrThe hex string to convert.
hexstr_lengthLength of the hexstr
outputWhere to write the converted binary data into.
output_sizeSize of the output buffer (make sure to allocate at least (hexstr_length / 2) + 1 bytes!).
output_length[OPTIONAL] Where to write the output array length into. This is always gonna be hexstr_length / 2, but you can still choose to write it out just to be sure. If you want to omit this: no problem.. just pass NULL!
Returns
0 if conversion succeeded. 1 if one or more required arguments were NULL or invalid. 2 if the hexadecimal string is in an invalid format (e.g. not divisible by 2). 3 if output buffer size was insufficient (needs to be at least (hexstr_length / 2) + 1 bytes).

◆ l8w8jwt_memcmp()

L8W8JWT_API int l8w8jwt_memcmp ( const void *  mem1,
const void *  mem2,
size_t  n 
)

Compares two blocks of memory against equality in a cryptographically safe manner (time-safe impl./constant-time comparison).

Parameters
mem1Memory block 1 to compare.
mem2Memory block 2 to compare.
nHow many bytes to compare.
Returns
Returns 0 if the two memory blocks are equal for the passed amount of bytes.

◆ l8w8jwt_strncmpic()

L8W8JWT_API int l8w8jwt_strncmpic ( const char *  str1,
const char *  str2,
size_t  n 
)

Compares two strings ignoring UPPER vs. lowercase.

Parameters
str1String to compare.
str2String to compare to.
nHow many characters of the string should be compared (starting from index 0)?
Returns
If the strings are equal, 0 is returned. Otherwise, something else.