string_utils.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2022 Otto-von-Guericke-Universität Magdeburg
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
23 #include <assert.h>
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 "flash_utils.h"
33 #include "modules.h"
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
43 typedef struct {
44  const char *start;
45  char *position;
46  size_t capacity;
48 
56 static inline void string_writer_init(string_writer_t *sw, void *buffer, size_t len)
57 {
58  assert(buffer && len);
59 
60  sw->start = buffer;
61  sw->position = buffer;
62  sw->capacity = len;
63  sw->position[0] = 0;
64 }
65 
71 static inline size_t string_writer_len(const string_writer_t *sw)
72 {
73  return sw->position - sw->start;
74 }
75 
81 static inline const char *string_writer_str(const string_writer_t *sw)
82 {
83  return sw->start;
84 }
85 
86 #ifndef DOXYGEN
90 #if IS_ACTIVE(HAS_FLASH_UTILS_ARCH)
91 #define __swprintf flash_swprintf
92 #else
93 #define __swprintf swprintf
94 __attribute__ ((format (printf, 2, 3)))
95 #endif
96 int __swprintf(string_writer_t *sw, FLASH_ATTR const char *restrict format, ...);
97 #else
110 int swprintf(string_writer_t *sw, FLASH_ATTR const char *restrict format, ...);
111 #endif /* DOXYGEN */
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 
188 void reverse_buf(void *buf, size_t len);
189 
190 #ifdef __cplusplus
191 }
192 #endif
193 
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:143
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:57
#define FLASH_ATTR
C type qualifier required to place a variable in flash.
Definition: flash_utils.h:64
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 i...
static const char * string_writer_str(const string_writer_t *sw)
Get the string contained by the string writer.
Definition: string_utils.h:81
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.
void reverse_buf(void *buf, size_t len)
Reverse the order of bytes in a 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:71
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
static void string_writer_init(string_writer_t *sw, void *buffer, size_t len)
Initialize a string writer structure.
Definition: string_utils.h:56
Common macros and compiler attributes/pragmas configuration.
strings.h
String Writer structure.
Definition: string_utils.h:43
char * position
current write pointer
Definition: string_utils.h:45
const char * start
start of the target buffer
Definition: string_utils.h:44
size_t capacity
remaining capacity of the buffer
Definition: string_utils.h:46