]>
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>
16 /* Archive IO operations for gz files
17 * Portions copyright under the zlib licence - this class was derived from gzio.c in that
22 static const char *cvsid
=
33 #include "io_stream.h"
35 #include "zlib/zlib.h"
36 #include "compress_gz.h"
38 #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
39 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
40 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
41 #define COMMENT 0x10 /* bit 4 set: file comment present */
42 #define RESERVED 0xE0 /* bits 5..7: reserved */
45 /* TODO make this a static member and federate the magic logic */
46 static int gz_magic
[2] = { 0x1f, 0x8b }; /* gzip magic header */
49 * Predicate: the stream is open for read. For writing the class constructor variant with
50 * mode must be called directly
52 compress_gz::compress_gz (io_stream
* parent
)
54 construct (parent
, "r");
57 compress_gz::compress_gz (io_stream
* parent
, const char *openmode
)
59 construct (parent
, openmode
);
63 compress_gz::construct (io_stream
* parent
, const char *openmode
)
67 z_err
= Z_STREAM_ERROR
;
74 int level
= Z_DEFAULT_COMPRESSION
; /* compression level */
75 int strategy
= Z_DEFAULT_STRATEGY
; /* compression strategy */
76 char *p
= (char *) openmode
;
77 char fmode
[80]; /* copy of openmode, without the compression level */
80 stream
.zalloc
= (alloc_func
) NULL
;
81 stream
.zfree
= (free_func
) NULL
;
82 stream
.opaque
= (voidpf
) NULL
;
83 stream
.next_in
= inbuf
= NULL
;
84 stream
.next_out
= outbuf
= NULL
;
85 stream
.avail_in
= stream
.avail_out
= 0;
88 crc
= crc32 (0L, Z_NULL
, 0);
97 if (*p
== 'w' || *p
== 'a')
99 if (*p
>= '0' && *p
<= '9')
105 strategy
= Z_FILTERED
;
109 strategy
= Z_HUFFMAN_ONLY
;
113 *m
++ = *p
; /* copy the mode */
116 while (*p
++ && m
!= fmode
+ sizeof (fmode
));
120 z_err
= Z_STREAM_ERROR
;
127 err
= deflateInit2 (&(stream
), level
,
128 Z_DEFLATED
, -MAX_WBITS
, 8, strategy
);
129 /* windowBits is passed < 0 to suppress zlib header */
131 stream
.next_out
= outbuf
= (Byte
*) malloc (16384);
132 if (err
!= Z_OK
|| outbuf
== Z_NULL
)
135 z_err
= Z_STREAM_ERROR
;
142 stream
.next_in
= inbuf
= (unsigned char *) malloc (16384);
143 err
= inflateInit2 (&stream
, -MAX_WBITS
);
144 /* windowBits is passed < 0 to tell that there is no zlib header.
145 * Note that in this case inflate *requires* an extra "dummy" byte
146 * after the compressed stream in order to complete decompression and
147 * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
148 * present after the compressed stream.
150 if (err
!= Z_OK
|| inbuf
== Z_NULL
)
153 z_err
= Z_STREAM_ERROR
;
157 stream
.avail_out
= 16384;
162 /* Write a very simple .gz header:
165 sprintf (temp
, "%c%c%c%c%c%c%c%c%c%c", gz_magic
[0], gz_magic
[1],
166 Z_DEFLATED
, 0 /*flags */ , 0, 0, 0, 0 /*time */ ,
167 0 /*xflags */ , 0x0b);
168 original
->write (temp
, 10);
170 /* We use 10L instead of ftell(s->file) to because ftell causes an
171 * fflush on some systems. This version of the library doesn't use
172 * startpos anyway in write mode, so this initialization is not
179 check_header (); /* skip the .gz header */
180 startpos
= (original
->tell () - stream
.avail_in
);
186 /* ===========================================================================
187 Outputs a long in LSB order to the given file
190 compress_gz::putLong (unsigned long x
)
193 for (n
= 0; n
< 4; n
++)
195 unsigned char c
= (unsigned char) (x
& 0xff);
196 original
->write (&c
, 1);
203 compress_gz::getLong ()
205 uLong x
= (uLong
) get_byte ();
208 x
+= ((uLong
) get_byte ()) << 8;
209 x
+= ((uLong
) get_byte ()) << 16;
212 z_err
= Z_DATA_ERROR
;
213 x
+= ((uLong
) c
) << 24;
219 compress_gz::read (void *buffer
, size_t len
)
226 ssize_t tmplen
= min (peeklen
, len
);
228 memcpy (buffer
, peekbuf
, tmplen
);
229 memmove (peekbuf
, peekbuf
+ tmplen
, tmplen
);
230 ssize_t tmpread
= read (&((char *) buffer
)[tmplen
], len
- tmplen
);
232 return tmpread
+ tmplen
;
237 Bytef
*start
= (Bytef
*) buffer
; /* starting point for crc computation */
238 Byte
*next_out
; /* == stream.next_out but not forced far (for MSDOS) */
241 return Z_STREAM_ERROR
;
243 if (z_err
== Z_DATA_ERROR
|| z_err
== Z_ERRNO
)
245 if (z_err
== Z_STREAM_END
)
248 next_out
= (Byte
*) buffer
;
249 stream
.next_out
= (Bytef
*) buffer
;
250 stream
.avail_out
= len
;
252 while (stream
.avail_out
!= 0)
257 /* Copy first the lookahead bytes: */
258 uInt n
= stream
.avail_in
;
259 if (n
> stream
.avail_out
)
260 n
= stream
.avail_out
;
263 memcpy (stream
.next_out
, stream
.next_in
, n
);
265 stream
.next_out
= next_out
;
267 stream
.avail_out
-= n
;
268 stream
.avail_in
-= n
;
270 if (stream
.avail_out
> 0)
272 stream
.avail_out
-= original
->read (next_out
, stream
.avail_out
);
274 len
-= stream
.avail_out
;
275 stream
.total_in
+= (uLong
) len
;
276 stream
.total_out
+= (uLong
) len
;
281 if (stream
.avail_in
== 0 && !z_eof
)
285 stream
.avail_in
= original
->read (inbuf
, 16384);
286 if (stream
.avail_in
== 0)
289 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 (void *buffer
, size_t len
)
346 return Z_STREAM_ERROR
;
348 stream
.next_in
= (Bytef
*) buffer
;
349 stream
.avail_in
= len
;
351 while (stream
.avail_in
!= 0)
354 if (stream
.avail_out
== 0)
357 stream
.next_out
= outbuf
;
358 if (original
->write (outbuf
, 16384) != 16384)
363 stream
.avail_out
= 16384;
365 z_err
= deflate (&(stream
), Z_NO_FLUSH
);
369 crc
= crc32 (crc
, (const Bytef
*) buffer
, len
);
371 return (int) (len
- stream
.avail_in
);
375 compress_gz::peek (void *buffer
, size_t len
)
377 log (LOG_TIMESTAMP
, "compress_gz::peek called");
379 return Z_STREAM_ERROR
;
380 /* can only peek 512 bytes */
386 size_t want
= len
- peeklen
;
387 ssize_t got
= read (&peekbuf
[peeklen
], want
);
393 /* we may have read less than requested. */
394 memcpy (buffer
, peekbuf
, peeklen
);
399 memcpy (buffer
, peekbuf
, len
);
407 log (LOG_TIMESTAMP
, "compress_gz::tell called");
412 compress_gz::error ()
414 // log (LOG_TIMESTAMP, "compress_gz::error called");
419 compress_gz::set_mtime (int time
)
422 return original
->set_mtime (time
);
427 compress_gz::get_mtime ()
430 return original
->get_mtime ();
435 compress_gz::destroy ()
439 if (stream
.state
!= NULL
)
443 z_err
= deflateEnd (&(stream
));
445 else if (mode
== 'r')
447 z_err
= inflateEnd (&(stream
));
460 compress_gz::~compress_gz ()
462 log (LOG_TIMESTAMP
, "compress_gz::~gz called");
465 z_err
= do_flush (Z_FINISH
);
473 putLong (stream
.total_in
);
480 compress_gz::do_flush (int flush
)
485 return Z_STREAM_ERROR
;
486 stream
.avail_in
= 0; /* should be zero already anyway */
489 len
= 16384 - stream
.avail_out
;
492 if ((uInt
) original
->write (outbuf
, len
) != len
)
497 stream
.next_out
= outbuf
;
498 stream
.avail_out
= 16384;
502 z_err
= deflate (&(stream
), flush
);
503 /* Ignore the second of two consecutive flushes: */
504 if (len
== 0 && z_err
== Z_BUF_ERROR
)
506 /* deflate has finished flushing only when it hasn't used up
507 * all the available space in the output buffer:
509 done
= (stream
.avail_out
!= 0 || z_err
== Z_STREAM_END
);
510 if (z_err
!= Z_OK
&& z_err
!= Z_STREAM_END
)
513 return z_err
== Z_STREAM_END
? Z_OK
: z_err
;
521 /* ===========================================================================
522 * Read a byte from a gz_stream; update next_in and avail_in. Return EOF
524 * IN assertion: the stream s has been sucessfully opened for reading.
527 compress_gz::get_byte ()
531 if (stream
.avail_in
== 0)
534 stream
.avail_in
= original
->read (inbuf
, 16384);
535 if (stream
.avail_in
== 0)
538 if (original
->error ())
542 stream
.next_in
= inbuf
;
545 return *(stream
.next_in
)++;
549 /* ===========================================================================
550 Check the gzip header of a gz_stream opened for reading. Set the stream
551 mode to transparent if the gzip magic header is not present; set s->err
552 to Z_DATA_ERROR if the magic header is present but the rest of the header
554 IN assertion: the stream s has already been created sucessfully;
555 s->stream.avail_in is zero for the first time, but may be non-zero
556 for concatenated .gz files.
559 compress_gz::check_header ()
561 int method
; /* method byte */
562 int flags
; /* flags byte */
565 /* Check the gzip magic header */
566 for (len
= 0; len
< 2; len
++)
569 if (c
!= gz_magic
[len
])
572 stream
.avail_in
++, stream
.next_in
--;
575 stream
.avail_in
++, stream
.next_in
--;
578 z_err
= stream
.avail_in
!= 0 ? Z_OK
: Z_STREAM_END
;
582 method
= get_byte ();
584 if (method
!= Z_DEFLATED
|| (flags
& RESERVED
) != 0)
586 z_err
= Z_DATA_ERROR
;
590 /* Discard time, xflags and OS code: */
591 for (len
= 0; len
< 6; len
++)
593 if ((flags
& EXTRA_FIELD
) != 0)
594 { /* skip the extra field */
595 len
= (uInt
) get_byte ();
596 len
+= ((uInt
) get_byte ()) << 8;
597 /* len is garbage if EOF but the loop below will quit anyway */
598 while (len
-- != 0 && get_byte () != EOF
);
600 if ((flags
& ORIG_NAME
) != 0)
601 { /* skip the original file name */
602 while ((c
= get_byte ()) != 0 && c
!= EOF
);
604 if ((flags
& COMMENT
) != 0)
605 { /* skip the .gz file comment */
606 while ((c
= get_byte ()) != 0 && c
!= EOF
);
608 if ((flags
& HEAD_CRC
) != 0)
609 { /* skip the header crc */
610 for (len
= 0; len
< 2; len
++)
613 z_err
= z_eof
? Z_DATA_ERROR
: Z_OK
;
This page took 0.059739 seconds and 5 git commands to generate.