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 <assert.h>
25 #include <errno.h>
26 #include <stdint.h>
27 /* if explicit_bzero() is provided by standard C lib, it may be defined in
28  * either `string.h` or `strings.h`, so just include both here */
29 #include <string.h>
30 #include <strings.h>
31 #include <sys/types.h>
32 
33 #include "flash_utils.h"
34 #include "modules.h"
35 
36 #ifndef STRING_UTILS_H
37 #define STRING_UTILS_H
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
47 typedef struct {
48  const char *start;
49  char *position;
50  size_t capacity;
52 
60 static inline void string_writer_init(string_writer_t *sw, void *buffer, size_t len)
61 {
62  assert(buffer && len);
63 
64  sw->start = buffer;
65  sw->position = buffer;
66  sw->capacity = len;
67  sw->position[0] = 0;
68 }
69 
75 static inline size_t string_writer_len(const string_writer_t *sw)
76 {
77  return sw->position - sw->start;
78 }
79 
85 static inline const char *string_writer_str(const string_writer_t *sw)
86 {
87  return sw->start;
88 }
89 
93 #if IS_ACTIVE(HAS_FLASH_UTILS_ARCH)
94 #define __swprintf flash_swprintf
95 #else
96 #define __swprintf swprintf
97 __attribute__ ((format (printf, 2, 3)))
98 #endif
111 int __swprintf(string_writer_t *sw, FLASH_ATTR const char *restrict format, ...);
112 
113 #if IS_ACTIVE(HAS_FLASH_UTILS_ARCH)
114 #define swprintf(sw, fmt, ...) flash_swprintf(sw, TO_FLASH(fmt), ## __VA_ARGS__)
115 #endif
116 
117 /* explicit_bzero is provided if:
118  * - glibc is used as C lib (only with board natvie)
119  * - newlib is used and __BSD_VISIBILE is set
120  * - except for ESP8266, which is using an old version of newlib without it
121  * - picolibc is used and __BSD_VISIBLE is set
122  *
123  * for all other cases, we provide it here
124  */
125 #if !defined(CPU_NATIVE) \
126  && !(IS_USED(MODULE_PICOLIBC) && __BSD_VISIBLE) \
127  && !(IS_USED(MODULE_NEWLIB) && __BSD_VISIBLE && !defined(CPU_ESP8266))
128 
141 static inline void explicit_bzero(void *dest, size_t n_bytes)
142 {
143  volatile uint8_t *tmp = dest;
144  for (size_t i = 0; i < n_bytes; i++) {
145  tmp[i] = 0;
146  }
147 }
148 #endif
149 
168 ssize_t strscpy(char *dest, const char *src, size_t count);
169 
180 const void *memchk(const void *data, uint8_t c, size_t len);
181 
182 #ifdef __cplusplus
183 }
184 #endif
185 
186 #endif /* STRING_UTILS_H */
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:136
Utility functions, macros, and types for read-only memory.
#define printf(...)
A wrapper for the printf() function that passes arguments through unmodified, but fails to compile if...
Definition: stdio.h:60
#define FLASH_ATTR
C type qualifier required to place a variable in flash.
Definition: flash_utils.h:68
static const char * string_writer_str(const string_writer_t *sw)
Get the string contained by the string writer.
Definition: string_utils.h:85
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.
static size_t string_writer_len(const string_writer_t *sw)
Get the size of the string contained by the string writer.
Definition: string_utils.h:75
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.
Definition: string_utils.h:141
#define __swprintf
internal helper macro
Definition: string_utils.h:96
static void string_writer_init(string_writer_t *sw, void *buffer, size_t len)
Initialize a string writer structure.
Definition: string_utils.h:60
Common macros and compiler attributes/pragmas configuration.
strings.h
String Writer structure.
Definition: string_utils.h:47
char * position
current write pointer
Definition: string_utils.h:49
const char * start
start of the target buffer
Definition: string_utils.h:48
size_t capacity
remaining capacity of the buffer
Definition: string_utils.h:50