Lite³
A JSON-Compatible Zero-Copy Serialization Format
Loading...
Searching...
No Matches
lite3_context_api.h
Go to the documentation of this file.
1/*
2 Lite³: A JSON-Compatible Zero-Copy Serialization Format
3
4 Copyright © 2025 Elias de Jong <elias@fastserial.com>
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23
24 __ __________________ ____
25 _ ___ ___/ /___(_)_/ /_______|_ /
26 _ _____/ / __/ /_ __/ _ \_/_ <
27 ___ __/ /___/ / / /_ / __/____/
28 /_____/_/ \__/ \___/
29*/
30
39#ifndef LITE3_CONTEXT_API_H
40#define LITE3_CONTEXT_API_H
41
42#include <stdint.h>
43#include <stddef.h>
44#include <stdbool.h>
45#include <assert.h>
46#include <string.h>
47#include <errno.h>
48
49#include "lite3.h"
50
51
52
53#ifdef __cplusplus
54extern "C" {
55#endif
56
130#define LITE3_CONTEXT_BUF_SIZE_MIN 1024
131static_assert(LITE3_CONTEXT_BUF_SIZE_MIN > (size_t)LITE3_NODE_ALIGNMENT_MASK, "LITE3_CONTEXT_BUF_SIZE_MIN must be greater than LITE3_NODE_ALIGNMENT_MASK");
132
140typedef struct lite3_ctx {
141 uint8_t __attribute__((aligned(LITE3_NODE_ALIGNMENT))) *buf;
142 size_t buflen;
143 size_t bufsz;
144 void *underlying_buf;
146
147
148
173
183 const unsigned char *buf,
184 size_t buflen
185);
186
200 unsigned char *buf,
201 size_t buflen,
202 size_t bufsz
203);
204
216
229 lite3_ctx *ctx,
230 const unsigned char *buf,
231 size_t buflen
232);
233
243
244#ifndef DOXYGEN_IGNORE
245// Private function
246int lite3_ctx_grow_impl(lite3_ctx *ctx);
247#endif // DOXYGEN_IGNORE
249
250
251
272static inline int lite3_ctx_init_obj(lite3_ctx *ctx)
273{
274 return lite3_init_obj(ctx->buf, &ctx->buflen, ctx->bufsz);
275}
276
286static inline int lite3_ctx_init_arr(lite3_ctx *ctx)
287{
288 return lite3_init_arr(ctx->buf, &ctx->buflen, ctx->bufsz);
289}
291
292
293
329#define lite3_ctx_set_null(ctx, ofs, key) ({ \
330 const char *__lite3_key__ = (key); \
331 lite3_ctx_set_null_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
332})
333#ifndef DOXYGEN_IGNORE
334static inline int lite3_ctx_set_null_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data)
335{
336 int ret;
337 if ((ret = _lite3_verify_obj_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, key)) < 0)
338 return ret;
339 lite3_val *val;
340 errno = 0;
341 while ((ret = lite3_set_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, key, key_data, lite3_type_sizes[LITE3_TYPE_NULL], &val)) < 0) {
342 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
343 continue;
344 } else {
345 return ret;
346 }
347 }
348 val->type = (uint8_t)LITE3_TYPE_NULL;
349 return ret;
350}
351#endif // DOXYGEN_IGNORE
352
364#define lite3_ctx_set_bool(ctx, ofs, key, value) ({ \
365 const char *__lite3_key__ = (key); \
366 _lite3_ctx_set_bool_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), value); \
367})
368#ifndef DOXYGEN_IGNORE
369static inline int _lite3_ctx_set_bool_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data, bool value)
370{
371 int ret;
372 if ((ret = _lite3_verify_obj_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, key)) < 0)
373 return ret;
374 lite3_val *val;
375 errno = 0;
376 while ((ret = lite3_set_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, key, key_data, lite3_type_sizes[LITE3_TYPE_BOOL], &val)) < 0) {
377 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
378 continue;
379 } else {
380 return ret;
381 }
382 }
383 val->type = (uint8_t)LITE3_TYPE_BOOL;
384 memcpy(val->val, &value, lite3_type_sizes[LITE3_TYPE_BOOL]);
385 return ret;
386}
387#endif // DOXYGEN_IGNORE
388
400#define lite3_ctx_set_i64(ctx, ofs, key, value) ({ \
401 const char *__lite3_key__ = (key); \
402 _lite3_ctx_set_i64_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), value); \
403})
404#ifndef DOXYGEN_IGNORE
405static inline int _lite3_ctx_set_i64_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data, int64_t value)
406{
407 int ret;
408 if ((ret = _lite3_verify_obj_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, key)) < 0)
409 return ret;
410 lite3_val *val;
411 errno = 0;
412 while ((ret = lite3_set_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, key, key_data, lite3_type_sizes[LITE3_TYPE_I64], &val)) < 0) {
413 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
414 continue;
415 } else {
416 return ret;
417 }
418 }
419 val->type = (uint8_t)LITE3_TYPE_I64;
420 memcpy(val->val, &value, lite3_type_sizes[LITE3_TYPE_I64]);
421 return ret;
422}
423#endif // DOXYGEN_IGNORE
424
436#define lite3_ctx_set_f64(ctx, ofs, key, value) ({ \
437 const char *__lite3_key__ = (key); \
438 _lite3_ctx_set_f64_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), value); \
439})
440#ifndef DOXYGEN_IGNORE
441static inline int _lite3_ctx_set_f64_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data, double value)
442{
443 int ret;
444 if ((ret = _lite3_verify_obj_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, key)) < 0)
445 return ret;
446 lite3_val *val;
447 errno = 0;
448 while ((ret = lite3_set_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, key, key_data, lite3_type_sizes[LITE3_TYPE_F64], &val)) < 0) {
449 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
450 continue;
451 } else {
452 return ret;
453 }
454 }
455 val->type = (uint8_t)LITE3_TYPE_F64;
456 memcpy(val->val, &value, lite3_type_sizes[LITE3_TYPE_F64]);
457 return ret;
458}
459#endif // DOXYGEN_IGNORE
460
473#define lite3_ctx_set_bytes(ctx, ofs, key, bytes, bytes_len) ({ \
474 const char *__lite3_key__ = (key); \
475 _lite3_ctx_set_bytes_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), bytes, bytes_len); \
476})
477#ifndef DOXYGEN_IGNORE
478static inline int _lite3_ctx_set_bytes_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data, const unsigned char *__restrict bytes, size_t bytes_len)
479{
480 int ret;
481 if ((ret = _lite3_verify_obj_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, key)) < 0)
482 return ret;
483 lite3_val *val;
484 errno = 0;
485 while ((ret = lite3_set_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, key, key_data, lite3_type_sizes[LITE3_TYPE_BYTES] + bytes_len, &val)) < 0) {
486 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
487 continue;
488 } else {
489 return ret;
490 }
491 }
492 val->type = (uint8_t)LITE3_TYPE_BYTES;
493 memcpy(val->val, &bytes_len, lite3_type_sizes[LITE3_TYPE_BYTES]);
494 memcpy(val->val + lite3_type_sizes[LITE3_TYPE_BYTES], bytes, bytes_len);
495 return ret;
496}
497#endif // DOXYGEN_IGNORE
498
514#define lite3_ctx_set_str(ctx, ofs, key, str) ({ \
515 const char *__lite3_key__ = (key); \
516 _lite3_ctx_set_str_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), str); \
517})
518#ifndef DOXYGEN_IGNORE
519static inline int _lite3_ctx_set_str_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data, const char *__restrict str)
520{
521 int ret;
522 if ((ret = _lite3_verify_obj_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, key)) < 0)
523 return ret;
524 lite3_val *val;
525 size_t str_size = strlen(str) + 1;
526 errno = 0;
527 while ((ret = lite3_set_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, key, key_data, lite3_type_sizes[LITE3_TYPE_STRING] + str_size, &val)) < 0) {
528 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
529 continue;
530 } else {
531 return ret;
532 }
533 }
534 val->type = (uint8_t)LITE3_TYPE_STRING;
535 memcpy(val->val, &str_size, lite3_type_sizes[LITE3_TYPE_STRING]);
536 memcpy(val->val + lite3_type_sizes[LITE3_TYPE_STRING], str, str_size);
537 return ret;
538}
539#endif // DOXYGEN_IGNORE
540
556#define lite3_ctx_set_str_n(ctx, ofs, key, str, str_len) ({ \
557 const char *__lite3_key__ = (key); \
558 _lite3_ctx_set_str_n_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), str, str_len); \
559})
560#ifndef DOXYGEN_IGNORE
561static inline int _lite3_ctx_set_str_n_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data, const char *__restrict str, size_t str_len)
562{
563 int ret;
564 if ((ret = _lite3_verify_obj_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, key)) < 0)
565 return ret;
566 lite3_val *val;
567 size_t str_size = str_len + 1;
568 errno = 0;
569 while ((ret = lite3_set_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, key, key_data, lite3_type_sizes[LITE3_TYPE_STRING] + str_size, &val)) < 0) {
570 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
571 continue;
572 } else {
573 return ret;
574 }
575 }
576 val->type = (uint8_t)LITE3_TYPE_STRING;
577 memcpy(val->val, &str_size, lite3_type_sizes[LITE3_TYPE_STRING]);
578 memcpy(val->val + lite3_type_sizes[LITE3_TYPE_STRING], str, str_len);
579 *(val->val + lite3_type_sizes[LITE3_TYPE_STRING] + str_len) = 0x00; // Insert NULL-terminator
580 return ret;
581}
582#endif // DOXYGEN_IGNORE
583
595#define lite3_ctx_set_obj(ctx, ofs, key, out_ofs) ({ \
596 lite3_ctx *__lite3_ctx__ = (ctx); \
597 size_t __lite3_ofs__ = (ofs); \
598 const char *__lite3_key__ = (key); \
599 int __lite3_ret__; \
600 if ((__lite3_ret__ = _lite3_verify_obj_set( \
601 __lite3_ctx__->buf, \
602 &__lite3_ctx__->buflen, \
603 __lite3_ofs__, \
604 __lite3_ctx__->bufsz, \
605 __lite3_key__)) < 0) \
606 return __lite3_ret__; \
607 \
608 errno = 0; \
609 while ((__lite3_ret__ = lite3_set_obj_impl( \
610 __lite3_ctx__->buf, \
611 &__lite3_ctx__->buflen, \
612 __lite3_ofs__, \
613 __lite3_ctx__->bufsz, \
614 __lite3_key__, \
615 LITE3_KEY_DATA(key), \
616 out_ofs)) < 0) { \
617 if (errno == ENOBUFS && (lite3_ctx_grow_impl(__lite3_ctx__) == 0)) { \
618 continue; \
619 } else { \
620 return __lite3_ret__; \
621 } \
622 } \
623 __lite3_ret__; \
624})
625
637#define lite3_ctx_set_arr(ctx, ofs, key, out_ofs) ({ \
638 lite3_ctx *__lite3_ctx__ = (ctx); \
639 size_t __lite3_ofs__ = (ofs); \
640 const char *__lite3_key__ = (key); \
641 int __lite3_ret__; \
642 if ((__lite3_ret__ = _lite3_verify_obj_set( \
643 __lite3_ctx__->buf, \
644 &__lite3_ctx__->buflen, \
645 __lite3_ofs__, \
646 __lite3_ctx__->bufsz, \
647 __lite3_key__)) < 0) \
648 return __lite3_ret__; \
649 \
650 errno = 0; \
651 while ((__lite3_ret__ = lite3_set_arr_impl( \
652 __lite3_ctx__->buf, \
653 &__lite3_ctx__->buflen, \
654 __lite3_ofs__, \
655 __lite3_ctx__->bufsz, \
656 __lite3_key__, \
657 LITE3_KEY_DATA(key), \
658 out_ofs)) < 0) { \
659 if (errno == ENOBUFS && (lite3_ctx_grow_impl(__lite3_ctx__) == 0)) { \
660 continue; \
661 } else { \
662 return __lite3_ret__; \
663 } \
664 } \
665 __lite3_ret__; \
666})
668
669
670
690#ifndef DOXYGEN_IGNORE
691static inline int _lite3_ctx_set_by_index(lite3_ctx *ctx, size_t ofs, uint32_t index, size_t val_len, lite3_val **out)
692{
693 int ret;
694 if ((ret = _lite3_verify_arr_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz)) < 0)
695 return ret;
696 uint32_t size;
697 memcpy(&size, ctx->buf + ofs + LITE3_NODE_SIZE_KC_OFFSET, sizeof(size));
698 size >>= LITE3_NODE_SIZE_SHIFT;
699 if (LITE3_UNLIKELY(index > size)) {
700 LITE3_PRINT_ERROR("INVALID ARGUMENT: ARRAY INDEX %u OUT OF BOUNDS (size == %u)\n", index, size);
701 errno = EINVAL;
702 return -1;
703 }
704 lite3_key_data key_data = {
705 .hash = index,
706 .size = 0,
707 };
708 errno = 0;
709 while ((ret = lite3_set_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, NULL, key_data, val_len, out)) < 0) {
710 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
711 continue;
712 } else {
713 return ret;
714 }
715 }
716 return ret;
717}
718
719static inline int _lite3_ctx_set_by_append(lite3_ctx *ctx, size_t ofs, size_t val_len, lite3_val **out)
720{
721 int ret;
722 if ((ret = _lite3_verify_arr_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz)) < 0)
723 return ret;
724 uint32_t size;
725 memcpy(&size, ctx->buf + ofs + LITE3_NODE_SIZE_KC_OFFSET, sizeof(size));
726 size >>= LITE3_NODE_SIZE_SHIFT;
727 lite3_key_data key_data = {
728 .hash = size,
729 .size = 0,
730 };
731 errno = 0;
732 while ((ret = lite3_set_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, NULL, key_data, val_len, out)) < 0) {
733 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
734 continue;
735 } else {
736 return ret;
737 }
738 }
739 return ret;
740}
741#endif // DOXYGEN_IGNORE
742
750 lite3_ctx *ctx,
751 size_t ofs)
752{
753 lite3_val *val;
754 int ret;
755 if ((ret = _lite3_ctx_set_by_append(ctx, ofs, lite3_type_sizes[LITE3_TYPE_NULL], &val)) < 0)
756 return ret;
757 val->type = (uint8_t)LITE3_TYPE_NULL;
758 return ret;
759}
760
768 lite3_ctx *ctx,
769 size_t ofs,
770 bool value)
771{
772 lite3_val *val;
773 int ret;
774 if ((ret = _lite3_ctx_set_by_append(ctx, ofs, lite3_type_sizes[LITE3_TYPE_BOOL], &val)) < 0)
775 return ret;
776 val->type = (uint8_t)LITE3_TYPE_BOOL;
777 memcpy(val->val, &value, lite3_type_sizes[LITE3_TYPE_BOOL]);
778 return ret;
779}
780
787static inline int lite3_ctx_arr_append_i64(
788 lite3_ctx *ctx,
789 size_t ofs,
790 int64_t value)
791{
792 lite3_val *val;
793 int ret;
794 if ((ret = _lite3_ctx_set_by_append(ctx, ofs, lite3_type_sizes[LITE3_TYPE_I64], &val)) < 0)
795 return ret;
796 val->type = (uint8_t)LITE3_TYPE_I64;
797 memcpy(val->val, &value, lite3_type_sizes[LITE3_TYPE_I64]);
798 return ret;
799}
800
807static inline int lite3_ctx_arr_append_f64(
808 lite3_ctx *ctx,
809 size_t ofs,
810 double value)
811{
812 lite3_val *val;
813 int ret;
814 if ((ret = _lite3_ctx_set_by_append(ctx, ofs, lite3_type_sizes[LITE3_TYPE_F64], &val)) < 0)
815 return ret;
816 val->type = (uint8_t)LITE3_TYPE_F64;
817 memcpy(val->val, &value, lite3_type_sizes[LITE3_TYPE_F64]);
818 return ret;
819}
820
828 lite3_ctx *ctx,
829 size_t ofs,
830 const unsigned char *__restrict bytes,
831 size_t bytes_len)
832{
833 lite3_val *val;
834 int ret;
835 if ((ret = _lite3_ctx_set_by_append(ctx, ofs, lite3_type_sizes[LITE3_TYPE_BYTES] + bytes_len, &val)) < 0)
836 return ret;
837 val->type = (uint8_t)LITE3_TYPE_BYTES;
838 memcpy(val->val, &bytes_len, lite3_type_sizes[LITE3_TYPE_BYTES]);
839 memcpy(val->val + lite3_type_sizes[LITE3_TYPE_BYTES], bytes, bytes_len);
840 return ret;
841}
842
853static inline int lite3_ctx_arr_append_str(
854 lite3_ctx *ctx,
855 size_t ofs,
856 const char *__restrict str)
857{
858 lite3_val *val;
859 size_t str_size = strlen(str) + 1;
860 int ret;
861 if ((ret = _lite3_ctx_set_by_append(ctx, ofs, lite3_type_sizes[LITE3_TYPE_STRING] + str_size, &val)) < 0)
862 return ret;
863 val->type = (uint8_t)LITE3_TYPE_STRING;
864 memcpy(val->val, &str_size, lite3_type_sizes[LITE3_TYPE_STRING]);
865 memcpy(val->val + lite3_type_sizes[LITE3_TYPE_STRING], str, str_size);
866 return ret;
867}
868
879 lite3_ctx *ctx,
880 size_t ofs,
881 const char *__restrict str,
882 size_t str_len)
883{
884 lite3_val *val;
885 size_t str_size = str_len + 1;
886 int ret;
887 if ((ret = _lite3_ctx_set_by_append(ctx, ofs, lite3_type_sizes[LITE3_TYPE_STRING] + str_size, &val)) < 0)
888 return ret;
889 val->type = (uint8_t)LITE3_TYPE_STRING;
890 memcpy(val->val, &str_size, lite3_type_sizes[LITE3_TYPE_STRING]);
891 memcpy(val->val + lite3_type_sizes[LITE3_TYPE_STRING], str, str_len);
892 *(val->val + lite3_type_sizes[LITE3_TYPE_STRING] + str_len) = 0x00; // Insert NULL-terminator
893 return ret;
894}
895
902static inline int lite3_ctx_arr_append_obj(
903 lite3_ctx *ctx,
904 size_t ofs,
905 size_t *__restrict out_ofs)
906{
907 int ret;
908 if ((ret = _lite3_verify_arr_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz)) < 0)
909 return ret;
910 errno = 0;
911 while ((ret = lite3_arr_append_obj_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, out_ofs)) < 0) {
912 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
913 continue;
914 } else {
915 return ret;
916 }
917 }
918 return ret;
919}
920
927static inline int lite3_ctx_arr_append_arr(
928 lite3_ctx *ctx,
929 size_t ofs,
930 size_t *__restrict out_ofs)
931{
932 int ret;
933 if ((ret = _lite3_verify_arr_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz)) < 0)
934 return ret;
935 errno = 0;
936 while ((ret = lite3_arr_append_arr_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, out_ofs)) < 0) {
937 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
938 continue;
939 } else {
940 return ret;
941 }
942 }
943 return ret;
944}
946
947
979static inline int lite3_ctx_arr_set_null(
980 lite3_ctx *ctx,
981 size_t ofs,
982 uint32_t index)
983{
984 lite3_val *val;
985 int ret;
986 if ((ret = _lite3_ctx_set_by_index(ctx, ofs, index, lite3_type_sizes[LITE3_TYPE_NULL], &val)) < 0)
987 return ret;
988 val->type = (uint8_t)LITE3_TYPE_NULL;
989 return ret;
990}
991
998static inline int lite3_ctx_arr_set_bool(
999 lite3_ctx *ctx,
1000 size_t ofs,
1001 uint32_t index,
1002 bool value)
1003{
1004 lite3_val *val;
1005 int ret;
1006 if ((ret = _lite3_ctx_set_by_index(ctx, ofs, index, lite3_type_sizes[LITE3_TYPE_BOOL], &val)) < 0)
1007 return ret;
1008 val->type = (uint8_t)LITE3_TYPE_BOOL;
1009 memcpy(val->val, &value, lite3_type_sizes[LITE3_TYPE_BOOL]);
1010 return ret;
1011}
1012
1019static inline int lite3_ctx_arr_set_i64(
1020 lite3_ctx *ctx,
1021 size_t ofs,
1022 uint32_t index,
1023 int64_t value)
1024{
1025 lite3_val *val;
1026 int ret;
1027 if ((ret = _lite3_ctx_set_by_index(ctx, ofs, index, lite3_type_sizes[LITE3_TYPE_I64], &val)) < 0)
1028 return ret;
1029 val->type = (uint8_t)LITE3_TYPE_I64;
1030 memcpy(val->val, &value, lite3_type_sizes[LITE3_TYPE_I64]);
1031 return ret;
1032}
1033
1040static inline int lite3_ctx_arr_set_f64(
1041 lite3_ctx *ctx,
1042 size_t ofs,
1043 uint32_t index,
1044 double value)
1045{
1046 lite3_val *val;
1047 int ret;
1048 if ((ret = _lite3_ctx_set_by_index(ctx, ofs, index, lite3_type_sizes[LITE3_TYPE_F64], &val)) < 0)
1049 return ret;
1050 val->type = (uint8_t)LITE3_TYPE_F64;
1051 memcpy(val->val, &value, lite3_type_sizes[LITE3_TYPE_F64]);
1052 return ret;
1053}
1054
1061static inline int lite3_ctx_arr_set_bytes(
1062 lite3_ctx *ctx,
1063 size_t ofs,
1064 uint32_t index,
1065 const unsigned char *__restrict bytes,
1066 size_t bytes_len)
1067{
1068 lite3_val *val;
1069 int ret;
1070 if ((ret = _lite3_ctx_set_by_index(ctx, ofs, index, lite3_type_sizes[LITE3_TYPE_BYTES] + bytes_len, &val)) < 0)
1071 return ret;
1072 val->type = (uint8_t)LITE3_TYPE_BYTES;
1073 memcpy(val->val, &bytes_len, lite3_type_sizes[LITE3_TYPE_BYTES]);
1074 memcpy(val->val + lite3_type_sizes[LITE3_TYPE_BYTES], bytes, bytes_len);
1075 return ret;
1076}
1077
1088static inline int lite3_ctx_arr_set_str(
1089 lite3_ctx *ctx,
1090 size_t ofs,
1091 uint32_t index,
1092 const char *__restrict str)
1093{
1094 lite3_val *val;
1095 size_t str_size = strlen(str) + 1;
1096 int ret;
1097 if ((ret = _lite3_ctx_set_by_index(ctx, ofs, index, lite3_type_sizes[LITE3_TYPE_STRING] + str_size, &val)) < 0)
1098 return ret;
1099 val->type = (uint8_t)LITE3_TYPE_STRING;
1100 memcpy(val->val, &str_size, lite3_type_sizes[LITE3_TYPE_STRING]);
1101 memcpy(val->val + lite3_type_sizes[LITE3_TYPE_STRING], str, str_size);
1102 return ret;
1103}
1104
1114static inline int lite3_ctx_arr_set_str_n(
1115 lite3_ctx *ctx,
1116 size_t ofs,
1117 uint32_t index,
1118 const char *__restrict str,
1119 size_t str_len)
1120{
1121 lite3_val *val;
1122 size_t str_size = str_len + 1;
1123 int ret;
1124 if ((ret = _lite3_ctx_set_by_index(ctx, ofs, index, lite3_type_sizes[LITE3_TYPE_STRING] + str_size, &val)) < 0)
1125 return ret;
1126 val->type = (uint8_t)LITE3_TYPE_STRING;
1127 memcpy(val->val, &str_size, lite3_type_sizes[LITE3_TYPE_STRING]);
1128 memcpy(val->val + lite3_type_sizes[LITE3_TYPE_STRING], str, str_len);
1129 *(val->val + lite3_type_sizes[LITE3_TYPE_STRING] + str_len) = 0x00; // Insert NULL-terminator
1130 return ret;
1131}
1132
1139static inline int lite3_ctx_arr_set_obj(
1140 lite3_ctx *ctx,
1141 size_t ofs,
1142 uint32_t index,
1143 size_t *__restrict out_ofs)
1144{
1145 int ret;
1146 if ((ret = _lite3_verify_arr_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz)) < 0)
1147 return ret;
1148 errno = 0;
1149 while ((ret = lite3_arr_set_obj_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, index, out_ofs)) < 0) {
1150 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
1151 continue;
1152 } else {
1153 return ret;
1154 }
1155 }
1156 return ret;
1157}
1158
1165static inline int lite3_ctx_arr_set_arr(
1166 lite3_ctx *ctx,
1167 size_t ofs,
1168 uint32_t index,
1169 size_t *__restrict out_ofs)
1170{
1171 int ret;
1172 if ((ret = _lite3_verify_arr_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz)) < 0)
1173 return ret;
1174 errno = 0;
1175 while ((ret = lite3_arr_set_arr_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, index, out_ofs)) < 0) {
1176 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
1177 continue;
1178 } else {
1179 return ret;
1180 }
1181 }
1182 return ret;
1183}
1185
1186
1204#ifdef LITE3_DEBUG
1205static inline void lite3_ctx_print(lite3_ctx *ctx) { lite3_print(ctx->buf, ctx->buflen); }
1206#else
1207static inline void lite3_ctx_print(lite3_ctx *ctx) { (void)ctx; }
1208#endif
1209
1219{
1220 if (_lite3_verify_get(ctx->buf, ctx->buflen, 0) < 0)
1221 return LITE3_TYPE_INVALID;
1222 return (enum lite3_type)(*(ctx->buf));
1223}
1224
1235#define lite3_ctx_get_type(ctx, ofs, key) ({ \
1236 const char *__lite3_key__ = (key); \
1237 _lite3_ctx_get_type_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1238})
1239#ifndef DOXYGEN_IGNORE
1240static inline enum lite3_type _lite3_ctx_get_type_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data)
1241{
1242 return _lite3_get_type_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1243}
1244#endif // DOXYGEN_IGNORE
1245
1256static inline enum lite3_type lite3_ctx_arr_get_type(lite3_ctx *ctx, size_t ofs, uint32_t index)
1257{
1258 return lite3_arr_get_type(ctx->buf, ctx->buflen, ofs, index);
1259}
1260
1275#define lite3_ctx_get_type_size(ctx, ofs, key, out) ({ \
1276 const char *__lite3_key__ = (key); \
1277 _lite3_ctx_get_type_size_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1278})
1279#ifndef DOXYGEN_IGNORE
1280static inline int _lite3_ctx_get_type_size_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data, size_t *__restrict out)
1281{
1282 return _lite3_get_type_size_impl(ctx->buf, ctx->buflen, ofs, key, key_data, out);
1283}
1284#endif // DOXYGEN_IGNORE
1285
1296#define lite3_ctx_exists(ctx, ofs, key) ({ \
1297 const char *__lite3_key__ = (key); \
1298 _lite3_ctx_exists_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1299})
1300#ifndef DOXYGEN_IGNORE
1301static inline bool _lite3_ctx_exists_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data)
1302{
1303 return _lite3_exists_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1304}
1305#endif // DOXYGEN_IGNORE
1306
1315static inline int lite3_ctx_count(
1316 lite3_ctx *ctx,
1317 size_t ofs,
1318 uint32_t *out)
1319{
1320 return lite3_count(ctx->buf, ctx->buflen, ofs, out);
1321}
1322
1333#define lite3_ctx_is_null(ctx, ofs, key) ({ \
1334 const char *__lite3_key__ = (key); \
1335 _lite3_ctx_is_null_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1336})
1337#ifndef DOXYGEN_IGNORE
1338static inline bool _lite3_ctx_is_null_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data)
1339{
1340 return _lite3_is_null_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1341}
1342#endif // DOXYGEN_IGNORE
1343
1354#define lite3_ctx_is_bool(ctx, ofs, key) ({ \
1355 const char *__lite3_key__ = (key); \
1356 _lite3_ctx_is_bool_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1357})
1358#ifndef DOXYGEN_IGNORE
1359static inline bool _lite3_ctx_is_bool_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data)
1360{
1361 return _lite3_is_bool_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1362}
1363#endif // DOXYGEN_IGNORE
1364
1375#define lite3_ctx_is_i64(ctx, ofs, key) ({ \
1376 const char *__lite3_key__ = (key); \
1377 _lite3_ctx_is_i64_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1378})
1379#ifndef DOXYGEN_IGNORE
1380static inline bool _lite3_ctx_is_i64_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data)
1381{
1382 return _lite3_is_i64_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1383}
1384#endif // DOXYGEN_IGNORE
1385
1396#define lite3_ctx_is_f64(ctx, ofs, key) ({ \
1397 const char *__lite3_key__ = (key); \
1398 _lite3_ctx_is_f64_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1399})
1400#ifndef DOXYGEN_IGNORE
1401static inline bool _lite3_ctx_is_f64_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data)
1402{
1403 return _lite3_is_f64_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1404}
1405#endif // DOXYGEN_IGNORE
1406
1417#define lite3_ctx_is_bytes(ctx, ofs, key) ({ \
1418 const char *__lite3_key__ = (key); \
1419 _lite3_ctx_is_bytes_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1420})
1421#ifndef DOXYGEN_IGNORE
1422static inline bool _lite3_ctx_is_bytes_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data)
1423{
1424 return _lite3_is_bytes_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1425}
1426#endif // DOXYGEN_IGNORE
1427
1438#define lite3_ctx_is_str(ctx, ofs, key) ({ \
1439 const char *__lite3_key__ = (key); \
1440 _lite3_ctx_is_str_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1441})
1442#ifndef DOXYGEN_IGNORE
1443static inline bool _lite3_ctx_is_str_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data)
1444{
1445 return _lite3_is_str_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1446}
1447#endif // DOXYGEN_IGNORE
1448
1459#define lite3_ctx_is_obj(ctx, ofs, key) ({ \
1460 const char *__lite3_key__ = (key); \
1461 _lite3_ctx_is_obj_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1462})
1463#ifndef DOXYGEN_IGNORE
1464static inline bool _lite3_ctx_is_obj_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data)
1465{
1466
1467 return _lite3_is_obj_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1468}
1469#endif // DOXYGEN_IGNORE
1470
1481#define lite3_ctx_is_arr(ctx, ofs, key) ({ \
1482 const char *__lite3_key__ = (key); \
1483 _lite3_ctx_is_arr_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1484})
1485#ifndef DOXYGEN_IGNORE
1486static inline bool _lite3_ctx_is_arr_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data)
1487{
1488 return _lite3_is_arr_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1489}
1490#endif // DOXYGEN_IGNORE
1492
1493
1494
1526#define lite3_ctx_get(ctx, ofs, key, out) ({ \
1527 lite3_ctx *__lite3_ctx__ = (ctx); \
1528 size_t __lite3_ofs__ = (ofs); \
1529 int __lite3_ret__; \
1530 if ((__lite3_ret__ = _lite3_verify_get(__lite3_ctx__->buf, __lite3_ctx__->buflen, __lite3_ofs__)) < 0) \
1531 return __lite3_ret__; \
1532 const char *__lite3_key__ = (key); \
1533 lite3_get_impl(__lite3_ctx__->buf, __lite3_ctx__->buflen, __lite3_ofs__, __lite3_key__, LITE3_KEY_DATA(key), out); \
1534})
1535
1547#define lite3_ctx_get_bool(ctx, ofs, key, out) ({ \
1548 const char *__lite3_key__ = (key); \
1549 _lite3_ctx_get_bool_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1550})
1551#ifndef DOXYGEN_IGNORE
1552static inline int _lite3_ctx_get_bool_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data, bool *out)
1553{
1554 return _lite3_get_bool_impl(ctx->buf, ctx->buflen, ofs, key, key_data, out);
1555}
1556#endif // DOXYGEN_IGNORE
1557
1569#define lite3_ctx_get_i64(ctx, ofs, key, out) ({ \
1570 const char *__lite3_key__ = (key); \
1571 _lite3_ctx_get_i64_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1572})
1573#ifndef DOXYGEN_IGNORE
1574static inline int _lite3_ctx_get_i64_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data, int64_t *out)
1575{
1576 return _lite3_get_i64_impl(ctx->buf, ctx->buflen, ofs, key, key_data, out);
1577}
1578#endif // DOXYGEN_IGNORE
1579
1591#define lite3_ctx_get_f64(ctx, ofs, key, out) ({ \
1592 const char *__lite3_key__ = (key); \
1593 _lite3_ctx_get_f64_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1594})
1595#ifndef DOXYGEN_IGNORE
1596static inline int _lite3_ctx_get_f64_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data, double *out)
1597{
1598 return _lite3_get_f64_impl(ctx->buf, ctx->buflen, ofs, key, key_data, out);
1599}
1600#endif // DOXYGEN_IGNORE
1601
1613#define lite3_ctx_get_bytes(ctx, ofs, key, out) ({ \
1614 const char *__lite3_key__ = (key); \
1615 _lite3_ctx_get_bytes_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1616})
1617#ifndef DOXYGEN_IGNORE
1618static inline int _lite3_ctx_get_bytes_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data, lite3_bytes *out)
1619{
1620 return _lite3_get_bytes_impl(ctx->buf, ctx->buflen, ofs, key, key_data, out);
1621}
1622#endif // DOXYGEN_IGNORE
1623
1635#define lite3_ctx_get_str(ctx, ofs, key, out) ({ \
1636 const char *__lite3_key__ = (key); \
1637 _lite3_ctx_get_str_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1638})
1639#ifndef DOXYGEN_IGNORE
1640static inline int _lite3_ctx_get_str_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data, lite3_str *out)
1641{
1642 return _lite3_get_str_impl(ctx->buf, ctx->buflen, ofs, key, key_data, out);
1643}
1644#endif // DOXYGEN_IGNORE
1645
1657#define lite3_ctx_get_obj(ctx, ofs, key, out) ({ \
1658 const char *__lite3_key__ = (key); \
1659 _lite3_ctx_get_obj_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1660})
1661#ifndef DOXYGEN_IGNORE
1662static inline int _lite3_ctx_get_obj_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data, size_t *__restrict out_ofs)
1663{
1664 return _lite3_get_obj_impl(ctx->buf, ctx->buflen, ofs, key, key_data, out_ofs);
1665}
1666#endif // DOXYGEN_IGNORE
1667
1679#define lite3_ctx_get_arr(ctx, ofs, key, out) ({ \
1680 const char *__lite3_key__ = (key); \
1681 _lite3_ctx_get_arr_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1682})
1683#ifndef DOXYGEN_IGNORE
1684static inline int _lite3_ctx_get_arr_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data, size_t *__restrict out_ofs)
1685{
1686 return _lite3_get_arr_impl(ctx->buf, ctx->buflen, ofs, key, key_data, out_ofs);
1687}
1688#endif // DOXYGEN_IGNORE
1690
1691
1692
1715static inline int lite3_ctx_arr_get_bool(
1716 lite3_ctx *ctx,
1717 size_t ofs,
1718 uint32_t index,
1719 bool *out)
1720{
1721 return lite3_arr_get_bool(ctx->buf, ctx->buflen, ofs, index, out);
1722}
1723
1730static inline int lite3_ctx_arr_get_i64(
1731 lite3_ctx *ctx,
1732 size_t ofs,
1733 uint32_t index,
1734 int64_t *out)
1735{
1736 return lite3_arr_get_i64(ctx->buf, ctx->buflen, ofs, index, out);
1737}
1738
1745static inline int lite3_ctx_arr_get_f64(
1746 lite3_ctx *ctx,
1747 size_t ofs,
1748 uint32_t index,
1749 double *out)
1750{
1751 return lite3_arr_get_f64(ctx->buf, ctx->buflen, ofs, index, out);
1752}
1753
1760static inline int lite3_ctx_arr_get_bytes(
1761 lite3_ctx *ctx,
1762 size_t ofs,
1763 uint32_t index,
1764 lite3_bytes *out)
1765{
1766 return lite3_arr_get_bytes(ctx->buf, ctx->buflen, ofs, index, out);
1767}
1768
1775static inline int lite3_ctx_arr_get_str(
1776 lite3_ctx *ctx,
1777 size_t ofs,
1778 uint32_t index,
1779 lite3_str *out)
1780{
1781 return lite3_arr_get_str(ctx->buf, ctx->buflen, ofs, index, out);
1782}
1783
1790static inline int lite3_ctx_arr_get_obj(
1791 lite3_ctx *ctx,
1792 size_t ofs,
1793 uint32_t index,
1794 size_t *__restrict out_ofs)
1795{
1796 return lite3_arr_get_obj(ctx->buf, ctx->buflen, ofs, index, out_ofs);
1797}
1798
1805static inline int lite3_ctx_arr_get_arr(
1806 lite3_ctx *ctx,
1807 size_t ofs,
1808 uint32_t index,
1809 size_t *__restrict out_ofs)
1810{
1811 return lite3_arr_get_arr(ctx->buf, ctx->buflen, ofs, index, out_ofs);
1812}
1814
1815
1816
1829#ifdef DOXYGEN_ONLY
1831#define LITE3_ITER_ITEM 1
1833#define LITE3_ITER_DONE 0
1834#endif
1835
1842static inline int lite3_ctx_iter_create(
1843 lite3_ctx *ctx,
1844 size_t ofs,
1845 lite3_iter *out)
1846{
1847 return lite3_iter_create(ctx->buf, ctx->buflen, ofs, out);
1848}
1849
1866static inline int lite3_ctx_iter_next(
1867 lite3_ctx *ctx,
1868 lite3_iter *iter,
1869 lite3_str *out_key,
1870 size_t *out_val_ofs)
1871{
1872 return lite3_iter_next(ctx->buf, ctx->buflen, iter, out_key, out_val_ofs);
1873}
1875
1876
1907#if defined(DOXYGEN_ONLY) && !defined(LITE3_JSON)
1908#define LITE3_JSON
1909#endif // DOXYGEN_ONLY
1910
1911#ifdef LITE3_JSON
1921static inline int lite3_ctx_json_dec(
1922 lite3_ctx *ctx,
1923 const char *__restrict json_str,
1924 size_t json_len)
1925{
1926 int ret;
1927 errno = 0;
1928 while ((ret = lite3_json_dec(ctx->buf, &ctx->buflen, ctx->bufsz, json_str, json_len)) < 0) {
1929 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
1930 continue;
1931 } else {
1932 return ret;
1933 }
1934 }
1935 return ret;
1936}
1937
1947static inline int lite3_ctx_json_dec_file(
1948 lite3_ctx *ctx,
1949 const char *__restrict path)
1950{
1951 int ret;
1952 errno = 0;
1953 while ((ret = lite3_json_dec_file(ctx->buf, &ctx->buflen, ctx->bufsz, path)) < 0) {
1954 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
1955 continue;
1956 } else {
1957 return ret;
1958 }
1959 }
1960 return ret;
1961}
1962
1972static inline int lite3_ctx_json_dec_fp(
1973 lite3_ctx *ctx,
1974 FILE *fp)
1975{
1976 int ret;
1977 errno = 0;
1978 while ((ret = lite3_json_dec_fp(ctx->buf, &ctx->buflen, ctx->bufsz, fp)) < 0) {
1979 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
1980 continue;
1981 } else {
1982 return ret;
1983 }
1984 }
1985 return ret;
1986}
1987
2001static inline int lite3_ctx_json_print(
2002 lite3_ctx *ctx,
2003 size_t ofs)
2004{
2005 return lite3_json_print(ctx->buf, ctx->buflen, ofs);
2006}
2007
2021static inline char *lite3_ctx_json_enc(
2022 lite3_ctx *ctx,
2023 size_t ofs,
2024 size_t *__restrict out_len)
2025{
2026 return lite3_json_enc(ctx->buf, ctx->buflen, ofs, out_len);
2027}
2028
2044static inline char *lite3_ctx_json_enc_pretty(
2045 lite3_ctx *ctx,
2046 size_t ofs,
2047 size_t *__restrict out_len)
2048{
2049 return lite3_json_enc_pretty(ctx->buf, ctx->buflen, ofs, out_len);
2050}
2051
2062static inline int64_t lite3_ctx_json_enc_buf(
2063 lite3_ctx *ctx,
2064 size_t ofs,
2065 char *__restrict json_buf,
2066 size_t json_bufsz
2067)
2068{
2069 return lite3_json_enc_buf(ctx->buf, ctx->buflen, ofs, json_buf, json_bufsz);
2070}
2071
2084static inline int64_t lite3_ctx_json_enc_pretty_buf(
2085 lite3_ctx *ctx,
2086 size_t ofs,
2087 char *__restrict json_buf,
2088 size_t json_bufsz)
2089{
2090 return lite3_json_enc_pretty_buf(ctx->buf, ctx->buflen, ofs, json_buf, json_bufsz);
2091}
2092#else
2093static inline int lite3_ctx_json_dec(lite3_ctx *ctx, const char *__restrict json_str, size_t json_len)
2094{
2095 (void)ctx; (void)json_str; (void)json_len;
2096 return -1;
2097}
2098
2099static inline int lite3_ctx_json_dec_file(lite3_ctx *ctx, const char *__restrict path)
2100{
2101 (void)ctx; (void)path;
2102 return -1;
2103}
2104
2105static inline int lite3_ctx_json_dec_fp(lite3_ctx *ctx, FILE *fp)
2106{
2107 (void)ctx; (void)fp;
2108 return -1;
2109}
2110
2111static inline int lite3_ctx_json_print(lite3_ctx *ctx, size_t ofs)
2112{
2113 (void)ctx; (void)ofs;
2114 return 0;
2115}
2116
2117static inline char *lite3_ctx_json_enc(lite3_ctx *ctx, size_t ofs, size_t *out_len)
2118{
2119 (void)ctx; (void)ofs; (void)out_len;
2120 return NULL;
2121}
2122
2123static inline char *lite3_ctx_json_enc_pretty(lite3_ctx *ctx, size_t ofs, size_t *out_len)
2124{
2125 (void)ctx; (void)ofs; (void)out_len;
2126 return NULL;
2127}
2128
2129static inline int64_t lite3_ctx_json_enc_buf(lite3_ctx *ctx, size_t ofs, char *__restrict json_buf, size_t json_bufsz)
2130{
2131 (void)ctx; (void)ofs; (void)json_buf; (void)json_bufsz;
2132 return -1;
2133}
2134
2135static inline int64_t lite3_ctx_json_enc_pretty_buf(lite3_ctx *ctx, size_t ofs, char *__restrict json_buf, size_t json_bufsz)
2136{
2137 (void)ctx; (void)ofs; (void)json_buf; (void)json_bufsz;
2138 return -1;
2139}
2140#endif
2142
2143#ifdef __cplusplus
2144}
2145#endif
2146
2147#endif // LITE3_CONTEXT_API_H
static int lite3_arr_get_obj(const unsigned char *buf, size_t buflen, size_t ofs, uint32_t index, size_t *__restrict out_ofs)
Get object by index.
Definition lite3.h:2596
static int lite3_arr_get_i64(const unsigned char *buf, size_t buflen, size_t ofs, uint32_t index, int64_t *out)
Get integer value by index.
Definition lite3.h:2481
static int lite3_arr_get_bool(const unsigned char *buf, size_t buflen, size_t ofs, uint32_t index, bool *out)
Get boolean value by index.
Definition lite3.h:2455
static int lite3_arr_get_f64(const unsigned char *buf, size_t buflen, size_t ofs, uint32_t index, double *out)
Get floating point value by index.
Definition lite3.h:2507
static int lite3_arr_get_str(const unsigned char *buf, size_t buflen, size_t ofs, uint32_t index, lite3_str *out)
Get string value by index.
Definition lite3.h:2564
static int lite3_arr_get_bytes(const unsigned char *buf, size_t buflen, size_t ofs, uint32_t index, lite3_bytes *out)
Get bytes value by index.
Definition lite3.h:2533
static int lite3_arr_get_arr(const unsigned char *buf, size_t buflen, size_t ofs, uint32_t index, size_t *__restrict out_ofs)
Get array by index.
Definition lite3.h:2622
#define LITE3_CONTEXT_BUF_SIZE_MIN
The minimum buffer size for a Lite³ context.
#define LITE3_NODE_ALIGNMENT
B-tree node alignment.
Definition lite3.h:275
#define LITE3_NODE_SIZE_KC_OFFSET
Offset of the size_kc field inside struct node.
Definition lite3.h:322
static int lite3_ctx_arr_append_bytes(lite3_ctx *ctx, size_t ofs, const unsigned char *__restrict bytes, size_t bytes_len)
Append bytes to array.
static int lite3_ctx_arr_append_f64(lite3_ctx *ctx, size_t ofs, double value)
Append floating point to array.
static int lite3_ctx_arr_append_null(lite3_ctx *ctx, size_t ofs)
Append null to array.
static int lite3_ctx_arr_append_str_n(lite3_ctx *ctx, size_t ofs, const char *__restrict str, size_t str_len)
Append string to array by length.
static int lite3_ctx_arr_append_str(lite3_ctx *ctx, size_t ofs, const char *__restrict str)
Append string to array.
static int lite3_ctx_arr_append_obj(lite3_ctx *ctx, size_t ofs, size_t *__restrict out_ofs)
Append object to array.
static int lite3_ctx_arr_append_arr(lite3_ctx *ctx, size_t ofs, size_t *__restrict out_ofs)
Append array to array.
static int lite3_ctx_arr_append_i64(lite3_ctx *ctx, size_t ofs, int64_t value)
Append integer to array.
static int lite3_ctx_arr_append_bool(lite3_ctx *ctx, size_t ofs, bool value)
Append boolean to array.
static int lite3_ctx_arr_get_arr(lite3_ctx *ctx, size_t ofs, uint32_t index, size_t *__restrict out_ofs)
Get array by index.
static int lite3_ctx_arr_get_str(lite3_ctx *ctx, size_t ofs, uint32_t index, lite3_str *out)
Get string value by index.
static int lite3_ctx_arr_get_i64(lite3_ctx *ctx, size_t ofs, uint32_t index, int64_t *out)
Get integer value by index.
static int lite3_ctx_arr_get_bool(lite3_ctx *ctx, size_t ofs, uint32_t index, bool *out)
Get boolean value by index.
static int lite3_ctx_arr_get_bytes(lite3_ctx *ctx, size_t ofs, uint32_t index, lite3_bytes *out)
Get bytes value by index.
static int lite3_ctx_arr_get_obj(lite3_ctx *ctx, size_t ofs, uint32_t index, size_t *__restrict out_ofs)
Get object by index.
static int lite3_ctx_arr_get_f64(lite3_ctx *ctx, size_t ofs, uint32_t index, double *out)
Get floating point value by index.
static int lite3_ctx_arr_set_i64(lite3_ctx *ctx, size_t ofs, uint32_t index, int64_t value)
Set integer in array.
static int lite3_ctx_arr_set_arr(lite3_ctx *ctx, size_t ofs, uint32_t index, size_t *__restrict out_ofs)
Set array in array.
static int lite3_ctx_arr_set_str(lite3_ctx *ctx, size_t ofs, uint32_t index, const char *__restrict str)
Set string in array.
static int lite3_ctx_arr_set_f64(lite3_ctx *ctx, size_t ofs, uint32_t index, double value)
Set float in array.
static int lite3_ctx_arr_set_obj(lite3_ctx *ctx, size_t ofs, uint32_t index, size_t *__restrict out_ofs)
Set object in array.
static int lite3_ctx_arr_set_str_n(lite3_ctx *ctx, size_t ofs, uint32_t index, const char *__restrict str, size_t str_len)
Set string in array by length.
static int lite3_ctx_arr_set_bool(lite3_ctx *ctx, size_t ofs, uint32_t index, bool value)
Set boolean in array.
static int lite3_ctx_arr_set_bytes(lite3_ctx *ctx, size_t ofs, uint32_t index, const unsigned char *__restrict bytes, size_t bytes_len)
Set bytes in array.
static int lite3_ctx_arr_set_null(lite3_ctx *ctx, size_t ofs, uint32_t index)
Set null in array.
static int lite3_ctx_init_arr(lite3_ctx *ctx)
Initialize a Lite³ context as an array.
static int lite3_ctx_init_obj(lite3_ctx *ctx)
Initialize a Lite³ context as an object.
static int lite3_ctx_iter_create(lite3_ctx *ctx, size_t ofs, lite3_iter *out)
Create a lite3 iterator for the given object or array.
static int lite3_ctx_iter_next(lite3_ctx *ctx, lite3_iter *iter, lite3_str *out_key, size_t *out_val_ofs)
Get the next item from a lite3 iterator.
static int64_t lite3_ctx_json_enc_pretty_buf(lite3_ctx *ctx, size_t ofs, char *__restrict json_buf, size_t json_bufsz)
Convert Lite³ to prettified JSON and write to output buffer.
static char * lite3_ctx_json_enc(lite3_ctx *ctx, size_t ofs, size_t *__restrict out_len)
Convert Lite³ to JSON string.
static int lite3_ctx_json_dec(lite3_ctx *ctx, const char *__restrict json_str, size_t json_len)
Convert JSON string to Lite³
static int lite3_ctx_json_print(lite3_ctx *ctx, size_t ofs)
Print Lite³ buffer as JSON to stdout
static int64_t lite3_ctx_json_enc_buf(lite3_ctx *ctx, size_t ofs, char *__restrict json_buf, size_t json_bufsz)
Convert Lite³ to JSON and write to output buffer.
static int lite3_ctx_json_dec_file(lite3_ctx *ctx, const char *__restrict path)
Convert JSON from file path to Lite³
static int lite3_ctx_json_dec_fp(lite3_ctx *ctx, FILE *fp)
Convert JSON from file pointer to Lite³
static char * lite3_ctx_json_enc_pretty(lite3_ctx *ctx, size_t ofs, size_t *__restrict out_len)
Convert Lite³ to JSON prettified string.
static lite3_ctx * lite3_ctx_create(void)
Create context with minimum size.
lite3_ctx * lite3_ctx_create_with_size(size_t bufsz)
Create context with custom size.
Definition ctx_api.c:75
void lite3_ctx_destroy(lite3_ctx *ctx)
Destroy context.
Definition ctx_api.c:229
lite3_ctx * lite3_ctx_create_take_ownership(unsigned char *buf, size_t buflen, size_t bufsz)
Create context by taking ownership of a buffer.
Definition ctx_api.c:126
lite3_ctx * lite3_ctx_create_from_buf(const unsigned char *buf, size_t buflen)
Create context by copying from a buffer.
Definition ctx_api.c:97
int lite3_ctx_import_from_buf(lite3_ctx *ctx, const unsigned char *buf, size_t buflen)
Copy data into existing context.
Definition ctx_api.c:193
static int lite3_ctx_count(lite3_ctx *ctx, size_t ofs, uint32_t *out)
Write back the number of object entries or array elements.
static enum lite3_type lite3_ctx_arr_get_type(lite3_ctx *ctx, size_t ofs, uint32_t index)
Find array value by index and return value type.
static enum lite3_type lite3_ctx_get_root_type(lite3_ctx *ctx)
Get the root type of a Lite³ buffer.
static void lite3_ctx_print(lite3_ctx *ctx)
View the internal structure of a Lite³ buffer.
int lite3_iter_next(const unsigned char *buf, size_t buflen, lite3_iter *iter, lite3_str *out_key, size_t *out_val_ofs)
Get the next item from a lite3 iterator.
Definition lite3.c:373
static int lite3_iter_create(const unsigned char *buf, size_t buflen, size_t ofs, lite3_iter *out)
Create a lite3 iterator for the given object or array.
Definition lite3.h:2689
int lite3_json_dec_file(unsigned char *buf, size_t *__restrict out_buflen, size_t bufsz, const char *__restrict path)
Convert JSON from file path to Lite³
int64_t lite3_json_enc_pretty_buf(const unsigned char *buf, size_t buflen, size_t ofs, char *__restrict json_buf, size_t json_bufsz)
Convert Lite³ to prettified JSON and write to output buffer.
char * lite3_json_enc_pretty(const unsigned char *buf, size_t buflen, size_t ofs, size_t *__restrict out_len)
Convert Lite³ to JSON prettified string.
int lite3_json_print(const unsigned char *buf, size_t buflen, size_t ofs)
Print Lite³ buffer as JSON to stdout
int lite3_json_dec(unsigned char *buf, size_t *__restrict out_buflen, size_t bufsz, const char *__restrict json_str, size_t json_len)
Convert JSON string to Lite³
int lite3_json_dec_fp(unsigned char *buf, size_t *__restrict out_buflen, size_t bufsz, FILE *fp)
Convert JSON from file pointer to Lite³
int64_t lite3_json_enc_buf(const unsigned char *buf, size_t buflen, size_t ofs, char *__restrict json_buf, size_t json_bufsz)
Convert Lite³ to JSON and write to output buffer.
char * lite3_json_enc(const unsigned char *buf, size_t buflen, size_t ofs, size_t *__restrict out_len)
Convert Lite³ to JSON string.
lite3_type
enum containing all Lite³ types
Definition lite3.h:500
@ LITE3_TYPE_INVALID
any type value equal or greater than this is considered invalid
Definition lite3.h:509
@ LITE3_TYPE_STRING
maps to 'string' type in JSON
Definition lite3.h:506
@ LITE3_TYPE_BOOL
maps to 'boolean' type in JSON; underlying datatype: bool
Definition lite3.h:502
@ LITE3_TYPE_BYTES
coverted to base64 string in JSON
Definition lite3.h:505
@ LITE3_TYPE_F64
maps to 'number' type in JSON; underlying datatype: double
Definition lite3.h:504
@ LITE3_TYPE_I64
maps to 'number' type in JSON; underlying datatype: int64_t
Definition lite3.h:503
@ LITE3_TYPE_NULL
maps to 'null' type in JSON
Definition lite3.h:501
static int lite3_count(unsigned char *buf, size_t buflen, size_t ofs, uint32_t *out)
Write back the number of object entries or array elements.
Definition lite3.h:1888
static enum lite3_type lite3_arr_get_type(const unsigned char *buf, size_t buflen, size_t ofs, uint32_t index)
Find value by index and return value type.
Definition lite3.h:1805
Lite³ Buffer API Header.
Struct holding a reference to a bytes value inside a Lite³ buffer.
Definition lite3.h:562
Lite³ context struct.
Struct containing iterator state.
Definition lite3.h:2671
Struct holding a reference to a string inside a Lite³ buffer.
Definition lite3.h:591
Struct representing a value inside a Lite³ buffer.
Definition lite3.h:521