Loading...
Searching...
No Matches
Utility functions that are missing in <tt>string.h</tt>

This header provides utility functions that the standard C libs string.h lacks, such as explicit_bzero. More...

Detailed Description

This header provides utility functions that the standard C libs string.h lacks, such as explicit_bzero.

Files

file  string_utils.h
 Utility functions that are missing in string.h
 

Functions

static void explicit_bzero (void *dest, size_t n_bytes)
 Like memset(dest, 0, n_bytes), but secure.
 
ssize_t strscpy (char *dest, const char *src, size_t count)
 Copy the string, or as much of it as fits, into the dest buffer.
 
const void * memchk (const void *data, uint8_t c, size_t len)
 Check if the entire buffer is filled with the same byte.
 

Function Documentation

◆ explicit_bzero()

static void explicit_bzero ( void *  dest,
size_t  n_bytes 
)
inlinestatic

Like memset(dest, 0, n_bytes), but secure.

Unlike memset(dest, 0, n_bytes), this will zero out the memory even in cases the compiler would optimize out the call to memset().

Note
This is only sensible to use for sensitive data. For non-sensitive data, keep using memset() for performance reasons.
Parameters
[in,out]destMemory to clear
[in]n_bytesSize of memory to clear in bytes

Definition at line 65 of file string_utils.h.

◆ memchk()

const void * memchk ( const void *  data,
uint8_t  c,
size_t  len 
)

Check if the entire buffer is filled with the same byte.

Parameters
[in]dataThe buffer to probe
[in]cThe byte to check of
[in]lenSize of the buffer
Returns
NULL if the entire buffer is filled with c
pointer to the first non-matching byte

◆ strscpy()

ssize_t strscpy ( char *  dest,
const char *  src,
size_t  count 
)

Copy the string, or as much of it as fits, into the dest buffer.

Preferred to strncpy since it always returns a valid string, and doesn't unnecessarily force the tail of the destination buffer to be zeroed. If the zeroing is desired, it's likely cleaner to use strscpy with an overflow test, then just memset the tail of the dest buffer.

Parameters
[out]destWhere to copy the string to
[in]srcWhere to copy the string from
[in]countSize of destination buffer
Precondition
The destination buffer is at least one byte large, as otherwise the terminating zero byte won't fit
Returns
the number of characters copied (not including the trailing zero)
Return values
-E2BIGthe destination buffer wasn't big enough