From: Marius Gavrilescu Date: Sat, 6 Jan 2018 18:06:47 +0000 (+0200) Subject: Add bz2 and zlib support X-Git-Tag: 0.002~1 X-Git-Url: http://git.ieval.ro/?p=slob.git;a=commitdiff_plain;h=1ca60f5557ed528aa1cdd7c50737c5d51b94682a Add bz2 and zlib support --- diff --git a/MANIFEST b/MANIFEST index 48b0971..c54d7da 100644 --- a/MANIFEST +++ b/MANIFEST @@ -4,5 +4,7 @@ MANIFEST README t/Slob.t t/freedict-uncompressed.slob +t/freedict-bz2.slob t/freedict-lzma2.slob +t/freedict-zlib.slob lib/Slob.pm diff --git a/Makefile.PL b/Makefile.PL index 317d6c4..5688370 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -14,7 +14,9 @@ WriteMakefile( LICENSE => 'perl', SIGN => 1, PREREQ_PM => { - qw/Compress::Raw::Lzma 0/, + qw/Compress::Raw::Bzip2 0 + Compress::Raw::Lzma 0 + Compress::Raw::Zlib 0/, }, META_ADD => { dynamic_config => 0, diff --git a/lib/Slob.pm b/lib/Slob.pm index b64664f..3e9697f 100644 --- a/lib/Slob.pm +++ b/lib/Slob.pm @@ -10,7 +10,9 @@ use constant MAGIC => "!-1SLOB\x1F"; use Carp qw/croak verbose/; use Encode; +use Compress::Raw::Bzip2; use Compress::Raw::Lzma; +use Compress::Raw::Zlib; our %UNCOMPRESS = ( '' => sub { $_[0] }, @@ -19,9 +21,37 @@ our %UNCOMPRESS = ( my ($lzma2, $code, $output); ($lzma2, $code) = Compress::Raw::Lzma::RawDecoder->new(Filter => Lzma::Filter::Lzma2()); die "Error creating LZMA2 decoder: $code\n" unless $code == LZMA_OK; + $code = $lzma2->code($input, $output); - die "Did not reach end of stream" if $code == LZMA_OK; - die "Error decoding LZMA2: $code" if $code != LZMA_STREAM_END; + die "Did not reach end of stream\n" if $code == LZMA_OK; + die "Error decoding LZMA2: $code\n" if $code != LZMA_STREAM_END; + $output + }, + + 'bz2' => sub { + my ($input) = @_; + my ($bz2, $code, $output); + ($bz2, $code)= Compress::Raw::Bunzip2->new; + die "Error creating Bunzip2: $code\n" unless $code == Z_OK; + + $code = $bz2->bzinflate($input, $output); + die "Did not reach end of stream\n" if $code == BZ_OK; + die "Error decoding Bzip2: $code\n" if $code != BZ_STREAM_END; + + $output + }, + + 'zlib' => sub { + my ($input) = @_; + my ($zlib, $code, $output); + ($zlib, $code) = Compress::Raw::Zlib::Inflate->new( + -WindowBits => WANT_GZIP_OR_ZLIB + ); + die "Error creating Zlib inflate: $code\n" unless $code == Z_OK; + + $code = $zlib->inflate($input, \$output, 1); + die "Did not reach end of stream\n" if $code == Z_OK; + die "Error inflating zlib: $code\n" if $code != Z_STREAM_END; $output } ); diff --git a/t/Slob.t b/t/Slob.t index ef2aa21..70bc6e2 100644 --- a/t/Slob.t +++ b/t/Slob.t @@ -2,11 +2,12 @@ use strict; use warnings; -use Test::More tests => 13; +use Test::More tests => 25; BEGIN { use_ok('Slob') }; -for my $path (qw/freedict-uncompressed.slob freedict-lzma2.slob/) { - my $slob = Slob->new("t/$path"); +for my $path () { + note "Now using $path"; + my $slob = Slob->new($path); my $nr_of_entries = $slob->ref_count; diff --git a/t/freedict-bz2.slob b/t/freedict-bz2.slob new file mode 100644 index 0000000..ec77322 Binary files /dev/null and b/t/freedict-bz2.slob differ diff --git a/t/freedict-zlib.slob b/t/freedict-zlib.slob new file mode 100644 index 0000000..b74dd62 Binary files /dev/null and b/t/freedict-zlib.slob differ