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 115 enum 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 130 enum 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 146 struct ucl_macro { 147 char *name; 148 ucl_macro_handler handler; 149 void* ud; 150 UT_hash_handle hh; 151 }; 152 153 struct ucl_stack { 154 ucl_object_t *obj; 155 struct ucl_stack *next; 156 int level; 157 }; 158 159 struct 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 unsigned priority; 167 struct ucl_chunk *next; 168 }; 169 170 #ifdef HAVE_OPENSSL 171 struct ucl_pubkey { 172 EVP_PKEY *key; 173 struct ucl_pubkey *next; 174 }; 175 #else 176 struct ucl_pubkey { 177 struct ucl_pubkey *next; 178 }; 179 #endif 180 181 struct ucl_variable { 182 char *var; 183 char *value; 184 size_t var_len; 185 size_t value_len; 186 struct ucl_variable *prev, *next; 187 }; 188 189 struct ucl_parser { 190 enum ucl_parser_state state; 191 enum ucl_parser_state prev_state; 192 unsigned int recursion; 193 int flags; 194 ucl_object_t *top_obj; 195 ucl_object_t *cur_obj; 196 char *cur_file; 197 struct ucl_macro *macroes; 198 struct ucl_stack *stack; 199 struct ucl_chunk *chunks; 200 struct ucl_pubkey *keys; 201 struct ucl_variable *variables; 202 ucl_variable_handler var_handler; 203 void *var_data; 204 UT_string *err; 205 }; 206 207 struct ucl_object_userdata { 208 ucl_object_t obj; 209 ucl_userdata_dtor dtor; 210 ucl_userdata_emitter emitter; 211 }; 212 213 /** 214 * Unescape json string inplace 215 * @param str 216 */ 217 size_t ucl_unescape_json_string (char *str, size_t len); 218 219 /** 220 * Handle include macro 221 * @param data include data 222 * @param len length of data 223 * @param ud user data 224 * @param err error ptr 225 * @return 226 */ 227 bool ucl_include_handler (const unsigned char *data, size_t len, 228 const ucl_object_t *args, void* ud); 229 230 bool ucl_try_include_handler (const unsigned char *data, size_t len, 231 const ucl_object_t *args, void* ud); 232 233 /** 234 * Handle includes macro 235 * @param data include data 236 * @param len length of data 237 * @param ud user data 238 * @param err error ptr 239 * @return 240 */ 241 bool ucl_includes_handler (const unsigned char *data, size_t len, 242 const ucl_object_t *args, void* ud); 243 244 size_t ucl_strlcpy (char *dst, const char *src, size_t siz); 245 size_t ucl_strlcpy_unsafe (char *dst, const char *src, size_t siz); 246 size_t ucl_strlcpy_tolower (char *dst, const char *src, size_t siz); 247 248 249 #ifdef __GNUC__ 250 static inline void 251 ucl_create_err (UT_string **err, const char *fmt, ...) 252 __attribute__ (( format( printf, 2, 3) )); 253 #endif 254 255 static inline void 256 ucl_create_err (UT_string **err, const char *fmt, ...) 257 258 { 259 if (*err == NULL) { 260 utstring_new (*err); 261 va_list ap; 262 va_start (ap, fmt); 263 utstring_printf_va (*err, fmt, ap); 264 va_end (ap); 265 } 266 } 267 268 /** 269 * Check whether a given string contains a boolean value 270 * @param obj object to set 271 * @param start start of a string 272 * @param len length of a string 273 * @return true if a string is a boolean value 274 */ 275 static inline bool 276 ucl_maybe_parse_boolean (ucl_object_t *obj, const unsigned char *start, size_t len) 277 { 278 const char *p = (const char *)start; 279 bool ret = false, val = false; 280 281 if (len == 5) { 282 if ((p[0] == 'f' || p[0] == 'F') && strncasecmp (p, "false", 5) == 0) { 283 ret = true; 284 val = false; 285 } 286 } 287 else if (len == 4) { 288 if ((p[0] == 't' || p[0] == 'T') && strncasecmp (p, "true", 4) == 0) { 289 ret = true; 290 val = true; 291 } 292 } 293 else if (len == 3) { 294 if ((p[0] == 'y' || p[0] == 'Y') && strncasecmp (p, "yes", 3) == 0) { 295 ret = true; 296 val = true; 297 } 298 else if ((p[0] == 'o' || p[0] == 'O') && strncasecmp (p, "off", 3) == 0) { 299 ret = true; 300 val = false; 301 } 302 } 303 else if (len == 2) { 304 if ((p[0] == 'n' || p[0] == 'N') && strncasecmp (p, "no", 2) == 0) { 305 ret = true; 306 val = false; 307 } 308 else if ((p[0] == 'o' || p[0] == 'O') && strncasecmp (p, "on", 2) == 0) { 309 ret = true; 310 val = true; 311 } 312 } 313 314 if (ret) { 315 obj->type = UCL_BOOLEAN; 316 obj->value.iv = val; 317 } 318 319 return ret; 320 } 321 322 /** 323 * Check numeric string 324 * @param obj object to set if a string is numeric 325 * @param start start of string 326 * @param end end of string 327 * @param pos position where parsing has stopped 328 * @param allow_double allow parsing of floating point values 329 * @return 0 if string is numeric and error code (EINVAL or ERANGE) in case of conversion error 330 */ 331 int ucl_maybe_parse_number (ucl_object_t *obj, 332 const char *start, const char *end, const char **pos, 333 bool allow_double, bool number_bytes, bool allow_time); 334 335 336 static inline const ucl_object_t * 337 ucl_hash_search_obj (ucl_hash_t* hashlin, ucl_object_t *obj) 338 { 339 return (const ucl_object_t *)ucl_hash_search (hashlin, obj->key, obj->keylen); 340 } 341 342 static inline ucl_hash_t * ucl_hash_insert_object (ucl_hash_t *hashlin, 343 const ucl_object_t *obj, 344 bool ignore_case) UCL_WARN_UNUSED_RESULT; 345 346 static inline ucl_hash_t * 347 ucl_hash_insert_object (ucl_hash_t *hashlin, 348 const ucl_object_t *obj, 349 bool ignore_case) 350 { 351 if (hashlin == NULL) { 352 hashlin = ucl_hash_create (ignore_case); 353 } 354 ucl_hash_insert (hashlin, obj, obj->key, obj->keylen); 355 356 return hashlin; 357 } 358 359 /** 360 * Get standard emitter context for a specified emit_type 361 * @param emit_type type of emitter 362 * @return context or NULL if input is invalid 363 */ 364 const struct ucl_emitter_context * 365 ucl_emit_get_standard_context (enum ucl_emitter emit_type); 366 367 /** 368 * Serialize string as JSON string 369 * @param str string to emit 370 * @param buf target buffer 371 */ 372 void ucl_elt_string_write_json (const char *str, size_t size, 373 struct ucl_emitter_context *ctx); 374 375 /** 376 * Write multiline string using `EOD` as string terminator 377 * @param str 378 * @param size 379 * @param ctx 380 */ 381 void ucl_elt_string_write_multiline (const char *str, size_t size, 382 struct ucl_emitter_context *ctx); 383 384 /** 385 * Emit a single object to string 386 * @param obj 387 * @return 388 */ 389 unsigned char * ucl_object_emit_single_json (const ucl_object_t *obj); 390 391 /** 392 * Check whether a specified string is long and should be likely printed in 393 * multiline mode 394 * @param obj 395 * @return 396 */ 397 bool ucl_maybe_long_string (const ucl_object_t *obj); 398 399 #endif /* UCL_INTERNAL_H_ */ 400