Debian Codegraph @

src/OVAL/probes/SEAP/sexp-manip_r.c

raw symbols SCIP
1 /*
2 * Copyright 2011 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 #include <config.h>
24 #include <string.h>
25 #include <errno.h>
26
27 #include "_sexp-types.h"
28 #include "_sexp-value.h"
29 #include "_sexp-rawptr.h"
30 #include "public/sexp-manip_r.h"
31 #include "debug_priv.h"
32
33 SEXP_t *SEXP_init(SEXP_t *sexp_mem)
34 {
35 if (sexp_mem == NULL) {
36 errno = EFAULT;
37 return (NULL);
38 }
39
40 sexp_mem->s_type = NULL;
41 sexp_mem->s_valp = 0;
42
43 #if !defined(NDEBUG) || defined(VALIDATE_SEXP)
44 sexp_mem->__magic0 = SEXP_MAGIC0;
45 sexp_mem->__magic1 = SEXP_MAGIC1;
46 #endif
47 return (sexp_mem);
48 }
49
50 SEXP_t *SEXP_number_newi_32_r(SEXP_t *sexp_mem, int32_t n)
51 {
52 SEXP_val_t v_dsc;
53
54 if (sexp_mem == NULL) {
55 errno = EFAULT;
56 return (NULL);
57 }
58
59 if (SEXP_val_new (&v_dsc, sizeof (SEXP_numtype_t) + sizeof (int32_t),
60 SEXP_VALTYPE_NUMBER) != 0)
61 {
62 /* TODO: handle this */
63 return (NULL);
64 }
65
66 SEXP_NCASTP(i32,v_dsc.mem)->t = SEXP_NUM_INT32;
67 SEXP_NCASTP(i32,v_dsc.mem)->n = n;
68
69 SEXP_init(sexp_mem);
70 sexp_mem->s_type = NULL;
71 sexp_mem->s_valp = v_dsc.ptr;
72
73 return (sexp_mem);
74 }
75
76 SEXP_t *SEXP_number_newu_32_r(SEXP_t *sexp_mem, uint32_t n)
77 {
78 SEXP_val_t v_dsc;
79
80 if (sexp_mem == NULL) {
81 errno = EFAULT;
82 return (NULL);
83 }
84
85 if (SEXP_val_new (&v_dsc, sizeof (SEXP_numtype_t) + sizeof (uint32_t),
86 SEXP_VALTYPE_NUMBER) != 0)
87 {
88 /* TODO: handle this */
89 return (NULL);
90 }
91
92 SEXP_NCASTP(u32,v_dsc.mem)->t = SEXP_NUM_UINT32;
93 SEXP_NCASTP(u32,v_dsc.mem)->n = n;
94
95 SEXP_init(sexp_mem);
96 sexp_mem->s_type = NULL;
97 sexp_mem->s_valp = v_dsc.ptr;
98
99 return (sexp_mem);
100 }
101
102 SEXP_t *SEXP_number_newu_64_r(SEXP_t *sexp_mem, uint64_t n)
103 {
104 SEXP_val_t v_dsc;
105
106 if (sexp_mem == NULL) {
107 errno = EFAULT;
108 return (NULL);
109 }
110
111 if (SEXP_val_new (&v_dsc, sizeof (SEXP_numtype_t) + sizeof (uint64_t),
112 SEXP_VALTYPE_NUMBER) != 0)
113 {
114 /* TODO: handle this */
115 return (NULL);
116 }
117
118 SEXP_NCASTP(u64,v_dsc.mem)->t = SEXP_NUM_UINT64;
119 SEXP_NCASTP(u64,v_dsc.mem)->n = n;
120
121 SEXP_init(sexp_mem);
122 sexp_mem->s_type = NULL;
123 sexp_mem->s_valp = v_dsc.ptr;
124
125 return (sexp_mem);
126 }
127
128 SEXP_t *SEXP_number_newi_64_r(SEXP_t *sexp_mem, int64_t n)
129 {
130 SEXP_val_t v_dsc;
131
132 if (sexp_mem == NULL) {
133 errno = EFAULT;
134 return (NULL);
135 }
136
137 if (SEXP_val_new (&v_dsc, sizeof (SEXP_numtype_t) + sizeof (int64_t),
138 SEXP_VALTYPE_NUMBER) != 0)
139 {
140 /* TODO: handle this */
141 return (NULL);
142 }
143
144 SEXP_NCASTP(i64,v_dsc.mem)->t = SEXP_NUM_INT64;
145 SEXP_NCASTP(i64,v_dsc.mem)->n = n;
146
147 SEXP_init(sexp_mem);
148 sexp_mem->s_type = NULL;
149 sexp_mem->s_valp = v_dsc.ptr;
150
151 return (sexp_mem);
152 }
153
154 SEXP_t *SEXP_number_newf_r(SEXP_t *sexp_mem, double n)
155 {
156 SEXP_val_t v_dsc;
157
158 if (sexp_mem == NULL) {
159 errno = EFAULT;
160 return (NULL);
161 }
162
163 if (SEXP_val_new (&v_dsc, sizeof (SEXP_numtype_t) + sizeof (double),
164 SEXP_VALTYPE_NUMBER) != 0)
165 {
166 /* TODO: handle this */
167 return (NULL);
168 }
169
170 SEXP_NCASTP(f,v_dsc.mem)->t = SEXP_NUM_DOUBLE;
171 SEXP_NCASTP(f,v_dsc.mem)->n = n;
172
173 SEXP_init(sexp_mem);
174 sexp_mem->s_type = NULL;
175 sexp_mem->s_valp = v_dsc.ptr;
176
177 return (sexp_mem);
178 }
179
180 SEXP_t *SEXP_number_newb_r(SEXP_t *sexp_mem, bool n)
181 {
182 SEXP_val_t v_dsc;
183
184 if (sexp_mem == NULL) {
185 errno = EFAULT;
186 return (NULL);
187 }
188
189 if (SEXP_val_new (&v_dsc, sizeof (SEXP_numtype_t) + sizeof (bool),
190 SEXP_VALTYPE_NUMBER) != 0)
191 {
192 /* TODO: handle this */
193 return (NULL);
194 }
195
196 SEXP_NCASTP(b,v_dsc.mem)->t = SEXP_NUM_BOOL;
197 SEXP_NCASTP(b,v_dsc.mem)->n = n;
198
199 SEXP_init(sexp_mem);
200 sexp_mem->s_type = NULL;
201 sexp_mem->s_valp = v_dsc.ptr;
202
203 return (sexp_mem);
204 }
205
206 SEXP_t *SEXP_string_new_r (SEXP_t *sexp_mem, const void *string, size_t length)
207 {
208 SEXP_val_t v_dsc;
209
210 if (sexp_mem == NULL) {
211 errno = EFAULT;
212 return (NULL);
213 }
214
215 if (SEXP_val_new (&v_dsc, sizeof (char) * length,
216 SEXP_VALTYPE_STRING) != 0)
217 {
218 /* TODO: handle this */
219 return (NULL);
220 }
221
222 memcpy (v_dsc.mem, string, sizeof (char) * length);
223
224 SEXP_init(sexp_mem);
225 sexp_mem->s_type = NULL;
226 sexp_mem->s_valp = v_dsc.ptr;
227
228 return (sexp_mem);
229 }
230
231 SEXP_t *SEXP_string_newf_r(SEXP_t *sexp_mem, const char *format, ...)
232 {
233 va_list ap;
234 SEXP_t *se;
235
236 va_start(ap, format);
237 se = SEXP_string_newf_rv(sexp_mem, format, ap);
238 va_end(ap);
239
240 return (se);
241 }
242
243 SEXP_t *SEXP_string_newf_rv(SEXP_t *sexp_mem, const char *format, va_list ap)
244 {
245 SEXP_val_t v_dsc;
246 char *v_string = NULL;
247 int v_strlen = 0;
248 va_list copy;
249
250 if (sexp_mem == NULL) {
251 errno = EFAULT;
252 return (NULL);
253 }
254
255 va_copy(copy, ap);
256 v_strlen = vsnprintf(v_string, v_strlen, format, copy);
257 va_end(copy);
258 if (v_strlen < 0) {
259 return NULL;
260 }
261 v_strlen++; /* For '\0' */
262 v_string = malloc(v_strlen);
263 v_strlen = vsnprintf(v_string, v_strlen, format, ap);
264 if (v_strlen < 0) {
265 free(v_string);
266 return NULL;
267 }
268
269 if (SEXP_val_new (&v_dsc, sizeof (char) * v_strlen,
270 SEXP_VALTYPE_STRING) != 0)
271 {
272 /* TODO: handle this */
273 return (NULL);
274 }
275
276 memcpy (v_dsc.mem, v_string, sizeof (char) * v_strlen);
277 free(v_string);
278
279 SEXP_init(sexp_mem);
280 sexp_mem->s_type = NULL;
281 sexp_mem->s_valp = v_dsc.ptr;
282
283 return (sexp_mem);
284 }
285
286 SEXP_t *SEXP_list_new_rv (SEXP_t *sexp_mem, SEXP_t *memb, va_list alist)
287 {
288 SEXP_val_t v_dsc;
289 SEXP_t *s_ptr[32];
290 size_t s_cur;
291 uint8_t b_exp;
292
293 s_cur = 0;
294 s_ptr[s_cur] = memb;
295
296 while (s_ptr[s_cur] != NULL) {
297 _A(s_cur < (sizeof s_ptr / sizeof (SEXP_t *)));
298 SEXP_VALIDATE(s_ptr[s_cur]);
299
300 s_ptr[++s_cur] = va_arg (alist, SEXP_t *);
301 }
302
303 if (SEXP_val_new (&v_dsc, sizeof (void *) + sizeof (uint16_t),
304 SEXP_VALTYPE_LIST) != 0)
305 {
306 /* TODO: handle this */
307 return (NULL);
308 }
309
310 if (s_cur > 0) {
311 for (b_exp = 0; (size_t)(1 << b_exp) < s_cur; ++b_exp);
312
313 SEXP_LCASTP(v_dsc.mem)->offset = 0;
314 SEXP_LCASTP(v_dsc.mem)->b_addr = (void *)SEXP_rawval_lblk_new (b_exp);
315
316 if (SEXP_rawval_lblk_fill ((uintptr_t)SEXP_LCASTP(v_dsc.mem)->b_addr,
317 s_ptr, s_cur) != ((uintptr_t)SEXP_LCASTP(v_dsc.mem)->b_addr))
318 {
319 /* TODO: handle this */
320 return (NULL);
321 }
322 } else {
323 SEXP_LCASTP(v_dsc.mem)->offset = 0;
324 SEXP_LCASTP(v_dsc.mem)->b_addr = NULL;
325 }
326
327 SEXP_init(sexp_mem);
328 sexp_mem->s_type = NULL;
329 sexp_mem->s_valp = v_dsc.ptr;
330
331 SEXP_VALIDATE(sexp_mem);
332
333 return (sexp_mem);
334 }
335
336 SEXP_t *SEXP_list_new_r (SEXP_t *sexp_mem, SEXP_t *memb, ...)
337 {
338 va_list ap;
339 SEXP_t *ret;
340
341 va_start(ap, memb);
342 ret = SEXP_list_new_rv(sexp_mem, memb, ap);
343 va_end(ap);
344
345 return (ret);
346 }
347
348 SEXP_t *SEXP_list_rest_r (SEXP_t *rest, const SEXP_t *list)
349 {
350 SEXP_val_t v_dsc_o, v_dsc_r;
351 struct SEXP_val_lblk *lblk;
352
353 if (rest == NULL) {
354 errno = EINVAL;
355 return (NULL);
356 }
357
358 if (list == NULL) {
359 errno = EINVAL;
360 return (NULL);
361 }
362
363 SEXP_VALIDATE(list);
364
365 SEXP_val_dsc (&v_dsc_o, list->s_valp);
366
367 if (v_dsc_o.type != SEXP_VALTYPE_LIST) {
368 errno = EINVAL;
369 return (NULL);
370 }
371
372 if (SEXP_val_new (&v_dsc_r, sizeof (void *) + sizeof (uint16_t),
373 SEXP_VALTYPE_LIST) != 0)
374 {
375 /* TODO: handle this */
376 return (NULL);
377 }
378
379 SEXP_LCASTP(v_dsc_r.mem)->offset = SEXP_LCASTP(v_dsc_o.mem)->offset + 1;
380 SEXP_LCASTP(v_dsc_r.mem)->b_addr = SEXP_LCASTP(v_dsc_o.mem)->b_addr;
381
382 lblk = SEXP_VALP_LBLK(SEXP_LCASTP(v_dsc_r.mem)->b_addr);
383
384 if (lblk != NULL) {
385 if (SEXP_LCASTP(v_dsc_r.mem)->offset == lblk->real) {
386 SEXP_LCASTP(v_dsc_r.mem)->offset = 0;
387 SEXP_LCASTP(v_dsc_r.mem)->b_addr = SEXP_VALP_LBLK(lblk->nxsz);
388 }
389
390 if (SEXP_VALP_LBLK(SEXP_LCASTP(v_dsc_r.mem)->b_addr) != NULL)
391 SEXP_LCASTP(v_dsc_r.mem)->b_addr = (void *)SEXP_rawval_lblk_incref ((uintptr_t) SEXP_LCASTP(v_dsc_r.mem)->b_addr);
392 }
393
394 SEXP_init(rest);
395 rest->s_type = NULL;
396 rest->s_valp = SEXP_val_ptr (&v_dsc_r);
397
398 SEXP_VALIDATE(rest);
399
400 return (rest);
401 }
402
403 int SEXP_unref_r(SEXP_t *s_exp)
404 {
405 if (SEXP_refs(s_exp) != 1)
406 return (-1);
407 else
408 SEXP_rawval_decref(s_exp->s_valp);
409 return (0);
410 }
411
412 #if defined(NDEBUG)
413 void SEXP_free_r (SEXP_t *s_exp)
414 #else
415 void __SEXP_free_r (SEXP_t *s_exp, const char *file, uint32_t line, const char *func)
416 #endif
417 {
418 #if !defined(NDEBUG) && defined(SEAP_VERBOSE_DEBUG)
419 dD("s_exp=%p (%s:%u:%s)", s_exp, file, line, func);
420 #endif
421 if (s_exp == NULL)
422 return;
423
424 if (!SEXP_softrefp(s_exp) && SEXP_typeof(s_exp) != SEXP_TYPE_EMPTY) {
425 SEXP_val_t v_dsc;
426
427 SEXP_VALIDATE(s_exp);
428 SEXP_val_dsc (&v_dsc, s_exp->s_valp);
429
430 if (SEXP_rawval_decref (s_exp->s_valp)) {
431 switch (v_dsc.type) {
432 case SEXP_VALTYPE_STRING:
433 oscap_aligned_free(v_dsc.hdr);
434 break;
435 case SEXP_VALTYPE_NUMBER:
436 oscap_aligned_free(v_dsc.hdr);
437 break;
438 case SEXP_VALTYPE_LIST:
439 if (SEXP_LCASTP(v_dsc.mem)->b_addr != NULL)
440 SEXP_rawval_lblk_free ((uintptr_t)SEXP_LCASTP(v_dsc.mem)->b_addr, SEXP_free_r);
441
442 oscap_aligned_free(v_dsc.hdr);
443 break;
444 default:
445 abort ();
446 }
447 }
448 }
449
450 #if !defined(NDEBUG) || defined(VALIDATE_SEXP)
451 s_exp->s_valp = 0;
452 s_exp->s_type = NULL;
453 s_exp->__magic0 = SEXP_MAGIC0_INV;
454 s_exp->__magic1 = SEXP_MAGIC1_INV;
455 #endif
456 return;
457 }