summaryrefslogtreecommitdiffhomepage
path: root/libs/luci-lib-jsonc/src/jsonc.c
blob: 5abb738f5f912ff3f96947d52f7710b7b9fbe648 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/*
Copyright 2015 Jo-Philipp Wich <jow@openwrt.org>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#define _GNU_SOURCE

#include <math.h>
#include <stdint.h>
#include <stdbool.h>
#include <json-c/json.h>

#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

#define LUCI_JSONC "luci.jsonc"
#define LUCI_JSONC_PARSER "luci.jsonc.parser"

struct seen {
	size_t size;
	size_t len;
	const void *ptrs[];
};

struct json_state {
	struct json_object *obj;
	struct json_tokener *tok;
	enum json_tokener_error err;
};

static void _json_to_lua(lua_State *L, struct json_object *obj);
static struct json_object * _lua_to_json(lua_State *L, int index);
static struct json_object * _lua_to_json_rec(lua_State *L, int index, struct seen **seen);

static int json_new(lua_State *L)
{
	struct json_state *s;
	struct json_tokener *tok = json_tokener_new();

	if (!tok)
		return 0;

	s = lua_newuserdata(L, sizeof(*s));

	if (!s)
	{
		json_tokener_free(tok);
		return 0;
	}

	s->tok = tok;
	s->obj = NULL;
	s->err = json_tokener_continue;

	luaL_getmetatable(L, LUCI_JSONC_PARSER);
	lua_setmetatable(L, -2);

	return 1;
}

static int json_parse(lua_State *L)
{
	size_t len;
	const char *json = luaL_checklstring(L, 1, &len);
	struct json_state s = {
		.tok = json_tokener_new()
	};

	if (!s.tok)
		return 0;

	s.obj = json_tokener_parse_ex(s.tok, json, len);
	s.err = json_tokener_get_error(s.tok);

	if (s.obj)
	{
		_json_to_lua(L, s.obj);
		json_object_put(s.obj);
	}
	else
	{
		lua_pushnil(L);
	}

	if (s.err == json_tokener_continue)
		s.err = json_tokener_error_parse_eof;

	if (s.err)
		lua_pushstring(L, json_tokener_error_desc(s.err));

	json_tokener_free(s.tok);
	return (1 + !!s.err);
}

static int json_stringify(lua_State *L)
{
	struct json_object *obj = _lua_to_json(L, 1);
	bool pretty = lua_toboolean(L, 2);
	int flags = 0;

	if (pretty)
		flags |= JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED;

	lua_pushstring(L, json_object_to_json_string_ext(obj, flags));
	json_object_put(obj);
	return 1;
}


static int json_parse_chunk(lua_State *L)
{
	size_t len;
	struct json_state *s = luaL_checkudata(L, 1, LUCI_JSONC_PARSER);
	const char *chunk = luaL_checklstring(L, 2, &len);

	s->obj = json_tokener_parse_ex(s->tok, chunk, len);
    s->err = json_tokener_get_error(s->tok);

	if (!s->err)
	{
		lua_pushboolean(L, true);
		return 1;
	}
	else if (s->err == json_tokener_continue)
	{
		lua_pushboolean(L, false);
		return 1;
	}

	lua_pushnil(L);
	lua_pushstring(L, json_tokener_error_desc(s->err));
	return 2;
}

static void _json_to_lua(lua_State *L, struct json_object *obj)
{
	int64_t v;
	int n;

	switch (json_object_get_type(obj))
	{
	case json_type_object:
		lua_newtable(L);
		json_object_object_foreach(obj, key, val)
		{
			_json_to_lua(L, val);
			lua_setfield(L, -2, key);
		}
		break;

	case json_type_array:
		lua_newtable(L);
		for (n = 0; n < json_object_array_length(obj); n++)
		{
			_json_to_lua(L, json_object_array_get_idx(obj, n));
			lua_rawseti(L, -2, n + 1);
		}
		break;

	case json_type_boolean:
		lua_pushboolean(L, json_object_get_boolean(obj));
		break;

	case json_type_int:
		v = json_object_get_int64(obj);
		if (sizeof(lua_Integer) > sizeof(int32_t) ||
		    (v >= INT32_MIN && v <= INT32_MAX))
			lua_pushinteger(L, (lua_Integer)v);
		else
			lua_pushnumber(L, (lua_Number)v);
		break;

	case json_type_double:
		lua_pushnumber(L, json_object_get_double(obj));
		break;

	case json_type_string:
		lua_pushstring(L, json_object_get_string(obj));
		break;

	case json_type_null:
		lua_pushnil(L);
		break;
	}
}

static int json_parse_get(lua_State *L)
{
	struct json_state *s = luaL_checkudata(L, 1, LUCI_JSONC_PARSER);

	if (!s->obj || s->err)
		lua_pushnil(L);
	else
		_json_to_lua(L, s->obj);

	return 1;
}

static int _lua_test_array(lua_State *L, int index)
{
	int max = 0;
	lua_Number idx;

	if (!lua_checkstack(L, 2))
		return -1;

	lua_pushnil(L);

	/* check for non-integer keys */
	while (lua_next(L, index))
	{
		if (lua_type(L, -2) != LUA_TNUMBER)
			goto out;

		idx = lua_tonumber(L, -2);

		if (idx != (lua_Number)(lua_Integer)idx)
			goto out;

		if (idx <= 0)
			goto out;

		if (idx > max)
			max = idx;

		lua_pop(L, 1);
		continue;

out:
		lua_pop(L, 2);
		return -1;
	}

	/* check for holes */
	//for (i = 1; i <= max; i++)
	//{
	//	lua_rawgeti(L, index, i);
	//
	//	if (lua_isnil(L, -1))
	//	{
	//		lua_pop(L, 1);
	//		return 0;
	//	}
	//
	//	lua_pop(L, 1);
	//}

	return max;
}


static bool visited(struct seen **sp, const void *ptr) {
	struct seen *s = *sp;
	size_t i;

	if (s->len >= s->size)
	{
		i = s->size + 10;
		s = realloc(*sp, sizeof(struct seen) + sizeof(void *) * i);

		if (!s)
		{
			if (*sp)
				free(*sp);

			*sp = NULL;
			return true;
		}

		s->size = i;
		*sp = s;
	}

	for (i = 0; i < s->len; i++)
		if (s->ptrs[i] == ptr)
			return true;

	s->ptrs[s->len++] = ptr;
	return false;
}

static struct json_object * _lua_to_json_rec(lua_State *L, int index,
                                             struct seen **seen)
{
	lua_Number nd;
	struct json_object *obj;
	const char *key;
	int i, max;

	if (index < 0)
		index = lua_gettop(L) + index + 1;

	switch (lua_type(L, index))
	{
	case LUA_TTABLE:
		if (visited(seen, lua_topointer(L, index)))
			return NULL;

		max = _lua_test_array(L, index);

		if (max >= 0)
		{
			obj = json_object_new_array();

			if (!obj)
				return NULL;

			if (!lua_checkstack(L, 1))
				return NULL;

			for (i = 1; i <= max; i++)
			{
				lua_rawgeti(L, index, i);

				json_object_array_put_idx(obj, i - 1,
				                          _lua_to_json_rec(L, -1, seen));

				lua_pop(L, 1);
			}

			return obj;
		}

		obj = json_object_new_object();

		if (!obj)
			return NULL;

		if (!lua_checkstack(L, 3))
			return NULL;

		lua_pushnil(L);

		while (lua_next(L, index))
		{
			lua_pushvalue(L, -2);
			key = lua_tostring(L, -1);

			if (key)
				json_object_object_add(obj, key,
				                       _lua_to_json_rec(L, -2, seen));

			lua_pop(L, 2);
		}

		return obj;

	case LUA_TNIL:
		return NULL;

	case LUA_TBOOLEAN:
		return json_object_new_boolean(lua_toboolean(L, index));

	case LUA_TNUMBER:
		if (lua_isinteger(L, index))
			return json_object_new_int64(lua_tointeger(L, index));

		nd = lua_tonumber(L, index);

		if (isfinite(nd) && trunc(nd) == nd)
			return json_object_new_int64(nd);

		return json_object_new_double(nd);

	case LUA_TSTRING:
		return json_object_new_string(lua_tostring(L, index));
	}

	return NULL;
}

static struct json_object * _lua_to_json(lua_State *L, int index)
{
	struct seen *s = calloc(sizeof(struct seen) + sizeof(void *) * 10, 1);
	struct json_object *rv;

	if (!s)
		return NULL;

	s->size = 10;

	rv = _lua_to_json_rec(L, index, &s);

	free(s);

	return rv;
}

static int json_parse_set(lua_State *L)
{
	struct json_state *s = luaL_checkudata(L, 1, LUCI_JSONC_PARSER);

	s->err = 0;
	s->obj = _lua_to_json(L, 2);

	return 0;
}

static int json_parse_sink_closure(lua_State *L)
{
	bool finished = lua_toboolean(L, lua_upvalueindex(2));
	if (lua_isnil(L, 1))
	{
		// no more data available
		if (finished)
		{
			// we were finished parsing
			lua_pushboolean(L, true);
			return 1;
		}
		else
		{
			lua_pushnil(L);
			lua_pushstring(L, "Incomplete JSON data");
			return 2;
		}
	}
	else
	{
		if (finished)
		{
			lua_pushnil(L);
			lua_pushstring(L, "Unexpected data after complete JSON object");
			return 2;
		}
		else
		{
			// luci.jsonc.parser.chunk()
			lua_pushcfunction(L, json_parse_chunk);
			// parser object from closure
			lua_pushvalue(L, lua_upvalueindex(1));
			// chunk
			lua_pushvalue(L, 1);
			lua_call(L, 2, 2);

			if (lua_isnil(L, -2))
			{
				// an error occurred, leave (nil, errmsg) on the stack and return it
				return 2;
			}
			else if (lua_toboolean(L, -2))
			{
				// finished reading, set finished=true and return nil to prevent further input
				lua_pop(L, 2);
				lua_pushboolean(L, true);
				lua_replace(L, lua_upvalueindex(2));
				lua_pushnil(L);
				return 1;
			}
			else
			{
				// not finished reading, return true
				lua_pop(L, 2);
				lua_pushboolean(L, true);
				return 1;
			}
		}
	}
}

static int json_parse_sink(lua_State *L)
{
	luaL_checkudata(L, 1, LUCI_JSONC_PARSER);
	lua_pushboolean(L, false);
	lua_pushcclosure(L, json_parse_sink_closure, 2);
	return 1;
}

static int json_tostring(lua_State *L)
{
	struct json_state *s = luaL_checkudata(L, 1, LUCI_JSONC_PARSER);
	bool pretty = lua_toboolean(L, 2);
	int flags = 0;

	if (pretty)
		flags |= JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED;

	lua_pushstring(L, json_object_to_json_string_ext(s->obj, flags));
	return 1;
}

static int json_gc(lua_State *L)
{
	struct json_state *s = luaL_checkudata(L, 1, LUCI_JSONC_PARSER);

	if (s->obj)
		json_object_put(s->obj);

	if (s->tok)
		json_tokener_free(s->tok);

	return 0;
}


static const luaL_reg jsonc_methods[] = {
	{ "new",			json_new          },
	{ "parse",			json_parse        },
	{ "stringify",		json_stringify    },

	{ }
};

static const luaL_reg jsonc_parser_methods[] = {
	{ "parse",			json_parse_chunk  },
	{ "get",			json_parse_get    },
	{ "set",			json_parse_set    },
	{ "sink",			json_parse_sink   },
	{ "stringify",		json_tostring     },

	{ "__gc",			json_gc           },
	{ "__tostring",		json_tostring     },

	{ }
};


int luaopen_luci_jsonc(lua_State *L)
{
	luaL_register(L, LUCI_JSONC, jsonc_methods);

	luaL_newmetatable(L, LUCI_JSONC_PARSER);
	luaL_register(L, NULL, jsonc_parser_methods);
	lua_pushvalue(L, -1);
	lua_setfield(L, -2, "__index");
	lua_pop(L, 1);

	return 1;
}