summaryrefslogtreecommitdiff
path: root/matcher.c
blob: d2a8767c7ca399a204e0c52dcc4b69d6885c8880 (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
/*
 * Copyright (C) 2013-2014 Jo-Philipp Wich <jo@mein.io>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "parser.h"
#include "matcher.h"


static struct json_object *
jp_match_next(struct jp_opcode *ptr,
              struct json_object *root, struct json_object *cur,
              jp_match_cb_t cb, void *priv);

static bool
jp_json_to_op(struct json_object *obj, struct jp_opcode *op)
{
	switch (json_object_get_type(obj))
	{
	case json_type_boolean:
		op->type = T_BOOL;
		op->num = json_object_get_boolean(obj);
		return true;

	case json_type_int:
		op->type = T_NUMBER;
		op->num = json_object_get_int(obj);
		return true;

	case json_type_string:
		op->type = T_STRING;
		op->str = (char *)json_object_get_string(obj);
		return true;

	default:
		return false;
	}
}

static bool
jp_resolve(struct json_object *root, struct json_object *cur,
           struct jp_opcode *op, struct jp_opcode *res)
{
	struct json_object *val;

	switch (op->type)
	{
	case T_THIS:
		val = jp_match(op, cur, NULL, NULL);

		if (val)
			return jp_json_to_op(val, res);

		return false;

	case T_ROOT:
		val = jp_match(op, root, NULL, NULL);

		if (val)
			return jp_json_to_op(val, res);

		return false;

	default:
		*res = *op;
		return true;
	}
}

static bool
jp_cmp(struct jp_opcode *op, struct json_object *root, struct json_object *cur)
{
	int delta;
	struct jp_opcode left, right;

	if (!jp_resolve(root, cur, op->down, &left) ||
        !jp_resolve(root, cur, op->down->sibling, &right))
		return false;

	if (left.type != right.type)
		return false;

	switch (left.type)
	{
	case T_BOOL:
	case T_NUMBER:
		delta = left.num - right.num;
		break;

	case T_STRING:
		delta = strcmp(left.str, right.str);
		break;

	default:
		return false;
	}

	switch (op->type)
	{
	case T_EQ:
		return (delta == 0);

	case T_LT:
		return (delta < 0);

	case T_LE:
		return (delta <= 0);

	case T_GT:
		return (delta > 0);

	case T_GE:
		return (delta >= 0);

	case T_NE:
		return (delta != 0);

	default:
		return false;
	}
}

static bool
jp_regmatch(struct jp_opcode *op, struct json_object *root, struct json_object *cur)
{
	struct jp_opcode left, right;
	char lbuf[22], rbuf[22], *lval, *rval;
	int err, rflags = REG_NOSUB | REG_NEWLINE;
	regex_t preg;


	if (!jp_resolve(root, cur, op->down, &left) ||
	    !jp_resolve(root, cur, op->down->sibling, &right))
		return false;

	if (left.type == T_REGEXP)
	{
		switch (right.type)
		{
		case T_BOOL:
			lval = right.num ? "true" : "false";
			break;

		case T_NUMBER:
			snprintf(lbuf, sizeof(lbuf), "%d", right.num);
			lval = lbuf;
			break;

		case T_STRING:
			lval = right.str;
			break;

		default:
			return false;
		}

		rval = left.str;
		rflags = left.num;
	}
	else
	{
		switch (left.type)
		{
		case T_BOOL:
			lval = left.num ? "true" : "false";
			break;

		case T_NUMBER:
			snprintf(lbuf, sizeof(lbuf), "%d", left.num);
			lval = lbuf;
			break;

		case T_STRING:
			lval = left.str;
			break;

		default:
			return false;
		}

		switch (right.type)
		{
		case T_BOOL:
			rval = right.num ? "true" : "false";
			break;

		case T_NUMBER:
			snprintf(rbuf, sizeof(rbuf), "%d", right.num);
			rval = rbuf;
			break;

		case T_STRING:
			rval = right.str;
			break;

		case T_REGEXP:
			rval = right.str;
			rflags = right.num;
			break;

		default:
			return false;
		}
	}

	if (regcomp(&preg, rval, rflags))
		return false;

	err = regexec(&preg, lval, 0, NULL, 0);

	regfree(&preg);

	return err ? false : true;
}

static bool
jp_expr(struct jp_opcode *op, struct json_object *root, struct json_object *cur,
        int idx, const char *key, jp_match_cb_t cb, void *priv)
{
	struct jp_opcode *sop;

	switch (op->type)
	{
	case T_WILDCARD:
		return true;

	case T_EQ:
	case T_NE:
	case T_LT:
	case T_LE:
	case T_GT:
	case T_GE:
		return jp_cmp(op, root, cur);

	case T_MATCH:
		return jp_regmatch(op, root, cur);

	case T_ROOT:
		return !!jp_match(op, root, NULL, NULL);

	case T_THIS:
		return !!jp_match(op, cur, NULL, NULL);

	case T_NOT:
		return !jp_expr(op->down, root, cur, idx, key, cb, priv);

	case T_AND:
		for (sop = op->down; sop; sop = sop->sibling)
			if (!jp_expr(sop, root, cur, idx, key, cb, priv))
				return false;
		return true;

	case T_OR:
	case T_UNION:
		for (sop = op->down; sop; sop = sop->sibling)
			if (jp_expr(sop, root, cur, idx, key, cb, priv))
				return true;
		return false;

	case T_STRING:
		return (key && !strcmp(op->str, key));

	case T_NUMBER:
		return (idx == op->num);

	default:
		return false;
	}
}

static struct json_object *
jp_match_expr(struct jp_opcode *ptr,
              struct json_object *root, struct json_object *cur,
              jp_match_cb_t cb, void *priv)
{
	int idx, len;
	struct json_object *tmp, *res = NULL;

	switch (json_object_get_type(cur))
	{
	case json_type_object:
		; /* a label can only be part of a statement and a declaration is not a statement */
		json_object_object_foreach(cur, key, val)
		{
			if (jp_expr(ptr, root, val, -1, key, cb, priv))
			{
				tmp = jp_match_next(ptr->sibling, root, val, cb, priv);

				if (tmp && !res)
					res = tmp;
			}
		}

		break;

	case json_type_array:
		len = json_object_array_length(cur);

		for (idx = 0; idx < len; idx++)
		{
			tmp = json_object_array_get_idx(cur, idx);

			if (jp_expr(ptr, root, tmp, idx, NULL, cb, priv))
			{
				tmp = jp_match_next(ptr->sibling, root, tmp, cb, priv);

				if (tmp && !res)
					res = tmp;
			}
		}

		break;

	default:
		break;
	}

	return res;
}

static struct json_object *
jp_match_next(struct jp_opcode *ptr,
              struct json_object *root, struct json_object *cur,
              jp_match_cb_t cb, void *priv)
{
	int idx;
	struct json_object *next = NULL;

	if (!ptr)
	{
		if (cb)
			cb(cur, priv);

		return cur;
	}

	switch (ptr->type)
	{
	case T_STRING:
	case T_LABEL:
		if (json_object_object_get_ex(cur, ptr->str, &next))
			return jp_match_next(ptr->sibling, root, next, cb, priv);

		break;

	case T_NUMBER:
		if (json_object_get_type(cur) == json_type_array)
		{
			idx = ptr->num;

			if (idx < 0)
				idx += json_object_array_length(cur);

			if (idx >= 0)
				next = json_object_array_get_idx(cur, idx);

			if (next)
				return jp_match_next(ptr->sibling, root, next, cb, priv);
		}

		break;

	default:
		return jp_match_expr(ptr, root, cur, cb, priv);
	}

	return NULL;
}

struct json_object *
jp_match(struct jp_opcode *path, json_object *jsobj,
         jp_match_cb_t cb, void *priv)
{
	if (path->type == T_LABEL)
		path = path->down;

	return jp_match_next(path->down, jsobj, jsobj, cb, priv);
}