123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- #!/usr/bin/perl
- # Author: Trizen
- # Date: 15 December 2022
- # https://github.com/trizen
- # Compress/decompress files using LZ77 compression.
- use 5.020;
- use strict;
- use warnings;
- use experimental qw(signatures);
- use Getopt::Std qw(getopts);
- use File::Basename qw(basename);
- use constant {
- PKGNAME => 'LZ77',
- VERSION => '0.02',
- FORMAT => 'lz77',
- 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 compression ($str) {
- my @rep;
- 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(@rep, $p, $n, $chars[$la + $n]);
- $la += $n + 1;
- $prefix .= $token;
- }
- pack('(SCa)*', @rep);
- }
- # 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)) {
- print $out_fh compression($chunk);
- }
- # 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 $chunk = '';
- while (read($fh, (my $str), 4 * CHUNK_SIZE)) {
- my @decoded = unpack('(SCa)*', $str);
- while (@decoded) {
- my ($s, $l, $c) = splice(@decoded, 0, 3);
- $chunk .= substr($chunk, $s, $l) . $c;
- if (length($chunk) >= CHUNK_SIZE) {
- print $out_fh $chunk;
- $chunk = '';
- }
- }
- }
- print $out_fh $chunk;
- # Close the file
- close $fh;
- close $out_fh;
- }
- main();
- exit(0);
|