aboutsummaryrefslogtreecommitdiffstats
path: root/src/handle_json.h
blob: ddf593470fd2ae1b36c74ec8f33b4bbf46f44e10 (plain) (blame)
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
#include "json.h"

bool get_jsonf_size(json_value * value, int * w, int * h) {
	json_object_entry _size = value->u.object.values[0];
	json_object_entry * _size_objects = _size.value->u.object.values;

	if (strcmp(_size.name, "size"))
		return true;

	// Width.
	if (!strcmp(_size_objects[0].name, "w"))
		*w = _size_objects[0].value->u.integer;
	else if (!strcmp(_size_objects[0].name, "h"))
		*h = _size_objects[0].value->u.integer;
	else
		return true;

	// Height
	if (!strcmp(_size_objects[1].name, "w"))
		*w = _size_objects[1].value->u.integer;
	else if (!strcmp(_size_objects[1].name, "h"))
		*h = _size_objects[1].value->u.integer;
	else
		return true;

	if (*w > 255 || *w < 2 || *h > 255 || *h < 2)
		return true;

	return false;
}

bool read_data_jsonf(json_value * value, int w, int h, unsigned char ** buf) {
	json_object_entry _size = value->u.object.values[0];
	json_object_entry _map = value->u.object.values[1];
	json_value ** astart = _map.value->u.array.values;

	if (strcmp(_size.name, "size") ||
			strcmp(_map.name, "map"))
		return true;

	unsigned char y, x;
	long int int_value;

	for (y = 0; y < h; y++)
		for (x = 0; x < w; x++) {

			if (astart[y]->u.array.values[x]->type != json_integer)
				return true;

			int_value = astart[y]->u.array.values[x]->u.integer;

			if (int_value > 8 || int_value < 0)
				return true;
			buf[y][x] = (unsigned char)int_value;
		}

	return false;
}