Debian Codegraph @

src/basic/iovec-wrapper.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdio.h>
4
5 #include "alloc-util.h"
6 #include "iovec-util.h"
7 #include "iovec-wrapper.h"
8 #include "string-util.h"
9 #include "unaligned.h"
10
11 void iovw_done(struct iovec_wrapper *iovw) {
12 assert(iovw);
13
14 iovw->iovec = mfree(iovw->iovec);
15 iovw->count = 0;
16 }
17
18 void iovw_done_free(struct iovec_wrapper *iovw) {
19 assert(iovw);
20
21 FOREACH_ARRAY(i, iovw->iovec, iovw->count)
22 iovec_done(i);
23
24 iovw_done(iovw);
25 }
26
27 struct iovec_wrapper* iovw_free(struct iovec_wrapper *iovw) {
28 if (!iovw)
29 return NULL;
30
31 iovw_done(iovw);
32 return mfree(iovw);
33 }
34
35 struct iovec_wrapper* iovw_free_free(struct iovec_wrapper *iovw) {
36 if (!iovw)
37 return NULL;
38
39 iovw_done_free(iovw);
40 return mfree(iovw);
41 }
42
43 int iovw_compare(const struct iovec_wrapper *a, const struct iovec_wrapper *b) {
44 int r;
45
46 if (a == b)
47 return 0;
48
49 if (!a || !b)
50 return CMP(!!a, !!b);
51
52 /* Note, this performs structural (element-by-element) comparison, not content-based comparison.
53 * Two wrappers with identical concatenated content but different element boundaries
54 * (e.g., ["fo","o"] vs ["f","oo"]) will not compare as equal. */
55
56 for (size_t i = 0, n = MIN(a->count, b->count); i < n; i++) {
57 r = iovec_memcmp(a->iovec + i, b->iovec + i);
58 if (r != 0)
59 return r;
60 }
61
62 return CMP(a->count, b->count);
63 }
64
65 int iovw_put_full(struct iovec_wrapper *iovw, bool accept_zero, void *data, size_t len) {
66 assert(iovw);
67 assert(data || len == 0);
68
69 if (len == 0 && !accept_zero)
70 return 0;
71
72 if (iovw->count >= IOV_MAX)
73 return -E2BIG;
74
75 if (!GREEDY_REALLOC(iovw->iovec, iovw->count + 1))
76 return -ENOMEM;
77
78 iovw->iovec[iovw->count++] = IOVEC_MAKE(data, len);
79 return 1;
80 }
81
82 int iovw_put_iov_full(struct iovec_wrapper *iovw, bool accept_zero, const struct iovec *iov) {
83 assert(iovw);
84
85 if (!iov)
86 return 0;
87
88 return iovw_put_full(iovw, accept_zero, iov->iov_base, iov->iov_len);
89 }
90
91 int iovw_put_iovw_full(struct iovec_wrapper *iovw, bool accept_zero, const struct iovec_wrapper *source) {
92 int r;
93
94 assert(iovw);
95
96 if (iovw_isempty(source))
97 return 0;
98
99 /* We will reallocate iovw->iovec, hence the source cannot point to the same object. */
100 if (iovw == source)
101 return -EINVAL;
102
103 if (iovw->count > SIZE_MAX - source->count)
104 return -E2BIG;
105 if (iovw->count + source->count > IOV_MAX)
106 return -E2BIG;
107
108 if (accept_zero) {
109 if (!GREEDY_REALLOC_APPEND(iovw->iovec, iovw->count, source->iovec, source->count))
110 return -ENOMEM;
111
112 return 0;
113 }
114
115 /* When accept_zero is false, we need to filter zero length iovec in source. */
116 size_t original_count = iovw->count;
117
118 FOREACH_ARRAY(iovec, source->iovec, source->count) {
119 r = iovw_put_iov_full(iovw, accept_zero, iovec);
120 if (r < 0)
121 goto rollback;
122 }
123
124 return 0;
125
126 rollback:
127 iovw->count = original_count;
128 return r;
129 }
130
131 int iovw_consume_full(struct iovec_wrapper *iovw, bool accept_zero, void *data, size_t len) {
132 /* Move data into iovw or free on error */
133 int r;
134
135 r = iovw_put_full(iovw, accept_zero, data, len);
136 if (r <= 0)
137 free(data);
138
139 return r;
140 }
141
142 int iovw_consume_iov_full(struct iovec_wrapper *iovw, bool accept_zero, struct iovec *iov) {
143 int r;
144
145 assert(iovw);
146
147 if (!iov)
148 return 0;
149
150 r = iovw_put_iov_full(iovw, accept_zero, iov);
151 if (r <= 0)
152 iovec_done(iov);
153 else
154 /* On success, iov->iov_base is now owned by iovw. Let's emptify iov, but do not call
155 * iovec_done(), of course. */
156 *iov = (struct iovec) {};
157
158 return r;
159 }
160
161 int iovw_extend_full(struct iovec_wrapper *iovw, bool accept_zero, const void *data, size_t len) {
162 assert(iovw);
163 assert(data || len == 0);
164
165 if (len == 0)
166 return iovw_put_full(iovw, accept_zero, /* data= */ NULL, /* len= */ 0);
167
168 void *c = memdup(data, len);
169 if (!c)
170 return -ENOMEM;
171
172 return iovw_consume_full(iovw, accept_zero, c, len);
173 }
174
175 int iovw_extend_iov_full(struct iovec_wrapper *iovw, bool accept_zero, const struct iovec *iov) {
176 assert(iovw);
177
178 if (!iov)
179 return 0;
180
181 return iovw_extend_full(iovw, accept_zero, iov->iov_base, iov->iov_len);
182 }
183
184 int iovw_extend_iovw_full(struct iovec_wrapper *iovw, bool accept_zero, const struct iovec_wrapper *source) {
185 int r;
186
187 assert(iovw);
188
189 /* This duplicates the source and merges it into the iovw. */
190
191 if (iovw_isempty(source))
192 return 0;
193
194 /* iovw->iovec will be reallocated in the loop below, hence source cannot point to the same object. */
195 if (iovw == source)
196 return -EINVAL;
197
198 if (iovw->count > SIZE_MAX - source->count)
199 return -E2BIG;
200 if (iovw->count + source->count > IOV_MAX)
201 return -E2BIG;
202
203 size_t original_count = iovw->count;
204
205 FOREACH_ARRAY(iovec, source->iovec, source->count) {
206 r = iovw_extend_iov_full(iovw, accept_zero, iovec);
207 if (r < 0)
208 goto rollback;
209 }
210
211 return 0;
212
213 rollback:
214 for (size_t i = original_count; i < iovw->count; i++)
215 iovec_done(iovw->iovec + i);
216
217 iovw->count = original_count;
218 return r;
219 }
220
221 int iovw_put_string_field_full(struct iovec_wrapper *iovw, bool replace, const char *field, const char *value) {
222 _cleanup_free_ char *x = NULL;
223 int r;
224
225 assert(iovw);
226
227 x = strjoin(field, value);
228 if (!x)
229 return -ENOMEM;
230
231 if (replace)
232 FOREACH_ARRAY(iovec, iovw->iovec, iovw->count)
233 if (memory_startswith(iovec->iov_base, iovec->iov_len, field)) {
234 iovec->iov_len = strlen(x);
235 free_and_replace(iovec->iov_base, x);
236 return 0;
237 }
238
239 r = iovw_put(iovw, x, strlen(x));
240 if (r >= 0)
241 TAKE_PTR(x);
242
243 return r;
244 }
245
246 int iovw_put_string_fieldf_full(struct iovec_wrapper *iovw, bool replace, const char *field, const char *format, ...) {
247 _cleanup_free_ char *value = NULL;
248 va_list ap;
249 int r;
250
251 assert(format);
252
253 va_start(ap, format);
254 r = vasprintf(&value, format, ap);
255 va_end(ap);
256 if (r < 0)
257 return -ENOMEM;
258
259 return iovw_put_string_field_full(iovw, replace, field, value);
260 }
261
262 int iovw_put_string_field_free(struct iovec_wrapper *iovw, const char *field, char *value) {
263 _cleanup_free_ _unused_ char *free_ptr = value;
264
265 return iovw_put_string_field(iovw, field, value);
266 }
267
268 void iovw_rebase(struct iovec_wrapper *iovw, void *old, void *new) {
269 assert(iovw);
270
271 FOREACH_ARRAY(i, iovw->iovec, iovw->count) {
272 assert(i->iov_base >= old);
273 i->iov_base = (uint8_t*) i->iov_base - (uint8_t*) old + (uint8_t*) new;
274 }
275 }
276
277 size_t iovw_size(const struct iovec_wrapper *iovw) {
278 if (iovw_isempty(iovw))
279 return 0;
280
281 return iovec_total_size(iovw->iovec, iovw->count);
282 }
283
284 int iovw_concat(const struct iovec_wrapper *iovw, struct iovec *ret) {
285 assert(iovw);
286 assert(ret);
287
288 /* Squish a series of iovecs into a single iovec. */
289
290 size_t len = iovw_size(iovw);
291 if (len == SIZE_MAX)
292 return -E2BIG; /* Prevent theoretical overflow */
293
294 /* Always allocate one more byte to make the result usable as a NUL-terminated string. */
295 _cleanup_free_ uint8_t *buf = malloc(len + 1);
296 if (!buf)
297 return -ENOMEM;
298
299 uint8_t *p = buf;
300 FOREACH_ARRAY(i, iovw->iovec, iovw->count)
301 p = mempcpy_safe(p, i->iov_base, i->iov_len);
302
303 *p = 0;
304
305 *ret = IOVEC_MAKE(TAKE_PTR(buf), len);
306 return 0;
307 }
308
309 char* iovw_to_cstring(const struct iovec_wrapper *iovw) {
310 assert(iovw);
311
312 /* Squish a series of iovecs into a C string. Embedded NULs are not allowed.
313 * The caller is expected to filter them out when populating the data. */
314
315 _cleanup_(iovec_done) struct iovec iov = {};
316 if (iovw_concat(iovw, &iov) < 0)
317 return NULL;
318
319 assert(!memchr(iov.iov_base, 0, iov.iov_len));
320 return TAKE_PTR(iov.iov_base);
321 }
322
323 int iovec_split(const struct iovec *iov, size_t length_size, struct iovec_wrapper *ret) {
324 int r;
325
326 assert(IN_SET(length_size, 1, 2, 4));
327 assert(ret);
328
329 /* This parses the input iovec as length-prefixed data, and stores the result as iovec_wrapper.
330 * Note, zero-length entries are silently dropped. */
331
332 if (!iovec_is_set(iov)) {
333 *ret = (struct iovec_wrapper) {};
334 return 0;
335 }
336
337 _cleanup_(iovw_done_free) struct iovec_wrapper iovw = {};
338 for (struct iovec i = *iov; iovec_is_set(&i); ) {
339 if (i.iov_len < length_size)
340 return -EBADMSG;
341
342 size_t len;
343 switch (length_size) {
344 case 1:
345 len = *(uint8_t*) i.iov_base;
346 break;
347 case 2:
348 len = unaligned_read_be16(i.iov_base);
349 break;
350 case 4:
351 len = unaligned_read_be32(i.iov_base);
352 break;
353 default:
354 assert_not_reached();
355 }
356
357 iovec_inc(&i, length_size);
358
359 if (len == 0)
360 continue;
361
362 if (i.iov_len < len)
363 return -EBADMSG;
364
365 r = iovw_extend(&iovw, i.iov_base, len);
366 if (r < 0)
367 return r;
368
369 iovec_inc(&i, len);
370 }
371
372 *ret = TAKE_STRUCT(iovw);
373 return 0;
374 }
375
376 int iovw_merge(const struct iovec_wrapper *iovw, size_t length_size, struct iovec *ret) {
377 assert(IN_SET(length_size, 1, 2, 4));
378 assert(ret);
379
380 /* This is the inverse of iovec_split(), and builds a length-prefixed data from iovec_wrapper.
381 * Note, zero-length entries are silently dropped. */
382
383 size_t sz = iovw_size(iovw);
384 if (sz == 0) {
385 *ret = (struct iovec) {};
386 return 0;
387 }
388 if (sz == SIZE_MAX)
389 return -E2BIG;
390
391 if (size_multiply_overflow(length_size, iovw->count))
392 return -E2BIG;
393
394 sz = size_add(sz, iovw->count * length_size);
395 if (sz == SIZE_MAX)
396 return -E2BIG;
397
398 _cleanup_free_ uint8_t *buf = new(uint8_t, sz);
399 if (!buf)
400 return -ENOMEM;
401
402 uint8_t *p = buf;
403 FOREACH_ARRAY(iov, iovw->iovec, iovw->count) {
404 if (iov->iov_len == 0)
405 continue;
406
407 switch (length_size) {
408 case 1:
409 if (iov->iov_len > UINT8_MAX)
410 return -ERANGE;
411
412 *p = iov->iov_len;
413 break;
414 case 2:
415 if (iov->iov_len > UINT16_MAX)
416 return -ERANGE;
417
418 unaligned_write_be16(p, iov->iov_len);
419 break;
420 case 4:
421 if (iov->iov_len > UINT32_MAX)
422 return -ERANGE;
423
424 unaligned_write_be32(p, iov->iov_len);
425 break;
426 default:
427 assert_not_reached();
428 }
429 p += length_size;
430
431 p = mempcpy(p, iov->iov_base, iov->iov_len);
432 }
433
434 assert(sz >= (size_t) (p - buf));
435 sz = p - buf;
436
437 *ret = IOVEC_MAKE(TAKE_PTR(buf), sz);
438 return 0;
439 }