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 ())
295 stream
.next_in
= inbuf
;
297 z_err
= inflate (&(stream
), Z_NO_FLUSH
);
299 if (z_err
== Z_STREAM_END
)
301 /* Check CRC and original size */
302 crc
= crc32 (crc
, start
, (uInt
) (stream
.next_out
- start
));
303 start
= stream
.next_out
;
305 if (getLong () != crc
)
307 z_err
= Z_DATA_ERROR
;
312 /* The uncompressed length returned by above getlong() may
313 * be different from stream.total_out) in case of
314 * concatenated .gz files. Check for such files:
319 uLong total_in
= stream
.total_in
;
320 uLong total_out
= stream
.total_out
;
322 inflateReset (&(stream
));
323 stream
.total_in
= total_in
;
324 stream
.total_out
= total_out
;
325 crc
= crc32 (0L, Z_NULL
, 0);
329 if (z_err
!= Z_OK
|| z_eof
)
332 crc
= crc32 (crc
, start
, (uInt
) (stream
.next_out
- start
));
334 return (int) (len
- stream
.avail_out
);
338 /* ===========================================================================
339 Writes the given number of uncompressed bytes into the compressed file.
340 gzwrite returns the number of bytes actually written (0 in case of error).
343 compress_gz::write (const void *buffer
, size_t len
)
347 z_err
= Z_STREAM_ERROR
;
351 stream
.next_in
= (Bytef
*) buffer
;
352 stream
.avail_in
= len
;
354 while (stream
.avail_in
!= 0)
357 if (stream
.avail_out
== 0)
360 stream
.next_out
= outbuf
;
361 if (original
->write (outbuf
, 16384) != 16384)
366 stream
.avail_out
= 16384;
368 z_err
= deflate (&(stream
), Z_NO_FLUSH
);
372 crc
= crc32 (crc
, (const Bytef
*) buffer
, len
);
374 return (int) (len
- stream
.avail_in
);
378 compress_gz::peek (void *buffer
, size_t len
)
382 z_err
= Z_STREAM_ERROR
;
385 /* can only peek 512 bytes */
394 size_t want
= len
- peeklen
;
395 ssize_t got
= read (&peekbuf
[peeklen
], want
);
401 /* we may have read less than requested. */
402 memcpy (buffer
, peekbuf
, peeklen
);
407 memcpy (buffer
, peekbuf
, len
);
415 throw new std::logic_error("compress_gz::tell is not implemented");
419 compress_gz::seek (off_t where
, io_stream_seek_t whence
)
421 if ((whence
== IO_SEEK_SET
) && (where
== 0))
423 off_t result
= original
->seek(where
, whence
);
429 throw new std::logic_error("compress_gz::seek is not implemented");
433 compress_gz::error ()
435 if (z_err
&& z_err
!= Z_STREAM_END
)
441 compress_gz::set_mtime (time_t time
)
444 return original
->set_mtime (time
);
449 compress_gz::get_mtime ()
452 return original
->get_mtime ();
457 compress_gz::get_mode ()
460 return original
->get_mode ();
465 compress_gz::release_original ()
467 owns_original
= false;
471 compress_gz::destroy ()
479 if (stream
.state
!= NULL
)
483 z_err
= deflateEnd (&(stream
));
485 else if (mode
== 'r')
487 z_err
= inflateEnd (&(stream
));
503 compress_gz::~compress_gz ()
507 z_err
= do_flush (Z_FINISH
);
511 putLong (stream
.total_in
);
515 if (original
&& owns_original
)
520 compress_gz::do_flush (int flush
)
525 return Z_STREAM_ERROR
;
526 stream
.avail_in
= 0; /* should be zero already anyway */
529 len
= 16384 - stream
.avail_out
;
532 if ((uInt
) original
->write (outbuf
, len
) != len
)
537 stream
.next_out
= outbuf
;
538 stream
.avail_out
= 16384;
542 z_err
= deflate (&(stream
), flush
);
543 /* Ignore the second of two consecutive flushes: */
544 if (len
== 0 && z_err
== Z_BUF_ERROR
)
546 /* deflate has finished flushing only when it hasn't used up
547 * all the available space in the output buffer:
549 done
= (stream
.avail_out
!= 0 || z_err
== Z_STREAM_END
);
550 if (z_err
!= Z_OK
&& z_err
!= Z_STREAM_END
)
553 return z_err
== Z_STREAM_END
? Z_OK
: z_err
;
556 /* ===========================================================================
557 * Read a byte from a gz_stream; update next_in and avail_in. Return EOF
559 * IN assertion: the stream s has been sucessfully opened for reading.
562 compress_gz::get_byte ()
566 if (stream
.avail_in
== 0)
569 stream
.avail_in
= original
->read (inbuf
, 16384);
570 if (stream
.avail_in
<= 0)
573 if (original
->error ())
578 stream
.next_in
= inbuf
;
581 return *(stream
.next_in
)++;
585 /* ===========================================================================
586 Check the gzip header of a gz_stream opened for reading. Set the stream
587 mode to transparent if the gzip magic header is not present; set s->err
588 to Z_DATA_ERROR if the magic header is present but the rest of the header
590 IN assertion: the stream s has already been created sucessfully;
591 s->stream.avail_in is zero for the first time, but may be non-zero
592 for concatenated .gz files.
595 compress_gz::check_header ()
597 int method
; /* method byte */
598 int flags
; /* flags byte */
601 /* Check the gzip magic header */
602 for (len
= 0; len
< 2; len
++)
605 if (c
!= gz_magic
[len
])
608 stream
.avail_in
++, stream
.next_in
--;
611 stream
.avail_in
++, stream
.next_in
--;
614 z_err
= stream
.avail_in
!= 0 ? Z_OK
: Z_STREAM_END
;
618 method
= get_byte ();
620 if (method
!= Z_DEFLATED
|| (flags
& RESERVED
) != 0)
622 z_err
= Z_DATA_ERROR
;
626 /* Discard time, xflags and OS code: */
627 for (len
= 0; len
< 6; len
++)
629 if ((flags
& EXTRA_FIELD
) != 0)
630 { /* skip the extra field */
631 len
= (uInt
) get_byte ();
632 len
+= ((uInt
) get_byte ()) << 8;
633 /* len is garbage if EOF but the loop below will quit anyway */
634 while (len
-- != 0 && get_byte () != EOF
);
636 if ((flags
& ORIG_NAME
) != 0)
637 { /* skip the original file name */
638 while ((c
= get_byte ()) != 0 && c
!= EOF
);
640 if ((flags
& COMMENT
) != 0)
641 { /* skip the .gz file comment */
642 while ((c
= get_byte ()) != 0 && c
!= EOF
);
644 if ((flags
& HEAD_CRC
) != 0)
645 { /* skip the header crc */
646 for (len
= 0; len
< 2; len
++)
649 z_err
= z_eof
? Z_DATA_ERROR
: Z_OK
;