msgpuck.h

filter: Statementstmt 0.0% 0 / 557 Branchbran 0.0% 0 / 134 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 691 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« msgpack.c ↑ Summary tp.h »
1 #ifndef MSGPUCK_H_INCLUDED
2 #define MSGPUCK_H_INCLUDED
3 /*
4 * Copyright (c) 2013 MsgPuck Authors
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * 1. Redistributions of source code must retain the above
12 * copyright notice, this list of conditions and the
13 * following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials
18 * provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
24 * <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
31 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34  
35 /**
36 * \file msgpuck.h
37 * MsgPuck
38 * \brief MsgPuck is a simple and efficient MsgPack encoder/decoder
39 * library in a single self-contained file.
40 *
41 * Usage example:
42 * \code
43 * // Encode
44 * char buf[1024];
45 * char *w = buf;
46 * w = mp_encode_array(w, 4)
47 * w = mp_encode_uint(w, 10);
48 * w = mp_encode_str(w, "hello world", strlen("hello world"));
49 * w = mp_encode_bool(w, true);
50 * w = mp_encode_double(w, 3.1415);
51 *
52 * // Validate
53 * const char *b = buf;
54 * int r = mp_check(&b);
55 * assert(!r)
56 * assert(b == w);
57 *
58 * // Decode
59 * uint32_t size;
60 * uint64_t ival;
61 * const char *sval;
62 * uint32_t sval_len;
63 * bool bval;
64 * double dval;
65 *
66 * const char *r = buf;
67 *
68 * size = mp_decode_array(&r);
69 * // size is 4
70 *
71 * ival = mp_decode_uint(&r);
72 * // ival is 10;
73 *
74 * sval = mp_decode_str(&r, &sval_len);
75 * // sval is "hello world", sval_len is strlen("hello world")
76 *
77 * bval = mp_decode_bool(&r);
78 * // bval is true
79 *
80 * dval = mp_decode_double(&r);
81 * // dval is 3.1415
82 *
83 * assert(r == w);
84 * \endcode
85 *
86 * \note Supported compilers.
87 * The implementation requires a C99+ or C++03+ compatible compiler.
88 *
89 * \note Inline functions.
90 * The implementation is compatible with both C99 and GNU inline functions.
91 * Please define MP_SOURCE 1 before \#include <msgpuck.h> in a single
92 * compilation unit. This module will be used to store non-inlined versions of
93 * functions and global tables.
94 */
95  
96 #if defined(__cplusplus) && !defined(__STDC_CONSTANT_MACROS)
97 #define __STDC_CONSTANT_MACROS 1 /* make С++ to be happy */
98 #endif
99 #if defined(__cplusplus) && !defined(__STDC_LIMIT_MACROS)
100 #define __STDC_LIMIT_MACROS 1 /* make С++ to be happy */
101 #endif
102 #include <stdlib.h>
103 #include <stdint.h>
104 #include <stddef.h>
105 #include <stdbool.h>
106 #include <string.h>
107 #include <assert.h>
108  
109 #if defined(__cplusplus)
110 extern "C" {
111 #endif /* defined(__cplusplus) */
112  
113 /*
114 * {{{ Platform-specific definitions
115 */
116  
117 /** \cond 0 **/
118  
119 #if defined(__GNUC__) && !defined(__GNUC_STDC_INLINE__)
120 #if !defined(MP_SOURCE)
121 #define MP_PROTO extern inline
122 #define MP_IMPL extern inline
123 #else /* defined(MP_SOURCE) */
124 #define MP_PROTO
125 #define MP_IMPL
126 #endif
127 #define MP_ALWAYSINLINE
128 #else /* C99 inline */
129 #if !defined(MP_SOURCE)
130 #define MP_PROTO inline
131 #define MP_IMPL inline
132 #else /* defined(MP_SOURCE) */
133 #define MP_PROTO extern inline
134 #define MP_IMPL inline
135 #endif
136 #define MP_ALWAYSINLINE __attribute__((always_inline))
137 #endif /* GNU inline or C99 inline */
138  
139 #if !defined __GNUC_MINOR__ || defined __INTEL_COMPILER || \
140 defined __SUNPRO_C || defined __SUNPRO_CC
141 #define MP_GCC_VERSION(major, minor) 0
142 #else
143 #define MP_GCC_VERSION(major, minor) (__GNUC__ > (major) || \
144 (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
145 #endif
146  
147 #if !defined(__has_builtin)
148 #define __has_builtin(x) 0 /* clang */
149 #endif
150  
151 #if MP_GCC_VERSION(2, 9) || __has_builtin(__builtin_expect)
152 #define mp_likely(x) __builtin_expect((x), 1)
153 #define mp_unlikely(x) __builtin_expect((x), 0)
154 #else
155 #define mp_likely(x) (x)
156 #define mp_unlikely(x) (x)
157 #endif
158  
159 #if MP_GCC_VERSION(4, 5) || __has_builtin(__builtin_unreachable)
160 #define mp_unreachable() (assert(0), __builtin_unreachable())
161 #else
162 MP_PROTO void
163 mp_unreachable(void) __attribute__((noreturn));
164 MP_PROTO void
165 mp_unreachable(void) { assert(0); abort(); }
166 #define mp_unreachable() (assert(0))
167 #endif
168  
169 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
170  
171 #if MP_GCC_VERSION(4, 8) || __has_builtin(__builtin_bswap16)
172 #define mp_bswap_u16(x) __builtin_bswap16(x)
173 #else /* !MP_GCC_VERSION(4, 8) */
174 #define mp_bswap_u16(x) ( \
175 (((x) << 8) & 0xff00) | \
176 (((x) >> 8) & 0x00ff) )
177 #endif
178  
179 #if MP_GCC_VERSION(4, 3) || __has_builtin(__builtin_bswap32)
180 #define mp_bswap_u32(x) __builtin_bswap32(x)
181 #else /* !MP_GCC_VERSION(4, 3) */
182 #define mp_bswap_u32(x) ( \
183 (((x) << 24) & UINT32_C(0xff000000)) | \
184 (((x) << 8) & UINT32_C(0x00ff0000)) | \
185 (((x) >> 8) & UINT32_C(0x0000ff00)) | \
186 (((x) >> 24) & UINT32_C(0x000000ff)) )
187 #endif
188  
189 #if MP_GCC_VERSION(4, 3) || __has_builtin(__builtin_bswap64)
190 #define mp_bswap_u64(x) __builtin_bswap64(x)
191 #else /* !MP_GCC_VERSION(4, 3) */
192 #define mp_bswap_u64(x) (\
193 (((x) << 56) & UINT64_C(0xff00000000000000)) | \
194 (((x) << 40) & UINT64_C(0x00ff000000000000)) | \
195 (((x) << 24) & UINT64_C(0x0000ff0000000000)) | \
196 (((x) << 8) & UINT64_C(0x000000ff00000000)) | \
197 (((x) >> 8) & UINT64_C(0x00000000ff000000)) | \
198 (((x) >> 24) & UINT64_C(0x0000000000ff0000)) | \
199 (((x) >> 40) & UINT64_C(0x000000000000ff00)) | \
200 (((x) >> 56) & UINT64_C(0x00000000000000ff)) )
201 #endif
202  
203 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
204  
205 #define mp_bswap_u16(x) (x)
206 #define mp_bswap_u32(x) (x)
207 #define mp_bswap_u64(x) (x)
208  
209 #else
210 #error Unsupported __BYTE_ORDER__
211 #endif
212  
213 #if !defined(__FLOAT_WORD_ORDER__)
214 #define __FLOAT_WORD_ORDER__ __BYTE_ORDER__
215 #endif /* defined(__FLOAT_WORD_ORDER__) */
216  
217 #if __FLOAT_WORD_ORDER__ == __ORDER_LITTLE_ENDIAN__
218 MP_PROTO float
219 mp_bswap_float(float f);
220  
221 MP_PROTO double
222 mp_bswap_double(double d);
223  
224 MP_IMPL float
225 0 mp_bswap_float(float f)
226 {
227 union {
228 float f;
229 uint32_t n;
230 } cast;
231 0 cast.f = f;
232 0 cast.n = mp_bswap_u32(cast.n);
233 0 return cast.f;
234 }
235  
236 MP_IMPL double
237 0 mp_bswap_double(double d)
238 {
239 union {
240 double d;
241 uint64_t n;
242 } cast;
243 0 cast.d = d;
244 0 cast.n = mp_bswap_u64(cast.n);
245 0 return cast.d;
246 }
247 #elif __FLOAT_WORD_ORDER__ == __ORDER_BIG_ENDIAN__
248 #define mp_bswap_float(x) (x)
249 #define mp_bswap_double(x) (x)
250 #else
251 #error Unsupported __FLOAT_WORD_ORDER__
252 #endif
253  
254 /** \endcond */
255  
256 /*
257 * }}}
258 */
259  
260 /*
261 * {{{ API definition
262 */
263  
264 /**
265 * \brief MsgPack data types
266 */
267 enum mp_type {
268 MP_NIL = 0,
269 MP_UINT,
270 MP_INT,
271 MP_STR,
272 MP_BIN,
273 MP_ARRAY,
274 MP_MAP,
275 MP_BOOL,
276 MP_FLOAT,
277 MP_DOUBLE,
278 MP_EXT
279 };
280  
281 /**
282 * \brief Determine MsgPack type by a first byte \a c of encoded data.
283 *
284 * Example usage:
285 * \code
286 * assert(MP_ARRAY == mp_typeof(0x90));
287 * \endcode
288 *
289 * \param c - a first byte of encoded data
290 * \return MsgPack type
291 */
292 MP_PROTO __attribute__((pure)) enum mp_type
293 mp_typeof(const char c);
294  
295 /**
296 * \brief Calculate exact buffer size needed to store an array header of
297 * \a size elements. Maximum return value is 5. For performance reasons you
298 * can preallocate buffer for maximum size without calling the function.
299 * \param size - a number of elements
300 * \return buffer size in bytes (max is 5)
301 */
302 MP_PROTO __attribute__((const)) uint32_t
303 mp_sizeof_array(uint32_t size);
304  
305 /**
306 * \brief Encode an array header of \a size elements.
307 *
308 * All array members must be encoded after the header.
309 *
310 * Example usage:
311 * \code
312 * // Encode
313 * char buf[1024];
314 * char *w = buf;
315 * w = mp_encode_array(w, 2)
316 * w = mp_encode_uint(w, 10);
317 * w = mp_encode_uint(w, 15);
318 *
319 * // Decode
320 * const char *r = buf;
321 * uint32_t size = mp_decode_array(&r);
322 * for (uint32_t i = 0; i < size; i++) {
323 * uint64_t val = mp_decode_uint(&r);
324 * }
325 * assert (r == w);
326 * \endcode
327 * It is your responsibility to ensure that \a data has enough space.
328 * \param data - a buffer
329 * \param size - a number of elements
330 * \return \a data + \link mp_sizeof_array() mp_sizeof_array(size) \endlink
331 * \sa mp_sizeof_array
332 */
333 MP_PROTO char *
334 mp_encode_array(char *data, uint32_t size);
335  
336 /**
337 * \brief Check that \a cur buffer has enough bytes to decode an array header
338 * \param cur buffer
339 * \param end end of the buffer
340 * \retval 0 - buffer has enough bytes
341 * \retval > 0 - the number of remaining bytes to read
342 * \pre cur < end
343 * \pre mp_typeof(*cur) == MP_ARRAY
344 */
345 MP_PROTO __attribute__((pure)) ptrdiff_t
346 mp_check_array(const char *cur, const char *end);
347  
348 /**
349 * \brief Decode an array header from MsgPack \a data.
350 *
351 * All array members must be decoded after the header.
352 * \param data - the pointer to a buffer
353 * \return the number of elements in an array
354 * \post *data = *data + mp_sizeof_array(retval)
355 * \sa \link mp_encode_array() An usage example \endlink
356 */
357 MP_PROTO uint32_t
358 mp_decode_array(const char **data);
359  
360 /**
361 * \brief Calculate exact buffer size needed to store a map header of
362 * \a size elements. Maximum return value is 5. For performance reasons you
363 * can preallocate buffer for maximum size without calling the function.
364 * \param size - a number of elements
365 * \return buffer size in bytes (max is 5)
366 */
367 MP_PROTO __attribute__((const)) uint32_t
368 mp_sizeof_map(uint32_t size);
369  
370 /**
371 * \brief Encode a map header of \a size elements.
372 *
373 * All map key-value pairs must be encoded after the header.
374 *
375 * Example usage:
376 * \code
377 * char buf[1024];
378 *
379 * // Encode
380 * char *w = buf;
381 * w = mp_encode_map(b, 2);
382 * w = mp_encode_str(b, "key1", 4);
383 * w = mp_encode_str(b, "value1", 6);
384 * w = mp_encode_str(b, "key2", 4);
385 * w = mp_encode_str(b, "value2", 6);
386 *
387 * // Decode
388 * const char *r = buf;
389 * uint32_t size = mp_decode_map(&r);
390 * for (uint32_t i = 0; i < size; i++) {
391 * // Use switch(mp_typeof(**r)) to support more types
392 * uint32_t key_len, val_len;
393 * const char *key = mp_decode_str(&r, key_len);
394 * const char *val = mp_decode_str(&r, val_len);
395 * }
396 * assert (r == w);
397 * \endcode
398 * It is your responsibility to ensure that \a data has enough space.
399 * \param data - a buffer
400 * \param size - a number of key/value pairs
401 * \return \a data + \link mp_sizeof_map() mp_sizeof_map(size)\endlink
402 * \sa mp_sizeof_map
403 */
404 MP_PROTO char *
405 mp_encode_map(char *data, uint32_t size);
406  
407 /**
408 * \brief Check that \a cur buffer has enough bytes to decode a map header
409 * \param cur buffer
410 * \param end end of the buffer
411 * \retval 0 - buffer has enough bytes
412 * \retval > 0 - the number of remaining bytes to read
413 * \pre cur < end
414 * \pre mp_typeof(*cur) == MP_MAP
415 */
416 MP_PROTO __attribute__((pure)) ptrdiff_t
417 mp_check_map(const char *cur, const char *end);
418  
419 /**
420 * \brief Decode a map header from MsgPack \a data.
421 *
422 * All map key-value pairs must be decoded after the header.
423 * \param data - the pointer to a buffer
424 * \return the number of key/value pairs in a map
425 * \post *data = *data + mp_sizeof_array(retval)
426 * \sa \link mp_encode_map() An usage example \endlink
427 */
428 MP_PROTO uint32_t
429 mp_decode_map(const char **data);
430  
431 /**
432 * \brief Calculate exact buffer size needed to store an integer \a num.
433 * Maximum return value is 9. For performance reasons you can preallocate
434 * buffer for maximum size without calling the function.
435 * Example usage:
436 * \code
437 * char **data = ...;
438 * char *end = *data;
439 * my_buffer_ensure(mp_sizeof_uint(x), &end);
440 * // my_buffer_ensure(9, &end);
441 * mp_encode_uint(buffer, x);
442 * \endcode
443 * \param num - a number
444 * \return buffer size in bytes (max is 9)
445 */
446 MP_PROTO __attribute__((const)) uint32_t
447 mp_sizeof_uint(uint64_t num);
448  
449 /**
450 * \brief Calculate exact buffer size needed to store an integer \a num.
451 * Maximum return value is 9. For performance reasons you can preallocate
452 * buffer for maximum size without calling the function.
453 * \param num - a number
454 * \return buffer size in bytes (max is 9)
455 * \pre \a num < 0
456 */
457 MP_PROTO __attribute__((const)) uint32_t
458 mp_sizeof_int(int64_t num);
459  
460 /**
461 * \brief Encode an unsigned integer \a num.
462 * It is your responsibility to ensure that \a data has enough space.
463 * \param data - a buffer
464 * \param num - a number
465 * \return \a data + mp_sizeof_uint(\a num)
466 * \sa \link mp_encode_array() An usage example \endlink
467 * \sa mp_sizeof_uint()
468 */
469 MP_PROTO char *
470 mp_encode_uint(char *data, uint64_t num);
471  
472 /**
473 * \brief Encode a signed integer \a num.
474 * It is your responsibility to ensure that \a data has enough space.
475 * \param data - a buffer
476 * \param num - a number
477 * \return \a data + mp_sizeof_int(\a num)
478 * \sa \link mp_encode_array() An usage example \endlink
479 * \sa mp_sizeof_int()
480 * \pre \a num < 0
481 */
482 MP_PROTO char *
483 mp_encode_int(char *data, int64_t num);
484  
485 /**
486 * \brief Check that \a cur buffer has enough bytes to decode an uint
487 * \param cur buffer
488 * \param end end of the buffer
489 * \retval 0 - buffer has enough bytes
490 * \retval > 0 - the number of remaining bytes to read
491 * \pre cur < end
492 * \pre mp_typeof(*cur) == MP_UINT
493 */
494 MP_PROTO __attribute__((pure)) ptrdiff_t
495 mp_check_uint(const char *cur, const char *end);
496  
497 /**
498 * \brief Check that \a cur buffer has enough bytes to decode an int
499 * \param cur buffer
500 * \param end end of the buffer
501 * \retval 0 - buffer has enough bytes
502 * \retval > 0 - the number of remaining bytes to read
503 * \pre cur < end
504 * \pre mp_typeof(*cur) == MP_INT
505 */
506 MP_PROTO __attribute__((pure)) ptrdiff_t
507 mp_check_int(const char *cur, const char *end);
508  
509 /**
510 * \brief Decode an unsigned integer from MsgPack \a data
511 * \param data - the pointer to a buffer
512 * \return an unsigned number
513 * \post *data = *data + mp_sizeof_uint(retval)
514 */
515 MP_PROTO uint64_t
516 mp_decode_uint(const char **data);
517  
518 /**
519 * \brief Decode a signed integer from MsgPack \a data
520 * \param data - the pointer to a buffer
521 * \return an unsigned number
522 * \post *data = *data + mp_sizeof_int(retval)
523 */
524 MP_PROTO int64_t
525 mp_decode_int(const char **data);
526  
527 /**
528 * \brief Compare two packed unsigned integers.
529 *
530 * The function is faster than two mp_decode_uint() calls.
531 * \param data_a unsigned int a
532 * \param data_b unsigned int b
533 * \retval < 0 when \a a < \a b
534 * \retval 0 when \a a == \a b
535 * \retval > 0 when \a a > \a b
536 */
537 MP_PROTO __attribute__((pure)) int
538 mp_compare_uint(const char *data_a, const char *data_b);
539  
540 /**
541 * \brief Calculate exact buffer size needed to store a float \a num.
542 * The return value is always 5. The function was added to provide integrity of
543 * the library.
544 * \param num - a float
545 * \return buffer size in bytes (always 5)
546 */
547 MP_PROTO __attribute__((const)) uint32_t
548 mp_sizeof_float(float num);
549  
550 /**
551 * \brief Calculate exact buffer size needed to store a double \a num.
552 * The return value is either 5 or 9. The function was added to provide
553 * integrity of the library. For performance reasons you can preallocate buffer
554 * for maximum size without calling the function.
555 * \param num - a double
556 * \return buffer size in bytes (5 or 9)
557 */
558 MP_PROTO __attribute__((const)) uint32_t
559 mp_sizeof_double(double num);
560  
561 /**
562 * \brief Encode a float \a num.
563 * It is your responsibility to ensure that \a data has enough space.
564 * \param data - a buffer
565 * \param num - a float
566 * \return \a data + mp_sizeof_float(\a num)
567 * \sa mp_sizeof_float()
568 * \sa \link mp_encode_array() An usage example \endlink
569 */
570 MP_PROTO char *
571 mp_encode_float(char *data, float num);
572  
573 /**
574 * \brief Encode a double \a num.
575 * It is your responsibility to ensure that \a data has enough space.
576 * \param data - a buffer
577 * \param num - a float
578 * \return \a data + mp_sizeof_double(\a num)
579 * \sa \link mp_encode_array() An usage example \endlink
580 * \sa mp_sizeof_double()
581 */
582 MP_PROTO char *
583 mp_encode_double(char *data, double num);
584  
585 /**
586 * \brief Check that \a cur buffer has enough bytes to decode a float
587 * \param cur buffer
588 * \param end end of the buffer
589 * \retval 0 - buffer has enough bytes
590 * \retval > 0 - the number of remaining bytes to read
591 * \pre cur < end
592 * \pre mp_typeof(*cur) == MP_FLOAT
593 */
594 MP_PROTO __attribute__((pure)) ptrdiff_t
595 mp_check_float(const char *cur, const char *end);
596  
597 /**
598 * \brief Check that \a cur buffer has enough bytes to decode a double
599 * \param cur buffer
600 * \param end end of the buffer
601 * \retval 0 - buffer has enough bytes
602 * \retval > 0 - the number of remaining bytes to read
603 * \pre cur < end
604 * \pre mp_typeof(*cur) == MP_DOUBLE
605 */
606 MP_PROTO __attribute__((pure)) ptrdiff_t
607 mp_check_double(const char *cur, const char *end);
608  
609 /**
610 * \brief Decode a float from MsgPack \a data
611 * \param data - the pointer to a buffer
612 * \return a float
613 * \post *data = *data + mp_sizeof_float(retval)
614 */
615 MP_PROTO float
616 mp_decode_float(const char **data);
617  
618 /**
619 * \brief Decode a double from MsgPack \a data
620 * \param data - the pointer to a buffer
621 * \return a double
622 * \post *data = *data + mp_sizeof_double(retval)
623 */
624 MP_PROTO double
625 mp_decode_double(const char **data);
626  
627 /**
628 * \brief Calculate exact buffer size needed to store a string header of
629 * length \a num. Maximum return value is 5. For performance reasons you can
630 * preallocate buffer for maximum size without calling the function.
631 * \param len - a string length
632 * \return size in chars (max is 5)
633 */
634 MP_PROTO __attribute__((const)) uint32_t
635 mp_sizeof_strl(uint32_t len);
636  
637 /**
638 * \brief Equivalent to mp_sizeof_strl(\a len) + \a len.
639 * \param len - a string length
640 * \return size in chars (max is 5 + \a len)
641 */
642 MP_PROTO __attribute__((const)) uint32_t
643 mp_sizeof_str(uint32_t len);
644  
645 /**
646 * \brief Calculate exact buffer size needed to store a binstring header of
647 * length \a num. Maximum return value is 5. For performance reasons you can
648 * preallocate buffer for maximum size without calling the function.
649 * \param len - a string length
650 * \return size in chars (max is 5)
651 */
652 MP_PROTO __attribute__((const)) uint32_t
653 mp_sizeof_binl(uint32_t len);
654  
655 /**
656 * \brief Equivalent to mp_sizeof_binl(\a len) + \a len.
657 * \param len - a string length
658 * \return size in chars (max is 5 + \a len)
659 */
660 MP_PROTO __attribute__((const)) uint32_t
661 mp_sizeof_bin(uint32_t len);
662  
663 /**
664 * \brief Encode a string header of length \a len.
665 *
666 * The function encodes MsgPack header (\em only header) for a string of
667 * length \a len. You should append actual string data to the buffer manually
668 * after encoding the header (exactly \a len bytes without trailing '\0').
669 *
670 * This approach is very useful for cases when the total length of the string
671 * is known in advance, but the string data is not stored in a single
672 * continuous buffer (e.g. network packets).
673 *
674 * It is your responsibility to ensure that \a data has enough space.
675 * Usage example:
676 * \code
677 * char buffer[1024];
678 * char *b = buffer;
679 * b = mp_encode_strl(b, hdr.total_len);
680 * char *s = b;
681 * memcpy(b, pkt1.data, pkt1.len)
682 * b += pkt1.len;
683 * // get next packet
684 * memcpy(b, pkt2.data, pkt2.len)
685 * b += pkt2.len;
686 * // get next packet
687 * memcpy(b, pkt1.data, pkt3.len)
688 * b += pkt3.len;
689 *
690 * // Check that all data was received
691 * assert(hdr.total_len == (uint32_t) (b - s))
692 * \endcode
693 * Hint: you can dynamically reallocate the buffer during the process.
694 * \param data - a buffer
695 * \param len - a string length
696 * \return \a data + mp_sizeof_strl(len)
697 * \sa mp_sizeof_strl()
698 */
699 MP_PROTO char *
700 mp_encode_strl(char *data, uint32_t len);
701  
702 /**
703 * \brief Encode a string of length \a len.
704 * The function is equivalent to mp_encode_strl() + memcpy.
705 * \param data - a buffer
706 * \param str - a pointer to string data
707 * \param len - a string length
708 * \return \a data + mp_sizeof_str(len) ==
709 * data + mp_sizeof_strl(len) + len
710 * \sa mp_encode_strl
711 */
712 MP_PROTO char *
713 mp_encode_str(char *data, const char *str, uint32_t len);
714  
715 /**
716 * \brief Encode a binstring header of length \a len.
717 * See mp_encode_strl() for more details.
718 * \param data - a bufer
719 * \param len - a string length
720 * \return data + mp_sizeof_binl(\a len)
721 * \sa mp_encode_strl
722 */
723 MP_PROTO char *
724 mp_encode_binl(char *data, uint32_t len);
725  
726 /**
727 * \brief Encode a binstring of length \a len.
728 * The function is equivalent to mp_encode_binl() + memcpy.
729 * \param data - a buffer
730 * \param str - a pointer to binstring data
731 * \param len - a binstring length
732 * \return \a data + mp_sizeof_bin(\a len) ==
733 * data + mp_sizeof_binl(\a len) + \a len
734 * \sa mp_encode_strl
735 */
736 MP_PROTO char *
737 mp_encode_bin(char *data, const char *str, uint32_t len);
738  
739 /**
740 * \brief Check that \a cur buffer has enough bytes to decode a string header
741 * \param cur buffer
742 * \param end end of the buffer
743 * \retval 0 - buffer has enough bytes
744 * \retval > 0 - the number of remaining bytes to read
745 * \pre cur < end
746 * \pre mp_typeof(*cur) == MP_STR
747 */
748 MP_PROTO __attribute__((pure)) ptrdiff_t
749 mp_check_strl(const char *cur, const char *end);
750  
751 /**
752 * \brief Check that \a cur buffer has enough bytes to decode a binstring header
753 * \param cur buffer
754 * \param end end of the buffer
755 * \retval 0 - buffer has enough bytes
756 * \retval > 0 - the number of remaining bytes to read
757 * \pre cur < end
758 * \pre mp_typeof(*cur) == MP_BIN
759 */
760 MP_PROTO __attribute__((pure)) ptrdiff_t
761 mp_check_binl(const char *cur, const char *end);
762  
763 /**
764 * \brief Decode a length of a string from MsgPack \a data
765 * \param data - the pointer to a buffer
766 * \return a length of astring
767 * \post *data = *data + mp_sizeof_strl(retval)
768 * \sa mp_encode_strl
769 */
770 MP_PROTO uint32_t
771 mp_decode_strl(const char **data);
772  
773 /**
774 * \brief Decode a string from MsgPack \a data
775 * \param data - the pointer to a buffer
776 * \param len - the pointer to save a string length
777 * \return a pointer to a decoded string
778 * \post *data = *data + mp_sizeof_str(*len)
779 * \sa mp_encode_binl
780 */
781 MP_PROTO const char *
782 mp_decode_str(const char **data, uint32_t *len);
783  
784 /**
785 * \brief Decode a length of a binstring from MsgPack \a data
786 * \param data - the pointer to a buffer
787 * \return a length of a binstring
788 * \post *data = *data + mp_sizeof_binl(retval)
789 * \sa mp_encode_binl
790 */
791 MP_PROTO uint32_t
792 mp_decode_binl(const char **data);
793  
794 /**
795 * \brief Decode a binstring from MsgPack \a data
796 * \param data - the pointer to a buffer
797 * \param len - the pointer to save a binstring length
798 * \return a pointer to a decoded binstring
799 * \post *data = *data + mp_sizeof_str(*len)
800 * \sa mp_encode_binl
801 */
802 MP_PROTO const char *
803 mp_decode_bin(const char **data, uint32_t *len);
804  
805 /**
806 * \brief Calculate exact buffer size needed to store the nil value.
807 * The return value is always 1. The function was added to provide integrity of
808 * the library.
809 * \return buffer size in bytes (always 1)
810 */
811 MP_PROTO __attribute__((const)) uint32_t
812 mp_sizeof_nil(void);
813  
814 /**
815 * \brief Encode the nil value.
816 * It is your responsibility to ensure that \a data has enough space.
817 * \param data - a buffer
818 * \return \a data + mp_sizeof_nil()
819 * \sa \link mp_encode_array() An usage example \endlink
820 * \sa mp_sizeof_nil()
821 */
822 MP_PROTO char *
823 mp_encode_nil(char *data);
824  
825 /**
826 * \brief Check that \a cur buffer has enough bytes to decode nil
827 * \param cur buffer
828 * \param end end of the buffer
829 * \retval 0 - buffer has enough bytes
830 * \retval > 0 - the number of remaining bytes to read
831 * \pre cur < end
832 * \pre mp_typeof(*cur) == MP_NIL
833 */
834 MP_PROTO __attribute__((pure)) ptrdiff_t
835 mp_check_nil(const char *cur, const char *end);
836  
837 /**
838 * \brief Decode the nil value from MsgPack \a data
839 * \param data - the pointer to a buffer
840 * \post *data = *data + mp_sizeof_nil()
841 */
842 MP_PROTO void
843 mp_decode_nil(const char **data);
844  
845 /**
846 * \brief Calculate exact buffer size needed to store a boolean value.
847 * The return value is always 1. The function was added to provide integrity of
848 * the library.
849 * \return buffer size in bytes (always 1)
850 */
851 MP_PROTO __attribute__((const)) uint32_t
852 mp_sizeof_bool(bool val);
853  
854 /**
855 * \brief Encode a bool value \a val.
856 * It is your responsibility to ensure that \a data has enough space.
857 * \param data - a buffer
858 * \param val - a bool
859 * \return \a data + mp_sizeof_bool(val)
860 * \sa \link mp_encode_array() An usage example \endlink
861 * \sa mp_sizeof_bool()
862 */
863 MP_PROTO char *
864 mp_encode_bool(char *data, bool val);
865  
866 /**
867 * \brief Check that \a cur buffer has enough bytes to decode a bool value
868 * \param cur buffer
869 * \param end end of the buffer
870 * \retval 0 - buffer has enough bytes
871 * \retval > 0 - the number of remaining bytes to read
872 * \pre cur < end
873 * \pre mp_typeof(*cur) == MP_BOOL
874 */
875 MP_PROTO __attribute__((pure)) ptrdiff_t
876 mp_check_bool(const char *cur, const char *end);
877  
878 /**
879 * \brief Decode a bool value from MsgPack \a data
880 * \param data - the pointer to a buffer
881 * \return a decoded bool value
882 * \post *data = *data + mp_sizeof_bool(retval)
883 */
884 MP_PROTO bool
885 mp_decode_bool(const char **data);
886  
887 /**
888 * \brief Skip one element in a packed \a data.
889 *
890 * The function is faster than mp_typeof + mp_decode_XXX() combination.
891 * For arrays and maps the function also skips all members.
892 * For strings and binstrings the function also skips the string data.
893 *
894 * Usage example:
895 * \code
896 * char buf[1024];
897 *
898 * char *w = buf;
899 * // First MsgPack object
900 * w = mp_encode_uint(w, 10);
901 *
902 * // Second MsgPack object
903 * w = mp_encode_array(w, 4);
904 * w = mp_encode_array(w, 2);
905 * // Begin of an inner array
906 * w = mp_encode_str(w, "second inner 1", 14);
907 * w = mp_encode_str(w, "second inner 2", 14);
908 * // End of an inner array
909 * w = mp_encode_str(w, "second", 6);
910 * w = mp_encode_uint(w, 20);
911 * w = mp_encode_bool(w, true);
912 *
913 * // Third MsgPack object
914 * w = mp_encode_str(w, "third", 5);
915 * // EOF
916 *
917 * const char *r = buf;
918 *
919 * // First MsgPack object
920 * assert(mp_typeof(**r) == MP_UINT);
921 * mp_next(&r); // skip the first object
922 *
923 * // Second MsgPack object
924 * assert(mp_typeof(**r) == MP_ARRAY);
925 * mp_decode_array(&r);
926 * assert(mp_typeof(**r) == MP_ARRAY); // inner array
927 * mp_next(&r); // -->> skip the entire inner array (with all members)
928 * assert(mp_typeof(**r) == MP_STR); // second
929 * mp_next(&r);
930 * assert(mp_typeof(**r) == MP_UINT); // 20
931 * mp_next(&r);
932 * assert(mp_typeof(**r) == MP_BOOL); // true
933 * mp_next(&r);
934 *
935 * // Third MsgPack object
936 * assert(mp_typeof(**r) == MP_STR); // third
937 * mp_next(&r);
938 *
939 * assert(r == w); // EOF
940 *
941 * \endcode
942 * \param data - the pointer to a buffer
943 * \post *data = *data + mp_sizeof_TYPE() where TYPE is mp_typeof(**data)
944 */
945 MP_PROTO void
946 mp_next(const char **data);
947  
948 /**
949 * \brief Equivalent to mp_next() but also validates MsgPack in \a data.
950 * \param data - the pointer to a buffer
951 * \param end - the end of a buffer
952 * \retval 0 when MsgPack in \a data is valid.
953 * \retval != 0 when MsgPack in \a data is not valid.
954 * \post *data = *data + mp_sizeof_TYPE() where TYPE is mp_typeof(**data)
955 * \post *data is not defined if MsgPack is not valid
956 * \sa mp_next()
957 */
958 MP_PROTO int
959 mp_check(const char **data, const char *end);
960  
961 /*
962 * }}}
963 */
964  
965 /*
966 * {{{ Implementation
967 */
968  
969 /** \cond 0 */
970 extern const enum mp_type mp_type_hint[];
971 extern const int8_t mp_parser_hint[];
972  
973 MP_IMPL MP_ALWAYSINLINE enum mp_type
974 0 mp_typeof(const char c)
975 {
976 0 return mp_type_hint[(unsigned char) c];
977 }
978  
979 MP_IMPL uint32_t
980 0 mp_sizeof_array(uint32_t size)
981 {
982 0 ▶ if (size <= 15) {
Branch: 1 branch
BranchTrueFalseTotal
if (size <= 15) { 0 0 0
983 0 return 1;
984 0 ▶ } else if (size <= UINT16_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (size <= UINT16_MAX) { 0 0 0
985 0 return 1 + sizeof(uint16_t);
986 } else {
987 0 return 1 + sizeof(uint32_t);
988 }
989 }
990  
991 MP_IMPL char *
992 0 mp_encode_array(char *data, uint32_t size)
993 {
994 0 ▶ if (size <= 15) {
Branch: 1 branch
BranchTrueFalseTotal
if (size <= 15) { 0 0 0
995 0 *(unsigned char *) (data++) = 0x90 | size;
996 0 return data;
997 0 ▶ } else if (size <= UINT16_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (size <= UINT16_MAX) { 0 0 0
998 0 *(data++) = 0xdc;
999 0 *(uint16_t *) data = mp_bswap_u16(size);
1000 0 return data + sizeof(uint16_t);
1001 } else {
1002 0 *(data++) = 0xdd;
1003 0 *(uint32_t *) data = mp_bswap_u32(size);
1004 0 return data + sizeof(uint32_t);
1005 }
1006 }
1007  
1008 MP_IMPL ptrdiff_t
1009 0 mp_check_array(const char *cur, const char *end)
1010 {
1011 assert(cur < end);
1012 assert(mp_typeof(*cur) == MP_ARRAY);
1013 0 unsigned char c = *(unsigned char *) cur;
1014 0 ▶ if (mp_likely(!(c & 0x40)))
Branch: 1 branch
BranchTrueFalseTotal
if (mp_likely(!(c & 0x40))) 0 0 0
1015 0 return 1 - (end - cur);
1016  
1017 assert(c >= 0xdc && c <= 0xdd); /* must be checked above by mp_typeof */
1018 0 uint32_t hsize = 2U << (c & 0x1); /* 0xdc->2, 0xdd->4 */
1019 0 return 1 + hsize - (end - cur);
1020 }
1021  
1022 MP_PROTO uint32_t
1023 mp_decode_array_slowpath(unsigned char c, const char **data);
1024  
1025 MP_IMPL uint32_t
1026 0 mp_decode_array_slowpath(unsigned char c, const char **data)
1027 {
1028 uint32_t size;
1029 0 switch (c & 0x1) {
1030 0 case 0xdc & 0x1:
1031 0 size = mp_bswap_u16(*(uint16_t *) *data);
1032 0 *data += sizeof(uint16_t);
1033 0 return size;
1034 0 case 0xdd & 0x1:
1035 0 size = mp_bswap_u32(*(uint32_t *) *data);
1036 0 *data += sizeof(uint32_t);
1037 0 return size;
1038 0 default:
1039 0 mp_unreachable();
1040 }
1041 }
1042  
1043 MP_IMPL MP_ALWAYSINLINE uint32_t
1044 0 mp_decode_array(const char **data)
1045 {
1046 0 unsigned char c = (unsigned char) **data;
1047 0 *data += 1;
1048  
1049 0 ▶ if (mp_likely(!(c & 0x40)))
Branch: 2 branches
BranchTrueFalseTotal
if (mp_likely(!(c & 0x40))) 0 0 0
if (mp_likely(!(c & 0x40))) 0 0 0
1050 0 return (c & 0xf);
1051  
1052 0 return mp_decode_array_slowpath(c, data);
1053 }
1054  
1055 MP_IMPL uint32_t
1056 0 mp_sizeof_map(uint32_t size)
1057 {
1058 0 ▶ if (size <= 15) {
Branch: 1 branch
BranchTrueFalseTotal
if (size <= 15) { 0 0 0
1059 0 return 1;
1060 0 ▶ } else if (size <= UINT16_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (size <= UINT16_MAX) { 0 0 0
1061 0 return 1 + sizeof(uint16_t);
1062 } else {
1063 0 return 1 + sizeof(uint32_t);
1064 }
1065 }
1066  
1067 MP_IMPL char *
1068 0 mp_encode_map(char *data, uint32_t size)
1069 {
1070 0 ▶ if (size <= 15) {
Branch: 1 branch
BranchTrueFalseTotal
if (size <= 15) { 0 0 0
1071 0 *(data++) = 0x80 | (char) size;
1072 0 return data;
1073 0 ▶ } else if (size <= UINT16_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (size <= UINT16_MAX) { 0 0 0
1074 0 *(data++) = 0xde;
1075 0 *(uint16_t *) data = mp_bswap_u16(size);
1076 0 return data + sizeof(uint16_t);
1077 } else {
1078 0 *(data++) = 0xdf;
1079 0 *(uint32_t *) data = mp_bswap_u32(size);
1080 0 return data + sizeof(uint32_t);
1081 }
1082 }
1083  
1084 MP_IMPL ptrdiff_t
1085 0 mp_check_map(const char *cur, const char *end)
1086 {
1087 assert(cur < end);
1088 assert(mp_typeof(*cur) == MP_MAP);
1089 0 unsigned char c = *(unsigned char *) cur;
1090 0 ▶ if (mp_likely((c & ~0xfU) == 0x80))
Branch: 1 branch
BranchTrueFalseTotal
if (mp_likely((c & ~0xfU) == 0x80)) 0 0 0
1091 0 return 1 - (end - cur);
1092  
1093 assert(c >= 0xde && c <= 0xdf); /* must be checked above by mp_typeof */
1094 0 uint32_t hsize = 2U << (c & 0x1); /* 0xde->2, 0xdf->4 */
1095 0 return 1 + hsize - (end - cur);
1096 }
1097  
1098 MP_IMPL uint32_t
1099 0 mp_decode_map(const char **data)
1100 {
1101 0 unsigned char c = (unsigned char) **data;
1102 0 *data += 1;
1103 uint32_t size;
1104 0 switch (c) {
1105 0 case 0x80 ... 0x8f:
1106 0 return c & 0xf;
1107 0 case 0xde:
1108 0 size = mp_bswap_u16(*(uint16_t *) *data);
1109 0 *data += sizeof(uint16_t);
1110 0 return size;
1111 0 case 0xdf:
1112 0 size = mp_bswap_u32(*(uint32_t *) *data);
1113 0 *data += sizeof(uint32_t);
1114 0 return size;
1115 0 default:
1116 0 mp_unreachable();
1117 }
1118 }
1119  
1120 MP_IMPL uint32_t
1121 0 mp_sizeof_uint(uint64_t num)
1122 {
1123 0 ▶ if (num <= 0x7f) {
Branch: 1 branch
BranchTrueFalseTotal
if (num <= 0x7f) { 0 0 0
1124 0 return 1;
1125 0 ▶ } else if (num <= UINT8_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (num <= UINT8_MAX) { 0 0 0
1126 0 return 1 + sizeof(uint8_t);
1127 0 ▶ } else if (num <= UINT16_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (num <= UINT16_MAX) { 0 0 0
1128 0 return 1 + sizeof(uint16_t);
1129 0 ▶ } else if (num <= UINT32_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (num <= UINT32_MAX) { 0 0 0
1130 0 return 1 + sizeof(uint32_t);
1131 } else {
1132 0 return 1 + sizeof(uint64_t);
1133 }
1134 }
1135  
1136 MP_IMPL uint32_t
1137 0 mp_sizeof_int(int64_t num)
1138 {
1139 assert(num < 0);
1140 0 ▶ if (num >= -0x20) {
Branch: 1 branch
BranchTrueFalseTotal
if (num >= -0x20) { 0 0 0
1141 0 return 1;
1142 0 ▶ } else if (num >= INT8_MIN && num <= INT8_MAX) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (num >= INT8_MIN && num <= INT8_MAX) { 0 0 0
} else if (num >= INT8_MIN && num <= INT8_MAX) { 0 0 0
1143 0 return 1 + sizeof(int8_t);
1144 0 ▶ } else if (num >= INT16_MIN && num <= UINT16_MAX) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (num >= INT16_MIN && num <= UINT16_MAX) { 0 0 0
} else if (num >= INT16_MIN && num <= UINT16_MAX) { 0 0 0
1145 0 return 1 + sizeof(int16_t);
1146 0 ▶ } else if (num >= INT32_MIN && num <= UINT32_MAX) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (num >= INT32_MIN && num <= UINT32_MAX) { 0 0 0
} else if (num >= INT32_MIN && num <= UINT32_MAX) { 0 0 0
1147 0 return 1 + sizeof(int32_t);
1148 } else {
1149 0 return 1 + sizeof(int64_t);
1150 }
1151 }
1152  
1153 MP_IMPL ptrdiff_t
1154 0 mp_check_uint(const char *cur, const char *end)
1155 {
1156 assert(cur < end);
1157 assert(mp_typeof(*cur) == MP_UINT);
1158 0 return 1 + mp_parser_hint[*(unsigned char *) cur] - (end - cur);
1159 }
1160  
1161 MP_IMPL ptrdiff_t
1162 0 mp_check_int(const char *cur, const char *end)
1163 {
1164 assert(cur < end);
1165 assert(mp_typeof(*cur) == MP_INT);
1166 0 return 1 + mp_parser_hint[*(unsigned char *) cur] - (end - cur);
1167 }
1168  
1169 MP_IMPL char *
1170 0 mp_encode_uint(char *data, uint64_t num)
1171 {
1172 0 ▶ if (num <= 0x7f) {
Branch: 1 branch
BranchTrueFalseTotal
if (num <= 0x7f) { 0 0 0
1173 0 *data = num;
1174 0 return data + 1;
1175 0 ▶ } else if (num <= UINT8_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (num <= UINT8_MAX) { 0 0 0
1176 0 *data = 0xcc;
1177 0 data++;
1178 0 *(uint8_t *) data = num;
1179 0 return data + sizeof(uint8_t);
1180 0 ▶ } else if (num <= UINT16_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (num <= UINT16_MAX) { 0 0 0
1181 0 *data = 0xcd;
1182 0 data++;
1183 0 *(uint16_t *) data = mp_bswap_u16(num);
1184 0 return data + sizeof(uint16_t);
1185 0 ▶ } else if (num <= UINT32_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (num <= UINT32_MAX) { 0 0 0
1186 0 *data = 0xce;
1187 0 data++;
1188 0 *(uint32_t *) data = mp_bswap_u32(num);
1189 0 return data + sizeof(uint32_t);
1190 } else {
1191 0 *data = 0xcf;
1192 0 data++;
1193 0 *(uint64_t *) data = mp_bswap_u64(num);
1194 0 return data + sizeof(uint64_t);
1195 }
1196 }
1197  
1198 MP_IMPL char *
1199 0 mp_encode_int(char *data, int64_t num)
1200 {
1201 assert(num < 0);
1202 0 ▶ if (num >= -0x20) {
Branch: 1 branch
BranchTrueFalseTotal
if (num >= -0x20) { 0 0 0
1203 0 *data = (0xe0 | num);
1204 0 return data + 1;
1205 0 ▶ } else if (num >= INT8_MIN) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (num >= INT8_MIN) { 0 0 0
1206 0 *data = 0xd0;
1207 0 data++;
1208 0 *(int8_t *) data = num;
1209 0 return data + sizeof(int8_t);
1210 0 ▶ } else if (num >= INT16_MIN) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (num >= INT16_MIN) { 0 0 0
1211 0 *data = 0xd1;
1212 0 data++;
1213 0 *(int16_t *) data = mp_bswap_u16(num);
1214 0 return data + sizeof(int16_t);
1215 0 ▶ } else if (num >= INT32_MIN) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (num >= INT32_MIN) { 0 0 0
1216 0 *data = 0xd2;
1217 0 data++;
1218 0 *(int32_t *) data = mp_bswap_u32(num);
1219 0 return data + sizeof(int32_t);
1220 } else {
1221 0 *data = 0xd3;
1222 0 data++;
1223 0 *(int64_t *) data = mp_bswap_u64(num);
1224 0 return data + sizeof(int64_t);
1225 }
1226 }
1227  
1228 MP_IMPL uint64_t
1229 0 mp_decode_uint(const char **data)
1230 {
1231 0 unsigned char c = (unsigned char) **data;
1232 0 *data += 1;
1233 uint64_t val;
1234  
1235 0 switch (c) {
1236 0 case 0x00 ... 0x7f:
1237 0 return c;
1238 0 case 0xcc:
1239 0 val = *(uint8_t *) *data;
1240 0 *data += sizeof(uint8_t);
1241 0 return val;
1242 0 case 0xcd:
1243 0 val = mp_bswap_u16(*(uint16_t *) *data);
1244 0 *data += sizeof(uint16_t);
1245 0 return val;
1246 0 case 0xce:
1247 0 val = mp_bswap_u32(*(uint32_t *) *data);
1248 0 *data += sizeof(uint32_t);
1249 0 return val;
1250 0 case 0xcf:
1251 0 val = mp_bswap_u64(*(uint64_t *) *data);
1252 0 *data += sizeof(uint64_t);
1253 0 return val;
1254 0 default:
1255 0 mp_unreachable();
1256 }
1257 }
1258  
1259 MP_IMPL int
1260 0 mp_compare_uint(const char *data_a, const char *data_b)
1261 {
1262 0 unsigned char ca = (unsigned char) *data_a;
1263 0 unsigned char cb = (unsigned char) *data_b;
1264  
1265 0 int r = ca - cb;
1266 0 ▶ if (r != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (r != 0) 0 0 0
1267 0 return r;
1268  
1269 0 ▶ if (ca <= 0x7f)
Branch: 1 branch
BranchTrueFalseTotal
if (ca <= 0x7f) 0 0 0
1270 0 return 0;
1271  
1272 0 ++data_a;
1273 0 ++data_b;
1274 uint64_t a, b;
1275 0 switch (ca & 0x3) {
1276 0 case 0xcc & 0x3:
1277 0 a = *(uint8_t *) data_a;
1278 0 b = *(uint8_t *) data_b;
1279 0 break;
1280 0 case 0xcd & 0x3:
1281 0 a = mp_bswap_u16(*(uint16_t *) data_a);
1282 0 b = mp_bswap_u16(*(uint16_t *) data_b);
1283 0 break;
1284 0 case 0xce & 0x3:
1285 0 a = mp_bswap_u32(*(uint32_t *) data_a);
1286 0 b = mp_bswap_u32(*(uint32_t *) data_b);
1287 0 break;
1288 0 case 0xcf & 0x3:
1289 0 a = mp_bswap_u64(*(uint64_t *) data_a);
1290 0 b = mp_bswap_u64(*(uint64_t *) data_b);
1291 0 ▶ return a < b ? -1 : a > b;
Branch: 1 branch
BranchTrueFalseTotal
return a < b ? -1 : a > b; 0 0 0
1292 break;
1293 0 default:
1294 0 mp_unreachable();
1295 }
1296  
1297 0 int64_t v = (a - b);
1298 0 return (v > 0) - (v < 0);
1299 }
1300  
1301 MP_IMPL int64_t
1302 0 mp_decode_int(const char **data)
1303 {
1304 0 unsigned char c = (unsigned char) **data;
1305 0 *data += 1;
1306 int64_t val;
1307 0 switch (c) {
1308 0 case 0xe0 ... 0xff:
1309 0 return (int8_t) (c);
1310 0 case 0xd0:
1311 0 val = *(int8_t *) *data;
1312 0 *data += sizeof(uint8_t);
1313 0 return (int8_t) val;
1314 0 case 0xd1:
1315 0 val = mp_bswap_u16(*(int16_t *) *data);
1316 0 *data += sizeof(uint16_t);
1317 0 return (int16_t) val;
1318 0 case 0xd2:
1319 0 val = mp_bswap_u32(*(int32_t *) *data);
1320 0 *data += sizeof(uint32_t);
1321 0 return (int32_t) val;
1322 0 case 0xd3:
1323 0 val = mp_bswap_u64(*(int64_t *) *data);
1324 0 *data += sizeof(int64_t);
1325 0 return val;
1326 0 default:
1327 0 mp_unreachable();
1328 }
1329 }
1330  
1331 MP_IMPL uint32_t
1332 0 mp_sizeof_float(float num)
1333 {
1334 (void) num;
1335 0 return 1 + sizeof(float);
1336 }
1337  
1338 MP_IMPL uint32_t
1339 0 mp_sizeof_double(double num)
1340 {
1341 (void) num;
1342 0 return 1 + sizeof(double);
1343 }
1344  
1345 MP_IMPL ptrdiff_t
1346 0 mp_check_float(const char *cur, const char *end)
1347 {
1348 assert(cur < end);
1349 assert(mp_typeof(*cur) == MP_FLOAT);
1350 0 return 1 + sizeof(float) - (end - cur);
1351 }
1352  
1353 MP_IMPL ptrdiff_t
1354 0 mp_check_double(const char *cur, const char *end)
1355 {
1356 assert(cur < end);
1357 assert(mp_typeof(*cur) == MP_DOUBLE);
1358 0 return 1 + sizeof(double) - (end - cur);
1359 }
1360  
1361 MP_IMPL char *
1362 0 mp_encode_float(char *data, float num)
1363 {
1364 0 *data = 0xca;
1365 0 data++;
1366 0 *(float *) data = mp_bswap_float(num);
1367 0 return data + sizeof(float);
1368 }
1369  
1370 MP_IMPL char *
1371 0 mp_encode_double(char *data, double num)
1372 {
1373 0 *data = 0xcb;
1374 0 data++;
1375 0 *(double *) data = mp_bswap_double(num);
1376 0 return data + sizeof(double);
1377 }
1378  
1379 MP_IMPL float
1380 0 mp_decode_float(const char **data)
1381 {
1382 0 unsigned char c = (unsigned char) **data;
1383 assert(c == 0xca);
1384 (void) c;
1385 0 *data += 1;
1386 0 float val = mp_bswap_float(*(float *) *data);
1387 0 *data += sizeof(float);
1388 0 return val;
1389 }
1390  
1391 MP_IMPL double
1392 0 mp_decode_double(const char **data)
1393 {
1394 0 unsigned char c = (unsigned char) **data;
1395 assert(c == 0xcb);
1396 (void) c;
1397 0 *data += 1;
1398 0 double val = mp_bswap_double(*(double *) *data);
1399 0 *data += sizeof(double);
1400 0 return val;
1401 }
1402  
1403 MP_IMPL uint32_t
1404 0 mp_sizeof_strl(uint32_t len)
1405 {
1406 0 ▶ if (len <= 31) {
Branch: 1 branch
BranchTrueFalseTotal
if (len <= 31) { 0 0 0
1407 0 return 1;
1408 0 ▶ } else if (len <= UINT8_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (len <= UINT8_MAX) { 0 0 0
1409 0 return 1 + sizeof(uint8_t);
1410 0 ▶ } else if (len <= UINT16_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (len <= UINT16_MAX) { 0 0 0
1411 0 return 1 + sizeof(uint16_t);
1412 } else {
1413 0 return 1 + sizeof(uint32_t);
1414 }
1415 }
1416  
1417 MP_IMPL uint32_t
1418 0 mp_sizeof_str(uint32_t len)
1419 {
1420 0 return mp_sizeof_strl(len) + len;
1421 }
1422  
1423 MP_IMPL uint32_t
1424 0 mp_sizeof_binl(uint32_t len)
1425 {
1426 0 ▶ if (len <= UINT8_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if (len <= UINT8_MAX) { 0 0 0
1427 0 return 1 + sizeof(uint8_t);
1428 0 ▶ } else if (len <= UINT16_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (len <= UINT16_MAX) { 0 0 0
1429 0 return 1 + sizeof(uint16_t);
1430 } else {
1431 0 return 1 + sizeof(uint32_t);
1432 }
1433 }
1434  
1435 MP_IMPL uint32_t
1436 0 mp_sizeof_bin(uint32_t len)
1437 {
1438 0 return mp_sizeof_binl(len) + len;
1439 }
1440  
1441 MP_IMPL char *
1442 0 mp_encode_strl(char *data, uint32_t len)
1443 {
1444 0 ▶ if (len <= 31) {
Branch: 1 branch
BranchTrueFalseTotal
if (len <= 31) { 0 0 0
1445 0 *data = 0xa0 | (unsigned char) len;
1446 0 data += 1;
1447 0 ▶ } else if (len <= UINT8_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (len <= UINT8_MAX) { 0 0 0
1448 0 *data = 0xd9;
1449 0 data += 1;
1450 0 *(uint8_t *) data = len;
1451 0 data += sizeof(uint8_t);
1452 0 ▶ } else if (len <= UINT16_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (len <= UINT16_MAX) { 0 0 0
1453 0 *data = 0xda;
1454 0 data += 1;
1455 0 *(uint16_t *) data = mp_bswap_u16(len);
1456 0 data += sizeof(uint16_t);
1457 } else {
1458 0 *data = 0xdb;
1459 0 data += 1;
1460 0 *(uint32_t *) data = mp_bswap_u32(len);
1461 0 data += sizeof(uint32_t);
1462 }
1463  
1464 0 return data;
1465 }
1466  
1467 MP_IMPL char *
1468 0 mp_encode_str(char *data, const char *str, uint32_t len)
1469 {
1470 0 data = mp_encode_strl(data, len);
1471 0 memcpy(data, str, len);
1472 0 return data + len;
1473 }
1474  
1475 MP_IMPL char *
1476 0 mp_encode_binl(char *data, uint32_t len)
1477 {
1478 0 ▶ if (len <= UINT8_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if (len <= UINT8_MAX) { 0 0 0
1479 0 *data = 0xc4;
1480 0 data += 1;
1481 0 *(uint8_t *) data = len;
1482 0 data += sizeof(uint8_t);
1483 0 ▶ } else if (len <= UINT16_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (len <= UINT16_MAX) { 0 0 0
1484 0 *data = 0xc5;
1485 0 data += 1;
1486 0 *(uint16_t *) data = mp_bswap_u16(len);
1487 0 data += sizeof(uint16_t);
1488 } else {
1489 0 *data = 0xc6;
1490 0 data += 1;
1491 0 *(uint32_t *) data = mp_bswap_u32(len);
1492 0 data += sizeof(uint32_t);
1493 }
1494  
1495 0 return data;
1496 }
1497  
1498 MP_IMPL char *
1499 0 mp_encode_bin(char *data, const char *str, uint32_t len)
1500 {
1501 0 data = mp_encode_binl(data, len);
1502 0 memcpy(data, str, len);
1503 0 return data + len;
1504 }
1505  
1506 MP_IMPL ptrdiff_t
1507 0 mp_check_strl(const char *cur, const char *end)
1508 {
1509 assert(cur < end);
1510 assert(mp_typeof(*cur) == MP_STR);
1511  
1512 0 unsigned char c = *(unsigned char *) cur;
1513 0 ▶ if (mp_likely(c & ~0x1f) == 0xa0)
Branch: 1 branch
BranchTrueFalseTotal
if (mp_likely(c & ~0x1f) == 0xa0) 0 0 0
1514 0 return 1 - (end - cur);
1515  
1516 assert(c >= 0xd9 && c <= 0xdb); /* must be checked above by mp_typeof */
1517 0 uint32_t hsize = 1U << (c & 0x3) >> 1; /* 0xd9->1, 0xda->2, 0xdb->4 */
1518 0 return 1 + hsize - (end - cur);
1519 }
1520  
1521 MP_IMPL ptrdiff_t
1522 0 mp_check_binl(const char *cur, const char *end)
1523 {
1524 0 unsigned char c = *(unsigned char *) cur;
1525 assert(cur < end);
1526 assert(mp_typeof(c) == MP_BIN);
1527  
1528 assert(c >= 0xc4 && c <= 0xc6); /* must be checked above by mp_typeof */
1529 0 uint32_t hsize = 1U << (c & 0x3); /* 0xc4->1, 0xc5->2, 0xc6->4 */
1530 0 return 1 + hsize - (end - cur);
1531 }
1532  
1533 MP_IMPL uint32_t
1534 0 mp_decode_strl(const char **data)
1535 {
1536 0 unsigned char c = (unsigned char) **data;
1537 0 *data += 1;
1538  
1539 uint32_t len;
1540 0 switch (c) {
1541 0 case 0xa0 ... 0xbf:
1542 0 len = c & 0x1f;
1543 0 return len;
1544 0 case 0xd9:
1545 0 len = *(uint8_t *) *data;
1546 0 *data += sizeof(uint8_t);
1547 0 return len;
1548 0 case 0xda:
1549 0 len = mp_bswap_u16(*(uint16_t *) *data);
1550 0 *data += sizeof(uint16_t);
1551 0 return len;
1552 0 case 0xdb:
1553 0 len = mp_bswap_u32(*(uint32_t *) *data);
1554 0 *data += sizeof(uint32_t);
1555 0 return len;
1556 0 default:
1557 0 mp_unreachable();
1558 }
1559 }
1560  
1561 MP_IMPL const char *
1562 0 mp_decode_str(const char **data, uint32_t *len)
1563 {
1564 assert(len != NULL);
1565  
1566 0 *len = mp_decode_strl(data);
1567 0 const char *str = *data;
1568 0 *data += *len;
1569 0 return str;
1570 }
1571  
1572 MP_IMPL uint32_t
1573 0 mp_decode_binl(const char **data)
1574 {
1575 0 unsigned char c = (unsigned char) **data;
1576 0 *data += 1;
1577 uint32_t len;
1578  
1579 0 switch (c) {
1580 0 case 0xc4:
1581 0 len = *(uint8_t *) *data;
1582 0 *data += sizeof(uint8_t);
1583 0 return len;
1584 0 case 0xc5:
1585 0 len = mp_bswap_u16(*(uint16_t *) *data);
1586 0 *data += sizeof(uint16_t);
1587 0 return len;
1588 0 case 0xc6:
1589 0 len = mp_bswap_u32(*(uint32_t *) *data);
1590 0 *data += sizeof(uint32_t);
1591 0 return len;
1592 0 default:
1593 0 mp_unreachable();
1594 }
1595 }
1596  
1597 MP_IMPL const char *
1598 0 mp_decode_bin(const char **data, uint32_t *len)
1599 {
1600 assert(len != NULL);
1601  
1602 0 *len = mp_decode_binl(data);
1603 0 const char *str = *data;
1604 0 *data += *len;
1605 0 return str;
1606 }
1607  
1608 MP_IMPL uint32_t
1609 0 mp_sizeof_nil()
1610 {
1611 0 return 1;
1612 }
1613  
1614 MP_IMPL char *
1615 0 mp_encode_nil(char *data)
1616 {
1617 0 *data = 0xc0;
1618 0 return data + 1;
1619 }
1620  
1621 MP_IMPL ptrdiff_t
1622 0 mp_check_nil(const char *cur, const char *end)
1623 {
1624 assert(cur < end);
1625 assert(mp_typeof(*cur) == MP_NIL);
1626 0 return 1 - (end - cur);
1627 }
1628  
1629 MP_IMPL void
1630 0 mp_decode_nil(const char **data)
1631 {
1632 0 unsigned char c = (unsigned char) **data;
1633 assert(c == 0xc0);
1634 (void) c;
1635 0 *data += 1;
1636 0 }
1637  
1638 MP_IMPL uint32_t
1639 0 mp_sizeof_bool(bool val)
1640 {
1641 (void) val;
1642 0 return 1;
1643 }
1644  
1645 MP_IMPL char *
1646 0 mp_encode_bool(char *data, bool val)
1647 {
1648 0 *data = 0xc2 | (val & 1);
1649 0 return data + 1;
1650 }
1651  
1652 MP_IMPL ptrdiff_t
1653 0 mp_check_bool(const char *cur, const char *end)
1654 {
1655 assert(cur < end);
1656 assert(mp_typeof(*cur) == MP_BOOL);
1657 0 return 1 - (end - cur);
1658 }
1659  
1660 MP_IMPL bool
1661 0 mp_decode_bool(const char **data)
1662 {
1663 0 unsigned char c = (unsigned char) **data;
1664 0 *data += 1;
1665 0 switch (c) {
1666 0 case 0xc3:
1667 0 return true;
1668 0 case 0xc2:
1669 0 return false;
1670 0 default:
1671 0 mp_unreachable();
1672 }
1673 }
1674  
1675 /** See mp_parser_hint */
1676 enum {
1677 MP_HINT = -32,
1678 MP_HINT_STR_8 = MP_HINT,
1679 MP_HINT_STR_16 = MP_HINT - 1,
1680 MP_HINT_STR_32 = MP_HINT - 2,
1681 MP_HINT_ARRAY_16 = MP_HINT - 3,
1682 MP_HINT_ARRAY_32 = MP_HINT - 4,
1683 MP_HINT_MAP_16 = MP_HINT - 5,
1684 MP_HINT_MAP_32 = MP_HINT - 6,
1685 MP_HINT_EXT_8 = MP_HINT - 7,
1686 MP_HINT_EXT_16 = MP_HINT - 8,
1687 MP_HINT_EXT_32 = MP_HINT - 9
1688 };
1689  
1690 MP_PROTO void
1691 mp_next_slowpath(const char **data, int k);
1692  
1693 MP_IMPL void
1694 0 mp_next_slowpath(const char **data, int k)
1695 {
1696 0 ▶ for (; k > 0; k--) {
Branch: 1 branch
BranchTrueFalseTotal
for (; k > 0; k--) { 0 0 0
1697 0 unsigned char c = (unsigned char) **data;
1698 0 int l = mp_parser_hint[c];
1699 0 ▶ if (mp_likely(l >= 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (mp_likely(l >= 0)) { 0 0 0
1700 0 *data += l + 1;
1701 0 continue;
1702 0 ▶ } else if (mp_likely(l > MP_HINT)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (mp_likely(l > MP_HINT)) { 0 0 0
1703 0 k -= l;
1704 0 *data += 1;
1705 0 continue;
1706 }
1707  
1708 0 *data += 1;
1709 0 switch (l) {
1710 0 case MP_HINT_STR_8:
1711 /* MP_STR (8) */
1712 0 *data += *(uint8_t *) *data + sizeof(uint8_t);
1713 0 break;
1714 0 case MP_HINT_STR_16:
1715 /* MP_STR (16) */
1716 0 *data += mp_bswap_u16(*(uint16_t *) *data) +
1717 sizeof(uint16_t);
1718 0 break;
1719 0 case MP_HINT_STR_32:
1720 /* MP_STR (32) */
1721 0 *data += mp_bswap_u32(*(uint32_t *) *data) +
1722 sizeof(uint32_t);
1723 0 break;
1724 0 case MP_HINT_ARRAY_16:
1725 /* MP_ARRAY (16) */
1726 0 k += mp_bswap_u16(*(uint16_t *) *data);
1727 0 *data += sizeof(uint16_t);
1728 0 break;
1729 0 case MP_HINT_ARRAY_32:
1730 /* MP_ARRAY (32) */
1731 0 k += mp_bswap_u32(*(uint32_t *) *data);
1732 0 *data += sizeof(uint32_t);
1733 0 break;
1734 0 case MP_HINT_MAP_16:
1735 /* MP_MAP (16) */
1736 0 k += 2 * mp_bswap_u16(*(uint16_t *) *data);
1737 0 *data += sizeof(uint16_t);
1738 0 break;
1739 0 case MP_HINT_MAP_32:
1740 /* MP_MAP (32) */
1741 0 k += 2 * mp_bswap_u32(*(uint32_t *) *data);
1742 0 *data += sizeof(uint32_t);
1743 0 break;
1744 0 case MP_HINT_EXT_8:
1745 /* MP_EXT (8) */
1746 0 *data += *(uint8_t *) *data + sizeof(uint8_t) + 1;
1747 0 break;
1748 0 case MP_HINT_EXT_16:
1749 /* MP_EXT (16) */
1750 0 *data += mp_bswap_u16(*(uint16_t *) *data) +
1751 sizeof(uint16_t) + 1;
1752 0 break;
1753 0 case MP_HINT_EXT_32:
1754 /* MP_EXT (32) */
1755 0 *data += mp_bswap_u32(*(uint32_t *) *data) +
1756 sizeof(uint32_t) + 1;
1757 0 break;
1758 0 default:
1759 0 mp_unreachable();
1760 }
1761 }
1762 0 }
1763  
1764 MP_IMPL void
1765 0 mp_next(const char **data)
1766 {
1767 0 int k = 1;
1768 0 ▶ for (; k > 0; k--) {
Branch: 1 branch
BranchTrueFalseTotal
for (; k > 0; k--) { 0 0 0
1769 0 unsigned char c = (unsigned char) **data;
1770 0 int l = mp_parser_hint[c];
1771 0 ▶ if (mp_likely(l >= 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (mp_likely(l >= 0)) { 0 0 0
1772 0 *data += l + 1;
1773 0 continue;
1774 0 ▶ } else if (mp_likely(c == 0xd9)){
Branch: 1 branch
BranchTrueFalseTotal
} else if (mp_likely(c == 0xd9)){ 0 0 0
1775 /* MP_STR (8) */
1776 0 *data += 1;
1777 0 *data += *(uint8_t *) *data + sizeof(uint8_t);
1778 0 continue;
1779 0 ▶ } else if (l > MP_HINT) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (l > MP_HINT) { 0 0 0
1780 0 k -= l;
1781 0 *data += 1;
1782 0 continue;
1783 } else {
1784 0 return mp_next_slowpath(data, k);
1785 }
1786 }
1787 }
1788  
1789 MP_IMPL int
1790 0 mp_check(const char **data, const char *end)
1791 {
1792 int k;
1793 0 ▶ for (k = 1; k > 0; k--) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 1; k > 0; k--) { 0 0 0
1794 0 ▶ if (mp_unlikely(*data >= end))
Branch: 1 branch
BranchTrueFalseTotal
if (mp_unlikely(*data >= end)) 0 0 0
1795 0 return 1;
1796  
1797 0 unsigned char c = (unsigned char) **data;
1798 0 *data += 1;
1799 0 int l = mp_parser_hint[c];
1800 0 ▶ if (mp_likely(l >= 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (mp_likely(l >= 0)) { 0 0 0
1801 0 *data += l;
1802 0 continue;
1803 0 ▶ } else if (mp_likely(l > MP_HINT)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (mp_likely(l > MP_HINT)) { 0 0 0
1804 0 k -= l;
1805 0 continue;
1806 }
1807  
1808 0 switch (l) {
1809 0 case MP_HINT_STR_8:
1810 /* MP_STR (8) */
1811 0 ▶ if (mp_unlikely(*data + sizeof(uint8_t) > end))
Branch: 1 branch
BranchTrueFalseTotal
if (mp_unlikely(*data + sizeof(uint8_t) > end)) 0 0 0
1812 0 return 1;
1813 0 *data += *(uint8_t *) *data + sizeof(uint8_t);
1814 0 break;
1815 0 case MP_HINT_STR_16:
1816 /* MP_STR (16) */
1817 0 ▶ if (mp_unlikely(*data + sizeof(uint16_t) > end))
Branch: 1 branch
BranchTrueFalseTotal
if (mp_unlikely(*data + sizeof(uint16_t) > end)) 0 0 0
1818 0 return 1;
1819 0 *data += mp_bswap_u16(*(uint16_t *) *data) +
1820 sizeof(uint16_t);
1821 0 break;
1822 0 case MP_HINT_STR_32:
1823 /* MP_STR (32) */
1824 0 ▶ if (mp_unlikely(*data + sizeof(uint32_t) > end))
Branch: 1 branch
BranchTrueFalseTotal
if (mp_unlikely(*data + sizeof(uint32_t) > end)) 0 0 0
1825 0 return 1;
1826 0 *data += mp_bswap_u32(*(uint32_t *) *data) +
1827 sizeof(uint32_t);
1828 0 break;
1829 0 case MP_HINT_ARRAY_16:
1830 /* MP_ARRAY (16) */
1831 0 ▶ if (mp_unlikely(*data + sizeof(uint16_t) > end))
Branch: 1 branch
BranchTrueFalseTotal
if (mp_unlikely(*data + sizeof(uint16_t) > end)) 0 0 0
1832 0 return 1;
1833 0 k += mp_bswap_u16(*(uint16_t *) *data);
1834 0 *data += sizeof(uint16_t);
1835 0 break;
1836 0 case MP_HINT_ARRAY_32:
1837 /* MP_ARRAY (32) */
1838 0 ▶ if (mp_unlikely(*data + sizeof(uint32_t) > end))
Branch: 1 branch
BranchTrueFalseTotal
if (mp_unlikely(*data + sizeof(uint32_t) > end)) 0 0 0
1839 0 return 1;
1840 0 k += mp_bswap_u32(*(uint32_t *) *data);
1841 0 *data += sizeof(uint32_t);
1842 0 break;
1843 0 case MP_HINT_MAP_16:
1844 /* MP_MAP (16) */
1845 0 ▶ if (mp_unlikely(*data + sizeof(uint16_t) > end))
Branch: 1 branch
BranchTrueFalseTotal
if (mp_unlikely(*data + sizeof(uint16_t) > end)) 0 0 0
1846 0 return false;
1847 0 k += 2 * mp_bswap_u16(*(uint16_t *) *data);
1848 0 *data += sizeof(uint16_t);
1849 0 break;
1850 0 case MP_HINT_MAP_32:
1851 /* MP_MAP (32) */
1852 0 ▶ if (mp_unlikely(*data + sizeof(uint32_t) > end))
Branch: 1 branch
BranchTrueFalseTotal
if (mp_unlikely(*data + sizeof(uint32_t) > end)) 0 0 0
1853 0 return 1;
1854 0 k += 2 * mp_bswap_u32(*(uint32_t *) *data);
1855 0 *data += sizeof(uint32_t);
1856 0 break;
1857 0 case MP_HINT_EXT_8:
1858 /* MP_EXT (8) */
1859 0 ▶ if (mp_unlikely(*data + sizeof(uint8_t) + 1 > end))
Branch: 1 branch
BranchTrueFalseTotal
if (mp_unlikely(*data + sizeof(uint8_t) + 1 > end)) 0 0 0
1860 0 return 1;
1861 0 *data += *(uint8_t *) *data + sizeof(uint8_t);
1862 0 break;
1863 0 case MP_HINT_EXT_16:
1864 /* MP_EXT (16) */
1865 0 ▶ if (mp_unlikely(*data + sizeof(uint16_t) + 1 > end))
Branch: 1 branch
BranchTrueFalseTotal
if (mp_unlikely(*data + sizeof(uint16_t) + 1 > end)) 0 0 0
1866 0 return 1;
1867 0 *data += mp_bswap_u16(*(uint16_t *) *data) +
1868 sizeof(uint16_t) + 1;
1869 0 break;
1870 0 case MP_HINT_EXT_32:
1871 /* MP_EXT (32) */
1872 0 ▶ if (mp_unlikely(*data + sizeof(uint32_t) + 1 > end))
Branch: 1 branch
BranchTrueFalseTotal
if (mp_unlikely(*data + sizeof(uint32_t) + 1 > end)) 0 0 0
1873 0 return 1;
1874 0 *data += mp_bswap_u32(*(uint32_t *) *data) +
1875 sizeof(uint32_t) + 1;
1876 0 break;
1877 0 default:
1878 0 mp_unreachable();
1879 }
1880 }
1881  
1882 0 ▶ if (mp_unlikely(*data > end))
Branch: 1 branch
BranchTrueFalseTotal
if (mp_unlikely(*data > end)) 0 0 0
1883 0 return 1;
1884  
1885 0 return 0;
1886 }
1887  
1888 /** \endcond */
1889  
1890 /*
1891 * }}}
1892 */
1893  
1894 /*
1895 * {{{ Implementation: parser tables
1896 */
1897  
1898 /** \cond 0 */
1899  
1900 #if defined(MP_SOURCE)
1901  
1902 /**
1903 * This lookup table used by mp_sizeof() to determine enum mp_type by the first
1904 * byte of MsgPack element.
1905 */
1906 const enum mp_type mp_type_hint[256]= {
1907 /* {{{ MP_UINT (fixed) */
1908 /* 0x00 */ MP_UINT,
1909 /* 0x01 */ MP_UINT,
1910 /* 0x02 */ MP_UINT,
1911 /* 0x03 */ MP_UINT,
1912 /* 0x04 */ MP_UINT,
1913 /* 0x05 */ MP_UINT,
1914 /* 0x06 */ MP_UINT,
1915 /* 0x07 */ MP_UINT,
1916 /* 0x08 */ MP_UINT,
1917 /* 0x09 */ MP_UINT,
1918 /* 0x0a */ MP_UINT,
1919 /* 0x0b */ MP_UINT,
1920 /* 0x0c */ MP_UINT,
1921 /* 0x0d */ MP_UINT,
1922 /* 0x0e */ MP_UINT,
1923 /* 0x0f */ MP_UINT,
1924 /* 0x10 */ MP_UINT,
1925 /* 0x11 */ MP_UINT,
1926 /* 0x12 */ MP_UINT,
1927 /* 0x13 */ MP_UINT,
1928 /* 0x14 */ MP_UINT,
1929 /* 0x15 */ MP_UINT,
1930 /* 0x16 */ MP_UINT,
1931 /* 0x17 */ MP_UINT,
1932 /* 0x18 */ MP_UINT,
1933 /* 0x19 */ MP_UINT,
1934 /* 0x1a */ MP_UINT,
1935 /* 0x1b */ MP_UINT,
1936 /* 0x1c */ MP_UINT,
1937 /* 0x1d */ MP_UINT,
1938 /* 0x1e */ MP_UINT,
1939 /* 0x1f */ MP_UINT,
1940 /* 0x20 */ MP_UINT,
1941 /* 0x21 */ MP_UINT,
1942 /* 0x22 */ MP_UINT,
1943 /* 0x23 */ MP_UINT,
1944 /* 0x24 */ MP_UINT,
1945 /* 0x25 */ MP_UINT,
1946 /* 0x26 */ MP_UINT,
1947 /* 0x27 */ MP_UINT,
1948 /* 0x28 */ MP_UINT,
1949 /* 0x29 */ MP_UINT,
1950 /* 0x2a */ MP_UINT,
1951 /* 0x2b */ MP_UINT,
1952 /* 0x2c */ MP_UINT,
1953 /* 0x2d */ MP_UINT,
1954 /* 0x2e */ MP_UINT,
1955 /* 0x2f */ MP_UINT,
1956 /* 0x30 */ MP_UINT,
1957 /* 0x31 */ MP_UINT,
1958 /* 0x32 */ MP_UINT,
1959 /* 0x33 */ MP_UINT,
1960 /* 0x34 */ MP_UINT,
1961 /* 0x35 */ MP_UINT,
1962 /* 0x36 */ MP_UINT,
1963 /* 0x37 */ MP_UINT,
1964 /* 0x38 */ MP_UINT,
1965 /* 0x39 */ MP_UINT,
1966 /* 0x3a */ MP_UINT,
1967 /* 0x3b */ MP_UINT,
1968 /* 0x3c */ MP_UINT,
1969 /* 0x3d */ MP_UINT,
1970 /* 0x3e */ MP_UINT,
1971 /* 0x3f */ MP_UINT,
1972 /* 0x40 */ MP_UINT,
1973 /* 0x41 */ MP_UINT,
1974 /* 0x42 */ MP_UINT,
1975 /* 0x43 */ MP_UINT,
1976 /* 0x44 */ MP_UINT,
1977 /* 0x45 */ MP_UINT,
1978 /* 0x46 */ MP_UINT,
1979 /* 0x47 */ MP_UINT,
1980 /* 0x48 */ MP_UINT,
1981 /* 0x49 */ MP_UINT,
1982 /* 0x4a */ MP_UINT,
1983 /* 0x4b */ MP_UINT,
1984 /* 0x4c */ MP_UINT,
1985 /* 0x4d */ MP_UINT,
1986 /* 0x4e */ MP_UINT,
1987 /* 0x4f */ MP_UINT,
1988 /* 0x50 */ MP_UINT,
1989 /* 0x51 */ MP_UINT,
1990 /* 0x52 */ MP_UINT,
1991 /* 0x53 */ MP_UINT,
1992 /* 0x54 */ MP_UINT,
1993 /* 0x55 */ MP_UINT,
1994 /* 0x56 */ MP_UINT,
1995 /* 0x57 */ MP_UINT,
1996 /* 0x58 */ MP_UINT,
1997 /* 0x59 */ MP_UINT,
1998 /* 0x5a */ MP_UINT,
1999 /* 0x5b */ MP_UINT,
2000 /* 0x5c */ MP_UINT,
2001 /* 0x5d */ MP_UINT,
2002 /* 0x5e */ MP_UINT,
2003 /* 0x5f */ MP_UINT,
2004 /* 0x60 */ MP_UINT,
2005 /* 0x61 */ MP_UINT,
2006 /* 0x62 */ MP_UINT,
2007 /* 0x63 */ MP_UINT,
2008 /* 0x64 */ MP_UINT,
2009 /* 0x65 */ MP_UINT,
2010 /* 0x66 */ MP_UINT,
2011 /* 0x67 */ MP_UINT,
2012 /* 0x68 */ MP_UINT,
2013 /* 0x69 */ MP_UINT,
2014 /* 0x6a */ MP_UINT,
2015 /* 0x6b */ MP_UINT,
2016 /* 0x6c */ MP_UINT,
2017 /* 0x6d */ MP_UINT,
2018 /* 0x6e */ MP_UINT,
2019 /* 0x6f */ MP_UINT,
2020 /* 0x70 */ MP_UINT,
2021 /* 0x71 */ MP_UINT,
2022 /* 0x72 */ MP_UINT,
2023 /* 0x73 */ MP_UINT,
2024 /* 0x74 */ MP_UINT,
2025 /* 0x75 */ MP_UINT,
2026 /* 0x76 */ MP_UINT,
2027 /* 0x77 */ MP_UINT,
2028 /* 0x78 */ MP_UINT,
2029 /* 0x79 */ MP_UINT,
2030 /* 0x7a */ MP_UINT,
2031 /* 0x7b */ MP_UINT,
2032 /* 0x7c */ MP_UINT,
2033 /* 0x7d */ MP_UINT,
2034 /* 0x7e */ MP_UINT,
2035 /* 0x7f */ MP_UINT,
2036 /* }}} */
2037  
2038 /* {{{ MP_MAP (fixed) */
2039 /* 0x80 */ MP_MAP,
2040 /* 0x81 */ MP_MAP,
2041 /* 0x82 */ MP_MAP,
2042 /* 0x83 */ MP_MAP,
2043 /* 0x84 */ MP_MAP,
2044 /* 0x85 */ MP_MAP,
2045 /* 0x86 */ MP_MAP,
2046 /* 0x87 */ MP_MAP,
2047 /* 0x88 */ MP_MAP,
2048 /* 0x89 */ MP_MAP,
2049 /* 0x8a */ MP_MAP,
2050 /* 0x8b */ MP_MAP,
2051 /* 0x8c */ MP_MAP,
2052 /* 0x8d */ MP_MAP,
2053 /* 0x8e */ MP_MAP,
2054 /* 0x8f */ MP_MAP,
2055 /* }}} */
2056  
2057 /* {{{ MP_ARRAY (fixed) */
2058 /* 0x90 */ MP_ARRAY,
2059 /* 0x91 */ MP_ARRAY,
2060 /* 0x92 */ MP_ARRAY,
2061 /* 0x93 */ MP_ARRAY,
2062 /* 0x94 */ MP_ARRAY,
2063 /* 0x95 */ MP_ARRAY,
2064 /* 0x96 */ MP_ARRAY,
2065 /* 0x97 */ MP_ARRAY,
2066 /* 0x98 */ MP_ARRAY,
2067 /* 0x99 */ MP_ARRAY,
2068 /* 0x9a */ MP_ARRAY,
2069 /* 0x9b */ MP_ARRAY,
2070 /* 0x9c */ MP_ARRAY,
2071 /* 0x9d */ MP_ARRAY,
2072 /* 0x9e */ MP_ARRAY,
2073 /* 0x9f */ MP_ARRAY,
2074 /* }}} */
2075  
2076 /* {{{ MP_STR (fixed) */
2077 /* 0xa0 */ MP_STR,
2078 /* 0xa1 */ MP_STR,
2079 /* 0xa2 */ MP_STR,
2080 /* 0xa3 */ MP_STR,
2081 /* 0xa4 */ MP_STR,
2082 /* 0xa5 */ MP_STR,
2083 /* 0xa6 */ MP_STR,
2084 /* 0xa7 */ MP_STR,
2085 /* 0xa8 */ MP_STR,
2086 /* 0xa9 */ MP_STR,
2087 /* 0xaa */ MP_STR,
2088 /* 0xab */ MP_STR,
2089 /* 0xac */ MP_STR,
2090 /* 0xad */ MP_STR,
2091 /* 0xae */ MP_STR,
2092 /* 0xaf */ MP_STR,
2093 /* 0xb0 */ MP_STR,
2094 /* 0xb1 */ MP_STR,
2095 /* 0xb2 */ MP_STR,
2096 /* 0xb3 */ MP_STR,
2097 /* 0xb4 */ MP_STR,
2098 /* 0xb5 */ MP_STR,
2099 /* 0xb6 */ MP_STR,
2100 /* 0xb7 */ MP_STR,
2101 /* 0xb8 */ MP_STR,
2102 /* 0xb9 */ MP_STR,
2103 /* 0xba */ MP_STR,
2104 /* 0xbb */ MP_STR,
2105 /* 0xbc */ MP_STR,
2106 /* 0xbd */ MP_STR,
2107 /* 0xbe */ MP_STR,
2108 /* 0xbf */ MP_STR,
2109 /* }}} */
2110  
2111 /* {{{ MP_NIL, MP_BOOL */
2112 /* 0xc0 */ MP_NIL,
2113 /* 0xc1 */ MP_EXT, /* never used */
2114 /* 0xc2 */ MP_BOOL,
2115 /* 0xc3 */ MP_BOOL,
2116 /* }}} */
2117  
2118 /* {{{ MP_BIN */
2119 /* 0xc4 */ MP_BIN, /* MP_BIN(8) */
2120 /* 0xc5 */ MP_BIN, /* MP_BIN(16) */
2121 /* 0xc6 */ MP_BIN, /* MP_BIN(32) */
2122 /* }}} */
2123  
2124 /* {{{ MP_EXT */
2125 /* 0xc7 */ MP_EXT,
2126 /* 0xc8 */ MP_EXT,
2127 /* 0xc9 */ MP_EXT,
2128 /* }}} */
2129  
2130 /* {{{ MP_FLOAT, MP_DOUBLE */
2131 /* 0xca */ MP_FLOAT,
2132 /* 0xcb */ MP_DOUBLE,
2133 /* }}} */
2134  
2135 /* {{{ MP_UINT */
2136 /* 0xcc */ MP_UINT,
2137 /* 0xcd */ MP_UINT,
2138 /* 0xce */ MP_UINT,
2139 /* 0xcf */ MP_UINT,
2140 /* }}} */
2141  
2142 /* {{{ MP_INT */
2143 /* 0xd0 */ MP_INT, /* MP_INT (8) */
2144 /* 0xd1 */ MP_INT, /* MP_INT (16) */
2145 /* 0xd2 */ MP_INT, /* MP_INT (32) */
2146 /* 0xd3 */ MP_INT, /* MP_INT (64) */
2147 /* }}} */
2148  
2149 /* {{{ MP_EXT */
2150 /* 0xd4 */ MP_EXT, /* MP_INT (8) */
2151 /* 0xd5 */ MP_EXT, /* MP_INT (16) */
2152 /* 0xd6 */ MP_EXT, /* MP_INT (32) */
2153 /* 0xd7 */ MP_EXT, /* MP_INT (64) */
2154 /* 0xd8 */ MP_EXT, /* MP_INT (127) */
2155 /* }}} */
2156  
2157 /* {{{ MP_STR */
2158 /* 0xd9 */ MP_STR, /* MP_STR(8) */
2159 /* 0xda */ MP_STR, /* MP_STR(16) */
2160 /* 0xdb */ MP_STR, /* MP_STR(32) */
2161 /* }}} */
2162  
2163 /* {{{ MP_ARRAY */
2164 /* 0xdc */ MP_ARRAY, /* MP_ARRAY(16) */
2165 /* 0xdd */ MP_ARRAY, /* MP_ARRAY(32) */
2166 /* }}} */
2167  
2168 /* {{{ MP_MAP */
2169 /* 0xde */ MP_MAP, /* MP_MAP (16) */
2170 /* 0xdf */ MP_MAP, /* MP_MAP (32) */
2171 /* }}} */
2172  
2173 /* {{{ MP_INT */
2174 /* 0xe0 */ MP_INT,
2175 /* 0xe1 */ MP_INT,
2176 /* 0xe2 */ MP_INT,
2177 /* 0xe3 */ MP_INT,
2178 /* 0xe4 */ MP_INT,
2179 /* 0xe5 */ MP_INT,
2180 /* 0xe6 */ MP_INT,
2181 /* 0xe7 */ MP_INT,
2182 /* 0xe8 */ MP_INT,
2183 /* 0xe9 */ MP_INT,
2184 /* 0xea */ MP_INT,
2185 /* 0xeb */ MP_INT,
2186 /* 0xec */ MP_INT,
2187 /* 0xed */ MP_INT,
2188 /* 0xee */ MP_INT,
2189 /* 0xef */ MP_INT,
2190 /* 0xf0 */ MP_INT,
2191 /* 0xf1 */ MP_INT,
2192 /* 0xf2 */ MP_INT,
2193 /* 0xf3 */ MP_INT,
2194 /* 0xf4 */ MP_INT,
2195 /* 0xf5 */ MP_INT,
2196 /* 0xf6 */ MP_INT,
2197 /* 0xf7 */ MP_INT,
2198 /* 0xf8 */ MP_INT,
2199 /* 0xf9 */ MP_INT,
2200 /* 0xfa */ MP_INT,
2201 /* 0xfb */ MP_INT,
2202 /* 0xfc */ MP_INT,
2203 /* 0xfd */ MP_INT,
2204 /* 0xfe */ MP_INT,
2205 /* 0xff */ MP_INT
2206 /* }}} */
2207 };
2208  
2209 /**
2210 * This lookup table used by mp_next() and mp_check() to determine
2211 * size of MsgPack element by its first byte.
2212 * A positive value contains size of the element (excluding the first byte).
2213 * A negative value means the element is compound (e.g. array or map)
2214 * of size (-n).
2215 * MP_HINT_* values used for special cases handled by switch() statement.
2216 */
2217 const int8_t mp_parser_hint[256] = {
2218 /* {{{ MP_UINT(fixed) **/
2219 /* 0x00 */ 0,
2220 /* 0x01 */ 0,
2221 /* 0x02 */ 0,
2222 /* 0x03 */ 0,
2223 /* 0x04 */ 0,
2224 /* 0x05 */ 0,
2225 /* 0x06 */ 0,
2226 /* 0x07 */ 0,
2227 /* 0x08 */ 0,
2228 /* 0x09 */ 0,
2229 /* 0x0a */ 0,
2230 /* 0x0b */ 0,
2231 /* 0x0c */ 0,
2232 /* 0x0d */ 0,
2233 /* 0x0e */ 0,
2234 /* 0x0f */ 0,
2235 /* 0x10 */ 0,
2236 /* 0x11 */ 0,
2237 /* 0x12 */ 0,
2238 /* 0x13 */ 0,
2239 /* 0x14 */ 0,
2240 /* 0x15 */ 0,
2241 /* 0x16 */ 0,
2242 /* 0x17 */ 0,
2243 /* 0x18 */ 0,
2244 /* 0x19 */ 0,
2245 /* 0x1a */ 0,
2246 /* 0x1b */ 0,
2247 /* 0x1c */ 0,
2248 /* 0x1d */ 0,
2249 /* 0x1e */ 0,
2250 /* 0x1f */ 0,
2251 /* 0x20 */ 0,
2252 /* 0x21 */ 0,
2253 /* 0x22 */ 0,
2254 /* 0x23 */ 0,
2255 /* 0x24 */ 0,
2256 /* 0x25 */ 0,
2257 /* 0x26 */ 0,
2258 /* 0x27 */ 0,
2259 /* 0x28 */ 0,
2260 /* 0x29 */ 0,
2261 /* 0x2a */ 0,
2262 /* 0x2b */ 0,
2263 /* 0x2c */ 0,
2264 /* 0x2d */ 0,
2265 /* 0x2e */ 0,
2266 /* 0x2f */ 0,
2267 /* 0x30 */ 0,
2268 /* 0x31 */ 0,
2269 /* 0x32 */ 0,
2270 /* 0x33 */ 0,
2271 /* 0x34 */ 0,
2272 /* 0x35 */ 0,
2273 /* 0x36 */ 0,
2274 /* 0x37 */ 0,
2275 /* 0x38 */ 0,
2276 /* 0x39 */ 0,
2277 /* 0x3a */ 0,
2278 /* 0x3b */ 0,
2279 /* 0x3c */ 0,
2280 /* 0x3d */ 0,
2281 /* 0x3e */ 0,
2282 /* 0x3f */ 0,
2283 /* 0x40 */ 0,
2284 /* 0x41 */ 0,
2285 /* 0x42 */ 0,
2286 /* 0x43 */ 0,
2287 /* 0x44 */ 0,
2288 /* 0x45 */ 0,
2289 /* 0x46 */ 0,
2290 /* 0x47 */ 0,
2291 /* 0x48 */ 0,
2292 /* 0x49 */ 0,
2293 /* 0x4a */ 0,
2294 /* 0x4b */ 0,
2295 /* 0x4c */ 0,
2296 /* 0x4d */ 0,
2297 /* 0x4e */ 0,
2298 /* 0x4f */ 0,
2299 /* 0x50 */ 0,
2300 /* 0x51 */ 0,
2301 /* 0x52 */ 0,
2302 /* 0x53 */ 0,
2303 /* 0x54 */ 0,
2304 /* 0x55 */ 0,
2305 /* 0x56 */ 0,
2306 /* 0x57 */ 0,
2307 /* 0x58 */ 0,
2308 /* 0x59 */ 0,
2309 /* 0x5a */ 0,
2310 /* 0x5b */ 0,
2311 /* 0x5c */ 0,
2312 /* 0x5d */ 0,
2313 /* 0x5e */ 0,
2314 /* 0x5f */ 0,
2315 /* 0x60 */ 0,
2316 /* 0x61 */ 0,
2317 /* 0x62 */ 0,
2318 /* 0x63 */ 0,
2319 /* 0x64 */ 0,
2320 /* 0x65 */ 0,
2321 /* 0x66 */ 0,
2322 /* 0x67 */ 0,
2323 /* 0x68 */ 0,
2324 /* 0x69 */ 0,
2325 /* 0x6a */ 0,
2326 /* 0x6b */ 0,
2327 /* 0x6c */ 0,
2328 /* 0x6d */ 0,
2329 /* 0x6e */ 0,
2330 /* 0x6f */ 0,
2331 /* 0x70 */ 0,
2332 /* 0x71 */ 0,
2333 /* 0x72 */ 0,
2334 /* 0x73 */ 0,
2335 /* 0x74 */ 0,
2336 /* 0x75 */ 0,
2337 /* 0x76 */ 0,
2338 /* 0x77 */ 0,
2339 /* 0x78 */ 0,
2340 /* 0x79 */ 0,
2341 /* 0x7a */ 0,
2342 /* 0x7b */ 0,
2343 /* 0x7c */ 0,
2344 /* 0x7d */ 0,
2345 /* 0x7e */ 0,
2346 /* 0x7f */ 0,
2347 /* }}} */
2348  
2349 /* {{{ MP_MAP (fixed) */
2350 /* 0x80 */ 0, /* empty map - just skip one byte */
2351 /* 0x81 */ -2, /* 2 elements follow */
2352 /* 0x82 */ -4,
2353 /* 0x83 */ -6,
2354 /* 0x84 */ -8,
2355 /* 0x85 */ -10,
2356 /* 0x86 */ -12,
2357 /* 0x87 */ -14,
2358 /* 0x88 */ -16,
2359 /* 0x89 */ -18,
2360 /* 0x8a */ -20,
2361 /* 0x8b */ -22,
2362 /* 0x8c */ -24,
2363 /* 0x8d */ -26,
2364 /* 0x8e */ -28,
2365 /* 0x8f */ -30,
2366 /* }}} */
2367  
2368 /* {{{ MP_ARRAY (fixed) */
2369 /* 0x90 */ 0, /* empty array - just skip one byte */
2370 /* 0x91 */ -1, /* 1 element follows */
2371 /* 0x92 */ -2,
2372 /* 0x93 */ -3,
2373 /* 0x94 */ -4,
2374 /* 0x95 */ -5,
2375 /* 0x96 */ -6,
2376 /* 0x97 */ -7,
2377 /* 0x98 */ -8,
2378 /* 0x99 */ -9,
2379 /* 0x9a */ -10,
2380 /* 0x9b */ -11,
2381 /* 0x9c */ -12,
2382 /* 0x9d */ -13,
2383 /* 0x9e */ -14,
2384 /* 0x9f */ -15,
2385 /* }}} */
2386  
2387 /* {{{ MP_STR (fixed) */
2388 /* 0xa0 */ 0,
2389 /* 0xa1 */ 1,
2390 /* 0xa2 */ 2,
2391 /* 0xa3 */ 3,
2392 /* 0xa4 */ 4,
2393 /* 0xa5 */ 5,
2394 /* 0xa6 */ 6,
2395 /* 0xa7 */ 7,
2396 /* 0xa8 */ 8,
2397 /* 0xa9 */ 9,
2398 /* 0xaa */ 10,
2399 /* 0xab */ 11,
2400 /* 0xac */ 12,
2401 /* 0xad */ 13,
2402 /* 0xae */ 14,
2403 /* 0xaf */ 15,
2404 /* 0xb0 */ 16,
2405 /* 0xb1 */ 17,
2406 /* 0xb2 */ 18,
2407 /* 0xb3 */ 19,
2408 /* 0xb4 */ 20,
2409 /* 0xb5 */ 21,
2410 /* 0xb6 */ 22,
2411 /* 0xb7 */ 23,
2412 /* 0xb8 */ 24,
2413 /* 0xb9 */ 25,
2414 /* 0xba */ 26,
2415 /* 0xbb */ 27,
2416 /* 0xbc */ 28,
2417 /* 0xbd */ 29,
2418 /* 0xbe */ 30,
2419 /* 0xbf */ 31,
2420 /* }}} */
2421  
2422 /* {{{ MP_NIL, MP_BOOL */
2423 /* 0xc0 */ 0, /* MP_NIL */
2424 /* 0xc1 */ 0, /* never used */
2425 /* 0xc2 */ 0, /* MP_BOOL*/
2426 /* 0xc3 */ 0, /* MP_BOOL*/
2427 /* }}} */
2428  
2429 /* {{{ MP_BIN */
2430 /* 0xc4 */ MP_HINT_STR_8, /* MP_BIN (8) */
2431 /* 0xc5 */ MP_HINT_STR_16, /* MP_BIN (16) */
2432 /* 0xc6 */ MP_HINT_STR_32, /* MP_BIN (32) */
2433 /* }}} */
2434  
2435 /* {{{ MP_EXT */
2436 /* 0xc7 */ MP_HINT_EXT_8, /* MP_EXT (8) */
2437 /* 0xc8 */ MP_HINT_EXT_16, /* MP_EXT (16) */
2438 /* 0xc9 */ MP_HINT_EXT_32, /* MP_EXT (32) */
2439 /* }}} */
2440  
2441 /* {{{ MP_FLOAT, MP_DOUBLE */
2442 /* 0xca */ sizeof(float), /* MP_FLOAT */
2443 /* 0xcb */ sizeof(double), /* MP_DOUBLE */
2444 /* }}} */
2445  
2446 /* {{{ MP_UINT */
2447 /* 0xcc */ sizeof(uint8_t), /* MP_UINT (8) */
2448 /* 0xcd */ sizeof(uint16_t), /* MP_UINT (16) */
2449 /* 0xce */ sizeof(uint32_t), /* MP_UINT (32) */
2450 /* 0xcf */ sizeof(uint64_t), /* MP_UINT (64) */
2451 /* }}} */
2452  
2453 /* {{{ MP_INT */
2454 /* 0xd0 */ sizeof(uint8_t), /* MP_INT (8) */
2455 /* 0xd1 */ sizeof(uint16_t), /* MP_INT (8) */
2456 /* 0xd2 */ sizeof(uint32_t), /* MP_INT (8) */
2457 /* 0xd3 */ sizeof(uint64_t), /* MP_INT (8) */
2458 /* }}} */
2459  
2460 /* {{{ MP_EXT (fixext) */
2461 /* 0xd4 */ 2, /* MP_EXT (fixext 8) */
2462 /* 0xd5 */ 3, /* MP_EXT (fixext 16) */
2463 /* 0xd6 */ 5, /* MP_EXT (fixext 32) */
2464 /* 0xd7 */ 9, /* MP_EXT (fixext 64) */
2465 /* 0xd8 */ 17, /* MP_EXT (fixext 128) */
2466 /* }}} */
2467  
2468 /* {{{ MP_STR */
2469 /* 0xd9 */ MP_HINT_STR_8, /* MP_STR (8) */
2470 /* 0xda */ MP_HINT_STR_16, /* MP_STR (16) */
2471 /* 0xdb */ MP_HINT_STR_32, /* MP_STR (32) */
2472 /* }}} */
2473  
2474 /* {{{ MP_ARRAY */
2475 /* 0xdc */ MP_HINT_ARRAY_16, /* MP_ARRAY (16) */
2476 /* 0xdd */ MP_HINT_ARRAY_32, /* MP_ARRAY (32) */
2477 /* }}} */
2478  
2479 /* {{{ MP_MAP */
2480 /* 0xde */ MP_HINT_MAP_16, /* MP_MAP (16) */
2481 /* 0xdf */ MP_HINT_MAP_32, /* MP_MAP (32) */
2482 /* }}} */
2483  
2484 /* {{{ MP_INT (fixed) */
2485 /* 0xe0 */ 0,
2486 /* 0xe1 */ 0,
2487 /* 0xe2 */ 0,
2488 /* 0xe3 */ 0,
2489 /* 0xe4 */ 0,
2490 /* 0xe5 */ 0,
2491 /* 0xe6 */ 0,
2492 /* 0xe7 */ 0,
2493 /* 0xe8 */ 0,
2494 /* 0xe9 */ 0,
2495 /* 0xea */ 0,
2496 /* 0xeb */ 0,
2497 /* 0xec */ 0,
2498 /* 0xed */ 0,
2499 /* 0xee */ 0,
2500 /* 0xef */ 0,
2501 /* 0xf0 */ 0,
2502 /* 0xf1 */ 0,
2503 /* 0xf2 */ 0,
2504 /* 0xf3 */ 0,
2505 /* 0xf4 */ 0,
2506 /* 0xf5 */ 0,
2507 /* 0xf6 */ 0,
2508 /* 0xf7 */ 0,
2509 /* 0xf8 */ 0,
2510 /* 0xf9 */ 0,
2511 /* 0xfa */ 0,
2512 /* 0xfb */ 0,
2513 /* 0xfc */ 0,
2514 /* 0xfd */ 0,
2515 /* 0xfe */ 0,
2516 /* 0xff */ 0
2517 /* }}} */
2518 };
2519  
2520 #endif /* defined(MP_SOURCE) */
2521  
2522 /** \endcond */
2523  
2524 /*
2525 * }}}
2526 */
2527  
2528 #if defined(__cplusplus)
2529 } /* extern "C" */
2530 #endif /* defined(__cplusplus) */
2531  
2532 #undef MP_SOURCE
2533 #undef MP_PROTO
2534 #undef MP_IMPL
2535 #undef MP_ALWAYSINLINE
2536 #undef MP_GCC_VERSION
2537  
2538 #endif /* MSGPUCK_H_INCLUDED */
« msgpack.c ↑ Summary tp.h »