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_H
24 #include <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_H
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"
44 #include "strbuf.h"
45
46 strbuf_t *strbuf_new (size_t max)
47 {
48 strbuf_t *buf;
49
50 buf = malloc (sizeof (strbuf_t));
51
52 if (buf != NULL) {
53 buf->beg = NULL;
54 buf->lbo = NULL;
55 buf->blkmax = max;
56 buf->blkoff = 0;
57 buf->size = 0;
58 }
59
60 return (buf);
61 }
62
63 void strbuf_free (strbuf_t *buf)
64 {
65 struct strblk *blk, *next;
66
67 next = buf->beg;
68
69 while (next != NULL) {
70 blk = next;
71 next = next->next;
72 free (blk);
73 }
74
75 memset (buf, 0, sizeof (strbuf_t));
76 free (buf);
77 return;
78 }
79
80 size_t strbuf_size (strbuf_t *buf)
81 {
82 return (buf->size);
83 }
84
85 static struct strblk *__strblk_new (size_t len)
86 {
87 struct strblk *blk;
88
89 blk = malloc (sizeof (struct strblk *) + sizeof (size_t) + (sizeof (char) * len));
90 blk->next = NULL;
91 blk->size = 0;
92
93 return (blk);
94 }
95
96 static int __strbuf_add (strbuf_t *buf, char *str, size_t len)
97 {
98 struct strblk *cur, *prev;
99 size_t cpylen;
100
101 if (buf->beg != NULL)
102 if (buf->lbo != NULL)
103 cur = buf->lbo->next;
104 else
105 cur = buf->beg;
106 else {
107 cur = buf->beg = __strblk_new (buf->blkmax);
108
109 if (cur == NULL)
110 return (-1);
111 }
112
113 prev = buf->lbo;
114
115 for (;;) {
116 cpylen = buf->blkmax - buf->blkoff;
117
118 if (cpylen >= len)
119 cpylen = len;
120
121 memcpy (cur->data + buf->blkoff, str, sizeof (char) * cpylen);
122
123 buf->size += cpylen;
124 cur->size += cpylen;
125
126 len -= cpylen;
127 str += cpylen;
128
129 buf->blkoff += cpylen;
130
131 assert (buf->blkoff <= buf->blkmax);
132
133 if (buf->blkoff == buf->blkmax) {
134 cur->next = __strblk_new (buf->blkmax);
135 prev = cur;
136 cur = cur->next;
137 buf->blkoff = 0;
138 }
139
140 if (len == 0)
141 break;
142 }
143
144 buf->lbo = prev;
145
146 return (0);
147 }
148
149 int strbuf_add (strbuf_t *buf, const char *str, size_t len)
150 {
151 return __strbuf_add (buf, (char *)str, len);
152 }
153
154 int strbuf_addf (strbuf_t *buf, char *str, size_t len)
155 {
156 int ret;
157
158 ret = __strbuf_add (buf, str, len);
159
160 if (ret == 0) {
161 free (str);
162 return (0);
163 } else
164 return (ret);
165 }
166
167 int strbuf_addc (strbuf_t *buf, char ch)
168 {
169 /* XXX: direct? */
170 return __strbuf_add (buf, &ch, 1);
171 }
172
173 int strbuf_trunc (strbuf_t *buf, size_t len)
174 {
175 return (0);
176 }
177
178 size_t strbuf_length (strbuf_t *buf)
179 {
180 return (buf->size);
181 }
182
183 char *strbuf_cstr (strbuf_t *buf)
184 {
185 struct strblk *cur;
186 char *stroff, *strbeg;
187
188 strbeg = malloc(buf->size + 1); /* +1 for terminating '\0' */
189 stroff = strbeg;
190
191 if (strbeg == NULL)
192 return (NULL);
193
194 cur = buf->beg;
195
196 while (cur != NULL) {
197 memcpy (stroff, cur->data, sizeof (char) * cur->size);
198 stroff += cur->size;
199 cur = cur->next;
200 }
201 strbeg[buf->size] = '\0';
202
203 return (strbeg);
204 }
205
206 char *strbuf_cstr_r (strbuf_t *buf, char *str, size_t len)
207 {
208 struct strblk *cur;
209 char *stroff;
210
211 if ((len - 1) < buf->size) {
212 errno = ENOBUFS;
213 return (NULL);
214 }
215
216 stroff = str;
217 cur = buf->beg;
218
219 while (cur != NULL) {
220 memcpy (stroff, cur->data, sizeof (char) * cur->size);
221 stroff += cur->size;
222 cur = cur->next;
223 }
224
225 return (str);
226 }
227
228 char *strbuf_copy (strbuf_t *buf, void *dst, size_t len)
229 {
230 register struct strblk *cur;
231 register uint8_t *mem;
232
233 if (len < buf->size) {
234 errno = ENOBUFS;
235 return (NULL);
236 }
237
238 mem = (uint8_t *)dst;
239 cur = buf->beg;
240
241 while (cur != NULL) {
242 memcpy (mem, cur->data, sizeof (char) * cur->size);
243 mem += cur->size;
244 cur = cur->next;
245 }
246
247 return (dst);
248 }
249
250 size_t strbuf_fwrite (FILE *fp, strbuf_t *buf)
251 {
252 struct strblk *cur;
253 size_t size;
254
255 cur = buf->beg;
256 size = 0;
257
258 while (cur != NULL) {
259 size += fwrite (cur->data, sizeof (char),
260 cur->next == NULL ? buf->blkoff : cur->size, fp);
261 cur = cur->next;
262 }
263
264 return (size);
265 }
266
267 #ifdef HAVE_UIO_H
268
269 ssize_t strbuf_write (strbuf_t *buf, int fd)
270 {
271 struct strblk *cur;
272 ssize_t rsize, wsize;
273
274 struct 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 = NULL;
281 cur = buf->beg;
282 iot = (buf->size / buf->blkmax) + 1;
283
284 dD("total I/O vectors = %d", iot);
285
286 while (iot > 0) {
287 /*
288 * Prepare I/O vector
289 */
290 if (iot > IOV_MAX) {
291 dD("iot (%d) > IOV_MAX (%d)", iot, IOV_MAX);
292 iow = IOV_MAX;
293 iot -= IOV_MAX;
294 } else {
295 dD("iot (%d) < IOV_MAX (%d)", iot, IOV_MAX);
296 iow = iot;
297 iot = 0;
298 }
299
300 void *new_iov = realloc (iov, sizeof (struct iovec) * iow);
301 if (new_iov == NULL) {
302 int e = errno;
303 free(iov);
304 errno = e;
305 return -1;
306 }
307 iov = new_iov;
308 ioc = 0;
309
310 while (cur != NULL && ioc < iow) {
311 iov[ioc].iov_base = cur->data;
312 iov[ioc].iov_len = cur->size;
313
314 ++ioc;
315 cur = cur->next;
316 }
317
318 dD("ioc = %d", ioc);
319
320 /*
321 * Write
322 */
323 wsize = writev (fd, iov, ioc);
324
325 if (wsize < 0) {
326 dE("writev(%d, %p, %d) failed: %u, %s.", fd, iov, ioc, errno, strerror (errno));
327 free(iov);
328 return (-1);
329 }
330
331 rsize += wsize;
332 }
333
334 free (iov);
335 dD("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