| 1 | package Data::Transform::Zlib; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Compress::Raw::Zlib qw(Z_OK Z_STREAM_END Z_FINISH Z_SYNC_FLUSH); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use base qw(Data::Transform); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 NAME | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | Data::Transform::Zlib - A Filter for RFC195[0-2] | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | Data::Transform::Zlib provides a filter for performing (de-)compression | ||||||||||||||||||||||||||||
| 20 | using L<Compress::Raw::Zlib>. Since it is just a wrapper around that | ||||||||||||||||||||||||||||
| 21 | module, it supports the same features. | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 METHODS | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | Data::Transform::Zlib implements the standard Data::Transform API. Only | ||||||||||||||||||||||||||||
| 26 | additions and differences are documented here. | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =cut | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | sub BUFFER () { 0 } | ||||||||||||||||||||||||||||
| 31 | sub DEFLATER () { 1 } | ||||||||||||||||||||||||||||
| 32 | sub DEFLATE_OPTIONS () { 2 } | ||||||||||||||||||||||||||||
| 33 | sub INFLATER () { 3 } | ||||||||||||||||||||||||||||
| 34 | sub INFLATE_OPTIONS () { 4 } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =head2 new | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | new() creates a new object. It accepts the following named parameters: | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =over 2 | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =item inflate | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | A hashref containing parameters suitable to pass to Compress::Raw::Zlib::Inflate(). | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =item deflate | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | A hashref containing parameters suitable to pass to Compress::Raw::Zlib::Deflate(). | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =back | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | The only really interesting thing to set here is -WindowBits. If you | ||||||||||||||||||||||||||||
| 53 | set it to WANT_GZIP (symbol exported by Compress::Raw::Zlib), it will | ||||||||||||||||||||||||||||
| 54 | use gzip compression instead of zlib. | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | See the L<Compress::Raw::Zlib> documentation for more details. | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =cut | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=19.0
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | 2 | my $type = shift; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 2 | my $self = [ | |||||||||||||||||||||||||||
| 64 | [], | ||||||||||||||||||||||||||||
| 65 | ]; | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 2 | ▶ | croak "$type requires an even number of parameters" if @_ % 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 2 | my %param = @_; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 2 | ▶ | my $deflate_options = $self->[DEFLATE_OPTIONS] = $param{deflate} || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$param{'deflate'} || {}
MC/DC0%0/1
$param{'deflate'} || {}
$param{'deflate'}
Truth table50%1/2
A
A= $param{'deflate'}
| |||||||||||||||||||||||||||||
| 71 | 2 | ▶ | $self->[DEFLATER] = Compress::Raw::Zlib::Deflate->new(%$deflate_options) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | or croak "Couldn't create deflater object"; | ||||||||||||||||||||||||||||
| 73 | 2 | ▶ | my $inflate_options = $self->[INFLATE_OPTIONS] = $param{inflate} || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$param{'inflate'} || {}
MC/DC0%0/1
$param{'inflate'} || {}
$param{'inflate'}
Truth table50%1/2
A
A= $param{'inflate'}
| |||||||||||||||||||||||||||||
| 74 | 2 | ▶ | $self->[INFLATER] = Compress::Raw::Zlib::Inflate->new(%$inflate_options) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | or croak "Couldn't create inflater object"; | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 2 | return bless $self, $type; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 1 | ▶ | sub clone { | ||||||||||||||||||||||||||
|
Subroutine
clone: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 81 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 82 | 1 | my $deflate_options = $self->[DEFLATE_OPTIONS]; | |||||||||||||||||||||||||||
| 83 | 1 | my $deflater = Compress::Raw::Zlib::Deflate->new( %$deflate_options ); | |||||||||||||||||||||||||||
| 84 | 1 | my $inflate_options = $self->[INFLATE_OPTIONS]; | |||||||||||||||||||||||||||
| 85 | 1 | my $inflater = Compress::Raw::Zlib::Inflate->new( %$inflate_options ); | |||||||||||||||||||||||||||
| 86 | 1 | my $nself = [ | |||||||||||||||||||||||||||
| 87 | [], | ||||||||||||||||||||||||||||
| 88 | $deflater, | ||||||||||||||||||||||||||||
| 89 | $deflate_options, | ||||||||||||||||||||||||||||
| 90 | $inflater, | ||||||||||||||||||||||||||||
| 91 | $inflate_options, | ||||||||||||||||||||||||||||
| 92 | ]; | ||||||||||||||||||||||||||||
| 93 | 1 | return bless $nself, ref $self; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 17 | ▶ | sub _handle_get_data { | ||||||||||||||||||||||||||
|
Subroutine
_handle_get_data: called CC=6 SCAR=18.1
| |||||||||||||||||||||||||||||
| 97 | 17 | my ($self, $data) = @_; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 17 | ▶ | return unless $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 6 | my $out; | |||||||||||||||||||||||||||
| 102 | 6 | my $status = $self->[INFLATER]->inflate($data, $out); | |||||||||||||||||||||||||||
| 103 | 6 | ▶ | unless ($status == Z_OK or $status == Z_STREAM_END) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$status == Z_OK() or $status == Z_STREAM_END()
MC/DC0%0/2
$status == Z_OK() or $status == Z_STREAM_END()
$status == Z_OK()
$status == Z_STREAM_END()
Truth table66%2/3
A or B
A= $status == Z_OK()B= $status == Z_STREAM_END()
| |||||||||||||||||||||||||||||
| 104 | 0 | return Data::Transform::Meta::Error->new( | |||||||||||||||||||||||||||
| 105 | "Couldn\'t inflate buffer ($status)" | ||||||||||||||||||||||||||||
| 106 | ); | ||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | 6 | ▶ | if ($status == Z_STREAM_END) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 3 | $self->[INFLATER] = Compress::Raw::Zlib::Inflate->new(%{ $self->[INFLATE_OPTIONS]}); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | 6 | ▶ | return $out if $out; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 3 | return; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 3 | ▶ | sub _handle_put_meta { | ||||||||||||||||||||||||||
|
Subroutine
_handle_put_meta: called CC=7 SCAR=21.0
| |||||||||||||||||||||||||||||
| 116 | 3 | my ($self, $meta) = @_; | |||||||||||||||||||||||||||
| 117 | 3 | my ($type, @ret); | |||||||||||||||||||||||||||
| 118 | 3 | ▶ | $type = Z_FINISH if ($meta->isa('Data::Transform::Meta::EOF')); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 3 | ▶ | $type = Z_SYNC_FLUSH if (defined $meta->data and $meta->data eq 'sync'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $meta->data and $meta->data eq "sync"
MC/DC0%0/2
defined $meta->data and $meta->data eq "sync"
defined $meta->data
$meta->data eq "sync"
Truth table33%1/3
A and B
A= defined $meta->dataB= $meta->data eq "sync"
| |||||||||||||||||||||||||||||
| 120 | 3 | ▶ | if (defined $type) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 3 | my ($fout,$fstat); | |||||||||||||||||||||||||||
| 122 | 3 | $fstat = $self->[DEFLATER]->flush($fout, $type); | |||||||||||||||||||||||||||
| 123 | 3 | push (@ret, $fout); | |||||||||||||||||||||||||||
| 124 | 3 | ▶ | unless ( $fstat == Z_OK ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 0 | warn "Error flushing data ($fstat)"; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | 3 | ▶ | if ($type == Z_FINISH) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | $self->[DEFLATER] = Compress::Raw::Zlib::Deflate->new( | ||||||||||||||||||||||||||||
| 129 | 3 | %{$self->[DEFLATE_OPTIONS]} | |||||||||||||||||||||||||||
| 130 | ); | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | 3 | push @ret, $meta; | |||||||||||||||||||||||||||
| 134 | 3 | return @ret; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 5 | ▶ | sub _handle_put_data { | ||||||||||||||||||||||||||
|
Subroutine
_handle_put_data: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 138 | 5 | my ($self, $data) = @_; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 5 | my ($dstat, $dout); | |||||||||||||||||||||||||||
| 141 | 5 | $dstat = $self->[DEFLATER]->deflate($data, $dout); | |||||||||||||||||||||||||||
| 142 | 5 | ▶ | unless ($dstat == Z_OK) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 0 | warn "Error deflating data ($dstat)"; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | 5 | return $dout; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 1 | 1; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head1 METADATA | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | Due to how the zlib protocol works, it is important that you don't forget | ||||||||||||||||||||||||||||
| 153 | to close the stream by sending a L<Data::Transform::Meta::EOF> packet when | ||||||||||||||||||||||||||||
| 154 | you're writing data. Otherwise the filter might be holding back data while | ||||||||||||||||||||||||||||
| 155 | waiting to see if additional data may help compression. | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | In case you're using this in a request/response protocol like XMPP (with | ||||||||||||||||||||||||||||
| 158 | stream compression enabled), you will also have to send a | ||||||||||||||||||||||||||||
| 159 | L<Data::Transform::Meta> packet with the string "sync" as the content after | ||||||||||||||||||||||||||||
| 160 | each request/response. This makes the filter flush what is in its buffer, | ||||||||||||||||||||||||||||
| 161 | so you can be sure your request or response gets sent out. Otherwise, | ||||||||||||||||||||||||||||
| 162 | the filter might be waiting for more input to see whether it can compress | ||||||||||||||||||||||||||||
| 163 | the data even better, while you need the packet sent so you can get an | ||||||||||||||||||||||||||||
| 164 | answer from the remote side. | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | Data::Transform::Zlib was adapted from the POE::Filter::Zlib filter | ||||||||||||||||||||||||||||
| 169 | which was written by Chris Williams <chris@bingosnet.co.uk> | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | Martijn van Beers <martijn@cpan.org> did the adapting and maintains it. | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | Copyright C<(c)> Chris Williams and Martijn van Beers. | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | This program is free software; you can redistribute it and/or modify it under the terms of the GNU GPL, version 3.0 or higher. | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | L<Data::Transform> | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | L<Compress::Raw::Zlib> | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =cut | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||