Debian Codegraph @

src/basic/iovec-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "iovec-util.h"
5 #include "string-util.h"
6
7 static const uint8_t nul_byte = 0;
8
9 const struct iovec iovec_nul_byte = {
10 .iov_base = (void*) &nul_byte,
11 .iov_len = 1,
12 };
13
14 const struct iovec iovec_empty = {
15 .iov_base = (void*) &nul_byte,
16 .iov_len = 0,
17 };
18
19 size_t iovec_total_size(const struct iovec *iovec, size_t n) {
20 size_t sum = 0;
21
22 assert(iovec || n == 0);
23
24 FOREACH_ARRAY(j, iovec, n) {
25 if (j->iov_len > SIZE_MAX - sum)
26 return SIZE_MAX; /* Indicate overflow. */
27 sum += j->iov_len;
28 }
29
30 return sum;
31 }
32
33 bool iovec_inc_many(struct iovec *iovec, size_t n, size_t k) {
34 assert(iovec || n == 0);
35
36 /* Returns true if there is nothing else to send (bytes written cover all of the iovec),
37 * false if there's still work to do. */
38
39 bool have = false;
40 FOREACH_ARRAY(j, iovec, n) {
41 if (j->iov_len == 0)
42 continue;
43 if (k == 0)
44 return false;
45
46 size_t sub = MIN(j->iov_len, k);
47 iovec_inc(j, sub);
48 k -= sub;
49
50 have = have || iovec_is_set(j);
51 }
52
53 assert(k == 0); /* Anything else would mean that we wrote more bytes than available,
54 * or the kernel reported writing more bytes than sent. */
55
56 return !have;
57 }
58
59 struct iovec* iovec_make_string(struct iovec *iovec, const char *s) {
60 assert(iovec);
61
62 *iovec = IOVEC_MAKE(s, strlen_ptr(s));
63 return iovec;
64 }
65
66 void iovec_done_erase(struct iovec *iovec) {
67 assert(iovec);
68
69 iovec->iov_base = erase_and_free(iovec->iov_base);
70 iovec->iov_len = 0;
71 }
72
73 char* set_iovec_string_field(struct iovec *iovec, size_t *n_iovec, const char *field, const char *value) {
74 char *x;
75
76 assert(iovec);
77 assert(n_iovec);
78
79 x = strjoin(field, value);
80 if (x)
81 iovec[(*n_iovec)++] = IOVEC_MAKE_STRING(x);
82 return x;
83 }
84
85 char* set_iovec_string_field_free(struct iovec *iovec, size_t *n_iovec, const char *field, char *value) {
86 char *x;
87
88 assert(iovec);
89 assert(n_iovec);
90
91 x = set_iovec_string_field(iovec, n_iovec, field, value);
92 free(value);
93 return x;
94 }
95
96 void iovec_array_free(struct iovec *iovec, size_t n_iovec) {
97 assert(iovec || n_iovec == 0);
98
99 FOREACH_ARRAY(i, iovec, n_iovec)
100 free(i->iov_base);
101
102 free(iovec);
103 }
104
105 int iovec_memcmp(const struct iovec *a, const struct iovec *b) {
106
107 if (a == b)
108 return 0;
109
110 return memcmp_nn(a ? a->iov_base : NULL,
111 a ? a->iov_len : 0,
112 b ? b->iov_base : NULL,
113 b ? b->iov_len : 0);
114 }
115
116 struct iovec* iovec_memdup(const struct iovec *source, struct iovec *ret) {
117 assert(ret);
118
119 if (!iovec_is_set(source))
120 *ret = (struct iovec) {};
121 else {
122 void *p = memdup(source->iov_base, source->iov_len);
123 if (!p)
124 return NULL;
125
126 *ret = IOVEC_MAKE(p, source->iov_len);
127 }
128
129 return ret;
130 }
131
132 int iovec_done_and_memdup(struct iovec *iovec, const struct iovec *source) {
133 assert(iovec);
134
135 if (iovec_equal(iovec, source))
136 return 0;
137
138 struct iovec copy;
139 if (!iovec_memdup(source, &copy))
140 return -ENOMEM;
141
142 iovec_done(iovec);
143 *iovec = copy;
144 return 1;
145 }
146
147 struct iovec* iovec_append(struct iovec *iovec, const struct iovec *append) {
148 assert(iovec_is_valid(iovec));
149
150 if (!iovec_is_set(append))
151 return iovec;
152
153 if (!greedy_realloc_append(&iovec->iov_base, &iovec->iov_len, append->iov_base, append->iov_len, 1))
154 return NULL;
155
156 return iovec;
157 }