Translated using Weblate (Chinese (Simplified))
[cygwin-setup.git] / compress_gz.cc
blobc7d6d1af1cf3df5cf060aaeb48a428f0d4a9651d
1 /*
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
10 * http://www.gnu.org/
12 * Written by Robert Collins <rbtcollins@hotmail.com>
16 /*
17 * Portions copyright under the zlib licence - this class was derived from
18 * gzio.c in that library.
21 #include "compress_gz.h"
23 #include <stdexcept>
25 #include <errno.h>
26 #include <memory.h>
27 #include <malloc.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)
44 original = parent;
45 owns_original = true;
46 openmode = "r";
47 construct ();
50 compress_gz::compress_gz (io_stream * parent, const char *_openmode)
52 original = parent;
53 owns_original = true;
54 openmode = _openmode;
55 construct ();
58 void
59 compress_gz::construct ()
61 peeklen = 0;
62 int err;
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 */
67 char *m = fmode;
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;
75 z_err = Z_OK;
76 z_eof = 0;
77 crc = crc32 (0L, Z_NULL, 0);
78 msg = NULL;
79 transparent = 0;
81 mode = '\0';
83 if (!original)
85 z_err = Z_STREAM_ERROR;
86 return;
91 if (*p == 'r')
92 mode = 'r';
93 if (*p == 'w' || *p == 'a')
94 mode = 'w';
95 if (*p >= '0' && *p <= '9')
97 level = *p - '0';
99 else if (*p == 'f')
101 strategy = Z_FILTERED;
103 else if (*p == 'h')
105 strategy = Z_HUFFMAN_ONLY;
107 else
109 *m++ = *p; /* copy the mode */
112 while (*p++ && m != fmode + sizeof (fmode));
113 if (mode == '\0')
115 destroy ();
116 z_err = Z_STREAM_ERROR;
117 return;
121 if (mode == 'w')
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)
130 destroy ();
131 z_err = Z_STREAM_ERROR;
132 return;
135 else
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)
148 destroy ();
149 z_err = Z_STREAM_ERROR;
150 return;
153 stream.avail_out = 16384;
155 errno = 0;
156 if (mode == 'w')
158 /* Write a very simple .gz header:
160 char temp[20];
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);
165 startpos = 10L;
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
169 * necessary.
172 else
175 check_header (); /* skip the .gz header */
176 startpos = (original->tell () - stream.avail_in);
179 return;
182 /* ===========================================================================
183 Outputs a long in LSB order to the given file
185 void
186 compress_gz::putLong (unsigned long x)
188 int n;
189 for (n = 0; n < 4; n++)
191 unsigned char c = (unsigned char) (x & 0xff);
192 original->write (&c, 1);
193 x = x >> 8;
198 uLong
199 compress_gz::getLong ()
201 uLong x = (uLong) get_byte ();
202 int c;
204 x += ((uLong) get_byte ()) << 8;
205 x += ((uLong) get_byte ()) << 16;
206 c = get_byte ();
207 if (c == EOF)
208 z_err = Z_DATA_ERROR;
209 x += ((uLong) c) << 24;
210 return x;
214 ssize_t
215 compress_gz::read (void *buffer, size_t len)
217 if (!len)
218 return 0;
220 if (peeklen)
222 ssize_t tmplen = std::min (peeklen, len);
223 peeklen -= tmplen;
224 memcpy (buffer, peekbuf, tmplen);
225 memmove (peekbuf, peekbuf + tmplen, tmplen);
226 ssize_t tmpread = read (&((char *) buffer)[tmplen], len - tmplen);
227 if (tmpread >= 0)
228 return tmpread + tmplen;
229 else
230 return tmpread;
233 Bytef *start = (Bytef *) buffer; /* starting point for crc computation */
234 Byte *next_out; /* == stream.next_out but not forced far (for MSDOS) */
236 if (mode != 'r')
238 z_err = Z_STREAM_ERROR;
239 return -1;
242 if (z_err == Z_DATA_ERROR || z_err == Z_ERRNO)
243 return -1;
244 if (z_err == Z_STREAM_END)
245 return 0; /* EOF */
247 next_out = (Byte *) buffer;
248 stream.next_out = (Bytef *) buffer;
249 stream.avail_out = len;
251 while (stream.avail_out != 0)
254 if (transparent)
256 /* Copy first the lookahead bytes: */
257 uInt n = stream.avail_in;
258 if (n > stream.avail_out)
259 n = stream.avail_out;
260 if (n > 0)
262 memcpy (stream.next_out, stream.next_in, n);
263 next_out += n;
264 stream.next_out = next_out;
265 stream.next_in += n;
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;
276 if (len == 0)
277 z_eof = 1;
278 return (int) len;
280 if (stream.avail_in == 0 && !z_eof)
283 errno = 0;
284 stream.avail_in = original->read (inbuf, 16384);
285 if (stream.avail_in <= 0)
287 z_eof = 1;
288 if (original->error ())
290 stream.avail_in = 0;
291 z_err = Z_ERRNO;
292 break;
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;
309 else
311 (void) getLong ();
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:
316 check_header ();
317 if (z_err == Z_OK)
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)
330 break;
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).
342 ssize_t
343 compress_gz::write (const void *buffer, size_t len)
345 if (mode != 'w')
347 z_err = Z_STREAM_ERROR;
348 return -1;
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)
363 z_err = Z_ERRNO;
364 break;
366 stream.avail_out = 16384;
368 z_err = deflate (&(stream), Z_NO_FLUSH);
369 if (z_err != Z_OK)
370 break;
372 crc = crc32 (crc, (const Bytef *) buffer, len);
374 return (int) (len - stream.avail_in);
377 ssize_t
378 compress_gz::peek (void *buffer, size_t len)
380 if (mode != 'r')
382 z_err = Z_STREAM_ERROR;
383 return -1;
385 /* can only peek 512 bytes */
386 if (len > 512)
388 z_err = ENOMEM;
389 return -1;
392 if (len > peeklen)
394 size_t want = len - peeklen;
395 ssize_t got = read (&peekbuf[peeklen], want);
396 if (got >= 0)
397 peeklen += got;
398 else
399 /* error */
400 return got;
401 /* we may have read less than requested. */
402 memcpy (buffer, peekbuf, peeklen);
403 return peeklen;
405 else
407 memcpy (buffer, peekbuf, len);
408 return len;
412 off_t
413 compress_gz::tell ()
415 throw new std::logic_error("compress_gz::tell is not implemented");
418 off_t
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);
424 destroy();
425 construct();
426 return result;
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)
436 return z_err;
437 return 0;
441 compress_gz::set_mtime (time_t time)
443 if (original)
444 return original->set_mtime (time);
445 return 1;
448 time_t
449 compress_gz::get_mtime ()
451 if (original)
452 return original->get_mtime ();
453 return 0;
456 mode_t
457 compress_gz::get_mode ()
459 if (original)
460 return original->get_mode ();
461 return 0;
464 void
465 compress_gz::release_original ()
467 owns_original = false;
470 void
471 compress_gz::destroy ()
473 if (msg)
475 free (msg);
476 msg = NULL;
479 if (stream.state != NULL)
481 if (mode == 'w')
483 z_err = deflateEnd (&(stream));
485 else if (mode == 'r')
487 z_err = inflateEnd (&(stream));
491 if (inbuf)
493 free (inbuf);
494 inbuf = NULL;
496 if (outbuf)
498 free (outbuf);
499 outbuf = NULL;
503 compress_gz::~compress_gz ()
505 if (mode == 'w')
507 z_err = do_flush (Z_FINISH);
508 if (z_err == Z_OK)
510 putLong (crc);
511 putLong (stream.total_in);
514 destroy ();
515 if (original && owns_original)
516 delete original;
520 compress_gz::do_flush (int flush)
522 uInt len;
523 int done = 0;
524 if (mode != 'w')
525 return Z_STREAM_ERROR;
526 stream.avail_in = 0; /* should be zero already anyway */
527 for (;;)
529 len = 16384 - stream.avail_out;
530 if (len != 0)
532 if ((uInt) original->write (outbuf, len) != len)
534 z_err = Z_ERRNO;
535 return Z_ERRNO;
537 stream.next_out = outbuf;
538 stream.avail_out = 16384;
540 if (done)
541 break;
542 z_err = deflate (&(stream), flush);
543 /* Ignore the second of two consecutive flushes: */
544 if (len == 0 && z_err == Z_BUF_ERROR)
545 z_err = Z_OK;
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)
551 break;
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
558 * for end of file.
559 * IN assertion: the stream s has been sucessfully opened for reading.
562 compress_gz::get_byte ()
564 if (z_eof)
565 return EOF;
566 if (stream.avail_in == 0)
568 errno = 0;
569 stream.avail_in = original->read (inbuf, 16384);
570 if (stream.avail_in <= 0)
572 z_eof = 1;
573 if (original->error ())
574 z_err = Z_ERRNO;
575 stream.avail_in = 0;
576 return EOF;
578 stream.next_in = inbuf;
580 stream.avail_in--;
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
589 is incorrect.
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.
594 void
595 compress_gz::check_header ()
597 int method; /* method byte */
598 int flags; /* flags byte */
599 uInt len;
600 int c;
601 /* Check the gzip magic header */
602 for (len = 0; len < 2; len++)
604 c = get_byte ();
605 if (c != gz_magic[len])
607 if (len != 0)
608 stream.avail_in++, stream.next_in--;
609 if (c != EOF)
611 stream.avail_in++, stream.next_in--;
612 transparent = 1;
614 z_err = stream.avail_in != 0 ? Z_OK : Z_STREAM_END;
615 return;
618 method = get_byte ();
619 flags = get_byte ();
620 if (method != Z_DEFLATED || (flags & RESERVED) != 0)
622 z_err = Z_DATA_ERROR;
623 return;
626 /* Discard time, xflags and OS code: */
627 for (len = 0; len < 6; len++)
628 (void) get_byte ();
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++)
647 (void) get_byte ();
649 z_err = z_eof ? Z_DATA_ERROR : Z_OK;