| 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 | use strict; | ||
| 18 | use warnings; | ||
| 19 | warnings::register_categories( 'HTTP::Promise' ); | ||
| 20 | use HTTP::Promise::Stream; | ||
| 21 | use parent -norequire, qw( HTTP::Promise::Stream::Generic ); | ||
| 22 | use vars qw( @EXPORT_OK $VERSION $EXCEPTION_CLASS $LZWError ); | ||
| 23 | # use Nice::Try; | ||
| 24 | use constant { | ||
| 25 | ENCODE_BUFFER_SIZE => ( 32 * 1024 ), | ||
| 26 | DECODE_BUFFER_SIZE => ( 32 * 1024 ), | ||
| 27 | }; | ||
| 28 | our @EXPORT_OK = qw( decode_lzw encode_lzw ); | ||
| 29 | our $EXCEPTION_CLASS = 'HTTP::Promise::Exception'; | ||
| 30 | our $VERSION = 'v0.2.0'; | ||
| 31 | }; | ||
| 32 | |||
| 33 | use strict; | ||
| 34 | use warnings; | ||
| 35 | |||
| 36 | sub decode | ||
| 37 | { | ||
| 38 | my $self = shift( @_ ); | ||
| 39 | my $from = shift( @_ ); | ||
| 40 | my $to = shift( @_ ); | ||
| 41 | my $opts = $self->_get_args_as_hash( @_ ); | ||
| 42 | my( $from_fh, $reader ) = $self->_get_glob_from_arg( $from ); | ||
| 43 | my( $to_fh, $writer ) = $self->_get_glob_from_arg( $to, write => 1 ); | ||
| 44 | return( $self->pass_error ) if( !defined( $from_fh ) || !defined( $to_fh ) ); | ||
| 45 | my( $n, $buff ); | ||
| 46 | $self->_load_class( 'Compress::LZW::Decompressor', { no_import => 1 } ) || return( $self->pass_error ); | ||
| 47 | my $c = Compress::LZW::Decompressor->new; | ||
| 48 | |||
| 49 | while( $n = $reader->( $buff, DECODE_BUFFER_SIZE ) ) | ||
| 50 | { | ||
| 51 | my $decoded = $c->decompress( $buff ); | ||
| 52 | # try-catch | ||
| 53 | local $@; | ||
| 54 | my $rv = eval | ||
| 55 | { | ||
| 56 | $writer->( $decoded ); | ||
| 57 | }; | ||
| 58 | if( $@ ) | ||
| 59 | { | ||
| 60 | return( $self->error( "Error decompressing with LZW: $@" ) ); | ||
| 61 | } | ||
| 62 | return( $self->pass_error ) if( !defined( $rv ) ); | ||
| 63 | } | ||
| 64 | return( $self->pass_error ) if( !defined( $n ) ); | ||
| 65 | return( $self ); | ||
| 66 | } | ||
| 67 | |||
| 68 | sub decode_lzw | ||
| 69 | { | ||
| 70 | my $s = __PACKAGE__->new; | ||
| 71 | my $rv = $s->decode( @_ ); | ||
| 72 | if( !defined( $rv ) ) | ||
| 73 | { | ||
| 74 | $LZWError = $s->error; | ||
| 75 | return( $s->pass_error ); | ||
| 76 | } | ||
| 77 | else | ||
| 78 | { | ||
| 79 | undef( $LZWError ); | ||
| 80 | return( $rv ); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | sub encode | ||
| 85 | { | ||
| 86 | my $self = shift( @_ ); | ||
| 87 | my $from = shift( @_ ); | ||
| 88 | my $to = shift( @_ ); | ||
| 89 | my $opts = $self->_get_args_as_hash( @_ ); | ||
| 90 | my( $from_fh, $reader ) = $self->_get_glob_from_arg( $from ); | ||
| 91 | my( $to_fh, $writer ) = $self->_get_glob_from_arg( $to, write => 1 ); | ||
| 92 | return( $self->pass_error ) if( !defined( $from_fh ) || !defined( $to_fh ) ); | ||
| 93 | my( $n, $buff ); | ||
| 94 | $self->_load_class( 'Compress::LZW::Compressor', { no_import => 1 } ) || return( $self->pass_error ); | ||
| 95 | my $c = Compress::LZW::Compressor->new; | ||
| 96 | |||
| 97 | while( $n = $reader->( $buff, ENCODE_BUFFER_SIZE ) ) | ||
| 98 | { | ||
| 99 | my $encoded = $c->compress( $buff ); | ||
| 100 | # try-catch | ||
| 101 | local $@; | ||
| 102 | my $rv = eval | ||
| 103 | { | ||
| 104 | $writer->( $encoded ); | ||
| 105 | }; | ||
| 106 | if( $@ ) | ||
| 107 | { | ||
| 108 | return( $self->error( "Error compressing with LZW: $@" ) ); | ||
| 109 | } | ||
| 110 | return( $self->pass_error ) if( !defined( $rv ) ); | ||
| 111 | } | ||
| 112 | |||
| 113 | return( $self->pass_error ) if( !defined( $n ) ); | ||
| 114 | return( $self ); | ||
| 115 | } | ||
| 116 | |||
| 117 | sub encode_lzw | ||
| 118 | { | ||
| 119 | my $s = __PACKAGE__->new; | ||
| 120 | my $rv = $s->encode( @_ ); | ||
| 121 | if( !defined( $rv ) ) | ||
| 122 | { | ||
| 123 | $LZWError = $s->error; | ||
| 124 | return( $s->pass_error ); | ||
| 125 | } | ||
| 126 | else | ||
| 127 | { | ||
| 128 | undef( $LZWError ); | ||
| 129 | return( $rv ); | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | sub is_decoder_installed | ||
| 134 | { | ||
| 135 | local $@; | ||
| 136 | eval | ||
| 137 | { | ||
| 138 | local $SIG{__DIE__} = sub{}; | ||
| 139 | require Compress::LZW::Decompressor; | ||
| 140 | }; | ||
| 141 | return( $@ ? 0 : 1 ); | ||
| 142 | } | ||
| 143 | |||
| 144 | sub is_encoder_installed | ||
| 145 | { | ||
| 146 | local $@; | ||
| 147 | eval | ||
| 148 | { | ||
| 149 | local $SIG{__DIE__} = sub{}; | ||
| 150 | require Compress::LZW::Compressor; | ||
| 151 | }; | ||
| 152 | return( $@ ? 0 : 1 ); | ||
| 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 | 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 |