2 * Copyright (c) 2001, Robert Collins.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * A copy of the GNU General Public License can be found at
12 * Written by Robert Collins <rbtcollins@hotmail.com>
17 * Portions copyright under the zlib licence - this class was derived from
18 * gzio.c in that library.
21 #include "compress_gz.h"
29 #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
30 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
31 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
32 #define COMMENT 0x10 /* bit 4 set: file comment present */
33 #define RESERVED 0xE0 /* bits 5..7: reserved */
35 /* TODO make this a static member and federate the magic logic */
36 static int gz_magic
[2] = { 0x1f, 0x8b }; /* gzip magic header */
39 * Predicate: the stream is open for read. For writing the class constructor variant with
40 * mode must be called directly
42 compress_gz::compress_gz (io_stream
* parent
)
50 compress_gz::compress_gz (io_stream
* parent
, const char *_openmode
)
59 compress_gz::construct ()
63 int level
= Z_DEFAULT_COMPRESSION
; /* compression level */
64 int strategy
= Z_DEFAULT_STRATEGY
; /* compression strategy */
65 char *p
= (char *) openmode
;
66 char fmode
[80]; /* copy of openmode, without the compression level */
69 stream
.zalloc
= (alloc_func
) NULL
;
70 stream
.zfree
= (free_func
) NULL
;
71 stream
.opaque
= (voidpf
) NULL
;
72 stream
.next_in
= inbuf
= NULL
;
73 stream
.next_out
= outbuf
= NULL
;
74 stream
.avail_in
= stream
.avail_out
= 0;
77 crc
= crc32 (0L, Z_NULL
, 0);
85 z_err
= Z_STREAM_ERROR
;
93 if (*p
== 'w' || *p
== 'a')
95 if (*p
>= '0' && *p
<= '9')
101 strategy
= Z_FILTERED
;
105 strategy
= Z_HUFFMAN_ONLY
;
109 *m
++ = *p
; /* copy the mode */
112 while (*p
++ && m
!= fmode
+ sizeof (fmode
));
116 z_err
= Z_STREAM_ERROR
;
123 err
= deflateInit2 (&(stream
), level
,
124 Z_DEFLATED
, -MAX_WBITS
, 8, strategy
);
125 /* windowBits is passed < 0 to suppress zlib header */
127 stream
.next_out
= outbuf
= (Byte
*) malloc (16384);
128 if (err
!= Z_OK
|| outbuf
== Z_NULL
)
131 z_err
= Z_STREAM_ERROR
;
138 stream
.next_in
= inbuf
= (unsigned char *) malloc (16384);
139 err
= inflateInit2 (&stream
, -MAX_WBITS
);
140 /* windowBits is passed < 0 to tell that there is no zlib header.
141 * Note that in this case inflate *requires* an extra "dummy" byte
142 * after the compressed stream in order to complete decompression and
143 * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
144 * present after the compressed stream.
146 if (err
!= Z_OK
|| inbuf
== Z_NULL
)
149 z_err
= Z_STREAM_ERROR
;
153 stream
.avail_out
= 16384;
158 /* Write a very simple .gz header:
161 sprintf (temp
, "%c%c%c%c%c%c%c%c%c%c", gz_magic
[0], gz_magic
[1],
162 Z_DEFLATED
, 0 /*flags */ , 0, 0, 0, 0 /*time */ ,
163 0 /*xflags */ , 0x0b);
164 original
->write (temp
, 10);
166 /* We use 10L instead of ftell(s->file) to because ftell causes an
167 * fflush on some systems. This version of the library doesn't use
168 * startpos anyway in write mode, so this initialization is not
175 check_header (); /* skip the .gz header */
176 startpos
= (original
->tell () - stream
.avail_in
);
182 /* ===========================================================================
183 Outputs a long in LSB order to the given file
186 compress_gz::putLong (unsigned long x
)
189 for (n
= 0; n
< 4; n
++)
191 unsigned char c
= (unsigned char) (x
& 0xff);
192 original
->write (&c
, 1);
199 compress_gz::getLong ()
201 uLong x
= (uLong
) get_byte ();
204 x
+= ((uLong
) get_byte ()) << 8;
205 x
+= ((uLong
) get_byte ()) << 16;
208 z_err
= Z_DATA_ERROR
;
209 x
+= ((uLong
) c
) << 24;
215 compress_gz::read (void *buffer
, size_t len
)
222 ssize_t tmplen
= std::min (peeklen
, len
);
224 memcpy (buffer
, peekbuf
, tmplen
);
225 memmove (peekbuf
, peekbuf
+ tmplen
, tmplen
);
226 ssize_t tmpread
= read (&((char *) buffer
)[tmplen
], len
- tmplen
);
228 return tmpread
+ tmplen
;
233 Bytef
*start
= (Bytef
*) buffer
; /* starting point for crc computation */
234 Byte
*next_out
; /* == stream.next_out but not forced far (for MSDOS) */
238 z_err
= Z_STREAM_ERROR
;
242 if (z_err
== Z_DATA_ERROR
|| z_err
== Z_ERRNO
)
244 if (z_err
== Z_STREAM_END
)
247 next_out
= (Byte
*) buffer
;
248 stream
.next_out
= (Bytef
*) buffer
;
249 stream
.avail_out
= len
;
251 while (stream
.avail_out
!= 0)
256 /* Copy first the lookahead bytes: */
257 uInt n
= stream
.avail_in
;
258 if (n
> stream
.avail_out
)
259 n
= stream
.avail_out
;
262 memcpy (stream
.next_out
, stream
.next_in
, n
);
264 stream
.next_out
= next_out
;
266 stream
.avail_out
-= n
;
267 stream
.avail_in
-= n
;
269 if (stream
.avail_out
> 0)
271 stream
.avail_out
-= original
->read (next_out
, stream
.avail_out
);
273 len
-= stream
.avail_out
;
274 stream
.total_in
+= (uLong
) len
;
275 stream
.total_out
+= (uLong
) len
;
280 if (stream
.avail_in
== 0 && !z_eof
)
284 stream
.avail_in
= original
->read (inbuf
, 16384);
285 if (stream
.avail_in
== 0)
288 if (original
->error ())
294 stream
.next_in
= inbuf
;
296 z_err
= inflate (&(stream
), Z_NO_FLUSH
);
298 if (z_err
== Z_STREAM_END
)
300 /* Check CRC and original size */
301 crc
= crc32 (crc
, start
, (uInt
) (stream
.next_out
- start
));
302 start
= stream
.next_out
;
304 if (getLong () != crc
)
306 z_err
= Z_DATA_ERROR
;
311 /* The uncompressed length returned by above getlong() may
312 * be different from stream.total_out) in case of
313 * concatenated .gz files. Check for such files:
318 uLong total_in
= stream
.total_in
;
319 uLong total_out
= stream
.total_out
;
321 inflateReset (&(stream
));
322 stream
.total_in
= total_in
;
323 stream
.total_out
= total_out
;
324 crc
= crc32 (0L, Z_NULL
, 0);
328 if (z_err
!= Z_OK
|| z_eof
)
331 crc
= crc32 (crc
, start
, (uInt
) (stream
.next_out
- start
));
333 return (int) (len
- stream
.avail_out
);
337 /* ===========================================================================
338 Writes the given number of uncompressed bytes into the compressed file.
339 gzwrite returns the number of bytes actually written (0 in case of error).
342 compress_gz::write (const void *buffer
, size_t len
)
346 z_err
= Z_STREAM_ERROR
;
350 stream
.next_in
= (Bytef
*) buffer
;
351 stream
.avail_in
= len
;
353 while (stream
.avail_in
!= 0)
356 if (stream
.avail_out
== 0)
359 stream
.next_out
= outbuf
;
360 if (original
->write (outbuf
, 16384) != 16384)
365 stream
.avail_out
= 16384;
367 z_err
= deflate (&(stream
), Z_NO_FLUSH
);
371 crc
= crc32 (crc
, (const Bytef
*) buffer
, len
);
373 return (int) (len
- stream
.avail_in
);
377 compress_gz::peek (void *buffer
, size_t len
)
381 z_err
= Z_STREAM_ERROR
;
384 /* can only peek 512 bytes */
393 size_t want
= len
- peeklen
;
394 ssize_t got
= read (&peekbuf
[peeklen
], want
);
400 /* we may have read less than requested. */
401 memcpy (buffer
, peekbuf
, peeklen
);
406 memcpy (buffer
, peekbuf
, len
);
414 throw new std::logic_error("compress_gz::tell is not implemented");
418 compress_gz::seek (long where
, io_stream_seek_t whence
)
420 if ((whence
== IO_SEEK_SET
) && (where
== 0))
422 int result
= original
->seek(where
, whence
);
428 throw new std::logic_error("compress_gz::seek is not implemented");
432 compress_gz::error ()
434 if (z_err
&& z_err
!= Z_STREAM_END
)
440 compress_gz::set_mtime (time_t time
)
443 return original
->set_mtime (time
);
448 compress_gz::get_mtime ()
451 return original
->get_mtime ();
456 compress_gz::get_mode ()
459 return original
->get_mode ();
464 compress_gz::release_original ()
466 owns_original
= false;
470 compress_gz::destroy ()
478 if (stream
.state
!= NULL
)
482 z_err
= deflateEnd (&(stream
));
484 else if (mode
== 'r')
486 z_err
= inflateEnd (&(stream
));
502 compress_gz::~compress_gz ()
506 z_err
= do_flush (Z_FINISH
);
510 putLong (stream
.total_in
);
514 if (original
&& owns_original
)
519 compress_gz::do_flush (int flush
)
524 return Z_STREAM_ERROR
;
525 stream
.avail_in
= 0; /* should be zero already anyway */
528 len
= 16384 - stream
.avail_out
;
531 if ((uInt
) original
->write (outbuf
, len
) != len
)
536 stream
.next_out
= outbuf
;
537 stream
.avail_out
= 16384;
541 z_err
= deflate (&(stream
), flush
);
542 /* Ignore the second of two consecutive flushes: */
543 if (len
== 0 && z_err
== Z_BUF_ERROR
)
545 /* deflate has finished flushing only when it hasn't used up
546 * all the available space in the output buffer:
548 done
= (stream
.avail_out
!= 0 || z_err
== Z_STREAM_END
);
549 if (z_err
!= Z_OK
&& z_err
!= Z_STREAM_END
)
552 return z_err
== Z_STREAM_END
? Z_OK
: z_err
;
555 /* ===========================================================================
556 * Read a byte from a gz_stream; update next_in and avail_in. Return EOF
558 * IN assertion: the stream s has been sucessfully opened for reading.
561 compress_gz::get_byte ()
565 if (stream
.avail_in
== 0)
568 stream
.avail_in
= original
->read (inbuf
, 16384);
569 if (stream
.avail_in
== 0)
572 if (original
->error ())
576 stream
.next_in
= inbuf
;
579 return *(stream
.next_in
)++;
583 /* ===========================================================================
584 Check the gzip header of a gz_stream opened for reading. Set the stream
585 mode to transparent if the gzip magic header is not present; set s->err
586 to Z_DATA_ERROR if the magic header is present but the rest of the header
588 IN assertion: the stream s has already been created sucessfully;
589 s->stream.avail_in is zero for the first time, but may be non-zero
590 for concatenated .gz files.
593 compress_gz::check_header ()
595 int method
; /* method byte */
596 int flags
; /* flags byte */
599 /* Check the gzip magic header */
600 for (len
= 0; len
< 2; len
++)
603 if (c
!= gz_magic
[len
])
606 stream
.avail_in
++, stream
.next_in
--;
609 stream
.avail_in
++, stream
.next_in
--;
612 z_err
= stream
.avail_in
!= 0 ? Z_OK
: Z_STREAM_END
;
616 method
= get_byte ();
618 if (method
!= Z_DEFLATED
|| (flags
& RESERVED
) != 0)
620 z_err
= Z_DATA_ERROR
;
624 /* Discard time, xflags and OS code: */
625 for (len
= 0; len
< 6; len
++)
627 if ((flags
& EXTRA_FIELD
) != 0)
628 { /* skip the extra field */
629 len
= (uInt
) get_byte ();
630 len
+= ((uInt
) get_byte ()) << 8;
631 /* len is garbage if EOF but the loop below will quit anyway */
632 while (len
-- != 0 && get_byte () != EOF
);
634 if ((flags
& ORIG_NAME
) != 0)
635 { /* skip the original file name */
636 while ((c
= get_byte ()) != 0 && c
!= EOF
);
638 if ((flags
& COMMENT
) != 0)
639 { /* skip the .gz file comment */
640 while ((c
= get_byte ()) != 0 && c
!= EOF
);
642 if ((flags
& HEAD_CRC
) != 0)
643 { /* skip the header crc */
644 for (len
= 0; len
< 2; len
++)
647 z_err
= z_eof
? Z_DATA_ERROR
: Z_OK
;