-
Notifications
You must be signed in to change notification settings - Fork 7
/
file.c
executable file
·2917 lines (2596 loc) · 106 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-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 internal file operations (fdnum)
u64 myfilesize(int fdnum) {
struct stat xstat;
if(fdnum < 0) {
return(g_memory_file[-fdnum].size);
}
CHECK_FILENUM
if(g_filenumber[fdnum].fd) {
xstat.st_size = 0;
fstat(fileno(g_filenumber[fdnum].fd), &xstat);
return(xstat.st_size);
}
// 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);
return(((u_int)(-1)) >> 1); // 0x7fffffff...
}
int fcoverage(int fdnum) {
memory_file_t *memfile;
filenumber_t *filez;
u_int coverage = 0,
fsize = 0,
offset = 0;
int perc = 0;
if(fdnum < 0) {
memfile = &g_memory_file[-fdnum];
coverage = memfile->coverage;
fsize = myfilesize(fdnum);
offset = myftell(fdnum);
} else {
//CHECK_FILENUM //do NOT use it 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;
fprintf(stderr,
" coverage file %-3d %3d%% %-10"PRIu" %-10"PRIu" . offset %"PRIx"\n",
(i32)fdnum,
(i32)perc,
coverage,
fsize,
offset);
}
return perc;
}
int myfclose(int fdnum) {
memory_file_t *memfile;
filenumber_t *filez;
fcoverage(fdnum);
if(g_enable_hexhtml) hexhtml_build(fdnum);
if(fdnum < 0) {
memfile = &g_memory_file[-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_FILENUM //do NOT use it because the file can be unexistent too!
filez = &g_filenumber[fdnum];
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;
}
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;
if(!fname) return 0;
if((fdnum < 0) || !strnicmp(fname, MEMORY_FNAME, MEMORY_FNAMESZ)) {
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) {
sprintf(tmp, "%u", sockfile->port);
re_strdup(&filez->fullname, fname, NULL);
filez->filename = realloc(filez->filename, strlen(sockfile->host) + 1 + strlen(tmp) + 1);
sprintf(filez->filename, "%s:%s", sockfile->host, tmp);
re_strdup(&filez->basename, sockfile->host, NULL);
re_strdup(&filez->fileext, tmp, NULL);
filez->sd = sockfile;
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) {
filez->fd = xfopen(fname, "r+b"); // do NOT modify, it must be both read/write
if(!filez->fd) {
if(g_reimport) {
if(error) STD_ERR(QUICKBMS_ERROR_FILE_WRITE);
return -1;
} else {
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;
}
}
}
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,\n"
" I suggest you to answer N to the following question and using\n"
" quickbms_4gb_files.exe that has no limitations.\n"
" are you sure you want to continue in any case (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 problems try to use quickbms_4gb_files.exe\n");
}
#endif
if(g_enable_hexhtml) {
hexhtml_init(fdnum, filesize);
}
// filesize
//filez->filesize = filesize;
// fullname
p = get_fullpath_from_name(fname);
re_strdup(&filez->fullname, p, NULL); // allocate
FREE(p)
// filename
p = get_filename(filez->fullname);
re_strdup(&filez->filename, p, NULL);
// prev_basename
re_strdup(&filez->prev_basename, filez->basename, NULL); // allocate
// 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, filez->fullname, NULL); // allocate
p = mystrrchrs(filez->filepath, PATH_DELIMITERS);
if(!p) p = filez->filepath;
*p = 0;
// fullbasename
re_strdup(&filez->fullbasename, filez->fullname, NULL); // allocate
p = mystrrchrs(filez->fullbasename, PATH_DELIMITERS);
if(!p) p = filez->fullbasename;
p = strrchr(p, '.');
if(p) *p = 0;
if(g_mex_default && !fdnum) g_mex_default_init(1);
return 0;
}
u_int myftell(int fdnum) {
if(fdnum < 0) {
return(g_memory_file[-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) {
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(g_file_xor_size) post_fseek_actions_do(g_file_xor_pos)
if(g_file_rot_size) post_fseek_actions_do(g_file_rot_pos)
if(g_file_crypt_size) post_fseek_actions_do(g_file_crypt_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) {
memfile = &g_memory_file[-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;
}
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(g_file_xor_size) {
for(i = 0; i < size; i++) {
data[i] ^= g_file_xor[(*g_file_xor_pos) % g_file_xor_size];
(*g_file_xor_pos)++;
}
}
if(g_file_rot_size) {
for(i = 0; i < size; i++) {
data[i] += g_file_rot[(*g_file_rot_pos) % g_file_rot_size];
(*g_file_rot_pos)++;
}
}
if(g_file_crypt_size) {
perform_encryption(data, size);
}
if(g_mex_default) bytesread_eof(fdnum, size);
}
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) {
memfile = &g_memory_file[-fdnum];
if(!memfile->data) {
fdnum = -fdnum;
if(fdnum == 1) {
fprintf(stderr, "\nError: in this script MEMORY_FILE has not been used/declared yet\n");
} else {
fprintf(stderr, "\nError: in this script MEMORY_FILE%d has not been used/declared yet\n", (i32)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((len != size) && quit_if_diff) {
fprintf(stderr, "\n"
"Error: incomplete input file %d: %s\n"
" Can't read %"PRIu" 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,
g_filenumber[fdnum].fullname ? g_filenumber[fdnum].fullname : (u8 *)"",
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;
}
int myfw(int fdnum, u8 *data, int size) {
memory_file_t *memfile = NULL;
int len = 0,
tmp;
// if(!data) not necessary
if(size < 0) {
fprintf(stderr, "\n"
"Error: problems with input file number %d, can't write negative size.\n"
"\n", (i32)fdnum);
if(g_continue_anyway) return -1;
myexit(QUICKBMS_ERROR_FILE_WRITE);
}
post_fread_actions(-1, data, size);
if(fdnum < 0) {
memfile = &g_memory_file[-fdnum];
if(!memfile->data) {
fdnum = -fdnum;
if(fdnum == 1) {
fprintf(stderr, "\nError: in this script MEMORY_FILE has not been used/declared yet\n");
} else {
fprintf(stderr, "\nError: in this script MEMORY_FILE%d has not been used/declared yet\n", (i32)fdnum);
}
myexit(QUICKBMS_ERROR_BMS);
}
len = size;
tmp = memfile->pos + len;
if((tmp < memfile->pos) || (tmp < len)) ALLOC_ERR;
if(tmp > memfile->size) {
memfile->size = tmp;
myalloc(&memfile->data, memfile->size, &memfile->maxsize);
}
memcpy(memfile->data + memfile->pos, data, len);
memfile->pos += len;
} else {
CHECK_FILENUM
if(g_filenumber[fdnum].fd) {
// seems impossible but if you use the following script it will give no error
// get DUMMY long # if you remove this line it will work
// put 1234 long
// so, also for better security, I have added the -w check directly here
if(g_write_mode) {
len = fwrite(data, 1, size, g_filenumber[fdnum].fd);
fflush(g_filenumber[fdnum].fd);
}
}
else if(g_filenumber[fdnum].sd) len = socket_write( g_filenumber[fdnum].sd, data, size);
else if(g_filenumber[fdnum].pd) len = process_write( g_filenumber[fdnum].pd, data, size);
else if(g_filenumber[fdnum].ad) len = audio_write( g_filenumber[fdnum].ad, data, size);
else if(g_filenumber[fdnum].vd) len = video_write( g_filenumber[fdnum].vd, data, size);
else if(g_filenumber[fdnum].md) len = winmsg_write( g_filenumber[fdnum].md, data, size);
else {
fprintf(stderr, "\n"
"Error: I forgot to implement the myfw operation for this file type\n"
" contact me!\n");
myexit(QUICKBMS_ERROR_BMS);
}
}
if(len != size) {
fprintf(stderr, "\n"
"Error: problems with input file number %d, can't write %"PRIu" bytes.\n"
"%s"
"\n", (i32)fdnum, size - len,
g_write_mode ? "" : "\n you MUST use the -w option for enabling the file writing mode\n");
if(g_continue_anyway) return -1;
myexit(QUICKBMS_ERROR_FILE_WRITE);
}
return len;
}
int myfgetc(int fdnum) {
int c;
u8 buff[1];
c = myfr(fdnum, buff, 1, TRUE);
if(c <= 0) return -1;
return(buff[0]);
}
int myfputc(int c, int fdnum) {
int ret;
u8 buff[1];
buff[0] = c;
ret = myfw(fdnum, buff, 1);
if(ret < 0) return ret;
return(c);
}
int myfseek_stream(int fdnum, u_int offset) {
int i;
for(i = 0; i < offset; i++) {
if(myfgetc(fdnum) < 0) return -1;
}
return 0;
}
int myfseek(int fdnum, u_int offset, int type) {
memory_file_t *memfile = NULL;
u_int oldoff,
oldsize;
int err = 0;
if(type == SEEK_END) {
if((int)offset > 0) offset = -offset;
}
oldoff = myftell(fdnum);
oldsize = myfilesize(fdnum);
if(fdnum < 0) {
memfile = &g_memory_file[-fdnum];
switch(type) {
case SEEK_SET: memfile->pos = offset; break;
case SEEK_CUR: memfile->pos += offset; break;
case SEEK_END: memfile->pos = memfile->size + offset; break;
default: break;
}
if(memfile->pos < 0) memfile->pos = 0;
if(memfile->pos > memfile->size) {
if(g_append_mode) {
if(g_append_mode == APPEND_MODE_APPEND) {
memfile->pos = memfile->size;
} else {
// allocate space
memfile->size = memfile->pos;
if(memfile->size > memfile->maxsize) {
memfile->maxsize = memfile->size;
if(memfile->maxsize == -1) ALLOC_ERR;
memfile->data = realloc(memfile->data, memfile->maxsize + 1);
if(!memfile->data) STD_ERR(QUICKBMS_ERROR_MEMORY);
memfile->data[memfile->maxsize] = 0;
}
memset(memfile->data + oldsize, 0, memfile->size - oldsize);
}
} else {
err = -1;
}
}
} else {
CHECK_FILENUM
if(g_filenumber[fdnum].fd) {
if(type == SEEK_SET) {
err = fseek(g_filenumber[fdnum].fd, offset, type);
} else { // signed
err = fseek(g_filenumber[fdnum].fd, (int)offset, type);
}
// if(g_append_mode)
// there is probably no problem in reserving space in a file when it's used "w+b"
} else if(g_filenumber[fdnum].sd) {
err = myfseek_stream(fdnum, offset); // SEEK_CUR
} else if(g_filenumber[fdnum].pd) {
switch(type) {
case SEEK_SET: ((process_file_t *)g_filenumber[fdnum].pd)->pos = (void *)offset; break;
case SEEK_CUR: ((process_file_t *)g_filenumber[fdnum].pd)->pos += offset; break;
case SEEK_END: ((process_file_t *)g_filenumber[fdnum].pd)->pos = ((process_file_t *)g_filenumber[fdnum].pd)->base + ((process_file_t *)g_filenumber[fdnum].pd)->size + offset; break;
default: break;
}
} else if(g_filenumber[fdnum].ad) {
err = myfseek_stream(fdnum, offset); // SEEK_CUR
} else if(g_filenumber[fdnum].vd) {
err = myfseek_stream(fdnum, offset); // SEEK_CUR
} else if(g_filenumber[fdnum].md) {
err = myfseek_stream(fdnum, offset); // SEEK_CUR
} else {
fprintf(stderr, "\n"
"Error: I forgot to implement the myfseek operation for this file type\n"
" contact me!\n");
myexit(QUICKBMS_ERROR_BMS);
}
}
if(err) {
fprintf(stderr, "\nError: [myfseek] the offset 0x%"PRIx" in the file %d can't be reached\n", offset, (i32)fdnum);
if(g_continue_anyway) return -1;
myexit(QUICKBMS_ERROR_FILE_READ);
}
post_fseek_actions(fdnum, myftell(fdnum) - oldoff);
return 0;
}
int getxx(u8 *tmp, int bytes) {
u_int num;
int i;
if(!tmp) return 0;
num = 0;
for(i = 0; i < bytes; i++) {
if(g_endian == MYLITTLE_ENDIAN) {
if(i >= (int)sizeof(num)) continue;
num |= ((u_int)tmp[i] << (u_int)(i << (u_int)3));
} else {
if(i < (bytes - (int)sizeof(num))) continue;
num |= ((u_int)tmp[i] << (u_int)((bytes - (u_int)1 - i) << (u_int)3));
}
}
return(num);
}
int putxx(u8 *data, u_int num, int bytes) {
int i;
if(!data) return 0;
for(i = 0; i < bytes; i++) {
if(g_endian == MYLITTLE_ENDIAN) {
if(i < (int)sizeof(num)) data[i] = num >> (i << (u_int)3);
else data[i] = 0;
} else {
if(i >= (bytes - (int)sizeof(num))) data[i] = num >> ((bytes - (u_int)1 - i) << (u_int)3);
else data[i] = 0;
}
}
return(bytes);
}
int fputxx(int fdnum, int num, int bytes) {
u8 tmp[bytes];
// if(!fd) do nothing, modify mywr
putxx(tmp, num, bytes);
return(myfw(fdnum, tmp, bytes));
}
int fgetxx(int fdnum, int bytes, int *error) {
int tmp_error;
int ret;
u8 tmp[bytes];
if(!error) error = &tmp_error;
*error = 0;
// if(!fd) do nothing, modify myfr
ret = myfr(fdnum, tmp, bytes, TRUE);
if(ret < 0) {
*error = 1;
return -1;
}
ret = getxx(tmp, bytes);
if(g_endian_killer) { // reverse endianess
g_endian = (g_endian == MYLITTLE_ENDIAN) ? MYBIG_ENDIAN : MYLITTLE_ENDIAN;
myfseek(fdnum, -bytes, SEEK_CUR);
fputxx(fdnum, ret, bytes);
g_endian = (g_endian == MYLITTLE_ENDIAN) ? MYBIG_ENDIAN : MYLITTLE_ENDIAN;
}
return ret;
}
// how the bits reading works:
// the idea is having something that doesn't occupy much space in the file arrays (6 bytes per file)
// and that is not touched by the other functions to avoid to loose performances for a rarely used
// function so I have used the following fields:
// bitchr = the current byte read from the file
// bitpos = the amount of bits of bitchr that have been consumed (3 bits)
// bitoff = the current offset, it's necessary to know if in the meantime
// the user has changed offset and so bitpos must be resetted
u_int fd_read_bits(u_int bits, u8 *bitchr, u8 *bitpos, int fd) {
u_int ret = 0;
int i,
t;
u8 bc = 0,
bp = 0;
if(bitchr) bc = *bitchr;
if(bitpos) bp = *bitpos;
//if(bits > 32) return 0; // it's already called only for max 32 bits
(bp) &= 7; // just for security
for(i = 0; i < bits; i++) {
if(!bp) {
t = myfgetc(fd);
bc = (t < 0) ? 0 : t;
}
if(g_endian == MYLITTLE_ENDIAN) { // uhmmm I don't think it's very fast... but works
ret = (ret >> (u_int)1) | (u_int)((((u_int)bc >> (u_int)bp) & (u_int)1) << (u_int)(bits - 1));
} else {
ret = (ret << (u_int)1) | (u_int)((((u_int)bc << (u_int)bp) >> (u_int)7) & (u_int)1);
}
(bp)++;
(bp) &= 7; // leave it here
}
if(bitchr) *bitchr = bc;
if(bitpos) *bitpos = bp;
return ret;
}
int fd_write_bits(u_int num, u_int bits, u8 *bitchr, u8 *bitpos, int fd) {
int i,
t,
bit,
rem = 0;
u8 bc = 0,
bp = 0;
if(bitchr) bc = *bitchr;
if(bitpos) bp = *bitpos;
//if(bits > 32) return 0; // it's already called only for max 32 bits
(bp) &= 7; // just for security
for(i = 0; i < bits; i++) {
if(!bp) {
if(rem) {
myfseek(fd, -1, SEEK_CUR);
myfputc(bc, fd);
rem = 0;
}
t = myfgetc(fd);
if(t < 0) {
bc = 0;
myfputc(bc, fd);
} else {
bc = t;
}
}
if(g_endian == MYLITTLE_ENDIAN) { // uhmmm I don't think it's very fast... but works
t = (u_int)1 << (u_int)bp;
bit = (num >> (u_int)i) & (u_int)1;
} else {
t = (u_int)1 << (u_int)(7 - bp);
bit = (num >> (u_int)((bits - i) - 1)) & 1;
}
if(bit) {
bc |= t; // put 1
} else {
bc &= ~t; // put 0
}
(bp)++;
(bp) &= 7; // leave it here
rem++;
}
if(rem) {
myfseek(fd, -1, SEEK_CUR);
myfputc(bc, fd);
}
if(bitchr) *bitchr = bc;
if(bitpos) *bitpos = bp;
return i;
}
int bits2str(u8 *out, int outsz, int bits, u8 *bitchr, u8 *pos, int fd) {
int max8 = 8;
u8 *o;
if(!out) return 0;
//outsz -= (*pos >> 3); pos is 3 bit
if(outsz <= 0) return 0;
if(outsz < (bits >> (int)3)) {
bits = outsz << (int)3;
}
for(o = out; bits > 0; bits -= max8) {
if(bits < 8) max8 = bits;
*o++ = fd_read_bits(max8, bitchr, pos, fd);
}
return o - out;
}
int str2bits(u8 *in, int insz, int bits, u8 *bitchr, u8 *pos, int fd) {
int max8 = 8;
u8 *o;
if(!in) return 0;
//insz -= (*pos >> 3); pos is 3 bit
if(insz <= 0) return 0;
if(insz < (bits >> (int)3)) {
bits = insz << (int)3;
}
for(o = in; bits > 0; bits -= max8) {
if(bits < 8) max8 = bits;
fd_write_bits(*o++, max8, bitchr, pos, fd);
}
return(o - in);
}
int my_fdbits(int fdnum, u8 *out_bitchr, u8 *out_bitpos, u_int *out_bitoff, u8 in_bitchr, u8 in_bitpos, u_int in_bitoff) {
if(fdnum < 0) {
if(out_bitchr && out_bitpos && out_bitoff) {
*out_bitchr = g_memory_file[-fdnum].bitchr;
*out_bitpos = g_memory_file[-fdnum].bitpos;
*out_bitoff = g_memory_file[-fdnum].bitoff;
} else {
g_memory_file[-fdnum].bitchr = in_bitchr;
g_memory_file[-fdnum].bitpos = in_bitpos;
g_memory_file[-fdnum].bitoff = in_bitoff;
}
} else {
CHECK_FILENUM
if(out_bitchr && out_bitpos && out_bitoff) {
*out_bitchr = g_filenumber[fdnum].bitchr;
*out_bitpos = g_filenumber[fdnum].bitpos;
*out_bitoff = g_filenumber[fdnum].bitoff;
} else {
g_filenumber[fdnum].bitchr = in_bitchr;
g_filenumber[fdnum].bitpos = in_bitpos;
g_filenumber[fdnum].bitoff = in_bitoff;
}
}
return 0;
}
int myatoifile(u8 *str) { // for quick usage
int fdnum;
if(str && !strnicmp(str, MEMORY_FNAME, MEMORY_FNAMESZ)) {
fdnum = get_memory_file(str);
} else if(str && !strnicmp(str, "ARRAY", 5)) {
fdnum = myatoi(str + 5);
} else {
if(!str || !str[0]) return 0; // default is file number 0
if(!myisdechex_string(str)) return(MAX_FILES); // the syntax of idstring sux!
fdnum = myatoi(str);
}
//if((fdnum <= 0) || (fdnum > MAX_FILES)) {
if((fdnum < -MAX_FILES) || (fdnum > MAX_FILES)) {
fprintf(stderr, "\nError: [myatoifile] invalid FILE number (%d)\n", (i32)fdnum);
myexit(QUICKBMS_ERROR_BMS);
}
return(fdnum);
}
int dumpa_memory_file(memory_file_t *memfile, u8 **ret_data, int size, int *ret_size) {
u8 *data;
data = *ret_data;
if(size == -1) ALLOC_ERR;
if(g_append_mode) {
if(g_append_mode == APPEND_MODE_APPEND) { // append
memfile->pos = memfile->size;
if((memfile->size + size) < memfile->size) ALLOC_ERR;
memfile->size += size;
} else if(g_append_mode == APPEND_MODE_OVERWRITE) { // 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;
} else if(g_append_mode == APPEND_MODE_BEFORE) { // before
memfile->pos = 0;
if((memfile->size + size) < memfile->size) ALLOC_ERR;
memfile->size += size;
}
} else {
memfile->pos = 0;
memfile->size = size;
}
if((memfile->pos + size) < memfile->pos) ALLOC_ERR;
memfile->bitchr = 0; // reset the bit stuff
memfile->bitpos = 0;
memfile->bitoff = 0;
// the following are the new instructions for using less memory
if(ret_size && !memfile->data && data) {
memfile->data = data; // direct assignment
*ret_data = NULL; // set to NULL, do NOT free!
*ret_size = 0;
goto quit;
}
if((u_int)memfile->size > (u_int)memfile->maxsize) {
memfile->maxsize = memfile->size;
if(memfile->maxsize == -1) ALLOC_ERR;
memfile->data = realloc(memfile->data, memfile->maxsize + 1);
if(!memfile->data) STD_ERR(QUICKBMS_ERROR_MEMORY);
memfile->data[memfile->maxsize] = 0;
} else if(!memfile->data && !memfile->maxsize) { // avoids some rare problems in some rare cases
if(memfile->maxsize == -1) ALLOC_ERR;
memfile->data = realloc(memfile->data, memfile->maxsize + 1);
if(!memfile->data) STD_ERR(QUICKBMS_ERROR_MEMORY);
memfile->data[memfile->maxsize] = 0;
}
if(g_append_mode == APPEND_MODE_BEFORE) {
mymemmove(memfile->data + size, memfile->data, memfile->size - size);
}
if(memfile->data) {
memcpy(memfile->data + memfile->pos, data, size);