12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034 |
- #!/usr/bin/perl
- # Author: Trizen
- # Date: 15 June 2023
- # Edit: 06 February 2024
- # https://github.com/trizen
- # Compress/decompress files using Burrows-Wheeler Transform (BWT) + Move-to-front transform (MTF) + LZ77 compression (LZSS) + Arithmetic Coding (in fixed bits).
- # Encoding the literals and the pointers using a DEFLATE-like approach.
- # References:
- # Data Compression (Summer 2023) - Lecture 11 - DEFLATE (gzip)
- # https://youtube.com/watch?v=SJPvNi4HrWQ
- #
- # 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 List::Util qw(max uniq sum);
- use POSIX qw(ceil log2);
- use constant {
- PKGNAME => 'BWLZA',
- VERSION => '0.03',
- FORMAT => 'bwlza',
- CHUNK_SIZE => 1 << 17, # higher value = better compression
- LOOKAHEAD_LEN => 128,
- };
- # Arithmetic Coding settings
- use constant BITS => 32;
- use constant MAX => oct('0b' . ('1' x BITS));
- # Container signature
- use constant SIGNATURE => uc(FORMAT) . chr(3);
- # [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 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;
- }
- sub create_cfreq ($freq) {
- my @cf;
- my $T = 0;
- foreach my $i (sort { $a <=> $b } keys %$freq) {
- $freq->{$i} // next;
- $cf[$i] = $T;
- $T += $freq->{$i};
- $cf[$i + 1] = $T;
- }
- return (\@cf, $T);
- }
- sub ac_encode ($bytes_arr) {
- my $enc = '';
- my $EOF_SYMBOL = (max(@$bytes_arr) // 0) + 1;
- my @bytes = (@$bytes_arr, $EOF_SYMBOL);
- my %freq;
- ++$freq{$_} for @bytes;
- my ($cf, $T) = create_cfreq(\%freq);
- if ($T > MAX) {
- die "Too few bits: $T > ${\MAX}";
- }
- my $low = 0;
- my $high = MAX;
- my $uf_count = 0;
- foreach my $c (@bytes) {
- my $w = $high - $low + 1;
- $high = ($low + int(($w * $cf->[$c + 1]) / $T) - 1) & MAX;
- $low = ($low + int(($w * $cf->[$c]) / $T)) & MAX;
- if ($high > MAX) {
- die "high > MAX: $high > ${\MAX}";
- }
- if ($low >= $high) { die "$low >= $high" }
- while (1) {
- if (($high >> (BITS - 1)) == ($low >> (BITS - 1))) {
- my $bit = $high >> (BITS - 1);
- $enc .= $bit;
- if ($uf_count > 0) {
- $enc .= join('', 1 - $bit) x $uf_count;
- $uf_count = 0;
- }
- $low <<= 1;
- ($high <<= 1) |= 1;
- }
- elsif (((($low >> (BITS - 2)) & 0x1) == 1) && ((($high >> (BITS - 2)) & 0x1) == 0)) {
- ($high <<= 1) |= (1 << (BITS - 1));
- $high |= 1;
- ($low <<= 1) &= ((1 << (BITS - 1)) - 1);
- ++$uf_count;
- }
- else {
- last;
- }
- $low &= MAX;
- $high &= MAX;
- }
- }
- $enc .= '0';
- $enc .= '1';
- while (length($enc) % 8 != 0) {
- $enc .= '1';
- }
- return ($enc, \%freq);
- }
- sub ac_decode ($fh, $freq) {
- my ($cf, $T) = create_cfreq($freq);
- my @dec;
- my $low = 0;
- my $high = MAX;
- my $enc = oct('0b' . join '', map { getc($fh) // 1 } 1 .. BITS);
- my @table;
- foreach my $i (sort { $a <=> $b } keys %$freq) {
- foreach my $j ($cf->[$i] .. $cf->[$i + 1] - 1) {
- $table[$j] = $i;
- }
- }
- my $EOF_SYMBOL = max(keys %$freq) // 0;
- while (1) {
- my $w = $high - $low + 1;
- my $ss = int((($T * ($enc - $low + 1)) - 1) / $w);
- my $i = $table[$ss] // last;
- last if ($i == $EOF_SYMBOL);
- push @dec, $i;
- $high = ($low + int(($w * $cf->[$i + 1]) / $T) - 1) & MAX;
- $low = ($low + int(($w * $cf->[$i]) / $T)) & MAX;
- if ($high > MAX) {
- die "error";
- }
- if ($low >= $high) { die "$low >= $high" }
- while (1) {
- if (($high >> (BITS - 1)) == ($low >> (BITS - 1))) {
- ($high <<= 1) |= 1;
- $low <<= 1;
- ($enc <<= 1) |= (getc($fh) // 1);
- }
- elsif (((($low >> (BITS - 2)) & 0x1) == 1) && ((($high >> (BITS - 2)) & 0x1) == 0)) {
- ($high <<= 1) |= (1 << (BITS - 1));
- $high |= 1;
- ($low <<= 1) &= ((1 << (BITS - 1)) - 1);
- $enc = (($enc >> (BITS - 1)) << (BITS - 1)) | (($enc & ((1 << (BITS - 2)) - 1)) << 1) | (getc($fh) // 1);
- }
- else {
- last;
- }
- $low &= MAX;
- $high &= MAX;
- $enc &= MAX;
- }
- }
- return \@dec;
- }
- sub create_ac_entry ($bytes, $out_fh) {
- my ($enc, $freq) = ac_encode($bytes);
- my $max_symbol = max(keys %$freq) // 0;
- my @freqs;
- foreach my $k (0 .. $max_symbol) {
- push @freqs, $freq->{$k} // 0;
- }
- push @freqs, length($enc) >> 3;
- say "Max symbol: $max_symbol";
- print $out_fh delta_encode(\@freqs);
- print $out_fh pack("B*", $enc);
- }
- sub decode_ac_entry ($fh) {
- my @freqs = @{delta_decode($fh)};
- my $bits_len = pop(@freqs);
- my %freq;
- foreach my $i (0 .. $#freqs) {
- if ($freqs[$i]) {
- $freq{$i} = $freqs[$i];
- }
- }
- say "Encoded length: $bits_len";
- my $bits = read_bits($fh, $bits_len << 3);
- if ($bits_len > 0) {
- open my $bits_fh, '<:raw', \$bits;
- return ac_decode($bits_fh, \%freq);
- }
- return [];
- }
- 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);
- }
- }
- }
- create_ac_entry(\@len_symbols, $out_fh);
- create_ac_entry(\@dist_symbols, $out_fh);
- print $out_fh pack('B*', $offset_bits);
- }
- sub deflate_decode ($fh) {
- my $len_symbols = decode_ac_entry($fh);
- my $dist_symbols = decode_ac_entry($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 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 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 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 < 254 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 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 encode_alphabet ($alphabet) {
- my %table;
- @table{@$alphabet} = ();
- my $populated = 0;
- my @marked;
- for (my $i = 0 ; $i <= 255 ; $i += 32) {
- my $enc = 0;
- foreach my $j (0 .. 31) {
- if (exists($table{$i + $j})) {
- $enc |= 1 << $j;
- }
- }
- if ($enc == 0) {
- $populated <<= 1;
- }
- else {
- ($populated <<= 1) |= 1;
- push @marked, $enc;
- }
- }
- my $delta = delta_encode([@marked], 1);
- say "Populated : ", sprintf('%08b', $populated);
- say "Marked : @marked";
- say "Delta len : ", length($delta);
- my $encoded = '';
- $encoded .= chr($populated);
- $encoded .= $delta;
- return $encoded;
- }
- sub decode_alphabet ($fh) {
- my @populated = split(//, sprintf('%08b', ord(getc($fh) // die "error")));
- my $marked = delta_decode($fh, 1);
- my @alphabet;
- for (my $i = 0 ; $i <= 255 ; $i += 32) {
- if (shift(@populated)) {
- my $m = shift(@$marked);
- foreach my $j (0 .. 31) {
- if ($m & 1) {
- push @alphabet, $i + $j;
- }
- $m >>= 1;
- }
- }
- }
- return \@alphabet;
- }
- sub lzss_compression ($data, $out_fh) {
- my (@uncompressed, @indices, @lengths);
- lz77_compression($data, \@uncompressed, \@indices, \@lengths);
- my $est_ratio = length($data) / (scalar(@uncompressed) + scalar(@lengths) + 2 * scalar(@indices));
- say "\nEst. ratio: ", $est_ratio, " (", scalar(@uncompressed), " uncompressed bytes)";
- deflate_encode(\@uncompressed, \@indices, \@lengths, $out_fh);
- }
- sub lzss_decompression ($fh) {
- my ($uncompressed, $indices, $lengths) = deflate_decode($fh);
- lz77_decompression($uncompressed, $indices, $lengths);
- }
- sub compression ($chunk, $out_fh) {
- my @chunk_bytes = unpack('C*', $chunk);
- my $data = pack('C*', @{rle4_encode(\@chunk_bytes)});
- my ($bwt, $idx) = bwt_encode($data);
- my @bytes = unpack('C*', $bwt);
- my @alphabet = sort { $a <=> $b } uniq(@bytes);
- my $enc_bytes = mtf_encode(\@bytes, [@alphabet]);
- if (max(@$enc_bytes) < 255) {
- print $out_fh chr(1);
- $enc_bytes = rle_encode($enc_bytes);
- }
- else {
- print $out_fh chr(0);
- $enc_bytes = rle4_encode($enc_bytes);
- }
- print $out_fh pack('N', $idx);
- print $out_fh encode_alphabet(\@alphabet);
- lzss_compression(pack('C*', @$enc_bytes), $out_fh);
- }
- sub decompression ($fh, $out_fh) {
- my $rle_encoded = ord(getc($fh) // die "error");
- my $idx = unpack('N', join('', map { getc($fh) // die "error" } 1 .. 4));
- my $alphabet = decode_alphabet($fh);
- my $dec = lzss_decompression($fh);
- my $bytes = [unpack('C*', $dec)];
- if ($rle_encoded) {
- $bytes = rle_decode($bytes);
- }
- else {
- $bytes = rle4_decode($bytes);
- }
- $bytes = mtf_decode($bytes, [@$alphabet]);
- print $out_fh pack('C*', @{rle4_decode([unpack('C*', bwt_decode(pack('C*', @$bytes), $idx))])});
- }
- # 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)) {
- compression($chunk, $out_fh);
- }
- # Close the output file
- 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)) {
- decompression($fh, $out_fh);
- }
- # Close the output file
- close $out_fh;
- }
- main();
- exit(0);
|