| 1 | package EBook::Tools::LZSS; | ||
| 2 | 2 | ▶ | use warnings; use strict; use utf8; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 2 | ▶ | use English qw( -no_match_vars ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use version 0.74; our $VERSION = qv("0.4.8"); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | # Perl Critic overrides: | ||
| 7 | ## no critic (Package variable) | ||
| 8 | # RequireBriefOpen seems to be way too brief to be useful | ||
| 9 | ## no critic (RequireBriefOpen) | ||
| 10 | # Double-sigils are needed for lexical filehandles in clear print statements | ||
| 11 | ## no critic (Double-sigil dereference) | ||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | EBook::Tools::LZSS - Lempel-Ziv-Storer-Szymanski compression and decompression | ||
| 16 | |||
| 17 | =head1 SYNOPSIS | ||
| 18 | |||
| 19 | use EBook::Tools::LZSS; | ||
| 20 | my $lzss = EBook::Tools::LZSS->new(lengthbits => 3, | ||
| 21 | offsetbits => 14, | ||
| 22 | windowinit => 'the man'); | ||
| 23 | my $textref = $lzss->uncompress(\$data); | ||
| 24 | |||
| 25 | =cut | ||
| 26 | |||
| 27 | require Exporter; | ||
| 28 | 2 | ▶ | use base qw(Exporter); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 29 | |||
| 30 | our @EXPORT_OK; | ||
| 31 | @EXPORT_OK = qw ( | ||
| 32 | ); | ||
| 33 | our %EXPORT_TAGS = ('all' => [@EXPORT_OK]); | ||
| 34 | |||
| 35 | |||
| 36 | 2 | ▶ | use Bit::Vector; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 37 | 2 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 38 | 2 | ▶ | use EBook::Tools qw(:all); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 39 | use Encode; | ||
| 40 | use File::Basename qw(dirname fileparse); | ||
| 41 | use File::Path; # Exports 'mkpath' and 'rmtree' | ||
| 42 | binmode(STDERR,":utf8"); | ||
| 43 | |||
| 44 | |||
| 45 | use constant ENCODED => 0; | ||
| 46 | use constant UNCODED => 1; | ||
| 47 | |||
| 48 | |||
| 49 | |||
| 50 | #################################################### | ||
| 51 | ########## CONSTRUCTOR AND INITIALIZATION ########## | ||
| 52 | #################################################### | ||
| 53 | |||
| 54 | my %rwfields = ( | ||
| 55 | 'lengthbits' => 'integer', | ||
| 56 | 'offsetbits' => 'integer', | ||
| 57 | 'windowinit' => 'string', | ||
| 58 | 'windowsize' => 'integer', | ||
| 59 | 'windowstart' => 'integer', | ||
| 60 | 'maxuncoded' => 'integer', | ||
| 61 | 'screwybits' => 'boolean', | ||
| 62 | 'verbose' => 'integer', | ||
| 63 | ); | ||
| 64 | my %rofields = ( | ||
| 65 | ); | ||
| 66 | my %privatefields = ( | ||
| 67 | ); | ||
| 68 | |||
| 69 | # A simple 'use fields' will not work here: use takes place inside | ||
| 70 | # BEGIN {}, so the @...fields variables won't exist. | ||
| 71 | require fields; | ||
| 72 | fields->import( | ||
| 73 | keys(%rwfields),keys(%rofields),keys(%privatefields) | ||
| 74 | ); | ||
| 75 | |||
| 76 | |||
| 77 | =head1 CONSTRUCTOR AND INITIALIZATION | ||
| 78 | |||
| 79 | =head2 C<new(%args)> | ||
| 80 | |||
| 81 | Instantiates a new EBook::Tools::LZSS object. | ||
| 82 | |||
| 83 | =head3 Arguments | ||
| 84 | |||
| 85 | All arguments are optional, but must be identical between compression | ||
| 86 | and decompression for the result to be valid. | ||
| 87 | |||
| 88 | =over | ||
| 89 | |||
| 90 | =item * C<lengthbits> | ||
| 91 | |||
| 92 | The number of bits used to encode the length of a LZSS reference. If | ||
| 93 | not specified defaults to 4 bits. | ||
| 94 | |||
| 95 | The eBookwise .IMP format typically compresses with 3 length bits. | ||
| 96 | |||
| 97 | Note that the actual length of the LZSS reference in bytes is greater | ||
| 98 | than the value stored in the length bits. The actual number of bytes | ||
| 99 | returned is the decoded length bits value plus C<maxuncoded> plus 1, | ||
| 100 | |||
| 101 | =item * C<offsetbits> | ||
| 102 | |||
| 103 | The number of bits used to encode the offset to a LZSS reference. | ||
| 104 | This also determines the size of the sliding window of reference data. | ||
| 105 | If not specified, it defaults to 12 bits (4096-byte window). | ||
| 106 | |||
| 107 | The eBookwise .IMP format typically compresses with 14 offset bits | ||
| 108 | (16384-byte window). | ||
| 109 | |||
| 110 | =item * C<windowinit> | ||
| 111 | |||
| 112 | A string used to initalize the sliding window. If specified, this | ||
| 113 | string MUST be the same length as the window size, or the subroutine | ||
| 114 | will croak. If not specified, the window will be initialized with | ||
| 115 | spaces. | ||
| 116 | |||
| 117 | =item * C<windowstart> | ||
| 118 | |||
| 119 | The first byte position in the window that will be overwritten by | ||
| 120 | decoded text. If not specified, defaults to 0. | ||
| 121 | |||
| 122 | =item * C<maxuncoded> | ||
| 123 | |||
| 124 | The maximum number of uncoded bytes (?). This currently isn't used | ||
| 125 | for that purpose, but determines the actual length of a LZSS reference. | ||
| 126 | |||
| 127 | =item * C<screwybits> | ||
| 128 | |||
| 129 | If set to true and the number of offset bits is greater than 8, then | ||
| 130 | the offset bits will be read first in a chunk of 8 for the least | ||
| 131 | significant bits, and then the remaining bits will be read and use as | ||
| 132 | the most significant bits. This seems to be necessary for | ||
| 133 | compatibility with Michael Dipperstein's LZSS C compression library | ||
| 134 | but does not hold true for IMP e-book LZSS decompression. | ||
| 135 | |||
| 136 | =item * C<verbose> | ||
| 137 | |||
| 138 | If set to true, compression and uncompression will provide additional | ||
| 139 | status feedback on STDOUT. | ||
| 140 | |||
| 141 | =back | ||
| 142 | |||
| 143 | =cut | ||
| 144 | |||
| 145 | sub new ## no critic (Always unpack @_ first) | ||
| 146 | { | ||
| 147 | my $self = shift; | ||
| 148 | my $class = ref($self) || $self; | ||
| 149 | my %args = @_; | ||
| 150 | my $subname = (caller(0))[3]; | ||
| 151 | debug(2,"DEBUG[",$subname,"]"); | ||
| 152 | |||
| 153 | my %valid_args = ( | ||
| 154 | 'lengthbits' => 1, | ||
| 155 | 'offsetbits' => 1, | ||
| 156 | 'windowinit' => 1, | ||
| 157 | 'windowstart' => 1, | ||
| 158 | 'maxuncoded' => 1, | ||
| 159 | 'screwybits' => 1, | ||
| 160 | 'verbose' => 1, | ||
| 161 | ); | ||
| 162 | |||
| 163 | foreach my $arg (keys %args) | ||
| 164 | { | ||
| 165 | croak($subname,"(): invalid argument '",$arg,"'") | ||
| 166 | if(!$valid_args{$arg}); | ||
| 167 | } | ||
| 168 | |||
| 169 | $self = fields::new($class); | ||
| 170 | $self->{lengthbits} = $args{lengthbits} || 4; | ||
| 171 | $self->{offsetbits} = $args{offsetbits} || 12; | ||
| 172 | $self->{windowsize} = 1 << $self->{offsetbits}; | ||
| 173 | $self->{windowinit} = $args{windowinit} || ' ' x $self->{windowsize}; | ||
| 174 | if(defined $args{windowstart}) | ||
| 175 | { | ||
| 176 | $self->{windowstart} = $args{windowstart}; | ||
| 177 | } | ||
| 178 | else { $self->{windowstart} = 0; } | ||
| 179 | $self->{maxuncoded} = $args{maxuncoded} || 2; | ||
| 180 | $self->{screwybits} = $args{screwybits}; | ||
| 181 | $self->{verbose} = $args{verbose} || 0; | ||
| 182 | |||
| 183 | if($self->{windowinit} | ||
| 184 | and length($self->{windowinit}) != $self->{windowsize}) | ||
| 185 | { | ||
| 186 | croak($subname,"(): \n window initialization string size (", | ||
| 187 | length($self->{windowinit}), | ||
| 188 | " does not match specified number of offset bits (", | ||
| 189 | $self->{windowsize},")\n") | ||
| 190 | } | ||
| 191 | |||
| 192 | return $self; | ||
| 193 | } | ||
| 194 | |||
| 195 | |||
| 196 | ############################# | ||
| 197 | ########## METHODS ########## | ||
| 198 | ############################# | ||
| 199 | |||
| 200 | =head1 METHODS | ||
| 201 | |||
| 202 | =head2 C<uncompress(\$dataref)> | ||
| 203 | |||
| 204 | Takes a reference to a compressed data string, uncompresses the data | ||
| 205 | string and returns a reference to the uncompressed string. | ||
| 206 | |||
| 207 | =cut | ||
| 208 | |||
| 209 | sub uncompress :method | ||
| 210 | { | ||
| 211 | my $self = shift; | ||
| 212 | my ($dataref) = @_; | ||
| 213 | my $subname = (caller(0))[3]; | ||
| 214 | debug(2,"DEBUG[",$subname,"]"); | ||
| 215 | |||
| 216 | croak($subname,"(): no data provided!\n") | ||
| 217 | unless($dataref); | ||
| 218 | croak($subname,"(): data is not a reference!\n") | ||
| 219 | unless(ref $dataref); | ||
| 220 | |||
| 221 | my $lengthbits = $self->{lengthbits} || 4; | ||
| 222 | my $offsetbits = $self->{offsetbits} || 12; | ||
| 223 | my $windowsize = $self->{windowsize}; | ||
| 224 | my $window = $self->{windowinit}; | ||
| 225 | my $max_uncoded = $self->{maxuncoded} || 2; | ||
| 226 | my $max_encoded = (1 << $lengthbits) + $max_uncoded; | ||
| 227 | my $windowpos = $self->{windowstart} || 0; | ||
| 228 | |||
| 229 | if(length($window) != $windowsize) | ||
| 230 | { | ||
| 231 | croak($subname,"(): \n window initialization string size (", | ||
| 232 | length($window), | ||
| 233 | " does not match specified number of offset bits (", | ||
| 234 | $windowsize,")\n") | ||
| 235 | } | ||
| 236 | |||
| 237 | if($self->{verbose} > 1) | ||
| 238 | { | ||
| 239 | print {*STDERR} ("Uncompressing with ",$lengthbits," length bits, ", | ||
| 240 | $offsetbits," offset bits, windowpos = ", | ||
| 241 | $windowpos,"\n"); | ||
| 242 | } | ||
| 243 | |||
| 244 | my $bitsize = length($$dataref) * 8; | ||
| 245 | my $bitvector = Bit::Vector->new($bitsize); | ||
| 246 | my $bitoffset; | ||
| 247 | my $bitpos = 0; | ||
| 248 | my $bits; | ||
| 249 | |||
| 250 | my $encflag; | ||
| 251 | my $dataoffset = 0; | ||
| 252 | my $lzss_offset; | ||
| 253 | my $lzss_length; | ||
| 254 | my $lzss_text; | ||
| 255 | my $uncompressed; | ||
| 256 | |||
| 257 | # Unfortunately, the bitstream needs to be processed from left to | ||
| 258 | # right, and Bit::Vector really likes to process from right to | ||
| 259 | # left, so we have to process by chunk instead of using | ||
| 260 | # Block_Store | ||
| 261 | while($bitpos < $bitsize) | ||
| 262 | { | ||
| 263 | $bitoffset = $bitsize-$bitpos; | ||
| 264 | $bits = unpack('C',substr($$dataref,$dataoffset,1)); | ||
| 265 | $bitvector->Chunk_Store(8,$bitoffset-8,$bits); | ||
| 266 | $dataoffset++; | ||
| 267 | $bitpos += 8; | ||
| 268 | } | ||
| 269 | |||
| 270 | $bitpos = 0; | ||
| 271 | while($bitpos < $bitsize - 8) | ||
| 272 | { | ||
| 273 | # If there are less than 8 bits left, there's nothing to | ||
| 274 | # decode and we can stop. Otherwise, uncompress. | ||
| 275 | |||
| 276 | if($self->{verbose} or $EBook::Tools::debug > 0) | ||
| 277 | { | ||
| 278 | my $percent = int( ($bitpos / $bitsize) * 100); | ||
| 279 | |||
| 280 | print("Uncompressing text... [",$percent,"%]\r") | ||
| 281 | if($percent % 5 == 0); | ||
| 282 | } | ||
| 283 | $bitoffset = $bitsize - $bitpos; | ||
| 284 | $encflag = $bitvector->Chunk_Read(1,$bitoffset-1); | ||
| 285 | $bitpos++; | ||
| 286 | $bitoffset--; | ||
| 287 | |||
| 288 | if($encflag == UNCODED) | ||
| 289 | { | ||
| 290 | if($bitoffset - 8 < 0) | ||
| 291 | { | ||
| 292 | debug(1,"DEBUG: ran out of bits at bit position ", | ||
| 293 | $bitpos,"!"); | ||
| 294 | last; | ||
| 295 | } | ||
| 296 | |||
| 297 | $bitoffset -= 8; | ||
| 298 | $bitpos += 8; | ||
| 299 | $bits = $bitvector->Chunk_Read(8,$bitoffset); | ||
| 300 | $uncompressed .= chr($bits); | ||
| 301 | substr($window,$windowpos,1,chr($bits)); | ||
| 302 | $windowpos++; | ||
| 303 | $windowpos %= $windowsize; | ||
| 304 | } | ||
| 305 | else | ||
| 306 | { | ||
| 307 | if($bitoffset - ($offsetbits + $lengthbits) < 0) | ||
| 308 | { | ||
| 309 | if($bitoffset >= 16) | ||
| 310 | { | ||
| 311 | carp($subname,,"(): ran out of bits at bit position ", | ||
| 312 | $bitpos," [",$bitoffset," bits remaining]!"); | ||
| 313 | } | ||
| 314 | last; | ||
| 315 | } | ||
| 316 | if($self->{screwybits} and $offsetbits > 8) | ||
| 317 | { | ||
| 318 | my $lowoffset; | ||
| 319 | my $highoffset; | ||
| 320 | my $extrabits = $offsetbits - 8; | ||
| 321 | |||
| 322 | $bitoffset -= 8; | ||
| 323 | $lowoffset = $bitvector->Chunk_Read(8,$bitoffset); | ||
| 324 | $bitoffset -= $extrabits; | ||
| 325 | $highoffset = $bitvector->Chunk_Read($extrabits,$bitoffset); | ||
| 326 | |||
| 327 | $lzss_offset = (256 * $highoffset) + $lowoffset; | ||
| 328 | } | ||
| 329 | else | ||
| 330 | { | ||
| 331 | $bitoffset -= $offsetbits; | ||
| 332 | $lzss_offset = $bitvector->Chunk_Read($offsetbits,$bitoffset); | ||
| 333 | } | ||
| 334 | $bitoffset -= $lengthbits; | ||
| 335 | $lzss_length = $bitvector->Chunk_Read($lengthbits,$bitoffset); | ||
| 336 | |||
| 337 | if($lzss_offset > $windowsize) | ||
| 338 | { | ||
| 339 | carp($subname,"(): LZSS offset points outside window!\n"); | ||
| 340 | last; | ||
| 341 | } | ||
| 342 | |||
| 343 | if($lzss_length == 0 | ||
| 344 | and $lzss_offset == 0) | ||
| 345 | { | ||
| 346 | if($bitoffset >= 16) | ||
| 347 | { | ||
| 348 | carp($subname,"(): invalid LZSS code at bit position ", | ||
| 349 | $bitpos," [",$bitoffset," bits remaining]!\n"); | ||
| 350 | } | ||
| 351 | last; | ||
| 352 | } | ||
| 353 | $bitpos += ($offsetbits + $lengthbits); | ||
| 354 | $lzss_length += $max_uncoded + 1; | ||
| 355 | |||
| 356 | foreach my $lzss_pos (0 .. ($lzss_length - 1) ) | ||
| 357 | { | ||
| 358 | my $windowoffset; | ||
| 359 | my $char = substr($window, | ||
| 360 | ($lzss_offset + $lzss_pos) % $windowsize, | ||
| 361 | 1); | ||
| 362 | substr($window,$windowpos,1,$char); | ||
| 363 | $windowpos++; | ||
| 364 | $windowpos %= $windowsize; | ||
| 365 | $uncompressed .= $char; | ||
| 366 | } | ||
| 367 | } | ||
| 368 | } | ||
| 369 | |||
| 370 | if($self->{verbose} or $EBook::Tools::debug > 0) | ||
| 371 | { | ||
| 372 | print("Finished uncompressing text.\n"); | ||
| 373 | } | ||
| 374 | return \$uncompressed; | ||
| 375 | } | ||
| 376 | |||
| 377 | |||
| 378 | ################################ | ||
| 379 | ########## PROCEDURES ########## | ||
| 380 | ################################ | ||
| 381 | |||
| 382 | |||
| 383 | ########## END CODE ########## | ||
| 384 | |||
| 385 | =head1 BUGS AND LIMITATIONS | ||
| 386 | |||
| 387 | =over | ||
| 388 | |||
| 389 | =item * Compression not yet implemented. | ||
| 390 | |||
| 391 | =item * The LZSS algorithm isn't documented in the POD. | ||
| 392 | |||
| 393 | =item * Once compression is implemented and the algorithm is | ||
| 394 | documented, this module is a good candidate for being forked off into | ||
| 395 | its own project. Relying on it to keep its current name may be | ||
| 396 | unwise. | ||
| 397 | |||
| 398 | =back | ||
| 399 | |||
| 400 | |||
| 401 | =head1 AUTHOR | ||
| 402 | |||
| 403 | Zed Pobre <zed@debian.org> | ||
| 404 | |||
| 405 | The design of this module was based on the C LZSS library by Michael | ||
| 406 | Dipperstein, version 0.5.2, at http://michael.dipperstein.com/lzss/ | ||
| 407 | |||
| 408 | |||
| 409 | =head1 LICENSE AND COPYRIGHT | ||
| 410 | |||
| 411 | Copyright 2008 Zed Pobre | ||
| 412 | |||
| 413 | Licensed to the public under the terms of the GNU GPL, version 2. | ||
| 414 | |||
| 415 | =cut | ||
| 416 | |||
| 417 | 1; | ||
| 418 | __END__ | ||