1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012 |
- #!/usr/bin/perl
- # Author: Trizen
- # Date: 10 September 2023
- # Edit: 19 March 2024
- # https://github.com/trizen
- # Compress/decompress files using Burrows-Wheeler Transform (BWT) + Variable Run-Length encoding + LZSS + Bzip2.
- # Reference:
- # Data Compression (Summer 2023) - Lecture 13 - BZip2
- # https://youtube.com/watch?v=cvoZbBZ3M2A
- use 5.036;
- use Getopt::Std qw(getopts);
- use File::Basename qw(basename);
- use List::Util qw(max uniq);
- use POSIX qw(ceil log2);
- use constant {
- PKGNAME => 'BWRLZ2',
- VERSION => '0.01',
- FORMAT => 'bwrlz2',
- CHUNK_SIZE => 1 << 17, # higher value = better compression
- LOOKAHEAD_LEN => 128,
- };
- # Container signature
- use constant SIGNATURE => uc(FORMAT) . chr(1);
- # [distance value, offset bits]
- my @DISTANCE_SYMBOLS = map { [$_, 0] } (0 .. 4);
- until ($DISTANCE_SYMBOLS[-1][0] > CHUNK_SIZE) {
- push @DISTANCE_SYMBOLS, [int($DISTANCE_SYMBOLS[-1][0] * (4 / 3)), $DISTANCE_SYMBOLS[-1][1] + 1];
- push @DISTANCE_SYMBOLS, [int($DISTANCE_SYMBOLS[-1][0] * (3 / 2)), $DISTANCE_SYMBOLS[-1][1]];
- }
- # [length, offset bits]
- my @LENGTH_SYMBOLS = ((map { [$_, 0] } (3 .. 10)));
- {
- my $delta = 1;
- until ($LENGTH_SYMBOLS[-1][0] > 163) {
- push @LENGTH_SYMBOLS, [$LENGTH_SYMBOLS[-1][0] + $delta, $LENGTH_SYMBOLS[-1][1] + 1];
- $delta *= 2;
- push @LENGTH_SYMBOLS, [$LENGTH_SYMBOLS[-1][0] + $delta, $LENGTH_SYMBOLS[-1][1]];
- push @LENGTH_SYMBOLS, [$LENGTH_SYMBOLS[-1][0] + $delta, $LENGTH_SYMBOLS[-1][1]];
- push @LENGTH_SYMBOLS, [$LENGTH_SYMBOLS[-1][0] + $delta, $LENGTH_SYMBOLS[-1][1]];
- }
- push @LENGTH_SYMBOLS, [258, 0];
- }
- my @DISTANCE_INDICES;
- foreach my $i (0 .. $#DISTANCE_SYMBOLS) {
- my ($min, $bits) = @{$DISTANCE_SYMBOLS[$i]};
- foreach my $k ($min .. $min + (1 << $bits) - 1) {
- last if ($k > CHUNK_SIZE);
- $DISTANCE_INDICES[$k] = $i;
- }
- }
- my @LENGTH_INDICES;
- foreach my $i (0 .. $#LENGTH_SYMBOLS) {
- my ($min, $bits) = @{$LENGTH_SYMBOLS[$i]};
- foreach my $k ($min .. $min + (1 << $bits) - 1) {
- $LENGTH_INDICES[$k] = $i;
- }
- }
- 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);
- }
- }
- sub lz77_compression ($str, $uncompressed, $indices, $lengths) {
- my $la = 0;
- my $prefix = '';
- my @chars = split(//, $str);
- my $end = $#chars;
- my $min_len = $LENGTH_SYMBOLS[0][0];
- my $max_len = $LENGTH_SYMBOLS[-1][0];
- my %literal_freq;
- my %distance_freq;
- my $literal_count = 0;
- my $distance_count = 0;
- while ($la <= $end) {
- my $n = 1;
- my $p = length($prefix);
- my $tmp;
- my $token = $chars[$la];
- while ( $n <= $max_len
- and $la + $n <= $end
- and ($tmp = rindex($prefix, $token, $p)) >= 0) {
- $p = $tmp;
- $token .= $chars[$la + $n];
- ++$n;
- }
- my $enc_bits_len = 0;
- my $literal_bits_len = 0;
- if ($n > $min_len) {
- my $dist = $DISTANCE_SYMBOLS[$DISTANCE_INDICES[$la - $p]];
- $enc_bits_len += $dist->[1] + ceil(log2((1 + $distance_count) / (1 + ($distance_freq{$dist->[0]} // 0))));
- my $len_idx = $LENGTH_INDICES[$n - 1];
- my $len = $LENGTH_SYMBOLS[$len_idx];
- $enc_bits_len += $len->[1] + ceil(log2((1 + $literal_count) / (1 + ($literal_freq{$len_idx + 256} // 0))));
- my %freq;
- foreach my $c (unpack('C*', substr($prefix, $p, $n - 1) . $chars[$la + $n - 1])) {
- ++$freq{$c};
- $literal_bits_len += ceil(log2(($n + $literal_count) / ($freq{$c} + ($literal_freq{$c} // 0))));
- }
- }
- if ($n > $min_len and $enc_bits_len < $literal_bits_len) {
- push @$lengths, $n - 1;
- push @$indices, $la - $p;
- push @$uncompressed, undef;
- my $dist_idx = $DISTANCE_INDICES[$la - $p];
- my $dist = $DISTANCE_SYMBOLS[$dist_idx];
- ++$distance_count;
- ++$distance_freq{$dist->[0]};
- ++$literal_count;
- ++$literal_freq{$LENGTH_INDICES[$n - 1] + 256};
- $la += $n - 1;
- $prefix .= substr($token, 0, -1);
- }
- else {
- my @bytes = unpack('C*', substr($prefix, $p, $n - 1) . $chars[$la + $n - 1]);
- push @$uncompressed, @bytes;
- push @$lengths, (0) x scalar(@bytes);
- push @$indices, (0) x scalar(@bytes);
- ++$literal_freq{$_} for @bytes;
- $literal_count += $n;
- $la += $n;
- $prefix .= $token;
- }
- }
- return;
- }
- sub lz77_decompression ($literals, $distances, $lengths) {
- my $chunk = '';
- my $offset = 0;
- foreach my $i (0 .. $#$literals) {
- if ($lengths->[$i] != 0) {
- $chunk .= substr($chunk, $offset - $distances->[$i], $lengths->[$i]);
- $offset += $lengths->[$i];
- }
- else {
- $chunk .= chr($literals->[$i]);
- $offset += 1;
- }
- }
- return $chunk;
- }
- sub deflate_encode ($literals, $distances, $lengths, $out_fh) {
- my @len_symbols;
- my @dist_symbols;
- my $offset_bits = '';
- foreach my $j (0 .. $#{$literals}) {
- if ($lengths->[$j] == 0) {
- push @len_symbols, $literals->[$j];
- next;
- }
- my $len = $lengths->[$j];
- my $dist = $distances->[$j];
- {
- my $len_idx = $LENGTH_INDICES[$len];
- my ($min, $bits) = @{$LENGTH_SYMBOLS[$len_idx]};
- push @len_symbols, $len_idx + 256;
- if ($bits > 0) {
- $offset_bits .= sprintf('%0*b', $bits, $len - $min);
- }
- }
- {
- my $dist_idx = $DISTANCE_INDICES[$dist];
- my ($min, $bits) = @{$DISTANCE_SYMBOLS[$dist_idx]};
- push @dist_symbols, $dist_idx;
- if ($bits > 0) {
- $offset_bits .= sprintf('%0*b', $bits, $dist - $min);
- }
- }
- }
- bz2_compression_symbolic(\@len_symbols, $out_fh);
- bz2_compression_symbolic(\@dist_symbols, $out_fh);
- print $out_fh pack('B*', $offset_bits);
- }
- sub deflate_decode ($fh) {
- my $len_symbols = bz2_decompression_symbolic($fh);
- my $dist_symbols = bz2_decompression_symbolic($fh);
- my $bits_len = 0;
- foreach my $i (@$dist_symbols) {
- $bits_len += $DISTANCE_SYMBOLS[$i][1];
- }
- foreach my $i (@$len_symbols) {
- if ($i >= 256) {
- $bits_len += $LENGTH_SYMBOLS[$i - 256][1];
- }
- }
- my $bits = read_bits($fh, $bits_len);
- my @literals;
- my @lengths;
- my @distances;
- my $j = 0;
- foreach my $i (@$len_symbols) {
- if ($i >= 256) {
- my $dist = $dist_symbols->[$j++];
- push @literals, undef;
- push @lengths, $LENGTH_SYMBOLS[$i - 256][0] + oct('0b' . substr($bits, 0, $LENGTH_SYMBOLS[$i - 256][1], ''));
- push @distances, $DISTANCE_SYMBOLS[$dist][0] + oct('0b' . substr($bits, 0, $DISTANCE_SYMBOLS[$dist][1], ''));
- }
- else {
- push @literals, $i;
- push @lengths, 0;
- push @distances, 0;
- }
- }
- return (\@literals, \@distances, \@lengths);
- }
- sub read_bit ($fh, $bitstring) {
- if (($$bitstring // '') eq '') {
- $$bitstring = unpack('b*', getc($fh) // return undef);
- }
- chop($$bitstring);
- }
- sub read_bits ($fh, $bits_len) {
- my $data = '';
- read($fh, $data, $bits_len >> 3);
- $data = unpack('B*', $data);
- while (length($data) < $bits_len) {
- $data .= unpack('B*', getc($fh) // return undef);
- }
- if (length($data) > $bits_len) {
- $data = substr($data, 0, $bits_len);
- }
- return $data;
- }
- sub delta_encode ($integers, $double = 0) {
- my @deltas;
- my $prev = 0;
- unshift(@$integers, scalar(@$integers));
- while (@$integers) {
- my $curr = shift(@$integers);
- push @deltas, $curr - $prev;
- $prev = $curr;
- }
- my $bitstring = '';
- foreach my $d (@deltas) {
- if ($d == 0) {
- $bitstring .= '0';
- }
- elsif ($double) {
- my $t = sprintf('%b', abs($d) + 1);
- my $l = sprintf('%b', length($t));
- $bitstring .= '1' . (($d < 0) ? '0' : '1') . ('1' x (length($l) - 1)) . '0' . substr($l, 1) . substr($t, 1);
- }
- else {
- my $t = sprintf('%b', abs($d));
- $bitstring .= '1' . (($d < 0) ? '0' : '1') . ('1' x (length($t) - 1)) . '0' . substr($t, 1);
- }
- }
- pack('B*', $bitstring);
- }
- sub delta_decode ($fh, $double = 0) {
- my @deltas;
- my $buffer = '';
- my $len = 0;
- for (my $k = 0 ; $k <= $len ; ++$k) {
- my $bit = read_bit($fh, \$buffer);
- if ($bit eq '0') {
- push @deltas, 0;
- }
- elsif ($double) {
- my $bit = read_bit($fh, \$buffer);
- my $bl = 0;
- ++$bl while (read_bit($fh, \$buffer) eq '1');
- my $bl2 = oct('0b1' . join('', map { read_bit($fh, \$buffer) } 1 .. $bl));
- my $int = oct('0b1' . join('', map { read_bit($fh, \$buffer) } 1 .. ($bl2 - 1)));
- push @deltas, ($bit eq '1' ? 1 : -1) * ($int - 1);
- }
- else {
- my $bit = read_bit($fh, \$buffer);
- my $n = 0;
- ++$n while (read_bit($fh, \$buffer) eq '1');
- my $d = oct('0b1' . join('', map { read_bit($fh, \$buffer) } 1 .. $n));
- push @deltas, ($bit eq '1' ? $d : -$d);
- }
- if ($k == 0) {
- $len = pop(@deltas);
- }
- }
- my @acc;
- my $prev = $len;
- foreach my $d (@deltas) {
- $prev += $d;
- push @acc, $prev;
- }
- return \@acc;
- }
- # produce encode and decode dictionary from a tree
- sub walk ($node, $code, $h, $rev_h) {
- my $c = $node->[0] // return ($h, $rev_h);
- if (ref $c) { walk($c->[$_], $code . $_, $h, $rev_h) for ('0', '1') }
- else { $h->{$c} = $code; $rev_h->{$code} = $c }
- return ($h, $rev_h);
- }
- # make a tree, and return resulting dictionaries
- sub mktree_from_freq ($freq) {
- my @nodes = map { [$_, $freq->{$_}] } sort { $a <=> $b } keys %$freq;
- do { # poor man's priority queue
- @nodes = sort { $a->[1] <=> $b->[1] } @nodes;
- my ($x, $y) = splice(@nodes, 0, 2);
- if (defined($x)) {
- if (defined($y)) {
- push @nodes, [[$x, $y], $x->[1] + $y->[1]];
- }
- else {
- push @nodes, [[$x], $x->[1]];
- }
- }
- } while (@nodes > 1);
- walk($nodes[0], '', {}, {});
- }
- sub huffman_encode ($bytes, $dict) {
- join('', @{$dict}{@$bytes});
- }
- sub huffman_decode ($bits, $hash) {
- local $" = '|';
- [split(' ', $bits =~ s/(@{[sort { length($a) <=> length($b) } keys %{$hash}]})/$hash->{$1} /gr)]; # very fast
- }
- sub create_huffman_entry ($bytes, $out_fh) {
- my %freq;
- ++$freq{$_} for @$bytes;
- my ($h, $rev_h) = mktree_from_freq(\%freq);
- my $enc = huffman_encode($bytes, $h);
- my $max_symbol = max(keys %freq) // 0;
- my @freqs;
- foreach my $i (0 .. $max_symbol) {
- push @freqs, $freq{$i} // 0;
- }
- print $out_fh delta_encode(\@freqs);
- print $out_fh pack("N", length($enc));
- print $out_fh pack("B*", $enc);
- }
- sub decode_huffman_entry ($fh) {
- my @freqs = @{delta_decode($fh)};
- my %freq;
- foreach my $i (0 .. $#freqs) {
- if ($freqs[$i]) {
- $freq{$i} = $freqs[$i];
- }
- }
- my (undef, $rev_dict) = mktree_from_freq(\%freq);
- my $enc_len = unpack('N', join('', map { getc($fh) // die "error" } 1 .. 4));
- if ($enc_len > 0) {
- return huffman_decode(read_bits($fh, $enc_len), $rev_dict);
- }
- return [];
- }
- sub mtf_encode ($bytes, $alphabet = [0 .. 255]) {
- my @C;
- my @table;
- @table[@$alphabet] = (0 .. $#{$alphabet});
- foreach my $c (@$bytes) {
- push @C, (my $index = $table[$c]);
- unshift(@$alphabet, splice(@$alphabet, $index, 1));
- @table[@{$alphabet}[0 .. $index]] = (0 .. $index);
- }
- return \@C;
- }
- sub mtf_decode ($encoded, $alphabet = [0 .. 255]) {
- my @S;
- foreach my $p (@$encoded) {
- push @S, $alphabet->[$p];
- unshift(@$alphabet, splice(@$alphabet, $p, 1));
- }
- return \@S;
- }
- sub rle_encode ($bytes) { # RLE2
- my @rle;
- my $end = $#{$bytes};
- for (my $i = 0 ; $i <= $end ; ++$i) {
- my $run = 0;
- while ($i <= $end and $bytes->[$i] == 0) {
- ++$run;
- ++$i;
- }
- if ($run >= 1) {
- my $t = sprintf('%b', $run + 1);
- push @rle, split(//, substr($t, 1));
- }
- if ($i <= $end) {
- push @rle, $bytes->[$i] + 1;
- }
- }
- return \@rle;
- }
- sub rle_decode ($rle) { # RLE2
- my @dec;
- my $end = $#{$rle};
- for (my $i = 0 ; $i <= $end ; ++$i) {
- my $k = $rle->[$i];
- if ($k == 0 or $k == 1) {
- my $run = 1;
- while (($i <= $end) and ($k == 0 or $k == 1)) {
- ($run <<= 1) |= $k;
- $k = $rle->[++$i];
- }
- push @dec, (0) x ($run - 1);
- }
- if ($i <= $end) {
- push @dec, $k - 1;
- }
- }
- return \@dec;
- }
- sub bwt_balanced ($s) { # O(n * LOOKAHEAD_LEN) space (fast)
- #<<<
- [
- map { $_->[1] } sort {
- ($a->[0] cmp $b->[0])
- || ((substr($s, $a->[1]) . substr($s, 0, $a->[1])) cmp(substr($s, $b->[1]) . substr($s, 0, $b->[1])))
- }
- map {
- my $t = substr($s, $_, LOOKAHEAD_LEN);
- if (length($t) < LOOKAHEAD_LEN) {
- $t .= substr($s, 0, ($_ < LOOKAHEAD_LEN) ? $_ : (LOOKAHEAD_LEN - length($t)));
- }
- [$t, $_]
- } 0 .. length($s) - 1
- ];
- #>>>
- }
- sub bwt_encode ($s) {
- my $bwt = bwt_balanced($s);
- my $ret = join('', map { substr($s, $_ - 1, 1) } @$bwt);
- my $idx = 0;
- foreach my $i (@$bwt) {
- $i || last;
- ++$idx;
- }
- return ($ret, $idx);
- }
- sub bwt_decode ($bwt, $idx) { # fast inversion
- my @tail = split(//, $bwt);
- my @head = sort @tail;
- my %indices;
- foreach my $i (0 .. $#tail) {
- push @{$indices{$tail[$i]}}, $i;
- }
- my @table;
- foreach my $v (@head) {
- push @table, shift(@{$indices{$v}});
- }
- my $dec = '';
- my $i = $idx;
- for (1 .. scalar(@head)) {
- $dec .= $head[$i];
- $i = $table[$i];
- }
- return $dec;
- }
- sub bwt_sort_symbolic ($s) { # O(n) space (slowish)
- my @cyclic = @$s;
- my $len = scalar(@cyclic);
- my $rle = 1;
- foreach my $i (1 .. $len - 1) {
- if ($cyclic[$i] != $cyclic[$i - 1]) {
- $rle = 0;
- last;
- }
- }
- $rle && return [0 .. $len - 1];
- [
- sort {
- my ($i, $j) = ($a, $b);
- while ($cyclic[$i] == $cyclic[$j]) {
- $i %= $len if (++$i >= $len);
- $j %= $len if (++$j >= $len);
- }
- $cyclic[$i] <=> $cyclic[$j];
- } 0 .. $len - 1
- ];
- }
- sub bwt_encode_symbolic ($s) {
- my $bwt = bwt_sort_symbolic($s);
- my @ret = map { $s->[$_ - 1] } @$bwt;
- my $idx = 0;
- foreach my $i (@$bwt) {
- $i || last;
- ++$idx;
- }
- return (\@ret, $idx);
- }
- sub bwt_decode_symbolic ($bwt, $idx) { # fast inversion
- my @tail = @$bwt;
- my @head = sort { $a <=> $b } @tail;
- my @indices;
- foreach my $i (0 .. $#tail) {
- push @{$indices[$tail[$i]]}, $i;
- }
- my @table;
- foreach my $v (@head) {
- push @table, shift(@{$indices[$v]});
- }
- my @dec;
- my $i = $idx;
- for (1 .. scalar(@head)) {
- push @dec, $head[$i];
- $i = $table[$i];
- }
- return \@dec;
- }
- sub encode_alphabet_symbolic ($alphabet) {
- # TODO: encode the alphabet more efficiently
- return delta_encode([reverse @$alphabet]);
- }
- sub decode_alphabet_symbolic ($fh) {
- return [reverse @{delta_decode($fh)}];
- }
- sub bz2_compression_symbolic ($symbols, $out_fh) {
- my ($bwt, $idx) = bwt_encode_symbolic($symbols);
- my @bytes = @$bwt;
- my @alphabet = sort { $a <=> $b } uniq(@bytes);
- my $alphabet_enc = encode_alphabet_symbolic(\@alphabet);
- say "BWT index = $idx";
- say "Max symbol: ", max(@alphabet) // 0;
- my $mtf = mtf_encode(\@bytes, [@alphabet]);
- my $rle = rle_encode($mtf);
- print $out_fh pack('N', $idx);
- print $out_fh $alphabet_enc;
- create_huffman_entry($rle, $out_fh);
- }
- sub bz2_decompression_symbolic ($fh) {
- my $idx = unpack('N', join('', map { getc($fh) // die "error" } 1 .. 4));
- my $alphabet = decode_alphabet_symbolic($fh);
- say "BWT index = $idx";
- say "Alphabet size: ", scalar(@$alphabet);
- my $rle = decode_huffman_entry($fh);
- my $mtf = rle_decode($rle);
- my $bwt = mtf_decode($mtf, $alphabet);
- my $data = bwt_decode_symbolic($bwt, $idx);
- return $data;
- }
- sub lzss_compression ($chunk, $out_fh) {
- my (@uncompressed, @indices, @lengths);
- lz77_compression($chunk, \@uncompressed, \@indices, \@lengths);
- my $est_ratio = length($chunk) / (scalar(@uncompressed) + scalar(@lengths) + 2 * scalar(@indices));
- say scalar(@uncompressed), ' -> ', $est_ratio;
- deflate_encode(\@uncompressed, \@indices, \@lengths, $out_fh);
- }
- sub lzss_decompression ($fh, $out_fh) {
- my ($uncompressed, $indices, $lengths) = deflate_decode($fh);
- print $out_fh lz77_decompression($uncompressed, $indices, $lengths);
- }
- sub run_length ($arr) {
- @$arr || return [];
- my @result = [$arr->[0], 1];
- my $prev_value = $arr->[0];
- foreach my $i (1 .. $#{$arr}) {
- my $curr_value = $arr->[$i];
- if ($curr_value eq $prev_value) {
- ++$result[-1][1];
- }
- else {
- push(@result, [$curr_value, 1]);
- }
- $prev_value = $curr_value;
- }
- return \@result;
- }
- sub rle4_encode ($bytes) { # RLE1
- my @rle;
- my $end = $#{$bytes};
- my $prev = -1;
- my $run = 0;
- for (my $i = 0 ; $i <= $end ; ++$i) {
- if ($bytes->[$i] == $prev) {
- ++$run;
- }
- else {
- $run = 1;
- }
- push @rle, $bytes->[$i];
- $prev = $bytes->[$i];
- if ($run >= 4) {
- $run = 0;
- $i += 1;
- while ($run < 255 and $i <= $end and $bytes->[$i] == $prev) {
- ++$run;
- ++$i;
- }
- push @rle, $run;
- $run = 1;
- if ($i <= $end) {
- $prev = $bytes->[$i];
- push @rle, $bytes->[$i];
- }
- }
- }
- return \@rle;
- }
- sub rle4_decode ($bytes) { # RLE1
- my @dec = $bytes->[0];
- my $end = $#{$bytes};
- my $prev = $bytes->[0];
- my $run = 1;
- for (my $i = 1 ; $i <= $end ; ++$i) {
- if ($bytes->[$i] == $prev) {
- ++$run;
- }
- else {
- $run = 1;
- }
- push @dec, $bytes->[$i];
- $prev = $bytes->[$i];
- if ($run >= 4) {
- if (++$i <= $end) {
- $run = $bytes->[$i];
- push @dec, (($prev) x $run);
- }
- $run = 0;
- }
- }
- return \@dec;
- }
- sub VLR_encoding ($bytes) {
- my $uncompressed = '';
- my $bitstream = '';
- my $rle = run_length($bytes);
- foreach my $cv (@$rle) {
- my ($c, $v) = @$cv;
- $uncompressed .= $c;
- if ($v == 1) {
- $bitstream .= '0';
- }
- else {
- my $t = sprintf('%b', $v);
- $bitstream .= join('', '1' x (length($t) - 1), '0', substr($t, 1));
- }
- }
- return ($uncompressed, pack('B*', $bitstream));
- }
- sub VLR_decoding ($uncompressed, $bits_fh) {
- my $decoded = '';
- my $buffer = '';
- foreach my $c (@$uncompressed) {
- my $bl = 0;
- while (read_bit($bits_fh, \$buffer) == 1) {
- ++$bl;
- }
- if ($bl > 0) {
- $decoded .= $c x oct('0b1' . join('', map { read_bit($bits_fh, \$buffer) } 1 .. $bl));
- }
- else {
- $decoded .= $c;
- }
- }
- return $decoded;
- }
- # 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;
- # Compress data
- while (read($fh, (my $chunk), CHUNK_SIZE)) {
- my ($bwt, $idx) = bwt_encode(pack('C*', @{rle4_encode([unpack('C*', $chunk)])}));
- my ($uncompressed, $lengths) = VLR_encoding([split(//, $bwt)]);
- print $out_fh pack('N', $idx);
- lzss_compression($uncompressed, $out_fh);
- lzss_compression($lengths, $out_fh);
- }
- 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 $uncompressed = '';
- my $lengths = '';
- open my $uc_fh, '>:raw', \$uncompressed;
- open my $len_fh, '+>:raw', \$lengths;
- my $idx = unpack('N', join('', map { getc($fh) // die "decompression error" } 1 .. 4));
- lzss_decompression($fh, $uc_fh); # uncompressed
- lzss_decompression($fh, $len_fh); # lengths
- seek($len_fh, 0, 0);
- my $dec = VLR_decoding([split(//, $uncompressed)], $len_fh);
- print $out_fh pack('C*', @{rle4_decode([unpack('C*', bwt_decode($dec, $idx))])});
- }
- close $fh;
- close $out_fh;
- }
- main();
- exit(0);
|