Deleted Added
full compact
ucl_util.c (262398) ucl_util.c (262975)
1/* Copyright (c) 2013, Vsevolod Stakhov
2 * All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above copyright

--- 21 unchanged lines hidden (view full) ---

30#ifdef HAVE_OPENSSL
31#include <openssl/err.h>
32#include <openssl/sha.h>
33#include <openssl/rsa.h>
34#include <openssl/ssl.h>
35#include <openssl/evp.h>
36#endif
37
1/* Copyright (c) 2013, Vsevolod Stakhov
2 * All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above copyright

--- 21 unchanged lines hidden (view full) ---

30#ifdef HAVE_OPENSSL
31#include <openssl/err.h>
32#include <openssl/sha.h>
33#include <openssl/rsa.h>
34#include <openssl/ssl.h>
35#include <openssl/evp.h>
36#endif
37
38#ifdef _WIN32
39#include <windows.h>
40
41#define PROT_READ 1
42#define PROT_WRITE 2
43#define PROT_READWRITE 3
44#define MAP_SHARED 1
45#define MAP_PRIVATE 2
46#define MAP_FAILED ((void *) -1)
47
48static void *mmap(char *addr, size_t length, int prot, int access, int fd, off_t offset)
49{
50 void *map = NULL;
51 HANDLE handle = INVALID_HANDLE_VALUE;
52
53 switch (prot) {
54 default:
55 case PROT_READ:
56 {
57 handle = CreateFileMapping((HANDLE) _get_osfhandle(fd), 0, PAGE_READONLY, 0, length, 0);
58 if (!handle) break;
59 map = (void *) MapViewOfFile(handle, FILE_MAP_READ, 0, 0, length);
60 CloseHandle(handle);
61 break;
62 }
63 case PROT_WRITE:
64 {
65 handle = CreateFileMapping((HANDLE) _get_osfhandle(fd), 0, PAGE_READWRITE, 0, length, 0);
66 if (!handle) break;
67 map = (void *) MapViewOfFile(handle, FILE_MAP_WRITE, 0, 0, length);
68 CloseHandle(handle);
69 break;
70 }
71 case PROT_READWRITE:
72 {
73 handle = CreateFileMapping((HANDLE) _get_osfhandle(fd), 0, PAGE_READWRITE, 0, length, 0);
74 if (!handle) break;
75 map = (void *) MapViewOfFile(handle, FILE_MAP_ALL_ACCESS, 0, 0, length);
76 CloseHandle(handle);
77 break;
78 }
79 }
80 if (map == (void *) NULL) {
81 return (void *) MAP_FAILED;
82 }
83 return (void *) ((char *) map + offset);
84}
85
86static int munmap(void *map,size_t length)
87{
88 if (!UnmapViewOfFile(map)) {
89 return(-1);
90 }
91 return(0);
92}
93
94static char* realpath(const char *path, char *resolved_path) {
95 char *p;
96 char tmp[MAX_PATH + 1];
97 strncpy(tmp, path, sizeof(tmp)-1);
98 p = tmp;
99 while(*p) {
100 if (*p == '/') *p = '\\';
101 p++;
102 }
103 return _fullpath(resolved_path, tmp, MAX_PATH);
104}
105#endif
106
38/**
39 * @file rcl_util.c
40 * Utilities for rcl parsing
41 */
42
43
44static void
45ucl_object_free_internal (ucl_object_t *obj, bool allow_rec)

--- 126 unchanged lines hidden (view full) ---

172 }
173 len --;
174 }
175 *t = '\0';
176
177 return (t - str);
178}
179
107/**
108 * @file rcl_util.c
109 * Utilities for rcl parsing
110 */
111
112
113static void
114ucl_object_free_internal (ucl_object_t *obj, bool allow_rec)

--- 126 unchanged lines hidden (view full) ---

241 }
242 len --;
243 }
244 *t = '\0';
245
246 return (t - str);
247}
248
180char *
249UCL_EXTERN char *
181ucl_copy_key_trash (ucl_object_t *obj)
182{
183 if (obj->trash_stack[UCL_TRASH_KEY] == NULL && obj->key != NULL) {
184 obj->trash_stack[UCL_TRASH_KEY] = malloc (obj->keylen + 1);
185 if (obj->trash_stack[UCL_TRASH_KEY] != NULL) {
186 memcpy (obj->trash_stack[UCL_TRASH_KEY], obj->key, obj->keylen);
187 obj->trash_stack[UCL_TRASH_KEY][obj->keylen] = '\0';
188 }
189 obj->key = obj->trash_stack[UCL_TRASH_KEY];
190 obj->flags |= UCL_OBJECT_ALLOCATED_KEY;
191 }
192
193 return obj->trash_stack[UCL_TRASH_KEY];
194}
195
250ucl_copy_key_trash (ucl_object_t *obj)
251{
252 if (obj->trash_stack[UCL_TRASH_KEY] == NULL && obj->key != NULL) {
253 obj->trash_stack[UCL_TRASH_KEY] = malloc (obj->keylen + 1);
254 if (obj->trash_stack[UCL_TRASH_KEY] != NULL) {
255 memcpy (obj->trash_stack[UCL_TRASH_KEY], obj->key, obj->keylen);
256 obj->trash_stack[UCL_TRASH_KEY][obj->keylen] = '\0';
257 }
258 obj->key = obj->trash_stack[UCL_TRASH_KEY];
259 obj->flags |= UCL_OBJECT_ALLOCATED_KEY;
260 }
261
262 return obj->trash_stack[UCL_TRASH_KEY];
263}
264
196char *
265UCL_EXTERN char *
197ucl_copy_value_trash (ucl_object_t *obj)
198{
199 if (obj->trash_stack[UCL_TRASH_VALUE] == NULL) {
200 if (obj->type == UCL_STRING) {
201 /* Special case for strings */
202 obj->trash_stack[UCL_TRASH_VALUE] = malloc (obj->len + 1);
203 if (obj->trash_stack[UCL_TRASH_VALUE] != NULL) {
204 memcpy (obj->trash_stack[UCL_TRASH_VALUE], obj->value.sv, obj->len);

--- 6 unchanged lines hidden (view full) ---

211 obj->trash_stack[UCL_TRASH_VALUE] = ucl_object_emit_single_json (obj);
212 obj->len = strlen (obj->trash_stack[UCL_TRASH_VALUE]);
213 }
214 obj->flags |= UCL_OBJECT_ALLOCATED_VALUE;
215 }
216 return obj->trash_stack[UCL_TRASH_VALUE];
217}
218
266ucl_copy_value_trash (ucl_object_t *obj)
267{
268 if (obj->trash_stack[UCL_TRASH_VALUE] == NULL) {
269 if (obj->type == UCL_STRING) {
270 /* Special case for strings */
271 obj->trash_stack[UCL_TRASH_VALUE] = malloc (obj->len + 1);
272 if (obj->trash_stack[UCL_TRASH_VALUE] != NULL) {
273 memcpy (obj->trash_stack[UCL_TRASH_VALUE], obj->value.sv, obj->len);

--- 6 unchanged lines hidden (view full) ---

280 obj->trash_stack[UCL_TRASH_VALUE] = ucl_object_emit_single_json (obj);
281 obj->len = strlen (obj->trash_stack[UCL_TRASH_VALUE]);
282 }
283 obj->flags |= UCL_OBJECT_ALLOCATED_VALUE;
284 }
285 return obj->trash_stack[UCL_TRASH_VALUE];
286}
287
219ucl_object_t*
288UCL_EXTERN ucl_object_t*
220ucl_parser_get_object (struct ucl_parser *parser)
221{
222 if (parser->state != UCL_STATE_ERROR && parser->top_obj != NULL) {
223 return ucl_object_ref (parser->top_obj);
224 }
225
226 return NULL;
227}
228
289ucl_parser_get_object (struct ucl_parser *parser)
290{
291 if (parser->state != UCL_STATE_ERROR && parser->top_obj != NULL) {
292 return ucl_object_ref (parser->top_obj);
293 }
294
295 return NULL;
296}
297
229void
298UCL_EXTERN void
230ucl_parser_free (struct ucl_parser *parser)
231{
232 struct ucl_stack *stack, *stmp;
233 struct ucl_macro *macro, *mtmp;
234 struct ucl_chunk *chunk, *ctmp;
235 struct ucl_pubkey *key, *ktmp;
236 struct ucl_variable *var, *vtmp;
237

--- 23 unchanged lines hidden (view full) ---

261
262 if (parser->err != NULL) {
263 utstring_free(parser->err);
264 }
265
266 UCL_FREE (sizeof (struct ucl_parser), parser);
267}
268
299ucl_parser_free (struct ucl_parser *parser)
300{
301 struct ucl_stack *stack, *stmp;
302 struct ucl_macro *macro, *mtmp;
303 struct ucl_chunk *chunk, *ctmp;
304 struct ucl_pubkey *key, *ktmp;
305 struct ucl_variable *var, *vtmp;
306

--- 23 unchanged lines hidden (view full) ---

330
331 if (parser->err != NULL) {
332 utstring_free(parser->err);
333 }
334
335 UCL_FREE (sizeof (struct ucl_parser), parser);
336}
337
269const char *
338UCL_EXTERN const char *
270ucl_parser_get_error(struct ucl_parser *parser)
271{
272 if (parser->err == NULL)
273 return NULL;
274
275 return utstring_body(parser->err);
276}
277
339ucl_parser_get_error(struct ucl_parser *parser)
340{
341 if (parser->err == NULL)
342 return NULL;
343
344 return utstring_body(parser->err);
345}
346
278bool
347UCL_EXTERN bool
279ucl_pubkey_add (struct ucl_parser *parser, const unsigned char *key, size_t len)
280{
281#ifndef HAVE_OPENSSL
282 ucl_create_err (&parser->err, "cannot check signatures without openssl");
283 return false;
284#else
285# if (OPENSSL_VERSION_NUMBER < 0x10000000L)
286 ucl_create_err (&parser->err, "cannot check signatures, openssl version is unsupported");

--- 387 unchanged lines hidden (view full) ---

674/**
675 * Handle include macro
676 * @param data include data
677 * @param len length of data
678 * @param ud user data
679 * @param err error ptr
680 * @return
681 */
348ucl_pubkey_add (struct ucl_parser *parser, const unsigned char *key, size_t len)
349{
350#ifndef HAVE_OPENSSL
351 ucl_create_err (&parser->err, "cannot check signatures without openssl");
352 return false;
353#else
354# if (OPENSSL_VERSION_NUMBER < 0x10000000L)
355 ucl_create_err (&parser->err, "cannot check signatures, openssl version is unsupported");

--- 387 unchanged lines hidden (view full) ---

743/**
744 * Handle include macro
745 * @param data include data
746 * @param len length of data
747 * @param ud user data
748 * @param err error ptr
749 * @return
750 */
682bool
751UCL_EXTERN bool
683ucl_include_handler (const unsigned char *data, size_t len, void* ud)
684{
685 struct ucl_parser *parser = ud;
686
687 if (*data == '/' || *data == '.') {
688 /* Try to load a file */
689 return ucl_include_file (data, len, parser, false, true);
690 }

--- 4 unchanged lines hidden (view full) ---

695/**
696 * Handle includes macro
697 * @param data include data
698 * @param len length of data
699 * @param ud user data
700 * @param err error ptr
701 * @return
702 */
752ucl_include_handler (const unsigned char *data, size_t len, void* ud)
753{
754 struct ucl_parser *parser = ud;
755
756 if (*data == '/' || *data == '.') {
757 /* Try to load a file */
758 return ucl_include_file (data, len, parser, false, true);
759 }

--- 4 unchanged lines hidden (view full) ---

764/**
765 * Handle includes macro
766 * @param data include data
767 * @param len length of data
768 * @param ud user data
769 * @param err error ptr
770 * @return
771 */
703bool
772UCL_EXTERN bool
704ucl_includes_handler (const unsigned char *data, size_t len, void* ud)
705{
706 struct ucl_parser *parser = ud;
707
708 if (*data == '/' || *data == '.') {
709 /* Try to load a file */
710 return ucl_include_file (data, len, parser, true, true);
711 }
712
713 return ucl_include_url (data, len, parser, true, true);
714}
715
716
773ucl_includes_handler (const unsigned char *data, size_t len, void* ud)
774{
775 struct ucl_parser *parser = ud;
776
777 if (*data == '/' || *data == '.') {
778 /* Try to load a file */
779 return ucl_include_file (data, len, parser, true, true);
780 }
781
782 return ucl_include_url (data, len, parser, true, true);
783}
784
785
717bool
786UCL_EXTERN bool
718ucl_try_include_handler (const unsigned char *data, size_t len, void* ud)
719{
720 struct ucl_parser *parser = ud;
721
722 if (*data == '/' || *data == '.') {
723 /* Try to load a file */
724 return ucl_include_file (data, len, parser, false, false);
725 }
726
727 return ucl_include_url (data, len, parser, false, false);
728}
729
787ucl_try_include_handler (const unsigned char *data, size_t len, void* ud)
788{
789 struct ucl_parser *parser = ud;
790
791 if (*data == '/' || *data == '.') {
792 /* Try to load a file */
793 return ucl_include_file (data, len, parser, false, false);
794 }
795
796 return ucl_include_url (data, len, parser, false, false);
797}
798
730bool
799UCL_EXTERN bool
731ucl_parser_set_filevars (struct ucl_parser *parser, const char *filename, bool need_expand)
732{
733 char realbuf[PATH_MAX], *curdir;
734
735 if (filename != NULL) {
736 if (need_expand) {
737 if (realpath (filename, realbuf) == NULL) {
738 return false;

--- 13 unchanged lines hidden (view full) ---

752 curdir = getcwd (realbuf, sizeof (realbuf));
753 ucl_parser_register_variable (parser, "FILENAME", "undef");
754 ucl_parser_register_variable (parser, "CURDIR", curdir);
755 }
756
757 return true;
758}
759
800ucl_parser_set_filevars (struct ucl_parser *parser, const char *filename, bool need_expand)
801{
802 char realbuf[PATH_MAX], *curdir;
803
804 if (filename != NULL) {
805 if (need_expand) {
806 if (realpath (filename, realbuf) == NULL) {
807 return false;

--- 13 unchanged lines hidden (view full) ---

821 curdir = getcwd (realbuf, sizeof (realbuf));
822 ucl_parser_register_variable (parser, "FILENAME", "undef");
823 ucl_parser_register_variable (parser, "CURDIR", curdir);
824 }
825
826 return true;
827}
828
760bool
829UCL_EXTERN bool
761ucl_parser_add_file (struct ucl_parser *parser, const char *filename)
762{
763 unsigned char *buf;
764 size_t len;
765 bool ret;
766 char realbuf[PATH_MAX];
767
768 if (realpath (filename, realbuf) == NULL) {

--- 281 unchanged lines hidden (view full) ---

1050 else {
1051 DL_APPEND (found, elt);
1052 }
1053 }
1054
1055 return top;
1056}
1057
830ucl_parser_add_file (struct ucl_parser *parser, const char *filename)
831{
832 unsigned char *buf;
833 size_t len;
834 bool ret;
835 char realbuf[PATH_MAX];
836
837 if (realpath (filename, realbuf) == NULL) {

--- 281 unchanged lines hidden (view full) ---

1119 else {
1120 DL_APPEND (found, elt);
1121 }
1122 }
1123
1124 return top;
1125}
1126
1127bool
1128ucl_object_delete_keyl(ucl_object_t *top, const char *key, size_t keylen)
1129{
1130 ucl_object_t *found;
1131
1132 found = ucl_object_find_keyl(top, key, keylen);
1133
1134 if (found == NULL)
1135 return false;
1136
1137 ucl_hash_delete(top->value.ov, found);
1138 ucl_object_unref (found);
1139 top->len --;
1140
1141 return true;
1142}
1143
1144bool
1145ucl_object_delete_key(ucl_object_t *top, const char *key)
1146{
1147 return ucl_object_delete_keyl(top, key, 0);
1148}
1149
1058ucl_object_t *
1059ucl_object_insert_key (ucl_object_t *top, ucl_object_t *elt,
1060 const char *key, size_t keylen, bool copy_key)
1061{
1062 return ucl_object_insert_key_common (top, elt, key, keylen, copy_key, false, false);
1063}
1064
1065ucl_object_t *

--- 92 unchanged lines hidden ---
1150ucl_object_t *
1151ucl_object_insert_key (ucl_object_t *top, ucl_object_t *elt,
1152 const char *key, size_t keylen, bool copy_key)
1153{
1154 return ucl_object_insert_key_common (top, elt, key, keylen, copy_key, false, false);
1155}
1156
1157ucl_object_t *

--- 92 unchanged lines hidden ---