6#include "sw/device/lib/ujson/ujson.h"
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"
18static bool is_space(
int c) {
return c ==
' ' || (c >=
'\t' && c <
'\t' + 5); }
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);
27void ujson_crc32_reset(ujson_t *uj) { crc32_init(&uj->
crc32); }
29uint32_t ujson_crc32_finish(ujson_t *uj) {
return crc32_finish(&uj->
crc32); }
31status_t ujson_putbuf(ujson_t *uj,
const char *buf,
size_t len) {
33 crc32_add(&uj->
crc32, buf, len);
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)) {
42 return (
size_t)result.value;
47status_t ujson_getc(ujson_t *uj) {
48 int16_t buffer = uj->
buffer;
51 return OK_STATUS(buffer);
56 crc32_add8(&uj->
crc32, (uint8_t)s.value);
62status_t ujson_ungetc(ujson_t *uj,
char ch) {
64 return FAILED_PRECONDITION();
70bool ujson_streq(
const char *a,
const char *b) {
71 while (*a && *b && *a == *b) {
79static status_t consume_whitespace(ujson_t *uj) {
82 ch = TRY(ujson_getc(uj));
83 }
while (is_space(ch));
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);
96 return OUT_OF_RANGE();
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);
108status_t ujson_consume(ujson_t *uj,
char ch) {
109 if (ch != TRY(consume_whitespace(uj))) {
115status_t ujson_consume_maybe(ujson_t *uj,
char ch) {
116 char got = (char)TRY(consume_whitespace(uj));
118 ujson_ungetc(uj, got);
124status_t ujson_parse_qs(ujson_t *uj,
char *str,
size_t len) {
128 TRY(ujson_consume(uj,
'"'));
130 ch = (char)TRY(ujson_getc(uj));
134 ch = (char)TRY(ujson_getc(uj));
156 ch = (char)TRY(consume_hex(uj));
159 return OUT_OF_RANGE();
172status_t ujson_deserialize_hex(ujson_t *uj, uint8_t *buf,
size_t max_len,
175 TRY(ujson_consume(uj,
'"'));
177 char ch = (char)TRY(ujson_getc(uj));
180 TRY(ujson_ungetc(uj, ch));
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);
191status_t ujson_parse_integer(ujson_t *uj,
void *result,
size_t rsz) {
192 char ch = (char)TRY(consume_whitespace(uj));
201 ch = (char)TRY(ujson_getc(uj));
206 ch = (char)TRY(ujson_getc(uj));
210 if (!(ch >=
'0' && ch <=
'9')) {
214 while (ch >=
'0' && ch <=
'9') {
215 if (value > UINT64_MAX / 10) {
216 return OUT_OF_RANGE();
219 if (value > UINT64_MAX - (ch -
'0')) {
220 return OUT_OF_RANGE();
232 return INVALID_ARGUMENT();
238 TRY(ujson_ungetc(uj, ch));
243 if (value > (uint64_t)INT64_MAX + 1) {
244 return OUT_OF_RANGE();
247 int64_t neg_value = -1 * (int64_t)value;
248 memcpy(result, &neg_value, rsz);
252 memcpy(result, &value, rsz);
256status_t ujson_deserialize_bool(ujson_t *uj,
bool *value) {
257 char got = (char)TRY(consume_whitespace(uj));
259 TRY(ujson_consume(uj,
'r'));
260 TRY(ujson_consume(uj,
'u'));
261 TRY(ujson_consume(uj,
'e'));
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'));
270 ujson_ungetc(uj, got);
276status_t ujson_deserialize_uint64_t(ujson_t *uj, uint64_t *value) {
277 return ujson_parse_integer(uj, (
void *)value,
sizeof(*value));
279status_t ujson_deserialize_uint32_t(ujson_t *uj, uint32_t *value) {
280 return ujson_parse_integer(uj, (
void *)value,
sizeof(*value));
282status_t ujson_deserialize_uint16_t(ujson_t *uj, uint16_t *value) {
283 return ujson_parse_integer(uj, (
void *)value,
sizeof(*value));
285status_t ujson_deserialize_uint8_t(ujson_t *uj, uint8_t *value) {
286 return ujson_parse_integer(uj, (
void *)value,
sizeof(*value));
288status_t ujson_deserialize_size_t(ujson_t *uj,
size_t *value) {
289 return ujson_parse_integer(uj, (
void *)value,
sizeof(*value));
291status_t ujson_deserialize_int64_t(ujson_t *uj, int64_t *value) {
292 return ujson_parse_integer(uj, (
void *)value,
sizeof(*value));
294status_t ujson_deserialize_int32_t(ujson_t *uj, int32_t *value) {
295 return ujson_parse_integer(uj, (
void *)value,
sizeof(*value));
297status_t ujson_deserialize_int16_t(ujson_t *uj, int16_t *value) {
298 return ujson_parse_integer(uj, (
void *)value,
sizeof(*value));
300status_t ujson_deserialize_int8_t(ujson_t *uj, int8_t *value) {
301 return ujson_parse_integer(uj, (
void *)value,
sizeof(*value));
304static const char hex[] =
"0123456789abcdef";
306status_t ujson_serialize_string(ujson_t *uj,
const char *buf) {
308 TRY(ujson_putbuf(uj,
"\"", 1));
309 while ((ch = (uint8_t)*buf) !=
'\0') {
310 if (ch < 0x20 || ch ==
'"' || ch ==
'\\' || ch >= 0x7f) {
313 TRY(ujson_putbuf(uj,
"\\\"", 2));
316 TRY(ujson_putbuf(uj,
"\\\\", 2));
319 TRY(ujson_putbuf(uj,
"\\b", 2));
322 TRY(ujson_putbuf(uj,
"\\f", 2));
325 TRY(ujson_putbuf(uj,
"\\n", 2));
328 TRY(ujson_putbuf(uj,
"\\r", 2));
331 TRY(ujson_putbuf(uj,
"\\t", 2));
334 char esc[] = {
'\\',
'u',
'0',
'0', hex[ch >> 4], hex[ch & 0xF]};
335 TRY(ujson_putbuf(uj, esc,
sizeof(esc)));
339 TRY(ujson_putbuf(uj, buf, 1));
343 TRY(ujson_putbuf(uj,
"\"", 1));
347status_t ujson_serialize_hex(ujson_t *uj,
const uint8_t *buf,
size_t len) {
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));
360 TRY(ujson_putbuf(uj, chunk, n));
362 TRY(ujson_putbuf(uj,
"\"", 1));
366static status_t ujson_serialize_integer64(ujson_t *uj, uint64_t value,
369 char *end = buf +
sizeof(buf);
379 value = udiv64_slow(value, 10, &remainder);
380 *--end =
'0' + (char)remainder;
387 TRY(ujson_putbuf(uj, end, len));
391static status_t ujson_serialize_integer32(ujson_t *uj, uint32_t value,
394 char *end = buf +
sizeof(buf);
402 *--end =
'0' + value % 10;
410 TRY(ujson_putbuf(uj, end, len));
414status_t ujson_serialize_bool(ujson_t *uj,
const bool *value) {
416 TRY(ujson_putbuf(uj,
"true", 4));
418 TRY(ujson_putbuf(uj,
"false", 5));
423status_t ujson_serialize_uint64_t(ujson_t *uj,
const uint64_t *value) {
424 return ujson_serialize_integer64(uj, *value,
false);
426status_t ujson_serialize_uint32_t(ujson_t *uj,
const uint32_t *value) {
427 return ujson_serialize_integer32(uj, *value,
false);
430status_t ujson_serialize_uint16_t(ujson_t *uj,
const uint16_t *value) {
431 return ujson_serialize_integer32(uj, *value,
false);
434status_t ujson_serialize_uint8_t(ujson_t *uj,
const uint8_t *value) {
435 return ujson_serialize_integer32(uj, *value,
false);
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);
442 return ujson_serialize_integer32(uj, *value,
false);
446status_t ujson_serialize_int64_t(ujson_t *uj,
const int64_t *value) {
447 return ujson_serialize_integer64(uj, (uint64_t)*value, *value < 0);
450status_t ujson_serialize_int32_t(ujson_t *uj,
const int32_t *value) {
451 return ujson_serialize_integer32(uj, (uint32_t)*value, *value < 0);
454status_t ujson_serialize_int16_t(ujson_t *uj,
const int16_t *value) {
455 return ujson_serialize_integer32(uj, (uint32_t)*value, *value < 0);
458status_t ujson_serialize_int8_t(ujson_t *uj,
const int8_t *value) {
459 return ujson_serialize_integer32(uj, (uint32_t)*value, *value < 0);
462status_t ujson_deserialize_status_t(ujson_t *uj, status_t *value) {
463 private_status_t code;
464 uint32_t module_id = 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,
'['))) {
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,
']'));
477 TRY(ujson_deserialize_uint32_t(uj, &arg));
479 TRY(ujson_consume(uj,
'}'));
481 status_create((absl_status_t)code, module_id, __FILE__, (int32_t)arg);
485status_t ujson_serialize_status_t(ujson_t *uj,
const status_t *value) {
490 base_fprintf(out,
"%!r", *value);