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