-
Notifications
You must be signed in to change notification settings - Fork 8
/
msgpck.cpp
825 lines (739 loc) · 17.6 KB
/
msgpck.cpp
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
/**
* Copyright (C) 2014 SINTEF <[email protected]>
*
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3, 29 June 2007;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.gnu.org/licenses/lgpl-3.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "Arduino.h"
#include "msgpck.h"
#define msgpck_empty 0xff
#define msgpck_nil 0xc0
#define msgpck_bool 0xc2
#define msgpck_uint 0xcc
#define msgpck_sint 0xd0
#define msgpck_float 0xca
#define msgpck_string 0xd9
#define msgpck_bin 0xc4
#define msgpck_ext 0xc7
#define msgpck_array 0xdc
#define msgpck_map 0xde
#define msgpck_unknown 0x00
uint8_t msgpck_what_next(Stream * s) {
int b = s->peek();
switch(b){
case -1:
return msgpck_empty;
break;
case 0xc0:
return msgpck_nil;
break;
case 0xc2:
return msgpck_bool;
break;
case 0xc3:
return msgpck_bool;
break;
case 0xc4:
return msgpck_bin;
break;
case 0xc5:
return msgpck_bin;
break;
case 0xc6:
return msgpck_bin;
break;
case 0xca:
return msgpck_float;
break;
case 0xcb:
return msgpck_float;
break;
case 0xcc:
return msgpck_uint;
break;
case 0xcd:
return msgpck_uint;
break;
case 0xce:
return msgpck_uint;
break;
case 0xcf:
return msgpck_uint;
break;
case 0xd0:
return msgpck_sint;
break;
case 0xd1:
return msgpck_sint;
break;
case 0xd2:
return msgpck_sint;
break;
case 0xd3:
return msgpck_sint;
break;
case 0xd4:
return msgpck_ext;
break;
case 0xd5:
return msgpck_ext;
break;
case 0xd6:
return msgpck_ext;
break;
case 0xd7:
return msgpck_ext;
break;
case 0xd8:
return msgpck_ext;
break;
case 0xd9:
return msgpck_string;
break;
case 0xda:
return msgpck_string;
break;
case 0xdb:
return msgpck_string;
break;
case 0xdc:
return msgpck_array;
break;
case 0xdd:
return msgpck_array;
break;
case 0xde:
return msgpck_map;
break;
case 0xdf:
return msgpck_map;
break;
default:
if(b >= 224)
return msgpck_uint;
if(b < 128)
return msgpck_sint;
if((b >> 5) == 5)
return msgpck_string;
if((b >> 4) == 8)
return msgpck_map;
if((b >> 4) == 9)
return msgpck_array;
return msgpck_unknown;
}
}
bool msgpck_nil_next(Stream * s) {
int b = s->peek();
return (b != -1) && (b == 0xc0);
}
bool msgpck_bool_next(Stream * s) {
int b = s->peek();
return (b != -1) && ((b == 0xc2) || (b == 0xc3));
}
bool msgpck_integer_next(Stream * s) {
int b = s->peek();
return ((b < 128) ||
(b >= 224) ||
(b == 0xcc) ||
(b == 0xcd) ||
(b == 0xce) ||
(b == 0xcf) ||
(b == 0xd0) ||
(b == 0xd1) ||
(b == 0xd2) ||
(b == 0xd3));
}
bool msgpck_signed_next(Stream * s) {
int b = s->peek();
return (b != -1) && ((b >= 224) ||
(b == 0xd0) ||
(b == 0xd1) ||
(b == 0xd2) ||
(b == 0xd3));
}
bool msgpck_float_next(Stream * s) {
int b = s->peek();
return (b != -1) && ((b == 0xca) || (b == 0xcb));
}
bool msgpck_string_next(Stream * s) {
int b = s->peek();
return (b != -1) && (((b >> 5) == 5) ||
(b == 0xd9) ||
(b == 0xda) ||
(b == 0xdb));
}
bool msgpck_bin_next(Stream * s) {
int b = s->peek();
return (b != -1) && ((b == 0xc4) ||
(b == 0xc5) ||
(b == 0xc6));
}
bool msgpck_array_next(Stream * s) {
int b = s->peek();
return (b != -1) && (((b >> 4) == 9) ||
(b == 0xdc) ||
(b == 0xdd));
}
bool msgpck_map_next(Stream * s) {
int b = s->peek();
return (b != -1) && (((b >> 4) == 8) ||
(b == 0xde) ||
(b == 0xdf));
}
bool msgpck_read_nil(Stream * s) {
uint8_t rfb;
return ((s->readBytes(&rfb,1) == 1) && (rfb == 0xc0));
}
bool msgpck_read_bool(Stream * s, bool *b) {
uint8_t rfb;
uint8_t dmp = s->readBytes(&rfb,1);
*b = (rfb == 0xc3);
return ((dmp == 1) && ((rfb == 0xc3) || (rfb == 0xc2)));
}
bool msgpck_read_integer(Stream * s, byte *b, uint8_t max_size) {
byte fb;
uint8_t read_size;
bool neg = false;
if (s->readBytes(&fb,1) != 1)
return false;
if(fb < 128) {
*b = fb;
read_size = 0;
return true;
} else if (fb >= 224) {
*b = fb;
read_size = 0;
uint8_t i;
for(i = max_size-1; i >= 1; i--) {
b[i] = 0xff;
}
return true;
} else if (fb == 0xcc) {
read_size = 1;
} else if (fb == 0xcd) {
read_size = 2;
} else if (fb == 0xce) {
read_size = 4;
} else if (fb == 0xcf) {
read_size = 8;
} else if (fb == 0xd0) {
read_size = 1;
neg = true;
} else if (fb == 0xd1) {
read_size = 2;
neg = true;
} else if (fb == 0xd2) {
read_size = 4;
neg = true;
} else if (fb == 0xd3) {
read_size = 8;
neg = true;
} else {
return false;
}
if(read_size > max_size)
return false;
bool res = true;
int8_t i;
for(i = read_size-1; i >= 0; i--) {
res &= s->readBytes(&b[i],1);
}
if(neg && ((b[read_size-1] >> 7) == 1)) {
for(i = max_size-1; i >= read_size; i--) {
b[i] = 0xff;
}
} else {
for(i = max_size-1; i >= read_size; i--) {
b[i] = 0x00;
}
}
return res;
}
bool msgpck_read_float(Stream * s, float *f) {
byte fb;
uint8_t read_size;
bool b = true;
if (s->readBytes(&fb,1) != 1)
return false;
if(fb == 0xca) {
read_size = 4;
union float_to_byte {
float f;
byte b[4];
} b2f;
b = s->readBytes(&(b2f.b[3]),1) == 1;
b = s->readBytes(&(b2f.b[2]),1) == 1;
b = s->readBytes(&(b2f.b[1]),1) == 1;
b = s->readBytes(&(b2f.b[0]),1) == 1;
*f = b2f.f;
return b;
} else if (fb == 0xcb) {
uint64_t dmp;
uint8_t * p = (uint8_t *) &dmp;
read_size = 8;
s->readBytes(p,read_size);
} else {
return false;
}
return false;
}
bool msgpck_read_string_length(Stream * s, uint32_t *str_size) {
*str_size = 0;
uint8_t fb;
bool b = true;
uint32_t read_size = 0;
uint8_t * p = (uint8_t *) &read_size;
if(s->readBytes(&fb,1) != 1) {
return false;
}
if((fb >> 5) == 5) {
read_size = fb & 0x1f;
} else if(fb == 0xd9) {
b &= s->readBytes(&p[0],1) == 1;
} else if(fb == 0xda) {
b &= s->readBytes(&p[1],1) == 1;
b &= s->readBytes(&p[0],1) == 1;
} else if(fb == 0xdb) {
b &= s->readBytes(&p[3],1) == 1;
b &= s->readBytes(&p[2],1) == 1;
b &= s->readBytes(&p[1],1) == 1;
b &= s->readBytes(&p[0],1) == 1;
} else {
return false;
}
if (!b) {
read_size = 0;
}
*str_size = read_size;
return b;
}
bool msgpck_read_buffer(Stream * s, uint8_t * buf, uint32_t size) {
size_t count = s->readBytes(buf, size);
return count == size;
}
bool msgpck_read_buffer(Stream * s, Print * p, uint32_t size) {
size_t bufSize = 15;
char buf[bufSize];
size_t length = 0;
size_t count = 0;
do
{
count = size - length;
if (count > bufSize)
{
count = bufSize;
}
count = s->readBytes(buf, count);
if (count > 0) {
p->write(buf, count);
length += count;
}
} while (size > length && count > 0);
return length == size;
}
bool msgpck_read_buffer_human(Stream * s, Print * p, uint32_t size) {
size_t bufSize = 15;
char buf[bufSize];
size_t length = 0;
size_t count = 0;
do
{
count = size - length;
if (count > bufSize)
{
count = bufSize;
}
count = s->readBytes(buf, count);
if (count > 0) {
for (size_t i = 0; i < count; i++) {
p->write(" 0x");
p->print(buf[i], HEX);
}
length += count;
}
} while (size > length && count > 0);
return length == size;
}
bool msgpck_read_string(Stream * s, Print * p) {
uint32_t str_size;
bool b = msgpck_read_string_length(s, &str_size);
if (!b) {
return false;
}
return msgpck_read_buffer(s, p, str_size);
}
bool msgpck_read_string(Stream * s, char * str, uint32_t max_size, uint32_t *str_size) {
bool b = msgpck_read_string_length(s, str_size);
if (!b || *str_size >= max_size) {
return false;
}
b = msgpck_read_buffer(s, (uint8_t*)str, *str_size);
if (!b) {
*str = 0;
} else {
str[*str_size] = 0;
}
return b;
}
bool msgpck_read_string(Stream * s, char * str, uint32_t max_size) {
uint32_t read_size;
return msgpck_read_string(s, str, max_size, &read_size);
}
bool msgpck_read_bin_length(Stream * s, uint32_t *bin_size) {
uint8_t fb;
bool b = true;
uint32_t read_size = 0;
uint8_t * p = (uint8_t *) &read_size;
if(s->readBytes(&fb,1) != 1) {
return false;
}
if(fb == 0xc4) {
b &= s->readBytes(&p[0],1) == 1;
} else if(fb == 0xc5) {
b &= s->readBytes(&p[1],1) == 1;
b &= s->readBytes(&p[0],1) == 1;
} else if(fb == 0xc6) {
b &= s->readBytes(&p[3],1) == 1;
b &= s->readBytes(&p[2],1) == 1;
b &= s->readBytes(&p[1],1) == 1;
b &= s->readBytes(&p[0],1) == 1;
} else {
return false;
}
if (!b) {
*bin_size = read_size;
}
return b;
}
bool msgpck_read_bin(Stream * s, byte * bin, uint32_t max_size, uint32_t *bin_size) {
bool b = msgpck_read_bin_length(s, bin_size);
if (!b || *bin_size > max_size) {
return false;
}
return msgpck_read_buffer(s, bin, *bin_size);
}
bool msgpck_read_bin(Stream * s, Print * p) {
uint32_t bin_size;
bool b = msgpck_read_bin_length(s, &bin_size);
if (!b) {
return false;
}
return msgpck_read_buffer(s, p, bin_size);
}
bool msgpck_read_bin_human(Stream * s, Print * p) {
uint32_t bin_size;
bool b = msgpck_read_bin_length(s, &bin_size);
if (!b) {
return false;
}
return msgpck_read_buffer_human(s, p, bin_size);
}
bool msgpck_read_bin(Stream * s, byte * bin, uint32_t max_size) {
uint32_t read_size;
return msgpck_read_bin(s, bin, max_size, &read_size);
}
bool msgpck_read_array_size(Stream * s, uint32_t * array_size) {
uint8_t fb;
bool b = true;
uint8_t * p = (uint8_t *) array_size;
if(s->readBytes(&fb,1) == 1) {
if((fb >> 4) == 0x09) {
*array_size = fb & 0x0f;
} else if(fb == 0xdc) {
*array_size = 0;
b &= s->readBytes(&p[1],1) == 1;
b &= s->readBytes(&p[0],1) == 1;
return b;
} else if(fb == 0xdd) {
*array_size = 0;
b &= s->readBytes(&p[3],1) == 1;
b &= s->readBytes(&p[2],1) == 1;
b &= s->readBytes(&p[1],1) == 1;
b &= s->readBytes(&p[0],1) == 1;
} else {
return false;
}
return b;
}
return false;
}
bool msgpck_read_map_size(Stream * s, uint32_t * map_size) {
uint8_t fb;
bool b = true;
uint8_t * p = (uint8_t *) map_size;
if(s->readBytes(&fb,1) == 1) {
if((fb >> 4) == 0x08) {
*map_size = fb & 0x0f;
} else if(fb == 0xde) {
*map_size = 0;
b &= s->readBytes(&p[1],1) == 1;
b &= s->readBytes(&p[0],1) == 1;
} else if(fb == 0xdf) {
*map_size = 0;
b &= s->readBytes(&p[3],1) == 1;
b &= s->readBytes(&p[2],1) == 1;
b &= s->readBytes(&p[1],1) == 1;
b &= s->readBytes(&p[0],1) == 1;
} else {
return false;
}
return b;
}
return false;
}
void msgpck_write_nil(Print * s) {
s->write(0xc0);
}
void msgpck_write_bool(Print * s, bool b) {
b ? s->write(0xc3) : s->write(0xc2);
}
void msgpck_write_integer(Print * s, uint8_t u) {
if(u < 128) {
s->write(u);
} else {
s->write(0xcc);
s->write(u);
}
}
void msgpck_write_integer(Print * s, uint16_t u) {
if(u < 256) {
msgpck_write_integer(s, (uint8_t) u);
} else {
s->write(0xcd);
s->write(u >> 8);
s->write(u & 0xff);
}
}
void msgpck_write_integer(Print * s, uint32_t u) {
if(u < 65536) {
msgpck_write_integer(s, (uint16_t) u);
} else {
s->write(0xce);
s->write(u >> 24);
s->write(u >> 16);
s->write(u >> 8);
s->write(u & 0xff);
}
}
void msgpck_write_integer(Print * s, uint64_t u) {
if(u < 4294967296) {
msgpck_write_integer(s, (uint32_t) u);
} else {
s->write(0xcd);
s->write((uint8_t)(u >> 56));
s->write((uint8_t)(u >> 48));
s->write((uint8_t)(u >> 40));
s->write((uint8_t)(u >> 32));
s->write((uint8_t)(u >> 24));
s->write((uint8_t)(u >> 16));
s->write((uint8_t)(u >> 8));
s->write((uint8_t)(u & 0xff));
}
}
void msgpck_write_integer(Print * s, int8_t i) {
if((i < -32) || (i > 127)) {
s->write(0xd0);
s->write(i);
} else {
s->write(i);
}
}
void msgpck_write_integer(Print * s, int16_t i) {
if(i > 0) {
msgpck_write_integer(s, (uint16_t) i);
} else if(i < -127) {
s->write(0xd1);
s->write(i >> 8);
s->write(i & 0xff);
} else {
msgpck_write_integer(s, (int8_t) i);
}
}
void msgpck_write_integer(Print * s, int32_t i) {
if(i > 0) {
msgpck_write_integer(s, (uint32_t) i);
} else if(i < -32768) {
s->write(0xd2);
s->write((i >> 24)& 0xff);
s->write((i >> 16)& 0xff);
s->write((i >> 8)& 0xff);
s->write(i & 0xff);
} else {
msgpck_write_integer(s, (int16_t) i);
}
}
void msgpck_write_integer(Print * s, int64_t i) {
if(i > 0) {
msgpck_write_integer(s, (uint64_t) i);
} else if(i < -2147483647) {
s->write(0xd3);
s->write((uint8_t)(i >> 56));
s->write((uint8_t)(i >> 48));
s->write((uint8_t)(i >> 40));
s->write((uint8_t)(i >> 32));
s->write((uint8_t)(i >> 24));
s->write((uint8_t)(i >> 16));
s->write((uint8_t)(i >> 8));
s->write((uint8_t)(i & 0xff));
} else {
msgpck_write_integer(s, (int32_t) i);
}
}
void msgpck_write_float(Print *s, float f) {
union float_to_byte {
float f;
byte b[4];
} f2b;
f2b.f = f;
s->write(0xca);
s->write(f2b.b[3]);
s->write(f2b.b[2]);
s->write(f2b.b[1]);
s->write(f2b.b[0]);
}
void msgpck_write_string(Print * s, char * str, uint32_t str_size) {
if(str_size > 65535) {
s->write(0xdb);
s->write((str_size >> 24) & 0xff);
s->write((str_size >> 16) & 0xff);
s->write((str_size >> 8) & 0xff);
s->write(str_size & 0xff);
} else if(str_size > 255) {
s->write(0xda);
s->write(str_size >> 8);
s->write(str_size & 0xff);
} else if (str_size > 31) {
s->write(0xd9);
s->write(str_size & 0xff);
} else {
s->write(0xa0 + str_size);
}
uint32_t i;
for(i=0;i<str_size;i++){
s->write(str[i]);
}
}
void msgpck_write_string(Print * s, String str) {
char buf[str.length()+1];
str.toCharArray(buf, str.length()+1);
msgpck_write_string(s, buf, str.length());
}
void msgpck_write_bin(Print * s, byte * b, uint32_t bin_size) {
if(bin_size > 65535) {
s->write(0xc6);
s->write((bin_size >> 24) & 0xff);
s->write((bin_size >> 16) & 0xff);
s->write((bin_size >> 8) & 0xff);
s->write(bin_size & 0xff);
} else if(bin_size > 255) {
s->write(0xc5);
s->write((bin_size >> 8) & 0xff);
s->write(bin_size & 0xff);
} else {
s->write(0xc4);
s->write(bin_size & 0xff);
}
uint32_t i;
for(i=0;i<bin_size;i++){
s->write(b[i]);
}
}
void msgpck_write_array_header(Print * s, uint32_t ar_size) {
if(ar_size > 65535) {
s->write(0xdd);
s->write(ar_size >> 24);
s->write(ar_size >> 16);
s->write(ar_size >> 8);
s->write(ar_size & 0xff);
} else if (ar_size > 15) {
s->write(0xdc);
s->write(ar_size >> 8);
s->write(ar_size & 0xff);
} else {
s->write(0x90 + ar_size);
}
}
void msgpck_write_map_header(Print * s, uint32_t map_size) {
if(map_size > 65535) {
s->write(0xde);
s->write(map_size >> 24);
s->write(map_size >> 16);
s->write(map_size >> 8);
s->write(map_size & 0xff);
} else if (map_size > 15) {
s->write(0xdf);
s->write(map_size >> 8);
s->write(map_size & 0xff);
} else {
s->write(0x80 + map_size);
}
}
void msgpck_to_json(Print * output, Stream * input) {
uint8_t i;
if(msgpck_map_next(input)) {
uint32_t map_size;
msgpck_read_map_size(input, &map_size);
output->print("{");
for(i = 0; i < map_size; i++) {
if(i != 0)
output->print(", ");
output->print("\"");
msgpck_read_string(input, output);
output->print("\": ");
msgpck_to_json(output, input);
}
output->print("}");
} else if(msgpck_array_next(input)) {
uint32_t array_size;
msgpck_read_array_size(input, &array_size);
output->print("[");
for(i = 0; i < array_size; i++) {
if(i != 0)
output->print(", ");
msgpck_to_json(output, input);
}
output->print("]");
} else if (msgpck_nil_next(input)) {
msgpck_read_nil(input);
output->print("nil");
} else if(msgpck_bool_next(input)) {
bool b;
msgpck_read_bool(input, &b);
if(b) {
output->print("true");
} else {
output->print("false");
}
} else if(msgpck_integer_next(input)) {
if(msgpck_signed_next(input)) {
int32_t i = 0;
msgpck_read_integer(input, (uint8_t *) &i, 4);
output->print(i);
} else {
uint32_t u = 0;
msgpck_read_integer(input, (uint8_t *) &u, 4);
output->print(u);
}
} else if(msgpck_float_next(input)) {
float f;
msgpck_read_float(input, &f);
output->print(f);
} else if(msgpck_string_next(input)) {
output->print("\"");
msgpck_read_string(input, output);
output->print("\"");
} else if(msgpck_bin_next(input)) {
output->print("'");
msgpck_read_bin_human(input, output);
output->print("'");
}
}