Open SCAP Library
_sexp-parser.h
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Authors:
20  * "Daniel Kopecek" <dkopecek@redhat.com>
21  */
22 #pragma once
23 #ifndef SEXP_PARSER_H
24 #define SEXP_PARSER_H
25 
26 #include <stddef.h>
27 #include <stdint.h>
28 #include "public/sexp-parser.h"
29 #include "_sexp-manip.h"
30 #include "generic/spb.h"
31 
32 
33 /* Number classes */
34 #define SEXP_NUMCLASS_INV 0
35 #define SEXP_NUMCLASS_INT 1
36 #define SEXP_NUMCLASS_UINT 2
37 #define SEXP_NUMCLASS_FLT 3
38 #define SEXP_NUMCLASS_EXP 4
39 #define SEXP_NUMCLASS_FRA 5
40 #define SEXP_NUMCLASS_PRE 6
41 
42 #define SEXP_PFUNC_COUNT 8
43 
44 /*
45  * Parser state
46  */
47 struct SEXP_pstate {
48  /*
49  * Input data
50  */
51  spb_t *p_buffer; /* input buffer */
52  spb_size_t p_bufoff; /* start index - everything before this index in the buffer is already parsed */
53  spb_size_t p_explen; /* length of already parsed/checked part of the expression */
54  SEXP_pflags_t p_flags; /* current parser flags */
55  SEXP_t *p_sexp; /* last S-exp object */
56  SEXP_pflags_t p_flags0; /* initial parser flags */
57 
58  void *sp_data; /* subparser data */
59  void (*sp_free)(void *); /* function for freeing the subparser data */
60 
61  void *sp_shptr[SEXP_PFUNC_COUNT]; /* subparser shared pointer */
62  void (*sp_shfree[SEXP_PFUNC_COUNT])(void *); /* functions for freeing subparser shared pointer */
63 
64  uint8_t p_label; /* where to jump if p_explen > 0 */
65 
66  uint8_t p_numclass; /* number class */
67  uint8_t p_numbase; /* number base */
68  uint8_t p_numstage; /* number parsing stage */
69 
70  /*
71  * Output data
72  */
73  SEXP_lstack_t l_stack; /* output list stack */
74 
75  /*
76  * Value cache
77  */
78  uintptr_t v_bool[2]; /* true, false pre-allocated values */
79  uint8_t p_error;
80 };
81 
82 struct SEXP_pext_dsc {
83  spb_t *p_buffer;
84  spb_size_t p_bufoff;
85  spb_size_t p_explen;
86  SEXP_pflags_t p_flags;
87  SEXP_t *s_exp;
88  void *sp_data; /* subparser data */
89  void (*sp_free)(void *); /* function for freeing the subparser data */
90  void *sp_shptr[SEXP_PFUNC_COUNT];
91  void (*sp_shfree[SEXP_PFUNC_COUNT])(void *);
92 
93  uint8_t p_label;
94  uint8_t p_numclass;
95  uint8_t p_numbase;
96  uint8_t p_numstage;
97 
98  uintptr_t *v_bool;
99 };
100 
101 #define PEXT_DSC_INITIALIZER { NULL, 0, 0, NULL }
102 
103 #define __PARSE_RT int
104 #define __PARSE_PT(n1) struct SEXP_pext_dsc *n1
105 
106 typedef __PARSE_RT (SEXP_pfunc_t)(__PARSE_PT());
107 
108 
109 struct SEXP_psetup {
110  SEXP_format_t p_format; /* expected or required format (depends on p_flags) */
111  SEXP_pflags_t p_flags; /* initial parser flags */
112  SEXP_pfunc_t *p_funcp[SEXP_PFUNC_COUNT];
113 };
114 
115 #define SEXP_PFUNC_UL_STRING_SI 0
116 #define SEXP_PFUNC_UL_STRING_DQ 1
117 #define SEXP_PFUNC_UL_STRING_SQ 2
118 #define SEXP_PFUNC_KL_STRING 3
119 #define SEXP_PFUNC_UL_STRING_B64 4
120 #define SEXP_PFUNC_KL_STRING_B64 5
121 #define SEXP_PFUNC_UL_DATATYPE 6
122 #define SEXP_PFUNC_KL_DATATYPE 7
123 #define SEXP_PFUNC_BOOL 8
124 
125 __PARSE_RT SEXP_parse_ul_string_si (__PARSE_PT(dsc));
126 __PARSE_RT SEXP_parse_ul_string_dq (__PARSE_PT(dsc));
127 __PARSE_RT SEXP_parse_ul_string_sq (__PARSE_PT(dsc));
128 __PARSE_RT SEXP_parse_kl_string (__PARSE_PT(dsc));
129 __PARSE_RT SEXP_parse_ul_string_b64 (__PARSE_PT(dsc));
130 __PARSE_RT SEXP_parse_kl_string_b64 (__PARSE_PT(dsc));
131 __PARSE_RT SEXP_parse_ul_datatype (__PARSE_PT(dsc));
132 __PARSE_RT SEXP_parse_kl_datatype (__PARSE_PT(dsc));
133 __PARSE_RT SEXP_parse_bool (__PARSE_PT(dsc), bool val);
134 
135 #define SEXP_PSLOT_MAX 1024
136 
137 #define SEXP_PRET_SUCCESS 0
138 #define SEXP_PRET_EUNFIN 1 /* incomplete token */
139 #define SEXP_PRET_EINVAL 2 /* syntax error, invalid token */
140 #define SEXP_PRET_EUNDEF 255 /* unknown, unexpected error */
141 
142 #ifdef __GNUC__
143 # define __predict(expr, v) __builtin_expect (expr, v)
144 #else
145 # define __predict(expr, v) expr
146 #endif /* __GNUC__ */
147 
148 /*
149  * TODO: make this function public in the future
150  */
151 int SEXP_psetup_setpfunc(SEXP_psetup_t *psetup, int pfunctype, SEXP_pfunc_t *pfunc);
152 
153 
154 #endif /* SEXP_PARSER_H */
Definition: _sexp-manip.h:35
Definition: _sexp-parser.h:47
Definition: spb.h:48
Definition: sexp-types.h:82
Definition: _sexp-parser.h:109
Definition: _sexp-parser.h:82