| 1 | package Skilletor::Gzip; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: gunzip and inflate as Node's zlib binding does them, with zlib's messages | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 35 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 35 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 35 | ▶ | use Compress::Raw::Zlib (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 35 | ▶ | use Exporter qw( import ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 35 | ▶ | use Skilletor::Error::Source; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 35 | our $VERSION = '0.001'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 35 | our @EXPORT_OK = qw( gunzip_sync inflate_sync ); | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | sub _fail { Skilletor::Error::Source->throw( $_[0] ) } | ||||||||||||||||||||||||||
|
Subroutine
_fail: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | # One zlib stream over all of $input. $gunzip: after a member's end, a 0x00 byte ends the | ||||||||||||||||||||||||||||
| 18 | # input (padding); any other byte starts the next member – as node_zlib.cc does. | ||||||||||||||||||||||||||||
| 19 | 14 | ▶ | sub _run { | ||||||||||||||||||||||||||
|
Subroutine
_run: called CC=10 SCAR=28.7
| |||||||||||||||||||||||||||||
| 20 | 14 | my ( $input, $window_bits, $gunzip ) = @_; | |||||||||||||||||||||||||||
| 21 | 14 | my ( $z, $status ) = Compress::Raw::Zlib::Inflate->new( | |||||||||||||||||||||||||||
| 22 | -WindowBits => $window_bits, | ||||||||||||||||||||||||||||
| 23 | -ConsumeInput => 1, | ||||||||||||||||||||||||||||
| 24 | -LimitOutput => 0, | ||||||||||||||||||||||||||||
| 25 | -Bufsize => 65536, | ||||||||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||||||||
| 27 | 14 | ▶ | _fail( 'zlib: '.$status ) unless $z; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 14 | my $out = ''; | |||||||||||||||||||||||||||
| 29 | 14 | my $in = $input; | |||||||||||||||||||||||||||
| 30 | 14 | while (1) { | |||||||||||||||||||||||||||
| 31 | 14 | my $chunk = ''; | |||||||||||||||||||||||||||
| 32 | 14 | my $st = $z->inflate( $in, $chunk ); | |||||||||||||||||||||||||||
| 33 | 14 | $out .= $chunk; | |||||||||||||||||||||||||||
| 34 | 14 | ▶ | if ( $st == Compress::Raw::Zlib::Z_STREAM_END() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 13 | ▶ | last unless $gunzip && length $in; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$gunzip and length $in
MC/DC0%0/2
$gunzip and length $in
$gunzip
length $in
Truth table33%1/3
A and B
A= $gunzipB= length $in
| |||||||||||||||||||||||||||||
| 36 | 0 | ▶ | last if substr( $in, 0, 1 ) eq "\0"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 0 | $z->inflateReset; | |||||||||||||||||||||||||||
| 38 | 0 | next; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | 1 | ▶ | if ( $st == Compress::Raw::Zlib::Z_OK() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 0 | ▶ | next if length $in; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 0 | _fail('unexpected end of file'); | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | 1 | ▶ | if ( $st == Compress::Raw::Zlib::Z_BUF_ERROR() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 0 | _fail('unexpected end of file'); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | 1 | ▶ | _fail( $z->msg // "$st" ); | ||||||||||||||||||||||||||
|
Condition50%1/2
$z->msg // "$st"
MC/DC0%0/1
$z->msg // "$st"
$z->msg
Truth table50%1/2
A
A= $z->msg
| |||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | 13 | return $out; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 14 | ▶ | sub gunzip_sync { | ||||||||||||||||||||||||||
|
Subroutine
gunzip_sync: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | 14 | my ( $bytes ) = @_; | |||||||||||||||||||||||||||
| 54 | 14 | return _run( $bytes, Compress::Raw::Zlib::WANT_GZIP(), 1 ); | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # Content-Encoding: deflate – zlib-wrapped when the first byte says so, else raw (undici). | ||||||||||||||||||||||||||||
| 58 | 0 | ▶ | sub inflate_sync { | ||||||||||||||||||||||||||
|
Subroutine
inflate_sync: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | 0 | my ( $bytes ) = @_; | |||||||||||||||||||||||||||
| 60 | 0 | ▶ | my $zlib = length $bytes && ( ord( substr $bytes, 0, 1 ) & 0x0F ) == 0x08; | ||||||||||||||||||||||||||
|
Condition0%0/3
length $bytes && (ord substr($bytes, 0, 1) & 15) == 8
MC/DC0%0/2
length $bytes && (ord substr($bytes, 0, 1) & 15) == 8
length $bytes
(ord substr($bytes, 0, 1) & 15) == 8
Truth table0%0/3
A && B
A= length $bytesB= (ord substr($bytes, 0, 1) & 15) == 8
| |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | return _run( $bytes, $zlib ? Compress::Raw::Zlib::MAX_WBITS() : -Compress::Raw::Zlib::MAX_WBITS(), 0 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 35 | 1; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | __END__ | ||||||||||||||||||||||||||||