xref: /openbsd-src/gnu/usr.bin/perl/cpan/Compress-Raw-Zlib/t/02zlib.t (revision 3d61058aa5c692477b6d18acfbbdb653a9930ff9)
1b39c5158SmillertBEGIN {
2b39c5158Smillert    if ($ENV{PERL_CORE}) {
3b39c5158Smillert	chdir 't' if -d 't';
4b39c5158Smillert	@INC = ("../lib", "lib/compress");
5b39c5158Smillert    }
6b39c5158Smillert}
7b39c5158Smillert
8b39c5158Smillertuse lib qw(t t/compress);
9b39c5158Smillertuse strict;
10b39c5158Smillertuse warnings;
11b39c5158Smillertuse bytes;
12b39c5158Smillert
13b39c5158Smillertuse Test::More  ;
14b39c5158Smillert
158bad623eStbuse constant ZLIB_1_2_12_0 => 0x12C0;
16b39c5158Smillert
17b39c5158SmillertBEGIN
18b39c5158Smillert{
19b39c5158Smillert    # use Test::NoWarnings, if available
20b39c5158Smillert    my $extra = 0 ;
21b39c5158Smillert    $extra = 1
22b39c5158Smillert        if eval { require Test::NoWarnings ;  import Test::NoWarnings; 1 };
23b39c5158Smillert
24b39c5158Smillert
25b39c5158Smillert    my $count = 0 ;
26b39c5158Smillert    if ($] < 5.005) {
27eac174f2Safresh1        $count = 249 ;
28b39c5158Smillert    }
29b39c5158Smillert    elsif ($] >= 5.006) {
30eac174f2Safresh1        $count = 353 ;
31b39c5158Smillert    }
32b39c5158Smillert    else {
33eac174f2Safresh1        $count = 308 ;
34b39c5158Smillert    }
35b39c5158Smillert
36b39c5158Smillert    plan tests => $count + $extra;
37b39c5158Smillert
38b39c5158Smillert    use_ok('Compress::Raw::Zlib', 2) ;
39b39c5158Smillert}
40b39c5158Smillert
41fdcd7346Safresh1use CompTestUtils;
42fdcd7346Safresh1
43b39c5158Smillert
44898184e3Ssthenmy $Zlib_ver = Compress::Raw::Zlib::zlib_version ;
45898184e3Ssthen
46b39c5158Smillertmy $hello = <<EOM ;
47b39c5158Smillerthello world
48b39c5158Smillertthis is a test
49b39c5158SmillertEOM
50b39c5158Smillert
51b39c5158Smillertmy $len   = length $hello ;
52b39c5158Smillert
53b39c5158Smillert# Check zlib_version and ZLIB_VERSION are the same.
54fdcd7346Safresh1test_zlib_header_matches_library();
55b39c5158Smillert
56b39c5158Smillert{
57b39c5158Smillert    title "Error Cases" ;
58b39c5158Smillert
59b39c5158Smillert    eval { new Compress::Raw::Zlib::Deflate(-Level) };
60b39c5158Smillert    like $@,  mkErr("^Compress::Raw::Zlib::Deflate::new: Expected even number of parameters, got 1") ;
61b39c5158Smillert
62b39c5158Smillert    eval { new Compress::Raw::Zlib::Inflate(-Level) };
63b39c5158Smillert    like $@, mkErr("^Compress::Raw::Zlib::Inflate::new: Expected even number of parameters, got 1");
64b39c5158Smillert
65b39c5158Smillert    eval { new Compress::Raw::Zlib::Deflate(-Joe => 1) };
66b39c5158Smillert    like $@, mkErr('^Compress::Raw::Zlib::Deflate::new: unknown key value\(s\) Joe');
67b39c5158Smillert
68b39c5158Smillert    eval { new Compress::Raw::Zlib::Inflate(-Joe => 1) };
69b39c5158Smillert    like $@, mkErr('^Compress::Raw::Zlib::Inflate::new: unknown key value\(s\) Joe');
70b39c5158Smillert
71b39c5158Smillert    eval { new Compress::Raw::Zlib::Deflate(-Bufsize => 0) };
72b39c5158Smillert    like $@, mkErr("^Compress::Raw::Zlib::Deflate::new: Bufsize must be >= 1, you specified 0");
73b39c5158Smillert
74b39c5158Smillert    eval { new Compress::Raw::Zlib::Inflate(-Bufsize => 0) };
75b39c5158Smillert    like $@, mkErr("^Compress::Raw::Zlib::Inflate::new: Bufsize must be >= 1, you specified 0");
76b39c5158Smillert
77b39c5158Smillert    eval { new Compress::Raw::Zlib::Deflate(-Bufsize => -1) };
78b39c5158Smillert    like $@, mkErr("^Compress::Raw::Zlib::Deflate::new: Parameter 'Bufsize' must be an unsigned int, got '-1'");
79b39c5158Smillert
80b39c5158Smillert    eval { new Compress::Raw::Zlib::Inflate(-Bufsize => -1) };
81b39c5158Smillert    like $@, mkErr("^Compress::Raw::Zlib::Inflate::new: Parameter 'Bufsize' must be an unsigned int, got '-1'");
82b39c5158Smillert
83b39c5158Smillert    eval { new Compress::Raw::Zlib::Deflate(-Bufsize => "xxx") };
84b39c5158Smillert    like $@, mkErr("^Compress::Raw::Zlib::Deflate::new: Parameter 'Bufsize' must be an unsigned int, got 'xxx'");
85b39c5158Smillert
86b39c5158Smillert    eval { new Compress::Raw::Zlib::Inflate(-Bufsize => "xxx") };
87b39c5158Smillert    like $@, mkErr("^Compress::Raw::Zlib::Inflate::new: Parameter 'Bufsize' must be an unsigned int, got 'xxx'");
88b39c5158Smillert
89b39c5158Smillert    eval { new Compress::Raw::Zlib::Inflate(-Bufsize => 1, 2) };
90b39c5158Smillert    like $@, mkErr("^Compress::Raw::Zlib::Inflate::new: Expected even number of parameters, got 3");
91b39c5158Smillert
92b39c5158Smillert    eval { new Compress::Raw::Zlib::Deflate(-Bufsize => 1, 2) };
93b39c5158Smillert    like $@, mkErr("^Compress::Raw::Zlib::Deflate::new: Expected even number of parameters, got 3");
94b39c5158Smillert
95b39c5158Smillert}
96b39c5158Smillert
97b39c5158Smillert{
98b39c5158Smillert
99b39c5158Smillert    title  "deflate/inflate - small buffer";
100b39c5158Smillert    # ==============================
101b39c5158Smillert
102b39c5158Smillert    my $hello = "I am a HAL 9000 computer" ;
103b39c5158Smillert    my @hello = split('', $hello) ;
104b39c5158Smillert    my ($err, $x, $X, $status);
105b39c5158Smillert
106b39c5158Smillert    ok( ($x, $err) = new Compress::Raw::Zlib::Deflate ( -Bufsize => 1 ), "Create deflate object" );
107b39c5158Smillert    ok $x, "Compress::Raw::Zlib::Deflate ok" ;
108b39c5158Smillert    cmp_ok $err, '==', Z_OK, "status is Z_OK" ;
109b39c5158Smillert
110b39c5158Smillert    ok ! defined $x->msg() ;
111b39c5158Smillert    is $x->total_in(), 0, "total_in() == 0" ;
112b39c5158Smillert    is $x->total_out(), 0, "total_out() == 0" ;
113b39c5158Smillert
114b39c5158Smillert    $X = "" ;
115b39c5158Smillert    my $Answer = '';
116b39c5158Smillert    foreach (@hello)
117b39c5158Smillert    {
118b39c5158Smillert        $status = $x->deflate($_, $X) ;
119b39c5158Smillert        last unless $status == Z_OK ;
120b39c5158Smillert
121b39c5158Smillert        $Answer .= $X ;
122b39c5158Smillert    }
123b39c5158Smillert
124b39c5158Smillert    cmp_ok $status, '==', Z_OK, "deflate returned Z_OK" ;
125b39c5158Smillert
126b39c5158Smillert    cmp_ok  $x->flush($X), '==', Z_OK, "flush returned Z_OK" ;
127b39c5158Smillert    $Answer .= $X ;
128b39c5158Smillert
129b39c5158Smillert    ok ! defined $x->msg()  ;
130b39c5158Smillert    is $x->total_in(), length $hello, "total_in ok" ;
131b39c5158Smillert    is $x->total_out(), length $Answer, "total_out ok" ;
132b39c5158Smillert
133b39c5158Smillert    my @Answer = split('', $Answer) ;
134b39c5158Smillert
135b39c5158Smillert    my $k;
136b39c5158Smillert    ok(($k, $err) = new Compress::Raw::Zlib::Inflate( {-Bufsize => 1}) );
137b39c5158Smillert    ok $k, "Compress::Raw::Zlib::Inflate ok" ;
138b39c5158Smillert    cmp_ok $err, '==', Z_OK, "status is Z_OK" ;
139b39c5158Smillert
140b39c5158Smillert    ok ! defined $k->msg(), "No error messages" ;
141b39c5158Smillert    is $k->total_in(), 0, "total_in() == 0" ;
142b39c5158Smillert    is $k->total_out(), 0, "total_out() == 0" ;
143b39c5158Smillert    my $GOT = '';
144b39c5158Smillert    my $Z;
145b39c5158Smillert    $Z = 1 ;#x 2000 ;
146b39c5158Smillert    foreach (@Answer)
147b39c5158Smillert    {
148b39c5158Smillert        $status = $k->inflate($_, $Z) ;
149b39c5158Smillert        $GOT .= $Z ;
150b39c5158Smillert        last if $status == Z_STREAM_END or $status != Z_OK ;
151b39c5158Smillert
152b39c5158Smillert    }
153b39c5158Smillert
154b39c5158Smillert    cmp_ok $status, '==', Z_STREAM_END, "Got Z_STREAM_END" ;
155b39c5158Smillert    is $GOT, $hello, "uncompressed data matches ok" ;
156b39c5158Smillert    ok ! defined $k->msg(), "No error messages" ;
157b39c5158Smillert    is $k->total_in(), length $Answer, "total_in ok" ;
158b39c5158Smillert    is $k->total_out(), length $hello , "total_out ok";
159b39c5158Smillert
160b39c5158Smillert}
161b39c5158Smillert
162b39c5158Smillert
163b39c5158Smillert{
164b39c5158Smillert    # deflate/inflate - small buffer with a number
165b39c5158Smillert    # ==============================
166b39c5158Smillert
167b39c5158Smillert    my $hello = 6529 ;
168b39c5158Smillert
169b39c5158Smillert    ok  my ($x, $err) = new Compress::Raw::Zlib::Deflate ( -Bufsize => 1, -AppendOutput => 1 ) ;
170b39c5158Smillert    ok $x ;
171b39c5158Smillert    cmp_ok $err, '==', Z_OK ;
172b39c5158Smillert
173b39c5158Smillert    my $status;
174b39c5158Smillert    my $Answer = '';
175b39c5158Smillert
176b39c5158Smillert    cmp_ok $x->deflate($hello, $Answer), '==', Z_OK ;
177b39c5158Smillert
178b39c5158Smillert    cmp_ok $x->flush($Answer), '==', Z_OK ;
179b39c5158Smillert
180b39c5158Smillert    my @Answer = split('', $Answer) ;
181b39c5158Smillert
182b39c5158Smillert    my $k;
183b39c5158Smillert    ok(($k, $err) = new Compress::Raw::Zlib::Inflate( {-Bufsize => 1, -AppendOutput =>1}) );
184b39c5158Smillert    ok $k ;
185b39c5158Smillert    cmp_ok $err, '==', Z_OK ;
186b39c5158Smillert
187b39c5158Smillert    #my $GOT = '';
188b39c5158Smillert    my $GOT ;
189b39c5158Smillert    foreach (@Answer)
190b39c5158Smillert    {
191b39c5158Smillert        $status = $k->inflate($_, $GOT) ;
192b39c5158Smillert        last if $status == Z_STREAM_END or $status != Z_OK ;
193b39c5158Smillert
194b39c5158Smillert    }
195b39c5158Smillert
196b39c5158Smillert    cmp_ok $status, '==', Z_STREAM_END ;
197b39c5158Smillert    is $GOT, $hello ;
198b39c5158Smillert
199b39c5158Smillert}
200b39c5158Smillert
201b39c5158Smillert{
202b39c5158Smillert
203b39c5158Smillert# deflate/inflate options - AppendOutput
204b39c5158Smillert# ================================
205b39c5158Smillert
206b39c5158Smillert    # AppendOutput
207b39c5158Smillert    # CRC
208b39c5158Smillert
209b39c5158Smillert    my $hello = "I am a HAL 9000 computer" ;
210b39c5158Smillert    my @hello = split('', $hello) ;
211b39c5158Smillert
212b39c5158Smillert    ok  my ($x, $err) = new Compress::Raw::Zlib::Deflate ( {-Bufsize => 1, -AppendOutput =>1} ) ;
213b39c5158Smillert    ok $x ;
214b39c5158Smillert    cmp_ok $err, '==', Z_OK ;
215b39c5158Smillert
216b39c5158Smillert    my $status;
217b39c5158Smillert    my $X;
218b39c5158Smillert    foreach (@hello)
219b39c5158Smillert    {
220b39c5158Smillert        $status = $x->deflate($_, $X) ;
221b39c5158Smillert        last unless $status == Z_OK ;
222b39c5158Smillert    }
223b39c5158Smillert
224b39c5158Smillert    cmp_ok $status, '==', Z_OK ;
225b39c5158Smillert
226b39c5158Smillert    cmp_ok $x->flush($X), '==', Z_OK ;
227b39c5158Smillert
228b39c5158Smillert
229b39c5158Smillert    my @Answer = split('', $X) ;
230b39c5158Smillert
231b39c5158Smillert    my $k;
232b39c5158Smillert    ok(($k, $err) = new Compress::Raw::Zlib::Inflate( {-Bufsize => 1, -AppendOutput =>1}));
233b39c5158Smillert    ok $k ;
234b39c5158Smillert    cmp_ok $err, '==', Z_OK ;
235b39c5158Smillert
236b39c5158Smillert    my $Z;
237b39c5158Smillert    foreach (@Answer)
238b39c5158Smillert    {
239b39c5158Smillert        $status = $k->inflate($_, $Z) ;
240b39c5158Smillert        last if $status == Z_STREAM_END or $status != Z_OK ;
241b39c5158Smillert
242b39c5158Smillert    }
243b39c5158Smillert
244b39c5158Smillert    cmp_ok $status, '==', Z_STREAM_END ;
245b39c5158Smillert    is $Z, $hello ;
246b39c5158Smillert}
247b39c5158Smillert
248b39c5158Smillert
249b39c5158Smillert{
250b39c5158Smillert
251b39c5158Smillert    title "deflate/inflate - larger buffer";
252b39c5158Smillert    # ==============================
253b39c5158Smillert
254b39c5158Smillert    # generate a long random string
255b39c5158Smillert    my $contents = '' ;
256b39c5158Smillert    foreach (1 .. 50000)
257b39c5158Smillert      { $contents .= chr int rand 255 }
258b39c5158Smillert
259b39c5158Smillert
260b39c5158Smillert    ok my ($x, $err) = new Compress::Raw::Zlib::Deflate() ;
261b39c5158Smillert    ok $x ;
262b39c5158Smillert    cmp_ok $err, '==', Z_OK ;
263b39c5158Smillert
264b39c5158Smillert    my (%X, $Y, %Z, $X, $Z);
265b39c5158Smillert    #cmp_ok $x->deflate($contents, $X{key}), '==', Z_OK ;
266b39c5158Smillert    cmp_ok $x->deflate($contents, $X), '==', Z_OK ;
267b39c5158Smillert
268b39c5158Smillert    #$Y = $X{key} ;
269b39c5158Smillert    $Y = $X ;
270b39c5158Smillert
271b39c5158Smillert
272b39c5158Smillert    #cmp_ok $x->flush($X{key}), '==', Z_OK ;
273b39c5158Smillert    #$Y .= $X{key} ;
274b39c5158Smillert    cmp_ok $x->flush($X), '==', Z_OK ;
275b39c5158Smillert    $Y .= $X ;
276b39c5158Smillert
277b39c5158Smillert
278b39c5158Smillert
279b39c5158Smillert    my $keep = $Y ;
280b39c5158Smillert
281b39c5158Smillert    my $k;
282b39c5158Smillert    ok(($k, $err) = new Compress::Raw::Zlib::Inflate() );
283b39c5158Smillert    ok $k ;
284b39c5158Smillert    cmp_ok $err, '==', Z_OK ;
285b39c5158Smillert
286b39c5158Smillert    #cmp_ok $k->inflate($Y, $Z{key}), '==', Z_STREAM_END ;
287b39c5158Smillert    #ok $contents eq $Z{key} ;
288b39c5158Smillert    cmp_ok $k->inflate($Y, $Z), '==', Z_STREAM_END ;
289b39c5158Smillert    ok $contents eq $Z ;
290b39c5158Smillert
291b39c5158Smillert    # redo deflate with AppendOutput
292b39c5158Smillert
293b39c5158Smillert    ok (($k, $err) = new Compress::Raw::Zlib::Inflate(-AppendOutput => 1)) ;
294b39c5158Smillert    ok $k ;
295b39c5158Smillert    cmp_ok $err, '==', Z_OK ;
296b39c5158Smillert
297b39c5158Smillert    my $s ;
298b39c5158Smillert    my $out ;
299b39c5158Smillert    my @bits = split('', $keep) ;
300b39c5158Smillert    foreach my $bit (@bits) {
301b39c5158Smillert        $s = $k->inflate($bit, $out) ;
302b39c5158Smillert    }
303b39c5158Smillert
304b39c5158Smillert    cmp_ok $s, '==', Z_STREAM_END ;
305b39c5158Smillert
306b39c5158Smillert    ok $contents eq $out ;
307b39c5158Smillert
308b39c5158Smillert
309b39c5158Smillert}
310b39c5158Smillert
311b39c5158Smillert{
312b39c5158Smillert
313b39c5158Smillert    title "deflate/inflate - preset dictionary";
314b39c5158Smillert    # ===================================
315b39c5158Smillert
316b39c5158Smillert    my $dictionary = "hello" ;
317b39c5158Smillert    ok my $x = new Compress::Raw::Zlib::Deflate({-Level => Z_BEST_COMPRESSION,
318b39c5158Smillert			     -Dictionary => $dictionary}) ;
319b39c5158Smillert
320b39c5158Smillert    my $dictID = $x->dict_adler() ;
321b39c5158Smillert
322b39c5158Smillert    my ($X, $Y, $Z);
323b39c5158Smillert    cmp_ok $x->deflate($hello, $X), '==', Z_OK;
324b39c5158Smillert    cmp_ok $x->flush($Y), '==', Z_OK;
325b39c5158Smillert    $X .= $Y ;
326b39c5158Smillert
327b39c5158Smillert    ok my $k = new Compress::Raw::Zlib::Inflate(-Dictionary => $dictionary) ;
328b39c5158Smillert
329b39c5158Smillert    cmp_ok $k->inflate($X, $Z), '==', Z_STREAM_END;
330b39c5158Smillert    is $k->dict_adler(), $dictID;
331b39c5158Smillert    is $hello, $Z ;
332b39c5158Smillert
333b39c5158Smillert}
334b39c5158Smillert
335b39c5158Smillerttitle 'inflate - check remaining buffer after Z_STREAM_END';
336b39c5158Smillert#           and that ConsumeInput works.
337b39c5158Smillert# ===================================================
338b39c5158Smillert
339b39c5158Smillertfor my $consume ( 0 .. 1)
340b39c5158Smillert{
341b39c5158Smillert    ok my $x = new Compress::Raw::Zlib::Deflate(-Level => Z_BEST_COMPRESSION ) ;
342b39c5158Smillert
343b39c5158Smillert    my ($X, $Y, $Z);
344b39c5158Smillert    cmp_ok $x->deflate($hello, $X), '==', Z_OK;
345b39c5158Smillert    cmp_ok $x->flush($Y), '==', Z_OK;
346b39c5158Smillert    $X .= $Y ;
347b39c5158Smillert
348b39c5158Smillert    ok my $k = new Compress::Raw::Zlib::Inflate( -ConsumeInput => $consume) ;
349b39c5158Smillert
350b39c5158Smillert    my $first = substr($X, 0, 2) ;
351b39c5158Smillert    my $remember_first = $first ;
352b39c5158Smillert    my $last  = substr($X, 2) ;
353b39c5158Smillert    cmp_ok $k->inflate($first, $Z), '==', Z_OK;
354b39c5158Smillert    if ($consume) {
355b39c5158Smillert        ok $first eq "" ;
356b39c5158Smillert    }
357b39c5158Smillert    else {
358b39c5158Smillert        ok $first eq $remember_first ;
359b39c5158Smillert    }
360b39c5158Smillert
361b39c5158Smillert    my $T ;
362b39c5158Smillert    $last .= "appendage" ;
363b39c5158Smillert    my $remember_last = $last ;
364b39c5158Smillert    cmp_ok $k->inflate($last, $T),  '==', Z_STREAM_END;
365b39c5158Smillert    is $hello, $Z . $T  ;
366b39c5158Smillert    if ($consume) {
367b39c5158Smillert        is $last, "appendage" ;
368b39c5158Smillert    }
369b39c5158Smillert    else {
370b39c5158Smillert        is $last, $remember_last ;
371b39c5158Smillert    }
372b39c5158Smillert
373b39c5158Smillert}
374b39c5158Smillert
375b39c5158Smillert
376b39c5158Smillert
377b39c5158Smillert{
378b39c5158Smillert
379b39c5158Smillert    title 'Check - MAX_WBITS';
380b39c5158Smillert    # =================
381b39c5158Smillert
382b39c5158Smillert    my $hello = "Test test test test test";
383b39c5158Smillert    my @hello = split('', $hello) ;
384b39c5158Smillert
385b39c5158Smillert    ok  my ($x, $err) =
386b39c5158Smillert       new Compress::Raw::Zlib::Deflate ( -Bufsize => 1,
387b39c5158Smillert                                     -WindowBits => -MAX_WBITS(),
388b39c5158Smillert                                     -AppendOutput => 1 ) ;
389b39c5158Smillert    ok $x ;
390b39c5158Smillert    cmp_ok $err, '==', Z_OK ;
391b39c5158Smillert
392b39c5158Smillert    my $Answer = '';
393b39c5158Smillert    my $status;
394b39c5158Smillert    foreach (@hello)
395b39c5158Smillert    {
396b39c5158Smillert        $status = $x->deflate($_, $Answer) ;
397b39c5158Smillert        last unless $status == Z_OK ;
398b39c5158Smillert    }
399b39c5158Smillert
400b39c5158Smillert    cmp_ok $status, '==', Z_OK ;
401b39c5158Smillert
402b39c5158Smillert    cmp_ok $x->flush($Answer), '==', Z_OK ;
403b39c5158Smillert
404b39c5158Smillert    my @Answer = split('', $Answer) ;
405b39c5158Smillert    # Undocumented corner -- extra byte needed to get inflate to return
406b39c5158Smillert    # Z_STREAM_END when done.
407b39c5158Smillert    push @Answer, " " ;
408b39c5158Smillert
409b39c5158Smillert    my $k;
410b39c5158Smillert    ok(($k, $err) = new Compress::Raw::Zlib::Inflate(
411b39c5158Smillert			{-Bufsize => 1,
412b39c5158Smillert			-AppendOutput =>1,
413b39c5158Smillert			-WindowBits => -MAX_WBITS()})) ;
414b39c5158Smillert    ok $k ;
415b39c5158Smillert    cmp_ok $err, '==', Z_OK ;
416b39c5158Smillert
417b39c5158Smillert    my $GOT = '';
418b39c5158Smillert    foreach (@Answer)
419b39c5158Smillert    {
420b39c5158Smillert        $status = $k->inflate($_, $GOT) ;
421b39c5158Smillert        last if $status == Z_STREAM_END or $status != Z_OK ;
422b39c5158Smillert
423b39c5158Smillert    }
424b39c5158Smillert
425b39c5158Smillert    cmp_ok $status, '==', Z_STREAM_END ;
426b39c5158Smillert    is $GOT, $hello ;
427b39c5158Smillert
428b39c5158Smillert}
429b39c5158Smillert
430898184e3SsthenSKIP:
431b39c5158Smillert{
432b39c5158Smillert    title 'inflateSync';
433b39c5158Smillert
434898184e3Ssthen    skip "inflateSync needs zlib 1.2.1 or better, you have $Zlib_ver", 22
435898184e3Ssthen        if ZLIB_VERNUM() < 0x1210 ;
436898184e3Ssthen
437b39c5158Smillert    # create a deflate stream with flush points
438b39c5158Smillert
439b39c5158Smillert    my $hello = "I am a HAL 9000 computer" x 2001 ;
440b39c5158Smillert    my $goodbye = "Will I dream?" x 2010;
441b39c5158Smillert    my ($x, $err, $answer, $X, $Z, $status);
442b39c5158Smillert    my $Answer ;
443b39c5158Smillert
444b39c5158Smillert    #use Devel::Peek ;
445b39c5158Smillert    ok(($x, $err) = new Compress::Raw::Zlib::Deflate(AppendOutput => 1)) ;
446b39c5158Smillert    ok $x ;
447b39c5158Smillert    cmp_ok $err, '==', Z_OK ;
448b39c5158Smillert
449b39c5158Smillert    cmp_ok $x->deflate($hello, $Answer), '==', Z_OK;
450b39c5158Smillert
451b39c5158Smillert    # create a flush point
452b39c5158Smillert    cmp_ok $x->flush($Answer, Z_FULL_FLUSH), '==', Z_OK ;
453b39c5158Smillert
454b39c5158Smillert    my $len1 = length $Answer;
455b39c5158Smillert
456b39c5158Smillert    cmp_ok $x->deflate($goodbye, $Answer), '==', Z_OK;
457b39c5158Smillert
458b39c5158Smillert    cmp_ok $x->flush($Answer), '==', Z_OK ;
459b39c5158Smillert    my $len2 = length($Answer) - $len1 ;
460b39c5158Smillert
461b39c5158Smillert    my ($first, @Answer) = split('', $Answer) ;
462b39c5158Smillert
463b39c5158Smillert    my $k;
464b39c5158Smillert    ok(($k, $err) = new Compress::Raw::Zlib::Inflate()) ;
465b39c5158Smillert    ok $k ;
466b39c5158Smillert    cmp_ok $err, '==', Z_OK ;
467b39c5158Smillert
468b39c5158Smillert    cmp_ok  $k->inflate($first, $Z), '==', Z_OK;
469b39c5158Smillert
470b39c5158Smillert    # skip to the first flush point.
471b39c5158Smillert    while (@Answer)
472b39c5158Smillert    {
473b39c5158Smillert        my $byte = shift @Answer;
474b39c5158Smillert        $status = $k->inflateSync($byte) ;
475b39c5158Smillert        last unless $status == Z_DATA_ERROR;
476b39c5158Smillert    }
477b39c5158Smillert
478b39c5158Smillert    cmp_ok $status, '==', Z_OK;
479b39c5158Smillert
480b39c5158Smillert    my $GOT = '';
481b39c5158Smillert    foreach (@Answer)
482b39c5158Smillert    {
483b39c5158Smillert        my $Z = '';
484b39c5158Smillert        $status = $k->inflate($_, $Z) ;
485b39c5158Smillert        $GOT .= $Z if defined $Z ;
486b39c5158Smillert        # print "x $status\n";
487b39c5158Smillert        last if $status == Z_STREAM_END or $status != Z_OK ;
488b39c5158Smillert    }
489b39c5158Smillert
4908bad623eStb    # Z_STREAM_END returned by 1.12.2, Z_DATA_ERROR for older zlib
491fdcd7346Safresh1    # ZLIB_NG has the fix for all versions
492fdcd7346Safresh1    if (ZLIB_VERNUM >= ZLIB_1_2_12_0 ||  Compress::Raw::Zlib::is_zlibng)
4938bad623eStb    {
4948bad623eStb        cmp_ok $status, '==', Z_STREAM_END ;
4958bad623eStb    }
4968bad623eStb    else
4978bad623eStb    {
498b39c5158Smillert        cmp_ok $status, '==', Z_DATA_ERROR ;
4998bad623eStb    }
500b39c5158Smillert
5018bad623eStb    is $GOT, $goodbye ;
502b39c5158Smillert
503eac174f2Safresh1
504b39c5158Smillert    # Check inflateSync leaves good data in buffer
505b39c5158Smillert    my $rest = $Answer ;
506b39c5158Smillert    $rest =~ s/^(.)//;
507b39c5158Smillert    my $initial = $1 ;
508b39c5158Smillert
509b39c5158Smillert
510b39c5158Smillert    ok(($k, $err) = new Compress::Raw::Zlib::Inflate(ConsumeInput => 0)) ;
511b39c5158Smillert    ok $k ;
512b39c5158Smillert    cmp_ok $err, '==', Z_OK ;
513b39c5158Smillert
514b39c5158Smillert    cmp_ok $k->inflate($initial, $Z), '==', Z_OK;
515b39c5158Smillert
516b39c5158Smillert    # Skip to the flush point
517b39c5158Smillert    $status = $k->inflateSync($rest);
518b39c5158Smillert    cmp_ok $status, '==', Z_OK
519b39c5158Smillert     or diag "status '$status'\nlength rest is " . length($rest) . "\n" ;
520b39c5158Smillert
521b39c5158Smillert    is length($rest), $len2, "expected compressed output";
522b39c5158Smillert
523b39c5158Smillert    $GOT = '';
5248bad623eStb    $status = $k->inflate($rest, $GOT);
5258bad623eStb    # Z_STREAM_END returned by 1.12.2, Z_DATA_ERROR for older zlib
526fdcd7346Safresh1    if (ZLIB_VERNUM >= ZLIB_1_2_12_0 || Compress::Raw::Zlib::is_zlibng)
5278bad623eStb    {
5288bad623eStb        cmp_ok $status, '==', Z_STREAM_END ;
5298bad623eStb    }
5308bad623eStb    else
5318bad623eStb    {
5328bad623eStb        cmp_ok $status, '==', Z_DATA_ERROR ;
5338bad623eStb    }
5348bad623eStb
535b39c5158Smillert    is $GOT, $goodbye ;
536b39c5158Smillert}
537b39c5158Smillert
538b39c5158Smillert{
539b39c5158Smillert    title 'deflateParams';
540b39c5158Smillert
541b39c5158Smillert    my $hello = "I am a HAL 9000 computer" x 2001 ;
542b39c5158Smillert    my $goodbye = "Will I dream?" x 2010;
543b39c5158Smillert    my ($x, $input, $err, $answer, $X, $status, $Answer);
544b39c5158Smillert
545b39c5158Smillert    ok(($x, $err) = new Compress::Raw::Zlib::Deflate(
546b39c5158Smillert                       -AppendOutput   => 1,
547b39c5158Smillert                       -Level    => Z_DEFAULT_COMPRESSION,
548b39c5158Smillert                       -Strategy => Z_DEFAULT_STRATEGY)) ;
549b39c5158Smillert    ok $x ;
550b39c5158Smillert    cmp_ok $err, '==', Z_OK ;
551b39c5158Smillert
552b39c5158Smillert    ok $x->get_Level()    == Z_DEFAULT_COMPRESSION;
553b39c5158Smillert    ok $x->get_Strategy() == Z_DEFAULT_STRATEGY;
554b39c5158Smillert
555b39c5158Smillert    $status = $x->deflate($hello, $Answer) ;
556b39c5158Smillert    cmp_ok $status, '==', Z_OK ;
557b39c5158Smillert    $input .= $hello;
558b39c5158Smillert
559b39c5158Smillert    # error cases
560b39c5158Smillert    eval { $x->deflateParams() };
561b39c5158Smillert    like $@, mkErr('^Compress::Raw::Zlib::deflateParams needs Level and\/or Strategy');
562b39c5158Smillert
563b39c5158Smillert    eval { $x->deflateParams(-Bufsize => 0) };
564b39c5158Smillert    like $@, mkErr('^Compress::Raw::Zlib::Inflate::deflateParams: Bufsize must be >= 1, you specified 0');
565b39c5158Smillert
566b39c5158Smillert    eval { $x->deflateParams(-Joe => 3) };
567b39c5158Smillert    like $@, mkErr('^Compress::Raw::Zlib::deflateStream::deflateParams: unknown key value\(s\) Joe');
568b39c5158Smillert
569b39c5158Smillert    is $x->get_Level(),    Z_DEFAULT_COMPRESSION;
570b39c5158Smillert    is $x->get_Strategy(), Z_DEFAULT_STRATEGY;
571b39c5158Smillert
572b39c5158Smillert    # change both Level & Strategy
573b39c5158Smillert    $status = $x->deflateParams(-Level => Z_BEST_SPEED, -Strategy => Z_HUFFMAN_ONLY, -Bufsize => 1234) ;
574b39c5158Smillert    cmp_ok $status, '==', Z_OK ;
575b39c5158Smillert
576b39c5158Smillert    is $x->get_Level(),    Z_BEST_SPEED;
577b39c5158Smillert    is $x->get_Strategy(), Z_HUFFMAN_ONLY;
578b39c5158Smillert
5795759b3d2Safresh1    # change both Level & Strategy again without any calls to deflate
5805759b3d2Safresh1    $status = $x->deflateParams(-Level => Z_DEFAULT_COMPRESSION, -Strategy => Z_DEFAULT_STRATEGY, -Bufsize => 1234) ;
5815759b3d2Safresh1    cmp_ok $status, '==', Z_OK ;
5825759b3d2Safresh1
5835759b3d2Safresh1    is $x->get_Level(),    Z_DEFAULT_COMPRESSION;
5845759b3d2Safresh1    is $x->get_Strategy(), Z_DEFAULT_STRATEGY;
5855759b3d2Safresh1
586b39c5158Smillert    $status = $x->deflate($goodbye, $Answer) ;
587b39c5158Smillert    cmp_ok $status, '==', Z_OK ;
588b39c5158Smillert    $input .= $goodbye;
589b39c5158Smillert
590b39c5158Smillert    # change only Level
591b39c5158Smillert    $status = $x->deflateParams(-Level => Z_NO_COMPRESSION) ;
592b39c5158Smillert    cmp_ok $status, '==', Z_OK ;
593b39c5158Smillert
594b39c5158Smillert    is $x->get_Level(),    Z_NO_COMPRESSION;
5955759b3d2Safresh1    is $x->get_Strategy(), Z_DEFAULT_STRATEGY;
596b39c5158Smillert
597b39c5158Smillert    $status = $x->deflate($goodbye, $Answer) ;
598b39c5158Smillert    cmp_ok $status, '==', Z_OK ;
599b39c5158Smillert    $input .= $goodbye;
600b39c5158Smillert
601b39c5158Smillert    # change only Strategy
602b39c5158Smillert    $status = $x->deflateParams(-Strategy => Z_FILTERED) ;
603b39c5158Smillert    cmp_ok $status, '==', Z_OK ;
604b39c5158Smillert
605b39c5158Smillert    is $x->get_Level(),    Z_NO_COMPRESSION;
606b39c5158Smillert    is $x->get_Strategy(), Z_FILTERED;
607b39c5158Smillert
608b39c5158Smillert    $status = $x->deflate($goodbye, $Answer) ;
609b39c5158Smillert    cmp_ok $status, '==', Z_OK ;
610b39c5158Smillert    $input .= $goodbye;
611b39c5158Smillert
612b39c5158Smillert    cmp_ok $x->flush($Answer), '==', Z_OK ;
613b39c5158Smillert
614b39c5158Smillert    my $k;
615b39c5158Smillert    ok(($k, $err) = new Compress::Raw::Zlib::Inflate()) ;
616b39c5158Smillert    ok $k ;
617b39c5158Smillert    cmp_ok $err, '==', Z_OK ;
618b39c5158Smillert
619b39c5158Smillert    my $Z;
620b39c5158Smillert    $status = $k->inflate($Answer, $Z) ;
621b39c5158Smillert
622b39c5158Smillert    cmp_ok $status, '==', Z_STREAM_END ;
623b39c5158Smillert    is $Z, $input ;
624b39c5158Smillert}
625b39c5158Smillert
626b39c5158Smillert
627b39c5158Smillert{
628b39c5158Smillert    title "ConsumeInput and a read-only buffer trapped" ;
629b39c5158Smillert
630b39c5158Smillert    ok my $k = new Compress::Raw::Zlib::Inflate(-ConsumeInput => 1) ;
631b39c5158Smillert
632b39c5158Smillert    my $Z;
633b39c5158Smillert    eval { $k->inflate("abc", $Z) ; };
634b39c5158Smillert    like $@, mkErr("Compress::Raw::Zlib::Inflate::inflate input parameter cannot be read-only when ConsumeInput is specified");
635b39c5158Smillert
636b39c5158Smillert}
637b39c5158Smillert
638*3d61058aSafresh1SKIP:
639b39c5158Smillertforeach (1 .. 2)
640b39c5158Smillert{
641b39c5158Smillert    next if $] < 5.005 ;
642b39c5158Smillert
643b39c5158Smillert    title 'test inflate/deflate with a substr';
644b39c5158Smillert
645*3d61058aSafresh1    # # temp workaround for
646*3d61058aSafresh1    # # https://github.com/pmqs/Compress-Raw-Zlib/issues/27
647*3d61058aSafresh1    # skip "skipping substr tests for Perl 5.6.*", 15
648*3d61058aSafresh1    #     if $] < 5.008 ;
649*3d61058aSafresh1
650b39c5158Smillert    my $contents = '' ;
651b39c5158Smillert    foreach (1 .. 5000)
652b39c5158Smillert      { $contents .= chr int rand 255 }
653b39c5158Smillert    ok  my $x = new Compress::Raw::Zlib::Deflate(-AppendOutput => 1) ;
654b39c5158Smillert
655b39c5158Smillert    my $X ;
656*3d61058aSafresh1    # my $data = substr($contents,0) ;
657*3d61058aSafresh1    # my $status = $x->deflate($data, $X);
658b39c5158Smillert    my $status = $x->deflate(substr($contents,0), $X);
659b39c5158Smillert    cmp_ok $status, '==', Z_OK ;
660b39c5158Smillert
661b39c5158Smillert    cmp_ok $x->flush($X), '==', Z_OK  ;
662b39c5158Smillert
663b39c5158Smillert    my $append = "Appended" ;
664b39c5158Smillert    $X .= $append ;
665b39c5158Smillert
666b39c5158Smillert    ok my $k = new Compress::Raw::Zlib::Inflate(-AppendOutput => 1) ;
667b39c5158Smillert
668b39c5158Smillert    my $Z;
669b39c5158Smillert    my $keep = $X ;
670b39c5158Smillert    $status = $k->inflate(substr($X, 0), $Z) ;
671b39c5158Smillert
672b39c5158Smillert    cmp_ok $status, '==', Z_STREAM_END ;
673b39c5158Smillert    #print "status $status X [$X]\n" ;
674b39c5158Smillert    is $contents, $Z ;
675b39c5158Smillert    ok $X eq $append;
676b39c5158Smillert    #is length($X), length($append);
677b39c5158Smillert    #ok $X eq $keep;
678b39c5158Smillert    #is length($X), length($keep);
679b39c5158Smillert}
680b39c5158Smillert
681b39c5158Smillerttitle 'Looping Append test - checks that deRef_l resets the output buffer';
682b39c5158Smillertforeach (1 .. 2)
683b39c5158Smillert{
684b39c5158Smillert
685b39c5158Smillert    my $hello = "I am a HAL 9000 computer" ;
686b39c5158Smillert    my @hello = split('', $hello) ;
687b39c5158Smillert    my ($err, $x, $X, $status);
688b39c5158Smillert
689b39c5158Smillert    ok( ($x, $err) = new Compress::Raw::Zlib::Deflate ( -Bufsize => 1 ) );
690b39c5158Smillert    ok $x ;
691b39c5158Smillert    cmp_ok $err, '==', Z_OK ;
692b39c5158Smillert
693b39c5158Smillert    $X = "" ;
694b39c5158Smillert    my $Answer = '';
695b39c5158Smillert    foreach (@hello)
696b39c5158Smillert    {
697b39c5158Smillert        $status = $x->deflate($_, $X) ;
698b39c5158Smillert        last unless $status == Z_OK ;
699b39c5158Smillert
700b39c5158Smillert        $Answer .= $X ;
701b39c5158Smillert    }
702b39c5158Smillert
703b39c5158Smillert    cmp_ok $status, '==', Z_OK ;
704b39c5158Smillert
705b39c5158Smillert    cmp_ok  $x->flush($X), '==', Z_OK ;
706b39c5158Smillert    $Answer .= $X ;
707b39c5158Smillert
708b39c5158Smillert    my @Answer = split('', $Answer) ;
709b39c5158Smillert
710b39c5158Smillert    my $k;
711b39c5158Smillert    ok(($k, $err) = new Compress::Raw::Zlib::Inflate(-AppendOutput => 1) );
712b39c5158Smillert    ok $k ;
713b39c5158Smillert    cmp_ok $err, '==', Z_OK ;
714b39c5158Smillert
715b39c5158Smillert    my $GOT ;
716b39c5158Smillert    my $Z;
717b39c5158Smillert    $Z = 1 ;#x 2000 ;
718b39c5158Smillert    foreach (@Answer)
719b39c5158Smillert    {
720b39c5158Smillert        $status = $k->inflate($_, $GOT) ;
721b39c5158Smillert        last if $status == Z_STREAM_END or $status != Z_OK ;
722b39c5158Smillert    }
723b39c5158Smillert
724b39c5158Smillert    cmp_ok $status, '==', Z_STREAM_END ;
725b39c5158Smillert    is $GOT, $hello ;
726b39c5158Smillert
727b39c5158Smillert}
728b39c5158Smillert
729*3d61058aSafresh1SKIP: {
730b39c5158Smillertif ($] >= 5.005)
731b39c5158Smillert{
732b39c5158Smillert    title 'test inflate input parameter via substr';
733b39c5158Smillert
734*3d61058aSafresh1    # # temp workaround for
735*3d61058aSafresh1    # # https://github.com/pmqs/Compress-Raw-Zlib/issues/27
736*3d61058aSafresh1    # skip "skipping substr tests for Perl 5.6.*", 11
737*3d61058aSafresh1    #     if $] < 5.008 ;
738*3d61058aSafresh1
739b39c5158Smillert    my $hello = "I am a HAL 9000 computer" ;
740b39c5158Smillert    my $data = $hello ;
741b39c5158Smillert
742b39c5158Smillert    my($X, $Z);
743b39c5158Smillert
744b39c5158Smillert    ok my $x = new Compress::Raw::Zlib::Deflate ( -AppendOutput => 1 );
745b39c5158Smillert
746b39c5158Smillert    cmp_ok $x->deflate($data, $X), '==',  Z_OK ;
747b39c5158Smillert
748b39c5158Smillert    cmp_ok $x->flush($X), '==', Z_OK ;
749b39c5158Smillert
750b39c5158Smillert    my $append = "Appended" ;
751b39c5158Smillert    $X .= $append ;
752b39c5158Smillert    my $keep = $X ;
753b39c5158Smillert
754b39c5158Smillert    ok my $k = new Compress::Raw::Zlib::Inflate ( -AppendOutput => 1,
755b39c5158Smillert                                             -ConsumeInput => 1 ) ;
756b39c5158Smillert
757b39c5158Smillert    cmp_ok $k->inflate(substr($X, 0, -1), $Z), '==', Z_STREAM_END ; ;
758b39c5158Smillert
759b39c5158Smillert    ok $hello eq $Z ;
760b39c5158Smillert    is $X, $append;
761b39c5158Smillert
762b39c5158Smillert    $X = $keep ;
763b39c5158Smillert    $Z = '';
764b39c5158Smillert    ok $k = new Compress::Raw::Zlib::Inflate ( -AppendOutput => 1,
765b39c5158Smillert                                          -ConsumeInput => 0 ) ;
766b39c5158Smillert
767*3d61058aSafresh1    # my $data = substr($X, 0, -1);
768*3d61058aSafresh1    # cmp_ok $k->inflate($data, $Z), '==', Z_STREAM_END ; ;
769*3d61058aSafresh1    cmp_ok $k->inflate(substr($X, 0), $Z), '==', Z_STREAM_END ; ;
770b39c5158Smillert
771b39c5158Smillert    ok $hello eq $Z ;
772b39c5158Smillert    is $X, $keep;
773b39c5158Smillert
774b39c5158Smillert}
775*3d61058aSafresh1}
776b39c5158Smillert
777eac174f2Safresh1{
778eac174f2Safresh1    title 'RT#132734: test inflate append OOK output parameter';
779eac174f2Safresh1    # https://github.com/pmqs/Compress-Raw-Zlib/issues/3
780eac174f2Safresh1
781eac174f2Safresh1    my $hello = "I am a HAL 9000 computer" ;
782eac174f2Safresh1    my $data = $hello ;
783eac174f2Safresh1
784eac174f2Safresh1    my($X, $Z);
785eac174f2Safresh1
786eac174f2Safresh1    ok my $x = new Compress::Raw::Zlib::Deflate ( -AppendOutput => 1 );
787eac174f2Safresh1
788eac174f2Safresh1    cmp_ok $x->deflate($data, $X), '==',  Z_OK ;
789eac174f2Safresh1
790eac174f2Safresh1    cmp_ok $x->flush($X), '==', Z_OK ;
791eac174f2Safresh1
792eac174f2Safresh1    ok my $k = new Compress::Raw::Zlib::Inflate ( -AppendOutput => 1,
793eac174f2Safresh1                                             -ConsumeInput => 1 ) ;
794eac174f2Safresh1    $Z = 'prev. ' ;
795eac174f2Safresh1    substr($Z, 0, 4, ''); # chop off first 4 characters using offset
796eac174f2Safresh1    cmp_ok $Z, 'eq', '. ' ;
797eac174f2Safresh1
798eac174f2Safresh1    # use Devel::Peek ; Dump($Z) ; # shows OOK flag
799eac174f2Safresh1
800eac174f2Safresh1    # if (1) { # workaround
801eac174f2Safresh1    #     my $prev = $Z;
802eac174f2Safresh1    #     undef $Z ;
803eac174f2Safresh1    #     $Z = $prev ;
804eac174f2Safresh1    # }
805eac174f2Safresh1
806eac174f2Safresh1    cmp_ok $k->inflate($X, $Z), '==', Z_STREAM_END ;
807eac174f2Safresh1    # use Devel::Peek ; Dump($Z) ; # No OOK flag
808eac174f2Safresh1
809eac174f2Safresh1    cmp_ok $Z, 'eq', ". $hello" ;
810eac174f2Safresh1}
811eac174f2Safresh1
812eac174f2Safresh1
813eac174f2Safresh1{
814eac174f2Safresh1    title 'RT#132734: test deflate append OOK output parameter';
815eac174f2Safresh1    # https://github.com/pmqs/Compress-Raw-Zlib/issues/3
816eac174f2Safresh1
817eac174f2Safresh1    my $hello = "I am a HAL 9000 computer" ;
818eac174f2Safresh1    my $data = $hello ;
819eac174f2Safresh1
820eac174f2Safresh1    my($X, $Z);
821eac174f2Safresh1
822eac174f2Safresh1    $X = 'prev. ' ;
823eac174f2Safresh1    substr($X, 0, 6, ''); # chop off all characters using offset
824eac174f2Safresh1    cmp_ok $X, 'eq', '' ;
825eac174f2Safresh1
826eac174f2Safresh1    # use Devel::Peek ; Dump($X) ; # shows OOK flag
827eac174f2Safresh1
828eac174f2Safresh1    # if (1) { # workaround
829eac174f2Safresh1    #     my $prev = $Z;
830eac174f2Safresh1    #     undef $Z ;
831eac174f2Safresh1    #     $Z = $prev ;
832eac174f2Safresh1    # }
833eac174f2Safresh1
834eac174f2Safresh1    ok my $x = new Compress::Raw::Zlib::Deflate ( -AppendOutput => 1 );
835eac174f2Safresh1
836eac174f2Safresh1    cmp_ok $x->deflate($data, $X), '==',  Z_OK ;
837eac174f2Safresh1
838eac174f2Safresh1    cmp_ok $x->flush($X), '==', Z_OK ;
839eac174f2Safresh1
840eac174f2Safresh1    ok my $k = new Compress::Raw::Zlib::Inflate ( -AppendOutput => 1,
841eac174f2Safresh1                                             -ConsumeInput => 1 ) ;
842eac174f2Safresh1    cmp_ok $k->inflate($X, $Z), '==', Z_STREAM_END ;
843eac174f2Safresh1
844eac174f2Safresh1    is $Z, $hello ;
845eac174f2Safresh1}
846eac174f2Safresh1
847eac174f2Safresh1
848eac174f2Safresh1{
849eac174f2Safresh1    title 'RT#132734: test flush append OOK output parameter';
850eac174f2Safresh1    # https://github.com/pmqs/Compress-Raw-Zlib/issues/3
851eac174f2Safresh1
852eac174f2Safresh1    my $hello = "I am a HAL 9000 computer" ;
853eac174f2Safresh1    my $data = $hello ;
854eac174f2Safresh1
855eac174f2Safresh1    my($X, $Z);
856eac174f2Safresh1
857eac174f2Safresh1    my $F = 'prev. ' ;
858eac174f2Safresh1    substr($F, 0, 6, ''); # chop off all characters using offset
859eac174f2Safresh1    cmp_ok $F, 'eq', '' ;
860eac174f2Safresh1
861eac174f2Safresh1    # use Devel::Peek ; Dump($F) ; # shows OOK flag
862eac174f2Safresh1
863eac174f2Safresh1    ok my $x = new Compress::Raw::Zlib::Deflate ( -AppendOutput => 1 );
864eac174f2Safresh1
865eac174f2Safresh1    cmp_ok $x->deflate($data, $X), '==',  Z_OK ;
866eac174f2Safresh1
867eac174f2Safresh1    cmp_ok $x->flush($F), '==', Z_OK ;
868eac174f2Safresh1
869eac174f2Safresh1    ok my $k = new Compress::Raw::Zlib::Inflate ( -AppendOutput => 1,
870eac174f2Safresh1                                             -ConsumeInput => 1 ) ;
871eac174f2Safresh1    cmp_ok $k->inflate($X . $F, $Z), '==', Z_STREAM_END ;
872eac174f2Safresh1
873eac174f2Safresh1    is $Z, $hello ;
874eac174f2Safresh1}
875eac174f2Safresh1
876898184e3SsthenSKIP:
877b39c5158Smillert{
878898184e3Ssthen    skip "InflateScan needs zlib 1.2.1 or better, you have $Zlib_ver", 1
879898184e3Ssthen        if ZLIB_VERNUM() < 0x1210 ;
880898184e3Ssthen
881b39c5158Smillert    # regression - check that resetLastBlockByte can cope with a NULL
882b39c5158Smillert    # pointer.
883b39c5158Smillert    Compress::Raw::Zlib::InflateScan->new->resetLastBlockByte(undef);
884b39c5158Smillert    ok 1, "resetLastBlockByte(undef) is ok" ;
885b39c5158Smillert}
886b39c5158Smillert
887898184e3SsthenSKIP:
888b39c5158Smillert{
889b39c5158Smillert
890b39c5158Smillert    title "gzip mode";
891b39c5158Smillert    # ================
892b39c5158Smillert
893898184e3Ssthen    skip "gzip mode needs zlib 1.2.1 or better, you have $Zlib_ver", 13
894898184e3Ssthen        if ZLIB_VERNUM() < 0x1210 ;
895898184e3Ssthen
896b39c5158Smillert    my $hello = "I am a HAL 9000 computer" ;
897b39c5158Smillert    my @hello = split('', $hello) ;
898b39c5158Smillert    my ($err, $x, $X, $status);
899b39c5158Smillert
900b39c5158Smillert    ok( ($x, $err) = new Compress::Raw::Zlib::Deflate (
901b39c5158Smillert            WindowBits => WANT_GZIP ,
902b39c5158Smillert            AppendOutput => 1
903b39c5158Smillert        ), "Create deflate object" );
904b39c5158Smillert    ok $x, "Compress::Raw::Zlib::Deflate ok" ;
905b39c5158Smillert    cmp_ok $err, '==', Z_OK, "status is Z_OK" ;
906b39c5158Smillert
907b39c5158Smillert    $status = $x->deflate($hello, $X) ;
908b39c5158Smillert    cmp_ok $status, '==', Z_OK, "deflate returned Z_OK" ;
909b39c5158Smillert
910b39c5158Smillert    cmp_ok  $x->flush($X), '==', Z_OK, "flush returned Z_OK" ;
911b39c5158Smillert
912b39c5158Smillert    my ($k, $GOT);
913b39c5158Smillert    ($k, $err) = new Compress::Raw::Zlib::Inflate(
914b39c5158Smillert            WindowBits => WANT_GZIP ,
915b39c5158Smillert            ConsumeInput => 0 ,
916b39c5158Smillert            AppendOutput => 1);
917b39c5158Smillert    ok $k, "Compress::Raw::Zlib::Inflate WANT_GZIP ok" ;
918b39c5158Smillert    cmp_ok $err, '==', Z_OK, "status is Z_OK" ;
919b39c5158Smillert
920b39c5158Smillert    $status = $k->inflate($X, $GOT) ;
921b39c5158Smillert    cmp_ok $status, '==', Z_STREAM_END, "Got Z_STREAM_END" ;
922b39c5158Smillert    is $GOT, $hello, "uncompressed data matches ok" ;
923b39c5158Smillert
924b39c5158Smillert    $GOT = '';
925b39c5158Smillert    ($k, $err) = new Compress::Raw::Zlib::Inflate(
926b39c5158Smillert            WindowBits => WANT_GZIP_OR_ZLIB ,
927b39c5158Smillert            AppendOutput => 1);
928b39c5158Smillert    ok $k, "Compress::Raw::Zlib::Inflate WANT_GZIP_OR_ZLIB ok" ;
929b39c5158Smillert    cmp_ok $err, '==', Z_OK, "status is Z_OK" ;
930b39c5158Smillert
931b39c5158Smillert    $status = $k->inflate($X, $GOT) ;
932b39c5158Smillert    cmp_ok $status, '==', Z_STREAM_END, "Got Z_STREAM_END" ;
933b39c5158Smillert    is $GOT, $hello, "uncompressed data matches ok" ;
934b39c5158Smillert}
935b39c5158Smillert
936898184e3SsthenSKIP:
937b39c5158Smillert{
938b39c5158Smillert
939b39c5158Smillert    title "gzip error mode";
940b39c5158Smillert    # Create gzip -
941b39c5158Smillert    # read with no special windowbits setting - this will fail
942b39c5158Smillert    # then read with WANT_GZIP_OR_ZLIB - thi swill work
943b39c5158Smillert    # ================
944b39c5158Smillert
945898184e3Ssthen    skip "gzip mode needs zlib 1.2.1 or better, you have $Zlib_ver", 12
946898184e3Ssthen        if ZLIB_VERNUM() < 0x1210 ;
947898184e3Ssthen
948b39c5158Smillert    my $hello = "I am a HAL 9000 computer" ;
949b39c5158Smillert    my ($err, $x, $X, $status);
950b39c5158Smillert
951b39c5158Smillert    ok( ($x, $err) = new Compress::Raw::Zlib::Deflate (
952b39c5158Smillert            WindowBits => WANT_GZIP ,
953b39c5158Smillert            AppendOutput => 1
954b39c5158Smillert        ), "Create deflate object" );
955b39c5158Smillert    ok $x, "Compress::Raw::Zlib::Deflate ok" ;
956b39c5158Smillert    cmp_ok $err, '==', Z_OK, "status is Z_OK" ;
957b39c5158Smillert
958b39c5158Smillert    $status = $x->deflate($hello, $X) ;
959b39c5158Smillert    cmp_ok $status, '==', Z_OK, "deflate returned Z_OK" ;
960b39c5158Smillert
961b39c5158Smillert    cmp_ok  $x->flush($X), '==', Z_OK, "flush returned Z_OK" ;
962b39c5158Smillert
963b39c5158Smillert    my ($k, $GOT);
964b39c5158Smillert    ($k, $err) = new Compress::Raw::Zlib::Inflate(
965b39c5158Smillert            WindowBits => MAX_WBITS ,
966b39c5158Smillert            ConsumeInput => 0 ,
967b39c5158Smillert            AppendOutput => 1);
968b39c5158Smillert    ok $k, "Compress::Raw::Zlib::Inflate WANT_GZIP ok" ;
969b39c5158Smillert    cmp_ok $err, '==', Z_OK, "status is Z_OK" ;
970b39c5158Smillert
971b39c5158Smillert    $status = $k->inflate($X, $GOT) ;
972b39c5158Smillert    cmp_ok $status, '==', Z_DATA_ERROR, "Got Z_DATA_ERROR" ;
973b39c5158Smillert
974b39c5158Smillert    $GOT = '';
975b39c5158Smillert    ($k, $err) = new Compress::Raw::Zlib::Inflate(
976b39c5158Smillert            WindowBits => WANT_GZIP_OR_ZLIB ,
977b39c5158Smillert            AppendOutput => 1);
978b39c5158Smillert    ok $k, "Compress::Raw::Zlib::Inflate WANT_GZIP_OR_ZLIB ok" ;
979b39c5158Smillert    cmp_ok $err, '==', Z_OK, "status is Z_OK" ;
980b39c5158Smillert
981b39c5158Smillert    $status = $k->inflate($X, $GOT) ;
982b39c5158Smillert    cmp_ok $status, '==', Z_STREAM_END, "Got Z_STREAM_END" ;
983b39c5158Smillert    is $GOT, $hello, "uncompressed data matches ok" ;
984b39c5158Smillert}
985b39c5158Smillert
986898184e3SsthenSKIP:
987b39c5158Smillert{
988b39c5158Smillert    title "gzip/zlib error mode";
989b39c5158Smillert    # Create zlib -
990b39c5158Smillert    # read with no WANT_GZIP windowbits setting - this will fail
991b39c5158Smillert    # then read with WANT_GZIP_OR_ZLIB - thi swill work
992b39c5158Smillert    # ================
993b39c5158Smillert
994898184e3Ssthen    skip "gzip mode needs zlib 1.2.1 or better, you have $Zlib_ver", 12
995898184e3Ssthen        if ZLIB_VERNUM() < 0x1210 ;
996898184e3Ssthen
997b39c5158Smillert    my $hello = "I am a HAL 9000 computer" ;
998b39c5158Smillert    my ($err, $x, $X, $status);
999b39c5158Smillert
1000b39c5158Smillert    ok( ($x, $err) = new Compress::Raw::Zlib::Deflate (
1001b39c5158Smillert            AppendOutput => 1
1002b39c5158Smillert        ), "Create deflate object" );
1003b39c5158Smillert    ok $x, "Compress::Raw::Zlib::Deflate ok" ;
1004b39c5158Smillert    cmp_ok $err, '==', Z_OK, "status is Z_OK" ;
1005b39c5158Smillert
1006b39c5158Smillert    $status = $x->deflate($hello, $X) ;
1007b39c5158Smillert    cmp_ok $status, '==', Z_OK, "deflate returned Z_OK" ;
1008b39c5158Smillert
1009b39c5158Smillert    cmp_ok  $x->flush($X), '==', Z_OK, "flush returned Z_OK" ;
1010b39c5158Smillert
1011b39c5158Smillert    my ($k, $GOT);
1012b39c5158Smillert    ($k, $err) = new Compress::Raw::Zlib::Inflate(
1013b39c5158Smillert            WindowBits => WANT_GZIP ,
1014b39c5158Smillert            ConsumeInput => 0 ,
1015b39c5158Smillert            AppendOutput => 1);
1016b39c5158Smillert    ok $k, "Compress::Raw::Zlib::Inflate WANT_GZIP ok" ;
1017b39c5158Smillert    cmp_ok $err, '==', Z_OK, "status is Z_OK" ;
1018b39c5158Smillert
1019b39c5158Smillert    $status = $k->inflate($X, $GOT) ;
1020b39c5158Smillert    cmp_ok $status, '==', Z_DATA_ERROR, "Got Z_DATA_ERROR" ;
1021b39c5158Smillert
1022b39c5158Smillert    $GOT = '';
1023b39c5158Smillert    ($k, $err) = new Compress::Raw::Zlib::Inflate(
1024b39c5158Smillert            WindowBits => WANT_GZIP_OR_ZLIB ,
1025b39c5158Smillert            AppendOutput => 1);
1026b39c5158Smillert    ok $k, "Compress::Raw::Zlib::Inflate WANT_GZIP_OR_ZLIB ok" ;
1027b39c5158Smillert    cmp_ok $err, '==', Z_OK, "status is Z_OK" ;
1028b39c5158Smillert
1029b39c5158Smillert    $status = $k->inflate($X, $GOT) ;
1030b39c5158Smillert    cmp_ok $status, '==', Z_STREAM_END, "Got Z_STREAM_END" ;
1031b39c5158Smillert    is $GOT, $hello, "uncompressed data matches ok" ;
1032b39c5158Smillert}
1033b39c5158Smillert
1034898184e3Ssthen{
1035898184e3Ssthen    title "zlibCompileFlags";
1036898184e3Ssthen
1037898184e3Ssthen    my $flags = Compress::Raw::Zlib::zlibCompileFlags;
1038898184e3Ssthen
1039fdcd7346Safresh1    if (!Compress::Raw::Zlib::is_zlibng && ZLIB_VERNUM() < 0x1210)
1040898184e3Ssthen    {
1041898184e3Ssthen        is $flags, 0, "zlibCompileFlags == 0 if < 1.2.1";
1042898184e3Ssthen    }
1043898184e3Ssthen    else
1044898184e3Ssthen    {
1045898184e3Ssthen        ok $flags, "zlibCompileFlags != 0 if < 1.2.1";
1046898184e3Ssthen    }
1047898184e3Ssthen}
1048898184e3Ssthen
1049898184e3Ssthen{
105091f110e0Safresh1    title "repeated calls to flush after some compression";
1051898184e3Ssthen
1052898184e3Ssthen    my $hello = "I am a HAL 9000 computer" ;
1053898184e3Ssthen    my ($err, $x, $X, $status);
1054898184e3Ssthen
1055898184e3Ssthen    ok( ($x, $err) = new Compress::Raw::Zlib::Deflate ( ), "Create deflate object" );
1056898184e3Ssthen    isa_ok $x, "Compress::Raw::Zlib::deflateStream" ;
1057898184e3Ssthen    cmp_ok $err, '==', Z_OK, "status is Z_OK" ;
1058898184e3Ssthen
1059898184e3Ssthen    $status = $x->deflate($hello, $X) ;
1060898184e3Ssthen    cmp_ok $status, '==', Z_OK, "deflate returned Z_OK" ;
1061898184e3Ssthen
1062898184e3Ssthen    cmp_ok  $x->flush($X, Z_SYNC_FLUSH), '==', Z_OK, "flush returned Z_OK" ;
1063898184e3Ssthen    cmp_ok  $x->flush($X, Z_SYNC_FLUSH), '==', Z_OK, "second flush returned Z_OK" ;
1064898184e3Ssthen    is $X, "", "no output from second flush";
1065898184e3Ssthen}
1066898184e3Ssthen
106791f110e0Safresh1{
106891f110e0Safresh1    title "repeated calls to flush - no compression";
106991f110e0Safresh1
107091f110e0Safresh1    my $hello = "I am a HAL 9000 computer" ;
107191f110e0Safresh1    my ($err, $x, $X, $status);
107291f110e0Safresh1
107391f110e0Safresh1    ok( ($x, $err) = new Compress::Raw::Zlib::Deflate ( ), "Create deflate object" );
107491f110e0Safresh1    isa_ok $x, "Compress::Raw::Zlib::deflateStream" ;
107591f110e0Safresh1    cmp_ok $err, '==', Z_OK, "status is Z_OK" ;
107691f110e0Safresh1
107791f110e0Safresh1    cmp_ok  $x->flush($X, Z_SYNC_FLUSH), '==', Z_OK, "flush returned Z_OK" ;
107891f110e0Safresh1    cmp_ok  $x->flush($X, Z_SYNC_FLUSH), '==', Z_OK, "second flush returned Z_OK" ;
107991f110e0Safresh1    is $X, "", "no output from second flush";
108091f110e0Safresh1}
108191f110e0Safresh1
10825759b3d2Safresh1{
10835759b3d2Safresh1    title "crc32";
10845759b3d2Safresh1
10855759b3d2Safresh1    is eval('Compress::Raw::Zlib::crc32("A" x 0x100, 0, 0x100); 0x1234'), 0x1234;
10865759b3d2Safresh1    is $@,  '';
10875759b3d2Safresh1
10885759b3d2Safresh1    is eval('Compress::Raw::Zlib::crc32("A" x 0x100, 0, 0x101); 0x1234'), undef;
10895759b3d2Safresh1    like $@,  mkErr("^Offset out of range in Compress::Raw::Zlib::crc32") ;
10905759b3d2Safresh1
10915759b3d2Safresh1}
10925759b3d2Safresh1
1093eac174f2Safresh1SKIP:
1094eac174f2Safresh1{
1095eac174f2Safresh1    title "crc32_combine";
1096eac174f2Safresh1
1097eac174f2Safresh1   skip "crc32_combine needs zlib 1.2.3 or better, you have $Zlib_ver", 1
1098eac174f2Safresh1        if ZLIB_VERNUM() < 0x1230 ;
1099eac174f2Safresh1
1100eac174f2Safresh1    my $first = "1234";
1101eac174f2Safresh1    my $second = "5678";
1102eac174f2Safresh1
1103eac174f2Safresh1    my $crc1 = Compress::Raw::Zlib::crc32($first);
1104eac174f2Safresh1    my $crc2 = Compress::Raw::Zlib::crc32($second);
1105eac174f2Safresh1
1106eac174f2Safresh1    my $composite_crc = Compress::Raw::Zlib::crc32($first . $second);
1107eac174f2Safresh1
1108eac174f2Safresh1    my $combined_crc = Compress::Raw::Zlib::crc32_combine($crc1, $crc2, length $second);
1109eac174f2Safresh1
1110eac174f2Safresh1    is $combined_crc, $composite_crc ;
1111eac174f2Safresh1}
1112eac174f2Safresh1
1113eac174f2Safresh1SKIP:
1114eac174f2Safresh1{
1115eac174f2Safresh1    title "adler32_combine";
1116eac174f2Safresh1
1117eac174f2Safresh1   skip "adler32_combine needs zlib 1.2.3 or better, you have $Zlib_ver", 1
1118eac174f2Safresh1        if ZLIB_VERNUM() < 0x1230 ;
1119eac174f2Safresh1
1120eac174f2Safresh1    my $first = "1234";
1121eac174f2Safresh1    my $second = "5678";
1122eac174f2Safresh1
1123eac174f2Safresh1    my $adler1 = Compress::Raw::Zlib::adler32($first);
1124eac174f2Safresh1    my $adler2 = Compress::Raw::Zlib::adler32($second);
1125eac174f2Safresh1
1126eac174f2Safresh1    my $composite_adler = Compress::Raw::Zlib::adler32($first . $second);
1127eac174f2Safresh1
1128eac174f2Safresh1    my $combined_adler = Compress::Raw::Zlib::adler32_combine($adler1, $adler2, length $second);
1129eac174f2Safresh1
1130eac174f2Safresh1    is $combined_adler, $composite_adler ;
1131eac174f2Safresh1}
1132eac174f2Safresh1
11335759b3d2Safresh1if (0)
11345759b3d2Safresh1{
11355759b3d2Safresh1    title "RT #122695: sync flush appending extra empty uncompressed block";
11365759b3d2Safresh1
11375759b3d2Safresh1    my $hello = "I am a HAL 9000 computer" ;
11385759b3d2Safresh1    my ($err, $x, $X, $status);
11395759b3d2Safresh1
11405759b3d2Safresh1    ok( ($x, $err) = new Compress::Raw::Zlib::Deflate ( ), "Create deflate object" );
11415759b3d2Safresh1    isa_ok $x, "Compress::Raw::Zlib::deflateStream" ;
11425759b3d2Safresh1    cmp_ok $err, '==', Z_OK, "status is Z_OK" ;
11435759b3d2Safresh1
11445759b3d2Safresh1    cmp_ok  $x->flush($X, Z_SYNC_FLUSH), '==', Z_OK, "flush returned Z_OK" ;
11455759b3d2Safresh1    cmp_ok  $x->flush($X, Z_SYNC_FLUSH), '==', Z_OK, "second flush returned Z_OK" ;
11465759b3d2Safresh1    is $X, "", "no output from second flush";
11475759b3d2Safresh1}
11485759b3d2Safresh1
1149b39c5158Smillertexit if $] < 5.006 ;
1150b39c5158Smillert
1151b39c5158Smillerttitle 'Looping Append test with substr output - substr the end of the string';
1152b39c5158Smillertforeach (1 .. 2)
1153b39c5158Smillert{
1154b39c5158Smillert
1155b39c5158Smillert    my $hello = "I am a HAL 9000 computer" ;
1156b39c5158Smillert    my @hello = split('', $hello) ;
1157b39c5158Smillert    my ($err, $x, $X, $status);
1158b39c5158Smillert
1159b39c5158Smillert    ok( ($x, $err) = new Compress::Raw::Zlib::Deflate ( -Bufsize => 1,
1160b39c5158Smillert                                            -AppendOutput => 1 ) );
1161b39c5158Smillert    ok $x ;
1162b39c5158Smillert    cmp_ok $err, '==', Z_OK ;
1163b39c5158Smillert
1164b39c5158Smillert    $X = "" ;
1165b39c5158Smillert    my $Answer = '';
1166b39c5158Smillert    foreach (@hello)
1167b39c5158Smillert    {
1168b39c5158Smillert        $status = $x->deflate($_, substr($Answer, length($Answer))) ;
1169b39c5158Smillert        last unless $status == Z_OK ;
1170b39c5158Smillert
1171b39c5158Smillert    }
1172b39c5158Smillert
1173b39c5158Smillert    cmp_ok $status, '==', Z_OK ;
1174b39c5158Smillert
1175b39c5158Smillert    cmp_ok  $x->flush(substr($Answer, length($Answer))), '==', Z_OK ;
1176b39c5158Smillert
1177b39c5158Smillert    #cmp_ok length $Answer, ">", 0 ;
1178b39c5158Smillert
1179b39c5158Smillert    my @Answer = split('', $Answer) ;
1180b39c5158Smillert
1181b39c5158Smillert
1182b39c5158Smillert    my $k;
1183b39c5158Smillert    ok(($k, $err) = new Compress::Raw::Zlib::Inflate(-AppendOutput => 1) );
1184b39c5158Smillert    ok $k ;
1185b39c5158Smillert    cmp_ok $err, '==', Z_OK ;
1186b39c5158Smillert
1187b39c5158Smillert    my $GOT = '';
1188b39c5158Smillert    my $Z;
1189b39c5158Smillert    $Z = 1 ;#x 2000 ;
1190b39c5158Smillert    foreach (@Answer)
1191b39c5158Smillert    {
1192b39c5158Smillert        $status = $k->inflate($_, substr($GOT, length($GOT))) ;
1193b39c5158Smillert        last if $status == Z_STREAM_END or $status != Z_OK ;
1194b39c5158Smillert    }
1195b39c5158Smillert
1196b39c5158Smillert    cmp_ok $status, '==', Z_STREAM_END ;
1197b39c5158Smillert    is $GOT, $hello ;
1198b39c5158Smillert
1199b39c5158Smillert}
1200b39c5158Smillert
1201b39c5158Smillerttitle 'Looping Append test with substr output - substr the complete string';
1202b39c5158Smillertforeach (1 .. 2)
1203b39c5158Smillert{
1204b39c5158Smillert
1205b39c5158Smillert    my $hello = "I am a HAL 9000 computer" ;
1206b39c5158Smillert    my @hello = split('', $hello) ;
1207b39c5158Smillert    my ($err, $x, $X, $status);
1208b39c5158Smillert
1209b39c5158Smillert    ok( ($x, $err) = new Compress::Raw::Zlib::Deflate ( -Bufsize => 1,
1210b39c5158Smillert                                            -AppendOutput => 1 ) );
1211b39c5158Smillert    ok $x ;
1212b39c5158Smillert    cmp_ok $err, '==', Z_OK ;
1213b39c5158Smillert
1214b39c5158Smillert    $X = "" ;
1215b39c5158Smillert    my $Answer = '';
1216b39c5158Smillert    foreach (@hello)
1217b39c5158Smillert    {
1218b39c5158Smillert        $status = $x->deflate($_, substr($Answer, 0)) ;
1219b39c5158Smillert        last unless $status == Z_OK ;
1220b39c5158Smillert
1221b39c5158Smillert    }
1222b39c5158Smillert
1223b39c5158Smillert    cmp_ok $status, '==', Z_OK ;
1224b39c5158Smillert
1225b39c5158Smillert    cmp_ok  $x->flush(substr($Answer, 0)), '==', Z_OK ;
1226b39c5158Smillert
1227b39c5158Smillert    my @Answer = split('', $Answer) ;
1228b39c5158Smillert
1229b39c5158Smillert    my $k;
1230b39c5158Smillert    ok(($k, $err) = new Compress::Raw::Zlib::Inflate(-AppendOutput => 1) );
1231b39c5158Smillert    ok $k ;
1232b39c5158Smillert    cmp_ok $err, '==', Z_OK ;
1233b39c5158Smillert
1234b39c5158Smillert    my $GOT = '';
1235b39c5158Smillert    my $Z;
1236b39c5158Smillert    $Z = 1 ;#x 2000 ;
1237b39c5158Smillert    foreach (@Answer)
1238b39c5158Smillert    {
1239b39c5158Smillert        $status = $k->inflate($_, substr($GOT, 0)) ;
1240b39c5158Smillert        last if $status == Z_STREAM_END or $status != Z_OK ;
1241b39c5158Smillert    }
1242b39c5158Smillert
1243b39c5158Smillert    cmp_ok $status, '==', Z_STREAM_END ;
1244b39c5158Smillert    is $GOT, $hello ;
1245b39c5158Smillert}
1246