xref: /freebsd/contrib/libucl/src/ucl_util.c (revision b5864e6de2f3aa8eb9bb269ec86282598b5201b1)
1 /* Copyright (c) 2013, Vsevolod Stakhov
2  * Copyright (c) 2015 Allan Jude <allanjude@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *       * Redistributions of source code must retain the above copyright
8  *         notice, this list of conditions and the following disclaimer.
9  *       * Redistributions in binary form must reproduce the above copyright
10  *         notice, this list of conditions and the following disclaimer in the
11  *         documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16  * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24 
25 #include "ucl.h"
26 #include "ucl_internal.h"
27 #include "ucl_chartable.h"
28 #include "kvec.h"
29 #include <stdarg.h>
30 #include <stdio.h> /* for snprintf */
31 
32 #ifndef _WIN32
33 #include <glob.h>
34 #endif
35 
36 #ifdef HAVE_LIBGEN_H
37 #include <libgen.h> /* For dirname */
38 #endif
39 
40 typedef kvec_t(ucl_object_t *) ucl_array_t;
41 
42 #define UCL_ARRAY_GET(ar, obj) ucl_array_t *ar = \
43 	(ucl_array_t *)((obj) != NULL ? (obj)->value.av : NULL)
44 
45 #ifdef HAVE_OPENSSL
46 #include <openssl/err.h>
47 #include <openssl/sha.h>
48 #include <openssl/rsa.h>
49 #include <openssl/ssl.h>
50 #include <openssl/evp.h>
51 #endif
52 
53 #ifdef CURL_FOUND
54 /* Seems to be broken */
55 #define CURL_DISABLE_TYPECHECK 1
56 #include <curl/curl.h>
57 #endif
58 #ifdef HAVE_FETCH_H
59 #include <fetch.h>
60 #endif
61 
62 #ifdef _WIN32
63 #include <windows.h>
64 
65 #ifndef PROT_READ
66 #define PROT_READ       1
67 #endif
68 #ifndef PROT_WRITE
69 #define PROT_WRITE      2
70 #endif
71 #ifndef PROT_READWRITE
72 #define PROT_READWRITE  3
73 #endif
74 #ifndef MAP_SHARED
75 #define MAP_SHARED      1
76 #endif
77 #ifndef MAP_PRIVATE
78 #define MAP_PRIVATE     2
79 #endif
80 #ifndef MAP_FAILED
81 #define MAP_FAILED      ((void *) -1)
82 #endif
83 
84 #ifdef _WIN32
85 #include <limits.h>
86 #define NBBY CHAR_BIT
87 #endif
88 
89 static void *ucl_mmap(char *addr, size_t length, int prot, int access, int fd, off_t offset)
90 {
91 	void *map = NULL;
92 	HANDLE handle = INVALID_HANDLE_VALUE;
93 
94 	switch (prot) {
95 	default:
96 	case PROT_READ:
97 		{
98 			handle = CreateFileMapping((HANDLE) _get_osfhandle(fd), 0, PAGE_READONLY, 0, length, 0);
99 			if (!handle) break;
100 			map = (void *) MapViewOfFile(handle, FILE_MAP_READ, 0, 0, length);
101 			CloseHandle(handle);
102 			break;
103 		}
104 	case PROT_WRITE:
105 		{
106 			handle = CreateFileMapping((HANDLE) _get_osfhandle(fd), 0, PAGE_READWRITE, 0, length, 0);
107 			if (!handle) break;
108 			map = (void *) MapViewOfFile(handle, FILE_MAP_WRITE, 0, 0, length);
109 			CloseHandle(handle);
110 			break;
111 		}
112 	case PROT_READWRITE:
113 		{
114 			handle = CreateFileMapping((HANDLE) _get_osfhandle(fd), 0, PAGE_READWRITE, 0, length, 0);
115 			if (!handle) break;
116 			map = (void *) MapViewOfFile(handle, FILE_MAP_ALL_ACCESS, 0, 0, length);
117 			CloseHandle(handle);
118 			break;
119 		}
120 	}
121 	if (map == (void *) NULL) {
122 		return (void *) MAP_FAILED;
123 	}
124 	return (void *) ((char *) map + offset);
125 }
126 
127 static int ucl_munmap(void *map,size_t length)
128 {
129 	if (!UnmapViewOfFile(map)) {
130 		return(-1);
131 	}
132 	return(0);
133 }
134 
135 static char* ucl_realpath(const char *path, char *resolved_path) {
136     char *p;
137     char tmp[MAX_PATH + 1];
138     strncpy(tmp, path, sizeof(tmp)-1);
139     p = tmp;
140     while(*p) {
141         if (*p == '/') *p = '\\';
142         p++;
143     }
144     return _fullpath(resolved_path, tmp, MAX_PATH);
145 }
146 #else
147 #define ucl_mmap mmap
148 #define ucl_munmap munmap
149 #define ucl_realpath realpath
150 #endif
151 
152 typedef void (*ucl_object_dtor) (ucl_object_t *obj);
153 static void ucl_object_free_internal (ucl_object_t *obj, bool allow_rec,
154 		ucl_object_dtor dtor);
155 static void ucl_object_dtor_unref (ucl_object_t *obj);
156 
157 static void
158 ucl_object_dtor_free (ucl_object_t *obj)
159 {
160 	if (obj->trash_stack[UCL_TRASH_KEY] != NULL) {
161 		UCL_FREE (obj->hh.keylen, obj->trash_stack[UCL_TRASH_KEY]);
162 	}
163 	if (obj->trash_stack[UCL_TRASH_VALUE] != NULL) {
164 		UCL_FREE (obj->len, obj->trash_stack[UCL_TRASH_VALUE]);
165 	}
166 	/* Do not free ephemeral objects */
167 	if ((obj->flags & UCL_OBJECT_EPHEMERAL) == 0) {
168 		if (obj->type != UCL_USERDATA) {
169 			UCL_FREE (sizeof (ucl_object_t), obj);
170 		}
171 		else {
172 			struct ucl_object_userdata *ud = (struct ucl_object_userdata *)obj;
173 			if (ud->dtor) {
174 				ud->dtor (obj->value.ud);
175 			}
176 			UCL_FREE (sizeof (*ud), obj);
177 		}
178 	}
179 }
180 
181 /*
182  * This is a helper function that performs exactly the same as
183  * `ucl_object_unref` but it doesn't iterate over elements allowing
184  * to use it for individual elements of arrays and multiple values
185  */
186 static void
187 ucl_object_dtor_unref_single (ucl_object_t *obj)
188 {
189 	if (obj != NULL) {
190 #ifdef HAVE_ATOMIC_BUILTINS
191 		unsigned int rc = __sync_sub_and_fetch (&obj->ref, 1);
192 		if (rc == 0) {
193 #else
194 		if (--obj->ref == 0) {
195 #endif
196 			ucl_object_free_internal (obj, false, ucl_object_dtor_unref);
197 		}
198 	}
199 }
200 
201 static void
202 ucl_object_dtor_unref (ucl_object_t *obj)
203 {
204 	if (obj->ref == 0) {
205 		ucl_object_dtor_free (obj);
206 	}
207 	else {
208 		/* This may cause dtor unref being called one more time */
209 		ucl_object_dtor_unref_single (obj);
210 	}
211 }
212 
213 static void
214 ucl_object_free_internal (ucl_object_t *obj, bool allow_rec, ucl_object_dtor dtor)
215 {
216 	ucl_object_t *tmp, *sub;
217 
218 	while (obj != NULL) {
219 		if (obj->type == UCL_ARRAY) {
220 			UCL_ARRAY_GET (vec, obj);
221 			unsigned int i;
222 
223 			if (vec != NULL) {
224 				for (i = 0; i < vec->n; i ++) {
225 					sub = kv_A (*vec, i);
226 					if (sub != NULL) {
227 						tmp = sub;
228 						while (sub) {
229 							tmp = sub->next;
230 							dtor (sub);
231 							sub = tmp;
232 						}
233 					}
234 				}
235 				kv_destroy (*vec);
236 				UCL_FREE (sizeof (*vec), vec);
237 			}
238 			obj->value.av = NULL;
239 		}
240 		else if (obj->type == UCL_OBJECT) {
241 			if (obj->value.ov != NULL) {
242 				ucl_hash_destroy (obj->value.ov, (ucl_hash_free_func)dtor);
243 			}
244 			obj->value.ov = NULL;
245 		}
246 		tmp = obj->next;
247 		dtor (obj);
248 		obj = tmp;
249 
250 		if (!allow_rec) {
251 			break;
252 		}
253 	}
254 }
255 
256 void
257 ucl_object_free (ucl_object_t *obj)
258 {
259 	ucl_object_free_internal (obj, true, ucl_object_dtor_free);
260 }
261 
262 size_t
263 ucl_unescape_json_string (char *str, size_t len)
264 {
265 	char *t = str, *h = str;
266 	int i, uval;
267 
268 	if (len <= 1) {
269 		return len;
270 	}
271 	/* t is target (tortoise), h is source (hare) */
272 
273 	while (len) {
274 		if (*h == '\\') {
275 			h ++;
276 
277 			if (len == 1) {
278 				/*
279 				 * If \ is last, then do not try to go further
280 				 * Issue: #74
281 				 */
282 				len --;
283 				*t++ = '\\';
284 				continue;
285 			}
286 
287 			switch (*h) {
288 			case 'n':
289 				*t++ = '\n';
290 				break;
291 			case 'r':
292 				*t++ = '\r';
293 				break;
294 			case 'b':
295 				*t++ = '\b';
296 				break;
297 			case 't':
298 				*t++ = '\t';
299 				break;
300 			case 'f':
301 				*t++ = '\f';
302 				break;
303 			case '\\':
304 				*t++ = '\\';
305 				break;
306 			case '"':
307 				*t++ = '"';
308 				break;
309 			case 'u':
310 				/* Unicode escape */
311 				uval = 0;
312 				h ++; /* u character */
313 				len --;
314 
315 				if (len > 3) {
316 					for (i = 0; i < 4; i++) {
317 						uval <<= 4;
318 						if (isdigit (h[i])) {
319 							uval += h[i] - '0';
320 						}
321 						else if (h[i] >= 'a' && h[i] <= 'f') {
322 							uval += h[i] - 'a' + 10;
323 						}
324 						else if (h[i] >= 'A' && h[i] <= 'F') {
325 							uval += h[i] - 'A' + 10;
326 						}
327 						else {
328 							break;
329 						}
330 					}
331 
332 					/* Encode */
333 					if(uval < 0x80) {
334 						t[0] = (char)uval;
335 						t ++;
336 					}
337 					else if(uval < 0x800) {
338 						t[0] = 0xC0 + ((uval & 0x7C0) >> 6);
339 						t[1] = 0x80 + ((uval & 0x03F));
340 						t += 2;
341 					}
342 					else if(uval < 0x10000) {
343 						t[0] = 0xE0 + ((uval & 0xF000) >> 12);
344 						t[1] = 0x80 + ((uval & 0x0FC0) >> 6);
345 						t[2] = 0x80 + ((uval & 0x003F));
346 						t += 3;
347 					}
348 #if 0
349 					/* It's not actually supported now */
350 					else if(uval <= 0x10FFFF) {
351 						t[0] = 0xF0 + ((uval & 0x1C0000) >> 18);
352 						t[1] = 0x80 + ((uval & 0x03F000) >> 12);
353 						t[2] = 0x80 + ((uval & 0x000FC0) >> 6);
354 						t[3] = 0x80 + ((uval & 0x00003F));
355 						t += 4;
356 					}
357 #endif
358 					else {
359 						*t++ = '?';
360 					}
361 
362 					/* Consume 4 characters of source */
363 					h += 4;
364 					len -= 4;
365 
366 					if (len > 0) {
367 						len --; /* for '\' character */
368 					}
369 					continue;
370 				}
371 				else {
372 					*t++ = 'u';
373 				}
374 				break;
375 			default:
376 				*t++ = *h;
377 				break;
378 			}
379 			h ++;
380 			len --;
381 		}
382 		else {
383 			*t++ = *h++;
384 		}
385 
386 		if (len > 0) {
387 			len --;
388 		}
389 	}
390 	*t = '\0';
391 
392 	return (t - str);
393 }
394 
395 char *
396 ucl_copy_key_trash (const ucl_object_t *obj)
397 {
398 	ucl_object_t *deconst;
399 
400 	if (obj == NULL) {
401 		return NULL;
402 	}
403 	if (obj->trash_stack[UCL_TRASH_KEY] == NULL && obj->key != NULL) {
404 		deconst = __DECONST (ucl_object_t *, obj);
405 		deconst->trash_stack[UCL_TRASH_KEY] = malloc (obj->keylen + 1);
406 		if (deconst->trash_stack[UCL_TRASH_KEY] != NULL) {
407 			memcpy (deconst->trash_stack[UCL_TRASH_KEY], obj->key, obj->keylen);
408 			deconst->trash_stack[UCL_TRASH_KEY][obj->keylen] = '\0';
409 		}
410 		deconst->key = obj->trash_stack[UCL_TRASH_KEY];
411 		deconst->flags |= UCL_OBJECT_ALLOCATED_KEY;
412 	}
413 
414 	return obj->trash_stack[UCL_TRASH_KEY];
415 }
416 
417 char *
418 ucl_copy_value_trash (const ucl_object_t *obj)
419 {
420 	ucl_object_t *deconst;
421 
422 	if (obj == NULL) {
423 		return NULL;
424 	}
425 	if (obj->trash_stack[UCL_TRASH_VALUE] == NULL) {
426 		deconst = __DECONST (ucl_object_t *, obj);
427 		if (obj->type == UCL_STRING) {
428 
429 			/* Special case for strings */
430 			if (obj->flags & UCL_OBJECT_BINARY) {
431 				deconst->trash_stack[UCL_TRASH_VALUE] = malloc (obj->len);
432 				if (deconst->trash_stack[UCL_TRASH_VALUE] != NULL) {
433 					memcpy (deconst->trash_stack[UCL_TRASH_VALUE],
434 							obj->value.sv,
435 							obj->len);
436 					deconst->value.sv = obj->trash_stack[UCL_TRASH_VALUE];
437 				}
438 			}
439 			else {
440 				deconst->trash_stack[UCL_TRASH_VALUE] = malloc (obj->len + 1);
441 				if (deconst->trash_stack[UCL_TRASH_VALUE] != NULL) {
442 					memcpy (deconst->trash_stack[UCL_TRASH_VALUE],
443 							obj->value.sv,
444 							obj->len);
445 					deconst->trash_stack[UCL_TRASH_VALUE][obj->len] = '\0';
446 					deconst->value.sv = obj->trash_stack[UCL_TRASH_VALUE];
447 				}
448 			}
449 		}
450 		else {
451 			/* Just emit value in json notation */
452 			deconst->trash_stack[UCL_TRASH_VALUE] = ucl_object_emit_single_json (obj);
453 			deconst->len = strlen (obj->trash_stack[UCL_TRASH_VALUE]);
454 		}
455 		deconst->flags |= UCL_OBJECT_ALLOCATED_VALUE;
456 	}
457 
458 	return obj->trash_stack[UCL_TRASH_VALUE];
459 }
460 
461 ucl_object_t*
462 ucl_parser_get_object (struct ucl_parser *parser)
463 {
464 	if (parser->state != UCL_STATE_ERROR && parser->top_obj != NULL) {
465 		return ucl_object_ref (parser->top_obj);
466 	}
467 
468 	return NULL;
469 }
470 
471 void
472 ucl_parser_free (struct ucl_parser *parser)
473 {
474 	struct ucl_stack *stack, *stmp;
475 	struct ucl_macro *macro, *mtmp;
476 	struct ucl_chunk *chunk, *ctmp;
477 	struct ucl_pubkey *key, *ktmp;
478 	struct ucl_variable *var, *vtmp;
479 	ucl_object_t *tr, *trtmp;
480 
481 	if (parser == NULL) {
482 		return;
483 	}
484 
485 	if (parser->top_obj != NULL) {
486 		ucl_object_unref (parser->top_obj);
487 	}
488 
489 	if (parser->includepaths != NULL) {
490 		ucl_object_unref (parser->includepaths);
491 	}
492 
493 	LL_FOREACH_SAFE (parser->stack, stack, stmp) {
494 		free (stack);
495 	}
496 	HASH_ITER (hh, parser->macroes, macro, mtmp) {
497 		free (macro->name);
498 		HASH_DEL (parser->macroes, macro);
499 		UCL_FREE (sizeof (struct ucl_macro), macro);
500 	}
501 	LL_FOREACH_SAFE (parser->chunks, chunk, ctmp) {
502 		UCL_FREE (sizeof (struct ucl_chunk), chunk);
503 	}
504 	LL_FOREACH_SAFE (parser->keys, key, ktmp) {
505 		UCL_FREE (sizeof (struct ucl_pubkey), key);
506 	}
507 	LL_FOREACH_SAFE (parser->variables, var, vtmp) {
508 		free (var->value);
509 		free (var->var);
510 		UCL_FREE (sizeof (struct ucl_variable), var);
511 	}
512 	LL_FOREACH_SAFE (parser->trash_objs, tr, trtmp) {
513 		ucl_object_free_internal (tr, false, ucl_object_dtor_free);
514 	}
515 
516 	if (parser->err != NULL) {
517 		utstring_free (parser->err);
518 	}
519 
520 	if (parser->cur_file) {
521 		free (parser->cur_file);
522 	}
523 
524 	if (parser->comments) {
525 		ucl_object_unref (parser->comments);
526 	}
527 
528 	UCL_FREE (sizeof (struct ucl_parser), parser);
529 }
530 
531 const char *
532 ucl_parser_get_error(struct ucl_parser *parser)
533 {
534 	if (parser == NULL) {
535 		return NULL;
536 	}
537 
538 	if (parser->err == NULL) {
539 		return NULL;
540 	}
541 
542 	return utstring_body (parser->err);
543 }
544 
545 int
546 ucl_parser_get_error_code(struct ucl_parser *parser)
547 {
548 	if (parser == NULL) {
549 		return 0;
550 	}
551 
552 	return parser->err_code;
553 }
554 
555 unsigned
556 ucl_parser_get_column(struct ucl_parser *parser)
557 {
558 	if (parser == NULL || parser->chunks == NULL) {
559 		return 0;
560 	}
561 
562 	return parser->chunks->column;
563 }
564 
565 unsigned
566 ucl_parser_get_linenum(struct ucl_parser *parser)
567 {
568 	if (parser == NULL || parser->chunks == NULL) {
569 		return 0;
570 	}
571 
572 	return parser->chunks->line;
573 }
574 
575 void
576 ucl_parser_clear_error(struct ucl_parser *parser)
577 {
578 	if (parser != NULL && parser->err != NULL) {
579 		utstring_free(parser->err);
580 		parser->err = NULL;
581 		parser->err_code = 0;
582 	}
583 }
584 
585 bool
586 ucl_pubkey_add (struct ucl_parser *parser, const unsigned char *key, size_t len)
587 {
588 #ifndef HAVE_OPENSSL
589 	ucl_create_err (&parser->err, "cannot check signatures without openssl");
590 	return false;
591 #else
592 # if (OPENSSL_VERSION_NUMBER < 0x10000000L)
593 	ucl_create_err (&parser->err, "cannot check signatures, openssl version is unsupported");
594 	return EXIT_FAILURE;
595 # else
596 	struct ucl_pubkey *nkey;
597 	BIO *mem;
598 
599 	mem = BIO_new_mem_buf ((void *)key, len);
600 	nkey = UCL_ALLOC (sizeof (struct ucl_pubkey));
601 	if (nkey == NULL) {
602 		ucl_create_err (&parser->err, "cannot allocate memory for key");
603 		return false;
604 	}
605 	nkey->key = PEM_read_bio_PUBKEY (mem, &nkey->key, NULL, NULL);
606 	BIO_free (mem);
607 	if (nkey->key == NULL) {
608 		UCL_FREE (sizeof (struct ucl_pubkey), nkey);
609 		ucl_create_err (&parser->err, "%s",
610 				ERR_error_string (ERR_get_error (), NULL));
611 		return false;
612 	}
613 	LL_PREPEND (parser->keys, nkey);
614 # endif
615 #endif
616 	return true;
617 }
618 
619 #ifdef CURL_FOUND
620 struct ucl_curl_cbdata {
621 	unsigned char *buf;
622 	size_t buflen;
623 };
624 
625 static size_t
626 ucl_curl_write_callback (void* contents, size_t size, size_t nmemb, void* ud)
627 {
628 	struct ucl_curl_cbdata *cbdata = ud;
629 	size_t realsize = size * nmemb;
630 
631 	cbdata->buf = realloc (cbdata->buf, cbdata->buflen + realsize + 1);
632 	if (cbdata->buf == NULL) {
633 		return 0;
634 	}
635 
636 	memcpy (&(cbdata->buf[cbdata->buflen]), contents, realsize);
637 	cbdata->buflen += realsize;
638 	cbdata->buf[cbdata->buflen] = 0;
639 
640 	return realsize;
641 }
642 #endif
643 
644 /**
645  * Fetch a url and save results to the memory buffer
646  * @param url url to fetch
647  * @param len length of url
648  * @param buf target buffer
649  * @param buflen target length
650  * @return
651  */
652 bool
653 ucl_fetch_url (const unsigned char *url, unsigned char **buf, size_t *buflen,
654 		UT_string **err, bool must_exist)
655 {
656 
657 #ifdef HAVE_FETCH_H
658 	struct url *fetch_url;
659 	struct url_stat us;
660 	FILE *in;
661 
662 	fetch_url = fetchParseURL (url);
663 	if (fetch_url == NULL) {
664 		ucl_create_err (err, "invalid URL %s: %s",
665 				url, strerror (errno));
666 		return false;
667 	}
668 	if ((in = fetchXGet (fetch_url, &us, "")) == NULL) {
669 		if (!must_exist) {
670 			ucl_create_err (err, "cannot fetch URL %s: %s",
671 				url, strerror (errno));
672 		}
673 		fetchFreeURL (fetch_url);
674 		return false;
675 	}
676 
677 	*buflen = us.size;
678 	*buf = malloc (*buflen);
679 	if (*buf == NULL) {
680 		ucl_create_err (err, "cannot allocate buffer for URL %s: %s",
681 				url, strerror (errno));
682 		fclose (in);
683 		fetchFreeURL (fetch_url);
684 		return false;
685 	}
686 
687 	if (fread (*buf, *buflen, 1, in) != 1) {
688 		ucl_create_err (err, "cannot read URL %s: %s",
689 				url, strerror (errno));
690 		fclose (in);
691 		fetchFreeURL (fetch_url);
692 		return false;
693 	}
694 
695 	fetchFreeURL (fetch_url);
696 	return true;
697 #elif defined(CURL_FOUND)
698 	CURL *curl;
699 	int r;
700 	struct ucl_curl_cbdata cbdata;
701 
702 	curl = curl_easy_init ();
703 	if (curl == NULL) {
704 		ucl_create_err (err, "CURL interface is broken");
705 		return false;
706 	}
707 	if ((r = curl_easy_setopt (curl, CURLOPT_URL, url)) != CURLE_OK) {
708 		ucl_create_err (err, "invalid URL %s: %s",
709 				url, curl_easy_strerror (r));
710 		curl_easy_cleanup (curl);
711 		return false;
712 	}
713 	curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ucl_curl_write_callback);
714 	cbdata.buf = NULL;
715 	cbdata.buflen = 0;
716 	curl_easy_setopt (curl, CURLOPT_WRITEDATA, &cbdata);
717 
718 	if ((r = curl_easy_perform (curl)) != CURLE_OK) {
719 		if (!must_exist) {
720 			ucl_create_err (err, "error fetching URL %s: %s",
721 				url, curl_easy_strerror (r));
722 		}
723 		curl_easy_cleanup (curl);
724 		if (cbdata.buf) {
725 			free (cbdata.buf);
726 		}
727 		return false;
728 	}
729 	*buf = cbdata.buf;
730 	*buflen = cbdata.buflen;
731 
732 	return true;
733 #else
734 	ucl_create_err (err, "URL support is disabled");
735 	return false;
736 #endif
737 }
738 
739 /**
740  * Fetch a file and save results to the memory buffer
741  * @param filename filename to fetch
742  * @param len length of filename
743  * @param buf target buffer
744  * @param buflen target length
745  * @return
746  */
747 bool
748 ucl_fetch_file (const unsigned char *filename, unsigned char **buf, size_t *buflen,
749 		UT_string **err, bool must_exist)
750 {
751 	int fd;
752 	struct stat st;
753 
754 	if (stat (filename, &st) == -1 || !S_ISREG (st.st_mode)) {
755 		if (must_exist) {
756 			ucl_create_err (err, "cannot stat file %s: %s",
757 					filename, strerror (errno));
758 		}
759 		return false;
760 	}
761 	if (st.st_size == 0) {
762 		/* Do not map empty files */
763 		*buf = NULL;
764 		*buflen = 0;
765 	}
766 	else {
767 		if ((fd = open (filename, O_RDONLY)) == -1) {
768 			ucl_create_err (err, "cannot open file %s: %s",
769 					filename, strerror (errno));
770 			return false;
771 		}
772 		if ((*buf = ucl_mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) {
773 			close (fd);
774 			ucl_create_err (err, "cannot mmap file %s: %s",
775 					filename, strerror (errno));
776 			*buf = NULL;
777 
778 			return false;
779 		}
780 		*buflen = st.st_size;
781 		close (fd);
782 	}
783 
784 	return true;
785 }
786 
787 
788 #if (defined(HAVE_OPENSSL) && OPENSSL_VERSION_NUMBER >= 0x10000000L)
789 static inline bool
790 ucl_sig_check (const unsigned char *data, size_t datalen,
791 		const unsigned char *sig, size_t siglen, struct ucl_parser *parser)
792 {
793 	struct ucl_pubkey *key;
794 	char dig[EVP_MAX_MD_SIZE];
795 	unsigned int diglen;
796 	EVP_PKEY_CTX *key_ctx;
797 	EVP_MD_CTX *sign_ctx = NULL;
798 
799 	sign_ctx = EVP_MD_CTX_create ();
800 
801 	LL_FOREACH (parser->keys, key) {
802 		key_ctx = EVP_PKEY_CTX_new (key->key, NULL);
803 		if (key_ctx != NULL) {
804 			if (EVP_PKEY_verify_init (key_ctx) <= 0) {
805 				EVP_PKEY_CTX_free (key_ctx);
806 				continue;
807 			}
808 			if (EVP_PKEY_CTX_set_rsa_padding (key_ctx, RSA_PKCS1_PADDING) <= 0) {
809 				EVP_PKEY_CTX_free (key_ctx);
810 				continue;
811 			}
812 			if (EVP_PKEY_CTX_set_signature_md (key_ctx, EVP_sha256 ()) <= 0) {
813 				EVP_PKEY_CTX_free (key_ctx);
814 				continue;
815 			}
816 			EVP_DigestInit (sign_ctx, EVP_sha256 ());
817 			EVP_DigestUpdate (sign_ctx, data, datalen);
818 			EVP_DigestFinal (sign_ctx, dig, &diglen);
819 
820 			if (EVP_PKEY_verify (key_ctx, sig, siglen, dig, diglen) == 1) {
821 				EVP_MD_CTX_destroy (sign_ctx);
822 				EVP_PKEY_CTX_free (key_ctx);
823 				return true;
824 			}
825 
826 			EVP_PKEY_CTX_free (key_ctx);
827 		}
828 	}
829 
830 	EVP_MD_CTX_destroy (sign_ctx);
831 
832 	return false;
833 }
834 #endif
835 
836 struct ucl_include_params {
837 	bool check_signature;
838 	bool must_exist;
839 	bool use_glob;
840 	bool use_prefix;
841 	bool soft_fail;
842 	bool allow_glob;
843 	unsigned priority;
844 	enum ucl_duplicate_strategy strat;
845 	enum ucl_parse_type parse_type;
846 	const char *prefix;
847 	const char *target;
848 };
849 
850 /**
851  * Include an url to configuration
852  * @param data
853  * @param len
854  * @param parser
855  * @param err
856  * @return
857  */
858 static bool
859 ucl_include_url (const unsigned char *data, size_t len,
860 		struct ucl_parser *parser,
861 		struct ucl_include_params *params)
862 {
863 
864 	bool res;
865 	unsigned char *buf = NULL;
866 	size_t buflen = 0;
867 	struct ucl_chunk *chunk;
868 	char urlbuf[PATH_MAX];
869 	int prev_state;
870 
871 	snprintf (urlbuf, sizeof (urlbuf), "%.*s", (int)len, data);
872 
873 	if (!ucl_fetch_url (urlbuf, &buf, &buflen, &parser->err, params->must_exist)) {
874 		return !params->must_exist;
875 	}
876 
877 	if (params->check_signature) {
878 #if (defined(HAVE_OPENSSL) && OPENSSL_VERSION_NUMBER >= 0x10000000L)
879 		unsigned char *sigbuf = NULL;
880 		size_t siglen = 0;
881 		/* We need to check signature first */
882 		snprintf (urlbuf, sizeof (urlbuf), "%.*s.sig", (int)len, data);
883 		if (!ucl_fetch_url (urlbuf, &sigbuf, &siglen, &parser->err, true)) {
884 			return false;
885 		}
886 		if (!ucl_sig_check (buf, buflen, sigbuf, siglen, parser)) {
887 			ucl_create_err (&parser->err, "cannot verify url %s: %s",
888 							urlbuf,
889 							ERR_error_string (ERR_get_error (), NULL));
890 			if (siglen > 0) {
891 				ucl_munmap (sigbuf, siglen);
892 			}
893 			return false;
894 		}
895 		if (siglen > 0) {
896 			ucl_munmap (sigbuf, siglen);
897 		}
898 #endif
899 	}
900 
901 	prev_state = parser->state;
902 	parser->state = UCL_STATE_INIT;
903 
904 	res = ucl_parser_add_chunk_full (parser, buf, buflen, params->priority,
905 			params->strat, params->parse_type);
906 	if (res == true) {
907 		/* Remove chunk from the stack */
908 		chunk = parser->chunks;
909 		if (chunk != NULL) {
910 			parser->chunks = chunk->next;
911 			UCL_FREE (sizeof (struct ucl_chunk), chunk);
912 		}
913 	}
914 
915 	parser->state = prev_state;
916 	free (buf);
917 
918 	return res;
919 }
920 
921 /**
922  * Include a single file to the parser
923  * @param data
924  * @param len
925  * @param parser
926  * @param check_signature
927  * @param must_exist
928  * @param allow_glob
929  * @param priority
930  * @return
931  */
932 static bool
933 ucl_include_file_single (const unsigned char *data, size_t len,
934 		struct ucl_parser *parser, struct ucl_include_params *params)
935 {
936 	bool res;
937 	struct ucl_chunk *chunk;
938 	unsigned char *buf = NULL;
939 	char *old_curfile, *ext;
940 	size_t buflen = 0;
941 	char filebuf[PATH_MAX], realbuf[PATH_MAX];
942 	int prev_state;
943 	struct ucl_variable *cur_var, *tmp_var, *old_curdir = NULL,
944 			*old_filename = NULL;
945 	ucl_object_t *nest_obj = NULL, *old_obj = NULL, *new_obj = NULL;
946 	ucl_hash_t *container = NULL;
947 	struct ucl_stack *st = NULL;
948 
949 	snprintf (filebuf, sizeof (filebuf), "%.*s", (int)len, data);
950 	if (ucl_realpath (filebuf, realbuf) == NULL) {
951 		if (params->soft_fail) {
952 			return false;
953 		}
954 		if (!params->must_exist) {
955 			return true;
956 		}
957 		ucl_create_err (&parser->err, "cannot open file %s: %s",
958 									filebuf,
959 									strerror (errno));
960 		return false;
961 	}
962 
963 	if (parser->cur_file && strcmp (realbuf, parser->cur_file) == 0) {
964 		/* We are likely including the file itself */
965 		if (params->soft_fail) {
966 			return false;
967 		}
968 
969 		ucl_create_err (&parser->err, "trying to include the file %s from itself",
970 				realbuf);
971 		return false;
972 	}
973 
974 	if (!ucl_fetch_file (realbuf, &buf, &buflen, &parser->err, params->must_exist)) {
975 		if (params->soft_fail) {
976 			return false;
977 		}
978 
979 		return (!params->must_exist || false);
980 	}
981 
982 	if (params->check_signature) {
983 #if (defined(HAVE_OPENSSL) && OPENSSL_VERSION_NUMBER >= 0x10000000L)
984 		unsigned char *sigbuf = NULL;
985 		size_t siglen = 0;
986 		/* We need to check signature first */
987 		snprintf (filebuf, sizeof (filebuf), "%s.sig", realbuf);
988 		if (!ucl_fetch_file (filebuf, &sigbuf, &siglen, &parser->err, true)) {
989 			return false;
990 		}
991 		if (!ucl_sig_check (buf, buflen, sigbuf, siglen, parser)) {
992 			ucl_create_err (&parser->err, "cannot verify file %s: %s",
993 							filebuf,
994 							ERR_error_string (ERR_get_error (), NULL));
995 			if (sigbuf) {
996 				ucl_munmap (sigbuf, siglen);
997 			}
998 			return false;
999 		}
1000 		if (sigbuf) {
1001 			ucl_munmap (sigbuf, siglen);
1002 		}
1003 #endif
1004 	}
1005 
1006 	old_curfile = parser->cur_file;
1007 	parser->cur_file = strdup (realbuf);
1008 
1009 	/* Store old file vars */
1010 	DL_FOREACH_SAFE (parser->variables, cur_var, tmp_var) {
1011 		if (strcmp (cur_var->var, "CURDIR") == 0) {
1012 			old_curdir = cur_var;
1013 			DL_DELETE (parser->variables, cur_var);
1014 		}
1015 		else if (strcmp (cur_var->var, "FILENAME") == 0) {
1016 			old_filename = cur_var;
1017 			DL_DELETE (parser->variables, cur_var);
1018 		}
1019 	}
1020 
1021 	ucl_parser_set_filevars (parser, realbuf, false);
1022 
1023 	prev_state = parser->state;
1024 	parser->state = UCL_STATE_INIT;
1025 
1026 	if (params->use_prefix && params->prefix == NULL) {
1027 		/* Auto generate a key name based on the included filename */
1028 		params->prefix = basename (realbuf);
1029 		ext = strrchr (params->prefix, '.');
1030 		if (ext != NULL && (strcmp (ext, ".conf") == 0 || strcmp (ext, ".ucl") == 0)) {
1031 			/* Strip off .conf or .ucl */
1032 			*ext = '\0';
1033 		}
1034 	}
1035 	if (params->prefix != NULL) {
1036 		/* This is a prefixed include */
1037 		container = parser->stack->obj->value.ov;
1038 
1039 		old_obj = __DECONST (ucl_object_t *, ucl_hash_search (container,
1040 				params->prefix, strlen (params->prefix)));
1041 
1042 		if (strcasecmp (params->target, "array") == 0 && old_obj == NULL) {
1043 			/* Create an array with key: prefix */
1044 			old_obj = ucl_object_new_full (UCL_ARRAY, params->priority);
1045 			old_obj->key = params->prefix;
1046 			old_obj->keylen = strlen (params->prefix);
1047 			ucl_copy_key_trash(old_obj);
1048 			old_obj->prev = old_obj;
1049 			old_obj->next = NULL;
1050 
1051 			container = ucl_hash_insert_object (container, old_obj,
1052 					parser->flags & UCL_PARSER_KEY_LOWERCASE);
1053 			parser->stack->obj->len ++;
1054 
1055 			nest_obj = ucl_object_new_full (UCL_OBJECT, params->priority);
1056 			nest_obj->prev = nest_obj;
1057 			nest_obj->next = NULL;
1058 
1059 			ucl_array_append (old_obj, nest_obj);
1060 		}
1061 		else if (old_obj == NULL) {
1062 			/* Create an object with key: prefix */
1063 			nest_obj = ucl_object_new_full (UCL_OBJECT, params->priority);
1064 
1065 			if (nest_obj == NULL) {
1066 				ucl_create_err (&parser->err, "cannot allocate memory for an object");
1067 				if (buf) {
1068 					ucl_munmap (buf, buflen);
1069 				}
1070 
1071 				return false;
1072 			}
1073 
1074 			nest_obj->key = params->prefix;
1075 			nest_obj->keylen = strlen (params->prefix);
1076 			ucl_copy_key_trash(nest_obj);
1077 			nest_obj->prev = nest_obj;
1078 			nest_obj->next = NULL;
1079 
1080 			container = ucl_hash_insert_object (container, nest_obj,
1081 					parser->flags & UCL_PARSER_KEY_LOWERCASE);
1082 			parser->stack->obj->len ++;
1083 		}
1084 		else if (strcasecmp (params->target, "array") == 0 ||
1085 				ucl_object_type(old_obj) == UCL_ARRAY) {
1086 			if (ucl_object_type(old_obj) == UCL_ARRAY) {
1087 				/* Append to the existing array */
1088 				nest_obj = ucl_object_new_full (UCL_OBJECT, params->priority);
1089 				if (nest_obj == NULL) {
1090 					ucl_create_err (&parser->err, "cannot allocate memory for an object");
1091 					if (buf) {
1092 						ucl_munmap (buf, buflen);
1093 					}
1094 
1095 					return false;
1096 				}
1097 				nest_obj->prev = nest_obj;
1098 				nest_obj->next = NULL;
1099 
1100 				ucl_array_append (old_obj, nest_obj);
1101 			}
1102 			else {
1103 				/* Convert the object to an array */
1104 				new_obj = ucl_object_typed_new (UCL_ARRAY);
1105 				if (new_obj == NULL) {
1106 					ucl_create_err (&parser->err, "cannot allocate memory for an object");
1107 					if (buf) {
1108 						ucl_munmap (buf, buflen);
1109 					}
1110 
1111 					return false;
1112 				}
1113 				new_obj->key = old_obj->key;
1114 				new_obj->keylen = old_obj->keylen;
1115 				new_obj->flags |= UCL_OBJECT_MULTIVALUE;
1116 				new_obj->prev = new_obj;
1117 				new_obj->next = NULL;
1118 
1119 				nest_obj = ucl_object_new_full (UCL_OBJECT, params->priority);
1120 				if (nest_obj == NULL) {
1121 					ucl_create_err (&parser->err, "cannot allocate memory for an object");
1122 					if (buf) {
1123 						ucl_munmap (buf, buflen);
1124 					}
1125 
1126 					return false;
1127 				}
1128 				nest_obj->prev = nest_obj;
1129 				nest_obj->next = NULL;
1130 
1131 				ucl_array_append (new_obj, old_obj);
1132 				ucl_array_append (new_obj, nest_obj);
1133 				ucl_hash_replace (container, old_obj, new_obj);
1134 			}
1135 		}
1136 		else {
1137 			if (ucl_object_type (old_obj) == UCL_OBJECT) {
1138 				/* Append to existing Object*/
1139 				nest_obj = old_obj;
1140 			}
1141 			else {
1142 				/* The key is not an object */
1143 				ucl_create_err (&parser->err,
1144 						"Conflicting type for key: %s",
1145 						params->prefix);
1146 				if (buf) {
1147 					ucl_munmap (buf, buflen);
1148 				}
1149 
1150 				return false;
1151 			}
1152 		}
1153 
1154 		 /* Put all of the content of the include inside that object */
1155 		parser->stack->obj->value.ov = container;
1156 
1157 		st = UCL_ALLOC (sizeof (struct ucl_stack));
1158 		if (st == NULL) {
1159 			ucl_create_err (&parser->err, "cannot allocate memory for an object");
1160 			ucl_object_unref (nest_obj);
1161 
1162 			if (buf) {
1163 				ucl_munmap (buf, buflen);
1164 			}
1165 
1166 			return false;
1167 		}
1168 		st->obj = nest_obj;
1169 		st->level = parser->stack->level;
1170 		LL_PREPEND (parser->stack, st);
1171 		parser->cur_obj = nest_obj;
1172 	}
1173 
1174 	res = ucl_parser_add_chunk_full (parser, buf, buflen, params->priority,
1175 			params->strat, params->parse_type);
1176 
1177 	if (!res) {
1178 		if (!params->must_exist) {
1179 			/* Free error */
1180 			utstring_free (parser->err);
1181 			parser->err = NULL;
1182 			res = true;
1183 		}
1184 	}
1185 
1186 	/* Stop nesting the include, take 1 level off the stack */
1187 	if (params->prefix != NULL && nest_obj != NULL) {
1188 		parser->stack = st->next;
1189 		UCL_FREE (sizeof (struct ucl_stack), st);
1190 	}
1191 
1192 	/* Remove chunk from the stack */
1193 	chunk = parser->chunks;
1194 	if (chunk != NULL) {
1195 		parser->chunks = chunk->next;
1196 		UCL_FREE (sizeof (struct ucl_chunk), chunk);
1197 		parser->recursion --;
1198 	}
1199 
1200 	/* Restore old file vars */
1201 	if (parser->cur_file) {
1202 		free (parser->cur_file);
1203 	}
1204 
1205 	parser->cur_file = old_curfile;
1206 	DL_FOREACH_SAFE (parser->variables, cur_var, tmp_var) {
1207 		if (strcmp (cur_var->var, "CURDIR") == 0 && old_curdir) {
1208 			DL_DELETE (parser->variables, cur_var);
1209 			free (cur_var->var);
1210 			free (cur_var->value);
1211 			UCL_FREE (sizeof (struct ucl_variable), cur_var);
1212 		}
1213 		else if (strcmp (cur_var->var, "FILENAME") == 0 && old_filename) {
1214 			DL_DELETE (parser->variables, cur_var);
1215 			free (cur_var->var);
1216 			free (cur_var->value);
1217 			UCL_FREE (sizeof (struct ucl_variable), cur_var);
1218 		}
1219 	}
1220 	if (old_filename) {
1221 		DL_APPEND (parser->variables, old_filename);
1222 	}
1223 	if (old_curdir) {
1224 		DL_APPEND (parser->variables, old_curdir);
1225 	}
1226 
1227 	parser->state = prev_state;
1228 
1229 	if (buflen > 0) {
1230 		ucl_munmap (buf, buflen);
1231 	}
1232 
1233 	return res;
1234 }
1235 
1236 /**
1237  * Include a file to configuration
1238  * @param data
1239  * @param len
1240  * @param parser
1241  * @param err
1242  * @return
1243  */
1244 static bool
1245 ucl_include_file (const unsigned char *data, size_t len,
1246 		struct ucl_parser *parser, struct ucl_include_params *params)
1247 {
1248 	const unsigned char *p = data, *end = data + len;
1249 	bool need_glob = false;
1250 	int cnt = 0;
1251 	char glob_pattern[PATH_MAX];
1252 	size_t i;
1253 
1254 #ifndef _WIN32
1255 	if (!params->allow_glob) {
1256 		return ucl_include_file_single (data, len, parser, params);
1257 	}
1258 	else {
1259 		/* Check for special symbols in a filename */
1260 		while (p != end) {
1261 			if (*p == '*' || *p == '?') {
1262 				need_glob = true;
1263 				break;
1264 			}
1265 			p ++;
1266 		}
1267 		if (need_glob) {
1268 			glob_t globbuf;
1269 			memset (&globbuf, 0, sizeof (globbuf));
1270 			ucl_strlcpy (glob_pattern, (const char *)data,
1271 				(len + 1 < sizeof (glob_pattern) ? len + 1 : sizeof (glob_pattern)));
1272 			if (glob (glob_pattern, 0, NULL, &globbuf) != 0) {
1273 				return (!params->must_exist || false);
1274 			}
1275 			for (i = 0; i < globbuf.gl_pathc; i ++) {
1276 				if (!ucl_include_file_single ((unsigned char *)globbuf.gl_pathv[i],
1277 						strlen (globbuf.gl_pathv[i]), parser, params)) {
1278 					if (params->soft_fail) {
1279 						continue;
1280 					}
1281 					globfree (&globbuf);
1282 					return false;
1283 				}
1284 				cnt ++;
1285 			}
1286 			globfree (&globbuf);
1287 
1288 			if (cnt == 0 && params->must_exist) {
1289 				ucl_create_err (&parser->err, "cannot match any files for pattern %s",
1290 					glob_pattern);
1291 				return false;
1292 			}
1293 		}
1294 		else {
1295 			return ucl_include_file_single (data, len, parser, params);
1296 		}
1297 	}
1298 #else
1299 	/* Win32 compilers do not support globbing. Therefore, for Win32,
1300 	   treat allow_glob/need_glob as a NOOP and just return */
1301 	return ucl_include_file_single (data, len, parser, params);
1302 #endif
1303 
1304 	return true;
1305 }
1306 
1307 /**
1308  * Common function to handle .*include* macros
1309  * @param data
1310  * @param len
1311  * @param args
1312  * @param parser
1313  * @param default_try
1314  * @param default_sign
1315  * @return
1316  */
1317 static bool
1318 ucl_include_common (const unsigned char *data, size_t len,
1319 		const ucl_object_t *args, struct ucl_parser *parser,
1320 		bool default_try,
1321 		bool default_sign)
1322 {
1323 	bool allow_url = false, search = false;
1324 	const char *duplicate;
1325 	const ucl_object_t *param;
1326 	ucl_object_iter_t it = NULL, ip = NULL;
1327 	char ipath[PATH_MAX];
1328 	struct ucl_include_params params;
1329 
1330 	/* Default values */
1331 	params.soft_fail = default_try;
1332 	params.allow_glob = false;
1333 	params.check_signature = default_sign;
1334 	params.use_prefix = false;
1335 	params.target = "object";
1336 	params.prefix = NULL;
1337 	params.priority = 0;
1338 	params.parse_type = UCL_PARSE_UCL;
1339 	params.strat = UCL_DUPLICATE_APPEND;
1340 	params.must_exist = !default_try;
1341 
1342 	/* Process arguments */
1343 	if (args != NULL && args->type == UCL_OBJECT) {
1344 		while ((param = ucl_object_iterate (args, &it, true)) != NULL) {
1345 			if (param->type == UCL_BOOLEAN) {
1346 				if (strncmp (param->key, "try", param->keylen) == 0) {
1347 					params.must_exist = !ucl_object_toboolean (param);
1348 				}
1349 				else if (strncmp (param->key, "sign", param->keylen) == 0) {
1350 					params.check_signature = ucl_object_toboolean (param);
1351 				}
1352 				else if (strncmp (param->key, "glob", param->keylen) == 0) {
1353 					params.allow_glob = ucl_object_toboolean (param);
1354 				}
1355 				else if (strncmp (param->key, "url", param->keylen) == 0) {
1356 					allow_url = ucl_object_toboolean (param);
1357 				}
1358 				else if (strncmp (param->key, "prefix", param->keylen) == 0) {
1359 					params.use_prefix = ucl_object_toboolean (param);
1360 				}
1361 			}
1362 			else if (param->type == UCL_STRING) {
1363 				if (strncmp (param->key, "key", param->keylen) == 0) {
1364 					params.prefix = ucl_object_tostring (param);
1365 				}
1366 				else if (strncmp (param->key, "target", param->keylen) == 0) {
1367 					params.target = ucl_object_tostring (param);
1368 				}
1369 				else if (strncmp (param->key, "duplicate", param->keylen) == 0) {
1370 					duplicate = ucl_object_tostring (param);
1371 
1372 					if (strcmp (duplicate, "append") == 0) {
1373 						params.strat = UCL_DUPLICATE_APPEND;
1374 					}
1375 					else if (strcmp (duplicate, "merge") == 0) {
1376 						params.strat = UCL_DUPLICATE_MERGE;
1377 					}
1378 					else if (strcmp (duplicate, "rewrite") == 0) {
1379 						params.strat = UCL_DUPLICATE_REWRITE;
1380 					}
1381 					else if (strcmp (duplicate, "error") == 0) {
1382 						params.strat = UCL_DUPLICATE_ERROR;
1383 					}
1384 				}
1385 			}
1386 			else if (param->type == UCL_ARRAY) {
1387 				if (strncmp (param->key, "path", param->keylen) == 0) {
1388 					ucl_set_include_path (parser, __DECONST(ucl_object_t *, param));
1389 				}
1390 			}
1391 			else if (param->type == UCL_INT) {
1392 				if (strncmp (param->key, "priority", param->keylen) == 0) {
1393 					params.priority = ucl_object_toint (param);
1394 				}
1395 			}
1396 		}
1397 	}
1398 
1399 	if (parser->includepaths == NULL) {
1400 		if (allow_url && ucl_strnstr (data, "://", len) != NULL) {
1401 			/* Globbing is not used for URL's */
1402 			return ucl_include_url (data, len, parser, &params);
1403 		}
1404 		else if (data != NULL) {
1405 			/* Try to load a file */
1406 			return ucl_include_file (data, len, parser, &params);
1407 		}
1408 	}
1409 	else {
1410 		if (allow_url && ucl_strnstr (data, "://", len) != NULL) {
1411 			/* Globbing is not used for URL's */
1412 			return ucl_include_url (data, len, parser, &params);
1413 		}
1414 
1415 		ip = ucl_object_iterate_new (parser->includepaths);
1416 		while ((param = ucl_object_iterate_safe (ip, true)) != NULL) {
1417 			if (ucl_object_type(param) == UCL_STRING) {
1418 				snprintf (ipath, sizeof (ipath), "%s/%.*s", ucl_object_tostring(param),
1419 						(int)len, data);
1420 				if ((search = ucl_include_file (ipath, strlen (ipath),
1421 						parser, &params))) {
1422 					if (!params.allow_glob) {
1423 						break;
1424 					}
1425 				}
1426 			}
1427 		}
1428 		ucl_object_iterate_free (ip);
1429 		if (search == true) {
1430 			return true;
1431 		}
1432 		else {
1433 			ucl_create_err (&parser->err,
1434 					"cannot find file: %.*s in search path",
1435 					(int)len, data);
1436 			return false;
1437 		}
1438 	}
1439 
1440 	return false;
1441 }
1442 
1443 /**
1444  * Handle include macro
1445  * @param data include data
1446  * @param len length of data
1447  * @param args UCL object representing arguments to the macro
1448  * @param ud user data
1449  * @return
1450  */
1451 bool
1452 ucl_include_handler (const unsigned char *data, size_t len,
1453 		const ucl_object_t *args, void* ud)
1454 {
1455 	struct ucl_parser *parser = ud;
1456 
1457 	return ucl_include_common (data, len, args, parser, false, false);
1458 }
1459 
1460 /**
1461  * Handle includes macro
1462  * @param data include data
1463  * @param len length of data
1464  * @param args UCL object representing arguments to the macro
1465  * @param ud user data
1466  * @return
1467  */
1468 bool
1469 ucl_includes_handler (const unsigned char *data, size_t len,
1470 		const ucl_object_t *args, void* ud)
1471 {
1472 	struct ucl_parser *parser = ud;
1473 
1474 	return ucl_include_common (data, len, args, parser, false, true);
1475 }
1476 
1477 /**
1478  * Handle tryinclude macro
1479  * @param data include data
1480  * @param len length of data
1481  * @param args UCL object representing arguments to the macro
1482  * @param ud user data
1483  * @return
1484  */
1485 bool
1486 ucl_try_include_handler (const unsigned char *data, size_t len,
1487 		const ucl_object_t *args, void* ud)
1488 {
1489 	struct ucl_parser *parser = ud;
1490 
1491 	return ucl_include_common (data, len, args, parser, true, false);
1492 }
1493 
1494 /**
1495  * Handle priority macro
1496  * @param data include data
1497  * @param len length of data
1498  * @param args UCL object representing arguments to the macro
1499  * @param ud user data
1500  * @return
1501  */
1502 bool
1503 ucl_priority_handler (const unsigned char *data, size_t len,
1504 		const ucl_object_t *args, void* ud)
1505 {
1506 	struct ucl_parser *parser = ud;
1507 	unsigned priority = 255;
1508 	const ucl_object_t *param;
1509 	bool found = false;
1510 	char *value = NULL, *leftover = NULL;
1511 	ucl_object_iter_t it = NULL;
1512 
1513 	if (parser == NULL) {
1514 		return false;
1515 	}
1516 
1517 	/* Process arguments */
1518 	if (args != NULL && args->type == UCL_OBJECT) {
1519 		while ((param = ucl_object_iterate (args, &it, true)) != NULL) {
1520 			if (param->type == UCL_INT) {
1521 				if (strncmp (param->key, "priority", param->keylen) == 0) {
1522 					priority = ucl_object_toint (param);
1523 					found = true;
1524 				}
1525 			}
1526 		}
1527 	}
1528 
1529 	if (len > 0) {
1530 		value = malloc(len + 1);
1531 		ucl_strlcpy(value, (const char *)data, len + 1);
1532 		priority = strtol(value, &leftover, 10);
1533 		if (*leftover != '\0') {
1534 			ucl_create_err (&parser->err, "Invalid priority value in macro: %s",
1535 				value);
1536 			free(value);
1537 			return false;
1538 		}
1539 		free(value);
1540 		found = true;
1541 	}
1542 
1543 	if (found == true) {
1544 		parser->chunks->priority = priority;
1545 		return true;
1546 	}
1547 
1548 	ucl_create_err (&parser->err, "Unable to parse priority macro");
1549 	return false;
1550 }
1551 
1552 /**
1553  * Handle load macro
1554  * @param data include data
1555  * @param len length of data
1556  * @param args UCL object representing arguments to the macro
1557  * @param ud user data
1558  * @return
1559  */
1560 bool
1561 ucl_load_handler (const unsigned char *data, size_t len,
1562 		const ucl_object_t *args, void* ud)
1563 {
1564 	struct ucl_parser *parser = ud;
1565 	const ucl_object_t *param;
1566 	ucl_object_t *obj, *old_obj;
1567 	ucl_object_iter_t it = NULL;
1568 	bool try_load, multiline, test;
1569 	const char *target, *prefix;
1570 	char *load_file, *tmp;
1571 	unsigned char *buf;
1572 	size_t buflen;
1573 	unsigned priority;
1574 	int64_t iv;
1575 	ucl_object_t *container = NULL;
1576 	enum ucl_string_flags flags;
1577 
1578 	/* Default values */
1579 	try_load = false;
1580 	multiline = false;
1581 	test = false;
1582 	target = "string";
1583 	prefix = NULL;
1584 	load_file = NULL;
1585 	buf = NULL;
1586 	buflen = 0;
1587 	priority = 0;
1588 	obj = NULL;
1589 	old_obj = NULL;
1590 	flags = 0;
1591 
1592 	if (parser == NULL) {
1593 		return false;
1594 	}
1595 
1596 	/* Process arguments */
1597 	if (args != NULL && args->type == UCL_OBJECT) {
1598 		while ((param = ucl_object_iterate (args, &it, true)) != NULL) {
1599 			if (param->type == UCL_BOOLEAN) {
1600 				if (strncmp (param->key, "try", param->keylen) == 0) {
1601 					try_load = ucl_object_toboolean (param);
1602 				}
1603 				else if (strncmp (param->key, "multiline", param->keylen) == 0) {
1604 					multiline = ucl_object_toboolean (param);
1605 				}
1606 				else if (strncmp (param->key, "escape", param->keylen) == 0) {
1607 					test = ucl_object_toboolean (param);
1608 					if (test) {
1609 						flags |= UCL_STRING_ESCAPE;
1610 					}
1611 				}
1612 				else if (strncmp (param->key, "trim", param->keylen) == 0) {
1613 					test = ucl_object_toboolean (param);
1614 					if (test) {
1615 						flags |= UCL_STRING_TRIM;
1616 					}
1617 				}
1618 			}
1619 			else if (param->type == UCL_STRING) {
1620 				if (strncmp (param->key, "key", param->keylen) == 0) {
1621 					prefix = ucl_object_tostring (param);
1622 				}
1623 				else if (strncmp (param->key, "target", param->keylen) == 0) {
1624 					target = ucl_object_tostring (param);
1625 				}
1626 			}
1627 			else if (param->type == UCL_INT) {
1628 				if (strncmp (param->key, "priority", param->keylen) == 0) {
1629 					priority = ucl_object_toint (param);
1630 				}
1631 			}
1632 		}
1633 	}
1634 
1635 	if (prefix == NULL || strlen (prefix) == 0) {
1636 		ucl_create_err (&parser->err, "No Key specified in load macro");
1637 		return false;
1638 	}
1639 
1640 	if (len > 0) {
1641 		load_file = malloc (len + 1);
1642 		if (!load_file) {
1643 			ucl_create_err (&parser->err, "cannot allocate memory for suffix");
1644 
1645 			return false;
1646 		}
1647 
1648 		snprintf (load_file, len + 1, "%.*s", (int)len, data);
1649 
1650 		if (!ucl_fetch_file (load_file, &buf, &buflen, &parser->err,
1651 				!try_load)) {
1652 			free (load_file);
1653 
1654 			return (try_load || false);
1655 		}
1656 
1657 		free (load_file);
1658 		container = parser->stack->obj;
1659 		old_obj = __DECONST (ucl_object_t *, ucl_object_lookup (container,
1660 				prefix));
1661 
1662 		if (old_obj != NULL) {
1663 			ucl_create_err (&parser->err, "Key %s already exists", prefix);
1664 			if (buf) {
1665 				ucl_munmap (buf, buflen);
1666 			}
1667 
1668 			return false;
1669 		}
1670 
1671 		if (strcasecmp (target, "string") == 0) {
1672 			obj = ucl_object_fromstring_common (buf, buflen, flags);
1673 			ucl_copy_value_trash (obj);
1674 			if (multiline) {
1675 				obj->flags |= UCL_OBJECT_MULTILINE;
1676 			}
1677 		}
1678 		else if (strcasecmp (target, "int") == 0) {
1679 			tmp = malloc (buflen + 1);
1680 
1681 			if (tmp == NULL) {
1682 				ucl_create_err (&parser->err, "Memory allocation failed");
1683 				if (buf) {
1684 					ucl_munmap (buf, buflen);
1685 				}
1686 
1687 				return false;
1688 			}
1689 
1690 			snprintf (tmp, buflen + 1, "%.*s", (int)buflen, buf);
1691 			iv = strtoll (tmp, NULL, 10);
1692 			obj = ucl_object_fromint (iv);
1693 			free (tmp);
1694 		}
1695 
1696 		if (buf) {
1697 			ucl_munmap (buf, buflen);
1698 		}
1699 
1700 		if (obj != NULL) {
1701 			obj->key = prefix;
1702 			obj->keylen = strlen (prefix);
1703 			ucl_copy_key_trash (obj);
1704 			obj->prev = obj;
1705 			obj->next = NULL;
1706 			ucl_object_set_priority (obj, priority);
1707 			ucl_object_insert_key (container, obj, obj->key, obj->keylen, false);
1708 		}
1709 
1710 		return true;
1711 	}
1712 
1713 	ucl_create_err (&parser->err, "Unable to parse load macro");
1714 	return false;
1715 }
1716 
1717 bool
1718 ucl_inherit_handler (const unsigned char *data, size_t len,
1719 		const ucl_object_t *args, const ucl_object_t *ctx, void* ud)
1720 {
1721 	const ucl_object_t *parent, *cur;
1722 	ucl_object_t *target, *copy;
1723 	ucl_object_iter_t it = NULL;
1724 	bool replace = false;
1725 	struct ucl_parser *parser = ud;
1726 
1727 	parent = ucl_object_lookup_len (ctx, data, len);
1728 
1729 	/* Some sanity checks */
1730 	if (parent == NULL || ucl_object_type (parent) != UCL_OBJECT) {
1731 		ucl_create_err (&parser->err, "Unable to find inherited object %*.s",
1732 				(int)len, data);
1733 		return false;
1734 	}
1735 
1736 	if (parser->stack == NULL || parser->stack->obj == NULL ||
1737 			ucl_object_type (parser->stack->obj) != UCL_OBJECT) {
1738 		ucl_create_err (&parser->err, "Invalid inherit context");
1739 		return false;
1740 	}
1741 
1742 	target = parser->stack->obj;
1743 
1744 	if (args && (cur = ucl_object_lookup (args, "replace")) != NULL) {
1745 		replace = ucl_object_toboolean (cur);
1746 	}
1747 
1748 	while ((cur = ucl_object_iterate (parent, &it, true))) {
1749 		/* We do not replace existing keys */
1750 		if (!replace && ucl_object_lookup_len (target, cur->key, cur->keylen)) {
1751 			continue;
1752 		}
1753 
1754 		copy = ucl_object_copy (cur);
1755 
1756 		if (!replace) {
1757 			copy->flags |= UCL_OBJECT_INHERITED;
1758 		}
1759 
1760 		ucl_object_insert_key (target, copy, copy->key,
1761 				copy->keylen, false);
1762 	}
1763 
1764 	return true;
1765 }
1766 
1767 bool
1768 ucl_parser_set_filevars (struct ucl_parser *parser, const char *filename, bool need_expand)
1769 {
1770 	char realbuf[PATH_MAX], *curdir;
1771 
1772 	if (filename != NULL) {
1773 		if (need_expand) {
1774 			if (ucl_realpath (filename, realbuf) == NULL) {
1775 				return false;
1776 			}
1777 		}
1778 		else {
1779 			ucl_strlcpy (realbuf, filename, sizeof (realbuf));
1780 		}
1781 
1782 		/* Define variables */
1783 		ucl_parser_register_variable (parser, "FILENAME", realbuf);
1784 		curdir = dirname (realbuf);
1785 		ucl_parser_register_variable (parser, "CURDIR", curdir);
1786 	}
1787 	else {
1788 		/* Set everything from the current dir */
1789 		curdir = getcwd (realbuf, sizeof (realbuf));
1790 		ucl_parser_register_variable (parser, "FILENAME", "undef");
1791 		ucl_parser_register_variable (parser, "CURDIR", curdir);
1792 	}
1793 
1794 	return true;
1795 }
1796 
1797 bool
1798 ucl_parser_add_file_priority (struct ucl_parser *parser, const char *filename,
1799 		unsigned priority)
1800 {
1801 	unsigned char *buf;
1802 	size_t len;
1803 	bool ret;
1804 	char realbuf[PATH_MAX];
1805 
1806 	if (ucl_realpath (filename, realbuf) == NULL) {
1807 		ucl_create_err (&parser->err, "cannot open file %s: %s",
1808 				filename,
1809 				strerror (errno));
1810 		return false;
1811 	}
1812 
1813 	if (!ucl_fetch_file (realbuf, &buf, &len, &parser->err, true)) {
1814 		return false;
1815 	}
1816 
1817 	if (parser->cur_file) {
1818 		free (parser->cur_file);
1819 	}
1820 	parser->cur_file = strdup (realbuf);
1821 	ucl_parser_set_filevars (parser, realbuf, false);
1822 	ret = ucl_parser_add_chunk_priority (parser, buf, len, priority);
1823 
1824 	if (len > 0) {
1825 		ucl_munmap (buf, len);
1826 	}
1827 
1828 	return ret;
1829 }
1830 
1831 bool
1832 ucl_parser_add_file (struct ucl_parser *parser, const char *filename)
1833 {
1834 	if (parser == NULL) {
1835 		return false;
1836 	}
1837 
1838 	return ucl_parser_add_file_priority(parser, filename,
1839 			parser->default_priority);
1840 }
1841 
1842 bool
1843 ucl_parser_add_fd_priority (struct ucl_parser *parser, int fd,
1844 		unsigned priority)
1845 {
1846 	unsigned char *buf;
1847 	size_t len;
1848 	bool ret;
1849 	struct stat st;
1850 
1851 	if (fstat (fd, &st) == -1) {
1852 		ucl_create_err (&parser->err, "cannot stat fd %d: %s",
1853 			fd, strerror (errno));
1854 		return false;
1855 	}
1856 	if (st.st_size == 0) {
1857 		return true;
1858 	}
1859 	if ((buf = ucl_mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) {
1860 		ucl_create_err (&parser->err, "cannot mmap fd %d: %s",
1861 			fd, strerror (errno));
1862 		return false;
1863 	}
1864 
1865 	if (parser->cur_file) {
1866 		free (parser->cur_file);
1867 	}
1868 	parser->cur_file = NULL;
1869 	len = st.st_size;
1870 	ret = ucl_parser_add_chunk_priority (parser, buf, len, priority);
1871 
1872 	if (len > 0) {
1873 		ucl_munmap (buf, len);
1874 	}
1875 
1876 	return ret;
1877 }
1878 
1879 bool
1880 ucl_parser_add_fd (struct ucl_parser *parser, int fd)
1881 {
1882 	if (parser == NULL) {
1883 		return false;
1884 	}
1885 
1886 	return ucl_parser_add_fd_priority(parser, fd, parser->default_priority);
1887 }
1888 
1889 size_t
1890 ucl_strlcpy (char *dst, const char *src, size_t siz)
1891 {
1892 	char *d = dst;
1893 	const char *s = src;
1894 	size_t n = siz;
1895 
1896 	/* Copy as many bytes as will fit */
1897 	if (n != 0) {
1898 		while (--n != 0) {
1899 			if ((*d++ = *s++) == '\0') {
1900 				break;
1901 			}
1902 		}
1903 	}
1904 
1905 	if (n == 0 && siz != 0) {
1906 		*d = '\0';
1907 	}
1908 
1909 	return (s - src - 1);    /* count does not include NUL */
1910 }
1911 
1912 size_t
1913 ucl_strlcpy_unsafe (char *dst, const char *src, size_t siz)
1914 {
1915 	memcpy (dst, src, siz - 1);
1916 	dst[siz - 1] = '\0';
1917 
1918 	return siz - 1;
1919 }
1920 
1921 size_t
1922 ucl_strlcpy_tolower (char *dst, const char *src, size_t siz)
1923 {
1924 	char *d = dst;
1925 	const char *s = src;
1926 	size_t n = siz;
1927 
1928 	/* Copy as many bytes as will fit */
1929 	if (n != 0) {
1930 		while (--n != 0) {
1931 			if ((*d++ = tolower (*s++)) == '\0') {
1932 				break;
1933 			}
1934 		}
1935 	}
1936 
1937 	if (n == 0 && siz != 0) {
1938 		*d = '\0';
1939 	}
1940 
1941 	return (s - src);    /* count does not include NUL */
1942 }
1943 
1944 /*
1945  * Find the first occurrence of find in s
1946  */
1947 char *
1948 ucl_strnstr (const char *s, const char *find, int len)
1949 {
1950 	char c, sc;
1951 	int mlen;
1952 
1953 	if ((c = *find++) != 0) {
1954 		mlen = strlen (find);
1955 		do {
1956 			do {
1957 				if ((sc = *s++) == 0 || len-- == 0)
1958 					return (NULL);
1959 			} while (sc != c);
1960 		} while (strncmp (s, find, mlen) != 0);
1961 		s--;
1962 	}
1963 	return ((char *)s);
1964 }
1965 
1966 /*
1967  * Find the first occurrence of find in s, ignore case.
1968  */
1969 char *
1970 ucl_strncasestr (const char *s, const char *find, int len)
1971 {
1972 	char c, sc;
1973 	int mlen;
1974 
1975 	if ((c = *find++) != 0) {
1976 		c = tolower (c);
1977 		mlen = strlen (find);
1978 		do {
1979 			do {
1980 				if ((sc = *s++) == 0 || len-- == 0)
1981 					return (NULL);
1982 			} while (tolower (sc) != c);
1983 		} while (strncasecmp (s, find, mlen) != 0);
1984 		s--;
1985 	}
1986 	return ((char *)s);
1987 }
1988 
1989 ucl_object_t *
1990 ucl_object_fromstring_common (const char *str, size_t len, enum ucl_string_flags flags)
1991 {
1992 	ucl_object_t *obj;
1993 	const char *start, *end, *p, *pos;
1994 	char *dst, *d;
1995 	size_t escaped_len;
1996 
1997 	if (str == NULL) {
1998 		return NULL;
1999 	}
2000 
2001 	obj = ucl_object_new ();
2002 	if (obj) {
2003 		if (len == 0) {
2004 			len = strlen (str);
2005 		}
2006 		if (flags & UCL_STRING_TRIM) {
2007 			/* Skip leading spaces */
2008 			for (start = str; (size_t)(start - str) < len; start ++) {
2009 				if (!ucl_test_character (*start, UCL_CHARACTER_WHITESPACE_UNSAFE)) {
2010 					break;
2011 				}
2012 			}
2013 			/* Skip trailing spaces */
2014 			for (end = str + len - 1; end > start; end --) {
2015 				if (!ucl_test_character (*end, UCL_CHARACTER_WHITESPACE_UNSAFE)) {
2016 					break;
2017 				}
2018 			}
2019 			end ++;
2020 		}
2021 		else {
2022 			start = str;
2023 			end = str + len;
2024 		}
2025 
2026 		obj->type = UCL_STRING;
2027 		if (flags & UCL_STRING_ESCAPE) {
2028 			for (p = start, escaped_len = 0; p < end; p ++, escaped_len ++) {
2029 				if (ucl_test_character (*p, UCL_CHARACTER_JSON_UNSAFE)) {
2030 					escaped_len ++;
2031 				}
2032 			}
2033 			dst = malloc (escaped_len + 1);
2034 			if (dst != NULL) {
2035 				for (p = start, d = dst; p < end; p ++, d ++) {
2036 					if (ucl_test_character (*p, UCL_CHARACTER_JSON_UNSAFE)) {
2037 						switch (*p) {
2038 						case '\n':
2039 							*d++ = '\\';
2040 							*d = 'n';
2041 							break;
2042 						case '\r':
2043 							*d++ = '\\';
2044 							*d = 'r';
2045 							break;
2046 						case '\b':
2047 							*d++ = '\\';
2048 							*d = 'b';
2049 							break;
2050 						case '\t':
2051 							*d++ = '\\';
2052 							*d = 't';
2053 							break;
2054 						case '\f':
2055 							*d++ = '\\';
2056 							*d = 'f';
2057 							break;
2058 						case '\\':
2059 							*d++ = '\\';
2060 							*d = '\\';
2061 							break;
2062 						case '"':
2063 							*d++ = '\\';
2064 							*d = '"';
2065 							break;
2066 						}
2067 					}
2068 					else {
2069 						*d = *p;
2070 					}
2071 				}
2072 				*d = '\0';
2073 				obj->value.sv = dst;
2074 				obj->trash_stack[UCL_TRASH_VALUE] = dst;
2075 				obj->len = escaped_len;
2076 			}
2077 		}
2078 		else {
2079 			dst = malloc (end - start + 1);
2080 			if (dst != NULL) {
2081 				ucl_strlcpy_unsafe (dst, start, end - start + 1);
2082 				obj->value.sv = dst;
2083 				obj->trash_stack[UCL_TRASH_VALUE] = dst;
2084 				obj->len = end - start;
2085 			}
2086 		}
2087 		if ((flags & UCL_STRING_PARSE) && dst != NULL) {
2088 			/* Parse what we have */
2089 			if (flags & UCL_STRING_PARSE_BOOLEAN) {
2090 				if (!ucl_maybe_parse_boolean (obj, dst, obj->len) && (flags & UCL_STRING_PARSE_NUMBER)) {
2091 					ucl_maybe_parse_number (obj, dst, dst + obj->len, &pos,
2092 							flags & UCL_STRING_PARSE_DOUBLE,
2093 							flags & UCL_STRING_PARSE_BYTES,
2094 							flags & UCL_STRING_PARSE_TIME);
2095 				}
2096 			}
2097 			else {
2098 				ucl_maybe_parse_number (obj, dst, dst + obj->len, &pos,
2099 						flags & UCL_STRING_PARSE_DOUBLE,
2100 						flags & UCL_STRING_PARSE_BYTES,
2101 						flags & UCL_STRING_PARSE_TIME);
2102 			}
2103 		}
2104 	}
2105 
2106 	return obj;
2107 }
2108 
2109 static bool
2110 ucl_object_insert_key_common (ucl_object_t *top, ucl_object_t *elt,
2111 		const char *key, size_t keylen, bool copy_key, bool merge, bool replace)
2112 {
2113 	ucl_object_t *found, *tmp;
2114 	const ucl_object_t *cur;
2115 	ucl_object_iter_t it = NULL;
2116 	const char *p;
2117 	int ret = true;
2118 
2119 	if (elt == NULL || key == NULL) {
2120 		return false;
2121 	}
2122 
2123 	if (top == NULL) {
2124 		return false;
2125 	}
2126 
2127 	if (top->type != UCL_OBJECT) {
2128 		/* It is possible to convert NULL type to an object */
2129 		if (top->type == UCL_NULL) {
2130 			top->type = UCL_OBJECT;
2131 		}
2132 		else {
2133 			/* Refuse converting of other object types */
2134 			return false;
2135 		}
2136 	}
2137 
2138 	if (top->value.ov == NULL) {
2139 		top->value.ov = ucl_hash_create (false);
2140 	}
2141 
2142 	if (keylen == 0) {
2143 		keylen = strlen (key);
2144 	}
2145 
2146 	for (p = key; p < key + keylen; p ++) {
2147 		if (ucl_test_character (*p, UCL_CHARACTER_UCL_UNSAFE)) {
2148 			elt->flags |= UCL_OBJECT_NEED_KEY_ESCAPE;
2149 			break;
2150 		}
2151 	}
2152 
2153 	/* workaround for some use cases */
2154 	if (elt->trash_stack[UCL_TRASH_KEY] != NULL &&
2155 			key != (const char *)elt->trash_stack[UCL_TRASH_KEY]) {
2156 		/* Remove copied key */
2157 		free (elt->trash_stack[UCL_TRASH_KEY]);
2158 		elt->trash_stack[UCL_TRASH_KEY] = NULL;
2159 		elt->flags &= ~UCL_OBJECT_ALLOCATED_KEY;
2160 	}
2161 
2162 	elt->key = key;
2163 	elt->keylen = keylen;
2164 
2165 	if (copy_key) {
2166 		ucl_copy_key_trash (elt);
2167 	}
2168 
2169 	found = __DECONST (ucl_object_t *, ucl_hash_search_obj (top->value.ov, elt));
2170 
2171 	if (found == NULL) {
2172 		top->value.ov = ucl_hash_insert_object (top->value.ov, elt, false);
2173 		top->len ++;
2174 		if (replace) {
2175 			ret = false;
2176 		}
2177 	}
2178 	else {
2179 		if (replace) {
2180 			ucl_hash_replace (top->value.ov, found, elt);
2181 			ucl_object_unref (found);
2182 		}
2183 		else if (merge) {
2184 			if (found->type != UCL_OBJECT && elt->type == UCL_OBJECT) {
2185 				/* Insert old elt to new one */
2186 				ucl_object_insert_key_common (elt, found, found->key,
2187 						found->keylen, copy_key, false, false);
2188 				ucl_hash_delete (top->value.ov, found);
2189 				top->value.ov = ucl_hash_insert_object (top->value.ov, elt, false);
2190 			}
2191 			else if (found->type == UCL_OBJECT && elt->type != UCL_OBJECT) {
2192 				/* Insert new to old */
2193 				ucl_object_insert_key_common (found, elt, elt->key,
2194 						elt->keylen, copy_key, false, false);
2195 			}
2196 			else if (found->type == UCL_OBJECT && elt->type == UCL_OBJECT) {
2197 				/* Mix two hashes */
2198 				while ((cur = ucl_object_iterate (elt, &it, true)) != NULL) {
2199 					tmp = ucl_object_ref (cur);
2200 					ucl_object_insert_key_common (found, tmp, cur->key,
2201 							cur->keylen, copy_key, false, false);
2202 				}
2203 				ucl_object_unref (elt);
2204 			}
2205 			else {
2206 				/* Just make a list of scalars */
2207 				DL_APPEND (found, elt);
2208 			}
2209 		}
2210 		else {
2211 			DL_APPEND (found, elt);
2212 		}
2213 	}
2214 
2215 	return ret;
2216 }
2217 
2218 bool
2219 ucl_object_delete_keyl (ucl_object_t *top, const char *key, size_t keylen)
2220 {
2221 	ucl_object_t *found;
2222 
2223 	if (top == NULL || key == NULL) {
2224 		return false;
2225 	}
2226 
2227 	found = __DECONST (ucl_object_t *, ucl_object_lookup_len (top, key, keylen));
2228 
2229 	if (found == NULL) {
2230 		return false;
2231 	}
2232 
2233 	ucl_hash_delete (top->value.ov, found);
2234 	ucl_object_unref (found);
2235 	top->len --;
2236 
2237 	return true;
2238 }
2239 
2240 bool
2241 ucl_object_delete_key (ucl_object_t *top, const char *key)
2242 {
2243 	return ucl_object_delete_keyl (top, key, strlen (key));
2244 }
2245 
2246 ucl_object_t*
2247 ucl_object_pop_keyl (ucl_object_t *top, const char *key, size_t keylen)
2248 {
2249 	const ucl_object_t *found;
2250 
2251 	if (top == NULL || key == NULL) {
2252 		return false;
2253 	}
2254 	found = ucl_object_lookup_len (top, key, keylen);
2255 
2256 	if (found == NULL) {
2257 		return NULL;
2258 	}
2259 	ucl_hash_delete (top->value.ov, found);
2260 	top->len --;
2261 
2262 	return __DECONST (ucl_object_t *, found);
2263 }
2264 
2265 ucl_object_t*
2266 ucl_object_pop_key (ucl_object_t *top, const char *key)
2267 {
2268 	return ucl_object_pop_keyl (top, key, strlen (key));
2269 }
2270 
2271 bool
2272 ucl_object_insert_key (ucl_object_t *top, ucl_object_t *elt,
2273 		const char *key, size_t keylen, bool copy_key)
2274 {
2275 	return ucl_object_insert_key_common (top, elt, key, keylen, copy_key, false, false);
2276 }
2277 
2278 bool
2279 ucl_object_insert_key_merged (ucl_object_t *top, ucl_object_t *elt,
2280 		const char *key, size_t keylen, bool copy_key)
2281 {
2282 	return ucl_object_insert_key_common (top, elt, key, keylen, copy_key, true, false);
2283 }
2284 
2285 bool
2286 ucl_object_replace_key (ucl_object_t *top, ucl_object_t *elt,
2287 		const char *key, size_t keylen, bool copy_key)
2288 {
2289 	return ucl_object_insert_key_common (top, elt, key, keylen, copy_key, false, true);
2290 }
2291 
2292 bool
2293 ucl_object_merge (ucl_object_t *top, ucl_object_t *elt, bool copy)
2294 {
2295 	ucl_object_t *cur = NULL, *cp = NULL, *found = NULL;
2296 	ucl_object_iter_t iter = NULL;
2297 
2298 	if (top == NULL || top->type != UCL_OBJECT || elt == NULL || elt->type != UCL_OBJECT) {
2299 		return false;
2300 	}
2301 
2302 	/* Mix two hashes */
2303 	while ((cur = (ucl_object_t*)ucl_hash_iterate (elt->value.ov, &iter))) {
2304 		if (copy) {
2305 			cp = ucl_object_copy (cur);
2306 		}
2307 		else {
2308 			cp = ucl_object_ref (cur);
2309 		}
2310 		found = __DECONST(ucl_object_t *, ucl_hash_search (top->value.ov, cp->key, cp->keylen));
2311 		if (found == NULL) {
2312 			/* The key does not exist */
2313 			top->value.ov = ucl_hash_insert_object (top->value.ov, cp, false);
2314 			top->len ++;
2315 		}
2316 		else {
2317 			/* The key already exists, replace it */
2318 			ucl_hash_replace (top->value.ov, found, cp);
2319 			ucl_object_unref (found);
2320 		}
2321 	}
2322 
2323 	return true;
2324 }
2325 
2326 const ucl_object_t *
2327 ucl_object_lookup_len (const ucl_object_t *obj, const char *key, size_t klen)
2328 {
2329 	const ucl_object_t *ret;
2330 	ucl_object_t srch;
2331 
2332 	if (obj == NULL || obj->type != UCL_OBJECT || key == NULL) {
2333 		return NULL;
2334 	}
2335 
2336 	srch.key = key;
2337 	srch.keylen = klen;
2338 	ret = ucl_hash_search_obj (obj->value.ov, &srch);
2339 
2340 	return ret;
2341 }
2342 
2343 const ucl_object_t *
2344 ucl_object_lookup (const ucl_object_t *obj, const char *key)
2345 {
2346 	if (key == NULL) {
2347 		return NULL;
2348 	}
2349 
2350 	return ucl_object_lookup_len (obj, key, strlen (key));
2351 }
2352 
2353 const ucl_object_t*
2354 ucl_object_lookup_any (const ucl_object_t *obj,
2355 		const char *key, ...)
2356 {
2357 	va_list ap;
2358 	const ucl_object_t *ret = NULL;
2359 	const char *nk = NULL;
2360 
2361 	if (obj == NULL || key == NULL) {
2362 		return NULL;
2363 	}
2364 
2365 	ret = ucl_object_lookup_len (obj, key, strlen (key));
2366 
2367 	if (ret == NULL) {
2368 		va_start (ap, key);
2369 
2370 		while (ret == NULL) {
2371 			nk = va_arg (ap, const char *);
2372 
2373 			if (nk == NULL) {
2374 				break;
2375 			}
2376 			else {
2377 				ret = ucl_object_lookup_len (obj, nk, strlen (nk));
2378 			}
2379 		}
2380 
2381 		va_end (ap);
2382 	}
2383 
2384 	return ret;
2385 }
2386 
2387 const ucl_object_t*
2388 ucl_object_iterate (const ucl_object_t *obj, ucl_object_iter_t *iter, bool expand_values)
2389 {
2390 	const ucl_object_t *elt = NULL;
2391 
2392 	if (obj == NULL || iter == NULL) {
2393 		return NULL;
2394 	}
2395 
2396 	if (expand_values) {
2397 		switch (obj->type) {
2398 		case UCL_OBJECT:
2399 			return (const ucl_object_t*)ucl_hash_iterate (obj->value.ov, iter);
2400 			break;
2401 		case UCL_ARRAY: {
2402 			unsigned int idx;
2403 			UCL_ARRAY_GET (vec, obj);
2404 			idx = (unsigned int)(uintptr_t)(*iter);
2405 
2406 			if (vec != NULL) {
2407 				while (idx < kv_size (*vec)) {
2408 					if ((elt = kv_A (*vec, idx)) != NULL) {
2409 						idx ++;
2410 						break;
2411 					}
2412 					idx ++;
2413 				}
2414 				*iter = (void *)(uintptr_t)idx;
2415 			}
2416 
2417 			return elt;
2418 			break;
2419 		}
2420 		default:
2421 			/* Go to linear iteration */
2422 			break;
2423 		}
2424 	}
2425 	/* Treat everything as a linear list */
2426 	elt = *iter;
2427 	if (elt == NULL) {
2428 		elt = obj;
2429 	}
2430 	else if (elt == obj) {
2431 		return NULL;
2432 	}
2433 	*iter = __DECONST (void *, elt->next ? elt->next : obj);
2434 	return elt;
2435 
2436 	/* Not reached */
2437 	return NULL;
2438 }
2439 
2440 const char safe_iter_magic[4] = {'u', 'i', 't', 'e'};
2441 struct ucl_object_safe_iter {
2442 	char magic[4]; /* safety check */
2443 	const ucl_object_t *impl_it; /* implicit object iteration */
2444 	ucl_object_iter_t expl_it; /* explicit iteration */
2445 };
2446 
2447 #define UCL_SAFE_ITER(ptr) (struct ucl_object_safe_iter *)(ptr)
2448 #define UCL_SAFE_ITER_CHECK(it) do { \
2449 	assert (it != NULL); \
2450 	assert (memcmp (it->magic, safe_iter_magic, sizeof (it->magic)) == 0); \
2451  } while (0)
2452 
2453 ucl_object_iter_t
2454 ucl_object_iterate_new (const ucl_object_t *obj)
2455 {
2456 	struct ucl_object_safe_iter *it;
2457 
2458 	it = UCL_ALLOC (sizeof (*it));
2459 	if (it != NULL) {
2460 		memcpy (it->magic, safe_iter_magic, sizeof (it->magic));
2461 		it->expl_it = NULL;
2462 		it->impl_it = obj;
2463 	}
2464 
2465 	return (ucl_object_iter_t)it;
2466 }
2467 
2468 
2469 ucl_object_iter_t
2470 ucl_object_iterate_reset (ucl_object_iter_t it, const ucl_object_t *obj)
2471 {
2472 	struct ucl_object_safe_iter *rit = UCL_SAFE_ITER (it);
2473 
2474 	UCL_SAFE_ITER_CHECK (rit);
2475 
2476 	rit->impl_it = obj;
2477 	rit->expl_it = NULL;
2478 
2479 	return it;
2480 }
2481 
2482 const ucl_object_t*
2483 ucl_object_iterate_safe (ucl_object_iter_t it, bool expand_values)
2484 {
2485 	struct ucl_object_safe_iter *rit = UCL_SAFE_ITER (it);
2486 	const ucl_object_t *ret = NULL;
2487 
2488 	UCL_SAFE_ITER_CHECK (rit);
2489 
2490 	if (rit->impl_it == NULL) {
2491 		return NULL;
2492 	}
2493 
2494 	if (rit->impl_it->type == UCL_OBJECT || rit->impl_it->type == UCL_ARRAY) {
2495 		ret = ucl_object_iterate (rit->impl_it, &rit->expl_it, true);
2496 
2497 		if (ret == NULL) {
2498 			/* Need to switch to another implicit object in chain */
2499 			rit->impl_it = rit->impl_it->next;
2500 			rit->expl_it = NULL;
2501 			return ucl_object_iterate_safe (it, expand_values);
2502 		}
2503 	}
2504 	else {
2505 		/* Just iterate over the implicit array */
2506 		ret = rit->impl_it;
2507 		rit->impl_it = rit->impl_it->next;
2508 		if (expand_values) {
2509 			/* We flatten objects if need to expand values */
2510 			if (ret->type == UCL_OBJECT || ret->type == UCL_ARRAY) {
2511 				return ucl_object_iterate_safe (it, expand_values);
2512 			}
2513 		}
2514 	}
2515 
2516 	return ret;
2517 }
2518 
2519 void
2520 ucl_object_iterate_free (ucl_object_iter_t it)
2521 {
2522 	struct ucl_object_safe_iter *rit = UCL_SAFE_ITER (it);
2523 
2524 	UCL_SAFE_ITER_CHECK (rit);
2525 
2526 	UCL_FREE (sizeof (*rit), it);
2527 }
2528 
2529 const ucl_object_t *
2530 ucl_object_lookup_path (const ucl_object_t *top, const char *path_in) {
2531 	return ucl_object_lookup_path_char (top, path_in, '.');
2532 }
2533 
2534 
2535 const ucl_object_t *
2536 ucl_object_lookup_path_char (const ucl_object_t *top, const char *path_in, const char sep) {
2537 	const ucl_object_t *o = NULL, *found;
2538 	const char *p, *c;
2539 	char *err_str;
2540 	unsigned index;
2541 
2542 	if (path_in == NULL || top == NULL) {
2543 		return NULL;
2544 	}
2545 
2546 	found = NULL;
2547 	p = path_in;
2548 
2549 	/* Skip leading dots */
2550 	while (*p == sep) {
2551 		p ++;
2552 	}
2553 
2554 	c = p;
2555 	while (*p != '\0') {
2556 		p ++;
2557 		if (*p == sep || *p == '\0') {
2558 			if (p > c) {
2559 				switch (top->type) {
2560 				case UCL_ARRAY:
2561 					/* Key should be an int */
2562 					index = strtoul (c, &err_str, 10);
2563 					if (err_str != NULL && (*err_str != sep && *err_str != '\0')) {
2564 						return NULL;
2565 					}
2566 					o = ucl_array_find_index (top, index);
2567 					break;
2568 				default:
2569 					o = ucl_object_lookup_len (top, c, p - c);
2570 					break;
2571 				}
2572 				if (o == NULL) {
2573 					return NULL;
2574 				}
2575 				top = o;
2576 			}
2577 			if (*p != '\0') {
2578 				c = p + 1;
2579 			}
2580 		}
2581 	}
2582 	found = o;
2583 
2584 	return found;
2585 }
2586 
2587 
2588 ucl_object_t *
2589 ucl_object_new (void)
2590 {
2591 	return ucl_object_typed_new (UCL_NULL);
2592 }
2593 
2594 ucl_object_t *
2595 ucl_object_typed_new (ucl_type_t type)
2596 {
2597 	return ucl_object_new_full (type, 0);
2598 }
2599 
2600 ucl_object_t *
2601 ucl_object_new_full (ucl_type_t type, unsigned priority)
2602 {
2603 	ucl_object_t *new;
2604 
2605 	if (type != UCL_USERDATA) {
2606 		new = UCL_ALLOC (sizeof (ucl_object_t));
2607 		if (new != NULL) {
2608 			memset (new, 0, sizeof (ucl_object_t));
2609 			new->ref = 1;
2610 			new->type = (type <= UCL_NULL ? type : UCL_NULL);
2611 			new->next = NULL;
2612 			new->prev = new;
2613 			ucl_object_set_priority (new, priority);
2614 
2615 			if (type == UCL_ARRAY) {
2616 				new->value.av = UCL_ALLOC (sizeof (ucl_array_t));
2617 				if (new->value.av) {
2618 					memset (new->value.av, 0, sizeof (ucl_array_t));
2619 					UCL_ARRAY_GET (vec, new);
2620 
2621 					/* Preallocate some space for arrays */
2622 					kv_resize (ucl_object_t *, *vec, 8);
2623 				}
2624 			}
2625 		}
2626 	}
2627 	else {
2628 		new = ucl_object_new_userdata (NULL, NULL, NULL);
2629 		ucl_object_set_priority (new, priority);
2630 	}
2631 
2632 	return new;
2633 }
2634 
2635 ucl_object_t*
2636 ucl_object_new_userdata (ucl_userdata_dtor dtor,
2637 		ucl_userdata_emitter emitter,
2638 		void *ptr)
2639 {
2640 	struct ucl_object_userdata *new;
2641 	size_t nsize = sizeof (*new);
2642 
2643 	new = UCL_ALLOC (nsize);
2644 	if (new != NULL) {
2645 		memset (new, 0, nsize);
2646 		new->obj.ref = 1;
2647 		new->obj.type = UCL_USERDATA;
2648 		new->obj.next = NULL;
2649 		new->obj.prev = (ucl_object_t *)new;
2650 		new->dtor = dtor;
2651 		new->emitter = emitter;
2652 		new->obj.value.ud = ptr;
2653 	}
2654 
2655 	return (ucl_object_t *)new;
2656 }
2657 
2658 ucl_type_t
2659 ucl_object_type (const ucl_object_t *obj)
2660 {
2661 	if (obj == NULL) {
2662 		return UCL_NULL;
2663 	}
2664 
2665 	return obj->type;
2666 }
2667 
2668 ucl_object_t*
2669 ucl_object_fromstring (const char *str)
2670 {
2671 	return ucl_object_fromstring_common (str, 0, UCL_STRING_ESCAPE);
2672 }
2673 
2674 ucl_object_t *
2675 ucl_object_fromlstring (const char *str, size_t len)
2676 {
2677 	return ucl_object_fromstring_common (str, len, UCL_STRING_ESCAPE);
2678 }
2679 
2680 ucl_object_t *
2681 ucl_object_fromint (int64_t iv)
2682 {
2683 	ucl_object_t *obj;
2684 
2685 	obj = ucl_object_new ();
2686 	if (obj != NULL) {
2687 		obj->type = UCL_INT;
2688 		obj->value.iv = iv;
2689 	}
2690 
2691 	return obj;
2692 }
2693 
2694 ucl_object_t *
2695 ucl_object_fromdouble (double dv)
2696 {
2697 	ucl_object_t *obj;
2698 
2699 	obj = ucl_object_new ();
2700 	if (obj != NULL) {
2701 		obj->type = UCL_FLOAT;
2702 		obj->value.dv = dv;
2703 	}
2704 
2705 	return obj;
2706 }
2707 
2708 ucl_object_t*
2709 ucl_object_frombool (bool bv)
2710 {
2711 	ucl_object_t *obj;
2712 
2713 	obj = ucl_object_new ();
2714 	if (obj != NULL) {
2715 		obj->type = UCL_BOOLEAN;
2716 		obj->value.iv = bv;
2717 	}
2718 
2719 	return obj;
2720 }
2721 
2722 bool
2723 ucl_array_append (ucl_object_t *top, ucl_object_t *elt)
2724 {
2725 	UCL_ARRAY_GET (vec, top);
2726 
2727 	if (elt == NULL || top == NULL) {
2728 		return false;
2729 	}
2730 
2731 	if (vec == NULL) {
2732 		vec = UCL_ALLOC (sizeof (*vec));
2733 
2734 		if (vec == NULL) {
2735 			return false;
2736 		}
2737 
2738 		kv_init (*vec);
2739 		top->value.av = (void *)vec;
2740 	}
2741 
2742 	kv_push (ucl_object_t *, *vec, elt);
2743 
2744 	top->len ++;
2745 
2746 	return true;
2747 }
2748 
2749 bool
2750 ucl_array_prepend (ucl_object_t *top, ucl_object_t *elt)
2751 {
2752 	UCL_ARRAY_GET (vec, top);
2753 
2754 	if (elt == NULL || top == NULL) {
2755 		return false;
2756 	}
2757 
2758 	if (vec == NULL) {
2759 		vec = UCL_ALLOC (sizeof (*vec));
2760 		kv_init (*vec);
2761 		top->value.av = (void *)vec;
2762 		kv_push (ucl_object_t *, *vec, elt);
2763 	}
2764 	else {
2765 		/* Slow O(n) algorithm */
2766 		kv_prepend (ucl_object_t *, *vec, elt);
2767 	}
2768 
2769 	top->len ++;
2770 
2771 	return true;
2772 }
2773 
2774 bool
2775 ucl_array_merge (ucl_object_t *top, ucl_object_t *elt, bool copy)
2776 {
2777 	unsigned i;
2778 	ucl_object_t *cp = NULL;
2779 	ucl_object_t **obj;
2780 
2781 	if (elt == NULL || top == NULL || top->type != UCL_ARRAY || elt->type != UCL_ARRAY) {
2782 		return false;
2783 	}
2784 
2785 	if (copy) {
2786 		cp = ucl_object_copy (elt);
2787 	}
2788 	else {
2789 		cp = ucl_object_ref (elt);
2790 	}
2791 
2792 	UCL_ARRAY_GET (v1, top);
2793 	UCL_ARRAY_GET (v2, cp);
2794 
2795 	if (v1 && v2) {
2796 		kv_concat (ucl_object_t *, *v1, *v2);
2797 
2798 		for (i = v2->n; i < v1->n; i ++) {
2799 			obj = &kv_A (*v1, i);
2800 			if (*obj == NULL) {
2801 				continue;
2802 			}
2803 			top->len ++;
2804 		}
2805 	}
2806 
2807 	return true;
2808 }
2809 
2810 ucl_object_t *
2811 ucl_array_delete (ucl_object_t *top, ucl_object_t *elt)
2812 {
2813 	UCL_ARRAY_GET (vec, top);
2814 	ucl_object_t *ret = NULL;
2815 	unsigned i;
2816 
2817 	if (vec == NULL) {
2818 		return NULL;
2819 	}
2820 
2821 	for (i = 0; i < vec->n; i ++) {
2822 		if (kv_A (*vec, i) == elt) {
2823 			kv_del (ucl_object_t *, *vec, i);
2824 			ret = elt;
2825 			top->len --;
2826 			break;
2827 		}
2828 	}
2829 
2830 	return ret;
2831 }
2832 
2833 const ucl_object_t *
2834 ucl_array_head (const ucl_object_t *top)
2835 {
2836 	UCL_ARRAY_GET (vec, top);
2837 
2838 	if (vec == NULL || top == NULL || top->type != UCL_ARRAY ||
2839 			top->value.av == NULL) {
2840 		return NULL;
2841 	}
2842 
2843 	return (vec->n > 0 ? vec->a[0] : NULL);
2844 }
2845 
2846 const ucl_object_t *
2847 ucl_array_tail (const ucl_object_t *top)
2848 {
2849 	UCL_ARRAY_GET (vec, top);
2850 
2851 	if (top == NULL || top->type != UCL_ARRAY || top->value.av == NULL) {
2852 		return NULL;
2853 	}
2854 
2855 	return (vec->n > 0 ? vec->a[vec->n - 1] : NULL);
2856 }
2857 
2858 ucl_object_t *
2859 ucl_array_pop_last (ucl_object_t *top)
2860 {
2861 	UCL_ARRAY_GET (vec, top);
2862 	ucl_object_t **obj, *ret = NULL;
2863 
2864 	if (vec != NULL && vec->n > 0) {
2865 		obj = &kv_A (*vec, vec->n - 1);
2866 		ret = *obj;
2867 		kv_del (ucl_object_t *, *vec, vec->n - 1);
2868 		top->len --;
2869 	}
2870 
2871 	return ret;
2872 }
2873 
2874 ucl_object_t *
2875 ucl_array_pop_first (ucl_object_t *top)
2876 {
2877 	UCL_ARRAY_GET (vec, top);
2878 	ucl_object_t **obj, *ret = NULL;
2879 
2880 	if (vec != NULL && vec->n > 0) {
2881 		obj = &kv_A (*vec, 0);
2882 		ret = *obj;
2883 		kv_del (ucl_object_t *, *vec, 0);
2884 		top->len --;
2885 	}
2886 
2887 	return ret;
2888 }
2889 
2890 const ucl_object_t *
2891 ucl_array_find_index (const ucl_object_t *top, unsigned int index)
2892 {
2893 	UCL_ARRAY_GET (vec, top);
2894 
2895 	if (vec != NULL && vec->n > 0 && index < vec->n) {
2896 		return kv_A (*vec, index);
2897 	}
2898 
2899 	return NULL;
2900 }
2901 
2902 unsigned int
2903 ucl_array_index_of (ucl_object_t *top, ucl_object_t *elt)
2904 {
2905 	UCL_ARRAY_GET (vec, top);
2906 	unsigned i;
2907 
2908 	if (vec == NULL) {
2909 		return (unsigned int)(-1);
2910 	}
2911 
2912 	for (i = 0; i < vec->n; i ++) {
2913 		if (kv_A (*vec, i) == elt) {
2914 			return i;
2915 		}
2916 	}
2917 
2918 	return (unsigned int)(-1);
2919 }
2920 
2921 ucl_object_t *
2922 ucl_array_replace_index (ucl_object_t *top, ucl_object_t *elt,
2923 	unsigned int index)
2924 {
2925 	UCL_ARRAY_GET (vec, top);
2926 	ucl_object_t *ret = NULL;
2927 
2928 	if (vec != NULL && vec->n > 0 && index < vec->n) {
2929 		ret = kv_A (*vec, index);
2930 		kv_A (*vec, index) = elt;
2931 	}
2932 
2933 	return ret;
2934 }
2935 
2936 ucl_object_t *
2937 ucl_elt_append (ucl_object_t *head, ucl_object_t *elt)
2938 {
2939 
2940 	if (head == NULL) {
2941 		elt->next = NULL;
2942 		elt->prev = elt;
2943 		head = elt;
2944 	}
2945 	else {
2946 		elt->prev = head->prev;
2947 		head->prev->next = elt;
2948 		head->prev = elt;
2949 		elt->next = NULL;
2950 	}
2951 
2952 	return head;
2953 }
2954 
2955 bool
2956 ucl_object_todouble_safe (const ucl_object_t *obj, double *target)
2957 {
2958 	if (obj == NULL || target == NULL) {
2959 		return false;
2960 	}
2961 	switch (obj->type) {
2962 	case UCL_INT:
2963 		*target = obj->value.iv; /* Probaly could cause overflow */
2964 		break;
2965 	case UCL_FLOAT:
2966 	case UCL_TIME:
2967 		*target = obj->value.dv;
2968 		break;
2969 	default:
2970 		return false;
2971 	}
2972 
2973 	return true;
2974 }
2975 
2976 double
2977 ucl_object_todouble (const ucl_object_t *obj)
2978 {
2979 	double result = 0.;
2980 
2981 	ucl_object_todouble_safe (obj, &result);
2982 	return result;
2983 }
2984 
2985 bool
2986 ucl_object_toint_safe (const ucl_object_t *obj, int64_t *target)
2987 {
2988 	if (obj == NULL || target == NULL) {
2989 		return false;
2990 	}
2991 	switch (obj->type) {
2992 	case UCL_INT:
2993 		*target = obj->value.iv;
2994 		break;
2995 	case UCL_FLOAT:
2996 	case UCL_TIME:
2997 		*target = obj->value.dv; /* Loosing of decimal points */
2998 		break;
2999 	default:
3000 		return false;
3001 	}
3002 
3003 	return true;
3004 }
3005 
3006 int64_t
3007 ucl_object_toint (const ucl_object_t *obj)
3008 {
3009 	int64_t result = 0;
3010 
3011 	ucl_object_toint_safe (obj, &result);
3012 	return result;
3013 }
3014 
3015 bool
3016 ucl_object_toboolean_safe (const ucl_object_t *obj, bool *target)
3017 {
3018 	if (obj == NULL || target == NULL) {
3019 		return false;
3020 	}
3021 	switch (obj->type) {
3022 	case UCL_BOOLEAN:
3023 		*target = (obj->value.iv == true);
3024 		break;
3025 	default:
3026 		return false;
3027 	}
3028 
3029 	return true;
3030 }
3031 
3032 bool
3033 ucl_object_toboolean (const ucl_object_t *obj)
3034 {
3035 	bool result = false;
3036 
3037 	ucl_object_toboolean_safe (obj, &result);
3038 	return result;
3039 }
3040 
3041 bool
3042 ucl_object_tostring_safe (const ucl_object_t *obj, const char **target)
3043 {
3044 	if (obj == NULL || target == NULL) {
3045 		return false;
3046 	}
3047 
3048 	switch (obj->type) {
3049 	case UCL_STRING:
3050 		if (!(obj->flags & UCL_OBJECT_BINARY)) {
3051 			*target = ucl_copy_value_trash (obj);
3052 		}
3053 		break;
3054 	default:
3055 		return false;
3056 	}
3057 
3058 	return true;
3059 }
3060 
3061 const char *
3062 ucl_object_tostring (const ucl_object_t *obj)
3063 {
3064 	const char *result = NULL;
3065 
3066 	ucl_object_tostring_safe (obj, &result);
3067 	return result;
3068 }
3069 
3070 const char *
3071 ucl_object_tostring_forced (const ucl_object_t *obj)
3072 {
3073 	/* TODO: For binary strings we might encode string here */
3074 	if (!(obj->flags & UCL_OBJECT_BINARY)) {
3075 		return ucl_copy_value_trash (obj);
3076 	}
3077 
3078 	return NULL;
3079 }
3080 
3081 bool
3082 ucl_object_tolstring_safe (const ucl_object_t *obj, const char **target, size_t *tlen)
3083 {
3084 	if (obj == NULL || target == NULL) {
3085 		return false;
3086 	}
3087 	switch (obj->type) {
3088 	case UCL_STRING:
3089 		*target = obj->value.sv;
3090 		if (tlen != NULL) {
3091 			*tlen = obj->len;
3092 		}
3093 		break;
3094 	default:
3095 		return false;
3096 	}
3097 
3098 	return true;
3099 }
3100 
3101 const char *
3102 ucl_object_tolstring (const ucl_object_t *obj, size_t *tlen)
3103 {
3104 	const char *result = NULL;
3105 
3106 	ucl_object_tolstring_safe (obj, &result, tlen);
3107 	return result;
3108 }
3109 
3110 const char *
3111 ucl_object_key (const ucl_object_t *obj)
3112 {
3113 	return ucl_copy_key_trash (obj);
3114 }
3115 
3116 const char *
3117 ucl_object_keyl (const ucl_object_t *obj, size_t *len)
3118 {
3119 	if (len == NULL || obj == NULL) {
3120 		return NULL;
3121 	}
3122 	*len = obj->keylen;
3123 	return obj->key;
3124 }
3125 
3126 ucl_object_t *
3127 ucl_object_ref (const ucl_object_t *obj)
3128 {
3129 	ucl_object_t *res = NULL;
3130 
3131 	if (obj != NULL) {
3132 		if (obj->flags & UCL_OBJECT_EPHEMERAL) {
3133 			/*
3134 			 * Use deep copy for ephemeral objects, note that its refcount
3135 			 * is NOT increased, since ephemeral objects does not need refcount
3136 			 * at all
3137 			 */
3138 			res = ucl_object_copy (obj);
3139 		}
3140 		else {
3141 			res = __DECONST (ucl_object_t *, obj);
3142 #ifdef HAVE_ATOMIC_BUILTINS
3143 			(void)__sync_add_and_fetch (&res->ref, 1);
3144 #else
3145 			res->ref ++;
3146 #endif
3147 		}
3148 	}
3149 	return res;
3150 }
3151 
3152 static ucl_object_t *
3153 ucl_object_copy_internal (const ucl_object_t *other, bool allow_array)
3154 {
3155 
3156 	ucl_object_t *new;
3157 	ucl_object_iter_t it = NULL;
3158 	const ucl_object_t *cur;
3159 
3160 	new = malloc (sizeof (*new));
3161 
3162 	if (new != NULL) {
3163 		memcpy (new, other, sizeof (*new));
3164 		if (other->flags & UCL_OBJECT_EPHEMERAL) {
3165 			/* Copied object is always non ephemeral */
3166 			new->flags &= ~UCL_OBJECT_EPHEMERAL;
3167 		}
3168 		new->ref = 1;
3169 		/* Unlink from others */
3170 		new->next = NULL;
3171 		new->prev = new;
3172 
3173 		/* deep copy of values stored */
3174 		if (other->trash_stack[UCL_TRASH_KEY] != NULL) {
3175 			new->trash_stack[UCL_TRASH_KEY] =
3176 					strdup (other->trash_stack[UCL_TRASH_KEY]);
3177 			if (other->key == (const char *)other->trash_stack[UCL_TRASH_KEY]) {
3178 				new->key = new->trash_stack[UCL_TRASH_KEY];
3179 			}
3180 		}
3181 		if (other->trash_stack[UCL_TRASH_VALUE] != NULL) {
3182 			new->trash_stack[UCL_TRASH_VALUE] =
3183 					strdup (other->trash_stack[UCL_TRASH_VALUE]);
3184 			if (new->type == UCL_STRING) {
3185 				new->value.sv = new->trash_stack[UCL_TRASH_VALUE];
3186 			}
3187 		}
3188 
3189 		if (other->type == UCL_ARRAY || other->type == UCL_OBJECT) {
3190 			/* reset old value */
3191 			memset (&new->value, 0, sizeof (new->value));
3192 
3193 			while ((cur = ucl_object_iterate (other, &it, true)) != NULL) {
3194 				if (other->type == UCL_ARRAY) {
3195 					ucl_array_append (new, ucl_object_copy_internal (cur, false));
3196 				}
3197 				else {
3198 					ucl_object_t *cp = ucl_object_copy_internal (cur, true);
3199 					if (cp != NULL) {
3200 						ucl_object_insert_key (new, cp, cp->key, cp->keylen,
3201 								false);
3202 					}
3203 				}
3204 			}
3205 		}
3206 		else if (allow_array && other->next != NULL) {
3207 			LL_FOREACH (other->next, cur) {
3208 				ucl_object_t *cp = ucl_object_copy_internal (cur, false);
3209 				if (cp != NULL) {
3210 					DL_APPEND (new, cp);
3211 				}
3212 			}
3213 		}
3214 	}
3215 
3216 	return new;
3217 }
3218 
3219 ucl_object_t *
3220 ucl_object_copy (const ucl_object_t *other)
3221 {
3222 	return ucl_object_copy_internal (other, true);
3223 }
3224 
3225 void
3226 ucl_object_unref (ucl_object_t *obj)
3227 {
3228 	if (obj != NULL) {
3229 #ifdef HAVE_ATOMIC_BUILTINS
3230 		unsigned int rc = __sync_sub_and_fetch (&obj->ref, 1);
3231 		if (rc == 0) {
3232 #else
3233 		if (--obj->ref == 0) {
3234 #endif
3235 			ucl_object_free_internal (obj, true, ucl_object_dtor_unref);
3236 		}
3237 	}
3238 }
3239 
3240 int
3241 ucl_object_compare (const ucl_object_t *o1, const ucl_object_t *o2)
3242 {
3243 	const ucl_object_t *it1, *it2;
3244 	ucl_object_iter_t iter = NULL;
3245 	int ret = 0;
3246 
3247 	if (o1->type != o2->type) {
3248 		return (o1->type) - (o2->type);
3249 	}
3250 
3251 	switch (o1->type) {
3252 	case UCL_STRING:
3253 		if (o1->len == o2->len && o1->len > 0) {
3254 			ret = strcmp (ucl_object_tostring(o1), ucl_object_tostring(o2));
3255 		}
3256 		else {
3257 			ret = o1->len - o2->len;
3258 		}
3259 		break;
3260 	case UCL_FLOAT:
3261 	case UCL_INT:
3262 	case UCL_TIME:
3263 		ret = ucl_object_todouble (o1) - ucl_object_todouble (o2);
3264 		break;
3265 	case UCL_BOOLEAN:
3266 		ret = ucl_object_toboolean (o1) - ucl_object_toboolean (o2);
3267 		break;
3268 	case UCL_ARRAY:
3269 		if (o1->len == o2->len && o1->len > 0) {
3270 			UCL_ARRAY_GET (vec1, o1);
3271 			UCL_ARRAY_GET (vec2, o2);
3272 			unsigned i;
3273 
3274 			/* Compare all elements in both arrays */
3275 			for (i = 0; i < vec1->n; i ++) {
3276 				it1 = kv_A (*vec1, i);
3277 				it2 = kv_A (*vec2, i);
3278 
3279 				if (it1 == NULL && it2 != NULL) {
3280 					return -1;
3281 				}
3282 				else if (it2 == NULL && it1 != NULL) {
3283 					return 1;
3284 				}
3285 				else if (it1 != NULL && it2 != NULL) {
3286 					ret = ucl_object_compare (it1, it2);
3287 					if (ret != 0) {
3288 						break;
3289 					}
3290 				}
3291 			}
3292 		}
3293 		else {
3294 			ret = o1->len - o2->len;
3295 		}
3296 		break;
3297 	case UCL_OBJECT:
3298 		if (o1->len == o2->len && o1->len > 0) {
3299 			while ((it1 = ucl_object_iterate (o1, &iter, true)) != NULL) {
3300 				it2 = ucl_object_lookup (o2, ucl_object_key (it1));
3301 				if (it2 == NULL) {
3302 					ret = 1;
3303 					break;
3304 				}
3305 				ret = ucl_object_compare (it1, it2);
3306 				if (ret != 0) {
3307 					break;
3308 				}
3309 			}
3310 		}
3311 		else {
3312 			ret = o1->len - o2->len;
3313 		}
3314 		break;
3315 	default:
3316 		ret = 0;
3317 		break;
3318 	}
3319 
3320 	return ret;
3321 }
3322 
3323 int
3324 ucl_object_compare_qsort (const ucl_object_t **o1,
3325 		const ucl_object_t **o2)
3326 {
3327 	return ucl_object_compare (*o1, *o2);
3328 }
3329 
3330 void
3331 ucl_object_array_sort (ucl_object_t *ar,
3332 		int (*cmp)(const ucl_object_t **o1, const ucl_object_t **o2))
3333 {
3334 	UCL_ARRAY_GET (vec, ar);
3335 
3336 	if (cmp == NULL || ar == NULL || ar->type != UCL_ARRAY) {
3337 		return;
3338 	}
3339 
3340 	qsort (vec->a, vec->n, sizeof (ucl_object_t *),
3341 			(int (*)(const void *, const void *))cmp);
3342 }
3343 
3344 #define PRIOBITS 4
3345 
3346 unsigned int
3347 ucl_object_get_priority (const ucl_object_t *obj)
3348 {
3349 	if (obj == NULL) {
3350 		return 0;
3351 	}
3352 
3353 	return (obj->flags >> ((sizeof (obj->flags) * NBBY) - PRIOBITS));
3354 }
3355 
3356 void
3357 ucl_object_set_priority (ucl_object_t *obj,
3358 		unsigned int priority)
3359 {
3360 	if (obj != NULL) {
3361 		priority &= (0x1 << PRIOBITS) - 1;
3362 		priority <<= ((sizeof (obj->flags) * NBBY) - PRIOBITS);
3363 		priority |= obj->flags & ((1 << ((sizeof (obj->flags) * NBBY) -
3364 				PRIOBITS)) - 1);
3365 		obj->flags = priority;
3366 	}
3367 }
3368 
3369 bool
3370 ucl_object_string_to_type (const char *input, ucl_type_t *res)
3371 {
3372 	if (strcasecmp (input, "object") == 0) {
3373 		*res = UCL_OBJECT;
3374 	}
3375 	else if (strcasecmp (input, "array") == 0) {
3376 		*res = UCL_ARRAY;
3377 	}
3378 	else if (strcasecmp (input, "integer") == 0) {
3379 		*res = UCL_INT;
3380 	}
3381 	else if (strcasecmp (input, "number") == 0) {
3382 		*res = UCL_FLOAT;
3383 	}
3384 	else if (strcasecmp (input, "string") == 0) {
3385 		*res = UCL_STRING;
3386 	}
3387 	else if (strcasecmp (input, "boolean") == 0) {
3388 		*res = UCL_BOOLEAN;
3389 	}
3390 	else if (strcasecmp (input, "null") == 0) {
3391 		*res = UCL_NULL;
3392 	}
3393 	else if (strcasecmp (input, "userdata") == 0) {
3394 		*res = UCL_USERDATA;
3395 	}
3396 	else {
3397 		return false;
3398 	}
3399 
3400 	return true;
3401 }
3402 
3403 const char *
3404 ucl_object_type_to_string (ucl_type_t type)
3405 {
3406 	const char *res = "unknown";
3407 
3408 	switch (type) {
3409 	case UCL_OBJECT:
3410 		res = "object";
3411 		break;
3412 	case UCL_ARRAY:
3413 		res = "array";
3414 		break;
3415 	case UCL_INT:
3416 		res = "integer";
3417 		break;
3418 	case UCL_FLOAT:
3419 	case UCL_TIME:
3420 		res = "number";
3421 		break;
3422 	case UCL_STRING:
3423 		res = "string";
3424 		break;
3425 	case UCL_BOOLEAN:
3426 		res = "boolean";
3427 		break;
3428 	case UCL_USERDATA:
3429 		res = "userdata";
3430 		break;
3431 	case UCL_NULL:
3432 		res = "null";
3433 		break;
3434 	}
3435 
3436 	return res;
3437 }
3438 
3439 const ucl_object_t *
3440 ucl_parser_get_comments (struct ucl_parser *parser)
3441 {
3442 	if (parser && parser->comments) {
3443 		return parser->comments;
3444 	}
3445 
3446 	return NULL;
3447 }
3448 
3449 const ucl_object_t *
3450 ucl_comments_find (const ucl_object_t *comments,
3451 		const ucl_object_t *srch)
3452 {
3453 	if (comments && srch) {
3454 		return ucl_object_lookup_len (comments, (const char *)&srch,
3455 				sizeof (void *));
3456 	}
3457 
3458 	return NULL;
3459 }
3460 
3461 bool
3462 ucl_comments_move (ucl_object_t *comments,
3463 		const ucl_object_t *from, const ucl_object_t *to)
3464 {
3465 	const ucl_object_t *found;
3466 	ucl_object_t *obj;
3467 
3468 	if (comments && from && to) {
3469 		found = ucl_object_lookup_len (comments,
3470 				(const char *)&from, sizeof (void *));
3471 
3472 		if (found) {
3473 			/* Replace key */
3474 			obj = ucl_object_ref (found);
3475 			ucl_object_delete_keyl (comments, (const char *)&from,
3476 					sizeof (void *));
3477 			ucl_object_insert_key (comments, obj, (const char *)&to,
3478 					sizeof (void *), true);
3479 
3480 			return true;
3481 		}
3482 	}
3483 
3484 	return false;
3485 }
3486 
3487 void
3488 ucl_comments_add (ucl_object_t *comments, const ucl_object_t *obj,
3489 		const char *comment)
3490 {
3491 	if (comments && obj && comment) {
3492 		ucl_object_insert_key (comments, ucl_object_fromstring (comment),
3493 				(const char *)&obj, sizeof (void *), true);
3494 	}
3495 }
3496