]>
cygwin.com Git - cygwin-apps/setup.git/blob - compress_gz.cc
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"
27 #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
28 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
29 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
30 #define COMMENT 0x10 /* bit 4 set: file comment present */
31 #define RESERVED 0xE0 /* bits 5..7: reserved */
34 /* TODO make this a static member and federate the magic logic */
35 static int gz_magic
[2] = { 0x1f, 0x8b }; /* gzip magic header */
38 * Predicate: the stream is open for read. For writing the class constructor variant with
39 * mode must be called directly
41 compress_gz::compress_gz (io_stream
* parent
)
43 construct (parent
, "r");
46 compress_gz::compress_gz (io_stream
* parent
, const char *openmode
)
48 construct (parent
, openmode
);
52 compress_gz::construct (io_stream
* parent
, const char *openmode
)
57 int level
= Z_DEFAULT_COMPRESSION
; /* compression level */
58 int strategy
= Z_DEFAULT_STRATEGY
; /* compression strategy */
59 char *p
= (char *) openmode
;
60 char fmode
[80]; /* copy of openmode, without the compression level */
63 stream
.zalloc
= (alloc_func
) NULL
;
64 stream
.zfree
= (free_func
) NULL
;
65 stream
.opaque
= (voidpf
) NULL
;
66 stream
.next_in
= inbuf
= NULL
;
67 stream
.next_out
= outbuf
= NULL
;
68 stream
.avail_in
= stream
.avail_out
= 0;
71 crc
= crc32 (0L, Z_NULL
, 0);
79 z_err
= Z_STREAM_ERROR
;
87 if (*p
== 'w' || *p
== 'a')
89 if (*p
>= '0' && *p
<= '9')
95 strategy
= Z_FILTERED
;
99 strategy
= Z_HUFFMAN_ONLY
;
103 *m
++ = *p
; /* copy the mode */
106 while (*p
++ && m
!= fmode
+ sizeof (fmode
));
110 z_err
= Z_STREAM_ERROR
;
117 err
= deflateInit2 (&(stream
), level
,
118 Z_DEFLATED
, -MAX_WBITS
, 8, strategy
);
119 /* windowBits is passed < 0 to suppress zlib header */
121 stream
.next_out
= outbuf
= (Byte
*) malloc (16384);
122 if (err
!= Z_OK
|| outbuf
== Z_NULL
)
125 z_err
= Z_STREAM_ERROR
;
132 stream
.next_in
= inbuf
= (unsigned char *) malloc (16384);
133 err
= inflateInit2 (&stream
, -MAX_WBITS
);
134 /* windowBits is passed < 0 to tell that there is no zlib header.
135 * Note that in this case inflate *requires* an extra "dummy" byte
136 * after the compressed stream in order to complete decompression and
137 * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
138 * present after the compressed stream.
140 if (err
!= Z_OK
|| inbuf
== Z_NULL
)
143 z_err
= Z_STREAM_ERROR
;
147 stream
.avail_out
= 16384;
152 /* Write a very simple .gz header:
155 sprintf (temp
, "%c%c%c%c%c%c%c%c%c%c", gz_magic
[0], gz_magic
[1],
156 Z_DEFLATED
, 0 /*flags */ , 0, 0, 0, 0 /*time */ ,
157 0 /*xflags */ , 0x0b);
158 original
->write (temp
, 10);
160 /* We use 10L instead of ftell(s->file) to because ftell causes an
161 * fflush on some systems. This version of the library doesn't use
162 * startpos anyway in write mode, so this initialization is not
169 check_header (); /* skip the .gz header */
170 startpos
= (original
->tell () - stream
.avail_in
);
176 /* ===========================================================================
177 Outputs a long in LSB order to the given file
180 compress_gz::putLong (unsigned long x
)
183 for (n
= 0; n
< 4; n
++)
185 unsigned char c
= (unsigned char) (x
& 0xff);
186 original
->write (&c
, 1);
193 compress_gz::getLong ()
195 uLong x
= (uLong
) get_byte ();
198 x
+= ((uLong
) get_byte ()) << 8;
199 x
+= ((uLong
) get_byte ()) << 16;
202 z_err
= Z_DATA_ERROR
;
203 x
+= ((uLong
) c
) << 24;
209 compress_gz::read (void *buffer
, size_t len
)
216 ssize_t tmplen
= std::min (peeklen
, len
);
218 memcpy (buffer
, peekbuf
, tmplen
);
219 memmove (peekbuf
, peekbuf
+ tmplen
, tmplen
);
220 ssize_t tmpread
= read (&((char *) buffer
)[tmplen
], len
- tmplen
);
222 return tmpread
+ tmplen
;
227 Bytef
*start
= (Bytef
*) buffer
; /* starting point for crc computation */
228 Byte
*next_out
; /* == stream.next_out but not forced far (for MSDOS) */
231 return Z_STREAM_ERROR
;
233 if (z_err
== Z_DATA_ERROR
|| z_err
== Z_ERRNO
)
235 if (z_err
== Z_STREAM_END
)
238 next_out
= (Byte
*) buffer
;
239 stream
.next_out
= (Bytef
*) buffer
;
240 stream
.avail_out
= len
;
242 while (stream
.avail_out
!= 0)
247 /* Copy first the lookahead bytes: */
248 uInt n
= stream
.avail_in
;
249 if (n
> stream
.avail_out
)
250 n
= stream
.avail_out
;
253 memcpy (stream
.next_out
, stream
.next_in
, n
);
255 stream
.next_out
= next_out
;
257 stream
.avail_out
-= n
;
258 stream
.avail_in
-= n
;
260 if (stream
.avail_out
> 0)
262 stream
.avail_out
-= original
->read (next_out
, stream
.avail_out
);
264 len
-= stream
.avail_out
;
265 stream
.total_in
+= (uLong
) len
;
266 stream
.total_out
+= (uLong
) len
;
271 if (stream
.avail_in
== 0 && !z_eof
)
275 stream
.avail_in
= original
->read (inbuf
, 16384);
276 if (stream
.avail_in
== 0)
279 if (original
->error ())
285 stream
.next_in
= inbuf
;
287 z_err
= inflate (&(stream
), Z_NO_FLUSH
);
289 if (z_err
== Z_STREAM_END
)
291 /* Check CRC and original size */
292 crc
= crc32 (crc
, start
, (uInt
) (stream
.next_out
- start
));
293 start
= stream
.next_out
;
295 if (getLong () != crc
)
297 z_err
= Z_DATA_ERROR
;
302 /* The uncompressed length returned by above getlong() may
303 * be different from stream.total_out) in case of
304 * concatenated .gz files. Check for such files:
309 uLong total_in
= stream
.total_in
;
310 uLong total_out
= stream
.total_out
;
312 inflateReset (&(stream
));
313 stream
.total_in
= total_in
;
314 stream
.total_out
= total_out
;
315 crc
= crc32 (0L, Z_NULL
, 0);
319 if (z_err
!= Z_OK
|| z_eof
)
322 crc
= crc32 (crc
, start
, (uInt
) (stream
.next_out
- start
));
324 return (int) (len
- stream
.avail_out
);
328 /* ===========================================================================
329 Writes the given number of uncompressed bytes into the compressed file.
330 gzwrite returns the number of bytes actually written (0 in case of error).
333 compress_gz::write (const void *buffer
, size_t len
)
336 return Z_STREAM_ERROR
;
338 stream
.next_in
= (Bytef
*) buffer
;
339 stream
.avail_in
= len
;
341 while (stream
.avail_in
!= 0)
344 if (stream
.avail_out
== 0)
347 stream
.next_out
= outbuf
;
348 if (original
->write (outbuf
, 16384) != 16384)
353 stream
.avail_out
= 16384;
355 z_err
= deflate (&(stream
), Z_NO_FLUSH
);
359 crc
= crc32 (crc
, (const Bytef
*) buffer
, len
);
361 return (int) (len
- stream
.avail_in
);
365 compress_gz::peek (void *buffer
, size_t len
)
368 return Z_STREAM_ERROR
;
369 /* can only peek 512 bytes */
375 size_t want
= len
- peeklen
;
376 ssize_t got
= read (&peekbuf
[peeklen
], want
);
382 /* we may have read less than requested. */
383 memcpy (buffer
, peekbuf
, peeklen
);
388 memcpy (buffer
, peekbuf
, len
);
396 throw new logic_error("compress_gz::tell is not implemented");
400 compress_gz::seek (long where
, io_stream_seek_t whence
)
402 throw new logic_error("compress_gz::seek is not implemented");
406 compress_gz::error ()
408 if (z_err
&& z_err
!= Z_STREAM_END
)
414 compress_gz::set_mtime (int time
)
417 return original
->set_mtime (time
);
422 compress_gz::get_mtime ()
425 return original
->get_mtime ();
430 compress_gz::destroy ()
434 if (stream
.state
!= NULL
)
438 z_err
= deflateEnd (&(stream
));
440 else if (mode
== 'r')
442 z_err
= inflateEnd (&(stream
));
455 compress_gz::~compress_gz ()
459 z_err
= do_flush (Z_FINISH
);
467 putLong (stream
.total_in
);
473 compress_gz::do_flush (int flush
)
478 return Z_STREAM_ERROR
;
479 stream
.avail_in
= 0; /* should be zero already anyway */
482 len
= 16384 - stream
.avail_out
;
485 if ((uInt
) original
->write (outbuf
, len
) != len
)
490 stream
.next_out
= outbuf
;
491 stream
.avail_out
= 16384;
495 z_err
= deflate (&(stream
), flush
);
496 /* Ignore the second of two consecutive flushes: */
497 if (len
== 0 && z_err
== Z_BUF_ERROR
)
499 /* deflate has finished flushing only when it hasn't used up
500 * all the available space in the output buffer:
502 done
= (stream
.avail_out
!= 0 || z_err
== Z_STREAM_END
);
503 if (z_err
!= Z_OK
&& z_err
!= Z_STREAM_END
)
506 return z_err
== Z_STREAM_END
? Z_OK
: z_err
;
514 /* ===========================================================================
515 * Read a byte from a gz_stream; update next_in and avail_in. Return EOF
517 * IN assertion: the stream s has been sucessfully opened for reading.
520 compress_gz::get_byte ()
524 if (stream
.avail_in
== 0)
527 stream
.avail_in
= original
->read (inbuf
, 16384);
528 if (stream
.avail_in
== 0)
531 if (original
->error ())
535 stream
.next_in
= inbuf
;
538 return *(stream
.next_in
)++;
542 /* ===========================================================================
543 Check the gzip header of a gz_stream opened for reading. Set the stream
544 mode to transparent if the gzip magic header is not present; set s->err
545 to Z_DATA_ERROR if the magic header is present but the rest of the header
547 IN assertion: the stream s has already been created sucessfully;
548 s->stream.avail_in is zero for the first time, but may be non-zero
549 for concatenated .gz files.
552 compress_gz::check_header ()
554 int method
; /* method byte */
555 int flags
; /* flags byte */
558 /* Check the gzip magic header */
559 for (len
= 0; len
< 2; len
++)
562 if (c
!= gz_magic
[len
])
565 stream
.avail_in
++, stream
.next_in
--;
568 stream
.avail_in
++, stream
.next_in
--;
571 z_err
= stream
.avail_in
!= 0 ? Z_OK
: Z_STREAM_END
;
575 method
= get_byte ();
577 if (method
!= Z_DEFLATED
|| (flags
& RESERVED
) != 0)
579 z_err
= Z_DATA_ERROR
;
583 /* Discard time, xflags and OS code: */
584 for (len
= 0; len
< 6; len
++)
586 if ((flags
& EXTRA_FIELD
) != 0)
587 { /* skip the extra field */
588 len
= (uInt
) get_byte ();
589 len
+= ((uInt
) get_byte ()) << 8;
590 /* len is garbage if EOF but the loop below will quit anyway */
591 while (len
-- != 0 && get_byte () != EOF
);
593 if ((flags
& ORIG_NAME
) != 0)
594 { /* skip the original file name */
595 while ((c
= get_byte ()) != 0 && c
!= EOF
);
597 if ((flags
& COMMENT
) != 0)
598 { /* skip the .gz file comment */
599 while ((c
= get_byte ()) != 0 && c
!= EOF
);
601 if ((flags
& HEAD_CRC
) != 0)
602 { /* skip the header crc */
603 for (len
= 0; len
< 2; len
++)
606 z_err
= z_eof
? Z_DATA_ERROR
: Z_OK
;
This page took 0.061041 seconds and 5 git commands to generate.