1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-15 09:33:50 +01:00

sys/string_utils: add memchk()

Check if all bytes in a buffer are set to s certain value - inverse
of memset().
This commit is contained in:
Benjamin Valentin 2023-05-30 20:39:10 +02:00
parent 23790a3f52
commit c57b13f1e8
2 changed files with 25 additions and 0 deletions

View File

@ -91,6 +91,18 @@ static inline void explicit_bzero(void *dest, size_t n_bytes)
*/
ssize_t strscpy(char *dest, const char *src, size_t count);
/**
* @brief Check if the entire buffer is filled with the same byte.
*
* @param[in] data The buffer to probe
* @param[in] c The byte to check of
* @param[in] len Size of the buffer
*
* @return NULL if the entire buffer is filled with @p c
* @return pointer to the first non-matching byte
*/
const void *memchk(const void *data, uint8_t c, size_t len);
#ifdef __cplusplus
}
#endif

View File

@ -37,4 +37,17 @@ ssize_t strscpy(char *dest, const char *src, size_t count)
return -E2BIG;
}
}
const void *memchk(const void *data, uint8_t c, size_t len)
{
const uint8_t *end = (uint8_t *)data + len;
for (const uint8_t *d = data; d != end; ++d) {
if (c != *d) {
return d;
}
}
return NULL;
}
/** @} */