EBook-Tools
view release on metacpan or search on metacpan
lib/EBook/Tools/LZSS.pm view on Meta::CPAN
use Encode;
use File::Basename qw(dirname fileparse);
use File::Path; # Exports 'mkpath' and 'rmtree'
binmode(STDERR,':encoding(UTF-8)');
use constant ENCODED => 0;
use constant UNCODED => 1;
####################################################
########## CONSTRUCTOR AND INITIALIZATION ##########
####################################################
my %rwfields = (
'lengthbits' => 'integer',
'offsetbits' => 'integer',
'windowinit' => 'string',
'windowsize' => 'integer',
'windowstart' => 'integer',
'maxuncoded' => 'integer',
'screwybits' => 'boolean',
'verbose' => 'integer',
);
my %rofields = (
);
my %privatefields = (
);
# A simple 'use fields' will not work here: use takes place inside
# BEGIN {}, so the @...fields variables won't exist.
require fields;
fields->import(
keys(%rwfields),keys(%rofields),keys(%privatefields)
);
=head1 CONSTRUCTOR AND INITIALIZATION
=head2 C<new(%args)>
Instantiates a new EBook::Tools::LZSS object.
=head3 Arguments
All arguments are optional, but must be identical between compression
and decompression for the result to be valid.
=over
=item * C<lengthbits>
The number of bits used to encode the length of a LZSS reference. If
not specified defaults to 4 bits.
The eBookwise .IMP format typically compresses with 3 length bits.
Note that the actual length of the LZSS reference in bytes is greater
than the value stored in the length bits. The actual number of bytes
returned is the decoded length bits value plus C<maxuncoded> plus 1,
=item * C<offsetbits>
The number of bits used to encode the offset to a LZSS reference.
This also determines the size of the sliding window of reference data.
If not specified, it defaults to 12 bits (4096-byte window).
The eBookwise .IMP format typically compresses with 14 offset bits
(16384-byte window).
=item * C<windowinit>
A string used to initalize the sliding window. If specified, this
string MUST be the same length as the window size, or the subroutine
will croak. If not specified, the window will be initialized with
spaces.
=item * C<windowstart>
The first byte position in the window that will be overwritten by
decoded text. If not specified, defaults to 0.
=item * C<maxuncoded>
The maximum number of uncoded bytes (?). This currently isn't used
for that purpose, but determines the actual length of a LZSS reference.
=item * C<screwybits>
If set to true and the number of offset bits is greater than 8, then
the offset bits will be read first in a chunk of 8 for the least
significant bits, and then the remaining bits will be read and use as
the most significant bits. This seems to be necessary for
compatibility with Michael Dipperstein's LZSS C compression library
but does not hold true for IMP e-book LZSS decompression.
=item * C<verbose>
If set to true, compression and uncompression will provide additional
status feedback on STDOUT.
=back
=cut
sub new ## no critic (Always unpack @_ first)
{
my $self = shift;
my $class = ref($self) || $self;
my %args = @_;
my $subname = (caller(0))[3];
debug(2,"DEBUG[",$subname,"]");
my %valid_args = (
'lengthbits' => 1,
'offsetbits' => 1,
'windowinit' => 1,
'windowstart' => 1,
'maxuncoded' => 1,
'screwybits' => 1,
'verbose' => 1,
);
foreach my $arg (keys %args)
{
croak($subname,"(): invalid argument '",$arg,"'")
if(!$valid_args{$arg});
}
$self = fields::new($class);
$self->{lengthbits} = $args{lengthbits} || 4;
$self->{offsetbits} = $args{offsetbits} || 12;
$self->{windowsize} = 1 << $self->{offsetbits};
$self->{windowinit} = $args{windowinit} || ' ' x $self->{windowsize};
if(defined $args{windowstart})
{
$self->{windowstart} = $args{windowstart};
}
else { $self->{windowstart} = 0; }
$self->{maxuncoded} = $args{maxuncoded} || 2;
$self->{screwybits} = $args{screwybits};
( run in 0.538 second using v1.01-cache-2.11-cpan-2398b32b56e )