Useful utility functions.
More...
#include <stdlib.h>
#include <stddef.h>
#include "version.h"
Go to the source code of this file.
|
| 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) |
| |
Useful utility functions.
- Author
- Raphael Beck
◆ 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
◆ 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
-
| hexstr | The hex string to convert. |
| hexstr_length | Length of the hexstr |
| output | Where to write the converted binary data into. |
| output_size | Size 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
-
| mem1 | Memory block 1 to compare. |
| mem2 | Memory block 2 to compare. |
| n | How 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
-
| str1 | String to compare. |
| str2 | String to compare to. |
| n | How many characters of the string should be compared (starting from index 0)? |
- Returns
- If the strings are equal,
0 is returned. Otherwise, something else.