Pavona Software APIs
ujson.c
1// Copyright lowRISC contributors (OpenTitan project).
2// Copyright zeroRISC Inc.
3// Licensed under the Apache License, Version 2.0, see LICENSE for details.
4// SPDX-License-Identifier: Apache-2.0
5
6#include "sw/device/lib/ujson/ujson.h"
7
8#include <stdbool.h>
9#include <stdint.h>
10#include <string.h>
11
12#include "sw/device/lib/base/crc32.h"
14#include "sw/device/lib/base/status.h"
16#include "sw/device/lib/ujson/private_status.h"
17
18static bool is_space(int c) { return c == ' ' || (c >= '\t' && c < '\t' + 5); }
19
20ujson_t ujson_init(void *context, status_t (*getc)(void *),
21 status_t (*putbuf)(void *, const char *, size_t),
22 status_t (*flushbuf)(void *)) {
23 ujson_t u = UJSON_INIT(context, getc, putbuf, flushbuf);
24 return u;
25}
26
27void ujson_crc32_reset(ujson_t *uj) { crc32_init(&uj->crc32); }
28
29uint32_t ujson_crc32_finish(ujson_t *uj) { return crc32_finish(&uj->crc32); }
30
31status_t ujson_putbuf(ujson_t *uj, const char *buf, size_t len) {
32 uj->str_size += len;
33 crc32_add(&uj->crc32, buf, len);
34 return uj->putbuf(uj->io_context, buf, len);
35}
36
37static size_t ujson_putbuf_sink(ujson_t *uj, const char *buf, size_t len) {
38 status_t result = ujson_putbuf(uj, buf, len);
39 if (!status_ok(result)) {
40 return 0;
41 }
42 return (size_t)result.value;
43}
44
45status_t ujson_flushbuf(ujson_t *uj) { return uj->flushbuf(uj->io_context); }
46
47status_t ujson_getc(ujson_t *uj) {
48 int16_t buffer = uj->buffer;
49 if (buffer >= 0) {
50 uj->buffer = -1;
51 return OK_STATUS(buffer);
52 } else {
53 uj->str_size++;
54 status_t s = uj->getc(uj->io_context);
55 if (!status_err(s)) {
56 crc32_add8(&uj->crc32, (uint8_t)s.value);
57 }
58 return s;
59 }
60}
61
62status_t ujson_ungetc(ujson_t *uj, char ch) {
63 if (uj->buffer >= 0) {
64 return FAILED_PRECONDITION();
65 }
66 uj->buffer = ch;
67 return OK_STATUS();
68}
69
70bool ujson_streq(const char *a, const char *b) {
71 while (*a && *b && *a == *b) {
72 ++a;
73 ++b;
74 }
75 return *a == *b;
76}
77
78// Consumes whitespace returning first non-whitepsace character found.
79static status_t consume_whitespace(ujson_t *uj) {
80 int ch;
81 do {
82 ch = TRY(ujson_getc(uj));
83 } while (is_space(ch));
84 return OK_STATUS(ch);
85}
86
87static status_t consume_hexdigit(ujson_t *uj) {
88 int ch = TRY(ujson_getc(uj));
89 if (ch >= '0' && ch <= '9') {
90 return OK_STATUS(ch - '0');
91 } else if (ch >= 'A' && ch <= 'F') {
92 return OK_STATUS(ch - 'A' + 10);
93 } else if (ch >= 'a' && ch <= 'f') {
94 return OK_STATUS(ch - 'a' + 10);
95 } else {
96 return OUT_OF_RANGE();
97 }
98}
99
100static status_t consume_hex(ujson_t *uj) {
101 int a = TRY(consume_hexdigit(uj));
102 int b = TRY(consume_hexdigit(uj));
103 int c = TRY(consume_hexdigit(uj));
104 int d = TRY(consume_hexdigit(uj));
105 return OK_STATUS((a << 12) | (b << 8) | (c << 4) | d);
106}
107
108status_t ujson_consume(ujson_t *uj, char ch) {
109 if (ch != TRY(consume_whitespace(uj))) {
110 return NOT_FOUND();
111 }
112 return OK_STATUS();
113}
114
115status_t ujson_consume_maybe(ujson_t *uj, char ch) {
116 char got = (char)TRY(consume_whitespace(uj));
117 if (ch != got) {
118 ujson_ungetc(uj, got);
119 return OK_STATUS(0);
120 }
121 return OK_STATUS(1);
122}
123
124status_t ujson_parse_qs(ujson_t *uj, char *str, size_t len) {
125 char ch;
126 int n = 0;
127 len--; // One char for the nul terminator.
128 TRY(ujson_consume(uj, '"'));
129 while (true) {
130 ch = (char)TRY(ujson_getc(uj));
131 if (ch == '\"')
132 break;
133 if (ch == '\\') {
134 ch = (char)TRY(ujson_getc(uj));
135 switch (ch) {
136 case '"':
137 case '\\':
138 case '/':
139 break;
140 case 'b':
141 ch = '\b';
142 break;
143 case 'f':
144 ch = '\f';
145 break;
146 case 'n':
147 ch = '\n';
148 break;
149 case 'r':
150 ch = '\r';
151 break;
152 case 't':
153 ch = '\t';
154 break;
155 case 'u':
156 ch = (char)TRY(consume_hex(uj));
157 break;
158 default:
159 return OUT_OF_RANGE();
160 }
161 }
162 if (len > 0) {
163 *str++ = ch;
164 --len;
165 n++;
166 }
167 }
168 *str = '\0';
169 return OK_STATUS(n);
170}
171
172status_t ujson_deserialize_hex(ujson_t *uj, uint8_t *buf, size_t max_len,
173 size_t *len) {
174 size_t n = 0;
175 TRY(ujson_consume(uj, '"'));
176 while (true) {
177 char ch = (char)TRY(ujson_getc(uj));
178 if (ch == '"')
179 break;
180 TRY(ujson_ungetc(uj, ch));
181 if (n >= max_len)
182 return OUT_OF_RANGE();
183 int hi = TRY(consume_hexdigit(uj));
184 int lo = TRY(consume_hexdigit(uj));
185 buf[n++] = (uint8_t)((hi << 4) | lo);
186 }
187 *len = n;
188 return OK_STATUS();
189}
190
191status_t ujson_parse_integer(ujson_t *uj, void *result, size_t rsz) {
192 char ch = (char)TRY(consume_whitespace(uj));
193 bool neg = false;
194 bool quoted = false;
195
196 if (ch == '"') {
197 // If we encounter a quote while parsing an integer, assume that
198 // the quoted string will contain an integer. Get the next
199 // character and continue parsing as if we expect an integer.
200 quoted = true;
201 ch = (char)TRY(ujson_getc(uj));
202 }
203
204 if (ch == '-') {
205 neg = true;
206 ch = (char)TRY(ujson_getc(uj));
207 }
208 uint64_t value = 0;
209
210 if (!(ch >= '0' && ch <= '9')) {
211 return NOT_FOUND();
212 }
213 status_t s;
214 while (ch >= '0' && ch <= '9') {
215 if (value > UINT64_MAX / 10) {
216 return OUT_OF_RANGE();
217 }
218 value *= 10;
219 if (value > UINT64_MAX - (ch - '0')) {
220 return OUT_OF_RANGE();
221 }
222 value += ch - '0';
223 s = ujson_getc(uj);
224 if (status_err(s))
225 break;
226 ch = (char)s.value;
227 }
228
229 if (status_ok(s)) {
230 if (quoted) {
231 if (ch != '"') {
232 return INVALID_ARGUMENT();
233 }
234 // Close quote on an integer in quoted string.
235 // Don't have to unget the quote because we
236 // want to consume it.
237 } else {
238 TRY(ujson_ungetc(uj, ch));
239 }
240 }
241
242 if (neg) {
243 if (value > (uint64_t)INT64_MAX + 1) {
244 return OUT_OF_RANGE();
245 }
246
247 int64_t neg_value = -1 * (int64_t)value;
248 memcpy(result, &neg_value, rsz);
249 return OK_STATUS();
250 }
251
252 memcpy(result, &value, rsz);
253 return OK_STATUS();
254}
255
256status_t ujson_deserialize_bool(ujson_t *uj, bool *value) {
257 char got = (char)TRY(consume_whitespace(uj));
258 if (got == 't') {
259 TRY(ujson_consume(uj, 'r'));
260 TRY(ujson_consume(uj, 'u'));
261 TRY(ujson_consume(uj, 'e'));
262 *value = true;
263 } else if (got == 'f') {
264 TRY(ujson_consume(uj, 'a'));
265 TRY(ujson_consume(uj, 'l'));
266 TRY(ujson_consume(uj, 's'));
267 TRY(ujson_consume(uj, 'e'));
268 *value = false;
269 } else {
270 ujson_ungetc(uj, got);
271 return NOT_FOUND();
272 }
273 return OK_STATUS();
274}
275
276status_t ujson_deserialize_uint64_t(ujson_t *uj, uint64_t *value) {
277 return ujson_parse_integer(uj, (void *)value, sizeof(*value));
278}
279status_t ujson_deserialize_uint32_t(ujson_t *uj, uint32_t *value) {
280 return ujson_parse_integer(uj, (void *)value, sizeof(*value));
281}
282status_t ujson_deserialize_uint16_t(ujson_t *uj, uint16_t *value) {
283 return ujson_parse_integer(uj, (void *)value, sizeof(*value));
284}
285status_t ujson_deserialize_uint8_t(ujson_t *uj, uint8_t *value) {
286 return ujson_parse_integer(uj, (void *)value, sizeof(*value));
287}
288status_t ujson_deserialize_size_t(ujson_t *uj, size_t *value) {
289 return ujson_parse_integer(uj, (void *)value, sizeof(*value));
290}
291status_t ujson_deserialize_int64_t(ujson_t *uj, int64_t *value) {
292 return ujson_parse_integer(uj, (void *)value, sizeof(*value));
293}
294status_t ujson_deserialize_int32_t(ujson_t *uj, int32_t *value) {
295 return ujson_parse_integer(uj, (void *)value, sizeof(*value));
296}
297status_t ujson_deserialize_int16_t(ujson_t *uj, int16_t *value) {
298 return ujson_parse_integer(uj, (void *)value, sizeof(*value));
299}
300status_t ujson_deserialize_int8_t(ujson_t *uj, int8_t *value) {
301 return ujson_parse_integer(uj, (void *)value, sizeof(*value));
302}
303
304static const char hex[] = "0123456789abcdef";
305
306status_t ujson_serialize_string(ujson_t *uj, const char *buf) {
307 uint8_t ch;
308 TRY(ujson_putbuf(uj, "\"", 1));
309 while ((ch = (uint8_t)*buf) != '\0') {
310 if (ch < 0x20 || ch == '"' || ch == '\\' || ch >= 0x7f) {
311 switch (ch) {
312 case '"':
313 TRY(ujson_putbuf(uj, "\\\"", 2));
314 break;
315 case '\\':
316 TRY(ujson_putbuf(uj, "\\\\", 2));
317 break;
318 case '\b':
319 TRY(ujson_putbuf(uj, "\\b", 2));
320 break;
321 case '\f':
322 TRY(ujson_putbuf(uj, "\\f", 2));
323 break;
324 case '\n':
325 TRY(ujson_putbuf(uj, "\\n", 2));
326 break;
327 case '\r':
328 TRY(ujson_putbuf(uj, "\\r", 2));
329 break;
330 case '\t':
331 TRY(ujson_putbuf(uj, "\\t", 2));
332 break;
333 default: {
334 char esc[] = {'\\', 'u', '0', '0', hex[ch >> 4], hex[ch & 0xF]};
335 TRY(ujson_putbuf(uj, esc, sizeof(esc)));
336 }
337 }
338 } else {
339 TRY(ujson_putbuf(uj, buf, 1));
340 }
341 ++buf;
342 }
343 TRY(ujson_putbuf(uj, "\"", 1));
344 return OK_STATUS();
345}
346
347status_t ujson_serialize_hex(ujson_t *uj, const uint8_t *buf, size_t len) {
348 char chunk[32];
349 size_t n = 0;
350 TRY(ujson_putbuf(uj, "\"", 1));
351 for (size_t i = 0; i < len; ++i) {
352 chunk[n++] = hex[buf[i] >> 4];
353 chunk[n++] = hex[buf[i] & 0xF];
354 if (n == sizeof(chunk)) {
355 TRY(ujson_putbuf(uj, chunk, n));
356 n = 0;
357 }
358 }
359 if (n > 0) {
360 TRY(ujson_putbuf(uj, chunk, n));
361 }
362 TRY(ujson_putbuf(uj, "\"", 1));
363 return OK_STATUS();
364}
365
366static status_t ujson_serialize_integer64(ujson_t *uj, uint64_t value,
367 bool neg) {
368 char buf[24];
369 char *end = buf + sizeof(buf);
370 size_t len = 0;
371 *--end = '\0';
372
373 // If negative, two's complement for the absolute value.
374 if (neg)
375 value = ~value + 1;
376 do {
377 // We've banned __udivdi3; do division with the replacement function.
378 uint64_t remainder;
379 value = udiv64_slow(value, 10, &remainder);
380 *--end = '0' + (char)remainder;
381 ++len;
382 } while (value);
383 if (neg) {
384 *--end = '-';
385 ++len;
386 }
387 TRY(ujson_putbuf(uj, end, len));
388 return OK_STATUS();
389}
390
391static status_t ujson_serialize_integer32(ujson_t *uj, uint32_t value,
392 bool neg) {
393 char buf[24];
394 char *end = buf + sizeof(buf);
395 size_t len = 0;
396 *--end = '\0';
397
398 // If negative, two's complement for the absolute value.
399 if (neg)
400 value = ~value + 1;
401 do {
402 *--end = '0' + value % 10;
403 value /= 10;
404 ++len;
405 } while (value);
406 if (neg) {
407 *--end = '-';
408 ++len;
409 }
410 TRY(ujson_putbuf(uj, end, len));
411 return OK_STATUS();
412}
413
414status_t ujson_serialize_bool(ujson_t *uj, const bool *value) {
415 if (*value) {
416 TRY(ujson_putbuf(uj, "true", 4));
417 } else {
418 TRY(ujson_putbuf(uj, "false", 5));
419 }
420 return OK_STATUS();
421}
422
423status_t ujson_serialize_uint64_t(ujson_t *uj, const uint64_t *value) {
424 return ujson_serialize_integer64(uj, *value, false);
425}
426status_t ujson_serialize_uint32_t(ujson_t *uj, const uint32_t *value) {
427 return ujson_serialize_integer32(uj, *value, false);
428}
429
430status_t ujson_serialize_uint16_t(ujson_t *uj, const uint16_t *value) {
431 return ujson_serialize_integer32(uj, *value, false);
432}
433
434status_t ujson_serialize_uint8_t(ujson_t *uj, const uint8_t *value) {
435 return ujson_serialize_integer32(uj, *value, false);
436}
437
438status_t ujson_serialize_size_t(ujson_t *uj, const size_t *value) {
439 if (sizeof(size_t) == sizeof(uint64_t)) {
440 return ujson_serialize_integer64(uj, *value, false);
441 } else {
442 return ujson_serialize_integer32(uj, *value, false);
443 }
444}
445
446status_t ujson_serialize_int64_t(ujson_t *uj, const int64_t *value) {
447 return ujson_serialize_integer64(uj, (uint64_t)*value, *value < 0);
448}
449
450status_t ujson_serialize_int32_t(ujson_t *uj, const int32_t *value) {
451 return ujson_serialize_integer32(uj, (uint32_t)*value, *value < 0);
452}
453
454status_t ujson_serialize_int16_t(ujson_t *uj, const int16_t *value) {
455 return ujson_serialize_integer32(uj, (uint32_t)*value, *value < 0);
456}
457
458status_t ujson_serialize_int8_t(ujson_t *uj, const int8_t *value) {
459 return ujson_serialize_integer32(uj, (uint32_t)*value, *value < 0);
460}
461
462status_t ujson_deserialize_status_t(ujson_t *uj, status_t *value) {
463 private_status_t code;
464 uint32_t module_id = 0;
465 uint32_t arg = 0;
466 TRY(ujson_consume(uj, '{'));
467 TRY(ujson_deserialize_private_status_t(uj, &code));
468 TRY(ujson_consume(uj, ':'));
469 if (TRY(ujson_consume_maybe(uj, '['))) {
470 char module[4];
471 TRY(ujson_parse_qs(uj, module, sizeof(module)));
472 module_id = MAKE_MODULE_ID(module[0], module[1], module[2]);
473 TRY(ujson_consume(uj, ','));
474 TRY(ujson_deserialize_uint32_t(uj, &arg));
475 TRY(ujson_consume(uj, ']'));
476 } else {
477 TRY(ujson_deserialize_uint32_t(uj, &arg));
478 }
479 TRY(ujson_consume(uj, '}'));
480 *value =
481 status_create((absl_status_t)code, module_id, __FILE__, (int32_t)arg);
482 return OK_STATUS();
483}
484
485status_t ujson_serialize_status_t(ujson_t *uj, const status_t *value) {
486 buffer_sink_t out = {
487 .data = uj,
488 .sink = (sink_func_ptr)ujson_putbuf_sink,
489 };
490 base_fprintf(out, "%!r", *value);
491 return OK_STATUS();
492}