-
Notifications
You must be signed in to change notification settings - Fork 28
/
util.c
1127 lines (903 loc) · 23.7 KB
/
util.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
#include "util.h"
#include "sys_time.h"
#include "uart.h"
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <limits.h>
#include <stdarg.h>
static flash_sector_buffer_use_t flash_sector_buffer_use = fsb_free;
static bool flash_sector_buffer_private = false;
static string_new(attr_flash_align, flash_sector_buffer, SPI_FLASH_SEC_SIZE);
roflash const unsigned int crc16_tab[256] =
{
0x0000, 0xc0c1, 0xc181, 0x0140, 0xc301, 0x03c0, 0x0280, 0xc241,
0xc601, 0x06c0, 0x0780, 0xc741, 0x0500, 0xc5c1, 0xc481, 0x0440,
0xcc01, 0x0cc0, 0x0d80, 0xcd41, 0x0f00, 0xcfc1, 0xce81, 0x0e40,
0x0a00, 0xcac1, 0xcb81, 0x0b40, 0xc901, 0x09c0, 0x0880, 0xc841,
0xd801, 0x18c0, 0x1980, 0xd941, 0x1b00, 0xdbc1, 0xda81, 0x1a40,
0x1e00, 0xdec1, 0xdf81, 0x1f40, 0xdd01, 0x1dc0, 0x1c80, 0xdc41,
0x1400, 0xd4c1, 0xd581, 0x1540, 0xd701, 0x17c0, 0x1680, 0xd641,
0xd201, 0x12c0, 0x1380, 0xd341, 0x1100, 0xd1c1, 0xd081, 0x1040,
0xf001, 0x30c0, 0x3180, 0xf141, 0x3300, 0xf3c1, 0xf281, 0x3240,
0x3600, 0xf6c1, 0xf781, 0x3740, 0xf501, 0x35c0, 0x3480, 0xf441,
0x3c00, 0xfcc1, 0xfd81, 0x3d40, 0xff01, 0x3fc0, 0x3e80, 0xfe41,
0xfa01, 0x3ac0, 0x3b80, 0xfb41, 0x3900, 0xf9c1, 0xf881, 0x3840,
0x2800, 0xe8c1, 0xe981, 0x2940, 0xeb01, 0x2bc0, 0x2a80, 0xea41,
0xee01, 0x2ec0, 0x2f80, 0xef41, 0x2d00, 0xedc1, 0xec81, 0x2c40,
0xe401, 0x24c0, 0x2580, 0xe541, 0x2700, 0xe7c1, 0xe681, 0x2640,
0x2200, 0xe2c1, 0xe381, 0x2340, 0xe101, 0x21c0, 0x2080, 0xe041,
0xa001, 0x60c0, 0x6180, 0xa141, 0x6300, 0xa3c1, 0xa281, 0x6240,
0x6600, 0xa6c1, 0xa781, 0x6740, 0xa501, 0x65c0, 0x6480, 0xa441,
0x6c00, 0xacc1, 0xad81, 0x6d40, 0xaf01, 0x6fc0, 0x6e80, 0xae41,
0xaa01, 0x6ac0, 0x6b80, 0xab41, 0x6900, 0xa9c1, 0xa881, 0x6840,
0x7800, 0xb8c1, 0xb981, 0x7940, 0xbb01, 0x7bc0, 0x7a80, 0xba41,
0xbe01, 0x7ec0, 0x7f80, 0xbf41, 0x7d00, 0xbdc1, 0xbc81, 0x7c40,
0xb401, 0x74c0, 0x7580, 0xb541, 0x7700, 0xb7c1, 0xb681, 0x7640,
0x7200, 0xb2c1, 0xb381, 0x7340, 0xb101, 0x71c0, 0x7080, 0xb041,
0x5000, 0x90c1, 0x9181, 0x5140, 0x9301, 0x53c0, 0x5280, 0x9241,
0x9601, 0x56c0, 0x5780, 0x9741, 0x5500, 0x95c1, 0x9481, 0x5440,
0x9c01, 0x5cc0, 0x5d80, 0x9d41, 0x5f00, 0x9fc1, 0x9e81, 0x5e40,
0x5a00, 0x9ac1, 0x9b81, 0x5b40, 0x9901, 0x59c0, 0x5880, 0x9841,
0x8801, 0x48c0, 0x4980, 0x8941, 0x4b00, 0x8bc1, 0x8a81, 0x4a40,
0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0, 0x4c80, 0x8c41,
0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641,
0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040,
};
unsigned int crc16(unsigned int length, const uint8_t *data)
{
uint16_t crc;
unsigned int current;
unsigned int index;
for(crc = 0, current = 0; current < length; current++)
{
index = (crc ^ data[current]) & 0x00ff;
crc = (crc >> 8) ^ crc16_tab[index];
}
return(crc);
}
char flash_dram_buffer[1024];
string_t flash_dram =
{
.size = sizeof(flash_dram_buffer),
.length = 0,
.buffer = flash_dram_buffer,
};
iram int strecpy(char *dst, const char *src, int size)
{
int length = strlen(src);
if(length >= size)
length = size - 1;
if(length < 0)
length = 0;
memcpy(dst, src, length);
dst[length] = '\0';
return(length);
}
iram unsigned int flash_to_dram(bool cstr, const void *src_flash_unaligned, char *dst_dram, size_t length)
{
const uint32_t *src_flash;
unsigned int src_flash_index;
unsigned int src_flash_sub_index;
unsigned int dst_dram_index;
src_flash = (const uint32_t *)((uint32_t)src_flash_unaligned & ~0b11);
src_flash_sub_index = (uint32_t)src_flash_unaligned & 0b11;
for(src_flash_index = 0, dst_dram_index = 0; dst_dram_index < length; dst_dram_index++)
{
dst_dram[dst_dram_index] = (src_flash[src_flash_index] >> (src_flash_sub_index << 3)) & 0xff;
if(cstr)
{
if(!dst_dram[dst_dram_index])
break;
if((dst_dram_index + 1) >= length)
{
dst_dram[dst_dram_index] = 0;
break;
}
}
if((++src_flash_sub_index & 0b11) == 0)
{
src_flash_sub_index = 0;
src_flash_index++;
}
}
return(dst_dram_index);
}
iram void string_format_cstr(string_t *dst, const char *fmt, ...)
{
va_list ap;
int rendered_length, buffer_remaining;
// no space left at all, do nothing
if((buffer_remaining = dst->size - dst->length - 1) <= 0)
return;
va_start(ap, fmt);
rendered_length = vsnprintf(dst->buffer + dst->length, dst->size - dst->length - 1, fmt, ap);
va_end(ap);
// some snprintf implementations can return -1 when output doesn't fit the output buffer
// some snprintf implementations can return the original buffer size when the output doesn't fit the output buffer
// this means we can't always rely on the return value, assume vsnprintf filled all of the buffer_remaining space in those cases
if((rendered_length < 0) || (rendered_length > buffer_remaining))
rendered_length = buffer_remaining;
dst->length += rendered_length;
dst->buffer[dst->length] = '\0';
}
iram void string_format_flash_ptr(string_t *dst, const char *fmt_flash, ...)
{
va_list ap;
int rendered_length, buffer_remaining;
flash_to_dram(true, fmt_flash, flash_dram_buffer, sizeof(flash_dram_buffer));
// no space left at all, do nothing
if((buffer_remaining = dst->size - dst->length - 1) <= 0)
return;
va_start(ap, fmt_flash);
rendered_length = vsnprintf(dst->buffer + dst->length, buffer_remaining, flash_dram_buffer, ap);
va_end(ap);
// some snprintf implementations can return -1 when output doesn't fit the output buffer
// some snprintf implementations can return the original buffer size when the output doesn't fit the output buffer
// this means we can't always rely on the return value, assume vsnprintf filled all of the buffer_remaining space in those cases
if((rendered_length < 0) || (rendered_length > buffer_remaining))
rendered_length = buffer_remaining;
dst->length += rendered_length;
dst->buffer[dst->length] = '\0';
}
bool attr_nonnull string_match_cstr_flash(const string_t *s1, const char *s2)
{
char s2_in_dram[64];
flash_to_dram(true, s2, s2_in_dram, sizeof(s2_in_dram));
return(string_match_cstr(s1, s2_in_dram));
}
int attr_pure string_sep(const string_t *src, int offset, int occurrence, char c)
{
for(; (offset < src->size) && (offset < src->length) && (occurrence > 0); offset++)
if(string_at(src, offset) == c)
occurrence--;
if((offset >= src->size) || (offset >= src->length))
offset = -1;
return(offset);
}
int attr_pure string_find(const string_t *src, int offset, char c)
{
for(; offset < src->length; offset++)
if(string_at(src, offset) == c)
return(offset);
return(-1);
}
void string_replace(string_t *dst, int offset, char c)
{
if((offset + 1) < dst->size)
{
dst->buffer[offset] = c;
if(offset > dst->length)
{
dst->length = offset;
dst->buffer[dst->length + 1] = '\0';
}
}
}
void string_splice(string_t *dst, int dst_offset, const string_t *src, int src_offset, int length)
{
if(dst_offset < 0)
dst_offset = dst->length;
if(src_offset < 0)
src_offset = 0;
if(length < 0)
length = src->length - src_offset;
if((src_offset + length) > src->length)
length = src->length - src_offset;
if((dst_offset + length) > dst->size)
length = dst->size - dst_offset;
memcpy(dst->buffer + dst_offset, src->buffer + src_offset, length);
string_setlength(dst, dst_offset + length);
}
int string_trim(string_t *dst, int amount)
{
if(amount < 0)
amount = dst->length;
if(amount > dst->size)
amount = dst->size;
if(amount > dst->length)
amount = dst->length;
dst->length -= amount;
return(dst->length);
}
bool string_trim_nl(string_t *dst)
{
bool trimmed = false;
if((dst->length > 0) && (dst->buffer[dst->length - 1] == '\n'))
{
trimmed = true;
dst->length--;
if((dst->length > 0) && (dst->buffer[dst->length - 1] == '\r'))
dst->length--;
}
else
{
if((dst->length > 0) && (dst->buffer[dst->length - 1] == '\r'))
{
trimmed = true;
dst->length--;
if((dst->length > 0) && (dst->buffer[dst->length - 1] == '\n'))
dst->length--;
}
}
return(trimmed);
}
attr_nonnull void string_word_to_bin(string_t *dst, unsigned int word, unsigned int bits)
{
int ix;
for(ix = bits - 1; ix >= 0; ix--)
if(word & (1 << ix))
string_append_char(dst, '1');
else
string_append_char(dst, '0');
}
void string_bin_to_hex(string_t *dst, const unsigned char *src, int length)
{
int offset;
uint8_t out;
for(offset = 0; offset < length; offset++)
{
out = (src[offset] & 0xf0) >> 4;
if(out > 9)
out = (out - 10) + 'a';
else
out = out + '0';
string_append_char(dst, out);
out = (src[offset] & 0x0f) >> 0;
if(out > 9)
out = (out - 10) + 'a';
else
out = out + '0';
string_append_char(dst, out);
}
}
void string_ip(string_t *dst, ip_addr_t addr)
{
ip_addr_to_bytes_t ip_addr_to_bytes;
ip_addr_to_bytes.ip_addr = addr;
string_format(dst, "%u.%u.%u.%u",
ip_addr_to_bytes.byte[0],
ip_addr_to_bytes.byte[1],
ip_addr_to_bytes.byte[2],
ip_addr_to_bytes.byte[3]);
}
bool string_to_mac(mac_addr_t *addr, const string_t *src)
{
unsigned int ix, octet[6];
if(sscanf(string_buffer(src), "%x:%x:%x:%x:%x:%x ",
&octet[0], &octet[1], &octet[2],
&octet[3], &octet[4], &octet[5]) != 6)
return(false);
for(ix = 0; ix < 6; ix++)
(*addr)[ix] = octet[ix];
return(true);
}
void mac_to_string(string_t *dst, const mac_addr_t addr)
{
int ix;
mac_addr_to_bytes_t mac_addr_to_bytes;
for(ix = 0; ix < 6; ix++)
mac_addr_to_bytes.mac_addr[ix] = addr[ix];
string_format(dst, "%02x:%02x:%02x:%02x:%02x:%02x",
mac_addr_to_bytes.byte[0],
mac_addr_to_bytes.byte[1],
mac_addr_to_bytes.byte[2],
mac_addr_to_bytes.byte[3],
mac_addr_to_bytes.byte[4],
mac_addr_to_bytes.byte[5]);
}
void _flash_buffer_request(flash_sector_buffer_use_t use, bool pvt, const char *description,
string_t **string, char **cstr, unsigned int *size)
{
if(string_size(&flash_sector_buffer) != SPI_FLASH_SEC_SIZE)
{
log("request flash buffer: ");
log_from_flash_0(description);
log(": sector buffer size wrong: %d: ", string_size(&flash_sector_buffer));
goto error;
}
if((flash_sector_buffer_use != fsb_free) && ((flash_sector_buffer_use != use) && flash_sector_buffer_private))
{
log("request flash buffer: ");
log_from_flash_0(description);
log(": in use by %u: ", flash_sector_buffer_use);
goto error;
}
flash_sector_buffer_use = use;
flash_sector_buffer_private = pvt;
if(string)
*string = &flash_sector_buffer;
if(cstr)
*cstr = string_buffer_nonconst(&flash_sector_buffer);
if(size)
*size = string_size(&flash_sector_buffer);
return;
error:
if(string)
*string = (string_t *)0;
if(cstr)
*cstr = (char *)0;
if(size)
*size = 0;
return;
}
void _flash_buffer_release(flash_sector_buffer_use_t use, const char *description)
{
if(flash_sector_buffer_use == fsb_free)
{
log("release flash buffer: double free: ");
log_from_flash_0(description);
log("\n");
}
if((flash_sector_buffer_use != use) && flash_sector_buffer_private)
{
log("release flash buffer: conflicting free: from %u to %u: ", flash_sector_buffer_use, use);
log_from_flash_0(description);
log("\n");
}
flash_sector_buffer_use = fsb_free;
flash_sector_buffer_private = false;
}
flash_sector_buffer_use_t flash_buffer_using(void)
{
return(flash_sector_buffer_use);
}
bool flash_buffer_using_1(flash_sector_buffer_use_t one)
{
if(flash_sector_buffer_use == one)
return(true);
return(false);
}
bool flash_buffer_using_2(flash_sector_buffer_use_t one, flash_sector_buffer_use_t two)
{
if(flash_sector_buffer_use == one)
return(true);
if(flash_sector_buffer_use == two)
return(true);
return(false);
}
bool flash_buffer_using_3(flash_sector_buffer_use_t one, flash_sector_buffer_use_t two, flash_sector_buffer_use_t three)
{
if(flash_sector_buffer_use == one)
return(true);
if(flash_sector_buffer_use == two)
return(true);
if(flash_sector_buffer_use == three)
return(true);
return(false);
}
unsigned int logbuffer_display_current = 0;
string_t logbuffer =
{
.size = 0x3fffeb2c - 0x3fffe000 - 16,
.length = 0,
.buffer = (char *)0x3fffe000,
};
int attr_used __errno;
void espconn_init(void);
void espconn_init(void)
{
}
void reset(void)
{
system_restart();
}
attr_const const char *yesno(bool value)
{
if(!value)
return("no");
return("yes");
}
attr_const const char *onoff(bool value)
{
if(!value)
return("off");
return("on");
}
void logbuffer_clear(void)
{
logbuffer_display_current = 0;
string_clear(&logbuffer);
}
static iram void log_finish(const string_t *from)
{
static bool newline_logged = true;
string_new(, timedate, 32);
if(newline_logged && string_front(from) == '\n')
return;
if(config_flags_match(flag_log_to_uart))
uart_send_string(0, from);
if(config_flags_match(flag_log_to_buffer))
{
if(newline_logged && (config_flags_match(flag_log_date) || config_flags_match(flag_log_time)))
{
unsigned int month, day, hour, minute, second;
time_get(&hour, &minute, &second, (unsigned int *)0, &month, &day);
if(config_flags_match(flag_log_date) && config_flags_match(flag_log_time))
string_format_cstr(&timedate, "%02u/%02u %02u:%02u ", month, day, hour, minute);
else
if(config_flags_match(flag_log_date))
string_format_cstr(&timedate, "%02u/%02u ", month, day);
else
if(config_flags_match(flag_log_time))
string_format_cstr(&timedate, "%02u:%02u:%02u ", hour, minute, second);
}
if((string_length(from) + string_length(&timedate) + string_length(&logbuffer)) >= string_size(&logbuffer))
logbuffer_clear();
string_append_string(&logbuffer, &timedate);
string_append_string(&logbuffer, from);
}
newline_logged = string_back(from) == '\n';
}
iram void log_from_flash_0(const char *data_in_flash)
{
int length;
length = flash_to_dram(true, data_in_flash, string_buffer_nonconst(&flash_dram), string_size(&flash_dram));
string_setlength(&flash_dram, length);
log_finish(&flash_dram);
}
iram void log_from_flash_n(const char *fmt_in_flash, ...)
{
int length;
va_list ap;
char fmt_in_dram[128];
flash_to_dram(true, fmt_in_flash, fmt_in_dram, sizeof(fmt_in_dram));
va_start(ap, fmt_in_flash);
length = vsnprintf(string_buffer_nonconst(&flash_dram), string_size(&flash_dram), fmt_in_dram, ap);
va_end(ap);
if(length < 0)
return;
string_setlength(&flash_dram, length);
log_finish(&flash_dram);
}
iram void logchar_sdk(char c)
{
static bool nl_seen = true;
if(c == '\n')
{
nl_seen = true;
log("\n");
}
else
{
if((c < ' ') || (c > '~'))
c = ' ';
if(nl_seen)
{
nl_seen = false;
log("[sdk] %c", c);
}
else
log("%c", c);
}
}
iram void msleep(int msec)
{
while(msec-- > 0)
{
system_soft_wdt_feed();
os_delay_us(1000);
}
}
const void *flash_cache_pointer(uint32_t offset)
{
static void * const flash_window_start = (void *)0x40200000;
return((void *)((uint8_t *)flash_window_start + offset));
}
attr_pure ip_addr_t ip_addr(const char *src)
{
ip_addr_to_bytes_t ip_addr_to_bytes;
unsigned int byte_index, current_value;
if(!src)
goto error;
for(byte_index = 0, current_value = 0; *src && (byte_index < 4); src++)
{
if(*src == '.')
{
ip_addr_to_bytes.byte[byte_index++] = current_value;
current_value = 0;
continue;
}
if((*src >= '0') && (*src <= '9'))
{
current_value *= 10;
current_value += (uint8_t)*src - '0';
continue;
}
goto error;
}
if(byte_index != 3)
goto error;
ip_addr_to_bytes.byte[byte_index] = current_value;
return(ip_addr_to_bytes.ip_addr);
error:
ip_addr_to_bytes.byte[0] = 0;
ip_addr_to_bytes.byte[1] = 0;
ip_addr_to_bytes.byte[2] = 0;
ip_addr_to_bytes.byte[3] = 0;
return(ip_addr_to_bytes.ip_addr);
}
void power_save_enable(bool enable)
{
if(enable)
{
wifi_set_listen_interval(3);
wifi_set_sleep_level(MAX_SLEEP_T);
wifi_set_sleep_type(MODEM_SLEEP_T);
}
else
wifi_set_sleep_type(NONE_SLEEP_T);
}
unsigned int utf8_to_unicode(const char *src, unsigned int dst_size, unsigned int *dst)
{
enum
{
u8p_state_base = 0,
u8p_state_utf8_byte_3 = 1,
u8p_state_utf8_byte_2 = 2,
u8p_state_utf8_byte_1 = 3,
u8p_state_done = 4
} state;
unsigned int src_current;
unsigned int src_index;
unsigned int dst_index;
unsigned int unicode;
for(src_index = 0, dst_index = 0, state = u8p_state_base, unicode = 0; src && dst && (dst_index < dst_size) && src[src_index]; src_index++)
{
src_current = (unsigned int)src[src_index];
switch(state)
{
case u8p_state_base:
{
if((src_current & 0xe0) == 0xc0) // first of two bytes (11 bits)
{
unicode = src_current & 0x1f;
state = u8p_state_utf8_byte_1;
continue;
}
else
if((src_current & 0xf0) == 0xe0) // first of three bytes (16 bits)
{
unicode = src_current & 0x0f;
state = u8p_state_utf8_byte_2;
continue;
}
else
if((src_current & 0xf8) == 0xf0) // first of four bytes (21 bits)
{
unicode = src_current & 0x07;
state = u8p_state_utf8_byte_3;
continue;
}
else
if((src_current & 0x80) == 0x80)
{
log("utf8 parser: invalid utf8, bit 7 set: %x %c\n", src_current, (int)src_current);
unicode = '*';
}
else
unicode = src_current & 0x7f;
break;
}
case u8p_state_utf8_byte_3 ... u8p_state_utf8_byte_1:
{
if((src_current & 0xc0) == 0x80) // following bytes
{
unicode = (unicode << 6) | (src_current & 0x3f);
if(++state != u8p_state_done)
continue;
}
else
{
log("utf8 parser: invalid utf8, no prefix on following byte, state: %u: %x %c\n", state, src_current, (int)src_current);
unicode = '*';
}
break;
}
default:
{
log("utf8 parser: invalid state %u\n", state);
unicode = '*';
}
}
dst[dst_index++] = unicode;
unicode = 0;
state = u8p_state_base;
}
return(dst_index);
}
unsigned char *MD5(const unsigned char *src, unsigned int length, uint8_t digest[MD5_DIGEST_LENGTH])
{
MD5_CTX context;
MD5Init(&context);
MD5Update(&context, src, length);
MD5Final(digest, &context);
return(digest);
}
uint32_t MD5_trunc_32(unsigned int length, const unsigned char *src)
{
uint32_t checksum;
uint8_t digest[MD5_DIGEST_LENGTH];
MD5(src, length, digest);
checksum = (digest[0] << 24) | (digest[1] << 16) | (digest[2] << 8) | (digest[3] << 0);
return(checksum);
}
void MD5_text(const unsigned char *src, unsigned int length, string_t *digest_text)
{
unsigned char digest[MD5_DIGEST_LENGTH];
MD5(src, length, digest);
string_bin_to_hex(digest_text, digest, MD5_DIGEST_LENGTH);
}
unsigned char *SHA1(const unsigned char *src, unsigned int length, uint8_t digest[SHA_DIGEST_LENGTH])
{
SHA_CTX context;
SHA1Init(&context);
SHA1Update(&context, src, length);
SHA1Final(digest, &context);
return(digest);
}
void SHA1_text(const unsigned char *src, unsigned int length, string_t *digest_text)
{
unsigned char digest[SHA_DIGEST_LENGTH];
SHA1(src, length, digest);
string_bin_to_hex(digest_text, digest, SHA_DIGEST_LENGTH);
}
parse_error_t parse_string(int index, const string_t *src, string_t *dst, char delimiter)
{
uint8_t current;
int offset;
if((offset = string_sep(src, 0, index, delimiter)) < 0)
return(parse_out_of_range);
for(; offset < src->length; offset++)
{
current = string_at(src, offset);
if(current == delimiter)
break;
if((current != '\r') && (current != '\n'))
string_append_char(dst, current);
}
return(parse_ok);
}
static parse_error_t parse_int_all(int index, const string_t *src, int64_t *dst, int base, char delimiter, bool do_signed)
{
bool valid, negative;
int64_t value;
int offset;
char current;
negative = false;
value = 0;
valid = false;
if((offset = string_sep(src, 0, index, delimiter)) < 0)
return(parse_out_of_range);
if(do_signed && (offset < string_length(src)) && ((base == 0) || (base == 10)))
{
if(string_at(src, offset) == '-')
{
negative = true;
base = 10;
offset++;
}
else
{
if(string_at(src, offset) == '+')
{
negative = false;
base = 10;
offset++;
}
}
}
if(base == 0)
{
if(((offset + 1) < string_length(src)) &&
(string_at(src, offset) == '0') &&
(string_at(src, offset + 1) == 'x'))
{
base = 16;
offset += 2;
}
else
base = 10;
}
for(; offset < string_length(src); offset++)
{
current = string_at(src, offset);
if((current >= 'A') && (current <= 'Z'))
current |= 0x20;
if((current >= '0') && (current <= '9'))
{
value *= base;
value += current - '0';
}
else
{
if((base > 10) && (current >= 'a') && (current <= ('a' + base - 11)))
{
value *= base;
value += current - 'a' + 10;
}
else
{
if((current != '\0') && (current != delimiter) && (current != '\n') && (current != '\r'))
valid = false;
break;
}
}
valid = true;
}
if(!valid)
return(parse_invalid);
if(do_signed && negative)
*dst = 0 - value;
else
*dst = value;
return(parse_ok);
}
attr_nonnull parse_error_t parse_uint(int index, const string_t *src, unsigned int *dst, int base, char delimiter)
{
int64_t value;
parse_error_t error;
if((error = parse_int_all(index, src, &value, base, delimiter, false)) != parse_ok)
{
*dst = 0;
return(error);
}
*dst = (unsigned int)value;
return(parse_ok);
}
attr_nonnull parse_error_t parse_int(int index, const string_t *src, int *dst, int base, char delimiter)
{
int64_t value;
parse_error_t error;
if((error = parse_int_all(index, src, &value, base, delimiter, true)) != parse_ok)
{
*dst = 0;
return(error);
}
*dst = (int)value;
return(parse_ok);
}
attr_nonnull parse_error_t parse_float(int index, const string_t *src, double *dst, char delimiter)
{
int offset;
int decimal;
bool negative;
bool valid;
double result;
char current;
valid = false;
negative = false;
offset = 0;
result = 0;
decimal = 0;
if((offset = string_sep(src, 0, index, delimiter)) < 0)
return(parse_out_of_range);
if(offset < string_length(src))
{
if((string_at(src, offset) == '-'))
{
negative = true;
offset++;
}
else
{
if((string_at(src, offset) == '+'))
{
negative = false;
offset++;
}
}
}
for(; offset < string_length(src); offset++)
{