-
Notifications
You must be signed in to change notification settings - Fork 7
/
cmd.c
executable file
·6485 lines (5867 loc) · 228 KB
/
cmd.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
/*
Copyright 2009-2017 Luigi Auriemma
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
http://www.gnu.org/licenses/gpl-2.0.txt
*/
// QuickBMS CMD operations
void init_crc_get(crc_context *ctx, u8 *key, u8 *ivec) {
if(key && key[0]) ctx->poly = myatoi(key);
if(ivec) {
int x1=ctx->init,x2=ctx->final;
get_parameter_numbers(ivec,
&ctx->bits, &x1, &x2, &ctx->type, &ctx->rever, &ctx->bitmask_side, NULL);
ctx->init=x1; ctx->final=x2;
}
}
crc_context *init_crc(u8 *type, u8 *key, u8 *ivec) {
crc_context *ctx = NULL;
ctx = calloc(1, sizeof(crc_context));
if(!ctx) STD_ERR(QUICKBMS_ERROR_MEMORY);
ctx->poly = 0xedb88320; // it's the one where the second element is 0x77073096
ctx->bits = 32;
ctx->init = -1;
ctx->final = -1;
ctx->type = 0;
ctx->rever = 0;
ctx->bitmask_side = 1;
init_crc_get(ctx, key, ivec);
if(!type) type = "";
if(!strnicmp(type, "crc", 3)) type += 3;
// if you specify no polynomial (no key), quickbms will get the default parameters
if(
(!key || !key[0]) // useless, in case of key "" and size 16
&& (!ivec || !ivec[0])
) {
if(!strcmp(type, "8") || (ctx->bits == 8)) {
ctx->poly = 0;
ctx->bits = 8;
ctx->init = 0;
ctx->final = 0;
ctx->type = 5;
ctx->rever = 0;
ctx->bitmask_side = 0;
} else if(!strcmp(type, "16") || (ctx->bits == 16)) {
ctx->poly = 0xa001; // second value equal to 0xc0c1
ctx->bits = 16;
ctx->init = 0;
ctx->final = 0;
ctx->type = 0;
ctx->rever = 0;
ctx->bitmask_side = 1;
} else if(!strcmp(type, "64") || (ctx->bits == 64)) {
ctx->poly = 0xad93d23594c935a9LL; // second value 0x7ad870c830358979
ctx->bits = 64;
ctx->init = 0;
ctx->final = 0;
ctx->type = 0;
ctx->rever = 1;
ctx->bitmask_side = 0;
} else if(!strcmp(type, "djb2")) {
ctx->init = 5381;
ctx->type = 15;
} else if(!strcmp(type, "sdbm")) {
ctx->init = 65599;
ctx->type = 16;
}
}
crc_make_table((void *)ctx->table, NULL, ctx->poly, ctx->bits, g_endian, ctx->rever, ctx->bitmask_side, NULL);
return ctx;
}
int old_set_math_operator(u8 **data);
int check_condition(int cmd, int res1, u8 *xcond, int res2) {
int var1n,
var2n,
res,
sign,
idx,
ifs,
i,
res_db[MAX_IFS + 1] = {0};
u8 *cond,
*var1,
*var2,
*s1,
*s2,
*p1,
*p2,
*p,
*cond_db[MAX_IFS + 1] = {NULL};
if(cmd >= 0) {
if((CMD.var[0] < 0) || (CMD.var[2] < 0)) return(TRUE); // needed for CMD_Else!
}
for(i = 0; i < MAX_IFS; i++) {
res_db[i] = 0;
cond_db[i] = NULL;
}
idx = 0;
for(ifs = 0; ifs < MAX_IFS; ifs++) {
var1n = 0;
var2n = 0;
sign = 0;
var1 = NULL;
var2 = NULL;
res = FALSE;
if(cmd < 0) {
var1n = res1;
cond = xcond;
var2n = res2;
} else {
cond = STR(idx + 1);
if(VARISNUM(idx + 0) && VARISNUM(idx + 2)) {
var1n = VAR32(idx + 0);
var2n = VAR32(idx + 2);
} else {
var1 = VAR(idx + 0);
var2 = VAR(idx + 2);
if(var_is_a_constant_string(CMD.var[idx + 0]) || var_is_a_constant_string(CMD.var[idx + 2])) {
// do nothing because they are strings
} else if(myisdechex_string(var1) && myisdechex_string(var2)) {
var1 = NULL;
var2 = NULL;
var1n = VAR32(idx + 0);
var2n = VAR32(idx + 2);
}
// in the For command I use a Set instruction at the beginning of the cycle with a String type
// now the downside is that it's a bit slower but being used only at the beginning of the cycle there is no
// loss of time (some milliseconds on tons of For cycles) and there is the pro of using also things like:
// for i = "hello" != "ciao"
}
idx += 3;
}
// replacing strcmp with a switch changes nothing in performance
if(!cond) return(res);
if(cond[0] == 'u') { // only the first and only 'u' to avoid loss of performances
sign = 1;
cond++;
}
if(!strcmp(cond, "<") || !stricmp(cond, "minor") || !stricmp(cond, "lower") || !stricmp(cond, "below")) {
if(var1 && var2) {
if(sign) {
if(strcmp (var1, var2) < 0) res = TRUE;
} else {
if(stricmp(var1, var2) < 0) res = TRUE;
}
} else {
if(sign) {
if((u_int)var1n < (u_int)var2n) res = TRUE;
} else {
if(var1n < var2n) res = TRUE;
}
}
} else if(!strcmp(cond, ">") || !stricmp(cond, "major") || !stricmp(cond, "greater") || !stricmp(cond, "above")) {
if(var1 && var2) {
if(sign) {
if(strcmp (var1, var2) > 0) res = TRUE;
} else {
if(stricmp(var1, var2) > 0) res = TRUE;
}
} else {
if(sign) {
if((u_int)var1n > (u_int)var2n) res = TRUE;
} else {
if(var1n > var2n) res = TRUE;
}
}
} else if(!strcmp(cond, "<>") || !strcmp(cond, "!=") || !strcmp(cond, "!==") || !stricmp(cond, "different")) {
if(var1 && var2) {
if(sign) {
if(strcmp (var1, var2) != 0) res = TRUE;
} else {
if(stricmp(var1, var2) != 0) res = TRUE;
}
} else {
if(var1n != var2n) res = TRUE;
}
} else if(!strcmp(cond, "=") || !strcmp(cond, "==") || !strcmp(cond, "===") || !stricmp(cond, "equal") || !stricmp(cond, "strcmp") || !stricmp(cond, "stricmp") || !stricmp(cond, "strcasecmp")) {
if(var1 && var2) {
if(sign) {
if(!strcmp (var1, var2)) res = TRUE;
} else {
if(!stricmp(var1, var2)) res = TRUE;
}
} else {
if(var1n == var2n) res = TRUE;
}
} else if(!strcmp(cond, ">=")) {
if(var1 && var2) {
if(sign) {
if(strcmp (var1, var2) >= 0) res = TRUE;
} else {
if(stricmp(var1, var2) >= 0) res = TRUE;
}
} else {
if(sign) {
if((u_int)var1n >= (u_int)var2n) res = TRUE;
} else {
if(var1n >= var2n) res = TRUE;
}
}
} else if(!strcmp(cond, "<=")) {
if(var1 && var2) {
if(sign) {
if(strcmp (var1, var2) <= 0) res = TRUE;
} else {
if(stricmp(var1, var2) <= 0) res = TRUE;
}
} else {
if(sign) {
if((u_int)var1n <= (u_int)var2n) res = TRUE;
} else {
if(var1n <= var2n) res = TRUE;
}
}
// added by me
} else if(!strcmp(cond, "&") || !strcmp(cond, "&&") || !stricmp(cond, "and") || !stricmp(cond, "strstr") || !stricmp(cond, "stristr") || !stricmp(cond, "strcasestr")) {
if(var1 && var2) {
if(sign) {
if(strstr (var1, var2)) res = TRUE;
} else {
if(stristr(var1, var2)) res = TRUE;
}
} else {
if(var1n & var2n) res = TRUE;
}
} else if(!strcmp(cond, "^") || !strcmp(cond, "^^") || !stricmp(cond, "xor")) {
if(var1 && var2) {
if(sign) {
if(!strcmp (var1, var2)) res = TRUE;
} else {
if(!stricmp(var1, var2)) res = TRUE;
}
} else {
if(var1n ^ var2n) res = TRUE;
}
} else if(!strcmp(cond, "|") || !strcmp(cond, "||") || !stricmp(cond, "or")) {
if(var1 && var2) {
res = TRUE;
} else {
if(var1n | var2n) res = TRUE;
}
} else if(!strcmp(cond, "%") || !stricmp(cond, "mod")) {
if(var1 && var2) {
res = TRUE;
} else {
if(sign) {
if(!var2n || ((u_int)var1n % (u_int)var2n)) res = TRUE;
} else {
if(!var2n || (var1n % var2n)) res = TRUE;
}
}
} else if(!strcmp(cond, "/") || !stricmp(cond, "div")) {
if(var1 && var2) {
res = TRUE;
} else {
if(sign) {
if(!var2n || ((u_int)var1n / (u_int)var2n)) res = TRUE;
} else {
if(!var2n || (var1n / var2n)) res = TRUE;
}
}
} else if(!strcmp(cond, "<<") || !stricmp(cond, "shl")) {
if(var1 && var2) {
res = TRUE;
} else {
if(sign) {
if((u_int)var1n << (u_int)var2n) res = TRUE;
} else {
if(var1n << var2n) res = TRUE;
}
}
} else if(!strcmp(cond, ">>") || !stricmp(cond, "shr")) {
if(var1 && var2) {
res = TRUE;
} else {
if(sign) {
if((u_int)var1n >> (u_int)var2n) res = TRUE;
} else {
if(var1n >> var2n) res = TRUE;
}
}
} else if(!strcmp(cond, "!") || !stricmp(cond, "not")) {
if(var1 && var2) {
res = TRUE;
} else {
if(!var1n) res = TRUE;
}
} else if(!strcmp(cond, "!!")) {
if(var1) {
res = TRUE;
} else {
if(var1n) res = TRUE;
}
} else if(!strcmp(cond, "~") || !stricmp(cond, "complement")) {
if(var1 && var2) {
res = TRUE;
} else {
if(~var1n) res = TRUE;
}
} else if(!stricmp(cond, "strncmp") || !stricmp(cond, "strnicmp") || !stricmp(cond, "strncasecmp")) {
if(var1 && var2) {
if(sign) {
if(!strncmp (var1, var2, strlen(var2))) res = TRUE;
} else {
if(!strnicmp(var1, var2, strlen(var2))) res = TRUE;
}
} else {
if((var1n & var2n) == var2n) res = TRUE;
}
} else if(!stricmp(cond, "ext") || !stricmp(cond, "extension")) {
if(var1 && var2) {
p = strrchr(var1, '.');
if(p) {
if(sign) {
if(!strcmp (p + 1, var2)) res = TRUE;
} else {
if(!stricmp(p + 1, var2)) res = TRUE;
}
}
} else {
res = TRUE;
}
} else if(!stricmp(cond, "basename")) {
if(var1 && var2) {
#define check_condition_basename(X) \
s##X = mystrrchrs(var##X, PATH_DELIMITERS); \
if(!s##X) s##X = var##X; \
else while(*s##X && strchr(PATH_DELIMITERS, *s##X)) s##X++; \
p##X = strrchr(s##X, '.'); \
if(p##X) *p##X = 0;
check_condition_basename(1)
check_condition_basename(2)
if(sign) {
if(!strcmp (s1, s2)) res = TRUE;
} else {
if(!stricmp(s1, s2)) res = TRUE;
}
if(p1) *p1 = '.'; // restore
if(p2) *p2 = '.'; // restore
} else {
res = TRUE;
}
} else if(!stricmp(cond, "filepath")) {
if(var1 && var2) {
#define check_condition_filepath(X) \
for(p##X = var##X; *p##X; p##X++) { \
if(!strchr(PATH_DELIMITERS, *p##X)) break; \
} \
s##X = mystrrchrs(p##X, PATH_DELIMITERS); \
if(!s##X) s##X = p##X + strlen(p##X);
check_condition_filepath(1)
check_condition_filepath(2)
if((s1 - p1) == (s2 - p2)) {
for(; p1 != s1; p1++, p2++) {
if(strchr(PATH_DELIMITERS, *p1) && strchr(PATH_DELIMITERS, *p2)) continue;
if(sign) {
if(*p1 != *p2) break;
} else {
if(tolower(*p1) != tolower(*p2)) break;
}
}
if(p1 == s1) res = TRUE;
/* no slash/backslash check
if(sign) {
if(!strncmp (p1, p2, s1 - p1)) res = TRUE;
} else {
if(!strnicmp(p1, p2, s1 - p1)) res = TRUE;
}
*/
}
} else {
res = TRUE;
}
} else if(!stricmp(cond, "filename")) {
if(var1 && var2) {
s1 = mystrrchrs(var1, PATH_DELIMITERS);
if(!s1) s1 = var1;
else s1++;
s2 = mystrrchrs(var2, PATH_DELIMITERS);
if(!s2) s2 = var2;
else s2++;
if(sign) {
if(!strcmp (s1, s2)) res = TRUE;
} else {
if(!stricmp(s1, s2)) res = TRUE;
}
} else {
res = TRUE;
}
} else {
if(var1 && var2) {
fprintf(stderr, "\nError: invalid condition %s\n", cond);
myexit_cmd(cmd, QUICKBMS_ERROR_BMS);
}
int op = cond[0];
p = cond;
op = old_set_math_operator((u8 **)&p);
if(math_operations(cmd, var1n, op, var2n, sign)) res = TRUE;
}
if(g_verbose > 0) printf(" condition %s is%smet\n", cond, (res == TRUE) ? " " : " not ");
if(cmd < 0) break;
res_db[ifs] = res;
if((idx + 4) > MAX_ARGS) break;
cond = STR(idx);
if(!cond) break;
idx++;
cond_db[ifs] = cond;
}
// ifs is the number of handled "if" minus one
while(ifs > 0) {
for(i = 0; i < ifs; i++) {
// check current and next
res = check_condition(-1, res_db[i], cond_db[i], res_db[i + 1]);
// store in next that will become the new current
res_db[i + 1] = res;
}
// next->current
for(i = 0; i < ifs; i++) {
res_db[i] = res_db[i + 1];
cond_db[i] = cond_db[i + 1];
}
ifs--;
}
return(res);
}
int CMD_CLog_func(int cmd) {
int fd,
offset,
size,
zsize,
xsize = 0;
u8 *name;
name = VAR(0);
offset = VAR32(1);
zsize = VAR32(2);
size = VAR32(5);
fd = FILEZ(7);
if(CMD.var[8] >= 0) xsize = VAR32(8);
if(dumpa(fd, name, VARNAME(0), offset, size, zsize, xsize) < 0) return -1;
return 0;
}
int CMD_FindLoc_func(int cmd) {
static u8 *sign = NULL,
*buff = NULL;
int fd,
i,
idx,
len,
oldoff,
tmpoff,
offset = -1LL,
str_len,
sign_len,
endoff = -1LL;
u8 *str,
*ret_if_error;
fd = FILEZ(3);
oldoff = myftell(fd);
str = STR(2); // remember that str can be also a sequence of bytes, included 0x00!
str_len = NUM(2);
ret_if_error = STR(4);
if(CMD.var[5] >= 0) endoff = VAR32(5);
// the following has been disabled because causes troubles with old scripts
//quick_var_from_name_check(&str, &str_len); // so it supports also the variables
if(
(NUM(1) == BMS_TYPE_STRING) ||
(NUM(1) == BMS_TYPE_BINARY)
) {
if(str_len == strlen(str)) {
idx = get_var_from_name(str, -1);
if(idx >= 0) {
str = get_var(idx);
str_len = strlen(str);
}
}
sign_len = str_len;
if(sign_len == (u_int)-1LL) ALLOC_ERR;
sign = realloc(sign, sign_len + 1);
if(!sign) STD_ERR(QUICKBMS_ERROR_MEMORY);
memcpy(sign, str, sign_len);
sign[sign_len] = 0;
} else if(NUM(1) == BMS_TYPE_UNICODE) {
if(str_len == strlen(str)) {
idx = get_var_from_name(str, -1);
if(idx >= 0) {
str = get_var(idx);
str_len = strlen(str);
}
}
sign_len = (str_len + 1) * sizeof(u16) /*sizeof(wchar_t)*/; // yeah includes also the NULL delimiter, boring unicode
if(sign_len < str_len) ALLOC_ERR;
if(sign_len == (u_int)-1LL) ALLOC_ERR;
sign = realloc(sign, sign_len + 1);
if(!sign) STD_ERR(QUICKBMS_ERROR_MEMORY);
sign_len = utf8_to_utf16(str, str_len, sign, sign_len, 0);
} else {
sign_len = NUM(1); // yeah the type in NUM(1) is written for having the size of the parameter, watch enum
if(sign_len < 0) STD_ERR(QUICKBMS_ERROR_BMS);
if(sign_len == (u_int)-1LL) ALLOC_ERR;
sign = realloc(sign, sign_len + 1);
if(!sign) STD_ERR(QUICKBMS_ERROR_MEMORY);
sign[sign_len] = 0;
putxx(sign, myatoi(str), sign_len);
if(str_len == strlen(str)) {
idx = get_var_from_name(str, -1);
if(idx >= 0) {
sign_len = sizeof(u32); // 32bit
if(sign_len == (u_int)-1LL) ALLOC_ERR;
sign = realloc(sign, sign_len + 1);
if(!sign) STD_ERR(QUICKBMS_ERROR_MEMORY);
sign[sign_len] = 0;
putxx(sign, get_var32(idx), sign_len);
}
}
}
if(sign_len <= 0) goto quit;
if(sign_len > BUFFSZ) { // lazy boy
fprintf(stderr, "\nError: the FindLoc function works with a searchable string of max %d bytes\n", BUFFSZ);
myexit_cmd(cmd, QUICKBMS_ERROR_BMS);
}
if(!buff) {
buff = malloc(BUFFSZ + 1);
if(!buff) STD_ERR(QUICKBMS_ERROR_MEMORY);
}
int buffsz = BUFFSZ;
tmpoff = oldoff;
if(
(endoff == (u_int)-1LL) // default till end of file
|| (endoff > oldoff) // if endoff is above current offset
) {
for(;;) {
if(endoff == (u_int)-1LL) {
} else {
if((u_int)(endoff - tmpoff) < (u_int)buffsz) buffsz = endoff - tmpoff;
}
//len = myfr(fd, buff, -1, TRUE); // -1 uses BUFFSZ automatically and doesn't quit if the file terminates
len = myfr(fd, buff, buffsz, FALSE);
if(len < sign_len) break; // performes (len <= 0) too automatically
for(i = 0; i <= (len - sign_len); i++) {
if(!memcmp(buff + i, sign, sign_len)) {
offset = (myftell(fd) - len) + i;
goto quit;
}
}
tmpoff += i + 1;
myfseek(fd, tmpoff, SEEK_SET);
//myfseek(fd, sign_len - 1, SEEK_CUR);
}
} else if(endoff < oldoff) {
for(;;) {
if((u_int)(tmpoff - endoff) < (u_int)buffsz) buffsz = tmpoff - endoff;
tmpoff -= buffsz;
myfseek(fd, tmpoff, SEEK_SET);
len = myfr(fd, buff, buffsz, FALSE);
if(len < sign_len) break; // performes (len <= 0) too automatically
for(i = (len - sign_len); i >= 0; i--) {
if(!memcmp(buff + i, sign, sign_len)) {
offset = (myftell(fd) - len) + i;
goto quit;
}
}
tmpoff += (sign_len - 1);
}
}
quit:
myfseek(fd, oldoff, SEEK_SET);
if(offset == (u_int)-1LL) {
if(ret_if_error) {
add_var(CMD.var[0], NULL, ret_if_error, 0, -1);
} else {
return -1; // confirmed
}
} else {
add_var(CMD.var[0], NULL, NULL, offset, sizeof(int));
}
return 0;
}
int CMD_GetBits_func(int cmd) {
FDBITS
int fd,
len = -1,
tmpn = 0,
bits,
verbose_offset = 0;
u8 *tmp = NULL;
fd = NUM(2);
bits = VAR32(1);
if(g_verbose < 0) verbose_offset = myftell(fd);
my_fdbits(fd, &bitchr, &bitpos, &bitoff, 0, 0, 0);
if(myftell(fd) != bitoff) {
bitchr = 0;
bitpos = 0;
}
if(bits <= 32) {
tmpn = fd_read_bits(bits, &bitchr, &bitpos, fd);
} else {
len = ((bits + 7) & (~7)) / 8;
tmp = calloc(len + 1, 1);
if(!tmp) STD_ERR(QUICKBMS_ERROR_MEMORY);
len = bits2str(tmp, len, bits, &bitchr, &bitpos, fd);
}
my_fdbits(fd, NULL, NULL, NULL, bitchr, bitpos, myftell(fd));
if(tmp) {
if(g_verbose < 0) verbose_print(verbose_offset, "getbits", CMD.var[0], tmp, len, 0, bits);
add_var(CMD.var[0], NULL, tmp, 0, len);
FREE(tmp)
} else {
if(g_verbose < 0) verbose_print(verbose_offset, "getbits", CMD.var[0], NULL, 0, tmpn, bits);
add_var(CMD.var[0], NULL, NULL, tmpn, sizeof(int));
}
return 0;
}
int CMD_PutBits_func(int cmd) {
FDBITS
int fd,
len = -1,
tmpn = 0,
bits,
verbose_offset = 0;
u8 *tmp = NULL;
fd = NUM(2);
bits = VAR32(1);
if(g_verbose < 0) verbose_offset = myftell(fd);
my_fdbits(fd, &bitchr, &bitpos, &bitoff, 0, 0, 0);
if(myftell(fd) != bitoff) {
bitchr = 0;
bitpos = 0;
}
if(bits <= 32) {
tmpn = VAR32(0);
if(g_verbose < 0) verbose_print(verbose_offset, "putbits", CMD.var[0], NULL, 0, tmpn, bits);
fd_write_bits(tmpn, bits, &bitchr, &bitpos, fd);
} else {
len = ((bits + 7) & (~7)) / 8;
tmp = VAR(0);
if(len > VARSZ(0)) len = VARSZ(0);
if(g_verbose < 0) verbose_print(verbose_offset, "putbits", CMD.var[0], tmp, len, 0, bits);
len = str2bits(tmp, len, bits, &bitchr, &bitpos, fd);
}
my_fdbits(fd, NULL, NULL, NULL, bitchr, bitpos, myftell(fd));
return 0;
}
int CMD_Get_func(int cmd) {
int fd,
type,
tmpn = 0,
error = 0,
verbose_offset = 0;
u8 *tmp = NULL;
fd = FILEZ(2);
type = NUM(1);
if(g_verbose < 0) verbose_offset = myftell(fd);
tmp = myfrx(fd, type, &tmpn, &error);
// now tmp can be also NULL because myfrx is string/int
//if(!tmp) return -1; // here should be good to quit... but I leave it as is for back compatibility with the old quickbms!
if(error) return -1;
if(tmp) {
if(g_verbose < 0) verbose_print(verbose_offset, "get", CMD.var[0], tmp, -1, 0, type);
add_var(CMD.var[0], NULL, tmp, 0, -1);
} else {
if(g_verbose < 0) verbose_print(verbose_offset, "get", CMD.var[0], NULL, 0, tmpn, type);
add_var(CMD.var[0], NULL, NULL, tmpn, sizeof(int));
}
return 0;
}
int CMD_IDString_func(int cmd) {
static int buffsz = 0;
static u8 *sign = NULL,
*buff = NULL;
int fd,
len,
verbose_offset = 0;
fd = FILEZ(0);
sign = STR(1);
len = NUM(1);
if(len == (u_int)-1LL) ALLOC_ERR; // in case of updates
myalloc(&buff, len, &buffsz); // memcmp, so not + 1
if(g_verbose < 0) verbose_offset = myftell(fd);
myfr(fd, buff, len, TRUE);
if(memcmp(buff, sign, len)) {
if((len == 4) && // automatic endianess... cool
(buff[0] == sign[3]) && (buff[1] == sign[2]) &&
(buff[2] == sign[1]) && (buff[3] == sign[0])) {
g_endian = (g_endian == MYLITTLE_ENDIAN) ? MYBIG_ENDIAN : MYLITTLE_ENDIAN;
return 0;
}
/*
fprintf(stderr, "\n"
"- signature doesn't match the one expected by the script:\n"
" this one: \"%.60s\"\n"
" expected: \"%.60s\"\n",
buff, sign);
*/
fprintf(stderr, "\n"
"- signature of %d bytes at offset 0x%"PRIx" doesn't match the one\n"
" expected by the script:\n"
"\n"
" this one: \"%.60s\"\n",
(i32)len, myftell(fd) - len,
buff);
show_dump(2, buff, len, stderr);
fprintf(stderr, "\n"
" expected: \"%.60s\"\n",
sign);
show_dump(2, sign, len, stderr);
return -1;
}
if(g_verbose < 0) verbose_print(verbose_offset, "idstr ", CMD.var[0], buff, len, 0, len);
return 0;
}
int CMD_GoTo_func(int cmd) {
int fd,
pos;
u8 *str;
fd = FILEZ(1);
str = VAR(0);
if(!stricmp(str, "SOF") || !stricmp(str, "SEEK_SET")) {
myfseek(fd, 0, SEEK_SET);
} else if(!stricmp(str, "EOF") || !stricmp(str, "SEEK_END")) {
myfseek(fd, 0, SEEK_END);
} else {
pos = VAR32(0);
if((NUM(2) == SEEK_SET) && (pos < 0) && var_is_a_constant(CMD.var[0])) {
myfseek(fd, pos, SEEK_END); // only constants can be negative, not vars
} else {
myfseek(fd, pos, NUM(2)); //SEEK_SET);
}
}
return 0;
}
int CMD_SavePos_func(int cmd) {
int fd;
fd = FILEZ(1);
add_var(CMD.var[0], NULL, NULL, myftell(fd), sizeof(int));
return 0;
}
int old_set_math_operator(u8 **data) {
int ret = 0;
u8 *p;
p = *data;
#define SET_MATH_OPERATOR_ASSIGN(X, Y) \
if(!strnicmp(p, X, sizeof(X) - 1)) { \
ret = Y; \
p += sizeof(X) - 1; \
} else
// Do NOT start them with 'u' because it's handled as unsigned
// They must be longer than one char
SET_MATH_OPERATOR_ASSIGN("<<<", 'l')
SET_MATH_OPERATOR_ASSIGN(">>>", 'r')
SET_MATH_OPERATOR_ASSIGN("**", 'p')
SET_MATH_OPERATOR_ASSIGN("//", 'v')
SET_MATH_OPERATOR_ASSIGN("&&", 'x')
SET_MATH_OPERATOR_ASSIGN("<>", 'z')
SET_MATH_OPERATOR_ASSIGN("%%", -176)
SET_MATH_OPERATOR_ASSIGN("reverselong", -1000)
SET_MATH_OPERATOR_ASSIGN("reverseshort", -1001)
SET_MATH_OPERATOR_ASSIGN("reverselonglong", -1002)
// no restrictions here
if(*p == '?') { // this is an "else" from the previous SET_MATH_OPERATOR_ASSIGN
p++;
SET_MATH_OPERATOR_ASSIGN("add", '+')
SET_MATH_OPERATOR_ASSIGN("mul", '*')
SET_MATH_OPERATOR_ASSIGN("div", '/')
SET_MATH_OPERATOR_ASSIGN("sub", '-')
SET_MATH_OPERATOR_ASSIGN("xor", '^')
SET_MATH_OPERATOR_ASSIGN("and", '&')
SET_MATH_OPERATOR_ASSIGN("or", '|')
SET_MATH_OPERATOR_ASSIGN("mod", '%')
SET_MATH_OPERATOR_ASSIGN("not", '!')
SET_MATH_OPERATOR_ASSIGN("com", '~')
SET_MATH_OPERATOR_ASSIGN("shl", '<')
SET_MATH_OPERATOR_ASSIGN("shr", '>')
SET_MATH_OPERATOR_ASSIGN("rol", 'l')
SET_MATH_OPERATOR_ASSIGN("ror", 'r')
SET_MATH_OPERATOR_ASSIGN("byteswap",'s')
SET_MATH_OPERATOR_ASSIGN("bitswap", 'w')
SET_MATH_OPERATOR_ASSIGN("equal", '=')
SET_MATH_OPERATOR_ASSIGN("neg", 'n')
SET_MATH_OPERATOR_ASSIGN("abs", 'a')
SET_MATH_OPERATOR_ASSIGN("radix", 'v')
SET_MATH_OPERATOR_ASSIGN("power", 'p')
SET_MATH_OPERATOR_ASSIGN("align", 'x')
SET_MATH_OPERATOR_ASSIGN("bitswap2", 'z')
// math.h
SET_MATH_OPERATOR_ASSIGN("sin", -1)
SET_MATH_OPERATOR_ASSIGN("cos", -2)
SET_MATH_OPERATOR_ASSIGN("tan", -3)
SET_MATH_OPERATOR_ASSIGN("sinh", -4)
SET_MATH_OPERATOR_ASSIGN("cosh", -5)
SET_MATH_OPERATOR_ASSIGN("tanh", -6)
SET_MATH_OPERATOR_ASSIGN("asin", -7)
SET_MATH_OPERATOR_ASSIGN("acos", -8)
SET_MATH_OPERATOR_ASSIGN("atan", -9)
SET_MATH_OPERATOR_ASSIGN("atan2", -10)
SET_MATH_OPERATOR_ASSIGN("exp", -11)
SET_MATH_OPERATOR_ASSIGN("log", -12)
SET_MATH_OPERATOR_ASSIGN("log10", -13)
SET_MATH_OPERATOR_ASSIGN("pow", -14)
SET_MATH_OPERATOR_ASSIGN("sqrt", -15)
SET_MATH_OPERATOR_ASSIGN("ceil", -16)
SET_MATH_OPERATOR_ASSIGN("floor", -17)
SET_MATH_OPERATOR_ASSIGN("fabs", -18)
SET_MATH_OPERATOR_ASSIGN("ldexp", -19)
SET_MATH_OPERATOR_ASSIGN("frexp", -20)
SET_MATH_OPERATOR_ASSIGN("modf", -21)
SET_MATH_OPERATOR_ASSIGN("fmod", -22)
SET_MATH_OPERATOR_ASSIGN("_hypot", -23)
SET_MATH_OPERATOR_ASSIGN("_j0", -24)
SET_MATH_OPERATOR_ASSIGN("_j1", -25)
SET_MATH_OPERATOR_ASSIGN("_jn", -26)
SET_MATH_OPERATOR_ASSIGN("_y0", -27)
SET_MATH_OPERATOR_ASSIGN("_y1", -28)
SET_MATH_OPERATOR_ASSIGN("_yn", -29)
SET_MATH_OPERATOR_ASSIGN("sinf", -30)
SET_MATH_OPERATOR_ASSIGN("sinl", -31)
SET_MATH_OPERATOR_ASSIGN("cosf", -32)
SET_MATH_OPERATOR_ASSIGN("cosl", -33)
SET_MATH_OPERATOR_ASSIGN("tanf", -34)
SET_MATH_OPERATOR_ASSIGN("tanl", -35)
SET_MATH_OPERATOR_ASSIGN("asinf", -36)
SET_MATH_OPERATOR_ASSIGN("asinl", -37)
SET_MATH_OPERATOR_ASSIGN("acosf", -38)
SET_MATH_OPERATOR_ASSIGN("acosl", -39)
SET_MATH_OPERATOR_ASSIGN("atanf", -40)
SET_MATH_OPERATOR_ASSIGN("atanl", -41)
SET_MATH_OPERATOR_ASSIGN("atan2f", -42)
SET_MATH_OPERATOR_ASSIGN("atan2l", -43)
SET_MATH_OPERATOR_ASSIGN("sinhf", -44)
SET_MATH_OPERATOR_ASSIGN("sinhl", -45)
SET_MATH_OPERATOR_ASSIGN("coshf", -46)
SET_MATH_OPERATOR_ASSIGN("coshl", -47)
SET_MATH_OPERATOR_ASSIGN("tanhf", -48)
SET_MATH_OPERATOR_ASSIGN("tanhl", -49)
SET_MATH_OPERATOR_ASSIGN("acosh", -50)
SET_MATH_OPERATOR_ASSIGN("acoshf", -51)
SET_MATH_OPERATOR_ASSIGN("acoshl", -52)
SET_MATH_OPERATOR_ASSIGN("asinh", -53)
SET_MATH_OPERATOR_ASSIGN("asinhf", -54)
SET_MATH_OPERATOR_ASSIGN("asinhl", -55)
SET_MATH_OPERATOR_ASSIGN("atanh", -56)
SET_MATH_OPERATOR_ASSIGN("atanhf", -57)
SET_MATH_OPERATOR_ASSIGN("atanhl", -58)
SET_MATH_OPERATOR_ASSIGN("expf", -59)
SET_MATH_OPERATOR_ASSIGN("expl", -60)
SET_MATH_OPERATOR_ASSIGN("exp2", -61)
SET_MATH_OPERATOR_ASSIGN("exp2f", -62)
SET_MATH_OPERATOR_ASSIGN("exp2l", -63)
SET_MATH_OPERATOR_ASSIGN("expm1", -64)
SET_MATH_OPERATOR_ASSIGN("expm1f", -65)
SET_MATH_OPERATOR_ASSIGN("expm1l", -66)
SET_MATH_OPERATOR_ASSIGN("frexpf", -67)
SET_MATH_OPERATOR_ASSIGN("frexpl", -68)
SET_MATH_OPERATOR_ASSIGN("ilogb", -69)
SET_MATH_OPERATOR_ASSIGN("ilogbf", -70)
SET_MATH_OPERATOR_ASSIGN("ilogbl", -71)
SET_MATH_OPERATOR_ASSIGN("ldexpf", -72)
SET_MATH_OPERATOR_ASSIGN("ldexpl", -73)
SET_MATH_OPERATOR_ASSIGN("logf", -74)
SET_MATH_OPERATOR_ASSIGN("logl", -75)
SET_MATH_OPERATOR_ASSIGN("log10f", -76)
SET_MATH_OPERATOR_ASSIGN("log10l", -77)
SET_MATH_OPERATOR_ASSIGN("log1p", -78)
SET_MATH_OPERATOR_ASSIGN("log1pf", -79)
SET_MATH_OPERATOR_ASSIGN("log1pl", -80)
SET_MATH_OPERATOR_ASSIGN("log2", -81)
SET_MATH_OPERATOR_ASSIGN("log2f", -82)
SET_MATH_OPERATOR_ASSIGN("log2l", -83)
SET_MATH_OPERATOR_ASSIGN("logb", -84)
SET_MATH_OPERATOR_ASSIGN("logbf", -85)
SET_MATH_OPERATOR_ASSIGN("logbl", -86)
SET_MATH_OPERATOR_ASSIGN("modff", -87)
SET_MATH_OPERATOR_ASSIGN("modfl", -88)
SET_MATH_OPERATOR_ASSIGN("scalbn", -89)
SET_MATH_OPERATOR_ASSIGN("scalbnf", -90)
SET_MATH_OPERATOR_ASSIGN("scalbnl", -91)
SET_MATH_OPERATOR_ASSIGN("scalbln", -92)
SET_MATH_OPERATOR_ASSIGN("scalblnf", -93)
SET_MATH_OPERATOR_ASSIGN("scalblnl", -94)
SET_MATH_OPERATOR_ASSIGN("cbrt", -95)
SET_MATH_OPERATOR_ASSIGN("cbrtf", -96)
SET_MATH_OPERATOR_ASSIGN("cbrtl", -97)
SET_MATH_OPERATOR_ASSIGN("fabsf", -98)
SET_MATH_OPERATOR_ASSIGN("fabsl", -99)
SET_MATH_OPERATOR_ASSIGN("hypot", -100)
SET_MATH_OPERATOR_ASSIGN("hypotf", -101)
SET_MATH_OPERATOR_ASSIGN("hypotl", -102)
SET_MATH_OPERATOR_ASSIGN("powf", -103)
SET_MATH_OPERATOR_ASSIGN("powl", -104)
SET_MATH_OPERATOR_ASSIGN("sqrtf", -105)
SET_MATH_OPERATOR_ASSIGN("sqrtl", -106)
SET_MATH_OPERATOR_ASSIGN("erf", -107)
SET_MATH_OPERATOR_ASSIGN("erff", -108)
SET_MATH_OPERATOR_ASSIGN("erfl", -109)
SET_MATH_OPERATOR_ASSIGN("erfc", -110)
SET_MATH_OPERATOR_ASSIGN("erfcf", -111)
SET_MATH_OPERATOR_ASSIGN("erfcl", -112)
SET_MATH_OPERATOR_ASSIGN("lgamma", -113)