File:  [ELWIX - Embedded LightWeight unIX -] / libelwix / src / json.c
Revision 1.1.2.6: download - view: text, annotated - select for diffs - revision graph
Tue Nov 28 11:14:00 2017 UTC (6 years, 6 months ago) by misho
Branches: elwix4_5
finish json parser

    1: /*************************************************************************
    2: * (C) 2017 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
    3: *  by Michael Pounov <misho@elwix.org>
    4: *
    5: * $Author: misho $
    6: * $Id: json.c,v 1.1.2.6 2017/11/28 11:14:00 misho Exp $
    7: *
    8: **************************************************************************
    9: The ELWIX and AITNET software is distributed under the following
   10: terms:
   11: 
   12: All of the documentation and software included in the ELWIX and AITNET
   13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
   14: 
   15: Copyright 2004 - 2017
   16: 	by Michael Pounov <misho@elwix.org>.  All rights reserved.
   17: 
   18: Redistribution and use in source and binary forms, with or without
   19: modification, are permitted provided that the following conditions
   20: are met:
   21: 1. Redistributions of source code must retain the above copyright
   22:    notice, this list of conditions and the following disclaimer.
   23: 2. Redistributions in binary form must reproduce the above copyright
   24:    notice, this list of conditions and the following disclaimer in the
   25:    documentation and/or other materials provided with the distribution.
   26: 3. All advertising materials mentioning features or use of this software
   27:    must display the following acknowledgement:
   28: This product includes software developed by Michael Pounov <misho@elwix.org>
   29: ELWIX - Embedded LightWeight unIX and its contributors.
   30: 4. Neither the name of AITNET nor the names of its contributors
   31:    may be used to endorse or promote products derived from this software
   32:    without specific prior written permission.
   33: 
   34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
   35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   44: SUCH DAMAGE.
   45: */
   46: #include "global.h"
   47: 
   48: 
   49: /* JSON error strings */
   50: const char *jerrstr[] = {
   51: 	"No error",
   52: 	"Not enough tokens were provided",
   53: 	"Invalid character",
   54: 	"JSON string isn't full",
   55: 	"Invalid parameter", 
   56: 	NULL
   57: };
   58: 
   59: 
   60: /*
   61:  * json_init() - Initialize JSON handler
   62:  *
   63:  * @json = JSON handler, if there is NULL then dynamically will be allocated
   64:  * @jstrict = JSON strict mode, when we select strict mode every unquoted value is error
   65:  * return: =NULL error or !=NULL ready for use JSON handler and should be free with json_free()
   66:  */
   67: json_t *
   68: json_init(json_t * __restrict json, int jstrict)
   69: {
   70: 	json_t *j = json;
   71: 
   72: 	if (!j) {
   73: 		j = e_malloc(sizeof(json_t));
   74: 		if (!j) {
   75: 			LOGERR;
   76: 			return NULL;
   77: 		}
   78: 	}
   79: 
   80: 	memset(j, 0, sizeof(json_t));
   81: 	j->h_parent = -1;
   82: 	j->h_strict = jstrict;
   83: 
   84: 	/* handler is dynamically allocated! */
   85: 	if (!json)
   86: 		j->h_alloc = j;
   87: 
   88: 	return j;
   89: }
   90: 
   91: /*
   92:  * json_free() - Free JSON handler
   93:  *
   94:  * @json = JSON handler
   95:  * return: none
   96:  */
   97: void
   98: json_free(json_t * __restrict json)
   99: {
  100: 	if (json) {
  101: 		if (json->h_alloc)
  102: 			e_free(json);
  103: 		else
  104: 			memset(json, 0, sizeof(json_t));
  105: 	}
  106: }
  107: 
  108: static jtok_t *
  109: json_gettoken(json_t * __restrict json, jtok_t * __restrict jtoks, u_int toksnum)
  110: {
  111: 	jtok_t *tok;
  112: 
  113: 	assert(json || !(!jtoks && toksnum));
  114: 
  115: 	if (json->h_next >= toksnum) {
  116: 		elwix_SetErr(J_ERR_NOMEM, "%s", jerrstr[J_ERR_NOMEM]);
  117: 		return NULL;
  118: 	} else
  119: 		tok = &jtoks[json->h_next];
  120: 	tok->tok_idx = json->h_next++;
  121: 	tok->tok_start = tok->tok_end = tok->tok_parent = -1;
  122: 	tok->tok_size = 0;
  123: 
  124: 	return tok;
  125: }
  126: 
  127: inline void
  128: json_filltoken(jtok_t * __restrict tok, jtype_t type, long start, long end, long parent)
  129: {
  130: 	assert(tok);
  131: 
  132: 	tok->tok_type = type;
  133: 	tok->tok_start = start;
  134: 	tok->tok_end = end;
  135: 	tok->tok_parent = parent;
  136: 	tok->tok_size = 0;
  137: }
  138: 
  139: static int
  140: json_parse_string(json_t * __restrict json, const char *jstr, size_t jlen, jtok_t * __restrict jtoks, u_int toksnum)
  141: {
  142: 	jtok_t	*tok;
  143: 	u_long pos;
  144: 	char ch;
  145: 	register int i;
  146: 
  147: 	assert(json || jstr || !(!jtoks && toksnum));
  148: 
  149: 	for (pos = json->h_pos++; json->h_pos < jlen && jstr[json->h_pos]; json->h_pos++) {
  150: 		ch = jstr[json->h_pos];
  151: 
  152: 		if (ch == '\"') {
  153: 			if (!jtoks)
  154: 				return 0;
  155: 			if (!(tok = json_gettoken(json, jtoks, toksnum))) {
  156: 				json->h_pos = pos;
  157: 				return -1;
  158: 			} else
  159: 				json_filltoken(tok, J_STRING, pos + 1, json->h_pos, json->h_parent);
  160: 			return 0;
  161: 		}
  162: 
  163: 		if (ch == '\\' && json->h_pos + 1 < jlen) {
  164: 			switch (jstr[++json->h_pos]) {
  165: 				case '\"':
  166: 				case '/':
  167: 				case '\\':
  168: 				case 'b':
  169: 				case 'f':
  170: 				case 'r':
  171: 				case 'n':
  172: 				case 't':
  173: 					/* Allowed escaped symbols */
  174: 					break;
  175: 				case 'u':
  176: 					/* Allows escaped symbol \uXXXX */
  177: 					json->h_pos++;
  178: 					for (i = 0; i < 4 && json->h_pos < jlen && jstr[json->h_pos]; i++, json->h_pos++) {
  179: 						/* If it isn't a hex character we have an error */
  180: 						if (!((jstr[json->h_pos] >= 48 && jstr[json->h_pos] <= 57) || /* 0-9 */
  181: 								(jstr[json->h_pos] >= 65 && jstr[json->h_pos] <= 70) || /* A-F */
  182: 								(jstr[json->h_pos] >= 97 && jstr[json->h_pos] <= 102))) { /* a-f */
  183: 							json->h_pos = pos;
  184: 							elwix_SetErr(J_ERR_INVAL, "%s", jerrstr[J_ERR_INVAL]);
  185: 							return -1;
  186: 						}
  187: 					}
  188: 					json->h_pos--;
  189: 					break;
  190: 				default:
  191: 					/* Unexpected symbol */
  192: 					json->h_pos = pos;
  193: 					elwix_SetErr(J_ERR_INVAL, "%s", jerrstr[J_ERR_INVAL]);
  194: 					return -1;
  195: 			}
  196: 		}
  197: 	}
  198: 
  199: 	json->h_pos = pos;
  200: 	elwix_SetErr(J_ERR_PART, "%s", jerrstr[J_ERR_PART]);
  201: 	return -1;
  202: }
  203: 
  204: static int
  205: json_parse_value(json_t * __restrict json, const char *jstr, size_t jlen, jtok_t * __restrict jtoks, u_int toksnum)
  206: {
  207: 	jtok_t	*tok;
  208: 	u_long pos;
  209: 
  210: 	assert(json || jstr || !(!jtoks && toksnum));
  211: 
  212: 	for (pos = json->h_pos; json->h_pos < jlen && jstr[json->h_pos]; json->h_pos++) {
  213: 		switch (jstr[json->h_pos]) {
  214: 			case ':':
  215: 				if (json->h_strict)
  216: 					goto found;
  217: 				break;
  218: 			case '\t':
  219: 			case '\r':
  220: 			case '\n':
  221: 			case ' ':
  222: 			case ',':
  223: 			case ']':
  224: 			case '}':
  225: 				goto found;
  226: 		}
  227: 		if (jstr[json->h_pos] < 32 || jstr[json->h_pos] > 127) {
  228: 			json->h_pos = pos;
  229: 			elwix_SetErr(J_ERR_INVAL, "%s", jerrstr[J_ERR_INVAL]);
  230: 			return -1;
  231: 		}
  232: 	}
  233: 
  234: 	if (json->h_strict) {
  235: 		json->h_pos = pos;
  236: 		elwix_SetErr(J_ERR_PART, "%s", jerrstr[J_ERR_PART]);
  237: 		return -1;
  238: 	}
  239: found:
  240: 	if (jtoks) {
  241: 		if (!(tok = json_gettoken(json, jtoks, toksnum))) {
  242: 			json->h_pos = pos;
  243: 			return -1;
  244: 		} else
  245: 			json_filltoken(tok, J_VALUE, pos, json->h_pos, json->h_parent);
  246: 	}
  247: 
  248: 	json->h_pos--;
  249: 	return 0;
  250: }
  251: 
  252: /*
  253:  * json_parse() - Parse JSON string
  254:  *
  255:  * @json = JSON handler
  256:  * @jstr = JSON string
  257:  * @jlen = JSON string length
  258:  * @jtoks = Token array
  259:  * @toksnum = Token array size, return number of allocated tokens in array
  260:  * return: -1 error or number of found tokens 
  261:  */
  262: u_int
  263: json_parse(json_t * __restrict json, const char *jstr, size_t jlen, jtok_t * __restrict jtoks, u_int toksnum)
  264: {
  265: 	register int i;
  266: 	register u_int cx;
  267: 	jtype_t type;
  268: 	jtok_t *tok;
  269: 	char ch;
  270: 
  271: 	if (!json || !jstr || (!jtoks && toksnum)) {
  272: 		elwix_SetErr(J_ERR_PARAM, "%s", jerrstr[J_ERR_PARAM]);
  273: 		return (u_int) -1;
  274: 	}
  275: 
  276: 	for (cx = json->h_next; json->h_pos < jlen && jstr[json->h_pos]; json->h_pos++) {
  277: 		switch ((ch = jstr[json->h_pos])) {
  278: 			case '{':
  279: 			case '[':
  280: 				cx++;	/* start new object/array token */
  281: 				if (!jtoks)
  282: 					break;
  283: 
  284: 				tok = json_gettoken(json, jtoks, toksnum);
  285: 				if (!tok)
  286: 					return (u_int) -1;
  287: 				if (json->h_parent != -1) {
  288: 					jtoks[json->h_parent].tok_size++;
  289: 					tok->tok_parent = json->h_parent;
  290: 				}
  291: 				tok->tok_type = (ch == '{' ? J_OBJECT : J_ARRAY);
  292: 				tok->tok_start = json->h_pos;
  293: 				json->h_parent = json->h_next - 1;
  294: 				break;
  295: 			case '}':
  296: 			case ']':
  297: 				if (!jtoks)
  298: 					break;
  299: 
  300: 				if (json->h_next < 1) {
  301: 					elwix_SetErr(J_ERR_INVAL, "%s", jerrstr[J_ERR_INVAL]);
  302: 					return (u_int) -1;
  303: 				}
  304: 
  305: 				type = (ch == '}' ? J_OBJECT : J_ARRAY);
  306: 				tok = &jtoks[json->h_next - 1];
  307: 				while (42) {
  308: 					if (tok->tok_start != -1 && tok->tok_end == -1) {
  309: 						if (tok->tok_type != type) {
  310: 							elwix_SetErr(J_ERR_INVAL, "%s", jerrstr[J_ERR_INVAL]);
  311: 							return (u_int) -1;
  312: 						}
  313: 						tok->tok_end = json->h_pos + 1;
  314: 						json->h_parent = tok->tok_parent;
  315: 						break;
  316: 					}
  317: 					if (tok->tok_parent == -1) {
  318: 						if (tok->tok_type != type || json->h_parent == -1) {
  319: 							elwix_SetErr(J_ERR_INVAL, "%s", jerrstr[J_ERR_INVAL]);
  320: 							return (u_int) -1;
  321: 						}
  322: 						break;
  323: 					}
  324: 					tok = &jtoks[tok->tok_parent];
  325: 				}
  326: 				break;
  327: 			case '\"':
  328: 				if (json_parse_string(json, jstr, jlen, jtoks, toksnum) == -1) {
  329: 					elwix_SetErr(J_ERR_INVAL, "%s", jerrstr[J_ERR_INVAL]);
  330: 					return (u_int) -1;
  331: 				}
  332: 				cx++;	/* start new string token */
  333: 				if (jtoks && json->h_parent != -1)
  334: 					jtoks[json->h_parent].tok_size++;
  335: 				break;
  336: 			case '\t':
  337: 			case '\r':
  338: 			case '\n':
  339: 			case ' ':
  340: 				/* whitespace, skip */
  341: 				break;
  342: 			case ':':
  343: 				json->h_parent = json->h_next - 1;
  344: 				break;
  345: 			case ',':
  346: 				if (jtoks && json->h_parent != -1 && 
  347: 						jtoks[json->h_parent].tok_type != J_OBJECT && 
  348: 						jtoks[json->h_parent].tok_type != J_ARRAY)
  349: 					json->h_parent = jtoks[json->h_parent].tok_parent;
  350: 				break;
  351: 			case '-':
  352: 			case '0':
  353: 			case '1':
  354: 			case '2':
  355: 			case '3':
  356: 			case '4':
  357: 			case '5':
  358: 			case '6':
  359: 			case '7':
  360: 			case '8':
  361: 			case '9':
  362: 			case 't':
  363: 			case 'f':
  364: 			case 'n':
  365: 				if (json->h_strict) {
  366: 					if (jtoks && json->h_parent != -1) {
  367: 						/* they must not be keys of the object */
  368: 						if (jtoks[json->h_parent].tok_type == J_OBJECT || 
  369: 								(jtoks[json->h_parent].tok_type == J_STRING && 
  370: 								 jtoks[json->h_parent].tok_size)) {
  371: 							elwix_SetErr(J_ERR_INVAL, "%s", jerrstr[J_ERR_INVAL]);
  372: 							return (u_int) -1;
  373: 						}
  374: 					}
  375: 
  376: 					if (json_parse_value(json, jstr, jlen, jtoks, toksnum) == -1)
  377: 						return (u_int) -1;
  378: 					cx++;	/* start new value token */
  379: 					if (jtoks && json->h_parent != -1)
  380: 						jtoks[json->h_parent].tok_size++;
  381: 					break;
  382: 				}
  383: 			default:
  384: 				if (json->h_strict) {
  385: 					elwix_SetErr(J_ERR_INVAL, "%s", jerrstr[J_ERR_INVAL]);
  386: 					return (u_int) -1;
  387: 				}
  388: 
  389: 				if (json_parse_value(json, jstr, jlen, jtoks, toksnum) == -1)
  390: 					return (u_int) -1;
  391: 				cx++;	/* start new value token */
  392: 				if (jtoks && json->h_parent != -1)
  393: 					jtoks[json->h_parent].tok_size++;
  394: 				break;
  395: 		}
  396: 	}
  397: 
  398: 	if (jtoks) {
  399: 		for (i = json->h_next - 1; i >= 0; i--) {
  400: 			/* unmatched opened object or array */
  401: 			if (jtoks[i].tok_start != -1 && jtoks[i].tok_end == -1) {
  402: 				elwix_SetErr(J_ERR_PART, "%s", jerrstr[J_ERR_PART]);
  403: 				return (u_int) -1;
  404: 			}
  405: 		}
  406: 	} else
  407: 		cx++;	/* increment needed tokens number for termination empty token */
  408: 
  409: 	return cx;
  410: }
  411: 
  412: /*
  413:  * json_token2val() - Return token to AIT variable
  414:  *
  415:  * @jstr = JSON string
  416:  * @tok = Token for convert
  417:  * @return =NULL error or !=NULL allocated variable, after use should be ait_freeVar()
  418:  */
  419: ait_val_t *
  420: json_token2val(const char *jstr, jtok_t * __restrict tok)
  421: {
  422: 	ait_val_t *v = NULL;
  423: 
  424: 	if (!jstr || !tok)
  425: 		return NULL;
  426: 
  427: 	v = ait_allocVar();
  428: 	if (!v)
  429: 		return NULL;
  430: 
  431: 	AIT_SET_STRSIZ(v, tok->tok_end - tok->tok_start);
  432: 	strncpy(AIT_GET_STR(v), jstr + tok->tok_start, AIT_LEN(v) - 1);
  433: 
  434: 	return v;
  435: }
  436: 
  437: /*
  438:  * json_token2str() - Return token to string
  439:  *
  440:  * @jstr = JSON string
  441:  * @tok = Token for convert
  442:  * @return =NULL error or !=NULL allocated str, after use should be e_free()
  443:  */
  444: char *
  445: json_token2str(const char *jstr, jtok_t * __restrict tok)
  446: {
  447: 	char *str = NULL;
  448: 	size_t len;
  449: 
  450: 	if (!jstr || !tok)
  451: 		return NULL;
  452: 
  453: 	len = tok->tok_end - tok->tok_start;
  454: 	str = e_malloc(len + 1);
  455: 	if (!str)
  456: 		return NULL;
  457: 	else {
  458: 		strncpy(str, jstr + tok->tok_start, len);
  459: 		str[len] = 0;
  460: 	}
  461: 
  462: 	return str;
  463: }
  464: 
  465: /*
  466:  * json_token2num() - Return token to numeric
  467:  *
  468:  * @jstr = JSON string
  469:  * @tok = Token for convert
  470:  * @return number
  471:  */
  472: long
  473: json_token2num(const char *jstr, jtok_t * __restrict tok)
  474: {
  475: 	long ret = 0;
  476: 	char *str;
  477: 
  478: 	str = json_token2str(jstr, tok);
  479: 	if (!str)
  480: 		return 0;
  481: 
  482: 	ret = strtol(str, NULL, 0);
  483: 	e_free(str);
  484: 	return ret;
  485: }
  486: 
  487: /*
  488:  * json_findbykey() - Find data by key
  489:  *
  490:  * @jstr = JSON string
  491:  * @key = Search key
  492:  * @toks = Parsed tokens
  493:  * @toksnum = Number of parsed tokens
  494:  * return: =NULL error or !=NULL data token found 
  495:  */
  496: jtok_t *
  497: json_findbykey(const char *jstr, const char *key, jtok_t * __restrict toks, int toksnum)
  498: {
  499: 	jtok_t *tok = NULL;
  500: 	register int i;
  501: 	int klen;
  502: 
  503: 	if (!jstr || !key || !toks)
  504: 		return NULL;
  505: 	else
  506: 		klen = strlen(key);
  507: 
  508: 	for (i = 1; i < toksnum; i++) {
  509: 		if (toks[i].tok_type == J_STRING && 
  510: 				klen == toks[i].tok_end - toks[i].tok_start && 
  511: 				!strncmp(jstr + toks[i].tok_start, key, klen)) {
  512: 			tok = toks + i + 1;
  513: 			break;
  514: 		}
  515: 	}
  516: 
  517: 	return tok;
  518: }
  519: 
  520: /*
  521:  * json_token2array() - Convert token to string array
  522:  *
  523:  * @jstr = JSON string
  524:  * @tok = Token for convert
  525:  * return: =NULL error or !=NULL allocated array with string variables, 
  526:  * 		after use should be ait_freeVars()
  527:  */
  528: array_t *
  529: json_token2array(const char *jstr, jtok_t * __restrict tok)
  530: {
  531: 	array_t *arr = NULL;
  532: 	register int i;
  533: 	int siz;
  534: 	ait_val_t *v;
  535: 	jtok_t *t;
  536: 
  537: 	if (!jstr || !tok)
  538: 		return NULL;
  539: 
  540: 	siz = tok->tok_size;
  541: 	if (!siz && json_toktype(tok) != J_ARRAY && json_toktype(tok) != J_OBJECT)
  542: 		siz++;
  543: 
  544: 	arr = ait_allocVars(siz);
  545: 	if (!arr)
  546: 		return NULL;
  547: 
  548: 	if (tok->tok_type == J_STRING || tok->tok_type == J_VALUE) {
  549: 		v = ait_getVars(&arr, 0);
  550: 		AIT_SET_STRSIZ(v, json_toklen(tok) + 1);
  551: 		json_tokstrcpy(AIT_GET_STR(v), jstr, tok);
  552: 	} else if (tok->tok_type == J_ARRAY) {
  553: 		for (i = 0; i < tok->tok_size; i++) {
  554: 			t = &tok[i + 1];
  555: 			v = ait_getVars(&arr, i);
  556: 			AIT_SET_STRSIZ(v, json_toklen(t) + 1);
  557: 			json_tokstrcpy(AIT_GET_STR(v), jstr, t);
  558: 		}
  559: 	} else if (tok->tok_type == J_OBJECT) {
  560: 		for (i = 0; tok->tok_idx <= tok[i + 1].tok_parent; i++) {
  561: 			t = &tok[i + 1];
  562: 			v = ait_getVars(&arr, i);
  563: 			AIT_SET_STRSIZ(v, json_toklen(t) + 1);
  564: 			json_tokstrcpy(AIT_GET_STR(v), jstr, t);
  565: 		}
  566: 	} else {
  567: 		elwix_SetErr(J_ERR_PARAM, "%s", jerrstr[J_ERR_PARAM]);
  568: 		return NULL;
  569: 	}
  570: 
  571: 	return arr;
  572: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>