-
Notifications
You must be signed in to change notification settings - Fork 33
/
lzbwa_file_compression.pl
240 lines (183 loc) · 6.88 KB
/
lzbwa_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
#!/usr/bin/perl
# Author: Trizen
# Date: 05 September 2023
# Edit: 11 April 2024
# https://github.com/trizen
# Compress/decompress files using LZ77 compression + fixed-width integers encoding + Burrows-Wheeler Transform (BWT) + Arithmetic Coding.
# References:
# Data Compression (Summer 2023) - Lecture 13 - BZip2
# https://youtube.com/watch?v=cvoZbBZ3M2A
#
# Basic arithmetic coder in C++
# https://github.com/billbird/arith32
use 5.036;
use Getopt::Std qw(getopts);
use File::Basename qw(basename);
use Compression::Util qw(:all);
use constant {
PKGNAME => 'LZBWA',
VERSION => '0.01',
FORMAT => 'lzbwa',
CHUNK_SIZE => 1 << 16, # higher value = better compression
};
# Container signature
use constant SIGNATURE => uc(FORMAT) . chr(1);
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 version {
printf("%s %s\n", PKGNAME, VERSION);
exit;
}
sub valid_archive {
my ($fh) = @_;
if (read($fh, (my $sig), length(SIGNATURE), 0) == length(SIGNATURE)) {
$sig eq SIGNATURE || return;
}
return 1;
}
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);
}
}
# Compress file
sub compress_file ($input, $output) {
open my $fh, '<:raw', $input
or die "Can't open file <<$input>> for reading: $!";
my $header = SIGNATURE;
# Open the output file for writing
open my $out_fh, '>:raw', $output
or die "Can't open file <<$output>> for write: $!";
# Print the header
print $out_fh $header;
my $lengths_str = '';
my $matches_str = '';
my $uncompressed_str = '';
my @sizes;
my @distances_block;
open my $uc_fh, '>:raw', \$uncompressed_str;
open my $len_fh, '>:raw', \$lengths_str;
open my $match_fh, '>:raw', \$matches_str;
my $create_bz2_block = sub {
scalar(@sizes) > 0 or return;
print $out_fh delta_encode(\@sizes);
print $out_fh bwt_compress(
$uncompressed_str,
sub ($s) {
lzss_compress_symbolic($s, sub ($s) { mrl_compress_symbolic($s, \&create_ac_entry) });
}
);
print $out_fh bwt_compress($lengths_str, \&create_ac_entry);
print $out_fh bwt_compress($matches_str, \&create_ac_entry);
print $out_fh bwt_compress(symbols2string(\@distances_block), \&create_ac_entry);
@sizes = ();
@distances_block = ();
open $uc_fh, '>:raw', \$uncompressed_str;
open $len_fh, '>:raw', \$lengths_str;
open $match_fh, '>:raw', \$matches_str;
};
# Compress data
while (read($fh, (my $chunk), CHUNK_SIZE)) {
local $Compression::Util::LZ_MAX_DIST = 255;
my ($literals, $distances, $lengths, $matches) = lz77_encode($chunk);
my $est_ratio = length($chunk) / (4 * scalar(@$literals));
say "Est. ratio: ", $est_ratio, " (", scalar(@$literals), " uncompressed bytes)";
push(@sizes, scalar(@$literals), scalar(@$distances), scalar(@$lengths), scalar(@$matches));
print $uc_fh pack('C*', @$literals);
print $len_fh pack('C*', @$lengths);
print $match_fh pack('C*', @$matches);
push @distances_block, @$distances;
if (length($uncompressed_str) >= CHUNK_SIZE) {
$create_bz2_block->();
}
}
$create_bz2_block->();
close $out_fh;
}
# Decompress file
sub decompress_file ($input, $output) {
# Open and validate the input file
open my $fh, '<:raw', $input
or die "Can't open file <<$input>> for reading: $!";
valid_archive($fh) || die "$0: file `$input' is not a \U${\FORMAT}\E v${\VERSION} archive!\n";
# Open the output file
open my $out_fh, '>:raw', $output
or die "Can't open file <<$output>> for writing: $!";
while (!eof($fh)) {
my @sizes = @{delta_decode($fh)};
my @uncompressed = unpack(
'C*',
bwt_decompress(
$fh,
sub ($s) {
lzss_decompress_symbolic($s, sub ($s) { mrl_decompress_symbolic($s, \&decode_ac_entry) });
}
)
);
my @lengths = unpack('C*', bwt_decompress($fh, \&decode_ac_entry));
my @matches = unpack('C*', bwt_decompress($fh, \&decode_ac_entry));
my @distances = unpack('C*', bwt_decompress($fh, \&decode_ac_entry));
while (@uncompressed) {
my $literals_size = shift(@sizes) // die "decompression error";
my $distances_size = shift(@sizes) // die "decompression error";
my $lengths_size = shift(@sizes) // die "decompression error";
my $matches_size = shift(@sizes) // die "decompression error";
my @uncompressed_chunk = splice(@uncompressed, 0, $literals_size);
my @lengths_chunk = splice(@lengths, 0, $lengths_size);
my @matches_chunk = splice(@matches, 0, $matches_size);
my @distances_chunk = splice(@distances, 0, $distances_size);
scalar(@uncompressed_chunk) == $literals_size or die "decompression error";
scalar(@lengths_chunk) == $lengths_size or die "decompression error";
scalar(@matches_chunk) == $matches_size or die "decompression error";
scalar(@distances_chunk) == $distances_size or die "decompression error";
print $out_fh lz77_decode(\@uncompressed_chunk, \@distances_chunk, \@lengths_chunk, \@matches_chunk);
}
}
close $fh;
close $out_fh;
}
main();
exit(0);