Debian Codegraph @

src/OVAL/probes/SEAP/generic/strbuf.c

raw symbols SCIP
1 /*
2 * Copyright 2009 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * "Daniel Kopecek" <[email protected]>
21 */
22
23 #ifdef HAVE_CONFIG_Hcxx . . $ `<built-in>:409:9`!
24 #include <config.h>cxx . . $ `<file>/build/config.h`/
25 #endif
26
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdint.h>
30 #include <errno.h>
31 #include <assert.h>
32 #include <sys/types.h>
33 #include <limits.h>
34 #ifdef HAVE_UIO_Hcxx . . $ `build/config.h:80:9`!
35 #include <sys/uio.h>
36 #endif
37 #ifdef OS_WINDOWS
38 #include <io.h>
39 #else
40 #include <unistd.h>
41 #endif
42
43 #include "common/debug_priv.h"cxx . . $ `<file>/src/common/debug_priv.h`/
44 #include "strbuf.h"cxx . . $ `<file>/src/OVAL/probes/SEAP/generic/strbuf.h`/
45
46 strbuf_tcxx . . $ strbuf_t# *strbuf_newcxx . . $ strbuf_new(1b232a40e90dbc0f). (size_t max)
47 {
48 strbuf_tcxx . . $ strbuf_t# *buf;
49
50 buf = malloccxx . . $ malloc(bfcfa4d6b7f7ef64). (sizeof (strbuf_tcxx . . $ strbuf_t#));
51
52 if (buf != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
53 buf->begcxx . . $ $anonymous_type_5d3f97eecdd1a313_0#beg. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
54 buf->lbocxx . . $ $anonymous_type_5d3f97eecdd1a313_0#lbo. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
55 buf->blkmaxcxx . . $ $anonymous_type_5d3f97eecdd1a313_0#blkmax. = max;
56 buf->blkoffcxx . . $ $anonymous_type_5d3f97eecdd1a313_0#blkoff. = 0;
57 buf->sizecxx . . $ $anonymous_type_5d3f97eecdd1a313_0#size. = 0;
58 }
59
60 return (buf);
61 }
62
63 void strbuf_freecxx . . $ strbuf_free(b8a819aa52f3caa). (strbuf_tcxx . . $ strbuf_t# *buf)
64 {
65 struct strblkcxx . . $ strblk# *blk, *next;
66
67 next = buf->begcxx . . $ $anonymous_type_5d3f97eecdd1a313_0#beg.;
68
69 while (next != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
70 blk = next;
71 next = next->nextcxx . . $ strblk#next.;
72 freecxx . . $ free(5d22bdacc48458e8). (blk);
73 }
74
75 memsetcxx . . $ memset(d1de1e4fb215f1c6). (buf, 0, sizeof (strbuf_tcxx . . $ strbuf_t#));
76 freecxx . . $ free(5d22bdacc48458e8). (buf);
77 return;
78 }
79
80 size_t strbuf_sizecxx . . $ strbuf_size(950dc711ba0a4ba2). (strbuf_tcxx . . $ strbuf_t# *buf)
81 {
82 return (buf->sizecxx . . $ $anonymous_type_5d3f97eecdd1a313_0#size.);
83 }
84
85 static struct strblkcxx . . $ strblk# *__strblk_newcxx . . $ __strblk_new(1b9d8aee03545bad). (size_t len)
86 {
87 struct strblkcxx . . $ strblk# *blk;
88
89 blk = malloccxx . . $ malloc(bfcfa4d6b7f7ef64). (sizeof (struct strblkcxx . . $ strblk# *) + sizeof (size_t) + (sizeof (char) * len));
90 blk->nextcxx . . $ strblk#next. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
91 blk->sizecxx . . $ strblk#size. = 0;
92
93 return (blk);
94 }
95
96 static int __strbuf_addcxx . . $ __strbuf_add(13069e52f7633c24). (strbuf_tcxx . . $ strbuf_t# *buf, char *str, size_t len)
97 {
98 struct strblkcxx . . $ strblk# *cur, *prev;
99 size_t cpylen;
100
101 if (buf->begcxx . . $ $anonymous_type_5d3f97eecdd1a313_0#beg. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
102 if (buf->lbocxx . . $ $anonymous_type_5d3f97eecdd1a313_0#lbo. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
103 cur = buf->lbocxx . . $ $anonymous_type_5d3f97eecdd1a313_0#lbo.->nextcxx . . $ strblk#next.;
104 else
105 cur = buf->begcxx . . $ $anonymous_type_5d3f97eecdd1a313_0#beg.;
106 else {
107 cur = buf->begcxx . . $ $anonymous_type_5d3f97eecdd1a313_0#beg. = __strblk_newcxx . . $ __strblk_new(1b9d8aee03545bad). (buf->blkmaxcxx . . $ $anonymous_type_5d3f97eecdd1a313_0#blkmax.);
108
109 if (cur == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
110 return (-1);
111 }
112
113 prev = buf->lbocxx . . $ $anonymous_type_5d3f97eecdd1a313_0#lbo.;
114
115 for (;;) {
116 cpylen = buf->blkmaxcxx . . $ $anonymous_type_5d3f97eecdd1a313_0#blkmax. - buf->blkoffcxx . . $ $anonymous_type_5d3f97eecdd1a313_0#blkoff.;
117
118 if (cpylen >= len)
119 cpylen = len;
120
121 memcpycxx . . $ memcpy(cc817cb175f282d). (cur->datacxx . . $ strblk#data. + buf->blkoffcxx . . $ $anonymous_type_5d3f97eecdd1a313_0#blkoff., str, sizeof (char) * cpylen);
122
123 buf->sizecxx . . $ $anonymous_type_5d3f97eecdd1a313_0#size. += cpylen;
124 cur->sizecxx . . $ strblk#size. += cpylen;
125
126 len -= cpylen;
127 str += cpylen;
128
129 buf->blkoffcxx . . $ $anonymous_type_5d3f97eecdd1a313_0#blkoff. += cpylen;
130
131 assertcxx . . $ `/usr/include/assert.h:50:10`! (buf->blkoff <= buf->blkmax);
132
133 if (buf->blkoffcxx . . $ $anonymous_type_5d3f97eecdd1a313_0#blkoff. == buf->blkmaxcxx . . $ $anonymous_type_5d3f97eecdd1a313_0#blkmax.) {
134 cur->nextcxx . . $ strblk#next. = __strblk_newcxx . . $ __strblk_new(1b9d8aee03545bad). (buf->blkmaxcxx . . $ $anonymous_type_5d3f97eecdd1a313_0#blkmax.);
135 prev = cur;
136 cur = cur->nextcxx . . $ strblk#next.;
137 buf->blkoffcxx . . $ $anonymous_type_5d3f97eecdd1a313_0#blkoff. = 0;
138 }
139
140 if (len == 0)
141 break;
142 }
143
144 buf->lbocxx . . $ $anonymous_type_5d3f97eecdd1a313_0#lbo. = prev;
145
146 return (0);
147 }
148
149 int strbuf_addcxx . . $ strbuf_add(ce72e1452ef7fae). (strbuf_tcxx . . $ strbuf_t# *buf, const char *str, size_t len)
150 {
151 return __strbuf_addcxx . . $ __strbuf_add(13069e52f7633c24). (buf, (char *)str, len);
152 }
153
154 int strbuf_addfcxx . . $ strbuf_addf(13069e52f7633c24). (strbuf_tcxx . . $ strbuf_t# *buf, char *str, size_t len)
155 {
156 int ret;
157
158 ret = __strbuf_addcxx . . $ __strbuf_add(13069e52f7633c24). (buf, str, len);
159
160 if (ret == 0) {
161 freecxx . . $ free(5d22bdacc48458e8). (str);
162 return (0);
163 } else
164 return (ret);
165 }
166
167 int strbuf_addccxx . . $ strbuf_addc(8b9209537472a2cf). (strbuf_tcxx . . $ strbuf_t# *buf, char ch)
168 {
169 /* XXX: direct? */
170 return __strbuf_addcxx . . $ __strbuf_add(13069e52f7633c24). (buf, &ch, 1);
171 }
172
173 int strbuf_trunccxx . . $ strbuf_trunc(80f54fdde92cae7a). (strbuf_tcxx . . $ strbuf_t# *buf, size_t len)
174 {
175 return (0);
176 }
177
178 size_t strbuf_lengthcxx . . $ strbuf_length(950dc711ba0a4ba2). (strbuf_tcxx . . $ strbuf_t# *buf)
179 {
180 return (buf->sizecxx . . $ $anonymous_type_5d3f97eecdd1a313_0#size.);
181 }
182
183 char *strbuf_cstrcxx . . $ strbuf_cstr(91286d43c69cc643). (strbuf_tcxx . . $ strbuf_t# *buf)
184 {
185 struct strblkcxx . . $ strblk# *cur;
186 char *stroff, *strbeg;
187
188 strbeg = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(buf->sizecxx . . $ $anonymous_type_5d3f97eecdd1a313_0#size. + 1); /* +1 for terminating '\0' */
189 stroff = strbeg;
190
191 if (strbeg == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
192 return (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
193
194 cur = buf->begcxx . . $ $anonymous_type_5d3f97eecdd1a313_0#beg.;
195
196 while (cur != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
197 memcpycxx . . $ memcpy(cc817cb175f282d). (stroff, cur->datacxx . . $ strblk#data., sizeof (char) * cur->sizecxx . . $ strblk#size.);
198 stroff += cur->sizecxx . . $ strblk#size.;
199 cur = cur->nextcxx . . $ strblk#next.;
200 }
201 strbeg[buf->sizecxx . . $ $anonymous_type_5d3f97eecdd1a313_0#size.] = '\0';
202
203 return (strbeg);
204 }
205
206 char *strbuf_cstr_rcxx . . $ strbuf_cstr_r(8668bcafc67224c9). (strbuf_tcxx . . $ strbuf_t# *buf, char *str, size_t len)
207 {
208 struct strblkcxx . . $ strblk# *cur;
209 char *stroff;
210
211 if ((len - 1) < buf->sizecxx . . $ $anonymous_type_5d3f97eecdd1a313_0#size.) {
212 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = ENOBUFScxx . . $ `<external>/17922322188510340118/errno.h:89:9`!;
213 return (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
214 }
215
216 stroff = str;
217 cur = buf->begcxx . . $ $anonymous_type_5d3f97eecdd1a313_0#beg.;
218
219 while (cur != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
220 memcpycxx . . $ memcpy(cc817cb175f282d). (stroff, cur->datacxx . . $ strblk#data., sizeof (char) * cur->sizecxx . . $ strblk#size.);
221 stroff += cur->sizecxx . . $ strblk#size.;
222 cur = cur->nextcxx . . $ strblk#next.;
223 }
224
225 return (str);
226 }
227
228 char *strbuf_copycxx . . $ strbuf_copy(ebd6e8c8a33143d6). (strbuf_tcxx . . $ strbuf_t# *buf, void *dst, size_t len)
229 {
230 register struct strblkcxx . . $ strblk# *cur;
231 register uint8_tcxx . . $ uint8_t# *mem;
232
233 if (len < buf->sizecxx . . $ $anonymous_type_5d3f97eecdd1a313_0#size.) {
234 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = ENOBUFScxx . . $ `<external>/17922322188510340118/errno.h:89:9`!;
235 return (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
236 }
237
238 mem = (uint8_tcxx . . $ uint8_t# *)dst;
239 cur = buf->begcxx . . $ $anonymous_type_5d3f97eecdd1a313_0#beg.;
240
241 while (cur != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
242 memcpycxx . . $ memcpy(cc817cb175f282d). (mem, cur->datacxx . . $ strblk#data., sizeof (char) * cur->sizecxx . . $ strblk#size.);
243 mem += cur->sizecxx . . $ strblk#size.;
244 cur = cur->nextcxx . . $ strblk#next.;
245 }
246
247 return (dst);
248 }
249
250 size_t strbuf_fwritecxx . . $ strbuf_fwrite(8591c3e7bbcbdcee). (FILEcxx . . $ FILE# *fp, strbuf_tcxx . . $ strbuf_t# *buf)
251 {
252 struct strblkcxx . . $ strblk# *cur;
253 size_t size;
254
255 cur = buf->begcxx . . $ $anonymous_type_5d3f97eecdd1a313_0#beg.;
256 size = 0;
257
258 while (cur != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
259 size += fwritecxx . . $ fwrite(ab0609fa07c47d6e). (cur->datacxx . . $ strblk#data., sizeof (char),
260 cur->nextcxx . . $ strblk#next. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! ? buf->blkoffcxx . . $ $anonymous_type_5d3f97eecdd1a313_0#blkoff. : cur->sizecxx . . $ strblk#size., fp);
261 cur = cur->nextcxx . . $ strblk#next.;
262 }
263
264 return (size);
265 }
266
267 #ifdef HAVE_UIO_Hcxx . . $ `build/config.h:80:9`!
268
269 ssize_tcxx . . $ ssize_t# strbuf_writecxx . . $ strbuf_write(12057389d972c9be). (strbuf_tcxx . . $ strbuf_t# *buf, int fd)
270 {
271 struct strblkcxx . . $ strblk# *cur;
272 ssize_tcxx . . $ ssize_t# rsize, wsize;
273
274 struct ioveccxx . . $ iovec# *iov;
275 int ioc; /* helper/index */
276 int iot; /* total number of buffers */
277 int iow; /* total number of buffer that will be passed to the next writev */
278
279 rsize = 0;
280 iov = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
281 cur = buf->begcxx . . $ $anonymous_type_5d3f97eecdd1a313_0#beg.;
282 iot = (buf->sizecxx . . $ $anonymous_type_5d3f97eecdd1a313_0#size. / buf->blkmaxcxx . . $ $anonymous_type_5d3f97eecdd1a313_0#blkmax.) + 1;
283
284 dDcxx . . $ `src/common/debug_priv.h:64:9`!("total I/O vectors = %d", iot);
285
286 while (iot > 0) {
287 /*
288 * Prepare I/O vector
289 */
290 if (iot > IOV_MAXcxx . . $ `<external>/4858019643897469625/xopen_lim.h:66:10`!) {
291 dDcxx . . $ `src/common/debug_priv.h:64:9`!("iot (%d) > IOV_MAX (%d)", iot, IOV_MAXcxx . . $ `<external>/4858019643897469625/xopen_lim.h:66:10`!);
292 iow = IOV_MAXcxx . . $ `<external>/4858019643897469625/xopen_lim.h:66:10`!;
293 iot -= IOV_MAXcxx . . $ `<external>/4858019643897469625/xopen_lim.h:66:10`!;
294 } else {
295 dDcxx . . $ `src/common/debug_priv.h:64:9`!("iot (%d) < IOV_MAX (%d)", iot, IOV_MAXcxx . . $ `<external>/4858019643897469625/xopen_lim.h:66:10`!);
296 iow = iot;
297 iot = 0;
298 }
299
300 void *new_iov = realloccxx . . $ realloc(417ab160f6baadc6). (iov, sizeof (struct ioveccxx . . $ iovec#) * iow);
301 if (new_iov == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
302 int e = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
303 freecxx . . $ free(5d22bdacc48458e8).(iov);
304 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = e;
305 return -1;
306 }
307 iov = new_iov;
308 ioc = 0;
309
310 while (cur != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! && ioc < iow) {
311 iov[ioc].iov_basecxx . . $ iovec#iov_base. = cur->datacxx . . $ strblk#data.;
312 iov[ioc].iov_lencxx . . $ iovec#iov_len. = cur->sizecxx . . $ strblk#size.;
313
314 ++ioc;
315 cur = cur->nextcxx . . $ strblk#next.;
316 }
317
318 dDcxx . . $ `src/common/debug_priv.h:64:9`!("ioc = %d", ioc);
319
320 /*
321 * Write
322 */
323 wsize = writevcxx . . $ writev(a1ff61ef844db3de). (fd, iov, ioc);
324
325 if (wsize < 0) {
326 dEcxx . . $ `src/common/debug_priv.h:63:9`!("writev(%d, %p, %d) failed: %u, %s.", fd, iov, ioc, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!, strerror (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
327 freecxx . . $ free(5d22bdacc48458e8).(iov);
328 return (-1);
329 }
330
331 rsize += wsize;
332 }
333
334 freecxx . . $ free(5d22bdacc48458e8). (iov);
335 dDcxx . . $ `src/common/debug_priv.h:64:9`!("total bytes written: %zu", (size_t)rsize);
336
337 return (rsize);
338 }
339
340 #else
341
342 ssize_t strbuf_write (strbuf_t *buf, int fd)
343 {
344 struct strblk *cur;
345 size_t size;
346
347 cur = buf->beg;
348 size = 0;
349
350 while (cur != NULL) {
351 size += write(fd, cur->data, cur->next == NULL ? buf->blkoff : cur->size);
352 cur = cur->next;
353 }
354
355 return (size);
356 }
357
358 #endif