-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.c
executable file
·3030 lines (2475 loc) · 81.7 KB
/
utils.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 general functions
// the macro is better for size and debugging (no stack canary and ret)
//static void set_int3(const void *dllname, const void *hlib, const void *funcname, const void *funcaddr, const void *argc) {
#define set_int3(dllname, hlib, funcname, funcaddr, argc) { \
if(g_int3) { \
__asm__ __volatile__ ("movl %0, %%eax" :: "g"(dllname) : "ecx", "edx", "esi", "edi"); \
__asm__ __volatile__ ("movl %0, %%ecx" :: "g"(hlib) : "eax", "edx", "esi", "edi"); \
__asm__ __volatile__ ("movl %0, %%edx" :: "g"(funcname) : "eax", "ecx", "esi", "edi"); \
__asm__ __volatile__ ("movl %0, %%esi" :: "g"(funcaddr) : "eax", "ecx", "edx", "edi"); \
__asm__ __volatile__ ("movl %0, %%edi" :: "g"(argc) : "eax", "ecx", "edx", "esi" ); \
__asm__ __volatile__ ("int3"); \
__asm__ __volatile__ ("nop"); \
} \
}
u8 *show_dump(int left, u8 *data, int len, FILE *stream) {
int rem;
static const u8 hex[16] = "0123456789abcdef";
u8 leftbuff[80],
buff[67],
chr,
*bytes,
*p,
*limit,
*glimit = data + len;
#define show_dump_stream(X,Y) { \
if(stream) { \
fwrite(X, 1, Y, stream); \
} else { \
out_buffsz += Y; \
myalloc(&out_buff, out_buffsz + 1, NULL); \
memcpy(out_buff + out_buffsz - Y, X, Y); \
out_buff[out_buffsz] = 0; \
} \
}
int out_buffsz = 0;
u8 *out_buff = NULL;
if(!stream) {
out_buff = calloc(1, 1);
out_buff[0] = 0;
}
if(!data) return NULL;
if(len < 0) return NULL;
memset(buff + 2, ' ', 48);
memset(leftbuff, ' ', sizeof(leftbuff));
while(data < glimit) {
limit = data + 16;
if(limit > glimit) {
limit = glimit;
memset(buff, ' ', 48);
}
p = buff;
bytes = p + 50;
while(data < limit) {
chr = *data;
*p++ = hex[chr >> 4];
*p++ = hex[chr & 15];
p++;
*bytes++ = ((chr < ' ') || (chr >= 0x7f)) ? '.' : chr;
data++;
}
*bytes++ = '\n';
for(rem = left; rem >= sizeof(leftbuff); rem -= sizeof(leftbuff)) {
show_dump_stream(leftbuff, sizeof(leftbuff))
}
if(rem > 0) fwrite(leftbuff, rem, 1, stream);
show_dump_stream(buff, (bytes - buff))
}
return out_buff;
}
int check_extension(u8 *fname, u8 *ext) {
u8 *p;
if(!fname || !ext) return 0;
p = strrchr(fname, '.');
if(!p) return 0;
p++;
if(!stricmp(p, ext)) return 1;
return 0;
}
u8 *mystrcpy(u8 *dst, u8 *src, int max) {
u8 *p,
*l;
if(dst && (max > 0)) {
if(!src) src = "";
p = dst;
l = dst + max - 1;
while(p < l) {
if(!*src) break;
*p++ = *src++;
}
*p = 0;
}
return(dst);
}
u8 *mystrdup_simple(u8 *str) { // multiplatform compatible
int len;
u8 *o = NULL;
if(str) {
len = strlen(str);
o = malloc(len + 1);
if(!o) STD_ERR(QUICKBMS_ERROR_MEMORY);
memcpy(o, str, len + 1);
}
return o;
}
u8 *mystrdup(u8 **old_buff, u8 *str) { // multiplatform compatible
int len;
u8 *o = NULL;
if(old_buff) o = *old_buff;
if(str) {
len = strlen(str);
o = realloc(o, len + 1);
if(!o) STD_ERR(QUICKBMS_ERROR_MEMORY);
memcpy(o, str, len + 1);
}
if(old_buff) {
if(!o) {
FREE(*old_buff)
} else {
*old_buff = o;
}
}
return o;
}
u8 *mystrchrs(u8 *str, u8 *chrs) {
//int i;
u8 *p,
*ret = NULL;
if(str && chrs) {
for(p = str; *p; p++) {
if(strchr(chrs, *p)) return(p);
}
/*
for(i = 0; chrs[i]; i++) {
p = strchr(str, chrs[i]);
if(p && (!ret || (p < ret))) {
ret = p;
}
}
*/
}
return ret;
}
u8 *mystrrchrs(u8 *str, u8 *chrs) {
//int i;
u8 *p,
*ret = NULL;
if(str && chrs) {
for(p = str + strlen(str) - 1; p >= str; p--) {
if(strchr(chrs, *p)) return(p);
}
/*
for(i = 0; chrs[i]; i++) {
p = strrchr(str, chrs[i]);
if(p) {
str = p;
ret = p;
}
}
*/
}
return ret;
}
#define mystrstr stristr
#define mystrrstr strristr
/*
u8 *mystrstr(u8 *str, u8 *s) {
u8 *p;
if(str && s) {
for(p = str; *p; p++) {
if(!stricmp(p, s)) return(p);
}
}
return NULL;
}
u8 *mystrrstr(u8 *str, u8 *s) {
int slen;
u8 *p;
if(str && s) {
slen = strlen(s);
for(p = str + strlen(str) - slen; p >= str; p--) {
if(!stricmp(p, s)) return(p);
}
}
return NULL;
}
*/
// avoid NULL pointers
#define check_strcmp_args \
if(!a && !b) return 0; \
if(!a) return -1; \
if(!b) return 1;
i32 mystrcmp(const char *a, const char *b) {
check_strcmp_args
return real_strcmp(a, b);
}
i32 mystricmp(const char *a, const char *b) {
check_strcmp_args
return real_stricmp(a, b);
}
i32 mystrncmp(const char *a, const char *b, i32 n) {
check_strcmp_args
return real_strncmp(a, b, n);
}
i32 mystrnicmp(const char *a, const char *b, i32 n) {
check_strcmp_args
return real_strnicmp(a, b, n);
}
int check_is_dir(u8 *fname) {
struct stat xstat;
if(!fname) return 1;
if(stat(fname, &xstat) < 0) return 0;
if(!S_ISDIR(xstat.st_mode)) return 0;
return 1;
}
u8 *get_main_path(u8 *fname, u8 *argv0, u8 *output) {
static u8 fullname[PATHSZ + 1];
DWORD r;
u8 *p;
if(!output) output = fullname;
#ifdef WIN32
r = GetModuleFileName(NULL, output, PATHSZ);
if(!r || (r >= PATHSZ))
#endif
sprintf(output, "%.*s", PATHSZ, argv0);
if(check_is_dir(output)) return(output);
p = mystrrchrs(output, PATH_DELIMITERS);
if(fname) {
if(!p) p = output - 1;
sprintf(p + 1, "%.*s", PATHSZ - (p - output), fname);
} else {
if(p) *p = 0;
}
return(output);
}
int copycut_folder(u8 *input, u8 *output) {
u8 *p;
if(!output) return -1;
if(input) mystrcpy(output, input, PATHSZ);
if(check_is_dir(output)) return 0;
p = mystrrchrs(output, PATH_DELIMITERS);
if(!p) {
if(input) output[0] = 0;
} else {
*p = 0;
}
if(check_is_dir(output)) return 0;
return -1;
}
int get_my_endian(void) {
int endian = 1;
if(!*(char *)&endian) return MYBIG_ENDIAN; // big endian
return MYLITTLE_ENDIAN; // little endian
}
#ifdef WIN32
#define sWprintf swprintf
UINT_PTR CALLBACK OFN_DUMMY_HOOK(HWND hdlg, UINT uiMsg, WPARAM wParam, LPARAM lParam) {
return 0;
}
// Windows 8.1 has a bug that crashes quickbms if there is no hook,
// here I use dwMinorVersion >= 2 because GetVersionEx reports 2 for
// both 8 (which is safe) and 8.1.
// Note: now I use the manifest that returns the correct version (6.3)
#define get_file_work_around(ofn) \
if( \
((g_osver.dwMajorVersion >= 6) && (g_osver.dwMinorVersion >= 3)) \
|| (g_is_gui && !XDBG_ALLOC_ACTIVE) /* maybe the safe allocation has been disabled by the exception handler when restarted */ \
) { \
ofn.Flags |= OFN_ENABLEHOOK; \
ofn.lpfnHook = OFN_DUMMY_HOOK; \
} \
char *get_file(char *title, i32 bms, i32 multi) {
int maxlen;
char *filename;
if(multi) {
maxlen = MULTI_PATHSZ; // 32k limit ansi, no limit unicode
} else {
maxlen = PATHSZ;
}
filename = calloc(maxlen + 1, 1);
if(!filename) STD_ERR(QUICKBMS_ERROR_MEMORY);
printf("- %s\n", title);
#define _get_file(W,L) \
filename##W[0] = 0; \
memset(&ofn##W, 0, sizeof(ofn##W)); \
ofn##W.lStructSize = (g_osver.dwMajorVersion <= 4) ? OPENFILENAME_SIZE_VERSION_400 : sizeof(ofn##W); \
if(bms) { \
ofn##W.lpstrFilter = \
L## \
"script/plugin (bms/txt/wcx)\0" "*.bms;*.txt;*.wcx\0" \
/* "WCX plugin\0" "*.wcx\0" */ \
"(*.*)\0" "*.*\0" \
"\0" "\0"; \
} else { \
ofn##W.lpstrFilter = \
L## \
"(*.*)\0" "*.*\0" \
"\0" "\0"; \
} \
ofn##W.nFilterIndex = 1; \
ofn##W.lpstrFile = filename##W; \
ofn##W.nMaxFile = maxlen; \
ofn##W.lpstrTitle = title##W; \
ofn##W.Flags = OFN_PATHMUSTEXIST | \
OFN_FILEMUSTEXIST | \
OFN_LONGNAMES | \
OFN_EXPLORER | \
0x10000000 /*OFN_FORCESHOWHIDDEN*/ | \
OFN_ENABLESIZING | \
OFN_HIDEREADONLY | \
OFN_NOVALIDATE | \
0; \
\
if(multi) ofn##W.Flags |= OFN_ALLOWMULTISELECT; \
get_file_work_around(ofn##W) \
\
if(!GetOpenFileName##W(&ofn##W)) exit(1); // terminate immediately
if(g_osver.dwMajorVersion <= 4) {
// ANSI version
OPENFILENAME ofn;
_get_file(,)
} else {
// UNICODE version
OPENFILENAMEW ofnW;
wchar_t titleW[(strlen(title)+1) * sizeof(wchar_t)];
swprintf(titleW, L"%s", native_utf8_to_unicode(title));
wchar_t *filenameW = calloc(maxlen + 1, sizeof(wchar_t));
_get_file(W,L)
mystrcpy(filename, native_unicode_to_utf8(filenameW), maxlen + 1);
free(filenameW);
}
return(filename);
}
char *get_folder(char *title) {
char *p;
char *filename;
int maxlen = PATHSZ;
filename = malloc(maxlen + 1);
if(!filename) STD_ERR(QUICKBMS_ERROR_MEMORY);
printf("- %s\n", title);
#define _get_folder(W,L) \
s##W##printf(filename##W, L##"%s", L##"enter in the output folder and press Save"); \
memset(&ofn##W, 0, sizeof(ofn##W)); \
ofn##W.lStructSize = (g_osver.dwMajorVersion <= 4) ? OPENFILENAME_SIZE_VERSION_400 : sizeof(ofn##W); \
ofn##W.lpstrFilter = L## "(*.*)\0" "*.*\0" "\0" "\0"; \
ofn##W.nFilterIndex = 1; \
ofn##W.lpstrFile = filename##W; \
ofn##W.nMaxFile = maxlen; \
ofn##W.lpstrTitle = title##W; \
ofn##W.Flags = OFN_PATHMUSTEXIST | \
/* removed for folders OFN_FILEMUSTEXIST | */ \
OFN_LONGNAMES | \
OFN_EXPLORER | \
0x10000000 /*OFN_FORCESHOWHIDDEN*/ | \
OFN_ENABLESIZING | \
OFN_HIDEREADONLY | \
OFN_NOVALIDATE | \
0; \
\
get_file_work_around(ofn##W) \
\
if(!GetSaveFileName##W(&ofn##W)) exit(1); // terminate immediately
if(g_osver.dwMajorVersion <= 4) {
// ANSI version
OPENFILENAME ofn;
_get_folder(,)
} else {
// UNICODE version
OPENFILENAMEW ofnW;
wchar_t titleW[(strlen(title)+1) * sizeof(wchar_t)];
swprintf(titleW, L"%s", native_utf8_to_unicode(title));
wchar_t *filenameW = calloc(maxlen + 1, sizeof(wchar_t));
_get_folder(W,L)
mystrcpy(filename, native_unicode_to_utf8(filenameW), maxlen + 1);
free(filenameW);
}
p = mystrrchrs(filename, PATH_DELIMITERS);
if(p) *p = 0;
return(filename);
}
#undef sWprintf
#endif
int fgetz(u8 *data, int datalen, FILE *fd, u8 *fmt, ...) {
va_list ap;
u8 *p;
if(!data) return -1;
if(datalen <= 0) return -1;
if(fmt) {
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
}
data[0] = 0;
if(!fgets(data, datalen, fd)) {
if(fd == stdin) myexit(QUICKBMS_ERROR_UNKNOWN);
else return -1;
}
for(p = data; *p && (*p != '\r') && (*p != '\n'); p++);
*p = 0;
return(p - data);
}
QUICKBMS_int readbase(u8 *data, QUICKBMS_int size, QUICKBMS_int *readn) {
static const u8 table[256] = // fast performances
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\xff\xff\xff\xff\xff\xff"
"\xff\x0a\x0b\x0c\x0d\x0e\x0f\xff\xff\xff\xff\xff\xff\xff\xff\xff"
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
"\xff\x0a\x0b\x0c\x0d\x0e\x0f\xff\xff\xff\xff\xff\xff\xff\xff\xff"
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff";
int num = 0;
int sign;
u8 c,
*s,
*hex_fix;
s = data;
if(!data || !size || !data[0]) {
// do nothing (for readn)
} else {
// useful in some occasions, for example if the input is external!
for(; *s; s++) {
if(!strchr(" \t\r\n", *s)) break;
}
if(*s == '-') {
sign = -1;
s++;
} else {
sign = 0;
}
hex_fix = s;
for(; *s; s++) {
c = *s;
//if((c == 'x') || (c == 'X') || (c == '$')) { // auto base switching
if(
(((c == 'h') || (c == 'x') || (c == 'X')) && (s > hex_fix)) // 0x and 100h, NOT x123 or h123
|| (c == '$') // $1234 or 1234$
) {
size = 16;
continue;
}
c = table[c];
if(c >= size) break; // necessary to recognize the invalid chars based on the size
num = (num * size) + c;
}
if(sign) num = -num;
}
if(readn) *readn = s - data;
return(num);
}
int myisalnum(int chr) {
if((chr >= '0') && (chr <= '9')) return 1;
if((chr >= 'a') && (chr <= 'z')) return 1;
if((chr >= 'A') && (chr <= 'Z')) return 1;
if(chr == '-') return 1; // negative number
//if(chr == '+') return 1; // positive number
return 0;
}
int myishexdigit(int chr) {
if((chr >= 'A') && (chr <= 'F')) return 1;
if((chr >= 'a') && (chr <= 'f')) return 1;
if((chr >= '0') && (chr <= '9')) return 1; // this is enough because hex start ever with 0x
if(chr == '-') return 1; // negative number
//if(chr == '+') return 1; // positive number
//if(chr == '$') return 1; // delphi/vb hex
return 0;
}
int myisdigit(int chr) {
if((chr >= '0') && (chr <= '9')) return 1; // this is enough because hex start ever with 0x
if(chr == '-') return 1; // negative number
//if(chr == '+') return 1; // positive number
//if(chr == '$') return 1; // delphi/vb hex
return 0;
}
int myisdigitstr(u8 *str) { // only a quick version
int i;
if(!str) return 0;
if(!myisdigit(str[0])) return 0;
for(i = 1; str[i]; i++) {
if(i >= NUMBERSZ) return 0; // avoid to waste time with long strings
if(!strchr("0123456789abcdefABCDEFx$", str[i])) return 0;
}
return 1;
}
u8 *myitoa(int num) {
static const u8 table[] = "0123456789abcdef";
static u8 dstx[MULTISTATIC][3 + NUMBERSZ + 1] = {{""}};
static int dsty = 0;
u_int unum;
u8 tmp[NUMBERSZ + 1], // needed because hex numbers are inverted, I have already done various tests and this is the fastest!
*p, // even faster than using directly dst as output
*t,
*dst;
dst = (u8 *)dstx[dsty++ % MULTISTATIC];
if(!num) { // quick way, 0 is used enough often... ok it's probably useless
dst[0] = '0';
dst[1] = 0;
return(dst);
}
p = dst;
if(num < 0) {
num = -num;
*p++ = '-';
}
unum = num; // needed for the sign... many troubles
//if((unum >= 0) && (unum <= 9)) { // quick solution for numbers under 10, so uses only one char, (unum >= 0) avoids problems with 0x80000000
//*p++ = table[unum];
//*p = 0;
//return(dst);
//}
t = tmp + (NUMBERSZ - 1); // the -1 is needed (old tests)
*t = 0;
t--;
if(g_decimal_notation) {
do { // "unum" MUST be handled at the end of the cycle! example: 0
*t = table[unum % (u_int)10];
unum = unum / (u_int)10;
if(!unum) break;
t--;
} while(t >= tmp);
} else {
*p++ = '0'; // hex notation is better for debugging
*p++ = 'x';
do { // "unum" MUST be handled at the end of the cycle! example: 0
*t = table[unum & 15];
unum = unum >> (u_int)4;
if(!unum) break;
t--;
} while(t >= tmp);
}
strcpy(p, t);
//sprintf(dst, "%"PRId"", unum); // old "one-instruction-only" solution, mine is better
return(dst);
}
// it's all binary despite the name
u8 *strdupcpy(u8 *dst, int *dstlen, u8 *src, int srclen) {
int tmp;
if(srclen < 0) {
if(src) srclen = strlen(src);
else srclen = 0;
}
/*
// normal solution
//if(srclen < STRINGSZ) srclen = STRINGSZ; // disabled for testing
if(dstlen) *dstlen = srclen;
dst = realloc(dst, srclen + 2); // unicode
if(!dst) STD_ERR(QUICKBMS_ERROR_MEMORY);
// normal solution
*/
// optimized solution
if(!dstlen) {
dstlen = &tmp;
*dstlen = -1;
}
if(!dst || (*dstlen < srclen) || (*dstlen < 2)) { // NULL + unicode to avoid srclen 0
*dstlen = srclen;
//if(*dstlen == (u_int)-1LL) ALLOC_ERR; // note that dstlen can't be < 0 due to the "srclen < 0" check
//if(*dstlen == -2) ALLOC_ERR; // big endian undelimited unicode
if(*dstlen < STRINGSZ) *dstlen = STRINGSZ; // better for numbers and common filenames
dst = realloc(dst, (*dstlen) + 2); // big endian undelimited unicode (now it's rare but in future it may be more used)
if(!dst) STD_ERR(QUICKBMS_ERROR_MEMORY);
}
// optimized solution
if(dst) {
if(src) memcpy(dst, src, srclen);
else memset(dst, 0, srclen);
dst[srclen] = 0;
dst[srclen + 1] = 0; // big endian undelimited unicode
}
return(dst);
}
u8 *re_strdup(u8 **ret_dst, u8 *src, int *retlen) { // only for NULL delimited strings, NOT bytes!
u8 *dst;
if(ret_dst) dst = *ret_dst;
else dst = NULL;
dst = strdupcpy(dst, retlen, src, -1);
/*
int dstlen = -1;
// dst && src checked by strdupcpy
if(retlen) dstlen = *retlen;
dst = strdupcpy(dst, &dstlen, src, -1);
if(retlen) *retlen = dstlen;
*/
if(ret_dst) *ret_dst = dst;
return(dst);
}
int strdup_replace(u8 **dstp, u8 *src, int src_len, int *dstp_len) { // should improve a bit the performances
if(!dstp) return -1;
*dstp = strdupcpy(*dstp, dstp_len, src, src_len);
/*
int dst_len = -1;
u8 *dst;
if(!dstp) return -1;
dst = *dstp;
if(!dstp_len && dst) {
dst_len = strlen(dst); // or is it better to use "dst_len = 0"?
} else if(dstp_len) {
dst_len = *dstp_len;
}
dst = strdupcpy(dst, &dst_len, src, src_len);
*dstp = dst;
if(dstp_len) *dstp_len = dst_len;
*/
return 0;
}
int myisdechex_string(u8 *str) {
QUICKBMS_int len;
// I have already verified that using a quick test only on the first char doesn't improve the performances if compared to the current full check
if(!str) return 0;
readbase(str, 10, &len); // no need to know the number
if(len <= 0) return 0; // FALSE
if(len != strlen(str)) return 0; // otherwise there are huge problems with GetArray and If
// the downside is the lost of compatibility with rare things like 123;!
return 1; // TRUE
}
u16 swap16(u16 n) {
n = (((n & 0xff00) >> 8) |
((n & 0x00ff) << 8));
return(n);
}
u32 swap24(u32 n) {
n = (((n & 0xff0000) >> 16) |
((n & 0x00ff00) ) |
((n & 0x0000ff) << 16));
return(n);
}
u32 swap32(u32 n) {
n = (((n & 0xff000000) >> 24) |
((n & 0x00ff0000) >> 8) |
((n & 0x0000ff00) << 8) |
((n & 0x000000ff) << 24));
return(n);
}
u64 swap64(u64 n) {
//#ifdef QUICKBMS64
n = (((n & (u64)0xFF00000000000000ULL) >> (u64)56) |
((n & (u64)0x00FF000000000000ULL) >> (u64)40) |
((n & (u64)0x0000FF0000000000ULL) >> (u64)24) |
((n & (u64)0x000000FF00000000ULL) >> (u64) 8) |
((n & (u64)0x00000000FF000000ULL) << (u64) 8) |
((n & (u64)0x0000000000FF0000ULL) << (u64)24) |
((n & (u64)0x000000000000FF00ULL) << (u64)40) |
((n & (u64)0x00000000000000FFULL) << (u64)56));
//#else
// n = swap32(n);
//#endif
return(n);
}
u16 swap16le(u16 n) {
if(get_my_endian()) return swap16(n); // be cpu
return n; // le cpu
}
u16 swap16be(u16 n) {
if(get_my_endian()) return n; // be cpu
return swap16(n); // le cpu
}
u32 swap32le(u32 n) {
if(get_my_endian()) return swap32(n); // be cpu
return n; // le cpu
}
u32 swap32be(u32 n) {
if(get_my_endian()) return n; // be cpu
return swap32(n); // le cpu
}
u64 swap64le(u64 n) {
if(get_my_endian()) return swap64(n); // be cpu
return n; // le cpu
}
u64 swap64be(u64 n) {
if(get_my_endian()) return n; // be cpu
return swap64(n); // le cpu
}
u16 myhtons(u16 n) {
if(get_my_endian()) return(n);
return(swap16(n));
}
u16 myntohs(u16 n) {
if(get_my_endian()) return(n);
return(swap16(n));
}
u32 myhtonl(u32 n) {
if(get_my_endian()) return(n);
return(swap32(n));
}
u32 myntohl(u32 n) {
if(get_my_endian()) return(n);
return(swap32(n));
}
u8 *strristr(u8 *s1, u8 *s2) {
int s1n,
s2n;
u8 *p;
if(!s1 || !s2) return NULL;
s1n = strlen(s1);
s2n = strlen(s2);
if(s2n > s1n) return NULL;
for(p = s1 + (s1n - s2n); p >= s1; p--) {
if(!strnicmp(p, s2, s2n)) return(p);
}
return NULL;
}
int vspr(u8 **buff, u8 *fmt, va_list ap) {
int len,
mlen;
u8 *ret = NULL;
// NO, never! if(buff) *buff = NULL;
if(buff) ret = *buff;
if(!fmt) return 0;
mlen = strlen(fmt) + 128;
for(;;) {
ret = realloc(ret, mlen + 1);
if(!ret) return 0; // return -1;
len = vsnprintf(ret, mlen, fmt, ap);
if((len >= 0) && (len < mlen)) break;
mlen += 128;
}
ret[len] = 0;
if(buff) *buff = ret;
return len;
}
int spr(u8 **buff, u8 *fmt, ...) {
va_list ap;
int len;
va_start(ap, fmt);
len = vspr(buff, fmt, ap);
va_end(ap);
return len;
}
u8 *find_replace_string(u8 *buf, int *buflen, u8 *old, int oldlen, u8 *news, int newlen) {
int i,
len,
//len_bck,
tlen,
found;
u8 *nbuf,
*p;
if(!buf) return(buf);
found = 0;
len = -1;
if(buflen) len = *buflen;
if(len < 0) len = strlen(buf);
if(oldlen < 0) {
oldlen = 0;
if(old) oldlen = strlen(old);
}
tlen = len - oldlen;
//len_bck = len;
for(i = 0; i <= tlen; i++) {
if(!strnicmp(buf + i, old, oldlen)) found++;
}
if(!found) return(buf); // nothing to change: return buf or a positive value
//if(!news) return NULL; // if we want to know only if the searched string has been found, we will get NULL if YES and buf if NOT!!!
if(newlen < 0) {
newlen = 0;
if(news) newlen = strlen(news);
}
if(newlen <= oldlen) { // if the length of new string is equal/minor than the old one don't waste space for another buffer
nbuf = buf;
} else { // allocate the new size
nbuf = calloc(len + ((newlen - oldlen) * found) + 1, 1);
if(!nbuf) STD_ERR(QUICKBMS_ERROR_MEMORY);
}
p = nbuf;
for(i = 0; i <= tlen;) {
if(!strnicmp(buf + i, old, oldlen)) {
memcpy(p, news, newlen);
p += newlen;
i += oldlen;
} else {
*p++ = buf[i];
i++;
}
}
while(i < len) {
*p++ = buf[i];
i++;
}
len = p - nbuf;
if(buflen) *buflen = len;
nbuf[len] = 0; // hope the original input string has the +1 space
return(nbuf);
}
u8 *numbers_to_bytes(u8 *str, int *ret_size, int hex, int is_const) {
static int buffsz = 0;
static u8 *buff = NULL;
u_int num;
QUICKBMS_int len;
int i,
t,
size,
slash_fix;