LZMA compression + tests
[slob.git] / lib / Slob.pm
index 4b796cb91c26bd7562ccbb050d5bcbb2c9ba19bc..818ff785ed7fe6fd298fe3a9ff0c7142aa4713f8 100644 (file)
@@ -10,12 +10,30 @@ use constant MAGIC => "!-1SLOB\x1F";
 use Carp qw/croak verbose/;
 use Encode;
 
+use Compress::Raw::Lzma;
+
+our %UNCOMPRESS = (
+       '' => sub { $_[0] },
+       'lzma2' => sub {
+               my ($input) = @_;
+               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;
+               $output
+       }
+);
+
 sub new {
        my ($class, $path) = @_;
-       my $fh =
-         ref $path eq 'IO'
-         ? $path
-         :  open my $fh, '<', $path or croak "Cannot open \"$path\": $!";
+       my $fh;
+       if (ref $path eq 'IO') {
+               $fh = $path
+       } else {
+               open $fh, '<', $path or croak "Cannot open \"$path\": $!"
+       }
        my $self = bless {path => $path, fh => $fh}, $class;
        $self->{header} = $self->read_header;
        $self
@@ -109,7 +127,7 @@ sub ftell {
 
 sub uncompress {
        my ($self, $data) = @_;
-       $data
+       $UNCOMPRESS{$self->{header}{compression}}->($data)
 }
 
 sub read_header {
@@ -122,7 +140,7 @@ sub read_header {
        $self->{encoding} = $encoding;
 
        my $compression = $self->read_tiny_text;
-       die "Compression not yet supported" if $compression;
+       die "Compression '$compression' not yet supported" unless exists $UNCOMPRESS{$compression};
        my %tags = $self->read_tags;
        my @content_types = $self->read_content_types;
        my $blob_count = $self->read_int;
This page took 0.009938 seconds and 4 git commands to generate.