1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3 * Copyright (c) 2014, STMicroelectronics International N.V.
4 */
5
6 /*
7 * This file provides extensions for functions not defined in <string.h>
8 */
9
10 #ifndef __STRING_EXT_H
11 #define __STRING_EXT_H
12
13 #include <stddef.h>
14 #include <sys/cdefs.h>
15
16 /*
17 * Copy src to string dst of siz size. At most siz-1 characters
18 * will be copied. Always NUL terminates (unless siz == 0).
19 * Returns strlen(src); if retval >= siz, truncation occurred.
20 */
21 size_t strlcpy(char *dst, const char *src, size_t size);
22 size_t strlcat(char *dst, const char *src, size_t size);
23
24 /* A constant-time version of memcmp() */
25 int consttime_memcmp(const void *p1, const void *p2, size_t nb);
26
27 /* Deprecated. For backward compatibility. */
buf_compare_ct(const void * s1,const void * s2,size_t n)28 static inline int buf_compare_ct(const void *s1, const void *s2, size_t n)
29 {
30 return consttime_memcmp(s1, s2, n);
31 }
32
33 /* Variant of strdup() that uses nex_malloc() instead of malloc() */
34 char *nex_strdup(const char *s);
35
36 /*
37 * Like memset(s, 0, count) but prevents the compiler from optimizing the call
38 * away. Such "dead store elimination" optimizations typically occur when
39 * clearing a *local* variable that is not used after it is cleared; but
40 * link-time optimization (LTO) can also trigger code elimination in other
41 * circumstances. See "Dead Store Elimination (Still) Considered Harmful" [1]
42 * for details and examples (and note that the Cland compiler enables LTO by
43 * default!).
44 *
45 * [1] https://www.usenix.org/system/files/conference/usenixsecurity17/sec17-yang.pdf
46 *
47 * Practically speaking:
48 *
49 * - Use memzero_explicit() to *clear* (as opposed to initialize) *sensitive*
50 * data (such as keys, passwords, cryptographic state);
51 * - Otherwise, use memset().
52 */
53 void memzero_explicit(void *s, size_t count);
54
55 /*
56 * ins_array_elem() - insert an element in an array
57 * @base: start of the array
58 * @elem_count: the number of elements in the array
59 * @elem_size: the size of each element in the array
60 * @pos: element position counted in units of @elem_size
61 * @elem: pointer the the element to inser or NULL
62 *
63 * Makes room in the array at @pos by moving the element after this
64 * position one position further back.
65 *
66 * If @elem is non-NULL it's copied into the array at the indicated
67 * position.
68 *
69 * Returns a pointer to the inserted element.
70 */
71 void *ins_array_elem(void *base, size_t elem_count, size_t elem_size,
72 size_t pos, const void *elem);
73
74 /*
75 * ins_array_elem_zero_init() - insert a zero-initialized element in an array
76 * @base: start of the array
77 * @elem_count: the number of elements in the array
78 * @elem_size: the size of each element in the array
79 * @pos: element position counted in units of @elem_size
80 *
81 * Makes room in the array at @pos by moving the element after this
82 * position one position further back. The free position is
83 * zero-initialized.
84 *
85 * Returns a pointer to the free position.
86 */
87 void *ins_array_elem_zero_init(void *base, size_t elem_count, size_t elem_size,
88 size_t pos);
89
90 /*
91 * rem_array_elem() - remove an element from an array
92 * @base: start of the array
93 * @elem_count: the number of elements in the array
94 * @elem_size: the size of each element in the array
95 * @pos: element position counted in units of @elem_size
96 *
97 * Removes the element at @pos by advancing the element after this position
98 * to fill the space.
99 */
100 void rem_array_elem(void *base, size_t elem_count, size_t elem_size,
101 size_t pos);
102
103 /*
104 * rem_array_elem_zero_pad() - remove an element from an array
105 * @base: start of the array
106 * @elem_count: the number of elements in the array
107 * @elem_size: the size of each element in the array
108 * @pos: element position counted in units of @elem_size
109 *
110 * Removes the element at @pos by advancing the element after this position
111 * to fill the space. The now unused element at the end of the array is
112 * zero-initialized.
113 */
114 void rem_array_elem_zero_pad(void *base, size_t elem_count, size_t elem_size,
115 size_t pos);
116
117 #endif /* __STRING_EXT_H */
118