lzt2_file_compression.pl 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #!/usr/bin/perl
  2. # Author: Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Created on: 21 May 2014
  5. # Latest edit on: 28 May 2014
  6. # Website: https://github.com/trizen
  7. # A new type of LZ compression, featuring a very short decompression time.
  8. use 5.010;
  9. use strict;
  10. use autodie;
  11. use warnings;
  12. use Getopt::Std qw(getopts);
  13. use File::Basename qw(basename);
  14. use constant {
  15. PKGNAME => 'lzt2',
  16. VERSION => '0.01',
  17. FORMAT => 'lzt2',
  18. };
  19. use constant {
  20. MIN => 9,
  21. BUFFER => 2**16,
  22. SIGNATURE => uc(FORMAT) . chr(1),
  23. };
  24. sub usage {
  25. my ($code) = @_;
  26. print <<"EOH";
  27. usage: $0 [options] [input file] [output file]
  28. options:
  29. -e : extract
  30. -i <filename> : input filename
  31. -o <filename> : output filename
  32. -r : rewrite output
  33. -v : version number
  34. -h : this message
  35. examples:
  36. $0 document.txt
  37. $0 document.txt archive.${\FORMAT}
  38. $0 archive.${\FORMAT} document.txt
  39. $0 -e -i archive.${\FORMAT} -o document.txt
  40. EOH
  41. exit($code // 0);
  42. }
  43. sub version {
  44. printf("%s %s\n", PKGNAME, VERSION);
  45. exit;
  46. }
  47. sub main {
  48. my %opt;
  49. getopts('ei:o:vhr', \%opt);
  50. $opt{h} && usage(0);
  51. $opt{v} && version();
  52. my ($input, $output) = @ARGV;
  53. $input //= $opt{i} // usage(2);
  54. $output //= $opt{o};
  55. my $ext = qr{\.${\FORMAT}\z}io;
  56. if ($opt{e} || $input =~ $ext) {
  57. if (not defined $output) {
  58. ($output = basename($input)) =~ s{$ext}{}
  59. || die "$0: no output file specified!\n";
  60. }
  61. if (not $opt{r} and -e $output) {
  62. print "'$output' already exists! -- Replace? [y/N] ";
  63. <STDIN> =~ /^y/i || exit 17;
  64. }
  65. decompress($input, $output)
  66. || die "$0: error: decompression failed!\n";
  67. }
  68. elsif ($input !~ $ext || (defined($output) && $output =~ $ext)) {
  69. $output //= basename($input) . '.' . FORMAT;
  70. compress($input, $output)
  71. || die "$0: error: compression failed!\n";
  72. }
  73. else {
  74. warn "$0: don't know what to do...\n";
  75. usage(1);
  76. }
  77. }
  78. sub valid_archive {
  79. my ($fh) = @_;
  80. if (read($fh, (my $sig), length(SIGNATURE), 0) == length(SIGNATURE)) {
  81. $sig eq SIGNATURE || return;
  82. }
  83. return 1;
  84. }
  85. sub compress {
  86. my ($input, $output) = @_;
  87. # Open the input file
  88. open my $fh, '<:raw', $input;
  89. # Open the output file and write the archive signature
  90. open my $out_fh, '>:raw', $output;
  91. print {$out_fh} SIGNATURE;
  92. while ((my $len = read($fh, (my $block), BUFFER)) > 0) {
  93. my %dict;
  94. foreach my $i (reverse(MIN .. 255)) {
  95. for (my $lim = $len - $i * 2, my $j = 0 ; $j <= $lim ; $j++) {
  96. if ((my $pos = index($block, substr($block, $j, $i), $j + $i)) != -1) {
  97. if (not exists $dict{$pos} or $i > $dict{$pos}[1]) {
  98. $dict{$pos} = [$j, $i];
  99. }
  100. }
  101. else {
  102. $j += int($i / MIN) - 1;
  103. }
  104. }
  105. }
  106. my @pairs;
  107. my $last_pos = 0;
  108. my $uncompressed = '';
  109. for (my $i = 0 ; $i < $len ; $i++, $last_pos++) {
  110. if (exists $dict{$i}) {
  111. my ($key, $vlen) = @{$dict{$i}};
  112. push @pairs, [$last_pos, $key, $vlen];
  113. $i += $vlen - 1;
  114. $last_pos = 0;
  115. }
  116. else {
  117. $uncompressed .= substr($block, $i, 1);
  118. }
  119. }
  120. my $uncomp_len = length($uncompressed);
  121. printf("%3d -> %3d (%.2f%%)\n", $len, $uncomp_len, ($len - $uncomp_len) / $len * 100);
  122. print {$out_fh} pack('S', $uncomp_len - 1), pack('S', scalar @pairs), (map { pack('SSC', @{$_}) } @pairs), $uncompressed;
  123. }
  124. close $fh;
  125. close $out_fh;
  126. }
  127. sub decompress {
  128. my ($input, $output) = @_;
  129. # Open and validate the input file
  130. open my $fh, '<:raw', $input;
  131. valid_archive($fh) || die "$0: file `$input' is not a \U${\FORMAT}\E archive!\n";
  132. # Open the output file
  133. open my $out_fh, '>:raw', $output;
  134. while (read($fh, (my $len_byte), 2) > 0) {
  135. read($fh, (my $groups_byte), 2);
  136. my @dict;
  137. for my $i (1 .. unpack('S', $groups_byte)) {
  138. read($fh, (my $positions), 5);
  139. push @dict, [unpack('SSC', $positions)];
  140. }
  141. my $len = unpack('S', $len_byte) + 1;
  142. read($fh, (my $block), $len);
  143. my $last_pos = 0;
  144. my $decompressed = '';
  145. for (my $i = 0 ; $i <= $len ; $i++) {
  146. if (@dict and ($i - $last_pos == $dict[0][0])) {
  147. $decompressed .= substr($decompressed, $dict[0][1], $dict[0][2]);
  148. $last_pos = --$i;
  149. shift @dict;
  150. }
  151. else {
  152. $decompressed .= substr($block, $i, 1);
  153. }
  154. }
  155. print {$out_fh} $decompressed;
  156. }
  157. close $fh;
  158. close $out_fh;
  159. }
  160. main();
  161. exit(0);