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)
369#ifndef LITE3_HASH_PROBE_MAX
370#define LITE3_HASH_PROBE_MAX 128U
373#if LITE3_HASH_PROBE_MAX < 2
374 #error "LITE3_HASH_PROBE_MAX must be >= 2"
377#define LITE3_VERIFY_KEY_OK 0
378#define LITE3_VERIFY_KEY_HASH_COLLISION 1
393#define LITE3_KEY_HASH_COMPILE_TIME
395#ifndef DOXYGEN_IGNORE
396#ifdef LITE3_KEY_HASH_COMPILE_TIME
397#define LITE3_MOD 4294967296ULL
399#define LITE3_P0 33ULL
400#define LITE3_P1 ((LITE3_P0 * LITE3_P0) % LITE3_MOD)
401#define LITE3_P2 ((LITE3_P1 * LITE3_P1) % LITE3_MOD)
402#define LITE3_P3 ((LITE3_P2 * LITE3_P2) % LITE3_MOD)
403#define LITE3_P4 ((LITE3_P3 * LITE3_P3) % LITE3_MOD)
404#define LITE3_P5 ((LITE3_P4 * LITE3_P4) % LITE3_MOD)
406#define LITE3_BIT0(k) (((k) >> 0) & 1ULL)
407#define LITE3_BIT1(k) (((k) >> 1) & 1ULL)
408#define LITE3_BIT2(k) (((k) >> 2) & 1ULL)
409#define LITE3_BIT3(k) (((k) >> 3) & 1ULL)
410#define LITE3_BIT4(k) (((k) >> 4) & 1ULL)
411#define LITE3_BIT5(k) (((k) >> 5) & 1ULL)
413#define LITE3_POW33(k) \
415 * (LITE3_BIT0(k) ? LITE3_P0 : 1ULL) \
417 * (LITE3_BIT1(k) ? LITE3_P1 : 1ULL) \
419 * (LITE3_BIT2(k) ? LITE3_P2 : 1ULL) \
421 * (LITE3_BIT3(k) ? LITE3_P3 : 1ULL) \
423 * (LITE3_BIT4(k) ? LITE3_P4 : 1ULL) \
425 * (LITE3_BIT5(k) ? LITE3_P5 : 1ULL) \
428#define LITE3_SUM_1(s, i, p) \
429 ((unsigned long long)(unsigned char)(s)[(i)] * LITE3_POW33((p)))
431#define LITE3_SUM_2(s, i, p) \
432 (LITE3_SUM_1(s, (i), (p)) + LITE3_SUM_1(s, (i) + 1, (p) - 1))
434#define LITE3_SUM_4(s, i, p) \
435 (LITE3_SUM_2(s, (i), (p)) + LITE3_SUM_2(s, (i) + 2, (p) - 2))
437#define LITE3_SUM_8(s, i, p) \
438 (LITE3_SUM_4(s, (i), (p)) + LITE3_SUM_4(s, (i) + 4, (p) - 4))
440#define LITE3_SUM_16(s, i, p) \
441 (LITE3_SUM_8(s, (i), (p)) + LITE3_SUM_8(s, (i) + 8, (p) - 8))
443#define LITE3_SUM_32(s, i, p) \
444 (LITE3_SUM_16(s, (i), (p)) + LITE3_SUM_16(s, (i) + 16, (p) - 16))
446#define LITE3_OFFSET_BIT0(len) \
447 (((len & 2ULL) ? 2ULL : 0ULL) + \
448 ((len & 4ULL) ? 4ULL : 0ULL) + \
449 ((len & 8ULL) ? 8ULL : 0ULL) + \
450 ((len & 16ULL) ? 16ULL : 0ULL) + \
451 ((len & 32ULL) ? 32ULL : 0ULL))
453#define LITE3_OFFSET_BIT1(len) \
454 (((len & 4ULL) ? 4ULL : 0ULL) + \
455 ((len & 8ULL) ? 8ULL : 0ULL) + \
456 ((len & 16ULL) ? 16ULL : 0ULL) + \
457 ((len & 32ULL) ? 32ULL : 0ULL))
459#define LITE3_OFFSET_BIT2(len) \
460 (((len & 8ULL) ? 8ULL : 0ULL) + \
461 ((len & 16ULL) ? 16ULL : 0ULL) + \
462 ((len & 32ULL) ? 32ULL : 0ULL))
464#define LITE3_OFFSET_BIT3(len) \
465 (((len & 16ULL) ? 16ULL : 0ULL) + \
466 ((len & 32ULL) ? 32ULL : 0ULL))
468#define LITE3_OFFSET_BIT4(len) \
469 (((len & 32ULL) ? 32ULL : 0ULL))
471#define LITE3_OFFSET_BIT5(len) \
474#define LITE3_POLY_HASH(s, len) \
475 ((len) & 1ULL ? LITE3_SUM_1(s, LITE3_OFFSET_BIT0((len)), (len) - 1 - LITE3_OFFSET_BIT0((len))) : 0ULL) + \
476 ((len) & 2ULL ? LITE3_SUM_2(s, LITE3_OFFSET_BIT1((len)), (len) - 1 - LITE3_OFFSET_BIT1((len))) : 0ULL) + \
477 ((len) & 4ULL ? LITE3_SUM_4(s, LITE3_OFFSET_BIT2((len)), (len) - 1 - LITE3_OFFSET_BIT2((len))) : 0ULL) + \
478 ((len) & 8ULL ? LITE3_SUM_8(s, LITE3_OFFSET_BIT3((len)), (len) - 1 - LITE3_OFFSET_BIT3((len))) : 0ULL) + \
479 ((len) & 16ULL ? LITE3_SUM_16(s, LITE3_OFFSET_BIT4((len)), (len) - 1 - LITE3_OFFSET_BIT4((len))) : 0ULL) + \
480 ((len) & 32ULL ? LITE3_SUM_32(s, LITE3_OFFSET_BIT5((len)), (len) - 1 - LITE3_OFFSET_BIT5((len))) : 0ULL)
482#define LITE3_STRLEN(s) (sizeof(s) - 1)
489static inline lite3_key_data lite3_get_key_data(
const char *key) {
490 lite3_key_data key_data;
491 const char *key_cursor = key;
492 key_data.hash = LITE3_DJB2_HASH_SEED;
494 key_data.hash = ((key_data.hash << 5) + key_data.hash) + (uint8_t)(*key_cursor++);
495 key_data.size = (uint32_t)(key_cursor - key) + 1;
499#define LITE3_KEY_DATA(s) ( \
500 __builtin_constant_p(s) ? \
501 ((LITE3_STRLEN(s) < 64) ? \
503 .hash = (uint32_t)((LITE3_POLY_HASH(s, LITE3_STRLEN(s)) + LITE3_DJB2_HASH_SEED * LITE3_POW33(LITE3_STRLEN(s))) % LITE3_MOD), \
504 .size = (sizeof(s)), \
506 : lite3_get_key_data(s)) \
507 : lite3_get_key_data(__lite3_key__) \
510#define LITE3_KEY_DATA(s) lite3_get_key_data(__lite3_key__)
554#ifndef DOXYGEN_IGNORE
555#define LITE3_VAL_SIZE sizeof(lite3_val)
557static_assert(LITE3_VAL_SIZE <=
sizeof(size_t),
"LITE3_VAL_SIZE must be <= sizeof(size_t)");
559#ifndef DOXYGEN_IGNORE
560static const size_t lite3_type_sizes[] = {
572static_assert((
sizeof(lite3_type_sizes) /
sizeof(
size_t)) ==
LITE3_TYPE_COUNT,
"lite3_type_sizes[] element count != LITE3_TYPE_COUNT");
573static_assert(4 <=
sizeof(size_t),
"lite3_type_sizes[LITE3_TYPE_BYTES] and lite3_type_sizes[LITE3_TYPE_STRING] must fit inside size_t");
596#ifndef DOXYGEN_IGNORE
597#define LITE3_BYTES_LEN_SIZE sizeof(((lite3_bytes *)0)->len)
599static_assert(LITE3_BYTES_LEN_SIZE <=
sizeof(size_t),
"lite3_val_bytes() expects LITE3_BYTES_LEN_SIZE to be <= sizeof(size_t)");
625#ifndef DOXYGEN_IGNORE
626#define LITE3_STR_LEN_SIZE sizeof(((lite3_str *)0)->len)
628static_assert(LITE3_STR_LEN_SIZE <=
sizeof(size_t),
"lite3_val_str() expects LITE3_STR_LEN_SIZE to be <= sizeof(size_t)");
655#define LITE3_BYTES(buf, val) (const unsigned char *)_lite3_ptr_suppress_nonnull_warning( \
656 (uint32_t)(val).gen == *(uint32_t *)(buf) ? (val).ptr : NULL \
684#define LITE3_STR(buf, val) (const char *)_lite3_ptr_suppress_nonnull_warning( \
685 (uint32_t)(val).gen == *(uint32_t *)(buf) ? (val).ptr : NULL \
688#ifndef DOXYGEN_IGNORE
689static inline __attribute__((always_inline))
const void *_lite3_ptr_suppress_nonnull_warning(
const void *p) {
return p; }
720 size_t *__restrict out_buflen,
738 size_t *__restrict out_buflen,
775#ifndef DOXYGEN_IGNORE
776static inline int _lite3_verify_set(
unsigned char *buf,
size_t *__restrict inout_buflen,
size_t ofs,
size_t bufsz)
780 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: bufsz > LITE3_BUF_SIZE_MAX\n");
784 if (LITE3_UNLIKELY(*inout_buflen > bufsz)) {
785 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: inout_buflen > bufsz\n");
790 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: START OFFSET OUT OF BOUNDS\n");
797static inline int _lite3_verify_obj_set(
unsigned char *buf,
size_t *__restrict inout_buflen,
size_t ofs,
size_t bufsz)
800 if ((ret = _lite3_verify_set(buf, inout_buflen, ofs, bufsz)) < 0)
803 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: EXPECTING OBJECT TYPE\n");
810static inline int _lite3_verify_arr_set(
unsigned char *buf,
size_t *__restrict inout_buflen,
size_t ofs,
size_t bufsz)
813 if ((ret = _lite3_verify_set(buf, inout_buflen, ofs, bufsz)) < 0)
816 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: EXPECTING ARRAY TYPE\n");
824int 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);
839#define lite3_set_null(buf, inout_buflen, ofs, bufsz, key) ({ \
840 const char *__lite3_key__ = (key); \
841 _lite3_set_null_impl(buf, inout_buflen, ofs, bufsz, __lite3_key__, LITE3_KEY_DATA(key)); \
843#ifndef DOXYGEN_IGNORE
844static 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)
847 if ((ret = _lite3_verify_obj_set(buf, inout_buflen, ofs, bufsz)) < 0)
850 if ((ret = lite3_set_impl(buf, inout_buflen, ofs, bufsz, key, key_data, lite3_type_sizes[
LITE3_TYPE_NULL], &val)) < 0)
870#define lite3_set_bool(buf, inout_buflen, ofs, bufsz, key, value) ({ \
871 const char *__lite3_key__ = (key); \
872 _lite3_set_bool_impl(buf, inout_buflen, ofs, bufsz, __lite3_key__, LITE3_KEY_DATA(key), value); \
874#ifndef DOXYGEN_IGNORE
875static 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)
878 if ((ret = _lite3_verify_obj_set(buf, inout_buflen, ofs, bufsz)) < 0)
881 if ((ret = lite3_set_impl(buf, inout_buflen, ofs, bufsz, key, key_data, lite3_type_sizes[
LITE3_TYPE_BOOL], &val)) < 0)
902#define lite3_set_i64(buf, inout_buflen, ofs, bufsz, key, value) ({ \
903 const char *__lite3_key__ = (key); \
904 _lite3_set_i64_impl(buf, inout_buflen, ofs, bufsz, __lite3_key__, LITE3_KEY_DATA(key), value); \
906#ifndef DOXYGEN_IGNORE
907static 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)
910 if ((ret = _lite3_verify_obj_set(buf, inout_buflen, ofs, bufsz)) < 0)
913 if ((ret = lite3_set_impl(buf, inout_buflen, ofs, bufsz, key, key_data, lite3_type_sizes[
LITE3_TYPE_I64], &val)) < 0)
934#define lite3_set_f64(buf, inout_buflen, ofs, bufsz, key, value) ({ \
935 const char *__lite3_key__ = (key); \
936 _lite3_set_f64_impl(buf, inout_buflen, ofs, bufsz, __lite3_key__, LITE3_KEY_DATA(key), value); \
938#ifndef DOXYGEN_IGNORE
939static 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)
942 if ((ret = _lite3_verify_obj_set(buf, inout_buflen, ofs, bufsz)) < 0)
945 if ((ret = lite3_set_impl(buf, inout_buflen, ofs, bufsz, key, key_data, lite3_type_sizes[
LITE3_TYPE_F64], &val)) < 0)
967#define lite3_set_bytes(buf, inout_buflen, ofs, bufsz, key, bytes, bytes_len) ({ \
968 const char *__lite3_key__ = (key); \
969 _lite3_set_bytes_impl(buf, inout_buflen, ofs, bufsz, __lite3_key__, LITE3_KEY_DATA(key), bytes, bytes_len); \
971#ifndef DOXYGEN_IGNORE
972static 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)
975 if ((ret = _lite3_verify_obj_set(buf, inout_buflen, ofs, bufsz)) < 0)
978 if ((ret = lite3_set_impl(buf, inout_buflen, ofs, bufsz, key, key_data, lite3_type_sizes[
LITE3_TYPE_BYTES] + bytes_len, &val)) < 0)
1004#define lite3_set_str(buf, inout_buflen, ofs, bufsz, key, str) ({ \
1005 const char *__lite3_key__ = (key); \
1006 _lite3_set_str_impl(buf, inout_buflen, ofs, bufsz, __lite3_key__, LITE3_KEY_DATA(key), str); \
1008#ifndef DOXYGEN_IGNORE
1009static 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)
1012 if ((ret = _lite3_verify_obj_set(buf, inout_buflen, ofs, bufsz)) < 0)
1015 size_t str_size = strlen(str) + 1;
1016 if ((ret = lite3_set_impl(buf, inout_buflen, ofs, bufsz, key, key_data, lite3_type_sizes[
LITE3_TYPE_STRING] + str_size, &val)) < 0)
1042#define lite3_set_str_n(buf, inout_buflen, ofs, bufsz, key, str, str_len) ({ \
1043 const char *__lite3_key__ = (key); \
1044 _lite3_set_str_n_impl(buf, inout_buflen, ofs, bufsz, __lite3_key__, LITE3_KEY_DATA(key), str, str_len); \
1046#ifndef DOXYGEN_IGNORE
1047static 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)
1050 if ((ret = _lite3_verify_obj_set(buf, inout_buflen, ofs, bufsz)) < 0)
1053 size_t str_size = str_len + 1;
1054 if ((ret = lite3_set_impl(buf, inout_buflen, ofs, bufsz, key, key_data, lite3_type_sizes[
LITE3_TYPE_STRING] + str_size, &val)) < 0)
1077#define lite3_set_obj(buf, inout_buflen, ofs, bufsz, key, out_ofs) ({ \
1078 unsigned char *__lite3_buf__ = (buf); \
1079 size_t *__lite3_inout_buflen__ = (inout_buflen); \
1080 size_t __lite3_ofs__ = (ofs); \
1081 size_t __lite3_bufsz__ = (bufsz); \
1082 int __lite3_ret__; \
1083 if ((__lite3_ret__ = _lite3_verify_obj_set( \
1085 __lite3_inout_buflen__, \
1087 __lite3_bufsz__)) < 0) \
1088 return __lite3_ret__; \
1090 const char *__lite3_key__ = (key); \
1091 lite3_set_obj_impl( \
1093 __lite3_inout_buflen__, \
1097 LITE3_KEY_DATA(key), \
1100#ifndef DOXYGEN_IGNORE
1102int 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);
1118#define lite3_set_arr(buf, inout_buflen, ofs, bufsz, key, out_ofs) ({ \
1119 unsigned char *__lite3_buf__ = (buf); \
1120 size_t *__lite3_inout_buflen__ = (inout_buflen); \
1121 size_t __lite3_ofs__ = (ofs); \
1122 size_t __lite3_bufsz__ = (bufsz); \
1123 int __lite3_ret__; \
1124 if ((__lite3_ret__ = _lite3_verify_obj_set( \
1126 __lite3_inout_buflen__, \
1128 __lite3_bufsz__)) < 0) \
1129 return __lite3_ret__; \
1131 const char *__lite3_key__ = (key); \
1132 lite3_set_arr_impl( \
1134 __lite3_inout_buflen__, \
1138 LITE3_KEY_DATA(key), \
1141#ifndef DOXYGEN_IGNORE
1143int 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);
1173#ifndef DOXYGEN_IGNORE
1174static 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)
1177 if ((ret = _lite3_verify_arr_set(buf, inout_buflen, ofs, bufsz)) < 0)
1180 if (LITE3_UNLIKELY(index > size)) {
1181 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: ARRAY INDEX %u OUT OF BOUNDS (size == %u)\n", index, size);
1185 lite3_key_data key_data = {
1189 return lite3_set_impl(buf, inout_buflen, ofs, bufsz, NULL, key_data, val_len, out);
1192static 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)
1195 if ((ret = _lite3_verify_arr_set(buf, inout_buflen, ofs, bufsz)) < 0)
1198 lite3_key_data key_data = {
1202 return lite3_set_impl(buf, inout_buflen, ofs, bufsz, NULL, key_data, val_len, out);
1214 size_t *__restrict inout_buflen,
1220 if ((ret = _lite3_set_by_append(buf, inout_buflen, ofs, bufsz, lite3_type_sizes[
LITE3_TYPE_NULL], &val)) < 0)
1234 size_t *__restrict inout_buflen,
1241 if ((ret = _lite3_set_by_append(buf, inout_buflen, ofs, bufsz, lite3_type_sizes[
LITE3_TYPE_BOOL], &val)) < 0)
1256 size_t *__restrict inout_buflen,
1263 if ((ret = _lite3_set_by_append(buf, inout_buflen, ofs, bufsz, lite3_type_sizes[
LITE3_TYPE_I64], &val)) < 0)
1278 size_t *__restrict inout_buflen,
1285 if ((ret = _lite3_set_by_append(buf, inout_buflen, ofs, bufsz, lite3_type_sizes[
LITE3_TYPE_F64], &val)) < 0)
1300 size_t *__restrict inout_buflen,
1303 const unsigned char *__restrict bytes,
1308 if ((ret = _lite3_set_by_append(buf, inout_buflen, ofs, bufsz, lite3_type_sizes[
LITE3_TYPE_BYTES] + bytes_len, &val)) < 0)
1328 size_t *__restrict inout_buflen,
1331 const char *__restrict str)
1334 size_t str_size = strlen(str) + 1;
1336 if ((ret = _lite3_set_by_append(buf, inout_buflen, ofs, bufsz, lite3_type_sizes[
LITE3_TYPE_STRING] + str_size, &val)) < 0)
1355 size_t *__restrict inout_buflen,
1358 const char *__restrict str,
1362 size_t str_size = str_len + 1;
1364 if ((ret = _lite3_set_by_append(buf, inout_buflen, ofs, bufsz, lite3_type_sizes[
LITE3_TYPE_STRING] + str_size, &val)) < 0)
1379#ifndef DOXYGEN_IGNORE
1381int lite3_arr_append_obj_impl(
unsigned char *buf,
size_t *__restrict inout_buflen,
size_t ofs,
size_t bufsz,
size_t *__restrict out_ofs);
1386 size_t *__restrict inout_buflen,
1389 size_t *__restrict out_ofs)
1392 if ((ret = _lite3_verify_arr_set(buf, inout_buflen, ofs, bufsz)) < 0)
1394 return lite3_arr_append_obj_impl(buf, inout_buflen, ofs, bufsz, out_ofs);
1404#ifndef DOXYGEN_IGNORE
1406int lite3_arr_append_arr_impl(
unsigned char *buf,
size_t *__restrict inout_buflen,
size_t ofs,
size_t bufsz,
size_t *__restrict out_ofs);
1411 size_t *__restrict inout_buflen,
1414 size_t *__restrict out_ofs)
1417 if ((ret = _lite3_verify_arr_set(buf, inout_buflen, ofs, bufsz)) < 0)
1419 return lite3_arr_append_arr_impl(buf, inout_buflen, ofs, bufsz, out_ofs);
1464 size_t *__restrict inout_buflen,
1471 if ((ret = _lite3_set_by_index(buf, inout_buflen, ofs, bufsz, index, lite3_type_sizes[
LITE3_TYPE_NULL], &val)) < 0)
1485 size_t *__restrict inout_buflen,
1493 if ((ret = _lite3_set_by_index(buf, inout_buflen, ofs, bufsz, index, lite3_type_sizes[
LITE3_TYPE_BOOL], &val)) < 0)
1508 size_t *__restrict inout_buflen,
1516 if ((ret = _lite3_set_by_index(buf, inout_buflen, ofs, bufsz, index, lite3_type_sizes[
LITE3_TYPE_I64], &val)) < 0)
1531 size_t *__restrict inout_buflen,
1539 if ((ret = _lite3_set_by_index(buf, inout_buflen, ofs, bufsz, index, lite3_type_sizes[
LITE3_TYPE_F64], &val)) < 0)
1554 size_t *__restrict inout_buflen,
1558 const unsigned char *__restrict bytes,
1563 if ((ret = _lite3_set_by_index(buf, inout_buflen, ofs, bufsz, index, lite3_type_sizes[
LITE3_TYPE_BYTES] + bytes_len, &val)) < 0)
1583 size_t *__restrict inout_buflen,
1587 const char *__restrict str)
1590 size_t str_size = strlen(str) + 1;
1592 if ((ret = _lite3_set_by_index(buf, inout_buflen, ofs, bufsz, index, lite3_type_sizes[
LITE3_TYPE_STRING] + str_size, &val)) < 0)
1611 size_t *__restrict inout_buflen,
1615 const char *__restrict str,
1619 size_t str_size = str_len + 1;
1621 if ((ret = _lite3_set_by_index(buf, inout_buflen, ofs, bufsz, index, lite3_type_sizes[
LITE3_TYPE_STRING] + str_size, &val)) < 0)
1636#ifndef DOXYGEN_IGNORE
1638int 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);
1643 size_t *__restrict inout_buflen,
1647 size_t *__restrict out_ofs)
1650 if ((ret = _lite3_verify_arr_set(buf, inout_buflen, ofs, bufsz)) < 0)
1652 return lite3_arr_set_obj_impl(buf, inout_buflen, ofs, bufsz, index, out_ofs);
1662#ifndef DOXYGEN_IGNORE
1664int 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);
1669 size_t *__restrict inout_buflen,
1673 size_t *__restrict out_ofs)
1676 if ((ret = _lite3_verify_arr_set(buf, inout_buflen, ofs, bufsz)) < 0)
1678 return lite3_arr_set_arr_impl(buf, inout_buflen, ofs, bufsz, index, out_ofs);
1698#ifndef DOXYGEN_IGNORE
1699static inline int _lite3_verify_get(
const unsigned char *buf,
size_t buflen,
size_t ofs)
1703 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: buflen > LITE3_BUF_SIZE_MAX\n");
1708 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: START OFFSET OUT OF BOUNDS\n");
1715static inline int _lite3_verify_obj_get(
const unsigned char *buf,
size_t buflen,
size_t ofs)
1717 if (_lite3_verify_get(buf, buflen, ofs) < 0)
1720 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: EXPECTING OBJECT TYPE\n");
1727static inline int _lite3_verify_arr_get(
const unsigned char *buf,
size_t buflen,
size_t ofs)
1729 if (_lite3_verify_get(buf, buflen, ofs) < 0)
1732 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: EXPECTING ARRAY TYPE\n");
1740int 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);
1742static inline int _lite3_get_by_index(
const unsigned char *buf,
size_t buflen,
size_t ofs, uint32_t index,
lite3_val **out)
1745 if ((ret = _lite3_verify_arr_get(buf, buflen, ofs)) < 0)
1748 if (LITE3_UNLIKELY(index >= size)) {
1749 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: ARRAY INDEX %u OUT OF BOUNDS (size == %u)\n", index, size);
1753 lite3_key_data key_data = {
1757 return lite3_get_impl(buf, buflen, ofs, NULL, key_data, out);
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) < 0)
1782 if (lite3_get_impl(buf, buflen, ofs, key, key_data, &val) < 0)
1804#define lite3_get_type_size(buf, buflen, ofs, key, out) ({ \
1805 const char *__lite3_key__ = (key); \
1806 _lite3_get_type_size_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
1808#ifndef DOXYGEN_IGNORE
1809static 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)
1812 if ((ret = _lite3_verify_obj_get(buf, buflen, ofs)) < 0)
1815 if ((ret = lite3_get_impl(buf, buflen, ofs, key, key_data, &val)) < 0)
1822 *out = lite3_type_sizes[val->type];
1838#define lite3_exists(buf, buflen, ofs, key) ({ \
1839 const char *__lite3_key__ = (key); \
1840 _lite3_exists_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1842#ifndef DOXYGEN_IGNORE
1843static inline bool _lite3_exists_impl(
const unsigned char *buf,
size_t buflen,
size_t ofs,
const char *__restrict key, lite3_key_data key_data)
1845 if (_lite3_verify_obj_get(buf, buflen, ofs) < 0)
1848 if (lite3_get_impl(buf, buflen, ofs, key, key_data, &val) < 0)
1869 if ((ret = _lite3_verify_get(buf, buflen, ofs)) < 0)
1873 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: EXPECTING ARRAY OR OBJECT TYPE\n");
1892#define lite3_is_null(buf, buflen, ofs, key) ({ \
1893 const char *__lite3_key__ = (key); \
1894 _lite3_is_null_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1896#ifndef DOXYGEN_IGNORE
1897static 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)
1899 if (_lite3_verify_obj_get(buf, buflen, ofs) < 0)
1902 if (lite3_get_impl(buf, buflen, ofs, key, key_data, &val) < 0)
1919#define lite3_is_bool(buf, buflen, ofs, key) ({ \
1920 const char *__lite3_key__ = (key); \
1921 _lite3_is_bool_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1923#ifndef DOXYGEN_IGNORE
1924static 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)
1926 if (_lite3_verify_obj_get(buf, buflen, ofs) < 0)
1929 if (lite3_get_impl(buf, buflen, ofs, key, key_data, &val) < 0)
1946#define lite3_is_i64(buf, buflen, ofs, key) ({ \
1947 const char *__lite3_key__ = (key); \
1948 _lite3_is_i64_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1950#ifndef DOXYGEN_IGNORE
1951static 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)
1953 if (_lite3_verify_obj_get(buf, buflen, ofs) < 0)
1956 if (lite3_get_impl(buf, buflen, ofs, key, key_data, &val) < 0)
1973#define lite3_is_f64(buf, buflen, ofs, key) ({ \
1974 const char *__lite3_key__ = (key); \
1975 _lite3_is_f64_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
1977#ifndef DOXYGEN_IGNORE
1978static 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)
1980 if (_lite3_verify_obj_get(buf, buflen, ofs) < 0)
1983 if (lite3_get_impl(buf, buflen, ofs, key, key_data, &val) < 0)
2000#define lite3_is_bytes(buf, buflen, ofs, key) ({ \
2001 const char *__lite3_key__ = (key); \
2002 _lite3_is_bytes_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
2004#ifndef DOXYGEN_IGNORE
2005static 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)
2007 if (_lite3_verify_obj_get(buf, buflen, ofs) < 0)
2010 if (lite3_get_impl(buf, buflen, ofs, key, key_data, &val) < 0)
2027#define lite3_is_str(buf, buflen, ofs, key) ({ \
2028 const char *__lite3_key__ = (key); \
2029 _lite3_is_str_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
2031#ifndef DOXYGEN_IGNORE
2032static 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)
2034 if (_lite3_verify_obj_get(buf, buflen, ofs) < 0)
2037 if (lite3_get_impl(buf, buflen, ofs, key, key_data, &val) < 0)
2054#define lite3_is_obj(buf, buflen, ofs, key) ({ \
2055 const char *__lite3_key__ = (key); \
2056 _lite3_is_obj_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
2058#ifndef DOXYGEN_IGNORE
2059static 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)
2061 if (_lite3_verify_obj_get(buf, buflen, ofs) < 0)
2064 if (lite3_get_impl(buf, buflen, ofs, key, key_data, &val) < 0)
2081#define lite3_is_arr(buf, buflen, ofs, key) ({ \
2082 const char *__lite3_key__ = (key); \
2083 _lite3_is_arr_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key)); \
2085#ifndef DOXYGEN_IGNORE
2086static 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)
2088 if (_lite3_verify_obj_get(buf, buflen, ofs) < 0)
2091 if (lite3_get_impl(buf, buflen, ofs, key, key_data, &val) < 0)
2134#define lite3_get(buf, buflen, ofs, key, out) ({ \
2135 const unsigned char *__lite3_buf__ = (buf); \
2136 size_t __lite3_buflen__ = (buflen); \
2137 size_t __lite3_ofs__ = (ofs); \
2138 int __lite3_ret__; \
2139 if ((__lite3_ret__ = _lite3_verify_get(__lite3_buf__, __lite3_buflen__, __lite3_ofs__) < 0)) \
2140 return __lite3_ret__; \
2141 const char *__lite3_key__ = (key); \
2142 lite3_get_impl(__lite3_buf__, __lite3_buflen__, __lite3_ofs__, __lite3_key__, LITE3_KEY_DATA(key), out); \
2157#define lite3_get_bool(buf, buflen, ofs, key, out) ({ \
2158 const char *__lite3_key__ = (key); \
2159 _lite3_get_bool_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
2161#ifndef DOXYGEN_IGNORE
2162static 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)
2165 if ((ret = _lite3_verify_obj_get(buf, buflen, ofs)) < 0)
2168 if ((ret = lite3_get_impl(buf, buflen, ofs, key, key_data, &val)) < 0)
2171 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_BOOL\n");
2175 *out = (bool)(*(val->val));
2192#define lite3_get_i64(buf, buflen, ofs, key, out) ({ \
2193 const char *__lite3_key__ = (key); \
2194 _lite3_get_i64_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
2196#ifndef DOXYGEN_IGNORE
2197static 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)
2200 if ((ret = _lite3_verify_obj_get(buf, buflen, ofs)) < 0)
2203 if ((ret = lite3_get_impl(buf, buflen, ofs, key, key_data, &val)) < 0)
2206 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_I64\n");
2227#define lite3_get_f64(buf, buflen, ofs, key, out) ({ \
2228 const char *__lite3_key__ = (key); \
2229 _lite3_get_f64_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
2231#ifndef DOXYGEN_IGNORE
2232static 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)
2235 if ((ret = _lite3_verify_obj_get(buf, buflen, ofs)) < 0)
2238 if ((ret = lite3_get_impl(buf, buflen, ofs, key, key_data, &val)) < 0)
2241 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_F64\n");
2262#define lite3_get_bytes(buf, buflen, ofs, key, out) ({ \
2263 const char *__lite3_key__ = (key); \
2264 _lite3_get_bytes_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
2266#ifndef DOXYGEN_IGNORE
2267static 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)
2270 if ((ret = _lite3_verify_obj_get(buf, buflen, ofs)) < 0)
2273 if ((ret = lite3_get_impl(buf, buflen, ofs, key, key_data, &val)) < 0)
2276 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_BYTES\n");
2281 .
gen = *(uint32_t *)buf,
2302#define lite3_get_str(buf, buflen, ofs, key, out) ({ \
2303 const char *__lite3_key__ = (key); \
2304 _lite3_get_str_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
2306#ifndef DOXYGEN_IGNORE
2307static 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)
2310 if ((ret = _lite3_verify_obj_get(buf, buflen, ofs)) < 0)
2313 if ((ret = lite3_get_impl(buf, buflen, ofs, key, key_data, &val)) < 0)
2316 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_STRING\n");
2321 .
gen = *(uint32_t *)buf,
2343#define lite3_get_obj(buf, buflen, ofs, key, out) ({ \
2344 const char *__lite3_key__ = (key); \
2345 _lite3_get_obj_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
2347#ifndef DOXYGEN_IGNORE
2348static 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)
2351 if ((ret = _lite3_verify_obj_get(buf, buflen, ofs)) < 0)
2354 if ((ret = lite3_get_impl(buf, buflen, ofs, key, key_data, &val)) < 0)
2357 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_OBJECT\n");
2361 *out_ofs = (size_t)((uint8_t *)val - buf);
2378#define lite3_get_arr(buf, buflen, ofs, key, out) ({ \
2379 const char *__lite3_key__ = (key); \
2380 _lite3_get_arr_impl(buf, buflen, ofs, __lite3_key__, LITE3_KEY_DATA(key), out); \
2382#ifndef DOXYGEN_IGNORE
2383static 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)
2386 if ((ret = _lite3_verify_obj_get(buf, buflen, ofs)) < 0)
2389 if ((ret = lite3_get_impl(buf, buflen, ofs, key, key_data, &val)) < 0)
2392 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_ARRAY\n");
2396 *out_ofs = (size_t)((uint8_t *)val - buf);
2429 const unsigned char *buf,
2437 if ((ret = _lite3_get_by_index(buf, buflen, ofs, index, &val)) < 0)
2440 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_BOOL\n");
2444 *out = (bool)(*(val->val));
2455 const unsigned char *buf,
2463 if ((ret = _lite3_get_by_index(buf, buflen, ofs, index, &val)) < 0)
2466 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_I64\n");
2481 const unsigned char *buf,
2489 if ((ret = _lite3_get_by_index(buf, buflen, ofs, index, &val)) < 0)
2492 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_F64\n");
2507 const unsigned char *buf,
2515 if ((ret = _lite3_get_by_index(buf, buflen, ofs, index, &val)) < 0)
2518 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_BYTES\n");
2523 .
gen = *(uint32_t *)buf,
2538 const unsigned char *buf,
2546 if ((ret = _lite3_get_by_index(buf, buflen, ofs, index, &val)) < 0)
2549 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_STRING\n");
2554 .
gen = *(uint32_t *)buf,
2570 const unsigned char *buf,
2574 size_t *__restrict out_ofs)
2578 if ((ret = _lite3_get_by_index(buf, buflen, ofs, index, &val)) < 0)
2581 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_OBJECT\n");
2585 *out_ofs = (size_t)((uint8_t *)val - buf);
2596 const unsigned char *buf,
2600 size_t *__restrict out_ofs)
2604 if ((ret = _lite3_get_by_index(buf, buflen, ofs, index, &val)) < 0)
2607 LITE3_PRINT_ERROR(
"VALUE TYPE != LITE3_TYPE_ARRAY\n");
2611 *out_ofs = (size_t)((uint8_t *)val - buf);
2634#define LITE3_ITER_ITEM 1
2636#define LITE3_ITER_DONE 0
2651#ifndef DOXYGEN_IGNORE
2653int lite3_iter_create_impl(
const unsigned char *buf,
size_t buflen,
size_t ofs,
lite3_iter *out);
2663 const unsigned char *buf,
2669 if ((ret = _lite3_verify_get(buf, buflen, ofs)) < 0)
2671 return lite3_iter_create_impl(buf, buflen, ofs, out);
2691 const unsigned char *buf,
2735 return lite3_type_sizes[val->type];
2747static inline bool lite3_val_bool(
lite3_val *val)
2749 return *(
bool *)(val->val);
2752static inline int64_t lite3_val_i64(
lite3_val *val)
2755 memcpy(&tmp, val->val,
sizeof(tmp));
2759static inline double lite3_val_f64(
lite3_val *val)
2762 memcpy(&tmp, val->val,
sizeof(tmp));
2766static inline const char *lite3_val_str(
lite3_val *val)
2768 return (
const char *)val->val + LITE3_STR_LEN_SIZE;
2778 memcpy(out_len, val->val, LITE3_STR_LEN_SIZE);
2780 return (
const char *)val->val + LITE3_STR_LEN_SIZE;
2783static inline const unsigned char *lite3_val_bytes(
lite3_val *val,
size_t *out_len)
2786 memcpy(out_len, val->val, LITE3_BYTES_LEN_SIZE);
2787 return (
const unsigned char *)val->val + LITE3_BYTES_LEN_SIZE;
2831#if defined(DOXYGEN_ONLY) && !defined(LITE3_JSON)
2846#define LITE3_JSON_NESTING_DEPTH_MAX 32
2861 size_t *__restrict out_buflen,
2863 const char *__restrict json_str,
2880 size_t *__restrict out_buflen,
2882 const char *__restrict path
2898 size_t *__restrict out_buflen,
2917 const unsigned char *buf,
2936 const unsigned char *buf,
2939 size_t *__restrict out_len
2958 const unsigned char *buf,
2961 size_t *__restrict out_len
2975 const unsigned char *buf,
2978 char *__restrict json_buf,
2995 const unsigned char *buf,
2998 char *__restrict json_buf,
3002static 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)
3004 (void)buf; (void)out_buflen; (void)bufsz; (void)json_str; (void)json_len;
3008static inline int lite3_json_dec_file(
unsigned char *buf,
size_t *__restrict out_buflen,
size_t bufsz,
const char *__restrict path)
3010 (void)buf; (void)out_buflen; (void)bufsz; (void)path;
3014static inline int lite3_json_dec_fp(
unsigned char *buf,
size_t *__restrict out_buflen,
size_t bufsz, FILE *fp)
3016 (void)buf; (void)out_buflen; (void)bufsz; (void)fp;
3020static inline int lite3_json_print(
const unsigned char *buf,
size_t buflen,
size_t ofs)
3022 (void)buf; (void)buflen; (void)ofs;
3026static inline char *
lite3_json_enc(
const unsigned char *buf,
size_t buflen,
size_t ofs,
size_t *out_len)
3028 (void)buf; (void)buflen; (void)ofs; (void)out_len;
3032static inline char *
lite3_json_enc_pretty(
const unsigned char *buf,
size_t buflen,
size_t ofs,
size_t *out_len)
3034 (void)buf; (void)buflen; (void)ofs; (void)out_len;
3038static 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)
3040 (void)buf; (void)buflen; (void)ofs; (void)json_buf; (void)json_bufsz;
3044static 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)
3046 (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.