Debian Codegraph @

src/OVAL/probes/SEAP/public/sexp-manip.h

raw symbols SCIP
1 /**
2 * @file sexp-manip.h
3 * @brief S-expression object manipulation functions - public header
4 * @author "Daniel Kopecek" <[email protected]>
5 *
6 * @addtogroup OVAL
7 * @{
8 * @addtogroup PROBES
9 * @{
10 * @addtogroup SEXPRESSIONS
11 * @{
12 */
13 /*
14 * Copyright 2009 Red Hat Inc., Durham, North Carolina.
15 * All Rights Reserved.
16 *
17 * This library is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Lesser General Public
19 * License as published by the Free Software Foundation; either
20 * version 2.1 of the License, or (at your option) any later version.
21 *
22 * This library is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * Lesser General Public License for more details.
26 *
27 * You should have received a copy of the GNU Lesser General Public
28 * License along with this library; if not, write to the Free Software
29 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 *
31 * Authors:
32 * Daniel Kopecek <[email protected]>
33 */
34
35 #pragma once
36 #ifndef SEXP_MANIP_H
37 #define SEXP_MANIP_H
38
39 #include <stdarg.h>
40 #include <stddef.h>
41 #include <stdint.h>
42 #include <stdbool.h>
43 #include <sexp-types.h>
44 #include <helpers.h>
45 #include "oscap_export.h"
46 #include <common/util.h>
47
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51
52 /*
53 * number
54 */
55
56 /**
57 * Create a new sexp object from a value and a number type
58 * @param t the desired number type
59 * @param n pointer to the number value
60 */
61 OSCAP_API SEXP_t *SEXP_number_new (SEXP_numtype_t t, const void *n) __attribute__ ((nonnull (2)));
62
63 /**
64 * Create a new sexp object from a boolean value.
65 * @param n the boolean value to store
66 */
67 OSCAP_API SEXP_t *SEXP_number_newb (bool n);
68
69 /**
70 * Create a new sexp object from an integer.
71 * @param n the integer value to store
72 */
73 OSCAP_API SEXP_t *SEXP_number_newi_8 (int8_t n);
74
75 /**
76 * Create a new sexp object from an unsigned integer.
77 * @param n the unsigned integer value to store
78 */
79 OSCAP_API SEXP_t *SEXP_number_newu_8 (uint8_t n);
80
81 /**
82 * Get unsigned integer value from a sexp object.
83 * @param s_exp the queried sexp object
84 */
85 OSCAP_API uint8_t SEXP_number_getu_8 (const SEXP_t *s_exp);
86
87 /**
88 * Create a new sexp object from an integer.
89 * @param n the integer value to store
90 */
91 OSCAP_API SEXP_t *SEXP_number_newi_16 (int16_t n);
92
93 /**
94 * Create a new sexp object from an unsigned integer.
95 * @param n the unsigned integer value to store
96 */
97 OSCAP_API SEXP_t *SEXP_number_newu_16 (uint16_t n);
98
99 /**
100 * Create a new sexp object from an integer.
101 * The size of the integer may be architecture dependent.
102 */
103 #define SEXP_number_newi SEXP_number_newi_32
104
105 /**
106 * Create a new sexp object from an integer.
107 * @param n the integer value to store
108 */
109 OSCAP_API SEXP_t *SEXP_number_newi_32 (int32_t n);
110
111 /**
112 * Get integer value from a sexp object.
113 * The size of the integer may be architecture dependent.
114 */
115 #define SEXP_number_geti SEXP_number_geti_32
116
117 /**
118 * Get integer value from a sexp object.
119 * @param s_exp the queried sexp object
120 */
121 OSCAP_API int32_t SEXP_number_geti_32 (const SEXP_t *s_exp);
122
123 /**
124 * Get boolean value from a sexp object.
125 * @param s_exp the queried sexp object
126 */
127 OSCAP_API bool SEXP_number_getb (const SEXP_t *s_exp);
128
129 /**
130 * Create a new sexp object from an unsigned integer.
131 * The size of the integer may be architecture dependent.
132 */
133 #define SEXP_number_newu SEXP_number_newu_32
134
135 /**
136 * Create a new sexp object from an unsigned integer.
137 * @param n the integer value to store
138 */
139 OSCAP_API SEXP_t *SEXP_number_newu_32 (uint32_t n);
140
141 /**
142 * Get unsigned integer value from a sexp object.
143 * The size of the integer may be architecture dependent.
144 */
145 #define SEXP_number_getu SEXP_number_getu_32
146
147 /**
148 * Get unsigned integer value from a sexp object.
149 * @param s_exp the queried sexp object
150 */
151 OSCAP_API uint32_t SEXP_number_getu_32 (const SEXP_t *s_exp);
152
153 /**
154 * Create a new sexp object from an integer.
155 * @param n the integer value to store
156 */
157 OSCAP_API SEXP_t *SEXP_number_newi_64 (int64_t n);
158
159 /**
160 * Get integer value from a sexp object.
161 * @param s_exp the queried sexp object
162 */
163 OSCAP_API int64_t SEXP_number_geti_64 (const SEXP_t *s_exp);
164
165 /**
166 * Create a new sexp object from an unsigned integer.
167 * @param n the integer value to store
168 */
169 OSCAP_API SEXP_t *SEXP_number_newu_64 (uint64_t n);
170
171 /**
172 * Get unsigned integer value from a sexp object.
173 * @param s_exp the queried sexp object
174 */
175 OSCAP_API uint64_t SEXP_number_getu_64 (const SEXP_t *s_exp);
176
177 /**
178 * Create a new sexp object from an floating point value.
179 * @param n the floating point value to store
180 */
181 OSCAP_API SEXP_t *SEXP_number_newf (double n);
182
183 /**
184 * Get floating point value from a sexp object.
185 * @param s_exp the queried sexp object
186 */
187 OSCAP_API double SEXP_number_getf (const SEXP_t *s_exp);
188
189 /**
190 * Get a value from a sexp object according to a specified type
191 * @param s_exp the queried sexp object
192 * @param dst buffer for the value
193 * @param type the desired number type
194 */
195 OSCAP_API int SEXP_number_get (const SEXP_t *s_exp, void *dst, SEXP_numtype_t type);
196
197 /**
198 * Get unsigned integer value from a sexp object.
199 * @param s_exp the queried sexp object
200 */
201 OSCAP_API uint16_t SEXP_number_getu_16 (const SEXP_t *s_exp);
202
203 /**
204 * Free the specified sexp object.
205 * @param s_exp the object to be freed
206 */
207 OSCAP_API void SEXP_number_free (SEXP_t *s_exp);
208
209 /**
210 * Check whether the provided sexp object is a number.
211 * @param s_exp the sexp object to be tested
212 */
213 OSCAP_API bool SEXP_numberp (const SEXP_t *s_exp);
214
215 /**
216 * Get the number type of an object.
217 * @param sexp the queried sexp object
218 */
219 OSCAP_API SEXP_numtype_t SEXP_number_type (const SEXP_t *sexp);
220
221 /*
222 * string
223 */
224
225 /**
226 * Create a new sexp object from a string.
227 * @param string the string to be stored
228 * @param strlen the length of the string in bytes
229 */
230 OSCAP_API SEXP_t *SEXP_string_new (const void *string, size_t strlen) __attribute__ ((nonnull (1)));
231
232 /**
233 * Create a new sexp object from a format string.
234 * @param format the format of the new string
235 * @param ... arguments for the format
236 */
237 OSCAP_API SEXP_t *SEXP_string_newf (const char *format, ...) __attribute__ ((format (printf, 1, 2), nonnull (1)));
238
239 /**
240 * Free the specified sexp object.
241 * @param s_exp the object to be freed
242 */
243 OSCAP_API void SEXP_string_free (SEXP_t *s_exp);
244
245 /**
246 * Check whether the provided sexp object is a string.
247 * @param s_exp the sexp object to be tested
248 */
249 OSCAP_API bool SEXP_stringp (const SEXP_t *s_exp);
250
251 /**
252 * Get the length of a string in a sexp object.
253 * @param s_exp the queried sexp object
254 */
255 OSCAP_API size_t SEXP_string_length (const SEXP_t *s_exp);
256
257 /**
258 * Compare a string in a sexp object with a C string.
259 * @param s_exp the sexp object to be compared
260 * @param str the C string to be compared
261 */
262 OSCAP_API int SEXP_strcmp (const SEXP_t *s_exp, const char *str) __attribute__ ((nonnull (2)));
263
264 /**
265 * Compare a string in a sexp object with a C string.
266 * @param s_exp the sexp object to be compared
267 * @param str the C string to be compared
268 * @param n compare at most n bytes
269 */
270 OSCAP_API int SEXP_strncmp (const SEXP_t *s_exp, const char *str, size_t n) __attribute__ ((nonnull (2)));
271
272 /**
273 * Get the n-th byte of a string.
274 * @param s_exp the sexp object holding the string
275 * @param n the index of the desired character
276 */
277 OSCAP_API int SEXP_string_nth (const SEXP_t *s_exp, size_t n);
278
279 /**
280 * Get a C string from a sexp object.
281 */
282 OSCAP_API char *SEXP_string_cstr (const SEXP_t *s_exp);
283
284 /**
285 * Get a C string from a sexp object.
286 * The name is stored in the provided buffer.
287 * @param s_exp the queried object
288 * @param buf the buffer to store the name in
289 * @param len the length of the buffer
290 */
291 OSCAP_API size_t SEXP_string_cstr_r (const SEXP_t *s_exp, char *buf, size_t len) __attribute__ ((nonnull (2)));
292
293 /**
294 * Obsolete function.
295 */
296 OSCAP_API char *SEXP_string_cstrp (const SEXP_t *s_exp);
297
298 /**
299 * Get a C substring from a sexp object.
300 * @param s_sexp the queried sexp object
301 * @param beg the position of the fisrt character of the substring
302 * @param len the length of the substring
303 */
304 OSCAP_API char *SEXP_string_subcstr (const SEXP_t *s_exp, size_t beg, size_t len);
305
306 /**
307 * Compare two sexp strings.
308 * @param str_a the first string to compare
309 * @param str_b the second string to compare
310 */
311 OSCAP_API int SEXP_string_cmp (const SEXP_t *str_a, const SEXP_t *str_b);
312
313 /**
314 * Try to cast the supplied sexp string to a boolean.
315 * @param s_ext the string to be cast
316 */
317 OSCAP_API bool SEXP_string_getb (const SEXP_t *s_exp);
318
319 /*
320 * list
321 */
322
323 /**
324 * Create a new sexp list, optionally initialized with the provided sexp arguments.
325 * The argument list needs to be terminated with NULL.
326 * @param memb the first sexp object to be inserted into the new list. can be NULL.
327 * @param ... arbitrary number of elements to be inserted
328 */
329 OSCAP_API SEXP_t *SEXP_list_new (SEXP_t *memb, ...);
330
331 /**
332 * Free the specified sexp object.
333 * @param s_exp the object to be freed
334 */
335 OSCAP_API void SEXP_list_free (SEXP_t *s_exp);
336
337 /**
338 * Check whether the provided sexp object is a list.
339 * @param s_exp the sexp object to be tested
340 */
341 OSCAP_API bool SEXP_listp (const SEXP_t *s_exp);
342
343 /**
344 * Get the length of the sexp list.
345 * @param s_sexp the queried sexp object
346 */
347 OSCAP_API size_t SEXP_list_length (const SEXP_t *s_exp);
348
349 /**
350 * Get the first element of a list.
351 * This function increments element's reference count
352 * @param list the queried sexp object
353 */
354 OSCAP_API SEXP_t *SEXP_list_first (const SEXP_t *list);
355
356 /**
357 * Get the rest of a list.
358 * This function increments elements' reference count.
359 * @param list the queried sexp object
360 */
361 OSCAP_API SEXP_t *SEXP_list_rest (const SEXP_t *list);
362
363 /**
364 * Get the last element of a list.
365 * This function increments element's reference count.
366 * @param list the queried sexp object
367 */
368 OSCAP_API SEXP_t *SEXP_list_last (const SEXP_t *list);
369
370 /**
371 * Get the n-th element of a list.
372 * This function increments element's reference count.
373 * @param list the queried sexp object
374 * @param n the position of the element in the list
375 */
376 OSCAP_API SEXP_t *SEXP_list_nth (const SEXP_t *list, uint32_t n);
377
378 /**
379 * Add an element to a list.
380 * This function increments element's reference count.
381 * @param list the modified sexp object
382 * @param s_exp the element to be added
383 */
384 OSCAP_API SEXP_t *SEXP_list_add (SEXP_t *list, const SEXP_t *s_exp);
385
386 /**
387 * Create a new list containing the concatenated contents of two lists.
388 * This function increments element's reference count.
389 * @param list_a the first list to be contatenated
390 * @param list_b the list to be attached to the first one
391 */
392 OSCAP_API SEXP_t *SEXP_list_join (const SEXP_t *list_a, const SEXP_t *list_b);
393
394 /**
395 * Push an element to the head of a list.
396 * This function increments element's reference count.
397 * @param list the modified sexp object
398 * @param s_exp the element to be added
399 */
400 OSCAP_API SEXP_t *SEXP_list_push (SEXP_t *list, const SEXP_t *s_exp);
401
402 /**
403 * Extract the first element of a list.
404 * This function increments element's reference count.
405 * @param list the modified sexp object
406 */
407 OSCAP_API SEXP_t *SEXP_list_pop (SEXP_t *list);
408
409 /**
410 * Sort a list using `compare' as the comparison function.
411 */
412 OSCAP_API SEXP_t *SEXP_list_sort(SEXP_t *list, int(*compare)(const SEXP_t *, const SEXP_t *));
413
414 /**
415 * Replace the n-th element of a list.
416 * This function increments element's reference count.
417 * @param list the modified sexp object
418 * @param n the index of the element to be replaced
419 * @param s_exp the element to be added
420 * @return the replaced element
421 */
422 OSCAP_API SEXP_t *SEXP_list_replace (SEXP_t *list, uint32_t n, const SEXP_t *s_exp);
423
424 /**
425 * Get the first element of a list.
426 * This function creates a soft reference to the element.
427 * @param list the queried sexp object
428 */
429 OSCAP_API SEXP_t *SEXP_listref_first (SEXP_t *list);
430
431 /**
432 * Get the rest of a list.
433 * This function creates a soft reference to the list.
434 * @param list the queried sexp object
435 */
436 OSCAP_API SEXP_t *SEXP_listref_rest (SEXP_t *list);
437
438 /**
439 * Get the last element of a list.
440 * This function creates a soft reference to the element.
441 * @param list the queried sexp object
442 */
443 OSCAP_API SEXP_t *SEXP_listref_last (SEXP_t *list);
444
445 /**
446 * Get the n-th element of a list.
447 * This function creates a soft reference to the element.
448 * @param list the queried sexp object
449 * @param n the position of the element in the list
450 */
451 OSCAP_API SEXP_t *SEXP_listref_nth (SEXP_t *list, uint32_t n);
452
453 typedef struct SEXP_it SEXP_it_t;
454
455 #define SEXP_IT_RECURSIVE 0x01
456 #define SEXP_IT_HARDREF 0x02
457
458 typedef struct SEXP_list_it SEXP_list_it;
459
460 OSCAP_API SEXP_list_it *SEXP_list_it_new(const SEXP_t *list);
461 OSCAP_API SEXP_t *SEXP_list_it_next(SEXP_list_it *it);
462 OSCAP_API void SEXP_list_it_free(SEXP_list_it *it);
463
464 /* TODO: use alloca & softref_r here */
465 /**
466 * Iterate through a list, assigning each element to a variable.
467 * @param var the var variable is assigned one value from the list in each cycle
468 * @param list the list the loop iterates through
469 */
470 #define SEXP_list_foreach(var, list) \
471 for (uint32_t OSCAP_CONCAT(i,__LINE__) = 1; ((var) = SEXP_list_nth (list, OSCAP_CONCAT(i,__LINE__))) != NULL; ++OSCAP_CONCAT(i,__LINE__), SEXP_free (var), (var) = NULL)
472
473 /**
474 * Iterate through a sublist, assigning each element to a variable.
475 * @param var the var variable is assigned one value from the list in each cycle
476 * @param list the list the loop iterates through
477 * @param beg the index of the first element of the sublist
478 * @param end the index of the last element of the sublist
479 */
480 #define SEXP_sublist_foreach(var, list, beg, end) \
481 for (uint32_t OSCAP_CONCAT(i,__LINE__) = (beg); OSCAP_CONCAT(i,__LINE__) <= ((size_t)(end)) && ((var) = SEXP_list_nth (list, OSCAP_CONCAT(i,__LINE__))) != NULL; ++OSCAP_CONCAT(i,__LINE__), SEXP_free (var), (var) = NULL)
482
483 #define SEXP_LIST_END (UINT32_MAX - 1)
484
485 /*
486 * generic
487 */
488 OSCAP_API SEXP_t *SEXP_new (void);
489
490 OSCAP_API bool SEXP_emptyp(SEXP_t *sexp);
491
492 /**
493 * Create a new reference to a sexp object.
494 * @param s_exp the object of which to increment the reference count
495 */
496 OSCAP_API SEXP_t *SEXP_ref (const SEXP_t *s_exp);
497
498 OSCAP_API SEXP_t *SEXP_unref (SEXP_t *s_exp_o);
499
500 /**
501 * Create a new soft reference to a sexp object.
502 * @param s_exp the object to which create the soft reference
503 */
504 OSCAP_API SEXP_t *SEXP_softref (SEXP_t *s_exp);
505
506 /**
507 * Check whether an S-exp reference is a "soft" reference
508 * @param s_exp the S-exp reference
509 */
510 OSCAP_API bool SEXP_softrefp(const SEXP_t *s_exp);
511
512 /**
513 * Return the value of the reference counter
514 * @param ref
515 */
516 OSCAP_API uint32_t SEXP_refs (const SEXP_t *ref);
517
518 OSCAP_API bool SEXP_eq (const SEXP_t *a, const SEXP_t *b);
519
520 /**
521 * Compare reference pointers.
522 */
523 OSCAP_API int SEXP_refcmp(const SEXP_t *a, const SEXP_t *b);
524
525 OSCAP_API bool SEXP_deepcmp(const SEXP_t *a, const SEXP_t *b);
526
527 /**
528 * Free a sexp object.
529 * @param s_exp the object to be freed
530 */
531 OSCAP_API void SEXP_free (SEXP_t *s_exp);
532
533 /**
534 * Get the user data type of a sexp object.
535 * @param s_exp the object to be queried
536 */
537 OSCAP_API const char *SEXP_datatype (const SEXP_t *s_exp);
538
539 /**
540 * Set the user data type of a sexp object.
541 * @param s_exp the object to be modified
542 */
543 OSCAP_API int SEXP_datatype_set (SEXP_t *s_exp, const char *name) __attribute__ ((nonnull (2)));
544
545 /**
546 * Set the user data type of the nth sexp object in a list.
547 * @param list list containing the object to be modified
548 * @param n the position of the object
549 * @param name name of the user data type
550 */
551 OSCAP_API int SEXP_datatype_set_nth (SEXP_t *list, uint32_t n, const char *name) __attribute__ ((nonnull (3)));
552
553 /**
554 * Get the type of a sexp object.
555 * @param s_exp the object to be queried
556 */
557 OSCAP_API SEXP_type_t SEXP_typeof (const SEXP_t *s_exp);
558
559 /**
560 * Get a text description of the sexp object's type.
561 * @param s_exp the object to be queried
562 */
563 OSCAP_API const char *SEXP_strtype (const SEXP_t *s_exp);
564
565 OSCAP_API SEXP_t *SEXP_build (const char *s_str, ...);
566
567 OSCAP_API size_t SEXP_sizeof (const SEXP_t *s_exp);
568
569 #if !defined(NDEBUG)
570 # define SEXP_VALIDATE(s) __SEXP_VALIDATE(s, __FILE__, __LINE__, __PRETTY_FUNCTION__)
571 # include <stdlib.h>
572
573 OSCAP_API void __SEXP_VALIDATE(const SEXP_t *s_exp, const char *file, uint32_t line, const char *func);
574
575 #else
576 # define SEXP_VALIDATE(s)
577 #endif
578
579 #ifdef __cplusplus
580 }
581 #endif
582
583 #endif /* SEXP_MANIP_H */
584 /// @}
585 /// @}
586 /// @}