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.
25 #include "ucl_internal.h"
26 #include "ucl_chartable.h"
29 #include <libgen.h> /* For dirname */
33 #include <openssl/err.h>
34 #include <openssl/sha.h>
35 #include <openssl/rsa.h>
36 #include <openssl/ssl.h>
37 #include <openssl/evp.h>
41 #include <curl/curl.h>
56 #ifndef PROT_READWRITE
57 #define PROT_READWRITE 3
66 #define MAP_FAILED ((void *) -1)
69 static void *ucl_mmap(char *addr, size_t length, int prot, int access, int fd, off_t offset)
72 HANDLE handle = INVALID_HANDLE_VALUE;
78 handle = CreateFileMapping((HANDLE) _get_osfhandle(fd), 0, PAGE_READONLY, 0, length, 0);
80 map = (void *) MapViewOfFile(handle, FILE_MAP_READ, 0, 0, length);
86 handle = CreateFileMapping((HANDLE) _get_osfhandle(fd), 0, PAGE_READWRITE, 0, length, 0);
88 map = (void *) MapViewOfFile(handle, FILE_MAP_WRITE, 0, 0, length);
94 handle = CreateFileMapping((HANDLE) _get_osfhandle(fd), 0, PAGE_READWRITE, 0, length, 0);
96 map = (void *) MapViewOfFile(handle, FILE_MAP_ALL_ACCESS, 0, 0, length);
101 if (map == (void *) NULL) {
102 return (void *) MAP_FAILED;
104 return (void *) ((char *) map + offset);
107 static int ucl_munmap(void *map,size_t length)
109 if (!UnmapViewOfFile(map)) {
115 static char* ucl_realpath(const char *path, char *resolved_path) {
117 char tmp[MAX_PATH + 1];
118 strncpy(tmp, path, sizeof(tmp)-1);
121 if (*p == '/') *p = '\\';
124 return _fullpath(resolved_path, tmp, MAX_PATH);
127 #define ucl_mmap mmap
128 #define ucl_munmap munmap
129 #define ucl_realpath realpath
134 * Utilities for rcl parsing
137 typedef void (*ucl_object_dtor) (ucl_object_t *obj);
138 static void ucl_object_free_internal (ucl_object_t *obj, bool allow_rec,
139 ucl_object_dtor dtor);
140 static void ucl_object_dtor_unref (ucl_object_t *obj);
143 ucl_object_dtor_free (ucl_object_t *obj)
145 if (obj->trash_stack[UCL_TRASH_KEY] != NULL) {
146 UCL_FREE (obj->hh.keylen, obj->trash_stack[UCL_TRASH_KEY]);
148 if (obj->trash_stack[UCL_TRASH_VALUE] != NULL) {
149 UCL_FREE (obj->len, obj->trash_stack[UCL_TRASH_VALUE]);
151 UCL_FREE (sizeof (ucl_object_t), obj);
155 * This is a helper function that performs exactly the same as
156 * `ucl_object_unref` but it doesn't iterate over elements allowing
157 * to use it for individual elements of arrays and multiple values
160 ucl_object_dtor_unref_single (ucl_object_t *obj)
163 #ifdef HAVE_ATOMIC_BUILTINS
164 unsigned int rc = __sync_sub_and_fetch (&obj->ref, 1);
167 if (--obj->ref == 0) {
169 ucl_object_free_internal (obj, false, ucl_object_dtor_unref);
175 ucl_object_dtor_unref (ucl_object_t *obj)
178 ucl_object_dtor_free (obj);
181 /* This may cause dtor unref being called one more time */
182 ucl_object_dtor_unref_single (obj);
187 ucl_object_free_internal (ucl_object_t *obj, bool allow_rec, ucl_object_dtor dtor)
189 ucl_object_t *sub, *tmp;
191 while (obj != NULL) {
192 if (obj->type == UCL_ARRAY) {
194 while (sub != NULL) {
200 else if (obj->type == UCL_OBJECT) {
201 if (obj->value.ov != NULL) {
202 ucl_hash_destroy (obj->value.ov, (ucl_hash_free_func *)dtor);
216 ucl_object_free (ucl_object_t *obj)
218 ucl_object_free_internal (obj, true, ucl_object_dtor_free);
222 ucl_unescape_json_string (char *str, size_t len)
224 char *t = str, *h = str;
230 /* t is target (tortoise), h is source (hare) */
261 for (i = 0; i < 4; i++) {
263 if (isdigit (h[i])) {
266 else if (h[i] >= 'a' && h[i] <= 'f') {
267 uval += h[i] - 'a' + 10;
269 else if (h[i] >= 'A' && h[i] <= 'F') {
270 uval += h[i] - 'A' + 10;
283 else if(uval < 0x800) {
284 t[0] = 0xC0 + ((uval & 0x7C0) >> 6);
285 t[1] = 0x80 + ((uval & 0x03F));
288 else if(uval < 0x10000) {
289 t[0] = 0xE0 + ((uval & 0xF000) >> 12);
290 t[1] = 0x80 + ((uval & 0x0FC0) >> 6);
291 t[2] = 0x80 + ((uval & 0x003F));
294 else if(uval <= 0x10FFFF) {
295 t[0] = 0xF0 + ((uval & 0x1C0000) >> 18);
296 t[1] = 0x80 + ((uval & 0x03F000) >> 12);
297 t[2] = 0x80 + ((uval & 0x000FC0) >> 6);
298 t[3] = 0x80 + ((uval & 0x00003F));
327 ucl_copy_key_trash (const ucl_object_t *obj)
329 ucl_object_t *deconst;
334 if (obj->trash_stack[UCL_TRASH_KEY] == NULL && obj->key != NULL) {
335 deconst = __DECONST (ucl_object_t *, obj);
336 deconst->trash_stack[UCL_TRASH_KEY] = malloc (obj->keylen + 1);
337 if (deconst->trash_stack[UCL_TRASH_KEY] != NULL) {
338 memcpy (deconst->trash_stack[UCL_TRASH_KEY], obj->key, obj->keylen);
339 deconst->trash_stack[UCL_TRASH_KEY][obj->keylen] = '\0';
341 deconst->key = obj->trash_stack[UCL_TRASH_KEY];
342 deconst->flags |= UCL_OBJECT_ALLOCATED_KEY;
345 return obj->trash_stack[UCL_TRASH_KEY];
349 ucl_copy_value_trash (const ucl_object_t *obj)
351 ucl_object_t *deconst;
356 if (obj->trash_stack[UCL_TRASH_VALUE] == NULL) {
357 deconst = __DECONST (ucl_object_t *, obj);
358 if (obj->type == UCL_STRING) {
360 /* Special case for strings */
361 deconst->trash_stack[UCL_TRASH_VALUE] = malloc (obj->len + 1);
362 if (deconst->trash_stack[UCL_TRASH_VALUE] != NULL) {
363 memcpy (deconst->trash_stack[UCL_TRASH_VALUE], obj->value.sv, obj->len);
364 deconst->trash_stack[UCL_TRASH_VALUE][obj->len] = '\0';
365 deconst->value.sv = obj->trash_stack[UCL_TRASH_VALUE];
369 /* Just emit value in json notation */
370 deconst->trash_stack[UCL_TRASH_VALUE] = ucl_object_emit_single_json (obj);
371 deconst->len = strlen (obj->trash_stack[UCL_TRASH_VALUE]);
373 deconst->flags |= UCL_OBJECT_ALLOCATED_VALUE;
375 return obj->trash_stack[UCL_TRASH_VALUE];
378 UCL_EXTERN ucl_object_t*
379 ucl_parser_get_object (struct ucl_parser *parser)
381 if (parser->state != UCL_STATE_ERROR && parser->top_obj != NULL) {
382 return ucl_object_ref (parser->top_obj);
389 ucl_parser_free (struct ucl_parser *parser)
391 struct ucl_stack *stack, *stmp;
392 struct ucl_macro *macro, *mtmp;
393 struct ucl_chunk *chunk, *ctmp;
394 struct ucl_pubkey *key, *ktmp;
395 struct ucl_variable *var, *vtmp;
397 if (parser == NULL) {
401 if (parser->top_obj != NULL) {
402 ucl_object_unref (parser->top_obj);
405 LL_FOREACH_SAFE (parser->stack, stack, stmp) {
408 HASH_ITER (hh, parser->macroes, macro, mtmp) {
410 HASH_DEL (parser->macroes, macro);
411 UCL_FREE (sizeof (struct ucl_macro), macro);
413 LL_FOREACH_SAFE (parser->chunks, chunk, ctmp) {
414 UCL_FREE (sizeof (struct ucl_chunk), chunk);
416 LL_FOREACH_SAFE (parser->keys, key, ktmp) {
417 UCL_FREE (sizeof (struct ucl_pubkey), key);
419 LL_FOREACH_SAFE (parser->variables, var, vtmp) {
422 UCL_FREE (sizeof (struct ucl_variable), var);
425 if (parser->err != NULL) {
426 utstring_free(parser->err);
429 UCL_FREE (sizeof (struct ucl_parser), parser);
432 UCL_EXTERN const char *
433 ucl_parser_get_error(struct ucl_parser *parser)
435 if (parser == NULL) {
439 if (parser->err == NULL)
442 return utstring_body(parser->err);
446 ucl_pubkey_add (struct ucl_parser *parser, const unsigned char *key, size_t len)
449 ucl_create_err (&parser->err, "cannot check signatures without openssl");
452 # if (OPENSSL_VERSION_NUMBER < 0x10000000L)
453 ucl_create_err (&parser->err, "cannot check signatures, openssl version is unsupported");
456 struct ucl_pubkey *nkey;
459 mem = BIO_new_mem_buf ((void *)key, len);
460 nkey = UCL_ALLOC (sizeof (struct ucl_pubkey));
462 ucl_create_err (&parser->err, "cannot allocate memory for key");
465 nkey->key = PEM_read_bio_PUBKEY (mem, &nkey->key, NULL, NULL);
467 if (nkey->key == NULL) {
468 UCL_FREE (sizeof (struct ucl_pubkey), nkey);
469 ucl_create_err (&parser->err, "%s",
470 ERR_error_string (ERR_get_error (), NULL));
473 LL_PREPEND (parser->keys, nkey);
480 struct ucl_curl_cbdata {
486 ucl_curl_write_callback (void* contents, size_t size, size_t nmemb, void* ud)
488 struct ucl_curl_cbdata *cbdata = ud;
489 size_t realsize = size * nmemb;
491 cbdata->buf = realloc (cbdata->buf, cbdata->buflen + realsize + 1);
492 if (cbdata->buf == NULL) {
496 memcpy (&(cbdata->buf[cbdata->buflen]), contents, realsize);
497 cbdata->buflen += realsize;
498 cbdata->buf[cbdata->buflen] = 0;
505 * Fetch a url and save results to the memory buffer
506 * @param url url to fetch
507 * @param len length of url
508 * @param buf target buffer
509 * @param buflen target length
513 ucl_fetch_url (const unsigned char *url, unsigned char **buf, size_t *buflen,
514 UT_string **err, bool must_exist)
518 struct url *fetch_url;
522 fetch_url = fetchParseURL (url);
523 if (fetch_url == NULL) {
524 ucl_create_err (err, "invalid URL %s: %s",
525 url, strerror (errno));
528 if ((in = fetchXGet (fetch_url, &us, "")) == NULL) {
530 ucl_create_err (err, "cannot fetch URL %s: %s",
531 url, strerror (errno));
533 fetchFreeURL (fetch_url);
538 *buf = malloc (*buflen);
540 ucl_create_err (err, "cannot allocate buffer for URL %s: %s",
541 url, strerror (errno));
543 fetchFreeURL (fetch_url);
547 if (fread (*buf, *buflen, 1, in) != 1) {
548 ucl_create_err (err, "cannot read URL %s: %s",
549 url, strerror (errno));
551 fetchFreeURL (fetch_url);
555 fetchFreeURL (fetch_url);
557 #elif defined(CURL_FOUND)
560 struct ucl_curl_cbdata cbdata;
562 curl = curl_easy_init ();
564 ucl_create_err (err, "CURL interface is broken");
567 if ((r = curl_easy_setopt (curl, CURLOPT_URL, url)) != CURLE_OK) {
568 ucl_create_err (err, "invalid URL %s: %s",
569 url, curl_easy_strerror (r));
570 curl_easy_cleanup (curl);
573 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ucl_curl_write_callback);
575 cbdata.buflen = *buflen;
576 curl_easy_setopt (curl, CURLOPT_WRITEDATA, &cbdata);
578 if ((r = curl_easy_perform (curl)) != CURLE_OK) {
580 ucl_create_err (err, "error fetching URL %s: %s",
581 url, curl_easy_strerror (r));
583 curl_easy_cleanup (curl);
590 *buflen = cbdata.buflen;
594 ucl_create_err (err, "URL support is disabled");
600 * Fetch a file and save results to the memory buffer
601 * @param filename filename to fetch
602 * @param len length of filename
603 * @param buf target buffer
604 * @param buflen target length
608 ucl_fetch_file (const unsigned char *filename, unsigned char **buf, size_t *buflen,
609 UT_string **err, bool must_exist)
614 if (stat (filename, &st) == -1 || !S_ISREG (st.st_mode)) {
616 ucl_create_err (err, "cannot stat file %s: %s",
617 filename, strerror (errno));
621 if (st.st_size == 0) {
622 /* Do not map empty files */
627 if ((fd = open (filename, O_RDONLY)) == -1) {
628 ucl_create_err (err, "cannot open file %s: %s",
629 filename, strerror (errno));
632 if ((*buf = ucl_mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) {
634 ucl_create_err (err, "cannot mmap file %s: %s",
635 filename, strerror (errno));
638 *buflen = st.st_size;
646 #if (defined(HAVE_OPENSSL) && OPENSSL_VERSION_NUMBER >= 0x10000000L)
648 ucl_sig_check (const unsigned char *data, size_t datalen,
649 const unsigned char *sig, size_t siglen, struct ucl_parser *parser)
651 struct ucl_pubkey *key;
652 char dig[EVP_MAX_MD_SIZE];
654 EVP_PKEY_CTX *key_ctx;
655 EVP_MD_CTX *sign_ctx = NULL;
657 sign_ctx = EVP_MD_CTX_create ();
659 LL_FOREACH (parser->keys, key) {
660 key_ctx = EVP_PKEY_CTX_new (key->key, NULL);
661 if (key_ctx != NULL) {
662 if (EVP_PKEY_verify_init (key_ctx) <= 0) {
663 EVP_PKEY_CTX_free (key_ctx);
666 if (EVP_PKEY_CTX_set_rsa_padding (key_ctx, RSA_PKCS1_PADDING) <= 0) {
667 EVP_PKEY_CTX_free (key_ctx);
670 if (EVP_PKEY_CTX_set_signature_md (key_ctx, EVP_sha256 ()) <= 0) {
671 EVP_PKEY_CTX_free (key_ctx);
674 EVP_DigestInit (sign_ctx, EVP_sha256 ());
675 EVP_DigestUpdate (sign_ctx, data, datalen);
676 EVP_DigestFinal (sign_ctx, dig, &diglen);
678 if (EVP_PKEY_verify (key_ctx, sig, siglen, dig, diglen) == 1) {
679 EVP_MD_CTX_destroy (sign_ctx);
680 EVP_PKEY_CTX_free (key_ctx);
684 EVP_PKEY_CTX_free (key_ctx);
688 EVP_MD_CTX_destroy (sign_ctx);
695 * Include an url to configuration
703 ucl_include_url (const unsigned char *data, size_t len,
704 struct ucl_parser *parser, bool check_signature, bool must_exist)
708 unsigned char *buf = NULL;
710 struct ucl_chunk *chunk;
711 char urlbuf[PATH_MAX];
714 snprintf (urlbuf, sizeof (urlbuf), "%.*s", (int)len, data);
716 if (!ucl_fetch_url (urlbuf, &buf, &buflen, &parser->err, must_exist)) {
717 return (!must_exist || false);
720 if (check_signature) {
721 #if (defined(HAVE_OPENSSL) && OPENSSL_VERSION_NUMBER >= 0x10000000L)
722 unsigned char *sigbuf = NULL;
724 /* We need to check signature first */
725 snprintf (urlbuf, sizeof (urlbuf), "%.*s.sig", (int)len, data);
726 if (!ucl_fetch_url (urlbuf, &sigbuf, &siglen, &parser->err, true)) {
729 if (!ucl_sig_check (buf, buflen, sigbuf, siglen, parser)) {
730 ucl_create_err (&parser->err, "cannot verify url %s: %s",
732 ERR_error_string (ERR_get_error (), NULL));
734 ucl_munmap (sigbuf, siglen);
739 ucl_munmap (sigbuf, siglen);
744 prev_state = parser->state;
745 parser->state = UCL_STATE_INIT;
747 res = ucl_parser_add_chunk (parser, buf, buflen);
749 /* Remove chunk from the stack */
750 chunk = parser->chunks;
752 parser->chunks = chunk->next;
753 UCL_FREE (sizeof (struct ucl_chunk), chunk);
757 parser->state = prev_state;
764 * Include a file to configuration
772 ucl_include_file (const unsigned char *data, size_t len,
773 struct ucl_parser *parser, bool check_signature, bool must_exist)
776 struct ucl_chunk *chunk;
777 unsigned char *buf = NULL;
779 char filebuf[PATH_MAX], realbuf[PATH_MAX];
782 snprintf (filebuf, sizeof (filebuf), "%.*s", (int)len, data);
783 if (ucl_realpath (filebuf, realbuf) == NULL) {
787 ucl_create_err (&parser->err, "cannot open file %s: %s",
793 if (!ucl_fetch_file (realbuf, &buf, &buflen, &parser->err, must_exist)) {
794 return (!must_exist || false);
797 if (check_signature) {
798 #if (defined(HAVE_OPENSSL) && OPENSSL_VERSION_NUMBER >= 0x10000000L)
799 unsigned char *sigbuf = NULL;
801 /* We need to check signature first */
802 snprintf (filebuf, sizeof (filebuf), "%s.sig", realbuf);
803 if (!ucl_fetch_file (filebuf, &sigbuf, &siglen, &parser->err, true)) {
806 if (!ucl_sig_check (buf, buflen, sigbuf, siglen, parser)) {
807 ucl_create_err (&parser->err, "cannot verify file %s: %s",
809 ERR_error_string (ERR_get_error (), NULL));
811 ucl_munmap (sigbuf, siglen);
816 ucl_munmap (sigbuf, siglen);
821 ucl_parser_set_filevars (parser, realbuf, false);
823 prev_state = parser->state;
824 parser->state = UCL_STATE_INIT;
826 res = ucl_parser_add_chunk (parser, buf, buflen);
828 /* Remove chunk from the stack */
829 chunk = parser->chunks;
831 parser->chunks = chunk->next;
832 UCL_FREE (sizeof (struct ucl_chunk), chunk);
836 parser->state = prev_state;
839 ucl_munmap (buf, buflen);
846 * Handle include macro
847 * @param data include data
848 * @param len length of data
849 * @param ud user data
850 * @param err error ptr
854 ucl_include_handler (const unsigned char *data, size_t len, void* ud)
856 struct ucl_parser *parser = ud;
858 if (*data == '/' || *data == '.') {
859 /* Try to load a file */
860 return ucl_include_file (data, len, parser, false, true);
863 return ucl_include_url (data, len, parser, false, true);
867 * Handle includes macro
868 * @param data include data
869 * @param len length of data
870 * @param ud user data
871 * @param err error ptr
875 ucl_includes_handler (const unsigned char *data, size_t len, void* ud)
877 struct ucl_parser *parser = ud;
879 if (*data == '/' || *data == '.') {
880 /* Try to load a file */
881 return ucl_include_file (data, len, parser, true, true);
884 return ucl_include_url (data, len, parser, true, true);
889 ucl_try_include_handler (const unsigned char *data, size_t len, void* ud)
891 struct ucl_parser *parser = ud;
893 if (*data == '/' || *data == '.') {
894 /* Try to load a file */
895 return ucl_include_file (data, len, parser, false, false);
898 return ucl_include_url (data, len, parser, false, false);
902 ucl_parser_set_filevars (struct ucl_parser *parser, const char *filename, bool need_expand)
904 char realbuf[PATH_MAX], *curdir;
906 if (filename != NULL) {
908 if (ucl_realpath (filename, realbuf) == NULL) {
913 ucl_strlcpy (realbuf, filename, sizeof (realbuf));
916 /* Define variables */
917 ucl_parser_register_variable (parser, "FILENAME", realbuf);
918 curdir = dirname (realbuf);
919 ucl_parser_register_variable (parser, "CURDIR", curdir);
922 /* Set everything from the current dir */
923 curdir = getcwd (realbuf, sizeof (realbuf));
924 ucl_parser_register_variable (parser, "FILENAME", "undef");
925 ucl_parser_register_variable (parser, "CURDIR", curdir);
932 ucl_parser_add_file (struct ucl_parser *parser, const char *filename)
937 char realbuf[PATH_MAX];
939 if (ucl_realpath (filename, realbuf) == NULL) {
940 ucl_create_err (&parser->err, "cannot open file %s: %s",
946 if (!ucl_fetch_file (realbuf, &buf, &len, &parser->err, true)) {
950 ucl_parser_set_filevars (parser, realbuf, false);
951 ret = ucl_parser_add_chunk (parser, buf, len);
954 ucl_munmap (buf, len);
961 ucl_strlcpy (char *dst, const char *src, size_t siz)
967 /* Copy as many bytes as will fit */
970 if ((*d++ = *s++) == '\0') {
976 if (n == 0 && siz != 0) {
980 return (s - src - 1); /* count does not include NUL */
984 ucl_strlcpy_unsafe (char *dst, const char *src, size_t siz)
986 memcpy (dst, src, siz - 1);
993 ucl_strlcpy_tolower (char *dst, const char *src, size_t siz)
999 /* Copy as many bytes as will fit */
1002 if ((*d++ = tolower (*s++)) == '\0') {
1008 if (n == 0 && siz != 0) {
1012 return (s - src); /* count does not include NUL */
1016 ucl_object_fromstring_common (const char *str, size_t len, enum ucl_string_flags flags)
1019 const char *start, *end, *p, *pos;
1027 obj = ucl_object_new ();
1032 if (flags & UCL_STRING_TRIM) {
1033 /* Skip leading spaces */
1034 for (start = str; (size_t)(start - str) < len; start ++) {
1035 if (!ucl_test_character (*start, UCL_CHARACTER_WHITESPACE_UNSAFE)) {
1039 /* Skip trailing spaces */
1040 for (end = str + len - 1; end > start; end --) {
1041 if (!ucl_test_character (*end, UCL_CHARACTER_WHITESPACE_UNSAFE)) {
1052 obj->type = UCL_STRING;
1053 if (flags & UCL_STRING_ESCAPE) {
1054 for (p = start, escaped_len = 0; p < end; p ++, escaped_len ++) {
1055 if (ucl_test_character (*p, UCL_CHARACTER_JSON_UNSAFE)) {
1059 dst = malloc (escaped_len + 1);
1061 for (p = start, d = dst; p < end; p ++, d ++) {
1062 if (ucl_test_character (*p, UCL_CHARACTER_JSON_UNSAFE)) {
1099 obj->value.sv = dst;
1100 obj->trash_stack[UCL_TRASH_VALUE] = dst;
1101 obj->len = escaped_len;
1105 dst = malloc (end - start + 1);
1107 ucl_strlcpy_unsafe (dst, start, end - start + 1);
1108 obj->value.sv = dst;
1109 obj->trash_stack[UCL_TRASH_VALUE] = dst;
1110 obj->len = end - start;
1113 if ((flags & UCL_STRING_PARSE) && dst != NULL) {
1114 /* Parse what we have */
1115 if (flags & UCL_STRING_PARSE_BOOLEAN) {
1116 if (!ucl_maybe_parse_boolean (obj, dst, obj->len) && (flags & UCL_STRING_PARSE_NUMBER)) {
1117 ucl_maybe_parse_number (obj, dst, dst + obj->len, &pos,
1118 flags & UCL_STRING_PARSE_DOUBLE,
1119 flags & UCL_STRING_PARSE_BYTES,
1120 flags & UCL_STRING_PARSE_TIME);
1124 ucl_maybe_parse_number (obj, dst, dst + obj->len, &pos,
1125 flags & UCL_STRING_PARSE_DOUBLE,
1126 flags & UCL_STRING_PARSE_BYTES,
1127 flags & UCL_STRING_PARSE_TIME);
1136 ucl_object_insert_key_common (ucl_object_t *top, ucl_object_t *elt,
1137 const char *key, size_t keylen, bool copy_key, bool merge, bool replace)
1139 ucl_object_t *found, *tmp;
1140 const ucl_object_t *cur;
1141 ucl_object_iter_t it = NULL;
1145 if (elt == NULL || key == NULL) {
1153 if (top->type != UCL_OBJECT) {
1154 /* It is possible to convert NULL type to an object */
1155 if (top->type == UCL_NULL) {
1156 top->type = UCL_OBJECT;
1159 /* Refuse converting of other object types */
1164 if (top->value.ov == NULL) {
1165 top->value.ov = ucl_hash_create ();
1169 keylen = strlen (key);
1172 for (p = key; p < key + keylen; p ++) {
1173 if (ucl_test_character (*p, UCL_CHARACTER_UCL_UNSAFE)) {
1174 elt->flags |= UCL_OBJECT_NEED_KEY_ESCAPE;
1180 elt->keylen = keylen;
1183 ucl_copy_key_trash (elt);
1186 found = __DECONST (ucl_object_t *, ucl_hash_search_obj (top->value.ov, elt));
1189 top->value.ov = ucl_hash_insert_object (top->value.ov, elt);
1190 DL_APPEND (found, elt);
1198 ucl_hash_delete (top->value.ov, found);
1199 ucl_object_unref (found);
1200 top->value.ov = ucl_hash_insert_object (top->value.ov, elt);
1202 DL_APPEND (found, elt);
1205 if (found->type != UCL_OBJECT && elt->type == UCL_OBJECT) {
1206 /* Insert old elt to new one */
1207 ucl_object_insert_key_common (elt, found, found->key,
1208 found->keylen, copy_key, false, false);
1209 ucl_hash_delete (top->value.ov, found);
1210 top->value.ov = ucl_hash_insert_object (top->value.ov, elt);
1212 else if (found->type == UCL_OBJECT && elt->type != UCL_OBJECT) {
1213 /* Insert new to old */
1214 ucl_object_insert_key_common (found, elt, elt->key,
1215 elt->keylen, copy_key, false, false);
1217 else if (found->type == UCL_OBJECT && elt->type == UCL_OBJECT) {
1218 /* Mix two hashes */
1219 while ((cur = ucl_iterate_object (elt, &it, true)) != NULL) {
1220 tmp = ucl_object_ref (cur);
1221 ucl_object_insert_key_common (found, tmp, cur->key,
1222 cur->keylen, copy_key, false, false);
1224 ucl_object_unref (elt);
1227 /* Just make a list of scalars */
1228 DL_APPEND (found, elt);
1232 DL_APPEND (found, elt);
1240 ucl_object_delete_keyl (ucl_object_t *top, const char *key, size_t keylen)
1242 ucl_object_t *found;
1244 if (top == NULL || key == NULL) {
1248 found = __DECONST (ucl_object_t *, ucl_object_find_keyl (top, key, keylen));
1250 if (found == NULL) {
1254 ucl_hash_delete (top->value.ov, found);
1255 ucl_object_unref (found);
1262 ucl_object_delete_key (ucl_object_t *top, const char *key)
1264 return ucl_object_delete_keyl (top, key, strlen(key));
1268 ucl_object_pop_keyl (ucl_object_t *top, const char *key, size_t keylen)
1270 const ucl_object_t *found;
1272 if (top == NULL || key == NULL) {
1275 found = ucl_object_find_keyl (top, key, keylen);
1277 if (found == NULL) {
1280 ucl_hash_delete (top->value.ov, found);
1283 return __DECONST (ucl_object_t *, found);
1287 ucl_object_pop_key (ucl_object_t *top, const char *key)
1289 return ucl_object_pop_keyl (top, key, strlen(key));
1293 ucl_object_insert_key (ucl_object_t *top, ucl_object_t *elt,
1294 const char *key, size_t keylen, bool copy_key)
1296 return ucl_object_insert_key_common (top, elt, key, keylen, copy_key, false, false);
1300 ucl_object_insert_key_merged (ucl_object_t *top, ucl_object_t *elt,
1301 const char *key, size_t keylen, bool copy_key)
1303 return ucl_object_insert_key_common (top, elt, key, keylen, copy_key, true, false);
1307 ucl_object_replace_key (ucl_object_t *top, ucl_object_t *elt,
1308 const char *key, size_t keylen, bool copy_key)
1310 return ucl_object_insert_key_common (top, elt, key, keylen, copy_key, false, true);
1313 const ucl_object_t *
1314 ucl_object_find_keyl (const ucl_object_t *obj, const char *key, size_t klen)
1316 const ucl_object_t *ret;
1319 if (obj == NULL || obj->type != UCL_OBJECT || key == NULL) {
1325 ret = ucl_hash_search_obj (obj->value.ov, &srch);
1330 const ucl_object_t *
1331 ucl_object_find_key (const ucl_object_t *obj, const char *key)
1336 return ucl_object_find_keyl (obj, key, strlen(key));
1340 ucl_iterate_object (const ucl_object_t *obj, ucl_object_iter_t *iter, bool expand_values)
1342 const ucl_object_t *elt;
1344 if (obj == NULL || iter == NULL) {
1348 if (expand_values) {
1349 switch (obj->type) {
1351 return (const ucl_object_t*)ucl_hash_iterate (obj->value.ov, iter);
1356 elt = obj->value.av;
1361 else if (elt == obj->value.av) {
1364 *iter = elt->next ? elt->next : obj->value.av;
1367 /* Go to linear iteration */
1371 /* Treat everything as a linear list */
1379 else if (elt == obj) {
1382 *iter = __DECONST (void *, elt->next ? elt->next : obj);
1389 const ucl_object_t *
1390 ucl_lookup_path (const ucl_object_t *top, const char *path_in) {
1391 const ucl_object_t *o = NULL, *found;
1396 if (path_in == NULL || top == NULL) {
1403 /* Skip leading dots */
1409 while (*p != '\0') {
1411 if (*p == '.' || *p == '\0') {
1413 switch (top->type) {
1415 /* Key should be an int */
1416 index = strtoul (c, &err_str, 10);
1417 if (err_str != NULL && (*err_str != '.' && *err_str != '\0')) {
1420 o = ucl_array_find_index (top, index);
1423 o = ucl_object_find_keyl (top, c, p - c);
1443 ucl_object_new (void)
1446 new = malloc (sizeof (ucl_object_t));
1448 memset (new, 0, sizeof (ucl_object_t));
1450 new->type = UCL_NULL;
1456 ucl_object_typed_new (ucl_type_t type)
1459 new = malloc (sizeof (ucl_object_t));
1461 memset (new, 0, sizeof (ucl_object_t));
1463 new->type = (type <= UCL_NULL ? type : UCL_NULL);
1469 ucl_object_type (const ucl_object_t *obj)
1475 ucl_object_fromstring (const char *str)
1477 return ucl_object_fromstring_common (str, 0, UCL_STRING_ESCAPE);
1481 ucl_object_fromlstring (const char *str, size_t len)
1483 return ucl_object_fromstring_common (str, len, UCL_STRING_ESCAPE);
1487 ucl_object_fromint (int64_t iv)
1491 obj = ucl_object_new ();
1493 obj->type = UCL_INT;
1501 ucl_object_fromdouble (double dv)
1505 obj = ucl_object_new ();
1507 obj->type = UCL_FLOAT;
1515 ucl_object_frombool (bool bv)
1519 obj = ucl_object_new ();
1521 obj->type = UCL_BOOLEAN;
1529 ucl_array_append (ucl_object_t *top, ucl_object_t *elt)
1533 if (elt == NULL || top == NULL) {
1537 head = top->value.av;
1539 top->value.av = elt;
1543 elt->prev = head->prev;
1544 head->prev->next = elt;
1554 ucl_array_prepend (ucl_object_t *top, ucl_object_t *elt)
1558 if (elt == NULL || top == NULL) {
1563 head = top->value.av;
1565 top->value.av = elt;
1569 elt->prev = head->prev;
1573 top->value.av = elt;
1580 ucl_array_delete (ucl_object_t *top, ucl_object_t *elt)
1584 if (top == NULL || top->type != UCL_ARRAY || top->value.av == NULL) {
1587 head = top->value.av;
1589 if (elt->prev == elt) {
1590 top->value.av = NULL;
1592 else if (elt == head) {
1593 elt->next->prev = elt->prev;
1594 top->value.av = elt->next;
1597 elt->prev->next = elt->next;
1599 elt->next->prev = elt->prev;
1602 head->prev = elt->prev;
1612 const ucl_object_t *
1613 ucl_array_head (const ucl_object_t *top)
1615 if (top == NULL || top->type != UCL_ARRAY || top->value.av == NULL) {
1618 return top->value.av;
1621 const ucl_object_t *
1622 ucl_array_tail (const ucl_object_t *top)
1624 if (top == NULL || top->type != UCL_ARRAY || top->value.av == NULL) {
1627 return top->value.av->prev;
1631 ucl_array_pop_last (ucl_object_t *top)
1633 return ucl_array_delete (top, __DECONST(ucl_object_t *, ucl_array_tail (top)));
1637 ucl_array_pop_first (ucl_object_t *top)
1639 return ucl_array_delete (top, __DECONST(ucl_object_t *, ucl_array_head (top)));
1642 const ucl_object_t *
1643 ucl_array_find_index (const ucl_object_t *top, unsigned int index)
1645 ucl_object_iter_t it = NULL;
1646 const ucl_object_t *ret;
1648 if (top == NULL || top->type != UCL_ARRAY || top->len == 0 ||
1649 (index + 1) > top->len) {
1653 while ((ret = ucl_iterate_object (top, &it, true)) != NULL) {
1664 ucl_elt_append (ucl_object_t *head, ucl_object_t *elt)
1673 elt->prev = head->prev;
1674 head->prev->next = elt;
1683 ucl_object_todouble_safe (const ucl_object_t *obj, double *target)
1685 if (obj == NULL || target == NULL) {
1688 switch (obj->type) {
1690 *target = obj->value.iv; /* Probaly could cause overflow */
1694 *target = obj->value.dv;
1704 ucl_object_todouble (const ucl_object_t *obj)
1708 ucl_object_todouble_safe (obj, &result);
1713 ucl_object_toint_safe (const ucl_object_t *obj, int64_t *target)
1715 if (obj == NULL || target == NULL) {
1718 switch (obj->type) {
1720 *target = obj->value.iv;
1724 *target = obj->value.dv; /* Loosing of decimal points */
1734 ucl_object_toint (const ucl_object_t *obj)
1738 ucl_object_toint_safe (obj, &result);
1743 ucl_object_toboolean_safe (const ucl_object_t *obj, bool *target)
1745 if (obj == NULL || target == NULL) {
1748 switch (obj->type) {
1750 *target = (obj->value.iv == true);
1760 ucl_object_toboolean (const ucl_object_t *obj)
1762 bool result = false;
1764 ucl_object_toboolean_safe (obj, &result);
1769 ucl_object_tostring_safe (const ucl_object_t *obj, const char **target)
1771 if (obj == NULL || target == NULL) {
1775 switch (obj->type) {
1777 *target = ucl_copy_value_trash (obj);
1787 ucl_object_tostring (const ucl_object_t *obj)
1789 const char *result = NULL;
1791 ucl_object_tostring_safe (obj, &result);
1796 ucl_object_tostring_forced (const ucl_object_t *obj)
1798 return ucl_copy_value_trash (obj);
1802 ucl_object_tolstring_safe (const ucl_object_t *obj, const char **target, size_t *tlen)
1804 if (obj == NULL || target == NULL) {
1807 switch (obj->type) {
1809 *target = obj->value.sv;
1822 ucl_object_tolstring (const ucl_object_t *obj, size_t *tlen)
1824 const char *result = NULL;
1826 ucl_object_tolstring_safe (obj, &result, tlen);
1831 ucl_object_key (const ucl_object_t *obj)
1833 return ucl_copy_key_trash (obj);
1837 ucl_object_keyl (const ucl_object_t *obj, size_t *len)
1839 if (len == NULL || obj == NULL) {
1847 ucl_object_ref (const ucl_object_t *obj)
1849 ucl_object_t *res = NULL;
1852 res = __DECONST (ucl_object_t *, obj);
1853 #ifdef HAVE_ATOMIC_BUILTINS
1854 (void)__sync_add_and_fetch (&res->ref, 1);
1863 ucl_object_unref (ucl_object_t *obj)
1866 #ifdef HAVE_ATOMIC_BUILTINS
1867 unsigned int rc = __sync_sub_and_fetch (&obj->ref, 1);
1870 if (--obj->ref == 0) {
1872 ucl_object_free_internal (obj, true, ucl_object_dtor_unref);
1878 ucl_object_compare (const ucl_object_t *o1, const ucl_object_t *o2)
1880 const ucl_object_t *it1, *it2;
1881 ucl_object_iter_t iter = NULL;
1884 if (o1->type != o2->type) {
1885 return (o1->type) - (o2->type);
1890 if (o1->len == o2->len) {
1891 ret = strcmp (ucl_object_tostring(o1), ucl_object_tostring(o2));
1894 ret = o1->len - o2->len;
1900 ret = ucl_object_todouble (o1) - ucl_object_todouble (o2);
1903 ret = ucl_object_toboolean (o1) - ucl_object_toboolean (o2);
1906 if (o1->len == o2->len) {
1909 /* Compare all elements in both arrays */
1910 while (it1 != NULL && it2 != NULL) {
1911 ret = ucl_object_compare (it1, it2);
1920 ret = o1->len - o2->len;
1924 if (o1->len == o2->len) {
1925 while ((it1 = ucl_iterate_object (o1, &iter, true)) != NULL) {
1926 it2 = ucl_object_find_key (o2, ucl_object_key (it1));
1931 ret = ucl_object_compare (it1, it2);
1938 ret = o1->len - o2->len;
1950 ucl_object_array_sort (ucl_object_t *ar,
1951 int (*cmp)(const ucl_object_t *o1, const ucl_object_t *o2))
1953 if (cmp == NULL || ar == NULL || ar->type != UCL_ARRAY) {
1957 DL_SORT (ar->value.av, cmp);