msgpuck.h

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