ucl_internal.h revision 262395
1262395Sbapt/* Copyright (c) 2013, Vsevolod Stakhov 2262395Sbapt * All rights reserved. 3262395Sbapt * 4262395Sbapt * Redistribution and use in source and binary forms, with or without 5262395Sbapt * modification, are permitted provided that the following conditions are met: 6262395Sbapt * * Redistributions of source code must retain the above copyright 7262395Sbapt * notice, this list of conditions and the following disclaimer. 8262395Sbapt * * Redistributions in binary form must reproduce the above copyright 9262395Sbapt * notice, this list of conditions and the following disclaimer in the 10262395Sbapt * documentation and/or other materials provided with the distribution. 11262395Sbapt * 12262395Sbapt * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY 13262395Sbapt * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 14262395Sbapt * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 15262395Sbapt * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY 16262395Sbapt * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 17262395Sbapt * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 18262395Sbapt * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 19262395Sbapt * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 20262395Sbapt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 21262395Sbapt * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 22262395Sbapt */ 23262395Sbapt 24262395Sbapt#ifndef UCL_INTERNAL_H_ 25262395Sbapt#define UCL_INTERNAL_H_ 26262395Sbapt 27262395Sbapt#include <sys/types.h> 28262395Sbapt#include <sys/mman.h> 29262395Sbapt#include <sys/stat.h> 30262395Sbapt#include <sys/param.h> 31262395Sbapt 32262395Sbapt#include <limits.h> 33262395Sbapt#include <fcntl.h> 34262395Sbapt#include <errno.h> 35262395Sbapt#include <unistd.h> 36262395Sbapt#include <ctype.h> 37262395Sbapt 38262395Sbapt#include "utlist.h" 39262395Sbapt#include "utstring.h" 40262395Sbapt#include "uthash.h" 41262395Sbapt#include "ucl.h" 42262395Sbapt#include "ucl_hash.h" 43262395Sbapt#include "xxhash.h" 44262395Sbapt 45262395Sbapt#ifdef HAVE_OPENSSL 46262395Sbapt#include <openssl/evp.h> 47262395Sbapt#endif 48262395Sbapt 49262395Sbapt/** 50262395Sbapt * @file rcl_internal.h 51262395Sbapt * Internal structures and functions of UCL library 52262395Sbapt */ 53262395Sbapt 54262395Sbapt#define UCL_MAX_RECURSION 16 55262395Sbapt#define UCL_TRASH_KEY 0 56262395Sbapt#define UCL_TRASH_VALUE 1 57262395Sbapt 58262395Sbaptenum ucl_parser_state { 59262395Sbapt UCL_STATE_INIT = 0, 60262395Sbapt UCL_STATE_OBJECT, 61262395Sbapt UCL_STATE_ARRAY, 62262395Sbapt UCL_STATE_KEY, 63262395Sbapt UCL_STATE_VALUE, 64262395Sbapt UCL_STATE_AFTER_VALUE, 65262395Sbapt UCL_STATE_ARRAY_VALUE, 66262395Sbapt UCL_STATE_SCOMMENT, 67262395Sbapt UCL_STATE_MCOMMENT, 68262395Sbapt UCL_STATE_MACRO_NAME, 69262395Sbapt UCL_STATE_MACRO, 70262395Sbapt UCL_STATE_ERROR 71262395Sbapt}; 72262395Sbapt 73262395Sbaptenum ucl_character_type { 74262395Sbapt UCL_CHARACTER_DENIED = 0, 75262395Sbapt UCL_CHARACTER_KEY = 1, 76262395Sbapt UCL_CHARACTER_KEY_START = 1 << 1, 77262395Sbapt UCL_CHARACTER_WHITESPACE = 1 << 2, 78262395Sbapt UCL_CHARACTER_WHITESPACE_UNSAFE = 1 << 3, 79262395Sbapt UCL_CHARACTER_VALUE_END = 1 << 4, 80262395Sbapt UCL_CHARACTER_VALUE_STR = 1 << 5, 81262395Sbapt UCL_CHARACTER_VALUE_DIGIT = 1 << 6, 82262395Sbapt UCL_CHARACTER_VALUE_DIGIT_START = 1 << 7, 83262395Sbapt UCL_CHARACTER_ESCAPE = 1 << 8, 84262395Sbapt UCL_CHARACTER_KEY_SEP = 1 << 9, 85262395Sbapt UCL_CHARACTER_JSON_UNSAFE = 1 << 10, 86262395Sbapt UCL_CHARACTER_UCL_UNSAFE = 1 << 11 87262395Sbapt}; 88262395Sbapt 89262395Sbaptstruct ucl_macro { 90262395Sbapt char *name; 91262395Sbapt ucl_macro_handler handler; 92262395Sbapt void* ud; 93262395Sbapt UT_hash_handle hh; 94262395Sbapt}; 95262395Sbapt 96262395Sbaptstruct ucl_stack { 97262395Sbapt ucl_object_t *obj; 98262395Sbapt struct ucl_stack *next; 99262395Sbapt int level; 100262395Sbapt}; 101262395Sbapt 102262395Sbaptstruct ucl_chunk { 103262395Sbapt const unsigned char *begin; 104262395Sbapt const unsigned char *end; 105262395Sbapt const unsigned char *pos; 106262395Sbapt size_t remain; 107262395Sbapt unsigned int line; 108262395Sbapt unsigned int column; 109262395Sbapt struct ucl_chunk *next; 110262395Sbapt}; 111262395Sbapt 112262395Sbapt#ifdef HAVE_OPENSSL 113262395Sbaptstruct ucl_pubkey { 114262395Sbapt EVP_PKEY *key; 115262395Sbapt struct ucl_pubkey *next; 116262395Sbapt}; 117262395Sbapt#else 118262395Sbaptstruct ucl_pubkey { 119262395Sbapt struct ucl_pubkey *next; 120262395Sbapt}; 121262395Sbapt#endif 122262395Sbapt 123262395Sbaptstruct ucl_variable { 124262395Sbapt char *var; 125262395Sbapt char *value; 126262395Sbapt size_t var_len; 127262395Sbapt size_t value_len; 128262395Sbapt struct ucl_variable *next; 129262395Sbapt}; 130262395Sbapt 131262395Sbaptstruct ucl_parser { 132262395Sbapt enum ucl_parser_state state; 133262395Sbapt enum ucl_parser_state prev_state; 134262395Sbapt unsigned int recursion; 135262395Sbapt int flags; 136262395Sbapt ucl_object_t *top_obj; 137262395Sbapt ucl_object_t *cur_obj; 138262395Sbapt struct ucl_macro *macroes; 139262395Sbapt struct ucl_stack *stack; 140262395Sbapt struct ucl_chunk *chunks; 141262395Sbapt struct ucl_pubkey *keys; 142262395Sbapt struct ucl_variable *variables; 143262395Sbapt UT_string *err; 144262395Sbapt}; 145262395Sbapt 146262395Sbapt/** 147262395Sbapt * Unescape json string inplace 148262395Sbapt * @param str 149262395Sbapt */ 150262395Sbaptsize_t ucl_unescape_json_string (char *str, size_t len); 151262395Sbapt 152262395Sbapt/** 153262395Sbapt * Handle include macro 154262395Sbapt * @param data include data 155262395Sbapt * @param len length of data 156262395Sbapt * @param ud user data 157262395Sbapt * @param err error ptr 158262395Sbapt * @return 159262395Sbapt */ 160262395Sbaptbool ucl_include_handler (const unsigned char *data, size_t len, void* ud); 161262395Sbapt 162262395Sbaptbool ucl_try_include_handler (const unsigned char *data, size_t len, void* ud); 163262395Sbapt 164262395Sbapt/** 165262395Sbapt * Handle includes macro 166262395Sbapt * @param data include data 167262395Sbapt * @param len length of data 168262395Sbapt * @param ud user data 169262395Sbapt * @param err error ptr 170262395Sbapt * @return 171262395Sbapt */ 172262395Sbaptbool ucl_includes_handler (const unsigned char *data, size_t len, void* ud); 173262395Sbapt 174262395Sbaptsize_t ucl_strlcpy (char *dst, const char *src, size_t siz); 175262395Sbaptsize_t ucl_strlcpy_unsafe (char *dst, const char *src, size_t siz); 176262395Sbaptsize_t ucl_strlcpy_tolower (char *dst, const char *src, size_t siz); 177262395Sbapt 178262395Sbapt 179262395Sbapt#ifdef __GNUC__ 180262395Sbaptstatic inline void 181262395Sbaptucl_create_err (UT_string **err, const char *fmt, ...) 182262395Sbapt__attribute__ (( format( printf, 2, 3) )); 183262395Sbapt#endif 184262395Sbapt 185262395Sbaptstatic inline void 186262395Sbaptucl_create_err (UT_string **err, const char *fmt, ...) 187262395Sbapt 188262395Sbapt{ 189262395Sbapt if (*err == NULL) { 190262395Sbapt utstring_new (*err); 191262395Sbapt va_list ap; 192262395Sbapt va_start (ap, fmt); 193262395Sbapt utstring_printf_va (*err, fmt, ap); 194262395Sbapt va_end (ap); 195262395Sbapt } 196262395Sbapt} 197262395Sbapt 198262395Sbapt/** 199262395Sbapt * Check whether a given string contains a boolean value 200262395Sbapt * @param obj object to set 201262395Sbapt * @param start start of a string 202262395Sbapt * @param len length of a string 203262395Sbapt * @return true if a string is a boolean value 204262395Sbapt */ 205262395Sbaptstatic inline bool 206262395Sbaptucl_maybe_parse_boolean (ucl_object_t *obj, const unsigned char *start, size_t len) 207262395Sbapt{ 208262395Sbapt const unsigned char *p = start; 209262395Sbapt bool ret = false, val = false; 210262395Sbapt 211262395Sbapt if (len == 5) { 212262395Sbapt if ((p[0] == 'f' || p[0] == 'F') && strncasecmp (p, "false", 5) == 0) { 213262395Sbapt ret = true; 214262395Sbapt val = false; 215262395Sbapt } 216262395Sbapt } 217262395Sbapt else if (len == 4) { 218262395Sbapt if ((p[0] == 't' || p[0] == 'T') && strncasecmp (p, "true", 4) == 0) { 219262395Sbapt ret = true; 220262395Sbapt val = true; 221262395Sbapt } 222262395Sbapt } 223262395Sbapt else if (len == 3) { 224262395Sbapt if ((p[0] == 'y' || p[0] == 'Y') && strncasecmp (p, "yes", 3) == 0) { 225262395Sbapt ret = true; 226262395Sbapt val = true; 227262395Sbapt } 228262395Sbapt else if ((p[0] == 'o' || p[0] == 'O') && strncasecmp (p, "off", 3) == 0) { 229262395Sbapt ret = true; 230262395Sbapt val = false; 231262395Sbapt } 232262395Sbapt } 233262395Sbapt else if (len == 2) { 234262395Sbapt if ((p[0] == 'n' || p[0] == 'N') && strncasecmp (p, "no", 2) == 0) { 235262395Sbapt ret = true; 236262395Sbapt val = false; 237262395Sbapt } 238262395Sbapt else if ((p[0] == 'o' || p[0] == 'O') && strncasecmp (p, "on", 2) == 0) { 239262395Sbapt ret = true; 240262395Sbapt val = true; 241262395Sbapt } 242262395Sbapt } 243262395Sbapt 244262395Sbapt if (ret) { 245262395Sbapt obj->type = UCL_BOOLEAN; 246262395Sbapt obj->value.iv = val; 247262395Sbapt } 248262395Sbapt 249262395Sbapt return ret; 250262395Sbapt} 251262395Sbapt 252262395Sbapt/** 253262395Sbapt * Check numeric string 254262395Sbapt * @param obj object to set if a string is numeric 255262395Sbapt * @param start start of string 256262395Sbapt * @param end end of string 257262395Sbapt * @param pos position where parsing has stopped 258262395Sbapt * @param allow_double allow parsing of floating point values 259262395Sbapt * @return 0 if string is numeric and error code (EINVAL or ERANGE) in case of conversion error 260262395Sbapt */ 261262395Sbaptint ucl_maybe_parse_number (ucl_object_t *obj, 262262395Sbapt const char *start, const char *end, const char **pos, bool allow_double, bool number_bytes); 263262395Sbapt 264262395Sbapt 265262395Sbaptstatic inline ucl_object_t * 266262395Sbaptucl_hash_search_obj (ucl_hash_t* hashlin, ucl_object_t *obj) 267262395Sbapt{ 268262395Sbapt return (ucl_object_t *)ucl_hash_search (hashlin, obj->key, obj->keylen); 269262395Sbapt} 270262395Sbapt 271262395Sbaptstatic inline ucl_hash_t * 272262395Sbaptucl_hash_insert_object (ucl_hash_t *hashlin, ucl_object_t *obj) UCL_WARN_UNUSED_RESULT; 273262395Sbapt 274262395Sbaptstatic inline ucl_hash_t * 275262395Sbaptucl_hash_insert_object (ucl_hash_t *hashlin, ucl_object_t *obj) 276262395Sbapt{ 277262395Sbapt if (hashlin == NULL) { 278262395Sbapt hashlin = ucl_hash_create (); 279262395Sbapt } 280262395Sbapt ucl_hash_insert (hashlin, obj, obj->key, obj->keylen); 281262395Sbapt 282262395Sbapt return hashlin; 283262395Sbapt} 284262395Sbapt 285262395Sbapt/** 286262395Sbapt * Emit a single object to string 287262395Sbapt * @param obj 288262395Sbapt * @return 289262395Sbapt */ 290262395Sbaptunsigned char * ucl_object_emit_single_json (ucl_object_t *obj); 291262395Sbapt 292262395Sbapt#endif /* UCL_INTERNAL_H_ */ 293