-
Notifications
You must be signed in to change notification settings - Fork 22
/
mpack.c
3628 lines (3034 loc) · 117 KB
/
mpack.c
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
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Nicholas Fraser
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
/*
* This is the MPack 0.5.1 amalgamation package.
*
* http://github.com/ludocode/mpack
*/
#define MPACK_INTERNAL 1
#include "mpack.h"
/* mpack-platform.c */
#define MPACK_INTERNAL 1
/* #include "mpack-platform.h" */
#if MPACK_DEBUG && MPACK_STDIO
#include <stdarg.h>
#endif
#if MPACK_DEBUG && MPACK_STDIO
void mpack_assert_fail_format(const char* format, ...) {
char buffer[512];
va_list args;
va_start(args, format);
vsnprintf(buffer, sizeof(buffer), format, args);
va_end(args);
buffer[sizeof(buffer) - 1] = 0;
mpack_assert_fail(buffer);
}
void mpack_break_hit_format(const char* format, ...) {
char buffer[512];
va_list args;
va_start(args, format);
vsnprintf(buffer, sizeof(buffer), format, args);
va_end(args);
buffer[sizeof(buffer) - 1] = 0;
mpack_break_hit(buffer);
}
#endif
#if MPACK_CUSTOM_ASSERT
void mpack_break_hit(const char* message) {
// If we have a custom assert handler, break just wraps it
// for simplicity.
mpack_assert_fail(message);
}
#else
void mpack_assert_fail(const char* message) {
MPACK_UNUSED(message);
#if MPACK_STDIO
fprintf(stderr, "%s\n", message);
#endif
#if defined(__GCC__) || defined(__clang__)
__builtin_trap();
#elif WIN32
__debugbreak();
#endif
#if MPACK_STDLIB
abort();
#elif defined(__GCC__) || defined(__clang__)
__builtin_abort();
#endif
MPACK_UNREACHABLE;
}
void mpack_break_hit(const char* message) {
MPACK_UNUSED(message);
#if MPACK_STDIO
fprintf(stderr, "%s\n", message);
#endif
#if defined(__GCC__) || defined(__clang__)
__builtin_trap();
#elif WIN32
__debugbreak();
#elif MPACK_STDLIB
abort();
#elif defined(__GCC__) || defined(__clang__)
__builtin_abort();
#endif
}
#endif
#if !MPACK_STDLIB
// The below are adapted from the C wikibook:
// https://en.wikibooks.org/wiki/C_Programming/Strings
void* mpack_memset(void *s, int c, size_t n) {
unsigned char *us = (unsigned char *)s;
unsigned char uc = (unsigned char)c;
while (n-- != 0)
*us++ = uc;
return s;
}
void* mpack_memcpy(void *s1, const void *s2, size_t n) {
char * __restrict dst = (char *)s1;
const char * __restrict src = (const char *)s2;
while (n-- != 0)
*dst++ = *src++;
return s1;
}
void* mpack_memmove(void *s1, const void *s2, size_t n) {
char *p1 = (char *)s1;
const char *p2 = (const char *)s2;
if (p2 < p1 && p1 < p2 + n) {
p2 += n;
p1 += n;
while (n-- != 0)
*--p1 = *--p2;
} else
while (n-- != 0)
*p1++ = *p2++;
return s1;
}
int mpack_memcmp(const void* s1, const void* s2, size_t n) {
const unsigned char *us1 = (const unsigned char *) s1;
const unsigned char *us2 = (const unsigned char *) s2;
while (n-- != 0) {
if (*us1 != *us2)
return (*us1 < *us2) ? -1 : +1;
us1++;
us2++;
}
return 0;
}
size_t mpack_strlen(const char *s) {
const char *p = s;
while (*p != '\0')
p++;
return (size_t)(p - s);
}
#endif
#if defined(MPACK_MALLOC) && !defined(MPACK_REALLOC)
void* mpack_realloc(void* old_ptr, size_t used_size, size_t new_size) {
void* new_ptr = malloc(new_size);
if (new_ptr == NULL)
return NULL;
mpack_memcpy(new_ptr, old_ptr, used_size);
MPACK_FREE(old_ptr);
return new_ptr;
}
#endif
/* mpack-common.c */
#define MPACK_INTERNAL 1
/* #include "mpack-common.h" */
#if MPACK_DEBUG && MPACK_STDIO
#include <stdarg.h>
#endif
const char* mpack_error_to_string(mpack_error_t error) {
#if MPACK_DEBUG
switch (error) {
#define MPACK_ERROR_STRING_CASE(e) case e: return #e
MPACK_ERROR_STRING_CASE(mpack_ok);
MPACK_ERROR_STRING_CASE(mpack_error_io);
MPACK_ERROR_STRING_CASE(mpack_error_invalid);
MPACK_ERROR_STRING_CASE(mpack_error_type);
MPACK_ERROR_STRING_CASE(mpack_error_too_big);
MPACK_ERROR_STRING_CASE(mpack_error_memory);
MPACK_ERROR_STRING_CASE(mpack_error_bug);
MPACK_ERROR_STRING_CASE(mpack_error_data);
#undef MPACK_ERROR_STRING_CASE
default: break;
}
mpack_assert(0, "unrecognized error %i", (int)error);
return "(unknown mpack_error_t)";
#else
MPACK_UNUSED(error);
return "";
#endif
}
const char* mpack_type_to_string(mpack_type_t type) {
#if MPACK_DEBUG
switch (type) {
#define MPACK_TYPE_STRING_CASE(e) case e: return #e
MPACK_TYPE_STRING_CASE(mpack_type_nil);
MPACK_TYPE_STRING_CASE(mpack_type_bool);
MPACK_TYPE_STRING_CASE(mpack_type_float);
MPACK_TYPE_STRING_CASE(mpack_type_double);
MPACK_TYPE_STRING_CASE(mpack_type_int);
MPACK_TYPE_STRING_CASE(mpack_type_uint);
MPACK_TYPE_STRING_CASE(mpack_type_str);
MPACK_TYPE_STRING_CASE(mpack_type_bin);
MPACK_TYPE_STRING_CASE(mpack_type_ext);
MPACK_TYPE_STRING_CASE(mpack_type_array);
MPACK_TYPE_STRING_CASE(mpack_type_map);
#undef MPACK_TYPE_STRING_CASE
default: break;
}
mpack_assert(0, "unrecognized type %i", (int)type);
return "(unknown mpack_type_t)";
#else
MPACK_UNUSED(type);
return "";
#endif
}
int mpack_tag_cmp(mpack_tag_t left, mpack_tag_t right) {
// positive numbers may be stored as int; convert to uint
if (left.type == mpack_type_int && left.v.i >= 0) {
left.type = mpack_type_uint;
left.v.u = left.v.i;
}
if (right.type == mpack_type_int && right.v.i >= 0) {
right.type = mpack_type_uint;
right.v.u = right.v.i;
}
if (left.type != right.type)
return (int)left.type - (int)right.type;
switch (left.type) {
case mpack_type_nil:
return 0;
case mpack_type_bool:
return (int)left.v.b - (int)right.v.b;
case mpack_type_int:
if (left.v.i == right.v.i)
return 0;
return (left.v.i < right.v.i) ? -1 : 1;
case mpack_type_uint:
if (left.v.u == right.v.u)
return 0;
return (left.v.u < right.v.u) ? -1 : 1;
case mpack_type_array:
case mpack_type_map:
if (left.v.n == right.v.n)
return 0;
return (left.v.n < right.v.n) ? -1 : 1;
case mpack_type_str:
case mpack_type_bin:
if (left.v.l == right.v.l)
return 0;
return (left.v.l < right.v.l) ? -1 : 1;
case mpack_type_ext:
if (left.exttype == right.exttype) {
if (left.v.l == right.v.l)
return 0;
return (left.v.l < right.v.l) ? -1 : 1;
}
return (int)left.exttype - (int)right.exttype;
// floats should not normally be compared for equality. we compare
// with memcmp() to silence compiler warnings, but this will return
// equal if both are NaNs with the same representation (though we may
// want this, for instance if you are for some bizarre reason using
// floats as map keys.) i'm not sure what the right thing to
// do is here. check for NaN first? always return false if the type
// is float? use operator== and pragmas to silence compiler warning?
// please send me your suggestions.
// note also that we don't convert floats to doubles, so when this is
// used for ordering purposes, all floats are ordered before all
// doubles.
case mpack_type_float:
return mpack_memcmp(&left.v.f, &right.v.f, sizeof(left.v.f));
case mpack_type_double:
return mpack_memcmp(&left.v.d, &right.v.d, sizeof(left.v.d));
default:
break;
}
mpack_assert(0, "unrecognized type %i", (int)left.type);
return false;
}
#if MPACK_READ_TRACKING || MPACK_WRITE_TRACKING
#ifndef MPACK_TRACKING_INITIAL_CAPACITY
// seems like a reasonable number. we grow by doubling, and it only
// needs to be as long as the maximum depth of the message.
#define MPACK_TRACKING_INITIAL_CAPACITY 8
#endif
MPACK_INTERNAL_STATIC mpack_error_t mpack_track_init(mpack_track_t* track) {
track->count = 0;
track->capacity = MPACK_TRACKING_INITIAL_CAPACITY;
track->elements = (mpack_track_element_t*)MPACK_MALLOC(sizeof(mpack_track_element_t) * track->capacity);
if (track->elements == NULL)
return mpack_error_memory;
return mpack_ok;
}
MPACK_INTERNAL_STATIC mpack_error_t mpack_track_grow(mpack_track_t* track) {
mpack_assert(track->elements, "null track elements!");
mpack_assert(track->count == track->capacity, "incorrect growing?");
size_t new_capacity = track->capacity * 2;
mpack_track_element_t* new_elements = (mpack_track_element_t*)mpack_realloc(track->elements,
sizeof(mpack_track_element_t) * track->count, sizeof(mpack_track_element_t) * new_capacity);
if (new_elements == NULL)
return mpack_error_memory;
track->elements = new_elements;
track->capacity = new_capacity;
return mpack_ok;
}
#endif
/* mpack-writer.c */
#define MPACK_INTERNAL 1
/* #include "mpack-writer.h" */
#if MPACK_WRITER
#if MPACK_WRITE_TRACKING
#define MPACK_WRITER_TRACK(writer, error) mpack_writer_flag_if_error(writer, error)
static inline void mpack_writer_flag_if_error(mpack_writer_t* writer, mpack_error_t error) {
if (error != mpack_ok)
mpack_writer_flag_error(writer, error);
}
#else
#define MPACK_WRITER_TRACK(writer, error) MPACK_UNUSED(writer)
#endif
static inline void mpack_writer_track_element(mpack_writer_t* writer) {
MPACK_WRITER_TRACK(writer, mpack_track_element(&writer->track, true));
}
void mpack_writer_init(mpack_writer_t* writer, char* buffer, size_t size) {
mpack_memset(writer, 0, sizeof(*writer));
writer->buffer = buffer;
writer->size = size;
MPACK_WRITER_TRACK(writer, mpack_track_init(&writer->track));
}
void mpack_writer_init_error(mpack_writer_t* writer, mpack_error_t error) {
mpack_memset(writer, 0, sizeof(*writer));
writer->error = error;
}
#ifdef MPACK_MALLOC
typedef struct mpack_growable_writer_t {
char** target_data;
size_t* target_size;
} mpack_growable_writer_t;
static void mpack_growable_writer_flush(mpack_writer_t* writer, const char* data, size_t count) {
// This is an intrusive flush function which modifies the writer's buffer
// in response to a flush instead of emptying it in order to add more
// capacity for data. This removes the need to copy data from a fixed buffer
// into a growable one, improving performance.
//
// There are three ways flush can be called:
// - flushing the buffer during writing (used is zero, count is all data, data is buffer)
// - flushing extra data during writing (used is all flushed data, count is extra data, data is not buffer)
// - flushing during teardown (used and count are both all flushed data, data is buffer)
//
// We handle these here, making sure used is the total count in all three cases.
mpack_log("flush size %i used %i data %p buffer %p\n", (int)writer->size, (int)writer->used, data, writer->buffer);
// if the given data is not the old buffer, we'll need to actually copy it into the buffer
bool is_extra_data = (data != writer->buffer);
// if we're flushing all data (used is zero), we should actually grow
size_t new_size = writer->size;
if (writer->used == 0 && count != 0)
new_size *= 2;
while (new_size < (is_extra_data ? writer->used + count : count))
new_size *= 2;
if (new_size > writer->size) {
mpack_log("flush growing from %i to %i\n", (int)writer->size, (int)new_size);
char* new_buffer = (char*)mpack_realloc(writer->buffer, count, new_size);
if (new_buffer == NULL) {
mpack_writer_flag_error(writer, mpack_error_memory);
return;
}
writer->buffer = new_buffer;
writer->size = new_size;
}
if (is_extra_data) {
mpack_memcpy(writer->buffer + writer->used, data, count);
// add our extra data to count
writer->used += count;
} else {
// used is either zero or count; set it to count
writer->used = count;
}
}
static void mpack_growable_writer_teardown(mpack_writer_t* writer) {
mpack_growable_writer_t* growable_writer = (mpack_growable_writer_t*)writer->context;
if (mpack_writer_error(writer) == mpack_ok) {
// shrink the buffer to an appropriate size if the data is
// much smaller than the buffer
if (writer->used < writer->size / 2) {
char* buffer = (char*)mpack_realloc(writer->buffer, writer->used, writer->used);
if (!buffer) {
MPACK_FREE(writer->buffer);
mpack_writer_flag_error(writer, mpack_error_memory);
return;
}
writer->buffer = buffer;
writer->size = writer->used;
}
*growable_writer->target_data = writer->buffer;
*growable_writer->target_size = writer->used;
writer->buffer = NULL;
} else if (writer->buffer) {
MPACK_FREE(writer->buffer);
writer->buffer = NULL;
}
MPACK_FREE(growable_writer);
writer->context = NULL;
}
void mpack_writer_init_growable(mpack_writer_t* writer, char** target_data, size_t* target_size) {
*target_data = NULL;
*target_size = 0;
mpack_growable_writer_t* growable_writer = (mpack_growable_writer_t*) MPACK_MALLOC(sizeof(mpack_growable_writer_t));
if (growable_writer == NULL) {
mpack_writer_init_error(writer, mpack_error_memory);
return;
}
mpack_memset(growable_writer, 0, sizeof(*growable_writer));
growable_writer->target_data = target_data;
growable_writer->target_size = target_size;
size_t capacity = MPACK_BUFFER_SIZE;
char* buffer = (char*)MPACK_MALLOC(capacity);
mpack_writer_init(writer, buffer, capacity);
mpack_writer_set_context(writer, growable_writer);
mpack_writer_set_flush(writer, mpack_growable_writer_flush);
mpack_writer_set_teardown(writer, mpack_growable_writer_teardown);
}
#endif
#if MPACK_STDIO
typedef struct mpack_file_writer_t {
FILE* file;
char buffer[MPACK_BUFFER_SIZE];
} mpack_file_writer_t;
static void mpack_file_writer_flush(mpack_writer_t* writer, const char* buffer, size_t count) {
mpack_file_writer_t* file_writer = (mpack_file_writer_t*)writer->context;
size_t written = fwrite((const void*)buffer, 1, count, file_writer->file);
if (written != count)
mpack_writer_flag_error(writer, mpack_error_io);
}
static void mpack_file_writer_teardown(mpack_writer_t* writer) {
mpack_file_writer_t* file_writer = (mpack_file_writer_t*)writer->context;
if (file_writer->file) {
int ret = fclose(file_writer->file);
file_writer->file = NULL;
if (ret != 0)
mpack_writer_flag_error(writer, mpack_error_io);
}
MPACK_FREE(file_writer);
}
void mpack_writer_init_file(mpack_writer_t* writer, const char* filename) {
mpack_file_writer_t* file_writer = (mpack_file_writer_t*) MPACK_MALLOC(sizeof(mpack_file_writer_t));
if (file_writer == NULL) {
mpack_writer_init_error(writer, mpack_error_memory);
return;
}
file_writer->file = fopen(filename, "wb");
if (file_writer->file == NULL) {
mpack_writer_init_error(writer, mpack_error_io);
MPACK_FREE(file_writer);
return;
}
mpack_writer_init(writer, file_writer->buffer, sizeof(file_writer->buffer));
mpack_writer_set_context(writer, file_writer);
mpack_writer_set_flush(writer, mpack_file_writer_flush);
mpack_writer_set_teardown(writer, mpack_file_writer_teardown);
}
#endif
void mpack_writer_flag_error(mpack_writer_t* writer, mpack_error_t error) {
mpack_log("writer %p setting error %i: %s\n", writer, (int)error, mpack_error_to_string(error));
if (writer->error == mpack_ok) {
writer->error = error;
#if MPACK_SETJMP
if (writer->jump_env)
longjmp(*writer->jump_env, 1);
#endif
}
}
static void mpack_write_native_big(mpack_writer_t* writer, const char* p, size_t count) {
if (mpack_writer_error(writer) != mpack_ok)
return;
mpack_log("big write for %i bytes from %p, %i space left in buffer\n",
(int)count, p, (int)(writer->size - writer->used));
mpack_assert(count > writer->size - writer->used,
"big write requested for %i bytes, but there is %i available "
"space in buffer. call mpack_write_native() instead",
(int)count, (int)(writer->size - writer->used));
// we'll need a flush function
if (!writer->flush) {
mpack_writer_flag_error(writer, mpack_error_io);
return;
}
// we assume that the flush function is orders of magnitude slower
// than memcpy(), so we fill the buffer up first to try to flush as
// infrequently as possible.
// fill the remaining space in the buffer
size_t n = writer->size - writer->used;
if (count < n)
n = count;
mpack_memcpy(writer->buffer + writer->used, p, n);
writer->used += n;
p += n;
count -= n;
if (count == 0)
return;
// flush the buffer
size_t used = writer->used;
writer->used = 0;
writer->flush(writer, writer->buffer, used);
if (mpack_writer_error(writer) != mpack_ok)
return;
// note that an intrusive flush function (such as mpack_growable_writer_flush())
// may have changed size and/or reset used to a non-zero value. we treat both as
// though they may have changed, and there may still be data in the buffer.
// flush the extra data directly if it doesn't fit in the buffer
if (count > writer->size - writer->used) {
writer->flush(writer, p, count);
if (mpack_writer_error(writer) != mpack_ok)
return;
} else {
mpack_memcpy(writer->buffer + writer->used, p, count);
writer->used += count;
}
}
static inline void mpack_write_native(mpack_writer_t* writer, const char* p, size_t count) {
if (mpack_writer_error(writer) != mpack_ok)
return;
if (writer->size - writer->used < count) {
mpack_write_native_big(writer, p, count);
} else {
mpack_memcpy(writer->buffer + writer->used, p, count);
writer->used += count;
}
}
MPACK_ALWAYS_INLINE void mpack_store_native_u8_at(char* p, uint8_t val) {
uint8_t* u = (uint8_t*)p;
u[0] = val;
}
MPACK_ALWAYS_INLINE void mpack_store_native_u16_at(char* p, uint16_t val) {
uint8_t* u = (uint8_t*)p;
u[0] = (uint8_t)((val >> 8) & 0xFF);
u[1] = (uint8_t)( val & 0xFF);
}
MPACK_ALWAYS_INLINE void mpack_store_native_u32_at(char* p, uint32_t val) {
uint8_t* u = (uint8_t*)p;
u[0] = (uint8_t)((val >> 24) & 0xFF);
u[1] = (uint8_t)((val >> 16) & 0xFF);
u[2] = (uint8_t)((val >> 8) & 0xFF);
u[3] = (uint8_t)( val & 0xFF);
}
MPACK_ALWAYS_INLINE void mpack_store_native_u64_at(char* p, uint64_t val) {
uint8_t* u = (uint8_t*)p;
u[0] = (uint8_t)((val >> 56) & 0xFF);
u[1] = (uint8_t)((val >> 48) & 0xFF);
u[2] = (uint8_t)((val >> 40) & 0xFF);
u[3] = (uint8_t)((val >> 32) & 0xFF);
u[4] = (uint8_t)((val >> 24) & 0xFF);
u[5] = (uint8_t)((val >> 16) & 0xFF);
u[6] = (uint8_t)((val >> 8) & 0xFF);
u[7] = (uint8_t)( val & 0xFF);
}
static inline void mpack_write_native_u8(mpack_writer_t* writer, uint8_t val) {
if (writer->size - writer->used >= sizeof(val)) {
mpack_store_native_u8_at(writer->buffer + writer->used, val);
writer->used += sizeof(val);
} else {
char c[sizeof(val)];
mpack_store_native_u8_at(c, val);
mpack_write_native_big(writer, c, sizeof(c));
}
}
static inline void mpack_write_native_u16(mpack_writer_t* writer, uint16_t val) {
if (writer->size - writer->used >= sizeof(val)) {
mpack_store_native_u16_at(writer->buffer + writer->used, val);
writer->used += sizeof(val);
} else {
char c[sizeof(val)];
mpack_store_native_u16_at(c, val);
mpack_write_native_big(writer, c, sizeof(c));
}
}
static inline void mpack_write_native_u32(mpack_writer_t* writer, uint32_t val) {
if (writer->size - writer->used >= sizeof(val)) {
mpack_store_native_u32_at(writer->buffer + writer->used, val);
writer->used += sizeof(val);
} else {
char c[sizeof(val)];
mpack_store_native_u32_at(c, val);
mpack_write_native_big(writer, c, sizeof(c));
}
}
static inline void mpack_write_native_u64(mpack_writer_t* writer, uint64_t val) {
if (writer->size - writer->used >= sizeof(val)) {
mpack_store_native_u64_at(writer->buffer + writer->used, val);
writer->used += sizeof(val);
} else {
char c[sizeof(val)];
mpack_store_native_u64_at(c, val);
mpack_write_native_big(writer, c, sizeof(c));
}
}
static inline void mpack_write_native_i8 (mpack_writer_t* writer, int8_t val) {mpack_write_native_u8 (writer, (uint8_t )val);}
static inline void mpack_write_native_i16 (mpack_writer_t* writer, int16_t val) {mpack_write_native_u16 (writer, (uint16_t)val);}
static inline void mpack_write_native_i32 (mpack_writer_t* writer, int32_t val) {mpack_write_native_u32 (writer, (uint32_t)val);}
static inline void mpack_write_native_i64 (mpack_writer_t* writer, int64_t val) {mpack_write_native_u64 (writer, (uint64_t)val);}
static inline void mpack_write_native_float(mpack_writer_t* writer, float value) {
union {
float f;
uint32_t i;
} u;
u.f = value;
mpack_write_native_u32(writer, u.i);
}
static inline void mpack_write_native_double(mpack_writer_t* writer, double value) {
union {
double d;
uint64_t i;
} u;
u.d = value;
mpack_write_native_u64(writer, u.i);
}
mpack_error_t mpack_writer_destroy(mpack_writer_t* writer) {
MPACK_WRITER_TRACK(writer, mpack_track_destroy(&writer->track, false));
// flush any outstanding data
if (mpack_writer_error(writer) == mpack_ok && writer->used != 0 && writer->flush != NULL) {
writer->flush(writer, writer->buffer, writer->used);
writer->flush = NULL;
}
if (writer->teardown) {
writer->teardown(writer);
writer->teardown = NULL;
}
#if MPACK_SETJMP
if (writer->jump_env)
MPACK_FREE(writer->jump_env);
writer->jump_env = NULL;
#endif
return writer->error;
}
void mpack_writer_destroy_cancel(mpack_writer_t* writer) {
MPACK_WRITER_TRACK(writer, mpack_track_destroy(&writer->track, true));
if (writer->teardown)
writer->teardown(writer);
writer->teardown = NULL;
#if MPACK_SETJMP
if (writer->jump_env)
MPACK_FREE(writer->jump_env);
writer->jump_env = NULL;
#endif
}
void mpack_write_tag(mpack_writer_t* writer, mpack_tag_t value) {
mpack_writer_track_element(writer);
switch (value.type) {
case mpack_type_nil: mpack_write_nil (writer); break;
case mpack_type_bool: mpack_write_bool (writer, value.v.b); break;
case mpack_type_float: mpack_write_float (writer, value.v.f); break;
case mpack_type_double: mpack_write_double(writer, value.v.d); break;
case mpack_type_int: mpack_write_int (writer, value.v.i); break;
case mpack_type_uint: mpack_write_uint (writer, value.v.u); break;
case mpack_type_str: mpack_start_str(writer, value.v.l); break;
case mpack_type_bin: mpack_start_bin(writer, value.v.l); break;
case mpack_type_ext: mpack_start_ext(writer, value.exttype, value.v.l); break;
case mpack_type_array: mpack_start_array(writer, value.v.n); break;
case mpack_type_map: mpack_start_map(writer, value.v.n); break;
default:
mpack_assert(0, "unrecognized type %i", (int)value.type);
break;
}
}
void mpack_write_u8(mpack_writer_t* writer, uint8_t value) {
mpack_writer_track_element(writer);
if (value <= 0x7f) {
mpack_write_native_u8(writer, (uint8_t)value);
} else {
mpack_write_native_u8(writer, 0xcc);
mpack_write_native_u8(writer, (uint8_t)value);
}
}
void mpack_write_u16(mpack_writer_t* writer, uint16_t value) {
mpack_writer_track_element(writer);
if (value <= 0x7f) {
mpack_write_native_u8(writer, (uint8_t)value);
} else if (value <= UINT8_MAX) {
mpack_write_native_u8(writer, 0xcc);
mpack_write_native_u8(writer, (uint8_t)value);
} else {
mpack_write_native_u8(writer, 0xcd);
mpack_write_native_u16(writer, value);
}
}
void mpack_write_u32(mpack_writer_t* writer, uint32_t value) {
mpack_writer_track_element(writer);
if (value <= 0x7f) {
mpack_write_native_u8(writer, (uint8_t)value);
} else if (value <= UINT8_MAX) {
mpack_write_native_u8(writer, 0xcc);
mpack_write_native_u8(writer, (uint8_t)value);
} else if (value <= UINT16_MAX) {
mpack_write_native_u8(writer, 0xcd);
mpack_write_native_u16(writer, (uint16_t)value);
} else {
mpack_write_native_u8(writer, 0xce);
mpack_write_native_u32(writer, value);
}
}
void mpack_write_u64(mpack_writer_t* writer, uint64_t value) {
mpack_writer_track_element(writer);
if (value <= 0x7f) {
mpack_write_native_u8(writer, (uint8_t)value);
} else if (value <= UINT8_MAX) {
mpack_write_native_u8(writer, 0xcc);
mpack_write_native_u8(writer, (uint8_t)value);
} else if (value <= UINT16_MAX) {
mpack_write_native_u8(writer, 0xcd);
mpack_write_native_u16(writer, (uint16_t)value);
} else if (value <= UINT32_MAX) {
mpack_write_native_u8(writer, 0xce);
mpack_write_native_u32(writer, (uint32_t)value);
} else {
mpack_write_native_u8(writer, 0xcf);
mpack_write_native_u64(writer, value);
}
}
void mpack_write_i8(mpack_writer_t* writer, int8_t value) {
// write any non-negative number as a uint
if (value >= 0) {
mpack_write_u8(writer, (uint8_t)value);
return;
}
mpack_writer_track_element(writer);
if (value >= -32) {
mpack_write_native_i8(writer, (int8_t)0xe0 | (int8_t)value); // TODO: remove this (compatibility/1.1 difference?)
} else {
mpack_write_native_u8(writer, 0xd0);
mpack_write_native_i8(writer, value);
}
}
void mpack_write_i16(mpack_writer_t* writer, int16_t value) {
// write any non-negative number as a uint
if (value >= 0) {
mpack_write_u16(writer, (uint16_t)value);
return;
}
mpack_writer_track_element(writer);
if (value >= -32) {
mpack_write_native_i8(writer, (int8_t)0xe0 | (int8_t)value); // TODO: remove this (compatibility/1.1 difference?)
} else if (value >= INT8_MIN) {
mpack_write_native_u8(writer, 0xd0);
mpack_write_native_i8(writer, (int8_t)value);
} else {
mpack_write_native_u8(writer, 0xd1);
mpack_write_native_i16(writer, value);
}
}
void mpack_write_i32(mpack_writer_t* writer, int32_t value) {
// write any non-negative number as a uint
if (value >= 0) {
mpack_write_u32(writer, (uint32_t)value);
return;
}
mpack_writer_track_element(writer);
if (value >= -32) {
mpack_write_native_i8(writer, (int8_t)0xe0 | (int8_t)value); // TODO: remove this (compatibility/1.1 difference?)
} else if (value >= INT8_MIN) {
mpack_write_native_u8(writer, 0xd0);
mpack_write_native_i8(writer, (int8_t)value);
} else if (value >= INT16_MIN) {
mpack_write_native_u8(writer, 0xd1);
mpack_write_native_i16(writer, (int16_t)value);
} else {
mpack_write_native_u8(writer, 0xd2);
mpack_write_native_i32(writer, value);
}
}
void mpack_write_i64(mpack_writer_t* writer, int64_t value) {
// write any non-negative number as a uint
if (value >= 0) {
mpack_write_u64(writer, (uint64_t)value);
return;
}
mpack_writer_track_element(writer);
if (value >= -32) {
mpack_write_native_i8(writer, (int8_t)0xe0 | (int8_t)value); // TODO: remove this (compatibility/1.1 difference?)
} else if (value >= INT8_MIN) {
mpack_write_native_u8(writer, 0xd0);
mpack_write_native_i8(writer, (int8_t)value);
} else if (value >= INT16_MIN) {
mpack_write_native_u8(writer, 0xd1);
mpack_write_native_i16(writer, (int16_t)value);
} else if (value >= INT32_MIN) {
mpack_write_native_u8(writer, 0xd2);
mpack_write_native_i32(writer, (int32_t)value);
} else {
mpack_write_native_u8(writer, 0xd3);
mpack_write_native_i64(writer, value);
}
}
void mpack_write_bool(mpack_writer_t* writer, bool value) {
mpack_writer_track_element(writer);
mpack_write_native_u8(writer, (uint8_t)(0xc2 | (value ? 1 : 0)));
}
void mpack_write_true(mpack_writer_t* writer) {
mpack_writer_track_element(writer);
mpack_write_native_u8(writer, (uint8_t)0xc3);
}
void mpack_write_false(mpack_writer_t* writer) {
mpack_writer_track_element(writer);
mpack_write_native_u8(writer, (uint8_t)0xc2);
}
void mpack_write_nil(mpack_writer_t* writer) {
mpack_writer_track_element(writer);
mpack_write_native_u8(writer, 0xc0);
}
void mpack_write_float(mpack_writer_t* writer, float value) {
mpack_writer_track_element(writer);
mpack_write_native_u8(writer, 0xca);
mpack_write_native_float(writer, value);
}
void mpack_write_double(mpack_writer_t* writer, double value) {
mpack_writer_track_element(writer);
mpack_write_native_u8(writer, 0xcb);
mpack_write_native_double(writer, value);
}
#if MPACK_WRITE_TRACKING
void mpack_finish_array(mpack_writer_t* writer) {
MPACK_WRITER_TRACK(writer, mpack_track_pop(&writer->track, mpack_type_array));
}
void mpack_finish_map(mpack_writer_t* writer) {
MPACK_WRITER_TRACK(writer, mpack_track_pop(&writer->track, mpack_type_map));
}
void mpack_finish_str(mpack_writer_t* writer) {
MPACK_WRITER_TRACK(writer, mpack_track_pop(&writer->track, mpack_type_str));
}
void mpack_finish_bin(mpack_writer_t* writer) {
MPACK_WRITER_TRACK(writer, mpack_track_pop(&writer->track, mpack_type_bin));
}
void mpack_finish_ext(mpack_writer_t* writer) {
MPACK_WRITER_TRACK(writer, mpack_track_pop(&writer->track, mpack_type_ext));
}
void mpack_finish_type(mpack_writer_t* writer, mpack_type_t type) {
MPACK_WRITER_TRACK(writer, mpack_track_pop(&writer->track, type));
}
#endif
void mpack_start_array(mpack_writer_t* writer, uint32_t count) {
if (mpack_writer_error(writer) != mpack_ok)
return;