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
149 ucl_macro_handler handler;
150 ucl_context_macro_handler context_handler;
159 struct ucl_stack *next;
164 const unsigned char *begin;
165 const unsigned char *end;
166 const unsigned char *pos;
171 enum ucl_duplicate_strategy strategy;
172 enum ucl_parse_type parse_type;
173 struct ucl_chunk *next;
179 struct ucl_pubkey *next;
183 struct ucl_pubkey *next;
187 struct ucl_variable {
192 struct ucl_variable *prev, *next;
196 enum ucl_parser_state state;
197 enum ucl_parser_state prev_state;
198 unsigned int recursion;
200 unsigned default_priority;
202 ucl_object_t *top_obj;
203 ucl_object_t *cur_obj;
204 ucl_object_t *trash_objs;
205 ucl_object_t *includepaths;
207 struct ucl_macro *macroes;
208 struct ucl_stack *stack;
209 struct ucl_chunk *chunks;
210 struct ucl_pubkey *keys;
211 struct ucl_variable *variables;
212 ucl_variable_handler var_handler;
217 struct ucl_object_userdata {
219 ucl_userdata_dtor dtor;
220 ucl_userdata_emitter emitter;
224 * Unescape json string inplace
227 size_t ucl_unescape_json_string (char *str, size_t len);
230 * Handle include macro
231 * @param data include data
232 * @param len length of data
233 * @param args UCL object representing arguments to the macro
234 * @param ud user data
237 bool ucl_include_handler (const unsigned char *data, size_t len,
238 const ucl_object_t *args, void* ud);
241 * Handle tryinclude macro
242 * @param data include data
243 * @param len length of data
244 * @param args UCL object representing arguments to the macro
245 * @param ud user data
248 bool ucl_try_include_handler (const unsigned char *data, size_t len,
249 const ucl_object_t *args, void* ud);
252 * Handle includes macro
253 * @param data include data
254 * @param len length of data
255 * @param args UCL object representing arguments to the macro
256 * @param ud user data
259 bool ucl_includes_handler (const unsigned char *data, size_t len,
260 const ucl_object_t *args, void* ud);
263 * Handle priority macro
264 * @param data include data
265 * @param len length of data
266 * @param args UCL object representing arguments to the macro
267 * @param ud user data
270 bool ucl_priority_handler (const unsigned char *data, size_t len,
271 const ucl_object_t *args, void* ud);
275 * @param data include data
276 * @param len length of data
277 * @param args UCL object representing arguments to the macro
278 * @param ud user data
281 bool ucl_load_handler (const unsigned char *data, size_t len,
282 const ucl_object_t *args, void* ud);
284 * Handle inherit macro
285 * @param data include data
286 * @param len length of data
287 * @param args UCL object representing arguments to the macro
288 * @param ctx the current context object
289 * @param ud user data
292 bool ucl_inherit_handler (const unsigned char *data, size_t len,
293 const ucl_object_t *args, const ucl_object_t *ctx, void* ud);
295 size_t ucl_strlcpy (char *dst, const char *src, size_t siz);
296 size_t ucl_strlcpy_unsafe (char *dst, const char *src, size_t siz);
297 size_t ucl_strlcpy_tolower (char *dst, const char *src, size_t siz);
299 char *ucl_strnstr (const char *s, const char *find, int len);
300 char *ucl_strncasestr (const char *s, const char *find, int len);
304 ucl_create_err (UT_string **err, const char *fmt, ...)
305 __attribute__ (( format( printf, 2, 3) ));
308 #undef UCL_FATAL_ERRORS
311 ucl_create_err (UT_string **err, const char *fmt, ...)
317 utstring_printf_va (*err, fmt, ap);
321 #ifdef UCL_FATAL_ERRORS
327 * Check whether a given string contains a boolean value
328 * @param obj object to set
329 * @param start start of a string
330 * @param len length of a string
331 * @return true if a string is a boolean value
334 ucl_maybe_parse_boolean (ucl_object_t *obj, const unsigned char *start, size_t len)
336 const char *p = (const char *)start;
337 bool ret = false, val = false;
340 if ((p[0] == 'f' || p[0] == 'F') && strncasecmp (p, "false", 5) == 0) {
346 if ((p[0] == 't' || p[0] == 'T') && strncasecmp (p, "true", 4) == 0) {
352 if ((p[0] == 'y' || p[0] == 'Y') && strncasecmp (p, "yes", 3) == 0) {
356 else if ((p[0] == 'o' || p[0] == 'O') && strncasecmp (p, "off", 3) == 0) {
362 if ((p[0] == 'n' || p[0] == 'N') && strncasecmp (p, "no", 2) == 0) {
366 else if ((p[0] == 'o' || p[0] == 'O') && strncasecmp (p, "on", 2) == 0) {
372 if (ret && obj != NULL) {
373 obj->type = UCL_BOOLEAN;
381 * Check numeric string
382 * @param obj object to set if a string is numeric
383 * @param start start of string
384 * @param end end of string
385 * @param pos position where parsing has stopped
386 * @param allow_double allow parsing of floating point values
387 * @return 0 if string is numeric and error code (EINVAL or ERANGE) in case of conversion error
389 int ucl_maybe_parse_number (ucl_object_t *obj,
390 const char *start, const char *end, const char **pos,
391 bool allow_double, bool number_bytes, bool allow_time);
394 static inline const ucl_object_t *
395 ucl_hash_search_obj (ucl_hash_t* hashlin, ucl_object_t *obj)
397 return (const ucl_object_t *)ucl_hash_search (hashlin, obj->key, obj->keylen);
400 static inline ucl_hash_t * ucl_hash_insert_object (ucl_hash_t *hashlin,
401 const ucl_object_t *obj,
402 bool ignore_case) UCL_WARN_UNUSED_RESULT;
404 static inline ucl_hash_t *
405 ucl_hash_insert_object (ucl_hash_t *hashlin,
406 const ucl_object_t *obj,
409 if (hashlin == NULL) {
410 hashlin = ucl_hash_create (ignore_case);
412 ucl_hash_insert (hashlin, obj, obj->key, obj->keylen);
418 * Get standard emitter context for a specified emit_type
419 * @param emit_type type of emitter
420 * @return context or NULL if input is invalid
422 const struct ucl_emitter_context *
423 ucl_emit_get_standard_context (enum ucl_emitter emit_type);
426 * Serialize string as JSON string
427 * @param str string to emit
428 * @param buf target buffer
430 void ucl_elt_string_write_json (const char *str, size_t size,
431 struct ucl_emitter_context *ctx);
434 * Write multiline string using `EOD` as string terminator
439 void ucl_elt_string_write_multiline (const char *str, size_t size,
440 struct ucl_emitter_context *ctx);
443 * Emit a single object to string
447 unsigned char * ucl_object_emit_single_json (const ucl_object_t *obj);
450 * Check whether a specified string is long and should be likely printed in
455 bool ucl_maybe_long_string (const ucl_object_t *obj);
458 * Print integer to the msgpack output
462 void ucl_emitter_print_int_msgpack (struct ucl_emitter_context *ctx,
465 * Print integer to the msgpack output
469 void ucl_emitter_print_double_msgpack (struct ucl_emitter_context *ctx,
472 * Print double to the msgpack output
476 void ucl_emitter_print_bool_msgpack (struct ucl_emitter_context *ctx,
479 * Print string to the msgpack output
484 void ucl_emitter_print_string_msgpack (struct ucl_emitter_context *ctx,
485 const char *s, size_t len);
488 * Print binary string to the msgpack output
493 void ucl_emitter_print_binary_string_msgpack (struct ucl_emitter_context *ctx,
494 const char *s, size_t len);
497 * Print array preamble for msgpack
501 void ucl_emitter_print_array_msgpack (struct ucl_emitter_context *ctx,
505 * Print object preamble for msgpack
509 void ucl_emitter_print_object_msgpack (struct ucl_emitter_context *ctx,
512 * Print NULL to the msgpack output
515 void ucl_emitter_print_null_msgpack (struct ucl_emitter_context *ctx);
517 * Print object's key if needed to the msgpack output
522 void ucl_emitter_print_key_msgpack (bool print_key,
523 struct ucl_emitter_context *ctx,
524 const ucl_object_t *obj);
527 * Add new element to an object using the current merge strategy and priority
532 bool ucl_parser_process_object_element (struct ucl_parser *parser,
536 * Parse msgpack chunk
540 bool ucl_parse_msgpack (struct ucl_parser *parser);
542 #endif /* UCL_INTERNAL_H_ */