eiwd

no dbus iwd with iwctl functionality
git clone https://git.daat.foo/repos/eiwd.git
Log | Files | Refs | Submodules | README | LICENSE

json.c (9834B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2021  Intel Corporation. All rights reserved.
      6  *
      7  *  This library is free software; you can redistribute it and/or
      8  *  modify it under the terms of the GNU Lesser General Public
      9  *  License as published by the Free Software Foundation; either
     10  *  version 2.1 of the License, or (at your option) any later version.
     11  *
     12  *  This library is distributed in the hope that it will be useful,
     13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  *  Lesser General Public License for more details.
     16  *
     17  *  You should have received a copy of the GNU Lesser General Public
     18  *  License along with this library; if not, write to the Free Software
     19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     20  *
     21  */
     22 
     23 #ifdef HAVE_CONFIG_H
     24 #include <config.h>
     25 #endif
     26 
     27 #include <errno.h>
     28 
     29 #include <ell/ell.h>
     30 
     31 #include "src/json.h"
     32 
     33 #define JSMN_STATIC
     34 #define JSMN_STRICT
     35 #define JSMN_PARENT_LINKS
     36 #include "shared/jsmn.h"
     37 
     38 /* Max number of tokens supported. Increase if larger objects are expected */
     39 #define JSON_DEFAULT_TOKENS 60
     40 
     41 #define TOK_LEN(token) ((token)->end - (token)->start)
     42 #define TOK_PTR(json, token) (void *)((json) + (token)->start)
     43 #define TOK_TO_STR(json, token) \
     44 ({ \
     45 	char *_tmp = l_malloc(TOK_LEN((token)) + 1); \
     46 	memcpy(_tmp, TOK_PTR((json), (token)), TOK_LEN((token))); \
     47 	_tmp[TOK_LEN((token))] = '\0'; \
     48 	_tmp; \
     49 })
     50 
     51 #define ITER_END(iter) \
     52 	(((iter)->contents->tokens + (iter)->start) + (iter)->count)
     53 
     54 struct json_contents {
     55 	const char *json;
     56 	size_t json_len;
     57 	jsmntok_t *tokens;
     58 	int tokens_len;
     59 	jsmn_parser *p;
     60 };
     61 
     62 static jsmntok_t *next_key_in_parent(struct json_iter *iter, jsmntok_t *current)
     63 {
     64 	int parent = current->parent;
     65 
     66 	/* iterate the list and stop on the first token with the same parent */
     67 	while (++current < ITER_END(iter)) {
     68 		if (current->parent == parent)
     69 			return current;
     70 	}
     71 
     72 	return NULL;
     73 }
     74 
     75 static int count_tokens_in_container(struct json_iter *iter,
     76 							jsmntok_t *container)
     77 {
     78 	int idx = container - iter->contents->tokens;
     79 	jsmntok_t *contents;
     80 
     81 	for (contents = ++container; contents < ITER_END(iter); contents++)
     82 		if (contents->parent < idx)
     83 			break;
     84 
     85 	return contents - container;
     86 }
     87 
     88 static void iter_recurse(struct json_iter *iter, jsmntok_t *token,
     89 				struct json_iter *child)
     90 {
     91 	struct json_contents *c = iter->contents;
     92 
     93 	child->contents = c;
     94 	child->start = token - c->tokens;
     95 	child->current = child->start;
     96 	child->count = count_tokens_in_container(iter, token);
     97 
     98 	/*
     99 	 * Add one to include the object/array token itself. This is required
    100 	 * since 'current' points to the container initially. Only after a call
    101 	 * to json_iter_next() will 'current' point to the first token in the
    102 	 * container.
    103 	 */
    104 	if (token->type == JSMN_OBJECT || token->type == JSMN_ARRAY)
    105 		child->count++;
    106 }
    107 
    108 struct json_contents *json_contents_new(const char *json, size_t json_len)
    109 {
    110 	struct json_contents *c = l_new(struct json_contents, 1);
    111 
    112 	c->json = json;
    113 	c->json_len = json_len;
    114 	c->p = l_new(jsmn_parser, 1);
    115 	c->tokens = l_new(jsmntok_t, JSON_DEFAULT_TOKENS);
    116 
    117 	jsmn_init(c->p);
    118 	c->tokens_len = jsmn_parse(c->p, c->json, c->json_len,
    119 					c->tokens, JSON_DEFAULT_TOKENS);
    120 	if (c->tokens_len < 0) {
    121 		json_contents_free(c);
    122 		return NULL;
    123 	}
    124 
    125 	return c;
    126 }
    127 
    128 void json_iter_init(struct json_iter *iter, struct json_contents *c)
    129 {
    130 	iter->contents = c;
    131 	iter->start = 0;
    132 	iter->count = c->tokens_len;
    133 }
    134 
    135 void json_contents_free(struct json_contents *c)
    136 {
    137 	l_free(c->p);
    138 	l_free(c->tokens);
    139 	l_free(c);
    140 }
    141 
    142 struct json_arg {
    143 	enum json_type type;
    144 	void *value;
    145 	jsmntok_t *v;
    146 };
    147 
    148 static void push_arg(struct l_queue *q, enum json_type type,
    149 			void *value, jsmntok_t *v)
    150 {
    151 	struct json_arg *arg = l_new(struct json_arg, 1);
    152 
    153 	arg->type = type;
    154 	arg->value = value;
    155 	arg->v = v;
    156 
    157 	l_queue_push_head(q, arg);
    158 }
    159 
    160 static void assign_arg(void *data, void *user_data)
    161 {
    162 	struct json_iter *iter = user_data;
    163 	struct json_arg *arg = data;
    164 	struct json_contents *c = iter->contents;
    165 	char **sval;
    166 	struct json_iter *iter_val;
    167 
    168 	switch (arg->type) {
    169 	case JSON_STRING:
    170 		sval = arg->value;
    171 
    172 		*sval = arg->v ? TOK_TO_STR(c->json, arg->v) : NULL;
    173 
    174 		break;
    175 	case JSON_OBJECT:
    176 	case JSON_PRIMITIVE:
    177 	case JSON_ARRAY:
    178 		iter_val = arg->value;
    179 
    180 		if (!arg->v)
    181 			iter_val->start = -1;
    182 		else
    183 			iter_recurse(iter, arg->v, iter_val);
    184 
    185 		break;
    186 	default:
    187 		/* Types are verified earlier, this should never happen */
    188 		return;
    189 	}
    190 
    191 	l_free(arg);
    192 }
    193 
    194 bool json_iter_parse(struct json_iter *iter, enum json_type type, ...)
    195 {
    196 	struct json_contents *c = iter->contents;
    197 	va_list va;
    198 	int i;
    199 	int num = c->tokens->size;
    200 	jsmntok_t *next;
    201 	struct l_queue *args;
    202 
    203 	if (iter->start == -1)
    204 		return false;
    205 
    206 	if (c->tokens[iter->start].type != JSMN_OBJECT)
    207 		return false;
    208 
    209 	args = l_queue_new();
    210 
    211 	va_start(va, type);
    212 
    213 	while (true) {
    214 		enum json_flag flag;
    215 		char *key;
    216 		void *value;
    217 		jsmntok_t *v = NULL;
    218 		void *ptr;
    219 		size_t len;
    220 
    221 		/* Check the type is supported before wasting any cycles */
    222 		switch (type) {
    223 		case JSON_UNDEFINED:
    224 			goto done;
    225 		case JSON_STRING:
    226 		case JSON_OBJECT:
    227 		case JSON_PRIMITIVE:
    228 		case JSON_ARRAY:
    229 			break;
    230 		default:
    231 			goto error;
    232 		}
    233 
    234 		key = va_arg(va, char *);
    235 		value = va_arg(va, void *);
    236 		flag = va_arg(va, enum json_flag);
    237 
    238 		/* First key */
    239 		next = c->tokens + iter->start + 1;
    240 
    241 		/* Iterate over this objects keys */
    242 		for (i = 0; i < num; i++) {
    243 			ptr = TOK_PTR(c->json, next);
    244 			len = TOK_LEN(next);
    245 
    246 			if (next + 1 > ITER_END(iter))
    247 				goto error;
    248 
    249 			if (strlen(key) == len && !memcmp(ptr, key, len)) {
    250 				/* Key found but the wrong value type */
    251 				if ((next + 1)->type != (jsmntype_t)type)
    252 					goto error;
    253 
    254 				v = next + 1;
    255 				break;
    256 			}
    257 
    258 			next = next_key_in_parent(iter, next);
    259 			if (!next)
    260 				break;
    261 		}
    262 
    263 		if (flag == JSON_FLAG_MANDATORY && !v)
    264 			goto error;
    265 
    266 		/*
    267 		 * Still push even if an optional value doesn't exist (!v) so
    268 		 * the caller can check if it was found or not.
    269 		 */
    270 		if (value)
    271 			push_arg(args, type, value, v);
    272 
    273 		type = va_arg(va, enum json_type);
    274 	}
    275 
    276 done:
    277 	va_end(va);
    278 
    279 	l_queue_foreach(args, assign_arg, iter);
    280 	l_queue_destroy(args, NULL);
    281 
    282 	return true;
    283 
    284 error:
    285 	va_end(va);
    286 
    287 	l_queue_destroy(args, l_free);
    288 	return false;
    289 }
    290 
    291 static bool iter_get_primitive_data(struct json_iter *iter, void **ptr,
    292 					size_t *len)
    293 {
    294 	struct json_contents *c = iter->contents;
    295 	jsmntok_t *t = c->tokens + iter->current;
    296 
    297 	if (t->type != JSMN_PRIMITIVE)
    298 		return false;
    299 
    300 	*ptr = TOK_PTR(c->json, t);
    301 	*len = TOK_LEN(t);
    302 
    303 	return true;
    304 }
    305 
    306 bool json_iter_get_int(struct json_iter *iter, int *i)
    307 {
    308 	void *ptr;
    309 	size_t len;
    310 	long r;
    311 	int t;
    312 	char *endp;
    313 
    314 	if (!iter_get_primitive_data(iter, &ptr, &len))
    315 		return false;
    316 
    317 	if (!len)
    318 		return false;
    319 
    320 	errno = 0;
    321 
    322 	t = r = strtol(ptr, &endp, 10);
    323 	if (endp != ptr + len)
    324 		return false;
    325 
    326 	if (errno == ERANGE || errno == EINVAL || r != t)
    327 		return false;
    328 
    329 	if (i)
    330 		*i = r;
    331 
    332 	return true;
    333 }
    334 
    335 bool json_iter_get_uint(struct json_iter *iter, unsigned int *i)
    336 {
    337 	void *ptr;
    338 	size_t len;
    339 	unsigned long r;
    340 	unsigned int t;
    341 	char *endp;
    342 
    343 	if (!iter_get_primitive_data(iter, &ptr, &len))
    344 		return false;
    345 
    346 	if (!len || *((char *) ptr) == '-')
    347 		return false;
    348 
    349 	errno = 0;
    350 
    351 	t = r = strtoul(ptr, &endp, 10);
    352 	if (endp != ptr + len)
    353 		return false;
    354 
    355 	if (errno == ERANGE || errno == EINVAL || r != t)
    356 		return false;
    357 
    358 	if (i)
    359 		*i = r;
    360 
    361 	return true;
    362 }
    363 
    364 bool json_iter_get_boolean(struct json_iter *iter, bool *b)
    365 {
    366 	void *ptr;
    367 	size_t len;
    368 
    369 	if (!iter_get_primitive_data(iter, &ptr, &len))
    370 		return false;
    371 
    372 	if (len == 4 && !memcmp(ptr, "true", 4)) {
    373 		if (b)
    374 			*b = true;
    375 
    376 		return true;
    377 	} else if (len == 5 && !memcmp(ptr, "false", 5)) {
    378 		if (b)
    379 			*b = false;
    380 
    381 		return true;
    382 	}
    383 
    384 	return false;
    385 }
    386 
    387 bool json_iter_get_null(struct json_iter *iter)
    388 {
    389 	void *ptr;
    390 	size_t len;
    391 
    392 	if (!iter_get_primitive_data(iter, &ptr, &len))
    393 		return false;
    394 
    395 	if (len == 4 && !memcmp(ptr, "null", 4))
    396 		return true;
    397 
    398 	return false;
    399 }
    400 
    401 bool json_iter_get_container(struct json_iter *iter,
    402 				struct json_iter *container)
    403 {
    404 	struct json_contents *c = iter->contents;
    405 	jsmntok_t *t = c->tokens + iter->current;
    406 
    407 	if (t->type != JSMN_OBJECT && t->type != JSMN_ARRAY)
    408 		return false;
    409 
    410 	if (container)
    411 		iter_recurse(iter, t, container);
    412 
    413 	return true;
    414 }
    415 
    416 bool json_iter_get_string(struct json_iter *iter, char **s)
    417 {
    418 	struct json_contents *c = iter->contents;
    419 	jsmntok_t *t = c->tokens + iter->current;
    420 
    421 	if (t->type != JSMN_STRING)
    422 		return false;
    423 
    424 	if (s)
    425 		*s = TOK_TO_STR(c->json, t);
    426 
    427 	return true;
    428 }
    429 
    430 enum json_type json_iter_get_type(struct json_iter *iter)
    431 {
    432 	struct json_contents *c = iter->contents;
    433 	jsmntok_t *t = c->tokens + iter->current;
    434 
    435 	return (enum json_type) t->type;
    436 }
    437 
    438 bool json_iter_next(struct json_iter *iter)
    439 {
    440 	struct json_contents *c = iter->contents;
    441 	jsmntok_t *t = c->tokens + iter->current;
    442 	int inc = 1;
    443 
    444 	if (c->tokens[iter->start].type != JSMN_ARRAY)
    445 		return false;
    446 
    447 	/*
    448 	 * If this is the initial iteration skip this and just increment
    449 	 * current by 1 since this iterator points to a container which needs to
    450 	 * be advanced to the first token..
    451 	 *
    452 	 * In addition primitive types and empty containers will have a size
    453 	 * of 1, so no special handling is needed there.
    454 	 *
    455 	 * For non-empty containers 'current' needs to be advanced by all the
    456 	 * containers child tokens, plus the container itself.
    457 	 *
    458 	 * This check ensures:
    459 	 *    1. It is not the initial iteration
    460 	 *    2. This is a container
    461 	 *    3. The container is not empty
    462 	 */
    463 	if (iter->current != iter->start && ((t->type == JSMN_OBJECT ||
    464 					t->type == JSMN_ARRAY) && t->size))
    465 		inc = count_tokens_in_container(iter, t) + 1;
    466 
    467 	if (c->tokens + iter->current + inc >= ITER_END(iter))
    468 		return false;
    469 
    470 	iter->current += inc;
    471 
    472 	return true;
    473 }