xref: /openbsd-src/gnu/usr.bin/perl/cpan/Compress-Raw-Bzip2/t/compress/CompTestUtils.pm (revision 256a93a44f36679bee503f12e49566c2183f6181)
1b39c5158Smillertpackage CompTestUtils;
2b39c5158Smillert
3b39c5158Smillertpackage main ;
4b39c5158Smillert
5b39c5158Smillertuse strict ;
6b39c5158Smillertuse warnings;
7b39c5158Smillertuse bytes;
8b39c5158Smillert
9b39c5158Smillert#use lib qw(t t/compress);
10b39c5158Smillert
11b39c5158Smillertuse Carp ;
12b39c5158Smillert#use Test::More ;
13b39c5158Smillert
14b39c5158Smillert
15b39c5158Smillert
16b39c5158Smillertsub title
17b39c5158Smillert{
18b39c5158Smillert    #diag "" ;
19b39c5158Smillert    ok(1, $_[0]) ;
20b39c5158Smillert    #diag "" ;
21b39c5158Smillert}
22b39c5158Smillert
23b39c5158Smillertsub like_eval
24b39c5158Smillert{
25b39c5158Smillert    like $@, @_ ;
26b39c5158Smillert}
27b39c5158Smillert
28898184e3SsthenBEGIN {
29898184e3Ssthen    eval {
30898184e3Ssthen       require File::Temp;
31898184e3Ssthen     } ;
32898184e3Ssthen
33898184e3Ssthen}
34898184e3Ssthen
35898184e3Ssthen
36b39c5158Smillert{
37b39c5158Smillert    package LexFile ;
38b39c5158Smillert
39b39c5158Smillert    our ($index);
40b39c5158Smillert    $index = '00000';
41b39c5158Smillert
42b39c5158Smillert    sub new
43b39c5158Smillert    {
44b39c5158Smillert        my $self = shift ;
45b39c5158Smillert        foreach (@_)
46b39c5158Smillert        {
47898184e3Ssthen            Carp::croak "NO!!!!" if defined $_;
48898184e3Ssthen            # autogenerate the name if none supplied
49898184e3Ssthen            $_ = "tst" . $$ . "X" . $index ++ . ".tmp"
50b39c5158Smillert                unless defined $_;
51b39c5158Smillert        }
52b39c5158Smillert        chmod 0777, @_;
53b39c5158Smillert        for (@_) { 1 while unlink $_ } ;
54b39c5158Smillert        bless [ @_ ], $self ;
55b39c5158Smillert    }
56b39c5158Smillert
57b39c5158Smillert    sub DESTROY
58b39c5158Smillert    {
59b39c5158Smillert        my $self = shift ;
60b39c5158Smillert        chmod 0777, @{ $self } ;
61b39c5158Smillert        for (@$self) { 1 while unlink $_ } ;
62b39c5158Smillert    }
63b39c5158Smillert
64b39c5158Smillert}
65b39c5158Smillert
66b39c5158Smillert{
67b39c5158Smillert    package LexDir ;
68b39c5158Smillert
69b39c5158Smillert    use File::Path;
70898184e3Ssthen
71898184e3Ssthen    our ($index);
72898184e3Ssthen    $index = '00000';
735759b3d2Safresh1    our ($useTempFile);
745759b3d2Safresh1    our ($useTempDir);
75898184e3Ssthen
76b39c5158Smillert    sub new
77b39c5158Smillert    {
78b39c5158Smillert        my $self = shift ;
79898184e3Ssthen
80898184e3Ssthen        if ( $useTempDir)
81898184e3Ssthen        {
82898184e3Ssthen            foreach (@_)
83898184e3Ssthen            {
84898184e3Ssthen                Carp::croak "NO!!!!" if defined $_;
85898184e3Ssthen                $_ = File::Temp->newdir(DIR => '.');
86898184e3Ssthen                # Subsequent manipulations assume Unix syntax, metacharacters, etc.
87898184e3Ssthen                if ($^O eq 'VMS')
88898184e3Ssthen                {
89898184e3Ssthen                    $_->{DIRNAME} = VMS::Filespec::unixify($_->{DIRNAME});
90898184e3Ssthen                    $_->{DIRNAME} =~ s/\/$//;
91898184e3Ssthen                }
92898184e3Ssthen            }
93898184e3Ssthen            bless [ @_ ], $self ;
94898184e3Ssthen        }
95898184e3Ssthen        elsif ( $useTempFile)
96898184e3Ssthen        {
97898184e3Ssthen            foreach (@_)
98898184e3Ssthen            {
99898184e3Ssthen                Carp::croak "NO!!!!" if defined $_;
100898184e3Ssthen                $_ = File::Temp::tempdir(DIR => '.', CLEANUP => 1);
101898184e3Ssthen                # Subsequent manipulations assume Unix syntax, metacharacters, etc.
102898184e3Ssthen                if ($^O eq 'VMS')
103898184e3Ssthen                {
104898184e3Ssthen                    $_ = VMS::Filespec::unixify($_);
105898184e3Ssthen                    $_ =~ s/\/$//;
106898184e3Ssthen                }
107898184e3Ssthen            }
108898184e3Ssthen            bless [ @_ ], $self ;
109898184e3Ssthen        }
110898184e3Ssthen        else
111898184e3Ssthen        {
112898184e3Ssthen            foreach (@_)
113898184e3Ssthen            {
114898184e3Ssthen                Carp::croak "NO!!!!" if defined $_;
115898184e3Ssthen                # autogenerate the name if none supplied
116898184e3Ssthen                $_ = "tmpdir" . $$ . "X" . $index ++ . ".tmp" ;
117898184e3Ssthen            }
1185759b3d2Safresh1            foreach (@_)
1195759b3d2Safresh1            {
1205759b3d2Safresh1                rmtree $_, {verbose => 0, safe => 1}
1215759b3d2Safresh1                    if -d $_;
1225759b3d2Safresh1                mkdir $_, 0777
1235759b3d2Safresh1            }
124b39c5158Smillert            bless [ @_ ], $self ;
125b39c5158Smillert        }
126b39c5158Smillert
127898184e3Ssthen    }
128898184e3Ssthen
129b39c5158Smillert    sub DESTROY
130b39c5158Smillert    {
131898184e3Ssthen        if (! $useTempFile)
132898184e3Ssthen        {
133b39c5158Smillert            my $self = shift ;
1345759b3d2Safresh1            foreach (@$self)
1355759b3d2Safresh1            {
1365759b3d2Safresh1                rmtree $_, {verbose => 0, safe => 1}
1375759b3d2Safresh1                    if -d $_ ;
1385759b3d2Safresh1            }
139b39c5158Smillert        }
140b39c5158Smillert    }
141898184e3Ssthen}
142898184e3Ssthen
143b39c5158Smillertsub readFile
144b39c5158Smillert{
145b39c5158Smillert    my $f = shift ;
146b39c5158Smillert
147b39c5158Smillert    my @strings ;
148b39c5158Smillert
149b39c5158Smillert    if (IO::Compress::Base::Common::isaFilehandle($f))
150b39c5158Smillert    {
151b39c5158Smillert        my $pos = tell($f);
152b39c5158Smillert        seek($f, 0,0);
153b39c5158Smillert        @strings = <$f> ;
154b39c5158Smillert        seek($f, 0, $pos);
155b39c5158Smillert    }
156b39c5158Smillert    else
157b39c5158Smillert    {
158b39c5158Smillert        open (F, "<$f")
159b39c5158Smillert            or croak "Cannot open $f: $!\n" ;
160b39c5158Smillert        binmode F;
161b39c5158Smillert        @strings = <F> ;
162b39c5158Smillert        close F ;
163b39c5158Smillert    }
164b39c5158Smillert
165b39c5158Smillert    return @strings if wantarray ;
166b39c5158Smillert    return join "", @strings ;
167b39c5158Smillert}
168b39c5158Smillert
169b39c5158Smillertsub touch
170b39c5158Smillert{
171b39c5158Smillert    foreach (@_) { writeFile($_, '') }
172b39c5158Smillert}
173b39c5158Smillert
174b39c5158Smillertsub writeFile
175b39c5158Smillert{
176b39c5158Smillert    my($filename, @strings) = @_ ;
177b39c5158Smillert    1 while unlink $filename ;
178b39c5158Smillert    open (F, ">$filename")
179b39c5158Smillert        or croak "Cannot open $filename: $!\n" ;
180b39c5158Smillert    binmode F;
181b39c5158Smillert    foreach (@strings) {
182b39c5158Smillert        no warnings ;
183b39c5158Smillert        print F $_ ;
184b39c5158Smillert    }
185b39c5158Smillert    close F ;
186b39c5158Smillert}
187b39c5158Smillert
188b39c5158Smillertsub GZreadFile
189b39c5158Smillert{
190b39c5158Smillert    my ($filename) = shift ;
191b39c5158Smillert
192b39c5158Smillert    my ($uncomp) = "" ;
193b39c5158Smillert    my $line = "" ;
194b39c5158Smillert    my $fil = gzopen($filename, "rb")
195b39c5158Smillert        or croak "Cannopt open '$filename': $Compress::Zlib::gzerrno" ;
196b39c5158Smillert
197b39c5158Smillert    $uncomp .= $line
198b39c5158Smillert        while $fil->gzread($line) > 0;
199b39c5158Smillert
200b39c5158Smillert    $fil->gzclose ;
201b39c5158Smillert    return $uncomp ;
202b39c5158Smillert}
203b39c5158Smillert
204b39c5158Smillertsub hexDump
205b39c5158Smillert{
206b39c5158Smillert    my $d = shift ;
207b39c5158Smillert
208b39c5158Smillert    if (IO::Compress::Base::Common::isaFilehandle($d))
209b39c5158Smillert    {
210b39c5158Smillert        $d = readFile($d);
211b39c5158Smillert    }
212b39c5158Smillert    elsif (IO::Compress::Base::Common::isaFilename($d))
213b39c5158Smillert    {
214b39c5158Smillert        $d = readFile($d);
215b39c5158Smillert    }
216b39c5158Smillert    else
217b39c5158Smillert    {
218b39c5158Smillert        $d = $$d ;
219b39c5158Smillert    }
220b39c5158Smillert
221b39c5158Smillert    my $offset = 0 ;
222b39c5158Smillert
223b39c5158Smillert    $d = '' unless defined $d ;
224b39c5158Smillert    #while (read(STDIN, $data, 16)) {
225b39c5158Smillert    while (my $data = substr($d, 0, 16)) {
226b39c5158Smillert        substr($d, 0, 16) = '' ;
227b39c5158Smillert        printf "# %8.8lx    ", $offset;
228b39c5158Smillert        $offset += 16;
229b39c5158Smillert
230b39c5158Smillert        my @array = unpack('C*', $data);
231b39c5158Smillert        foreach (@array) {
232b39c5158Smillert            printf('%2.2x ', $_);
233b39c5158Smillert        }
234b39c5158Smillert        print "   " x (16 - @array)
235b39c5158Smillert            if @array < 16 ;
236b39c5158Smillert        $data =~ tr/\0-\37\177-\377/./;
237b39c5158Smillert        print "  $data\n";
238b39c5158Smillert    }
239b39c5158Smillert
240b39c5158Smillert}
241b39c5158Smillert
242b39c5158Smillertsub readHeaderInfo
243b39c5158Smillert{
244b39c5158Smillert    my $name = shift ;
245b39c5158Smillert    my %opts = @_ ;
246b39c5158Smillert
247b39c5158Smillert    my $string = <<EOM;
248b39c5158Smillertsome text
249b39c5158SmillertEOM
250b39c5158Smillert
251b39c5158Smillert    ok my $x = new IO::Compress::Gzip $name, %opts
252b39c5158Smillert        or diag "GzipError is $IO::Compress::Gzip::GzipError" ;
253b39c5158Smillert    ok $x->write($string) ;
254b39c5158Smillert    ok $x->close ;
255b39c5158Smillert
256b39c5158Smillert    #is GZreadFile($name), $string ;
257b39c5158Smillert
258b39c5158Smillert    ok my $gunz = new IO::Uncompress::Gunzip $name, Strict => 0
259b39c5158Smillert        or diag "GunzipError is $IO::Uncompress::Gunzip::GunzipError" ;
260b39c5158Smillert    ok my $hdr = $gunz->getHeaderInfo();
261b39c5158Smillert    my $uncomp ;
262b39c5158Smillert    ok $gunz->read($uncomp) ;
263b39c5158Smillert    ok $uncomp eq $string;
264b39c5158Smillert    ok $gunz->close ;
265b39c5158Smillert
266b39c5158Smillert    return $hdr ;
267b39c5158Smillert}
268b39c5158Smillert
269b39c5158Smillertsub cmpFile
270b39c5158Smillert{
271b39c5158Smillert    my ($filename, $uue) = @_ ;
272b39c5158Smillert    return readFile($filename) eq unpack("u", $uue) ;
273b39c5158Smillert}
274b39c5158Smillert
275b39c5158Smillert#sub isRawFormat
276b39c5158Smillert#{
277b39c5158Smillert#    my $class = shift;
278b39c5158Smillert#    # TODO -- add Lzma here?
279b39c5158Smillert#    my %raw = map { $_ => 1 } qw( RawDeflate );
280b39c5158Smillert#
281b39c5158Smillert#    return defined $raw{$class};
282b39c5158Smillert#}
283b39c5158Smillert
284b39c5158Smillert
285b39c5158Smillert
286b39c5158Smillertmy %TOP = (
287b39c5158Smillert    'IO::Uncompress::AnyInflate' => { Inverse  => 'IO::Compress::Gzip',
288b39c5158Smillert                                      Error    => 'AnyInflateError',
289b39c5158Smillert                                      TopLevel => 'anyinflate',
290b39c5158Smillert                                      Raw      => 0,
291b39c5158Smillert                            },
292b39c5158Smillert
293b39c5158Smillert    'IO::Uncompress::AnyUncompress' => { Inverse  => 'IO::Compress::Gzip',
294b39c5158Smillert                                         Error    => 'AnyUncompressError',
295b39c5158Smillert                                         TopLevel => 'anyuncompress',
296b39c5158Smillert                                         Raw      => 0,
297b39c5158Smillert                            },
298b39c5158Smillert
299b39c5158Smillert    'IO::Compress::Gzip' => { Inverse  => 'IO::Uncompress::Gunzip',
300b39c5158Smillert                              Error    => 'GzipError',
301b39c5158Smillert                              TopLevel => 'gzip',
302b39c5158Smillert                              Raw      => 0,
303b39c5158Smillert                            },
304b39c5158Smillert    'IO::Uncompress::Gunzip' => { Inverse  => 'IO::Compress::Gzip',
305b39c5158Smillert                                  Error    => 'GunzipError',
306b39c5158Smillert                                  TopLevel => 'gunzip',
307b39c5158Smillert                                  Raw      => 0,
308b39c5158Smillert                            },
309b39c5158Smillert
310b39c5158Smillert    'IO::Compress::Deflate' => { Inverse  => 'IO::Uncompress::Inflate',
311b39c5158Smillert                                 Error    => 'DeflateError',
312b39c5158Smillert                                 TopLevel => 'deflate',
313b39c5158Smillert                                 Raw      => 0,
314b39c5158Smillert                            },
315b39c5158Smillert    'IO::Uncompress::Inflate' => { Inverse  => 'IO::Compress::Deflate',
316b39c5158Smillert                                   Error    => 'InflateError',
317b39c5158Smillert                                   TopLevel => 'inflate',
318b39c5158Smillert                                   Raw      => 0,
319b39c5158Smillert                            },
320b39c5158Smillert
321b39c5158Smillert    'IO::Compress::RawDeflate' => { Inverse  => 'IO::Uncompress::RawInflate',
322b39c5158Smillert                                    Error    => 'RawDeflateError',
323b39c5158Smillert                                    TopLevel => 'rawdeflate',
324b39c5158Smillert                                    Raw      => 1,
325b39c5158Smillert                            },
326b39c5158Smillert    'IO::Uncompress::RawInflate' => { Inverse  => 'IO::Compress::RawDeflate',
327b39c5158Smillert                                      Error    => 'RawInflateError',
328b39c5158Smillert                                      TopLevel => 'rawinflate',
329b39c5158Smillert                                      Raw      => 1,
330b39c5158Smillert                            },
331b39c5158Smillert
332b39c5158Smillert    'IO::Compress::Zip' => { Inverse  => 'IO::Uncompress::Unzip',
333b39c5158Smillert                             Error    => 'ZipError',
334b39c5158Smillert                             TopLevel => 'zip',
335b39c5158Smillert                             Raw      => 0,
336b39c5158Smillert                            },
337b39c5158Smillert    'IO::Uncompress::Unzip' => { Inverse  => 'IO::Compress::Zip',
338b39c5158Smillert                                 Error    => 'UnzipError',
339b39c5158Smillert                                 TopLevel => 'unzip',
340b39c5158Smillert                                 Raw      => 0,
341b39c5158Smillert                            },
342b39c5158Smillert
343b39c5158Smillert    'IO::Compress::Bzip2' => { Inverse  => 'IO::Uncompress::Bunzip2',
344b39c5158Smillert                               Error    => 'Bzip2Error',
345b39c5158Smillert                               TopLevel => 'bzip2',
346b39c5158Smillert                               Raw      => 0,
347b39c5158Smillert                            },
348b39c5158Smillert    'IO::Uncompress::Bunzip2' => { Inverse  => 'IO::Compress::Bzip2',
349b39c5158Smillert                                   Error    => 'Bunzip2Error',
350b39c5158Smillert                                   TopLevel => 'bunzip2',
351b39c5158Smillert                                   Raw      => 0,
352b39c5158Smillert                            },
353b39c5158Smillert
354b39c5158Smillert    'IO::Compress::Lzop' => { Inverse  => 'IO::Uncompress::UnLzop',
355b39c5158Smillert                              Error    => 'LzopError',
356b39c5158Smillert                              TopLevel => 'lzop',
357b39c5158Smillert                              Raw      => 0,
358b39c5158Smillert                            },
359b39c5158Smillert    'IO::Uncompress::UnLzop' => { Inverse  => 'IO::Compress::Lzop',
360b39c5158Smillert                                  Error    => 'UnLzopError',
361b39c5158Smillert                                  TopLevel => 'unlzop',
362b39c5158Smillert                                  Raw      => 0,
363b39c5158Smillert                            },
364b39c5158Smillert
365b39c5158Smillert    'IO::Compress::Lzf' => { Inverse  => 'IO::Uncompress::UnLzf',
366b39c5158Smillert                             Error    => 'LzfError',
367b39c5158Smillert                             TopLevel => 'lzf',
368b39c5158Smillert                             Raw      => 0,
369b39c5158Smillert                            },
370b39c5158Smillert    'IO::Uncompress::UnLzf' => { Inverse  => 'IO::Compress::Lzf',
371b39c5158Smillert                                 Error    => 'UnLzfError',
372b39c5158Smillert                                 TopLevel => 'unlzf',
373b39c5158Smillert                                 Raw      => 0,
374b39c5158Smillert                            },
375b39c5158Smillert
376b39c5158Smillert    'IO::Compress::Lzma' => { Inverse  => 'IO::Uncompress::UnLzma',
377b39c5158Smillert                              Error    => 'LzmaError',
378b39c5158Smillert                              TopLevel => 'lzma',
379b39c5158Smillert                              Raw      => 1,
380b39c5158Smillert                            },
381b39c5158Smillert    'IO::Uncompress::UnLzma' => { Inverse  => 'IO::Compress::Lzma',
382b39c5158Smillert                                  Error    => 'UnLzmaError',
383b39c5158Smillert                                  TopLevel => 'unlzma',
384b39c5158Smillert                                  Raw      => 1,
385b39c5158Smillert                                },
386b39c5158Smillert
387b39c5158Smillert    'IO::Compress::Xz' => { Inverse  => 'IO::Uncompress::UnXz',
388b39c5158Smillert                            Error    => 'XzError',
389b39c5158Smillert                            TopLevel => 'xz',
390b39c5158Smillert                            Raw      => 0,
391b39c5158Smillert                          },
392b39c5158Smillert    'IO::Uncompress::UnXz' => { Inverse  => 'IO::Compress::Xz',
393b39c5158Smillert                                Error    => 'UnXzError',
394b39c5158Smillert                                TopLevel => 'unxz',
395b39c5158Smillert                                Raw      => 0,
396b39c5158Smillert                              },
397b39c5158Smillert
398*f3efcd01Safresh1    'IO::Compress::Lzip' => { Inverse  => 'IO::Uncompress::UnLzip',
399*f3efcd01Safresh1                            Error    => 'LzipError',
400*f3efcd01Safresh1                            TopLevel => 'lzip',
401*f3efcd01Safresh1                            Raw      => 0,
402*f3efcd01Safresh1                          },
403*f3efcd01Safresh1    'IO::Uncompress::UnLzip' => { Inverse  => 'IO::Compress::Lzip',
404*f3efcd01Safresh1                                Error    => 'UnLzipError',
405*f3efcd01Safresh1                                TopLevel => 'unlzip',
406*f3efcd01Safresh1                                Raw      => 0,
407*f3efcd01Safresh1                              },
408*f3efcd01Safresh1
409b39c5158Smillert    'IO::Compress::PPMd' => { Inverse  => 'IO::Uncompress::UnPPMd',
410b39c5158Smillert                              Error    => 'PPMdError',
411b39c5158Smillert                              TopLevel => 'ppmd',
412b39c5158Smillert                              Raw      => 0,
413b39c5158Smillert                            },
414b39c5158Smillert    'IO::Uncompress::UnPPMd' => { Inverse  => 'IO::Compress::PPMd',
415b39c5158Smillert                                  Error    => 'UnPPMdError',
416b39c5158Smillert                                  TopLevel => 'unppmd',
417b39c5158Smillert                                  Raw      => 0,
418b39c5158Smillert                                },
419*f3efcd01Safresh1    'IO::Compress::Zstd' => { Inverse  => 'IO::Uncompress::UnZstd',
420*f3efcd01Safresh1                              Error    => 'ZstdError',
421*f3efcd01Safresh1                              TopLevel => 'zstd',
422*f3efcd01Safresh1                              Raw      => 0,
423*f3efcd01Safresh1                            },
424*f3efcd01Safresh1    'IO::Uncompress::UnZstd' => { Inverse  => 'IO::Compress::Zstd',
425*f3efcd01Safresh1                                  Error    => 'UnZstdError',
426*f3efcd01Safresh1                                  TopLevel => 'unzstd',
427*f3efcd01Safresh1                                  Raw      => 0,
428*f3efcd01Safresh1                                },
429b39c5158Smillert
430b39c5158Smillert    'IO::Compress::DummyComp' => { Inverse  => 'IO::Uncompress::DummyUnComp',
431b39c5158Smillert                                   Error    => 'DummyCompError',
432b39c5158Smillert                                   TopLevel => 'dummycomp',
433b39c5158Smillert                                   Raw      => 0,
434b39c5158Smillert                                 },
435b39c5158Smillert    'IO::Uncompress::DummyUnComp' => { Inverse  => 'IO::Compress::DummyComp',
436b39c5158Smillert                                       Error    => 'DummyUnCompError',
437b39c5158Smillert                                       TopLevel => 'dummyunComp',
438b39c5158Smillert                                       Raw      => 0,
439b39c5158Smillert                                     },
440b39c5158Smillert);
441b39c5158Smillert
442b39c5158Smillert
443b39c5158Smillertfor my $key (keys %TOP)
444b39c5158Smillert{
445b39c5158Smillert    no strict;
446b39c5158Smillert    no warnings;
447b39c5158Smillert    $TOP{$key}{Error}    = \${ $key . '::' . $TOP{$key}{Error}    };
448b39c5158Smillert    $TOP{$key}{TopLevel} =     $key . '::' . $TOP{$key}{TopLevel}  ;
449b39c5158Smillert
450b39c5158Smillert    # Silence used once warning in really old perl
451b39c5158Smillert    my $dummy            = \${ $key . '::' . $TOP{$key}{Error}    };
452b39c5158Smillert
453b39c5158Smillert    #$TOP{$key . "::" . $TOP{$key}{TopLevel} } = $TOP{$key};
454b39c5158Smillert}
455b39c5158Smillert
456b39c5158Smillertsub uncompressBuffer
457b39c5158Smillert{
458b39c5158Smillert    my $compWith = shift ;
459b39c5158Smillert    my $buffer = shift ;
460b39c5158Smillert
461b39c5158Smillert
462b39c5158Smillert    my $out ;
463b39c5158Smillert    my $obj = $TOP{$compWith}{Inverse}->new( \$buffer, -Append => 1);
464b39c5158Smillert    1 while $obj->read($out) > 0 ;
465b39c5158Smillert    return $out ;
466b39c5158Smillert
467b39c5158Smillert}
468b39c5158Smillert
469b39c5158Smillert
470b39c5158Smillertsub getInverse
471b39c5158Smillert{
472b39c5158Smillert    my $class = shift ;
473b39c5158Smillert
474b39c5158Smillert    return $TOP{$class}{Inverse};
475b39c5158Smillert}
476b39c5158Smillert
477b39c5158Smillertsub getErrorRef
478b39c5158Smillert{
479b39c5158Smillert    my $class = shift ;
480b39c5158Smillert
481b39c5158Smillert    return $TOP{$class}{Error};
482b39c5158Smillert}
483b39c5158Smillert
484b39c5158Smillertsub getTopFuncRef
485b39c5158Smillert{
486b39c5158Smillert    my $class = shift ;
487b39c5158Smillert
488b39c5158Smillert    die "Cannot find $class"
489b39c5158Smillert        if ! defined $TOP{$class}{TopLevel};
490b39c5158Smillert    return \&{ $TOP{$class}{TopLevel} } ;
491b39c5158Smillert}
492b39c5158Smillert
493b39c5158Smillertsub getTopFuncName
494b39c5158Smillert{
495b39c5158Smillert    my $class = shift ;
496b39c5158Smillert
497b39c5158Smillert    return $TOP{$class}{TopLevel} ;
498b39c5158Smillert}
499b39c5158Smillert
500b39c5158Smillertsub compressBuffer
501b39c5158Smillert{
502b39c5158Smillert    my $compWith = shift ;
503b39c5158Smillert    my $buffer = shift ;
504b39c5158Smillert
505b39c5158Smillert
506b39c5158Smillert    my $out ;
507b39c5158Smillert    die "Cannot find $compWith"
508b39c5158Smillert        if ! defined $TOP{$compWith}{Inverse};
509b39c5158Smillert    my $obj = $TOP{$compWith}{Inverse}->new( \$out);
510b39c5158Smillert    $obj->write($buffer) ;
511b39c5158Smillert    $obj->close();
512b39c5158Smillert    return $out ;
513b39c5158Smillert}
514b39c5158Smillert
515b39c5158Smillertour ($AnyUncompressError);
516b39c5158SmillertBEGIN
517b39c5158Smillert{
518*f3efcd01Safresh1    eval ' use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError); ';
519b39c5158Smillert}
520b39c5158Smillert
521b39c5158Smillertsub anyUncompress
522b39c5158Smillert{
523b39c5158Smillert    my $buffer = shift ;
524b39c5158Smillert    my $already = shift;
525b39c5158Smillert
526b39c5158Smillert    my @opts = ();
527b39c5158Smillert    if (ref $buffer && ref $buffer eq 'ARRAY')
528b39c5158Smillert    {
529b39c5158Smillert        @opts = @$buffer;
530b39c5158Smillert        $buffer = shift @opts;
531b39c5158Smillert    }
532b39c5158Smillert
533b39c5158Smillert    if (ref $buffer)
534b39c5158Smillert    {
535b39c5158Smillert        croak "buffer is undef" unless defined $$buffer;
536b39c5158Smillert        croak "buffer is empty" unless length $$buffer;
537b39c5158Smillert
538b39c5158Smillert    }
539b39c5158Smillert
540b39c5158Smillert
541b39c5158Smillert    my $data ;
542b39c5158Smillert    if (IO::Compress::Base::Common::isaFilehandle($buffer))
543b39c5158Smillert    {
544b39c5158Smillert        $data = readFile($buffer);
545b39c5158Smillert    }
546b39c5158Smillert    elsif (IO::Compress::Base::Common::isaFilename($buffer))
547b39c5158Smillert    {
548b39c5158Smillert        $data = readFile($buffer);
549b39c5158Smillert    }
550b39c5158Smillert    else
551b39c5158Smillert    {
552b39c5158Smillert        $data = $$buffer ;
553b39c5158Smillert    }
554b39c5158Smillert
555b39c5158Smillert    if (defined $already && length $already)
556b39c5158Smillert    {
557b39c5158Smillert
558b39c5158Smillert        my $got = substr($data, 0, length($already));
559b39c5158Smillert        substr($data, 0, length($already)) = '';
560b39c5158Smillert
561b39c5158Smillert        is $got, $already, '  Already OK' ;
562b39c5158Smillert    }
563b39c5158Smillert
564b39c5158Smillert    my $out = '';
565b39c5158Smillert    my $o = new IO::Uncompress::AnyUncompress \$data,
566b39c5158Smillert                    Append => 1,
567b39c5158Smillert                    Transparent => 0,
568b39c5158Smillert                    RawInflate => 1,
569b39c5158Smillert                    UnLzma     => 1,
570b39c5158Smillert                    @opts
571b39c5158Smillert        or croak "Cannot open buffer/file: $AnyUncompressError" ;
572b39c5158Smillert
573b39c5158Smillert    1 while $o->read($out) > 0 ;
574b39c5158Smillert
575b39c5158Smillert    croak "Error uncompressing -- " . $o->error()
576b39c5158Smillert        if $o->error() ;
577b39c5158Smillert
578b39c5158Smillert    return $out ;
579b39c5158Smillert}
580b39c5158Smillert
581b39c5158Smillertsub getHeaders
582b39c5158Smillert{
583b39c5158Smillert    my $buffer = shift ;
584b39c5158Smillert    my $already = shift;
585b39c5158Smillert
586b39c5158Smillert    my @opts = ();
587b39c5158Smillert    if (ref $buffer && ref $buffer eq 'ARRAY')
588b39c5158Smillert    {
589b39c5158Smillert        @opts = @$buffer;
590b39c5158Smillert        $buffer = shift @opts;
591b39c5158Smillert    }
592b39c5158Smillert
593b39c5158Smillert    if (ref $buffer)
594b39c5158Smillert    {
595b39c5158Smillert        croak "buffer is undef" unless defined $$buffer;
596b39c5158Smillert        croak "buffer is empty" unless length $$buffer;
597b39c5158Smillert
598b39c5158Smillert    }
599b39c5158Smillert
600b39c5158Smillert
601b39c5158Smillert    my $data ;
602b39c5158Smillert    if (IO::Compress::Base::Common::isaFilehandle($buffer))
603b39c5158Smillert    {
604b39c5158Smillert        $data = readFile($buffer);
605b39c5158Smillert    }
606b39c5158Smillert    elsif (IO::Compress::Base::Common::isaFilename($buffer))
607b39c5158Smillert    {
608b39c5158Smillert        $data = readFile($buffer);
609b39c5158Smillert    }
610b39c5158Smillert    else
611b39c5158Smillert    {
612b39c5158Smillert        $data = $$buffer ;
613b39c5158Smillert    }
614b39c5158Smillert
615b39c5158Smillert    if (defined $already && length $already)
616b39c5158Smillert    {
617b39c5158Smillert
618b39c5158Smillert        my $got = substr($data, 0, length($already));
619b39c5158Smillert        substr($data, 0, length($already)) = '';
620b39c5158Smillert
621b39c5158Smillert        is $got, $already, '  Already OK' ;
622b39c5158Smillert    }
623b39c5158Smillert
624b39c5158Smillert    my $out = '';
625b39c5158Smillert    my $o = new IO::Uncompress::AnyUncompress \$data,
626b39c5158Smillert                MultiStream => 1,
627b39c5158Smillert                Append => 1,
628b39c5158Smillert                Transparent => 0,
629b39c5158Smillert                RawInflate => 1,
630b39c5158Smillert                UnLzma     => 1,
631b39c5158Smillert                @opts
632b39c5158Smillert        or croak "Cannot open buffer/file: $AnyUncompressError" ;
633b39c5158Smillert
634b39c5158Smillert    1 while $o->read($out) > 0 ;
635b39c5158Smillert
636b39c5158Smillert    croak "Error uncompressing -- " . $o->error()
637b39c5158Smillert        if $o->error() ;
638b39c5158Smillert
639b39c5158Smillert    return ($o->getHeaderInfo()) ;
640b39c5158Smillert
641b39c5158Smillert}
642b39c5158Smillert
643b39c5158Smillertsub mkComplete
644b39c5158Smillert{
645b39c5158Smillert    my $class = shift ;
646b39c5158Smillert    my $data = shift;
647b39c5158Smillert    my $Error = getErrorRef($class);
648b39c5158Smillert
649b39c5158Smillert    my $buffer ;
650b39c5158Smillert    my %params = ();
651b39c5158Smillert
652b39c5158Smillert    if ($class eq 'IO::Compress::Gzip') {
653b39c5158Smillert        %params = (
654b39c5158Smillert            Name       => "My name",
655b39c5158Smillert            Comment    => "a comment",
656b39c5158Smillert            ExtraField => ['ab' => "extra"],
657b39c5158Smillert            HeaderCRC  => 1);
658b39c5158Smillert    }
659b39c5158Smillert    elsif ($class eq 'IO::Compress::Zip'){
660b39c5158Smillert        %params = (
661b39c5158Smillert            Name              => "My name",
662b39c5158Smillert            Comment           => "a comment",
663b39c5158Smillert            ZipComment        => "last comment",
664b39c5158Smillert            exTime            => [100, 200, 300],
665b39c5158Smillert            ExtraFieldLocal   => ["ab" => "extra1"],
666b39c5158Smillert            ExtraFieldCentral => ["cd" => "extra2"],
667b39c5158Smillert        );
668b39c5158Smillert    }
669b39c5158Smillert
670b39c5158Smillert    my $z = new $class( \$buffer, %params)
671b39c5158Smillert        or croak "Cannot create $class object: $$Error";
672b39c5158Smillert    $z->write($data);
673b39c5158Smillert    $z->close();
674b39c5158Smillert
675b39c5158Smillert    my $unc = getInverse($class);
676b39c5158Smillert    anyUncompress(\$buffer) eq $data
677b39c5158Smillert        or die "bad bad bad";
678b39c5158Smillert    my $u = new $unc( \$buffer);
679b39c5158Smillert    my $info = $u->getHeaderInfo() ;
680b39c5158Smillert
681b39c5158Smillert
682b39c5158Smillert    return wantarray ? ($info, $buffer) : $buffer ;
683b39c5158Smillert}
684b39c5158Smillert
685b39c5158Smillertsub mkErr
686b39c5158Smillert{
687b39c5158Smillert    my $string = shift ;
688b39c5158Smillert    my ($dummy, $file, $line) = caller ;
689b39c5158Smillert    -- $line ;
690b39c5158Smillert
691b39c5158Smillert    $file = quotemeta($file);
692b39c5158Smillert
693b39c5158Smillert    #return "/$string\\s+at $file line $line/" if $] >= 5.006 ;
694b39c5158Smillert    return "/$string\\s+at /" ;
695b39c5158Smillert}
696b39c5158Smillert
697b39c5158Smillertsub mkEvalErr
698b39c5158Smillert{
699b39c5158Smillert    my $string = shift ;
700b39c5158Smillert
701b39c5158Smillert    #return "/$string\\s+at \\(eval /" if $] > 5.006 ;
702b39c5158Smillert    return "/$string\\s+at /" ;
703b39c5158Smillert}
704b39c5158Smillert
705b39c5158Smillertsub dumpObj
706b39c5158Smillert{
707b39c5158Smillert    my $obj = shift ;
708b39c5158Smillert
709b39c5158Smillert    my ($dummy, $file, $line) = caller ;
710b39c5158Smillert
711b39c5158Smillert    if (@_)
712b39c5158Smillert    {
713b39c5158Smillert        print "#\n# dumpOBJ from $file line $line @_\n" ;
714b39c5158Smillert    }
715b39c5158Smillert    else
716b39c5158Smillert    {
717b39c5158Smillert        print "#\n# dumpOBJ from $file line $line \n" ;
718b39c5158Smillert    }
719b39c5158Smillert
720b39c5158Smillert    my $max = 0 ;;
721b39c5158Smillert    foreach my $k (keys %{ *$obj })
722b39c5158Smillert    {
723b39c5158Smillert        $max = length $k if length $k > $max ;
724b39c5158Smillert    }
725b39c5158Smillert
726b39c5158Smillert    foreach my $k (sort keys %{ *$obj })
727b39c5158Smillert    {
728b39c5158Smillert        my $v = $obj->{$k} ;
729b39c5158Smillert        $v = '-undef-' unless defined $v;
730b39c5158Smillert        my $pad = ' ' x ($max - length($k) + 2) ;
731b39c5158Smillert        print "# $k$pad: [$v]\n";
732b39c5158Smillert    }
733b39c5158Smillert    print "#\n" ;
734b39c5158Smillert}
735b39c5158Smillert
736b39c5158Smillert
737b39c5158Smillertsub getMultiValues
738b39c5158Smillert{
739b39c5158Smillert    my $class = shift ;
740b39c5158Smillert
741*f3efcd01Safresh1    return (0,0) if $class =~ /lzf|lzma|zstd/i;
742b39c5158Smillert    return (1,0);
743b39c5158Smillert}
744b39c5158Smillert
745b39c5158Smillert
746b39c5158Smillertsub gotScalarUtilXS
747b39c5158Smillert{
748b39c5158Smillert    eval ' use Scalar::Util "dualvar" ';
749b39c5158Smillert    return $@ ? 0 : 1 ;
750b39c5158Smillert}
751b39c5158Smillert
752b39c5158Smillertpackage CompTestUtils;
753b39c5158Smillert
754b39c5158Smillert1;
755b39c5158Smillert__END__
756b39c5158Smillert	t/Test/Builder.pm
757b39c5158Smillert	t/Test/More.pm
758b39c5158Smillert	t/Test/Simple.pm
759b39c5158Smillert	t/compress/CompTestUtils.pm
760b39c5158Smillert	t/compress/any.pl
761b39c5158Smillert	t/compress/anyunc.pl
762b39c5158Smillert	t/compress/destroy.pl
763b39c5158Smillert	t/compress/generic.pl
764b39c5158Smillert	t/compress/merge.pl
765b39c5158Smillert	t/compress/multi.pl
766b39c5158Smillert	t/compress/newtied.pl
767b39c5158Smillert	t/compress/oneshot.pl
768b39c5158Smillert	t/compress/prime.pl
769b39c5158Smillert	t/compress/tied.pl
770b39c5158Smillert	t/compress/truncate.pl
771b39c5158Smillert	t/compress/zlib-generic.plParsing config.in...
772b39c5158SmillertBuilding Zlib enabled
773b39c5158SmillertAuto Detect Gzip OS Code..
774b39c5158SmillertSetting Gzip OS Code to 3 [Unix/Default]
775b39c5158SmillertLooks Good.
776