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 
9 #pragma once
10 
26 #include <assert.h>
27 #include <errno.h>
28 #include <stdint.h>
29 /* if explicit_bzero() is provided by standard C lib, it may be defined in
30  * either `string.h` or `strings.h`, so just include both here */
31 #include <string.h>
32 #include <strings.h>
33 #include <sys/types.h>
34 
35 #include "flash_utils.h"
36 #include "modules.h"
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
46 typedef struct {
47  const char *start;
48  char *position;
49  size_t capacity;
51 
59 static inline void string_writer_init(string_writer_t *sw, void *buffer, size_t len)
60 {
61  assert(buffer && len);
62 
63  sw->start = buffer;
64  sw->position = buffer;
65  sw->capacity = len;
66  sw->position[0] = 0;
67 }
68 
74 static inline size_t string_writer_len(const string_writer_t *sw)
75 {
76  return sw->position - sw->start;
77 }
78 
84 static inline const char *string_writer_str(const string_writer_t *sw)
85 {
86  return sw->start;
87 }
88 
89 #ifndef DOXYGEN
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
99 int __swprintf(string_writer_t *sw, FLASH_ATTR const char *restrict format, ...);
100 #else
113 int swprintf(string_writer_t *sw, FLASH_ATTR const char *restrict format, ...);
114 #endif /* DOXYGEN */
115 
116 #if IS_ACTIVE(HAS_FLASH_UTILS_ARCH)
117 #define swprintf(sw, fmt, ...) flash_swprintf(sw, TO_FLASH(fmt), ## __VA_ARGS__)
118 #endif
119 
120 /* explicit_bzero is provided if:
121  * - glibc is used as C lib (only with board natvie)
122  * - newlib is used and __BSD_VISIBILE is set
123  * - except for ESP8266, which is using an old version of newlib without it
124  * - picolibc is used and __BSD_VISIBLE is set
125  *
126  * for all other cases, we provide it here
127  */
128 #if !defined(CPU_NATIVE) \
129  && !(IS_USED(MODULE_PICOLIBC) && __BSD_VISIBLE) \
130  && !(IS_USED(MODULE_NEWLIB) && __BSD_VISIBLE && !defined(CPU_ESP8266))
131 
144 static inline void explicit_bzero(void *dest, size_t n_bytes)
145 {
146  volatile uint8_t *tmp = dest;
147  for (size_t i = 0; i < n_bytes; i++) {
148  tmp[i] = 0;
149  }
150 }
151 #endif
152 
171 ssize_t strscpy(char *dest, const char *src, size_t count);
172 
183 const void *memchk(const void *data, uint8_t c, size_t len);
184 
191 void reverse_buf(void *buf, size_t len);
192 
193 #ifdef __cplusplus
194 }
195 #endif
196 
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:135
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:67
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:84
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:74
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:144
static void string_writer_init(string_writer_t *sw, void *buffer, size_t len)
Initialize a string writer structure.
Definition: string_utils.h:59
Common macros and compiler attributes/pragmas configuration.
strings.h
String Writer structure.
Definition: string_utils.h:46
char * position
current write pointer
Definition: string_utils.h:48
const char * start
start of the target buffer
Definition: string_utils.h:47
size_t capacity
remaining capacity of the buffer
Definition: string_utils.h:49