Lines Matching refs:json

117 static int json_ensure_stack_size(OSSL_JSON_ENC *json, size_t num_bytes)  in json_ensure_stack_size()  argument
121 if (json->stack_bytes >= num_bytes) in json_ensure_stack_size()
124 if (num_bytes <= OSSL_NELEM(json->stack_small)) { in json_ensure_stack_size()
125 stack = json->stack_small; in json_ensure_stack_size()
127 if (json->stack == json->stack_small) in json_ensure_stack_size()
128 json->stack = NULL; in json_ensure_stack_size()
130 stack = OPENSSL_realloc(json->stack, num_bytes); in json_ensure_stack_size()
135 json->stack = stack; in json_ensure_stack_size()
136 json->stack_bytes = num_bytes; in json_ensure_stack_size()
141 static int json_push(OSSL_JSON_ENC *json, unsigned int v) in json_push() argument
146 if (json->stack_end_byte >= json->stack_bytes) { in json_push()
148 = (json->stack_bytes == 0) in json_push()
149 ? OSSL_NELEM(json->stack_small) in json_push()
150 : (json->stack_bytes * 2); in json_push()
152 if (!json_ensure_stack_size(json, new_size)) in json_push()
155 json->stack_bytes = new_size; in json_push()
159 json->stack[json->stack_end_byte] |= (v << json->stack_end_bit); in json_push()
161 json->stack[json->stack_end_byte] &= ~(1U << json->stack_end_bit); in json_push()
163 json->stack_end_bit = (json->stack_end_bit + 1) % CHAR_BIT; in json_push()
164 if (json->stack_end_bit == 0) in json_push()
165 ++json->stack_end_byte; in json_push()
174 static int json_pop(OSSL_JSON_ENC *json) in json_pop() argument
176 if (json->stack_end_byte == 0 && json->stack_end_bit == 0) in json_pop()
179 if (json->stack_end_bit == 0) { in json_pop()
180 --json->stack_end_byte; in json_pop()
181 json->stack_end_bit = CHAR_BIT - 1; in json_pop()
183 --json->stack_end_bit; in json_pop()
192 static int json_peek(OSSL_JSON_ENC *json) in json_peek() argument
196 obyte = json->stack_end_byte; in json_peek()
197 obit = json->stack_end_bit; in json_peek()
208 return (json->stack[obyte] & (1U << obit)) != 0; in json_peek()
222 static ossl_inline int in_ijson(const OSSL_JSON_ENC *json) in in_ijson() argument
224 return (json->flags & OSSL_JSON_FLAG_IJSON) != 0; in in_ijson()
227 static ossl_inline int in_seq(const OSSL_JSON_ENC *json) in in_seq() argument
229 return (json->flags & OSSL_JSON_FLAG_SEQ) != 0; in in_seq()
232 static ossl_inline int in_pretty(const OSSL_JSON_ENC *json) in in_pretty() argument
234 return (json->flags & OSSL_JSON_FLAG_PRETTY) != 0; in in_pretty()
237 int ossl_json_init(OSSL_JSON_ENC *json, BIO *bio, uint32_t flags) in ossl_json_init() argument
239 memset(json, 0, sizeof(*json)); in ossl_json_init()
240 json->flags = flags; in ossl_json_init()
241 json->error = 0; in ossl_json_init()
242 if (!wbuf_init(&json->wbuf, bio, 4096)) in ossl_json_init()
245 json->state = STATE_PRE_COMMA; in ossl_json_init()
249 void ossl_json_cleanup(OSSL_JSON_ENC *json) in ossl_json_cleanup() argument
251 wbuf_cleanup(&json->wbuf); in ossl_json_cleanup()
253 if (json->stack != json->stack_small) in ossl_json_cleanup()
254 OPENSSL_free(json->stack); in ossl_json_cleanup()
256 json->stack = NULL; in ossl_json_cleanup()
259 int ossl_json_flush_cleanup(OSSL_JSON_ENC *json) in ossl_json_flush_cleanup() argument
261 int ok = ossl_json_flush(json); in ossl_json_flush_cleanup()
263 ossl_json_cleanup(json); in ossl_json_flush_cleanup()
267 int ossl_json_reset(OSSL_JSON_ENC *json) in ossl_json_reset() argument
269 wbuf_clean(&json->wbuf); in ossl_json_reset()
270 json->stack_end_byte = 0; in ossl_json_reset()
271 json->stack_end_bit = 0; in ossl_json_reset()
272 json->error = 0; in ossl_json_reset()
276 int ossl_json_flush(OSSL_JSON_ENC *json) in ossl_json_flush() argument
278 return wbuf_flush(&json->wbuf, /*full=*/1); in ossl_json_flush()
281 int ossl_json_set0_sink(OSSL_JSON_ENC *json, BIO *bio) in ossl_json_set0_sink() argument
283 wbuf_set0_bio(&json->wbuf, bio); in ossl_json_set0_sink()
287 int ossl_json_in_error(OSSL_JSON_ENC *json) in ossl_json_in_error() argument
289 return json->error; in ossl_json_in_error()
297 static void json_write_qstring(OSSL_JSON_ENC *json, const char *str);
298 static void json_indent(OSSL_JSON_ENC *json);
300 static void json_raise_error(OSSL_JSON_ENC *json) in json_raise_error() argument
302 json->error = 1; in json_raise_error()
305 static void json_undefer(OSSL_JSON_ENC *json) in json_undefer() argument
307 if (!json->defer_indent) in json_undefer()
310 json_indent(json); in json_undefer()
313 static void json_write_char(OSSL_JSON_ENC *json, char ch) in json_write_char() argument
315 if (ossl_json_in_error(json)) in json_write_char()
318 json_undefer(json); in json_write_char()
319 if (!wbuf_write_char(&json->wbuf, ch)) in json_write_char()
320 json_raise_error(json); in json_write_char()
323 static void json_write_str(OSSL_JSON_ENC *json, const char *s) in json_write_str() argument
325 if (ossl_json_in_error(json)) in json_write_str()
328 json_undefer(json); in json_write_str()
329 if (!wbuf_write_str(&json->wbuf, s)) in json_write_str()
330 json_raise_error(json); in json_write_str()
333 static void json_indent(OSSL_JSON_ENC *json) in json_indent() argument
337 json->defer_indent = 0; in json_indent()
339 if (!in_pretty(json)) in json_indent()
342 json_write_char(json, '\n'); in json_indent()
344 depth = json->stack_end_byte * 8 + json->stack_end_bit; in json_indent()
346 json_write_str(json, " "); in json_indent()
349 static int json_pre_item(OSSL_JSON_ENC *json) in json_pre_item() argument
353 if (ossl_json_in_error(json)) in json_pre_item()
356 switch (json->state) { in json_pre_item()
358 s = json_peek(json); in json_pre_item()
361 json_raise_error(json); in json_pre_item()
366 json_write_char(json, ','); in json_pre_item()
367 if (ossl_json_in_error(json)) in json_pre_item()
370 json_indent(json); in json_pre_item()
373 if (s < 0 && in_seq(json)) in json_pre_item()
374 json_write_char(json, '\x1E'); in json_pre_item()
376 json->state = STATE_PRE_ITEM; in json_pre_item()
384 json_raise_error(json); in json_pre_item()
391 static void json_post_item(OSSL_JSON_ENC *json) in json_post_item() argument
393 int s = json_peek(json); in json_post_item()
395 json->state = STATE_PRE_COMMA; in json_post_item()
397 if (s < 0 && in_seq(json)) in json_post_item()
398 json_write_char(json, '\n'); in json_post_item()
406 static void composite_begin(OSSL_JSON_ENC *json, int type, char ch) in composite_begin() argument
408 if (!json_pre_item(json) in composite_begin()
409 || !json_push(json, type)) in composite_begin()
410 json_raise_error(json); in composite_begin()
412 json_write_char(json, ch); in composite_begin()
413 json->defer_indent = 1; in composite_begin()
421 static void composite_end(OSSL_JSON_ENC *json, int type, char ch) in composite_end() argument
423 int was_defer = json->defer_indent; in composite_end()
425 if (ossl_json_in_error(json)) in composite_end()
428 json->defer_indent = 0; in composite_end()
430 if (json_peek(json) != type) { in composite_end()
431 json_raise_error(json); in composite_end()
435 if (type == 0 && json->state == STATE_PRE_ITEM) { in composite_end()
436 json_raise_error(json); in composite_end()
440 if (!json_pop(json)) { in composite_end()
441 json_raise_error(json); in composite_end()
446 json_indent(json); in composite_end()
448 json_write_char(json, ch); in composite_end()
449 json_post_item(json); in composite_end()
453 void ossl_json_object_begin(OSSL_JSON_ENC *json) in ossl_json_object_begin() argument
455 composite_begin(json, 0, '{'); in ossl_json_object_begin()
456 json->state = STATE_PRE_KEY; in ossl_json_object_begin()
460 void ossl_json_object_end(OSSL_JSON_ENC *json) in ossl_json_object_end() argument
462 composite_end(json, 0, '}'); in ossl_json_object_end()
466 void ossl_json_array_begin(OSSL_JSON_ENC *json) in ossl_json_array_begin() argument
468 composite_begin(json, 1, '['); in ossl_json_array_begin()
469 json->state = STATE_PRE_ITEM; in ossl_json_array_begin()
473 void ossl_json_array_end(OSSL_JSON_ENC *json) in ossl_json_array_end() argument
475 composite_end(json, 1, ']'); in ossl_json_array_end()
482 void ossl_json_key(OSSL_JSON_ENC *json, const char *key) in ossl_json_key() argument
484 if (ossl_json_in_error(json)) in ossl_json_key()
487 if (json_peek(json) != 0) { in ossl_json_key()
489 json_raise_error(json); in ossl_json_key()
493 if (json->state == STATE_PRE_COMMA) { in ossl_json_key()
494 json_write_char(json, ','); in ossl_json_key()
495 json->state = STATE_PRE_KEY; in ossl_json_key()
498 json_indent(json); in ossl_json_key()
499 if (json->state != STATE_PRE_KEY) { in ossl_json_key()
500 json_raise_error(json); in ossl_json_key()
504 json_write_qstring(json, key); in ossl_json_key()
505 if (ossl_json_in_error(json)) in ossl_json_key()
508 json_write_char(json, ':'); in ossl_json_key()
509 if (in_pretty(json)) in ossl_json_key()
510 json_write_char(json, ' '); in ossl_json_key()
512 json->state = STATE_PRE_ITEM; in ossl_json_key()
516 void ossl_json_null(OSSL_JSON_ENC *json) in ossl_json_null() argument
518 if (!json_pre_item(json)) in ossl_json_null()
521 json_write_str(json, "null"); in ossl_json_null()
522 json_post_item(json); in ossl_json_null()
525 void ossl_json_bool(OSSL_JSON_ENC *json, int v) in ossl_json_bool() argument
527 if (!json_pre_item(json)) in ossl_json_bool()
530 json_write_str(json, v > 0 ? "true" : "false"); in ossl_json_bool()
531 json_post_item(json); in ossl_json_bool()
537 static void json_u64(OSSL_JSON_ENC *json, uint64_t v, int noquote) in json_u64() argument
540 int quote = !noquote && in_ijson(json) && v > (uint64_t)(POW_53 - 1); in json_u64()
542 if (!json_pre_item(json)) in json_u64()
546 json_write_char(json, '"'); in json_u64()
554 json_write_str(json, p); in json_u64()
557 json_write_char(json, '"'); in json_u64()
559 json_post_item(json); in json_u64()
562 void ossl_json_u64(OSSL_JSON_ENC *json, uint64_t v) in ossl_json_u64() argument
564 json_u64(json, v, 0); in ossl_json_u64()
568 void ossl_json_i64(OSSL_JSON_ENC *json, int64_t value) in ossl_json_i64() argument
574 ossl_json_u64(json, (uint64_t)value); in ossl_json_i64()
578 if (!json_pre_item(json)) in ossl_json_i64()
581 quote = in_ijson(json) in ossl_json_i64()
585 json_write_char(json, '"'); in ossl_json_i64()
587 json_write_char(json, '-'); in ossl_json_i64()
592 json_u64(json, uv, /*noquote=*/1); in ossl_json_i64()
594 if (quote && !ossl_json_in_error(json)) in ossl_json_i64()
595 json_write_char(json, '"'); in ossl_json_i64()
599 void ossl_json_f64(OSSL_JSON_ENC *json, double value) in ossl_json_f64() argument
603 if (!json_pre_item(json)) in ossl_json_f64()
614 json_raise_error(json); in ossl_json_f64()
621 json_write_str(json, buf); in ossl_json_f64()
622 json_post_item(json); in ossl_json_f64()
635 json_write_qstring_inner(OSSL_JSON_ENC *json, const char *str, size_t str_len, in json_write_qstring_inner() argument
643 if (ossl_json_in_error(json)) in json_write_qstring_inner()
646 json_write_char(json, '"'); in json_write_qstring_inner()
702 json_write_char(json, c); in json_write_qstring_inner()
708 json_write_str(json, o); in json_write_qstring_inner()
711 json_write_char(json, '"'); in json_write_qstring_inner()
715 json_write_qstring(OSSL_JSON_ENC *json, const char *str) in json_write_qstring() argument
717 json_write_qstring_inner(json, str, 0, 1); in json_write_qstring()
721 json_write_qstring_len(OSSL_JSON_ENC *json, const char *str, size_t str_len) in json_write_qstring_len() argument
723 json_write_qstring_inner(json, str, str_len, 0); in json_write_qstring_len()
726 void ossl_json_str(OSSL_JSON_ENC *json, const char *str) in ossl_json_str() argument
728 if (!json_pre_item(json)) in ossl_json_str()
731 json_write_qstring(json, str); in ossl_json_str()
732 json_post_item(json); in ossl_json_str()
735 void ossl_json_str_len(OSSL_JSON_ENC *json, const char *str, size_t str_len) in ossl_json_str_len() argument
737 if (!json_pre_item(json)) in ossl_json_str_len()
740 json_write_qstring_len(json, str, str_len); in ossl_json_str_len()
741 json_post_item(json); in ossl_json_str_len()
748 void ossl_json_str_hex(OSSL_JSON_ENC *json, const void *data, size_t data_len) in ossl_json_str_hex() argument
753 if (!json_pre_item(json)) in ossl_json_str_hex()
756 json_write_char(json, '"'); in ossl_json_str_hex()
760 json_write_char(json, hex_digit(c >> 4)); in ossl_json_str_hex()
761 json_write_char(json, hex_digit(c & 0x0F)); in ossl_json_str_hex()
764 json_write_char(json, '"'); in ossl_json_str_hex()
765 json_post_item(json); in ossl_json_str_hex()