This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
msgpuck.h
2195 lines (2007 loc) · 51.9 KB
/
msgpuck.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef MSGPUCK_H_INCLUDED
#define MSGPUCK_H_INCLUDED
/*
* Copyright (c) 2013-2017 MsgPuck Authors
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/**
* \file msgpuck.h
* MsgPuck
* \brief MsgPuck is a simple and efficient MsgPack encoder/decoder
* library in a single self-contained file.
*
* Usage example:
* \code
* // Encode
* char buf[1024];
* char *w = buf;
* w = mp_encode_array(w, 4);
* w = mp_encode_uint(w, 10);
* w = mp_encode_str(w, "hello world", strlen("hello world"));
* w = mp_encode_bool(w, true);
* w = mp_encode_double(w, 3.1415);
*
* // Validate
* const char *b = buf;
* int r = mp_check(&b, w);
* assert(!r);
* assert(b == w);
*
* // Decode
* uint32_t size;
* uint64_t ival;
* const char *sval;
* uint32_t sval_len;
* bool bval;
* double dval;
*
* const char *r = buf;
*
* size = mp_decode_array(&r);
* // size is 4
*
* ival = mp_decode_uint(&r);
* // ival is 10;
*
* sval = mp_decode_str(&r, &sval_len);
* // sval is "hello world", sval_len is strlen("hello world")
*
* bval = mp_decode_bool(&r);
* // bval is true
*
* dval = mp_decode_double(&r);
* // dval is 3.1415
*
* assert(r == w);
* \endcode
*
* \note Supported compilers.
* The implementation requires a C99+ or C++03+ compatible compiler.
*
* \note Inline functions.
* The implementation is compatible with both C99 and GNU inline functions.
* Please link libmsgpuck.a static library for non-inlined versions of
* functions and global tables.
*/
#if defined(__cplusplus) && !defined(__STDC_CONSTANT_MACROS)
#define __STDC_CONSTANT_MACROS 1 /* make С++ to be happy */
#endif
#if defined(__cplusplus) && !defined(__STDC_LIMIT_MACROS)
#define __STDC_LIMIT_MACROS 1 /* make С++ to be happy */
#endif
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#if defined(__cplusplus)
extern "C" {
#endif /* defined(__cplusplus) */
/*
* {{{ Platform-specific definitions
*/
/** \cond false **/
#if defined(__CC_ARM) /* set the alignment to 1 for armcc compiler */
#define MP_PACKED __packed
#else
#define MP_PACKED __attribute__((packed))
#endif
#if defined(MP_SOURCE)
#error MP_SOURCE is not supported anymore, please link libmsgpuck.a
#endif
#if defined(__GNUC__) && !defined(__GNUC_STDC_INLINE__)
#if !defined(MP_LIBRARY)
#define MP_PROTO extern inline
#define MP_IMPL extern inline
#else /* defined(MP_LIBRARY) */
#define MP_PROTO
#define MP_IMPL
#endif
#define MP_ALWAYSINLINE
#else /* C99 inline */
#if !defined(MP_LIBRARY)
#define MP_PROTO inline
#define MP_IMPL inline
#else /* defined(MP_LIBRARY) */
#define MP_PROTO extern inline
#define MP_IMPL inline
#endif
#define MP_ALWAYSINLINE __attribute__((always_inline))
#endif /* GNU inline or C99 inline */
#if !defined __GNUC_MINOR__ || defined __INTEL_COMPILER || \
defined __SUNPRO_C || defined __SUNPRO_CC
#define MP_GCC_VERSION(major, minor) 0
#else
#define MP_GCC_VERSION(major, minor) (__GNUC__ > (major) || \
(__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
#endif
#if !defined(__has_builtin)
#define __has_builtin(x) 0 /* clang */
#endif
#if MP_GCC_VERSION(2, 9) || __has_builtin(__builtin_expect)
#define mp_likely(x) __builtin_expect((x), 1)
#define mp_unlikely(x) __builtin_expect((x), 0)
#else
#define mp_likely(x) (x)
#define mp_unlikely(x) (x)
#endif
#if MP_GCC_VERSION(4, 5) || __has_builtin(__builtin_unreachable)
#define mp_unreachable() (assert(0), __builtin_unreachable())
#else
MP_PROTO void
mp_unreachable(void) __attribute__((noreturn));
MP_PROTO void
mp_unreachable(void) { assert(0); abort(); }
#define mp_unreachable() (assert(0))
#endif
#define mp_identity(x) (x) /* just to simplify mp_load/mp_store macroses */
#if MP_GCC_VERSION(4, 8) || __has_builtin(__builtin_bswap16)
#define mp_bswap_u16(x) __builtin_bswap16(x)
#else /* !MP_GCC_VERSION(4, 8) */
#define mp_bswap_u16(x) ( \
(((x) << 8) & 0xff00) | \
(((x) >> 8) & 0x00ff) )
#endif
#if MP_GCC_VERSION(4, 3) || __has_builtin(__builtin_bswap32)
#define mp_bswap_u32(x) __builtin_bswap32(x)
#else /* !MP_GCC_VERSION(4, 3) */
#define mp_bswap_u32(x) ( \
(((x) << 24) & UINT32_C(0xff000000)) | \
(((x) << 8) & UINT32_C(0x00ff0000)) | \
(((x) >> 8) & UINT32_C(0x0000ff00)) | \
(((x) >> 24) & UINT32_C(0x000000ff)) )
#endif
#if MP_GCC_VERSION(4, 3) || __has_builtin(__builtin_bswap64)
#define mp_bswap_u64(x) __builtin_bswap64(x)
#else /* !MP_GCC_VERSION(4, 3) */
#define mp_bswap_u64(x) (\
(((x) << 56) & UINT64_C(0xff00000000000000)) | \
(((x) << 40) & UINT64_C(0x00ff000000000000)) | \
(((x) << 24) & UINT64_C(0x0000ff0000000000)) | \
(((x) << 8) & UINT64_C(0x000000ff00000000)) | \
(((x) >> 8) & UINT64_C(0x00000000ff000000)) | \
(((x) >> 24) & UINT64_C(0x0000000000ff0000)) | \
(((x) >> 40) & UINT64_C(0x000000000000ff00)) | \
(((x) >> 56) & UINT64_C(0x00000000000000ff)) )
#endif
#define MP_LOAD_STORE(name, type, bswap) \
MP_PROTO type \
mp_load_##name(const char **data); \
MP_IMPL type \
mp_load_##name(const char **data) \
{ \
struct MP_PACKED cast { type val; }; \
type val = bswap(((struct cast *) *data)->val); \
*data += sizeof(type); \
return val; \
} \
MP_PROTO char * \
mp_store_##name(char *data, type val); \
MP_IMPL char * \
mp_store_##name(char *data, type val) \
{ \
struct MP_PACKED cast { type val; }; \
((struct cast *) (data))->val = bswap(val); \
return data + sizeof(type); \
}
MP_LOAD_STORE(u8, uint8_t, mp_identity);
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
MP_LOAD_STORE(u16, uint16_t, mp_bswap_u16);
MP_LOAD_STORE(u32, uint32_t, mp_bswap_u32);
MP_LOAD_STORE(u64, uint64_t, mp_bswap_u64);
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
MP_LOAD_STORE(u16, uint16_t, mp_identity);
MP_LOAD_STORE(u32, uint32_t, mp_identity);
MP_LOAD_STORE(u64, uint64_t, mp_identity);
#else
#error Unsupported __BYTE_ORDER__
#endif
#if !defined(__FLOAT_WORD_ORDER__)
#define __FLOAT_WORD_ORDER__ __BYTE_ORDER__
#endif /* defined(__FLOAT_WORD_ORDER__) */
#if __FLOAT_WORD_ORDER__ == __ORDER_LITTLE_ENDIAN__
/*
* Idiots from msgpack.org byte-swaps even IEEE754 float/double types.
* Some platforms (e.g. arm) cause SIGBUS on attempt to store
* invalid float in registers, so code like flt = mp_bswap_float(flt)
* can't be used here.
*/
union MP_PACKED mp_float_cast {
uint32_t u32;
float f;
};
union MP_PACKED mp_double_cast {
uint64_t u64;
double d;
};
MP_PROTO float
mp_load_float(const char **data);
MP_PROTO double
mp_load_double(const char **data);
MP_PROTO char *
mp_store_float(char *data, float val);
MP_PROTO char *
mp_store_double(char *data, double val);
MP_IMPL float
mp_load_float(const char **data)
{
union mp_float_cast cast = *(union mp_float_cast *) *data;
*data += sizeof(cast);
cast.u32 = mp_bswap_u32(cast.u32);
return cast.f;
}
MP_IMPL double
mp_load_double(const char **data)
{
union mp_double_cast cast = *(union mp_double_cast *) *data;
*data += sizeof(cast);
cast.u64 = mp_bswap_u64(cast.u64);
return cast.d;
}
MP_IMPL char *
mp_store_float(char *data, float val)
{
union mp_float_cast cast;
cast.f = val;
cast.u32 = mp_bswap_u32(cast.u32);
*(union mp_float_cast *) (data) = cast;
return data + sizeof(cast);
}
MP_IMPL char *
mp_store_double(char *data, double val)
{
union mp_double_cast cast;
cast.d = val;
cast.u64 = mp_bswap_u64(cast.u64);
*(union mp_double_cast *) (data) = cast;
return data + sizeof(cast);
}
#elif __FLOAT_WORD_ORDER__ == __ORDER_BIG_ENDIAN__
MP_LOAD_STORE(float, float, mp_identity);
MP_LOAD_STORE(double, double, mp_identity);
#else
#error Unsupported __FLOAT_WORD_ORDER__
#endif
#undef mp_identity
#undef MP_LOAD_STORE
/** \endcond */
/*
* }}}
*/
/*
* {{{ API definition
*/
/**
* \brief MsgPack data types
*/
enum mp_type {
MP_NIL = 0,
MP_UINT,
MP_INT,
MP_STR,
MP_BIN,
MP_ARRAY,
MP_MAP,
MP_BOOL,
MP_FLOAT,
MP_DOUBLE,
MP_EXT
};
/**
* \brief Determine MsgPack type by a first byte \a c of encoded data.
*
* Example usage:
* \code
* assert(MP_ARRAY == mp_typeof(0x90));
* \endcode
*
* \param c - a first byte of encoded data
* \return MsgPack type
*/
MP_PROTO __attribute__((pure)) enum mp_type
mp_typeof(const char c);
/**
* \brief Calculate exact buffer size needed to store an array header of
* \a size elements. Maximum return value is 5. For performance reasons you
* can preallocate buffer for maximum size without calling the function.
* \param size - a number of elements
* \return buffer size in bytes (max is 5)
*/
MP_PROTO __attribute__((const)) uint32_t
mp_sizeof_array(uint32_t size);
/**
* \brief Encode an array header of \a size elements.
*
* All array members must be encoded after the header.
*
* Example usage:
* \code
* // Encode
* char buf[1024];
* char *w = buf;
* w = mp_encode_array(w, 2);
* w = mp_encode_uint(w, 10);
* w = mp_encode_uint(w, 15);
*
* // Decode
* const char *r = buf;
* uint32_t size = mp_decode_array(&r);
* for (uint32_t i = 0; i < size; i++) {
* uint64_t val = mp_decode_uint(&r);
* }
* assert (r == w);
* \endcode
* It is your responsibility to ensure that \a data has enough space.
* \param data - a buffer
* \param size - a number of elements
* \return \a data + \link mp_sizeof_array() mp_sizeof_array(size) \endlink
* \sa mp_sizeof_array
*/
MP_PROTO char *
mp_encode_array(char *data, uint32_t size);
/**
* \brief Check that \a cur buffer has enough bytes to decode an array header
* \param cur buffer
* \param end end of the buffer
* \retval 0 - buffer has enough bytes
* \retval > 0 - the number of remaining bytes to read
* \pre cur < end
* \pre mp_typeof(*cur) == MP_ARRAY
*/
MP_PROTO __attribute__((pure)) ptrdiff_t
mp_check_array(const char *cur, const char *end);
/**
* \brief Decode an array header from MsgPack \a data.
*
* All array members must be decoded after the header.
* \param data - the pointer to a buffer
* \return the number of elements in an array
* \post *data = *data + mp_sizeof_array(retval)
* \sa \link mp_encode_array() An usage example \endlink
*/
MP_PROTO uint32_t
mp_decode_array(const char **data);
/**
* \brief Calculate exact buffer size needed to store a map header of
* \a size elements. Maximum return value is 5. For performance reasons you
* can preallocate buffer for maximum size without calling the function.
* \param size - a number of elements
* \return buffer size in bytes (max is 5)
*/
MP_PROTO __attribute__((const)) uint32_t
mp_sizeof_map(uint32_t size);
/**
* \brief Encode a map header of \a size elements.
*
* All map key-value pairs must be encoded after the header.
*
* Example usage:
* \code
* char buf[1024];
*
* // Encode
* char *w = buf;
* w = mp_encode_map(b, 2);
* w = mp_encode_str(b, "key1", 4);
* w = mp_encode_str(b, "value1", 6);
* w = mp_encode_str(b, "key2", 4);
* w = mp_encode_str(b, "value2", 6);
*
* // Decode
* const char *r = buf;
* uint32_t size = mp_decode_map(&r);
* for (uint32_t i = 0; i < size; i++) {
* // Use switch(mp_typeof(**r)) to support more types
* uint32_t key_len, val_len;
* const char *key = mp_decode_str(&r, key_len);
* const char *val = mp_decode_str(&r, val_len);
* }
* assert (r == w);
* \endcode
* It is your responsibility to ensure that \a data has enough space.
* \param data - a buffer
* \param size - a number of key/value pairs
* \return \a data + \link mp_sizeof_map() mp_sizeof_map(size)\endlink
* \sa mp_sizeof_map
*/
MP_PROTO char *
mp_encode_map(char *data, uint32_t size);
/**
* \brief Check that \a cur buffer has enough bytes to decode a map header
* \param cur buffer
* \param end end of the buffer
* \retval 0 - buffer has enough bytes
* \retval > 0 - the number of remaining bytes to read
* \pre cur < end
* \pre mp_typeof(*cur) == MP_MAP
*/
MP_PROTO __attribute__((pure)) ptrdiff_t
mp_check_map(const char *cur, const char *end);
/**
* \brief Decode a map header from MsgPack \a data.
*
* All map key-value pairs must be decoded after the header.
* \param data - the pointer to a buffer
* \return the number of key/value pairs in a map
* \post *data = *data + mp_sizeof_array(retval)
* \sa \link mp_encode_map() An usage example \endlink
*/
MP_PROTO uint32_t
mp_decode_map(const char **data);
/**
* \brief Calculate exact buffer size needed to store an integer \a num.
* Maximum return value is 9. For performance reasons you can preallocate
* buffer for maximum size without calling the function.
* Example usage:
* \code
* char **data = ...;
* char *end = *data;
* my_buffer_ensure(mp_sizeof_uint(x), &end);
* // my_buffer_ensure(9, &end);
* mp_encode_uint(buffer, x);
* \endcode
* \param num - a number
* \return buffer size in bytes (max is 9)
*/
MP_PROTO __attribute__((const)) uint32_t
mp_sizeof_uint(uint64_t num);
/**
* \brief Calculate exact buffer size needed to store an integer \a num.
* Maximum return value is 9. For performance reasons you can preallocate
* buffer for maximum size without calling the function.
* \param num - a number
* \return buffer size in bytes (max is 9)
* \pre \a num < 0
*/
MP_PROTO __attribute__((const)) uint32_t
mp_sizeof_int(int64_t num);
/**
* \brief Encode an unsigned integer \a num.
* It is your responsibility to ensure that \a data has enough space.
* \param data - a buffer
* \param num - a number
* \return \a data + mp_sizeof_uint(\a num)
* \sa \link mp_encode_array() An usage example \endlink
* \sa mp_sizeof_uint()
*/
MP_PROTO char *
mp_encode_uint(char *data, uint64_t num);
/**
* \brief Encode a signed integer \a num.
* It is your responsibility to ensure that \a data has enough space.
* \param data - a buffer
* \param num - a number
* \return \a data + mp_sizeof_int(\a num)
* \sa \link mp_encode_array() An usage example \endlink
* \sa mp_sizeof_int()
* \pre \a num < 0
*/
MP_PROTO char *
mp_encode_int(char *data, int64_t num);
/**
* \brief Check that \a cur buffer has enough bytes to decode an uint
* \param cur buffer
* \param end end of the buffer
* \retval 0 - buffer has enough bytes
* \retval > 0 - the number of remaining bytes to read
* \pre cur < end
* \pre mp_typeof(*cur) == MP_UINT
*/
MP_PROTO __attribute__((pure)) ptrdiff_t
mp_check_uint(const char *cur, const char *end);
/**
* \brief Check that \a cur buffer has enough bytes to decode an int
* \param cur buffer
* \param end end of the buffer
* \retval 0 - buffer has enough bytes
* \retval > 0 - the number of remaining bytes to read
* \pre cur < end
* \pre mp_typeof(*cur) == MP_INT
*/
MP_PROTO __attribute__((pure)) ptrdiff_t
mp_check_int(const char *cur, const char *end);
/**
* \brief Decode an unsigned integer from MsgPack \a data
* \param data - the pointer to a buffer
* \return an unsigned number
* \post *data = *data + mp_sizeof_uint(retval)
*/
MP_PROTO uint64_t
mp_decode_uint(const char **data);
/**
* \brief Decode a signed integer from MsgPack \a data
* \param data - the pointer to a buffer
* \return an unsigned number
* \post *data = *data + mp_sizeof_int(retval)
*/
MP_PROTO int64_t
mp_decode_int(const char **data);
/**
* \brief Compare two packed unsigned integers.
*
* The function is faster than two mp_decode_uint() calls.
* \param data_a unsigned int a
* \param data_b unsigned int b
* \retval < 0 when \a a < \a b
* \retval 0 when \a a == \a b
* \retval > 0 when \a a > \a b
*/
MP_PROTO __attribute__((pure)) int
mp_compare_uint(const char *data_a, const char *data_b);
/**
* \brief Calculate exact buffer size needed to store a float \a num.
* The return value is always 5. The function was added to provide integrity of
* the library.
* \param num - a float
* \return buffer size in bytes (always 5)
*/
MP_PROTO __attribute__((const)) uint32_t
mp_sizeof_float(float num);
/**
* \brief Calculate exact buffer size needed to store a double \a num.
* The return value is either 5 or 9. The function was added to provide
* integrity of the library. For performance reasons you can preallocate buffer
* for maximum size without calling the function.
* \param num - a double
* \return buffer size in bytes (5 or 9)
*/
MP_PROTO __attribute__((const)) uint32_t
mp_sizeof_double(double num);
/**
* \brief Encode a float \a num.
* It is your responsibility to ensure that \a data has enough space.
* \param data - a buffer
* \param num - a float
* \return \a data + mp_sizeof_float(\a num)
* \sa mp_sizeof_float()
* \sa \link mp_encode_array() An usage example \endlink
*/
MP_PROTO char *
mp_encode_float(char *data, float num);
/**
* \brief Encode a double \a num.
* It is your responsibility to ensure that \a data has enough space.
* \param data - a buffer
* \param num - a float
* \return \a data + mp_sizeof_double(\a num)
* \sa \link mp_encode_array() An usage example \endlink
* \sa mp_sizeof_double()
*/
MP_PROTO char *
mp_encode_double(char *data, double num);
/**
* \brief Check that \a cur buffer has enough bytes to decode a float
* \param cur buffer
* \param end end of the buffer
* \retval 0 - buffer has enough bytes
* \retval > 0 - the number of remaining bytes to read
* \pre cur < end
* \pre mp_typeof(*cur) == MP_FLOAT
*/
MP_PROTO __attribute__((pure)) ptrdiff_t
mp_check_float(const char *cur, const char *end);
/**
* \brief Check that \a cur buffer has enough bytes to decode a double
* \param cur buffer
* \param end end of the buffer
* \retval 0 - buffer has enough bytes
* \retval > 0 - the number of remaining bytes to read
* \pre cur < end
* \pre mp_typeof(*cur) == MP_DOUBLE
*/
MP_PROTO __attribute__((pure)) ptrdiff_t
mp_check_double(const char *cur, const char *end);
/**
* \brief Decode a float from MsgPack \a data
* \param data - the pointer to a buffer
* \return a float
* \post *data = *data + mp_sizeof_float(retval)
*/
MP_PROTO float
mp_decode_float(const char **data);
/**
* \brief Decode a double from MsgPack \a data
* \param data - the pointer to a buffer
* \return a double
* \post *data = *data + mp_sizeof_double(retval)
*/
MP_PROTO double
mp_decode_double(const char **data);
/**
* \brief Calculate exact buffer size needed to store a string header of
* length \a num. Maximum return value is 5. For performance reasons you can
* preallocate buffer for maximum size without calling the function.
* \param len - a string length
* \return size in chars (max is 5)
*/
MP_PROTO __attribute__((const)) uint32_t
mp_sizeof_strl(uint32_t len);
/**
* \brief Equivalent to mp_sizeof_strl(\a len) + \a len.
* \param len - a string length
* \return size in chars (max is 5 + \a len)
*/
MP_PROTO __attribute__((const)) uint32_t
mp_sizeof_str(uint32_t len);
/**
* \brief Calculate exact buffer size needed to store a binstring header of
* length \a num. Maximum return value is 5. For performance reasons you can
* preallocate buffer for maximum size without calling the function.
* \param len - a string length
* \return size in chars (max is 5)
*/
MP_PROTO __attribute__((const)) uint32_t
mp_sizeof_binl(uint32_t len);
/**
* \brief Equivalent to mp_sizeof_binl(\a len) + \a len.
* \param len - a string length
* \return size in chars (max is 5 + \a len)
*/
MP_PROTO __attribute__((const)) uint32_t
mp_sizeof_bin(uint32_t len);
/**
* \brief Encode a string header of length \a len.
*
* The function encodes MsgPack header (\em only header) for a string of
* length \a len. You should append actual string data to the buffer manually
* after encoding the header (exactly \a len bytes without trailing '\0').
*
* This approach is very useful for cases when the total length of the string
* is known in advance, but the string data is not stored in a single
* continuous buffer (e.g. network packets).
*
* It is your responsibility to ensure that \a data has enough space.
* Usage example:
* \code
* char buffer[1024];
* char *b = buffer;
* b = mp_encode_strl(b, hdr.total_len);
* char *s = b;
* memcpy(b, pkt1.data, pkt1.len);
* b += pkt1.len;
* // get next packet
* memcpy(b, pkt2.data, pkt2.len);
* b += pkt2.len;
* // get next packet
* memcpy(b, pkt1.data, pkt3.len);
* b += pkt3.len;
*
* // Check that all data was received
* assert(hdr.total_len == (uint32_t) (b - s))
* \endcode
* Hint: you can dynamically reallocate the buffer during the process.
* \param data - a buffer
* \param len - a string length
* \return \a data + mp_sizeof_strl(len)
* \sa mp_sizeof_strl()
*/
MP_PROTO char *
mp_encode_strl(char *data, uint32_t len);
/**
* \brief Encode a string of length \a len.
* The function is equivalent to mp_encode_strl() + memcpy.
* \param data - a buffer
* \param str - a pointer to string data
* \param len - a string length
* \return \a data + mp_sizeof_str(len) ==
* data + mp_sizeof_strl(len) + len
* \sa mp_encode_strl
*/
MP_PROTO char *
mp_encode_str(char *data, const char *str, uint32_t len);
/**
* \brief Encode a binstring header of length \a len.
* See mp_encode_strl() for more details.
* \param data - a bufer
* \param len - a string length
* \return data + mp_sizeof_binl(\a len)
* \sa mp_encode_strl
*/
MP_PROTO char *
mp_encode_binl(char *data, uint32_t len);
/**
* \brief Encode a binstring of length \a len.
* The function is equivalent to mp_encode_binl() + memcpy.
* \param data - a buffer
* \param str - a pointer to binstring data
* \param len - a binstring length
* \return \a data + mp_sizeof_bin(\a len) ==
* data + mp_sizeof_binl(\a len) + \a len
* \sa mp_encode_strl
*/
MP_PROTO char *
mp_encode_bin(char *data, const char *str, uint32_t len);
/**
* \brief Encode a sequence of values according to format string.
* Example: mp_format(buf, sz, "[%d {%d%s%d%s}]", 42, 0, "false", 1, "true");
* to get a msgpack array of two items: number 42 and map (0->"false, 2->"true")
* Does not write items that don't fit to data_size argument.
*
* \param data - a buffer
* \param data_size - a buffer size
* \param format - zero-end string, containing structure of resulting
* msgpack and types of next arguments.
* Format can contain '[' and ']' pairs, defining arrays,
* '{' and '}' pairs, defining maps, and format specifiers, described below:
* %d, %i - int
* %u - unsigned int
* %ld, %li - long
* %lu - unsigned long
* %lld, %lli - long long
* %llu - unsigned long long
* %hd, %hi - short
* %hu - unsigned short
* %hhd, %hhi - char (as number)
* %hhu - unsigned char (as number)
* %f - float
* %lf - double
* %b - bool
* %s - zero-end string
* %.*s - string with specified length
* %p - MsgPack data
* %.*p - MsgPack data with specified length
* %% is ignored
* %smthelse assert and undefined behaviour
* NIL - a nil value
* all other symbols are ignored.
*
* \return the number of requred bytes.
* \retval > data_size means that is not enough space
* and whole msgpack was not encoded.
*/
size_t
mp_format(char *data, size_t data_size, const char *format, ...);
/**
* \brief mp_format variation, taking variable argument list
* Example:
* va_list args;
* va_start(args, fmt);
* mp_vformat(data, data_size, fmt, args);
* va_end(args);
* \sa \link mp_format() mp_format() \endlink
*/
size_t
mp_vformat(char *data, size_t data_size, const char *format, va_list args);
/**
* \brief print MsgPack data \a file using JSON-like format.
* MP_EXT is printed as "undefined"
* \param file - pointer to file (or NULL for stdout)
* \param data - pointer to buffer containing msgpack object
* \retval >=0 - the number of bytes printed
* \retval -1 - error
* \sa fprintf()
*/
int
mp_fprint(FILE *file, const char *data);
/**
* \brief format MsgPack data to \a buf using JSON-like format.
* \sa mp_fprint()
* \param buf - buffer to use
* \param size - buffer size. This function write at most size bytes
* (including the terminating null byte ('\0').
* \param data - pointer to buffer containing msgpack object
* \retval <size - the number of characters printed (excluding the null byte)
* \retval >=size - the number of characters (excluding the null byte),
* which would have been written to the final string if
* enough space had been available.
* \retval -1 - error
* \sa snprintf()
*/
int
mp_snprint(char *buf, int size, const char *data);
/**
* \brief Check that \a cur buffer has enough bytes to decode a string header
* \param cur buffer
* \param end end of the buffer
* \retval 0 - buffer has enough bytes
* \retval > 0 - the number of remaining bytes to read
* \pre cur < end
* \pre mp_typeof(*cur) == MP_STR
*/
MP_PROTO __attribute__((pure)) ptrdiff_t
mp_check_strl(const char *cur, const char *end);
/**
* \brief Check that \a cur buffer has enough bytes to decode a binstring header
* \param cur buffer
* \param end end of the buffer
* \retval 0 - buffer has enough bytes
* \retval > 0 - the number of remaining bytes to read
* \pre cur < end
* \pre mp_typeof(*cur) == MP_BIN
*/
MP_PROTO __attribute__((pure)) ptrdiff_t
mp_check_binl(const char *cur, const char *end);
/**
* \brief Decode a length of a string from MsgPack \a data
* \param data - the pointer to a buffer
* \return a length of astring
* \post *data = *data + mp_sizeof_strl(retval)
* \sa mp_encode_strl
*/
MP_PROTO uint32_t
mp_decode_strl(const char **data);
/**
* \brief Decode a string from MsgPack \a data
* \param data - the pointer to a buffer
* \param len - the pointer to save a string length
* \return a pointer to a decoded string
* \post *data = *data + mp_sizeof_str(*len)
* \sa mp_encode_binl
*/
MP_PROTO const char *
mp_decode_str(const char **data, uint32_t *len);
/**
* \brief Decode a length of a binstring from MsgPack \a data
* \param data - the pointer to a buffer
* \return a length of a binstring
* \post *data = *data + mp_sizeof_binl(retval)
* \sa mp_encode_binl
*/
MP_PROTO uint32_t
mp_decode_binl(const char **data);
/**
* \brief Decode a binstring from MsgPack \a data
* \param data - the pointer to a buffer
* \param len - the pointer to save a binstring length
* \return a pointer to a decoded binstring
* \post *data = *data + mp_sizeof_str(*len)
* \sa mp_encode_binl
*/
MP_PROTO const char *
mp_decode_bin(const char **data, uint32_t *len);
/**
* \brief Decode a length of a string or binstring from MsgPack \a data
* \param data - the pointer to a buffer
* \return a length of a string
* \post *data = *data + mp_sizeof_strbinl(retval)
* \sa mp_encode_binl
*/
MP_PROTO uint32_t
mp_decode_strbinl(const char **data);
/**
* \brief Decode a string or binstring from MsgPack \a data
* \param data - the pointer to a buffer
* \param len - the pointer to save a binstring length
* \return a pointer to a decoded binstring
* \post *data = *data + mp_sizeof_strbinl(*len)
* \sa mp_encode_binl
*/
MP_PROTO const char *
mp_decode_strbin(const char **data, uint32_t *len);
/**
* \brief Calculate exact buffer size needed to store the nil value.
* The return value is always 1. The function was added to provide integrity of
* the library.
* \return buffer size in bytes (always 1)
*/
MP_PROTO __attribute__((const)) uint32_t
mp_sizeof_nil(void);
/**
* \brief Encode the nil value.
* It is your responsibility to ensure that \a data has enough space.