summaryrefslogtreecommitdiff
path: root/usr.sbin/rpki-client/json.c
blob: 3e6e5314ba520b535afeb66ead08bd486e89c8e6 (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
/*	$OpenBSD: json.c,v 1.4 2023/06/22 09:08:02 claudio Exp $ */

/*
 * Copyright (c) 2020 Claudio Jeker <claudio@openbsd.org>
 *
 * Permission to use, copy, modify, and 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 <ctype.h>
#include <err.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "json.h"

#define JSON_MAX_STACK	16

enum json_type {
	NONE,
	START,
	ARRAY,
	OBJECT
};

static struct json_stack {
	const char	*name;
	unsigned int	count;
	int		compact;
	enum json_type	type;
} stack[JSON_MAX_STACK];

static char indent[JSON_MAX_STACK + 1];
static int level;
static int eb;
static FILE *jsonfh;

static void
do_comma_indent(void)
{
	char sp = '\n';

	if (stack[level].compact)
		sp = ' ';

	if (stack[level].count++ > 0) {
		if (!eb)
			eb = fprintf(jsonfh, ",%c", sp) < 0;
	}

	if (stack[level].compact)
		return;
	if (!eb)
		eb = fprintf(jsonfh, "\t%.*s", level, indent) < 0;
}

static void
do_name(const char *name)
{
	if (stack[level].type == ARRAY)
		return;
	if (!eb)
		eb = fprintf(jsonfh, "\"%s\": ", name) < 0;
}

static int
do_find(enum json_type type, const char *name)
{
	int i;

	for (i = level; i > 0; i--)
		if (type == stack[i].type &&
		    strcmp(name, stack[i].name) == 0)
			return i;

	/* not found */
	return -1;
}

void
json_do_start(FILE *fh)
{
	memset(indent, '\t', JSON_MAX_STACK);
	memset(stack, 0, sizeof(stack));
	level = 0;
	stack[level].type = START;
	jsonfh = fh;
	eb = 0;

	eb = fprintf(jsonfh, "{\n") < 0;
}

int
json_do_finish(void)
{
	while (level > 0)
		json_do_end();
	if (!eb)
		eb = fprintf(jsonfh, "\n}\n") < 0;

	return -eb;
}

void
json_do_array(const char *name)
{
	int i, l;
	char sp = '\n';

	if ((l = do_find(ARRAY, name)) > 0) {
		/* array already in use, close element and move on */
		for (i = level - l; i > 0; i--)
			json_do_end();
		return;
	}
	/* Do not stack arrays, while allowed this is not needed */
	if (stack[level].type == ARRAY)
		json_do_end();

	if (stack[level].compact)
		sp = ' ';
	do_comma_indent();
	do_name(name);
	if (!eb)
		eb = fprintf(jsonfh, "[%c", sp) < 0;

	if (++level >= JSON_MAX_STACK)
		errx(1, "json stack too deep");

	stack[level].name = name;
	stack[level].type = ARRAY;
	stack[level].count = 0;
	/* inherit compact setting from above level */
	stack[level].compact = stack[level - 1].compact;
}

void
json_do_object(const char *name, int compact)
{
	int i, l;
	char sp = '\n';

	if ((l = do_find(OBJECT, name)) > 0) {
		/* roll back to that object and close it */
		for (i = level - l; i >= 0; i--)
			json_do_end();
	}

	if (compact)
		sp = ' ';
	do_comma_indent();
	do_name(name);
	if (!eb)
		eb = fprintf(jsonfh, "{%c", sp) < 0;

	if (++level >= JSON_MAX_STACK)
		errx(1, "json stack too deep");

	stack[level].name = name;
	stack[level].type = OBJECT;
	stack[level].count = 0;
	stack[level].compact = compact;
}

void
json_do_end(void)
{
	char c;

	if (stack[level].type == ARRAY)
		c = ']';
	else if (stack[level].type == OBJECT)
		c = '}';
	else
		errx(1, "json bad stack state");

	if (!stack[level].compact) {
		if (!eb)
			eb = fprintf(jsonfh, "\n%.*s%c", level, indent, c) < 0;
	} else {
		if (!eb)
			eb = fprintf(jsonfh, " %c", c) < 0;
	}

	stack[level].name = NULL;
	stack[level].type = NONE;
	stack[level].count = 0;
	stack[level].compact = 0;

	if (level-- <= 0)
		errx(1, "json stack underflow");

	stack[level].count++;
}

void
json_do_printf(const char *name, const char *fmt, ...)
{
	va_list ap;
	char *str;

	va_start(ap, fmt);
	if (!eb) {
		if (vasprintf(&str, fmt, ap) == -1)
			errx(1, "json printf failed");
		json_do_string(name, str);
		free(str);
	}
	va_end(ap);
}

void
json_do_string(const char *name, const char *v)
{
	unsigned char c;

	do_comma_indent();
	do_name(name);
	if (!eb)
		eb = fprintf(jsonfh, "\"") < 0;
	while ((c = *v++) != '\0' && !eb) {
		/* skip escaping '/' since our use case does not require it */
		switch (c) {
		case '"':
			eb = fprintf(jsonfh, "\\\"") < 0;
			break;
		case '\\':
			eb = fprintf(jsonfh, "\\\\") < 0;
			break;
		case '\b':
			eb = fprintf(jsonfh, "\\b") < 0;
			break;
		case '\f':
			eb = fprintf(jsonfh, "\\f") < 0;
			break;
		case '\n':
			eb = fprintf(jsonfh, "\\n") < 0;
			break;
		case '\r':
			eb = fprintf(jsonfh, "\\r") < 0;
			break;
		case '\t':
			eb = fprintf(jsonfh, "\\t") < 0;
			break;
		default:
			if (iscntrl(c))
				errx(1, "bad control character in string");
			eb = putc(c, jsonfh) == EOF;
			break;
		}
	}
	if (!eb)
		eb = fprintf(jsonfh, "\"") < 0;
}

void
json_do_hexdump(const char *name, void *buf, size_t len)
{
	uint8_t *data = buf;
	size_t i;

	do_comma_indent();
	do_name(name);
	if (!eb)
		eb = fprintf(jsonfh, "\"") < 0;
	for (i = 0; i < len; i++)
		if (!eb)
			eb = fprintf(jsonfh, "%02x", *(data + i)) < 0;
	if (!eb)
		eb = fprintf(jsonfh, "\"") < 0;
}

void
json_do_bool(const char *name, int v)
{
	do_comma_indent();
	do_name(name);
	if (v) {
		if (!eb)
			eb = fprintf(jsonfh, "true") < 0;
	} else {
		if (!eb)
			eb = fprintf(jsonfh, "false") < 0;
	}
}

void
json_do_uint(const char *name, unsigned long long v)
{
	do_comma_indent();
	do_name(name);
	if (!eb)
		eb = fprintf(jsonfh, "%llu", v) < 0;
}

void
json_do_int(const char *name, long long v)
{
	do_comma_indent();
	do_name(name);
	if (!eb)
		eb = fprintf(jsonfh, "%lld", v) < 0;
}

void
json_do_double(const char *name, double v)
{
	do_comma_indent();
	do_name(name);
	if (!eb)
		eb = fprintf(jsonfh, "%f", v) < 0;
}