-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathziplist
executable file
·267 lines (238 loc) · 14.2 KB
/
ziplist
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
#!/usr/bin/perl
# todo: check if installed unzip supports -I/-O options
# todo: allow to override detected ANSI and OEM code pages
# see also https://rt.cpan.org/Public/Bug/Display.html?id=35334
use POSIX qw(setlocale locale_h LC_ALL);
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
use open qw/ :std OUT :encoding(UTF-8) /;
use Encode qw/ decode /;
use Term::ANSIColor;
use Data::Dumper qw(Dumper);
#print Dumper \@var;
# Locale to code page translation tables
# from https://github.com/unxed/oemcp/
# Generated from Wine sources
$ltoem{'af_ZA'} = 850; $ltoem{'ar_SA'} = 720; $ltoem{'ar_LB'} = 720; $ltoem{'ar_EG'} = 720;
$ltoem{'ar_DZ'} = 720; $ltoem{'ar_BH'} = 720; $ltoem{'ar_IQ'} = 720; $ltoem{'ar_JO'} = 720;
$ltoem{'ar_KW'} = 720; $ltoem{'ar_LY'} = 720; $ltoem{'ar_MA'} = 720; $ltoem{'ar_OM'} = 720;
$ltoem{'ar_QA'} = 720; $ltoem{'ar_SY'} = 720; $ltoem{'ar_TN'} = 720; $ltoem{'ar_AE'} = 720;
$ltoem{'ar_YE'} = 720; $ltoem{'ast_ES'} = 850; $ltoem{'az_AZ'} = 866; $ltoem{'az_AZ'} = 857;
$ltoem{'be_BY'} = 866; $ltoem{'bg_BG'} = 866; $ltoem{'br_FR'} = 850; $ltoem{'ca_ES'} = 850;
$ltoem{'zh_CN'} = 936; $ltoem{'zh_TW'} = 950; $ltoem{'kw_GB'} = 850; $ltoem{'cs_CZ'} = 852;
$ltoem{'cy_GB'} = 850; $ltoem{'da_DK'} = 850; $ltoem{'de_AT'} = 850; $ltoem{'de_LI'} = 850;
$ltoem{'de_LU'} = 850; $ltoem{'de_CH'} = 850; $ltoem{'de_DE'} = 850; $ltoem{'el_GR'} = 737;
$ltoem{'en_AU'} = 850; $ltoem{'en_CA'} = 850; $ltoem{'en_GB'} = 850; $ltoem{'en_IE'} = 850;
$ltoem{'en_JM'} = 850; $ltoem{'en_BZ'} = 850; $ltoem{'en_PH'} = 437; $ltoem{'en_ZA'} = 437;
$ltoem{'en_TT'} = 850; $ltoem{'en_US'} = 437; $ltoem{'en_ZW'} = 437; $ltoem{'en_NZ'} = 850;
$ltoem{'es_PA'} = 850; $ltoem{'es_BO'} = 850; $ltoem{'es_CR'} = 850; $ltoem{'es_DO'} = 850;
$ltoem{'es_SV'} = 850; $ltoem{'es_EC'} = 850; $ltoem{'es_GT'} = 850; $ltoem{'es_HN'} = 850;
$ltoem{'es_NI'} = 850; $ltoem{'es_CL'} = 850; $ltoem{'es_MX'} = 850; $ltoem{'es_ES'} = 850;
$ltoem{'es_CO'} = 850; $ltoem{'es_ES'} = 850; $ltoem{'es_PE'} = 850; $ltoem{'es_AR'} = 850;
$ltoem{'es_PR'} = 850; $ltoem{'es_VE'} = 850; $ltoem{'es_UY'} = 850; $ltoem{'es_PY'} = 850;
$ltoem{'et_EE'} = 775; $ltoem{'eu_ES'} = 850; $ltoem{'fa_IR'} = 720; $ltoem{'fi_FI'} = 850;
$ltoem{'fo_FO'} = 850; $ltoem{'fr_FR'} = 850; $ltoem{'fr_BE'} = 850; $ltoem{'fr_CA'} = 850;
$ltoem{'fr_LU'} = 850; $ltoem{'fr_MC'} = 850; $ltoem{'fr_CH'} = 850; $ltoem{'ga_IE'} = 437;
$ltoem{'gd_GB'} = 850; $ltoem{'gv_IM'} = 850; $ltoem{'gl_ES'} = 850; $ltoem{'he_IL'} = 862;
$ltoem{'hr_HR'} = 852; $ltoem{'hu_HU'} = 852; $ltoem{'id_ID'} = 850; $ltoem{'is_IS'} = 850;
$ltoem{'it_IT'} = 850; $ltoem{'it_CH'} = 850; $ltoem{'iv_IV'} = 437; $ltoem{'ja_JP'} = 932;
$ltoem{'kk_KZ'} = 866; $ltoem{'ko_KR'} = 949; $ltoem{'ky_KG'} = 866; $ltoem{'lt_LT'} = 775;
$ltoem{'lv_LV'} = 775; $ltoem{'mk_MK'} = 866; $ltoem{'mn_MN'} = 866; $ltoem{'ms_BN'} = 850;
$ltoem{'ms_MY'} = 850; $ltoem{'nl_BE'} = 850; $ltoem{'nl_NL'} = 850; $ltoem{'nl_SR'} = 850;
$ltoem{'nn_NO'} = 850; $ltoem{'nb_NO'} = 850; $ltoem{'pl_PL'} = 852; $ltoem{'pt_BR'} = 850;
$ltoem{'pt_PT'} = 850; $ltoem{'rm_CH'} = 850; $ltoem{'ro_RO'} = 852; $ltoem{'ru_RU'} = 866;
$ltoem{'sk_SK'} = 852; $ltoem{'sl_SI'} = 852; $ltoem{'sq_AL'} = 852; $ltoem{'sr_RS'} = 855;
$ltoem{'sr_RS'} = 852; $ltoem{'sv_SE'} = 850; $ltoem{'sv_FI'} = 850; $ltoem{'sw_KE'} = 437;
$ltoem{'th_TH'} = 874; $ltoem{'tr_TR'} = 857; $ltoem{'tt_RU'} = 866; $ltoem{'uk_UA'} = 866;
$ltoem{'ur_PK'} = 720; $ltoem{'uz_UZ'} = 866; $ltoem{'uz_UZ'} = 857; $ltoem{'vi_VN'} = 1258;
$ltoem{'wa_BE'} = 850; $ltoem{'zh_HK'} = 950; $ltoem{'zh_SG'} = 936; $ltoem{'zh_MO'} = 950;
$ltansi{'af_ZA'} = 1252; $ltansi{'ar_SA'} = 1256; $ltansi{'ar_LB'} = 1256; $ltansi{'ar_EG'} = 1256;
$ltansi{'ar_DZ'} = 1256; $ltansi{'ar_BH'} = 1256; $ltansi{'ar_IQ'} = 1256; $ltansi{'ar_JO'} = 1256;
$ltansi{'ar_KW'} = 1256; $ltansi{'ar_LY'} = 1256; $ltansi{'ar_MA'} = 1256; $ltansi{'ar_OM'} = 1256;
$ltansi{'ar_QA'} = 1256; $ltansi{'ar_SY'} = 1256; $ltansi{'ar_TN'} = 1256; $ltansi{'ar_AE'} = 1256;
$ltansi{'ar_YE'} = 1256; $ltansi{'ast_ES'} = 1252; $ltansi{'az_AZ'} = 1251; $ltansi{'az_AZ'} = 1254;
$ltansi{'be_BY'} = 1251; $ltansi{'bg_BG'} = 1251; $ltansi{'br_FR'} = 1252; $ltansi{'ca_ES'} = 1252;
$ltansi{'zh_CN'} = 936; $ltansi{'zh_TW'} = 950; $ltansi{'kw_GB'} = 1252; $ltansi{'cs_CZ'} = 1250;
$ltansi{'cy_GB'} = 28604; $ltansi{'da_DK'} = 1252; $ltansi{'de_AT'} = 1252; $ltansi{'de_LI'} = 1252;
$ltansi{'de_LU'} = 1252; $ltansi{'de_CH'} = 1252; $ltansi{'de_DE'} = 1252; $ltansi{'el_GR'} = 1253;
$ltansi{'en_AU'} = 1252; $ltansi{'en_CA'} = 1252; $ltansi{'en_GB'} = 1252; $ltansi{'en_IE'} = 1252;
$ltansi{'en_JM'} = 1252; $ltansi{'en_BZ'} = 1252; $ltansi{'en_PH'} = 1252; $ltansi{'en_ZA'} = 1252;
$ltansi{'en_TT'} = 1252; $ltansi{'en_US'} = 1252; $ltansi{'en_ZW'} = 1252; $ltansi{'en_NZ'} = 1252;
$ltansi{'es_PA'} = 1252; $ltansi{'es_BO'} = 1252; $ltansi{'es_CR'} = 1252; $ltansi{'es_DO'} = 1252;
$ltansi{'es_SV'} = 1252; $ltansi{'es_EC'} = 1252; $ltansi{'es_GT'} = 1252; $ltansi{'es_HN'} = 1252;
$ltansi{'es_NI'} = 1252; $ltansi{'es_CL'} = 1252; $ltansi{'es_MX'} = 1252; $ltansi{'es_ES'} = 1252;
$ltansi{'es_CO'} = 1252; $ltansi{'es_ES'} = 1252; $ltansi{'es_PE'} = 1252; $ltansi{'es_AR'} = 1252;
$ltansi{'es_PR'} = 1252; $ltansi{'es_VE'} = 1252; $ltansi{'es_UY'} = 1252; $ltansi{'es_PY'} = 1252;
$ltansi{'et_EE'} = 1257; $ltansi{'eu_ES'} = 1252; $ltansi{'fa_IR'} = 1256; $ltansi{'fi_FI'} = 1252;
$ltansi{'fo_FO'} = 1252; $ltansi{'fr_FR'} = 1252; $ltansi{'fr_BE'} = 1252; $ltansi{'fr_CA'} = 1252;
$ltansi{'fr_LU'} = 1252; $ltansi{'fr_MC'} = 1252; $ltansi{'fr_CH'} = 1252; $ltansi{'ga_IE'} = 1252;
$ltansi{'gd_GB'} = 1252; $ltansi{'gv_IM'} = 1252; $ltansi{'gl_ES'} = 1252; $ltansi{'he_IL'} = 1255;
$ltansi{'hr_HR'} = 1250; $ltansi{'hu_HU'} = 1250; $ltansi{'id_ID'} = 1252; $ltansi{'is_IS'} = 1252;
$ltansi{'it_IT'} = 1252; $ltansi{'it_CH'} = 1252; $ltansi{'iv_IV'} = 1252; $ltansi{'ja_JP'} = 932;
$ltansi{'kk_KZ'} = 1251; $ltansi{'ko_KR'} = 949; $ltansi{'ky_KG'} = 1251; $ltansi{'lt_LT'} = 1257;
$ltansi{'lv_LV'} = 1257; $ltansi{'mk_MK'} = 1251; $ltansi{'mn_MN'} = 1251; $ltansi{'ms_BN'} = 1252;
$ltansi{'ms_MY'} = 1252; $ltansi{'nl_BE'} = 1252; $ltansi{'nl_NL'} = 1252; $ltansi{'nl_SR'} = 1252;
$ltansi{'nn_NO'} = 1252; $ltansi{'nb_NO'} = 1252; $ltansi{'pl_PL'} = 1250; $ltansi{'pt_BR'} = 1252;
$ltansi{'pt_PT'} = 1252; $ltansi{'rm_CH'} = 1252; $ltansi{'ro_RO'} = 1250; $ltansi{'ru_RU'} = 1251;
$ltansi{'sk_SK'} = 1250; $ltansi{'sl_SI'} = 1250; $ltansi{'sq_AL'} = 1250; $ltansi{'sr_RS'} = 1251;
$ltansi{'sr_RS'} = 1250; $ltansi{'sv_SE'} = 1252; $ltansi{'sv_FI'} = 1252; $ltansi{'sw_KE'} = 1252;
$ltansi{'th_TH'} = 874; $ltansi{'tr_TR'} = 1254; $ltansi{'tt_RU'} = 1251; $ltansi{'uk_UA'} = 1251;
$ltansi{'ur_PK'} = 1256; $ltansi{'uz_UZ'} = 1251; $ltansi{'uz_UZ'} = 1254; $ltansi{'vi_VN'} = 1258;
$ltansi{'wa_BE'} = 1252; $ltansi{'zh_HK'} = 950; $ltansi{'zh_SG'} = 936; $ltansi{'zh_MO'} = 950;
# Detect OEM and ANSI code page
# for current locale
$lc = setlocale(LC_ALL);
@lc = split /\./, $lc;
$lc = @lc[0];
$cp_oem = $ltoem{$lc};
$cp_ansi = $ltansi{$lc};
if (!$cp_oem) { $cp_oem = 437; }
if (!$cp_ansi) { $cp_ansi = 1252; }
$cp_oem = 'cp' . $cp_oem;
$cp_ansi = 'cp' . $cp_ansi;
($zipFile, $option) = @ARGV;
$zipFile = decode('UTF-8', $zipFile);
if (!$zipFile) { print "Usage: ziplist[.pl] zipfile.zip [-p]\n"; exit; }
my $zip = Archive::Zip->new();
unless ( $zip->read( $zipFile ) == AZ_OK ) { # Make sure archive got read
die 'read error';
}
$all_ok = 1;
foreach my $member ($zip->members()) {
# print "localExtraField: " . $member->localExtraField . " \n"; # always empty for some reason
# print "cdExtraField: " . $member->cdExtraField . " \n";
# parse cdExtraField to extract 0x7075 UTF-8 filename if possible
# see https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT
$filename_7075 = ''; $counter = 0;
while ($counter < length($member->cdExtraField)) {
$size = ord(substr($member->cdExtraField, $counter + 3, 1)) * 256 +
ord(substr($member->cdExtraField, $counter + 2, 1));
if ((ord(substr($member->cdExtraField, $counter + 1, 1)) == 0x70) &&
(ord(substr($member->cdExtraField, $counter, 1)) == 0x75))
{
$filename_7075 = decode('UTF-8',
substr($member->cdExtraField, $counter + 4 + 5, $size - 5));
}
$counter += (4 + $size);
}
# main magic is here, taken from far2l's "multiarc" zip reader
$convert_charset = '';
if ($member->{bitFlag} & 0x0800) {
# 0x0800 UTF-8 flag is set? assume "filename" filed is in utf-8
$filename = $member->fileName;
$filename_source = "Legacy in UTF-8";
} elsif (($member->fileAttributeFormat == 11) && ($member->versionMadeBy >= 20) && ($member->versionMadeBy < 25)) {
# packed on win by winzip 20..24? assume "filename" field is in ANSI charser
$convert_charset = $cp_ansi;
$filename = decode($convert_charset, $member->fileName);
$filename_source = "Legacy in ANSI";
} elsif (($member->fileAttributeFormat == 11) || ($member->fileAttributeFormat == 0)) {
# packed on dos or win? assume "filename" field is in UTF-8 charser
$convert_charset = $cp_oem;
$filename = decode($convert_charset, $member->fileName);
$filename_source = "Legacy in OEM";
} else {
# all other cases - assume UTF-8
# macos, for example
# why do we need utf-8 decode here, but not need in 0x0800 check branch?
# possibly bug in Archive::Zip
$filename = decode('utf-8', $member->fileName);
$filename_source = "Legacy in UTF-8";
}
$filename_legacy = $filename;
if ($filename_7075) {
# 0x7075 "UTF-8 filename" field is present? let's override detected filename with it
# (but save $convert_charset for possible later usage)
$filename = $filename_7075;
$filename_source = "0x7075";
}
=pdo
print "\n";
$filename_raw = $member->fileName;
$filename_hex = $member->fileName;
$filename_hex =~ s/(.)/sprintf '%04x ', ord $1/seg;
print "Legacy filename (raw): " . $filename_raw . "\n";
print "Legacy filename (raw hex): " . $filename_hex . "\n";
=cut
print "\n";
print "Packer version: " . $member->versionMadeBy . "\n";
print "Packer OS: " . $member->fileAttributeFormat . "\n";
print "0x0800 flag: " . (($member->{bitFlag} & 0x0800) ? 'yes' : 'no') . "\n"; # UTF-8 flag
print "Legacy filename encoding for current locale: " . ($convert_charset ? $convert_charset : 'utf-8') . "\n";
print "Legacy filename (decoded): " . $filename_legacy . "\n";
print "0x7075 UTF-8 filename: " . ($filename_7075 ? $filename_7075 : 'none') . "\n";
print "Filename, best guess: " . color('yellow') . $filename . color('reset') . "\n";
print "Best guess source: " . $filename_source . "\n";
# Generating switches for archivers
if ($filename_7075) { $convert_charset = ''; } # use 0x7075 if possible
$bsdtar = ''; $unzip = ''; $lsar = '';
if ($convert_charset) {
$bsdtar = '--option hdrcharset=' . $convert_charset;
}
if ($convert_charset) {
$unzip = '-I ' . $convert_charset . ' -O ' . $convert_charset;
} else {
# needed for google forms-generated zips
$unzip = '-I utf-8 -O utf-8';
}
if ($convert_charset) {
$lsar = '-e ' . $convert_charset;
}
print "Suggested additional bsdtar switches: " . ($bsdtar ? $bsdtar : 'none') . "\n";
print "Suggested additional unzip switches: " . ($unzip ? $unzip : 'none') . "\n";
print "Suggested additional lsar switches: " . ($lsar ? $lsar : 'none') . "\n";
# As "zip -d" does not support any charset option, you have to сonvert
# filenames you are planning to remove from archive
# to "Legacy filename encoding for current locale" (if it is not utf-8), like
# zip -d ARCHIVE.zip "`echo FILENAME_TO_DELETE | iconv -f utf-8 -t CHARSET`"
# This sometimes may require your system locale to match archive charset,
# but that's the best thing we can do with "zip -d"
# Escaping quotes in filename and zipfile before calling shell
$filename_unescaped = $filename;
$filename =~ s/"/\\"/g;
$zipFile =~ s/"/\\"/g;
# Probing archivers
if ($option eq '-p') {
print "Probing with bsdtar: ";
$cmd = 'bsdtar t ' . $bsdtar . ' -f "' . $zipFile . '" "' . $filename . '"';
print color('cyan');
print $cmd . "\n";
print color('red');
$ret = decode('utf-8', qx($cmd));
if (!$? && (index($ret, $filename_unescaped) != -1)) { print color('green'); } else { $all_ok = ''; }
print $ret;
print color('reset');
print "Probing with unzip: ";
$cmd = 'unzip ' . $unzip . ' -l "' . $zipFile . '" "' . $filename . '"';
print color('cyan');
print $cmd . "\n";
print color('red');
$ret = decode('utf-8', qx($cmd));
if (!$? && (index($ret, $filename_unescaped) != -1)) { print color('green'); } else { $all_ok = ''; }
print $ret;
print color('reset');
# "zip -d" has no charset option, but cat be supplied with transcoded command line
# We can not implement probing with "zip -d" without actually deleting the file
# TODO: copy file to temporary one, do "zip -d" probe, mv file back
print "Probing with lsar [unar package]: ";
$filename_for_lsar = $filename; $filename_for_lsar =~ s/\/$//ig; # lsar do not understands "folder/"
$cmd = 'lsar ' . $lsar . ' "' . $zipFile . '" "' . $filename_for_lsar . '"';
print color('cyan');
print $cmd . "\n";
if (index($filename, '._') == -1) {
print color('red');
$ret = decode('utf-8', qx($cmd));
if (!$? && (index($ret, $filename_unescaped) != -1)) { print color('green'); } else { $all_ok = ''; }
print $ret;
print color('reset');
} else {
print color('reset');
print "Skipped lsar probe for file with '._' in name: lsar will not list it anyway\n";
}
}
}
if ($option eq '-p') {
print "\nLast filename, best guess: " . color('yellow') . $filename . color('reset') . "\n";
print "Probing result: " .
( $all_ok ? (color('green') . 'PASSED') : (color('red') . 'FAIL') ) .
color('reset') . "\n";
}