2 package IO::Compress::Base ;
9 use IO::Compress::Base::Common;
12 use Scalar::Util qw(blessed readonly);
20 our (@ISA, $VERSION, $got_encode);
21 #@ISA = qw(Exporter IO::File);
25 #Can't locate object method "SWASHNEW" via package "utf8" (perhaps you forgot to load "utf8"?) at .../ext/Compress-Zlib/Gzip/blib/lib/Compress/Zlib/Common.pm line 16.
31 # Encode->import('encode', 'find_encoding');
34 #$got_encode = 1 unless $@;
39 ${ *$self->{ErrorNo} } = shift() + 0 ;
40 ${ *$self->{Error} } = '' ;
42 return ${ *$self->{ErrorNo} } ;
50 ${ *$self->{Error} } = shift ;
51 ${ *$self->{ErrorNo} } = shift() + 0 if @_ ;
59 $self->saveErrorString(0, $_[0]);
68 my $errno = *$self->{ErrorNo};
69 my $error = ${ *$self->{Error} };
73 *$self->{ErrorNo} = $errno ;
74 ${ *$self->{Error} } = $error ;
84 return ${ *$self->{Error} } ;
90 return ${ *$self->{ErrorNo} } ;
100 if (defined *$self->{FH}) {
101 my $here = tell(*$self->{FH});
102 return $self->saveErrorString(undef, "Cannot seek to end of output filehandle: $!", $!)
104 seek(*$self->{FH}, $offset, SEEK_SET)
105 or return $self->saveErrorString(undef, "Cannot seek to end of output filehandle: $!", $!) ;
106 defined *$self->{FH}->write($data, length $data)
107 or return $self->saveErrorString(undef, $!, $!) ;
108 seek(*$self->{FH}, $here, SEEK_SET)
109 or return $self->saveErrorString(undef, "Cannot seek to end of output filehandle: $!", $!) ;
112 substr(${ *$self->{Buffer} }, $offset, length($data)) = $data ;
125 if length $data == 0 && ! $last ;
127 if ( *$self->{FilterEnvelope} ) {
129 &{ *$self->{FilterEnvelope} }();
132 if ( defined *$self->{FH} ) {
133 defined *$self->{FH}->write( $data, length $data )
134 or return $self->saveErrorString(0, $!, $!);
137 ${ *$self->{Buffer} } .= $data ;
145 return ( 'MultiStream' => [1, 1, Parse_boolean, 1],
154 my $got = shift || IO::Compress::Base::Parameters::new();
159 'AutoClose' => [1, 1, Parse_boolean, 0],
160 #'Encoding' => [1, 1, Parse_any, undef],
161 'Strict' => [0, 1, Parse_boolean, 1],
162 'Append' => [1, 1, Parse_boolean, 0],
163 'BinModeIn' => [1, 1, Parse_boolean, 0],
165 'FilterEnvelope' => [1, 1, Parse_any, undef],
167 $self->getExtraParams(),
168 *$self->{OneShot} ? $self->getOneShotParams()
171 @_) or $self->croakError("${class}: $got->{Error}") ;
181 *$obj->{Closed} = 1 ;
183 my $class = ref $obj;
184 $obj->croakError("$class: Missing Output parameter")
187 my $outValue = shift ;
193 $got = $obj->checkParams($class, undef, @_)
197 my $lax = ! $got->value('Strict') ;
199 my $outType = whatIsOutput($outValue);
201 $obj->ckOutputParam($class, $outValue)
204 if ($outType eq 'buffer') {
205 *$obj->{Buffer} = $outValue;
209 *$obj->{Buffer} = \$buff ;
212 # Merge implies Append
213 my $merge = $got->value('Merge') ;
214 my $appendOutput = $got->value('Append') || $merge ;
215 *$obj->{Append} = $appendOutput;
216 *$obj->{FilterEnvelope} = $got->value('FilterEnvelope') ;
220 # Switch off Merge mode if output file/buffer is empty/doesn't exist
221 if (($outType eq 'buffer' && length $$outValue == 0 ) ||
222 ($outType ne 'buffer' && (! -e $outValue || (-w _ && -z _))) )
226 # If output is a file, check that it is writable
227 if ($outType eq 'filename' && -e $outValue && ! -w _)
228 { return $obj->saveErrorString(undef, "Output file '$outValue' is not writable" ) }
233 # if ($got->parsed('Encoding')) {
234 # $obj->croakError("$class: Encode module needed to use -Encoding")
237 # my $want_encoding = $got->value('Encoding');
238 # my $encoding = find_encoding($want_encoding);
240 # $obj->croakError("$class: Encoding '$want_encoding' is not available")
243 # *$obj->{Encoding} = $encoding;
247 or $obj->croakError("${class}: " . $obj->error());
250 $obj->saveStatus(STATUS_OK) ;
255 *$obj->{Compress} = $obj->mkComp($class, $got)
258 *$obj->{UnCompSize} = new U64 ;
259 *$obj->{CompSize} = new U64 ;
261 if ( $outType eq 'buffer') {
262 ${ *$obj->{Buffer} } = ''
263 unless $appendOutput ;
266 if ($outType eq 'handle') {
267 *$obj->{FH} = $outValue ;
268 setBinModeOutput(*$obj->{FH}) ;
270 *$obj->{Handle} = 1 ;
273 seek(*$obj->{FH}, 0, SEEK_END)
274 or return $obj->saveErrorString(undef, "Cannot seek to end of output filehandle: $!", $!) ;
278 elsif ($outType eq 'filename') {
282 *$obj->{FH} = new IO::File "$mode $outValue"
283 or return $obj->saveErrorString(undef, "cannot open file '$outValue': $!", $!) ;
284 *$obj->{StdIO} = ($outValue eq '-');
285 setBinModeOutput(*$obj->{FH}) ;
289 *$obj->{Header} = $obj->mkHeader($got) ;
290 $obj->output( *$obj->{Header} )
295 *$obj->{Compress} = $obj->createMerge($outValue, $outType)
299 *$obj->{Closed} = 0 ;
300 *$obj->{AutoClose} = $got->value('AutoClose') ;
301 *$obj->{Output} = $outValue;
302 *$obj->{ClassName} = $class;
304 *$obj->{OneShot} = 0 ;
313 my $outType = whatIsOutput($_[0]);
315 $self->croakError("$from: output parameter not a filename, filehandle or scalar ref")
318 $self->croakError("$from: output filename is undef or null string")
319 if $outType eq 'filename' && (! defined $_[0] || $_[0] eq '') ;
321 $self->croakError("$from: output buffer is read-only")
322 if $outType eq 'buffer' && readonly(${ $_[0] });
332 my $class= (caller)[0] ;
333 my $name = (caller(1))[3] ;
335 $obj->croakError("$name: expected at least 1 parameters\n")
342 my $x = new Validator($class, *$obj->{Error}, $name, $input, $output)
345 push @_, $output if $haveOut && $x->{Hash};
347 *$obj->{OneShot} = 1 ;
349 my $got = $obj->checkParams($name, undef, @_)
356 # while (my($k, $v) = each %$input)
359 # unless defined $v ;
361 # $obj->_singleTarget($x, 1, $k, $v, @_)
365 # return keys %$input ;
371 foreach my $pair (@{ $x->{Pairs} })
373 my ($from, $to) = @$pair ;
374 $obj->_singleTarget($x, 1, $from, $to, @_)
378 return scalar @{ $x->{Pairs} } ;
381 if (! $x->{oneOutput} )
383 my $inFile = ($x->{inType} eq 'filenames'
384 || $x->{inType} eq 'filename');
386 $x->{inType} = $inFile ? 'filename' : 'buffer';
388 foreach my $in ($x->{oneInput} ? $input : @$input)
393 $obj->_singleTarget($x, $inFile, $in, \$out, @_)
396 push @$output, \$out ;
397 #if ($x->{outType} eq 'array')
398 # { push @$output, \$out }
400 # { $output->{$in} = \$out }
406 # finally the 1 to 1 and n to 1
407 return $obj->_singleTarget($x, 1, $input, $output, @_);
409 croak "should not be here" ;
416 my $inputIsFilename = shift;
421 $obj->getFileInfo($x->{Got}, $input)
422 if isaFilename($input) and $inputIsFilename ;
424 my $z = $obj->_create($x->{Got}, @_)
428 defined $z->_wr2($input, $inputIsFilename)
429 or return $z->closeError(undef) ;
436 my $inputIsFilename = ($x->{inType} ne 'array');
437 my $keep = $x->{Got}->clone();
439 #for my $element ( ($x->{inType} eq 'hash') ? keys %$input : @$input)
440 for my $element ( @$input)
442 my $isFilename = isaFilename($element);
444 if ( $afterFirst ++ )
446 defined addInterStream($obj, $element, $isFilename)
447 or return $obj->closeError(undef) ;
451 $obj->getFileInfo($x->{Got}, $element)
454 $obj->_create($x->{Got}, @_)
458 defined $obj->_wr2($element, $isFilename)
459 or return $obj->closeError(undef) ;
461 *$obj->{Got} = $keep->clone();
463 return $obj->close() ;
473 my $inputIsFilename = shift;
475 my $input = $source ;
476 if (! $inputIsFilename)
482 if ( ref $input && ref $input eq 'SCALAR' )
484 return $self->syswrite($input, @_) ;
487 if ( ! ref $input || isaFilehandle($input))
489 my $isFilehandle = isaFilehandle($input) ;
493 if ( ! $isFilehandle )
495 $fh = new IO::File "<$input"
496 or return $self->saveErrorString(undef, "cannot open file '$input': $!", $!) ;
498 binmode $fh if *$self->{Got}->valueOrDefault('BinModeIn') ;
503 while (($status = read($fh, $buff, 16 * 1024)) > 0) {
504 $count += length $buff;
505 defined $self->syswrite($buff, @_)
509 return $self->saveErrorString(undef, $!, $!)
512 if ( (!$isFilehandle || *$self->{AutoClose}) && $input ne '-')
521 croak "Should not be here";
529 my $inputIsFilename = shift ;
531 if (*$self->{Got}->value('MultiStream'))
533 $self->getFileInfo(*$self->{Got}, $input)
534 #if isaFilename($input) and $inputIsFilename ;
535 if isaFilename($input) ;
537 # TODO -- newStream needs to allow gzip/zip header to be modified
538 return $self->newStream();
540 elsif (*$self->{Got}->value('AutoFlush'))
542 #return $self->flush(Z_FULL_FLUSH);
554 return $_[0] if ref($_[0]);
569 # TODO - memory leak with 5.8.0 - this isn't called until
578 sub filterUncompressed
588 $self->croakError( *$self->{ClassName} . "::write: not a scalar reference" )
589 unless ref $_[0] eq 'SCALAR' ;
598 my $slen = defined $$buffer ? length($$buffer) : 0;
601 $len = $_[1] if $_[1] < $len;
604 $offset = $_[2] || 0;
605 $self->croakError(*$self->{ClassName} . "::write: offset outside string")
609 $self->croakError( *$self->{ClassName} . "::write: offset outside string") if $offset < 0;
611 my $rem = $slen - $offset;
612 $len = $rem if $rem < $len;
615 $buffer = \substr($$buffer, $offset, $len) ;
618 return 0 if ! defined $$buffer || length $$buffer == 0 ;
620 my $buffer_length = defined $$buffer ? length($$buffer) : 0 ;
621 *$self->{UnCompSize}->add($buffer_length) ;
623 $self->filterUncompressed($buffer);
625 # if (*$self->{Encoding}) {
626 # $$buffer = *$self->{Encoding}->encode($$buffer);
630 my $status = *$self->{Compress}->compr($buffer, $outBuffer) ;
632 return $self->saveErrorString(undef, *$self->{Compress}{Error},
633 *$self->{Compress}{ErrorNo})
634 if $status == STATUS_ERROR;
636 *$self->{CompSize}->add(length $outBuffer) ;
638 $self->output($outBuffer)
641 return $buffer_length;
649 # $self = *$self{GLOB} ;
654 defined $self->syswrite(join($,, @_) . $\);
656 defined $self->syswrite(join("", @_) . $\);
660 defined $self->syswrite(join($,, @_));
662 defined $self->syswrite(join("", @_));
671 defined $self->syswrite(sprintf($fmt, @_));
681 my $status = *$self->{Compress}->flush($outBuffer, @_) ;
682 return $self->saveErrorString(0, *$self->{Compress}{Error},
683 *$self->{Compress}{ErrorNo})
684 if $status == STATUS_ERROR;
686 if ( defined *$self->{FH} ) {
687 *$self->{FH}->clearerr();
690 *$self->{CompSize}->add(length $outBuffer) ;
692 $self->output($outBuffer)
695 if ( defined *$self->{FH} ) {
696 defined *$self->{FH}->flush()
697 or return $self->saveErrorString(0, $!, $!);
707 $self->_writeTrailer()
710 my $got = $self->checkParams('newStream', *$self->{Got}, @_)
713 $self->ckParams($got)
714 or $self->croakError("newStream: $self->{Error}");
716 *$self->{Header} = $self->mkHeader($got) ;
717 $self->output(*$self->{Header} )
720 my $status = $self->reset() ;
721 return $self->saveErrorString(0, *$self->{Compress}{Error},
722 *$self->{Compress}{ErrorNo})
723 if $status == STATUS_ERROR;
725 *$self->{UnCompSize}->reset();
726 *$self->{CompSize}->reset();
734 return *$self->{Compress}->reset() ;
743 my $status = *$self->{Compress}->close($trailer) ;
744 return $self->saveErrorString(0, *$self->{Compress}{Error}, *$self->{Compress}{ErrorNo})
745 if $status == STATUS_ERROR;
747 *$self->{CompSize}->add(length $trailer) ;
749 $trailer .= $self->mkTrailer();
753 return $self->output($trailer);
756 sub _writeFinalTrailer
760 return $self->output($self->mkFinalTrailer());
767 return 1 if *$self->{Closed} || ! *$self->{Compress} ;
768 *$self->{Closed} = 1 ;
773 $self->_writeTrailer()
776 $self->_writeFinalTrailer()
779 $self->output( "", 1 )
782 if (defined *$self->{FH}) {
784 #if (! *$self->{Handle} || *$self->{AutoClose}) {
785 if ((! *$self->{Handle} || *$self->{AutoClose}) && ! *$self->{StdIO}) {
787 *$self->{FH}->close()
788 or return $self->saveErrorString(0, $!, $!);
790 delete *$self->{FH} ;
791 # This delete can set $! in older Perls, so reset the errno
806 # return *$self->{Compress}->crc32() ;
812 # return *$self->{Compress}->msg() ;
818 # return *$self->{Compress}->dict_adler() ;
824 # return *$self->{Compress}->get_Level() ;
830 # return *$self->{Compress}->get_Strategy() ;
838 return *$self->{UnCompSize}->get32bit() ;
845 return *$self->{Closed} ;
852 my $position = shift;
855 my $here = $self->tell() ;
858 #use IO::Handle qw(SEEK_SET SEEK_CUR SEEK_END);
861 if ($whence == IO::Handle::SEEK_SET) {
862 $target = $position ;
864 elsif ($whence == IO::Handle::SEEK_CUR || $whence == IO::Handle::SEEK_END) {
865 $target = $here + $position ;
868 $self->croakError(*$self->{ClassName} . "::seek: unknown value, $whence, for whence parameter");
871 # short circuit if seeking to current offset
872 return 1 if $target == $here ;
874 # Outlaw any attempt to seek backwards
875 $self->croakError(*$self->{ClassName} . "::seek: cannot seek backwards")
878 # Walk the file to the new offset
879 my $offset = $target - $here ;
882 defined $self->syswrite("\x00" x $offset)
892 # return defined *$self->{FH}
893 # ? binmode *$self->{FH}
900 return defined *$self->{FH}
901 ? *$self->{FH}->fileno()
908 return ! *$self->{Closed} ;
914 return defined *$self->{FH}
915 ? *$self->{FH}->autoflush(@_)
919 sub input_line_number
928 return sub { croak "$name Not Available: File opened only for output" ; } ;
931 *read = _notAvailable('read');
932 *READ = _notAvailable('read');
933 *readline = _notAvailable('readline');
934 *READLINE = _notAvailable('readline');
935 *getc = _notAvailable('getc');
936 *GETC = _notAvailable('getc');
947 *BINMODE = \&binmode;
949 #*sysread = \&_notAvailable;
950 #*syswrite = \&_write;
959 IO::Compress::Base - Base Class for IO::Compress modules
964 use IO::Compress::Base ;
969 This module is not intended for direct use in application code. Its sole
970 purpose if to to be sub-classed by IO::Compress modules.
977 L<Compress::Zlib>, L<IO::Compress::Gzip>, L<IO::Uncompress::Gunzip>, L<IO::Compress::Deflate>, L<IO::Uncompress::Inflate>, L<IO::Compress::RawDeflate>, L<IO::Uncompress::RawInflate>, L<IO::Compress::Bzip2>, L<IO::Uncompress::Bunzip2>, L<IO::Compress::Lzop>, L<IO::Uncompress::UnLzop>, L<IO::Compress::Lzf>, L<IO::Uncompress::UnLzf>, L<IO::Uncompress::AnyInflate>, L<IO::Uncompress::AnyUncompress>
979 L<Compress::Zlib::FAQ|Compress::Zlib::FAQ>
981 L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>,
982 L<Archive::Tar|Archive::Tar>,
991 This module was written by Paul Marquess, F<pmqs@cpan.org>.
995 =head1 MODIFICATION HISTORY
997 See the Changes file.
999 =head1 COPYRIGHT AND LICENSE
1001 Copyright (c) 2005-2006 Paul Marquess. All rights reserved.
1003 This program is free software; you can redistribute it and/or
1004 modify it under the same terms as Perl itself.