-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdump.cpp
executable file
·976 lines (884 loc) · 28.9 KB
/
dump.cpp
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
/* (C) 2003-2007 Willem Jan Hengeveld <[email protected]>
* Web: http://www.xs4all.nl/~itsme/
* http://wiki.xda-developers.com/
*
* $Id$
*
* this program provides various ways of (hex) dumping sections of binary files
*
*/
// DONE:
// ? dump 8000.mem -o 0x02400000 -s 0x400 -xx
// ? does not work as it should, .. offsets seem to be double from their real value.
// ... bug seems to have disappeared
//
// DONE:
// dump of a very large file ( >2M ) with all 0s
// the summary lines are not printed.
// todo:
// * DONE think of way to make winxp support sha256: now using openssl
// * DONE add simple add-checksum , and xor-checksum support
// * DONE make '*' summary only print '*' when more than X lines are the same
// * bug: "dump -s 16 -w 8 -4 -x x.nb" should not print ascii part in line.
// problem is incompatible interface to bighexdump and hexdump
// other problem is that the hexdumper has no state.
// * improved hexdumper
// * check for identical lines -before- converting to string
// * use MmapReader/FileReader/BlockDevice where appropriate
// * FIXED bug: dump -o 0x14 -f -4 -w 22 -s 0x56 file
// -> crash
// * FIXED bug: "dump - -o 11 -e 0x20d" should give the same output as "dump - -o 11 -l 0x202" -> it does not!
// * FIXED make "dump -o 123 -e -123 file" work.
//
// done:
// * dump -s STEP {-md5|-sum}
// should print hash/sum of each step block. ( and default to blocksize=stepsize )
//
//
//note: you can use dump also to read block devices,
// dump \\.\PhysicalDrive0 -xx -o 0 -l 0xa00000000 -s 0x100000000
// will dump 64 ascii chars every 4G of your 40G disk.
//
//
#define __STDC_LIMIT_MACROS
#ifndef NOMINMAX
#define NOMINMAX
#endif
//#include <util/wintypes.h>
#include <stdio.h>
#ifdef _WIN32
#include <io.h>
#endif
#ifndef _WIN32
#include <sys/stat.h>
#ifdef __MACH__
#ifdef __APPLE__
#include "TargetConditionals.h"
#endif
#if !TARGET_OS_IPHONE
// sys/disk.h is not in the ios sdk
#include <sys/disk.h>
#endif
#endif
#if defined(_ANDROID) || defined(__linux__)
#include <linux/fs.h>
extern "C" int futimes(int fd, const struct timeval tv[2]);
#endif
#endif
#ifndef _WIN32
#include <unistd.h> // ftruncate
#include <sys/ioctl.h>
#endif
#include <fcntl.h>
#ifdef _USE_WINCRYPTAPI
#include "dump_hash.h"
#elif defined(_USE_OPENSSL)
#include "dump_ossl_hash.h"
#elif defined(__ANDROID__)
#include "dump_android_hash.h"
#else
#include "dump_dummy_hash.h"
#endif
#include "dump_crc32.h"
#include "dump_sum.h"
#include "bighexdump.h"
#include "bigascdump.h"
#include <cpputils/formatter.h>
#include <cpputils/stringlibrary.h>
#include <cpputils/argparse.h>
#include <stdint.h>
#include <string.h>
#include <algorithm>
#include <optional>
#include <cpputils/hexdumper.h>
#define vectorptr(v) ((v).empty()?NULL:&(v)[0])
namespace std {
size_t min(int64_t a, size_t b)
{
#ifdef _WIN32
return (a<b) ? a : b;
#else
return b>=__INTMAX_MAX__ ? a : (a<b) ? a : b;
#endif
}
}
DumpUnitType g_dumpunit=DUMPUNIT_BYTE;
DumpFormat g_dumpformat= DUMP_HEX_ASCII;
int g_hashtype= 0;
uint64_t g_crc_initval= 0;
uint64_t g_crc_poly= 0xEDB88320;
uint64_t g_crc_bits= 32;
int g_nMaxUnitsPerLine=-1;
int64_t g_llStepSize= 0;
bool g_fulldump= false;
int g_summarizeThreshold=-1;
uint32_t g_chunksize= 1024*1024;
std::string hexstring(const std::vector<uint8_t>& bv)
{
return stringformat("%-b", bv);
}
std::string hexdump(uint64_t ofs, const uint8_t *p, size_t n, int type, int width)
{
std::stringstream buf;
buf << Hex::offset(ofs) << std::hex << std::setw(width) << std::left;
switch(type)
{
case 1: buf << Hex::dumper((const uint8_t*)p, n); break;
case 2: buf << Hex::dumper((const uint16_t*)p, n); break;
case 4: buf << Hex::dumper((const uint32_t*)p, n); break;
case 8: buf << Hex::dumper((const uint64_t*)p, n); break;
}
return buf.str();
}
uint64_t invmask(int bits)
{
return (uint64_t(1)<<bits)-1;
}
// skipbytes is used for non-seekable files ( like stdin )
void skipbytes(FILE *f, int64_t skip)
{
std::vector<uint8_t> buf;
buf.resize(std::min(skip,g_chunksize));
while (skip) {
size_t want= std::min(skip,buf.size());
fread(vectorptr(buf), 1, want, f);
skip-=want;
}
}
// seek > 4G when only 32 bit api is available.
int longseek(FILE *f, int64_t delta, int type)
{
// todo: lseek64(fileno(f), delta, type);
#if defined(_ANDROID) || defined(ANDROID)
if (type!=SEEK_CUR) {
if (delta<0x40000000) {
return fseek(f, delta, type);
}
return fseek(f, 0, type);
}
if (delta>0) {
while (delta>=0x40000000) {
fseek(f, 0x40000000, SEEK_CUR);
delta -= 0x40000000;
}
return fseek(f, delta, SEEK_CUR);
}
else {
while (delta<=-0x40000000) {
fseek(f, -0x40000000, SEEK_CUR);
delta += 0x40000000;
}
return fseek(f, delta, SEEK_CUR);
}
#elif defined(_WIN32)
return _fseeki64(f, delta, type);
#else
return fseeko(f, delta, type);
#endif
}
// hexdump stepped chunks of the file.
bool StepFile(const std::string& srcFilename, int64_t llBaseOffset, int64_t llOffset, int64_t llLength)
{
std::vector<uint8_t> buffer;
std::string prevline;
int nSameCount= 0;
bool fromStdin= srcFilename=="-";
FILE *f= NULL;
if (fromStdin) {
f= stdin;
#ifdef WIN32
if (-1==_setmode( _fileno( stdin ), _O_BINARY )) {
print("ERROR: _setmode(stdin, rb)");
return false;
}
#endif
}
else {
f= fopen(srcFilename.c_str(), "rb");
}
if (f==NULL) {
perror(srcFilename.c_str());
return false;
}
if (f==stdin) {
skipbytes(f, llOffset-llBaseOffset);
}
else if (longseek(f, llOffset-llBaseOffset, SEEK_SET))
{
fclose(f);
}
#ifdef _USE_WINCRYPTAPI
CryptProvider cprov;
#endif
while (llLength>0)
{
buffer.resize(DumpUnitSize(g_dumpunit)*g_nMaxUnitsPerLine);
uint32_t dwBytesWanted= std::min(llLength,buffer.size());
std::string line;
uint32_t dwNumberOfBytesRead= fread(vectorptr(buffer), 1, dwBytesWanted, f);
if (dwNumberOfBytesRead==0)
break;
if (g_dumpformat==DUMP_RAW) {
line.clear();
}
else if (g_dumpformat==DUMP_HASH) {
#ifdef _USE_WINCRYPTAPI
CryptHash hashcalc(cprov);
#define HASHTYPEOFFSET (ALG_CLASS_HASH | ALG_TYPE_ANY)
#else
CryptHash hashcalc;
#define HASHTYPEOFFSET 0
#endif
hashcalc.InitHash(g_hashtype);
hashcalc.AddData(buffer);
std::vector<uint8_t> hash = hashcalc.GetHash();
line= hexstring(hash);
}
else if (g_dumpformat==DUMP_HASHES) {
typedef std::vector<CryptHash*> CryptHashList;
CryptHashList hashes;
#define VALIDALGS 0x701e
for (int ihash=0 ; ihash<CryptHash::HASHTYPECOUNT ; ihash++) {
#ifdef _USE_WINCRYPTAPI
if (((1<<ihash)&VALIDALGS)==0)
continue;
hashes.push_back(new CryptHash(cprov));
#else
hashes.push_back(new CryptHash);
#endif
try {
hashes.back()->InitHash(ihash+HASHTYPEOFFSET);
}
catch(...) {
delete hashes.back();
hashes.resize(hashes.size()-1);
}
}
for (CryptHashList::iterator ih= hashes.begin() ; ih!=hashes.end() ; ih++)
{
(*ih)->AddData(buffer);
}
line.clear();
for (CryptHashList::iterator ih= hashes.begin() ; ih!=hashes.end() ; ih++)
{
std::vector<uint8_t> hash = (*ih)->GetHash();
if (!line.empty())
line += " ";
line += hexstring(hash);
}
}
else if (g_dumpformat==DUMP_CRC32) {
CRC32 crc(g_crc_initval, g_crc_poly, g_crc_bits);
crc.add_data(vectorptr(buffer), buffer.size());
line= stringformat("%08lx~%08lx", crc.crc, crc.crc^invmask(g_crc_bits));
}
else if (g_dumpformat==DUMP_SUM) {
DATASUM sum;
CRC32 crc(g_crc_initval, g_crc_poly, g_crc_bits);
CRC32 crc1(~g_crc_initval, g_crc_poly, g_crc_bits);
sum.add_data(vectorptr(buffer), buffer.size());
crc.add_data(vectorptr(buffer), buffer.size());
crc1.add_data(vectorptr(buffer), buffer.size());
line= stringformat("%08llx~%08llx %08llx~%08llx +%02llx LE:%04llx %08llx %16llx BE:%04llx %08llx %16llx ^%02x %04x %08lx %016llx",
crc.crc, crc.crc^invmask(g_crc_bits), crc1.crc, crc1.crc^invmask(g_crc_bits),
sum.sum1, sum.sum2_le, sum.sum4_le, sum.sum8_le, sum.sum2_be, sum.sum4_be, sum.sum8_be,
sum.sumxor1, sum.sumxor2, sum.sumxor4, sum.sumxor8);
}
else if (g_dumpformat==DUMP_STRINGS)
line= bigascdump(buffer);
else if (g_dumpformat==DUMP_ASCII)
line= asciidump(vectorptr(buffer), dwNumberOfBytesRead);
else {
line= hexdump(llOffset, vectorptr(buffer), dwNumberOfBytesRead, DumpUnitSize(g_dumpunit), g_nMaxUnitsPerLine);
line.erase(0, line.find_first_of(' ')+1);
}
if (*line.rbegin()=='\n')
line.resize(line.size()-1);
if (g_dumpformat==DUMP_RAW)
fwrite(&buffer[0], 1, buffer.size(), stdout);
else if (!g_fulldump && line == prevline) {
nSameCount++;
}
else {
if (nSameCount>0 && (g_summarizeThreshold==-1 || nSameCount<=g_summarizeThreshold)) {
for (int i=0 ; i<nSameCount ; i++)
writedumpline(llOffset+g_llStepSize*(signed(i)-nSameCount), prevline);
}
else if (nSameCount>0 && g_summarizeThreshold>0 && nSameCount>g_summarizeThreshold)
print("* [ 0x%x lines ]\n", nSameCount);
nSameCount= 0;
writedumpline(llOffset, line);
}
prevline= line;
int64_t llStep= std::min(llLength, g_llStepSize);
if (f==stdin) {
skipbytes(f, llStep-dwNumberOfBytesRead);
}
else if (longseek(f, llStep-dwNumberOfBytesRead, SEEK_CUR))
{
fclose(f);
}
llLength -= llStep;
llOffset += llStep;
}
fclose(f);
if (g_dumpformat!=DUMP_RAW) {
if (nSameCount==1)
writedumpline(llOffset-g_llStepSize, prevline);
else if (nSameCount>1)
print("* [ 0x%x lines ]\n", nSameCount);
writedumpline(llOffset, "");
}
return true;
}
// normal hexdump of file
bool Dumpfile(const std::string& srcFilename, int64_t llBaseOffset, int64_t llOffset, int64_t llLength)
{
if (g_nMaxUnitsPerLine>=MAXUNITSPERLINE) {
printf("WARNING: -w 0x%x too large\n", g_nMaxUnitsPerLine);
return false;
}
uint32_t flags= hexdumpflags(g_dumpunit, g_nMaxUnitsPerLine, g_dumpformat)
| (g_fulldump?0:HEXDUMP_SUMMARIZE) | (g_dumpformat==DUMP_RAW?0:HEXDUMP_WITH_OFFSET);
bool fromStdin= srcFilename=="-";
FILE *f= NULL;
if (fromStdin) {
f= stdin;
#ifdef WIN32
if (-1==_setmode( _fileno( stdin ), _O_BINARY )) {
print("ERROR: _setmode(stdin, rb)");
return false;
}
#endif
}
else {
f= fopen(srcFilename.c_str(), "rb");
}
if (f==NULL) {
perror(srcFilename.c_str());
return false;
}
if (f==stdin) {
skipbytes(f, llOffset-llBaseOffset);
}
else if (longseek(f, llOffset-llBaseOffset, SEEK_SET))
{
fclose(f);
}
#ifdef _USE_WINCRYPTAPI
CryptProvider cprov;
CryptHash hashcalc(cprov);
#define HASHTYPEOFFSET (ALG_CLASS_HASH | ALG_TYPE_ANY)
#else
CryptHash hashcalc;
#define HASHTYPEOFFSET 0
#endif
if (g_dumpformat==DUMP_HASH)
hashcalc.InitHash(g_hashtype);
typedef std::vector<CryptHash*> CryptHashList;
CryptHashList hashes;
if (g_dumpformat==DUMP_HASHES) {
for (int ihash=0 ; ihash<CryptHash::HASHTYPECOUNT ; ihash++) {
#ifdef _USE_WINCRYPTAPI
hashes.push_back(new CryptHash(cprov));
#else
hashes.push_back(new CryptHash);
#endif
try {
hashes.back()->InitHash(ihash+HASHTYPEOFFSET);
}
catch(...) {
delete hashes.back();
hashes.resize(hashes.size()-1);
}
}
}
DATASUM sum;
CRC32 crc(g_crc_initval, g_crc_poly, g_crc_bits);
CRC32 crc1(~g_crc_initval, g_crc_poly, g_crc_bits);
std::vector<uint8_t> buf;
while (llLength>0)
{
buf.resize(g_chunksize);
uint32_t dwBytesWanted= std::min(llLength,buf.size());
uint32_t nRead= fread(vectorptr(buf), 1, dwBytesWanted, f);
if (nRead==0)
break;
buf.resize(nRead);
if (g_dumpformat==DUMP_HASH) {
hashcalc.AddData(buf);
}
else if (g_dumpformat==DUMP_HASHES) {
for (CryptHashList::iterator ih= hashes.begin() ; ih!=hashes.end() ; ih++)
(*ih)->AddData(buf);
}
else if (g_dumpformat==DUMP_CRC32) {
crc.add_data(vectorptr(buf), buf.size());
}
else if (g_dumpformat==DUMP_SUM) {
sum.add_data(vectorptr(buf), buf.size());
crc.add_data(vectorptr(buf), buf.size());
crc1.add_data(vectorptr(buf), buf.size());
}
else if (g_dumpformat==DUMP_RAW)
fwrite(&buf[0], 1, buf.size(), stdout);
else
bighexdump(llOffset, buf, flags | (llLength!=nRead ? HEXDUMP_MOREFOLLOWS : 0) );
llLength -= nRead;
llOffset += nRead;
}
fclose(f);
if (g_dumpformat==DUMP_HASH) {
std::vector<uint8_t> hash = hashcalc.GetHash();
print("%s\n", hexstring(hash));
}
else if (g_dumpformat==DUMP_HASHES) {
for (CryptHashList::iterator ih= hashes.begin() ; ih!=hashes.end() ; ih++)
{
std::vector<uint8_t> hash = (*ih)->GetHash();
print("%-10s: %s\n", (*ih)->hashname(), hexstring(hash));
}
}
else if (g_dumpformat==DUMP_CRC32) {
print("crc=%08llx invcrc=%08llx\n", crc.crc, crc.crc^invmask(g_crc_bits));
}
else if (g_dumpformat==DUMP_SUM) {
print("crc0=%08llx invcrc=%08llx\n", crc.crc, crc.crc^invmask(g_crc_bits));
print("crc-1=%08llx invcrc=%08llx\n", crc1.crc, crc1.crc^invmask(g_crc_bits));
print("addsum=%02llx LE:%04llx %08llx %16llx BE:%04llx %08llx %16llx sumxor=%02x %04x %08lx %016llx\n",
sum.sum1, sum.sum2_le, sum.sum4_le, sum.sum8_le,
sum.sum2_be, sum.sum4_be, sum.sum8_be,
sum.sumxor1, sum.sumxor2, sum.sumxor4, sum.sumxor8);
}
return true;
}
// copy multiple chunks of srcfile to dstfile
bool CopyFileSteps(const std::string& srcFilename, const std::string& dstFilename, int64_t llBaseOffset, int64_t llOffset, int64_t llLength)
{
std::vector<uint8_t> buffer;
bool fromStdin= srcFilename=="-";
FILE *f= NULL;
if (fromStdin) {
f= stdin;
#ifdef WIN32
if (-1==_setmode( _fileno( stdin ), _O_BINARY )) {
print("ERROR: _setmode(stdin, rb)");
return false;
}
#endif
}
else {
f= fopen(srcFilename.c_str(), "rb");
}
if (f==NULL) {
perror(srcFilename.c_str());
return false;
}
if (f==stdin) {
skipbytes(f, llOffset-llBaseOffset);
}
else if (longseek(f, llOffset-llBaseOffset, SEEK_SET))
{
fclose(f);
}
FILE *g= fopen(dstFilename.c_str(), "w+b");
if (g==NULL) {
perror(dstFilename.c_str());
return false;
}
while (llLength>0)
{
buffer.resize(DumpUnitSize(g_dumpunit)*g_nMaxUnitsPerLine);
uint32_t dwBytesWanted= std::min(llLength,buffer.size());
uint32_t dwNumberOfBytesRead= fread(vectorptr(buffer), 1, dwBytesWanted, f);
if (dwNumberOfBytesRead==0)
break;
fwrite(&buffer[0], 1, buffer.size(), g);
int64_t llStep= std::min(llLength, g_llStepSize);
if (f==stdin) {
skipbytes(f, llStep-dwNumberOfBytesRead);
}
else if (longseek(f, llStep-dwNumberOfBytesRead, SEEK_CUR))
{
fclose(f);
}
llLength -= llStep;
llOffset += llStep;
}
fclose(g);
fclose(f);
return true;
}
// copy section of srcfile to dstfile
bool Copyfile(const std::string& srcFilename, const std::string& dstFilename, int64_t llBaseOffset, int64_t llOffset, int64_t llLength)
{
FILE *f= NULL;
bool fromStdin= srcFilename=="-";
if (fromStdin) {
f= stdin;
#ifdef WIN32
if (-1==_setmode( _fileno( stdin ), _O_BINARY )) {
print("ERROR: _setmode(stdin, rb)");
return false;
}
#endif
}
else {
f= fopen(srcFilename.c_str(), "rb");
}
if (f==NULL) {
perror(srcFilename.c_str());
return false;
}
if (f==stdin) {
skipbytes(f, llOffset-llBaseOffset);
}
else if (longseek(f, llOffset-llBaseOffset, SEEK_SET))
{
fclose(f);
}
FILE *g= fopen(dstFilename.c_str(), "w+b");
if (g==NULL) {
perror(dstFilename.c_str());
return false;
}
std::vector<uint8_t> buf;
while (llLength>0)
{
buf.resize(g_chunksize);
uint32_t dwBytesWanted= std::min(llLength,buf.size());
uint32_t nRead= fread(vectorptr(buf), 1, dwBytesWanted, f);
if (nRead==0)
break;
buf.resize(nRead);
fwrite(&buf[0], 1, buf.size(), g);
llLength -= nRead;
llOffset += nRead;
}
fclose(g);
fclose(f);
return true;
}
// filesize for various platforms, files, blockdevice.
int64_t GetFileSize(const std::string& filename)
{
#ifdef WIN32
HANDLE hSrc = CreateFile(filename.c_str(), GENERIC_READ, FILE_SHARE_WRITE|FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE == hSrc)
{
print("ERROR: Unable to open file %s", filename.c_str());
return -1;
}
DWORD dwSizeH;
DWORD dwSizeL= GetFileSize(hSrc, &dwSizeH);
CloseHandle(hSrc);
return ((int64_t)dwSizeH<<32)+dwSizeL;
#else
struct stat st;
if (stat(filename.c_str(), &st)) {
print("ERROR: stat");
return -1;
}
if (st.st_mode&S_IFREG)
return st.st_size;
else if (st.st_mode&S_IFBLK) {
int h= open(filename.c_str(), O_RDONLY);
uint64_t devsize;
#ifdef DKIOCGETBLOCKCOUNT
uint64_t bkcount;
uint32_t bksize;
if (-1==ioctl(h, DKIOCGETBLOCKCOUNT, &bkcount)) {
close(h);
print("ERROR: ioctl(DKIOCGETBLOCKCOUNT)");
return -1;
}
if (-1==ioctl(h, DKIOCGETBLOCKSIZE, &bksize)) {
close(h);
print("ERROR: ioctl(DKIOCGETBLOCKSIZE)");
return -1;
}
devsize = bkcount*bksize;
#endif
#ifdef BLKGETSIZE64
if (-1==ioctl(h, BLKGETSIZE64, &devsize)) {
close(h);
print("ERROR: ioctl(BLKGETSIZE64)");
return -1;
}
#endif
close(h);
return devsize;
}
else {
printf("could not get size for device\n");
return -1;
}
#endif
}
void usage()
{
printf("(C) 2003-2008 Willem jan Hengeveld [email protected]\n");
printf("Usage: dump [options] FILENAME [OUTFILENAME]\n");
printf(" when outfilename is specified, the binary data is writen to it.\n");
printf(" -b BASE : specify base offset - what offset has first byte of the file\n");
printf(" -o OFS : what offset to display\n");
printf(" -l LEN : length to dump\n");
printf(" -e OFS : end offset ( alternative for -l )\n");
printf(" -w N : how many words to print on each line\n");
printf(" -s SIZE : step with SIZE through memory\n");
printf(" -r SIZE : read chunk size, default 1M\n");
printf(" -1,2,4 : what to print: byte, word, dword\n");
printf(" -a : ascdump iso hexdump\n");
#ifdef MD5_DIGEST_LENGTH
printf(" -md5 : print md5sum of selected memory range\n");
#endif
#ifdef SHA1_DIGEST_LENGTH
printf(" -sha1 : print sha1 of selected memory range\n");
#endif
#ifdef SHA256_DIGEST_LENGTH
printf(" -sha256: print sha256 of selected memory range\n");
#endif
printf(" -crc : print crc32 of selected memory range\n");
printf(" -crc:INIT:POLY:bits default: -crc:0:0xEDB88320:32\n");
printf(" -sum : print checksums of selected memory range\n");
printf(" -h : calc all known hash types\n");
printf(" -f : full - do not summarize identical lines\n");
printf(" -S N : summarize threshold\n");
printf(" -c : print raw memory to stdout\n");
printf(" -x : print only hex\n");
printf(" -xx : print only fixed length ascii dumps\n");
}
int main(int argc, char **argv)
{
std::optional<int64_t> llOffset;
std::optional<int64_t> llEndOffset;
std::optional<int64_t> llLength;
std::optional<int64_t> llBaseOffset;
std::string srcFilename;
std::string dstFilename;
int nDumpUnitSize = 1;
std::string crcspec;
int argsfound = 0;
for (auto& arg : ArgParser(argc, argv))
switch(arg.option())
{
case 'b': llBaseOffset = arg.getint(); break;
case 'o': llOffset = arg.getint(); break;
case 'e': llEndOffset = arg.getint(); break;
case 'l': llLength = arg.getint(); break;
case 'h': g_dumpformat= DUMP_HASHES; break;
case 'r':
#if !defined(__ANDROID__)
#ifdef RIPEMD160_DIGEST_LENGTH
if (arg.match("-ripemd160")) {
g_dumpformat= DUMP_HASH;
g_hashtype= CryptHash::RIPEMD160;
}
else
#endif
#endif
g_chunksize = arg.getuint(); break;
case 'w': g_nMaxUnitsPerLine = arg.getint(); break;
case 's': if (0)
;
#ifdef SHA1_DIGEST_LENGTH
else if (arg.match("-sha1")) {
g_dumpformat= DUMP_HASH;
g_hashtype= CryptHash::SHA1;
}
#endif
#if !defined(__ANDROID__)
#ifdef SHA256_DIGEST_LENGTH
else if (arg.match("-sha256")) {
g_dumpformat= DUMP_HASH;
g_hashtype= CryptHash::SHA256;
}
#endif
#ifdef SHA384_DIGEST_LENGTH
else if (arg.match("-sha384")) {
g_dumpformat= DUMP_HASH;
g_hashtype= CryptHash::SHA384;
}
#endif
#ifdef SHA512_DIGEST_LENGTH
else if (arg.match("-sha512")) {
g_dumpformat= DUMP_HASH;
g_hashtype= CryptHash::SHA512;
}
#endif
#endif
else if (arg.match("-sum"))
g_dumpformat= DUMP_SUM;
else
g_llStepSize = arg.getint();
break;
case 'm': if (0)
;
#ifdef MD5_DIGEST_LENGTH
else if (arg.match("-md5")) {
g_dumpformat= DUMP_HASH;
g_hashtype= CryptHash::MD5;
}
#endif
#ifdef MD2_DIGEST_LENGTH
else if (arg.match("-md2")) {
g_dumpformat= DUMP_HASH;
g_hashtype= CryptHash::MD2;
}
#endif
#ifdef MD4_DIGEST_LENGTH
else if (arg.match("-md4")) {
g_dumpformat= DUMP_HASH;
g_hashtype= CryptHash::MD4;
}
#endif
#if !defined(__ANDROID__)
#ifdef RIPEMD160_DIGEST_LENGTH
else if (arg.match("-md160")) {
g_dumpformat= DUMP_HASH;
g_hashtype= CryptHash::RIPEMD160;
}
#endif
#endif
break;
case 'a': g_dumpformat= DUMP_STRINGS; break;
case 'c': if (arg.match("-crc")) {
g_dumpformat= DUMP_CRC32;
crcspec = arg.getstr();
}
else
g_dumpformat= DUMP_RAW;
break;
case 'f': g_fulldump= true; break;
case 'S': g_summarizeThreshold = arg.getuint(); break;
case 'x': if (arg.count()==2)
g_dumpformat= DUMP_ASCII;
else
g_dumpformat= DUMP_HEX;
break;
case '1': case '2': case '4': case '8':
nDumpUnitSize= arg.option()-'0';
break;
case 0: // single '-'
case -1:
switch (argsfound++) {
case 0: srcFilename= arg.getstr(); break;
case 1: dstFilename= arg.getstr(); break;
}
break;
default:
usage();
return 1;
}
if (argsfound==0 || argsfound>2)
{
usage();
return 1;
}
if (!crcspec.empty()) {
auto res1 = parsesigned(crcspec.begin(), crcspec.end(), 0);
g_crc_initval= res1.first;
if (res1.second!=crcspec.end()) {
auto res2 = parseunsigned(res1.second+1, crcspec.end(), 0);
g_crc_poly= res2.first;
if (res2.second!=crcspec.end()) {
auto res3 = parseunsigned(res2.second+1, crcspec.end(), 0);
g_crc_bits= res3.first;
}
}
}
// 64 = highest 2^n, such that addrsize + 2^n <= screenwidth
// 32 = highest 2^n, such that addrsize + 3*2^n <= screenwidth+25 ...
// 16 = highest 2^n, such that addrsize + 4*2^n <= screenwidth
if (g_nMaxUnitsPerLine<0) {
if (g_dumpformat==DUMP_ASCII)
g_nMaxUnitsPerLine= 64/nDumpUnitSize;
else if (g_dumpformat==DUMP_HEX)
g_nMaxUnitsPerLine= 32/nDumpUnitSize;
else if (g_dumpformat>=DUMP_HASH)
g_nMaxUnitsPerLine= g_llStepSize;
else
g_nMaxUnitsPerLine= 16/nDumpUnitSize;
}
g_dumpunit=
nDumpUnitSize==1?DUMPUNIT_BYTE:
nDumpUnitSize==2?DUMPUNIT_WORD:
nDumpUnitSize==4?DUMPUNIT_DWORD:
nDumpUnitSize==8?DUMPUNIT_QWORD:DUMPUNIT_BYTE;
if (g_dumpformat==DUMP_RAW) {
#ifdef WIN32
if (-1==_setmode( _fileno( stdout ), _O_BINARY )) {
print("ERROR: _setmode(stdout, rb)");
return 1;
}
#endif
}
bool fromStdin= srcFilename=="-";
int64_t llFileSize= fromStdin ? -1 : GetFileSize(srcFilename);
if (!llBaseOffset)
llBaseOffset = 0;
// determine start
if (!llOffset)
llOffset = llBaseOffset;
else if (llOffset.value() < 0) {
if (fromStdin || llFileSize < 0) {
printf("dumping end of stdin stream not yet implemented\n");
return 1;
}
llOffset = llBaseOffset.value() + llOffset.value() + llFileSize;
}
// determine end
if (!llLength && !llEndOffset)
{
if (llFileSize >= 0) {
llEndOffset = llBaseOffset.value() + llFileSize;
llLength = llEndOffset.value() - llOffset.value();
}
// else: filesize, length unknown - until EOF.
else {
llEndOffset = INT64_MAX;
llLength = INT64_MAX;
}
}
else if (!llLength && llEndOffset) {
if (llEndOffset < 0) {
if (fromStdin || llFileSize < 0) {
printf("Can't use negative offsets, on stdin\n");
return 1;
}
llEndOffset = llEndOffset.value() + llFileSize;
}
llLength = llEndOffset.value() - llOffset.value();
}
else if (llLength && !llEndOffset) {
if (llLength < 0) {
printf("Can't use negative length\n");
return 1;
}
llEndOffset = llOffset.value() + llLength.value();
}
else {
if (llEndOffset != llOffset.value() + llLength.value()) {
printf("inconsistent use of -l, -o and -e\n");
return 1;
}
}
if (llOffset.value() < llBaseOffset.value()) {
printf("offset must be >= baseoffset\n");
return 1;
}
if (!dstFilename.empty()) {
if (g_llStepSize)
CopyFileSteps(srcFilename, dstFilename, llBaseOffset.value(), llOffset.value(), llLength.value());
else
Copyfile(srcFilename, dstFilename, llBaseOffset.value(), llOffset.value(), llLength.value());
}
else {
if (g_llStepSize)
StepFile(srcFilename, llBaseOffset.value(), llOffset.value(), llLength.value());
else
Dumpfile(srcFilename, llBaseOffset.value(), llOffset.value(), llLength.value());
}
return 0;
}