ucl_internal.h revision 264789
1/* Copyright (c) 2013, Vsevolod Stakhov
2 * All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *       * Redistributions of source code must retain the above copyright
7 *         notice, this list of conditions and the following disclaimer.
8 *       * Redistributions in binary form must reproduce the above copyright
9 *         notice, this list of conditions and the following disclaimer in the
10 *         documentation and/or other materials provided with the distribution.
11 *
12 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
13 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15 * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
16 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
19 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 */
23
24#ifndef UCL_INTERNAL_H_
25#define UCL_INTERNAL_H_
26
27#ifdef HAVE_CONFIG_H
28#include "config.h"
29#else
30/* Help embedded builds */
31#define HAVE_SYS_TYPES_H
32#define HAVE_SYS_MMAN_H
33#define HAVE_SYS_STAT_H
34#define HAVE_SYS_PARAM_H
35#define HAVE_LIMITS_H
36#define HAVE_FCNTL_H
37#define HAVE_ERRNO_H
38#define HAVE_UNISTD_H
39#define HAVE_CTYPE_H
40#define HAVE_STDIO_H
41#define HAVE_STRING_H
42#define HAVE_FLOAT_H
43#define HAVE_LIBGEN_H
44#define HAVE_MATH_H
45#define HAVE_STDBOOL_H
46#define HAVE_STDINT_H
47#define HAVE_STDARG_H
48#ifndef _WIN32
49# define HAVE_REGEX_H
50#endif
51#endif
52
53#ifdef HAVE_SYS_TYPES_H
54#include <sys/types.h>
55#endif
56
57#ifdef HAVE_SYS_MMAN_H
58# ifndef _WIN32
59#  include <sys/mman.h>
60# endif
61#endif
62#ifdef HAVE_SYS_STAT_H
63#include <sys/stat.h>
64#endif
65#ifdef HAVE_SYS_PARAM_H
66#include <sys/param.h>
67#endif
68
69#ifdef HAVE_LIMITS_H
70#include <limits.h>
71#endif
72#ifdef HAVE_FCNTL_H
73#include <fcntl.h>
74#endif
75#ifdef HAVE_ERRNO_H
76#include <errno.h>
77#endif
78#ifdef HAVE_UNISTD_H
79#include <unistd.h>
80#endif
81#ifdef HAVE_CTYPE_H
82#include <ctype.h>
83#endif
84#ifdef HAVE_STDIO_H
85#include <stdio.h>
86#endif
87#ifdef HAVE_STRING_H
88#include <string.h>
89#endif
90
91#include "utlist.h"
92#include "utstring.h"
93#include "uthash.h"
94#include "ucl.h"
95#include "ucl_hash.h"
96#include "xxhash.h"
97
98#ifdef HAVE_OPENSSL
99#include <openssl/evp.h>
100#endif
101
102#ifndef __DECONST
103#define __DECONST(type, var)    ((type)(uintptr_t)(const void *)(var))
104#endif
105
106/**
107 * @file rcl_internal.h
108 * Internal structures and functions of UCL library
109 */
110
111#define UCL_MAX_RECURSION 16
112#define UCL_TRASH_KEY 0
113#define UCL_TRASH_VALUE 1
114
115enum ucl_parser_state {
116	UCL_STATE_INIT = 0,
117	UCL_STATE_OBJECT,
118	UCL_STATE_ARRAY,
119	UCL_STATE_KEY,
120	UCL_STATE_VALUE,
121	UCL_STATE_AFTER_VALUE,
122	UCL_STATE_ARRAY_VALUE,
123	UCL_STATE_SCOMMENT,
124	UCL_STATE_MCOMMENT,
125	UCL_STATE_MACRO_NAME,
126	UCL_STATE_MACRO,
127	UCL_STATE_ERROR
128};
129
130enum ucl_character_type {
131	UCL_CHARACTER_DENIED = 0,
132	UCL_CHARACTER_KEY = 1,
133	UCL_CHARACTER_KEY_START = 1 << 1,
134	UCL_CHARACTER_WHITESPACE = 1 << 2,
135	UCL_CHARACTER_WHITESPACE_UNSAFE = 1 << 3,
136	UCL_CHARACTER_VALUE_END = 1 << 4,
137	UCL_CHARACTER_VALUE_STR = 1 << 5,
138	UCL_CHARACTER_VALUE_DIGIT = 1 << 6,
139	UCL_CHARACTER_VALUE_DIGIT_START = 1 << 7,
140	UCL_CHARACTER_ESCAPE = 1 << 8,
141	UCL_CHARACTER_KEY_SEP = 1 << 9,
142	UCL_CHARACTER_JSON_UNSAFE = 1 << 10,
143	UCL_CHARACTER_UCL_UNSAFE = 1 << 11
144};
145
146struct ucl_macro {
147	char *name;
148	ucl_macro_handler handler;
149	void* ud;
150	UT_hash_handle hh;
151};
152
153struct ucl_stack {
154	ucl_object_t *obj;
155	struct ucl_stack *next;
156	int level;
157};
158
159struct ucl_chunk {
160	const unsigned char *begin;
161	const unsigned char *end;
162	const unsigned char *pos;
163	size_t remain;
164	unsigned int line;
165	unsigned int column;
166	struct ucl_chunk *next;
167};
168
169#ifdef HAVE_OPENSSL
170struct ucl_pubkey {
171	EVP_PKEY *key;
172	struct ucl_pubkey *next;
173};
174#else
175struct ucl_pubkey {
176	struct ucl_pubkey *next;
177};
178#endif
179
180struct ucl_variable {
181	char *var;
182	char *value;
183	size_t var_len;
184	size_t value_len;
185	struct ucl_variable *next;
186};
187
188struct ucl_parser {
189	enum ucl_parser_state state;
190	enum ucl_parser_state prev_state;
191	unsigned int recursion;
192	int flags;
193	ucl_object_t *top_obj;
194	ucl_object_t *cur_obj;
195	struct ucl_macro *macroes;
196	struct ucl_stack *stack;
197	struct ucl_chunk *chunks;
198	struct ucl_pubkey *keys;
199	struct ucl_variable *variables;
200	UT_string *err;
201};
202
203/**
204 * Unescape json string inplace
205 * @param str
206 */
207size_t ucl_unescape_json_string (char *str, size_t len);
208
209/**
210 * Handle include macro
211 * @param data include data
212 * @param len length of data
213 * @param ud user data
214 * @param err error ptr
215 * @return
216 */
217bool ucl_include_handler (const unsigned char *data, size_t len, void* ud);
218
219bool ucl_try_include_handler (const unsigned char *data, size_t len, void* ud);
220
221/**
222 * Handle includes macro
223 * @param data include data
224 * @param len length of data
225 * @param ud user data
226 * @param err error ptr
227 * @return
228 */
229bool ucl_includes_handler (const unsigned char *data, size_t len, void* ud);
230
231size_t ucl_strlcpy (char *dst, const char *src, size_t siz);
232size_t ucl_strlcpy_unsafe (char *dst, const char *src, size_t siz);
233size_t ucl_strlcpy_tolower (char *dst, const char *src, size_t siz);
234
235
236#ifdef __GNUC__
237static inline void
238ucl_create_err (UT_string **err, const char *fmt, ...)
239__attribute__ (( format( printf, 2, 3) ));
240#endif
241
242static inline void
243ucl_create_err (UT_string **err, const char *fmt, ...)
244
245{
246	if (*err == NULL) {
247		utstring_new (*err);
248		va_list ap;
249		va_start (ap, fmt);
250		utstring_printf_va (*err, fmt, ap);
251		va_end (ap);
252	}
253}
254
255/**
256 * Check whether a given string contains a boolean value
257 * @param obj object to set
258 * @param start start of a string
259 * @param len length of a string
260 * @return true if a string is a boolean value
261 */
262static inline bool
263ucl_maybe_parse_boolean (ucl_object_t *obj, const unsigned char *start, size_t len)
264{
265	const unsigned char *p = start;
266	bool ret = false, val = false;
267
268	if (len == 5) {
269		if ((p[0] == 'f' || p[0] == 'F') && strncasecmp (p, "false", 5) == 0) {
270			ret = true;
271			val = false;
272		}
273	}
274	else if (len == 4) {
275		if ((p[0] == 't' || p[0] == 'T') && strncasecmp (p, "true", 4) == 0) {
276			ret = true;
277			val = true;
278		}
279	}
280	else if (len == 3) {
281		if ((p[0] == 'y' || p[0] == 'Y') && strncasecmp (p, "yes", 3) == 0) {
282			ret = true;
283			val = true;
284		}
285		else if ((p[0] == 'o' || p[0] == 'O') && strncasecmp (p, "off", 3) == 0) {
286			ret = true;
287			val = false;
288		}
289	}
290	else if (len == 2) {
291		if ((p[0] == 'n' || p[0] == 'N') && strncasecmp (p, "no", 2) == 0) {
292			ret = true;
293			val = false;
294		}
295		else if ((p[0] == 'o' || p[0] == 'O') && strncasecmp (p, "on", 2) == 0) {
296			ret = true;
297			val = true;
298		}
299	}
300
301	if (ret) {
302		obj->type = UCL_BOOLEAN;
303		obj->value.iv = val;
304	}
305
306	return ret;
307}
308
309/**
310 * Check numeric string
311 * @param obj object to set if a string is numeric
312 * @param start start of string
313 * @param end end of string
314 * @param pos position where parsing has stopped
315 * @param allow_double allow parsing of floating point values
316 * @return 0 if string is numeric and error code (EINVAL or ERANGE) in case of conversion error
317 */
318int ucl_maybe_parse_number (ucl_object_t *obj,
319		const char *start, const char *end, const char **pos,
320		bool allow_double, bool number_bytes, bool allow_time);
321
322
323static inline const ucl_object_t *
324ucl_hash_search_obj (ucl_hash_t* hashlin, ucl_object_t *obj)
325{
326	return (const ucl_object_t *)ucl_hash_search (hashlin, obj->key, obj->keylen);
327}
328
329static inline ucl_hash_t *
330ucl_hash_insert_object (ucl_hash_t *hashlin, const ucl_object_t *obj) UCL_WARN_UNUSED_RESULT;
331
332static inline ucl_hash_t *
333ucl_hash_insert_object (ucl_hash_t *hashlin, const ucl_object_t *obj)
334{
335	if (hashlin == NULL) {
336		hashlin = ucl_hash_create ();
337	}
338	ucl_hash_insert (hashlin, obj, obj->key, obj->keylen);
339
340	return hashlin;
341}
342
343/**
344 * Emit a single object to string
345 * @param obj
346 * @return
347 */
348unsigned char * ucl_object_emit_single_json (const ucl_object_t *obj);
349
350#endif /* UCL_INTERNAL_H_ */
351