Skip to content

Commit

Permalink
Changes for zlib-ng
Browse files Browse the repository at this point in the history
  • Loading branch information
pmqs committed Jun 23, 2022
1 parent ede5537 commit 2bd52d2
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 87 deletions.
23 changes: 23 additions & 0 deletions lib/IO/Compress/Adapter/Deflate.pm
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,29 @@ sub mkCompObject
} ;
}

sub mkCompObject1
{
my $crc32 = shift ;
my $adler32 = shift ;
my $level = shift ;
my $strategy = shift ;

my ($def, $status) = Compress::Raw::Zlib::Deflate->new(
-AppendOutput => 1,
-CRC32 => $crc32,
-ADLER32 => $adler32,
-Level => $level,
-Strategy => $strategy,
-WindowBits => MAX_WBITS);

return (undef, "Cannot create Deflate object: $status", $status)
if $status != Z_OK;

return bless {'Def' => $def,
'Error' => '',
} ;
}

sub compr
{
my $self = shift ;
Expand Down
93 changes: 15 additions & 78 deletions lib/IO/Compress/Deflate.pm
Original file line number Diff line number Diff line change
Expand Up @@ -42,105 +42,42 @@ sub deflate
return $obj->_def(@_);
}


sub bitmask($$$$)
{
my $into = shift ;
my $value = shift ;
my $offset = shift ;
my $mask = shift ;

return $into | (($value & $mask) << $offset ) ;
}

sub mkDeflateHdr($$$;$)
{
my $method = shift ;
my $cinfo = shift;
my $level = shift;
my $fdict_adler = shift ;

my $cmf = 0;
my $flg = 0;
my $fdict = 0;
$fdict = 1 if defined $fdict_adler;

$cmf = bitmask($cmf, $method, ZLIB_CMF_CM_OFFSET, ZLIB_CMF_CM_BITS);
$cmf = bitmask($cmf, $cinfo, ZLIB_CMF_CINFO_OFFSET, ZLIB_CMF_CINFO_BITS);

$flg = bitmask($flg, $fdict, ZLIB_FLG_FDICT_OFFSET, ZLIB_FLG_FDICT_BITS);
$flg = bitmask($flg, $level, ZLIB_FLG_LEVEL_OFFSET, ZLIB_FLG_LEVEL_BITS);

my $fcheck = 31 - ($cmf * 256 + $flg) % 31 ;
$flg = bitmask($flg, $fcheck, ZLIB_FLG_FCHECK_OFFSET, ZLIB_FLG_FCHECK_BITS);

my $hdr = pack("CC", $cmf, $flg) ;
$hdr .= pack("N", $fdict_adler) if $fdict ;

return $hdr;
}

sub mkHeader
sub mkComp
{
my $self = shift ;
my $param = shift ;
my $got = shift ;

my $level = $param->getValue('level');
my $strategy = $param->getValue('strategy');
my ($obj, $errstr, $errno) = IO::Compress::Adapter::Deflate::mkCompObject1(
$got->getValue('crc32'),
$got->getValue('adler32'),
$got->getValue('level'),
$got->getValue('strategy')
);

my $lflag ;
$level = 6
if $level == Z_DEFAULT_COMPRESSION ;
return $self->saveErrorString(undef, $errstr, $errno)
if ! defined $obj;

if (ZLIB_VERNUM >= 0x1210)
{
if ($strategy >= Z_HUFFMAN_ONLY || $level < 2)
{ $lflag = ZLIB_FLG_LEVEL_FASTEST }
elsif ($level < 6)
{ $lflag = ZLIB_FLG_LEVEL_FAST }
elsif ($level == 6)
{ $lflag = ZLIB_FLG_LEVEL_DEFAULT }
else
{ $lflag = ZLIB_FLG_LEVEL_SLOWEST }
}
else
{
$lflag = ($level - 1) >> 1 ;
$lflag = 3 if $lflag > 3 ;
}

#my $wbits = (MAX_WBITS - 8) << 4 ;
my $wbits = 7;
mkDeflateHdr(ZLIB_CMF_CM_DEFLATED, $wbits, $lflag);
return $obj;
}

sub ckParams

sub mkHeader
{
my $self = shift ;
my $got = shift;

$got->setValue('adler32' => 1);
return 1 ;
return '';
}


sub mkTrailer
{
my $self = shift ;
return pack("N", *$self->{Compress}->adler32()) ;
return '';
}

sub mkFinalTrailer
{
return '';
}

#sub newHeader
#{
# my $self = shift ;
# return *$self->{Header};
#}

sub getExtraParams
{
my $self = shift ;
Expand Down
4 changes: 2 additions & 2 deletions t/000prereq.t
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ BEGIN
}
}

# need zlib 1.2.0 or better
# need zlib 1.2.0 or better or zlib-ng

cmp_ok Compress::Raw::Zlib::ZLIB_VERNUM(), ">=", 0x1200
ok Compress::Raw::Zlib::is_zlibng() || Compress::Raw::Zlib::ZLIB_VERNUM() >= 0x1200
or diag "IO::Compress needs zlib 1.2.0 or better, you have " . Compress::Raw::Zlib::zlib_version();

use_ok('Scalar::Util') ;
Expand Down
7 changes: 4 additions & 3 deletions t/005defhdr.t
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,15 @@ EOM

my $hdr1 = ReadHeaderInfoZlib($string, %$opts);

# zlib-ng with Level 1 sets the CINFO value to 5 for some reason. All others use expected value of 7
my $cinfoValue = Compress::Raw::Zlib::is_zlibng() && defined $opts->{'-Level'} && $opts->{'-Level'} == 1 ? 5 : 7;
is $hdr->{CM}, 8, " CM is 8";
is $hdr->{CINFO}, 7, " CINFO is 7";
is $hdr->{CINFO}, $cinfoValue, " CINFO is $cinfoValue";
is $hdr->{FDICT}, 0, " FDICT is 0";

while (my ($k, $v) = each %$expect)
{
if (ZLIB_VERNUM >= 0x1220)
if (Compress::Raw::Zlib::is_zlibng() || ZLIB_VERNUM >= 0x1220)
{ is $hdr->{$k}, $v, " $k is $v" }
else
{ ok 1, " Skip test for $k" }
Expand Down Expand Up @@ -357,4 +359,3 @@ EOM
ok $gunz->close ;
}
}

6 changes: 4 additions & 2 deletions t/101truncate-rawdeflate.t
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use warnings;

use Test::More ;

use Compress::Raw::Zlib;

BEGIN {
plan skip_all => "Lengthy Tests Disabled\n" .
"set COMPRESS_ZLIB_RUN_ALL or COMPRESS_ZLIB_RUN_MOST to run this test suite"
Expand All @@ -21,8 +23,8 @@ BEGIN {
$extra = 1
if eval { require Test::NoWarnings ; import Test::NoWarnings; 1 };

plan tests => 625 + $extra;

my $tests = Compress::Raw::Zlib::is_zlibng() ? 615 : 625;
plan tests => $tests + $extra;
};


Expand Down
4 changes: 3 additions & 1 deletion t/111const-deflate.t
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ BEGIN {
$extra = 1
if eval { require Test::NoWarnings ; import Test::NoWarnings; 1 };

plan tests => 355 + $extra ;
plan tests => 390 + $extra ;
}


Expand All @@ -30,6 +30,8 @@ BEGIN {
my %all;
for my $symbol (@Compress::Raw::Zlib::DEFLATE_CONSTANTS)
{
next if $symbol eq 'Z_NULL';

eval "defined Compress::Raw::Zlib::$symbol" ;
$all{$symbol} = ! $@ ;
}
Expand Down
3 changes: 2 additions & 1 deletion t/cz-03zlib-v1.t
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,8 @@ EOM
($GOT, $status) = $k->inflate($rest) ;

# Z_STREAM_END returned by 1.12.2, Z_DATA_ERROR for older zlib
if (ZLIB_VERNUM >= ZLIB_1_2_12_0)
# always Z_STREAM_ENDin zlib_ng
if (ZLIB_VERNUM >= ZLIB_1_2_12_0 || Compress::Raw::Zlib::is_zlibng)
{
cmp_ok $status, '==', Z_STREAM_END ;
}
Expand Down

0 comments on commit 2bd52d2

Please sign in to comment.