forked from camgunz/cmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmp.c
3537 lines (2978 loc) · 79 KB
/
cmp.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) 2020 Charles Gunyon
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.
*/
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "cmp.h"
static const uint32_t version = 19;
static const uint32_t mp_version = 5;
enum {
POSITIVE_FIXNUM_MARKER = 0x00,
FIXMAP_MARKER = 0x80,
FIXARRAY_MARKER = 0x90,
FIXSTR_MARKER = 0xA0,
NIL_MARKER = 0xC0,
FALSE_MARKER = 0xC2,
TRUE_MARKER = 0xC3,
BIN8_MARKER = 0xC4,
BIN16_MARKER = 0xC5,
BIN32_MARKER = 0xC6,
EXT8_MARKER = 0xC7,
EXT16_MARKER = 0xC8,
EXT32_MARKER = 0xC9,
FLOAT_MARKER = 0xCA,
DOUBLE_MARKER = 0xCB,
U8_MARKER = 0xCC,
U16_MARKER = 0xCD,
U32_MARKER = 0xCE,
U64_MARKER = 0xCF,
S8_MARKER = 0xD0,
S16_MARKER = 0xD1,
S32_MARKER = 0xD2,
S64_MARKER = 0xD3,
FIXEXT1_MARKER = 0xD4,
FIXEXT2_MARKER = 0xD5,
FIXEXT4_MARKER = 0xD6,
FIXEXT8_MARKER = 0xD7,
FIXEXT16_MARKER = 0xD8,
STR8_MARKER = 0xD9,
STR16_MARKER = 0xDA,
STR32_MARKER = 0xDB,
ARRAY16_MARKER = 0xDC,
ARRAY32_MARKER = 0xDD,
MAP16_MARKER = 0xDE,
MAP32_MARKER = 0xDF,
NEGATIVE_FIXNUM_MARKER = 0xE0
};
enum {
FIXARRAY_SIZE = 0xF,
FIXMAP_SIZE = 0xF,
FIXSTR_SIZE = 0x1F
};
enum {
ERROR_NONE,
STR_DATA_LENGTH_TOO_LONG_ERROR,
BIN_DATA_LENGTH_TOO_LONG_ERROR,
ARRAY_LENGTH_TOO_LONG_ERROR,
MAP_LENGTH_TOO_LONG_ERROR,
INPUT_VALUE_TOO_LARGE_ERROR,
FIXED_VALUE_WRITING_ERROR,
TYPE_MARKER_READING_ERROR,
TYPE_MARKER_WRITING_ERROR,
DATA_READING_ERROR,
DATA_WRITING_ERROR,
EXT_TYPE_READING_ERROR,
EXT_TYPE_WRITING_ERROR,
INVALID_TYPE_ERROR,
LENGTH_READING_ERROR,
LENGTH_WRITING_ERROR,
SKIP_DEPTH_LIMIT_EXCEEDED_ERROR,
INTERNAL_ERROR,
DISABLED_FLOATING_POINT_ERROR,
ERROR_MAX
};
const char * const cmp_error_messages[ERROR_MAX + 1] = {
"No Error",
"Specified string data length is too long (> 0xFFFFFFFF)",
"Specified binary data length is too long (> 0xFFFFFFFF)",
"Specified array length is too long (> 0xFFFFFFFF)",
"Specified map length is too long (> 0xFFFFFFFF)",
"Input value is too large",
"Error writing fixed value",
"Error reading type marker",
"Error writing type marker",
"Error reading packed data",
"Error writing packed data",
"Error reading ext type",
"Error writing ext type",
"Invalid type",
"Error reading size",
"Error writing size",
"Depth limit exceeded while skipping",
"Internal error",
"Floating point operations disabled",
"Max Error"
};
#if WORDS_BIGENDIAN == 0
#define is_bigendian() (false)
#elif WORDS_BIGENDIAN == 1
#define is_bigendian() (true)
#else
static const int32_t _i = 1;
#define is_bigendian() ((*(char *)&_i) == 0)
#endif
static uint16_t be16(uint16_t x) {
char *b = (char *)&x;
if (!is_bigendian()) {
char swap = b[0];
b[0] = b[1];
b[1] = swap;
}
return x;
}
static uint32_t be32(uint32_t x) {
char *b = (char *)&x;
if (!is_bigendian()) {
char swap = b[0];
b[0] = b[3];
b[3] = swap;
swap = b[1];
b[1] = b[2];
b[2] = swap;
}
return x;
}
static uint64_t be64(uint64_t x) {
char *b = (char *)&x;
if (!is_bigendian()) {
char swap;
swap = b[0];
b[0] = b[7];
b[7] = swap;
swap = b[1];
b[1] = b[6];
b[6] = swap;
swap = b[2];
b[2] = b[5];
b[5] = swap;
swap = b[3];
b[3] = b[4];
b[4] = swap;
}
return x;
}
#ifndef CMP_NO_FLOAT
static float decode_befloat(const char *b) {
float f = 0.;
char *fb = (char *)&f;
if (!is_bigendian()) {
fb[0] = b[3];
fb[1] = b[2];
fb[2] = b[1];
fb[3] = b[0];
}
return f;
}
static double decode_bedouble(const char *b) {
double d = 0.;
char *db = (char *)&d;
if (!is_bigendian()) {
db[0] = b[7];
db[1] = b[6];
db[2] = b[5];
db[3] = b[4];
db[4] = b[3];
db[5] = b[2];
db[6] = b[1];
db[7] = b[0];
}
return d;
}
#endif /* CMP_NO_FLOAT */
static bool read_byte(cmp_ctx_t *ctx, uint8_t *x) {
return ctx->read(ctx, x, sizeof(uint8_t));
}
static bool write_byte(cmp_ctx_t *ctx, uint8_t x) {
return (ctx->write(ctx, &x, sizeof(uint8_t)) == (sizeof(uint8_t)));
}
static bool skip_bytes(cmp_ctx_t *ctx, size_t count) {
if (ctx->skip) {
return ctx->skip(ctx, count);
}
else {
uint8_t floor;
size_t i;
for (i = 0; i < count; i++) {
if (!ctx->read(ctx, &floor, sizeof(uint8_t))) {
return false;
}
}
return true;
}
}
static bool read_type_marker(cmp_ctx_t *ctx, uint8_t *marker) {
if (read_byte(ctx, marker)) {
return true;
}
ctx->error = TYPE_MARKER_READING_ERROR;
return false;
}
static bool write_type_marker(cmp_ctx_t *ctx, uint8_t marker) {
if (write_byte(ctx, marker))
return true;
ctx->error = TYPE_MARKER_WRITING_ERROR;
return false;
}
static bool write_fixed_value(cmp_ctx_t *ctx, uint8_t value) {
if (write_byte(ctx, value))
return true;
ctx->error = FIXED_VALUE_WRITING_ERROR;
return false;
}
static bool type_marker_to_cmp_type(uint8_t type_marker, uint8_t *cmp_type) {
if (type_marker <= 0x7F) {
*cmp_type = CMP_TYPE_POSITIVE_FIXNUM;
return true;
}
if (type_marker <= 0x8F) {
*cmp_type = CMP_TYPE_FIXMAP;
return true;
}
if (type_marker <= 0x9F) {
*cmp_type = CMP_TYPE_FIXARRAY;
return true;
}
if (type_marker <= 0xBF) {
*cmp_type = CMP_TYPE_FIXSTR;
return true;
}
if (type_marker >= 0xE0) {
*cmp_type = CMP_TYPE_NEGATIVE_FIXNUM;
return true;
}
switch (type_marker) {
case NIL_MARKER:
*cmp_type = CMP_TYPE_NIL;
return true;
case FALSE_MARKER:
*cmp_type = CMP_TYPE_BOOLEAN;
return true;
case TRUE_MARKER:
*cmp_type = CMP_TYPE_BOOLEAN;
return true;
case BIN8_MARKER:
*cmp_type = CMP_TYPE_BIN8;
return true;
case BIN16_MARKER:
*cmp_type = CMP_TYPE_BIN16;
return true;
case BIN32_MARKER:
*cmp_type = CMP_TYPE_BIN32;
return true;
case EXT8_MARKER:
*cmp_type = CMP_TYPE_EXT8;
return true;
case EXT16_MARKER:
*cmp_type = CMP_TYPE_EXT16;
return true;
case EXT32_MARKER:
*cmp_type = CMP_TYPE_EXT32;
return true;
case FLOAT_MARKER:
*cmp_type = CMP_TYPE_FLOAT;
return true;
case DOUBLE_MARKER:
*cmp_type = CMP_TYPE_DOUBLE;
return true;
case U8_MARKER:
*cmp_type = CMP_TYPE_UINT8;
return true;
case U16_MARKER:
*cmp_type = CMP_TYPE_UINT16;
return true;
case U32_MARKER:
*cmp_type = CMP_TYPE_UINT32;
return true;
case U64_MARKER:
*cmp_type = CMP_TYPE_UINT64;
return true;
case S8_MARKER:
*cmp_type = CMP_TYPE_SINT8;
return true;
case S16_MARKER:
*cmp_type = CMP_TYPE_SINT16;
return true;
case S32_MARKER:
*cmp_type = CMP_TYPE_SINT32;
return true;
case S64_MARKER:
*cmp_type = CMP_TYPE_SINT64;
return true;
case FIXEXT1_MARKER:
*cmp_type = CMP_TYPE_FIXEXT1;
return true;
case FIXEXT2_MARKER:
*cmp_type = CMP_TYPE_FIXEXT2;
return true;
case FIXEXT4_MARKER:
*cmp_type = CMP_TYPE_FIXEXT4;
return true;
case FIXEXT8_MARKER:
*cmp_type = CMP_TYPE_FIXEXT8;
return true;
case FIXEXT16_MARKER:
*cmp_type = CMP_TYPE_FIXEXT16;
return true;
case STR8_MARKER:
*cmp_type = CMP_TYPE_STR8;
return true;
case STR16_MARKER:
*cmp_type = CMP_TYPE_STR16;
return true;
case STR32_MARKER:
*cmp_type = CMP_TYPE_STR32;
return true;
case ARRAY16_MARKER:
*cmp_type = CMP_TYPE_ARRAY16;
return true;
case ARRAY32_MARKER:
*cmp_type = CMP_TYPE_ARRAY32;
return true;
case MAP16_MARKER:
*cmp_type = CMP_TYPE_MAP16;
return true;
case MAP32_MARKER:
*cmp_type = CMP_TYPE_MAP32;
return true;
default:
return false;
}
}
static bool read_type_size(cmp_ctx_t *ctx, uint8_t type_marker,
uint8_t cmp_type,
uint32_t *size) {
uint8_t u8temp = 0;
uint16_t u16temp = 0;
uint32_t u32temp = 0;
switch (cmp_type) {
case CMP_TYPE_POSITIVE_FIXNUM:
*size = 0;
return true;
case CMP_TYPE_FIXMAP:
*size = (type_marker & FIXMAP_SIZE);
return true;
case CMP_TYPE_FIXARRAY:
*size = (type_marker & FIXARRAY_SIZE);
return true;
case CMP_TYPE_FIXSTR:
*size = (type_marker & FIXSTR_SIZE);
return true;
case CMP_TYPE_NIL:
*size = 0;
return true;
case CMP_TYPE_BOOLEAN:
*size = 0;
return true;
case CMP_TYPE_BIN8:
if (!ctx->read(ctx, &u8temp, sizeof(uint8_t))) {
ctx->error = LENGTH_READING_ERROR;
return false;
}
*size = u8temp;
return true;
case CMP_TYPE_BIN16:
if (!ctx->read(ctx, &u16temp, sizeof(uint16_t))) {
ctx->error = LENGTH_READING_ERROR;
return false;
}
*size = be16(u16temp);
return true;
case CMP_TYPE_BIN32:
if (!ctx->read(ctx, &u32temp, sizeof(uint32_t))) {
ctx->error = LENGTH_READING_ERROR;
return false;
}
*size = be32(u32temp);
return true;
case CMP_TYPE_EXT8:
if (!ctx->read(ctx, &u8temp, sizeof(uint8_t))) {
ctx->error = LENGTH_READING_ERROR;
return false;
}
*size = u8temp;
return true;
case CMP_TYPE_EXT16:
if (!ctx->read(ctx, &u16temp, sizeof(uint16_t))) {
ctx->error = LENGTH_READING_ERROR;
return false;
}
*size = be16(u16temp);
return true;
case CMP_TYPE_EXT32:
if (!ctx->read(ctx, &u32temp, sizeof(uint32_t))) {
ctx->error = LENGTH_READING_ERROR;
return false;
}
*size = be32(u32temp);
return true;
case CMP_TYPE_FLOAT:
*size = 4;
return true;
case CMP_TYPE_DOUBLE:
*size = 8;
return true;
case CMP_TYPE_UINT8:
*size = 1;
return true;
case CMP_TYPE_UINT16:
*size = 2;
return true;
case CMP_TYPE_UINT32:
*size = 4;
return true;
case CMP_TYPE_UINT64:
*size = 8;
return true;
case CMP_TYPE_SINT8:
*size = 1;
return true;
case CMP_TYPE_SINT16:
*size = 2;
return true;
case CMP_TYPE_SINT32:
*size = 4;
return true;
case CMP_TYPE_SINT64:
*size = 8;
return true;
case CMP_TYPE_FIXEXT1:
*size = 1;
return true;
case CMP_TYPE_FIXEXT2:
*size = 2;
return true;
case CMP_TYPE_FIXEXT4:
*size = 4;
return true;
case CMP_TYPE_FIXEXT8:
*size = 8;
return true;
case CMP_TYPE_FIXEXT16:
*size = 16;
return true;
case CMP_TYPE_STR8:
if (!ctx->read(ctx, &u8temp, sizeof(uint8_t))) {
ctx->error = DATA_READING_ERROR;
return false;
}
*size = u8temp;
return true;
case CMP_TYPE_STR16:
if (!ctx->read(ctx, &u16temp, sizeof(uint16_t))) {
ctx->error = DATA_READING_ERROR;
return false;
}
*size = be16(u16temp);
return true;
case CMP_TYPE_STR32:
if (!ctx->read(ctx, &u32temp, sizeof(uint32_t))) {
ctx->error = DATA_READING_ERROR;
return false;
}
*size = be32(u32temp);
return true;
case CMP_TYPE_ARRAY16:
if (!ctx->read(ctx, &u16temp, sizeof(uint16_t))) {
ctx->error = DATA_READING_ERROR;
return false;
}
*size = be16(u16temp);
return true;
case CMP_TYPE_ARRAY32:
if (!ctx->read(ctx, &u32temp, sizeof(uint32_t))) {
ctx->error = DATA_READING_ERROR;
return false;
}
*size = be32(u32temp);
return true;
case CMP_TYPE_MAP16:
if (!ctx->read(ctx, &u16temp, sizeof(uint16_t))) {
ctx->error = DATA_READING_ERROR;
return false;
}
*size = be16(u16temp);
return true;
case CMP_TYPE_MAP32:
if (!ctx->read(ctx, &u32temp, sizeof(uint32_t))) {
ctx->error = DATA_READING_ERROR;
return false;
}
*size = be32(u32temp);
return true;
case CMP_TYPE_NEGATIVE_FIXNUM:
*size = 0;
return true;
default:
ctx->error = INVALID_TYPE_ERROR;
return false;
}
}
static bool read_obj_data(cmp_ctx_t *ctx, uint8_t type_marker,
cmp_object_t *obj) {
switch (obj->type) {
case CMP_TYPE_POSITIVE_FIXNUM:
obj->as.u8 = type_marker;
return true;
case CMP_TYPE_NEGATIVE_FIXNUM:
obj->as.s8 = type_marker;
return true;
case CMP_TYPE_NIL:
obj->as.u8 = 0;
return true;
case CMP_TYPE_BOOLEAN:
switch (type_marker) {
case TRUE_MARKER:
obj->as.boolean = true;
return true;
case FALSE_MARKER:
obj->as.boolean = false;
return true;
default:
break;
}
ctx->error = INTERNAL_ERROR;
return false;
case CMP_TYPE_UINT8:
if (!ctx->read(ctx, &obj->as.u8, sizeof(uint8_t))) {
ctx->error = DATA_READING_ERROR;
return false;
}
return true;
case CMP_TYPE_UINT16:
if (!ctx->read(ctx, &obj->as.u16, sizeof(uint16_t))) {
ctx->error = DATA_READING_ERROR;
return false;
}
obj->as.u16 = be16(obj->as.u16);
return true;
case CMP_TYPE_UINT32:
if (!ctx->read(ctx, &obj->as.u32, sizeof(uint32_t))) {
ctx->error = DATA_READING_ERROR;
return false;
}
obj->as.u32 = be32(obj->as.u32);
return true;
case CMP_TYPE_UINT64:
if (!ctx->read(ctx, &obj->as.u64, sizeof(uint64_t))) {
ctx->error = DATA_READING_ERROR;
return false;
}
obj->as.u64 = be64(obj->as.u64);
return true;
case CMP_TYPE_SINT8:
if (!ctx->read(ctx, &obj->as.s8, sizeof(int8_t))) {
ctx->error = DATA_READING_ERROR;
return false;
}
return true;
case CMP_TYPE_SINT16:
if (!ctx->read(ctx, &obj->as.s16, sizeof(int16_t))) {
ctx->error = DATA_READING_ERROR;
return false;
}
obj->as.s16 = be16(obj->as.s16);
return true;
case CMP_TYPE_SINT32:
if (!ctx->read(ctx, &obj->as.s32, sizeof(int32_t))) {
ctx->error = DATA_READING_ERROR;
return false;
}
obj->as.s32 = be32(obj->as.s32);
return true;
case CMP_TYPE_SINT64:
if (!ctx->read(ctx, &obj->as.s64, sizeof(int64_t))) {
ctx->error = DATA_READING_ERROR;
return false;
}
obj->as.s64 = be64(obj->as.s64);
return true;
case CMP_TYPE_FLOAT:
{
#ifndef CMP_NO_FLOAT
char bytes[4];
if (!ctx->read(ctx, bytes, 4)) {
ctx->error = DATA_READING_ERROR;
return false;
}
obj->as.flt = decode_befloat(bytes);
return true;
#else /* CMP_NO_FLOAT */
ctx->error = DISABLED_FLOATING_POINT_ERROR;
return false;
#endif /* CMP_NO_FLOAT */
}
case CMP_TYPE_DOUBLE:
{
#ifndef CMP_NO_FLOAT
char bytes[8];
if (!ctx->read(ctx, bytes, 8)) {
ctx->error = DATA_READING_ERROR;
return false;
}
obj->as.dbl = decode_bedouble(bytes);
return true;
#else /* CMP_NO_FLOAT */
ctx->error = DISABLED_FLOATING_POINT_ERROR;
return false;
#endif /* CMP_NO_FLOAT */
}
case CMP_TYPE_BIN8:
case CMP_TYPE_BIN16:
case CMP_TYPE_BIN32:
return read_type_size(ctx, type_marker, obj->type, &obj->as.bin_size);
case CMP_TYPE_FIXSTR:
case CMP_TYPE_STR8:
case CMP_TYPE_STR16:
case CMP_TYPE_STR32:
return read_type_size(ctx, type_marker, obj->type, &obj->as.str_size);
case CMP_TYPE_FIXARRAY:
case CMP_TYPE_ARRAY16:
case CMP_TYPE_ARRAY32:
return read_type_size(ctx, type_marker, obj->type, &obj->as.array_size);
case CMP_TYPE_FIXMAP:
case CMP_TYPE_MAP16:
case CMP_TYPE_MAP32:
return read_type_size(ctx, type_marker, obj->type, &obj->as.map_size);
case CMP_TYPE_FIXEXT1:
if (!ctx->read(ctx, &obj->as.ext.type, sizeof(int8_t))) {
ctx->error = EXT_TYPE_READING_ERROR;
return false;
}
obj->as.ext.size = 1;
return true;
case CMP_TYPE_FIXEXT2:
if (!ctx->read(ctx, &obj->as.ext.type, sizeof(int8_t))) {
ctx->error = EXT_TYPE_READING_ERROR;
return false;
}
obj->as.ext.size = 2;
return true;
case CMP_TYPE_FIXEXT4:
if (!ctx->read(ctx, &obj->as.ext.type, sizeof(int8_t))) {
ctx->error = EXT_TYPE_READING_ERROR;
return false;
}
obj->as.ext.size = 4;
return true;
case CMP_TYPE_FIXEXT8:
if (!ctx->read(ctx, &obj->as.ext.type, sizeof(int8_t))) {
ctx->error = EXT_TYPE_READING_ERROR;
return false;
}
obj->as.ext.size = 8;
return true;
case CMP_TYPE_FIXEXT16:
if (!ctx->read(ctx, &obj->as.ext.type, sizeof(int8_t))) {
ctx->error = EXT_TYPE_READING_ERROR;
return false;
}
obj->as.ext.size = 16;
return true;
case CMP_TYPE_EXT8:
if (!read_type_size(ctx, type_marker, obj->type, &obj->as.ext.size)) {
return false;
}
if (!ctx->read(ctx, &obj->as.ext.type, sizeof(int8_t))) {
ctx->error = EXT_TYPE_READING_ERROR;
return false;
}
return true;
case CMP_TYPE_EXT16:
if (!read_type_size(ctx, type_marker, obj->type, &obj->as.ext.size)) {
return false;
}
if (!ctx->read(ctx, &obj->as.ext.type, sizeof(int8_t))) {
ctx->error = EXT_TYPE_READING_ERROR;
return false;
}
obj->as.ext.type = obj->as.ext.type;
return true;
case CMP_TYPE_EXT32:
if (!read_type_size(ctx, type_marker, obj->type, &obj->as.ext.size)) {
return false;
}
if (!ctx->read(ctx, &obj->as.ext.type, sizeof(int8_t))) {
ctx->error = EXT_TYPE_READING_ERROR;
return false;
}
obj->as.ext.type = obj->as.ext.type;
return true;
default:
break;
}
ctx->error = INVALID_TYPE_ERROR;
return false;
}
void cmp_init(cmp_ctx_t *ctx, void *buf, cmp_reader read,
cmp_skipper skip,
cmp_writer write) {
ctx->error = ERROR_NONE;
ctx->buf = buf;
ctx->read = read;
ctx->skip = skip;
ctx->write = write;
}
uint32_t cmp_version(void) {
return version;
}
uint32_t cmp_mp_version(void) {
return mp_version;
}
const char* cmp_strerror(cmp_ctx_t *ctx) {
if (ctx->error > ERROR_NONE && ctx->error < ERROR_MAX)
return cmp_error_messages[ctx->error];
return "";
}
bool cmp_write_pfix(cmp_ctx_t *ctx, uint8_t c) {
if (c <= 0x7F)
return write_fixed_value(ctx, c);
ctx->error = INPUT_VALUE_TOO_LARGE_ERROR;
return false;
}
bool cmp_write_nfix(cmp_ctx_t *ctx, int8_t c) {
if (c >= -32 && c <= -1)
return write_fixed_value(ctx, c);
ctx->error = INPUT_VALUE_TOO_LARGE_ERROR;
return false;
}
bool cmp_write_sfix(cmp_ctx_t *ctx, int8_t c) {
if (c >= 0)
return cmp_write_pfix(ctx, c);
if (c >= -32 && c <= -1)
return cmp_write_nfix(ctx, c);
ctx->error = INPUT_VALUE_TOO_LARGE_ERROR;
return false;
}
bool cmp_write_s8(cmp_ctx_t *ctx, int8_t c) {
if (!write_type_marker(ctx, S8_MARKER))
return false;
return ctx->write(ctx, &c, sizeof(int8_t));
}
bool cmp_write_s16(cmp_ctx_t *ctx, int16_t s) {
if (!write_type_marker(ctx, S16_MARKER))
return false;
s = be16(s);
return ctx->write(ctx, &s, sizeof(int16_t));
}
bool cmp_write_s32(cmp_ctx_t *ctx, int32_t i) {
if (!write_type_marker(ctx, S32_MARKER))
return false;
i = be32(i);
return ctx->write(ctx, &i, sizeof(int32_t));
}
bool cmp_write_s64(cmp_ctx_t *ctx, int64_t l) {
if (!write_type_marker(ctx, S64_MARKER))
return false;
l = be64(l);
return ctx->write(ctx, &l, sizeof(int64_t));
}
bool cmp_write_integer(cmp_ctx_t *ctx, int64_t d) {
if (d >= 0)
return cmp_write_uinteger(ctx, d);
if (d >= -32)
return cmp_write_nfix(ctx, (int8_t)d);
if (d >= -128)
return cmp_write_s8(ctx, (int8_t)d);
if (d >= -32768)
return cmp_write_s16(ctx, (int16_t)d);
if (d >= (-2147483647 - 1))
return cmp_write_s32(ctx, (int32_t)d);
return cmp_write_s64(ctx, d);
}
bool cmp_write_ufix(cmp_ctx_t *ctx, uint8_t c) {
return cmp_write_pfix(ctx, c);
}
bool cmp_write_u8(cmp_ctx_t *ctx, uint8_t c) {
if (!write_type_marker(ctx, U8_MARKER))
return false;
return ctx->write(ctx, &c, sizeof(uint8_t));
}
bool cmp_write_u16(cmp_ctx_t *ctx, uint16_t s) {
if (!write_type_marker(ctx, U16_MARKER))
return false;
s = be16(s);
return ctx->write(ctx, &s, sizeof(uint16_t));
}
bool cmp_write_u32(cmp_ctx_t *ctx, uint32_t i) {
if (!write_type_marker(ctx, U32_MARKER))
return false;
i = be32(i);
return ctx->write(ctx, &i, sizeof(uint32_t));
}
bool cmp_write_u64(cmp_ctx_t *ctx, uint64_t l) {
if (!write_type_marker(ctx, U64_MARKER))
return false;
l = be64(l);
return ctx->write(ctx, &l, sizeof(uint64_t));
}
bool cmp_write_uinteger(cmp_ctx_t *ctx, uint64_t u) {
if (u <= 0x7F)
return cmp_write_pfix(ctx, (uint8_t)u);
if (u <= 0xFF)
return cmp_write_u8(ctx, (uint8_t)u);
if (u <= 0xFFFF)
return cmp_write_u16(ctx, (uint16_t)u);
if (u <= 0xFFFFFFFF)
return cmp_write_u32(ctx, (uint32_t)u);
return cmp_write_u64(ctx, u);
}
#ifndef CMP_NO_FLOAT
bool cmp_write_float(cmp_ctx_t *ctx, float f) {
if (!write_type_marker(ctx, FLOAT_MARKER))
return false;
/*
* We may need to swap the float's bytes, but we can't just swap them inside
* the float because the swapped bytes may not constitute a valid float.
* Therefore, we have to create a buffer and swap the bytes there.
*/
if (!is_bigendian()) {
char swapped[sizeof(float)];
char *fbuf = (char *)&f;
size_t i;
for (i = 0; i < sizeof(float); i++)
swapped[i] = fbuf[sizeof(float) - i - 1];
return ctx->write(ctx, swapped, sizeof(float));
}
return ctx->write(ctx, &f, sizeof(float));
}
bool cmp_write_double(cmp_ctx_t *ctx, double d) {
if (!write_type_marker(ctx, DOUBLE_MARKER))
return false;
/* Same deal for doubles */
if (!is_bigendian()) {
char swapped[sizeof(double)];
char *dbuf = (char *)&d;
size_t i;
for (i = 0; i < sizeof(double); i++)
swapped[i] = dbuf[sizeof(double) - i - 1];
return ctx->write(ctx, swapped, sizeof(double));
}
return ctx->write(ctx, &d, sizeof(double));
}
bool cmp_write_decimal(cmp_ctx_t *ctx, double d) {
float f = (float)d;
double df = (double)f;
if (df == d)
return cmp_write_float(ctx, f);
else
return cmp_write_double(ctx, d);
}
#endif /* CMP_NO_FLOAT */
bool cmp_write_nil(cmp_ctx_t *ctx) {
return write_type_marker(ctx, NIL_MARKER);
}
bool cmp_write_true(cmp_ctx_t *ctx) {
return write_type_marker(ctx, TRUE_MARKER);
}
bool cmp_write_false(cmp_ctx_t *ctx) {
return write_type_marker(ctx, FALSE_MARKER);
}
bool cmp_write_bool(cmp_ctx_t *ctx, bool b) {
if (b)
return cmp_write_true(ctx);
return cmp_write_false(ctx);
}
bool cmp_write_u8_as_bool(cmp_ctx_t *ctx, uint8_t b) {
if (b)
return cmp_write_true(ctx);
return cmp_write_false(ctx);
}
bool cmp_write_fixstr_marker(cmp_ctx_t *ctx, uint8_t size) {