• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 #pragma once
3 /*
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *   you may not use this file except in compliance with the License.
6  *   You may obtain a copy of the License at
7  *
8  *       http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *   distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *   See the License for the specific language governing permissions and
14  *   limitations under the License.
15  */
16 
17 // TO ENABLE INLINE FUNCTIONS:
18 //   ON MSVC: enable the 'Inline Function Expansion' (/Ob2) compiler option, and maybe the
19 //            'Whole Program Optimitazion' (/GL), that requires the
20 //            'Link Time Code Generation' (/LTCG) linker option to be enabled too
21 
22 #ifndef BINN_H
23 #define BINN_H
24 
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdint.h>
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 #ifndef NULL
34 #ifdef __cplusplus
35 #define NULL 0
36 #else
37 #define NULL ((void*) 0)
38 #endif
39 #endif
40 
41 #ifndef TRUE
42 #define TRUE 1
43 #endif
44 
45 #ifndef FALSE
46 #define FALSE 0
47 #endif
48 
49 #ifndef BOOL
50 typedef int BOOL;
51 #endif
52 
53 #ifndef BINN_PRIVATE
54 #ifdef DEBUG
55 #define BINN_PRIVATE
56 #else
57 #define BINN_PRIVATE static
58 #endif
59 #endif
60 
61 #ifdef _MSC_VER
62 #define INLINE        __inline
63 #define ALWAYS_INLINE __forceinline
64 #else
65 // you can change to 'extern inline' if using the gcc option -flto
66 #define INLINE        static inline
67 #define ALWAYS_INLINE static inline __attribute__((always_inline))
68 #endif
69 
70 #ifndef int64
71 #if defined(_MSC_VER) || defined(__BORLANDC__)
72 typedef __int64          int64;
73 typedef unsigned __int64 uint64;
74 #else
75 typedef int64_t  int64;
76 typedef uint64_t uint64;
77 #endif
78 #endif
79 
80 // BINN CONSTANTS  ----------------------------------------
81 
82 // magic number:  0x1F 0xb1 0x22 0x1F  =>  0x1FB1221F or 0x1F22B11F
83 // because the BINN_STORAGE_NOBYTES (binary 000) may not have so many sub-types (BINN_STORAGE_HAS_MORE = 0x10)
84 #define BINN_MAGIC      0x1F22B11F
85 #define MAX_BINN_HEADER 9        // [1:type][4:size][4:count]
86 #define MIN_BINN_SIZE   3        // [1:type][1:size][1:count]
87 #define MAX_BIN_KEY_LEN 255
88 
89 #define INVALID_BINN 0
90 
91 // Storage Data Types  ------------------------------------
92 
93 #define BINN_STORAGE_NOBYTES   0x00
94 #define BINN_STORAGE_BYTE      0x20  //  8 bits
95 #define BINN_STORAGE_WORD      0x40  // 16 bits -- the endianess (byte order) is automatically corrected
96 #define BINN_STORAGE_DWORD     0x60  // 32 bits -- the endianess (byte order) is automatically corrected
97 #define BINN_STORAGE_QWORD     0x80  // 64 bits -- the endianess (byte order) is automatically corrected
98 #define BINN_STORAGE_STRING    0xA0  // Are stored with null termination
99 #define BINN_STORAGE_BLOB      0xC0
100 #define BINN_STORAGE_CONTAINER 0xE0
101 #define BINN_STORAGE_VIRTUAL   0x80000
102 //--
103 #define BINN_STORAGE_MIN BINN_STORAGE_NOBYTES
104 #define BINN_STORAGE_MAX BINN_STORAGE_CONTAINER
105 
106 #define BINN_STORAGE_MASK     0xE0
107 #define BINN_STORAGE_MASK16   0xE000
108 #define BINN_STORAGE_HAS_MORE 0x10
109 #define BINN_TYPE_MASK        0x0F
110 #define BINN_TYPE_MASK16      0x0FFF
111 
112 #define BINN_MAX_VALUE_MASK 0xFFFFF
113 //++
114 
115 // Data Formats  ------------------------------------------
116 
117 #define BINN_LIST   0xE0
118 #define BINN_MAP    0xE1
119 #define BINN_OBJECT 0xE2
120 
121 #define BINN_NULL  0x00
122 #define BINN_TRUE  0x01
123 #define BINN_FALSE 0x02
124 
125 #define BINN_UINT8 0x20      // (BYTE) (unsigned byte) Is the default format for the BYTE type
126 #define BINN_INT8  0x21      // (BYTE) (signed byte, from -128 to +127. The 0x80 is the sign bit, so the range in hex is
127                              // from 0x80 [-128] to 0x7F [127], being 0x00 = 0 and 0xFF = -1)
128 #define BINN_UINT16 0x40     // (WORD) (unsigned integer) Is the default format for the WORD type
129 #define BINN_INT16  0x41     // (WORD) (signed integer)
130 #define BINN_UINT32 0x60     // (DWORD) (unsigned integer) Is the default format for the DWORD type
131 #define BINN_INT32  0x61     // (DWORD) (signed integer)
132 #define BINN_UINT64 0x80     // (QWORD) (unsigned integer) Is the default format for the QWORD type
133 #define BINN_INT64  0x81     // (QWORD) (signed integer)
134 
135 #define BINN_SCHAR BINN_INT8
136 #define BINN_UCHAR BINN_UINT8
137 
138 #define BINN_STRING   0xA0    // (STRING) Raw String
139 #define BINN_DATETIME 0xA1    // (STRING) iso8601 format -- YYYY-MM-DD HH:MM:SS
140 #define BINN_DATE     0xA2    // (STRING) iso8601 format -- YYYY-MM-DD
141 #define BINN_TIME     0xA3    // (STRING) iso8601 format -- HH:MM:SS
142 #define BINN_DECIMAL  0xA4    // (STRING) High precision number - used for generic decimal values and for those ones
143                               // that cannot be represented in the float64 format.
144 #define BINN_CURRENCYSTR 0xA5 // (STRING) With currency unit/symbol - check for some iso standard format
145 #define BINN_SINGLE_STR  0xA6 // (STRING) Can be restored to float32
146 #define BINN_DOUBLE_STR  0xA7 // (STRING) May be restored to float64
147 
148 #define BINN_FLOAT32 0x62    // (DWORD)
149 #define BINN_FLOAT64 0x82    // (QWORD)
150 #define BINN_FLOAT   BINN_FLOAT32
151 #define BINN_SINGLE  BINN_FLOAT32
152 #define BINN_DOUBLE  BINN_FLOAT64
153 
154 #define BINN_CURRENCY 0x83   // (QWORD)
155 
156 #define BINN_BLOB 0xC0       // (BLOB) Raw Blob
157 
158 
159 // virtual types:
160 
161 #define BINN_BOOL 0x80061       // (DWORD) The value may be 0 or 1
162 
163 #ifdef BINN_EXTENDED
164 //#define BINN_SINGLE    0x800A1  // (STRING) Can be restored to float32
165 //#define BINN_DOUBLE    0x800A2  // (STRING) May be restored to float64
166 #endif
167 
168 //#define BINN_BINN      0x800E1  // (CONTAINER)
169 //#define BINN_BINN_BUFFER  0x800C1  // (BLOB) user binn. it's not open by the parser
170 
171 // extended content types:
172 
173 // strings:
174 #define BINN_HTML       0xB001
175 #define BINN_XML        0xB002
176 #define BINN_JSON       0xB003
177 #define BINN_JAVASCRIPT 0xB004
178 #define BINN_CSS        0xB005
179 
180 // blobs:
181 #define BINN_JPEG 0xD001
182 #define BINN_GIF  0xD002
183 #define BINN_PNG  0xD003
184 #define BINN_BMP  0xD004
185 
186 // type families
187 #define BINN_FAMILY_NONE   0x00
188 #define BINN_FAMILY_NULL   0xf1
189 #define BINN_FAMILY_INT    0xf2
190 #define BINN_FAMILY_FLOAT  0xf3
191 #define BINN_FAMILY_STRING 0xf4
192 #define BINN_FAMILY_BLOB   0xf5
193 #define BINN_FAMILY_BOOL   0xf6
194 #define BINN_FAMILY_BINN   0xf7
195 
196 // integer types related to signal
197 #define BINN_SIGNED_INT   11
198 #define BINN_UNSIGNED_INT 22
199 
200 typedef void (*binn_mem_free)(void*);
201 
202 typedef void (*binn_user_data_free)(void*);
203 
204 #define BINN_STATIC    ((binn_mem_free) 0)
205 #define BINN_TRANSIENT ((binn_mem_free) - 1)
206 
207 
208 #define BINN_IS_CONTAINER_TYPE(type_) ((type_) >= BINN_LIST && (type_) <= BINN_OBJECT)
209 
210 #define BINN_IS_INT_TYPE(type_) ((type_) >= BINN_UINT8 && (type_) <= BINN_INT64)
211 
212 
213 // --- BINN STRUCTURE --------------------------------------------------------------
214 
215 struct binn_struct {
216   int header;        // this struct header holds the magic number (BINN_MAGIC) that identifies this memory block as a
217                      // binn structure
218   BOOL allocated;    // the struct can be allocated using malloc_fn() or can be on the stack
219   BOOL writable;     // did it was create for writing? it can use the pbuf if not unified with ptr
220   BOOL dirty;        // the container header is not written to the buffer
221   //
222   void *pbuf;        // use *ptr below?
223   BOOL  pre_allocated;
224   int   alloc_size;
225   int   used_size;
226   //
227   int   type;
228   void *ptr;
229   int   size;
230   int   count;
231   //
232   binn_mem_free freefn;  // used only when type == BINN_STRING or BINN_BLOB
233   //
234   void *user_data;
235   binn_user_data_free userdata_freefn;
236   //
237   union {
238     int8_t   vint8;
239     int16_t  vint16;
240     int32_t  vint32;
241     int64_t  vint64;
242     uint8_t  vuint8;
243     uint16_t vuint16;
244     uint32_t vuint32;
245     uint64_t vuint64;
246     //
247     signed char    vchar;
248     unsigned char  vuchar;
249     signed short   vshort;
250     unsigned short vushort;
251     signed int     vint;
252     unsigned int   vuint;
253     //
254     float  vfloat;
255     double vdouble;
256     //
257     BOOL vbool;
258   };
259 };
260 
261 typedef struct binn_struct binn;
262 
263 // --- GENERAL FUNCTIONS  ----------------------------------------------------------
264 
265 void binn_set_alloc_functions(
266   void*(*new_malloc)(size_t), void*(*new_realloc)(void*, size_t),
267   void (*new_free)(void*));
268 int binn_create_type(int storage_type, int data_type_index);
269 BOOL binn_get_type_info(int long_type, int *pstorage_type, int *pextra_type);
270 int binn_get_write_storage(int type);
271 int binn_get_read_storage(int type);
272 BOOL binn_is_container(binn *item);
273 
274 void binn_set_user_data(binn *item, void *user_data, binn_user_data_free freefn);
275 
276 // --- WRITE FUNCTIONS  ------------------------------------------------------------
277 
278 BOOL binn_save_header(binn *item);
279 
280 // create a new binn allocating memory for the structure
281 binn *binn_new(int type, int size, void *buffer);
282 binn *binn_list();
283 binn *binn_map();
284 binn *binn_object();
285 
286 // create a new binn storing the structure on the stack
287 BOOL binn_create(binn *item, int type, int size, void *buffer);
288 BOOL binn_create_list(binn *list);
289 BOOL binn_create_map(binn *map);
290 BOOL binn_create_object(binn *object);
291 
292 // create a new binn as a copy from another
293 binn *binn_copy(void *old);
294 
295 BOOL binn_list_add_new(binn *list, binn *value);
296 BOOL binn_map_set_new(binn *map, int id, binn *value);
297 BOOL binn_object_set_new(binn *obj, const char *key, binn *value);
298 BOOL binn_object_set_new2(binn *obj, const char *key, int keylen, binn *value);
299 
300 // extended interface
301 BOOL binn_list_add(binn *list, int type, void *pvalue, int size);
302 BOOL binn_map_set(binn *map, int id, int type, void *pvalue, int size);
303 BOOL binn_object_set(binn *obj, const char *key, int type, void *pvalue, int size);
304 BOOL binn_object_set2(binn *obj, const char *key, int keylen, int type, void *pvalue, int size);
305 
306 // release memory
307 void binn_free(binn *item);
308 
309 // free the binn structure but keeps the binn buffer allocated, returning a pointer to it.
310 // use the free function to release the buffer later
311 void *binn_release(binn *item);
312 
313 // --- CREATING VALUES ---------------------------------------------------
314 
315 binn *binn_value(int type, void *pvalue, int size, binn_mem_free freefn);
316 
binn_init_item(binn * item)317 ALWAYS_INLINE void binn_init_item(binn *item) {
318   memset(item, 0, sizeof(binn));
319   item->header = BINN_MAGIC;
320 }
321 
binn_int8(signed char value)322 ALWAYS_INLINE binn *binn_int8(signed char value) {
323   return binn_value(BINN_INT8, &value, 0, NULL);
324 }
325 
binn_int16(short value)326 ALWAYS_INLINE binn *binn_int16(short value) {
327   return binn_value(BINN_INT16, &value, 0, NULL);
328 }
329 
binn_int32(int value)330 ALWAYS_INLINE binn *binn_int32(int value) {
331   return binn_value(BINN_INT32, &value, 0, NULL);
332 }
333 
binn_int64(int64 value)334 ALWAYS_INLINE binn *binn_int64(int64 value) {
335   return binn_value(BINN_INT64, &value, 0, NULL);
336 }
337 
binn_uint8(unsigned char value)338 ALWAYS_INLINE binn *binn_uint8(unsigned char value) {
339   return binn_value(BINN_UINT8, &value, 0, NULL);
340 }
341 
binn_uint16(unsigned short value)342 ALWAYS_INLINE binn *binn_uint16(unsigned short value) {
343   return binn_value(BINN_UINT16, &value, 0, NULL);
344 }
345 
binn_uint32(unsigned int value)346 ALWAYS_INLINE binn *binn_uint32(unsigned int value) {
347   return binn_value(BINN_UINT32, &value, 0, NULL);
348 }
349 
binn_uint64(uint64 value)350 ALWAYS_INLINE binn *binn_uint64(uint64 value) {
351   return binn_value(BINN_UINT64, &value, 0, NULL);
352 }
353 
binn_float(float value)354 ALWAYS_INLINE binn *binn_float(float value) {
355   return binn_value(BINN_FLOAT, &value, 0, NULL);
356 }
357 
binn_double(double value)358 ALWAYS_INLINE binn *binn_double(double value) {
359   return binn_value(BINN_DOUBLE, &value, 0, NULL);
360 }
361 
binn_bool(BOOL value)362 ALWAYS_INLINE binn *binn_bool(BOOL value) {
363   return binn_value(BINN_BOOL, &value, 0, NULL);
364 }
365 
binn_null()366 ALWAYS_INLINE binn *binn_null() {
367   return binn_value(BINN_NULL, NULL, 0, NULL);
368 }
369 
binn_string(const char * str,binn_mem_free freefn)370 ALWAYS_INLINE binn *binn_string(const char *str, binn_mem_free freefn) {
371   return binn_value(BINN_STRING, (void*) str, 0, freefn);
372 }
373 
binn_blob(void * ptr,int size,binn_mem_free freefn)374 ALWAYS_INLINE binn *binn_blob(void *ptr, int size, binn_mem_free freefn) {
375   return binn_value(BINN_BLOB, ptr, size, freefn);
376 }
377 
378 // --- READ FUNCTIONS  -------------------------------------------------------------
379 
380 // these functions accept pointer to the binn structure and pointer to the binn buffer
381 void *binn_ptr(void *ptr);
382 int binn_size(void *ptr);
383 int binn_buf_size(const void *ptr);
384 int binn_type(void *ptr);
385 int binn_buf_type(const void *pbuf);
386 int binn_count(void *ptr);
387 int binn_buf_count(const void *pbuf);
388 BOOL binn_is_valid_header(const void *pbuf, int *ptype, int *pcount, int *psize, int *pheadersize);
389 
390 BOOL binn_is_valid(void *ptr, int *ptype, int *pcount, int *psize);
391 
392 /* the function returns the values (type, count and size) and they don't need to be
393    initialized. these values are read from the buffer. example:
394 
395    int type, count, size;
396    result = binn_is_valid(ptr, &type, &count, &size);
397  */
398 BOOL binn_is_valid_ex(void *ptr, int *ptype, int *pcount, int *psize);
399 
400 /* if some value is informed (type, count or size) then the function will check if
401    the value returned from the serialized data matches the informed value. otherwise
402    the values must be initialized to zero. example:
403 
404    int type=0, count=0, size = known_size;
405    result = binn_is_valid_ex(ptr, &type, &count, &size);
406  */
407 
408 BOOL binn_is_struct(void *ptr);
409 
410 // Loading a binn buffer into a binn value - this is optional
411 
412 BOOL binn_load(void *data, binn *item);      // on stack
413 binn *binn_open(void *data);                 // allocated
414 
415 // easiest interface to use, but don't check if the value is there
416 
417 signed char binn_list_int8(void *list, int pos);
418 short binn_list_int16(void *list, int pos);
419 int binn_list_int32(void *list, int pos);
420 int64 binn_list_int64(void *list, int pos);
421 unsigned char binn_list_uint8(void *list, int pos);
422 unsigned short binn_list_uint16(void *list, int pos);
423 unsigned int binn_list_uint32(void *list, int pos);
424 uint64 binn_list_uint64(void *list, int pos);
425 float binn_list_float(void *list, int pos);
426 double binn_list_double(void *list, int pos);
427 BOOL binn_list_bool(void *list, int pos);
428 BOOL binn_list_null(void *list, int pos);
429 char *binn_list_str(void *list, int pos);
430 void *binn_list_blob(void *list, int pos, int *psize);
431 void *binn_list_list(void *list, int pos);
432 void *binn_list_map(void *list, int pos);
433 void *binn_list_object(void *list, int pos);
434 
435 signed char binn_map_int8(void *map, int id);
436 short binn_map_int16(void *map, int id);
437 int binn_map_int32(void *map, int id);
438 int64 binn_map_int64(void *map, int id);
439 unsigned char binn_map_uint8(void *map, int id);
440 unsigned short binn_map_uint16(void *map, int id);
441 unsigned int binn_map_uint32(void *map, int id);
442 uint64 binn_map_uint64(void *map, int id);
443 float binn_map_float(void *map, int id);
444 double binn_map_double(void *map, int id);
445 BOOL binn_map_bool(void *map, int id);
446 BOOL binn_map_null(void *map, int id);
447 char *binn_map_str(void *map, int id);
448 void *binn_map_blob(void *map, int id, int *psize);
449 void *binn_map_list(void *map, int id);
450 void *binn_map_map(void *map, int id);
451 void *binn_map_object(void *map, int id);
452 
453 signed char binn_object_int8(void *obj, const char *key);
454 short binn_object_int16(void *obj, const char *key);
455 int binn_object_int32(void *obj, const char *key);
456 int64 binn_object_int64(void *obj, const char *key);
457 unsigned char binn_object_uint8(void *obj, const char *key);
458 unsigned short binn_object_uint16(void *obj, const char *key);
459 unsigned int binn_object_uint32(void *obj, const char *key);
460 uint64 binn_object_uint64(void *obj, const char *key);
461 float binn_object_float(void *obj, const char *key);
462 double binn_object_double(void *obj, const char *key);
463 BOOL binn_object_bool(void *obj, const char *key);
464 BOOL binn_object_null(void *obj, const char *key);
465 char *binn_object_str(void *obj, const char *key);
466 void *binn_object_blob(void *obj, const char *key, int *psize);
467 void *binn_object_list(void *obj, const char *key);
468 void *binn_object_map(void *obj, const char *key);
469 void *binn_object_object(void *obj, const char *key);
470 
471 
472 // return a pointer to an allocated binn structure - must be released with the free() function or equivalent set in
473 // binn_set_alloc_functions()
474 binn *binn_list_value(void *list, int pos);
475 binn *binn_map_value(void *map, int id);
476 binn *binn_object_value(void *obj, const char *key);
477 
478 // read the value to a binn structure on the stack
479 BOOL binn_list_get_value(void *list, int pos, binn *value);
480 BOOL binn_map_get_value(void *map, int id, binn *value);
481 BOOL binn_object_get_value(void *obj, const char *key, binn *value);
482 
483 // single interface - these functions check the data type
484 BOOL binn_list_get(void *list, int pos, int type, void *pvalue, int *psize);
485 BOOL binn_map_get(void *map, int id, int type, void *pvalue, int *psize);
486 BOOL binn_object_get(void *obj, const char *key, int type, void *pvalue, int *psize);
487 
488 // these 3 functions return a pointer to the value and the data type
489 // they are thread-safe on big-endian devices
490 // on little-endian devices they are thread-safe only to return pointers to list, map, object, blob and strings
491 // the returned pointer to 16, 32 and 64 bits values must be used only by single-threaded applications
492 void *binn_list_read(void *list, int pos, int *ptype, int *psize);
493 void *binn_map_read(void *map, int id, int *ptype, int *psize);
494 void *binn_object_read(void *obj, const char *key, int *ptype, int *psize);
495 
496 // READ PAIR FUNCTIONS
497 
498 // these functions use base 1 in the 'pos' argument
499 
500 // on stack
501 BOOL binn_map_get_pair(void *map, int pos, int *pid, binn *value);
502 BOOL binn_object_get_pair(
503   void *obj, int pos, char *pkey,
504   binn *value);                                   // must free the memory returned in the pkey
505 
506 // allocated
507 binn *binn_map_pair(void *map, int pos, int *pid);
508 binn *binn_object_pair(void *obj, int pos, char *pkey);     // must free the memory returned in the pkey
509 
510 // these 2 functions return a pointer to the value and the data type
511 // they are thread-safe on big-endian devices
512 // on little-endian devices they are thread-safe only to return pointers to list, map, object, blob and strings
513 // the returned pointer to 16, 32 and 64 bits values must be used only by single-threaded applications
514 void *binn_map_read_pair(void *ptr, int pos, int *pid, int *ptype, int *psize);
515 void *binn_object_read_pair(void *ptr, int pos, char *pkey, int *ptype, int *psize);
516 
517 // SEQUENTIAL READ FUNCTIONS
518 
519 typedef struct binn_iter_struct {
520   unsigned char *pnext;
521   unsigned char *plimit;
522   int type;
523   int count;
524   int current;
525 } binn_iter;
526 
527 BOOL binn_iter_init(binn_iter *iter, void *pbuf, int type);
528 
529 // allocated
530 binn *binn_list_next_value(binn_iter *iter);
531 binn *binn_map_next_value(binn_iter *iter, int *pid);
532 binn *binn_object_next_value(binn_iter *iter, char *pkey);     // the key must be declared as: char key[256];
533 
534 // on stack
535 BOOL binn_list_next(binn_iter *iter, binn *value);
536 BOOL binn_map_next(binn_iter *iter, int *pid, binn *value);
537 BOOL binn_object_next(
538   binn_iter *iter, char *pkey,
539   binn *value);                                 // the key must be declared as: char key[256];
540 BOOL binn_object_next2(binn_iter *iter, char **pkey, int *klen, binn *value);
541 
542 // these 3 functions return a pointer to the value and the data type
543 // they are thread-safe on big-endian devices
544 // on little-endian devices they are thread-safe only to return pointers to list, map, object, blob and strings
545 // the returned pointer to 16, 32 and 64 bits values must be used only by single-threaded applications
546 void *binn_list_read_next(binn_iter *iter, int *ptype, int *psize);
547 void *binn_map_read_next(binn_iter *iter, int *pid, int *ptype, int *psize);
548 void *binn_object_read_next(
549   binn_iter *iter, char *pkey, int *ptype,
550   int *psize);                                      // the key must be declared as: char key[256];
551 
552 // --- MACROS ------------------------------------------------------------
553 
554 #define binn_is_writable(item) (item)->writable;
555 
556 // set values on stack allocated binn structures
557 
558 #define binn_set_null(item) do { (item)->type = BINN_NULL; } while (0)
559 
560 #define binn_set_bool(item, value) do { (item)->type = BINN_BOOL; (item)->vbool = value; (item)->ptr = &((item)->vbool); \
561 } while (0)
562 
563 #define binn_set_int(item, value)   do { (item)->type = BINN_INT32; (item)->vint32 = value; \
564                                          (item)->ptr = &((item)->vint32); } while (0)
565 #define binn_set_int64(item, value) do { (item)->type = BINN_INT64; (item)->vint64 = value; \
566                                          (item)->ptr = &((item)->vint64); } while (0)
567 
568 #define binn_set_uint(item, value)   do { (item)->type = BINN_UINT32; (item)->vuint32 = value; \
569                                           (item)->ptr = &((item)->vuint32); } while (0)
570 #define binn_set_uint64(item, value) do { (item)->type = BINN_UINT64; (item)->vuint64 = value; \
571                                           (item)->ptr = &((item)->vuint64); } while (0)
572 
573 #define binn_set_float(item, value)  do { (item)->type = BINN_FLOAT;  (item)->vfloat = value; \
574                                           (item)->ptr = &((item)->vfloat); } while (0)
575 #define binn_set_double(item, value) do { (item)->type = BINN_DOUBLE; (item)->vdouble = value; \
576                                           (item)->ptr = &((item)->vdouble); } while (0)
577 
578 //#define binn_set_string(item,str,pfree)    do { (item)->type = BINN_STRING; (item)->ptr = str; (item)->freefn = pfree;
579 // } while (0)
580 //#define binn_set_blob(item,ptr,size,pfree) do { (item)->type = BINN_BLOB;   (item)->ptr = ptr; (item)->freefn = pfree;
581 // (item)->size = size; } while (0)
582 BOOL binn_set_string(binn *item, char *str, binn_mem_free pfree);
583 BOOL binn_set_blob(binn *item, void *ptr, int size, binn_mem_free pfree);
584 
585 //#define binn_double(value) {       (item)->type = BINN_DOUBLE; (item)->vdouble = value; (item)->ptr =
586 // &((item)->vdouble) }
587 
588 // FOREACH MACROS
589 // must use these declarations in the function that will use them:
590 //  binn_iter iter;
591 //  char key[256];  // only for the object
592 //  int  id;        // only for the map
593 //  binn value;
594 
595 #define binn_object_foreach(object, key, value)   \
596   binn_iter_init(&iter, object, BINN_OBJECT);   \
597   while (binn_object_next(&iter, key, &value))
598 
599 #define binn_map_foreach(map, id, value)      \
600   binn_iter_init(&iter, map, BINN_MAP);     \
601   while (binn_map_next(&iter, &id, &value))
602 
603 #define binn_list_foreach(list, value)      \
604   binn_iter_init(&iter, list, BINN_LIST); \
605   while (binn_list_next(&iter, &value))
606 
607 /*************************************************************************************/
608 /*** SET FUNCTIONS *******************************************************************/
609 /*************************************************************************************/
610 
binn_list_add_int8(binn * list,signed char value)611 ALWAYS_INLINE BOOL binn_list_add_int8(binn *list, signed char value) {
612   return binn_list_add(list, BINN_INT8, &value, 0);
613 }
614 
binn_list_add_int16(binn * list,short value)615 ALWAYS_INLINE BOOL binn_list_add_int16(binn *list, short value) {
616   return binn_list_add(list, BINN_INT16, &value, 0);
617 }
618 
binn_list_add_int32(binn * list,int value)619 ALWAYS_INLINE BOOL binn_list_add_int32(binn *list, int value) {
620   return binn_list_add(list, BINN_INT32, &value, 0);
621 }
622 
binn_list_add_int64(binn * list,int64 value)623 ALWAYS_INLINE BOOL binn_list_add_int64(binn *list, int64 value) {
624   return binn_list_add(list, BINN_INT64, &value, 0);
625 }
626 
binn_list_add_uint8(binn * list,unsigned char value)627 ALWAYS_INLINE BOOL binn_list_add_uint8(binn *list, unsigned char value) {
628   return binn_list_add(list, BINN_UINT8, &value, 0);
629 }
630 
binn_list_add_uint16(binn * list,unsigned short value)631 ALWAYS_INLINE BOOL binn_list_add_uint16(binn *list, unsigned short value) {
632   return binn_list_add(list, BINN_UINT16, &value, 0);
633 }
634 
binn_list_add_uint32(binn * list,unsigned int value)635 ALWAYS_INLINE BOOL binn_list_add_uint32(binn *list, unsigned int value) {
636   return binn_list_add(list, BINN_UINT32, &value, 0);
637 }
638 
binn_list_add_uint64(binn * list,uint64 value)639 ALWAYS_INLINE BOOL binn_list_add_uint64(binn *list, uint64 value) {
640   return binn_list_add(list, BINN_UINT64, &value, 0);
641 }
642 
binn_list_add_float(binn * list,float value)643 ALWAYS_INLINE BOOL binn_list_add_float(binn *list, float value) {
644   return binn_list_add(list, BINN_FLOAT32, &value, 0);
645 }
646 
binn_list_add_double(binn * list,double value)647 ALWAYS_INLINE BOOL binn_list_add_double(binn *list, double value) {
648   return binn_list_add(list, BINN_FLOAT64, &value, 0);
649 }
650 
binn_list_add_bool(binn * list,BOOL value)651 ALWAYS_INLINE BOOL binn_list_add_bool(binn *list, BOOL value) {
652   return binn_list_add(list, BINN_BOOL, &value, 0);
653 }
654 
binn_list_add_null(binn * list)655 ALWAYS_INLINE BOOL binn_list_add_null(binn *list) {
656   return binn_list_add(list, BINN_NULL, NULL, 0);
657 }
658 
binn_list_add_str(binn * list,char * str)659 ALWAYS_INLINE BOOL binn_list_add_str(binn *list, char *str) {
660   return binn_list_add(list, BINN_STRING, str, 0);
661 }
662 
binn_list_add_const_str(binn * list,const char * str)663 ALWAYS_INLINE BOOL binn_list_add_const_str(binn *list, const char *str) {
664   return binn_list_add(list, BINN_STRING, (char*) str, 0);
665 }
666 
binn_list_add_blob(binn * list,void * ptr,int size)667 ALWAYS_INLINE BOOL binn_list_add_blob(binn *list, void *ptr, int size) {
668   return binn_list_add(list, BINN_BLOB, ptr, size);
669 }
670 
binn_list_add_list(binn * list,void * list2)671 ALWAYS_INLINE BOOL binn_list_add_list(binn *list, void *list2) {
672   return binn_list_add(list, BINN_LIST, binn_ptr(list2), binn_size(list2));
673 }
674 
binn_list_add_map(binn * list,void * map)675 ALWAYS_INLINE BOOL binn_list_add_map(binn *list, void *map) {
676   return binn_list_add(list, BINN_MAP, binn_ptr(map), binn_size(map));
677 }
678 
binn_list_add_object(binn * list,void * obj)679 ALWAYS_INLINE BOOL binn_list_add_object(binn *list, void *obj) {
680   return binn_list_add(list, BINN_OBJECT, binn_ptr(obj), binn_size(obj));
681 }
682 
binn_list_add_value(binn * list,binn * value)683 ALWAYS_INLINE BOOL binn_list_add_value(binn *list, binn *value) {
684   return binn_list_add(list, value->type, binn_ptr(value), binn_size(value));
685 }
686 
687 /*************************************************************************************/
688 
binn_map_set_int8(binn * map,int id,signed char value)689 ALWAYS_INLINE BOOL binn_map_set_int8(binn *map, int id, signed char value) {
690   return binn_map_set(map, id, BINN_INT8, &value, 0);
691 }
692 
binn_map_set_int16(binn * map,int id,short value)693 ALWAYS_INLINE BOOL binn_map_set_int16(binn *map, int id, short value) {
694   return binn_map_set(map, id, BINN_INT16, &value, 0);
695 }
696 
binn_map_set_int32(binn * map,int id,int value)697 ALWAYS_INLINE BOOL binn_map_set_int32(binn *map, int id, int value) {
698   return binn_map_set(map, id, BINN_INT32, &value, 0);
699 }
700 
binn_map_set_int64(binn * map,int id,int64 value)701 ALWAYS_INLINE BOOL binn_map_set_int64(binn *map, int id, int64 value) {
702   return binn_map_set(map, id, BINN_INT64, &value, 0);
703 }
704 
binn_map_set_uint8(binn * map,int id,unsigned char value)705 ALWAYS_INLINE BOOL binn_map_set_uint8(binn *map, int id, unsigned char value) {
706   return binn_map_set(map, id, BINN_UINT8, &value, 0);
707 }
708 
binn_map_set_uint16(binn * map,int id,unsigned short value)709 ALWAYS_INLINE BOOL binn_map_set_uint16(binn *map, int id, unsigned short value) {
710   return binn_map_set(map, id, BINN_UINT16, &value, 0);
711 }
712 
binn_map_set_uint32(binn * map,int id,unsigned int value)713 ALWAYS_INLINE BOOL binn_map_set_uint32(binn *map, int id, unsigned int value) {
714   return binn_map_set(map, id, BINN_UINT32, &value, 0);
715 }
716 
binn_map_set_uint64(binn * map,int id,uint64 value)717 ALWAYS_INLINE BOOL binn_map_set_uint64(binn *map, int id, uint64 value) {
718   return binn_map_set(map, id, BINN_UINT64, &value, 0);
719 }
720 
binn_map_set_float(binn * map,int id,float value)721 ALWAYS_INLINE BOOL binn_map_set_float(binn *map, int id, float value) {
722   return binn_map_set(map, id, BINN_FLOAT32, &value, 0);
723 }
724 
binn_map_set_double(binn * map,int id,double value)725 ALWAYS_INLINE BOOL binn_map_set_double(binn *map, int id, double value) {
726   return binn_map_set(map, id, BINN_FLOAT64, &value, 0);
727 }
728 
binn_map_set_bool(binn * map,int id,BOOL value)729 ALWAYS_INLINE BOOL binn_map_set_bool(binn *map, int id, BOOL value) {
730   return binn_map_set(map, id, BINN_BOOL, &value, 0);
731 }
732 
binn_map_set_null(binn * map,int id)733 ALWAYS_INLINE BOOL binn_map_set_null(binn *map, int id) {
734   return binn_map_set(map, id, BINN_NULL, NULL, 0);
735 }
736 
binn_map_set_str(binn * map,int id,char * str)737 ALWAYS_INLINE BOOL binn_map_set_str(binn *map, int id, char *str) {
738   return binn_map_set(map, id, BINN_STRING, str, 0);
739 }
740 
binn_map_set_blob(binn * map,int id,void * ptr,int size)741 ALWAYS_INLINE BOOL binn_map_set_blob(binn *map, int id, void *ptr, int size) {
742   return binn_map_set(map, id, BINN_BLOB, ptr, size);
743 }
744 
binn_map_set_list(binn * map,int id,void * list)745 ALWAYS_INLINE BOOL binn_map_set_list(binn *map, int id, void *list) {
746   return binn_map_set(map, id, BINN_LIST, binn_ptr(list), binn_size(list));
747 }
748 
binn_map_set_map(binn * map,int id,void * map2)749 ALWAYS_INLINE BOOL binn_map_set_map(binn *map, int id, void *map2) {
750   return binn_map_set(map, id, BINN_MAP, binn_ptr(map2), binn_size(map2));
751 }
752 
binn_map_set_object(binn * map,int id,void * obj)753 ALWAYS_INLINE BOOL binn_map_set_object(binn *map, int id, void *obj) {
754   return binn_map_set(map, id, BINN_OBJECT, binn_ptr(obj), binn_size(obj));
755 }
756 
binn_map_set_value(binn * map,int id,binn * value)757 ALWAYS_INLINE BOOL binn_map_set_value(binn *map, int id, binn *value) {
758   return binn_map_set(map, id, value->type, binn_ptr(value), binn_size(value));
759 }
760 
761 /*************************************************************************************/
762 
binn_object_set_int8(binn * obj,const char * key,signed char value)763 ALWAYS_INLINE BOOL binn_object_set_int8(binn *obj, const char *key, signed char value) {
764   return binn_object_set(obj, key, BINN_INT8, &value, 0);
765 }
766 
binn_object_set_int16(binn * obj,const char * key,short value)767 ALWAYS_INLINE BOOL binn_object_set_int16(binn *obj, const char *key, short value) {
768   return binn_object_set(obj, key, BINN_INT16, &value, 0);
769 }
770 
binn_object_set_int32(binn * obj,const char * key,int value)771 ALWAYS_INLINE BOOL binn_object_set_int32(binn *obj, const char *key, int value) {
772   return binn_object_set(obj, key, BINN_INT32, &value, 0);
773 }
774 
binn_object_set_int64(binn * obj,const char * key,int64 value)775 ALWAYS_INLINE BOOL binn_object_set_int64(binn *obj, const char *key, int64 value) {
776   return binn_object_set(obj, key, BINN_INT64, &value, 0);
777 }
778 
binn_object_set_uint8(binn * obj,const char * key,unsigned char value)779 ALWAYS_INLINE BOOL binn_object_set_uint8(binn *obj, const char *key, unsigned char value) {
780   return binn_object_set(obj, key, BINN_UINT8, &value, 0);
781 }
782 
binn_object_set_uint16(binn * obj,const char * key,unsigned short value)783 ALWAYS_INLINE BOOL binn_object_set_uint16(binn *obj, const char *key, unsigned short value) {
784   return binn_object_set(obj, key, BINN_UINT16, &value, 0);
785 }
786 
binn_object_set_uint32(binn * obj,const char * key,unsigned int value)787 ALWAYS_INLINE BOOL binn_object_set_uint32(binn *obj, const char *key, unsigned int value) {
788   return binn_object_set(obj, key, BINN_UINT32, &value, 0);
789 }
790 
binn_object_set_uint64(binn * obj,const char * key,uint64 value)791 ALWAYS_INLINE BOOL binn_object_set_uint64(binn *obj, const char *key, uint64 value) {
792   return binn_object_set(obj, key, BINN_UINT64, &value, 0);
793 }
794 
binn_object_set_float(binn * obj,const char * key,float value)795 ALWAYS_INLINE BOOL binn_object_set_float(binn *obj, const char *key, float value) {
796   return binn_object_set(obj, key, BINN_FLOAT32, &value, 0);
797 }
798 
binn_object_set_double(binn * obj,const char * key,double value)799 ALWAYS_INLINE BOOL binn_object_set_double(binn *obj, const char *key, double value) {
800   return binn_object_set(obj, key, BINN_FLOAT64, &value, 0);
801 }
802 
binn_object_set_bool(binn * obj,const char * key,BOOL value)803 ALWAYS_INLINE BOOL binn_object_set_bool(binn *obj, const char *key, BOOL value) {
804   return binn_object_set(obj, key, BINN_BOOL, &value, 0);
805 }
806 
binn_object_set_null(binn * obj,const char * key)807 ALWAYS_INLINE BOOL binn_object_set_null(binn *obj, const char *key) {
808   return binn_object_set(obj, key, BINN_NULL, NULL, 0);
809 }
810 
binn_object_set_str(binn * obj,const char * key,const char * str)811 ALWAYS_INLINE BOOL binn_object_set_str(binn *obj, const char *key, const char *str) {
812   return binn_object_set(obj, key, BINN_STRING, (char*) str, 0);  // todo
813 }
814 
binn_object_set_blob(binn * obj,const char * key,void * ptr,int size)815 ALWAYS_INLINE BOOL binn_object_set_blob(binn *obj, const char *key, void *ptr, int size) {
816   return binn_object_set(obj, key, BINN_BLOB, ptr, size);
817 }
818 
binn_object_set_list(binn * obj,const char * key,void * list)819 ALWAYS_INLINE BOOL binn_object_set_list(binn *obj, const char *key, void *list) {
820   return binn_object_set(obj, key, BINN_LIST, binn_ptr(list), binn_size(list));
821 }
822 
binn_object_set_map(binn * obj,const char * key,void * map)823 ALWAYS_INLINE BOOL binn_object_set_map(binn *obj, const char *key, void *map) {
824   return binn_object_set(obj, key, BINN_MAP, binn_ptr(map), binn_size(map));
825 }
826 
binn_object_set_object(binn * obj,const char * key,void * obj2)827 ALWAYS_INLINE BOOL binn_object_set_object(binn *obj, const char *key, void *obj2) {
828   return binn_object_set(obj, key, BINN_OBJECT, binn_ptr(obj2), binn_size(obj2));
829 }
830 
binn_object_set_value(binn * obj,const char * key,binn * value)831 ALWAYS_INLINE BOOL binn_object_set_value(binn *obj, const char *key, binn *value) {
832   return binn_object_set(obj, key, value->type, binn_ptr(value), binn_size(value));
833 }
834 
binn_object_set_value2(binn * obj,const char * key,int keylen,binn * value)835 ALWAYS_INLINE BOOL binn_object_set_value2(binn *obj, const char *key, int keylen, binn *value) {
836   return binn_object_set2(obj, key, keylen, value->type, binn_ptr(value), binn_size(value));
837 }
838 
839 /*************************************************************************************/
840 /*** GET FUNCTIONS *******************************************************************/
841 /*************************************************************************************/
842 
binn_list_get_int8(void * list,int pos,signed char * pvalue)843 ALWAYS_INLINE BOOL binn_list_get_int8(void *list, int pos, signed char *pvalue) {
844   return binn_list_get(list, pos, BINN_INT8, pvalue, NULL);
845 }
846 
binn_list_get_int16(void * list,int pos,short * pvalue)847 ALWAYS_INLINE BOOL binn_list_get_int16(void *list, int pos, short *pvalue) {
848   return binn_list_get(list, pos, BINN_INT16, pvalue, NULL);
849 }
850 
binn_list_get_int32(void * list,int pos,int * pvalue)851 ALWAYS_INLINE BOOL binn_list_get_int32(void *list, int pos, int *pvalue) {
852   return binn_list_get(list, pos, BINN_INT32, pvalue, NULL);
853 }
854 
binn_list_get_int64(void * list,int pos,int64 * pvalue)855 ALWAYS_INLINE BOOL binn_list_get_int64(void *list, int pos, int64 *pvalue) {
856   return binn_list_get(list, pos, BINN_INT64, pvalue, NULL);
857 }
858 
binn_list_get_uint8(void * list,int pos,unsigned char * pvalue)859 ALWAYS_INLINE BOOL binn_list_get_uint8(void *list, int pos, unsigned char *pvalue) {
860   return binn_list_get(list, pos, BINN_UINT8, pvalue, NULL);
861 }
862 
binn_list_get_uint16(void * list,int pos,unsigned short * pvalue)863 ALWAYS_INLINE BOOL binn_list_get_uint16(void *list, int pos, unsigned short *pvalue) {
864   return binn_list_get(list, pos, BINN_UINT16, pvalue, NULL);
865 }
866 
binn_list_get_uint32(void * list,int pos,unsigned int * pvalue)867 ALWAYS_INLINE BOOL binn_list_get_uint32(void *list, int pos, unsigned int *pvalue) {
868   return binn_list_get(list, pos, BINN_UINT32, pvalue, NULL);
869 }
870 
binn_list_get_uint64(void * list,int pos,uint64 * pvalue)871 ALWAYS_INLINE BOOL binn_list_get_uint64(void *list, int pos, uint64 *pvalue) {
872   return binn_list_get(list, pos, BINN_UINT64, pvalue, NULL);
873 }
874 
binn_list_get_float(void * list,int pos,float * pvalue)875 ALWAYS_INLINE BOOL binn_list_get_float(void *list, int pos, float *pvalue) {
876   return binn_list_get(list, pos, BINN_FLOAT32, pvalue, NULL);
877 }
878 
binn_list_get_double(void * list,int pos,double * pvalue)879 ALWAYS_INLINE BOOL binn_list_get_double(void *list, int pos, double *pvalue) {
880   return binn_list_get(list, pos, BINN_FLOAT64, pvalue, NULL);
881 }
882 
binn_list_get_bool(void * list,int pos,BOOL * pvalue)883 ALWAYS_INLINE BOOL binn_list_get_bool(void *list, int pos, BOOL *pvalue) {
884   return binn_list_get(list, pos, BINN_BOOL, pvalue, NULL);
885 }
886 
binn_list_get_str(void * list,int pos,char ** pvalue)887 ALWAYS_INLINE BOOL binn_list_get_str(void *list, int pos, char **pvalue) {
888   return binn_list_get(list, pos, BINN_STRING, pvalue, NULL);
889 }
890 
binn_list_get_blob(void * list,int pos,void ** pvalue,int * psize)891 ALWAYS_INLINE BOOL binn_list_get_blob(void *list, int pos, void **pvalue, int *psize) {
892   return binn_list_get(list, pos, BINN_BLOB, pvalue, psize);
893 }
894 
binn_list_get_list(void * list,int pos,void ** pvalue)895 ALWAYS_INLINE BOOL binn_list_get_list(void *list, int pos, void **pvalue) {
896   return binn_list_get(list, pos, BINN_LIST, pvalue, NULL);
897 }
898 
binn_list_get_map(void * list,int pos,void ** pvalue)899 ALWAYS_INLINE BOOL binn_list_get_map(void *list, int pos, void **pvalue) {
900   return binn_list_get(list, pos, BINN_MAP, pvalue, NULL);
901 }
902 
binn_list_get_object(void * list,int pos,void ** pvalue)903 ALWAYS_INLINE BOOL binn_list_get_object(void *list, int pos, void **pvalue) {
904   return binn_list_get(list, pos, BINN_OBJECT, pvalue, NULL);
905 }
906 
907 /***************************************************************************/
908 
binn_map_get_int8(void * map,int id,signed char * pvalue)909 ALWAYS_INLINE BOOL binn_map_get_int8(void *map, int id, signed char *pvalue) {
910   return binn_map_get(map, id, BINN_INT8, pvalue, NULL);
911 }
912 
binn_map_get_int16(void * map,int id,short * pvalue)913 ALWAYS_INLINE BOOL binn_map_get_int16(void *map, int id, short *pvalue) {
914   return binn_map_get(map, id, BINN_INT16, pvalue, NULL);
915 }
916 
binn_map_get_int32(void * map,int id,int * pvalue)917 ALWAYS_INLINE BOOL binn_map_get_int32(void *map, int id, int *pvalue) {
918   return binn_map_get(map, id, BINN_INT32, pvalue, NULL);
919 }
920 
binn_map_get_int64(void * map,int id,int64 * pvalue)921 ALWAYS_INLINE BOOL binn_map_get_int64(void *map, int id, int64 *pvalue) {
922   return binn_map_get(map, id, BINN_INT64, pvalue, NULL);
923 }
924 
binn_map_get_uint8(void * map,int id,unsigned char * pvalue)925 ALWAYS_INLINE BOOL binn_map_get_uint8(void *map, int id, unsigned char *pvalue) {
926   return binn_map_get(map, id, BINN_UINT8, pvalue, NULL);
927 }
928 
binn_map_get_uint16(void * map,int id,unsigned short * pvalue)929 ALWAYS_INLINE BOOL binn_map_get_uint16(void *map, int id, unsigned short *pvalue) {
930   return binn_map_get(map, id, BINN_UINT16, pvalue, NULL);
931 }
932 
binn_map_get_uint32(void * map,int id,unsigned int * pvalue)933 ALWAYS_INLINE BOOL binn_map_get_uint32(void *map, int id, unsigned int *pvalue) {
934   return binn_map_get(map, id, BINN_UINT32, pvalue, NULL);
935 }
936 
binn_map_get_uint64(void * map,int id,uint64 * pvalue)937 ALWAYS_INLINE BOOL binn_map_get_uint64(void *map, int id, uint64 *pvalue) {
938   return binn_map_get(map, id, BINN_UINT64, pvalue, NULL);
939 }
940 
binn_map_get_float(void * map,int id,float * pvalue)941 ALWAYS_INLINE BOOL binn_map_get_float(void *map, int id, float *pvalue) {
942   return binn_map_get(map, id, BINN_FLOAT32, pvalue, NULL);
943 }
944 
binn_map_get_double(void * map,int id,double * pvalue)945 ALWAYS_INLINE BOOL binn_map_get_double(void *map, int id, double *pvalue) {
946   return binn_map_get(map, id, BINN_FLOAT64, pvalue, NULL);
947 }
948 
binn_map_get_bool(void * map,int id,BOOL * pvalue)949 ALWAYS_INLINE BOOL binn_map_get_bool(void *map, int id, BOOL *pvalue) {
950   return binn_map_get(map, id, BINN_BOOL, pvalue, NULL);
951 }
952 
binn_map_get_str(void * map,int id,char ** pvalue)953 ALWAYS_INLINE BOOL binn_map_get_str(void *map, int id, char **pvalue) {
954   return binn_map_get(map, id, BINN_STRING, pvalue, NULL);
955 }
956 
binn_map_get_blob(void * map,int id,void ** pvalue,int * psize)957 ALWAYS_INLINE BOOL binn_map_get_blob(void *map, int id, void **pvalue, int *psize) {
958   return binn_map_get(map, id, BINN_BLOB, pvalue, psize);
959 }
960 
binn_map_get_list(void * map,int id,void ** pvalue)961 ALWAYS_INLINE BOOL binn_map_get_list(void *map, int id, void **pvalue) {
962   return binn_map_get(map, id, BINN_LIST, pvalue, NULL);
963 }
964 
binn_map_get_map(void * map,int id,void ** pvalue)965 ALWAYS_INLINE BOOL binn_map_get_map(void *map, int id, void **pvalue) {
966   return binn_map_get(map, id, BINN_MAP, pvalue, NULL);
967 }
968 
binn_map_get_object(void * map,int id,void ** pvalue)969 ALWAYS_INLINE BOOL binn_map_get_object(void *map, int id, void **pvalue) {
970   return binn_map_get(map, id, BINN_OBJECT, pvalue, NULL);
971 }
972 
973 /***************************************************************************/
974 
975 // usage:
976 //   if (binn_object_get_int32(obj, "key", &value) == FALSE) xxx;
977 
binn_object_get_int8(void * obj,const char * key,signed char * pvalue)978 ALWAYS_INLINE BOOL binn_object_get_int8(void *obj, const char *key, signed char *pvalue) {
979   return binn_object_get(obj, key, BINN_INT8, pvalue, NULL);
980 }
981 
binn_object_get_int16(void * obj,const char * key,short * pvalue)982 ALWAYS_INLINE BOOL binn_object_get_int16(void *obj, const char *key, short *pvalue) {
983   return binn_object_get(obj, key, BINN_INT16, pvalue, NULL);
984 }
985 
binn_object_get_int32(void * obj,const char * key,int * pvalue)986 ALWAYS_INLINE BOOL binn_object_get_int32(void *obj, const char *key, int *pvalue) {
987   return binn_object_get(obj, key, BINN_INT32, pvalue, NULL);
988 }
989 
binn_object_get_int64(void * obj,const char * key,int64 * pvalue)990 ALWAYS_INLINE BOOL binn_object_get_int64(void *obj, const char *key, int64 *pvalue) {
991   return binn_object_get(obj, key, BINN_INT64, pvalue, NULL);
992 }
993 
binn_object_get_uint8(void * obj,const char * key,unsigned char * pvalue)994 ALWAYS_INLINE BOOL binn_object_get_uint8(void *obj, const char *key, unsigned char *pvalue) {
995   return binn_object_get(obj, key, BINN_UINT8, pvalue, NULL);
996 }
997 
binn_object_get_uint16(void * obj,const char * key,unsigned short * pvalue)998 ALWAYS_INLINE BOOL binn_object_get_uint16(void *obj, const char *key, unsigned short *pvalue) {
999   return binn_object_get(obj, key, BINN_UINT16, pvalue, NULL);
1000 }
1001 
binn_object_get_uint32(void * obj,const char * key,unsigned int * pvalue)1002 ALWAYS_INLINE BOOL binn_object_get_uint32(void *obj, const char *key, unsigned int *pvalue) {
1003   return binn_object_get(obj, key, BINN_UINT32, pvalue, NULL);
1004 }
1005 
binn_object_get_uint64(void * obj,const char * key,uint64 * pvalue)1006 ALWAYS_INLINE BOOL binn_object_get_uint64(void *obj, const char *key, uint64 *pvalue) {
1007   return binn_object_get(obj, key, BINN_UINT64, pvalue, NULL);
1008 }
1009 
binn_object_get_float(void * obj,const char * key,float * pvalue)1010 ALWAYS_INLINE BOOL binn_object_get_float(void *obj, const char *key, float *pvalue) {
1011   return binn_object_get(obj, key, BINN_FLOAT32, pvalue, NULL);
1012 }
1013 
binn_object_get_double(void * obj,const char * key,double * pvalue)1014 ALWAYS_INLINE BOOL binn_object_get_double(void *obj, const char *key, double *pvalue) {
1015   return binn_object_get(obj, key, BINN_FLOAT64, pvalue, NULL);
1016 }
1017 
binn_object_get_bool(void * obj,const char * key,BOOL * pvalue)1018 ALWAYS_INLINE BOOL binn_object_get_bool(void *obj, const char *key, BOOL *pvalue) {
1019   return binn_object_get(obj, key, BINN_BOOL, pvalue, NULL);
1020 }
1021 
binn_object_get_str(void * obj,const char * key,char ** pvalue)1022 ALWAYS_INLINE BOOL binn_object_get_str(void *obj, const char *key, char **pvalue) {
1023   return binn_object_get(obj, key, BINN_STRING, pvalue, NULL);
1024 }
1025 
binn_object_get_blob(void * obj,const char * key,void ** pvalue,int * psize)1026 ALWAYS_INLINE BOOL binn_object_get_blob(void *obj, const char *key, void **pvalue, int *psize) {
1027   return binn_object_get(obj, key, BINN_BLOB, pvalue, psize);
1028 }
1029 
binn_object_get_list(void * obj,const char * key,void ** pvalue)1030 ALWAYS_INLINE BOOL binn_object_get_list(void *obj, const char *key, void **pvalue) {
1031   return binn_object_get(obj, key, BINN_LIST, pvalue, NULL);
1032 }
1033 
binn_object_get_map(void * obj,const char * key,void ** pvalue)1034 ALWAYS_INLINE BOOL binn_object_get_map(void *obj, const char *key, void **pvalue) {
1035   return binn_object_get(obj, key, BINN_MAP, pvalue, NULL);
1036 }
1037 
binn_object_get_object(void * obj,const char * key,void ** pvalue)1038 ALWAYS_INLINE BOOL binn_object_get_object(void *obj, const char *key, void **pvalue) {
1039   return binn_object_get(obj, key, BINN_OBJECT, pvalue, NULL);
1040 }
1041 
1042 /***************************************************************************/
1043 
1044 BOOL binn_get_int32(binn *value, int *pint);
1045 BOOL binn_get_int64(binn *value, int64 *pint);
1046 BOOL binn_get_double(binn *value, double *pfloat);
1047 BOOL binn_get_bool(binn *value, BOOL *pbool);
1048 char *binn_get_str(binn *value);
1049 
1050 // boolean string values:
1051 // 1, true, yes, on
1052 // 0, false, no, off
1053 
1054 // boolean number values:
1055 // !=0 [true]
1056 // ==0 [false]
1057 
1058 
1059 #ifdef __cplusplus
1060 }
1061 #endif
1062 
1063 #endif //BINN_H
1064