| 1 | package Net::SSH::Perl::Comp::Zlib; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Carp qw( croak ); | ||
| 6 | require Compress::Zlib; | ||
| 7 | |||
| 8 | use Net::SSH::Perl::Comp; | ||
| 9 | use base qw( Net::SSH::Perl::Comp ); | ||
| 10 | |||
| 11 | sub init { | ||
| 12 | my $comp = shift; | ||
| 13 | my($level) = @_; | ||
| 14 | my($err); | ||
| 15 | |||
| 16 | ($comp->{d}, $err) = Compress::Zlib::deflateInit({ Level => $level }); | ||
| 17 | croak "Can't create outgoing compression stream" | ||
| 18 | unless $err == Compress::Zlib::Z_OK(); | ||
| 19 | |||
| 20 | ($comp->{i}, $err) = Compress::Zlib::inflateInit(); | ||
| 21 | croak "Can't create incoming compression stream" | ||
| 22 | unless $err == Compress::Zlib::Z_OK(); | ||
| 23 | } | ||
| 24 | |||
| 25 | sub compress { | ||
| 26 | my $comp = shift; | ||
| 27 | my($data) = @_; | ||
| 28 | my $d = $comp->{d}; | ||
| 29 | my($compressed, $err); | ||
| 30 | { | ||
| 31 | my($output, $out); | ||
| 32 | ($output, $err) = $d->deflate($data); | ||
| 33 | last unless $err == Compress::Zlib::Z_OK(); | ||
| 34 | ($out, $err) = $d->flush(Compress::Zlib::Z_PARTIAL_FLUSH()); | ||
| 35 | last unless $err == Compress::Zlib::Z_OK(); | ||
| 36 | |||
| 37 | $compressed = $output . $out; | ||
| 38 | } | ||
| 39 | croak "Error while compressing: $err" unless defined $compressed; | ||
| 40 | $compressed; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub uncompress { | ||
| 44 | my $comp = shift; | ||
| 45 | my($data) = @_; | ||
| 46 | my $i = $comp->{i}; | ||
| 47 | my($inflated, $err); | ||
| 48 | { | ||
| 49 | my($out); | ||
| 50 | ($out, $err) = $i->inflate($data); | ||
| 51 | last unless $err == Compress::Zlib::Z_OK(); | ||
| 52 | |||
| 53 | $inflated = $out; | ||
| 54 | } | ||
| 55 | croak "Error while inflating: $err" | ||
| 56 | unless defined $inflated; | ||
| 57 | $inflated; | ||
| 58 | } | ||
| 59 | |||
| 60 | 1; | ||
| 61 | __END__ |