Loading...
Searching...
No Matches
string_utils.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 Otto-von-Guericke-Universität Magdeburg
3 *
4 * This file is subject to the terms and conditions of the GNU Lesser General
5 * Public License v2.1. See the file LICENSE in the top level directory for more
6 * details.
7 */
8
24#include <errno.h>
25#include <stdint.h>
26/* if explicit_bzero() is provided by standard C lib, it may be defined in
27 * either `string.h` or `strings.h`, so just include both here */
28#include <string.h>
29#include <strings.h>
30#include <sys/types.h>
31
32#include "modules.h"
33
34#ifndef STRING_UTILS_H
35#define STRING_UTILS_H
36
37#ifdef __cplusplus
38extern "C" {
39#endif
40
41/* explicit_bzero is provided if:
42 * - glibc is used as C lib (only with board natvie)
43 * - newlib is used and __BSD_VISIBILE is set
44 * - except for ESP8266, which is using an old version of newlib without it
45 * - picolibc is used and __BSD_VISIBLE is set
46 *
47 * for all other cases, we provide it here
48 */
49#if !defined(CPU_NATIVE) \
50 && !(IS_USED(MODULE_PICOLIBC) && __BSD_VISIBLE) \
51 && !(IS_USED(MODULE_NEWLIB) && __BSD_VISIBLE && !defined(CPU_ESP8266))
52
65static inline void explicit_bzero(void *dest, size_t n_bytes)
66{
67 volatile uint8_t *tmp = dest;
68 for (size_t i = 0; i < n_bytes; i++) {
69 tmp[i] = 0;
70 }
71}
72#endif
73
92ssize_t strscpy(char *dest, const char *src, size_t count);
93
104const void *memchk(const void *data, uint8_t c, size_t len);
105
106#ifdef __cplusplus
107}
108#endif
109
110#endif /* STRING_UTILS_H */
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.
static void explicit_bzero(void *dest, size_t n_bytes)
Like memset(dest, 0, n_bytes), but secure.
Common macros and compiler attributes/pragmas configuration.
strings.h