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_11';
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 ;
120 return ( 'MultiStream' => [1, 1, Parse_boolean, 1],
129 my $got = shift || IO::Compress::Base::Parameters::new();
134 'AutoClose' => [1, 1, Parse_boolean, 0],
135 #'Encoding' => [1, 1, Parse_any, undef],
136 'Strict' => [0, 1, Parse_boolean, 1],
137 'Append' => [1, 1, Parse_boolean, 0],
138 'BinModeIn' => [1, 1, Parse_boolean, 0],
140 $self->getExtraParams(),
141 *$self->{OneShot} ? $self->getOneShotParams()
144 @_) or $self->croakError("${class}: $got->{Error}") ;
154 *$obj->{Closed} = 1 ;
156 my $class = ref $obj;
157 $obj->croakError("$class: Missing Output parameter")
160 my $outValue = shift ;
166 $got = $obj->checkParams($class, undef, @_)
170 my $lax = ! $got->value('Strict') ;
172 my $outType = whatIsOutput($outValue);
174 $obj->ckOutputParam($class, $outValue)
177 if ($outType eq 'buffer') {
178 *$obj->{Buffer} = $outValue;
182 *$obj->{Buffer} = \$buff ;
185 # Merge implies Append
186 my $merge = $got->value('Merge') ;
187 my $appendOutput = $got->value('Append') || $merge ;
191 # Switch off Merge mode if output file/buffer is empty/doesn't exist
192 if (($outType eq 'buffer' && length $$outValue == 0 ) ||
193 ($outType ne 'buffer' && (! -e $outValue || (-w _ && -z _))) )
197 # If output is a file, check that it is writable
198 if ($outType eq 'filename' && -e $outValue && ! -w _)
199 { return $obj->saveErrorString(undef, "Output file '$outValue' is not writable" ) }
201 elsif ($outType eq 'handle' && ! -w $outValue)
202 { return $obj->saveErrorString(undef, "Output filehandle is not writable" ) }
206 # if ($got->parsed('Encoding')) {
207 # $obj->croakError("$class: Encode module needed to use -Encoding")
210 # my $want_encoding = $got->value('Encoding');
211 # my $encoding = find_encoding($want_encoding);
213 # $obj->croakError("$class: Encoding '$want_encoding' is not available")
216 # *$obj->{Encoding} = $encoding;
220 or $obj->croakError("${class}: " . $obj->error());
223 $obj->saveStatus(STATUS_OK) ;
228 *$obj->{Compress} = $obj->mkComp($class, $got)
231 *$obj->{BytesWritten} = 0 ;
232 *$obj->{UnCompSize_32bit} = 0 ;
234 *$obj->{Header} = $obj->mkHeader($got) ;
236 if ( $outType eq 'buffer') {
237 ${ *$obj->{Buffer} } = ''
238 unless $appendOutput ;
239 ${ *$obj->{Buffer} } .= *$obj->{Header};
242 if ($outType eq 'handle') {
243 *$obj->{FH} = $outValue ;
244 setBinModeOutput(*$obj->{FH}) ;
246 *$obj->{Handle} = 1 ;
249 seek(*$obj->{FH}, 0, SEEK_END)
250 or return $obj->saveErrorString(undef, "Cannot seek to end of output filehandle: $!", $!) ;
254 elsif ($outType eq 'filename') {
258 *$obj->{FH} = new IO::File "$mode $outValue"
259 or return $obj->saveErrorString(undef, "cannot open file '$outValue': $!", $!) ;
260 *$obj->{StdIO} = ($outValue eq '-');
261 setBinModeOutput(*$obj->{FH}) ;
265 if (length *$obj->{Header}) {
266 defined *$obj->{FH}->write(*$obj->{Header}, length(*$obj->{Header}))
267 or return $obj->saveErrorString(undef, $!, $!) ;
273 *$obj->{Compress} = $obj->createMerge($outValue, $outType)
277 *$obj->{Closed} = 0 ;
278 *$obj->{AutoClose} = $got->value('AutoClose') ;
279 *$obj->{Output} = $outValue;
280 *$obj->{ClassName} = $class;
282 *$obj->{OneShot} = 0 ;
291 my $outType = whatIsOutput($_[0]);
293 $self->croakError("$from: output parameter not a filename, filehandle or scalar ref")
296 $self->croakError("$from: output filename is undef or null string")
297 if $outType eq 'filename' && (! defined $_[0] || $_[0] eq '') ;
299 $self->croakError("$from: output buffer is read-only")
300 if $outType eq 'buffer' && readonly(${ $_[0] });
310 my $class= (caller)[0] ;
311 my $name = (caller(1))[3] ;
313 $obj->croakError("$name: expected at least 1 parameters\n")
320 my $x = new Validator($class, *$obj->{Error}, $name, $input, $output)
323 push @_, $output if $haveOut && $x->{Hash};
325 *$obj->{OneShot} = 1 ;
327 my $got = $obj->checkParams($name, undef, @_)
334 # while (my($k, $v) = each %$input)
337 # unless defined $v ;
339 # $obj->_singleTarget($x, 1, $k, $v, @_)
343 # return keys %$input ;
349 foreach my $pair (@{ $x->{Pairs} })
351 my ($from, $to) = @$pair ;
352 $obj->_singleTarget($x, 1, $from, $to, @_)
356 return scalar @{ $x->{Pairs} } ;
359 if (! $x->{oneOutput} )
361 my $inFile = ($x->{inType} eq 'filenames'
362 || $x->{inType} eq 'filename');
364 $x->{inType} = $inFile ? 'filename' : 'buffer';
366 foreach my $in ($x->{oneInput} ? $input : @$input)
371 $obj->_singleTarget($x, $inFile, $in, \$out, @_)
374 push @$output, \$out ;
375 #if ($x->{outType} eq 'array')
376 # { push @$output, \$out }
378 # { $output->{$in} = \$out }
384 # finally the 1 to 1 and n to 1
385 return $obj->_singleTarget($x, 1, $input, $output, @_);
387 croak "should not be here" ;
394 my $inputIsFilename = shift;
399 $obj->getFileInfo($x->{Got}, $input)
400 if isaFilename($input) and $inputIsFilename ;
402 my $z = $obj->_create($x->{Got}, @_)
406 defined $z->_wr2($input, $inputIsFilename)
407 or return $z->closeError(undef) ;
414 my $inputIsFilename = ($x->{inType} ne 'array');
415 my $keep = $x->{Got}->clone();
417 #for my $element ( ($x->{inType} eq 'hash') ? keys %$input : @$input)
418 for my $element ( @$input)
420 my $isFilename = isaFilename($element);
422 if ( $afterFirst ++ )
424 defined addInterStream($obj, $element, $isFilename)
425 or return $obj->closeError(undef) ;
429 $obj->getFileInfo($x->{Got}, $element)
432 $obj->_create($x->{Got}, @_)
436 defined $obj->_wr2($element, $isFilename)
437 or return $obj->closeError(undef) ;
439 *$obj->{Got} = $keep->clone();
441 return $obj->close() ;
451 my $inputIsFilename = shift;
453 my $input = $source ;
454 if (! $inputIsFilename)
460 if ( ref $input && ref $input eq 'SCALAR' )
462 return $self->syswrite($input, @_) ;
465 if ( ! ref $input || isaFilehandle($input))
467 my $isFilehandle = isaFilehandle($input) ;
471 if ( ! $isFilehandle )
473 $fh = new IO::File "<$input"
474 or return $self->saveErrorString(undef, "cannot open file '$input': $!", $!) ;
476 binmode $fh if *$self->{Got}->valueOrDefault('BinModeIn') ;
481 while (($status = read($fh, $buff, 4096)) > 0) {
482 $count += length $buff;
483 defined $self->syswrite($buff, @_)
487 return $self->saveErrorString(undef, $!, $!)
490 if ( (!$isFilehandle || *$self->{AutoClose}) && $input ne '-')
499 croak "Should not be here";
507 my $inputIsFilename = shift ;
509 if (*$self->{Got}->value('MultiStream'))
511 $self->getFileInfo(*$self->{Got}, $input)
512 #if isaFilename($input) and $inputIsFilename ;
513 if isaFilename($input) ;
515 # TODO -- newStream needs to allow gzip/zip header to be modified
516 return $self->newStream();
518 elsif (*$self->{Got}->value('AutoFlush'))
520 #return $self->flush(Z_FULL_FLUSH);
528 return $_[0] if ref($_[0]);
542 # TODO - memory leak with 5.8.0 - this isn't called until
551 sub filterUncompressed
561 $self->croakError( *$self->{ClassName} . "::write: not a scalar reference" )
562 unless ref $_[0] eq 'SCALAR' ;
571 my $slen = defined $$buffer ? length($$buffer) : 0;
574 $len = $_[1] if $_[1] < $len;
577 $offset = $_[2] || 0;
578 $self->croakError(*$self->{ClassName} . "::write: offset outside string")
582 $self->croakError( *$self->{ClassName} . "::write: offset outside string") if $offset < 0;
584 my $rem = $slen - $offset;
585 $len = $rem if $rem < $len;
588 $buffer = \substr($$buffer, $offset, $len) ;
591 return 0 if ! defined $$buffer || length $$buffer == 0 ;
593 my $buffer_length = defined $$buffer ? length($$buffer) : 0 ;
594 *$self->{BytesWritten} += $buffer_length ;
595 my $rest = 0xFFFFFFFF - *$self->{UnCompSize_32bit} ;
596 if ($buffer_length > $rest) {
597 *$self->{UnCompSize_32bit} = $buffer_length - $rest - 1;
600 *$self->{UnCompSize_32bit} += $buffer_length ;
603 $self->filterUncompressed($buffer);
605 # if (*$self->{Encoding}) {
606 # $$buffer = *$self->{Encoding}->encode($$buffer);
609 #my $length = length $$buffer;
611 my $status = *$self->{Compress}->compr($buffer, *$self->{Buffer}) ;
613 return $self->saveErrorString(undef, *$self->{Compress}{Error},
614 *$self->{Compress}{ErrorNo})
615 if $status == STATUS_ERROR;
618 if ( defined *$self->{FH} and length ${ *$self->{Buffer} }) {
619 defined *$self->{FH}->write( ${ *$self->{Buffer} }, length ${ *$self->{Buffer} } )
620 or return $self->saveErrorString(undef, $!, $!);
621 ${ *$self->{Buffer} } = '' ;
624 return $buffer_length;
632 # $self = *$self{GLOB} ;
637 defined $self->syswrite(join($,, @_) . $\);
639 defined $self->syswrite(join("", @_) . $\);
643 defined $self->syswrite(join($,, @_));
645 defined $self->syswrite(join("", @_));
654 defined $self->syswrite(sprintf($fmt, @_));
663 my $status = *$self->{Compress}->flush(*$self->{Buffer}, @_) ;
664 return $self->saveErrorString(0, *$self->{Compress}{Error},
665 *$self->{Compress}{ErrorNo})
666 if $status == STATUS_ERROR;
668 if ( defined *$self->{FH} ) {
669 *$self->{FH}->clearerr();
670 defined *$self->{FH}->write(${ *$self->{Buffer} }, length ${ *$self->{Buffer} })
671 or return $self->saveErrorString(0, $!, $!);
672 defined *$self->{FH}->flush()
673 or return $self->saveErrorString(0, $!, $!);
674 ${ *$self->{Buffer} } = '' ;
684 $self->_writeTrailer()
687 my $got = $self->checkParams('newStream', *$self->{Got}, @_)
690 $self->ckParams($got)
691 or $self->croakError("newStream: $self->{Error}");
693 *$self->{Header} = $self->mkHeader($got) ;
694 ${ *$self->{Buffer} } .= *$self->{Header} ;
696 if (defined *$self->{FH})
698 defined *$self->{FH}->write(${ *$self->{Buffer} },
699 length ${ *$self->{Buffer} })
700 or return $self->saveErrorString(0, $!, $!);
701 ${ *$self->{Buffer} } = '' ;
704 #my $status = *$self->{Compress}->reset() ;
705 my $status = $self->reset() ;
706 return $self->saveErrorString(0, *$self->{Compress}{Error},
707 *$self->{Compress}{ErrorNo})
708 if $status == STATUS_ERROR;
710 *$self->{BytesWritten} = 0 ;
711 *$self->{UnCompSize_32bit} = 0 ;
719 return *$self->{Compress}->reset() ;
726 my $status = *$self->{Compress}->close(*$self->{Buffer}) ;
727 return $self->saveErrorString(0, *$self->{Compress}{Error}, *$self->{Compress}{ErrorNo})
728 if $status == STATUS_ERROR;
730 my $trailer = $self->mkTrailer();
734 ${ *$self->{Buffer} } .= $trailer;
736 return 1 if ! defined *$self->{FH} ;
738 defined *$self->{FH}->write(${ *$self->{Buffer} }, length ${ *$self->{Buffer} })
739 or return $self->saveErrorString(0, $!, $!);
741 ${ *$self->{Buffer} } = '' ;
746 sub _writeFinalTrailer
750 ${ *$self->{Buffer} } .= $self->mkFinalTrailer();
752 return 1 if ! defined *$self->{FH} ;
754 defined *$self->{FH}->write(${ *$self->{Buffer} }, length ${ *$self->{Buffer} })
755 or return $self->saveErrorString(0, $!, $!);
757 ${ *$self->{Buffer} } = '' ;
766 return 1 if *$self->{Closed} || ! *$self->{Compress} ;
767 *$self->{Closed} = 1 ;
772 $self->_writeTrailer()
775 $self->_writeFinalTrailer()
778 if (defined *$self->{FH}) {
779 #if (! *$self->{Handle} || *$self->{AutoClose}) {
780 if ((! *$self->{Handle} || *$self->{AutoClose}) && ! *$self->{StdIO}) {
782 *$self->{FH}->close()
783 or return $self->saveErrorString(0, $!, $!);
785 delete *$self->{FH} ;
786 # This delete can set $! in older Perls, so reset the errno
801 # return *$self->{Compress}->crc32() ;
807 # return *$self->{Compress}->msg() ;
813 # return *$self->{Compress}->dict_adler() ;
819 # return *$self->{Compress}->get_Level() ;
825 # return *$self->{Compress}->get_Strategy() ;
833 return *$self->{BytesWritten} ;
840 return *$self->{Closed} ;
847 my $position = shift;
850 my $here = $self->tell() ;
853 #use IO::Handle qw(SEEK_SET SEEK_CUR SEEK_END);
856 if ($whence == IO::Handle::SEEK_SET) {
857 $target = $position ;
859 elsif ($whence == IO::Handle::SEEK_CUR || $whence == IO::Handle::SEEK_END) {
860 $target = $here + $position ;
863 $self->croakError(*$self->{ClassName} . "::seek: unknown value, $whence, for whence parameter");
866 # short circuit if seeking to current offset
867 return 1 if $target == $here ;
869 # Outlaw any attempt to seek backwards
870 $self->croakError(*$self->{ClassName} . "::seek: cannot seek backwards")
873 # Walk the file to the new offset
874 my $offset = $target - $here ;
877 defined $self->syswrite("\x00" x $offset)
887 # return defined *$self->{FH}
888 # ? binmode *$self->{FH}
895 return defined *$self->{FH}
896 ? *$self->{FH}->fileno()
903 return ! *$self->{Closed} ;
909 return defined *$self->{FH}
910 ? *$self->{FH}->autoflush(@_)
914 sub input_line_number
923 return sub { croak "$name Not Available: File opened only for output" ; } ;
926 *read = _notAvailable('read');
927 *READ = _notAvailable('read');
928 *readline = _notAvailable('readline');
929 *READLINE = _notAvailable('readline');
930 *getc = _notAvailable('getc');
931 *GETC = _notAvailable('getc');
942 *BINMODE = \&binmode;
944 #*sysread = \&_notAvailable;
945 #*syswrite = \&_write;
954 IO::Compress::Base - Base Class for IO::Compress modules
959 use IO::Compress::Base ;
964 This module is not intended for direct use in application code. Its sole
965 purpose if to to be sub-classed by IO::Compress modules.
972 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>
974 L<Compress::Zlib::FAQ|Compress::Zlib::FAQ>
976 L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>,
977 L<Archive::Tar|Archive::Tar>,
986 This module was written by Paul Marquess, F<pmqs@cpan.org>.
990 =head1 MODIFICATION HISTORY
992 See the Changes file.
994 =head1 COPYRIGHT AND LICENSE
996 Copyright (c) 2005-2006 Paul Marquess. All rights reserved.
998 This program is free software; you can redistribute it and/or
999 modify it under the same terms as Perl itself.