forked from lorabasics/basicstation
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuj.c
More file actions
1361 lines (1245 loc) · 37 KB
/
uj.c
File metadata and controls
1361 lines (1245 loc) · 37 KB
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
/*
* --- Revised 3-Clause BSD License ---
* Copyright Semtech Corporation 2022. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * 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.
* * Neither the name of the Semtech corporation nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 SEMTECH CORPORATION. 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.
*/
#include <stdarg.h>
#include <stdio.h>
#include "uj.h"
#include "xq.h" // %J - txjob only
#include "kwcrc.h"
static int nextChar (ujdec_t* dec) {
if( dec->read_pos >= dec->json_end ) {
dec->read_pos++;
return 0;
}
return *dec->read_pos++;
}
static void backChar (ujdec_t* dec) {
dec->read_pos -= 1;
}
static int skipWsp (ujdec_t* dec) {
while(1) {
int c = nextChar(dec);
switch( c ) {
case '\t':
case '\r':
case '\n':
case ' ':
break;
case '/': {
if( nextChar(dec) != '*' )
uj_error(dec, "Bad start of comment");
int d = 0;
while(1) {
c = nextChar(dec);
if( c == 0 ) { // or limit to a single line: || c == '\n'
uj_error(dec, "Unterminated /*.. comment");
// NOT REACHED
}
if( d == '*' && c == '/' )
break;
d = c;
}
break;
}
default:
return c;
}
}
}
static void nextLit (ujdec_t* dec, const char* s) {
while( nextChar(dec) == s[0] ) {
if( *++s == 0 )
return;
}
uj_error(dec, "Expecting literal (null,true,false)");
// NOT REACHED
}
static void parseString (ujdec_t* dec) {
ujcrc_t crc = 0;
char* wp = (dec->mode & UJ_MODE_SKIP) ? NULL : dec->read_pos;
dec->str.beg = dec->read_pos;
assert(dec->read_pos[-1] == '"');
while(1) {
int c = nextChar(dec);
switch( c ) {
case 0: {
uj_error(dec, "Malformed string - no closing quote");
// NOT REACHED
}
case '"': {
// Make it a C string - assumes no \0 in the middle
if( wp ) *wp = 0;
dec->str.crc = UJ_FINISH_CRC(crc);
dec->str.len = wp - dec->str.beg;
return;
}
case '\\': {
switch( c = nextChar(dec) ) {
case '"':
case '\\':
case '/': break;
case 'b': c = '\b'; break;
case 'f': c = '\f'; break;
case 'n': c = '\n'; break;
case 'r': c = '\r'; break;
case 't': c = '\t'; break;
case 'u': {
c = ((rt_hexDigit(nextChar(dec)) << 12) |
(rt_hexDigit(nextChar(dec)) << 8) |
(rt_hexDigit(nextChar(dec)) << 4) |
(rt_hexDigit(nextChar(dec)) ) );
if( c < 0 ) {
uj_error(dec, "Malformed \\u escape sequence");
// NOT REACHED
}
if( c >= (1<<7) ) {
char cu[2];
int ci;
if( c < (1<<11) ) { // 2^7 <= c < 2^11 ==> 5+6 bits
ci = 1;
cu[1] = 0xC0 | (c>>6); // encode 5 bits
} else { // 2^11 <= c < 2^16 ==> 4+6+6 bits
ci = 0;
cu[0] = 0xE0 | (c>>12); // encode 4 bits
cu[1] = 0x80 | ((c>>6)&0x3F); // 6 bits
}
for(; ci < 2; ci++ ) {
if( wp ) *wp++ = cu[ci];
crc = UJ_UPDATE_CRC(crc,cu[ci]);
}
c = 0x80|(c&0x3F); // encode 6 bits
}
break;
}
default:
uj_error(dec, "Illegally escaped character");
// NOT REACHED
}
}
}
if( wp ) *wp++ = c;
crc = UJ_UPDATE_CRC(crc,c);
}
}
static int decDigits (ujdec_t* dec, uL_t* pv, int* pn) {
uL_t v = 0;
int c, n = 0;
while( (c = nextChar(dec)) >= '0' && c <= '9' ) {
v = v*10 + c - '0';
n++;
}
if( n == 0 ) {
uj_error(dec, "Expecting some decimal digits");
// NOT REACHED
}
*pn = n;
*pv = v;
return c;
}
static void parseNumber (ujdec_t* dec, int signum) {
uL_t num;
int dummy;
uL_t frac = 0;
int nFrac = 0;
sL_t exp;
int expSign = 0; // no exponent
int c;
c = decDigits(dec, &num, &dummy);
if( c=='.' ) {
c = decDigits(dec, &frac, &nFrac);
}
if( c=='e' || c=='E' ) {
expSign = 1;
c = nextChar(dec);
/**/ if( c=='-' ) expSign = -1;
else if( c!='+' ) backChar(dec);
decDigits(dec, (uL_t*)&exp, &dummy);
}
backChar(dec);
if( nFrac == 0 && expSign == 0 ) {
dec->type = signum < 0 ? UJ_SNUM: UJ_UNUM;
dec->snum = signum * num;
return;
}
double f = frac;
while( --nFrac >= 0 )
f /= 10;
f += (double)num;
if( expSign != 0 ) {
while( --exp >= 0 )
f = expSign > 0 ? f*10 : f/10;
}
dec->fnum = signum * f;
dec->type = UJ_FNUM;
}
static void do_enter (ujdec_t* dec, int what, char brace, const char* err) {
if( skipWsp(dec) != brace ) {
uj_error(dec, err);
// NOT REACHED
}
if( dec->nest_level == UJ_MAX_NEST )
uj_error(dec, "JSON nested too deeply");
int nl = dec->nest_level;
if( nl >= 0 ) {
if( (dec->nest_type & 1) == UJ_N_OBJ ) {
// Inside an object - remember the offset to the field key
dec->nest_stack[nl] = dec->field.name - dec->json_beg;
} else {
// Inside an array - remember the array slot
assert(dec->index >= 0);
dec->nest_stack[nl] = dec->index;
}
dec->nest_type <<= 1;
}
dec->nest_type |= what;
dec->nest_level = nl + 1;
}
static void do_exit (ujdec_t* dec, int what, char brace, const char* err) {
if( dec->nest_level < 0 || (dec->nest_type & 1) != what ) {
uj_error(dec, "Internal parser error - do_exit");
// NOT REACHED
}
if( skipWsp(dec) != brace ) {
uj_error(dec, err);
// NOT REACHED
}
dec->nest_level -= 1;
dec->nest_type >>= 1;
dec->type = UJ_UNDEF;
if( dec->nest_level < 0 ) {
dec->field.name = NULL;
dec->field.crc = 0;
dec->index = -1;
}
else if( (dec->nest_type & 1) == UJ_N_OBJ ) {
dec->field.name = &dec->json_beg[dec->nest_stack[dec->nest_level]];
dec->field.crc = 0;
} else {
dec->index = dec->nest_stack[dec->nest_level];
}
}
void uj_enterObject (ujdec_t* dec) {
do_enter(dec, UJ_N_OBJ, '{', "Expecting an object");
dec->field.name = NULL;
dec->field.crc = 0;
}
void uj_enterArray (ujdec_t* dec) {
do_enter(dec, UJ_N_ARY, '[', "Expecting an array");
dec->index = -1;
}
void uj_exitObject (ujdec_t* dec) {
do_exit(dec, UJ_N_OBJ, '}', "Expecting a closing }");
}
void uj_exitArray (ujdec_t* dec) {
do_exit(dec, UJ_N_ARY, ']', "Expecting a closing ]");
}
ujcrc_t uj_nextField (ujdec_t* dec) {
dec->type = UJ_UNDEF;
int c = skipWsp(dec);
if( c == '}' ) {
backChar(dec);
return 0; // no more object fields
}
if( dec->field.name != NULL ) { // very first field?
if( c != ',' ) {
uj_error(dec, "Expecting a comma");
// NOT REACHED
}
c = skipWsp(dec);
}
if( c != '"' ) {
uj_error(dec, "Expecting a field");
// NOT REACHED
}
parseString(dec);
dec->field.name = dec->str.beg;
dec->field.crc = dec->str.crc;
if( skipWsp(dec) != ':' ) {
uj_error(dec, "Expecting a colon");
// NOT REACHED
}
return dec->field.crc;
}
int uj_nextSlot (ujdec_t* dec) {
dec->type = UJ_UNDEF;
int c = skipWsp(dec);
if( c == ']' ) {
backChar(dec);
return -1; // no more array slots
}
if( dec->index >= 0 ) {
// any slot after first
if( c != ',' ) {
uj_error(dec, "Expecting a comma");
// NOT REACHED
}
} else {
// very first slot?
backChar(dec);
}
dec->index += 1;
return dec->index;
}
int uj_indexedField (ujdec_t* dec, str_t prefix) {
if( dec->field.name == NULL || dec->field.crc == 0 )
return -1;
int n = strlen(prefix);
if( strncmp(dec->field.name, prefix, n) != 0 )
return -1;
str_t s, p;
s = p = dec->field.name + n;
sL_t idx = rt_readDec(&s);
if( s==p || s[0] ) return -1;
return idx;
}
sL_t uj_intRange (ujdec_t* dec, sL_t minval, sL_t maxval) {
sL_t v = uj_int(dec);
if( v < minval || v > maxval )
uj_error(dec, "Field value not in range [%ld..%ld]: %ld", minval, maxval, v);
return v;
}
sL_t uj_intRangeOr (ujdec_t* dec, sL_t minval, sL_t maxval, sL_t orval) {
sL_t v = uj_int(dec);
if( v != orval && (v < minval || v > maxval) )
uj_error(dec, "Field value not %ld or in range [%ld..%ld]: %ld", orval, minval, maxval, v);
return v;
}
void uj_error (ujdec_t* dec, const char* fmt, ...) {
ujbuf_t b;
if( log_special(MOD_JSN|ERROR, &b) ) {
int nl = dec->nest_level;
if( nl >= 0 ) {
xprintf(&b, "@");
int li = -1;
while( ++li <= nl ) {
int what = (dec->nest_type >> (nl-li)) & 1;
ujoff_t off = dec->nest_stack[li];
if( what == UJ_N_OBJ ) {
char* f = li==nl ? dec->field.name : (char*)&dec->json_beg[off];
xprintf(&b, ".%.20s", f);
} else {
xprintf(&b, "[%d]", li==nl ? dec->index : off);
}
}
}
xprintf(&b, ": ");
va_list ap;
va_start(ap, fmt);
vxprintf(&b, fmt, ap);
va_end(ap);
log_specialFlush(b.pos);
}
longjmp(dec->on_err, 1);
}
void uj_assertEOF (ujdec_t* dec) {
if( skipWsp(dec) != 0 ) {
uj_error(dec, "Expecting EOF but found garbage: %.20s", dec->read_pos-1);
// NOT REACHED
}
}
ujtype_t uj_nextValue (ujdec_t* dec) {
if( dec->type != UJ_UNDEF )
return dec->type;
int c = skipWsp(dec);
dec->val = dec->read_pos - 1;
switch( c ) {
case 0: {
uj_error(dec, "Unexpected EOF");
// NOT REACHED
}
case '"': {
dec->type = UJ_STRING;
parseString(dec);
break;
}
case '-': {
parseNumber(dec, -1);
break;
}
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9': {
backChar(dec);
parseNumber(dec, 1);
break;
}
case 't': {
nextLit(dec,"true"+1);
dec->type = UJ_BOOL;
dec->snum = 1;
break;
}
case 'f': {
nextLit(dec,"false"+1);
dec->type = UJ_BOOL;
dec->snum = 0;
break;
}
case 'n': {
nextLit(dec,"null"+1);
dec->type = UJ_NULL;
dec->snum = 0;
break;
}
case '{':
case '[': {
dec->type = c=='{' ? UJ_OBJECT : UJ_ARRAY;
backChar(dec); // enter expects to find opening brace
break;
}
default: {
uj_error(dec, "Syntax error");
// NOT REACHED
}
}
return dec->type;
}
static void skipValue (ujdec_t* dec) {
uj_nextValue(dec);
if( dec->type == UJ_OBJECT ) {
uj_enterObject(dec);
while( uj_nextField(dec) )
skipValue(dec);
uj_exitObject(dec);
}
else if( dec->type == UJ_ARRAY ) {
uj_enterArray(dec);
while( uj_nextSlot(dec) >= 0 )
skipValue(dec);
uj_exitArray(dec);
}
}
ujbuf_t uj_skipValue (ujdec_t* dec) {
// Skip leading WSP - this is for code that uses skipped value as text (e.g. s2conf)
skipWsp(dec);
backChar(dec);
ujbuf_t buf = { .buf = dec->read_pos, .bufsize = 0, .pos = 0 };
dec->mode |= UJ_MODE_SKIP;
skipValue(dec);
buf.bufsize = dec->read_pos - buf.buf;
dec->mode &= ~UJ_MODE_SKIP;
return buf;
}
void uj_iniDecoder (ujdec_t* dec, char* json, ujoff_t jsonlen) {
memset(dec, 0, sizeof(*dec));
dec->json_beg = dec->read_pos = json;
dec->json_end = json+jsonlen;
dec->nest_level = -1;
}
int uj_null (ujdec_t* dec) {
ujtype_t t = uj_nextValue(dec);
return t == UJ_NULL;
}
int uj_bool (ujdec_t* dec) {
ujtype_t t = uj_nextValue(dec);
if( t != UJ_BOOL )
uj_error(dec,"Expecting a bool value");
return dec->unum;
}
sL_t uj_int (ujdec_t* dec) {
ujtype_t t = uj_nextValue(dec);
if( t != UJ_SNUM && t != UJ_UNUM )
uj_error(dec,"Expecting an integer value");
return dec->snum;
}
uL_t uj_uint (ujdec_t* dec) {
ujtype_t t = uj_nextValue(dec);
if( t != UJ_UNUM )
uj_error(dec,"Expecting a positive integer value");
return dec->unum;
}
double uj_num (ujdec_t* dec) {
ujtype_t t = uj_nextValue(dec);
if( t == UJ_SNUM )
return (double)dec->snum;
if( t == UJ_UNUM )
return (double)dec->unum;
if( t != UJ_FNUM )
uj_error(dec,"Expecting a number");
return dec->fnum;
}
char* uj_str (ujdec_t* dec) {
ujtype_t t = uj_nextValue(dec);
if( t != UJ_STRING )
uj_error(dec,"Expecting a string value");
return dec->str.beg;
}
ujcrc_t uj_keyword (ujdec_t* dec) {
ujtype_t t = uj_nextValue(dec);
if( t != UJ_STRING )
uj_error(dec,"Expecting a string value");
return dec->str.crc;
}
int uj_hexstr (ujdec_t* dec, u1_t* buf, int bufsiz) {
ujtype_t t = uj_nextValue(dec);
if( t != UJ_STRING )
uj_error(dec,"Expecting a string value with hex digits");
char* s = dec->str.beg;
int len = dec->str.len;
if( (len & 1) != 0 )
uj_error(dec,"Hex string has odd number of characters");
if( len/2 > bufsiz )
uj_error(dec,"Hex string too long: %d bytes, buffer is %d", len/2, bufsiz);
for( int i=0; i<len; i+=2 ) {
int b = (rt_hexDigit(s[i])<<4) | rt_hexDigit(s[i+1]);
if( b < 0 )
uj_error(dec,"Hex string contains illegal characters: %c%c", s[i], s[i+1]);
buf[i/2] = b;
}
return len/2;
}
uL_t uj_eui (ujdec_t* dec) {
ujtype_t t = uj_nextValue(dec);
if( t == UJ_SNUM || t == UJ_UNUM )
return dec->unum;
if( t != UJ_STRING )
uj_error(dec,"Expecting a string value with an EUI");
char* s = dec->str.beg;
int len = dec->str.len;
uL_t eui = 0;
int i;
for( i=0; i<len; i+=2 ) {
int b = (rt_hexDigit(s[i])<<4) | rt_hexDigit(s[i+1]);
if( b < 0 )
uj_error(dec,"EUI contains illegal hex characters: %c%c", s[i], s[i+1]);
i += (s[i+2] == '-'); // - is optional
eui = (eui << 8) | b; // fewer/more hex bytes would be ok too
}
return eui;
}
//
// map=0
// back = 0
// i = 0
// for c in 'msgtype':
// i += 1
// b = c.encode('ascii')[0]
// print('1F=%02X /3=%02X' % (b&0x1F, (b&0x1F)//3))
// map |= 1<<(b & 0x1F)
// back |= i << (4*(b & 0xF))
// print('0x1F map: 0x%X' % map);
// print('backtrack 16*4: 0x%X' % back);
//
// This function will never fail and use dec->on_err
// It returns 0 if no msgtype is being found
//
ujcrc_t uj_msgtype(ujdec_t* dec) {
char* s = dec->json_beg-7;
char* e = dec->json_end;
while( (s+=7) < e ) {
int c = s[0];
if( (c & 0xE0) != 0x60 || ((1 << (c & 0x1F)) & 0x21920A0) == 0 )
continue;
char* beg = s - ((0x10005030742006ULL >> ((c&0xF)<<2)) & 0xF);
if( beg < dec->json_beg || beg + 10 > e )
continue;
if( strncmp(beg,"\"msgtype\"", 9) != 0 )
continue;
dec->read_pos = beg+9;
if( skipWsp(dec) != ':' || skipWsp(dec) != '"' ) {
nomatch:
s = dec->read_pos-7;
dec->read_pos = dec->json_beg;
continue;
}
dec->str.beg = dec->read_pos;
ujcrc_t crc = 0;
while( (c = nextChar(dec)) != '"' ) {
if( c==0 || c=='\\' )
goto nomatch;
crc = UJ_UPDATE_CRC(crc, c);
}
dec->str.len = dec->read_pos - dec->str.beg - 1;
dec->str.crc = UJ_FINISH_CRC(crc);
dec->read_pos = dec->json_beg;
return dec->str.crc;
}
return 0;
}
// --------------------------------------------------------------------------------
//
// Encoder
//
// --------------------------------------------------------------------------------
#define snXp(b, fmt, ...) ((b)->pos += snprintf((b)->buf + (b)->pos, (b)->bufsize - (b)->pos, fmt, ##__VA_ARGS__))
static int lastChar (ujbuf_t* b) {
if( !b->pos )
return -1;
return b->buf[b->pos-1];
}
static void addChar (ujbuf_t* b, char c) {
if( b->pos < b->bufsize )
b->buf[b->pos++] = c;
}
static void addHex2 (ujbuf_t* b, int v) {
if( b->pos < b->bufsize )
b->buf[b->pos++] = "0123456789ABCDEF"[(v>>4)&0xF];
if( b->pos < b->bufsize )
b->buf[b->pos++] = "0123456789ABCDEF"[v&0xF];
}
// Add string - n<=0 add string until \0
// - n>0 at most n chars
void xputs (ujbuf_t* b, const char* s, int n) {
while( *s && n != 0 ) {
if( b->pos >= b->bufsize )
break;
b->buf[b->pos++] = *s++;
n--;
}
}
static const char* B64 = ("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/");
static void b64Encode (ujbuf_t* b, const u1_t* d, int len) {
int di=0, spos=b->pos;
while( di < len ) {
u4_t v = ((d[di] << 16) |
((di+1 < len ? d[di+1] : 0) << 8) |
((di+2 < len ? d[di+2] : 0) ) );
di += 3;
addChar(b, B64[(v >> 18) & 0x3f]);
addChar(b, B64[(v >> 12) & 0x3f]);
addChar(b, B64[(v >> 6) & 0x3f]);
addChar(b, B64[(v >> 0) & 0x3f]);
}
if( di > len ) {
b->pos = spos + len/3*4 + 4 - (di-len);
while( --di >= len )
addChar(b, '=');
}
}
static void anotherValue(ujbuf_t* b) {
int c = lastChar(b);
if( c == -1 || c == ',' || c== ':' || c == '[' || c == '{' )
return;
addChar(b, ',');
}
static void anotherString(ujbuf_t* b) {
if( b->pos >= 2 && b->buf[b->pos-1] == '\b' && b->buf[b->pos-2] == '"' ) {
b->pos -= 2;
return;
}
anotherValue(b);
addChar(b, '"');
}
// Ensure termination with \0
int xeos(ujbuf_t* b) {
if( b->pos < b->bufsize ) {
b->buf[b->pos] = 0;
return 1; // ok - no overflow
}
// Make sure it's always terminated
b->buf[b->bufsize-1] = 0;
return 0; // overflow
}
// Ensure last char is \n
int xeol(ujbuf_t* b) {
if( b->pos < b->bufsize ) {
if( b->pos > 0 && b->buf[b->pos-1] != '\n' )
b->buf[b->pos++] = '\n';
return 1; // ok - no overflow
}
// Make sure it's always terminated with c
int i = b->pos = b->bufsize;
b->buf[i-1] = '\n';
return 0; // overflow
}
void uj_mergeStr(ujbuf_t* b) {
// Next encoded string is merged with previous one
addChar(b, '\b');
}
void uj_encOpen(ujbuf_t* b, char brace) {
anotherValue(b);
addChar(b, brace);
}
void uj_encClose(ujbuf_t* b, char brace) {
addChar(b, brace);
}
void uj_encNull(ujbuf_t* b) {
anotherValue(b);
xputs(b, "null", -1);
}
void uj_encBool(ujbuf_t* b, int val) {
anotherValue(b);
xputs(b, val ? "true" : "false", -1);
}
void uj_encInt(ujbuf_t* b, sL_t val) {
anotherValue(b);
xprintf(b, "%ld", val);
}
void uj_encUint(ujbuf_t* b, uL_t val) {
anotherValue(b);
xprintf(b, "%lu", val);
}
void uj_encNum(ujbuf_t* b, double val) {
anotherValue(b);
int n = snprintf(b->buf + b->pos, b->bufsize - b->pos, "%g", val);
b->pos += n;
}
void uj_encTime(ujbuf_t* b, double val) {
anotherValue(b);
int n = snprintf(b->buf + b->pos, b->bufsize - b->pos, "%.6f", val);
b->pos += n;
}
void uj_encFTime(ujbuf_t* b, double val) {
anotherValue(b);
int n = snprintf(b->buf + b->pos, b->bufsize - b->pos, "%.9f", val);
b->pos += n;
}
void uj_encStr (ujbuf_t* b, const char* s) {
if( s == NULL ) {
uj_encNull(b);
return;
}
anotherString(b);
int c;
while( (c=*s++) && b->pos < b->bufsize ) {
switch(c) {
case '\\':
case '"': break;
case '\b': c = 'b'; break;
case '\f': c = 'f'; break;
case '\n': c = 'n'; break;
case '\r': c = 'r'; break;
case '\t': c = 't'; break;
default:
if( c >=0 && c < 0x20 ) {
addChar(b,'\\');
addChar(b,'u');
addHex2(b,0);
addHex2(b,c);
continue;
}
addChar(b, c);
continue;
}
addChar(b, '\\');
addChar(b, c);
}
addChar(b, '"');
}
void uj_encHex(ujbuf_t* b, const u1_t* d, int len) {
if( d == NULL ) {
uj_encNull(b);
return;
}
anotherString(b);
for( int i=0; i<len; i++ )
addHex2(b, d[i]);
addChar(b, '"');
}
static void encMac(ujbuf_t* b, uL_t mac) {
addHex2(b,mac>>40);
for( int i=40-8; i>=0; i-=8 ) {
addChar(b,':');
addHex2(b,mac>>i);
}
}
void uj_encMac(ujbuf_t* b, uL_t mac) {
anotherString(b);
encMac(b, mac);
addChar(b, '"');
}
static void encDate(ujbuf_t* b, uL_t tm) {
struct datetime dt = rt_datetime(tm);
snXp(b, "%04d-%02d-%02d ", dt.year, dt.month, dt.day);
snXp(b, "%02d:%02d:%02d", dt.hour, dt.minute, dt.second);
}
void uj_encDate(ujbuf_t* b, uL_t tm) {
anotherString(b);
encDate(b, tm);
addChar(b, '"');
}
static void encEui(ujbuf_t* b, uL_t eui, int nlsb) {
if( nlsb == 0 || nlsb >= 8 ) { // render all bytes
addHex2(b,eui>>56);
nlsb = 7;
}
for( int i=nlsb*8-8; i>=0; i-=8 ) {
addChar(b,'-');
addHex2(b,eui>>i);
}
}
void uj_encEui(ujbuf_t* b, uL_t eui) {
anotherString(b);
encEui(b, eui, 0);
addChar(b, '"');
}
static void encId6 (ujbuf_t* b, uL_t eui) {
u2_t g[4] = { eui>>48, eui>>32, eui>>16, eui };
if( !g[0] && !g[1] ) {
if( !g[2] )
xprintf(b, "::%x", g[3]);
else
xprintf(b, "::%x:%x", g[2], g[3]);
return;
}
if( !g[2] && !g[3] ) {
if( !g[1] )
xprintf(b, "%x::", g[0]);
else
xprintf(b, "%x:%x::", g[0], g[1]);
return;
}
if( !g[1] && !g[2] ) {
xprintf(b, "%x::%x", g[0], g[3]);
return;
}
xprintf(b, "%x:%x:%x:%x", g[0], g[1], g[2], g[3]);
}
void uj_encId6(ujbuf_t* b, uL_t eui) {
anotherString(b);
encId6(b, eui);
addChar(b, '"');
}
void uj_encKey (ujbuf_t* b, const char* key) {
uj_encStr(b, key);
addChar(b, ':');
}
static int encArg(ujbuf_t* b, int type, va_list* args) {
switch(type) {
case 'b': uj_encBool (b, va_arg(*args, int)); break;
case 'i': uj_encInt (b, va_arg(*args, int)); break;
case 'I': uj_encInt (b, va_arg(*args, sL_t)); break;
case 'u': uj_encUint (b, va_arg(*args, unsigned)); break;
case 'U': uj_encUint (b, va_arg(*args, uL_t)); break;
case 'D': uj_encDate (b, va_arg(*args, uL_t)); break;
case 'g': uj_encNum (b, va_arg(*args, double)); break;
case 'T': uj_encTime (b, va_arg(*args, double)); break;
case 'F': uj_encFTime(b, va_arg(*args, double)); break;
case 's': uj_encStr (b, va_arg(*args, char*)); break;
case 'E': uj_encEui (b, va_arg(*args, uL_t)); break;
case 'M': uj_encMac (b, va_arg(*args, uL_t)); break;
case '6': uj_encId6 (b, va_arg(*args, uL_t)); break;
case 'H': {
int dl = va_arg(*args, int);
const u1_t* d = va_arg(*args, const u1_t*);
uj_encHex(b, d, dl);
break;
}
default: return 0;
}
return 1;
}
void uj_encKV (ujbuf_t* b, const char* key, char type, ...) {
va_list ap;
uj_encKey(b, key);
va_start(ap, type);
encArg(b, type, &ap);
va_end(ap);
}
void uj_encKVn (ujbuf_t* b, ...) {
va_list ap;
va_start(ap, b);
do {
const char* key = va_arg(ap, const char*);
if( key == NULL )
break;
if( key[0] == '}' && key[1] == 0 ) {
uj_encClose(b, '}');
continue;
}
uj_encKey(b, key);
int type = va_arg(ap, int);
if( type == '{' ) {
uj_encOpen(b, '{');
continue;
}
if( type == '[' ) {
uj_encOpen(b, '[');
while(1) {
type = va_arg(ap, int);
if( type == ']' ) {
uj_encClose(b, ']');
break;
}
if( !encArg(b, type, &ap) )
goto stop;
}
continue;
}
if( !encArg(b, type, &ap) )
break;
} while(1);
stop:
va_end(ap);
}
// --------------------------------------------------------------------------------
//
// Special print stuff
//
// --------------------------------------------------------------------------------
#if defined(CFG_surrogate_snprintf_64bit)
// Minihub's snprintf cannot deal with 64bit integers - we provide a surrogate
// impl here which does not cover all formatting features (padding, truncation etc.)
// but which is good enough for now
int surrogate_snprintf_64bit(char* buf, int len, uL_t val, char fmt) {
if( val == 0 ) {
if( len > 0 )