Commit | Line | Data |
---|---|---|
25f0751f PM |
1 | |
2 | package IO::Compress::Base ; | |
3 | ||
4 | require 5.004 ; | |
5 | ||
6 | use strict ; | |
7 | use warnings; | |
8 | ||
9 | use IO::Compress::Base::Common; | |
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 | ||
63ad731d | 23 | $VERSION = '2.001'; |
25f0751f PM |
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 PM |
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 PM |
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 PM |
165 | 'FilterEnvelope' => [1, 1, Parse_any, undef], |
166 | ||
25f0751f PM |
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 PM |
215 | *$obj->{Append} = $appendOutput; |
216 | *$obj->{FilterEnvelope} = $got->value('FilterEnvelope') ; | |
25f0751f PM |
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 PM |
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 PM |
258 | *$obj->{UnCompSize} = new U64 ; |
259 | *$obj->{CompSize} = new U64 ; | |
25f0751f PM |
260 | |
261 | if ( $outType eq 'buffer') { | |
262 | ${ *$obj->{Buffer} } = '' | |
263 | unless $appendOutput ; | |
25f0751f PM |
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 PM |
288 | |
289 | *$obj->{Header} = $obj->mkHeader($got) ; | |
290 | $obj->output( *$obj->{Header} ) | |
291 | or return undef; | |
25f0751f PM |
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 PM |
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 PM |
548 | sub getFileInfo |
549 | { | |
550 | } | |
551 | ||
25f0751f PM |
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 | ||
e7d45986 | 568 | |
25f0751f PM |
569 | # TODO - memory leak with 5.8.0 - this isn't called until |
570 | # global destruction | |
571 | # | |
572 | %{ *$self } = () ; | |
573 | undef $self ; | |
574 | } | |
575 | ||
576 | ||
577 | ||
2b4e0969 PM |
578 | sub filterUncompressed |
579 | { | |
580 | } | |
581 | ||
25f0751f PM |
582 | sub syswrite |
583 | { | |
584 | my $self = shift ; | |
585 | ||
586 | my $buffer ; | |
587 | if (ref $_[0] ) { | |
588 | $self->croakError( *$self->{ClassName} . "::write: not a scalar reference" ) | |
589 | unless ref $_[0] eq 'SCALAR' ; | |
590 | $buffer = $_[0] ; | |
591 | } | |
592 | else { | |
593 | $buffer = \$_[0] ; | |
594 | } | |
595 | ||
596 | ||
597 | if (@_ > 1) { | |
598 | my $slen = defined $$buffer ? length($$buffer) : 0; | |
599 | my $len = $slen; | |
600 | my $offset = 0; | |
601 | $len = $_[1] if $_[1] < $len; | |
602 | ||
603 | if (@_ > 2) { | |
604 | $offset = $_[2] || 0; | |
605 | $self->croakError(*$self->{ClassName} . "::write: offset outside string") | |
606 | if $offset > $slen; | |
607 | if ($offset < 0) { | |
608 | $offset += $slen; | |
609 | $self->croakError( *$self->{ClassName} . "::write: offset outside string") if $offset < 0; | |
610 | } | |
611 | my $rem = $slen - $offset; | |
612 | $len = $rem if $rem < $len; | |
613 | } | |
614 | ||
615 | $buffer = \substr($$buffer, $offset, $len) ; | |
616 | } | |
617 | ||
618 | return 0 if ! defined $$buffer || length $$buffer == 0 ; | |
619 | ||
620 | my $buffer_length = defined $$buffer ? length($$buffer) : 0 ; | |
e7d45986 | 621 | *$self->{UnCompSize}->add($buffer_length) ; |
25f0751f | 622 | |
2b4e0969 PM |
623 | $self->filterUncompressed($buffer); |
624 | ||
25f0751f PM |
625 | # if (*$self->{Encoding}) { |
626 | # $$buffer = *$self->{Encoding}->encode($$buffer); | |
627 | # } | |
628 | ||
e7d45986 PM |
629 | my $outBuffer=''; |
630 | my $status = *$self->{Compress}->compr($buffer, $outBuffer) ; | |
25f0751f PM |
631 | |
632 | return $self->saveErrorString(undef, *$self->{Compress}{Error}, | |
633 | *$self->{Compress}{ErrorNo}) | |
634 | if $status == STATUS_ERROR; | |
635 | ||
e7d45986 | 636 | *$self->{CompSize}->add(length $outBuffer) ; |
25f0751f | 637 | |
e7d45986 PM |
638 | $self->output($outBuffer) |
639 | or return undef; | |
25f0751f PM |
640 | |
641 | return $buffer_length; | |
642 | } | |
643 | ||
644 | sub print | |
645 | { | |
646 | my $self = shift; | |
647 | ||
648 | #if (ref $self) { | |
649 | # $self = *$self{GLOB} ; | |
650 | #} | |
651 | ||
652 | if (defined $\) { | |
653 | if (defined $,) { | |
654 | defined $self->syswrite(join($,, @_) . $\); | |
655 | } else { | |
656 | defined $self->syswrite(join("", @_) . $\); | |
657 | } | |
658 | } else { | |
659 | if (defined $,) { | |
660 | defined $self->syswrite(join($,, @_)); | |
661 | } else { | |
662 | defined $self->syswrite(join("", @_)); | |
663 | } | |
664 | } | |
665 | } | |
666 | ||
667 | sub printf | |
668 | { | |
669 | my $self = shift; | |
670 | my $fmt = shift; | |
671 | defined $self->syswrite(sprintf($fmt, @_)); | |
672 | } | |
673 | ||
674 | ||
675 | ||
676 | sub flush | |
677 | { | |
678 | my $self = shift ; | |
679 | ||
e7d45986 PM |
680 | my $outBuffer=''; |
681 | my $status = *$self->{Compress}->flush($outBuffer, @_) ; | |
25f0751f PM |
682 | return $self->saveErrorString(0, *$self->{Compress}{Error}, |
683 | *$self->{Compress}{ErrorNo}) | |
684 | if $status == STATUS_ERROR; | |
685 | ||
686 | if ( defined *$self->{FH} ) { | |
687 | *$self->{FH}->clearerr(); | |
e7d45986 PM |
688 | } |
689 | ||
690 | *$self->{CompSize}->add(length $outBuffer) ; | |
691 | ||
692 | $self->output($outBuffer) | |
693 | or return 0; | |
694 | ||
695 | if ( defined *$self->{FH} ) { | |
25f0751f PM |
696 | defined *$self->{FH}->flush() |
697 | or return $self->saveErrorString(0, $!, $!); | |
25f0751f PM |
698 | } |
699 | ||
700 | return 1; | |
701 | } | |
702 | ||
703 | sub newStream | |
704 | { | |
705 | my $self = shift ; | |
706 | ||
707 | $self->_writeTrailer() | |
708 | or return 0 ; | |
709 | ||
710 | my $got = $self->checkParams('newStream', *$self->{Got}, @_) | |
711 | or return 0 ; | |
712 | ||
713 | $self->ckParams($got) | |
714 | or $self->croakError("newStream: $self->{Error}"); | |
715 | ||
716 | *$self->{Header} = $self->mkHeader($got) ; | |
e7d45986 PM |
717 | $self->output(*$self->{Header} ) |
718 | or return 0; | |
25f0751f | 719 | |
2b4e0969 | 720 | my $status = $self->reset() ; |
25f0751f PM |
721 | return $self->saveErrorString(0, *$self->{Compress}{Error}, |
722 | *$self->{Compress}{ErrorNo}) | |
723 | if $status == STATUS_ERROR; | |
724 | ||
e7d45986 PM |
725 | *$self->{UnCompSize}->reset(); |
726 | *$self->{CompSize}->reset(); | |
25f0751f PM |
727 | |
728 | return 1 ; | |
729 | } | |
730 | ||
2b4e0969 PM |
731 | sub reset |
732 | { | |
733 | my $self = shift ; | |
734 | return *$self->{Compress}->reset() ; | |
735 | } | |
736 | ||
25f0751f PM |
737 | sub _writeTrailer |
738 | { | |
739 | my $self = shift ; | |
740 | ||
e7d45986 PM |
741 | my $trailer = ''; |
742 | ||
743 | my $status = *$self->{Compress}->close($trailer) ; | |
25f0751f PM |
744 | return $self->saveErrorString(0, *$self->{Compress}{Error}, *$self->{Compress}{ErrorNo}) |
745 | if $status == STATUS_ERROR; | |
746 | ||
e7d45986 PM |
747 | *$self->{CompSize}->add(length $trailer) ; |
748 | ||
749 | $trailer .= $self->mkTrailer(); | |
25f0751f PM |
750 | defined $trailer |
751 | or return 0; | |
752 | ||
e7d45986 | 753 | return $self->output($trailer); |
25f0751f PM |
754 | } |
755 | ||
756 | sub _writeFinalTrailer | |
757 | { | |
758 | my $self = shift ; | |
759 | ||
e7d45986 | 760 | return $self->output($self->mkFinalTrailer()); |
25f0751f PM |
761 | } |
762 | ||
763 | sub close | |
764 | { | |
765 | my $self = shift ; | |
766 | ||
767 | return 1 if *$self->{Closed} || ! *$self->{Compress} ; | |
768 | *$self->{Closed} = 1 ; | |
769 | ||
770 | untie *$self | |
771 | if $] >= 5.008 ; | |
772 | ||
773 | $self->_writeTrailer() | |
774 | or return 0 ; | |
775 | ||
776 | $self->_writeFinalTrailer() | |
777 | or return 0 ; | |
778 | ||
e7d45986 PM |
779 | $self->output( "", 1 ) |
780 | or return 0; | |
781 | ||
25f0751f | 782 | if (defined *$self->{FH}) { |
e7d45986 | 783 | |
25f0751f PM |
784 | #if (! *$self->{Handle} || *$self->{AutoClose}) { |
785 | if ((! *$self->{Handle} || *$self->{AutoClose}) && ! *$self->{StdIO}) { | |
786 | $! = 0 ; | |
787 | *$self->{FH}->close() | |
788 | or return $self->saveErrorString(0, $!, $!); | |
789 | } | |
790 | delete *$self->{FH} ; | |
791 | # This delete can set $! in older Perls, so reset the errno | |
792 | $! = 0 ; | |
793 | } | |
794 | ||
795 | return 1; | |
796 | } | |
797 | ||
798 | ||
799 | #sub total_in | |
800 | #sub total_out | |
801 | #sub msg | |
802 | # | |
803 | #sub crc | |
804 | #{ | |
805 | # my $self = shift ; | |
806 | # return *$self->{Compress}->crc32() ; | |
807 | #} | |
808 | # | |
809 | #sub msg | |
810 | #{ | |
811 | # my $self = shift ; | |
812 | # return *$self->{Compress}->msg() ; | |
813 | #} | |
814 | # | |
815 | #sub dict_adler | |
816 | #{ | |
817 | # my $self = shift ; | |
818 | # return *$self->{Compress}->dict_adler() ; | |
819 | #} | |
820 | # | |
821 | #sub get_Level | |
822 | #{ | |
823 | # my $self = shift ; | |
824 | # return *$self->{Compress}->get_Level() ; | |
825 | #} | |
826 | # | |
827 | #sub get_Strategy | |
828 | #{ | |
829 | # my $self = shift ; | |
830 | # return *$self->{Compress}->get_Strategy() ; | |
831 | #} | |
832 | ||
833 | ||
834 | sub tell | |
835 | { | |
836 | my $self = shift ; | |
837 | ||
e7d45986 | 838 | return *$self->{UnCompSize}->get32bit() ; |
25f0751f PM |
839 | } |
840 | ||
841 | sub eof | |
842 | { | |
843 | my $self = shift ; | |
844 | ||
845 | return *$self->{Closed} ; | |
846 | } | |
847 | ||
848 | ||
849 | sub seek | |
850 | { | |
851 | my $self = shift ; | |
852 | my $position = shift; | |
853 | my $whence = shift ; | |
854 | ||
855 | my $here = $self->tell() ; | |
856 | my $target = 0 ; | |
857 | ||
858 | #use IO::Handle qw(SEEK_SET SEEK_CUR SEEK_END); | |
859 | use IO::Handle ; | |
860 | ||
861 | if ($whence == IO::Handle::SEEK_SET) { | |
862 | $target = $position ; | |
863 | } | |
864 | elsif ($whence == IO::Handle::SEEK_CUR || $whence == IO::Handle::SEEK_END) { | |
865 | $target = $here + $position ; | |
866 | } | |
867 | else { | |
868 | $self->croakError(*$self->{ClassName} . "::seek: unknown value, $whence, for whence parameter"); | |
869 | } | |
870 | ||
871 | # short circuit if seeking to current offset | |
872 | return 1 if $target == $here ; | |
873 | ||
874 | # Outlaw any attempt to seek backwards | |
875 | $self->croakError(*$self->{ClassName} . "::seek: cannot seek backwards") | |
876 | if $target < $here ; | |
877 | ||
878 | # Walk the file to the new offset | |
879 | my $offset = $target - $here ; | |
880 | ||
881 | my $buffer ; | |
882 | defined $self->syswrite("\x00" x $offset) | |
883 | or return 0; | |
884 | ||
885 | return 1 ; | |
886 | } | |
887 | ||
888 | sub binmode | |
889 | { | |
890 | 1; | |
891 | # my $self = shift ; | |
892 | # return defined *$self->{FH} | |
893 | # ? binmode *$self->{FH} | |
894 | # : 1 ; | |
895 | } | |
896 | ||
897 | sub fileno | |
898 | { | |
899 | my $self = shift ; | |
900 | return defined *$self->{FH} | |
901 | ? *$self->{FH}->fileno() | |
902 | : undef ; | |
903 | } | |
904 | ||
905 | sub opened | |
906 | { | |
907 | my $self = shift ; | |
908 | return ! *$self->{Closed} ; | |
909 | } | |
910 | ||
911 | sub autoflush | |
912 | { | |
913 | my $self = shift ; | |
914 | return defined *$self->{FH} | |
915 | ? *$self->{FH}->autoflush(@_) | |
916 | : undef ; | |
917 | } | |
918 | ||
919 | sub input_line_number | |
920 | { | |
921 | return undef ; | |
922 | } | |
923 | ||
924 | ||
925 | sub _notAvailable | |
926 | { | |
927 | my $name = shift ; | |
928 | return sub { croak "$name Not Available: File opened only for output" ; } ; | |
929 | } | |
930 | ||
931 | *read = _notAvailable('read'); | |
932 | *READ = _notAvailable('read'); | |
933 | *readline = _notAvailable('readline'); | |
934 | *READLINE = _notAvailable('readline'); | |
935 | *getc = _notAvailable('getc'); | |
936 | *GETC = _notAvailable('getc'); | |
937 | ||
938 | *FILENO = \&fileno; | |
939 | *PRINT = \&print; | |
940 | *PRINTF = \&printf; | |
941 | *WRITE = \&syswrite; | |
942 | *write = \&syswrite; | |
943 | *SEEK = \&seek; | |
944 | *TELL = \&tell; | |
945 | *EOF = \&eof; | |
946 | *CLOSE = \&close; | |
947 | *BINMODE = \&binmode; | |
948 | ||
949 | #*sysread = \&_notAvailable; | |
950 | #*syswrite = \&_write; | |
951 | ||
952 | 1; | |
953 | ||
954 | __END__ | |
955 | ||
956 | =head1 NAME | |
957 | ||
958 | ||
959 | IO::Compress::Base - Base Class for IO::Compress modules | |
960 | ||
961 | ||
962 | =head1 SYNOPSIS | |
963 | ||
964 | use IO::Compress::Base ; | |
965 | ||
966 | =head1 DESCRIPTION | |
967 | ||
968 | ||
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. | |
971 | ||
972 | ||
973 | ||
974 | ||
975 | =head1 SEE ALSO | |
976 | ||
258133d1 | 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> |
25f0751f PM |
978 | |
979 | L<Compress::Zlib::FAQ|Compress::Zlib::FAQ> | |
980 | ||
981 | L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>, | |
982 | L<Archive::Tar|Archive::Tar>, | |
983 | L<IO::Zlib|IO::Zlib> | |
984 | ||
985 | ||
986 | ||
987 | ||
988 | ||
25f0751f PM |
989 | =head1 AUTHOR |
990 | ||
cb7abd7f | 991 | This module was written by Paul Marquess, F<pmqs@cpan.org>. |
25f0751f PM |
992 | |
993 | ||
994 | ||
995 | =head1 MODIFICATION HISTORY | |
996 | ||
997 | See the Changes file. | |
998 | ||
999 | =head1 COPYRIGHT AND LICENSE | |
25f0751f PM |
1000 | |
1001 | Copyright (c) 2005-2006 Paul Marquess. All rights reserved. | |
1002 | ||
1003 | This program is free software; you can redistribute it and/or | |
1004 | modify it under the same terms as Perl itself. | |
1005 | ||
1006 |