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 = (*(uint32_t *)(ctx->buf + ofs + LITE3_NODE_SIZE_KC_OFFSET)) >> LITE3_NODE_SIZE_SHIFT;
697 if (LITE3_UNLIKELY(index > size)) {
698 LITE3_PRINT_ERROR("INVALID ARGUMENT: ARRAY INDEX %u OUT OF BOUNDS (size == %u)\n", index, size);
699 errno = EINVAL;
700 return -1;
701 }
702 lite3_key_data key_data = {
703 .hash = index,
704 .size = 0,
705 };
706 errno = 0;
707 while ((ret = lite3_set_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, NULL, key_data, val_len, out)) < 0) {
708 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
709 continue;
710 } else {
711 return ret;
712 }
713 }
714 return ret;
715}
716
717static inline int _lite3_ctx_set_by_append(lite3_ctx *ctx, size_t ofs, size_t val_len, lite3_val **out)
718{
719 int ret;
720 if ((ret = _lite3_verify_arr_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz)) < 0)
721 return ret;
722 uint32_t size = (*(uint32_t *)(ctx->buf + ofs + LITE3_NODE_SIZE_KC_OFFSET)) >> LITE3_NODE_SIZE_SHIFT;
723 lite3_key_data key_data = {
724 .hash = size,
725 .size = 0,
726 };
727 errno = 0;
728 while ((ret = lite3_set_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, NULL, key_data, val_len, out)) < 0) {
729 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
730 continue;
731 } else {
732 return ret;
733 }
734 }
735 return ret;
736}
737#endif // DOXYGEN_IGNORE
738
746 lite3_ctx *ctx,
747 size_t ofs)
748{
749 lite3_val *val;
750 int ret;
751 if ((ret = _lite3_ctx_set_by_append(ctx, ofs, lite3_type_sizes[LITE3_TYPE_NULL], &val)) < 0)
752 return ret;
753 val->type = (uint8_t)LITE3_TYPE_NULL;
754 return ret;
755}
756
764 lite3_ctx *ctx,
765 size_t ofs,
766 bool value)
767{
768 lite3_val *val;
769 int ret;
770 if ((ret = _lite3_ctx_set_by_append(ctx, ofs, lite3_type_sizes[LITE3_TYPE_BOOL], &val)) < 0)
771 return ret;
772 val->type = (uint8_t)LITE3_TYPE_BOOL;
773 memcpy(val->val, &value, lite3_type_sizes[LITE3_TYPE_BOOL]);
774 return ret;
775}
776
783static inline int lite3_ctx_arr_append_i64(
784 lite3_ctx *ctx,
785 size_t ofs,
786 int64_t value)
787{
788 lite3_val *val;
789 int ret;
790 if ((ret = _lite3_ctx_set_by_append(ctx, ofs, lite3_type_sizes[LITE3_TYPE_I64], &val)) < 0)
791 return ret;
792 val->type = (uint8_t)LITE3_TYPE_I64;
793 memcpy(val->val, &value, lite3_type_sizes[LITE3_TYPE_I64]);
794 return ret;
795}
796
803static inline int lite3_ctx_arr_append_f64(
804 lite3_ctx *ctx,
805 size_t ofs,
806 double value)
807{
808 lite3_val *val;
809 int ret;
810 if ((ret = _lite3_ctx_set_by_append(ctx, ofs, lite3_type_sizes[LITE3_TYPE_F64], &val)) < 0)
811 return ret;
812 val->type = (uint8_t)LITE3_TYPE_F64;
813 memcpy(val->val, &value, lite3_type_sizes[LITE3_TYPE_F64]);
814 return ret;
815}
816
824 lite3_ctx *ctx,
825 size_t ofs,
826 const unsigned char *__restrict bytes,
827 size_t bytes_len)
828{
829 lite3_val *val;
830 int ret;
831 if ((ret = _lite3_ctx_set_by_append(ctx, ofs, lite3_type_sizes[LITE3_TYPE_BYTES] + bytes_len, &val)) < 0)
832 return ret;
833 val->type = (uint8_t)LITE3_TYPE_BYTES;
834 memcpy(val->val, &bytes_len, lite3_type_sizes[LITE3_TYPE_BYTES]);
835 memcpy(val->val + lite3_type_sizes[LITE3_TYPE_BYTES], bytes, bytes_len);
836 return ret;
837}
838
849static inline int lite3_ctx_arr_append_str(
850 lite3_ctx *ctx,
851 size_t ofs,
852 const char *__restrict str)
853{
854 lite3_val *val;
855 size_t str_size = strlen(str) + 1;
856 int ret;
857 if ((ret = _lite3_ctx_set_by_append(ctx, ofs, lite3_type_sizes[LITE3_TYPE_STRING] + str_size, &val)) < 0)
858 return ret;
859 val->type = (uint8_t)LITE3_TYPE_STRING;
860 memcpy(val->val, &str_size, lite3_type_sizes[LITE3_TYPE_STRING]);
861 memcpy(val->val + lite3_type_sizes[LITE3_TYPE_STRING], str, str_size);
862 return ret;
863}
864
875 lite3_ctx *ctx,
876 size_t ofs,
877 const char *__restrict str,
878 size_t str_len)
879{
880 lite3_val *val;
881 size_t str_size = str_len + 1;
882 int ret;
883 if ((ret = _lite3_ctx_set_by_append(ctx, ofs, lite3_type_sizes[LITE3_TYPE_STRING] + str_size, &val)) < 0)
884 return ret;
885 val->type = (uint8_t)LITE3_TYPE_STRING;
886 memcpy(val->val, &str_size, lite3_type_sizes[LITE3_TYPE_STRING]);
887 memcpy(val->val + lite3_type_sizes[LITE3_TYPE_STRING], str, str_len);
888 *(val->val + lite3_type_sizes[LITE3_TYPE_STRING] + str_len) = 0x00; // Insert NULL-terminator
889 return ret;
890}
891
898static inline int lite3_ctx_arr_append_obj(
899 lite3_ctx *ctx,
900 size_t ofs,
901 size_t *__restrict out_ofs)
902{
903 int ret;
904 if ((ret = _lite3_verify_arr_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz)) < 0)
905 return ret;
906 errno = 0;
907 while ((ret = lite3_arr_append_obj_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, out_ofs)) < 0) {
908 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
909 continue;
910 } else {
911 return ret;
912 }
913 }
914 return ret;
915}
916
923static inline int lite3_ctx_arr_append_arr(
924 lite3_ctx *ctx,
925 size_t ofs,
926 size_t *__restrict out_ofs)
927{
928 int ret;
929 if ((ret = _lite3_verify_arr_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz)) < 0)
930 return ret;
931 errno = 0;
932 while ((ret = lite3_arr_append_arr_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, out_ofs)) < 0) {
933 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
934 continue;
935 } else {
936 return ret;
937 }
938 }
939 return ret;
940}
942
943
975static inline int lite3_ctx_arr_set_null(
976 lite3_ctx *ctx,
977 size_t ofs,
978 uint32_t index)
979{
980 lite3_val *val;
981 int ret;
982 if ((ret = _lite3_ctx_set_by_index(ctx, ofs, index, lite3_type_sizes[LITE3_TYPE_NULL], &val)) < 0)
983 return ret;
984 val->type = (uint8_t)LITE3_TYPE_NULL;
985 return ret;
986}
987
994static inline int lite3_ctx_arr_set_bool(
995 lite3_ctx *ctx,
996 size_t ofs,
997 uint32_t index,
998 bool value)
999{
1000 lite3_val *val;
1001 int ret;
1002 if ((ret = _lite3_ctx_set_by_index(ctx, ofs, index, lite3_type_sizes[LITE3_TYPE_BOOL], &val)) < 0)
1003 return ret;
1004 val->type = (uint8_t)LITE3_TYPE_BOOL;
1005 memcpy(val->val, &value, lite3_type_sizes[LITE3_TYPE_BOOL]);
1006 return ret;
1007}
1008
1015static inline int lite3_ctx_arr_set_i64(
1016 lite3_ctx *ctx,
1017 size_t ofs,
1018 uint32_t index,
1019 int64_t value)
1020{
1021 lite3_val *val;
1022 int ret;
1023 if ((ret = _lite3_ctx_set_by_index(ctx, ofs, index, lite3_type_sizes[LITE3_TYPE_I64], &val)) < 0)
1024 return ret;
1025 val->type = (uint8_t)LITE3_TYPE_I64;
1026 memcpy(val->val, &value, lite3_type_sizes[LITE3_TYPE_I64]);
1027 return ret;
1028}
1029
1036static inline int lite3_ctx_arr_set_f64(
1037 lite3_ctx *ctx,
1038 size_t ofs,
1039 uint32_t index,
1040 double value)
1041{
1042 lite3_val *val;
1043 int ret;
1044 if ((ret = _lite3_ctx_set_by_index(ctx, ofs, index, lite3_type_sizes[LITE3_TYPE_F64], &val)) < 0)
1045 return ret;
1046 val->type = (uint8_t)LITE3_TYPE_F64;
1047 memcpy(val->val, &value, lite3_type_sizes[LITE3_TYPE_F64]);
1048 return ret;
1049}
1050
1057static inline int lite3_ctx_arr_set_bytes(
1058 lite3_ctx *ctx,
1059 size_t ofs,
1060 uint32_t index,
1061 const unsigned char *__restrict bytes,
1062 size_t bytes_len)
1063{
1064 lite3_val *val;
1065 int ret;
1066 if ((ret = _lite3_ctx_set_by_index(ctx, ofs, index, lite3_type_sizes[LITE3_TYPE_BYTES] + bytes_len, &val)) < 0)
1067 return ret;
1068 val->type = (uint8_t)LITE3_TYPE_BYTES;
1069 memcpy(val->val, &bytes_len, lite3_type_sizes[LITE3_TYPE_BYTES]);
1070 memcpy(val->val + lite3_type_sizes[LITE3_TYPE_BYTES], bytes, bytes_len);
1071 return ret;
1072}
1073
1084static inline int lite3_ctx_arr_set_str(
1085 lite3_ctx *ctx,
1086 size_t ofs,
1087 uint32_t index,
1088 const char *__restrict str)
1089{
1090 lite3_val *val;
1091 size_t str_size = strlen(str) + 1;
1092 int ret;
1093 if ((ret = _lite3_ctx_set_by_index(ctx, ofs, index, lite3_type_sizes[LITE3_TYPE_STRING] + str_size, &val)) < 0)
1094 return ret;
1095 val->type = (uint8_t)LITE3_TYPE_STRING;
1096 memcpy(val->val, &str_size, lite3_type_sizes[LITE3_TYPE_STRING]);
1097 memcpy(val->val + lite3_type_sizes[LITE3_TYPE_STRING], str, str_size);
1098 return ret;
1099}
1100
1110static inline int lite3_ctx_arr_set_str_n(
1111 lite3_ctx *ctx,
1112 size_t ofs,
1113 uint32_t index,
1114 const char *__restrict str,
1115 size_t str_len)
1116{
1117 lite3_val *val;
1118 size_t str_size = str_len + 1;
1119 int ret;
1120 if ((ret = _lite3_ctx_set_by_index(ctx, ofs, index, lite3_type_sizes[LITE3_TYPE_STRING] + str_size, &val)) < 0)
1121 return ret;
1122 val->type = (uint8_t)LITE3_TYPE_STRING;
1123 memcpy(val->val, &str_size, lite3_type_sizes[LITE3_TYPE_STRING]);
1124 memcpy(val->val + lite3_type_sizes[LITE3_TYPE_STRING], str, str_len);
1125 *(val->val + lite3_type_sizes[LITE3_TYPE_STRING] + str_len) = 0x00; // Insert NULL-terminator
1126 return ret;
1127}
1128
1135static inline int lite3_ctx_arr_set_obj(
1136 lite3_ctx *ctx,
1137 size_t ofs,
1138 uint32_t index,
1139 size_t *__restrict out_ofs)
1140{
1141 int ret;
1142 if ((ret = _lite3_verify_arr_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz)) < 0)
1143 return ret;
1144 errno = 0;
1145 while ((ret = lite3_arr_set_obj_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, index, out_ofs)) < 0) {
1146 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
1147 continue;
1148 } else {
1149 return ret;
1150 }
1151 }
1152 return ret;
1153}
1154
1161static inline int lite3_ctx_arr_set_arr(
1162 lite3_ctx *ctx,
1163 size_t ofs,
1164 uint32_t index,
1165 size_t *__restrict out_ofs)
1166{
1167 int ret;
1168 if ((ret = _lite3_verify_arr_set(ctx->buf, &ctx->buflen, ofs, ctx->bufsz)) < 0)
1169 return ret;
1170 errno = 0;
1171 while ((ret = lite3_arr_set_arr_impl(ctx->buf, &ctx->buflen, ofs, ctx->bufsz, index, out_ofs)) < 0) {
1172 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
1173 continue;
1174 } else {
1175 return ret;
1176 }
1177 }
1178 return ret;
1179}
1181
1182
1200#ifdef LITE3_DEBUG
1201static inline void lite3_ctx_print(lite3_ctx *ctx) { lite3_print(ctx->buf, ctx->buflen); }
1202#else
1203static inline void lite3_ctx_print(lite3_ctx *ctx) { (void)ctx; }
1204#endif
1205
1215{
1216 if (_lite3_verify_get(ctx->buf, ctx->buflen, 0) < 0)
1217 return LITE3_TYPE_INVALID;
1218 return (enum lite3_type)(*(ctx->buf));
1219}
1220
1231#define lite3_ctx_get_type(ctx, ofs, key) ({ \
1232 const char *__lite3_key__ = (key); \
1233 _lite3_ctx_get_type_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1234})
1235#ifndef DOXYGEN_IGNORE
1236static inline enum lite3_type _lite3_ctx_get_type_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data)
1237{
1238 return _lite3_get_type_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1239}
1240#endif // DOXYGEN_IGNORE
1241
1252static inline enum lite3_type lite3_ctx_arr_get_type(lite3_ctx *ctx, size_t ofs, uint32_t index)
1253{
1254 return lite3_arr_get_type(ctx->buf, ctx->buflen, ofs, index);
1255}
1256
1271#define lite3_ctx_get_type_size(ctx, ofs, key, out) ({ \
1272 const char *__lite3_key__ = (key); \
1273 _lite3_ctx_get_type_size_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1274})
1275#ifndef DOXYGEN_IGNORE
1276static 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)
1277{
1278 return _lite3_get_type_size_impl(ctx->buf, ctx->buflen, ofs, key, key_data, out);
1279}
1280#endif // DOXYGEN_IGNORE
1281
1292#define lite3_ctx_exists(ctx, ofs, key) ({ \
1293 const char *__lite3_key__ = (key); \
1294 _lite3_ctx_exists_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1295})
1296#ifndef DOXYGEN_IGNORE
1297static inline bool _lite3_ctx_exists_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data)
1298{
1299 return _lite3_exists_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1300}
1301#endif // DOXYGEN_IGNORE
1302
1311static inline int lite3_ctx_count(
1312 lite3_ctx *ctx,
1313 size_t ofs,
1314 uint32_t *out)
1315{
1316 return lite3_count(ctx->buf, ctx->buflen, ofs, out);
1317}
1318
1329#define lite3_ctx_is_null(ctx, ofs, key) ({ \
1330 const char *__lite3_key__ = (key); \
1331 _lite3_ctx_is_null_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1332})
1333#ifndef DOXYGEN_IGNORE
1334static inline bool _lite3_ctx_is_null_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data)
1335{
1336 return _lite3_is_null_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1337}
1338#endif // DOXYGEN_IGNORE
1339
1350#define lite3_ctx_is_bool(ctx, ofs, key) ({ \
1351 const char *__lite3_key__ = (key); \
1352 _lite3_ctx_is_bool_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1353})
1354#ifndef DOXYGEN_IGNORE
1355static inline bool _lite3_ctx_is_bool_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data)
1356{
1357 return _lite3_is_bool_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1358}
1359#endif // DOXYGEN_IGNORE
1360
1371#define lite3_ctx_is_i64(ctx, ofs, key) ({ \
1372 const char *__lite3_key__ = (key); \
1373 _lite3_ctx_is_i64_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1374})
1375#ifndef DOXYGEN_IGNORE
1376static inline bool _lite3_ctx_is_i64_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data)
1377{
1378 return _lite3_is_i64_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1379}
1380#endif // DOXYGEN_IGNORE
1381
1392#define lite3_ctx_is_f64(ctx, ofs, key) ({ \
1393 const char *__lite3_key__ = (key); \
1394 _lite3_ctx_is_f64_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1395})
1396#ifndef DOXYGEN_IGNORE
1397static inline bool _lite3_ctx_is_f64_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data)
1398{
1399 return _lite3_is_f64_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1400}
1401#endif // DOXYGEN_IGNORE
1402
1413#define lite3_ctx_is_bytes(ctx, ofs, key) ({ \
1414 const char *__lite3_key__ = (key); \
1415 _lite3_ctx_is_bytes_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1416})
1417#ifndef DOXYGEN_IGNORE
1418static inline bool _lite3_ctx_is_bytes_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data)
1419{
1420 return _lite3_is_bytes_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1421}
1422#endif // DOXYGEN_IGNORE
1423
1434#define lite3_ctx_is_str(ctx, ofs, key) ({ \
1435 const char *__lite3_key__ = (key); \
1436 _lite3_ctx_is_str_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1437})
1438#ifndef DOXYGEN_IGNORE
1439static inline bool _lite3_ctx_is_str_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data)
1440{
1441 return _lite3_is_str_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1442}
1443#endif // DOXYGEN_IGNORE
1444
1455#define lite3_ctx_is_obj(ctx, ofs, key) ({ \
1456 const char *__lite3_key__ = (key); \
1457 _lite3_ctx_is_obj_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1458})
1459#ifndef DOXYGEN_IGNORE
1460static inline bool _lite3_ctx_is_obj_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data)
1461{
1462
1463 return _lite3_is_obj_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1464}
1465#endif // DOXYGEN_IGNORE
1466
1477#define lite3_ctx_is_arr(ctx, ofs, key) ({ \
1478 const char *__lite3_key__ = (key); \
1479 _lite3_ctx_is_arr_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1480})
1481#ifndef DOXYGEN_IGNORE
1482static inline bool _lite3_ctx_is_arr_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data)
1483{
1484 return _lite3_is_arr_impl(ctx->buf, ctx->buflen, ofs, key, key_data);
1485}
1486#endif // DOXYGEN_IGNORE
1488
1489
1490
1522#define lite3_ctx_get(ctx, ofs, key, out) ({ \
1523 lite3_ctx *__lite3_ctx__ = (ctx); \
1524 size_t __lite3_ofs__ = (ofs); \
1525 int __lite3_ret__; \
1526 if ((__lite3_ret__ = _lite3_verify_get(__lite3_ctx__->buf, __lite3_ctx__->buflen, __lite3_ofs__)) < 0) \
1527 return __lite3_ret__; \
1528 const char *__lite3_key__ = (key); \
1529 lite3_get_impl(__lite3_ctx__->buf, __lite3_ctx__->buflen, __lite3_ofs__, __lite3_key__, LITE3_KEY_DATA(key), out); \
1530})
1531
1543#define lite3_ctx_get_bool(ctx, ofs, key, out) ({ \
1544 const char *__lite3_key__ = (key); \
1545 _lite3_ctx_get_bool_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1546})
1547#ifndef DOXYGEN_IGNORE
1548static inline int _lite3_ctx_get_bool_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data, bool *out)
1549{
1550 return _lite3_get_bool_impl(ctx->buf, ctx->buflen, ofs, key, key_data, out);
1551}
1552#endif // DOXYGEN_IGNORE
1553
1565#define lite3_ctx_get_i64(ctx, ofs, key, out) ({ \
1566 const char *__lite3_key__ = (key); \
1567 _lite3_ctx_get_i64_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1568})
1569#ifndef DOXYGEN_IGNORE
1570static 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)
1571{
1572 return _lite3_get_i64_impl(ctx->buf, ctx->buflen, ofs, key, key_data, out);
1573}
1574#endif // DOXYGEN_IGNORE
1575
1587#define lite3_ctx_get_f64(ctx, ofs, key, out) ({ \
1588 const char *__lite3_key__ = (key); \
1589 _lite3_ctx_get_f64_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1590})
1591#ifndef DOXYGEN_IGNORE
1592static inline int _lite3_ctx_get_f64_impl(lite3_ctx *ctx, size_t ofs, const char *__restrict key, lite3_key_data key_data, double *out)
1593{
1594 return _lite3_get_f64_impl(ctx->buf, ctx->buflen, ofs, key, key_data, out);
1595}
1596#endif // DOXYGEN_IGNORE
1597
1609#define lite3_ctx_get_bytes(ctx, ofs, key, out) ({ \
1610 const char *__lite3_key__ = (key); \
1611 _lite3_ctx_get_bytes_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1612})
1613#ifndef DOXYGEN_IGNORE
1614static 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)
1615{
1616 return _lite3_get_bytes_impl(ctx->buf, ctx->buflen, ofs, key, key_data, out);
1617}
1618#endif // DOXYGEN_IGNORE
1619
1631#define lite3_ctx_get_str(ctx, ofs, key, out) ({ \
1632 const char *__lite3_key__ = (key); \
1633 _lite3_ctx_get_str_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1634})
1635#ifndef DOXYGEN_IGNORE
1636static 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)
1637{
1638 return _lite3_get_str_impl(ctx->buf, ctx->buflen, ofs, key, key_data, out);
1639}
1640#endif // DOXYGEN_IGNORE
1641
1653#define lite3_ctx_get_obj(ctx, ofs, key, out) ({ \
1654 const char *__lite3_key__ = (key); \
1655 _lite3_ctx_get_obj_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1656})
1657#ifndef DOXYGEN_IGNORE
1658static 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)
1659{
1660 return _lite3_get_obj_impl(ctx->buf, ctx->buflen, ofs, key, key_data, out_ofs);
1661}
1662#endif // DOXYGEN_IGNORE
1663
1675#define lite3_ctx_get_arr(ctx, ofs, key, out) ({ \
1676 const char *__lite3_key__ = (key); \
1677 _lite3_ctx_get_arr_impl(ctx, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1678})
1679#ifndef DOXYGEN_IGNORE
1680static 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)
1681{
1682 return _lite3_get_arr_impl(ctx->buf, ctx->buflen, ofs, key, key_data, out_ofs);
1683}
1684#endif // DOXYGEN_IGNORE
1686
1687
1688
1711static inline int lite3_ctx_arr_get_bool(
1712 lite3_ctx *ctx,
1713 size_t ofs,
1714 uint32_t index,
1715 bool *out)
1716{
1717 return lite3_arr_get_bool(ctx->buf, ctx->buflen, ofs, index, out);
1718}
1719
1726static inline int lite3_ctx_arr_get_i64(
1727 lite3_ctx *ctx,
1728 size_t ofs,
1729 uint32_t index,
1730 int64_t *out)
1731{
1732 return lite3_arr_get_i64(ctx->buf, ctx->buflen, ofs, index, out);
1733}
1734
1741static inline int lite3_ctx_arr_get_f64(
1742 lite3_ctx *ctx,
1743 size_t ofs,
1744 uint32_t index,
1745 double *out)
1746{
1747 return lite3_arr_get_f64(ctx->buf, ctx->buflen, ofs, index, out);
1748}
1749
1756static inline int lite3_ctx_arr_get_bytes(
1757 lite3_ctx *ctx,
1758 size_t ofs,
1759 uint32_t index,
1760 lite3_bytes *out)
1761{
1762 return lite3_arr_get_bytes(ctx->buf, ctx->buflen, ofs, index, out);
1763}
1764
1771static inline int lite3_ctx_arr_get_str(
1772 lite3_ctx *ctx,
1773 size_t ofs,
1774 uint32_t index,
1775 lite3_str *out)
1776{
1777 return lite3_arr_get_str(ctx->buf, ctx->buflen, ofs, index, out);
1778}
1779
1786static inline int lite3_ctx_arr_get_obj(
1787 lite3_ctx *ctx,
1788 size_t ofs,
1789 uint32_t index,
1790 size_t *__restrict out_ofs)
1791{
1792 return lite3_arr_get_obj(ctx->buf, ctx->buflen, ofs, index, out_ofs);
1793}
1794
1801static inline int lite3_ctx_arr_get_arr(
1802 lite3_ctx *ctx,
1803 size_t ofs,
1804 uint32_t index,
1805 size_t *__restrict out_ofs)
1806{
1807 return lite3_arr_get_arr(ctx->buf, ctx->buflen, ofs, index, out_ofs);
1808}
1810
1811
1812
1825#ifdef DOXYGEN_ONLY
1827#define LITE3_ITER_ITEM 1
1829#define LITE3_ITER_DONE 0
1830#endif
1831
1838static inline int lite3_ctx_iter_create(
1839 lite3_ctx *ctx,
1840 size_t ofs,
1841 lite3_iter *out)
1842{
1843 return lite3_iter_create(ctx->buf, ctx->buflen, ofs, out);
1844}
1845
1862static inline int lite3_ctx_iter_next(
1863 lite3_ctx *ctx,
1864 lite3_iter *iter,
1865 lite3_str *out_key,
1866 size_t *out_val_ofs)
1867{
1868 return lite3_iter_next(ctx->buf, ctx->buflen, iter, out_key, out_val_ofs);
1869}
1871
1872
1903#if defined(DOXYGEN_ONLY) && !defined(LITE3_JSON)
1904#define LITE3_JSON
1905#endif // DOXYGEN_ONLY
1906
1907#ifdef LITE3_JSON
1917static inline int lite3_ctx_json_dec(
1918 lite3_ctx *ctx,
1919 const char *__restrict json_str,
1920 size_t json_len)
1921{
1922 int ret;
1923 errno = 0;
1924 while ((ret = lite3_json_dec(ctx->buf, &ctx->buflen, ctx->bufsz, json_str, json_len)) < 0) {
1925 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
1926 continue;
1927 } else {
1928 return ret;
1929 }
1930 }
1931 return ret;
1932}
1933
1943static inline int lite3_ctx_json_dec_file(
1944 lite3_ctx *ctx,
1945 const char *__restrict path)
1946{
1947 int ret;
1948 errno = 0;
1949 while ((ret = lite3_json_dec_file(ctx->buf, &ctx->buflen, ctx->bufsz, path)) < 0) {
1950 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
1951 continue;
1952 } else {
1953 return ret;
1954 }
1955 }
1956 return ret;
1957}
1958
1968static inline int lite3_ctx_json_dec_fp(
1969 lite3_ctx *ctx,
1970 FILE *fp)
1971{
1972 int ret;
1973 errno = 0;
1974 while ((ret = lite3_json_dec_fp(ctx->buf, &ctx->buflen, ctx->bufsz, fp)) < 0) {
1975 if (errno == ENOBUFS && (lite3_ctx_grow_impl(ctx) == 0)) {
1976 continue;
1977 } else {
1978 return ret;
1979 }
1980 }
1981 return ret;
1982}
1983
1997static inline int lite3_ctx_json_print(
1998 lite3_ctx *ctx,
1999 size_t ofs)
2000{
2001 return lite3_json_print(ctx->buf, ctx->buflen, ofs);
2002}
2003
2017static inline char *lite3_ctx_json_enc(
2018 lite3_ctx *ctx,
2019 size_t ofs,
2020 size_t *__restrict out_len)
2021{
2022 return lite3_json_enc(ctx->buf, ctx->buflen, ofs, out_len);
2023}
2024
2040static inline char *lite3_ctx_json_enc_pretty(
2041 lite3_ctx *ctx,
2042 size_t ofs,
2043 size_t *__restrict out_len)
2044{
2045 return lite3_json_enc_pretty(ctx->buf, ctx->buflen, ofs, out_len);
2046}
2047
2058static inline int64_t lite3_ctx_json_enc_buf(
2059 lite3_ctx *ctx,
2060 size_t ofs,
2061 char *__restrict json_buf,
2062 size_t json_bufsz
2063)
2064{
2065 return lite3_json_enc_buf(ctx->buf, ctx->buflen, ofs, json_buf, json_bufsz);
2066}
2067
2080static inline int64_t lite3_ctx_json_enc_pretty_buf(
2081 lite3_ctx *ctx,
2082 size_t ofs,
2083 char *__restrict json_buf,
2084 size_t json_bufsz)
2085{
2086 return lite3_json_enc_pretty_buf(ctx->buf, ctx->buflen, ofs, json_buf, json_bufsz);
2087}
2088#else
2089static inline int lite3_ctx_json_dec(lite3_ctx *ctx, const char *__restrict json_str, size_t json_len)
2090{
2091 (void)ctx; (void)json_str; (void)json_len;
2092 return -1;
2093}
2094
2095static inline int lite3_ctx_json_dec_file(lite3_ctx *ctx, const char *__restrict path)
2096{
2097 (void)ctx; (void)path;
2098 return -1;
2099}
2100
2101static inline int lite3_ctx_json_dec_fp(lite3_ctx *ctx, FILE *fp)
2102{
2103 (void)ctx; (void)fp;
2104 return -1;
2105}
2106
2107static inline int lite3_ctx_json_print(lite3_ctx *ctx, size_t ofs)
2108{
2109 (void)ctx; (void)ofs;
2110 return 0;
2111}
2112
2113static inline char *lite3_ctx_json_enc(lite3_ctx *ctx, size_t ofs, size_t *out_len)
2114{
2115 (void)ctx; (void)ofs; (void)out_len;
2116 return NULL;
2117}
2118
2119static inline char *lite3_ctx_json_enc_pretty(lite3_ctx *ctx, size_t ofs, size_t *out_len)
2120{
2121 (void)ctx; (void)ofs; (void)out_len;
2122 return NULL;
2123}
2124
2125static inline int64_t lite3_ctx_json_enc_buf(lite3_ctx *ctx, size_t ofs, char *__restrict json_buf, size_t json_bufsz)
2126{
2127 (void)ctx; (void)ofs; (void)json_buf; (void)json_bufsz;
2128 return -1;
2129}
2130
2131static inline int64_t lite3_ctx_json_enc_pretty_buf(lite3_ctx *ctx, size_t ofs, char *__restrict json_buf, size_t json_bufsz)
2132{
2133 (void)ctx; (void)ofs; (void)json_buf; (void)json_bufsz;
2134 return -1;
2135}
2136#endif
2138
2139#ifdef __cplusplus
2140}
2141#endif
2142
2143#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:2589
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:2474
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:2448
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:2500
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:2557
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:2526
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:2615
#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:369
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:2682
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:1882
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:1799
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:2664
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