bwlza_file_compression.pl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #!/usr/bin/perl
  2. # Author: Trizen
  3. # Date: 15 June 2023
  4. # Edit: 21 March 2024
  5. # https://github.com/trizen
  6. # Compress/decompress files using Burrows-Wheeler Transform (BWT) + Move-to-front transform (MTF) + LZ77 compression (LZSS) + Arithmetic Coding (in fixed bits).
  7. # Encoding the literals and the pointers using a DEFLATE-like approach.
  8. # References:
  9. # Data Compression (Summer 2023) - Lecture 11 - DEFLATE (gzip)
  10. # https://youtube.com/watch?v=SJPvNi4HrWQ
  11. #
  12. # Data Compression (Summer 2023) - Lecture 13 - BZip2
  13. # https://youtube.com/watch?v=cvoZbBZ3M2A
  14. #
  15. # Basic arithmetic coder in C++
  16. # https://github.com/billbird/arith32
  17. use 5.036;
  18. use Getopt::Std qw(getopts);
  19. use File::Basename qw(basename);
  20. use List::Util qw(max uniq);
  21. use Compression::Util qw(:all);
  22. use constant {
  23. PKGNAME => 'BWLZA',
  24. VERSION => '0.03',
  25. FORMAT => 'bwlza',
  26. CHUNK_SIZE => 1 << 17, # higher value = better compression
  27. };
  28. # Container signature
  29. use constant SIGNATURE => uc(FORMAT) . chr(3);
  30. sub usage {
  31. my ($code) = @_;
  32. print <<"EOH";
  33. usage: $0 [options] [input file] [output file]
  34. options:
  35. -e : extract
  36. -i <filename> : input filename
  37. -o <filename> : output filename
  38. -r : rewrite output
  39. -v : version number
  40. -h : this message
  41. examples:
  42. $0 document.txt
  43. $0 document.txt archive.${\FORMAT}
  44. $0 archive.${\FORMAT} document.txt
  45. $0 -e -i archive.${\FORMAT} -o document.txt
  46. EOH
  47. exit($code // 0);
  48. }
  49. sub version {
  50. printf("%s %s\n", PKGNAME, VERSION);
  51. exit;
  52. }
  53. sub valid_archive {
  54. my ($fh) = @_;
  55. if (read($fh, (my $sig), length(SIGNATURE), 0) == length(SIGNATURE)) {
  56. $sig eq SIGNATURE || return;
  57. }
  58. return 1;
  59. }
  60. sub main {
  61. my %opt;
  62. getopts('ei:o:vhr', \%opt);
  63. $opt{h} && usage(0);
  64. $opt{v} && version();
  65. my ($input, $output) = @ARGV;
  66. $input //= $opt{i} // usage(2);
  67. $output //= $opt{o};
  68. my $ext = qr{\.${\FORMAT}\z}io;
  69. if ($opt{e} || $input =~ $ext) {
  70. if (not defined $output) {
  71. ($output = basename($input)) =~ s{$ext}{}
  72. || die "$0: no output file specified!\n";
  73. }
  74. if (not $opt{r} and -e $output) {
  75. print "'$output' already exists! -- Replace? [y/N] ";
  76. <STDIN> =~ /^y/i || exit 17;
  77. }
  78. decompress_file($input, $output)
  79. || die "$0: error: decompression failed!\n";
  80. }
  81. elsif ($input !~ $ext || (defined($output) && $output =~ $ext)) {
  82. $output //= basename($input) . '.' . FORMAT;
  83. compress_file($input, $output)
  84. || die "$0: error: compression failed!\n";
  85. }
  86. else {
  87. warn "$0: don't know what to do...\n";
  88. usage(1);
  89. }
  90. }
  91. sub compression ($chunk, $out_fh) {
  92. my @chunk_bytes = unpack('C*', $chunk);
  93. my $data = pack('C*', @{rle4_encode(\@chunk_bytes, 254)});
  94. my ($bwt, $idx) = bwt_encode($data);
  95. my @bytes = unpack('C*', $bwt);
  96. my @alphabet = sort { $a <=> $b } uniq(@bytes);
  97. my $enc_bytes = mtf_encode(\@bytes, \@alphabet);
  98. if (max(@$enc_bytes) < 255) {
  99. print $out_fh chr(1);
  100. $enc_bytes = zrle_encode($enc_bytes);
  101. }
  102. else {
  103. print $out_fh chr(0);
  104. $enc_bytes = rle4_encode($enc_bytes);
  105. }
  106. print $out_fh pack('N', $idx);
  107. print $out_fh encode_alphabet(\@alphabet);
  108. print $out_fh lzss_compress(pack('C*', @$enc_bytes), \&create_ac_entry);
  109. }
  110. sub decompression ($fh, $out_fh) {
  111. my $rle_encoded = ord(getc($fh) // die "error");
  112. my $idx = unpack('N', join('', map { getc($fh) // die "error" } 1 .. 4));
  113. my $alphabet = decode_alphabet($fh);
  114. my $dec = lzss_decompress($fh, \&decode_ac_entry);
  115. my $bytes = [unpack('C*', $dec)];
  116. if ($rle_encoded) {
  117. $bytes = zrle_decode($bytes);
  118. }
  119. else {
  120. $bytes = rle4_decode($bytes);
  121. }
  122. $bytes = mtf_decode($bytes, $alphabet);
  123. print $out_fh symbols2string(rle4_decode(string2symbols(bwt_decode(pack('C*', @$bytes), $idx))));
  124. }
  125. # Compress file
  126. sub compress_file ($input, $output) {
  127. open my $fh, '<:raw', $input
  128. or die "Can't open file <<$input>> for reading: $!";
  129. my $header = SIGNATURE;
  130. # Open the output file for writing
  131. open my $out_fh, '>:raw', $output
  132. or die "Can't open file <<$output>> for write: $!";
  133. # Print the header
  134. print $out_fh $header;
  135. # Compress data
  136. while (read($fh, (my $chunk), CHUNK_SIZE)) {
  137. compression($chunk, $out_fh);
  138. }
  139. # Close the output file
  140. close $out_fh;
  141. }
  142. # Decompress file
  143. sub decompress_file ($input, $output) {
  144. # Open and validate the input file
  145. open my $fh, '<:raw', $input
  146. or die "Can't open file <<$input>> for reading: $!";
  147. valid_archive($fh) || die "$0: file `$input' is not a \U${\FORMAT}\E v${\VERSION} archive!\n";
  148. # Open the output file
  149. open my $out_fh, '>:raw', $output
  150. or die "Can't open file <<$output>> for writing: $!";
  151. while (!eof($fh)) {
  152. decompression($fh, $out_fh);
  153. }
  154. # Close the output file
  155. close $out_fh;
  156. }
  157. main();
  158. exit(0);