1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
// SPDX-License-Identifier: LGPL-2.1+
#ifndef __NM_STR_BUF_H__
#define __NM_STR_BUF_H__
#include "nm-shared-utils.h"
#include "nm-secret-utils.h"
/*****************************************************************************/
/* NMStrBuf is not unlike GString. The main difference is that it can use
* nm_explicit_bzero() when growing the buffer. */
typedef struct _NMStrBuf {
char *_priv_str;
/* The unions only exist because we allow/encourage read-only access
* to the "len" and "allocated" fields, but modifying the fields is
* only allowed to the NMStrBuf implementation itself. */
union {
/*const*/ gsize len;
gsize _priv_len;
};
union {
/*const*/ gsize allocated;
gsize _priv_allocated;
};
bool _priv_do_bzero_mem;
} NMStrBuf;
/*****************************************************************************/
static inline void
_nm_str_buf_assert (NMStrBuf *strbuf)
{
nm_assert (strbuf);
nm_assert (strbuf->_priv_str);
nm_assert (strbuf->_priv_allocated > 0);
nm_assert (strbuf->_priv_len <= strbuf->_priv_allocated);
}
static inline void
nm_str_buf_init (NMStrBuf *strbuf,
gsize len,
bool do_bzero_mem)
{
nm_assert (strbuf);
nm_assert (len > 0);
strbuf->_priv_str = g_malloc (len);
strbuf->_priv_allocated = len;
strbuf->_priv_len = 0;
strbuf->_priv_do_bzero_mem = do_bzero_mem;
_nm_str_buf_assert (strbuf);
}
void _nm_str_buf_ensure_size (NMStrBuf *strbuf,
gsize new_size,
gboolean reserve_exact);
static inline void
nm_str_buf_maybe_expand (NMStrBuf *strbuf,
gsize reserve,
gboolean reserve_exact)
{
_nm_str_buf_assert (strbuf);
/* currently we always require to reserve a non-zero number of bytes. */
nm_assert (reserve > 0);
nm_assert (strbuf->_priv_len < G_MAXSIZE - reserve);
/* @reserve is the extra space that we require. */
if (G_UNLIKELY (reserve > strbuf->_priv_allocated - strbuf->_priv_len))
_nm_str_buf_ensure_size (strbuf, strbuf->_priv_len + reserve, reserve_exact);
}
/*****************************************************************************/
/**
* nm_str_buf_set_size:
* @strbuf: the initialized #NMStrBuf
* @new_len: the new length
* @honor_do_bzero_mem: if %TRUE, the shrinked memory will be cleared, if
* do_bzero_mem is set. This should be usually set to %TRUE, unless
* you know that the shrinked memory does not contain data that requires to be
* cleared. When growing the size, this value has no effect.
* @reserve_exact: when growing the buffer, reserve the exact amount of bytes.
* If %FALSE, the buffer may allocate more memory than requested to grow
* exponentially.
*
* This is like g_string_set_size(). If new_len is smaller than the
* current length, the string gets truncated (excess memory will be cleared).
*
* When extending the length, the added bytes are undefined (like with
* g_string_set_size(). Likewise, if you first pre-allocate a buffer with
* nm_str_buf_maybe_expand(), then write to the bytes, and finally set
* the appropriate size, then that works as expected (by not clearing the
* pre-existing, grown buffer).
*/
static inline void
nm_str_buf_set_size (NMStrBuf *strbuf,
gsize new_len,
gboolean honor_do_bzero_mem,
gboolean reserve_exact)
{
_nm_str_buf_assert (strbuf);
if (new_len < strbuf->_priv_len) {
if ( honor_do_bzero_mem
&& strbuf->_priv_do_bzero_mem) {
/* we only clear the memory that we wrote to. */
nm_explicit_bzero (&strbuf->_priv_str[new_len], strbuf->_priv_len - new_len);
}
} else if (new_len > strbuf->_priv_len) {
nm_str_buf_maybe_expand (strbuf,
new_len - strbuf->_priv_len + (reserve_exact ? 0u : 1u),
reserve_exact);
} else
return;
strbuf->_priv_len = new_len;
}
/*****************************************************************************/
static inline void
nm_str_buf_erase (NMStrBuf *strbuf,
gsize pos,
gssize len,
gboolean honor_do_bzero_mem)
{
gsize new_len;
_nm_str_buf_assert (strbuf);
nm_assert (pos <= strbuf->_priv_len);
if (len == 0)
return;
if (len < 0) {
/* truncate the string before pos */
nm_assert (len == -1);
new_len = pos;
} else {
gsize l = len;
nm_assert (l <= strbuf->_priv_len - pos);
new_len = strbuf->_priv_len - l;
if (pos + l < strbuf->_priv_len) {
memmove (&strbuf->_priv_str[pos],
&strbuf->_priv_str[pos + l],
strbuf->_priv_len - (pos + l));
}
}
nm_assert (new_len <= strbuf->_priv_len);
nm_str_buf_set_size (strbuf, new_len, honor_do_bzero_mem, TRUE);
}
/*****************************************************************************/
static inline void
nm_str_buf_append_c (NMStrBuf *strbuf,
char ch)
{
nm_str_buf_maybe_expand (strbuf, 2, FALSE);
strbuf->_priv_str[strbuf->_priv_len++] = ch;
}
static inline void
nm_str_buf_append_c2 (NMStrBuf *strbuf,
char ch0,
char ch1)
{
nm_str_buf_maybe_expand (strbuf, 3, FALSE);
strbuf->_priv_str[strbuf->_priv_len++] = ch0;
strbuf->_priv_str[strbuf->_priv_len++] = ch1;
}
static inline void
nm_str_buf_append_c4 (NMStrBuf *strbuf,
char ch0,
char ch1,
char ch2,
char ch3)
{
nm_str_buf_maybe_expand (strbuf, 5, FALSE);
strbuf->_priv_str[strbuf->_priv_len++] = ch0;
strbuf->_priv_str[strbuf->_priv_len++] = ch1;
strbuf->_priv_str[strbuf->_priv_len++] = ch2;
strbuf->_priv_str[strbuf->_priv_len++] = ch3;
}
static inline void
nm_str_buf_append_len (NMStrBuf *strbuf,
const char *str,
gsize len)
{
_nm_str_buf_assert (strbuf);
if (len > 0) {
nm_str_buf_maybe_expand (strbuf, len + 1, FALSE);
memcpy (&strbuf->_priv_str[strbuf->_priv_len], str, len);
strbuf->_priv_len += len;
}
}
static inline void
nm_str_buf_append (NMStrBuf *strbuf,
const char *str)
{
nm_assert (str);
nm_str_buf_append_len (strbuf, str, strlen (str));
}
void nm_str_buf_append_printf (NMStrBuf *strbuf,
const char *format,
...) _nm_printf (2, 3);
static inline void
nm_str_buf_ensure_trailing_c (NMStrBuf *strbuf, char ch)
{
_nm_str_buf_assert (strbuf);
if ( strbuf->_priv_len == 0
|| strbuf->_priv_str[strbuf->_priv_len - 1] != ch)
nm_str_buf_append_c (strbuf, ch);
}
/*****************************************************************************/
static inline gboolean
nm_str_buf_is_initalized (NMStrBuf *strbuf)
{
nm_assert (strbuf);
#if NM_MORE_ASSERTS
if (strbuf->_priv_str)
_nm_str_buf_assert (strbuf);
#endif
return !!strbuf->_priv_str;
}
/**
* nm_str_buf_get_str:
* @strbuf: the #NMStrBuf instance
*
* Returns the NUL terminated internal string.
*
* While constructing the string, the intermediate buffer
* is not NUL terminated (this makes it different from GString).
* Usually, one would build the string and retrieve it at the
* end with nm_str_buf_finalize(). This returns the NUL terminated
* buffer that was appended so far. Contrary to nm_str_buf_finalize(), you
* can still append more data to the buffer and this does not transfer ownership
* of the string.
*
* Returns: (transfer none): the internal string. The string
* is of length "strbuf->len", which may be larger if the
* returned string contains NUL characters (binary). The terminating
* NUL character is always present after "strbuf->len" characters.
*/
static inline const char *
nm_str_buf_get_str (NMStrBuf *strbuf)
{
nm_str_buf_maybe_expand (strbuf, 1, FALSE);
strbuf->_priv_str[strbuf->_priv_len] = '\0';
return strbuf->_priv_str;
}
static inline char *
nm_str_buf_get_str_unsafe (NMStrBuf *strbuf)
{
_nm_str_buf_assert (strbuf);
return strbuf->_priv_str;
}
/**
* nm_str_buf_finalize:
* @strbuf: an initilized #NMStrBuf
* @out_len: (out): (allow-none): optional output
* argument with the length of the returned string.
*
* Returns: (transfer full): the string of the buffer
* which must be freed by the caller. The @strbuf
* is afterwards in undefined state, though it can be
* reused after nm_str_buf_init(). */
static inline char *
nm_str_buf_finalize (NMStrBuf *strbuf,
gsize *out_len)
{
nm_str_buf_maybe_expand (strbuf, 1, TRUE);
strbuf->_priv_str[strbuf->_priv_len] = '\0';
NM_SET_OUT (out_len, strbuf->_priv_len);
/* the buffer is in invalid state afterwards, however, we clear it
* so far, that nm_auto_str_buf and nm_str_buf_destroy() is happy. */
return g_steal_pointer (&strbuf->_priv_str);
}
/**
* nm_str_buf_destroy:
* @strbuf: an initialized #NMStrBuf
*
* Frees the associated memory of @strbuf. The buffer
* afterwards is in undefined state, but can be re-initialized
* with nm_str_buf_init().
*/
static inline void
nm_str_buf_destroy (NMStrBuf *strbuf)
{
if (!strbuf->_priv_str)
return;
_nm_str_buf_assert (strbuf);
if (strbuf->_priv_do_bzero_mem)
nm_explicit_bzero (strbuf->_priv_str, strbuf->_priv_len);
g_free (strbuf->_priv_str);
/* the buffer is in invalid state afterwards, however, we clear it
* so far, that nm_auto_str_buf is happy when calling
* nm_str_buf_destroy() again. */
strbuf->_priv_str = NULL;
}
#define nm_auto_str_buf nm_auto (nm_str_buf_destroy)
#endif /* __NM_STR_BUF_H__ */
|