This header provides utility functions that the standard C libs string.h
lacks, such as explicit_bzero.
More...
This header provides utility functions that the standard C libs string.h
lacks, such as explicit_bzero.
|
static void | string_writer_init (string_writer_t *sw, void *buffer, size_t len) |
| Initialize a string writer structure.
|
|
static size_t | string_writer_len (const string_writer_t *sw) |
| Get the size of the string contained by the string writer.
|
|
static const char * | string_writer_str (const string_writer_t *sw) |
| Get the string contained by the string writer.
|
|
int | __swprintf (string_writer_t *sw, FLASH_ATTR const char *restrict format,...) |
| Write a formatted string to a buffer The string will be truncated if there is not enough space left in the destination buffer.
|
|
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.
|
|
◆ __swprintf
#define __swprintf swprintf |
◆ __swprintf()
Write a formatted string to a buffer The string will be truncated if there is not enough space left in the destination buffer.
A terminating \0
character is always included.
- Parameters
-
[in] | sw | String Writer to write to |
[in] | format | format string to write |
- Returns
- number of bytes written on success -E2BIG if the string was truncated
◆ 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] | dest | Memory to clear |
[in] | n_bytes | Size of memory to clear in bytes |
Definition at line 141 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] | data | The buffer to probe |
[in] | c | The byte to check of |
[in] | len | Size of the buffer |
- Returns
- NULL if the entire buffer is filled with
c
-
pointer to the first non-matching byte
◆ string_writer_init()
static void string_writer_init |
( |
string_writer_t * |
sw, |
|
|
void * |
buffer, |
|
|
size_t |
len |
|
) |
| |
|
inlinestatic |
Initialize a string writer structure.
- Parameters
-
[out] | sw | String Writer object to initialize |
[in] | buffer | destination buffer |
[in] | len | size of the destination buffer |
Definition at line 60 of file string_utils.h.
◆ string_writer_len()
Get the size of the string contained by the string writer.
- Parameters
-
[in] | sw | String Writer to query |
- Returns
- size of the string
Definition at line 75 of file string_utils.h.
◆ string_writer_str()
Get the string contained by the string writer.
- Parameters
-
[in] | sw | String Writer to query |
- Returns
- the string assembled by string writer
Definition at line 85 of file string_utils.h.
◆ 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] | dest | Where to copy the string to |
[in] | src | Where to copy the string from |
[in] | count | Size 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
-
-E2BIG | the destination buffer wasn't big enough |