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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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_chunk {
  189. const unsigned char *begin;
  190. const unsigned char *end;
  191. const unsigned char *pos;
  192. char *fname;
  193. size_t remain;
  194. unsigned int line;
  195. unsigned int column;
  196. unsigned priority;
  197. enum ucl_duplicate_strategy strategy;
  198. enum ucl_parse_type parse_type;
  199. struct ucl_parser_special_handler *special_handler;
  200. struct ucl_chunk *next;
  201. };
  202. #ifdef HAVE_OPENSSL
  203. struct ucl_pubkey {
  204. EVP_PKEY *key;
  205. struct ucl_pubkey *next;
  206. };
  207. #else
  208. struct ucl_pubkey {
  209. struct ucl_pubkey *next;
  210. };
  211. #endif
  212. struct ucl_variable {
  213. char *var;
  214. char *value;
  215. size_t var_len;
  216. size_t value_len;
  217. struct ucl_variable *prev, *next;
  218. };
  219. struct ucl_parser {
  220. enum ucl_parser_state state;
  221. enum ucl_parser_state prev_state;
  222. unsigned int recursion;
  223. int flags;
  224. unsigned default_priority;
  225. int err_code;
  226. ucl_object_t *top_obj;
  227. ucl_object_t *cur_obj;
  228. ucl_object_t *trash_objs;
  229. ucl_object_t *includepaths;
  230. char *cur_file;
  231. struct ucl_macro *macroes;
  232. struct ucl_stack *stack;
  233. struct ucl_chunk *chunks;
  234. struct ucl_pubkey *keys;
  235. struct ucl_parser_special_handler *special_handlers;
  236. struct ucl_variable *variables;
  237. ucl_variable_handler var_handler;
  238. void *var_data;
  239. ucl_object_t *comments;
  240. ucl_object_t *last_comment;
  241. UT_string *err;
  242. };
  243. struct ucl_object_userdata {
  244. ucl_object_t obj;
  245. ucl_userdata_dtor dtor;
  246. ucl_userdata_emitter emitter;
  247. };
  248. /**
  249. * Unescape json string inplace
  250. * @param str
  251. */
  252. size_t ucl_unescape_json_string (char *str, size_t len);
  253. /**
  254. * Unescape single quoted string inplace
  255. * @param str
  256. */
  257. size_t ucl_unescape_squoted_string (char *str, size_t len);
  258. /**
  259. * Handle include macro
  260. * @param data include data
  261. * @param len length of data
  262. * @param args UCL object representing arguments to the macro
  263. * @param ud user data
  264. * @return
  265. */
  266. bool ucl_include_handler (const unsigned char *data, size_t len,
  267. const ucl_object_t *args, void* ud);
  268. /**
  269. * Handle tryinclude macro
  270. * @param data include data
  271. * @param len length of data
  272. * @param args UCL object representing arguments to the macro
  273. * @param ud user data
  274. * @return
  275. */
  276. bool ucl_try_include_handler (const unsigned char *data, size_t len,
  277. const ucl_object_t *args, void* ud);
  278. /**
  279. * Handle includes macro
  280. * @param data include data
  281. * @param len length of data
  282. * @param args UCL object representing arguments to the macro
  283. * @param ud user data
  284. * @return
  285. */
  286. bool ucl_includes_handler (const unsigned char *data, size_t len,
  287. const ucl_object_t *args, void* ud);
  288. /**
  289. * Handle priority macro
  290. * @param data include data
  291. * @param len length of data
  292. * @param args UCL object representing arguments to the macro
  293. * @param ud user data
  294. * @return
  295. */
  296. bool ucl_priority_handler (const unsigned char *data, size_t len,
  297. const ucl_object_t *args, void* ud);
  298. /**
  299. * Handle load macro
  300. * @param data include data
  301. * @param len length of data
  302. * @param args UCL object representing arguments to the macro
  303. * @param ud user data
  304. * @return
  305. */
  306. bool ucl_load_handler (const unsigned char *data, size_t len,
  307. const ucl_object_t *args, void* ud);
  308. /**
  309. * Handle inherit macro
  310. * @param data include data
  311. * @param len length of data
  312. * @param args UCL object representing arguments to the macro
  313. * @param ctx the current context object
  314. * @param ud user data
  315. * @return
  316. */
  317. bool ucl_inherit_handler (const unsigned char *data, size_t len,
  318. const ucl_object_t *args, const ucl_object_t *ctx, void* ud);
  319. size_t ucl_strlcpy (char *dst, const char *src, size_t siz);
  320. size_t ucl_strlcpy_unsafe (char *dst, const char *src, size_t siz);
  321. size_t ucl_strlcpy_tolower (char *dst, const char *src, size_t siz);
  322. char *ucl_strnstr (const char *s, const char *find, int len);
  323. char *ucl_strncasestr (const char *s, const char *find, int len);
  324. #ifdef __GNUC__
  325. static inline void
  326. ucl_create_err (UT_string **err, const char *fmt, ...)
  327. __attribute__ (( format( printf, 2, 3) ));
  328. #endif
  329. #undef UCL_FATAL_ERRORS
  330. static inline void
  331. ucl_create_err (UT_string **err, const char *fmt, ...)
  332. {
  333. if (*err == NULL) {
  334. utstring_new (*err);
  335. va_list ap;
  336. va_start (ap, fmt);
  337. utstring_printf_va (*err, fmt, ap);
  338. va_end (ap);
  339. }
  340. #ifdef UCL_FATAL_ERRORS
  341. assert (0);
  342. #endif
  343. }
  344. /**
  345. * Check whether a given string contains a boolean value
  346. * @param obj object to set
  347. * @param start start of a string
  348. * @param len length of a string
  349. * @return true if a string is a boolean value
  350. */
  351. static inline bool
  352. ucl_maybe_parse_boolean (ucl_object_t *obj, const unsigned char *start, size_t len)
  353. {
  354. const char *p = (const char *)start;
  355. bool ret = false, val = false;
  356. if (len == 5) {
  357. if ((p[0] == 'f' || p[0] == 'F') && strncasecmp (p, "false", 5) == 0) {
  358. ret = true;
  359. val = false;
  360. }
  361. }
  362. else if (len == 4) {
  363. if ((p[0] == 't' || p[0] == 'T') && strncasecmp (p, "true", 4) == 0) {
  364. ret = true;
  365. val = true;
  366. }
  367. }
  368. else if (len == 3) {
  369. if ((p[0] == 'y' || p[0] == 'Y') && strncasecmp (p, "yes", 3) == 0) {
  370. ret = true;
  371. val = true;
  372. }
  373. else if ((p[0] == 'o' || p[0] == 'O') && strncasecmp (p, "off", 3) == 0) {
  374. ret = true;
  375. val = false;
  376. }
  377. }
  378. else if (len == 2) {
  379. if ((p[0] == 'n' || p[0] == 'N') && strncasecmp (p, "no", 2) == 0) {
  380. ret = true;
  381. val = false;
  382. }
  383. else if ((p[0] == 'o' || p[0] == 'O') && strncasecmp (p, "on", 2) == 0) {
  384. ret = true;
  385. val = true;
  386. }
  387. }
  388. if (ret && obj != NULL) {
  389. obj->type = UCL_BOOLEAN;
  390. obj->value.iv = val;
  391. }
  392. return ret;
  393. }
  394. /**
  395. * Check numeric string
  396. * @param obj object to set if a string is numeric
  397. * @param start start of string
  398. * @param end end of string
  399. * @param pos position where parsing has stopped
  400. * @param allow_double allow parsing of floating point values
  401. * @return 0 if string is numeric and error code (EINVAL or ERANGE) in case of conversion error
  402. */
  403. int ucl_maybe_parse_number (ucl_object_t *obj,
  404. const char *start, const char *end, const char **pos,
  405. bool allow_double, bool number_bytes, bool allow_time);
  406. static inline const ucl_object_t *
  407. ucl_hash_search_obj (ucl_hash_t* hashlin, ucl_object_t *obj)
  408. {
  409. return (const ucl_object_t *)ucl_hash_search (hashlin, obj->key, obj->keylen);
  410. }
  411. static inline ucl_hash_t * ucl_hash_insert_object (ucl_hash_t *hashlin,
  412. const ucl_object_t *obj,
  413. bool ignore_case) UCL_WARN_UNUSED_RESULT;
  414. static inline ucl_hash_t *
  415. ucl_hash_insert_object (ucl_hash_t *hashlin,
  416. const ucl_object_t *obj,
  417. bool ignore_case)
  418. {
  419. if (hashlin == NULL) {
  420. hashlin = ucl_hash_create (ignore_case);
  421. }
  422. ucl_hash_insert (hashlin, obj, obj->key, obj->keylen);
  423. return hashlin;
  424. }
  425. /**
  426. * Get standard emitter context for a specified emit_type
  427. * @param emit_type type of emitter
  428. * @return context or NULL if input is invalid
  429. */
  430. const struct ucl_emitter_context *
  431. ucl_emit_get_standard_context (enum ucl_emitter emit_type);
  432. /**
  433. * Serialize string as JSON string
  434. * @param str string to emit
  435. * @param buf target buffer
  436. */
  437. void ucl_elt_string_write_json (const char *str, size_t size,
  438. struct ucl_emitter_context *ctx);
  439. /**
  440. * Serialize string as single quoted string
  441. * @param str string to emit
  442. * @param buf target buffer
  443. */
  444. void
  445. ucl_elt_string_write_squoted (const char *str, size_t size,
  446. struct ucl_emitter_context *ctx);
  447. /**
  448. * Write multiline string using `EOD` as string terminator
  449. * @param str
  450. * @param size
  451. * @param ctx
  452. */
  453. void ucl_elt_string_write_multiline (const char *str, size_t size,
  454. struct ucl_emitter_context *ctx);
  455. /**
  456. * Emit a single object to string
  457. * @param obj
  458. * @return
  459. */
  460. unsigned char * ucl_object_emit_single_json (const ucl_object_t *obj);
  461. /**
  462. * Check whether a specified string is long and should be likely printed in
  463. * multiline mode
  464. * @param obj
  465. * @return
  466. */
  467. bool ucl_maybe_long_string (const ucl_object_t *obj);
  468. /**
  469. * Print integer to the msgpack output
  470. * @param ctx
  471. * @param val
  472. */
  473. void ucl_emitter_print_int_msgpack (struct ucl_emitter_context *ctx,
  474. int64_t val);
  475. /**
  476. * Print integer to the msgpack output
  477. * @param ctx
  478. * @param val
  479. */
  480. void ucl_emitter_print_double_msgpack (struct ucl_emitter_context *ctx,
  481. double val);
  482. /**
  483. * Print double to the msgpack output
  484. * @param ctx
  485. * @param val
  486. */
  487. void ucl_emitter_print_bool_msgpack (struct ucl_emitter_context *ctx,
  488. bool val);
  489. /**
  490. * Print string to the msgpack output
  491. * @param ctx
  492. * @param s
  493. * @param len
  494. */
  495. void ucl_emitter_print_string_msgpack (struct ucl_emitter_context *ctx,
  496. const char *s, size_t len);
  497. /**
  498. * Print binary string to the msgpack output
  499. * @param ctx
  500. * @param s
  501. * @param len
  502. */
  503. void ucl_emitter_print_binary_string_msgpack (struct ucl_emitter_context *ctx,
  504. const char *s, size_t len);
  505. /**
  506. * Print array preamble for msgpack
  507. * @param ctx
  508. * @param len
  509. */
  510. void ucl_emitter_print_array_msgpack (struct ucl_emitter_context *ctx,
  511. size_t len);
  512. /**
  513. * Print object preamble for msgpack
  514. * @param ctx
  515. * @param len
  516. */
  517. void ucl_emitter_print_object_msgpack (struct ucl_emitter_context *ctx,
  518. size_t len);
  519. /**
  520. * Print NULL to the msgpack output
  521. * @param ctx
  522. */
  523. void ucl_emitter_print_null_msgpack (struct ucl_emitter_context *ctx);
  524. /**
  525. * Print object's key if needed to the msgpack output
  526. * @param print_key
  527. * @param ctx
  528. * @param obj
  529. */
  530. void ucl_emitter_print_key_msgpack (bool print_key,
  531. struct ucl_emitter_context *ctx,
  532. const ucl_object_t *obj);
  533. /**
  534. * Fetch URL into a buffer
  535. * @param url url to fetch
  536. * @param buf pointer to buffer (must be freed by callee)
  537. * @param buflen pointer to buffer length
  538. * @param err pointer to error argument
  539. * @param must_exist fail if cannot find a url
  540. */
  541. bool ucl_fetch_url (const unsigned char *url,
  542. unsigned char **buf,
  543. size_t *buflen,
  544. UT_string **err,
  545. bool must_exist);
  546. /**
  547. * Fetch a file and save results to the memory buffer
  548. * @param filename filename to fetch
  549. * @param len length of filename
  550. * @param buf target buffer
  551. * @param buflen target length
  552. * @return
  553. */
  554. bool ucl_fetch_file (const unsigned char *filename,
  555. unsigned char **buf,
  556. size_t *buflen,
  557. UT_string **err,
  558. bool must_exist);
  559. /**
  560. * Add new element to an object using the current merge strategy and priority
  561. * @param parser
  562. * @param nobj
  563. * @return
  564. */
  565. bool ucl_parser_process_object_element (struct ucl_parser *parser,
  566. ucl_object_t *nobj);
  567. /**
  568. * Parse msgpack chunk
  569. * @param parser
  570. * @return
  571. */
  572. bool ucl_parse_msgpack (struct ucl_parser *parser);
  573. bool ucl_parse_csexp (struct ucl_parser *parser);
  574. /**
  575. * Free ucl chunk
  576. * @param chunk
  577. */
  578. void ucl_chunk_free (struct ucl_chunk *chunk);
  579. #endif /* UCL_INTERNAL_H_ */