-
Notifications
You must be signed in to change notification settings - Fork 4
/
fileutil.c
1275 lines (1240 loc) · 36.8 KB
/
fileutil.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
/**
* @namespace biew
* @file fileutil.c
* @brief This file contains file utilities of BIEW project.
* @version -
* @remark this source file is part of Binary vIEW project (BIEW).
* The Binary vIEW (BIEW) is copyright (C) 1995 Nickols_K.
* All rights reserved. This software is redistributable under the
* licence given in the file "Licence.en" ("Licence.ru" in russian
* translation) distributed in the BIEW archive.
* @note Requires POSIX compatible development system
*
* @author Nickols_K
* @since 1995
* @note Development, fixes and improvements
**/
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <limits.h>
#include <errno.h>
#include <ctype.h>
#include "editor.h"
#include "bmfile.h"
#include "tstrings.h"
#include "plugins/hexmode.h"
#include "plugins/disasm.h"
#include "beyeutil.h"
#include "bconsole.h"
#include "reg_form.h"
#include "libbeye/pmalloc.h"
#include "libbeye/bbio.h"
#include "libbeye/twin.h"
#include "libbeye/kbd_code.h"
extern tBool fioUseMMF;
static tBool ChSize( void )
{
__fileoff_t psize,tile = 0;
#if __WORDSIZE >= 32
if(Get16DigitDlg(" Change size of file ","Num. of bytes (+-dec):",3,(__fileoff_t *)&tile))
#else
if(Get8DigitDlg(" Change size of file ","Num. of bytes (+-dec):",3,(unsigned long *)&tile))
#endif
{
if(tile != 0)
{
psize = BMGetFLength();
psize += tile;
if(psize > 0)
{
tBool ret;
int my_errno = 0;
char *fname = BMName();
BGLOBAL bHandle;
bHandle = biewOpenRW(fname,BBIO_SMALL_CACHE_SIZE);
if(bHandle == &bNull)
{
err:
errnoMessageBox(RESIZE_FAIL,NULL,my_errno);
return False;
}
ret = bioChSize(bHandle,psize);
my_errno = errno;
bioClose(bHandle);
if(ret == False) goto err;
BMReRead();
return ret;
}
else ErrMessageBox("Invalid new length",NULL);
}
}
return False;
}
static tBool __NEAR__ __FASTCALL__ InsBlock(BGLOBAL bHandle,__filesize_t start,__fileoff_t psize)
{
char *buffer;
__filesize_t tile,oflen,flen,crpos,cwpos;
unsigned numtowrite;
oflen = bioFLength(bHandle);
flen = oflen + psize;
tile = oflen - start;
buffer = PMalloc(51200U);
if(!buffer) return 0;
if(!bioChSize(bHandle,oflen+psize))
{
ErrMessageBox(EXPAND_FAIL,NULL);
PFREE(buffer);
return False;
}
crpos = oflen-min(tile,51200U);
cwpos = flen-min(tile,51200U);
numtowrite = (unsigned)min(tile,51200U);
while(tile)
{
bioSeek(bHandle,crpos,BIO_SEEK_SET);
bioReadBuffer(bHandle,buffer,numtowrite);
bioSeek(bHandle,cwpos,BIO_SEEK_SET);
bioWriteBuffer(bHandle,buffer,numtowrite);
tile -= numtowrite;
numtowrite = (unsigned)min(tile,51200U);
crpos -= numtowrite;
cwpos -= numtowrite;
}
tile = oflen - start;
cwpos = start;
memset(buffer,0,51200U);
while(psize)
{
numtowrite = (unsigned)min((__filesize_t)psize,51200U);
bioSeek(bHandle,cwpos,BIO_SEEK_SET);
bioWriteBuffer(bHandle,buffer,numtowrite);
psize -= numtowrite;
cwpos += numtowrite;
}
PFREE(buffer);
return True;
}
static tBool __NEAR__ __FASTCALL__ DelBlock(BGLOBAL bHandle,__filesize_t start,__fileoff_t psize)
{
char *buffer;
__filesize_t tile,oflen,crpos,cwpos;
unsigned numtowrite;
oflen = bioFLength(bHandle);
tile = oflen - start;
buffer = PMalloc(51200U);
if(!buffer) return False;
crpos = start-psize; /** psize is negative value */
cwpos = start;
while(tile)
{
numtowrite = (unsigned)min(tile,51200U);
bioSeek(bHandle,crpos,BIO_SEEK_SET);
bioReadBuffer(bHandle,buffer,numtowrite);
bioSeek(bHandle,cwpos,BIO_SEEK_SET);
bioWriteBuffer(bHandle,buffer,numtowrite);
tile -= numtowrite;
crpos += numtowrite;
cwpos += numtowrite;
}
PFREE(buffer);
if(!bioChSize(bHandle,oflen+psize))
{
ErrMessageBox(TRUNC_FAIL,NULL);
PFREE(buffer);
}
return True;
}
static tBool InsDelBlock( void )
{
__filesize_t start;
static __fileoff_t psize;
tBool ret = False;
start = BMGetCurrFilePos();
if(GetInsDelBlkDlg(" Insert or delete block to/from file ",&start,&psize))
{
__filesize_t fpos;
BGLOBAL bHandle;
char *fname;
fpos = BMGetCurrFilePos();
if(start > BMGetFLength()) { ErrMessageBox("Start is outside of file",NULL); return 0; }
if(!psize) return 0;
if(psize < 0) if(start+labs(psize) > BMGetFLength()) { ErrMessageBox("Use change size operation instead of block deletion",NULL); return 0; }
fname = BMName();
bHandle = biewOpenRW(fname,BBIO_SMALL_CACHE_SIZE);
if(bHandle == &bNull)
{
errnoMessageBox(OPEN_FAIL,NULL,errno);
}
else
{
if(psize < 0) ret = DelBlock(bHandle,start,psize);
else ret = InsBlock(bHandle,start,psize);
bioClose(bHandle);
BMReRead();
}
BMSeek(fpos,BM_SEEK_SET);
}
return ret;
}
static char ff_fname[FILENAME_MAX+1] = "biew.$$$";
static char xlat_fname[FILENAME_MAX+1];
static __filesize_t ff_startpos = 0L,ff_len = 0L;
static void __NEAR__ __FASTCALL__ printObject(FILE *fout,unsigned obj_num,char *oname,int oclass,int obitness,__filesize_t size)
{
const char *name,*btn;
char onumname[30];
switch(obitness)
{
case DAB_USE16: btn = "USE16"; break;
case DAB_USE32: btn = "USE32"; break;
case DAB_USE64: btn = "USE64"; break;
case DAB_USE128:btn = "USE128"; break;
case DAB_USE256:btn = "USE256"; break;
default: btn = "";
}
name = oname[0] ? oname : oclass == OC_DATA ? "DUMP_DATA" :
oclass == OC_CODE ? "DUMP_TEXT" :
"Unknown";
if(!oname[0]) { sprintf(onumname,"%s%u",name,obj_num); name = onumname; }
#if (__WORDSIZE >=32) && !defined(__QNX4__)
fprintf(fout,"\nSEGMENT %s BYTE PUBLIC %s '%s'\n; size: %llu bytes\n\n"
#else
fprintf(fout,"\nSEGMENT %s BYTE PUBLIC %s '%s'\n; size: %lu bytes\n\n"
#endif
,name
,btn
,oclass == OC_DATA ? "DATA" : oclass == OC_CODE ? "CODE" : "NoObject"
,size);
}
static void __NEAR__ __FASTCALL__ printHdr(FILE * fout,REGISTRY_BIN *fmt)
{
const char *cptr,*cptr1,*cptr2;
time_t tim;
cptr = cptr1 = ";"; cptr2 = "";
time(&tim);
fprintf(fout,"%s\n%sDisassembler dump of \'%s\'\n"
#if (__WORDSIZE >= 32) && !defined(__QNX4__)
"%sRange : %16llXH-%16llXH\n"
#else
"%sRange : %08lXH-%08lXH\n"
#endif
"%sWritten by "BIEW_VER_MSG"\n"
"%sDumped : %s\n"
"%sFormat : %s\n"
"%s\n\n"
,cptr1,cptr,BMName()
,cptr,ff_startpos,ff_startpos+ff_len
,cptr
,cptr,ctime(&tim)
,cptr,fmt->name
,cptr2);
}
static unsigned __NEAR__ __FASTCALL__ printHelpComment(char *buff,MBuffer codebuff,DisasmRet *dret)
{
unsigned len,j;
if(dis_severity > DISCOMSEV_NONE)
{
len = 3+strlen(dis_comments);
strcat(buff,dis_comments);
strcat(buff," ; ");
}
else len = 0;
for(j = 0;j < dret->codelen;j++)
{
memcpy((char *)&buff[len],(char *)Get2Digit(codebuff[j]),2);
len += 2;
}
buff[len] = 0;
return len;
}
extern REGISTRY_MODE disMode;
#define GET_FUNC_CLASS(x) x == SC_LOCAL ? "private" : "public"
static void __NEAR__ __FASTCALL__ make_addr_column(char *buff,__filesize_t offset)
{
if(hexAddressResolv && detectedFormat->AddressResolving)
{
buff[0] = 0;
detectedFormat->AddressResolving(buff,offset);
}
else sprintf(buff,"L%s",Get8Digit(offset));
strcat(buff,":");
}
static void __make_dump_name(const char *end)
{
/* construct name */
char *p;
strcpy(ff_fname,BMName());
p = strrchr(ff_fname,'.');
if(!p) p = &ff_fname[strlen(ff_fname)];
strcpy(p,end);
}
static tBool FStore( void )
{
unsigned long flags;
char *tmp_buff;
__filesize_t endpos,cpos;
tmp_buff = PMalloc(0x1000);
if(!tmp_buff)
{
MemOutBox("temporary buffer initialization");
return False;
}
flags = FSDLG_USEMODES | FSDLG_BINMODE | FSDLG_COMMENT;
DumpMode = True;
ff_startpos = BMGetCurrFilePos();
if(!ff_len) ff_len = BMGetFLength() - ff_startpos;
__make_dump_name(".$$$");
if(GetFStoreDlg(" Save information to file ",ff_fname,&flags,&ff_startpos,&ff_len,FILE_PRMT))
{
endpos = ff_startpos + ff_len;
endpos = endpos > BMGetFLength() ? BMGetFLength() : endpos;
if(endpos > ff_startpos)
{
TWindow *progress_wnd;
unsigned prcnt_counter,oprcnt_counter;
cpos = BMGetCurrFilePos();
progress_wnd = PercentWnd("Saving ..."," Save block to file ");
if(!(flags & FSDLG_ASMMODE)) /** Write in binary mode */
{
BGLOBAL _bioHandle;
bhandle_t handle;
__filesize_t wsize,crpos,pwsize,awsize;
unsigned rem;
wsize = endpos - ff_startpos;
if(__IsFileExists(ff_fname) == False) handle = __OsCreate(ff_fname);
else
{
handle = __OsOpen(ff_fname,FO_READWRITE | SO_DENYNONE);
if(handle == NULL_HANDLE) handle = __OsOpen(ff_fname,FO_READWRITE | SO_COMPAT);
if(handle == NULL_HANDLE)
{
use_err:
errnoMessageBox("Can't use file",NULL,errno);
goto Exit;
}
__OsTruncFile(handle,0L);
}
__OsClose(handle);
_bioHandle = bioOpen(ff_fname,FO_READWRITE | SO_DENYNONE,BBIO_CACHE_SIZE,fioUseMMF ? BIO_OPT_USEMMF : BIO_OPT_DB);
if(_bioHandle == &bNull) _bioHandle = bioOpen(ff_fname,FO_READWRITE | SO_COMPAT,BBIO_CACHE_SIZE,fioUseMMF ? BIO_OPT_USEMMF : BIO_OPT_DB);
if(_bioHandle == &bNull) goto use_err;
crpos = ff_startpos;
bioSeek(_bioHandle,0L,SEEKF_START);
prcnt_counter = oprcnt_counter = 0;
pwsize = 0;
awsize = wsize;
while(wsize)
{
unsigned real_size;
rem = (unsigned)min(wsize,4096);
if(!BMReadBufferEx(tmp_buff,rem,crpos,BM_SEEK_SET))
{
errnoMessageBox(READ_FAIL,NULL,errno);
bioClose(_bioHandle);
goto Exit;
}
real_size = activeMode->convert_cp ? activeMode->convert_cp((char *)tmp_buff,rem,True) : rem;
if(!bioWriteBuffer(_bioHandle,tmp_buff,real_size))
{
errnoMessageBox(WRITE_FAIL,NULL,errno);
bioClose(_bioHandle);
goto Exit;
}
wsize -= rem;
crpos += rem;
pwsize += rem;
prcnt_counter = (unsigned)((pwsize*100)/awsize);
if(prcnt_counter != oprcnt_counter)
{
oprcnt_counter = prcnt_counter;
if(!ShowPercentInWnd(progress_wnd,prcnt_counter)) break;
}
}
bioClose(_bioHandle);
}
else /** Write in disassembler mode */
{
FILE * fout = NULL;
unsigned char *codebuff;
char *file_cache = NULL,*tmp_buff2 = NULL;
unsigned MaxInsnLen;
char func_name[300],obj_name[300],data_dis[300];
__filesize_t func_pa,stop;
unsigned func_class;
__filesize_t awsize,pwsize;
tBool has_string;
__filesize_t obj_start,obj_end;
int obj_class,obj_bitness;
unsigned obj_num;
if(activeMode != &disMode) disMode.init();
if(flags & FSDLG_STRUCTS)
{
if(detectedFormat->set_state) detectedFormat->set_state(PS_ACTIVE);
if(detectedFormat->prepare_structs)
detectedFormat->prepare_structs(ff_startpos,ff_startpos+ff_len);
}
MaxInsnLen = activeDisasm->max_insn_len();
codebuff = PMalloc(MaxInsnLen);
if(!codebuff)
{
MemOutBox("Disasm initialization");
goto dis_exit;
}
tmp_buff2 = PMalloc(0x1000);
file_cache = PMalloc(BBIO_SMALL_CACHE_SIZE);
fout = fopen(ff_fname,"wt");
if(fout == NULL)
{
errnoMessageBox(WRITE_FAIL,NULL,errno);
PFREE(codebuff);
goto Exit;
}
if(file_cache) setvbuf(fout,file_cache,_IOFBF,BBIO_SMALL_CACHE_SIZE);
if(flags & FSDLG_COMMENT)
{
printHdr(fout,detectedFormat);
}
if(flags & FSDLG_STRUCTS)
{
if(detectedFormat->GetObjAttr)
{
obj_num = detectedFormat->GetObjAttr(ff_startpos,obj_name,
sizeof(obj_name),&obj_start,
&obj_end,&obj_class,&obj_bitness);
obj_name[sizeof(obj_name)-1] = 0;
}
else goto defobj;
}
else
{
defobj:
obj_num = 0;
obj_start = 0;
obj_end = BMGetFLength();
obj_name[0] = 0;
obj_class = OC_CODE;
obj_bitness = detectedFormat->query_bitness ? detectedFormat->query_bitness(ff_startpos) : DAB_USE16;
}
if(flags & FSDLG_STRUCTS) printObject(fout,obj_num,obj_name,obj_class,obj_bitness,obj_end - obj_start);
func_pa = 0;
if(flags & FSDLG_STRUCTS)
{
if(detectedFormat->GetPubSym)
{
func_pa = detectedFormat->GetPubSym(func_name,sizeof(func_name),
&func_class,ff_startpos,True);
func_name[sizeof(func_name)-1] = 0;
if(func_pa)
{
fprintf(fout,"%s %s:\n"
,GET_FUNC_CLASS(func_class)
,func_name);
if(func_pa < ff_startpos && flags & FSDLG_COMMENT)
{
fprintf(fout,"; ...\n");
}
}
func_pa = detectedFormat->GetPubSym(func_name,sizeof(func_name),
&func_class,ff_startpos,False);
func_name[sizeof(func_name)-1] = 0;
}
}
prcnt_counter = oprcnt_counter = 0;
awsize = endpos - ff_startpos;
pwsize = 0;
has_string = False;
while(1)
{
DisasmRet dret;
int len;
if(flags & FSDLG_STRUCTS)
{
if(detectedFormat->GetObjAttr)
{
if(ff_startpos >= obj_end)
{
obj_num = detectedFormat->GetObjAttr(ff_startpos,obj_name,
sizeof(obj_name),&obj_start,
&obj_end,&obj_class,
&obj_bitness);
obj_name[sizeof(obj_name)-1] = 0;
printObject(fout,obj_num,obj_name,obj_class,obj_bitness,obj_end - obj_start);
}
}
if(obj_class == OC_NOOBJECT)
{
__filesize_t diff;
#if (__WORDSIZE >= 32) && !defined(__QNX4__)
fprintf(fout,"; L%016llXH-L%016llXH - no object\n",obj_start,obj_end);
#else
fprintf(fout,"; L%08lXH-L%08lXH - no object\n",obj_start,obj_end);
#endif
dret.codelen = min(UCHAR_MAX,obj_end - ff_startpos);
/** some functions can placed in virtual area of objects
mean at end of true data, but before next object */
while(func_pa && func_pa >= obj_start && func_pa < obj_end && func_pa > ff_startpos)
{
diff = func_pa - ff_startpos;
#if (__WORDSIZE >= 32) && !defined(__QNX4__)
if(diff) fprintf(fout,"resb %16llXH\n",diff);
fprintf(fout,"%s %s: ;at offset - %16llXH\n"
#else
if(diff) fprintf(fout,"resb %08lXH\n",diff);
fprintf(fout,"%s %s: ;at offset - %08lXH\n"
#endif
,GET_FUNC_CLASS(func_class)
,func_name
,func_pa);
ff_startpos = func_pa;
func_pa = detectedFormat->GetPubSym(func_name,sizeof(func_name),
&func_class,ff_startpos,False);
func_name[sizeof(func_name)-1] = 0;
if(func_pa == ff_startpos)
{
fprintf(fout,"...Probably internal error of biew...\n");
break;
}
}
diff = obj_end - ff_startpos;
#if (__WORDSIZE>=32) && !defined(__QNX4__)
if(diff) fprintf(fout,"resb %16llXH\n",diff);
#else
if(diff) fprintf(fout,"resb %08lXH\n",diff);
#endif
ff_startpos = obj_end;
goto next_obj;
}
if(detectedFormat->GetPubSym && func_pa)
{
int not_silly;
not_silly = 0;
while(ff_startpos == func_pa)
{
/* print out here all public labels */
fprintf(fout,"%s %s:\n"
,GET_FUNC_CLASS(func_class)
,func_name);
func_pa = detectedFormat->GetPubSym(func_name,sizeof(func_name),
&func_class,ff_startpos,False);
func_name[sizeof(func_name)-1] = 0;
not_silly++;
if(not_silly > 100)
{
fprintf(fout,"; [snipped out] ...\n");
break;
}
}
}
}
memset(codebuff,0,sizeof(codebuff));
BMReadBufferEx((void *)codebuff,MaxInsnLen,ff_startpos,BM_SEEK_SET);
if(obj_class == OC_CODE)
dret = Disassembler(ff_startpos,codebuff,__DISF_NORMAL);
else /** Data object */
{
unsigned dis_data_len,ifreq,data_len;
char coll_str[__TVIO_MAXSCREENWIDTH];
size_t cstr_idx = 0;
dis_data_len = min(sizeof(coll_str)-1,MaxInsnLen);
for(cstr_idx = 0;cstr_idx < dis_data_len;cstr_idx++)
{
if(isprint(codebuff[cstr_idx]))
{
coll_str[cstr_idx] = codebuff[cstr_idx];
}
else break;
}
coll_str[cstr_idx] = 0;
switch(obj_bitness)
{
case DAB_USE16: dis_data_len = 2; break;
case DAB_USE32: dis_data_len = 4; break;
case DAB_USE64: dis_data_len = 8; break;
case DAB_USE128: dis_data_len = 16; break;
case DAB_USE256: dis_data_len = 32; break;
default: dis_data_len = 1; break;
}
data_len = 0;
sprintf(data_dis,"db ");
if(cstr_idx > 1)
{
sprintf(&data_dis[strlen(data_dis)],"'%s'",coll_str);
dret.codelen = cstr_idx;
has_string = True;
}
else
{
for(ifreq = 0;ifreq < dis_data_len;ifreq++)
{
if(isprint(codebuff[ifreq]) && isprint(codebuff[ifreq+1]))
break;
if(isprint(codebuff[ifreq]) && has_string)
sprintf(&data_dis[strlen(data_dis)],"'%c',",codebuff[ifreq]);
else
sprintf(&data_dis[strlen(data_dis)],"%02Xh,",codebuff[ifreq]);
data_len++;
has_string = False;
}
dret.codelen = data_len;
}
dret.str = data_dis;
dret.pro_clone = 0;
dis_severity = DISCOMSEV_NONE;
}
stop = func_pa ? min(func_pa,obj_end) : obj_end;
if(flags & FSDLG_STRUCTS)
{
if(detectedFormat->GetPubSym && stop && stop > ff_startpos &&
ff_startpos + dret.codelen > stop)
{
unsigned lim,ii;
make_addr_column(tmp_buff,ff_startpos);
strcat(tmp_buff," db ");
lim = (unsigned)(stop-ff_startpos);
if(lim > MaxInsnLen) lim = MaxInsnLen;
for(ii = 0;ii < lim;ii++)
sprintf(&tmp_buff[strlen(tmp_buff)],"%s ",Get2Digit(codebuff[ii]));
dret.codelen = lim;
}
else goto normline;
}
else
{
normline:
make_addr_column(tmp_buff,ff_startpos);
sprintf(&tmp_buff[strlen(tmp_buff)]," %s",dret.str);
}
len = strlen(tmp_buff);
if(flags & FSDLG_COMMENT)
{
if(len < 48)
{
memset(&tmp_buff[len],' ',48-len);
len = 48;
tmp_buff[len] = 0;
}
strcat(tmp_buff,"; ");
len += 2;
len += printHelpComment(&((char *)tmp_buff)[len],codebuff,&dret);
}
if(tmp_buff2)
{
szSpace2Tab(tmp_buff2,tmp_buff);
szTrimTrailingSpace(tmp_buff2);
}
strcat(tmp_buff2 ? tmp_buff2 : tmp_buff,"\n");
if(fputs(tmp_buff2 ? tmp_buff2 : tmp_buff,fout) == EOF)
{
errnoMessageBox(WRITE_FAIL,NULL,errno);
goto dis_exit;
}
if(flags & FSDLG_STRUCTS)
{
if(detectedFormat->GetPubSym && stop && ff_startpos != stop &&
ff_startpos + dret.codelen > stop)
dret.codelen = stop - ff_startpos;
}
if(!dret.codelen)
{
ErrMessageBox("Internal fatal error"," Put structures ");
goto dis_exit;
}
ff_startpos += dret.codelen;
next_obj:
if(ff_startpos >= endpos) break;
pwsize += dret.codelen;
prcnt_counter = (unsigned)((pwsize*100)/awsize);
if(prcnt_counter != oprcnt_counter)
{
oprcnt_counter = prcnt_counter;
if(!ShowPercentInWnd(progress_wnd,prcnt_counter)) break;
}
}
dis_exit:
PFREE(codebuff);
fclose(fout);
if(file_cache) PFREE(file_cache);
if(tmp_buff2) PFREE(tmp_buff2);
if(flags & FSDLG_STRUCTS)
{
if(detectedFormat->drop_structs) detectedFormat->drop_structs();
if(detectedFormat->set_state) detectedFormat->set_state(PS_INACTIVE);
}
if(activeMode != &disMode) disMode.term();
}
Exit:
CloseWnd(progress_wnd);
BMSeek(cpos,BM_SEEK_SET);
}
else ErrMessageBox("Start position > end position!",NULL);
}
PFREE(tmp_buff);
DumpMode = False;
return False;
}
static tBool FRestore( void )
{
__filesize_t endpos,cpos;
unsigned long flags;
tBool ret;
ret = False;
flags = FSDLG_NOMODES;
__make_dump_name(".$$$");
if(GetFStoreDlg(" Restore information from file ",ff_fname,&flags,&ff_startpos,&ff_len,FILE_PRMT))
{
__filesize_t flen,lval;
bhandle_t handle;
BGLOBAL bHandle;
char *fname;
endpos = ff_startpos + ff_len;
handle = __OsOpen(ff_fname,FO_READONLY | SO_DENYNONE);
if(handle == NULL_HANDLE) handle = __OsOpen(ff_fname,FO_READONLY | SO_COMPAT);
if(handle == NULL_HANDLE) goto err;
flen = __FileLength(handle);
__OsClose(handle);
lval = endpos - ff_startpos;
endpos = lval > flen ? flen + ff_startpos : endpos;
endpos = endpos > BMGetFLength() ? BMGetFLength() : endpos;
if(endpos > ff_startpos)
{
__filesize_t wsize,cwpos;
unsigned remaind;
void *tmp_buff;
handle = __OsOpen(ff_fname,FO_READONLY | SO_DENYNONE);
if(handle == NULL_HANDLE) handle = __OsOpen(ff_fname,FO_READONLY | SO_COMPAT);
if(handle == NULL_HANDLE)
{
err:
errnoMessageBox(OPEN_FAIL,NULL,errno);
return False;
}
cpos = BMGetCurrFilePos();
wsize = endpos - ff_startpos;
cwpos = ff_startpos;
__OsSeek(handle,0L,SEEKF_START);
tmp_buff = PMalloc(4096);
if(!tmp_buff)
{
MemOutBox("temporary buffer initialization");
return False;
}
fname = BMName();
bHandle = biewOpenRW(fname,BBIO_SMALL_CACHE_SIZE);
if(bHandle != &bNull)
{
while(wsize)
{
remaind = (unsigned)min(wsize,4096);
if((unsigned)__OsRead(handle,tmp_buff,remaind) != remaind)
{
errnoMessageBox(READ_FAIL,NULL,errno);
__OsClose(handle);
ret = False;
goto bye;
}
bioSeek(bHandle,cwpos,BIO_SEEK_SET);
if(!bioWriteBuffer(bHandle,tmp_buff,remaind))
{
errnoMessageBox(WRITE_FAIL,NULL,errno);
ret = False;
goto bye;
}
wsize -= remaind;
cwpos += remaind;
}
bye:
bioClose(bHandle);
BMReRead();
}
else errnoMessageBox(OPEN_FAIL,NULL,errno);
PFREE(tmp_buff);
__OsClose(handle);
BMSeek(cpos,BM_SEEK_SET);
ret = True;
}
else ErrMessageBox("Start position > end position!",NULL);
}
return ret;
}
static void __NEAR__ __FASTCALL__ CryptFunc(char * buff,unsigned len,char *pass)
{
char ch,cxor;
unsigned i,j;
unsigned bigkey_idx;
unsigned passlen;
char big_key[UCHAR_MAX];
cxor = 0;
passlen = strlen(pass);
memset(big_key,0,sizeof(big_key));
for(j = 0;j < passlen;j++) cxor += pass[j]+j;
cxor ^= passlen + len;
for(j = i = 0;i < UCHAR_MAX;i++,j++)
{
if(j > passlen) j = 0;
bigkey_idx = (pass[j] + i) * cxor;
if(bigkey_idx > UCHAR_MAX) bigkey_idx = (bigkey_idx & 0xFF) ^ ((bigkey_idx >> 8) & 0xFF);
big_key[i] = bigkey_idx;
}
for(bigkey_idx = j = i = 0;i < len;i++,bigkey_idx++,j++)
{
unsigned short xor;
if(bigkey_idx > UCHAR_MAX)
{
/** rotate of big key */
ch = big_key[0];
memmove(big_key,&big_key[1],UCHAR_MAX-1);
big_key[UCHAR_MAX-1] = ch;
bigkey_idx = 0;
}
if(j > passlen) j = 0;
xor = (big_key[bigkey_idx] + i)*cxor;
if(xor > UCHAR_MAX) xor = (xor & 0xFF) ^ ((xor >> 8) & 0xFF);
buff[i] = buff[i] ^ xor;
}
/** rotate of pass */
ch = pass[0];
memmove(pass,&pass[1],passlen-1);
pass[passlen-1] = ch;
}
static tBool CryptBlock( void )
{
__filesize_t endpos,cpos;
unsigned long flags;
char pass[81];
tBool ret;
ret = False;
ff_startpos = BMGetCurrFilePos();
if(!ff_len) ff_len = BMGetFLength() - ff_startpos;
pass[0] = 0;
flags = FSDLG_NOMODES;
if(GetFStoreDlg(" (De)Crypt block of file ",pass,&flags,&ff_startpos,&ff_len,"Input password (WARNING! password will be displayed):"))
{
__filesize_t flen,lval;
endpos = ff_startpos + ff_len;
flen = BMGetFLength();
lval = endpos - ff_startpos;
endpos = lval > flen ? flen + ff_startpos : endpos;
endpos = endpos > BMGetFLength() ? BMGetFLength() : endpos;
if(!pass[0]) { ErrMessageBox("Password can't be empty",NULL); return False; }
if(endpos > ff_startpos)
{
__filesize_t wsize,cwpos;
unsigned remaind;
char *fname;
BGLOBAL bHandle;
void *tmp_buff;
cpos = BMGetCurrFilePos();
wsize = endpos - ff_startpos;
cwpos = ff_startpos;
tmp_buff = PMalloc(4096);
if(!tmp_buff)
{
MemOutBox("temporary buffer initialization");
return False;
}
fname = BMName();
bHandle = biewOpenRW(fname,BBIO_SMALL_CACHE_SIZE);
if(bHandle != &bNull)
{
bioSeek(bHandle,ff_startpos,SEEK_SET);
while(wsize)
{
remaind = (unsigned)min(wsize,4096);
if(!bioReadBuffer(bHandle,tmp_buff,remaind))
{
errnoMessageBox(READ_FAIL,NULL,errno);
ret = False;
goto bye;
}
CryptFunc(tmp_buff,remaind,pass);
bioSeek(bHandle,cwpos,BIO_SEEK_SET);
if(!(bioWriteBuffer(bHandle,tmp_buff,remaind)))
{
errnoMessageBox(WRITE_FAIL,NULL,errno);
ret = False;
goto bye;
}
wsize -= remaind;
cwpos += remaind;
}
bye:
bioClose(bHandle);
BMReRead();
}
PFREE(tmp_buff);
BMSeek(cpos,BM_SEEK_SET);
ret = True;
}
else ErrMessageBox("Start position > end position!",NULL);
}
return ret;
}
static void __NEAR__ __FASTCALL__ EndianifyBlock(char * buff,unsigned len, int type)
{
unsigned i, step;
if(!type) return; /* for now */
switch(type)
{
default:
#ifdef INT64_C
case 3: step = 8;
break;
#endif
case 2: step = 4;
break;
case 1:
step = 2;
break;
}
len /= step;
len *= step;
for(i = 0;i < len;i+=step, buff+=step)
{
switch(type)
{
default:
#ifdef INT64_C
case 3: *((tUInt64 *)buff) = ByteSwapLL(*((tUInt64 *)buff));
break;
#endif
case 2: *((tUInt32 *)buff) = ByteSwapL(*((tUInt32 *)buff));
break;
case 1:
*((tUInt16 *)buff) = ByteSwapS(*((tUInt16 *)buff));
break;
}
}
}
static tBool ReverseBlock( void )
{
__filesize_t endpos,cpos;
unsigned long flags;
tBool ret;
ret = False;
ff_startpos = BMGetCurrFilePos();
if(!ff_len) ff_len = BMGetFLength() - ff_startpos;
flags = FSDLG_USEBITNS;
if(GetFStoreDlg(" Endianify block of file ",NULL,&flags,&ff_startpos,&ff_len,NULL))
{
__filesize_t flen,lval;
endpos = ff_startpos + ff_len;
flen = BMGetFLength();
lval = endpos - ff_startpos;
endpos = lval > flen ? flen + ff_startpos : endpos;
endpos = endpos > BMGetFLength() ? BMGetFLength() : endpos;
if(endpos > ff_startpos)
{
__filesize_t wsize,cwpos;
unsigned remaind;
char *fname;
BGLOBAL bHandle;
void *tmp_buff;
cpos = BMGetCurrFilePos();
wsize = endpos - ff_startpos;
cwpos = ff_startpos;
tmp_buff = PMalloc(4096);
if(!tmp_buff)
{
MemOutBox("temporary buffer initialization");
return False;
}
fname = BMName();
bHandle = biewOpenRW(fname,BBIO_SMALL_CACHE_SIZE);
if(bHandle != &bNull)
{
bioSeek(bHandle,ff_startpos,SEEK_SET);
while(wsize)
{
remaind = (unsigned)min(wsize,4096);
if(!bioReadBuffer(bHandle,tmp_buff,remaind))
{
errnoMessageBox(READ_FAIL,NULL,errno);
ret = False;
goto bye;
}
EndianifyBlock(tmp_buff,remaind, flags & FSDLG_BTNSMASK);
bioSeek(bHandle,cwpos,BIO_SEEK_SET);
if(!(bioWriteBuffer(bHandle,tmp_buff,remaind)))
{
errnoMessageBox(WRITE_FAIL,NULL,errno);
ret = False;
goto bye;
}
wsize -= remaind;
cwpos += remaind;
}
bye:
bioClose(bHandle);
BMReRead();
}
PFREE(tmp_buff);
BMSeek(cpos,BM_SEEK_SET);