1 /* Copyright (c) 2013, Vsevolod Stakhov
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
12 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
13 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15 * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
16 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
19 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 #ifndef UCL_INTERNAL_H_
25 #define UCL_INTERNAL_H_
30 /* Help embedded builds */
31 #define HAVE_SYS_TYPES_H
32 #define HAVE_SYS_MMAN_H
33 #define HAVE_SYS_STAT_H
34 #define HAVE_SYS_PARAM_H
45 #define HAVE_STDBOOL_H
53 #ifdef HAVE_SYS_TYPES_H
54 #include <sys/types.h>
57 #ifdef HAVE_SYS_MMAN_H
59 # include <sys/mman.h>
62 #ifdef HAVE_SYS_STAT_H
65 #ifdef HAVE_SYS_PARAM_H
66 #include <sys/param.h>
99 #include <openssl/evp.h>
103 #define __DECONST(type, var) ((type)(uintptr_t)(const void *)(var))
107 * @file rcl_internal.h
108 * Internal structures and functions of UCL library
111 #define UCL_MAX_RECURSION 16
112 #define UCL_TRASH_KEY 0
113 #define UCL_TRASH_VALUE 1
115 enum ucl_parser_state {
121 UCL_STATE_AFTER_VALUE,
122 UCL_STATE_ARRAY_VALUE,
125 UCL_STATE_MACRO_NAME,
130 enum ucl_character_type {
131 UCL_CHARACTER_DENIED = 0,
132 UCL_CHARACTER_KEY = 1,
133 UCL_CHARACTER_KEY_START = 1 << 1,
134 UCL_CHARACTER_WHITESPACE = 1 << 2,
135 UCL_CHARACTER_WHITESPACE_UNSAFE = 1 << 3,
136 UCL_CHARACTER_VALUE_END = 1 << 4,
137 UCL_CHARACTER_VALUE_STR = 1 << 5,
138 UCL_CHARACTER_VALUE_DIGIT = 1 << 6,
139 UCL_CHARACTER_VALUE_DIGIT_START = 1 << 7,
140 UCL_CHARACTER_ESCAPE = 1 << 8,
141 UCL_CHARACTER_KEY_SEP = 1 << 9,
142 UCL_CHARACTER_JSON_UNSAFE = 1 << 10,
143 UCL_CHARACTER_UCL_UNSAFE = 1 << 11
148 ucl_macro_handler handler;
155 struct ucl_stack *next;
160 const unsigned char *begin;
161 const unsigned char *end;
162 const unsigned char *pos;
166 struct ucl_chunk *next;
172 struct ucl_pubkey *next;
176 struct ucl_pubkey *next;
180 struct ucl_variable {
185 struct ucl_variable *next;
189 enum ucl_parser_state state;
190 enum ucl_parser_state prev_state;
191 unsigned int recursion;
193 ucl_object_t *top_obj;
194 ucl_object_t *cur_obj;
195 struct ucl_macro *macroes;
196 struct ucl_stack *stack;
197 struct ucl_chunk *chunks;
198 struct ucl_pubkey *keys;
199 struct ucl_variable *variables;
200 ucl_variable_handler var_handler;
206 * Unescape json string inplace
209 size_t ucl_unescape_json_string (char *str, size_t len);
212 * Handle include macro
213 * @param data include data
214 * @param len length of data
215 * @param ud user data
216 * @param err error ptr
219 bool ucl_include_handler (const unsigned char *data, size_t len, void* ud);
221 bool ucl_try_include_handler (const unsigned char *data, size_t len, void* ud);
224 * Handle includes macro
225 * @param data include data
226 * @param len length of data
227 * @param ud user data
228 * @param err error ptr
231 bool ucl_includes_handler (const unsigned char *data, size_t len, void* ud);
233 size_t ucl_strlcpy (char *dst, const char *src, size_t siz);
234 size_t ucl_strlcpy_unsafe (char *dst, const char *src, size_t siz);
235 size_t ucl_strlcpy_tolower (char *dst, const char *src, size_t siz);
240 ucl_create_err (UT_string **err, const char *fmt, ...)
241 __attribute__ (( format( printf, 2, 3) ));
245 ucl_create_err (UT_string **err, const char *fmt, ...)
252 utstring_printf_va (*err, fmt, ap);
258 * Check whether a given string contains a boolean value
259 * @param obj object to set
260 * @param start start of a string
261 * @param len length of a string
262 * @return true if a string is a boolean value
265 ucl_maybe_parse_boolean (ucl_object_t *obj, const unsigned char *start, size_t len)
267 const unsigned char *p = start;
268 bool ret = false, val = false;
271 if ((p[0] == 'f' || p[0] == 'F') && strncasecmp (p, "false", 5) == 0) {
277 if ((p[0] == 't' || p[0] == 'T') && strncasecmp (p, "true", 4) == 0) {
283 if ((p[0] == 'y' || p[0] == 'Y') && strncasecmp (p, "yes", 3) == 0) {
287 else if ((p[0] == 'o' || p[0] == 'O') && strncasecmp (p, "off", 3) == 0) {
293 if ((p[0] == 'n' || p[0] == 'N') && strncasecmp (p, "no", 2) == 0) {
297 else if ((p[0] == 'o' || p[0] == 'O') && strncasecmp (p, "on", 2) == 0) {
304 obj->type = UCL_BOOLEAN;
312 * Check numeric string
313 * @param obj object to set if a string is numeric
314 * @param start start of string
315 * @param end end of string
316 * @param pos position where parsing has stopped
317 * @param allow_double allow parsing of floating point values
318 * @return 0 if string is numeric and error code (EINVAL or ERANGE) in case of conversion error
320 int ucl_maybe_parse_number (ucl_object_t *obj,
321 const char *start, const char *end, const char **pos,
322 bool allow_double, bool number_bytes, bool allow_time);
325 static inline const ucl_object_t *
326 ucl_hash_search_obj (ucl_hash_t* hashlin, ucl_object_t *obj)
328 return (const ucl_object_t *)ucl_hash_search (hashlin, obj->key, obj->keylen);
331 static inline ucl_hash_t *
332 ucl_hash_insert_object (ucl_hash_t *hashlin, const ucl_object_t *obj) UCL_WARN_UNUSED_RESULT;
334 static inline ucl_hash_t *
335 ucl_hash_insert_object (ucl_hash_t *hashlin, const ucl_object_t *obj)
337 if (hashlin == NULL) {
338 hashlin = ucl_hash_create ();
340 ucl_hash_insert (hashlin, obj, obj->key, obj->keylen);
346 * Emit a single object to string
350 unsigned char * ucl_object_emit_single_json (const ucl_object_t *obj);
352 #endif /* UCL_INTERNAL_H_ */