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	ucl_variable_handler var_handler;
201	void *var_data;
202	UT_string *err;
203};
204
205/**
206 * Unescape json string inplace
207 * @param str
208 */
209size_t ucl_unescape_json_string (char *str, size_t len);
210
211/**
212 * Handle include macro
213 * @param data include data
214 * @param len length of data
215 * @param ud user data
216 * @param err error ptr
217 * @return
218 */
219bool ucl_include_handler (const unsigned char *data, size_t len, void* ud);
220
221bool ucl_try_include_handler (const unsigned char *data, size_t len, void* ud);
222
223/**
224 * Handle includes macro
225 * @param data include data
226 * @param len length of data
227 * @param ud user data
228 * @param err error ptr
229 * @return
230 */
231bool ucl_includes_handler (const unsigned char *data, size_t len, void* ud);
232
233size_t ucl_strlcpy (char *dst, const char *src, size_t siz);
234size_t ucl_strlcpy_unsafe (char *dst, const char *src, size_t siz);
235size_t ucl_strlcpy_tolower (char *dst, const char *src, size_t siz);
236
237
238#ifdef __GNUC__
239static inline void
240ucl_create_err (UT_string **err, const char *fmt, ...)
241__attribute__ (( format( printf, 2, 3) ));
242#endif
243
244static inline void
245ucl_create_err (UT_string **err, const char *fmt, ...)
246
247{
248	if (*err == NULL) {
249		utstring_new (*err);
250		va_list ap;
251		va_start (ap, fmt);
252		utstring_printf_va (*err, fmt, ap);
253		va_end (ap);
254	}
255}
256
257/**
258 * Check whether a given string contains a boolean value
259 * @param obj object to set
260 * @param start start of a string
261 * @param len length of a string
262 * @return true if a string is a boolean value
263 */
264static inline bool
265ucl_maybe_parse_boolean (ucl_object_t *obj, const unsigned char *start, size_t len)
266{
267	const unsigned char *p = start;
268	bool ret = false, val = false;
269
270	if (len == 5) {
271		if ((p[0] == 'f' || p[0] == 'F') && strncasecmp (p, "false", 5) == 0) {
272			ret = true;
273			val = false;
274		}
275	}
276	else if (len == 4) {
277		if ((p[0] == 't' || p[0] == 'T') && strncasecmp (p, "true", 4) == 0) {
278			ret = true;
279			val = true;
280		}
281	}
282	else if (len == 3) {
283		if ((p[0] == 'y' || p[0] == 'Y') && strncasecmp (p, "yes", 3) == 0) {
284			ret = true;
285			val = true;
286		}
287		else if ((p[0] == 'o' || p[0] == 'O') && strncasecmp (p, "off", 3) == 0) {
288			ret = true;
289			val = false;
290		}
291	}
292	else if (len == 2) {
293		if ((p[0] == 'n' || p[0] == 'N') && strncasecmp (p, "no", 2) == 0) {
294			ret = true;
295			val = false;
296		}
297		else if ((p[0] == 'o' || p[0] == 'O') && strncasecmp (p, "on", 2) == 0) {
298			ret = true;
299			val = true;
300		}
301	}
302
303	if (ret) {
304		obj->type = UCL_BOOLEAN;
305		obj->value.iv = val;
306	}
307
308	return ret;
309}
310
311/**
312 * Check numeric string
313 * @param obj object to set if a string is numeric
314 * @param start start of string
315 * @param end end of string
316 * @param pos position where parsing has stopped
317 * @param allow_double allow parsing of floating point values
318 * @return 0 if string is numeric and error code (EINVAL or ERANGE) in case of conversion error
319 */
320int ucl_maybe_parse_number (ucl_object_t *obj,
321		const char *start, const char *end, const char **pos,
322		bool allow_double, bool number_bytes, bool allow_time);
323
324
325static inline const ucl_object_t *
326ucl_hash_search_obj (ucl_hash_t* hashlin, ucl_object_t *obj)
327{
328	return (const ucl_object_t *)ucl_hash_search (hashlin, obj->key, obj->keylen);
329}
330
331static inline ucl_hash_t *
332ucl_hash_insert_object (ucl_hash_t *hashlin, const ucl_object_t *obj) UCL_WARN_UNUSED_RESULT;
333
334static inline ucl_hash_t *
335ucl_hash_insert_object (ucl_hash_t *hashlin, const ucl_object_t *obj)
336{
337	if (hashlin == NULL) {
338		hashlin = ucl_hash_create ();
339	}
340	ucl_hash_insert (hashlin, obj, obj->key, obj->keylen);
341
342	return hashlin;
343}
344
345/**
346 * Emit a single object to string
347 * @param obj
348 * @return
349 */
350unsigned char * ucl_object_emit_single_json (const ucl_object_t *obj);
351
352#endif /* UCL_INTERNAL_H_ */
353