[3325] | 1 | /* zip.c -- compress files to the gzip or pkzip format
|
---|
| 2 |
|
---|
| 3 | Copyright (C) 1997, 1998, 1999, 2006 Free Software Foundation, Inc.
|
---|
| 4 | Copyright (C) 1992-1993 Jean-loup Gailly
|
---|
| 5 |
|
---|
| 6 | This program is free software; you can redistribute it and/or modify
|
---|
| 7 | it under the terms of the GNU General Public License as published by
|
---|
| 8 | the Free Software Foundation; either version 2, or (at your option)
|
---|
| 9 | any later version.
|
---|
| 10 |
|
---|
| 11 | This program is distributed in the hope that it will be useful,
|
---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 14 | GNU General Public License for more details.
|
---|
| 15 |
|
---|
| 16 | You should have received a copy of the GNU General Public License
|
---|
| 17 | along with this program; if not, write to the Free Software Foundation,
|
---|
| 18 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
|
---|
| 19 |
|
---|
| 20 | #ifdef RCSID
|
---|
| 21 | static char rcsid[] = "$Id: zip.c,v 1.6 2006/12/11 18:54:39 eggert Exp $";
|
---|
| 22 | #endif
|
---|
| 23 |
|
---|
| 24 | #include <config.h>
|
---|
| 25 | #include <ctype.h>
|
---|
| 26 |
|
---|
| 27 | #include "tailor.h"
|
---|
| 28 | #include "gzip.h"
|
---|
| 29 | #include "crypt.h"
|
---|
| 30 |
|
---|
| 31 | #include "timespec.h"
|
---|
| 32 |
|
---|
| 33 | #ifdef HAVE_UNISTD_H
|
---|
| 34 | # include <unistd.h>
|
---|
| 35 | #endif
|
---|
| 36 | #ifdef HAVE_FCNTL_H
|
---|
| 37 | # include <fcntl.h>
|
---|
| 38 | #endif
|
---|
| 39 |
|
---|
| 40 | local ulg crc; /* crc on uncompressed file data */
|
---|
| 41 | off_t header_bytes; /* number of bytes in gzip header */
|
---|
| 42 |
|
---|
| 43 | /* ===========================================================================
|
---|
| 44 | * Deflate in to out.
|
---|
| 45 | * IN assertions: the input and output buffers are cleared.
|
---|
| 46 | * The variables time_stamp and save_orig_name are initialized.
|
---|
| 47 | */
|
---|
| 48 | int zip(in, out)
|
---|
| 49 | int in, out; /* input and output file descriptors */
|
---|
| 50 | {
|
---|
| 51 | uch flags = 0; /* general purpose bit flags */
|
---|
| 52 | ush attr = 0; /* ascii/binary flag */
|
---|
| 53 | ush deflate_flags = 0; /* pkzip -es, -en or -ex equivalent */
|
---|
| 54 | ulg stamp;
|
---|
| 55 |
|
---|
| 56 | ifd = in;
|
---|
| 57 | ofd = out;
|
---|
| 58 | outcnt = 0;
|
---|
| 59 |
|
---|
| 60 | /* Write the header to the gzip file. See algorithm.doc for the format */
|
---|
| 61 |
|
---|
| 62 | method = DEFLATED;
|
---|
| 63 | put_byte(GZIP_MAGIC[0]); /* magic header */
|
---|
| 64 | put_byte(GZIP_MAGIC[1]);
|
---|
| 65 | put_byte(DEFLATED); /* compression method */
|
---|
| 66 |
|
---|
| 67 | if (save_orig_name) {
|
---|
| 68 | flags |= ORIG_NAME;
|
---|
| 69 | }
|
---|
| 70 | put_byte(flags); /* general flags */
|
---|
| 71 | stamp = (0 <= time_stamp.tv_sec && time_stamp.tv_sec <= 0xffffffff
|
---|
| 72 | ? (ulg) time_stamp.tv_sec
|
---|
| 73 | : (ulg) 0);
|
---|
| 74 | put_long (stamp);
|
---|
| 75 |
|
---|
| 76 | /* Write deflated file to zip file */
|
---|
| 77 | crc = updcrc(0, 0);
|
---|
| 78 |
|
---|
| 79 | bi_init(out);
|
---|
| 80 | ct_init(&attr, &method);
|
---|
| 81 | lm_init(level, &deflate_flags);
|
---|
| 82 |
|
---|
| 83 | put_byte((uch)deflate_flags); /* extra flags */
|
---|
| 84 | put_byte(OS_CODE); /* OS identifier */
|
---|
| 85 |
|
---|
| 86 | if (save_orig_name) {
|
---|
| 87 | char *p = gzip_base_name (ifname); /* Don't save the directory part. */
|
---|
| 88 | do {
|
---|
| 89 | put_char(*p);
|
---|
| 90 | } while (*p++);
|
---|
| 91 | }
|
---|
| 92 | header_bytes = (off_t)outcnt;
|
---|
| 93 |
|
---|
| 94 | (void)deflate();
|
---|
| 95 |
|
---|
| 96 | #if !defined(NO_SIZE_CHECK) && !defined(RECORD_IO)
|
---|
| 97 | /* Check input size (but not in VMS -- variable record lengths mess it up)
|
---|
| 98 | * and not on MSDOS -- diet in TSR mode reports an incorrect file size)
|
---|
| 99 | */
|
---|
| 100 | if (ifile_size != -1L && bytes_in != ifile_size) {
|
---|
| 101 | fprintf(stderr, "%s: %s: file size changed while zipping\n",
|
---|
| 102 | program_name, ifname);
|
---|
| 103 | }
|
---|
| 104 | #endif
|
---|
| 105 |
|
---|
| 106 | /* Write the crc and uncompressed size */
|
---|
| 107 | put_long(crc);
|
---|
| 108 | put_long((ulg)bytes_in);
|
---|
| 109 | header_bytes += 2*sizeof(long);
|
---|
| 110 |
|
---|
| 111 | flush_outbuf();
|
---|
| 112 | return OK;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 |
|
---|
| 116 | /* ===========================================================================
|
---|
| 117 | * Read a new buffer from the current input file, perform end-of-line
|
---|
| 118 | * translation, and update the crc and input file size.
|
---|
| 119 | * IN assertion: size >= 2 (for end-of-line translation)
|
---|
| 120 | */
|
---|
| 121 | int file_read(buf, size)
|
---|
| 122 | char *buf;
|
---|
| 123 | unsigned size;
|
---|
| 124 | {
|
---|
| 125 | unsigned len;
|
---|
| 126 |
|
---|
| 127 | Assert(insize == 0, "inbuf not empty");
|
---|
| 128 |
|
---|
| 129 | len = read_buffer (ifd, buf, size);
|
---|
| 130 | if (len == 0) return (int)len;
|
---|
| 131 | if (len == (unsigned)-1) {
|
---|
| 132 | read_error();
|
---|
| 133 | return EOF;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | crc = updcrc((uch*)buf, len);
|
---|
| 137 | bytes_in += (off_t)len;
|
---|
| 138 | return (int)len;
|
---|
| 139 | }
|
---|