safec
3.2
Safe C Library - ISO TR24731 Bounds Checking Interface
|
Macros | |
#define | EXTERN extern |
#define | RSIZE_MAX_MEM16 ( RSIZE_MAX_MEM/2 ) |
#define | RSIZE_MAX_MEM32 ( RSIZE_MAX_MEM/4 ) |
#define | RSIZE_MAX_WMEM ( RSIZE_MAX_MEM/sizeof(wchar_t) ) |
Functions | |
EXTERN constraint_handler_t | set_mem_constraint_handler_s (constraint_handler_t handler) |
The set_mem_constraint_handler_s function sets the runtime-constraint handler to be handler. More... | |
EXTERN errno_t | memcpy_s (void *restrict dest, rsize_t dmax, const void *restrict src, rsize_t slen) |
This function copies at most smax bytes from src to dest, up to dmax. More... | |
EXTERN errno_t | memmove_s (void *dest, rsize_t dmax, const void *src, rsize_t slen) |
The memmove_s function copies smax bytes from the region pointed to by src into the region pointed to by dest. More... | |
EXTERN errno_t | memset_s (void *dest, rsize_t dmax, int value, rsize_t n) |
Sets the first n bytes starting at dest to the specified value, but maximal dmax bytes. More... | |
EXTERN errno_t | memcmp_s (const void *dest, rsize_t dmax, const void *src, rsize_t slen, int *diff) |
Compares memory until they differ, and their difference sign (-1,0,1) is returned in diff . More... | |
EXTERN errno_t | memcmp16_s (const uint16_t *dest, rsize_t dmax, const uint16_t *src, rsize_t slen, int *diff) |
Compares memory until they differ, and their difference is returned in diff. More... | |
EXTERN errno_t | memcmp32_s (const uint32_t *dest, rsize_t dmax, const uint32_t *src, rsize_t slen, int *diff) |
Compares memory until they differ, and their difference is returned in diff. More... | |
EXTERN errno_t | memcpy16_s (uint16_t *dest, rsize_t dmax, const uint16_t *src, rsize_t slen) |
This function copies at most smax uint16_ts from src to dest, up to dmax. More... | |
EXTERN errno_t | memcpy32_s (uint32_t *dest, rsize_t dmax, const uint32_t *src, rsize_t slen) |
This function copies at most smax uint32_ts from src to dest, up to dmax. More... | |
EXTERN errno_t | memmove16_s (uint16_t *dest, rsize_t dmax, const uint16_t *src, rsize_t slen) |
The memmove16_s function copies smax uint16_t from the region pointed to by src into the region pointed to by dest. More... | |
EXTERN errno_t | memmove32_s (uint32_t *dest, rsize_t dmax, const uint32_t *src, rsize_t slen) |
The memmove32_s function copies smax uint32_ts from the region pointed to by src into the region pointed to by dest. More... | |
EXTERN errno_t | memset16_s (uint16_t *dest, rsize_t dmax, uint16_t value, rsize_t n) |
Sets the first n uint16_t values starting at dest to the specified value, but maximal dmax bytes. More... | |
EXTERN errno_t | memset32_s (uint32_t *dest, rsize_t dmax, uint32_t value, rsize_t n) |
Sets len uint32_t starting at dest to the specified value. More... | |
EXTERN errno_t | memzero_s (void *dest, rsize_t dmax) |
Zeros len bytes starting at dest. More... | |
EXTERN errno_t | memzero16_s (uint16_t *dest, rsize_t dmax) |
Zeros len uint16_ts starting at dest. More... | |
EXTERN errno_t | memzero32_s (uint32_t *dest, rsize_t dmax) |
Zeros len uint32_ts starting at dest. More... | |
EXTERN errno_t | memchr_s (const void *restrict dest, rsize_t dlen, const int ch, void **result) |
Finds the first occurrence of ch (after conversion to unsigned char as if by (unsigned char)ch ) in the null-terminated buffer pointed to by dest (each character interpreted as unsigned char ). More... | |
EXTERN errno_t | memrchr_s (const void *restrict dest, rsize_t dlen, const int ch, void **result) |
Finds the last occurrence of ch (after conversion to char as if by (char)ch) in the null-terminated byte string pointed to by dest (each character interpreted as unsigned char). More... | |
EXTERN int | timingsafe_bcmp (const void *b1, const void *b2, size_t n) |
Compare all memory bytes. More... | |
EXTERN int | timingsafe_memcmp (const void *b1, const void *b2, size_t len) |
Compare all memory bytes. More... | |
EXTERN errno_t | memccpy_s (void *dest, rsize_t dmax, const void *src, int c, rsize_t n) |
EXTERN errno_t | wmemcpy_s (wchar_t *restrict dest, rsize_t dmax, const wchar_t *restrict src, rsize_t smax) |
EXTERN errno_t | wmemmove_s (wchar_t *dest, rsize_t dmax, const wchar_t *src, rsize_t smax) |
The wmemmove_s function copies smax wchar_t's from the region pointed to by src into the region pointed to by dest. More... | |
EXTERN errno_t | wmemcmp_s (const wchar_t *dest, rsize_t dmax, const wchar_t *src, rsize_t slen, int *diff) |
Compares buffers of wide chars until they differ, and return 0 if the same or -1 or 1 in diff. More... | |
#define EXTERN extern |
#define RSIZE_MAX_MEM16 ( RSIZE_MAX_MEM/2 ) |
#define RSIZE_MAX_MEM32 ( RSIZE_MAX_MEM/4 ) |
#define RSIZE_MAX_WMEM ( RSIZE_MAX_MEM/sizeof(wchar_t) ) |
EXTERN constraint_handler_t set_mem_constraint_handler_s | ( | constraint_handler_t | handler | ) |
The set_mem_constraint_handler_s function sets the runtime-constraint handler to be handler.
The runtime-constraint handler is the function to be called when a library function detects a runtime-constraint order:
EXTERN errno_t memcpy_s | ( | void *restrict | dest, |
rsize_t | dmax, | ||
const void *restrict | src, | ||
rsize_t | smax | ||
) |
This function copies at most smax bytes from src to dest, up to dmax.
[out] | dest | pointer to the memory that will be replaced by src. |
[in] | dmax | maximum length of the resulting dest, in bytes |
[in] | src | pointer to the memory that will be copied to dest |
[in] | smax | maximum number bytes of src that can be copied |
EOK | when operation is successful |
ESNULLP | when dest/src is NULL POINTER |
ESZEROL | when dmax = ZERO. Before C11 also with smax = ZERO |
ESLEMAX | when dmax/smax > RSIZE_MAX_MEM |
ESNOSPC | when dmax < smax |
ESOVRLP | when src memory overlaps dst |
The memmove_s function copies smax bytes from the region pointed to by src into the region pointed to by dest.
This copying takes place as if the smax bytes from the region pointed to by src are first copied into a temporary array of smax bytes that does not overlap the region pointed to by dest or src, and then the smax bytes from the temporary array are copied into the object region to by dest.
[out] | dest | pointer to the memory that will be replaced by src. |
[in] | dmax | maximum length of the resulting dest, in bytes |
[in] | src | pointer to the memory that will be copied to dest |
[in] | smax | maximum number bytes of src that can be copied |
EOK | when operation is successful |
ESNULLP | when dst/src is NULL POINTER |
ESZEROL | when dmax = ZERO. Before C11 also with smax = ZERO |
ESLEMAX | when dmax/smax > RSIZE_MAX_MEM |
ESNOSPC | when dmax < smax |
Sets the first n bytes starting at dest to the specified value, but maximal dmax bytes.
[out] | dest | pointer to memory that will be set to the value |
[in] | dmax | maximum number of bytes to be written |
[in] | value | byte value to be written |
[in] | n | number of bytes to be set |
EOK | when operation is successful |
ESNULLP | when dest is NULL pointer (EINVAL with C11) |
ESZEROL | when n = ZERO (unless C11) |
ESLEMAX | when dmax/n > RSIZE_MAX_MEM or value > 255 |
ESNOSPC | when dmax < n |
EXTERN errno_t memcmp_s | ( | const void * | dest, |
rsize_t | dmax, | ||
const void * | src, | ||
rsize_t | smax, | ||
int * | diff | ||
) |
Compares memory until they differ, and their difference sign (-1,0,1) is returned in
diff
.
If the block of memory is the same, *diff=0
.
dest | pointer to memory to compare against |
src | pointer to the source memory to compare with dest |
dmax | maximum length of dest, in bytes |
smax | length of the source memory block |
*diff | pointer to the diff which is an integer greater than, equal to or less than zero according to whether the object pointed to by dest is greater than, equal to or less than the object pointed to by src. |
EOK | when operation is successful |
ESNULLP | when dst/src is NULL POINTER |
ESZEROL | when dmax/smax = ZERO |
ESLEMAX | when dmax/smax > RSIZE_MAX_MEM |
ESNOSPC | when dmax < smax |
EXTERN errno_t memcmp16_s | ( | const uint16_t * | dest, |
rsize_t | dmax, | ||
const uint16_t * | src, | ||
rsize_t | smax, | ||
int * | diff | ||
) |
Compares memory until they differ, and their difference is returned in diff.
If the block of memory is the same, diff=0.
dest | pointer to memory to compare against |
src | pointer to the source memory to compare with dest |
dmax | maximum length of dest, in bytess |
smax | length of the source memory block |
*diff | pointer to the diff which is an integer greater than, equal to or less than zero according to whether the object pointed to by dest is greater than, equal to or less than the object pointed to by src. |
EOK | when operation is successful |
ESNULLP | when dst/src is NULL POINTER |
ESZEROL | when dmax/smax = ZERO |
ESLEMAX | when dmax/smax > RSIZE_MAX_MEM |
ESNOSPC | when dmax < smax |
EXTERN errno_t memcmp32_s | ( | const uint32_t * | dest, |
rsize_t | dmax, | ||
const uint32_t * | src, | ||
rsize_t | smax, | ||
int * | diff | ||
) |
Compares memory until they differ, and their difference is returned in diff.
If the block of memory is the same, diff=0.
dest | pointer to memory to compare against |
src | pointer to the source memory to compare with dest |
dmax | maximum length of dest, in bytess |
smax | length of the source memory block |
*diff | pointer to the diff which is an integer greater than, equal to or less than zero according to whether the object pointed to by dest is greater than, equal to or less than the object pointed to by src. |
EOK | when operation is successful |
ESNULLP | when dst/src is NULL POINTER |
ESZEROL | when dmax/smax = ZERO |
ESLEMAX | when dmax/smax > RSIZE_MAX_MEM |
ESNOSPC | when dmax < smax |
This function copies at most smax uint16_ts from src to dest, up to dmax.
[out] | dest | pointer to the memory that will be replaced by src. |
[in] | dmax | maximum length of the resulting dest, in bytes |
[in] | src | pointer to the memory that will be copied to dest |
[in] | smax | maximum number bytes of src that can be copied |
EOK | when operation is successful |
ESNULLP | when dst/src is NULL POINTER |
ESZEROL | when dmax = ZERO. Before C11 also with smax = ZERO |
ESLEMAX | when dmax/smax > RSIZE_MAX_MEM16 |
ESNOSPC | when dmax < smax |
ESOVRLP | when src memory overlaps dst |
This function copies at most smax uint32_ts from src to dest, up to dmax.
[out] | dest | pointer to the memory that will be replaced by src. |
[in] | dmax | maximum length of the resulting dest, in bytes |
[in] | src | pointer to the memory that will be copied to dest |
[in] | smax | maximum number bytes of src that can be copied |
EOK | when operation is successful |
ESNULLP | when dst/src is NULL POINTER |
ESZEROL | when dmax = ZERO. Before C11 also with smax = ZERO |
ESLEMAX | when dmax/smax > RSIZE_MAX_MEM |
ESNOSPC | when dmax < smax |
ESOVRLP | when src memory overlaps dst |
The memmove16_s function copies smax uint16_t from the region pointed to by src into the region pointed to by dest.
This copying takes place as if the smax uint16_t from the region pointed to by src are first copied into a temporary array of smax uint16_t that does not overlap the regions pointed to by dest or src, and then the smax uint16_t from the temporary array are copied into the region pointed to by dest.
[out] | dest | pointer to the memory that will be replaced by src. |
[in] | dmax | maximum length of the resulting dest, in bytes |
[in] | src | pointer to the memory that will be copied to dest |
[in] | smax | maximum number bytes of src that can be copied |
EOK | when operation is successful |
ESNULLP | when dst/src is NULL POINTER |
ESZEROL | when dmax = ZERO. Before C11 also with smax = ZERO |
ESLEMAX | when dmax/smax > RSIZE_MAX_MEM |
ESNOSPC | when dmax < smax |
The memmove32_s function copies smax uint32_ts from the region pointed to by src into the region pointed to by dest.
This copying takes place as if the smax uint32_ts from the region pointed to by src are first copied into a temporary array of smax uint32_ts that does not overlap the regions pointed to by dest or src, and then the smax uint32_ts from the temporary array are copied into the region pointed to by dest.
[out] | dest | pointer to the memory that will be replaced by src. |
[in] | dmax | maximum length of the resulting dest, in bytes |
[in] | src | pointer to the memory that will be copied to dest |
[in] | smax | maximum number bytes of src that can be copied |
EOK | when operation is successful |
ESNULLP | when dst/src is NULL POINTER |
ESZEROL | when dmax = ZERO. Before C11 also with smax = ZERO |
ESLEMAX | when dmax/smax > RSIZE_MAX_MEM |
ESNOSPC | when dmax < smax |
Sets the first n uint16_t values starting at dest to the specified value, but maximal dmax bytes.
[out] | dest | pointer to memory that will be set to the value |
[in] | dmax | maximum number of bytes to be written |
[in] | value | byte value to be written |
[in] | n | number of short (2-byte) words to be set |
EOK | when operation is successful |
ESNULLP | when dest is NULL POINTER |
ESZEROL | Only before C11 when n = ZERO |
ESLEMAX | when dmax > RSIZE_MAX_MEM |
ESLEMAX | when n > RSIZE_MAX_MEM16 |
ESNOSPC | when dmax/2 < n |
Sets len uint32_t starting at dest to the specified value.
[out] | dest | pointer to memory that will be set to the value |
[in] | dmax | maximum number of bytes to be written |
[in] | value | byte value to be written |
[in] | n | number of 4-byte words to be set |
EOK | when operation is successful |
ESNULLP | when dest is NULL POINTER |
ESZEROL | Only before C11 when n = ZERO |
ESLEMAX | when dmax > RSIZE_MAX_MEM |
ESLEMAX | when n > RSIZE_MAX_MEM32 |
ESNOSPC | when dmax/4 < n |
Zeros len bytes starting at dest.
[out] | dest | pointer to memory to be zeroed. |
[in] | len | number of bytes to be zeroed |
EOK | when operation is successful |
ESNULLP | when dest is NULL POINTER |
ESZEROL | when len = ZERO |
ESLEMAX | when len > RSIZE_MAX_MEM |
Zeros len uint16_ts starting at dest.
[out] | dest | pointer to memory to be zeroed. |
[in] | len | number of uint16_ts to be zeroed |
EOK | when operation is successful |
ESNULLP | when dest is NULL POINTER |
ESZEROL | when len = ZERO |
ESLEMAX | when len > RSIZE_MAX_MEM16 |
Zeros len uint32_ts starting at dest.
[out] | dest | pointer to memory to be zeroed. |
[in] | len | number of uint32_ts to be zeroed |
EOK | when operation is successful |
ESNULLP | when dest is NULL POINTER |
ESZEROL | when len = ZERO |
ESLEMAX | when len > RSIZE_MAX_MEM32 |
Finds the first occurrence of ch
(after conversion to unsigned char as if by (unsigned char)ch
) in the null-terminated buffer pointed to by dest
(each character interpreted as unsigned char
).
[in] | dest | pointer to buffer to compare against |
[in] | dlen | length of dest to search in |
[in] | ch | character to search for |
[out] | result | pointer to result in dest |
EOK | when successfully character found. |
ESNULLP | when dest/result is a NULL pointer |
ESZEROL | when dlen = 0 |
ESLEMAX | when dlen > RSIZE_MAX_STR |
ESLEMAX | when ch > 255 |
ESNOTFND | when ch not found in dest |
Finds the last occurrence of ch (after conversion to char as if by (char)ch) in the null-terminated byte string pointed to by dest (each character interpreted as unsigned char).
The terminating null character is considered to be a part of the string and can be found when searching for '\0'.
[in] | dest | pointer to string to compare against |
[in] | dlen | restricted maximum length of dest |
[in] | ch | character to search for |
[out] | result | pointer to char* in dest |
EOK | when successfully character found. |
ESNULLP | when dest/result is a NULL pointer |
ESZEROL | when dlen = 0 |
ESLEMAX | when dlen > RSIZE_MAX_STR |
ESLEMAX | when ch > 255 |
ESNOTFND | when ch not found in dest |
EXTERN int timingsafe_bcmp | ( | const void * | b1, |
const void * | b2, | ||
size_t | n | ||
) |
Compare all memory bytes.
Return 0 or not zero.
The running time is independent of the byte sequences compared, making them safe to use for comparing secret values such as cryptographic MACs. In contrast, bcmp(3) and memcmp(3) may short-circuit after finding the first differing byte.
b1 | pointer to memory to compare against |
b2 | pointer to the source memory to compare with b1 |
n | number of bytes to compare in both buffers |
0 | if the buffers are equal, or not zero if they are not equal. |
EXTERN int timingsafe_memcmp | ( | const void * | b1, |
const void * | b2, | ||
size_t | len | ||
) |
Compare all memory bytes.
Return their difference sign (-1,0,1).
The running time is independent of the byte sequences compared, making them safe to use for comparing secret values such as cryptographic MACs. In contrast, bcmp(3) and memcmp(3) may short-circuit after finding the first differing byte.
b1 | pointer to memory to compare against |
b2 | pointer to the source memory to compare with b1 |
len | number of bytes to compare in both buffers |
-1,0,or | 1 |
EXTERN errno_t wmemcpy_s | ( | wchar_t *restrict | dest, |
rsize_t | dmax, | ||
const wchar_t *restrict | src, | ||
rsize_t | smax | ||
) |
The wmemmove_s function copies smax wchar_t's from the region pointed to by src into the region pointed to by dest.
This copying takes place as if the smax wchar_ts from the region pointed to by src are first copied into a temporary array of smax wchar_ts that does not overlap the regions pointed to by dest or src, and then the smax wchar_ts from the temporary array are copied into the region pointed to by dest.
[out] | dest | pointer to the memory that will be replaced by src. |
[in] | dmax | maximum length of the resulting dest, in bytes |
[in] | src | pointer to the memory that will be copied to dest |
[in] | smax | maximum number bytes of src that can be copied |
EOK | when operation is successful |
ESNULLP | when dst/src is NULL POINTER |
ESZEROL | when dmax/smax = ZERO |
ESLEMAX | when dmax/smax > RSIZE_MAX_MEM |
ESNOSPC | when dmax < smax |
EXTERN errno_t wmemcmp_s | ( | const wchar_t * | dest, |
rsize_t | dmax, | ||
const wchar_t * | src, | ||
rsize_t | smax, | ||
int * | diff | ||
) |
Compares buffers of wide chars until they differ, and return 0 if the same or -1 or 1 in diff.
dest | pointer to wchar_t buffer to compare against |
src | pointer to the source wchar_t buffer to compare with dest |
dmax | maximum length of dest, in number of wchar_t |
smax | length of the source memory block |
*diff | pointer to the diff which is -1, 0 or 1 according to whether the object pointed to by dest is greater than, equal to or less than the object pointed to by src. Note that musl returns the diff here. |
EOK | when operation is successful |
ESNULLP | when dest/src is NULL POINTER |
ESZEROL | when dmax/smax = ZERO |
ESLEMAX | when dmax/smax > RSIZE_MAX_WMEM |
ESNOSPC | when dmax < smax |