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
275#define LITE3_NODE_ALIGNMENT 4
277#ifndef DOXYGEN_IGNORE
278#define LITE3_NODE_ALIGNMENT_MASK ((uintptr_t)(LITE3_NODE_ALIGNMENT - 1))
293#define LITE3_NODE_SIZE 96
308#define LITE3_TREE_HEIGHT_MAX 9
322#define LITE3_NODE_SIZE_KC_OFFSET 32
327#ifndef DOXYGEN_IGNORE
328#define LITE3_NODE_SIZE_SHIFT 6
329#define LITE3_NODE_SIZE_MASK ((u32)~((1 << 6) - 1))
331#define LITE3_DJB2_HASH_SEED ((uint32_t)5381)
341#ifndef LITE3_HASH_PROBE_MAX
342#define LITE3_HASH_PROBE_MAX 128U
345#if LITE3_HASH_PROBE_MAX < 2
346 #error "LITE3_HASH_PROBE_MAX must be >= 2"
349#define LITE3_VERIFY_KEY_OK 0
350#define LITE3_VERIFY_KEY_HASH_COLLISION 1
365#define LITE3_KEY_HASH_COMPILE_TIME
367#ifndef DOXYGEN_IGNORE
368#ifdef LITE3_KEY_HASH_COMPILE_TIME
369#define LITE3_MOD 4294967296ULL
371#define LITE3_P0 33ULL
372#define LITE3_P1 ((LITE3_P0 * LITE3_P0) % LITE3_MOD)
373#define LITE3_P2 ((LITE3_P1 * LITE3_P1) % LITE3_MOD)
374#define LITE3_P3 ((LITE3_P2 * LITE3_P2) % LITE3_MOD)
375#define LITE3_P4 ((LITE3_P3 * LITE3_P3) % LITE3_MOD)
376#define LITE3_P5 ((LITE3_P4 * LITE3_P4) % LITE3_MOD)
378#define LITE3_BIT0(k) (((k) >> 0) & 1ULL)
379#define LITE3_BIT1(k) (((k) >> 1) & 1ULL)
380#define LITE3_BIT2(k) (((k) >> 2) & 1ULL)
381#define LITE3_BIT3(k) (((k) >> 3) & 1ULL)
382#define LITE3_BIT4(k) (((k) >> 4) & 1ULL)
383#define LITE3_BIT5(k) (((k) >> 5) & 1ULL)
385#define LITE3_POW33(k) \
387 * (LITE3_BIT0(k) ? LITE3_P0 : 1ULL) \
389 * (LITE3_BIT1(k) ? LITE3_P1 : 1ULL) \
391 * (LITE3_BIT2(k) ? LITE3_P2 : 1ULL) \
393 * (LITE3_BIT3(k) ? LITE3_P3 : 1ULL) \
395 * (LITE3_BIT4(k) ? LITE3_P4 : 1ULL) \
397 * (LITE3_BIT5(k) ? LITE3_P5 : 1ULL) \
400#define LITE3_SUM_1(s, i, p) \
401 ((unsigned long long)(unsigned char)(s)[(i)] * LITE3_POW33((p)))
403#define LITE3_SUM_2(s, i, p) \
404 (LITE3_SUM_1(s, (i), (p)) + LITE3_SUM_1(s, (i) + 1, (p) - 1))
406#define LITE3_SUM_4(s, i, p) \
407 (LITE3_SUM_2(s, (i), (p)) + LITE3_SUM_2(s, (i) + 2, (p) - 2))
409#define LITE3_SUM_8(s, i, p) \
410 (LITE3_SUM_4(s, (i), (p)) + LITE3_SUM_4(s, (i) + 4, (p) - 4))
412#define LITE3_SUM_16(s, i, p) \
413 (LITE3_SUM_8(s, (i), (p)) + LITE3_SUM_8(s, (i) + 8, (p) - 8))
415#define LITE3_SUM_32(s, i, p) \
416 (LITE3_SUM_16(s, (i), (p)) + LITE3_SUM_16(s, (i) + 16, (p) - 16))
418#define LITE3_OFFSET_BIT0(len) \
419 (((len & 2ULL) ? 2ULL : 0ULL) + \
420 ((len & 4ULL) ? 4ULL : 0ULL) + \
421 ((len & 8ULL) ? 8ULL : 0ULL) + \
422 ((len & 16ULL) ? 16ULL : 0ULL) + \
423 ((len & 32ULL) ? 32ULL : 0ULL))
425#define LITE3_OFFSET_BIT1(len) \
426 (((len & 4ULL) ? 4ULL : 0ULL) + \
427 ((len & 8ULL) ? 8ULL : 0ULL) + \
428 ((len & 16ULL) ? 16ULL : 0ULL) + \
429 ((len & 32ULL) ? 32ULL : 0ULL))
431#define LITE3_OFFSET_BIT2(len) \
432 (((len & 8ULL) ? 8ULL : 0ULL) + \
433 ((len & 16ULL) ? 16ULL : 0ULL) + \
434 ((len & 32ULL) ? 32ULL : 0ULL))
436#define LITE3_OFFSET_BIT3(len) \
437 (((len & 16ULL) ? 16ULL : 0ULL) + \
438 ((len & 32ULL) ? 32ULL : 0ULL))
440#define LITE3_OFFSET_BIT4(len) \
441 (((len & 32ULL) ? 32ULL : 0ULL))
443#define LITE3_OFFSET_BIT5(len) \
446#define LITE3_POLY_HASH(s, len) \
447 ((len) & 1ULL ? LITE3_SUM_1(s, LITE3_OFFSET_BIT0((len)), (len) - 1 - LITE3_OFFSET_BIT0((len))) : 0ULL) + \
448 ((len) & 2ULL ? LITE3_SUM_2(s, LITE3_OFFSET_BIT1((len)), (len) - 1 - LITE3_OFFSET_BIT1((len))) : 0ULL) + \
449 ((len) & 4ULL ? LITE3_SUM_4(s, LITE3_OFFSET_BIT2((len)), (len) - 1 - LITE3_OFFSET_BIT2((len))) : 0ULL) + \
450 ((len) & 8ULL ? LITE3_SUM_8(s, LITE3_OFFSET_BIT3((len)), (len) - 1 - LITE3_OFFSET_BIT3((len))) : 0ULL) + \
451 ((len) & 16ULL ? LITE3_SUM_16(s, LITE3_OFFSET_BIT4((len)), (len) - 1 - LITE3_OFFSET_BIT4((len))) : 0ULL) + \
452 ((len) & 32ULL ? LITE3_SUM_32(s, LITE3_OFFSET_BIT5((len)), (len) - 1 - LITE3_OFFSET_BIT5((len))) : 0ULL)
454#define LITE3_STRLEN(s) (sizeof(s) - 1)
461static inline lite3_key_data lite3_get_key_data(
const char *key) {
462 lite3_key_data key_data;
463 const char *key_cursor = key;
464 key_data.hash = LITE3_DJB2_HASH_SEED;
466 key_data.hash = ((key_data.hash << 5) + key_data.hash) + (uint8_t)(*key_cursor++);
467 key_data.size = (uint32_t)(key_cursor - key) + 1;
471#define LITE3_KEY_DATA(s) ( \
472 __builtin_constant_p(s) ? \
473 ((LITE3_STRLEN(s) < 64) ? \
475 .hash = (uint32_t)((LITE3_POLY_HASH(s, LITE3_STRLEN(s)) + LITE3_DJB2_HASH_SEED * LITE3_POW33(LITE3_STRLEN(s))) % LITE3_MOD), \
476 .size = (sizeof(s)), \
478 : lite3_get_key_data(s)) \
479 : lite3_get_key_data(__lite3_key__) \
482#define LITE3_KEY_DATA(s) lite3_get_key_data(__lite3_key__)
526#ifndef DOXYGEN_IGNORE
527#define LITE3_VAL_SIZE sizeof(lite3_val)
529static_assert(LITE3_VAL_SIZE <=
sizeof(size_t),
"LITE3_VAL_SIZE must be <= sizeof(size_t)");
531#ifndef DOXYGEN_IGNORE
532static const size_t lite3_type_sizes[] = {
544static_assert((
sizeof(lite3_type_sizes) /
sizeof(
size_t)) ==
LITE3_TYPE_COUNT,
"lite3_type_sizes[] element count != LITE3_TYPE_COUNT");
545static_assert(4 <=
sizeof(size_t),
"lite3_type_sizes[LITE3_TYPE_BYTES] and lite3_type_sizes[LITE3_TYPE_STRING] must fit inside size_t");
568#ifndef DOXYGEN_IGNORE
569#define LITE3_BYTES_LEN_SIZE sizeof(((lite3_bytes *)0)->len)
571static_assert(LITE3_BYTES_LEN_SIZE <=
sizeof(size_t),
"lite3_val_bytes() expects LITE3_BYTES_LEN_SIZE to be <= sizeof(size_t)");
597#ifndef DOXYGEN_IGNORE
598#define LITE3_STR_LEN_SIZE sizeof(((lite3_str *)0)->len)
600static_assert(LITE3_STR_LEN_SIZE <=
sizeof(size_t),
"lite3_val_str() expects LITE3_STR_LEN_SIZE to be <= sizeof(size_t)");
627#define LITE3_BYTES(buf, val) (const unsigned char *)_lite3_ptr_suppress_nonnull_warning( \
628 (uint32_t)(val).gen == *(uint32_t *)(buf) ? (val).ptr : NULL \
656#define LITE3_STR(buf, val) (const char *)_lite3_ptr_suppress_nonnull_warning( \
657 (uint32_t)(val).gen == *(uint32_t *)(buf) ? (val).ptr : NULL \
660#ifndef DOXYGEN_IGNORE
661static inline __attribute__((always_inline))
const void *_lite3_ptr_suppress_nonnull_warning(
const void *p) {
return p; }
692 size_t *__restrict out_buflen,
710 size_t *__restrict out_buflen,
747#ifndef DOXYGEN_IGNORE
748static inline int _lite3_verify_set(
unsigned char *buf,
size_t *__restrict inout_buflen,
size_t ofs,
size_t bufsz)
752 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: bufsz > LITE3_BUF_SIZE_MAX\n");
756 if (LITE3_UNLIKELY(*inout_buflen > bufsz)) {
757 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: inout_buflen > bufsz\n");
762 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: START OFFSET OUT OF BOUNDS\n");
769static inline int _lite3_verify_obj_set(
unsigned char *buf,
size_t *__restrict inout_buflen,
size_t ofs,
size_t bufsz,
const char *__restrict key)
772 if ((ret = _lite3_verify_set(buf, inout_buflen, ofs, bufsz)) < 0)
775 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: EXPECTING OBJECT TYPE\n");
779 if (LITE3_UNLIKELY(!key)) {
780 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: EXPECTING NON-NULL KEY\n");
787static inline int _lite3_verify_arr_set(
unsigned char *buf,
size_t *__restrict inout_buflen,
size_t ofs,
size_t bufsz)
790 if ((ret = _lite3_verify_set(buf, inout_buflen, ofs, bufsz)) < 0)
793 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: EXPECTING ARRAY TYPE\n");
801int 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);
816#define lite3_set_null(buf, inout_buflen, ofs, bufsz, key) ({ \
817 const char *__lite3_key__ = (key); \
818 _lite3_set_null_impl(buf, inout_buflen, ofs, bufsz, __lite3_key__, LITE3_KEY_DATA(key)); \
820#ifndef DOXYGEN_IGNORE
821static 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)
824 if ((ret = _lite3_verify_obj_set(buf, inout_buflen, ofs, bufsz, key)) < 0)
827 if ((ret = lite3_set_impl(buf, inout_buflen, ofs, bufsz, key, key_data, lite3_type_sizes[
LITE3_TYPE_NULL], &val)) < 0)
847#define lite3_set_bool(buf, inout_buflen, ofs, bufsz, key, value) ({ \
848 const char *__lite3_key__ = (key); \
849 _lite3_set_bool_impl(buf, inout_buflen, ofs, bufsz, __lite3_key__, LITE3_KEY_DATA(key), value); \
851#ifndef DOXYGEN_IGNORE
852static 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)
855 if ((ret = _lite3_verify_obj_set(buf, inout_buflen, ofs, bufsz, key)) < 0)
858 if ((ret = lite3_set_impl(buf, inout_buflen, ofs, bufsz, key, key_data, lite3_type_sizes[
LITE3_TYPE_BOOL], &val)) < 0)
879#define lite3_set_i64(buf, inout_buflen, ofs, bufsz, key, value) ({ \
880 const char *__lite3_key__ = (key); \
881 _lite3_set_i64_impl(buf, inout_buflen, ofs, bufsz, __lite3_key__, LITE3_KEY_DATA(key), value); \
883#ifndef DOXYGEN_IGNORE
884static 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)
887 if ((ret = _lite3_verify_obj_set(buf, inout_buflen, ofs, bufsz, key)) < 0)
890 if ((ret = lite3_set_impl(buf, inout_buflen, ofs, bufsz, key, key_data, lite3_type_sizes[
LITE3_TYPE_I64], &val)) < 0)
911#define lite3_set_f64(buf, inout_buflen, ofs, bufsz, key, value) ({ \
912 const char *__lite3_key__ = (key); \
913 _lite3_set_f64_impl(buf, inout_buflen, ofs, bufsz, __lite3_key__, LITE3_KEY_DATA(key), value); \
915#ifndef DOXYGEN_IGNORE
916static 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)
919 if ((ret = _lite3_verify_obj_set(buf, inout_buflen, ofs, bufsz, key)) < 0)
922 if ((ret = lite3_set_impl(buf, inout_buflen, ofs, bufsz, key, key_data, lite3_type_sizes[
LITE3_TYPE_F64], &val)) < 0)
944#define lite3_set_bytes(buf, inout_buflen, ofs, bufsz, key, bytes, bytes_len) ({ \
945 const char *__lite3_key__ = (key); \
946 _lite3_set_bytes_impl(buf, inout_buflen, ofs, bufsz, __lite3_key__, LITE3_KEY_DATA(key), bytes, bytes_len); \
948#ifndef DOXYGEN_IGNORE
949static 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)
952 if ((ret = _lite3_verify_obj_set(buf, inout_buflen, ofs, bufsz, key)) < 0)
955 if ((ret = lite3_set_impl(buf, inout_buflen, ofs, bufsz, key, key_data, lite3_type_sizes[
LITE3_TYPE_BYTES] + bytes_len, &val)) < 0)
981#define lite3_set_str(buf, inout_buflen, ofs, bufsz, key, str) ({ \
982 const char *__lite3_key__ = (key); \
983 _lite3_set_str_impl(buf, inout_buflen, ofs, bufsz, __lite3_key__, LITE3_KEY_DATA(key), str); \
985#ifndef DOXYGEN_IGNORE
986static 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)
989 if ((ret = _lite3_verify_obj_set(buf, inout_buflen, ofs, bufsz, key)) < 0)
992 size_t str_size = strlen(str) + 1;
993 if ((ret = lite3_set_impl(buf, inout_buflen, ofs, bufsz, key, key_data, lite3_type_sizes[
LITE3_TYPE_STRING] + str_size, &val)) < 0)
1019#define lite3_set_str_n(buf, inout_buflen, ofs, bufsz, key, str, str_len) ({ \
1020 const char *__lite3_key__ = (key); \
1021 _lite3_set_str_n_impl(buf, inout_buflen, ofs, bufsz, __lite3_key__, LITE3_KEY_DATA(key), str, str_len); \
1023#ifndef DOXYGEN_IGNORE
1024static 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)
1027 if ((ret = _lite3_verify_obj_set(buf, inout_buflen, ofs, bufsz, key)) < 0)
1030 size_t str_size = str_len + 1;
1031 if ((ret = lite3_set_impl(buf, inout_buflen, ofs, bufsz, key, key_data, lite3_type_sizes[
LITE3_TYPE_STRING] + str_size, &val)) < 0)
1054#define lite3_set_obj(buf, inout_buflen, ofs, bufsz, key, out_ofs) ({ \
1055 unsigned char *__lite3_buf__ = (buf); \
1056 size_t *__lite3_inout_buflen__ = (inout_buflen); \
1057 size_t __lite3_ofs__ = (ofs); \
1058 size_t __lite3_bufsz__ = (bufsz); \
1059 const char *__lite3_key__ = (key); \
1060 int __lite3_ret__; \
1061 if ((__lite3_ret__ = _lite3_verify_obj_set( \
1063 __lite3_inout_buflen__, \
1066 __lite3_key__)) < 0) \
1067 return __lite3_ret__; \
1069 lite3_set_obj_impl( \
1071 __lite3_inout_buflen__, \
1075 LITE3_KEY_DATA(key), \
1078#ifndef DOXYGEN_IGNORE
1080int 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);
1096#define lite3_set_arr(buf, inout_buflen, ofs, bufsz, key, out_ofs) ({ \
1097 unsigned char *__lite3_buf__ = (buf); \
1098 size_t *__lite3_inout_buflen__ = (inout_buflen); \
1099 size_t __lite3_ofs__ = (ofs); \
1100 size_t __lite3_bufsz__ = (bufsz); \
1101 const char *__lite3_key__ = (key); \
1102 int __lite3_ret__; \
1103 if ((__lite3_ret__ = _lite3_verify_obj_set( \
1105 __lite3_inout_buflen__, \
1108 __lite3_key__)) < 0) \
1109 return __lite3_ret__; \
1111 lite3_set_arr_impl( \
1113 __lite3_inout_buflen__, \
1117 LITE3_KEY_DATA(key), \
1120#ifndef DOXYGEN_IGNORE
1122int 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);
1152#ifndef DOXYGEN_IGNORE
1153static 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)
1156 if ((ret = _lite3_verify_arr_set(buf, inout_buflen, ofs, bufsz)) < 0)
1159 if (LITE3_UNLIKELY(index > size)) {
1160 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: ARRAY INDEX %u OUT OF BOUNDS (size == %u)\n", index, size);
1164 lite3_key_data key_data = {
1168 return lite3_set_impl(buf, inout_buflen, ofs, bufsz, NULL, key_data, val_len, out);
1171static 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)
1174 if ((ret = _lite3_verify_arr_set(buf, inout_buflen, ofs, bufsz)) < 0)
1177 lite3_key_data key_data = {
1181 return lite3_set_impl(buf, inout_buflen, ofs, bufsz, NULL, key_data, val_len, out);
1193 size_t *__restrict inout_buflen,
1199 if ((ret = _lite3_set_by_append(buf, inout_buflen, ofs, bufsz, lite3_type_sizes[
LITE3_TYPE_NULL], &val)) < 0)
1213 size_t *__restrict inout_buflen,
1220 if ((ret = _lite3_set_by_append(buf, inout_buflen, ofs, bufsz, lite3_type_sizes[
LITE3_TYPE_BOOL], &val)) < 0)
1235 size_t *__restrict inout_buflen,
1242 if ((ret = _lite3_set_by_append(buf, inout_buflen, ofs, bufsz, lite3_type_sizes[
LITE3_TYPE_I64], &val)) < 0)
1257 size_t *__restrict inout_buflen,
1264 if ((ret = _lite3_set_by_append(buf, inout_buflen, ofs, bufsz, lite3_type_sizes[
LITE3_TYPE_F64], &val)) < 0)
1279 size_t *__restrict inout_buflen,
1282 const unsigned char *__restrict bytes,
1287 if ((ret = _lite3_set_by_append(buf, inout_buflen, ofs, bufsz, lite3_type_sizes[
LITE3_TYPE_BYTES] + bytes_len, &val)) < 0)
1307 size_t *__restrict inout_buflen,
1310 const char *__restrict str)
1313 size_t str_size = strlen(str) + 1;
1315 if ((ret = _lite3_set_by_append(buf, inout_buflen, ofs, bufsz, lite3_type_sizes[
LITE3_TYPE_STRING] + str_size, &val)) < 0)
1334 size_t *__restrict inout_buflen,
1337 const char *__restrict str,
1341 size_t str_size = str_len + 1;
1343 if ((ret = _lite3_set_by_append(buf, inout_buflen, ofs, bufsz, lite3_type_sizes[
LITE3_TYPE_STRING] + str_size, &val)) < 0)
1358#ifndef DOXYGEN_IGNORE
1360int lite3_arr_append_obj_impl(
unsigned char *buf,
size_t *__restrict inout_buflen,
size_t ofs,
size_t bufsz,
size_t *__restrict out_ofs);
1365 size_t *__restrict inout_buflen,
1368 size_t *__restrict out_ofs)
1371 if ((ret = _lite3_verify_arr_set(buf, inout_buflen, ofs, bufsz)) < 0)
1373 return lite3_arr_append_obj_impl(buf, inout_buflen, ofs, bufsz, out_ofs);
1383#ifndef DOXYGEN_IGNORE
1385int lite3_arr_append_arr_impl(
unsigned char *buf,
size_t *__restrict inout_buflen,
size_t ofs,
size_t bufsz,
size_t *__restrict out_ofs);
1390 size_t *__restrict inout_buflen,
1393 size_t *__restrict out_ofs)
1396 if ((ret = _lite3_verify_arr_set(buf, inout_buflen, ofs, bufsz)) < 0)
1398 return lite3_arr_append_arr_impl(buf, inout_buflen, ofs, bufsz, out_ofs);
1443 size_t *__restrict inout_buflen,
1450 if ((ret = _lite3_set_by_index(buf, inout_buflen, ofs, bufsz, index, lite3_type_sizes[
LITE3_TYPE_NULL], &val)) < 0)
1464 size_t *__restrict inout_buflen,
1472 if ((ret = _lite3_set_by_index(buf, inout_buflen, ofs, bufsz, index, lite3_type_sizes[
LITE3_TYPE_BOOL], &val)) < 0)
1487 size_t *__restrict inout_buflen,
1495 if ((ret = _lite3_set_by_index(buf, inout_buflen, ofs, bufsz, index, lite3_type_sizes[
LITE3_TYPE_I64], &val)) < 0)
1510 size_t *__restrict inout_buflen,
1518 if ((ret = _lite3_set_by_index(buf, inout_buflen, ofs, bufsz, index, lite3_type_sizes[
LITE3_TYPE_F64], &val)) < 0)
1533 size_t *__restrict inout_buflen,
1537 const unsigned char *__restrict bytes,
1542 if ((ret = _lite3_set_by_index(buf, inout_buflen, ofs, bufsz, index, lite3_type_sizes[
LITE3_TYPE_BYTES] + bytes_len, &val)) < 0)
1562 size_t *__restrict inout_buflen,
1566 const char *__restrict str)
1569 size_t str_size = strlen(str) + 1;
1571 if ((ret = _lite3_set_by_index(buf, inout_buflen, ofs, bufsz, index, lite3_type_sizes[
LITE3_TYPE_STRING] + str_size, &val)) < 0)
1590 size_t *__restrict inout_buflen,
1594 const char *__restrict str,
1598 size_t str_size = str_len + 1;
1600 if ((ret = _lite3_set_by_index(buf, inout_buflen, ofs, bufsz, index, lite3_type_sizes[
LITE3_TYPE_STRING] + str_size, &val)) < 0)
1615#ifndef DOXYGEN_IGNORE
1617int 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);
1622 size_t *__restrict inout_buflen,
1626 size_t *__restrict out_ofs)
1629 if ((ret = _lite3_verify_arr_set(buf, inout_buflen, ofs, bufsz)) < 0)
1631 return lite3_arr_set_obj_impl(buf, inout_buflen, ofs, bufsz, index, out_ofs);
1641#ifndef DOXYGEN_IGNORE
1643int 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);
1648 size_t *__restrict inout_buflen,
1652 size_t *__restrict out_ofs)
1655 if ((ret = _lite3_verify_arr_set(buf, inout_buflen, ofs, bufsz)) < 0)
1657 return lite3_arr_set_arr_impl(buf, inout_buflen, ofs, bufsz, index, out_ofs);
1677#ifndef DOXYGEN_IGNORE
1678static inline int _lite3_verify_get(
const unsigned char *buf,
size_t buflen,
size_t ofs)
1682 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: buflen > LITE3_BUF_SIZE_MAX\n");
1687 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: START OFFSET OUT OF BOUNDS\n");
1694static inline int _lite3_verify_obj_get(
const unsigned char *buf,
size_t buflen,
size_t ofs,
const char *__restrict key)
1696 if (_lite3_verify_get(buf, buflen, ofs) < 0)
1699 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: EXPECTING OBJECT TYPE\n");
1703 if (LITE3_UNLIKELY(!key)) {
1704 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: EXPECTING NON-NULL KEY\n");
1711static inline int _lite3_verify_arr_get(
const unsigned char *buf,
size_t buflen,
size_t ofs)
1713 if (_lite3_verify_get(buf, buflen, ofs) < 0)
1716 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: EXPECTING ARRAY TYPE\n");
1724int 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);
1726static inline int _lite3_get_by_index(
const unsigned char *buf,
size_t buflen,
size_t ofs, uint32_t index,
lite3_val **out)
1729 if ((ret = _lite3_verify_arr_get(buf, buflen, ofs)) < 0)
1732 if (LITE3_UNLIKELY(index >= size)) {
1733 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: ARRAY INDEX %u OUT OF BOUNDS (size == %u)\n", index, size);
1737 lite3_key_data key_data = {
1741 return lite3_get_impl(buf, buflen, ofs, NULL, key_data, out);
1756 if (_lite3_verify_get(buf, buflen, 0) < 0)
1772#define lite3_get_type(buf, buflen, ofs, key) ({ \
1773 const char *__lite3_key__ = (key); \
1774 _lite3_get_type_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1776#ifndef DOXYGEN_IGNORE
1777static 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)
1779 if (_lite3_verify_obj_get(buf, buflen, ofs, key) < 0)
1782 if (lite3_get_impl(buf, buflen, ofs, key, key_data, &val) < 0)
1801 if (_lite3_verify_arr_get(buf, buflen, ofs) < 0)
1804 if (_lite3_get_by_index(buf, buflen, ofs, index, &val) < 0)
1824#define lite3_get_type_size(buf, buflen, ofs, key, out) ({ \
1825 const char *__lite3_key__ = (key); \
1826 _lite3_get_type_size_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1828#ifndef DOXYGEN_IGNORE
1829static 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)
1832 if ((ret = _lite3_verify_obj_get(buf, buflen, ofs, key)) < 0)
1835 if ((ret = lite3_get_impl(buf, buflen, ofs, key, key_data, &val)) < 0)
1842 *out = lite3_type_sizes[val->type];
1858#define lite3_exists(buf, buflen, ofs, key) ({ \
1859 const char *__lite3_key__ = (key); \
1860 _lite3_exists_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1862#ifndef DOXYGEN_IGNORE
1863static inline bool _lite3_exists_impl(
const unsigned char *buf,
size_t buflen,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1865 if (_lite3_verify_obj_get(buf, buflen, ofs, key) < 0)
1868 if (lite3_get_impl(buf, buflen, ofs, key, key_data, &val) < 0)
1889 if ((ret = _lite3_verify_get(buf, buflen, ofs)) < 0)
1893 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: EXPECTING ARRAY OR OBJECT TYPE\n");
1912#define lite3_is_null(buf, buflen, ofs, key) ({ \
1913 const char *__lite3_key__ = (key); \
1914 _lite3_is_null_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1916#ifndef DOXYGEN_IGNORE
1917static 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)
1919 if (_lite3_verify_obj_get(buf, buflen, ofs, key) < 0)
1922 if (lite3_get_impl(buf, buflen, ofs, key, key_data, &val) < 0)
1939#define lite3_is_bool(buf, buflen, ofs, key) ({ \
1940 const char *__lite3_key__ = (key); \
1941 _lite3_is_bool_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1943#ifndef DOXYGEN_IGNORE
1944static 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)
1946 if (_lite3_verify_obj_get(buf, buflen, ofs, key) < 0)
1949 if (lite3_get_impl(buf, buflen, ofs, key, key_data, &val) < 0)
1966#define lite3_is_i64(buf, buflen, ofs, key) ({ \
1967 const char *__lite3_key__ = (key); \
1968 _lite3_is_i64_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1970#ifndef DOXYGEN_IGNORE
1971static 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)
1973 if (_lite3_verify_obj_get(buf, buflen, ofs, key) < 0)
1976 if (lite3_get_impl(buf, buflen, ofs, key, key_data, &val) < 0)
1993#define lite3_is_f64(buf, buflen, ofs, key) ({ \
1994 const char *__lite3_key__ = (key); \
1995 _lite3_is_f64_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1997#ifndef DOXYGEN_IGNORE
1998static 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)
2000 if (_lite3_verify_obj_get(buf, buflen, ofs, key) < 0)
2003 if (lite3_get_impl(buf, buflen, ofs, key, key_data, &val) < 0)
2020#define lite3_is_bytes(buf, buflen, ofs, key) ({ \
2021 const char *__lite3_key__ = (key); \
2022 _lite3_is_bytes_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
2024#ifndef DOXYGEN_IGNORE
2025static 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)
2027 if (_lite3_verify_obj_get(buf, buflen, ofs, key) < 0)
2030 if (lite3_get_impl(buf, buflen, ofs, key, key_data, &val) < 0)
2047#define lite3_is_str(buf, buflen, ofs, key) ({ \
2048 const char *__lite3_key__ = (key); \
2049 _lite3_is_str_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
2051#ifndef DOXYGEN_IGNORE
2052static 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)
2054 if (_lite3_verify_obj_get(buf, buflen, ofs, key) < 0)
2057 if (lite3_get_impl(buf, buflen, ofs, key, key_data, &val) < 0)
2074#define lite3_is_obj(buf, buflen, ofs, key) ({ \
2075 const char *__lite3_key__ = (key); \
2076 _lite3_is_obj_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
2078#ifndef DOXYGEN_IGNORE
2079static 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)
2081 if (_lite3_verify_obj_get(buf, buflen, ofs, key) < 0)
2084 if (lite3_get_impl(buf, buflen, ofs, key, key_data, &val) < 0)
2101#define lite3_is_arr(buf, buflen, ofs, key) ({ \
2102 const char *__lite3_key__ = (key); \
2103 _lite3_is_arr_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
2105#ifndef DOXYGEN_IGNORE
2106static 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)
2108 if (_lite3_verify_obj_get(buf, buflen, ofs, key) < 0)
2111 if (lite3_get_impl(buf, buflen, ofs, key, key_data, &val) < 0)
2154#define lite3_get(buf, buflen, ofs, key, out) ({ \
2155 const unsigned char *__lite3_buf__ = (buf); \
2156 size_t __lite3_buflen__ = (buflen); \
2157 size_t __lite3_ofs__ = (ofs); \
2158 int __lite3_ret__; \
2159 if ((__lite3_ret__ = _lite3_verify_get(__lite3_buf__, __lite3_buflen__, __lite3_ofs__) < 0)) \
2160 return __lite3_ret__; \
2161 const char *__lite3_key__ = (key); \
2162 lite3_get_impl(__lite3_buf__, __lite3_buflen__, __lite3_ofs__, __lite3_key__, LITE3_KEY_DATA(key), out); \
2177#define lite3_get_bool(buf, buflen, ofs, key, out) ({ \
2178 const char *__lite3_key__ = (key); \
2179 _lite3_get_bool_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
2181#ifndef DOXYGEN_IGNORE
2182static 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)
2185 if ((ret = _lite3_verify_obj_get(buf, buflen, ofs, key)) < 0)
2188 if ((ret = lite3_get_impl(buf, buflen, ofs, key, key_data, &val)) < 0)
2191 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_BOOL\n");
2195 *out = (bool)(*(val->val));
2212#define lite3_get_i64(buf, buflen, ofs, key, out) ({ \
2213 const char *__lite3_key__ = (key); \
2214 _lite3_get_i64_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
2216#ifndef DOXYGEN_IGNORE
2217static 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)
2220 if ((ret = _lite3_verify_obj_get(buf, buflen, ofs, key)) < 0)
2223 if ((ret = lite3_get_impl(buf, buflen, ofs, key, key_data, &val)) < 0)
2226 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_I64\n");
2247#define lite3_get_f64(buf, buflen, ofs, key, out) ({ \
2248 const char *__lite3_key__ = (key); \
2249 _lite3_get_f64_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
2251#ifndef DOXYGEN_IGNORE
2252static 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)
2255 if ((ret = _lite3_verify_obj_get(buf, buflen, ofs, key)) < 0)
2258 if ((ret = lite3_get_impl(buf, buflen, ofs, key, key_data, &val)) < 0)
2261 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_F64\n");
2282#define lite3_get_bytes(buf, buflen, ofs, key, out) ({ \
2283 const char *__lite3_key__ = (key); \
2284 _lite3_get_bytes_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
2286#ifndef DOXYGEN_IGNORE
2287static 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)
2290 if ((ret = _lite3_verify_obj_get(buf, buflen, ofs, key)) < 0)
2293 if ((ret = lite3_get_impl(buf, buflen, ofs, key, key_data, &val)) < 0)
2296 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_BYTES\n");
2301 .
gen = *(uint32_t *)buf,
2322#define lite3_get_str(buf, buflen, ofs, key, out) ({ \
2323 const char *__lite3_key__ = (key); \
2324 _lite3_get_str_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
2326#ifndef DOXYGEN_IGNORE
2327static 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)
2330 if ((ret = _lite3_verify_obj_get(buf, buflen, ofs, key)) < 0)
2333 if ((ret = lite3_get_impl(buf, buflen, ofs, key, key_data, &val)) < 0)
2336 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_STRING\n");
2341 .
gen = *(uint32_t *)buf,
2363#define lite3_get_obj(buf, buflen, ofs, key, out) ({ \
2364 const char *__lite3_key__ = (key); \
2365 _lite3_get_obj_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
2367#ifndef DOXYGEN_IGNORE
2368static 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)
2371 if ((ret = _lite3_verify_obj_get(buf, buflen, ofs, key)) < 0)
2374 if ((ret = lite3_get_impl(buf, buflen, ofs, key, key_data, &val)) < 0)
2377 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_OBJECT\n");
2381 *out_ofs = (size_t)((uint8_t *)val - buf);
2398#define lite3_get_arr(buf, buflen, ofs, key, out) ({ \
2399 const char *__lite3_key__ = (key); \
2400 _lite3_get_arr_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
2402#ifndef DOXYGEN_IGNORE
2403static 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)
2406 if ((ret = _lite3_verify_obj_get(buf, buflen, ofs, key)) < 0)
2409 if ((ret = lite3_get_impl(buf, buflen, ofs, key, key_data, &val)) < 0)
2412 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_ARRAY\n");
2416 *out_ofs = (size_t)((uint8_t *)val - buf);
2449 const unsigned char *buf,
2457 if ((ret = _lite3_get_by_index(buf, buflen, ofs, index, &val)) < 0)
2460 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_BOOL\n");
2464 *out = (bool)(*(val->val));
2475 const unsigned char *buf,
2483 if ((ret = _lite3_get_by_index(buf, buflen, ofs, index, &val)) < 0)
2486 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_I64\n");
2501 const unsigned char *buf,
2509 if ((ret = _lite3_get_by_index(buf, buflen, ofs, index, &val)) < 0)
2512 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_F64\n");
2527 const unsigned char *buf,
2535 if ((ret = _lite3_get_by_index(buf, buflen, ofs, index, &val)) < 0)
2538 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_BYTES\n");
2543 .
gen = *(uint32_t *)buf,
2558 const unsigned char *buf,
2566 if ((ret = _lite3_get_by_index(buf, buflen, ofs, index, &val)) < 0)
2569 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_STRING\n");
2574 .
gen = *(uint32_t *)buf,
2590 const unsigned char *buf,
2594 size_t *__restrict out_ofs)
2598 if ((ret = _lite3_get_by_index(buf, buflen, ofs, index, &val)) < 0)
2601 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_OBJECT\n");
2605 *out_ofs = (size_t)((uint8_t *)val - buf);
2616 const unsigned char *buf,
2620 size_t *__restrict out_ofs)
2624 if ((ret = _lite3_get_by_index(buf, buflen, ofs, index, &val)) < 0)
2627 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_ARRAY\n");
2631 *out_ofs = (size_t)((uint8_t *)val - buf);
2654#define LITE3_ITER_ITEM 1
2656#define LITE3_ITER_DONE 0
2671#ifndef DOXYGEN_IGNORE
2673int lite3_iter_create_impl(
const unsigned char *buf,
size_t buflen,
size_t ofs,
lite3_iter *out);
2683 const unsigned char *buf,
2689 if ((ret = _lite3_verify_get(buf, buflen, ofs)) < 0)
2691 return lite3_iter_create_impl(buf, buflen, ofs, out);
2711 const unsigned char *buf,
2755 return lite3_type_sizes[val->type];
2767static inline bool lite3_val_bool(
lite3_val *val)
2769 return *(
bool *)(val->val);
2772static inline int64_t lite3_val_i64(
lite3_val *val)
2775 memcpy(&tmp, val->val,
sizeof(tmp));
2779static inline double lite3_val_f64(
lite3_val *val)
2782 memcpy(&tmp, val->val,
sizeof(tmp));
2786static inline const char *lite3_val_str(
lite3_val *val)
2788 return (
const char *)val->val + LITE3_STR_LEN_SIZE;
2798 memcpy(out_len, val->val, LITE3_STR_LEN_SIZE);
2800 return (
const char *)val->val + LITE3_STR_LEN_SIZE;
2803static inline const unsigned char *lite3_val_bytes(
lite3_val *val,
size_t *out_len)
2806 memcpy(out_len, val->val, LITE3_BYTES_LEN_SIZE);
2807 return (
const unsigned char *)val->val + LITE3_BYTES_LEN_SIZE;
2851#if defined(DOXYGEN_ONLY) && !defined(LITE3_JSON)
2866#define LITE3_JSON_NESTING_DEPTH_MAX 32
2881 size_t *__restrict out_buflen,
2883 const char *__restrict json_str,
2900 size_t *__restrict out_buflen,
2902 const char *__restrict path
2918 size_t *__restrict out_buflen,
2937 const unsigned char *buf,
2956 const unsigned char *buf,
2959 size_t *__restrict out_len
2978 const unsigned char *buf,
2981 size_t *__restrict out_len
2995 const unsigned char *buf,
2998 char *__restrict json_buf,
3015 const unsigned char *buf,
3018 char *__restrict json_buf,
3022static 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)
3024 (void)buf; (void)out_buflen; (void)bufsz; (void)json_str; (void)json_len;
3028static inline int lite3_json_dec_file(
unsigned char *buf,
size_t *__restrict out_buflen,
size_t bufsz,
const char *__restrict path)
3030 (void)buf; (void)out_buflen; (void)bufsz; (void)path;
3034static inline int lite3_json_dec_fp(
unsigned char *buf,
size_t *__restrict out_buflen,
size_t bufsz, FILE *fp)
3036 (void)buf; (void)out_buflen; (void)bufsz; (void)fp;
3040static inline int lite3_json_print(
const unsigned char *buf,
size_t buflen,
size_t ofs)
3042 (void)buf; (void)buflen; (void)ofs;
3046static inline char *
lite3_json_enc(
const unsigned char *buf,
size_t buflen,
size_t ofs,
size_t *out_len)
3048 (void)buf; (void)buflen; (void)ofs; (void)out_len;
3052static inline char *
lite3_json_enc_pretty(
const unsigned char *buf,
size_t buflen,
size_t ofs,
size_t *out_len)
3054 (void)buf; (void)buflen; (void)ofs; (void)out_len;
3058static 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)
3060 (void)buf; (void)buflen; (void)ofs; (void)json_buf; (void)json_bufsz;
3064static 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)
3066 (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 enum lite3_type lite3_get_root_type(const unsigned char *buf, size_t buflen)
Get the root type of a Lite³ buffer.
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 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.
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.