perlio.c: (Coverity) eliminate temp ptr that confuses Coverity into thinking there...
[p5sagit/p5-mst-13.2.git] / ext / Compress / IO / Zlib / lib / IO / Compress / Gzip.pm
1
2 package IO::Compress::Gzip ;
3
4 require 5.004 ;
5
6 use strict ;
7 use warnings;
8 use bytes;
9
10
11 use IO::Compress::RawDeflate;
12
13 use Compress::Raw::Zlib ;
14 use IO::Compress::Base::Common qw(:Status :Parse createSelfTiedObject);
15 use IO::Compress::Gzip::Constants;
16
17 BEGIN
18 {
19     if (defined &utf8::downgrade ) 
20       { *noUTF8 = \&utf8::downgrade }
21     else
22       { *noUTF8 = sub {} }  
23 }
24
25 require Exporter ;
26
27 our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $GzipError);
28
29 $VERSION = '2.000_10';
30 $GzipError = '' ;
31
32 @ISA    = qw(Exporter IO::Compress::RawDeflate);
33 @EXPORT_OK = qw( $GzipError gzip ) ;
34 %EXPORT_TAGS = %IO::Compress::RawDeflate::DEFLATE_CONSTANTS ;
35 push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ;
36 Exporter::export_ok_tags('all');
37
38 sub new
39 {
40     my $class = shift ;
41
42     my $obj = createSelfTiedObject($class, \$GzipError);
43
44     $obj->_create(undef, @_);
45 }
46
47
48 sub gzip
49 {
50     my $obj = createSelfTiedObject(undef, \$GzipError);
51     return $obj->_def(@_);
52 }
53
54 #sub newHeader
55 #{
56 #    my $self = shift ;
57 #    #return GZIP_MINIMUM_HEADER ;
58 #    return $self->mkHeader(*$self->{Got});
59 #}
60
61 sub getExtraParams
62 {
63     my $self = shift ;
64
65     return (
66             # zlib behaviour
67             $self->getZlibParams(),
68
69             # Gzip header fields
70             'Minimal'   => [0, 1, Parse_boolean,   0],
71             'Comment'   => [0, 1, Parse_any,       undef],
72             'Name'      => [0, 1, Parse_any,       undef],
73             'Time'      => [0, 1, Parse_any,       undef],
74             'TextFlag'  => [0, 1, Parse_boolean,   0],
75             'HeaderCRC' => [0, 1, Parse_boolean,   0],
76             'OS_Code'   => [0, 1, Parse_unsigned,  $Compress::Raw::Zlib::gzip_os_code],
77             'ExtraField'=> [0, 1, Parse_string,    undef],
78             'ExtraFlags'=> [0, 1, Parse_any,       undef],
79
80         );
81 }
82
83
84 sub ckParams
85 {
86     my $self = shift ;
87     my $got = shift ;
88
89     # gzip always needs crc32
90     $got->value('CRC32' => 1);
91
92     return 1
93         if $got->value('Merge') ;
94
95     my $lax = ! $got->value('Strict') ;
96
97
98     {
99         if (! $got->parsed('Time') ) {
100             # Modification time defaults to now.
101             $got->value('Time' => time) ;
102         }
103
104         # Check that the Name & Comment don't have embedded NULLs
105         # Also check that they only contain ISO 8859-1 chars.
106         if ($got->parsed('Name') && defined $got->value('Name')) {
107             my $name = $got->value('Name');
108                 
109             return $self->saveErrorString(undef, "Null Character found in Name",
110                                                 Z_DATA_ERROR)
111                 if ! $lax && $name =~ /\x00/ ;
112
113             return $self->saveErrorString(undef, "Non ISO 8859-1 Character found in Name",
114                                                 Z_DATA_ERROR)
115                 if ! $lax && $name =~ /$GZIP_FNAME_INVALID_CHAR_RE/o ;
116         }
117
118         if ($got->parsed('Comment') && defined $got->value('Comment')) {
119             my $comment = $got->value('Comment');
120
121             return $self->saveErrorString(undef, "Null Character found in Comment",
122                                                 Z_DATA_ERROR)
123                 if ! $lax && $comment =~ /\x00/ ;
124
125             return $self->saveErrorString(undef, "Non ISO 8859-1 Character found in Comment",
126                                                 Z_DATA_ERROR)
127                 if ! $lax && $comment =~ /$GZIP_FCOMMENT_INVALID_CHAR_RE/o;
128         }
129
130         if ($got->parsed('OS_Code') ) {
131             my $value = $got->value('OS_Code');
132
133             return $self->saveErrorString(undef, "OS_Code must be between 0 and 255, got '$value'")
134                 if $value < 0 || $value > 255 ;
135             
136         }
137
138         # gzip only supports Deflate at present
139         $got->value('Method' => Z_DEFLATED) ;
140
141         if ( ! $got->parsed('ExtraFlags')) {
142             $got->value('ExtraFlags' => 2) 
143                 if $got->value('Level') == Z_BEST_SPEED ;
144             $got->value('ExtraFlags' => 4) 
145                 if $got->value('Level') == Z_BEST_COMPRESSION ;
146         }
147
148         if ($got->parsed('ExtraField')) {
149
150             my $bad = $self->parseExtraField($got, $lax) ;
151             return $self->saveErrorString(undef, $bad, Z_DATA_ERROR)
152                 if $bad ;
153
154             my $len = length $got->value('ExtraField') ;
155             return $self->saveErrorString(undef, ExtraFieldError("Too Large"), 
156                                                         Z_DATA_ERROR)
157                 if $len > GZIP_FEXTRA_MAX_SIZE;
158         }
159     }
160
161     return 1;
162 }
163
164 sub mkTrailer
165 {
166     my $self = shift ;
167     return pack("V V", *$self->{Compress}->crc32(), 
168                        *$self->{UnCompSize_32bit});
169 }
170
171 sub getInverseClass
172 {
173     return ('IO::Uncompress::Gunzip',
174                 \$IO::Uncompress::Gunzip::GunzipError);
175 }
176
177 sub getFileInfo
178 {
179     my $self = shift ;
180     my $params = shift;
181     my $filename = shift ;
182
183     my $defaultTime = (stat($filename))[9] ;
184
185     $params->value('Name' => $filename)
186         if ! $params->parsed('Name') ;
187
188     $params->value('Time' => $defaultTime) 
189         if ! $params->parsed('Time') ;
190 }
191
192
193 sub mkHeader
194 {
195     my $self = shift ;
196     my $param = shift ;
197
198     # stort-circuit if a minimal header is requested.
199     return GZIP_MINIMUM_HEADER if $param->value('Minimal') ;
200
201     # METHOD
202     my $method = $param->valueOrDefault('Method', GZIP_CM_DEFLATED) ;
203
204     # FLAGS
205     my $flags       = GZIP_FLG_DEFAULT ;
206     $flags |= GZIP_FLG_FTEXT    if $param->value('TextFlag') ;
207     $flags |= GZIP_FLG_FHCRC    if $param->value('HeaderCRC') ;
208     $flags |= GZIP_FLG_FEXTRA   if $param->wantValue('ExtraField') ;
209     $flags |= GZIP_FLG_FNAME    if $param->wantValue('Name') ;
210     $flags |= GZIP_FLG_FCOMMENT if $param->wantValue('Comment') ;
211     
212     # MTIME
213     my $time = $param->valueOrDefault('Time', GZIP_MTIME_DEFAULT) ;
214
215     # EXTRA FLAGS
216     my $extra_flags = $param->valueOrDefault('ExtraFlags', GZIP_XFL_DEFAULT);
217
218     # OS CODE
219     my $os_code = $param->valueOrDefault('OS_Code', GZIP_OS_DEFAULT) ;
220
221
222     my $out = pack("C4 V C C", 
223             GZIP_ID1,   # ID1
224             GZIP_ID2,   # ID2
225             $method,    # Compression Method
226             $flags,     # Flags
227             $time,      # Modification Time
228             $extra_flags, # Extra Flags
229             $os_code,   # Operating System Code
230             ) ;
231
232     # EXTRA
233     if ($flags & GZIP_FLG_FEXTRA) {
234         my $extra = $param->value('ExtraField') ;
235         $out .= pack("v", length $extra) . $extra ;
236     }
237
238     # NAME
239     if ($flags & GZIP_FLG_FNAME) {
240         my $name .= $param->value('Name') ;
241         $name =~ s/\x00.*$//;
242         $out .= $name ;
243         # Terminate the filename with NULL unless it already is
244         $out .= GZIP_NULL_BYTE 
245             if !length $name or
246                substr($name, 1, -1) ne GZIP_NULL_BYTE ;
247     }
248
249     # COMMENT
250     if ($flags & GZIP_FLG_FCOMMENT) {
251         my $comment .= $param->value('Comment') ;
252         $comment =~ s/\x00.*$//;
253         $out .= $comment ;
254         # Terminate the comment with NULL unless it already is
255         $out .= GZIP_NULL_BYTE
256             if ! length $comment or
257                substr($comment, 1, -1) ne GZIP_NULL_BYTE;
258     }
259
260     # HEADER CRC
261     $out .= pack("v", crc32($out) & 0x00FF ) if $param->value('HeaderCRC') ;
262
263     noUTF8($out);
264
265     return $out ;
266 }
267
268 sub ExtraFieldError
269 {
270     return "Error with ExtraField Parameter: $_[0]" ;
271 }
272
273 sub validateExtraFieldPair
274 {
275     my $pair = shift ;
276     my $lax  = shift ;
277
278     return ExtraFieldError("Not an array ref")
279         unless ref $pair &&  ref $pair eq 'ARRAY';
280
281     return ExtraFieldError("SubField must have two parts")
282         unless @$pair == 2 ;
283
284     return ExtraFieldError("SubField ID is a reference")
285         if ref $pair->[0] ;
286
287     return ExtraFieldError("SubField Data is a reference")
288         if ref $pair->[1] ;
289
290     # ID is exactly two chars   
291     return ExtraFieldError("SubField ID not two chars long")
292         unless length $pair->[0] == GZIP_FEXTRA_SUBFIELD_ID_SIZE ;
293
294     # Check that the 2nd byte of the ID isn't 0    
295     return ExtraFieldError("SubField ID 2nd byte is 0x00")
296         if ! $lax && substr($pair->[0], 1, 1) eq "\x00" ;
297
298     return ExtraFieldError("SubField Data too long")
299         if length $pair->[1] > GZIP_FEXTRA_SUBFIELD_MAX_SIZE ;
300
301
302     return undef ;
303 }
304
305 sub parseExtra
306 {
307     my $data = shift ;
308     my $lax = shift ;
309
310     return undef
311         if $lax ;
312
313     my $XLEN = length $data ;
314
315     return ExtraFieldError("Too Large")
316         if $XLEN > GZIP_FEXTRA_MAX_SIZE;
317
318     my $offset = 0 ;
319     while ($offset < $XLEN) {
320
321         return ExtraFieldError("FEXTRA Body")
322             if $offset + GZIP_FEXTRA_SUBFIELD_HEADER_SIZE  > $XLEN ;
323
324         my $id = substr($data, $offset, GZIP_FEXTRA_SUBFIELD_ID_SIZE);    
325         $offset += GZIP_FEXTRA_SUBFIELD_ID_SIZE;
326
327         my $subLen =  unpack("v", substr($data, $offset,
328                                             GZIP_FEXTRA_SUBFIELD_LEN_SIZE));
329         $offset += GZIP_FEXTRA_SUBFIELD_LEN_SIZE ;
330
331         return ExtraFieldError("FEXTRA Body")
332             if $offset + $subLen > $XLEN ;
333
334         my $bad = validateExtraFieldPair( [$id, 
335                                             substr($data, $offset, $subLen)], $lax );
336         return $bad if $bad ;
337
338         $offset += $subLen ;
339     }
340         
341     return undef ;
342 }
343
344 sub parseExtraField
345 {
346     my $self = shift ;
347     my $got  = shift ;
348     my $lax  = shift ;
349
350     # ExtraField can be any of
351     #
352     #    -ExtraField => $data
353     #    -ExtraField => [$id1, $data1,
354     #                    $id2, $data2]
355     #                     ...
356     #                   ]
357     #    -ExtraField => [ [$id1 => $data1],
358     #                     [$id2 => $data2],
359     #                     ...
360     #                   ]
361     #    -ExtraField => { $id1 => $data1,
362     #                     $id2 => $data2,
363     #                     ...
364     #                   }
365
366     
367     return undef
368         unless $got->parsed('ExtraField') ;
369
370     return parseExtra($got->value('ExtraField'), $lax)
371         unless ref $got->value('ExtraField') ;
372
373     my $data = $got->value('ExtraField');
374     my $out = '' ;
375
376     if (ref $data eq 'ARRAY') {    
377         if (ref $data->[0]) {
378
379             foreach my $pair (@$data) {
380                 return ExtraFieldError("Not list of lists")
381                     unless ref $pair eq 'ARRAY' ;
382
383                 my $bad = validateExtraFieldPair($pair, $lax) ;
384                 return $bad if $bad ;
385
386                 $out .= $pair->[0] . pack("v", length $pair->[1]) . 
387                         $pair->[1] ;
388             }   
389         }   
390         else {
391             return ExtraFieldError("Not even number of elements")
392                 unless @$data % 2  == 0;
393
394             for (my $ix = 0; $ix <= length(@$data) -1 ; $ix += 2) {
395                 my $bad = validateExtraFieldPair([$data->[$ix], $data->[$ix+1]], $lax) ;
396                 return $bad if $bad ;
397
398                 $out .= $data->[$ix] . pack("v", length $data->[$ix+1]) . 
399                         $data->[$ix+1] ;
400             }   
401         }
402     }   
403     elsif (ref $data eq 'HASH') {    
404         while (my ($id, $info) = each %$data) {
405             my $bad = validateExtraFieldPair([$id, $info], $lax);
406             return $bad if $bad ;
407
408             $out .= $id .  pack("v", length $info) . $info ;
409         }   
410     }   
411     else {
412         return ExtraFieldError("Not a scalar, array ref or hash ref") ;
413     }
414
415     $got->value('ExtraField' => $out);
416
417     return undef;
418 }
419
420 sub mkFinalTrailer
421 {
422     return '';
423 }
424
425 1; 
426
427 __END__
428
429 =head1 NAME
430
431
432
433 IO::Compress::Gzip - Write RFC 1952 files/buffers
434  
435  
436
437 =head1 SYNOPSIS
438
439     use IO::Compress::Gzip qw(gzip $GzipError) ;
440
441
442     my $status = gzip $input => $output [,OPTS] 
443         or die "gzip failed: $GzipError\n";
444
445     my $z = new IO::Compress::Gzip $output [,OPTS]
446         or die "gzip failed: $GzipError\n";
447
448     $z->print($string);
449     $z->printf($format, $string);
450     $z->write($string);
451     $z->syswrite($string [, $length, $offset]);
452     $z->flush();
453     $z->tell();
454     $z->eof();
455     $z->seek($position, $whence);
456     $z->binmode();
457     $z->fileno();
458     $z->opened();
459     $z->autoflush();
460     $z->input_line_number();
461     $z->newStream( [OPTS] );
462     
463     $z->deflateParams();
464     
465     $z->close() ;
466
467     $GzipError ;
468
469     # IO::File mode
470
471     print $z $string;
472     printf $z $format, $string;
473     tell $z
474     eof $z
475     seek $z, $position, $whence
476     binmode $z
477     fileno $z
478     close $z ;
479     
480
481 =head1 DESCRIPTION
482
483
484
485 B<WARNING -- This is a Beta release>. 
486
487 =over 5
488
489 =item * DO NOT use in production code.
490
491 =item * The documentation is incomplete in places.
492
493 =item * Parts of the interface defined here are tentative.
494
495 =item * Please report any problems you find.
496
497 =back
498
499
500
501
502 This module provides a Perl interface that allows writing compressed
503 data to files or buffer as defined in RFC 1952.
504
505
506
507 All the gzip headers defined in RFC 1952 can be created using
508 this module.
509
510
511
512
513
514
515
516 For reading RFC 1952 files/buffers, see the companion module 
517 L<IO::Uncompress::Gunzip|IO::Uncompress::Gunzip>.
518
519
520 =head1 Functional Interface
521
522 A top-level function, C<gzip>, is provided to carry out
523 "one-shot" compression between buffers and/or files. For finer
524 control over the compression process, see the L</"OO Interface">
525 section.
526
527     use IO::Compress::Gzip qw(gzip $GzipError) ;
528
529     gzip $input => $output [,OPTS] 
530         or die "gzip failed: $GzipError\n";
531
532
533
534 The functional interface needs Perl5.005 or better.
535
536
537 =head2 gzip $input => $output [, OPTS]
538
539
540 C<gzip> expects at least two parameters, C<$input> and C<$output>.
541
542 =head3 The C<$input> parameter
543
544 The parameter, C<$input>, is used to define the source of
545 the uncompressed data. 
546
547 It can take one of the following forms:
548
549 =over 5
550
551 =item A filename
552
553 If the C<$input> parameter is a simple scalar, it is assumed to be a
554 filename. This file will be opened for reading and the input data
555 will be read from it.
556
557 =item A filehandle
558
559 If the C<$input> parameter is a filehandle, the input data will be
560 read from it.
561 The string '-' can be used as an alias for standard input.
562
563 =item A scalar reference 
564
565 If C<$input> is a scalar reference, the input data will be read
566 from C<$$input>.
567
568 =item An array reference 
569
570 If C<$input> is an array reference, each element in the array must be a
571 filename.
572
573 The input data will be read from each file in turn. 
574
575 The complete array will be walked to ensure that it only
576 contains valid filenames before any data is compressed.
577
578
579
580 =item An Input FileGlob string
581
582 If C<$input> is a string that is delimited by the characters "<" and ">"
583 C<gzip> will assume that it is an I<input fileglob string>. The
584 input is the list of files that match the fileglob.
585
586 If the fileglob does not match any files ...
587
588 See L<File::GlobMapper|File::GlobMapper> for more details.
589
590
591 =back
592
593 If the C<$input> parameter is any other type, C<undef> will be returned.
594
595
596 In addition, if C<$input> is a simple filename, the default values for
597 the C<Name> and C<Time> options will be sourced from that file.
598
599 If you do not want to use these defaults they can be overridden by
600 explicitly setting the C<Name> and C<Time> options or by setting the
601 C<Minimal> parameter.
602
603
604
605 =head3 The C<$output> parameter
606
607 The parameter C<$output> is used to control the destination of the
608 compressed data. This parameter can take one of these forms.
609
610 =over 5
611
612 =item A filename
613
614 If the C<$output> parameter is a simple scalar, it is assumed to be a
615 filename.  This file will be opened for writing and the compressed
616 data will be written to it.
617
618 =item A filehandle
619
620 If the C<$output> parameter is a filehandle, the compressed data
621 will be written to it.
622 The string '-' can be used as an alias for standard output.
623
624
625 =item A scalar reference 
626
627 If C<$output> is a scalar reference, the compressed data will be
628 stored in C<$$output>.
629
630
631
632 =item An Array Reference
633
634 If C<$output> is an array reference, the compressed data will be
635 pushed onto the array.
636
637 =item An Output FileGlob
638
639 If C<$output> is a string that is delimited by the characters "<" and ">"
640 C<gzip> will assume that it is an I<output fileglob string>. The
641 output is the list of files that match the fileglob.
642
643 When C<$output> is an fileglob string, C<$input> must also be a fileglob
644 string. Anything else is an error.
645
646 =back
647
648 If the C<$output> parameter is any other type, C<undef> will be returned.
649
650
651
652 =head2 Notes
653
654 When C<$input> maps to multiple files/buffers and C<$output> is a single
655 file/buffer the compressed input files/buffers will all be stored
656 in C<$output> as a single compressed stream.
657
658
659
660 =head2 Optional Parameters
661
662 Unless specified below, the optional parameters for C<gzip>,
663 C<OPTS>, are the same as those used with the OO interface defined in the
664 L</"Constructor Options"> section below.
665
666 =over 5
667
668 =item AutoClose =E<gt> 0|1
669
670 This option applies to any input or output data streams to 
671 C<gzip> that are filehandles.
672
673 If C<AutoClose> is specified, and the value is true, it will result in all
674 input and/or output filehandles being closed once C<gzip> has
675 completed.
676
677 This parameter defaults to 0.
678
679
680
681 =item BinModeIn =E<gt> 0|1
682
683 When reading from a file or filehandle, set C<binmode> before reading.
684
685 Defaults to 0.
686
687
688
689
690
691 =item -Append =E<gt> 0|1
692
693 TODO
694
695
696 =back
697
698
699
700 =head2 Examples
701
702 To read the contents of the file C<file1.txt> and write the compressed
703 data to the file C<file1.txt.gz>.
704
705     use strict ;
706     use warnings ;
707     use IO::Compress::Gzip qw(gzip $GzipError) ;
708
709     my $input = "file1.txt";
710     gzip $input => "$input.gz"
711         or die "gzip failed: $GzipError\n";
712
713
714 To read from an existing Perl filehandle, C<$input>, and write the
715 compressed data to a buffer, C<$buffer>.
716
717     use strict ;
718     use warnings ;
719     use IO::Compress::Gzip qw(gzip $GzipError) ;
720     use IO::File ;
721
722     my $input = new IO::File "<file1.txt"
723         or die "Cannot open 'file1.txt': $!\n" ;
724     my $buffer ;
725     gzip $input => \$buffer 
726         or die "gzip failed: $GzipError\n";
727
728 To compress all files in the directory "/my/home" that match "*.txt"
729 and store the compressed data in the same directory
730
731     use strict ;
732     use warnings ;
733     use IO::Compress::Gzip qw(gzip $GzipError) ;
734
735     gzip '</my/home/*.txt>' => '<*.gz>'
736         or die "gzip failed: $GzipError\n";
737
738 and if you want to compress each file one at a time, this will do the trick
739
740     use strict ;
741     use warnings ;
742     use IO::Compress::Gzip qw(gzip $GzipError) ;
743
744     for my $input ( glob "/my/home/*.txt" )
745     {
746         my $output = "$input.gz" ;
747         gzip $input => $output 
748             or die "Error compressing '$input': $GzipError\n";
749     }
750
751
752 =head1 OO Interface
753
754 =head2 Constructor
755
756 The format of the constructor for C<IO::Compress::Gzip> is shown below
757
758     my $z = new IO::Compress::Gzip $output [,OPTS]
759         or die "IO::Compress::Gzip failed: $GzipError\n";
760
761 It returns an C<IO::Compress::Gzip> object on success and undef on failure. 
762 The variable C<$GzipError> will contain an error message on failure.
763
764 If you are running Perl 5.005 or better the object, C<$z>, returned from 
765 IO::Compress::Gzip can be used exactly like an L<IO::File|IO::File> filehandle. 
766 This means that all normal output file operations can be carried out 
767 with C<$z>. 
768 For example, to write to a compressed file/buffer you can use either of 
769 these forms
770
771     $z->print("hello world\n");
772     print $z "hello world\n";
773
774 The mandatory parameter C<$output> is used to control the destination
775 of the compressed data. This parameter can take one of these forms.
776
777 =over 5
778
779 =item A filename
780
781 If the C<$output> parameter is a simple scalar, it is assumed to be a
782 filename. This file will be opened for writing and the compressed data
783 will be written to it.
784
785 =item A filehandle
786
787 If the C<$output> parameter is a filehandle, the compressed data will be
788 written to it.
789 The string '-' can be used as an alias for standard output.
790
791
792 =item A scalar reference 
793
794 If C<$output> is a scalar reference, the compressed data will be stored
795 in C<$$output>.
796
797 =back
798
799 If the C<$output> parameter is any other type, C<IO::Compress::Gzip>::new will
800 return undef.
801
802 =head2 Constructor Options
803
804 C<OPTS> is any combination of the following options:
805
806 =over 5
807
808 =item AutoClose =E<gt> 0|1
809
810 This option is only valid when the C<$output> parameter is a filehandle. If
811 specified, and the value is true, it will result in the C<$output> being
812 closed once either the C<close> method is called or the C<IO::Compress::Gzip>
813 object is destroyed.
814
815 This parameter defaults to 0.
816
817 =item Append =E<gt> 0|1
818
819 Opens C<$output> in append mode. 
820
821 The behaviour of this option is dependent on the type of C<$output>.
822
823 =over 5
824
825 =item * A Buffer
826
827 If C<$output> is a buffer and C<Append> is enabled, all compressed data
828 will be append to the end if C<$output>. Otherwise C<$output> will be
829 cleared before any data is written to it.
830
831 =item * A Filename
832
833 If C<$output> is a filename and C<Append> is enabled, the file will be
834 opened in append mode. Otherwise the contents of the file, if any, will be
835 truncated before any compressed data is written to it.
836
837 =item * A Filehandle
838
839 If C<$output> is a filehandle, the file pointer will be positioned to the
840 end of the file via a call to C<seek> before any compressed data is written
841 to it.  Otherwise the file pointer will not be moved.
842
843 =back
844
845 This parameter defaults to 0.
846
847
848
849
850
851 =item Merge =E<gt> 0|1
852
853 This option is used to compress input data and append it to an existing
854 compressed data stream in C<$output>. The end result is a single compressed
855 data stream stored in C<$output>. 
856
857
858
859 It is a fatal error to attempt to use this option when C<$output> is not an
860 RFC 1952 data stream.
861
862
863
864 There are a number of other limitations with the C<Merge> option:
865
866 =over 5 
867
868 =item 1
869
870 This module needs to have been built with zlib 1.2.1 or better to work. A
871 fatal error will be thrown if C<Merge> is used with an older version of
872 zlib.  
873
874 =item 2
875
876 If C<$output> is a file or a filehandle, it must be seekable.
877
878 =back
879
880
881 This parameter defaults to 0.
882
883
884
885 =item -Level 
886
887 Defines the compression level used by zlib. The value should either be
888 a number between 0 and 9 (0 means no compression and 9 is maximum
889 compression), or one of the symbolic constants defined below.
890
891    Z_NO_COMPRESSION
892    Z_BEST_SPEED
893    Z_BEST_COMPRESSION
894    Z_DEFAULT_COMPRESSION
895
896 The default is Z_DEFAULT_COMPRESSION.
897
898 Note, these constants are not imported by C<IO::Compress::Gzip> by default.
899
900     use IO::Compress::Gzip qw(:strategy);
901     use IO::Compress::Gzip qw(:constants);
902     use IO::Compress::Gzip qw(:all);
903
904 =item -Strategy 
905
906 Defines the strategy used to tune the compression. Use one of the symbolic
907 constants defined below.
908
909    Z_FILTERED
910    Z_HUFFMAN_ONLY
911    Z_RLE
912    Z_FIXED
913    Z_DEFAULT_STRATEGY
914
915 The default is Z_DEFAULT_STRATEGY.
916
917
918
919
920
921
922 =item -Minimal =E<gt> 0|1
923
924 If specified, this option will force the creation of the smallest possible
925 compliant gzip header (which is exactly 10 bytes long) as defined in
926 RFC 1952.
927
928 See the section titled "Compliance" in RFC 1952 for a definition 
929 of the values used for the fields in the gzip header.
930
931 All other parameters that control the content of the gzip header will
932 be ignored if this parameter is set to 1.
933
934 This parameter defaults to 0.
935
936 =item -Comment =E<gt> $comment
937
938 Stores the contents of C<$comment> in the COMMENT field in
939 the gzip header.
940 By default, no comment field is written to the gzip file.
941
942 If the C<-Strict> option is enabled, the comment can only consist of ISO
943 8859-1 characters plus line feed.
944
945 If the C<-Strict> option is disabled, the comment field can contain any
946 character except NULL. If any null characters are present, the field
947 will be truncated at the first NULL.
948
949 =item -Name =E<gt> $string
950
951 Stores the contents of C<$string> in the gzip NAME header field. If
952 C<Name> is not specified, no gzip NAME field will be created.
953
954 If the C<-Strict> option is enabled, C<$string> can only consist of ISO
955 8859-1 characters.
956
957 If C<-Strict> is disabled, then C<$string> can contain any character
958 except NULL. If any null characters are present, the field will be
959 truncated at the first NULL.
960
961 =item -Time =E<gt> $number
962
963 Sets the MTIME field in the gzip header to $number.
964
965 This field defaults to the time the C<IO::Compress::Gzip> object was created
966 if this option is not specified.
967
968 =item -TextFlag =E<gt> 0|1
969
970 This parameter controls the setting of the FLG.FTEXT bit in the gzip
971 header. It is used to signal that the data stored in the gzip file/buffer
972 is probably text.
973
974 The default is 0. 
975
976 =item -HeaderCRC =E<gt> 0|1
977
978 When true this parameter will set the FLG.FHCRC bit to 1 in the gzip header
979 and set the CRC16 header field to the CRC of the complete gzip header
980 except the CRC16 field itself.
981
982 B<Note> that gzip files created with the C<HeaderCRC> flag set to 1 cannot
983 be read by most, if not all, of the the standard gunzip utilities, most
984 notably gzip version 1.2.4. You should therefore avoid using this option if
985 you want to maximize the portability of your gzip files.
986
987 This parameter defaults to 0.
988
989 =item -OS_Code =E<gt> $value
990
991 Stores C<$value> in the gzip OS header field. A number between 0 and 255 is
992 valid.
993
994 If not specified, this parameter defaults to the OS code of the Operating
995 System this module was built on. The value 3 is used as a catch-all for all
996 Unix variants and unknown Operating Systems.
997
998 =item -ExtraField =E<gt> $data
999
1000 This parameter allows additional metadata to be stored in the ExtraField in
1001 the gzip header. An RFC 1952 compliant ExtraField consists of zero or more
1002 subfields. Each subfield consists of a two byte header followed by the
1003 subfield data.
1004
1005 The list of subfields can be supplied in any of the following formats
1006
1007     -ExtraField => [$id1, $data1,
1008                     $id2, $data2,
1009                      ...
1010                    ]
1011     -ExtraField => [ [$id1 => $data1],
1012                      [$id2 => $data2],
1013                      ...
1014                    ]
1015     -ExtraField => { $id1 => $data1,
1016                      $id2 => $data2,
1017                      ...
1018                    }
1019
1020 Where C<$id1>, C<$id2> are two byte subfield ID's. The second byte of
1021 the ID cannot be 0, unless the C<Strict> option has been disabled.
1022
1023 If you use the hash syntax, you have no control over the order in which
1024 the ExtraSubFields are stored, plus you cannot have SubFields with
1025 duplicate ID.
1026
1027 Alternatively the list of subfields can by supplied as a scalar, thus
1028
1029     -ExtraField => $rawdata
1030
1031 If you use the raw format, and the C<Strict> option is enabled,
1032 C<IO::Compress::Gzip> will check that C<$rawdata> consists of zero or more
1033 conformant sub-fields. When C<Strict> is disabled, C<$rawdata> can
1034 consist of any arbitrary byte stream.
1035
1036 The maximum size of the Extra Field 65535 bytes.
1037
1038 =item -ExtraFlags =E<gt> $value
1039
1040 Sets the XFL byte in the gzip header to C<$value>.
1041
1042 If this option is not present, the value stored in XFL field will be
1043 determined by the setting of the C<Level> option.
1044
1045 If C<Level =E<gt> Z_BEST_SPEED> has been specified then XFL is set to 2.
1046 If C<Level =E<gt> Z_BEST_COMPRESSION> has been specified then XFL is set to 4.
1047 Otherwise XFL is set to 0.
1048
1049
1050
1051 =item -Strict =E<gt> 0|1
1052
1053
1054
1055 C<Strict> will optionally police the values supplied with other options
1056 to ensure they are compliant with RFC1952.
1057
1058 This option is enabled by default.
1059
1060 If C<Strict> is enabled the following behaviour will be policed:
1061
1062 =over 5
1063
1064 =item * 
1065
1066 The value supplied with the C<Name> option can only contain ISO 8859-1
1067 characters.
1068
1069 =item * 
1070
1071 The value supplied with the C<Comment> option can only contain ISO 8859-1
1072 characters plus line-feed.
1073
1074 =item *
1075
1076 The values supplied with the C<-Name> and C<-Comment> options cannot
1077 contain multiple embedded nulls.
1078
1079 =item * 
1080
1081 If an C<ExtraField> option is specified and it is a simple scalar,
1082 it must conform to the sub-field structure as defined in RFC 1952.
1083
1084 =item * 
1085
1086 If an C<ExtraField> option is specified the second byte of the ID will be
1087 checked in each subfield to ensure that it does not contain the reserved
1088 value 0x00.
1089
1090 =back
1091
1092 When C<Strict> is disabled the following behaviour will be policed:
1093
1094 =over 5
1095
1096 =item * 
1097
1098 The value supplied with C<-Name> option can contain
1099 any character except NULL.
1100
1101 =item * 
1102
1103 The value supplied with C<-Comment> option can contain any character
1104 except NULL.
1105
1106 =item *
1107
1108 The values supplied with the C<-Name> and C<-Comment> options can contain
1109 multiple embedded nulls. The string written to the gzip header will
1110 consist of the characters up to, but not including, the first embedded
1111 NULL.
1112
1113 =item * 
1114
1115 If an C<ExtraField> option is specified and it is a simple scalar, the
1116 structure will not be checked. The only error is if the length is too big.
1117
1118 =item * 
1119
1120 The ID header in an C<ExtraField> sub-field can consist of any two bytes.
1121
1122 =back
1123
1124
1125
1126 =back
1127
1128 =head2 Examples
1129
1130 TODO
1131
1132 =head1 Methods 
1133
1134 =head2 print
1135
1136 Usage is
1137
1138     $z->print($data)
1139     print $z $data
1140
1141 Compresses and outputs the contents of the C<$data> parameter. This
1142 has the same behaviour as the C<print> built-in.
1143
1144 Returns true if successful.
1145
1146 =head2 printf
1147
1148 Usage is
1149
1150     $z->printf($format, $data)
1151     printf $z $format, $data
1152
1153 Compresses and outputs the contents of the C<$data> parameter.
1154
1155 Returns true if successful.
1156
1157 =head2 syswrite
1158
1159 Usage is
1160
1161     $z->syswrite $data
1162     $z->syswrite $data, $length
1163     $z->syswrite $data, $length, $offset
1164
1165 Compresses and outputs the contents of the C<$data> parameter.
1166
1167 Returns the number of uncompressed bytes written, or C<undef> if
1168 unsuccessful.
1169
1170 =head2 write
1171
1172 Usage is
1173
1174     $z->write $data
1175     $z->write $data, $length
1176     $z->write $data, $length, $offset
1177
1178 Compresses and outputs the contents of the C<$data> parameter.
1179
1180 Returns the number of uncompressed bytes written, or C<undef> if
1181 unsuccessful.
1182
1183 =head2 flush
1184
1185 Usage is
1186
1187
1188     $z->flush;
1189     $z->flush($flush_type);
1190
1191
1192 Flushes any pending compressed data to the output file/buffer.
1193
1194
1195 This method takes an optional parameter, C<$flush_type>, that controls
1196 how the flushing will be carried out. By default the C<$flush_type>
1197 used is C<Z_FINISH>. Other valid values for C<$flush_type> are
1198 C<Z_NO_FLUSH>, C<Z_SYNC_FLUSH>, C<Z_FULL_FLUSH> and C<Z_BLOCK>. It is
1199 strongly recommended that you only set the C<flush_type> parameter if
1200 you fully understand the implications of what it does - overuse of C<flush>
1201 can seriously degrade the level of compression achieved. See the C<zlib>
1202 documentation for details.
1203
1204
1205 Returns true on success.
1206
1207
1208 =head2 tell
1209
1210 Usage is
1211
1212     $z->tell()
1213     tell $z
1214
1215 Returns the uncompressed file offset.
1216
1217 =head2 eof
1218
1219 Usage is
1220
1221     $z->eof();
1222     eof($z);
1223
1224
1225
1226 Returns true if the C<close> method has been called.
1227
1228
1229
1230 =head2 seek
1231
1232     $z->seek($position, $whence);
1233     seek($z, $position, $whence);
1234
1235
1236
1237
1238 Provides a sub-set of the C<seek> functionality, with the restriction
1239 that it is only legal to seek forward in the output file/buffer.
1240 It is a fatal error to attempt to seek backward.
1241
1242 Empty parts of the file/buffer will have NULL (0x00) bytes written to them.
1243
1244
1245
1246 The C<$whence> parameter takes one the usual values, namely SEEK_SET,
1247 SEEK_CUR or SEEK_END.
1248
1249 Returns 1 on success, 0 on failure.
1250
1251 =head2 binmode
1252
1253 Usage is
1254
1255     $z->binmode
1256     binmode $z ;
1257
1258 This is a noop provided for completeness.
1259
1260 =head2 opened
1261
1262     $z->opened()
1263
1264 Returns true if the object currently refers to a opened file/buffer. 
1265
1266 =head2 autoflush
1267
1268     my $prev = $z->autoflush()
1269     my $prev = $z->autoflush(EXPR)
1270
1271 If the C<$z> object is associated with a file or a filehandle, this method
1272 returns the current autoflush setting for the underlying filehandle. If
1273 C<EXPR> is present, and is non-zero, it will enable flushing after every
1274 write/print operation.
1275
1276 If C<$z> is associated with a buffer, this method has no effect and always
1277 returns C<undef>.
1278
1279 B<Note> that the special variable C<$|> B<cannot> be used to set or
1280 retrieve the autoflush setting.
1281
1282 =head2 input_line_number
1283
1284     $z->input_line_number()
1285     $z->input_line_number(EXPR)
1286
1287
1288 This method always returns C<undef> when compressing. 
1289
1290
1291
1292 =head2 fileno
1293
1294     $z->fileno()
1295     fileno($z)
1296
1297 If the C<$z> object is associated with a file or a filehandle, this method
1298 will return the underlying file descriptor.
1299
1300 If the C<$z> object is is associated with a buffer, this method will
1301 return undef.
1302
1303 =head2 close
1304
1305     $z->close() ;
1306     close $z ;
1307
1308
1309
1310 Flushes any pending compressed data and then closes the output file/buffer. 
1311
1312
1313
1314 For most versions of Perl this method will be automatically invoked if
1315 the IO::Compress::Gzip object is destroyed (either explicitly or by the
1316 variable with the reference to the object going out of scope). The
1317 exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In
1318 these cases, the C<close> method will be called automatically, but
1319 not until global destruction of all live objects when the program is
1320 terminating.
1321
1322 Therefore, if you want your scripts to be able to run on all versions
1323 of Perl, you should call C<close> explicitly and not rely on automatic
1324 closing.
1325
1326 Returns true on success, otherwise 0.
1327
1328 If the C<AutoClose> option has been enabled when the IO::Compress::Gzip
1329 object was created, and the object is associated with a file, the
1330 underlying file will also be closed.
1331
1332
1333
1334
1335 =head2 newStream([OPTS])
1336
1337 Usage is
1338
1339     $z->newStream( [OPTS] )
1340
1341 Closes the current compressed data stream and starts a new one.
1342
1343 OPTS consists of the following sub-set of the the options that are
1344 available when creating the C<$z> object,
1345
1346 =over 5
1347
1348
1349
1350 =item * Level
1351
1352
1353
1354 =back
1355
1356
1357 =head2 deflateParams
1358
1359 Usage is
1360
1361     $z->deflateParams
1362
1363 TODO
1364
1365
1366 =head1 Importing 
1367
1368
1369 A number of symbolic constants are required by some methods in 
1370 C<IO::Compress::Gzip>. None are imported by default.
1371
1372
1373
1374 =over 5
1375
1376 =item :all
1377
1378
1379 Imports C<gzip>, C<$GzipError> and all symbolic
1380 constants that can be used by C<IO::Compress::Gzip>. Same as doing this
1381
1382     use IO::Compress::Gzip qw(gzip $GzipError :constants) ;
1383
1384 =item :constants
1385
1386 Import all symbolic constants. Same as doing this
1387
1388     use IO::Compress::Gzip qw(:flush :level :strategy) ;
1389
1390 =item :flush
1391
1392 These symbolic constants are used by the C<flush> method.
1393
1394     Z_NO_FLUSH
1395     Z_PARTIAL_FLUSH
1396     Z_SYNC_FLUSH
1397     Z_FULL_FLUSH
1398     Z_FINISH
1399     Z_BLOCK
1400
1401 =item :level
1402
1403 These symbolic constants are used by the C<Level> option in the constructor.
1404
1405     Z_NO_COMPRESSION
1406     Z_BEST_SPEED
1407     Z_BEST_COMPRESSION
1408     Z_DEFAULT_COMPRESSION
1409
1410
1411 =item :strategy
1412
1413 These symbolic constants are used by the C<Strategy> option in the constructor.
1414
1415     Z_FILTERED
1416     Z_HUFFMAN_ONLY
1417     Z_RLE
1418     Z_FIXED
1419     Z_DEFAULT_STRATEGY
1420     
1421
1422 =back
1423
1424 For 
1425
1426 =head1 EXAMPLES
1427
1428 TODO
1429
1430
1431
1432
1433
1434
1435 =head1 SEE ALSO
1436
1437 L<Compress::Zlib>, 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>
1438
1439 L<Compress::Zlib::FAQ|Compress::Zlib::FAQ>
1440
1441 L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>,
1442 L<Archive::Tar|Archive::Tar>,
1443 L<IO::Zlib|IO::Zlib>
1444
1445
1446 For RFC 1950, 1951 and 1952 see 
1447 F<http://www.faqs.org/rfcs/rfc1950.html>,
1448 F<http://www.faqs.org/rfcs/rfc1951.html> and
1449 F<http://www.faqs.org/rfcs/rfc1952.html>
1450
1451 The I<zlib> compression library was written by Jean-loup Gailly
1452 F<gzip@prep.ai.mit.edu> and Mark Adler F<madler@alumni.caltech.edu>.
1453
1454 The primary site for the I<zlib> compression library is
1455 F<http://www.zlib.org>.
1456
1457 The primary site for gzip is F<http://www.gzip.org>.
1458
1459
1460
1461
1462 =head1 AUTHOR
1463
1464 This module was written by Paul Marquess, F<pmqs@cpan.org>. 
1465
1466
1467
1468 =head1 MODIFICATION HISTORY
1469
1470 See the Changes file.
1471
1472 =head1 COPYRIGHT AND LICENSE
1473
1474 Copyright (c) 2005-2006 Paul Marquess. All rights reserved.
1475
1476 This program is free software; you can redistribute it and/or
1477 modify it under the same terms as Perl itself.
1478
1479