-
Notifications
You must be signed in to change notification settings - Fork 4
/
unarwrapper
executable file
·442 lines (410 loc) · 10.4 KB
/
unarwrapper
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
#!/usr/bin/perl
# Use "unarwrapper unar PARAMETERS" instead of "unar PARAMETERS"
# and "unarwrapper lsar PARAMETERS" instead of "lsar PARAMETERS"
# to process all locales-releated stuff automatically
use POSIX qw(setlocale locale_h LC_ALL);
# 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; }
# todo: unar/lsar uses different names for some code pages
# for example, it treats code page "cp932" as "windows-31j"
# so code page name translation is needed here
# see https://github.com/elfmz/far2l/issues/775 for details
# Prepare converters (for "zip -d")
use Text::Iconv;
$to_oem = Text::Iconv->new("utf-8", "cp" . $cp_oem);
$to_ansi = Text::Iconv->new("utf-8", "cp" . $cp_ansi);
# Detect operating mode (zip, zip -d, unzip),
# prepare transcoded arguments for "zip -d"
foreach (@ARGV) {
$unar ||= ($_ eq 'unar');
$lsar ||= ($_ eq 'lsar');
push(@arg, '"' . $_ . '"');
}
if (scalar(@arg) == 1) {
# Info screen
help();
} elsif ($lsar) {
# Trying OEM
foreach (@arg) {
push (@arg2, $_);
if (!$inserted) {
push (@arg2, "-e");
push (@arg2, "cp" . $cp_oem);
$inserted = 1;
}
}
$cmd = join ' ', @arg2;
$ret1 = qx($cmd);
$ec1 = $?;
# Trying UTF-8
@arg2 = '';
$inserted = '';
foreach (@arg) {
push (@arg2, $_);
if (!$inserted) {
push (@arg2, "-e");
push (@arg2, "utf-8");
$inserted = 1;
}
}
$cmd = join ' ', @arg2;
$ret2 = qx($cmd);
$ec2 = $?;
# Too long output probably means wrong charset,
# so prefer most compact one
# fixme: we can not detect ANSI archives this way,
# also this will fail if different files
# use different encodings. the only way to solve this
# is to implement "our own lsar" for .zip's
if (length($ret1) < length($ret2)) {
print $ret1;
exit $ec1 / 256;
} else {
print $ret2;
exit $ec2 / 256;
}
} elsif ($unar) {
# Inject "-e CODEPAGE"
foreach (@arg) {
push (@arg2, $_);
if (!$inserted) {
push (@arg2, "-e");
push (@arg2, "cp" . $cp_oem);
$inserted = 1;
}
}
$cmd = join ' ', @arg2;
$ret = qx($cmd);
if ($? != 0) {
# File not found in archive?
# Retrying as UTF-8
@arg2 = '';
$inserted = '';
foreach (@arg) {
push (@arg2, $_);
if (!$inserted) {
push (@arg2, "-e");
push (@arg2, "utf-8");
$inserted = 1;
}
}
$cmd = join ' ', @arg2;
$ret = qx($cmd);
}
if ($? != 0) {
# File still not found in archive?
# Retrying as ANSI
@arg2 = '';
$inserted = '';
foreach (@arg) {
push (@arg2, $_);
if (!$inserted) {
push (@arg2, "-e");
push (@arg2, "cp" . $cp_ansi);
$inserted = 1;
}
}
$cmd = join ' ', @arg2;
$ret = qx($cmd);
}
print $ret;
exit($?/256);
} else {
if (@arg) {
# Pass to called program "as is"
$cmd = join ' ', @arg;
$ret = qx($cmd);
print $ret;
exit($?/256);
} else {
help();
}
}
sub help() {
print "\nunarwrapper, version 0.1\n\n";
print "This wrapper around unar and lsar tools detects OEM/ANSI code pages\n";
print "from system locale to process .zip archives using correct charset\n";
print "without any user interraction.\n\n";
print "Preserves unar/lsar return codes and output,\n";
print "so can be used with any program utilizing unar/lsar,\n";
print "like File Roller, Engrampa, Ark, far2l, etc.\n\n";
print "License: Public Domain.\n\n";
print "Usage:\n";
print "unarwrapper unar PARAMETERS\n";
print "unarwrapper lsar PARAMETERS\n\n";
print "Use unar --help or lsar --help for more details\n";
}