]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/libucl/src/ucl_internal.h
Update lldb to trunk r290819 and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / libucl / src / ucl_internal.h
1 /* Copyright (c) 2013, Vsevolod Stakhov
2  * All rights reserved.
3  *
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.
11  *
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.
22  */
23
24 #ifndef UCL_INTERNAL_H_
25 #define UCL_INTERNAL_H_
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #else
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
35 #define HAVE_LIMITS_H
36 #define HAVE_FCNTL_H
37 #define HAVE_ERRNO_H
38 #define HAVE_UNISTD_H
39 #define HAVE_CTYPE_H
40 #define HAVE_STDIO_H
41 #define HAVE_STRING_H
42 #define HAVE_FLOAT_H
43 #define HAVE_LIBGEN_H
44 #define HAVE_MATH_H
45 #define HAVE_STDBOOL_H
46 #define HAVE_STDINT_H
47 #define HAVE_STDARG_H
48 #ifndef _WIN32
49 # define HAVE_REGEX_H
50 #endif
51 #endif
52
53 #ifdef HAVE_SYS_TYPES_H
54 #include <sys/types.h>
55 #endif
56
57 #ifdef HAVE_SYS_MMAN_H
58 # ifndef _WIN32
59 #  include <sys/mman.h>
60 # endif
61 #endif
62 #ifdef HAVE_SYS_STAT_H
63 #include <sys/stat.h>
64 #endif
65 #ifdef HAVE_SYS_PARAM_H
66 #include <sys/param.h>
67 #endif
68
69 #ifdef HAVE_LIMITS_H
70 #include <limits.h>
71 #endif
72 #ifdef HAVE_FCNTL_H
73 #include <fcntl.h>
74 #endif
75 #ifdef HAVE_ERRNO_H
76 #include <errno.h>
77 #endif
78 #ifdef HAVE_UNISTD_H
79 #include <unistd.h>
80 #endif
81 #ifdef HAVE_CTYPE_H
82 #include <ctype.h>
83 #endif
84 #ifdef HAVE_STDIO_H
85 #include <stdio.h>
86 #endif
87 #ifdef HAVE_STRING_H
88 #include <string.h>
89 #endif
90
91 #include "utlist.h"
92 #include "utstring.h"
93 #include "uthash.h"
94 #include "ucl.h"
95 #include "ucl_hash.h"
96
97 #ifdef HAVE_OPENSSL
98 #include <openssl/evp.h>
99 #endif
100
101 #ifndef __DECONST
102 #define __DECONST(type, var)    ((type)(uintptr_t)(const void *)(var))
103 #endif
104
105 /**
106  * @file rcl_internal.h
107  * Internal structures and functions of UCL library
108  */
109
110 #define UCL_MAX_RECURSION 16
111 #define UCL_TRASH_KEY 0
112 #define UCL_TRASH_VALUE 1
113
114 enum ucl_parser_state {
115         UCL_STATE_INIT = 0,
116         UCL_STATE_OBJECT,
117         UCL_STATE_ARRAY,
118         UCL_STATE_KEY,
119         UCL_STATE_VALUE,
120         UCL_STATE_AFTER_VALUE,
121         UCL_STATE_ARRAY_VALUE,
122         UCL_STATE_SCOMMENT,
123         UCL_STATE_MCOMMENT,
124         UCL_STATE_MACRO_NAME,
125         UCL_STATE_MACRO,
126         UCL_STATE_ERROR
127 };
128
129 enum ucl_character_type {
130         UCL_CHARACTER_DENIED = 0,
131         UCL_CHARACTER_KEY = 1,
132         UCL_CHARACTER_KEY_START = 1 << 1,
133         UCL_CHARACTER_WHITESPACE = 1 << 2,
134         UCL_CHARACTER_WHITESPACE_UNSAFE = 1 << 3,
135         UCL_CHARACTER_VALUE_END = 1 << 4,
136         UCL_CHARACTER_VALUE_STR = 1 << 5,
137         UCL_CHARACTER_VALUE_DIGIT = 1 << 6,
138         UCL_CHARACTER_VALUE_DIGIT_START = 1 << 7,
139         UCL_CHARACTER_ESCAPE = 1 << 8,
140         UCL_CHARACTER_KEY_SEP = 1 << 9,
141         UCL_CHARACTER_JSON_UNSAFE = 1 << 10,
142         UCL_CHARACTER_UCL_UNSAFE = 1 << 11
143 };
144
145 struct ucl_macro {
146         char *name;
147         union {
148                 ucl_macro_handler handler;
149                 ucl_context_macro_handler context_handler;
150         } h;
151         void* ud;
152         bool is_context;
153         UT_hash_handle hh;
154 };
155
156 struct ucl_stack {
157         ucl_object_t *obj;
158         struct ucl_stack *next;
159         uint64_t level;
160 };
161
162 struct ucl_chunk {
163         const unsigned char *begin;
164         const unsigned char *end;
165         const unsigned char *pos;
166         size_t remain;
167         unsigned int line;
168         unsigned int column;
169         unsigned priority;
170         enum ucl_duplicate_strategy strategy;
171         enum ucl_parse_type parse_type;
172         struct ucl_chunk *next;
173 };
174
175 #ifdef HAVE_OPENSSL
176 struct ucl_pubkey {
177         EVP_PKEY *key;
178         struct ucl_pubkey *next;
179 };
180 #else
181 struct ucl_pubkey {
182         struct ucl_pubkey *next;
183 };
184 #endif
185
186 struct ucl_variable {
187         char *var;
188         char *value;
189         size_t var_len;
190         size_t value_len;
191         struct ucl_variable *prev, *next;
192 };
193
194 struct ucl_parser {
195         enum ucl_parser_state state;
196         enum ucl_parser_state prev_state;
197         unsigned int recursion;
198         int flags;
199         unsigned default_priority;
200         int err_code;
201         ucl_object_t *top_obj;
202         ucl_object_t *cur_obj;
203         ucl_object_t *trash_objs;
204         ucl_object_t *includepaths;
205         char *cur_file;
206         struct ucl_macro *macroes;
207         struct ucl_stack *stack;
208         struct ucl_chunk *chunks;
209         struct ucl_pubkey *keys;
210         struct ucl_variable *variables;
211         ucl_variable_handler var_handler;
212         void *var_data;
213         ucl_object_t *comments;
214         ucl_object_t *last_comment;
215         UT_string *err;
216 };
217
218 struct ucl_object_userdata {
219         ucl_object_t obj;
220         ucl_userdata_dtor dtor;
221         ucl_userdata_emitter emitter;
222 };
223
224 /**
225  * Unescape json string inplace
226  * @param str
227  */
228 size_t ucl_unescape_json_string (char *str, size_t len);
229
230 /**
231  * Handle include macro
232  * @param data include data
233  * @param len length of data
234  * @param args UCL object representing arguments to the macro
235  * @param ud user data
236  * @return
237  */
238 bool ucl_include_handler (const unsigned char *data, size_t len,
239                 const ucl_object_t *args, void* ud);
240
241 /**
242  * Handle tryinclude macro
243  * @param data include data
244  * @param len length of data
245  * @param args UCL object representing arguments to the macro
246  * @param ud user data
247  * @return
248  */
249 bool ucl_try_include_handler (const unsigned char *data, size_t len,
250                 const ucl_object_t *args, void* ud);
251
252 /**
253  * Handle includes macro
254  * @param data include data
255  * @param len length of data
256  * @param args UCL object representing arguments to the macro
257  * @param ud user data
258  * @return
259  */
260 bool ucl_includes_handler (const unsigned char *data, size_t len,
261                 const ucl_object_t *args, void* ud);
262
263 /**
264  * Handle priority macro
265  * @param data include data
266  * @param len length of data
267  * @param args UCL object representing arguments to the macro
268  * @param ud user data
269  * @return
270  */
271 bool ucl_priority_handler (const unsigned char *data, size_t len,
272                 const ucl_object_t *args, void* ud);
273
274 /**
275  * Handle load macro
276  * @param data include data
277  * @param len length of data
278  * @param args UCL object representing arguments to the macro
279  * @param ud user data
280  * @return
281  */
282 bool ucl_load_handler (const unsigned char *data, size_t len,
283                 const ucl_object_t *args, void* ud);
284 /**
285  * Handle inherit macro
286  * @param data include data
287  * @param len length of data
288  * @param args UCL object representing arguments to the macro
289  * @param ctx the current context object
290  * @param ud user data
291  * @return
292  */
293 bool ucl_inherit_handler (const unsigned char *data, size_t len,
294                 const ucl_object_t *args, const ucl_object_t *ctx, void* ud);
295
296 size_t ucl_strlcpy (char *dst, const char *src, size_t siz);
297 size_t ucl_strlcpy_unsafe (char *dst, const char *src, size_t siz);
298 size_t ucl_strlcpy_tolower (char *dst, const char *src, size_t siz);
299
300 char *ucl_strnstr (const char *s, const char *find, int len);
301 char *ucl_strncasestr (const char *s, const char *find, int len);
302
303 #ifdef __GNUC__
304 static inline void
305 ucl_create_err (UT_string **err, const char *fmt, ...)
306 __attribute__ (( format( printf, 2, 3) ));
307 #endif
308
309 #undef UCL_FATAL_ERRORS
310
311 static inline void
312 ucl_create_err (UT_string **err, const char *fmt, ...)
313 {
314         if (*err == NULL) {
315                 utstring_new (*err);
316                 va_list ap;
317                 va_start (ap, fmt);
318                 utstring_printf_va (*err, fmt, ap);
319                 va_end (ap);
320         }
321
322 #ifdef UCL_FATAL_ERRORS
323         assert (0);
324 #endif
325 }
326
327 /**
328  * Check whether a given string contains a boolean value
329  * @param obj object to set
330  * @param start start of a string
331  * @param len length of a string
332  * @return true if a string is a boolean value
333  */
334 static inline bool
335 ucl_maybe_parse_boolean (ucl_object_t *obj, const unsigned char *start, size_t len)
336 {
337         const char *p = (const char *)start;
338         bool ret = false, val = false;
339
340         if (len == 5) {
341                 if ((p[0] == 'f' || p[0] == 'F') && strncasecmp (p, "false", 5) == 0) {
342                         ret = true;
343                         val = false;
344                 }
345         }
346         else if (len == 4) {
347                 if ((p[0] == 't' || p[0] == 'T') && strncasecmp (p, "true", 4) == 0) {
348                         ret = true;
349                         val = true;
350                 }
351         }
352         else if (len == 3) {
353                 if ((p[0] == 'y' || p[0] == 'Y') && strncasecmp (p, "yes", 3) == 0) {
354                         ret = true;
355                         val = true;
356                 }
357                 else if ((p[0] == 'o' || p[0] == 'O') && strncasecmp (p, "off", 3) == 0) {
358                         ret = true;
359                         val = false;
360                 }
361         }
362         else if (len == 2) {
363                 if ((p[0] == 'n' || p[0] == 'N') && strncasecmp (p, "no", 2) == 0) {
364                         ret = true;
365                         val = false;
366                 }
367                 else if ((p[0] == 'o' || p[0] == 'O') && strncasecmp (p, "on", 2) == 0) {
368                         ret = true;
369                         val = true;
370                 }
371         }
372
373         if (ret && obj != NULL) {
374                 obj->type = UCL_BOOLEAN;
375                 obj->value.iv = val;
376         }
377
378         return ret;
379 }
380
381 /**
382  * Check numeric string
383  * @param obj object to set if a string is numeric
384  * @param start start of string
385  * @param end end of string
386  * @param pos position where parsing has stopped
387  * @param allow_double allow parsing of floating point values
388  * @return 0 if string is numeric and error code (EINVAL or ERANGE) in case of conversion error
389  */
390 int ucl_maybe_parse_number (ucl_object_t *obj,
391                 const char *start, const char *end, const char **pos,
392                 bool allow_double, bool number_bytes, bool allow_time);
393
394
395 static inline const ucl_object_t *
396 ucl_hash_search_obj (ucl_hash_t* hashlin, ucl_object_t *obj)
397 {
398         return (const ucl_object_t *)ucl_hash_search (hashlin, obj->key, obj->keylen);
399 }
400
401 static inline ucl_hash_t * ucl_hash_insert_object (ucl_hash_t *hashlin,
402                 const ucl_object_t *obj,
403                 bool ignore_case) UCL_WARN_UNUSED_RESULT;
404
405 static inline ucl_hash_t *
406 ucl_hash_insert_object (ucl_hash_t *hashlin,
407                 const ucl_object_t *obj,
408                 bool ignore_case)
409 {
410         if (hashlin == NULL) {
411                 hashlin = ucl_hash_create (ignore_case);
412         }
413         ucl_hash_insert (hashlin, obj, obj->key, obj->keylen);
414
415         return hashlin;
416 }
417
418 /**
419  * Get standard emitter context for a specified emit_type
420  * @param emit_type type of emitter
421  * @return context or NULL if input is invalid
422  */
423 const struct ucl_emitter_context *
424 ucl_emit_get_standard_context (enum ucl_emitter emit_type);
425
426 /**
427  * Serialize string as JSON string
428  * @param str string to emit
429  * @param buf target buffer
430  */
431 void ucl_elt_string_write_json (const char *str, size_t size,
432                 struct ucl_emitter_context *ctx);
433
434 /**
435  * Write multiline string using `EOD` as string terminator
436  * @param str
437  * @param size
438  * @param ctx
439  */
440 void ucl_elt_string_write_multiline (const char *str, size_t size,
441                 struct ucl_emitter_context *ctx);
442
443 /**
444  * Emit a single object to string
445  * @param obj
446  * @return
447  */
448 unsigned char * ucl_object_emit_single_json (const ucl_object_t *obj);
449
450 /**
451  * Check whether a specified string is long and should be likely printed in
452  * multiline mode
453  * @param obj
454  * @return
455  */
456 bool ucl_maybe_long_string (const ucl_object_t *obj);
457
458 /**
459  * Print integer to the msgpack output
460  * @param ctx
461  * @param val
462  */
463 void ucl_emitter_print_int_msgpack (struct ucl_emitter_context *ctx,
464                 int64_t val);
465 /**
466  * Print integer to the msgpack output
467  * @param ctx
468  * @param val
469  */
470 void ucl_emitter_print_double_msgpack (struct ucl_emitter_context *ctx,
471                 double val);
472 /**
473  * Print double to the msgpack output
474  * @param ctx
475  * @param val
476  */
477 void ucl_emitter_print_bool_msgpack (struct ucl_emitter_context *ctx,
478                 bool val);
479 /**
480  * Print string to the msgpack output
481  * @param ctx
482  * @param s
483  * @param len
484  */
485 void ucl_emitter_print_string_msgpack (struct ucl_emitter_context *ctx,
486                 const char *s, size_t len);
487
488 /**
489  * Print binary string to the msgpack output
490  * @param ctx
491  * @param s
492  * @param len
493  */
494 void ucl_emitter_print_binary_string_msgpack (struct ucl_emitter_context *ctx,
495                 const char *s, size_t len);
496
497 /**
498  * Print array preamble for msgpack
499  * @param ctx
500  * @param len
501  */
502 void ucl_emitter_print_array_msgpack (struct ucl_emitter_context *ctx,
503                 size_t len);
504
505 /**
506  * Print object preamble for msgpack
507  * @param ctx
508  * @param len
509  */
510 void ucl_emitter_print_object_msgpack (struct ucl_emitter_context *ctx,
511                 size_t len);
512 /**
513  * Print NULL to the msgpack output
514  * @param ctx
515  */
516 void ucl_emitter_print_null_msgpack (struct ucl_emitter_context *ctx);
517 /**
518  * Print object's key if needed to the msgpack output
519  * @param print_key
520  * @param ctx
521  * @param obj
522  */
523 void ucl_emitter_print_key_msgpack (bool print_key,
524                 struct ucl_emitter_context *ctx,
525                 const ucl_object_t *obj);
526
527 /**
528  * Fetch URL into a buffer
529  * @param url url to fetch
530  * @param buf pointer to buffer (must be freed by callee)
531  * @param buflen pointer to buffer length
532  * @param err pointer to error argument
533  * @param must_exist fail if cannot find a url
534  */
535 bool ucl_fetch_url (const unsigned char *url,
536                 unsigned char **buf,
537                 size_t *buflen,
538                 UT_string **err,
539                 bool must_exist);
540
541 /**
542  * Fetch a file and save results to the memory buffer
543  * @param filename filename to fetch
544  * @param len length of filename
545  * @param buf target buffer
546  * @param buflen target length
547  * @return
548  */
549 bool ucl_fetch_file (const unsigned char *filename,
550                 unsigned char **buf,
551                 size_t *buflen,
552                 UT_string **err,
553                 bool must_exist);
554
555 /**
556  * Add new element to an object using the current merge strategy and priority
557  * @param parser
558  * @param nobj
559  * @return
560  */
561 bool ucl_parser_process_object_element (struct ucl_parser *parser,
562                 ucl_object_t *nobj);
563
564 /**
565  * Parse msgpack chunk
566  * @param parser
567  * @return
568  */
569 bool ucl_parse_msgpack (struct ucl_parser *parser);
570
571 #endif /* UCL_INTERNAL_H_ */