Loading...
Searching...
No Matches
string_utils.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2022 Otto-von-Guericke-Universität Magdeburg
3 * SPDX-FileCopyrightText: 2022-2026 ML!PA Consulting GmbH
4 * SPDX-License-Identifier: LGPL-2.1-only
5 */
6
7#pragma once
8
25
26#include <assert.h>
27#include <stdint.h>
28/* if explicit_bzero() is provided by standard C lib, it may be defined in
29 * either `string.h` or `strings.h`, so just include both here */
30#include <string.h>
31#include <strings.h>
32#include <sys/types.h>
33
34#include "compiler_hints.h"
35#include "flash_utils.h"
36#include "modules.h"
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
46typedef struct {
47 const char *start;
48 char *position;
49 size_t capacity;
51
59ACCESS(write_only, 2, 3)
60static 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
75static inline size_t string_writer_len(const string_writer_t *sw)
76{
77 return sw->position - sw->start;
78}
79
85static inline const char *string_writer_str(const string_writer_t *sw)
86{
87 return sw->start;
88}
89
90#ifndef DOXYGEN
94# if IS_ACTIVE(HAS_FLASH_UTILS_ARCH)
95# define __swprintf flash_swprintf
96# else
97# define __swprintf swprintf
98__attribute__((format(printf, 2, 3)))
99# endif
100int __swprintf(string_writer_t *sw, FLASH_ATTR const char *restrict format, ...);
101#else
114int swprintf(string_writer_t *sw, FLASH_ATTR const char *restrict format, ...);
115#endif /* DOXYGEN */
116
117#if IS_ACTIVE(HAS_FLASH_UTILS_ARCH)
118# define swprintf(sw, fmt, ...) flash_swprintf(sw, TO_FLASH(fmt), ##__VA_ARGS__)
119#endif
120
121/* explicit_bzero is provided if:
122 * - glibc is used as C lib (only with board natvie)
123 * - newlib is used and __BSD_VISIBLE is set
124 * - except for ESP8266, which is using an old version of newlib without it
125 * - picolibc is used and __BSD_VISIBLE is set
126 *
127 * for all other cases, we provide it here
128 */
129#if !defined(CPU_NATIVE) \
130 && !(IS_USED(MODULE_PICOLIBC) && __BSD_VISIBLE) \
131 && !(IS_USED(MODULE_NEWLIB) && __BSD_VISIBLE && !defined(CPU_ESP8266))
132
145static inline void explicit_bzero(void *dest, size_t n_bytes)
146{
147 volatile uint8_t *tmp = dest;
148 for (size_t i = 0; i < n_bytes; i++) {
149 tmp[i] = 0;
150 }
151}
152#endif
153
172ACCESS(write_only, 1, 3)
173ssize_t strscpy(char *dest, const char *src, size_t count);
174
185ACCESS(read_only, 1, 3)
186const void *memchk(const void *data, uint8_t c, size_t len);
187
194ACCESS(read_write, 1, 2)
195void reverse_buf(void *buf, size_t len);
196
207ACCESS(read_write, 1, 3)
208ACCESS(read_only, 2, 3)
209void memxor(void *dst, void *src, size_t size);
210
220ACCESS(write_only, 1, 3)
221ACCESS(read_only, 2, 3)
222void memcpy_reversed(void *restrict dst, const void *restrict src, size_t size);
223
224#ifdef __cplusplus
225}
226#endif
227
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Definition assert.h:143
Common macros and compiler attributes/pragmas configuration.
#define ACCESS(mode, ptr_idx, size_idx)
Emit an attribute (if supported by the compiler) that declares how a function will access its paramet...
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: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...
void memcpy_reversed(void *restrict dst, const void *restrict src, size_t size)
Copies src to dst in reverse order.
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.
const void * memchk(const void *data, uint8_t c, size_t len)
Check if the entire buffer is filled with the same byte.
void memxor(void *dst, void *src, size_t size)
XOR the bytes of two buffers and store the result in dst.
static void explicit_bzero(void *dest, size_t n_bytes)
Like memset(dest, 0, n_bytes), but secure.
static const char * string_writer_str(const string_writer_t *sw)
Get the string contained by the string writer.
static void string_writer_init(string_writer_t *sw, void *buffer, size_t len)
Initialize a string writer structure.
Common macros and compiler attributes/pragmas configuration.
strings.h
String Writer structure.
char * position
current write pointer
const char * start
start of the target buffer
size_t capacity
remaining capacity of the buffer