lib/HTTP/Promise/Stream/LZW.pm

filter: Statementstmt 31.1% 29 / 93 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 52.9% 9 / 17 Podpod 100% 6 / 6 totaltotal 27.8% 44 / 158 CC 19 SCAR 35.5
CC
19
Cov
22%
decode
72.0
encode
72.0
decode_lzw
6.0
CRAP 34.7
« lib/HTTP/Promise/Body/Form.pm ↑ Summary lib/HTTP/Promise/Stream/Brotli.pm »
1 ##----------------------------------------------------------------------------
2 ## Asynchronous HTTP Request and Promise - ~/lib/HTTP/Promise/Stream/LZW.pm
3 ## Version v0.2.0
4 ## Copyright(c) 2022 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2022/05/04
7 ## Modified 2023/09/08
8 ## All rights reserved.
9 ##
10 ##
11 ## This program is free software; you can redistribute it and/or modify it
12 ## under the same terms as Perl itself.
13 ##----------------------------------------------------------------------------
14 package HTTP::Promise::Stream::LZW;
15 BEGIN
16 {
17 2 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶ warnings::register_categories( 'HTTP::Promise' );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶ use HTTP::Promise::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶ use parent -norequire, qw( HTTP::Promise::Stream::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶ use vars qw( @EXPORT_OK $VERSION $EXCEPTION_CLASS $LZWError );
Subroutine BEGIN: called CC=1 SCAR=0.0
23 # use Nice::Try;
24 use constant {
25 2 ENCODE_BUFFER_SIZE => ( 32 * 1024 ),
26 DECODE_BUFFER_SIZE => ( 32 * 1024 ),
27 2 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 our @EXPORT_OK = qw( decode_lzw encode_lzw );
29 2 our $EXCEPTION_CLASS = 'HTTP::Promise::Exception';
30 2 our $VERSION = 'v0.2.0';
31 };
32  
33 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 sub decode
37 {
38 0 ▶ my $self = shift( @_ );
Subroutine decode: not called CC=8 SCAR=42.8
Pod documented
39 0 my $from = shift( @_ );
40 0 my $to = shift( @_ );
41 0 my $opts = $self->_get_args_as_hash( @_ );
42 0 my( $from_fh, $reader ) = $self->_get_glob_from_arg( $from );
43 0 my( $to_fh, $writer ) = $self->_get_glob_from_arg( $to, write => 1 );
44 0 ▶ return( $self->pass_error ) if( !defined( $from_fh ) || !defined( $to_fh ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from_fh and defined $to_fh 0 0 0
Condition0%0/3
defined $from_fh and defined $to_fh
!ll&&!rl&&r
000
MC/DC0%0/2
defined $from_fh and defined $to_fh
defined $from_fh defined $to_fh
Truth table0%0/3
A and B
A= defined $from_fhB= defined $to_fh
ABresult
0X0
100
111
45 0 my( $n, $buff );
46 0 ▶ $self->_load_class( 'Compress::LZW::Decompressor', { no_import => 1 } ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("Compress::LZW::Decompressor", {"no_import", 1}) or return $self->pass_error 0 0 0
47 0 my $c = Compress::LZW::Decompressor->new;
48
49 0 ▶ while( $n = $reader->( $buff, DECODE_BUFFER_SIZE ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($n = &$reader($buff, 32768)) 0 0 0
50 {
51 0 my $decoded = $c->decompress( $buff );
52 # try-catch
53 0 local $@;
54 my $rv = eval
55 0 {
56 0 $writer->( $decoded );
57 };
58 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
59 {
60 0 return( $self->error( "Error decompressing with LZW: $@" ) );
61 }
62 0 ▶ return( $self->pass_error ) if( !defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 0 0
63 }
64 0 ▶ return( $self->pass_error ) if( !defined( $n ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 0 0
65 0 return( $self );
66 }
67  
68 sub decode_lzw
69 {
70 0 ▶ my $s = __PACKAGE__->new;
Subroutine decode_lzw: not called CC=2 SCAR=17.9
Pod documented
71 0 my $rv = $s->decode( @_ );
72 0 ▶ if( !defined( $rv ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rv) { } 0 0 0
73 {
74 0 $LZWError = $s->error;
75 0 return( $s->pass_error );
76 }
77 else
78 {
79 0 undef( $LZWError );
80 0 return( $rv );
81 }
82 }
83  
84 sub encode
85 {
86 0 ▶ my $self = shift( @_ );
Subroutine encode: not called CC=8 SCAR=42.8
Pod documented
87 0 my $from = shift( @_ );
88 0 my $to = shift( @_ );
89 0 my $opts = $self->_get_args_as_hash( @_ );
90 0 my( $from_fh, $reader ) = $self->_get_glob_from_arg( $from );
91 0 my( $to_fh, $writer ) = $self->_get_glob_from_arg( $to, write => 1 );
92 0 ▶ return( $self->pass_error ) if( !defined( $from_fh ) || !defined( $to_fh ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from_fh and defined $to_fh 0 0 0
Condition0%0/3
defined $from_fh and defined $to_fh
!ll&&!rl&&r
000
MC/DC0%0/2
defined $from_fh and defined $to_fh
defined $from_fh defined $to_fh
Truth table0%0/3
A and B
A= defined $from_fhB= defined $to_fh
ABresult
0X0
100
111
93 0 my( $n, $buff );
94 0 ▶ $self->_load_class( 'Compress::LZW::Compressor', { no_import => 1 } ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("Compress::LZW::Compressor", {"no_import", 1}) or return $self->pass_error 0 0 0
95 0 my $c = Compress::LZW::Compressor->new;
96
97 0 ▶ while( $n = $reader->( $buff, ENCODE_BUFFER_SIZE ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($n = &$reader($buff, 32768)) 0 0 0
98 {
99 0 my $encoded = $c->compress( $buff );
100 # try-catch
101 0 local $@;
102 my $rv = eval
103 0 {
104 0 $writer->( $encoded );
105 };
106 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
107 {
108 0 return( $self->error( "Error compressing with LZW: $@" ) );
109 }
110 0 ▶ return( $self->pass_error ) if( !defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 0 0
111 }
112  
113 0 ▶ return( $self->pass_error ) if( !defined( $n ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 0 0
114 0 return( $self );
115 }
116  
117 sub encode_lzw
118 {
119 0 ▶ my $s = __PACKAGE__->new;
Subroutine encode_lzw: not called CC=2 SCAR=17.9
Pod documented
120 0 my $rv = $s->encode( @_ );
121 0 ▶ if( !defined( $rv ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rv) { } 0 0 0
122 {
123 0 $LZWError = $s->error;
124 0 return( $s->pass_error );
125 }
126 else
127 {
128 0 undef( $LZWError );
129 0 return( $rv );
130 }
131 }
132  
133 sub is_decoder_installed
134 {
135 0 ▶ local $@;
Subroutine is_decoder_installed: not called CC=2 SCAR=17.9
Pod documented
136 eval
137 0 {
138 0 ▶ local $SIG{__DIE__} = sub{};
Subroutine __ANON__: not called CC=1 SCAR=6.9
139 0 require Compress::LZW::Decompressor;
140 };
141 0 ▶ return( $@ ? 0 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
142 }
143  
144 sub is_encoder_installed
145 {
146 0 ▶ local $@;
Subroutine is_encoder_installed: not called CC=2 SCAR=17.9
Pod documented
147 eval
148 0 {
149 0 ▶ local $SIG{__DIE__} = sub{};
Subroutine __ANON__: not called CC=1 SCAR=6.9
150 0 require Compress::LZW::Compressor;
151 };
152 0 ▶ return( $@ ? 0 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
153 }
154  
155 # NOTE: sub FREEZE is inherited
156  
157 # NOTE: sub STORABLE_freeze is inherited
158  
159 # NOTE: sub STORABLE_thaw is inherited
160  
161 # NOTE: sub THAW is inherited
162  
163 2 1;
164 # NOTE: POD
165 __END__
166  
167 =encoding utf-8
168  
169 =head1 NAME
170  
171 HTTP::Promise::Stream::LZW - Stream Encoder for LZW Compression
172  
173 =head1 SYNOPSIS
174  
175 use HTTP::Promise::Stream::LZW;
176 my $s = HTTP::Promise::Stream::LZW->new ||
177 die( HTTP::Promise::Stream::LZW->error, "\n" );
178 $s->encode( $input => $output ) ||
179 die( $s->error );
180 $s->decode( $input => $output ) || die( $s->error );
181 HTTP::Promise::Stream::LZW::encode_lzw( $input => $output ) ||
182 die( $HTTP::Promise::Stream::LZW::LZWError );
183 HTTP::Promise::Stream::LZW::decode_lzw( $input => $output ) ||
184 die( $HTTP::Promise::Stream::LZW::LZWError );
185  
186 =head1 VERSION
187  
188 v0.2.0
189  
190 =head1 DESCRIPTION
191  
192 This implements an encoding and decoding mechanism for LZW compression using either of the following on input and output:
193  
194 =over 4
195  
196 =item C<filepath>
197  
198 If the parameter is neither a scalar reference nor a file handle, it will be assumed to be a file path.
199  
200 =item C<file handle>
201  
202 This can be a native file handle, or an object oriented one as long as it implements the C<print> or C<write>, and C<read> methods. The C<read> method is expected to return the number of bytes read or C<undef> upon error. The C<print> and C<write> methods are expected to simply return true upon success and C<undef> upon error.
203  
204 Alternatively, those methods can die and those exceptions wil be caught.
205  
206 =item C<scalar reference>
207  
208 This can be a simple scalar reference, or an object scalar reference.
209  
210 =back
211  
212 This module requires L<Compress::LZW> to be installed or it will return an error.
213  
214 =head1 CONSTRUCTOR
215  
216 =head2 new
217  
218 Creates a new L<HTTP::Promise::Stream::LZW> object and returns it.
219  
220 =head1 METHODS
221  
222 =head2 decode
223  
224 This takes 2 arguments: an input and an output. Each one can be either a file path, a file handle, or a scalar reference.
225  
226 It will decode the LZW encoded data and write the result into the output.
227  
228 It returns true upon success and sets an L<error|Module::Generic/error> and return C<undef> upon error.
229  
230 =head2 encode
231  
232 This takes 2 arguments: an input and an output. Each one can be either a file path, a file handle, or a scalar reference.
233  
234 It will encode the data into LZW encoded data and write the result into the output.
235  
236 It returns true upon success and sets an L<error|Module::Generic/error> and return C<undef> upon error.
237  
238 =head1 CLASS FUNCTIONS
239  
240 The following class functions are available and can also be exported, such as:
241  
242 use HTTP::Promise::Stream::Brotli qw( decode_lzw encode_lzw );
243  
244 =head2 decode_lzw
245  
246 This takes the same 2 arguments used in L</decode>: an input and an output. Each one can be either a file path, a file handle, or a scalar reference.
247  
248 It will decode the LZW encoded data and write the result into the output.
249  
250 It returns true upon success, and upon error, it will set the error in the global variable C<$UUError> and return C<undef>
251  
252 my $decoded = HTTP::Promise::Stream::LZW::decode_lzw( $encoded );
253 die( "Something went wrong: $HTTP::Promise::Stream::LZW::LZWError\n" if( !defined( $decoded ) );
254 print( "Decoded data is: $decoded\n" );
255  
256 =head2 encode_lzw
257  
258 This takes the same 2 arguments used in L</encode>: an input and an output. Each one can be either a file path, a file handle, or a scalar reference.
259  
260 It will encode the data into LZW encoded data and write the result into the output.
261  
262 It returns true upon success, and upon error, it will set the error in the global variable C<$LZWError> and return C<undef>
263  
264 my $encoded = HTTP::Promise::Stream::LZW::encode_lzw( $data );
265 die( "Something went wrong: $HTTP::Promise::Stream::LZW::LZWError\n" if( !defined( $encoded ) );
266 print( "Encoded data is: $encoded\n" );
267  
268 =head2 is_decoder_installed
269  
270 Returns true if the module L<Compress::LZW::Decompressor> is installed, false otherwise.
271  
272 =head2 is_encoder_installed
273  
274 Returns true if the module L<Compress::LZW::Compressor> is installed, false otherwise.
275  
276 =head1 THREAD-SAFETY
277  
278 This module is thread-safe for all operations, as it operates on per-object state and uses thread-safe external libraries.
279  
280 =head1 AUTHOR
281  
282 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
283  
284 =head1 SEE ALSO
285  
286 L<Compress::LZW>
287  
288 L<Discussion on Stackoverflow|http://web.archive.org/web/20170310213520/https://stackoverflow.com/questions/3855204/looking-for-library-which-implements-lzw-compression-decompression>, L<Wikipedia page|https://fr.wikipedia.org/wiki/Lempel-Ziv-Welch>
289  
290 L<HTTP::Promise>, L<HTTP::Promise::Request>, L<HTTP::Promise::Response>, L<HTTP::Promise::Message>, L<HTTP::Promise::Entity>, L<HTTP::Promise::Headers>, L<HTTP::Promise::Body>, L<HTTP::Promise::Body::Form>, L<HTTP::Promise::Body::Form::Data>, L<HTTP::Promise::Body::Form::Field>, L<HTTP::Promise::Status>, L<HTTP::Promise::MIME>, L<HTTP::Promise::Parser>, L<HTTP::Promise::IO>, L<HTTP::Promise::Stream>, L<HTTP::Promise::Exception>
291  
292 =head1 COPYRIGHT & LICENSE
293  
294 Copyright(c) 2022 DEGUEST Pte. Ltd.
295  
296 All rights reserved.
297  
298 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
299  
300 =cut
« lib/HTTP/Promise/Body/Form.pm ↑ Summary lib/HTTP/Promise/Stream/Brotli.pm »