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);
23 $VERSION = '2.000_13';
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" ) }
230 elsif ($outType eq 'handle' && ! -w $outValue)
231 { return $obj->saveErrorString(undef, "Output filehandle is not writable" ) }
235 # if ($got->parsed('Encoding')) {
236 # $obj->croakError("$class: Encode module needed to use -Encoding")
239 # my $want_encoding = $got->value('Encoding');
240 # my $encoding = find_encoding($want_encoding);
242 # $obj->croakError("$class: Encoding '$want_encoding' is not available")
245 # *$obj->{Encoding} = $encoding;
249 or $obj->croakError("${class}: " . $obj->error());
252 $obj->saveStatus(STATUS_OK) ;
257 *$obj->{Compress} = $obj->mkComp($class, $got)
260 *$obj->{UnCompSize} = new U64 ;
261 *$obj->{CompSize} = new U64 ;
263 if ( $outType eq 'buffer') {
264 ${ *$obj->{Buffer} } = ''
265 unless $appendOutput ;
268 if ($outType eq 'handle') {
269 *$obj->{FH} = $outValue ;
270 setBinModeOutput(*$obj->{FH}) ;
272 *$obj->{Handle} = 1 ;
275 seek(*$obj->{FH}, 0, SEEK_END)
276 or return $obj->saveErrorString(undef, "Cannot seek to end of output filehandle: $!", $!) ;
280 elsif ($outType eq 'filename') {
284 *$obj->{FH} = new IO::File "$mode $outValue"
285 or return $obj->saveErrorString(undef, "cannot open file '$outValue': $!", $!) ;
286 *$obj->{StdIO} = ($outValue eq '-');
287 setBinModeOutput(*$obj->{FH}) ;
291 *$obj->{Header} = $obj->mkHeader($got) ;
292 $obj->output( *$obj->{Header} )
297 *$obj->{Compress} = $obj->createMerge($outValue, $outType)
301 *$obj->{Closed} = 0 ;
302 *$obj->{AutoClose} = $got->value('AutoClose') ;
303 *$obj->{Output} = $outValue;
304 *$obj->{ClassName} = $class;
306 *$obj->{OneShot} = 0 ;
315 my $outType = whatIsOutput($_[0]);
317 $self->croakError("$from: output parameter not a filename, filehandle or scalar ref")
320 $self->croakError("$from: output filename is undef or null string")
321 if $outType eq 'filename' && (! defined $_[0] || $_[0] eq '') ;
323 $self->croakError("$from: output buffer is read-only")
324 if $outType eq 'buffer' && readonly(${ $_[0] });
334 my $class= (caller)[0] ;
335 my $name = (caller(1))[3] ;
337 $obj->croakError("$name: expected at least 1 parameters\n")
344 my $x = new Validator($class, *$obj->{Error}, $name, $input, $output)
347 push @_, $output if $haveOut && $x->{Hash};
349 *$obj->{OneShot} = 1 ;
351 my $got = $obj->checkParams($name, undef, @_)
358 # while (my($k, $v) = each %$input)
361 # unless defined $v ;
363 # $obj->_singleTarget($x, 1, $k, $v, @_)
367 # return keys %$input ;
373 foreach my $pair (@{ $x->{Pairs} })
375 my ($from, $to) = @$pair ;
376 $obj->_singleTarget($x, 1, $from, $to, @_)
380 return scalar @{ $x->{Pairs} } ;
383 if (! $x->{oneOutput} )
385 my $inFile = ($x->{inType} eq 'filenames'
386 || $x->{inType} eq 'filename');
388 $x->{inType} = $inFile ? 'filename' : 'buffer';
390 foreach my $in ($x->{oneInput} ? $input : @$input)
395 $obj->_singleTarget($x, $inFile, $in, \$out, @_)
398 push @$output, \$out ;
399 #if ($x->{outType} eq 'array')
400 # { push @$output, \$out }
402 # { $output->{$in} = \$out }
408 # finally the 1 to 1 and n to 1
409 return $obj->_singleTarget($x, 1, $input, $output, @_);
411 croak "should not be here" ;
418 my $inputIsFilename = shift;
423 $obj->getFileInfo($x->{Got}, $input)
424 if isaFilename($input) and $inputIsFilename ;
426 my $z = $obj->_create($x->{Got}, @_)
430 defined $z->_wr2($input, $inputIsFilename)
431 or return $z->closeError(undef) ;
438 my $inputIsFilename = ($x->{inType} ne 'array');
439 my $keep = $x->{Got}->clone();
441 #for my $element ( ($x->{inType} eq 'hash') ? keys %$input : @$input)
442 for my $element ( @$input)
444 my $isFilename = isaFilename($element);
446 if ( $afterFirst ++ )
448 defined addInterStream($obj, $element, $isFilename)
449 or return $obj->closeError(undef) ;
453 $obj->getFileInfo($x->{Got}, $element)
456 $obj->_create($x->{Got}, @_)
460 defined $obj->_wr2($element, $isFilename)
461 or return $obj->closeError(undef) ;
463 *$obj->{Got} = $keep->clone();
465 return $obj->close() ;
475 my $inputIsFilename = shift;
477 my $input = $source ;
478 if (! $inputIsFilename)
484 if ( ref $input && ref $input eq 'SCALAR' )
486 return $self->syswrite($input, @_) ;
489 if ( ! ref $input || isaFilehandle($input))
491 my $isFilehandle = isaFilehandle($input) ;
495 if ( ! $isFilehandle )
497 $fh = new IO::File "<$input"
498 or return $self->saveErrorString(undef, "cannot open file '$input': $!", $!) ;
500 binmode $fh if *$self->{Got}->valueOrDefault('BinModeIn') ;
505 while (($status = read($fh, $buff, 16 * 1024)) > 0) {
506 $count += length $buff;
507 defined $self->syswrite($buff, @_)
511 return $self->saveErrorString(undef, $!, $!)
514 if ( (!$isFilehandle || *$self->{AutoClose}) && $input ne '-')
523 croak "Should not be here";
531 my $inputIsFilename = shift ;
533 if (*$self->{Got}->value('MultiStream'))
535 $self->getFileInfo(*$self->{Got}, $input)
536 #if isaFilename($input) and $inputIsFilename ;
537 if isaFilename($input) ;
539 # TODO -- newStream needs to allow gzip/zip header to be modified
540 return $self->newStream();
542 elsif (*$self->{Got}->value('AutoFlush'))
544 #return $self->flush(Z_FULL_FLUSH);
552 return $_[0] if ref($_[0]);
567 # TODO - memory leak with 5.8.0 - this isn't called until
576 sub filterUncompressed
586 $self->croakError( *$self->{ClassName} . "::write: not a scalar reference" )
587 unless ref $_[0] eq 'SCALAR' ;
596 my $slen = defined $$buffer ? length($$buffer) : 0;
599 $len = $_[1] if $_[1] < $len;
602 $offset = $_[2] || 0;
603 $self->croakError(*$self->{ClassName} . "::write: offset outside string")
607 $self->croakError( *$self->{ClassName} . "::write: offset outside string") if $offset < 0;
609 my $rem = $slen - $offset;
610 $len = $rem if $rem < $len;
613 $buffer = \substr($$buffer, $offset, $len) ;
616 return 0 if ! defined $$buffer || length $$buffer == 0 ;
618 my $buffer_length = defined $$buffer ? length($$buffer) : 0 ;
619 *$self->{UnCompSize}->add($buffer_length) ;
621 $self->filterUncompressed($buffer);
623 # if (*$self->{Encoding}) {
624 # $$buffer = *$self->{Encoding}->encode($$buffer);
628 my $status = *$self->{Compress}->compr($buffer, $outBuffer) ;
630 return $self->saveErrorString(undef, *$self->{Compress}{Error},
631 *$self->{Compress}{ErrorNo})
632 if $status == STATUS_ERROR;
634 *$self->{CompSize}->add(length $outBuffer) ;
636 $self->output($outBuffer)
639 return $buffer_length;
647 # $self = *$self{GLOB} ;
652 defined $self->syswrite(join($,, @_) . $\);
654 defined $self->syswrite(join("", @_) . $\);
658 defined $self->syswrite(join($,, @_));
660 defined $self->syswrite(join("", @_));
669 defined $self->syswrite(sprintf($fmt, @_));
679 my $status = *$self->{Compress}->flush($outBuffer, @_) ;
680 return $self->saveErrorString(0, *$self->{Compress}{Error},
681 *$self->{Compress}{ErrorNo})
682 if $status == STATUS_ERROR;
684 if ( defined *$self->{FH} ) {
685 *$self->{FH}->clearerr();
688 *$self->{CompSize}->add(length $outBuffer) ;
690 $self->output($outBuffer)
693 if ( defined *$self->{FH} ) {
694 defined *$self->{FH}->flush()
695 or return $self->saveErrorString(0, $!, $!);
705 $self->_writeTrailer()
708 my $got = $self->checkParams('newStream', *$self->{Got}, @_)
711 $self->ckParams($got)
712 or $self->croakError("newStream: $self->{Error}");
714 *$self->{Header} = $self->mkHeader($got) ;
715 $self->output(*$self->{Header} )
718 my $status = $self->reset() ;
719 return $self->saveErrorString(0, *$self->{Compress}{Error},
720 *$self->{Compress}{ErrorNo})
721 if $status == STATUS_ERROR;
723 *$self->{UnCompSize}->reset();
724 *$self->{CompSize}->reset();
732 return *$self->{Compress}->reset() ;
741 my $status = *$self->{Compress}->close($trailer) ;
742 return $self->saveErrorString(0, *$self->{Compress}{Error}, *$self->{Compress}{ErrorNo})
743 if $status == STATUS_ERROR;
745 *$self->{CompSize}->add(length $trailer) ;
747 $trailer .= $self->mkTrailer();
751 return $self->output($trailer);
754 sub _writeFinalTrailer
758 return $self->output($self->mkFinalTrailer());
765 return 1 if *$self->{Closed} || ! *$self->{Compress} ;
766 *$self->{Closed} = 1 ;
771 $self->_writeTrailer()
774 $self->_writeFinalTrailer()
777 $self->output( "", 1 )
780 if (defined *$self->{FH}) {
782 #if (! *$self->{Handle} || *$self->{AutoClose}) {
783 if ((! *$self->{Handle} || *$self->{AutoClose}) && ! *$self->{StdIO}) {
785 *$self->{FH}->close()
786 or return $self->saveErrorString(0, $!, $!);
788 delete *$self->{FH} ;
789 # This delete can set $! in older Perls, so reset the errno
804 # return *$self->{Compress}->crc32() ;
810 # return *$self->{Compress}->msg() ;
816 # return *$self->{Compress}->dict_adler() ;
822 # return *$self->{Compress}->get_Level() ;
828 # return *$self->{Compress}->get_Strategy() ;
836 return *$self->{UnCompSize}->get32bit() ;
843 return *$self->{Closed} ;
850 my $position = shift;
853 my $here = $self->tell() ;
856 #use IO::Handle qw(SEEK_SET SEEK_CUR SEEK_END);
859 if ($whence == IO::Handle::SEEK_SET) {
860 $target = $position ;
862 elsif ($whence == IO::Handle::SEEK_CUR || $whence == IO::Handle::SEEK_END) {
863 $target = $here + $position ;
866 $self->croakError(*$self->{ClassName} . "::seek: unknown value, $whence, for whence parameter");
869 # short circuit if seeking to current offset
870 return 1 if $target == $here ;
872 # Outlaw any attempt to seek backwards
873 $self->croakError(*$self->{ClassName} . "::seek: cannot seek backwards")
876 # Walk the file to the new offset
877 my $offset = $target - $here ;
880 defined $self->syswrite("\x00" x $offset)
890 # return defined *$self->{FH}
891 # ? binmode *$self->{FH}
898 return defined *$self->{FH}
899 ? *$self->{FH}->fileno()
906 return ! *$self->{Closed} ;
912 return defined *$self->{FH}
913 ? *$self->{FH}->autoflush(@_)
917 sub input_line_number
926 return sub { croak "$name Not Available: File opened only for output" ; } ;
929 *read = _notAvailable('read');
930 *READ = _notAvailable('read');
931 *readline = _notAvailable('readline');
932 *READLINE = _notAvailable('readline');
933 *getc = _notAvailable('getc');
934 *GETC = _notAvailable('getc');
945 *BINMODE = \&binmode;
947 #*sysread = \&_notAvailable;
948 #*syswrite = \&_write;
957 IO::Compress::Base - Base Class for IO::Compress modules
962 use IO::Compress::Base ;
967 This module is not intended for direct use in application code. Its sole
968 purpose if to to be sub-classed by IO::Compress modules.
975 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::Uncompress::AnyInflate>, L<IO::Uncompress::AnyUncompress>
977 L<Compress::Zlib::FAQ|Compress::Zlib::FAQ>
979 L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>,
980 L<Archive::Tar|Archive::Tar>,
989 This module was written by Paul Marquess, F<pmqs@cpan.org>.
993 =head1 MODIFICATION HISTORY
995 See the Changes file.
997 =head1 COPYRIGHT AND LICENSE
999 Copyright (c) 2005-2006 Paul Marquess. All rights reserved.
1001 This program is free software; you can redistribute it and/or
1002 modify it under the same terms as Perl itself.