51#if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN || \
52 defined(__BIG_ENDIAN__) || \
53 defined(__ARMEB__) || \
54 defined(__THUMBEB__) || \
55 defined(__AARCH64EB__) || \
56 defined(__or1k__) || defined(__OR1K__)
57 #error "Byte order must be little-endian"
60static_assert(
sizeof(double) == 8,
"Double must be 8 bytes");
63#define LITE3_LIKELY(expr) __builtin_expect(!!(expr), 1)
64#define LITE3_UNLIKELY(expr) __builtin_expect(!!(expr), 0)
137#ifndef DOXYGEN_IGNORE
138#define LITE3_ZERO_MEM_8 0x00
139#define LITE3_ZERO_MEM_32 0x00000000
152#define LITE3_ZERO_MEM_DELETED
154#if defined(DOXYGEN_ONLY) && !defined(LITE3_ZERO_MEM_DELETED)
155#define LITE3_ZERO_MEM_DELETED
169#define LITE3_ZERO_MEM_EXTRA
171#if defined(DOXYGEN_ONLY) && !defined(LITE3_ZERO_MEM_EXTRA)
172#define LITE3_ZERO_MEM_EXTRA
184#define LITE3_PREFETCHING
186#if defined(DOXYGEN_ONLY) && !defined(LITE3_PREFETCHING)
187#define LITE3_PREFETCHING
199#if defined(DOXYGEN_ONLY) && !defined(LITE3_ERROR_MESSAGES)
200#define LITE3_ERROR_MESSAGES
203#ifndef DOXYGEN_IGNORE
204#ifdef LITE3_ERROR_MESSAGES
206 #define LITE3_PRINT_ERROR(format, ...) printf(format, ##__VA_ARGS__)
208 #define LITE3_PRINT_ERROR(format, ...)
224#if defined(DOXYGEN_ONLY) && !defined(LITE3_DEBUG)
228#ifndef DOXYGEN_IGNORE
232 #ifndef LITE3_ZERO_MEM_DELETED
233 #define LITE3_ZERO_MEM_DELETED
236 #ifndef LITE3_ZERO_MEM_EXTRA
237 #define LITE3_ZERO_MEM_EXTRA
240 #undef LITE3_ZERO_MEM_8
241 #undef LITE3_ZERO_MEM_32
242 #define LITE3_ZERO_MEM_8 0x5F
243 #define LITE3_ZERO_MEM_32 0x5F5F5F5F
245 #define LITE3_PRINT_DEBUG(format, ...) printf(format, ##__VA_ARGS__)
246 void lite3_print(
const unsigned char *buf,
size_t buflen);
248 #define LITE3_PRINT_DEBUG(format, ...)
249 static inline void lite3_print(
const unsigned char *buf,
size_t buflen) { (void)buf; (void)buflen; }
259#define LITE3_BUF_SIZE_MAX UINT32_MAX
299#define LITE3_NODE_ALIGNMENT 4
305#ifndef DOXYGEN_IGNORE
306#define LITE3_NODE_ALIGNMENT_MASK ((uintptr_t)(LITE3_NODE_ALIGNMENT - 1))
321#define LITE3_NODE_SIZE 96
336#define LITE3_TREE_HEIGHT_MAX 9
350#define LITE3_NODE_SIZE_KC_OFFSET 32
355#ifndef DOXYGEN_IGNORE
356#define LITE3_NODE_SIZE_SHIFT 6
357#define LITE3_NODE_SIZE_MASK ((u32)~((1 << 6) - 1))
359#define LITE3_DJB2_HASH_SEED ((uint32_t)5381)
375#define LITE3_KEY_HASH_COMPILE_TIME
377#ifndef DOXYGEN_IGNORE
378#ifdef LITE3_KEY_HASH_COMPILE_TIME
379#define LITE3_MOD 4294967296ULL
381#define LITE3_P0 33ULL
382#define LITE3_P1 ((LITE3_P0 * LITE3_P0) % LITE3_MOD)
383#define LITE3_P2 ((LITE3_P1 * LITE3_P1) % LITE3_MOD)
384#define LITE3_P3 ((LITE3_P2 * LITE3_P2) % LITE3_MOD)
385#define LITE3_P4 ((LITE3_P3 * LITE3_P3) % LITE3_MOD)
386#define LITE3_P5 ((LITE3_P4 * LITE3_P4) % LITE3_MOD)
388#define LITE3_BIT0(k) (((k) >> 0) & 1ULL)
389#define LITE3_BIT1(k) (((k) >> 1) & 1ULL)
390#define LITE3_BIT2(k) (((k) >> 2) & 1ULL)
391#define LITE3_BIT3(k) (((k) >> 3) & 1ULL)
392#define LITE3_BIT4(k) (((k) >> 4) & 1ULL)
393#define LITE3_BIT5(k) (((k) >> 5) & 1ULL)
395#define LITE3_POW33(k) \
397 * (LITE3_BIT0(k) ? LITE3_P0 : 1ULL) \
399 * (LITE3_BIT1(k) ? LITE3_P1 : 1ULL) \
401 * (LITE3_BIT2(k) ? LITE3_P2 : 1ULL) \
403 * (LITE3_BIT3(k) ? LITE3_P3 : 1ULL) \
405 * (LITE3_BIT4(k) ? LITE3_P4 : 1ULL) \
407 * (LITE3_BIT5(k) ? LITE3_P5 : 1ULL) \
410#define LITE3_SUM_1(s, i, p) \
411 ((unsigned long long)(unsigned char)(s)[(i)] * LITE3_POW33((p)))
413#define LITE3_SUM_2(s, i, p) \
414 (LITE3_SUM_1(s, (i), (p)) + LITE3_SUM_1(s, (i) + 1, (p) - 1))
416#define LITE3_SUM_4(s, i, p) \
417 (LITE3_SUM_2(s, (i), (p)) + LITE3_SUM_2(s, (i) + 2, (p) - 2))
419#define LITE3_SUM_8(s, i, p) \
420 (LITE3_SUM_4(s, (i), (p)) + LITE3_SUM_4(s, (i) + 4, (p) - 4))
422#define LITE3_SUM_16(s, i, p) \
423 (LITE3_SUM_8(s, (i), (p)) + LITE3_SUM_8(s, (i) + 8, (p) - 8))
425#define LITE3_SUM_32(s, i, p) \
426 (LITE3_SUM_16(s, (i), (p)) + LITE3_SUM_16(s, (i) + 16, (p) - 16))
428#define LITE3_OFFSET_BIT0(len) \
429 (((len & 2ULL) ? 2ULL : 0ULL) + \
430 ((len & 4ULL) ? 4ULL : 0ULL) + \
431 ((len & 8ULL) ? 8ULL : 0ULL) + \
432 ((len & 16ULL) ? 16ULL : 0ULL) + \
433 ((len & 32ULL) ? 32ULL : 0ULL))
435#define LITE3_OFFSET_BIT1(len) \
436 (((len & 4ULL) ? 4ULL : 0ULL) + \
437 ((len & 8ULL) ? 8ULL : 0ULL) + \
438 ((len & 16ULL) ? 16ULL : 0ULL) + \
439 ((len & 32ULL) ? 32ULL : 0ULL))
441#define LITE3_OFFSET_BIT2(len) \
442 (((len & 8ULL) ? 8ULL : 0ULL) + \
443 ((len & 16ULL) ? 16ULL : 0ULL) + \
444 ((len & 32ULL) ? 32ULL : 0ULL))
446#define LITE3_OFFSET_BIT3(len) \
447 (((len & 16ULL) ? 16ULL : 0ULL) + \
448 ((len & 32ULL) ? 32ULL : 0ULL))
450#define LITE3_OFFSET_BIT4(len) \
451 (((len & 32ULL) ? 32ULL : 0ULL))
453#define LITE3_OFFSET_BIT5(len) \
456#define LITE3_POLY_HASH(s, len) \
457 ((len) & 1ULL ? LITE3_SUM_1(s, LITE3_OFFSET_BIT0((len)), (len) - 1 - LITE3_OFFSET_BIT0((len))) : 0ULL) + \
458 ((len) & 2ULL ? LITE3_SUM_2(s, LITE3_OFFSET_BIT1((len)), (len) - 1 - LITE3_OFFSET_BIT1((len))) : 0ULL) + \
459 ((len) & 4ULL ? LITE3_SUM_4(s, LITE3_OFFSET_BIT2((len)), (len) - 1 - LITE3_OFFSET_BIT2((len))) : 0ULL) + \
460 ((len) & 8ULL ? LITE3_SUM_8(s, LITE3_OFFSET_BIT3((len)), (len) - 1 - LITE3_OFFSET_BIT3((len))) : 0ULL) + \
461 ((len) & 16ULL ? LITE3_SUM_16(s, LITE3_OFFSET_BIT4((len)), (len) - 1 - LITE3_OFFSET_BIT4((len))) : 0ULL) + \
462 ((len) & 32ULL ? LITE3_SUM_32(s, LITE3_OFFSET_BIT5((len)), (len) - 1 - LITE3_OFFSET_BIT5((len))) : 0ULL)
464#define LITE3_STRLEN(s) (sizeof(s) - 1)
471static inline lite3_key_data lite3_get_key_data(
const char *key) {
472 lite3_key_data key_data;
473 const char *key_cursor = key;
474 key_data.hash = LITE3_DJB2_HASH_SEED;
476 key_data.hash = ((key_data.hash << 5) + key_data.hash) + (uint8_t)(*key_cursor++);
477 key_data.size = (uint32_t)(key_cursor - key) + 1;
481#define LITE3_KEY_DATA(s) ( \
482 __builtin_constant_p(s) ? \
483 ((LITE3_STRLEN(s) < 64) ? \
485 .hash = (uint32_t)((LITE3_POLY_HASH(s, LITE3_STRLEN(s)) + LITE3_DJB2_HASH_SEED * LITE3_POW33(LITE3_STRLEN(s))) % LITE3_MOD), \
486 .size = (sizeof(s)), \
488 : lite3_get_key_data(s)) \
489 : lite3_get_key_data(__lite3_key__) \
492#define LITE3_KEY_DATA(s) lite3_get_key_data(__lite3_key__)
536#ifndef DOXYGEN_IGNORE
537#define LITE3_VAL_SIZE sizeof(lite3_val)
539static_assert(LITE3_VAL_SIZE <=
sizeof(size_t),
"LITE3_VAL_SIZE must be <= sizeof(size_t)");
541#ifndef DOXYGEN_IGNORE
542static const size_t lite3_type_sizes[] = {
554static_assert((
sizeof(lite3_type_sizes) /
sizeof(
size_t)) ==
LITE3_TYPE_COUNT,
"lite3_type_sizes[] element count != LITE3_TYPE_COUNT");
555static_assert(4 <=
sizeof(size_t),
"lite3_type_sizes[LITE3_TYPE_BYTES] and lite3_type_sizes[LITE3_TYPE_STRING] must fit inside size_t");
578#ifndef DOXYGEN_IGNORE
579#define LITE3_BYTES_LEN_SIZE sizeof(((lite3_bytes *)0)->len)
581static_assert(LITE3_BYTES_LEN_SIZE <=
sizeof(size_t),
"lite3_val_bytes() expects LITE3_BYTES_LEN_SIZE to be <= sizeof(size_t)");
607#ifndef DOXYGEN_IGNORE
608#define LITE3_STR_LEN_SIZE sizeof(((lite3_str *)0)->len)
610static_assert(LITE3_STR_LEN_SIZE <=
sizeof(size_t),
"lite3_val_str() expects LITE3_STR_LEN_SIZE to be <= sizeof(size_t)");
637#define LITE3_BYTES(buf, val) (const unsigned char *)_lite3_ptr_suppress_nonnull_warning( \
638 (uint32_t)(val).gen == *(uint32_t *)(buf) ? (val).ptr : NULL \
666#define LITE3_STR(buf, val) (const char *)_lite3_ptr_suppress_nonnull_warning( \
667 (uint32_t)(val).gen == *(uint32_t *)(buf) ? (val).ptr : NULL \
670#ifndef DOXYGEN_IGNORE
671static inline __attribute__((always_inline))
const void *_lite3_ptr_suppress_nonnull_warning(
const void *p) {
return p; }
702 size_t *__restrict out_buflen,
720 size_t *__restrict out_buflen,
757#ifndef DOXYGEN_IGNORE
758static inline int _lite3_verify_set(
unsigned char *buf,
size_t *__restrict inout_buflen,
size_t ofs,
size_t bufsz)
762 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: bufsz > LITE3_BUF_SIZE_MAX\n");
766 if (LITE3_UNLIKELY(*inout_buflen > bufsz)) {
767 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: inout_buflen > bufsz\n");
772 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: START OFFSET OUT OF BOUNDS\n");
779static inline int _lite3_verify_obj_set(
unsigned char *buf,
size_t *__restrict inout_buflen,
size_t ofs,
size_t bufsz)
782 if ((ret = _lite3_verify_set(buf, inout_buflen, ofs, bufsz)) < 0)
785 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: EXPECTING OBJECT TYPE\n");
792static inline int _lite3_verify_arr_set(
unsigned char *buf,
size_t *__restrict inout_buflen,
size_t ofs,
size_t bufsz)
795 if ((ret = _lite3_verify_set(buf, inout_buflen, ofs, bufsz)) < 0)
798 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: EXPECTING ARRAY TYPE\n");
806int lite3_set_impl(
unsigned char *buf,
size_t *__restrict inout_buflen,
size_t ofs,
size_t bufsz,
const char *__restrict key, lite3_key_data key_data,
size_t val_len,
lite3_val **out);
821#define lite3_set_null(buf, inout_buflen, ofs, bufsz, key) ({ \
822 const char *__lite3_key__ = (key); \
823 _lite3_set_null_impl(buf, inout_buflen, ofs, bufsz, __lite3_key__, LITE3_KEY_DATA(key)); \
825#ifndef DOXYGEN_IGNORE
826static inline int _lite3_set_null_impl(
unsigned char *buf,
size_t *__restrict inout_buflen,
size_t ofs,
size_t bufsz,
const char *__restrict key, lite3_key_data key_data)
829 if ((ret = _lite3_verify_obj_set(buf, inout_buflen, ofs, bufsz)) < 0)
832 if ((ret = lite3_set_impl(buf, inout_buflen, ofs, bufsz, key, key_data, lite3_type_sizes[
LITE3_TYPE_NULL], &val)) < 0)
852#define lite3_set_bool(buf, inout_buflen, ofs, bufsz, key, value) ({ \
853 const char *__lite3_key__ = (key); \
854 _lite3_set_bool_impl(buf, inout_buflen, ofs, bufsz, __lite3_key__, LITE3_KEY_DATA(key), value); \
856#ifndef DOXYGEN_IGNORE
857static inline int _lite3_set_bool_impl(
unsigned char *buf,
size_t *__restrict inout_buflen,
size_t ofs,
size_t bufsz,
const char *__restrict key, lite3_key_data key_data,
bool value)
860 if ((ret = _lite3_verify_obj_set(buf, inout_buflen, ofs, bufsz)) < 0)
863 if ((ret = lite3_set_impl(buf, inout_buflen, ofs, bufsz, key, key_data, lite3_type_sizes[
LITE3_TYPE_BOOL], &val)) < 0)
884#define lite3_set_i64(buf, inout_buflen, ofs, bufsz, key, value) ({ \
885 const char *__lite3_key__ = (key); \
886 _lite3_set_i64_impl(buf, inout_buflen, ofs, bufsz, __lite3_key__, LITE3_KEY_DATA(key), value); \
888#ifndef DOXYGEN_IGNORE
889static inline int _lite3_set_i64_impl(
unsigned char *buf,
size_t *__restrict inout_buflen,
size_t ofs,
size_t bufsz,
const char *__restrict key, lite3_key_data key_data, int64_t value)
892 if ((ret = _lite3_verify_obj_set(buf, inout_buflen, ofs, bufsz)) < 0)
895 if ((ret = lite3_set_impl(buf, inout_buflen, ofs, bufsz, key, key_data, lite3_type_sizes[
LITE3_TYPE_I64], &val)) < 0)
916#define lite3_set_f64(buf, inout_buflen, ofs, bufsz, key, value) ({ \
917 const char *__lite3_key__ = (key); \
918 _lite3_set_f64_impl(buf, inout_buflen, ofs, bufsz, __lite3_key__, LITE3_KEY_DATA(key), value); \
920#ifndef DOXYGEN_IGNORE
921static inline int _lite3_set_f64_impl(
unsigned char *buf,
size_t *__restrict inout_buflen,
size_t ofs,
size_t bufsz,
const char *__restrict key, lite3_key_data key_data,
double value)
924 if ((ret = _lite3_verify_obj_set(buf, inout_buflen, ofs, bufsz)) < 0)
927 if ((ret = lite3_set_impl(buf, inout_buflen, ofs, bufsz, key, key_data, lite3_type_sizes[
LITE3_TYPE_F64], &val)) < 0)
949#define lite3_set_bytes(buf, inout_buflen, ofs, bufsz, key, bytes, bytes_len) ({ \
950 const char *__lite3_key__ = (key); \
951 _lite3_set_bytes_impl(buf, inout_buflen, ofs, bufsz, __lite3_key__, LITE3_KEY_DATA(key), bytes, bytes_len); \
953#ifndef DOXYGEN_IGNORE
954static inline int _lite3_set_bytes_impl(
unsigned char *buf,
size_t *__restrict inout_buflen,
size_t ofs,
size_t bufsz,
const char *__restrict key, lite3_key_data key_data,
const unsigned char *__restrict bytes,
size_t bytes_len)
957 if ((ret = _lite3_verify_obj_set(buf, inout_buflen, ofs, bufsz)) < 0)
960 if ((ret = lite3_set_impl(buf, inout_buflen, ofs, bufsz, key, key_data, lite3_type_sizes[
LITE3_TYPE_BYTES] + bytes_len, &val)) < 0)
986#define lite3_set_str(buf, inout_buflen, ofs, bufsz, key, str) ({ \
987 const char *__lite3_key__ = (key); \
988 _lite3_set_str_impl(buf, inout_buflen, ofs, bufsz, __lite3_key__, LITE3_KEY_DATA(key), str); \
990#ifndef DOXYGEN_IGNORE
991static inline int _lite3_set_str_impl(
unsigned char *buf,
size_t *__restrict inout_buflen,
size_t ofs,
size_t bufsz,
const char *__restrict key, lite3_key_data key_data,
const char *__restrict str)
994 if ((ret = _lite3_verify_obj_set(buf, inout_buflen, ofs, bufsz)) < 0)
997 size_t str_size = strlen(str) + 1;
998 if ((ret = lite3_set_impl(buf, inout_buflen, ofs, bufsz, key, key_data, lite3_type_sizes[
LITE3_TYPE_STRING] + str_size, &val)) < 0)
1024#define lite3_set_str_n(buf, inout_buflen, ofs, bufsz, key, str, str_len) ({ \
1025 const char *__lite3_key__ = (key); \
1026 _lite3_set_str_n_impl(buf, inout_buflen, ofs, bufsz, __lite3_key__, LITE3_KEY_DATA(key), str, str_len); \
1028#ifndef DOXYGEN_IGNORE
1029static inline int _lite3_set_str_n_impl(
unsigned char *buf,
size_t *__restrict inout_buflen,
size_t ofs,
size_t bufsz,
const char *__restrict key, lite3_key_data key_data,
const char *__restrict str,
size_t str_len)
1032 if ((ret = _lite3_verify_obj_set(buf, inout_buflen, ofs, bufsz)) < 0)
1035 size_t str_size = str_len + 1;
1036 if ((ret = lite3_set_impl(buf, inout_buflen, ofs, bufsz, key, key_data, lite3_type_sizes[
LITE3_TYPE_STRING] + str_size, &val)) < 0)
1059#define lite3_set_obj(buf, inout_buflen, ofs, bufsz, key, out_ofs) ({ \
1060 unsigned char *__lite3_buf__ = (buf); \
1061 size_t *__lite3_inout_buflen__ = (inout_buflen); \
1062 size_t __lite3_ofs__ = (ofs); \
1063 size_t __lite3_bufsz__ = (bufsz); \
1064 int __lite3_ret__; \
1065 if ((__lite3_ret__ = _lite3_verify_obj_set( \
1067 __lite3_inout_buflen__, \
1069 __lite3_bufsz__)) < 0) \
1070 return __lite3_ret__; \
1072 const char *__lite3_key__ = (key); \
1073 lite3_set_obj_impl( \
1075 __lite3_inout_buflen__, \
1079 LITE3_KEY_DATA(key), \
1082#ifndef DOXYGEN_IGNORE
1084int lite3_set_obj_impl(
unsigned char *buf,
size_t *__restrict inout_buflen,
size_t ofs,
size_t bufsz,
const char *__restrict key, lite3_key_data key_data,
size_t *__restrict out_ofs);
1100#define lite3_set_arr(buf, inout_buflen, ofs, bufsz, key, out_ofs) ({ \
1101 unsigned char *__lite3_buf__ = (buf); \
1102 size_t *__lite3_inout_buflen__ = (inout_buflen); \
1103 size_t __lite3_ofs__ = (ofs); \
1104 size_t __lite3_bufsz__ = (bufsz); \
1105 int __lite3_ret__; \
1106 if ((__lite3_ret__ = _lite3_verify_obj_set( \
1108 __lite3_inout_buflen__, \
1110 __lite3_bufsz__)) < 0) \
1111 return __lite3_ret__; \
1113 const char *__lite3_key__ = (key); \
1114 lite3_set_arr_impl( \
1116 __lite3_inout_buflen__, \
1120 LITE3_KEY_DATA(key), \
1123#ifndef DOXYGEN_IGNORE
1125int lite3_set_arr_impl(
unsigned char *buf,
size_t *__restrict inout_buflen,
size_t ofs,
size_t bufsz,
const char *__restrict key, lite3_key_data key_data,
size_t *__restrict out_ofs);
1155#ifndef DOXYGEN_IGNORE
1156static inline int _lite3_set_by_index(
unsigned char *buf,
size_t *__restrict inout_buflen,
size_t ofs,
size_t bufsz, uint32_t index,
size_t val_len,
lite3_val **out)
1159 if ((ret = _lite3_verify_arr_set(buf, inout_buflen, ofs, bufsz)) < 0)
1162 if (LITE3_UNLIKELY(index > size)) {
1163 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: ARRAY INDEX %u OUT OF BOUNDS (size == %u)\n", index, size);
1167 lite3_key_data key_data = {
1171 return lite3_set_impl(buf, inout_buflen, ofs, bufsz, NULL, key_data, val_len, out);
1174static inline int _lite3_set_by_append(
unsigned char *buf,
size_t *__restrict inout_buflen,
size_t ofs,
size_t bufsz,
size_t val_len,
lite3_val **out)
1177 if ((ret = _lite3_verify_arr_set(buf, inout_buflen, ofs, bufsz)) < 0)
1180 lite3_key_data key_data = {
1184 return lite3_set_impl(buf, inout_buflen, ofs, bufsz, NULL, key_data, val_len, out);
1196 size_t *__restrict inout_buflen,
1202 if ((ret = _lite3_set_by_append(buf, inout_buflen, ofs, bufsz, lite3_type_sizes[
LITE3_TYPE_NULL], &val)) < 0)
1216 size_t *__restrict inout_buflen,
1223 if ((ret = _lite3_set_by_append(buf, inout_buflen, ofs, bufsz, lite3_type_sizes[
LITE3_TYPE_BOOL], &val)) < 0)
1238 size_t *__restrict inout_buflen,
1245 if ((ret = _lite3_set_by_append(buf, inout_buflen, ofs, bufsz, lite3_type_sizes[
LITE3_TYPE_I64], &val)) < 0)
1260 size_t *__restrict inout_buflen,
1267 if ((ret = _lite3_set_by_append(buf, inout_buflen, ofs, bufsz, lite3_type_sizes[
LITE3_TYPE_F64], &val)) < 0)
1282 size_t *__restrict inout_buflen,
1285 const unsigned char *__restrict bytes,
1290 if ((ret = _lite3_set_by_append(buf, inout_buflen, ofs, bufsz, lite3_type_sizes[
LITE3_TYPE_BYTES] + bytes_len, &val)) < 0)
1310 size_t *__restrict inout_buflen,
1313 const char *__restrict str)
1316 size_t str_size = strlen(str) + 1;
1318 if ((ret = _lite3_set_by_append(buf, inout_buflen, ofs, bufsz, lite3_type_sizes[
LITE3_TYPE_STRING] + str_size, &val)) < 0)
1337 size_t *__restrict inout_buflen,
1340 const char *__restrict str,
1344 size_t str_size = str_len + 1;
1346 if ((ret = _lite3_set_by_append(buf, inout_buflen, ofs, bufsz, lite3_type_sizes[
LITE3_TYPE_STRING] + str_size, &val)) < 0)
1361#ifndef DOXYGEN_IGNORE
1363int lite3_arr_append_obj_impl(
unsigned char *buf,
size_t *__restrict inout_buflen,
size_t ofs,
size_t bufsz,
size_t *__restrict out_ofs);
1368 size_t *__restrict inout_buflen,
1371 size_t *__restrict out_ofs)
1374 if ((ret = _lite3_verify_arr_set(buf, inout_buflen, ofs, bufsz)) < 0)
1376 return lite3_arr_append_obj_impl(buf, inout_buflen, ofs, bufsz, out_ofs);
1386#ifndef DOXYGEN_IGNORE
1388int lite3_arr_append_arr_impl(
unsigned char *buf,
size_t *__restrict inout_buflen,
size_t ofs,
size_t bufsz,
size_t *__restrict out_ofs);
1393 size_t *__restrict inout_buflen,
1396 size_t *__restrict out_ofs)
1399 if ((ret = _lite3_verify_arr_set(buf, inout_buflen, ofs, bufsz)) < 0)
1401 return lite3_arr_append_arr_impl(buf, inout_buflen, ofs, bufsz, out_ofs);
1446 size_t *__restrict inout_buflen,
1453 if ((ret = _lite3_set_by_index(buf, inout_buflen, ofs, bufsz, index, lite3_type_sizes[
LITE3_TYPE_NULL], &val)) < 0)
1467 size_t *__restrict inout_buflen,
1475 if ((ret = _lite3_set_by_index(buf, inout_buflen, ofs, bufsz, index, lite3_type_sizes[
LITE3_TYPE_BOOL], &val)) < 0)
1490 size_t *__restrict inout_buflen,
1498 if ((ret = _lite3_set_by_index(buf, inout_buflen, ofs, bufsz, index, lite3_type_sizes[
LITE3_TYPE_I64], &val)) < 0)
1513 size_t *__restrict inout_buflen,
1521 if ((ret = _lite3_set_by_index(buf, inout_buflen, ofs, bufsz, index, lite3_type_sizes[
LITE3_TYPE_F64], &val)) < 0)
1536 size_t *__restrict inout_buflen,
1540 const unsigned char *__restrict bytes,
1545 if ((ret = _lite3_set_by_index(buf, inout_buflen, ofs, bufsz, index, lite3_type_sizes[
LITE3_TYPE_BYTES] + bytes_len, &val)) < 0)
1565 size_t *__restrict inout_buflen,
1569 const char *__restrict str)
1572 size_t str_size = strlen(str) + 1;
1574 if ((ret = _lite3_set_by_index(buf, inout_buflen, ofs, bufsz, index, lite3_type_sizes[
LITE3_TYPE_STRING] + str_size, &val)) < 0)
1593 size_t *__restrict inout_buflen,
1597 const char *__restrict str,
1601 size_t str_size = str_len + 1;
1603 if ((ret = _lite3_set_by_index(buf, inout_buflen, ofs, bufsz, index, lite3_type_sizes[
LITE3_TYPE_STRING] + str_size, &val)) < 0)
1618#ifndef DOXYGEN_IGNORE
1620int lite3_arr_set_obj_impl(
unsigned char *buf,
size_t *__restrict inout_buflen,
size_t ofs,
size_t bufsz, uint32_t index,
size_t *__restrict out_ofs);
1625 size_t *__restrict inout_buflen,
1629 size_t *__restrict out_ofs)
1632 if ((ret = _lite3_verify_arr_set(buf, inout_buflen, ofs, bufsz)) < 0)
1634 return lite3_arr_set_obj_impl(buf, inout_buflen, ofs, bufsz, index, out_ofs);
1644#ifndef DOXYGEN_IGNORE
1646int lite3_arr_set_arr_impl(
unsigned char *buf,
size_t *__restrict inout_buflen,
size_t ofs,
size_t bufsz, uint32_t index,
size_t *__restrict out_ofs);
1651 size_t *__restrict inout_buflen,
1655 size_t *__restrict out_ofs)
1658 if ((ret = _lite3_verify_arr_set(buf, inout_buflen, ofs, bufsz)) < 0)
1660 return lite3_arr_set_arr_impl(buf, inout_buflen, ofs, bufsz, index, out_ofs);
1680#ifndef DOXYGEN_IGNORE
1681static inline int _lite3_verify_get(
const unsigned char *buf,
size_t buflen,
size_t ofs)
1685 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: buflen > LITE3_BUF_SIZE_MAX\n");
1690 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: START OFFSET OUT OF BOUNDS\n");
1697static inline int _lite3_verify_obj_get(
const unsigned char *buf,
size_t buflen,
size_t ofs)
1699 if (_lite3_verify_get(buf, buflen, ofs) < 0)
1702 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: EXPECTING OBJECT TYPE\n");
1709static inline int _lite3_verify_arr_get(
const unsigned char *buf,
size_t buflen,
size_t ofs)
1711 if (_lite3_verify_get(buf, buflen, ofs) < 0)
1714 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: EXPECTING ARRAY TYPE\n");
1722int lite3_get_impl(
const unsigned char *buf,
size_t buflen,
size_t ofs,
const char *__restrict key, lite3_key_data key_data,
lite3_val **out);
1724static inline int _lite3_get_by_index(
const unsigned char *buf,
size_t buflen,
size_t ofs, uint32_t index,
lite3_val **out)
1727 if ((ret = _lite3_verify_arr_get(buf, buflen, ofs)) < 0)
1730 if (LITE3_UNLIKELY(index >= size)) {
1731 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: ARRAY INDEX %u OUT OF BOUNDS (size == %u)\n", index, size);
1735 lite3_key_data key_data = {
1739 return lite3_get_impl(buf, buflen, ofs, NULL, key_data, out);
1754#define lite3_get_type(buf, buflen, ofs, key) ({ \
1755 const char *__lite3_key__ = (key); \
1756 _lite3_get_type_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1758#ifndef DOXYGEN_IGNORE
1759static inline enum lite3_type _lite3_get_type_impl(
const unsigned char *buf,
size_t buflen,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1761 if (_lite3_verify_obj_get(buf, buflen, ofs) < 0)
1764 if (lite3_get_impl(buf, buflen, ofs, key, key_data, &val) < 0)
1786#define lite3_get_type_size(buf, buflen, ofs, key, out) ({ \
1787 const char *__lite3_key__ = (key); \
1788 _lite3_get_type_size_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1790#ifndef DOXYGEN_IGNORE
1791static inline int _lite3_get_type_size_impl(
const unsigned char *buf,
size_t buflen,
size_t ofs,
const char *__restrict key, lite3_key_data key_data,
size_t *__restrict out)
1794 if ((ret = _lite3_verify_obj_get(buf, buflen, ofs)) < 0)
1797 if ((ret = lite3_get_impl(buf, buflen, ofs, key, key_data, &val)) < 0)
1804 *out = lite3_type_sizes[val->type];
1820#define lite3_exists(buf, buflen, ofs, key) ({ \
1821 const char *__lite3_key__ = (key); \
1822 _lite3_exists_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1824#ifndef DOXYGEN_IGNORE
1825static inline bool _lite3_exists_impl(
const unsigned char *buf,
size_t buflen,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1827 if (_lite3_verify_obj_get(buf, buflen, ofs) < 0)
1830 if (lite3_get_impl(buf, buflen, ofs, key, key_data, &val) < 0)
1851 if ((ret = _lite3_verify_get(buf, buflen, ofs)) < 0)
1855 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: EXPECTING ARRAY OR OBJECT TYPE\n");
1874#define lite3_is_null(buf, buflen, ofs, key) ({ \
1875 const char *__lite3_key__ = (key); \
1876 _lite3_is_null_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1878#ifndef DOXYGEN_IGNORE
1879static inline bool _lite3_is_null_impl(
const unsigned char *buf,
size_t buflen,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1881 if (_lite3_verify_obj_get(buf, buflen, ofs) < 0)
1884 if (lite3_get_impl(buf, buflen, ofs, key, key_data, &val) < 0)
1901#define lite3_is_bool(buf, buflen, ofs, key) ({ \
1902 const char *__lite3_key__ = (key); \
1903 _lite3_is_bool_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1905#ifndef DOXYGEN_IGNORE
1906static inline bool _lite3_is_bool_impl(
const unsigned char *buf,
size_t buflen,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1908 if (_lite3_verify_obj_get(buf, buflen, ofs) < 0)
1911 if (lite3_get_impl(buf, buflen, ofs, key, key_data, &val) < 0)
1928#define lite3_is_i64(buf, buflen, ofs, key) ({ \
1929 const char *__lite3_key__ = (key); \
1930 _lite3_is_i64_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1932#ifndef DOXYGEN_IGNORE
1933static inline bool _lite3_is_i64_impl(
const unsigned char *buf,
size_t buflen,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1935 if (_lite3_verify_obj_get(buf, buflen, ofs) < 0)
1938 if (lite3_get_impl(buf, buflen, ofs, key, key_data, &val) < 0)
1955#define lite3_is_f64(buf, buflen, ofs, key) ({ \
1956 const char *__lite3_key__ = (key); \
1957 _lite3_is_f64_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1959#ifndef DOXYGEN_IGNORE
1960static inline bool _lite3_is_f64_impl(
const unsigned char *buf,
size_t buflen,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1962 if (_lite3_verify_obj_get(buf, buflen, ofs) < 0)
1965 if (lite3_get_impl(buf, buflen, ofs, key, key_data, &val) < 0)
1982#define lite3_is_bytes(buf, buflen, ofs, key) ({ \
1983 const char *__lite3_key__ = (key); \
1984 _lite3_is_bytes_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1986#ifndef DOXYGEN_IGNORE
1987static inline bool _lite3_is_bytes_impl(
const unsigned char *buf,
size_t buflen,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1989 if (_lite3_verify_obj_get(buf, buflen, ofs) < 0)
1992 if (lite3_get_impl(buf, buflen, ofs, key, key_data, &val) < 0)
2009#define lite3_is_str(buf, buflen, ofs, key) ({ \
2010 const char *__lite3_key__ = (key); \
2011 _lite3_is_str_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
2013#ifndef DOXYGEN_IGNORE
2014static inline bool _lite3_is_str_impl(
const unsigned char *buf,
size_t buflen,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
2016 if (_lite3_verify_obj_get(buf, buflen, ofs) < 0)
2019 if (lite3_get_impl(buf, buflen, ofs, key, key_data, &val) < 0)
2036#define lite3_is_obj(buf, buflen, ofs, key) ({ \
2037 const char *__lite3_key__ = (key); \
2038 _lite3_is_obj_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
2040#ifndef DOXYGEN_IGNORE
2041static inline bool _lite3_is_obj_impl(
const unsigned char *buf,
size_t buflen,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
2043 if (_lite3_verify_obj_get(buf, buflen, ofs) < 0)
2046 if (lite3_get_impl(buf, buflen, ofs, key, key_data, &val) < 0)
2063#define lite3_is_arr(buf, buflen, ofs, key) ({ \
2064 const char *__lite3_key__ = (key); \
2065 _lite3_is_arr_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
2067#ifndef DOXYGEN_IGNORE
2068static inline bool _lite3_is_arr_impl(
const unsigned char *buf,
size_t buflen,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
2070 if (_lite3_verify_obj_get(buf, buflen, ofs) < 0)
2073 if (lite3_get_impl(buf, buflen, ofs, key, key_data, &val) < 0)
2116#define lite3_get(buf, buflen, ofs, key, out) ({ \
2117 const unsigned char *__lite3_buf__ = (buf); \
2118 size_t __lite3_buflen__ = (buflen); \
2119 size_t __lite3_ofs__ = (ofs); \
2120 int __lite3_ret__; \
2121 if ((__lite3_ret__ = _lite3_verify_get(__lite3_buf__, __lite3_buflen__, __lite3_ofs__) < 0)) \
2122 return __lite3_ret__; \
2123 const char *__lite3_key__ = (key); \
2124 lite3_get_impl(__lite3_buf__, __lite3_buflen__, __lite3_ofs__, __lite3_key__, LITE3_KEY_DATA(key), out); \
2139#define lite3_get_bool(buf, buflen, ofs, key, out) ({ \
2140 const char *__lite3_key__ = (key); \
2141 _lite3_get_bool_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
2143#ifndef DOXYGEN_IGNORE
2144static inline int _lite3_get_bool_impl(
const unsigned char *buf,
size_t buflen,
size_t ofs,
const char *__restrict key, lite3_key_data key_data,
bool *out)
2147 if ((ret = _lite3_verify_obj_get(buf, buflen, ofs)) < 0)
2150 if ((ret = lite3_get_impl(buf, buflen, ofs, key, key_data, &val)) < 0)
2153 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_BOOL\n");
2157 *out = (bool)(*(val->val));
2174#define lite3_get_i64(buf, buflen, ofs, key, out) ({ \
2175 const char *__lite3_key__ = (key); \
2176 _lite3_get_i64_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
2178#ifndef DOXYGEN_IGNORE
2179static inline int _lite3_get_i64_impl(
const unsigned char *buf,
size_t buflen,
size_t ofs,
const char *__restrict key, lite3_key_data key_data, int64_t *out)
2182 if ((ret = _lite3_verify_obj_get(buf, buflen, ofs)) < 0)
2185 if ((ret = lite3_get_impl(buf, buflen, ofs, key, key_data, &val)) < 0)
2188 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_I64\n");
2209#define lite3_get_f64(buf, buflen, ofs, key, out) ({ \
2210 const char *__lite3_key__ = (key); \
2211 _lite3_get_f64_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
2213#ifndef DOXYGEN_IGNORE
2214static inline int _lite3_get_f64_impl(
const unsigned char *buf,
size_t buflen,
size_t ofs,
const char *__restrict key, lite3_key_data key_data,
double *out)
2217 if ((ret = _lite3_verify_obj_get(buf, buflen, ofs)) < 0)
2220 if ((ret = lite3_get_impl(buf, buflen, ofs, key, key_data, &val)) < 0)
2223 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_F64\n");
2244#define lite3_get_bytes(buf, buflen, ofs, key, out) ({ \
2245 const char *__lite3_key__ = (key); \
2246 _lite3_get_bytes_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
2248#ifndef DOXYGEN_IGNORE
2249static inline int _lite3_get_bytes_impl(
const unsigned char *buf,
size_t buflen,
size_t ofs,
const char *__restrict key, lite3_key_data key_data,
lite3_bytes *out)
2252 if ((ret = _lite3_verify_obj_get(buf, buflen, ofs)) < 0)
2255 if ((ret = lite3_get_impl(buf, buflen, ofs, key, key_data, &val)) < 0)
2258 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_BYTES\n");
2263 .
gen = *(uint32_t *)buf,
2284#define lite3_get_str(buf, buflen, ofs, key, out) ({ \
2285 const char *__lite3_key__ = (key); \
2286 _lite3_get_str_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
2288#ifndef DOXYGEN_IGNORE
2289static inline int _lite3_get_str_impl(
const unsigned char *buf,
size_t buflen,
size_t ofs,
const char *__restrict key, lite3_key_data key_data,
lite3_str *out)
2292 if ((ret = _lite3_verify_obj_get(buf, buflen, ofs)) < 0)
2295 if ((ret = lite3_get_impl(buf, buflen, ofs, key, key_data, &val)) < 0)
2298 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_STRING\n");
2303 .
gen = *(uint32_t *)buf,
2325#define lite3_get_obj(buf, buflen, ofs, key, out) ({ \
2326 const char *__lite3_key__ = (key); \
2327 _lite3_get_obj_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
2329#ifndef DOXYGEN_IGNORE
2330static inline int _lite3_get_obj_impl(
const unsigned char *buf,
size_t buflen,
size_t ofs,
const char *__restrict key, lite3_key_data key_data,
size_t *__restrict out_ofs)
2333 if ((ret = _lite3_verify_obj_get(buf, buflen, ofs)) < 0)
2336 if ((ret = lite3_get_impl(buf, buflen, ofs, key, key_data, &val)) < 0)
2339 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_OBJECT\n");
2343 *out_ofs = (size_t)((uint8_t *)val - buf);
2360#define lite3_get_arr(buf, buflen, ofs, key, out) ({ \
2361 const char *__lite3_key__ = (key); \
2362 _lite3_get_arr_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
2364#ifndef DOXYGEN_IGNORE
2365static inline int _lite3_get_arr_impl(
const unsigned char *buf,
size_t buflen,
size_t ofs,
const char *__restrict key, lite3_key_data key_data,
size_t *__restrict out_ofs)
2368 if ((ret = _lite3_verify_obj_get(buf, buflen, ofs)) < 0)
2371 if ((ret = lite3_get_impl(buf, buflen, ofs, key, key_data, &val)) < 0)
2374 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_ARRAY\n");
2378 *out_ofs = (size_t)((uint8_t *)val - buf);
2411 const unsigned char *buf,
2419 if ((ret = _lite3_get_by_index(buf, buflen, ofs, index, &val)) < 0)
2422 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_BOOL\n");
2426 *out = (bool)(*(val->val));
2437 const unsigned char *buf,
2445 if ((ret = _lite3_get_by_index(buf, buflen, ofs, index, &val)) < 0)
2448 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_I64\n");
2463 const unsigned char *buf,
2471 if ((ret = _lite3_get_by_index(buf, buflen, ofs, index, &val)) < 0)
2474 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_F64\n");
2489 const unsigned char *buf,
2497 if ((ret = _lite3_get_by_index(buf, buflen, ofs, index, &val)) < 0)
2500 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_BYTES\n");
2505 .
gen = *(uint32_t *)buf,
2520 const unsigned char *buf,
2528 if ((ret = _lite3_get_by_index(buf, buflen, ofs, index, &val)) < 0)
2531 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_STRING\n");
2536 .
gen = *(uint32_t *)buf,
2552 const unsigned char *buf,
2556 size_t *__restrict out_ofs)
2560 if ((ret = _lite3_get_by_index(buf, buflen, ofs, index, &val)) < 0)
2563 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_OBJECT\n");
2567 *out_ofs = (size_t)((uint8_t *)val - buf);
2578 const unsigned char *buf,
2582 size_t *__restrict out_ofs)
2586 if ((ret = _lite3_get_by_index(buf, buflen, ofs, index, &val)) < 0)
2589 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_ARRAY\n");
2593 *out_ofs = (size_t)((uint8_t *)val - buf);
2616#define LITE3_ITER_ITEM 1
2618#define LITE3_ITER_DONE 0
2633#ifndef DOXYGEN_IGNORE
2635int lite3_iter_create_impl(
const unsigned char *buf,
size_t buflen,
size_t ofs,
lite3_iter *out);
2645 const unsigned char *buf,
2651 if ((ret = _lite3_verify_get(buf, buflen, ofs)) < 0)
2653 return lite3_iter_create_impl(buf, buflen, ofs, out);
2673 const unsigned char *buf,
2717 return lite3_type_sizes[val->type];
2729static inline bool lite3_val_bool(
lite3_val *val)
2731 return *(
bool *)(val->val);
2734static inline int64_t lite3_val_i64(
lite3_val *val)
2737 memcpy(&tmp, val->val,
sizeof(tmp));
2741static inline double lite3_val_f64(
lite3_val *val)
2744 memcpy(&tmp, val->val,
sizeof(tmp));
2748static inline const char *lite3_val_str(
lite3_val *val)
2750 return (
const char *)val->val + LITE3_STR_LEN_SIZE;
2760 memcpy(out_len, val->val, LITE3_STR_LEN_SIZE);
2762 return (
const char *)val->val + LITE3_STR_LEN_SIZE;
2765static inline const unsigned char *lite3_val_bytes(
lite3_val *val,
size_t *out_len)
2768 memcpy(out_len, val->val, LITE3_BYTES_LEN_SIZE);
2769 return (
const unsigned char *)val->val + LITE3_BYTES_LEN_SIZE;
2813#if defined(DOXYGEN_ONLY) && !defined(LITE3_JSON)
2828#define LITE3_JSON_NESTING_DEPTH_MAX 32
2843 size_t *__restrict out_buflen,
2845 const char *__restrict json_str,
2862 size_t *__restrict out_buflen,
2864 const char *__restrict path
2880 size_t *__restrict out_buflen,
2899 const unsigned char *buf,
2918 const unsigned char *buf,
2921 size_t *__restrict out_len
2940 const unsigned char *buf,
2943 size_t *__restrict out_len
2957 const unsigned char *buf,
2960 char *__restrict json_buf,
2977 const unsigned char *buf,
2980 char *__restrict json_buf,
2984static inline int lite3_json_dec(
unsigned char *buf,
size_t *__restrict out_buflen,
size_t bufsz,
const char *__restrict json_str,
size_t json_len)
2986 (void)buf; (void)out_buflen; (void)bufsz; (void)json_str; (void)json_len;
2990static inline int lite3_json_dec_file(
unsigned char *buf,
size_t *__restrict out_buflen,
size_t bufsz,
const char *__restrict path)
2992 (void)buf; (void)out_buflen; (void)bufsz; (void)path;
2996static inline int lite3_json_dec_fp(
unsigned char *buf,
size_t *__restrict out_buflen,
size_t bufsz, FILE *fp)
2998 (void)buf; (void)out_buflen; (void)bufsz; (void)fp;
3002static inline int lite3_json_print(
const unsigned char *buf,
size_t buflen,
size_t ofs)
3004 (void)buf; (void)buflen; (void)ofs;
3008static inline char *
lite3_json_enc(
const unsigned char *buf,
size_t buflen,
size_t ofs,
size_t *out_len)
3010 (void)buf; (void)buflen; (void)ofs; (void)out_len;
3014static inline char *
lite3_json_enc_pretty(
const unsigned char *buf,
size_t buflen,
size_t ofs,
size_t *out_len)
3016 (void)buf; (void)buflen; (void)ofs; (void)out_len;
3020static inline int64_t
lite3_json_enc_buf(
const unsigned char *buf,
size_t buflen,
size_t ofs,
char *__restrict json_buf,
size_t json_bufsz)
3022 (void)buf; (void)buflen; (void)ofs; (void)json_buf; (void)json_bufsz;
3026static inline 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)
3028 (void)buf; (void)buflen; (void)ofs; (void)json_buf; (void)json_bufsz;
static int lite3_arr_append_f64(unsigned char *buf, size_t *__restrict inout_buflen, size_t ofs, size_t bufsz, double value)
Append floating point to array.
static int lite3_arr_append_bool(unsigned char *buf, size_t *__restrict inout_buflen, size_t ofs, size_t bufsz, bool value)
Append boolean to array.
static int lite3_arr_append_str_n(unsigned char *buf, size_t *__restrict inout_buflen, size_t ofs, size_t bufsz, const char *__restrict str, size_t str_len)
Append string to array by length.
static int lite3_arr_append_null(unsigned char *buf, size_t *__restrict inout_buflen, size_t ofs, size_t bufsz)
Append null to array.
static int lite3_arr_append_arr(unsigned char *buf, size_t *__restrict inout_buflen, size_t ofs, size_t bufsz, size_t *__restrict out_ofs)
Append array to array.
static int lite3_arr_append_i64(unsigned char *buf, size_t *__restrict inout_buflen, size_t ofs, size_t bufsz, int64_t value)
Append integer to array.
static int lite3_arr_append_bytes(unsigned char *buf, size_t *__restrict inout_buflen, size_t ofs, size_t bufsz, const unsigned char *__restrict bytes, size_t bytes_len)
Append bytes to array.
static int lite3_arr_append_obj(unsigned char *buf, size_t *__restrict inout_buflen, size_t ofs, size_t bufsz, size_t *__restrict out_ofs)
Append object to array.
static int lite3_arr_append_str(unsigned char *buf, size_t *__restrict inout_buflen, size_t ofs, size_t bufsz, const char *__restrict str)
Append string to array.
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.
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.
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.
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.
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.
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.
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.
static int lite3_arr_set_null(unsigned char *buf, size_t *__restrict inout_buflen, size_t ofs, size_t bufsz, uint32_t index)
Set null in array.
static int lite3_arr_set_obj(unsigned char *buf, size_t *__restrict inout_buflen, size_t ofs, size_t bufsz, uint32_t index, size_t *__restrict out_ofs)
Set object in array.
static int lite3_arr_set_str_n(unsigned char *buf, size_t *__restrict inout_buflen, size_t ofs, size_t bufsz, uint32_t index, const char *__restrict str, size_t str_len)
Set string in array by length.
static int lite3_arr_set_i64(unsigned char *buf, size_t *__restrict inout_buflen, size_t ofs, size_t bufsz, uint32_t index, int64_t value)
Set integer in array.
static int lite3_arr_set_str(unsigned char *buf, size_t *__restrict inout_buflen, size_t ofs, size_t bufsz, uint32_t index, const char *__restrict str)
Set string in array.
static int lite3_arr_set_bool(unsigned char *buf, size_t *__restrict inout_buflen, size_t ofs, size_t bufsz, uint32_t index, bool value)
Set boolean in array.
static int lite3_arr_set_bytes(unsigned char *buf, size_t *__restrict inout_buflen, size_t ofs, size_t bufsz, uint32_t index, const unsigned char *__restrict bytes, size_t bytes_len)
Set bytes in array.
static int lite3_arr_set_arr(unsigned char *buf, size_t *__restrict inout_buflen, size_t ofs, size_t bufsz, uint32_t index, size_t *__restrict out_ofs)
Set array in array.
static int lite3_arr_set_f64(unsigned char *buf, size_t *__restrict inout_buflen, size_t ofs, size_t bufsz, uint32_t index, double value)
Set float in array.
#define LITE3_BUF_SIZE_MAX
Maximum Lite³ buffer size.
#define LITE3_TREE_HEIGHT_MAX
Maximum B-tree height.
#define LITE3_NODE_SIZE
B-tree node size setting.
#define LITE3_NODE_SIZE_KC_OFFSET
Offset of the size_kc field inside struct node.
int lite3_init_arr(unsigned char *buf, size_t *__restrict out_buflen, size_t bufsz)
Initialize a Lite³ buffer as an array.
int lite3_init_obj(unsigned char *buf, size_t *__restrict out_buflen, size_t bufsz)
Initialize a Lite³ buffer as an object.
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.
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.
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
@ LITE3_TYPE_ARRAY
maps to 'array' type in JSON
@ LITE3_TYPE_INVALID
any type value equal or greater than this is considered invalid
@ LITE3_TYPE_STRING
maps to 'string' type in JSON
@ LITE3_TYPE_BOOL
maps to 'boolean' type in JSON; underlying datatype: bool
@ LITE3_TYPE_BYTES
coverted to base64 string in JSON
@ LITE3_TYPE_F64
maps to 'number' type in JSON; underlying datatype: double
@ LITE3_TYPE_OBJECT
maps to 'object' type in JSON
@ LITE3_TYPE_I64
maps to 'number' type in JSON; underlying datatype: int64_t
@ LITE3_TYPE_NULL
maps to 'null' type in JSON
@ LITE3_TYPE_COUNT
not an actual type, only used for counting
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.
static size_t lite3_val_type_size(lite3_val *val)
Returns the size of the value type.
static const char * lite3_val_str_n(lite3_val *val, size_t *out_len)
static enum lite3_type lite3_val_type(lite3_val *val)
Returns the value type of *val
Struct holding a reference to a bytes value inside a Lite³ buffer.
uint32_t gen
generation of the Lite³ buffer when this struct was returned
uint32_t len
byte array length (bytes)
const unsigned char * ptr
byte array pointer to bytes inside Lite³ buffer
Struct containing iterator state.
Struct holding a reference to a string inside a Lite³ buffer.
uint32_t len
char array length (characters, exclusive of NULL-terminator)
const char * ptr
char array pointer to string inside Lite³ buffer
uint32_t gen
generation of the Lite³ buffer when this struct was returned
Struct representing a value inside a Lite³ buffer.