-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathtzip2_file_compression.pl
317 lines (246 loc) · 7.46 KB
/
tzip2_file_compression.pl
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
#!/usr/bin/perl
# Author: Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 12 August 2013
# Website: https://trizenx.blogspot.com
#
## A very simple file compressor.
#
# Best usage of this script is to compress files which
# contains not so many different bytes (for example, DNA-sequences)
use 5.010;
use strict;
use autodie;
use warnings;
use List::Util qw(min);
use Getopt::Std qw(getopts);
use File::Basename qw(basename);
our $DEBUG = 0;
use constant {
CHUNK_SIZE => 1024, # in bytes
SIGNATURE => 'TZP2' . chr(1),
FORMAT => 'tzp2',
};
sub usage {
my ($code) = @_;
print <<"EOH";
usage: $0 [options] [input file] [output file]
options:
-e : extract
-i <filename> : input filename
-o <filename> : output filename
-r : rewrite output
-v : version number
-h : this message
examples:
$0 document.txt
$0 document.txt archive.${\FORMAT}
$0 archive.${\FORMAT} document.txt
$0 -e -i archive.${\FORMAT} -o document.txt
EOH
exit($code // 0);
}
sub main {
my %opt;
getopts('ei:o:vhr', \%opt);
$opt{h} && usage(0);
$opt{v} && version();
my ($input, $output) = @ARGV;
$input //= $opt{i} // usage(2);
$output //= $opt{o};
my $ext = qr{\.${\FORMAT}\z}io;
if ($opt{e} || $input =~ $ext) {
if (not defined $output) {
($output = basename($input)) =~ s{$ext}{}
|| die "$0: no output file specified!\n";
}
if (not $opt{r} and -e $output) {
print "'$output' already exists! -- Replace? [y/N] ";
<STDIN> =~ /^y/i || exit 17;
}
decompress_file($input, $output)
|| die "$0: error: decompression failed!\n";
}
elsif ($input !~ $ext || (defined($output) && $output =~ $ext)) {
$output //= basename($input) . '.' . FORMAT;
compress_file($input, $output)
|| die "$0: error: compression failed!\n";
}
else {
warn "$0: don't know what to do...\n";
usage(1);
}
}
sub _make_map {
my ($int) = @_;
my @groups = ([], [], []);
for my $i (1 .. 3) {
foreach my $j (0 .. length($int) - $i) {
$i > 1 && substr($int, $j, 1) == 0 && next;
(my $num = substr($int, $j, $i)) > 255 && next;
$groups[$i - 1][$j] = $num;
}
}
my @map = [[]];
for (my $j = 0 ; $j <= $#{$groups[0]} ; $j++) {
for (my $i = $j ; $i <= $#{$groups[0]} ; $i++) {
if (defined($groups[2][$i])) {
push @{$map[$j][$j]}, $groups[2][$i];
$i += 2;
}
elsif (defined($groups[1][$i])) {
push @{$map[$j][$j]}, $groups[1][$i];
$i += 1;
}
else {
push @{$map[$j][$j]}, $groups[0][$i];
}
}
}
return \@map;
}
sub int2bytes {
my ($int) = @_;
my $data = _make_map($int);
my @nums;
foreach my $arr (@{$data}) {
for my $i (0 .. $#{$arr}) {
if (ref($arr->[$i]) eq 'ARRAY') {
my $head = _make_map(substr($int, 0, $i));
push @nums, [@{$head->[0][0]}, @{$arr->[$i]}];
}
}
}
my $min = min(map { $#{$_} } @nums);
my @bytes = do {
my %seen;
grep { !$seen{join(' ', @{$_})}++ } grep { $#{$_} == $min } @nums;
};
return \@bytes;
}
sub next_power_of_two {
my ($number) = @_;
return 2 if $number <= 1;
## If the number is a power of
## two, then return it as it is.
unless ($number & ($number - 1)) {
return $number;
}
## Return the next power of two
return 2 << (log($number) / log(2));
}
sub valid_archive {
my ($fh) = @_;
if (read($fh, (my $sig), length(SIGNATURE), 0) == length(SIGNATURE)) {
$sig eq SIGNATURE || return;
}
return 1;
}
sub open_file {
my ($mode, $file) = @_;
open(my $fh, $mode, $file);
return $fh;
}
sub uniq_bytes {
my ($fh) = @_;
my %table;
while (my $size = read($fh, (my $chunk), CHUNK_SIZE)) {
@table{split //, $chunk} = ();
}
seek($fh, 0, 0);
return [keys %table];
}
sub info {
my (%info) = @_;
print STDERR <<"EOT";
input : $info{input}
output : $info{output}
filesize : $info{filesize}
bits num : $info{bits_num}
bytes num : $info{bytes_num}
compressing : $info{compress}
EOT
}
sub compress_file {
my ($input, $output) = @_;
my $fh = open_file('<:raw', $input);
my $out_fh = open_file('>:raw', $output);
my $filesize = -s $input;
my $uniq_bytes = uniq_bytes($fh);
my $bytes_num = scalar @{$uniq_bytes};
my $bits_num = log(next_power_of_two($bytes_num)) / log(2);
$DEBUG
&& info(
bytes_num => $bytes_num,
bits_num => $bits_num,
input => $input,
output => $output,
filesize => $filesize,
compress => 'true',
);
my %table;
my $bits_map = '';
foreach my $i (0 .. $#{$uniq_bytes}) {
$bits_map .= ($table{$uniq_bytes->[$i]} = sprintf("%0${bits_num}b", $i));
}
my $size_bytes = ${int2bytes($filesize)}[0];
print {$out_fh} SIGNATURE,
chr($#{$size_bytes} + 1),
join('', map { chr } @{$size_bytes}),
chr($bits_num), chr($bytes_num - 1),
join('', @{$uniq_bytes}), pack('B*', $bits_map);
while (my $size = read($fh, (my $chunk), CHUNK_SIZE)) {
print {$out_fh} scalar pack "B*", join('', @table{split //, $chunk});
}
return 1;
}
sub decompress_file {
my ($input, $output) = @_;
my $fh = open_file('<:raw', $input);
my $out_fh = open_file('>:raw', $output);
valid_archive($fh) || die "$0: file `$input' is not a TZP archive!\n";
my $fsize_len = do { read($fh, (my $byte), 1); ord $byte };
my $filesize = do {
read($fh, (my $bytes), $fsize_len);
join('', unpack('C*', $bytes));
};
my $bits_num = do { read($fh, (my $byte), 1); ord $byte };
my $bytes_num = do { read($fh, (my $byte), 1); 1 + ord $byte };
$DEBUG
&& info(
bytes_num => $bytes_num,
bits_num => $bits_num,
input => $input,
output => $output,
filesize => $filesize,
compress => 'false',
);
my $bytes = do { read($fh, (my $bytes), $bytes_num); [split(//, $bytes)] };
my $bits_len = $bits_num * $bytes_num;
if ((my $mod = $bits_len % 8) != 0) {
$bits_len += 8 - $mod;
}
my $bits = do { read($fh, my ($bytes), $bits_len / 8); unpack 'B*', $bytes };
my %table;
foreach my $byte (@{$bytes}) {
$table{substr($bits, 0, $bits_num, '')} = $byte;
}
my $byte_counter = 0;
my $prev_bits = '';
while (my $size = read($fh, (my $chunk), CHUNK_SIZE)) {
my $bits = $prev_bits . unpack('B*', $chunk);
my $bits_len = 8 * $size + length($prev_bits);
my $left = $bits_len % $bits_num;
$prev_bits =
$left == 0
? q{}
: substr($bits, $bits_len - $left, $bits_len, '');
if (($byte_counter += int($bits_len / $bits_num)) > $filesize) {
$bits_len -= ($byte_counter - $filesize);
}
print {$out_fh} join('', @{table}{unpack("(a$bits_num)" . int($bits_len / $bits_num), $bits)});
}
return 1;
}
main();
exit(0);