-
Notifications
You must be signed in to change notification settings - Fork 16
/
zipdetails
executable file
·8103 lines (6300 loc) · 231 KB
/
zipdetails
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
#!/usr/bin/perl
# zipdetails
#
# Display info on the contents of a Zip file
#
use 5.010; # for unpack "Q<"
my $NESTING_DEBUG = 0 ;
BEGIN {
# Check for a 32-bit Perl
if (!eval { pack "Q", 1 }) {
warn "zipdetails requires 64 bit integers, ",
"this Perl has 32 bit integers.\n";
exit(1);
}
}
BEGIN { pop @INC if $INC[-1] eq '.' }
use strict;
use warnings ;
no warnings 'portable'; # for unpacking > 2^32
use feature qw(state say);
use IO::File;
use Encode;
use Getopt::Long;
use List::Util qw(min max);
my $VERSION = '4.004' ;
sub fatal_tryWalk;
sub fatal_truncated ;
sub info ;
sub warning ;
sub error ;
sub debug ;
sub fatal ;
sub topLevelFatal ;
sub internalFatal;
sub need ;
sub decimalHex;
use constant MAX64 => 0xFFFFFFFFFFFFFFFF ;
use constant MAX32 => 0xFFFFFFFF ;
use constant MAX16 => 0xFFFF ;
# Compression types
use constant ZIP_CM_STORE => 0 ;
use constant ZIP_CM_IMPLODE => 6 ;
use constant ZIP_CM_DEFLATE => 8 ;
use constant ZIP_CM_BZIP2 => 12 ;
use constant ZIP_CM_LZMA => 14 ;
use constant ZIP_CM_PPMD => 98 ;
# General Purpose Flag
use constant ZIP_GP_FLAG_ENCRYPTED_MASK => (1 << 0) ;
use constant ZIP_GP_FLAG_STREAMING_MASK => (1 << 3) ;
use constant ZIP_GP_FLAG_PATCHED_MASK => (1 << 5) ;
use constant ZIP_GP_FLAG_STRONG_ENCRYPTED_MASK => (1 << 6) ;
use constant ZIP_GP_FLAG_LZMA_EOS_PRESENT => (1 << 1) ;
use constant ZIP_GP_FLAG_LANGUAGE_ENCODING => (1 << 11) ;
use constant ZIP_GP_FLAG_PKWARE_ENHANCED_COMP => (1 << 12) ;
use constant ZIP_GP_FLAG_ENCRYPTED_CD => (1 << 13) ;
# All the encryption flags
use constant ZIP_GP_FLAG_ALL_ENCRYPT => (ZIP_GP_FLAG_ENCRYPTED_MASK | ZIP_GP_FLAG_STRONG_ENCRYPTED_MASK | ZIP_GP_FLAG_ENCRYPTED_CD );
# Internal File Attributes
use constant ZIP_IFA_TEXT_MASK => 1;
# Signatures for each of the headers
use constant ZIP_LOCAL_HDR_SIG => 0x04034b50;
use constant ZIP_DATA_HDR_SIG => 0x08074b50;
use constant ZIP_CENTRAL_HDR_SIG => 0x02014b50;
use constant ZIP_END_CENTRAL_HDR_SIG => 0x06054b50;
use constant ZIP64_END_CENTRAL_REC_HDR_SIG => 0x06064b50;
use constant ZIP64_END_CENTRAL_LOC_HDR_SIG => 0x07064b50;
use constant ZIP_DIGITAL_SIGNATURE_SIG => 0x05054b50;
use constant ZIP_ARCHIVE_EXTRA_DATA_RECORD_SIG => 0x08064b50;
use constant ZIP_SINGLE_SEGMENT_MARKER => 0x30304b50; # APPNOTE 6.3.10, sec 8.5.4
# Extra sizes
use constant ZIP_EXTRA_HEADER_SIZE => 2 ;
use constant ZIP_EXTRA_MAX_SIZE => 0xFFFF ;
use constant ZIP_EXTRA_SUBFIELD_ID_SIZE => 2 ;
use constant ZIP_EXTRA_SUBFIELD_LEN_SIZE => 2 ;
use constant ZIP_EXTRA_SUBFIELD_HEADER_SIZE => ZIP_EXTRA_SUBFIELD_ID_SIZE +
ZIP_EXTRA_SUBFIELD_LEN_SIZE;
use constant ZIP_EXTRA_SUBFIELD_MAX_SIZE => ZIP_EXTRA_MAX_SIZE -
ZIP_EXTRA_SUBFIELD_HEADER_SIZE;
use constant ZIP_EOCD_MIN_SIZE => 22 ;
use constant ZIP_LD_FILENAME_OFFSET => 30;
use constant ZIP_CD_FILENAME_OFFSET => 46;
my %ZIP_CompressionMethods =
(
0 => 'Stored',
1 => 'Shrunk',
2 => 'Reduced compression factor 1',
3 => 'Reduced compression factor 2',
4 => 'Reduced compression factor 3',
5 => 'Reduced compression factor 4',
6 => 'Imploded',
7 => 'Reserved for Tokenizing compression algorithm',
8 => 'Deflated',
9 => 'Deflate64',
10 => 'PKWARE Data Compression Library Imploding',
11 => 'Reserved by PKWARE',
12 => 'BZIP2',
13 => 'Reserved by PKWARE',
14 => 'LZMA',
15 => 'Reserved by PKWARE',
16 => 'IBM z/OS CMPSC Compression',
17 => 'Reserved by PKWARE',
18 => 'IBM/TERSE or Xceed BWT', # APPNOTE has IBM/TERSE. Xceed reuses it unofficially
19 => 'IBM LZ77 z Architecture (PFS)',
20 => 'Ipaq8', # see https://encode.su/threads/1048-info-zip-lpaq8
92 => 'Reference', # Winzip Only from version 25
93 => 'Zstandard',
94 => 'MP3',
95 => 'XZ',
96 => 'WinZip JPEG Compression',
97 => 'WavPack compressed data',
98 => 'PPMd version I, Rev 1',
99 => 'AES Encryption', # Apple also use this code for LZFSE compression in IPA files
);
my %OS_Lookup = (
0 => "MS-DOS",
1 => "Amiga",
2 => "OpenVMS",
3 => "Unix",
4 => "VM/CMS",
5 => "Atari ST",
6 => "HPFS (OS/2, NT 3.x)",
7 => "Macintosh",
8 => "Z-System",
9 => "CP/M",
10 => "Windows NTFS or TOPS-20",
11 => "MVS or NTFS",
12 => "VSE or SMS/QDOS",
13 => "Acorn RISC OS",
14 => "VFAT",
15 => "alternate MVS",
16 => "BeOS",
17 => "Tandem",
18 => "OS/400",
19 => "OS/X (Darwin)",
30 => "AtheOS/Syllable",
);
{
package Signatures ;
my %Lookup = (
# Map unpacked signature to
# decoder
# name
# central flag
# Core Signatures
::ZIP_LOCAL_HDR_SIG, [ \&::LocalHeader, "Local File Header", 0 ],
::ZIP_DATA_HDR_SIG, [ \&::DataDescriptor, "Data Descriptor", 0 ],
::ZIP_CENTRAL_HDR_SIG, [ \&::CentralHeader, "Central Directory Header", 1 ],
::ZIP_END_CENTRAL_HDR_SIG, [ \&::EndCentralHeader, "End Central Directory Record", 1 ],
::ZIP_SINGLE_SEGMENT_MARKER, [ \&::SingleSegmentMarker, "Split Archive Single Segment Marker", 0],
# Zip64
::ZIP64_END_CENTRAL_REC_HDR_SIG, [ \&::Zip64EndCentralHeader, "Zip64 End of Central Directory Record", 1 ],
::ZIP64_END_CENTRAL_LOC_HDR_SIG, [ \&::Zip64EndCentralLocator, "Zip64 End of Central Directory Locator", 1 ],
# Digital signature (pkzip)
::ZIP_DIGITAL_SIGNATURE_SIG, [ \&::DigitalSignature, "Digital Signature", 1 ],
# Archive Encryption Headers (pkzip) - never seen this one
::ZIP_ARCHIVE_EXTRA_DATA_RECORD_SIG, [ \&::ArchiveExtraDataRecord, "Archive Extra Record", 1 ],
);
sub decoder
{
my $signature = shift ;
return undef
unless exists $Lookup{$signature};
return $Lookup{$signature}[0];
}
sub name
{
my $signature = shift ;
return 'UNKNOWN'
unless exists $Lookup{$signature};
return $Lookup{$signature}[1];
}
sub titleName
{
my $signature = shift ;
uc name($signature);
}
sub hexValue
{
my $signature = shift ;
sprintf "0x%X", $signature ;
}
sub hexValue32
{
my $signature = shift ;
sprintf "0x%08X", $signature ;
}
sub hexValue16
{
my $signature = shift ;
sprintf "0x%04X", $signature ;
}
sub nameAndHex
{
my $signature = shift ;
return "'" . name($signature) . "' (" . hexValue32($signature) . ")"
}
sub isCentralHeader
{
my $signature = shift ;
return undef
unless exists $Lookup{$signature};
return $Lookup{$signature}[2];
}
#sub isValidSignature
#{
# my $signature = shift ;
# return exists $Lookup{$signature}}
#}
sub getSigsForScan
{
my %sigs =
# map { $_ => 1 }
# map { substr($_->[0], 2, 2) => $_->[1] } # don't want the initial "PK"
map { substr(pack("V", $_), 2, 2) => $_ }
keys %Lookup ;
return %sigs;
}
}
my %Extras = (
# Local Central
# ID Name Handler min size max size min size max size
0x0001, ['ZIP64', \&decode_Zip64, 0, 28, 0, 28],
0x0007, ['AV Info', undef], # TODO
0x0008, ['Extended Language Encoding', undef], # TODO
0x0009, ['OS/2 extended attributes', undef], # TODO
0x000a, ['NTFS FileTimes', \&decode_NTFS_Filetimes, 32, 32, 32, 32],
0x000c, ['OpenVMS', \&decode_OpenVMS, 4, undef, 4, undef],
0x000d, ['Unix', undef],
0x000e, ['Stream & Fork Descriptors', undef], # TODO
0x000f, ['Patch Descriptor', undef],
0x0014, ['PKCS#7 Store for X.509 Certificates', undef],
0x0015, ['X.509 Certificate ID and Signature for individual file', undef],
0x0016, ['X.509 Certificate ID for Central Directory', undef],
0x0017, ['Strong Encryption Header', \&decode_strong_encryption, 12, undef, 12, undef],
0x0018, ['Record Management Controls', undef],
0x0019, ['PKCS#7 Encryption Recipient Certificate List', undef],
0x0020, ['Reserved for Timestamp record', undef],
0x0021, ['Policy Decryption Key Record', undef],
0x0022, ['Smartcrypt Key Provider Record', undef],
0x0023, ['Smartcrypt Policy Key Data Record', undef],
# The Header ID mappings defined by Info-ZIP and third parties are:
0x0065, ['IBM S/390 attributes - uncompressed', \&decode_MVS, 4, undef, 4, undef],
0x0066, ['IBM S/390 attributes - compressed', undef],
0x07c8, ['Info-ZIP Macintosh (old, J. Lee)', undef],
0x10c5, ['Minizip CMS Signature', \&decode_Minizip_Signature, undef, undef, undef, undef], # https://github.com/zlib-ng/minizip-ng/blob/master/doc/mz_extrafield.md
0x1986, ['Pixar USD', undef], # TODO
0x1a51, ['Minizip Hash', \&decode_Minizip_Hash, 4, undef, 4, undef], # https://github.com/zlib-ng/minizip-ng/blob/master/doc/mz_extrafield.md
0x2605, ['ZipIt Macintosh (first version)', undef],
0x2705, ['ZipIt Macintosh v 1.3.5 and newer (w/o full filename)', undef],
0x2805, ['ZipIt Macintosh v 1.3.5 and newer', undef],
0x334d, ["Info-ZIP Macintosh (new, D. Haase's 'Mac3' field)", undef], # TODO
0x4154, ['Tandem NSK [TA]', undef], # TODO
0x4341, ['Acorn/SparkFS [AC]', undef], # TODO
0x4453, ['Windows NT security descriptor [SD]', \&decode_NT_security, 11, undef, 4, 4], # TODO
0x4690, ['POSZIP 4690', undef],
0x4704, ['VM/CMS', undef],
0x470f, ['MVS', undef],
0x4854, ['Theos [TH]', undef],
0x4b46, ['FWKCS MD5 [FK]', undef],
0x4c41, ['OS/2 access control list [AL]', undef],
0x4d49, ['Info-ZIP OpenVMS (obsolete) [IM]', undef],
0x4d63, ['Macintosh SmartZIP [cM]', undef], # TODO
0x4f4c, ['Xceed original location [LO]', undef],
0x5356, ['AOS/VS (binary ACL) [VS]', undef],
0x5455, ['Extended Timestamp [UT]', \&decode_UT, 1, 13, 1, 13],
0x554e, ['Xceed unicode extra field [UN]', \&decode_Xceed_unicode, 6, undef, 8, undef],
0x564B, ['Key-Value Pairs [KV]', \&decode_Key_Value_Pair, 13, undef, 13, undef],# TODO -- https://github.com/sozip/keyvaluepairs-spec/blob/master/zip_keyvalue_extra_field_specification.md
0x5855, ['Unix Extra type 1 [UX]', \&decode_UX, 12, 12, 8, 8],
0x5a4c, ['ZipArchive Unicode Filename [LZ]', undef], # https://www.artpol-software.com/ZipArchive
0x5a4d, ['ZipArchive Offsets Array [MZ]', undef], # https://www.artpol-software.com/ZipArchive
0x6375, ['Unicode Comment [uc]', \&decode_uc, 5, undef, 5, undef],
0x6542, ['BeOS/Haiku [Be]', undef], # TODO
0x6854, ['Theos [Th]', undef],
0x7075, ['Unicode Path [up]', \&decode_up, 5, undef, 5, undef],
0x756e, ['ASi Unix [un]', \&decode_ASi_Unix], # TODO
0x7441, ['AtheOS [At]', undef],
0x7855, ['Unix Extra type 2 [Ux]', \&decode_Ux, 4,4, 0, 0 ],
0x7875, ['Unix Extra type 3 [ux]', \&decode_ux, 3, undef, 3, undef],
0x9901, ['AES Encryption', \&decode_AES, 7, 7, 7, 7],
0x9903, ['Reference', \&decode_Reference, 20, 20, 20, 20], # Added in WinZip ver 25
0xa11e, ['Data Stream Alignment', \&decode_DataStreamAlignment, 2, undef, 2, undef ],
0xA220, ['Open Packaging Growth Hint', \&decode_GrowthHint, 4, undef, 4, undef ],
0xCAFE, ['Java Executable', \&decode_Java_exe, 0, 0, 0, 0],
0xCDCD, ['Minizip Central Directory', \&decode_Minizip_CD, 8, 8, 8, 8], # https://github.com/zlib-ng/minizip-ng/blob/master/doc/mz_extrafield.md
0xd935, ['Android APK Alignment', undef], # TODO
0xE57a, ['ALZip Codepage', undef], # TODO
0xfb4a, ['SMS/QDOS', undef], # TODO
);
# Dummy entry only used in test harness, so only enable when ZIPDETAILS_TESTHARNESS is set
$Extras{0xFFFF} =
['DUMMY', \&decode_DUMMY, undef, undef, undef, undef]
if $ENV{ZIPDETAILS_TESTHARNESS} ;
sub extraFieldIdentifier
{
my $id = shift ;
my $name = $Extras{$id}[0] // "Unknown";
return "Extra Field '$name' (ID " . hexValue16($id) .")";
}
# Zip64EndCentralHeader version 2
my %HashIDLookup = (
0x0000 => 'none',
0x0001 => 'CRC32',
0x8003 => 'MD5',
0x8004 => 'SHA1',
0x8007 => 'RIPEMD160',
0x800C => 'SHA256',
0x800D => 'SHA384',
0x800E => 'SHA512',
);
# Zip64EndCentralHeader version 2, Strong Encryption Header & DecryptionHeader
my %AlgIdLookup = (
0x6601 => "DES",
0x6602 => "RC2 (version needed to extract < 5.2)",
0x6603 => "3DES 168",
0x6609 => "3DES 112",
0x660E => "AES 128",
0x660F => "AES 192",
0x6610 => "AES 256",
0x6702 => "RC2 (version needed to extract >= 5.2)",
0x6720 => "Blowfish",
0x6721 => "Twofish",
0x6801 => "RC4",
0xFFFF => "Unknown algorithm",
);
# Zip64EndCentralHeader version 2, Strong Encryption Header & DecryptionHeader
my %FlagsLookup = (
0x0001 => "Password required to decrypt",
0x0002 => "Certificates only",
0x0003 => "Password or certificate required to decrypt",
# Values > 0x0003 reserved for certificate processing
);
# Strong Encryption Header & DecryptionHeader
my %HashAlgLookup = (
0x8004 => 'SHA1',
);
my $FH;
my $ZIP64 = 0 ;
my $NIBBLES = 8;
my $LocalHeaderCount = 0;
my $CentralHeaderCount = 0;
my $InfoCount = 0;
my $WarningCount = 0;
my $ErrorCount = 0;
my $lastWasMessage = 0;
my $fatalDisabled = 0;
my $OFFSET = 0 ;
# Prefix data
my $POSSIBLE_PREFIX_DELTA = 0;
my $PREFIX_DELTA = 0;
my $TRAILING = 0 ;
my $PAYLOADLIMIT = 256;
my $ZERO = 0 ;
my $APK = 0 ;
my $START_APK = 0;
my $APK_LEN = 0;
my $CentralDirectory = CentralDirectory->new();
my $LocalDirectory = LocalDirectory->new();
my $HeaderOffsetIndex = HeaderOffsetIndex->new();
my $EOCD_Present = 0;
sub prOff
{
my $offset = shift;
my $s = offset($OFFSET);
$OFFSET += $offset;
return $s;
}
sub offset
{
my $v = shift ;
sprintf("%0${NIBBLES}X", $v);
}
# Format variables
my ($OFF, $ENDS_AT, $LENGTH, $CONTENT, $TEXT, $VALUE) ;
my $FMT1 = 'STDOUT1';
my $FMT2 = 'STDOUT2';
sub setupFormat
{
my $wantVerbose = shift ;
my $nibbles = shift;
my $width = '@' . ('>' x ($nibbles -1));
my $space = " " x length($width);
# See https://github.com/Perl/perl5/issues/14255 for issue with "^*" in perl < 5.22
# my $rightColumn = "^*" ;
my $rightColumn = "^" . ("<" x 132);
# Fill mode can split on space or newline chars
# Spliting on hyphen works differently from Perl 5.20 onwards
$: = " \n";
my $fmt ;
if ($wantVerbose) {
eval "format $FMT1 =
$width $width $width ^<<<<<<<<<<<^<<<<<<<<<<<<<<<<<<<< $rightColumn
\$OFF, \$ENDS_AT, \$LENGTH, \$CONTENT, \$TEXT, \$VALUE
$space $space $space ^<<<<<<<<<<<^<<<<<<<<<<<<<<<<<<<< $rightColumn~~
\$CONTENT, \$TEXT, \$VALUE
.
";
eval "format $FMT2 =
$width $width $width ^<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<< $rightColumn
\$OFF, \$ENDS_AT, \$LENGTH, \$CONTENT, \$TEXT, \$VALUE
$space $space $space ^<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<< $rightColumn~~
\$CONTENT, \$TEXT, \$VALUE
.
";
}
else {
eval "format $FMT1 =
$width ^<<<<<<<<<<<<<<<<<<<< $rightColumn
\$OFF, \$TEXT, \$VALUE
$space ^<<<<<<<<<<<<<<<<<<<< $rightColumn~~
\$TEXT, \$VALUE
.
";
eval "format $FMT2 =
$width ^<<<<<<<<<<<<<<<<<< $rightColumn
\$OFF, \$TEXT, \$VALUE
$space ^<<<<<<<<<<<<<<<<<< $rightColumn~~
\$TEXT, \$VALUE
.
"
}
no strict 'refs';
open($FMT1, ">&", \*STDOUT); select $FMT1; $| = 1 ;
open($FMT2, ">&", \*STDOUT); select $FMT2; $| = 1 ;
select 'STDOUT';
$| = 1;
}
sub mySpr
{
my $format = shift ;
return "" if ! defined $format;
return $format unless @_ ;
return sprintf $format, @_ ;
}
sub xDump
{
my $input = shift;
$input =~ tr/\0-\37\177-\377/./;
return $input;
}
sub hexDump
{
return uc join ' ', unpack('(H2)*', $_[0]);
}
sub hexDump16
{
return uc
join "\r",
map { join ' ', unpack('(H2)*', $_ ) }
unpack('(a16)*', $_[0]) ;
}
sub charDump2
{
sprintf "%v02X", $_[0];
}
sub charDump
{
sprintf "%vX", $_[0];
}
sub hexValue
{
return sprintf("0x%X", $_[0]);
}
sub hexValue32
{
return sprintf("0x%08X", $_[0]);
}
sub hexValue16
{
return sprintf("0x%04X", $_[0]);
}
sub outHexdump
{
my $size = shift;
my $text = shift;
my $limit = shift ;
return 0
if $size == 0;
# TODO - add a limit to data output
# if ($limit)
# {
# outSomeData($size, $text);
# }
# else
{
myRead(my $payload, $size);
out($payload, $text, hexDump16($payload));
}
return $size;
}
sub decimalHex
{
sprintf("%0*X (%u)", $_[1] // 0, $_[0], $_[0])
}
sub decimalHex0x
{
sprintf("0x%0*X (%u)", $_[1] // 0, $_[0], $_[0])
}
sub decimalHex0xUndef
{
return 'Unknown'
if ! defined $_[0];
return decimalHex0x @_;
}
sub out
{
my $data = shift;
my $text = shift;
my $format = shift;
my $size = length($data) ;
$ENDS_AT = offset($OFFSET + ($size ? $size - 1 : 0)) ;
$OFF = prOff($size);
$LENGTH = offset($size) ;
$CONTENT = hexDump($data);
$TEXT = $text;
$VALUE = mySpr $format, @_;
no warnings;
write $FMT1 ;
$lastWasMessage = 0;
}
sub out0
{
my $size = shift;
my $text = shift;
my $format = shift;
$ENDS_AT = offset($OFFSET + ($size ? $size - 1 : 0)) ;
$OFF = prOff($size);
$LENGTH = offset($size) ;
$CONTENT = '...';
$TEXT = $text;
$VALUE = mySpr $format, @_;
write $FMT1;
skip($FH, $size);
$lastWasMessage = 0;
}
sub out1
{
my $text = shift;
my $format = shift;
$ENDS_AT = '' ;
$OFF = '';
$LENGTH = '' ;
$CONTENT = '';
$TEXT = $text;
$VALUE = mySpr $format, @_;
write $FMT1;
$lastWasMessage = 0;
}
sub out2
{
my $data = shift ;
my $text = shift ;
my $format = shift;
my $size = length($data) ;
$ENDS_AT = offset($OFFSET + ($size ? $size - 1 : 0)) ;
$OFF = prOff($size);
$LENGTH = offset($size);
$CONTENT = hexDump($data);
$TEXT = $text;
$VALUE = mySpr $format, @_;
no warnings;
write $FMT2;
$lastWasMessage = 0;
}
sub Value
{
my $letter = shift;
if ($letter eq 'C')
{ return decimalHex($_[0], 2) }
elsif ($letter eq 'v')
{ return decimalHex($_[0], 4) }
elsif ($letter eq 'V')
{ return decimalHex($_[0], 8) }
elsif ($letter eq 'Q<')
{ return decimalHex($_[0], 16) }
else
{ internalFatal undef, "here letter $letter"}
}
sub outer
{
my $name = shift ;
my $unpack = shift ;
my $size = shift ;
my $cb1 = shift ;
my $cb2 = shift ;
myRead(my $buff, $size);
my (@value) = unpack $unpack, $buff;
my $hex = Value($unpack, @value);
if (defined $cb1) {
my $v ;
if (ref $cb1 eq 'CODE') {
$v = $cb1->(@value) ;
}
else {
$v = $cb1 ;
}
$v = "'" . $v unless $v =~ /^'/;
$v .= "'" unless $v =~ /'$/;
$hex .= " $v" ;
}
out $buff, $name, $hex ;
$cb2->(@value)
if defined $cb2 ;
return $value[0];
}
sub out_C
{
my $name = shift ;
my $cb1 = shift ;
my $cb2 = shift ;
outer($name, 'C', 1, $cb1, $cb2);
}
sub out_v
{
my $name = shift ;
my $cb1 = shift ;
my $cb2 = shift ;
outer($name, 'v', 2, $cb1, $cb2);
}
sub out_V
{
my $name = shift ;
my $cb1 = shift ;
my $cb2 = shift ;
outer($name, 'V', 4, $cb1, $cb2);
}
sub out_Q
{
my $name = shift ;
my $cb1 = shift ;
my $cb2 = shift ;
outer($name, 'Q<', 8, $cb1, $cb2);
}
sub outSomeData
{
my $size = shift;
my $message = shift;
my $redact = shift ;
# return if $size == 0;
if ($size > 0) {
if ($size > $PAYLOADLIMIT) {
my $before = $FH->tell();
out0 $size, $message;
} else {
myRead(my $buffer, $size );
$buffer = "X" x $size
if $redact;
out $buffer, $message, xDump $buffer ;
}
}
}
sub outSomeDataParagraph
{
my $size = shift;
my $message = shift;
my $redact = shift ;
return if $size == 0;
print "\n";
outSomeData($size, $message, $redact);
}
sub unpackValue_C
{
Value_v(unpack "C", $_[0]);
}
sub Value_C
{
return decimalHex($_[0], 2);
}
sub unpackValue_v
{
Value_v(unpack "v", $_[0]);
}
sub Value_v
{
return decimalHex($_[0], 4);
}
sub unpackValue_V
{
Value_V(unpack "V", $_[0]);
}
sub Value_V
{
return decimalHex($_[0] // 0, 8);
}
sub unpackValue_Q
{
my $v = unpack ("Q<", $_[0]);
Value_Q($v);
}
sub Value_Q
{
return decimalHex($_[0], 16);
}
sub read_Q
{
my $b ;
myRead($b, 8);
return ($b, unpack ("Q<" , $b));
}
sub read_V
{
my $b ;
myRead($b, 4);
return ($b, unpack ("V", $b));
}
sub read_v
{
my $b ;
myRead($b, 2);
return ($b, unpack "v", $b);
}
sub read_C
{
my $b ;
myRead($b, 1);
return ($b, unpack "C", $b);
}
sub seekTo
{
my $offset = shift ;
my $loc = shift ;
$loc = SEEK_SET
if ! defined $loc ;
$FH->seek($offset, $loc);
$OFFSET = $FH->tell();
}
sub rewindRelative
{
my $offset = shift ;
$FH->seek(-$offset, SEEK_CUR);
# $OFFSET -= $offset;
$OFFSET = $FH->tell();
}
sub deltaToNextSignature
{
my $start = $FH->tell();
my $got = scanForSignature(1);
my $delta = $FH->tell() - $start ;
seekTo($start);
if ($got)
{
return $delta ;
}
return 0 ;
}
sub scanForSignature
{
my $walk = shift // 0;
# $count is only used to when 'walk' is enabled.
# Want to scan for a PK header at the start of the file.
# All other PK headers are should be directly after the previous PK record.
state $count = 0;
$count += $walk;
my %sigs = Signatures::getSigsForScan();
my $start = $FH->tell();
# TODO -- Fix this?
if (1 || $count <= 1) {
my $last = '';
my $offset = 0;
my $buffer ;
BUFFER:
while ($FH->read($buffer, 1024 * 1000))
{
my $combine = $last . $buffer ;
my $ix = 0;
while (1)
{
$ix = index($combine, "PK", $ix) ;
if ($ix == -1)
{
$last = '';
next BUFFER;
}
my $rest = substr($combine, $ix + 2, 2);
if (! $sigs{$rest})
{
$ix += 2;
next;
}
# possible match
my $here = $FH->tell();
seekTo($here - length($combine) + $ix);
my $name = Signatures::name($sigs{$rest});
return $sigs{$rest};
}
$last = substr($combine, $ix+4);
}
}
else {
die "FIX THIS";
return ! $FH->eof();
}
# printf("scanForSignature %X\t%X (%X)\t%s\n", $start, $FH->tell(), $FH->tell() - $start, 'NO MATCH') ;
return 0;
}
my $is64In32 = 0;
my $opt_verbose = 0;
my $opt_scan = 0;
my $opt_walk = 0;
my $opt_Redact = 0;
my $opt_utc = 0;
my $opt_want_info_mesages = 1;
my $opt_want_warning_mesages = 1;
my $opt_want_error_mesages = 1;
my $opt_want_message_exit_status = 0;
my $exit_status_code = 0;
my $opt_help =0;
$Getopt::Long::bundling = 1 ;