123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- #!/usr/bin/perl
- # Author: Trizen
- # Date: 15 December 2022
- # Edit: 19 March 2024
- # https://github.com/trizen
- # Compress/decompress files using LZ77 compression + Huffman coding.
- use 5.020;
- use strict;
- use warnings;
- use experimental qw(signatures);
- use Getopt::Std qw(getopts);
- use File::Basename qw(basename);
- use List::Util qw(max);
- use constant {
- PKGNAME => 'LZH',
- VERSION => '0.02',
- FORMAT => 'lzh',
- CHUNK_SIZE => 1 << 16,
- };
- # Container signature
- use constant SIGNATURE => uc(FORMAT) . chr(2);
- 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 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 lz77_compression ($str, $uncompressed, $indices, $lengths) {
- my $la = 0;
- my $prefix = '';
- my @chars = split(//, $str);
- my $end = $#chars;
- while ($la <= $end) {
- my $n = 1;
- my $p = 0;
- my $tmp;
- my $token = $chars[$la];
- while ( $n < 255
- and $la + $n <= $end
- and ($tmp = index($prefix, $token, $p)) >= 0) {
- $p = $tmp;
- $token .= $chars[$la + $n];
- ++$n;
- }
- --$n;
- push @$indices, $p;
- push @$lengths, $n;
- push @$uncompressed, $chars[$la + $n];
- $la += $n + 1;
- $prefix .= $token;
- }
- return;
- }
- sub lz77_decompression ($uncompressed, $indices, $lengths) {
- my $ret = '';
- my $chunk = '';
- foreach my $i (0 .. $#{$uncompressed}) {
- $chunk .= substr($chunk, $indices->[$i], $lengths->[$i]) . chr($uncompressed->[$i]);
- if (length($chunk) >= CHUNK_SIZE) {
- $ret .= $chunk;
- $chunk = '';
- }
- }
- if ($chunk ne '') {
- $ret .= $chunk;
- }
- $ret;
- }
- # 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;
- say "Max symbol: $max_symbol";
- 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 [];
- }
- # 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 (@uncompressed, @indices, @lengths);
- # Compress data
- while (read($fh, (my $chunk), CHUNK_SIZE)) {
- lz77_compression($chunk, \@uncompressed, \@indices, \@lengths);
- }
- @indices = unpack('C*', pack('S*', @indices));
- @uncompressed = unpack('C*', join('', @uncompressed));
- create_huffman_entry(\@uncompressed, $out_fh);
- create_huffman_entry(\@indices, $out_fh);
- create_huffman_entry(\@lengths, $out_fh);
- # Close the 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: $!";
- my $uncompressed = decode_huffman_entry($fh);
- my @indices = unpack('S*', pack('C*', @{decode_huffman_entry($fh)}));
- my $lengths = decode_huffman_entry($fh);
- print $out_fh lz77_decompression($uncompressed, \@indices, $lengths);
- # Close the file
- close $fh;
- close $out_fh;
- }
- main();
- exit(0);
|