Commit | Line | Data |
25f0751f |
1 | |
2 | package IO::Compress::Base ; |
3 | |
4 | require 5.004 ; |
5 | |
6 | use strict ; |
7 | use warnings; |
8 | |
b0cda13f |
9 | use IO::Compress::Base::Common 2.003 ; |
25f0751f |
10 | |
11 | use IO::File ; |
12 | use Scalar::Util qw(blessed readonly); |
13 | |
14 | #use File::Glob; |
15 | #require Exporter ; |
16 | use Carp ; |
17 | use Symbol; |
18 | use bytes; |
19 | |
20 | our (@ISA, $VERSION, $got_encode); |
21 | #@ISA = qw(Exporter IO::File); |
22 | |
b0cda13f |
23 | $VERSION = '2.003'; |
25f0751f |
24 | |
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. |
26 | |
27 | #$got_encode = 0; |
28 | #eval |
29 | #{ |
30 | # require Encode; |
31 | # Encode->import('encode', 'find_encoding'); |
32 | #}; |
33 | # |
34 | #$got_encode = 1 unless $@; |
35 | |
36 | sub saveStatus |
37 | { |
38 | my $self = shift ; |
39 | ${ *$self->{ErrorNo} } = shift() + 0 ; |
40 | ${ *$self->{Error} } = '' ; |
41 | |
42 | return ${ *$self->{ErrorNo} } ; |
43 | } |
44 | |
45 | |
46 | sub saveErrorString |
47 | { |
48 | my $self = shift ; |
49 | my $retval = shift ; |
50 | ${ *$self->{Error} } = shift ; |
51 | ${ *$self->{ErrorNo} } = shift() + 0 if @_ ; |
52 | |
53 | return $retval; |
54 | } |
55 | |
56 | sub croakError |
57 | { |
58 | my $self = shift ; |
59 | $self->saveErrorString(0, $_[0]); |
60 | croak $_[0]; |
61 | } |
62 | |
63 | sub closeError |
64 | { |
65 | my $self = shift ; |
66 | my $retval = shift ; |
67 | |
68 | my $errno = *$self->{ErrorNo}; |
69 | my $error = ${ *$self->{Error} }; |
70 | |
71 | $self->close(); |
72 | |
73 | *$self->{ErrorNo} = $errno ; |
74 | ${ *$self->{Error} } = $error ; |
75 | |
76 | return $retval; |
77 | } |
78 | |
79 | |
80 | |
81 | sub error |
82 | { |
83 | my $self = shift ; |
84 | return ${ *$self->{Error} } ; |
85 | } |
86 | |
87 | sub errorNo |
88 | { |
89 | my $self = shift ; |
90 | return ${ *$self->{ErrorNo} } ; |
91 | } |
92 | |
93 | |
94 | sub writeAt |
95 | { |
96 | my $self = shift ; |
97 | my $offset = shift; |
98 | my $data = shift; |
99 | |
100 | if (defined *$self->{FH}) { |
101 | my $here = tell(*$self->{FH}); |
102 | return $self->saveErrorString(undef, "Cannot seek to end of output filehandle: $!", $!) |
103 | if $here < 0 ; |
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: $!", $!) ; |
110 | } |
111 | else { |
112 | substr(${ *$self->{Buffer} }, $offset, length($data)) = $data ; |
113 | } |
114 | |
115 | return 1; |
116 | } |
117 | |
e7d45986 |
118 | sub output |
119 | { |
120 | my $self = shift ; |
121 | my $data = shift ; |
122 | my $last = shift ; |
123 | |
124 | return 1 |
125 | if length $data == 0 && ! $last ; |
126 | |
127 | if ( *$self->{FilterEnvelope} ) { |
128 | *_ = \$data; |
129 | &{ *$self->{FilterEnvelope} }(); |
130 | } |
131 | |
132 | if ( defined *$self->{FH} ) { |
133 | defined *$self->{FH}->write( $data, length $data ) |
134 | or return $self->saveErrorString(0, $!, $!); |
135 | } |
136 | else { |
137 | ${ *$self->{Buffer} } .= $data ; |
138 | } |
139 | |
140 | return 1; |
141 | } |
142 | |
25f0751f |
143 | sub getOneShotParams |
144 | { |
145 | return ( 'MultiStream' => [1, 1, Parse_boolean, 1], |
146 | ); |
147 | } |
148 | |
149 | sub checkParams |
150 | { |
151 | my $self = shift ; |
152 | my $class = shift ; |
153 | |
154 | my $got = shift || IO::Compress::Base::Parameters::new(); |
155 | |
156 | $got->parse( |
157 | { |
158 | # Generic Parameters |
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], |
164 | |
e7d45986 |
165 | 'FilterEnvelope' => [1, 1, Parse_any, undef], |
166 | |
25f0751f |
167 | $self->getExtraParams(), |
168 | *$self->{OneShot} ? $self->getOneShotParams() |
169 | : (), |
170 | }, |
171 | @_) or $self->croakError("${class}: $got->{Error}") ; |
172 | |
173 | return $got ; |
174 | } |
175 | |
176 | sub _create |
177 | { |
178 | my $obj = shift; |
179 | my $got = shift; |
180 | |
181 | *$obj->{Closed} = 1 ; |
182 | |
183 | my $class = ref $obj; |
184 | $obj->croakError("$class: Missing Output parameter") |
185 | if ! @_ && ! $got ; |
186 | |
187 | my $outValue = shift ; |
188 | my $oneShot = 1 ; |
189 | |
190 | if (! $got) |
191 | { |
192 | $oneShot = 0 ; |
193 | $got = $obj->checkParams($class, undef, @_) |
194 | or return undef ; |
195 | } |
196 | |
197 | my $lax = ! $got->value('Strict') ; |
198 | |
199 | my $outType = whatIsOutput($outValue); |
200 | |
201 | $obj->ckOutputParam($class, $outValue) |
202 | or return undef ; |
203 | |
204 | if ($outType eq 'buffer') { |
205 | *$obj->{Buffer} = $outValue; |
206 | } |
207 | else { |
208 | my $buff = "" ; |
209 | *$obj->{Buffer} = \$buff ; |
210 | } |
211 | |
212 | # Merge implies Append |
213 | my $merge = $got->value('Merge') ; |
214 | my $appendOutput = $got->value('Append') || $merge ; |
e7d45986 |
215 | *$obj->{Append} = $appendOutput; |
216 | *$obj->{FilterEnvelope} = $got->value('FilterEnvelope') ; |
25f0751f |
217 | |
218 | if ($merge) |
219 | { |
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 _))) ) |
223 | { $merge = 0 } |
224 | } |
225 | |
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" ) } |
229 | |
25f0751f |
230 | |
231 | |
232 | # TODO - encoding |
233 | # if ($got->parsed('Encoding')) { |
234 | # $obj->croakError("$class: Encode module needed to use -Encoding") |
235 | # if ! $got_encode; |
236 | # |
237 | # my $want_encoding = $got->value('Encoding'); |
238 | # my $encoding = find_encoding($want_encoding); |
239 | # |
240 | # $obj->croakError("$class: Encoding '$want_encoding' is not available") |
241 | # if ! $encoding; |
242 | # |
243 | # *$obj->{Encoding} = $encoding; |
244 | # } |
245 | |
246 | $obj->ckParams($got) |
247 | or $obj->croakError("${class}: " . $obj->error()); |
248 | |
249 | |
250 | $obj->saveStatus(STATUS_OK) ; |
251 | |
252 | my $status ; |
253 | if (! $merge) |
254 | { |
255 | *$obj->{Compress} = $obj->mkComp($class, $got) |
256 | or return undef; |
257 | |
e7d45986 |
258 | *$obj->{UnCompSize} = new U64 ; |
259 | *$obj->{CompSize} = new U64 ; |
25f0751f |
260 | |
261 | if ( $outType eq 'buffer') { |
262 | ${ *$obj->{Buffer} } = '' |
263 | unless $appendOutput ; |
25f0751f |
264 | } |
265 | else { |
266 | if ($outType eq 'handle') { |
267 | *$obj->{FH} = $outValue ; |
268 | setBinModeOutput(*$obj->{FH}) ; |
269 | $outValue->flush() ; |
270 | *$obj->{Handle} = 1 ; |
271 | if ($appendOutput) |
272 | { |
273 | seek(*$obj->{FH}, 0, SEEK_END) |
274 | or return $obj->saveErrorString(undef, "Cannot seek to end of output filehandle: $!", $!) ; |
275 | |
276 | } |
277 | } |
278 | elsif ($outType eq 'filename') { |
279 | my $mode = '>' ; |
280 | $mode = '>>' |
281 | if $appendOutput; |
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}) ; |
286 | } |
25f0751f |
287 | } |
e7d45986 |
288 | |
289 | *$obj->{Header} = $obj->mkHeader($got) ; |
290 | $obj->output( *$obj->{Header} ) |
291 | or return undef; |
25f0751f |
292 | } |
293 | else |
294 | { |
295 | *$obj->{Compress} = $obj->createMerge($outValue, $outType) |
296 | or return undef; |
297 | } |
298 | |
299 | *$obj->{Closed} = 0 ; |
300 | *$obj->{AutoClose} = $got->value('AutoClose') ; |
301 | *$obj->{Output} = $outValue; |
302 | *$obj->{ClassName} = $class; |
303 | *$obj->{Got} = $got; |
304 | *$obj->{OneShot} = 0 ; |
305 | |
306 | return $obj ; |
307 | } |
308 | |
309 | sub ckOutputParam |
310 | { |
311 | my $self = shift ; |
312 | my $from = shift ; |
313 | my $outType = whatIsOutput($_[0]); |
314 | |
315 | $self->croakError("$from: output parameter not a filename, filehandle or scalar ref") |
316 | if ! $outType ; |
317 | |
318 | $self->croakError("$from: output filename is undef or null string") |
319 | if $outType eq 'filename' && (! defined $_[0] || $_[0] eq '') ; |
320 | |
321 | $self->croakError("$from: output buffer is read-only") |
322 | if $outType eq 'buffer' && readonly(${ $_[0] }); |
323 | |
324 | return 1; |
325 | } |
326 | |
327 | |
328 | sub _def |
329 | { |
330 | my $obj = shift ; |
331 | |
332 | my $class= (caller)[0] ; |
333 | my $name = (caller(1))[3] ; |
334 | |
335 | $obj->croakError("$name: expected at least 1 parameters\n") |
336 | unless @_ >= 1 ; |
337 | |
338 | my $input = shift ; |
339 | my $haveOut = @_ ; |
340 | my $output = shift ; |
341 | |
342 | my $x = new Validator($class, *$obj->{Error}, $name, $input, $output) |
343 | or return undef ; |
344 | |
345 | push @_, $output if $haveOut && $x->{Hash}; |
346 | |
347 | *$obj->{OneShot} = 1 ; |
348 | |
349 | my $got = $obj->checkParams($name, undef, @_) |
350 | or return undef ; |
351 | |
352 | $x->{Got} = $got ; |
353 | |
354 | # if ($x->{Hash}) |
355 | # { |
356 | # while (my($k, $v) = each %$input) |
357 | # { |
358 | # $v = \$input->{$k} |
359 | # unless defined $v ; |
360 | # |
361 | # $obj->_singleTarget($x, 1, $k, $v, @_) |
362 | # or return undef ; |
363 | # } |
364 | # |
365 | # return keys %$input ; |
366 | # } |
367 | |
368 | if ($x->{GlobMap}) |
369 | { |
370 | $x->{oneInput} = 1 ; |
371 | foreach my $pair (@{ $x->{Pairs} }) |
372 | { |
373 | my ($from, $to) = @$pair ; |
374 | $obj->_singleTarget($x, 1, $from, $to, @_) |
375 | or return undef ; |
376 | } |
377 | |
378 | return scalar @{ $x->{Pairs} } ; |
379 | } |
380 | |
381 | if (! $x->{oneOutput} ) |
382 | { |
383 | my $inFile = ($x->{inType} eq 'filenames' |
384 | || $x->{inType} eq 'filename'); |
385 | |
386 | $x->{inType} = $inFile ? 'filename' : 'buffer'; |
387 | |
388 | foreach my $in ($x->{oneInput} ? $input : @$input) |
389 | { |
390 | my $out ; |
391 | $x->{oneInput} = 1 ; |
392 | |
393 | $obj->_singleTarget($x, $inFile, $in, \$out, @_) |
394 | or return undef ; |
395 | |
396 | push @$output, \$out ; |
397 | #if ($x->{outType} eq 'array') |
398 | # { push @$output, \$out } |
399 | #else |
400 | # { $output->{$in} = \$out } |
401 | } |
402 | |
403 | return 1 ; |
404 | } |
405 | |
406 | # finally the 1 to 1 and n to 1 |
407 | return $obj->_singleTarget($x, 1, $input, $output, @_); |
408 | |
409 | croak "should not be here" ; |
410 | } |
411 | |
412 | sub _singleTarget |
413 | { |
414 | my $obj = shift ; |
415 | my $x = shift ; |
416 | my $inputIsFilename = shift; |
417 | my $input = shift; |
418 | |
419 | if ($x->{oneInput}) |
420 | { |
421 | $obj->getFileInfo($x->{Got}, $input) |
422 | if isaFilename($input) and $inputIsFilename ; |
423 | |
424 | my $z = $obj->_create($x->{Got}, @_) |
425 | or return undef ; |
426 | |
427 | |
428 | defined $z->_wr2($input, $inputIsFilename) |
429 | or return $z->closeError(undef) ; |
430 | |
431 | return $z->close() ; |
432 | } |
433 | else |
434 | { |
435 | my $afterFirst = 0 ; |
436 | my $inputIsFilename = ($x->{inType} ne 'array'); |
437 | my $keep = $x->{Got}->clone(); |
438 | |
439 | #for my $element ( ($x->{inType} eq 'hash') ? keys %$input : @$input) |
440 | for my $element ( @$input) |
441 | { |
442 | my $isFilename = isaFilename($element); |
443 | |
444 | if ( $afterFirst ++ ) |
445 | { |
446 | defined addInterStream($obj, $element, $isFilename) |
447 | or return $obj->closeError(undef) ; |
448 | } |
449 | else |
450 | { |
451 | $obj->getFileInfo($x->{Got}, $element) |
452 | if $isFilename; |
453 | |
454 | $obj->_create($x->{Got}, @_) |
455 | or return undef ; |
456 | } |
457 | |
458 | defined $obj->_wr2($element, $isFilename) |
459 | or return $obj->closeError(undef) ; |
460 | |
461 | *$obj->{Got} = $keep->clone(); |
462 | } |
463 | return $obj->close() ; |
464 | } |
465 | |
466 | } |
467 | |
468 | sub _wr2 |
469 | { |
470 | my $self = shift ; |
471 | |
472 | my $source = shift ; |
473 | my $inputIsFilename = shift; |
474 | |
475 | my $input = $source ; |
476 | if (! $inputIsFilename) |
477 | { |
478 | $input = \$source |
479 | if ! ref $source; |
480 | } |
481 | |
482 | if ( ref $input && ref $input eq 'SCALAR' ) |
483 | { |
484 | return $self->syswrite($input, @_) ; |
485 | } |
486 | |
487 | if ( ! ref $input || isaFilehandle($input)) |
488 | { |
489 | my $isFilehandle = isaFilehandle($input) ; |
490 | |
491 | my $fh = $input ; |
492 | |
493 | if ( ! $isFilehandle ) |
494 | { |
495 | $fh = new IO::File "<$input" |
496 | or return $self->saveErrorString(undef, "cannot open file '$input': $!", $!) ; |
497 | } |
498 | binmode $fh if *$self->{Got}->valueOrDefault('BinModeIn') ; |
499 | |
500 | my $status ; |
501 | my $buff ; |
502 | my $count = 0 ; |
e7d45986 |
503 | while (($status = read($fh, $buff, 16 * 1024)) > 0) { |
25f0751f |
504 | $count += length $buff; |
505 | defined $self->syswrite($buff, @_) |
506 | or return undef ; |
507 | } |
508 | |
509 | return $self->saveErrorString(undef, $!, $!) |
510 | if $status < 0 ; |
511 | |
512 | if ( (!$isFilehandle || *$self->{AutoClose}) && $input ne '-') |
513 | { |
514 | $fh->close() |
515 | or return undef ; |
516 | } |
517 | |
518 | return $count ; |
519 | } |
520 | |
521 | croak "Should not be here"; |
522 | return undef; |
523 | } |
524 | |
525 | sub addInterStream |
526 | { |
527 | my $self = shift ; |
528 | my $input = shift ; |
529 | my $inputIsFilename = shift ; |
530 | |
531 | if (*$self->{Got}->value('MultiStream')) |
532 | { |
533 | $self->getFileInfo(*$self->{Got}, $input) |
534 | #if isaFilename($input) and $inputIsFilename ; |
535 | if isaFilename($input) ; |
536 | |
537 | # TODO -- newStream needs to allow gzip/zip header to be modified |
538 | return $self->newStream(); |
539 | } |
540 | elsif (*$self->{Got}->value('AutoFlush')) |
541 | { |
542 | #return $self->flush(Z_FULL_FLUSH); |
543 | } |
544 | |
545 | return 1 ; |
546 | } |
547 | |
258133d1 |
548 | sub getFileInfo |
549 | { |
550 | } |
551 | |
25f0751f |
552 | sub TIEHANDLE |
553 | { |
554 | return $_[0] if ref($_[0]); |
555 | die "OOPS\n" ; |
556 | } |
557 | |
558 | sub UNTIE |
559 | { |
560 | my $self = shift ; |
561 | } |
562 | |
563 | sub DESTROY |
564 | { |
565 | my $self = shift ; |
566 | $self->close() ; |
567 | |
568 | # TODO - memory leak with 5.8.0 - this isn't called until |
569 | # global destruction |
570 | # |
571 | %{ *$self } = () ; |
572 | undef $self ; |
573 | } |
574 | |
575 | |
576 | |
2b4e0969 |
577 | sub filterUncompressed |
578 | { |
579 | } |
580 | |
25f0751f |
581 | sub syswrite |
582 | { |
583 | my $self = shift ; |
584 | |
585 | my $buffer ; |
586 | if (ref $_[0] ) { |
587 | $self->croakError( *$self->{ClassName} . "::write: not a scalar reference" ) |
588 | unless ref $_[0] eq 'SCALAR' ; |
589 | $buffer = $_[0] ; |
590 | } |
591 | else { |
592 | $buffer = \$_[0] ; |
593 | } |
594 | |
595 | |
596 | if (@_ > 1) { |
597 | my $slen = defined $$buffer ? length($$buffer) : 0; |
598 | my $len = $slen; |
599 | my $offset = 0; |
600 | $len = $_[1] if $_[1] < $len; |
601 | |
602 | if (@_ > 2) { |
603 | $offset = $_[2] || 0; |
604 | $self->croakError(*$self->{ClassName} . "::write: offset outside string") |
605 | if $offset > $slen; |
606 | if ($offset < 0) { |
607 | $offset += $slen; |
608 | $self->croakError( *$self->{ClassName} . "::write: offset outside string") if $offset < 0; |
609 | } |
610 | my $rem = $slen - $offset; |
611 | $len = $rem if $rem < $len; |
612 | } |
613 | |
614 | $buffer = \substr($$buffer, $offset, $len) ; |
615 | } |
616 | |
617 | return 0 if ! defined $$buffer || length $$buffer == 0 ; |
618 | |
619 | my $buffer_length = defined $$buffer ? length($$buffer) : 0 ; |
e7d45986 |
620 | *$self->{UnCompSize}->add($buffer_length) ; |
25f0751f |
621 | |
2b4e0969 |
622 | $self->filterUncompressed($buffer); |
623 | |
25f0751f |
624 | # if (*$self->{Encoding}) { |
625 | # $$buffer = *$self->{Encoding}->encode($$buffer); |
626 | # } |
627 | |
e7d45986 |
628 | my $outBuffer=''; |
629 | my $status = *$self->{Compress}->compr($buffer, $outBuffer) ; |
25f0751f |
630 | |
631 | return $self->saveErrorString(undef, *$self->{Compress}{Error}, |
632 | *$self->{Compress}{ErrorNo}) |
633 | if $status == STATUS_ERROR; |
634 | |
e7d45986 |
635 | *$self->{CompSize}->add(length $outBuffer) ; |
25f0751f |
636 | |
e7d45986 |
637 | $self->output($outBuffer) |
638 | or return undef; |
25f0751f |
639 | |
640 | return $buffer_length; |
641 | } |
642 | |
643 | sub print |
644 | { |
645 | my $self = shift; |
646 | |
647 | #if (ref $self) { |
648 | # $self = *$self{GLOB} ; |
649 | #} |
650 | |
651 | if (defined $\) { |
652 | if (defined $,) { |
653 | defined $self->syswrite(join($,, @_) . $\); |
654 | } else { |
655 | defined $self->syswrite(join("", @_) . $\); |
656 | } |
657 | } else { |
658 | if (defined $,) { |
659 | defined $self->syswrite(join($,, @_)); |
660 | } else { |
661 | defined $self->syswrite(join("", @_)); |
662 | } |
663 | } |
664 | } |
665 | |
666 | sub printf |
667 | { |
668 | my $self = shift; |
669 | my $fmt = shift; |
670 | defined $self->syswrite(sprintf($fmt, @_)); |
671 | } |
672 | |
673 | |
674 | |
675 | sub flush |
676 | { |
677 | my $self = shift ; |
678 | |
e7d45986 |
679 | my $outBuffer=''; |
680 | my $status = *$self->{Compress}->flush($outBuffer, @_) ; |
25f0751f |
681 | return $self->saveErrorString(0, *$self->{Compress}{Error}, |
682 | *$self->{Compress}{ErrorNo}) |
683 | if $status == STATUS_ERROR; |
684 | |
685 | if ( defined *$self->{FH} ) { |
686 | *$self->{FH}->clearerr(); |
e7d45986 |
687 | } |
688 | |
689 | *$self->{CompSize}->add(length $outBuffer) ; |
690 | |
691 | $self->output($outBuffer) |
692 | or return 0; |
693 | |
694 | if ( defined *$self->{FH} ) { |
25f0751f |
695 | defined *$self->{FH}->flush() |
696 | or return $self->saveErrorString(0, $!, $!); |
25f0751f |
697 | } |
698 | |
699 | return 1; |
700 | } |
701 | |
702 | sub newStream |
703 | { |
704 | my $self = shift ; |
705 | |
706 | $self->_writeTrailer() |
707 | or return 0 ; |
708 | |
709 | my $got = $self->checkParams('newStream', *$self->{Got}, @_) |
710 | or return 0 ; |
711 | |
712 | $self->ckParams($got) |
713 | or $self->croakError("newStream: $self->{Error}"); |
714 | |
715 | *$self->{Header} = $self->mkHeader($got) ; |
e7d45986 |
716 | $self->output(*$self->{Header} ) |
717 | or return 0; |
25f0751f |
718 | |
2b4e0969 |
719 | my $status = $self->reset() ; |
25f0751f |
720 | return $self->saveErrorString(0, *$self->{Compress}{Error}, |
721 | *$self->{Compress}{ErrorNo}) |
722 | if $status == STATUS_ERROR; |
723 | |
e7d45986 |
724 | *$self->{UnCompSize}->reset(); |
725 | *$self->{CompSize}->reset(); |
25f0751f |
726 | |
727 | return 1 ; |
728 | } |
729 | |
2b4e0969 |
730 | sub reset |
731 | { |
732 | my $self = shift ; |
733 | return *$self->{Compress}->reset() ; |
734 | } |
735 | |
25f0751f |
736 | sub _writeTrailer |
737 | { |
738 | my $self = shift ; |
739 | |
e7d45986 |
740 | my $trailer = ''; |
741 | |
742 | my $status = *$self->{Compress}->close($trailer) ; |
25f0751f |
743 | return $self->saveErrorString(0, *$self->{Compress}{Error}, *$self->{Compress}{ErrorNo}) |
744 | if $status == STATUS_ERROR; |
745 | |
e7d45986 |
746 | *$self->{CompSize}->add(length $trailer) ; |
747 | |
748 | $trailer .= $self->mkTrailer(); |
25f0751f |
749 | defined $trailer |
750 | or return 0; |
751 | |
e7d45986 |
752 | return $self->output($trailer); |
25f0751f |
753 | } |
754 | |
755 | sub _writeFinalTrailer |
756 | { |
757 | my $self = shift ; |
758 | |
e7d45986 |
759 | return $self->output($self->mkFinalTrailer()); |
25f0751f |
760 | } |
761 | |
762 | sub close |
763 | { |
764 | my $self = shift ; |
765 | |
766 | return 1 if *$self->{Closed} || ! *$self->{Compress} ; |
767 | *$self->{Closed} = 1 ; |
768 | |
769 | untie *$self |
770 | if $] >= 5.008 ; |
771 | |
772 | $self->_writeTrailer() |
773 | or return 0 ; |
774 | |
775 | $self->_writeFinalTrailer() |
776 | or return 0 ; |
777 | |
e7d45986 |
778 | $self->output( "", 1 ) |
779 | or return 0; |
780 | |
25f0751f |
781 | if (defined *$self->{FH}) { |
e7d45986 |
782 | |
25f0751f |
783 | #if (! *$self->{Handle} || *$self->{AutoClose}) { |
784 | if ((! *$self->{Handle} || *$self->{AutoClose}) && ! *$self->{StdIO}) { |
785 | $! = 0 ; |
786 | *$self->{FH}->close() |
787 | or return $self->saveErrorString(0, $!, $!); |
788 | } |
789 | delete *$self->{FH} ; |
790 | # This delete can set $! in older Perls, so reset the errno |
791 | $! = 0 ; |
792 | } |
f6fd7794 |
793 | |
25f0751f |
794 | return 1; |
795 | } |
796 | |
797 | |
798 | #sub total_in |
799 | #sub total_out |
800 | #sub msg |
801 | # |
802 | #sub crc |
803 | #{ |
804 | # my $self = shift ; |
805 | # return *$self->{Compress}->crc32() ; |
806 | #} |
807 | # |
808 | #sub msg |
809 | #{ |
810 | # my $self = shift ; |
811 | # return *$self->{Compress}->msg() ; |
812 | #} |
813 | # |
814 | #sub dict_adler |
815 | #{ |
816 | # my $self = shift ; |
817 | # return *$self->{Compress}->dict_adler() ; |
818 | #} |
819 | # |
820 | #sub get_Level |
821 | #{ |
822 | # my $self = shift ; |
823 | # return *$self->{Compress}->get_Level() ; |
824 | #} |
825 | # |
826 | #sub get_Strategy |
827 | #{ |
828 | # my $self = shift ; |
829 | # return *$self->{Compress}->get_Strategy() ; |
830 | #} |
831 | |
832 | |
833 | sub tell |
834 | { |
835 | my $self = shift ; |
836 | |
e7d45986 |
837 | return *$self->{UnCompSize}->get32bit() ; |
25f0751f |
838 | } |
839 | |
840 | sub eof |
841 | { |
842 | my $self = shift ; |
843 | |
844 | return *$self->{Closed} ; |
845 | } |
846 | |
847 | |
848 | sub seek |
849 | { |
850 | my $self = shift ; |
851 | my $position = shift; |
852 | my $whence = shift ; |
853 | |
854 | my $here = $self->tell() ; |
855 | my $target = 0 ; |
856 | |
857 | #use IO::Handle qw(SEEK_SET SEEK_CUR SEEK_END); |
858 | use IO::Handle ; |
859 | |
860 | if ($whence == IO::Handle::SEEK_SET) { |
861 | $target = $position ; |
862 | } |
863 | elsif ($whence == IO::Handle::SEEK_CUR || $whence == IO::Handle::SEEK_END) { |
864 | $target = $here + $position ; |
865 | } |
866 | else { |
867 | $self->croakError(*$self->{ClassName} . "::seek: unknown value, $whence, for whence parameter"); |
868 | } |
869 | |
870 | # short circuit if seeking to current offset |
871 | return 1 if $target == $here ; |
872 | |
873 | # Outlaw any attempt to seek backwards |
874 | $self->croakError(*$self->{ClassName} . "::seek: cannot seek backwards") |
875 | if $target < $here ; |
876 | |
877 | # Walk the file to the new offset |
878 | my $offset = $target - $here ; |
879 | |
880 | my $buffer ; |
881 | defined $self->syswrite("\x00" x $offset) |
882 | or return 0; |
883 | |
884 | return 1 ; |
885 | } |
886 | |
887 | sub binmode |
888 | { |
889 | 1; |
890 | # my $self = shift ; |
891 | # return defined *$self->{FH} |
892 | # ? binmode *$self->{FH} |
893 | # : 1 ; |
894 | } |
895 | |
896 | sub fileno |
897 | { |
898 | my $self = shift ; |
899 | return defined *$self->{FH} |
900 | ? *$self->{FH}->fileno() |
901 | : undef ; |
902 | } |
903 | |
904 | sub opened |
905 | { |
906 | my $self = shift ; |
907 | return ! *$self->{Closed} ; |
908 | } |
909 | |
910 | sub autoflush |
911 | { |
912 | my $self = shift ; |
913 | return defined *$self->{FH} |
914 | ? *$self->{FH}->autoflush(@_) |
915 | : undef ; |
916 | } |
917 | |
918 | sub input_line_number |
919 | { |
920 | return undef ; |
921 | } |
922 | |
923 | |
924 | sub _notAvailable |
925 | { |
926 | my $name = shift ; |
927 | return sub { croak "$name Not Available: File opened only for output" ; } ; |
928 | } |
929 | |
930 | *read = _notAvailable('read'); |
931 | *READ = _notAvailable('read'); |
932 | *readline = _notAvailable('readline'); |
933 | *READLINE = _notAvailable('readline'); |
934 | *getc = _notAvailable('getc'); |
935 | *GETC = _notAvailable('getc'); |
936 | |
937 | *FILENO = \&fileno; |
938 | *PRINT = \&print; |
939 | *PRINTF = \&printf; |
940 | *WRITE = \&syswrite; |
941 | *write = \&syswrite; |
942 | *SEEK = \&seek; |
943 | *TELL = \&tell; |
944 | *EOF = \&eof; |
945 | *CLOSE = \&close; |
946 | *BINMODE = \&binmode; |
947 | |
948 | #*sysread = \&_notAvailable; |
949 | #*syswrite = \&_write; |
950 | |
951 | 1; |
952 | |
953 | __END__ |
954 | |
955 | =head1 NAME |
956 | |
957 | |
958 | IO::Compress::Base - Base Class for IO::Compress modules |
959 | |
960 | |
961 | =head1 SYNOPSIS |
962 | |
963 | use IO::Compress::Base ; |
964 | |
965 | =head1 DESCRIPTION |
966 | |
967 | |
968 | This module is not intended for direct use in application code. Its sole |
969 | purpose if to to be sub-classed by IO::Compress modules. |
970 | |
971 | |
972 | |
973 | |
974 | =head1 SEE ALSO |
975 | |
258133d1 |
976 | 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> |
25f0751f |
977 | |
978 | L<Compress::Zlib::FAQ|Compress::Zlib::FAQ> |
979 | |
980 | L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>, |
981 | L<Archive::Tar|Archive::Tar>, |
982 | L<IO::Zlib|IO::Zlib> |
983 | |
984 | |
985 | |
986 | |
987 | |
25f0751f |
988 | =head1 AUTHOR |
989 | |
cb7abd7f |
990 | This module was written by Paul Marquess, F<pmqs@cpan.org>. |
25f0751f |
991 | |
992 | |
993 | |
994 | =head1 MODIFICATION HISTORY |
995 | |
996 | See the Changes file. |
997 | |
998 | =head1 COPYRIGHT AND LICENSE |
25f0751f |
999 | |
b0cda13f |
1000 | Copyright (c) 2005-2007 Paul Marquess. All rights reserved. |
25f0751f |
1001 | |
1002 | This program is free software; you can redistribute it and/or |
1003 | modify it under the same terms as Perl itself. |
1004 | |
1005 | |