Skip to content

Commit 2bd52d2

Browse files
committed
Changes for zlib-ng
1 parent ede5537 commit 2bd52d2

File tree

7 files changed

+53
-87
lines changed

7 files changed

+53
-87
lines changed

lib/IO/Compress/Adapter/Deflate.pm

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,29 @@ sub mkCompObject
4040
} ;
4141
}
4242

43+
sub mkCompObject1
44+
{
45+
my $crc32 = shift ;
46+
my $adler32 = shift ;
47+
my $level = shift ;
48+
my $strategy = shift ;
49+
50+
my ($def, $status) = Compress::Raw::Zlib::Deflate->new(
51+
-AppendOutput => 1,
52+
-CRC32 => $crc32,
53+
-ADLER32 => $adler32,
54+
-Level => $level,
55+
-Strategy => $strategy,
56+
-WindowBits => MAX_WBITS);
57+
58+
return (undef, "Cannot create Deflate object: $status", $status)
59+
if $status != Z_OK;
60+
61+
return bless {'Def' => $def,
62+
'Error' => '',
63+
} ;
64+
}
65+
4366
sub compr
4467
{
4568
my $self = shift ;

lib/IO/Compress/Deflate.pm

Lines changed: 15 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -42,105 +42,42 @@ sub deflate
4242
return $obj->_def(@_);
4343
}
4444

45-
46-
sub bitmask($$$$)
47-
{
48-
my $into = shift ;
49-
my $value = shift ;
50-
my $offset = shift ;
51-
my $mask = shift ;
52-
53-
return $into | (($value & $mask) << $offset ) ;
54-
}
55-
56-
sub mkDeflateHdr($$$;$)
57-
{
58-
my $method = shift ;
59-
my $cinfo = shift;
60-
my $level = shift;
61-
my $fdict_adler = shift ;
62-
63-
my $cmf = 0;
64-
my $flg = 0;
65-
my $fdict = 0;
66-
$fdict = 1 if defined $fdict_adler;
67-
68-
$cmf = bitmask($cmf, $method, ZLIB_CMF_CM_OFFSET, ZLIB_CMF_CM_BITS);
69-
$cmf = bitmask($cmf, $cinfo, ZLIB_CMF_CINFO_OFFSET, ZLIB_CMF_CINFO_BITS);
70-
71-
$flg = bitmask($flg, $fdict, ZLIB_FLG_FDICT_OFFSET, ZLIB_FLG_FDICT_BITS);
72-
$flg = bitmask($flg, $level, ZLIB_FLG_LEVEL_OFFSET, ZLIB_FLG_LEVEL_BITS);
73-
74-
my $fcheck = 31 - ($cmf * 256 + $flg) % 31 ;
75-
$flg = bitmask($flg, $fcheck, ZLIB_FLG_FCHECK_OFFSET, ZLIB_FLG_FCHECK_BITS);
76-
77-
my $hdr = pack("CC", $cmf, $flg) ;
78-
$hdr .= pack("N", $fdict_adler) if $fdict ;
79-
80-
return $hdr;
81-
}
82-
83-
sub mkHeader
45+
sub mkComp
8446
{
8547
my $self = shift ;
86-
my $param = shift ;
48+
my $got = shift ;
8749

88-
my $level = $param->getValue('level');
89-
my $strategy = $param->getValue('strategy');
50+
my ($obj, $errstr, $errno) = IO::Compress::Adapter::Deflate::mkCompObject1(
51+
$got->getValue('crc32'),
52+
$got->getValue('adler32'),
53+
$got->getValue('level'),
54+
$got->getValue('strategy')
55+
);
9056

91-
my $lflag ;
92-
$level = 6
93-
if $level == Z_DEFAULT_COMPRESSION ;
57+
return $self->saveErrorString(undef, $errstr, $errno)
58+
if ! defined $obj;
9459

95-
if (ZLIB_VERNUM >= 0x1210)
96-
{
97-
if ($strategy >= Z_HUFFMAN_ONLY || $level < 2)
98-
{ $lflag = ZLIB_FLG_LEVEL_FASTEST }
99-
elsif ($level < 6)
100-
{ $lflag = ZLIB_FLG_LEVEL_FAST }
101-
elsif ($level == 6)
102-
{ $lflag = ZLIB_FLG_LEVEL_DEFAULT }
103-
else
104-
{ $lflag = ZLIB_FLG_LEVEL_SLOWEST }
105-
}
106-
else
107-
{
108-
$lflag = ($level - 1) >> 1 ;
109-
$lflag = 3 if $lflag > 3 ;
110-
}
111-
112-
#my $wbits = (MAX_WBITS - 8) << 4 ;
113-
my $wbits = 7;
114-
mkDeflateHdr(ZLIB_CMF_CM_DEFLATED, $wbits, $lflag);
60+
return $obj;
11561
}
11662

117-
sub ckParams
63+
64+
sub mkHeader
11865
{
11966
my $self = shift ;
120-
my $got = shift;
121-
122-
$got->setValue('adler32' => 1);
123-
return 1 ;
67+
return '';
12468
}
12569

126-
12770
sub mkTrailer
12871
{
12972
my $self = shift ;
130-
return pack("N", *$self->{Compress}->adler32()) ;
73+
return '';
13174
}
13275

13376
sub mkFinalTrailer
13477
{
13578
return '';
13679
}
13780

138-
#sub newHeader
139-
#{
140-
# my $self = shift ;
141-
# return *$self->{Header};
142-
#}
143-
14481
sub getExtraParams
14582
{
14683
my $self = shift ;

t/000prereq.t

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ BEGIN
8686
}
8787
}
8888

89-
# need zlib 1.2.0 or better
89+
# need zlib 1.2.0 or better or zlib-ng
9090

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

9494
use_ok('Scalar::Util') ;

t/005defhdr.t

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,15 @@ EOM
168168

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

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

175177
while (my ($k, $v) = each %$expect)
176178
{
177-
if (ZLIB_VERNUM >= 0x1220)
179+
if (Compress::Raw::Zlib::is_zlibng() || ZLIB_VERNUM >= 0x1220)
178180
{ is $hdr->{$k}, $v, " $k is $v" }
179181
else
180182
{ ok 1, " Skip test for $k" }
@@ -357,4 +359,3 @@ EOM
357359
ok $gunz->close ;
358360
}
359361
}
360-

t/101truncate-rawdeflate.t

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use warnings;
1111

1212
use Test::More ;
1313

14+
use Compress::Raw::Zlib;
15+
1416
BEGIN {
1517
plan skip_all => "Lengthy Tests Disabled\n" .
1618
"set COMPRESS_ZLIB_RUN_ALL or COMPRESS_ZLIB_RUN_MOST to run this test suite"
@@ -21,8 +23,8 @@ BEGIN {
2123
$extra = 1
2224
if eval { require Test::NoWarnings ; import Test::NoWarnings; 1 };
2325

24-
plan tests => 625 + $extra;
25-
26+
my $tests = Compress::Raw::Zlib::is_zlibng() ? 615 : 625;
27+
plan tests => $tests + $extra;
2628
};
2729

2830

t/111const-deflate.t

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ BEGIN {
2020
$extra = 1
2121
if eval { require Test::NoWarnings ; import Test::NoWarnings; 1 };
2222

23-
plan tests => 355 + $extra ;
23+
plan tests => 390 + $extra ;
2424
}
2525

2626

@@ -30,6 +30,8 @@ BEGIN {
3030
my %all;
3131
for my $symbol (@Compress::Raw::Zlib::DEFLATE_CONSTANTS)
3232
{
33+
next if $symbol eq 'Z_NULL';
34+
3335
eval "defined Compress::Raw::Zlib::$symbol" ;
3436
$all{$symbol} = ! $@ ;
3537
}

t/cz-03zlib-v1.t

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,8 @@ EOM
703703
($GOT, $status) = $k->inflate($rest) ;
704704

705705
# Z_STREAM_END returned by 1.12.2, Z_DATA_ERROR for older zlib
706-
if (ZLIB_VERNUM >= ZLIB_1_2_12_0)
706+
# always Z_STREAM_ENDin zlib_ng
707+
if (ZLIB_VERNUM >= ZLIB_1_2_12_0 || Compress::Raw::Zlib::is_zlibng)
707708
{
708709
cmp_ok $status, '==', Z_STREAM_END ;
709710
}

0 commit comments

Comments
 (0)