You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ucl_internal.h 15KB

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