| 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 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 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' ); | |||||||||||||||||||||||||||
| 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 | 2 | ▶ | use constant { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 2 | ENCODE_BUFFER_SIZE => ( 32 * 1024 ), | |||||||||||||||||||||||||||
| 26 | DECODE_BUFFER_SIZE => ( 32 * 1024 ), | ||||||||||||||||||||||||||||
| 27 | 2 | }; | |||||||||||||||||||||||||||
| 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 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
decode: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | 0 | my $self = shift( @_ ); | |||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $from_fh and defined $to_fh
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
| |||||||||||||||||||||||||||||
| 45 | 0 | my( $n, $buff ); | |||||||||||||||||||||||||||
| 46 | 0 | ▶ | $self->_load_class( 'Compress::LZW::Decompressor', { no_import => 1 } ) || return( $self->pass_error ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 0 | my $c = Compress::LZW::Decompressor->new; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | while( $n = $reader->( $buff, DECODE_BUFFER_SIZE ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 59 | { | ||||||||||||||||||||||||||||
| 60 | 0 | return( $self->error( "Error decompressing with LZW: $@" ) ); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 0 | ▶ | return( $self->pass_error ) if( !defined( $rv ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | 0 | ▶ | return( $self->pass_error ) if( !defined( $n ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | return( $self ); | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | sub decode_lzw | ||||||||||||||||||||||||||||
| 69 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
decode_lzw: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 70 | 0 | my $s = __PACKAGE__->new; | |||||||||||||||||||||||||||
| 71 | 0 | my $rv = $s->decode( @_ ); | |||||||||||||||||||||||||||
| 72 | 0 | ▶ | if( !defined( $rv ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
encode: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||||||||||
| 86 | 0 | my $self = shift( @_ ); | |||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $from_fh and defined $to_fh
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
| |||||||||||||||||||||||||||||
| 93 | 0 | my( $n, $buff ); | |||||||||||||||||||||||||||
| 94 | 0 | ▶ | $self->_load_class( 'Compress::LZW::Compressor', { no_import => 1 } ) || return( $self->pass_error ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | my $c = Compress::LZW::Compressor->new; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | while( $n = $reader->( $buff, ENCODE_BUFFER_SIZE ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 107 | { | ||||||||||||||||||||||||||||
| 108 | 0 | return( $self->error( "Error compressing with LZW: $@" ) ); | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | 0 | ▶ | return( $self->pass_error ) if( !defined( $rv ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | ▶ | return( $self->pass_error ) if( !defined( $n ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 0 | return( $self ); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | sub encode_lzw | ||||||||||||||||||||||||||||
| 118 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
encode_lzw: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 119 | 0 | my $s = __PACKAGE__->new; | |||||||||||||||||||||||||||
| 120 | 0 | my $rv = $s->encode( @_ ); | |||||||||||||||||||||||||||
| 121 | 0 | ▶ | if( !defined( $rv ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
is_decoder_installed: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 135 | 0 | local $@; | |||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | sub is_encoder_installed | ||||||||||||||||||||||||||||
| 145 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
is_encoder_installed: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 146 | 0 | local $@; | |||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||