-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.c
4434 lines (3881 loc) · 159 KB
/
file.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-2022 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 internal file operations (fdnum)
int myfseek(int fdnum, u_int offset, int type);
int myfr(int fdnum, u8 *data, int size, int quit_if_diff);
int myfw(int fdnum, u8 *data, int size);
int myfread(FILE *fd, u8 *data, int size, int quit_if_diff) {
int len;
if(size < 0) {
size = BUFFSZ;
quit_if_diff = 0;
}
if(data) {
len = fread(data, 1, size, fd);
} else {
len = size;
if(fseek(fd, size, SEEK_CUR) < 0) len = -1;
}
if((len != size) && quit_if_diff) {
if(g_continue_anyway) return -1;
myexit(QUICKBMS_ERROR_FILE_READ);
}
return len;
}
u64 myfilesize(int fdnum) {
if(fdnum < 0) {
CHECK_MEMNUM(0)
return(g_memory_file[myabs(fdnum)].size);
}
CHECK_FILENUM
if(g_filenumber[fdnum].fd) {
return filesize(g_filenumber[fdnum].fd);
}
// sockets and streams want the max signed value
if(g_filenumber[fdnum].pd) return(((u_int)(-1)) >> 1); // 0x7fffffff... return(((process_file_t *)g_filenumber[fdnum].pd)->size);
if(g_filenumber[fdnum].vd) return(((video_file_t *)g_filenumber[fdnum].vd)->size);
if(g_filenumber[fdnum].sd && (((socket_file_t *)g_filenumber[fdnum].sd)->datasz > 0)) return ((socket_file_t *)g_filenumber[fdnum].sd)->datasz;
return(((u_int)(-1)) >> 1); // 0x7fffffff...
}
u_int memfile_resize(memory_file_t *memfile, u_int offset) {
u_int oldsize = memfile->size;
memfile->size = offset;
if(offset <= oldsize) {
if(memfile->pos > memfile->size) memfile->pos = memfile->size;
} else {
myalloc(&memfile->data, memfile->size, &memfile->maxsize);
memset(memfile->data + oldsize, 0, memfile->size - oldsize);
}
return memfile->size;
}
u_int myftruncate(int fdnum, int fsize) {
if(fdnum < 0) {
CHECK_MEMNUM(0)
return memfile_resize(&g_memory_file[myabs(fdnum)], fsize);
}
CHECK_FILENUM
if(g_filenumber[fdnum].fd) {
FILE *fd = g_filenumber[fdnum].fd;
fflush(fd);
#ifdef WIN32
HANDLE fh;
fh = (HANDLE)_get_osfhandle(fileno(fd));
if(fh == INVALID_HANDLE_VALUE) return 0;
u_int oldoff = ftell(fd);
fseek(fd, fsize, SEEK_SET);
int ret = SetEndOfFile(fh);
fseek(fd, oldoff, SEEK_SET);
if(ret < 0) return 0;
return fsize;
#else
return ftruncate(fileno(fd), fsize);
#endif
}
if(myfseek(fdnum, fsize, SEEK_SET) < 0) return myfilesize(fdnum); // no support for enlarging "files"
if(g_filenumber[fdnum].sd) return(((socket_file_t *)g_filenumber[fdnum].sd)->datasz = fsize);
if(g_filenumber[fdnum].pd) return((u_int)(((process_file_t *)g_filenumber[fdnum].pd)->size = fsize));
//if(g_filenumber[fdnum].ad) return(((audio_file_t *) g_filenumber[fdnum].ad)->size = fsize);
if(g_filenumber[fdnum].vd) return(((video_file_t *) g_filenumber[fdnum].vd)->size = fsize);
if(g_filenumber[fdnum].md) return(((winmsg_file_t *) g_filenumber[fdnum].md)->size = fsize);
return 0;
}
FILE *get_fdnum_file_descriptor(int fdnum) {
if(fdnum >= 0) {
CHECK_FILENUM
if(g_filenumber[fdnum].fd) {
return g_filenumber[fdnum].fd;
}
}
return NULL;
}
u8 *QUICKBMS_CACHED_IO(int *ret_size) {
#define QUICKBMS_CACHED_IO_SIZE (512 * 1024) // this is probably the best size for optimal speed
static u8 *tmp = NULL;
if(!tmp) {
tmp = malloc(QUICKBMS_CACHED_IO_SIZE);
if(!tmp) STD_ERR(QUICKBMS_ERROR_MEMORY);
}
if(ret_size) *ret_size = QUICKBMS_CACHED_IO_SIZE;
return tmp;
}
#define TMP_FILE_READ(FREAD, FD, DO) \
int t, \
tmpsz; \
u8 *tmp = QUICKBMS_CACHED_IO(&tmpsz); \
\
for(len = 0; len < size; len += t) { \
t = tmpsz; \
if((size - len) < t) t = size - len; \
t = FREAD(FD, tmp, t, TRUE); \
if(t <= 0) { \
DO; \
}
int make_file_space(FILE *fd, int space) {
u_int offset;
int size;
// there are only two solutions:
// 1) read and write one byte per time
// 2) read whole data
// caching works but it may happen (don't know when) that our amount is smaller than expected and making a mess
offset = ftell(fd);
size = filesize(fd) - offset;
if(size <= 0) return 0;
static int tmpsz = 0;
static u8 *tmp = NULL;
myalloc(&tmp, size, &tmpsz);
myfread(fd, tmp, size, TRUE);
fseek(fd, offset + space, SEEK_SET);
if(fwrite(tmp, 1, size, fd) != size) return -1;
//FREE(tmp)
fseek(fd, offset, SEEK_SET);
return 0;
}
int file_compare(FILE *fd, u8 *data, int size) {
int len;
TMP_FILE_READ(myfread, fd, return -1)
if(memcmp(tmp, data + len, t)) {
return -1;
}
}
return 0;
}
int file_duplicate(FILE *fd, FILE *fdo, int size) {
int len;
TMP_FILE_READ(myfread, fd, return -1)
if(fwrite(tmp, 1, t, fdo) != t) {
return -1;
}
}
return 0;
}
// just experimental, written on the fly
void file_display(int mode, u_int fsize, u_int offset, int size, double entropy) {
#define file_display_XY 64
static double table [file_display_XY][file_display_XY];
static int table_n[file_display_XY][file_display_XY];
int i,
j;
if(mode < 0) {
for(i = 0; i < file_display_XY; i++) {
for(j = 0; j < file_display_XY; j++) {
table[i][j] = 0.0;
table_n[i][j] = 0;
}
}
} else if(mode > 0) {
fputc('\n', stdout);
for(i = 0; i < file_display_XY; i++) {
fputc(' ', stdout);
fputc(' ', stdout);
for(j = 0; j < file_display_XY; j++) {
double f = table[i][j];
if(table_n[i][j]) f /= (double)table_n[i][j];
// this is just experimental, probably wrong
if(f <= 0.0) {
fputc('.', stdout);
} else if(f <= 4.0) {
fputc('X', stdout); // 'X' or 'O'
} else {
fputc('#', stdout); // '#' or '@'
}
}
fputc('\n', stdout);
}
fputc('\n', stdout);
} else {
if(!fsize) return;
if(size <= 0) return;
if(offset > fsize) return;
// lame work-around for files smaller than 64*64
if(fsize < (u_int)(file_display_XY * file_display_XY)) {
offset *= (u_int)(file_display_XY * file_display_XY);
size *= (u_int)(file_display_XY * file_display_XY);
fsize *= (u_int)(file_display_XY * file_display_XY);
}
int chunk = fsize / (u_int)(file_display_XY * file_display_XY);
if(chunk <= 0) chunk = 1;
i32 idx_offset = offset / chunk;
i32 idx_size = size / chunk;
for(i = 0; i < file_display_XY; i++) {
for(j = 0; j < file_display_XY; j++) {
if(idx_offset <= 0) {
if(entropy == 0.0) entropy = 0.0001; // just to give something to visualize
table[i][j] += entropy;
table_n[i][j]++;
if(idx_size <= 0) return;
idx_size--;
}
idx_offset--;
}
}
}
}
int parsing_debug_sort(parsing_debug_t *a, parsing_debug_t *b) {
return (a->offset > b->offset);
}
u8 *myitoa_decimal_names(u_int num) {
static char ret[NUMBERSZ]; // used only once in a printf
sprintf(ret, g_decimal_names ? "%"PRIu : "%"PRIx, num);
return ret;
}
u8 *myitoa_decimal_notation(u_int num) {
static int flip = 0; // in case we use it twice in a printf
static char ret[2][NUMBERSZ];
flip = (flip + 1) & 1;
sprintf(ret[flip], g_decimal_notation ? "%"PRIu : "%"PRIx, num);
return ret[flip];
}
int fcoverage(int fdnum) {
memory_file_t *memfile;
filenumber_t *filez = NULL;
u_int coverage = 0,
fsize = 0,
offset = 0;
int perc = 0;
if(fdnum < 0) {
CHECK_MEMNUM(0)
memfile = &g_memory_file[myabs(fdnum)];
coverage = memfile->coverage;
fsize = myfilesize(fdnum);
offset = myftell(fdnum);
} else {
CHECK_FILENUMX //do NOT use CHECK_FILENUM because the file can be unexistent too!
filez = &g_filenumber[fdnum];
coverage = filez->coverage;
if(filez->fd) fsize = myfilesize(fdnum);
if(filez->fd) offset = myftell(fdnum);
}
if(fsize) { // avoids division by zero
perc = (u64)((u64)coverage * (u64)100) / (u64)fsize;
u8 perc_fix = ' ';
if((perc < 0) || (perc > 100)) {
perc = 100;
perc_fix = '!';
}
fprintf(stderr,
" coverage file %-3d %3d%%%c %-10s %-10s . offset %"PRIx"\n",
(i32)fdnum,
(i32)perc, perc_fix,
myitoa_decimal_notation(coverage),
myitoa_decimal_notation(fsize),
offset);
if(g_parsing_debug && fdnum_is_valid && filez && filez->fd) { // filez already assigned
parsing_debug_t *parsing_debug, *parsing_debug_tmp1, *parsing_debug_tmp2;
parsing_debug = real_calloc(1, sizeof(parsing_debug_t));
parsing_debug->offset = fsize;
parsing_debug->end_offset = fsize;
parsing_debug->entropy = 0.0;
CDL_APPEND(filez->parsing_debug, parsing_debug); // EOF
CDL_SORT(filez->parsing_debug, parsing_debug_sort);
i32 chunks = 0;
CDL_COUNT(filez->parsing_debug, parsing_debug, chunks);
if(chunks > 1) {
double entropy = 0.0;
u_int last_offset = 0,
next_offset;
u8 fname[64];
strcpy(fname, "QUICKBMS_DEBUG_FILE");
if(fdnum > 0) sprintf(fname + strlen(fname), "%d", (i32)fdnum);
FILE *fd = xfopen(fname, "wb");
if(fd) {
file_display(-1, fsize, 0, 0, 0.0);
CDL_FOREACH(filez->parsing_debug, parsing_debug) {
file_display(0, fsize, parsing_debug->offset, parsing_debug->end_offset - parsing_debug->offset, parsing_debug->entropy);
entropy += parsing_debug->entropy;
fseek(filez->fd, last_offset, SEEK_SET);
file_duplicate(filez->fd, fd, parsing_debug->offset - last_offset);
last_offset = parsing_debug->end_offset;
if(parsing_debug->next) {
if((parsing_debug->next)->offset < last_offset) last_offset = (parsing_debug->next)->offset;
}
}
CDL_FOREACH_SAFE(filez->parsing_debug, parsing_debug, parsing_debug_tmp1, parsing_debug_tmp2) {
CDL_DELETE(filez->parsing_debug, parsing_debug);
real_free(parsing_debug);
}
fprintf(stderr, " > %s parsed entropy %g\n", fname, entropy / (double)chunks);
fprintf(stderr, " %-10s bytes dumped from %d chunks\n", myitoa_decimal_notation((int)ftell(fd)/*cast from 64bit*/), chunks);
file_display(1, fsize, 0, 0, 0.0);
FCLOSE(fd);
fseek(filez->fd, offset, SEEK_SET); // restore
}
}
}
}
return perc;
}
int myfreset(int fdnum) {
memory_file_t *memfile;
if(fdnum < 0) {
CHECK_MEMNUM(0)
memfile = &g_memory_file[myabs(fdnum)];
memfile_resize(memfile, 0);
}
myfseek(fdnum, 0, SEEK_SET);
return 0;
}
int myfclose(int fdnum) {
memory_file_t *memfile;
filenumber_t *filez;
fcoverage(fdnum);
if(g_enable_hexhtml) hexhtml_build(fdnum);
if(fdnum < 0) {
CHECK_MEMNUM(0)
memfile = &g_memory_file[myabs(fdnum)];
// do NOT free memfile->data, it can be reused
memfile->pos = 0;
memfile->size = 0;
if(memfile->hexhtml) {
FREE(memfile->hexhtml)
memfile->hexhtml_size = 0;
}
} else {
CHECK_FILENUMX //do NOT use CHECK_FILENUM because the file can be unexistent too!
filez = &g_filenumber[fdnum];
if((g_reimport < 0) && filez->fd) {
// it should work with archives like those of UE4
if(filez->tail_toc_offset && (filez->tail_toc_size > 0)) {
static u8 *tmp = NULL;
u_int oldoff;
tmp = realloc(tmp, filez->tail_toc_size);
if(!tmp) STD_ERR(QUICKBMS_ERROR_MEMORY);
oldoff = myftell(fdnum);
myfseek(fdnum, filez->tail_toc_offset, SEEK_SET);
myfr(fdnum, tmp, filez->tail_toc_size, TRUE);
myfseek(fdnum, 0, SEEK_END);
myfw(fdnum, tmp, filez->tail_toc_size);
myfseek(fdnum, oldoff, SEEK_SET);
}
}
if(filez->fd) { FCLOSE(filez->fd); filez->fd = NULL; }
else if(filez->sd) { socket_close(filez->sd); filez->sd = NULL; }
else if(filez->pd) { process_close(filez->pd); filez->pd = NULL; }
else if(filez->ad) { audio_close(filez->ad); filez->ad = NULL; }
else if(filez->vd) { video_close(filez->vd); filez->vd = NULL; }
else if(filez->md) { winmsg_close(filez->md); filez->md = NULL; }
if(filez->hexhtml) {
FREE(filez->hexhtml)
filez->hexhtml_size = 0;
}
}
return 0;
}
// it's necessary to use a copy of fname and a way to free it automatically
u8 *dumpa_backup_fname(u8 *fname) {
static u8 *buff = NULL;
return re_strdup(&buff, fname, NULL);
}
i32 mydown_get_host(u8 *url, u8 **hostx, u16 *portx, u8 **urix, u8 **userx, u8 **passx, i32 verbose);
int fdnum_uses_filexor(int fdnum, filexor_t *fx) {
if((fx->size > 0) && ((fx->fd < -MAX_FILES) || (fx->fd == fdnum))) {
return 1;
}
return 0;
}
int fdnum_open(u8 *fname, int fdnum, int error) {
static u8 filedir[PATHSZ + 1];
socket_file_t *sockfile;
process_file_t *procfile;
audio_file_t *audiofile;
video_file_t *videofile;
winmsg_file_t *winmsgfile;
filenumber_t *filez;
u64 filesize;
u8 tmp[32],
*p,
*fullname = fname;
if(!fname) return 0;
if((fdnum < 0) || is_MEMORY_FILE(fname)) {
fprintf(stderr, "\n"
"Error: the filenumber field is minor than 0, if you want to use MEMORY_FILE\n"
" you don't need to \"reopen\" it in this way, just specify MEMORY_FILE\n"
" as filenumber in the various commands like:\n"
" get VAR long MEMORY_FILE\n");
myexit(QUICKBMS_ERROR_BMS);
} else if(fdnum >= MAX_FILES) {
fprintf(stderr, "\nError: the BMS script uses more files than how much supported by this tool\n");
myexit(QUICKBMS_ERROR_BMS);
}
filez = &g_filenumber[fdnum];
if(!fname[0]) { // flushing only
if(filez->fd) fflush(filez->fd); // flushing is a bad idea, anyway I allow to force it
return 0;
}
myfclose(fdnum);
// do NOT use memset to clear the structure
filez->bitchr = 0;
filez->bitpos = 0;
filez->bitoff = 0;
filez->coverage = 0;
xgetcwd(filedir, PATHSZ);
if(strchr(fname, ':') || (fname[0] == '/')) {
fprintf(stderr, "- open input file %s\n", fname);
} else {
fprintf(stderr, "- open input file %s%c%s\n", filedir, PATHSLASH, fname);
}
// alternative input/output
if(strstr(fname, "://")) {
sockfile = socket_open(fname);
if(sockfile) {
filez->sd = sockfile;
re_strdup(&filez->fullname, fname, NULL);
if((sockfile->proto == IPPROTO_HTTP) || (sockfile->proto == IPPROTO_HTTPS)) {
u8 *host = NULL;
u16 port = 0;
u8 *uri = NULL;
mydown_get_host(fname, &host, &port, &uri, NULL, NULL, 0);
fullname = uri;
p = mystrchrs(fullname, "?&");
if(p) *p = 0;
goto set_filez;
// ???
if(host) real_free(host);
if(uri) real_free(uri);
} else {
sprintf(tmp, "%u", sockfile->port);
filez->filename = realloc(filez->filename, strlen(sockfile->host) + 1 + strlen(tmp) + 1);
p = get_filename(sockfile->host);
if(sockfile->port && !strchr(fname, '/')) {
sprintf(filez->filename, "%s:%s", p, tmp);
} else {
sprintf(filez->filename, "%s", p);
}
p = mystrchrs(filez->filename, "?&");
if(p) *p = 0;
re_strdup(&filez->basename, sockfile->host, NULL);
re_strdup(&filez->fileext, tmp, NULL); // the port
}
return 0;
}
procfile = process_open(fname);
if(procfile) {
sprintf(tmp, "%u", (i32)procfile->pid);
re_strdup(&filez->fullname, fname, NULL);
filez->filename = realloc(filez->filename, strlen(procfile->name) + 1 + strlen(tmp) + 1);
sprintf(filez->filename, "%s:%s", procfile->name, tmp);
re_strdup(&filez->basename, procfile->name, NULL);
re_strdup(&filez->fileext, tmp, NULL);
filez->pd = procfile;
return 0;
}
audiofile = audio_open(fname);
if(audiofile) {
re_strdup(&filez->fullname, fname, NULL);
re_strdup(&filez->filename, audiofile->name, NULL);
re_strdup(&filez->basename, audiofile->name, NULL);
re_strdup(&filez->fileext, "", NULL);
filez->ad = audiofile;
return 0;
}
videofile = video_open(fname);
if(videofile) {
re_strdup(&filez->fullname, fname, NULL);
re_strdup(&filez->filename, videofile->name, NULL);
re_strdup(&filez->basename, videofile->name, NULL);
re_strdup(&filez->fileext, "", NULL);
filez->vd = videofile;
return 0;
}
winmsgfile = winmsg_open(fname);
if(winmsgfile) {
re_strdup(&filez->fullname, fname, NULL);
re_strdup(&filez->filename, winmsgfile->name, NULL);
re_strdup(&filez->basename, winmsgfile->name, NULL);
re_strdup(&filez->fileext, "", NULL);
filez->md = winmsgfile;
return 0;
}
}
if(g_write_mode) {
g_force_readwrite_mode = 1;
filez->fd = xfopen(fname, "r+b"); // do NOT modify, it must be both read/write
g_force_readwrite_mode = 0; // automatically resetted by xfopen but it's ok
if(!filez->fd) {
if(g_reimport) {
if(error) STD_ERR(QUICKBMS_ERROR_FILE_WRITE);
return -1;
} else {
if(!error) return -1;
fprintf(stderr, "\n"
"- the file %s doesn't exist.\n"
" Do you want to create it from scratch (y/N)?\n"
" ", fname);
if(get_yesno(NULL) == 'y') {
filez->fd = xfopen(fname, "w+b"); // do NOT create new files! Use log for that
}
if(!filez->fd) {
if(error) STD_ERR(QUICKBMS_ERROR_FILE_WRITE);
return -1;
}
}
}
//setbuf(filez->fd, NULL); // seems to cause only problems... mah
} else {
if(!strcmp(fname, "-")) {
filez->fd = stdin; // blah
} else {
filez->fd = xfopen(fname, "rb");
if(!filez->fd) {
if(error) STD_ERR(QUICKBMS_ERROR_FILE_READ);
return -1;
}
filez->temporary_file = 0;
if(!stricmp(filez->fullname, TEMPORARY_FILE)) {
filez->temporary_file = 1;
}
}
}
fseek(filez->fd, 0, SEEK_END);
filesize = ftell(filez->fd);
fseek(filez->fd, 0, SEEK_SET);
#ifndef QUICKBMS64
if(filesize > (u64)0xffffffffLL) {
fprintf(stderr, "\n"
"- the file is bigger than 4 gigabytes so it's NOT supported by quickbms.exe,\n"
" I suggest you to answer 'n' to the following question and using\n"
" quickbms_4gb_files.exe that doesn't have such limitation.\n"
" are you sure you want to continue anyway (y/N)?\n"
" ");
if(get_yesno(NULL) != 'y') {
if(g_continue_anyway) return -1;
myexit(QUICKBMS_ERROR_USER);
}
} else if(filesize > (u64)0x7fffffffLL) {
fprintf(stderr,
"- the file is bigger than 2 gigabytes, it should work correctly but contact me\n"
" or the author of the script in case of problems or invalid extracted files.\n"
" in case of any problem try to use quickbms_4gb_files.exe first\n");
}
#endif
if(g_enable_hexhtml) {
hexhtml_init(fdnum, filesize);
}
// filesize
//filez->filesize = filesize;
if(filez->temporary_file) {
if(filez->filename) {
goto skip_set_filez; // keep the old references
}
}
// fullname
re_strdup(&filez->fullname, get_fullpath_from_name(fname, 0), NULL); // allocate
fullname = filez->fullname;
set_filez:
// filename
p = get_filename(fullname);
re_strdup(&filez->filename, p, NULL);
// basename
re_strdup(&filez->basename, filez->filename, NULL); // allocate
p = strrchr(filez->basename, '.');
if(p) *p = 0;
// extension
p = get_extension(filez->filename);
re_strdup(&filez->fileext, p, NULL);
// filepath
re_strdup(&filez->filepath, fullname, NULL); // allocate
p = mystrrchrs(filez->filepath, PATH_DELIMITERS);
if(!p) p = filez->filepath;
*p = 0;
// fullbasename
re_strdup(&filez->fullbasename, fullname, NULL); // allocate
p = mystrrchrs(filez->fullbasename, PATH_DELIMITERS);
if(!p) p = filez->fullbasename;
p = strrchr(p, '.');
if(p) *p = 0;
skip_set_filez:
// basically they are used for every operation but it's better if they get reinitialized when fdnum 0 is open,
// the alternative is calling myfseek(fdnum, 0, SEEK_SET) which is probably very time consuming,
// another possible alternative is: post_fseek_actions(fdnum, QUICKBMS_MIN_INT)
if(fdnum_uses_filexor(fdnum, &g_filexor)) *g_filexor.pos = 0;
if(fdnum_uses_filexor(fdnum, &g_filerot)) *g_filerot.pos = 0;
if(fdnum_uses_filexor(fdnum, &g_filecrypt)) *g_filecrypt.pos = 0;
if(!fdnum) {
if(g_mex_default) add_var(BytesRead_idx, NULL, NULL, 0, sizeof(int));
}
if(g_mex_default && !fdnum) g_mex_default_init(1);
return 0;
}
u_int myftell(int fdnum) {
if(fdnum < 0) {
CHECK_MEMNUM(0)
return(g_memory_file[myabs(fdnum)].pos);
}
CHECK_FILENUM
if(g_filenumber[fdnum].fd) return(ftell(g_filenumber[fdnum].fd));
if(g_filenumber[fdnum].sd) return(((socket_file_t *)g_filenumber[fdnum].sd)->pos);
if(g_filenumber[fdnum].pd) return((u_int)(((process_file_t *)g_filenumber[fdnum].pd)->pos));
if(g_filenumber[fdnum].ad) return(((audio_file_t *) g_filenumber[fdnum].ad)->pos);
if(g_filenumber[fdnum].vd) return(((video_file_t *) g_filenumber[fdnum].vd)->pos);
if(g_filenumber[fdnum].md) return(((winmsg_file_t *) g_filenumber[fdnum].md)->pos);
fprintf(stderr, "\n"
"Error: I forgot to implement the myftell operation for this file type\n"
" contact me!\n");
myexit(QUICKBMS_ERROR_BMS);
return 0;
}
void bytesread_eof(int fdnum, int len) {
if(!g_mex_default) return; // just in case...
int oldoff = 0;
if(!fdnum) {
oldoff = get_var32(BytesRead_idx);
oldoff += len;
if(oldoff < 0) oldoff = 0;
add_var(BytesRead_idx, NULL, NULL, oldoff, sizeof(int));
if(myftell(fdnum) >= myfilesize(fdnum)) {
//if(myfeof(fdnum)) { // feof doesn't work
add_var(NotEOF_idx, NULL, NULL, 0, sizeof(int));
}
}
}
void post_fseek_actions(int fdnum, int diff_offset) {
#define post_fseek_actions_do(X) { \
(*X) += diff_offset; \
/*if((*X) < 0) (*X) = 0;*/ \
}
if(fdnum_uses_filexor(fdnum, &g_filexor)) post_fseek_actions_do(g_filexor.pos)
if(fdnum_uses_filexor(fdnum, &g_filerot)) post_fseek_actions_do(g_filerot.pos)
if(fdnum_uses_filexor(fdnum, &g_filecrypt)) post_fseek_actions_do(g_filecrypt.pos)
if(g_mex_default) bytesread_eof(fdnum, diff_offset);
}
int myfeof(int fdnum) {
memory_file_t *memfile = NULL;
int ret = 0;
if(fdnum < 0) {
CHECK_MEMNUM(0)
memfile = &g_memory_file[myabs(fdnum)];
if(memfile->pos >= memfile->size) {
ret = 1;
}
} else {
CHECK_FILENUM
if(g_filenumber[fdnum].fd) ret = feof(g_filenumber[fdnum].fd);
// ret is already 0 for the others
}
return ret;
}
u8 *myfdata(int fdnum) {
memory_file_t *memfile = NULL;
if(fdnum < 0) {
CHECK_MEMNUM(0)
memfile = &g_memory_file[myabs(fdnum)];
return memfile->data;
} else {
CHECK_FILENUM
return NULL;
}
return NULL;
}
void post_fread_actions(int fdnum, u8 *data, int size) {
int i;
// fdnum is used only for bytesread_eof so ignore it
//if(!data) not needed here
if(fdnum_uses_filexor(fdnum, &g_filexor)) {
for(i = 0; i < size; i++) {
data[i] ^= g_filexor.key[(*g_filexor.pos) % g_filexor.size];
(*g_filexor.pos)++;
}
}
if(fdnum_uses_filexor(fdnum, &g_filerot)) {
for(i = 0; i < size; i++) {
data[i] += g_filerot.key[(*g_filerot.pos) % g_filerot.size];
(*g_filerot.pos)++;
}
}
if(fdnum_uses_filexor(fdnum, &g_filecrypt)) {
perform_encryption_and_crchash(data, size);
}
if(g_mex_default) bytesread_eof(fdnum, size);
}
// necessary for nameless files that read some bytes messing the correct coverage
int myfr_remove_coverage(int fdnum, int len) {
len = -len; // just for an easy copy&paste from myfr()
memory_file_t *memfile = NULL;
if(fdnum < 0) {
CHECK_MEMNUM(0)
memfile = &g_memory_file[myabs(fdnum)];
memfile->coverage += len;
} else {
CHECK_FILENUMX
g_filenumber[fdnum].coverage += len;
}
return 0;
}
int myfr(int fdnum, u8 *data, int size, int quit_if_diff) {
memory_file_t *memfile = NULL;
int len = 0;
//quit_if_diff = 1;
// if(!data) not necessary
if(size < 0) {
size = BUFFSZ;
quit_if_diff = 0;
}
if(fdnum < 0) {
CHECK_MEMNUM(size) // fake writer
memfile = &g_memory_file[myabs(fdnum)];
if(!memfile->data) {
if(myabs(fdnum) == 1) {
fprintf(stderr, "\nError: MEMORY_FILE has not been used/declared yet\n");
} else {
fprintf(stderr, "\nError: MEMORY_FILE%d has not been used/declared yet\n", (i32)myabs(fdnum));
}
myexit(QUICKBMS_ERROR_BMS);
}
len = size;
if((memfile->pos + size) > memfile->size) {
len = memfile->size - memfile->pos;
}
memcpy(data, memfile->data + memfile->pos, len);
memfile->pos += len;
memfile->coverage += len;
} else {
CHECK_FILENUM
if(g_filenumber[fdnum].fd) {
len = fread(data, 1, size, g_filenumber[fdnum].fd);
if(g_write_mode) {
/*
in "r+b" mode the offsets are not synchronized so happens horrible things like:
- read 7 bytes, write 7 bytes... from offset 0 instead of 7
- file of 12 bytes, read 7, read 4, write 7... fails because can't increase size
the following lame solution works perfectly and solves the problem
*/
fseek(g_filenumber[fdnum].fd, ftell(g_filenumber[fdnum].fd), SEEK_SET);
}
}
else if(g_filenumber[fdnum].sd) len = socket_read( g_filenumber[fdnum].sd, data, size);
else if(g_filenumber[fdnum].pd) len = process_read( g_filenumber[fdnum].pd, data, size);
else if(g_filenumber[fdnum].ad) len = audio_read( g_filenumber[fdnum].ad, data, size);
else if(g_filenumber[fdnum].vd) len = video_read( g_filenumber[fdnum].vd, data, size);
else if(g_filenumber[fdnum].md) len = winmsg_read( g_filenumber[fdnum].md, data, size);
else {
fprintf(stderr, "\n"
"Error: I forgot to implement the myfr operation for this file type\n"
" contact me!\n");
myexit(QUICKBMS_ERROR_BMS);
}
if(len < 0) len = 0; // some functions may return a -1 error
if(g_enable_hexhtml) hexhtml_add(fdnum, data, len);
g_filenumber[fdnum].coverage += len;
}
if(g_parsing_debug && fdnum_is_valid && g_filenumber[fdnum].fd) {
parsing_debug_t *parsing_debug = real_calloc(1, sizeof(parsing_debug_t));
parsing_debug->end_offset = myftell(fdnum);
parsing_debug->offset = parsing_debug->end_offset - len;
parsing_debug->entropy = calculate_entropy(data, len, NULL);
CDL_APPEND(g_filenumber[fdnum].parsing_debug, parsing_debug);
}
if((len != size) && quit_if_diff) {
u8 *fullname = "";
if(fdnum_is_valid && g_filenumber[fdnum].fullname) fullname = g_filenumber[fdnum].fullname;
fprintf(stderr, "\n"
"Error: incomplete input file %d: %s\n"
" Can't read %s bytes from offset %"PRIx".\n"
" Anyway don't worry, it's possible that the BMS script has been written\n"
" to exit in this way if it's reached the end of the archive so check it\n"
" or contact its author or verify that all the files have been extracted.\n"
" Please check the following coverage information to know if it's ok.\n"
"\n",
(i32)fdnum,
fullname,
myitoa_decimal_notation(size - len),
myftell(fdnum));
fcoverage(fdnum);
if(g_continue_anyway) return -1;
myexit(QUICKBMS_ERROR_FILE_READ);
}
post_fread_actions(fdnum, data, len);
return len;
}
// copied from dumpa_memory_file, experimental
int fdnum_append_mode(memory_file_t *memfile, FILE *fd, int size) {
if(g_append_mode == APPEND_MODE_NONE) return -1;
int cmd = g_last_cmd;
if(
(CMD.type != CMD_Put)
&& (CMD.type != CMD_PutDString)
&& (CMD.type != CMD_PutCT)
&& (CMD.type != CMD_PutBits) // ???
) return -1;
if(memfile) {
if(g_append_mode == APPEND_MODE_APPEND) {
memfile->pos = memfile->size;
if((memfile->size + size) < memfile->size) ALLOC_ERR;
memfile->size += size;
myalloc(&memfile->data, memfile->size, &memfile->maxsize);
} else if(g_append_mode == APPEND_MODE_OVERWRITE) {
// allow goto to decide where placing the new content
if((memfile->size + size) < memfile->size) ALLOC_ERR;
if((memfile->pos + size) > memfile->size) {
memfile->size = memfile->pos + size;
myalloc(&memfile->data, memfile->size, &memfile->maxsize);
}
} else if(g_append_mode == APPEND_MODE_BEFORE) {
memfile->pos = 0;
if((memfile->size + size) < memfile->size) ALLOC_ERR;
memfile->size += size;
myalloc(&memfile->data, memfile->size, &memfile->maxsize);
mymemmove(memfile->data + size, memfile->data, memfile->size - size);
} else if(g_append_mode == APPEND_MODE_INSERT) {
if((memfile->size + size) < memfile->size) ALLOC_ERR;
memfile->size += size;
myalloc(&memfile->data, memfile->size, &memfile->maxsize);
mymemmove(memfile->data + memfile->pos + size, memfile->data + memfile->pos, memfile->size - (memfile->pos + size));
}
} else if(fd) {
u_int fd_pos = ftell(fd);
u_int fd_size = filesize(fd);
if(g_append_mode == APPEND_MODE_APPEND) {
fseek(fd, 0, SEEK_END);
fd_pos = fd_size;
if((fd_size + size) < fd_size) ALLOC_ERR;