forked from Y-Less/sscanf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.cpp
1012 lines (985 loc) · 18.9 KB
/
data.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
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
/*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the sscanf 2.0 SA:MP plugin.
*
* The Initial Developer of the Original Code is Alex "Y_Less" Cole.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Special Thanks to:
*
* SA:MP Team past, present and future
*/
#include "SDK/amx/amx.h"
#include "sscanf.h"
#include "utils.h"
extern unsigned int
g_iTrueMax;
extern logprintf_t
logprintf,
real_logprintf;
void
qlog(char *, ...);
// Options:
//
// 1 = OLD_DEFAULT_NAME
//
// Parse values in "U(5)" as if they were a name that must be connected,
// instead of just any number.
//
// 2 = MATCH_NAME_PARTIAL
//
// When searching for players, match any part of their name not just the
// start.
//
// 4 = CELLMIN_ON_MATCHES
//
// If multiple player name matches are found, return 0x80000000.
//
// 8 = SSCANF_QUIET
//
// Disable all prints.
//
int
gOptions = 0;
void
RestoreOpts(int opt)
{
gOptions = opt;
if (gOptions & 8) logprintf = qlog;
else logprintf = real_logprintf;
}
void
DoOptions(char * name, cell value)
{
// Not the most flexible code I've ever written...
if (!strincmp(name, "OLD_DEFAULT_NAME", 16))
{
switch (value)
{
case 1:
gOptions |= 1;
break;
case 0:
gOptions &= ~1;
break;
case -1:
if (*(name + 16) == '=')
{
if (*(name + 17) == '0') gOptions &= ~1;
else gOptions |= 1;
}
else
{
logprintf("sscanf error: No option value.");
}
}
}
else if (!strincmp(name, "MATCH_NAME_PARTIAL", 18))
{
switch (value)
{
case 1:
gOptions |= 2;
break;
case 0:
gOptions &= ~2;
break;
case -1:
if (*(name + 18) == '=')
{
if (*(name + 19) == '0') gOptions &= ~2;
else gOptions |= 2;
}
else
{
logprintf("sscanf error: No option value.");
}
}
}
else if (!strincmp(name, "CELLMIN_ON_MATCHES", 18))
{
switch (value)
{
case 1:
gOptions |= 4;
break;
case 0:
gOptions &= ~4;
break;
case -1:
if (*(name + 18) == '=')
{
if (*(name + 19) == '0') gOptions &= ~4;
else gOptions |= 4;
}
else
{
logprintf("sscanf error: No option value.");
}
}
}
else if (!strincmp(name, "SSCANF_QUIET", 12))
{
switch (value)
{
case 1:
logprintf = qlog;
gOptions |= 8;
break;
case 0:
logprintf = real_logprintf;
gOptions &= ~8;
break;
case -1:
if (*(name + 12) == '=')
{
if (*(name + 13) == '0')
{
logprintf = real_logprintf;
gOptions &= ~8;
}
else
{
logprintf = qlog;
gOptions |= 8;
}
}
else
{
logprintf("sscanf error: No option value.");
}
}
}
else if (!strincmp(name, "OLD_DEFAULT_KUSTOM", 18) || !strincmp(name, "OLD_DEFAULT_CUSTOM", 18))
{
switch (value)
{
case 1:
gOptions |= 16;
break;
case 0:
gOptions &= ~16;
break;
case -1:
if (*(name + 18) == '=')
{
if (*(name + 19) == '0') gOptions &= ~16;
else gOptions |= 16;
}
else
{
logprintf("sscanf error: No option value.");
}
}
}
else
{
logprintf("sscanf error: Unknown option name.");
}
}
char
GetSingleType(char ** format)
{
char
* cur = *format,
tmp = *cur;
if (tmp == '<')
{
++cur;
char
ret = *cur;
if (ret)
{
++cur;
if (*cur == '>')
{
*format += 3;
return ret;
}
else
{
logprintf("sscanf warning: Unclosed specifier parameter, assuming '<', consider using something like p<<>.");
}
}
else
{
logprintf("sscanf warning: Unenclosed specifier parameters are deprecated, consider using something like p<<>.");
}
++(*format);
return '<';
}
else if (tmp)
{
// Legacy support.
logprintf("sscanf warning: Unenclosed specifier parameters are deprecated, consider using something like p<%c>.", tmp);
++(*format);
return tmp;
}
else
{
logprintf("sscanf warning: No specified parameter found.");
return ' ';
}
}
char *
GetMultiType(char ** format)
{
char
* cur = *format,
* ret = cur;
if (*cur == '<')
{
++ret;
bool
reop = false,
escape = false;
do
{
if (*cur == '\\')
{
escape = !escape;
}
else
{
escape = false;
}
++cur;
if (!escape)
{
if (reop)
{
if (*cur == '>')
{
reop = false;
++cur;
}
}
else
{
if (*cur == '<')
{
reop = true;
++cur;
}
}
}
}
while (*cur && (reop || escape || *cur != '>'));
if (*cur)
{
// Only gets here if there is a closing '>'.
*cur = '\0';
*format = cur + 1;
return ret;
}
else
{
logprintf("sscanf error: Unclosed specifier parameters.");
}
}
else
{
logprintf("sscanf error: No specified parameters found.");
}
*format = cur;
return 0;
}
unsigned int
GetUserString(char * string, char ** end)
{
unsigned int
id = 0;
char
cur;
// Get the full name, they can't contain spaces in this code. If a
// player's name manages to contain spaces (e.g. via SetPlayerName), then
// you can only do the part of the name up to the space.
while ((cur = *string) && !IsSpacer(cur))
{
// Valid string item.
++string;
if ((unsigned char)(cur - '0') >= 10)
{
// Invalid number, so not an ID.
break;
}
// Still a valid ID, continue collecting.
id = (id * 10) + (cur - '0');
}
*end = string;
// Don't use extra checks every loop any more, just one will do.
if (!IsSpacer(cur))
{
// Invalid ID, just find the end of the name.
FindSpacer(end);
// Save the ID as too large for detection later.
id = g_iTrueMax;
}
return id;
}
int
GetDecValue(char ** const input)
{
char *
str = *input;
int
val = 0;
unsigned char
cur;
// Convert to a number and test it.
while ((cur = (unsigned char)(*str - '0')) < 10)
{
// Update the current value.
val = (val * 10) + cur;
// Update the current pointer.
++str;
}
// Save the pointer.
*input = str;
// Covert the sign and return without an if.
return val;
}
int
GetDec(char ** const input)
{
char *
str = *input;
int
neg = 1;
switch (*str)
{
case '-':
neg = -1;
// FALLTHROUGH
case '+':
// Check there is valid data after
if (((unsigned char)(*(++str) - '0')) >= 10)
{
// Just return now, the caller will recognise this as bad.
return 0;
}
}
*input = str;
return GetDecValue(input) * neg;
}
int
GetOctValue(char ** const input)
{
char *
str = *input;
int
val = 0;
unsigned char
cur;
// Convert to a number and test it.
while ((cur = (unsigned char)(*str - '0')) < 8)
{
// Update the current value.
val = (val * 8) + cur;
// Update the current pointer.
++str;
}
// Save the pointer.
*input = str;
// Covert the sign and return without an if.
return val;
}
int
GetOct(char ** const input)
{
char *
str = *input;
int
neg = 1;
switch (*str)
{
case '-':
neg = -1;
// FALLTHROUGH
case '+':
// Check there is valid data after
if (((unsigned char)(*(++str) - '0')) >= 8)
{
// Just return now, the caller will recognise this as bad.
return 0;
}
}
*input = str;
return GetOctValue(input) * neg;
}
int
GetHexValue(char ** const input)
{
char *
str = *input;
int
val = 0;
// Rewrote it using a big switch.
for ( ; ; )
{
switch (*str)
{
case '0':
val = (val * 16) + 0x00;
break;
case '1':
val = (val * 16) + 0x01;
break;
case '2':
val = (val * 16) + 0x02;
break;
case '3':
val = (val * 16) + 0x03;
break;
case '4':
val = (val * 16) + 0x04;
break;
case '5':
val = (val * 16) + 0x05;
break;
case '6':
val = (val * 16) + 0x06;
break;
case '7':
val = (val * 16) + 0x07;
break;
case '8':
val = (val * 16) + 0x08;
break;
case '9':
val = (val * 16) + 0x09;
break;
case 'a':
case 'A':
val = (val * 16) + 0x0A;
break;
case 'b':
case 'B':
val = (val * 16) + 0x0B;
break;
case 'c':
case 'C':
val = (val * 16) + 0x0C;
break;
case 'd':
case 'D':
val = (val * 16) + 0x0D;
break;
case 'e':
case 'E':
val = (val * 16) + 0x0E;
break;
case 'f':
case 'F':
val = (val * 16) + 0x0F;
break;
default:
// UGLY UGLY UGLY!
goto sscanf_hex_switch;
}
++str;
}
// UGLY UGLY UGLY - Needed for the double level break, which isn't native
// in C.
sscanf_hex_switch:
// Save the pointer.
*input = str;
// Covert the sign and return without an if.
return val;
}
int
GetHex(char ** const input)
{
char *
str = *input;
int
neg = 1;
switch (*str)
{
case '-':
neg = -1;
// FALLTHROUGH
case '+':
// Check there is valid data after
if (((unsigned char)(*(++str) - '0')) >= 10)
{
// Just return now, the caller will recognise this as bad.
return 0;
}
}
if (*str == '0')
{
if (*(str + 1) == 'x' || *(str + 1) == 'X')
{
// Check there is real data, otherwise it's bad.
str += 2;
if ((*str < '0') || ((*str > '9') && ((*str | 0x20) < 'a')) || ((*str | 0x20) > 'f'))
{
*input = str - 1;
return 0;
}
}
}
else if ((*str < '0') || ((*str > '9') && ((*str | 0x20) < 'a')) || ((*str | 0x20) > 'f'))
{
*input = str;
return 0;
}
*input = str;
return GetHexValue(input) * neg;
// Convert to a number and test it. Horribly manually optimised - one of
// these days I'll try write an ASM hex reader and see how well that works.
// Actually I think I have written one before, but I don't know where it is
// and I'm not sure how optimised it is. Anyway, this version works and
// avoids loads of big switches (although a single large switch may be more
// efficient thinking about it).
/*while ((cur = (unsigned char)((*str | 0x20) - '0')))
{
if (cur < 10)
{
// 0 - 9
val = (val * 16) + cur;
}
else
{
cur -= ('a' - '0');
if (cur < 6)
{
// A - F, a - f
val = (val * 16) + cur + 10;
}
else
{
// End of the number.
// Save the pointer.
*input = str;
// Covert the sign and return without an if.
return val * neg;
}
}
}*/
}
unsigned int
GetBoolValue(char ** const input)
{
char *
str = *input;
unsigned int
val = 0;
for ( ; ; ++str)
{
if (*str == '0')
{
val <<= 1;
}
else if (*str == '1')
{
val = (val << 1) | 1;
}
else
{
break;
}
}
// Save the pointer.
*input = str;
return val;
}
int
GetBool(char ** const input)
{
char *
str = *input;
if (*str == '0')
{
if (*(str + 1) == 'b' || *(str + 1) == 'B')
{
// Check there is real data, otherwise it's bad.
str += 2;
if (*str != '0' && *str != '1')
{
*input = str - 1;
return 0;
}
}
}
else if (*str != '1')
{
*input = str;
return 0;
}
*input = str;
return (int)GetBoolValue(input);
}
int
GetNumber(char ** const input)
{
char *
str = *input;
int
neg = 1;
switch (*str)
{
case '-':
neg = -1;
// FALLTHROUGH
case '+':
// Check there is valid data after
if (((unsigned char)(*(++str) - '0')) >= 10)
{
// Just return now, the caller will recognise this as bad.
return 0;
}
}
if (*str == '0')
{
++str;
switch (*str)
{
case 'x':
case 'X':
// Hex.
++str;
if (((*str >= '0') && (*str <= '9')) || (((*str | 0x20) >= 'a') && ((*str | 0x20) <= 'f')))
{
*input = str;
return GetHexValue(input) * neg;
}
else
{
*input = str - 1;
return 0;
}
case 'b':
case 'B':
// Bool.
if (neg == -1)
{
// Can't have negative booleans.
*input = str;
return 0;
}
else
{
++str;
if ((*str == '0') || (*str == '1'))
{
*input = str;
return (int)GetBoolValue(input);
}
else
{
*input = str - 1;
return 0;
}
}
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
// Octal.
*input = str;
return GetOctValue(input) * neg;
case '8':
case '9':
// Decimal.
break;
default:
*input = str;
return 0;
}
}
else if ((*str < '0') || (*str > '9'))
{
*input = str;
return 0;
}
*input = str;
return GetDecValue(input) * neg;
}
bool
GetLogical(char ** const input)
{
// Boolean values - if it's not '0' or 'false' then it's true. I could be
// strict and make them enter '1' or 'true' for true, but I'm not going to.
char *
string = *input;
bool
ret;
switch (*string++)
{
case '0':
// Set the value to false till we decide otherwise.
ret = false;
while (!IsSpacer(*string))
{
if (*string++ != '0')
{
// Value is not false, thus is true.
ret = true;
break;
}
}
// Let the cleanup code below handle excess chars.
break;
case 'f':
case 'F':
// Default to true unless we find "false" exactly.
ret = true;
if ((*string | 0x20) == 'a')
{
++string;
if ((*string | 0x20) == 'l')
{
++string;
if ((*string | 0x20) == 's')
{
++string;
if ((*string | 0x20) == 'e')
{
++string;
// We have the whole word, check that it's on its
// own, i.e. is followed by a space or delimiter
// Note that if your delimiter is in the word
// "false", you may have a bug here but they seem
// like odd delimiter characters tbh.
if (IsSpacer(*string))
{
// Exact word found on its own so save the fact
// that it's false.
ret = false;
}
}
}
}
}
break;
default:
ret = true;
break;
}
// Skip the rest of the data.
*input = string;
FindSpacer(input);
return ret;
}
bool
FindDefaultStart(char ** const str)
{
// Skip the default value passed for optional parameters.
if (**str == '(')
{
++(*str);
SkipWhitespace(str);
return true;
}
else
{
logprintf("sscanf warning: No default value found.");
}
return false;
}
void
SkipDefault(char ** const str)
{
if (FindDefaultStart(str))
{
// Default value found - skip it.
do
{
++(*str);
}
while (**str && **str != ')');
if (**str)
{
// Current pointer points to the close bracket, skip it.
++(*str);
}
else
{
logprintf("sscanf warning: Unclosed default value.");
}
}
}
void
SkipDefaultEx(char ** const data)
{
// Skip the default value passed for optional parameters.
char *
str = *data;
if (*str == '(')
{
// Default value found - skip it.
bool
escape = false;
while (*str && (escape || *str != ')'))
{
if (*str == '\\')
{
// Invert the escape, they may do:
// S(a\)
// Where the default string is "a\", in which case we need a
// way to tell the system not to include the close bracket,
// which would be:
// S(a\\)
// Or, in real PAWN terms:
// S(a\\\\)
// Of course, they can also do:
// S(\\\\\\\\\\))
// To get a final string of "\\)". Yes, you need FOUR slashes
// in a PAWN string to get just one in the output string.
escape = !escape;
}
else
{
escape = false;
}
++str;
}
if (*str)
{
// Current pointer points to the close bracket, skip it.
++str;
}
else
{
logprintf("sscanf warning: Unclosed default value.");
}
}
else
{
logprintf("sscanf warning: No default value found.");
}
*data = str;
}
void
SkipLength(char ** const input)
{
// Get an easy pointer to the data to manipulate.
char *
str = *input;
if (*str == '[')
{
while (*(++str))
{
// Don't check the length is valid, that's effort and slow.
if (*str == ']')
{
*input = str + 1;
return;
}
}
// If we get here then the end of the string was reached before the
// valid end of the length.
*input = str;
logprintf("sscanf warning: Missing string length end.");
}
else
{
logprintf("sscanf warning: Arrays without a length are deprecated, please add a destination size.");
}
}
int
GetLength(char ** const input, bool error)
{
if (**input == '[')
{
++(*input);
int
length = GetDec(input);
char *
str = *input;
if (length <= 0)
{
if (error)
{
length = 0;
logprintf("sscanf error: Invalid data length.");
}
else
{
length = SSCANF_MAX_LENGTH;
logprintf("sscanf warning: Invalid data length.");
}
}
if (*str == ']')
{
// Valid end: [number]
*input = str + 1;
return length;
}
else if (*str)
{
// Invalid character: [numberX]
logprintf("sscanf warning: Invalid character in data length.");
// Loop through the string till we find an end to the size.
while (*(++str))
{
if (*str == ']')
{
// Valid end: [numberX]
*input = str + 1;
return length;
}
}
}
// Invalid end: [number
logprintf("sscanf warning: Missing length end.");
*input = str;
return length;
}
else if (error)
{
logprintf("sscanf error: String/array must include a length, please add a destination size.");
return 0;
}
else
{
logprintf("sscanf warning: Strings without a length are deprecated, please add a destination size.");
return SSCANF_MAX_LENGTH;
}
}
void
GetJaggedSlot(cell * start, int count, int size, int slot, cell ** rs, int * rl)
{
// Get the start of the real data after the header.
cell
* cur,
* dstart = start + count,
* dend = dstart + (count * size),
* sstart = (cell *)((char *)(start + slot) + *(start + slot));
while (start < dstart)
{
cur = (cell *)((char *)start + *start);
// Use "<" not "<=" to avoid assigning 0-length slots.
if (sstart < cur && cur < dend)