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;
167 struct ucl_chunk *next;
173 struct ucl_pubkey *next;
177 struct ucl_pubkey *next;
181 struct ucl_variable {
186 struct ucl_variable *prev, *next;
190 enum ucl_parser_state state;
191 enum ucl_parser_state prev_state;
192 unsigned int recursion;
194 ucl_object_t *top_obj;
195 ucl_object_t *cur_obj;
197 struct ucl_macro *macroes;
198 struct ucl_stack *stack;
199 struct ucl_chunk *chunks;
200 struct ucl_pubkey *keys;
201 struct ucl_variable *variables;
202 ucl_variable_handler var_handler;
207 struct ucl_object_userdata {
209 ucl_userdata_dtor dtor;
210 ucl_userdata_emitter emitter;
214 * Unescape json string inplace
217 size_t ucl_unescape_json_string (char *str, size_t len);
220 * Handle include macro
221 * @param data include data
222 * @param len length of data
223 * @param ud user data
224 * @param err error ptr
227 bool ucl_include_handler (const unsigned char *data, size_t len,
228 const ucl_object_t *args, void* ud);
230 bool ucl_try_include_handler (const unsigned char *data, size_t len,
231 const ucl_object_t *args, void* ud);
234 * Handle includes macro
235 * @param data include data
236 * @param len length of data
237 * @param ud user data
238 * @param err error ptr
241 bool ucl_includes_handler (const unsigned char *data, size_t len,
242 const ucl_object_t *args, void* ud);
244 size_t ucl_strlcpy (char *dst, const char *src, size_t siz);
245 size_t ucl_strlcpy_unsafe (char *dst, const char *src, size_t siz);
246 size_t ucl_strlcpy_tolower (char *dst, const char *src, size_t siz);
251 ucl_create_err (UT_string **err, const char *fmt, ...)
252 __attribute__ (( format( printf, 2, 3) ));
256 ucl_create_err (UT_string **err, const char *fmt, ...)
263 utstring_printf_va (*err, fmt, ap);
269 * Check whether a given string contains a boolean value
270 * @param obj object to set
271 * @param start start of a string
272 * @param len length of a string
273 * @return true if a string is a boolean value
276 ucl_maybe_parse_boolean (ucl_object_t *obj, const unsigned char *start, size_t len)
278 const char *p = (const char *)start;
279 bool ret = false, val = false;
282 if ((p[0] == 'f' || p[0] == 'F') && strncasecmp (p, "false", 5) == 0) {
288 if ((p[0] == 't' || p[0] == 'T') && strncasecmp (p, "true", 4) == 0) {
294 if ((p[0] == 'y' || p[0] == 'Y') && strncasecmp (p, "yes", 3) == 0) {
298 else if ((p[0] == 'o' || p[0] == 'O') && strncasecmp (p, "off", 3) == 0) {
304 if ((p[0] == 'n' || p[0] == 'N') && strncasecmp (p, "no", 2) == 0) {
308 else if ((p[0] == 'o' || p[0] == 'O') && strncasecmp (p, "on", 2) == 0) {
315 obj->type = UCL_BOOLEAN;
323 * Check numeric string
324 * @param obj object to set if a string is numeric
325 * @param start start of string
326 * @param end end of string
327 * @param pos position where parsing has stopped
328 * @param allow_double allow parsing of floating point values
329 * @return 0 if string is numeric and error code (EINVAL or ERANGE) in case of conversion error
331 int ucl_maybe_parse_number (ucl_object_t *obj,
332 const char *start, const char *end, const char **pos,
333 bool allow_double, bool number_bytes, bool allow_time);
336 static inline const ucl_object_t *
337 ucl_hash_search_obj (ucl_hash_t* hashlin, ucl_object_t *obj)
339 return (const ucl_object_t *)ucl_hash_search (hashlin, obj->key, obj->keylen);
342 static inline ucl_hash_t * ucl_hash_insert_object (ucl_hash_t *hashlin,
343 const ucl_object_t *obj,
344 bool ignore_case) UCL_WARN_UNUSED_RESULT;
346 static inline ucl_hash_t *
347 ucl_hash_insert_object (ucl_hash_t *hashlin,
348 const ucl_object_t *obj,
351 if (hashlin == NULL) {
352 hashlin = ucl_hash_create (ignore_case);
354 ucl_hash_insert (hashlin, obj, obj->key, obj->keylen);
360 * Get standard emitter context for a specified emit_type
361 * @param emit_type type of emitter
362 * @return context or NULL if input is invalid
364 const struct ucl_emitter_context *
365 ucl_emit_get_standard_context (enum ucl_emitter emit_type);
368 * Serialize string as JSON string
369 * @param str string to emit
370 * @param buf target buffer
372 void ucl_elt_string_write_json (const char *str, size_t size,
373 struct ucl_emitter_context *ctx);
376 * Write multiline string using `EOD` as string terminator
381 void ucl_elt_string_write_multiline (const char *str, size_t size,
382 struct ucl_emitter_context *ctx);
385 * Emit a single object to string
389 unsigned char * ucl_object_emit_single_json (const ucl_object_t *obj);
392 * Check whether a specified string is long and should be likely printed in
397 bool ucl_maybe_long_string (const ucl_object_t *obj);
399 #endif /* UCL_INTERNAL_H_ */