Imported Upstream version 1.6
[debian/gzip] / zip.c
1 /* zip.c -- compress files to the gzip or pkzip format
2
3    Copyright (C) 1997-1999, 2006-2007, 2009-2013 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 3, 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 #include <config.h>
21 #include <ctype.h>
22
23 #include "tailor.h"
24 #include "gzip.h"
25
26 #include <unistd.h>
27 #include <fcntl.h>
28
29 local ulg crc;       /* crc on uncompressed file data */
30 off_t header_bytes;   /* number of bytes in gzip header */
31
32 /* ===========================================================================
33  * Deflate in to out.
34  * IN assertions: the input and output buffers are cleared.
35  *   The variables time_stamp and save_orig_name are initialized.
36  */
37 int zip(in, out)
38     int in, out;            /* input and output file descriptors */
39 {
40     uch  flags = 0;         /* general purpose bit flags */
41     ush  attr = 0;          /* ascii/binary flag */
42     ush  deflate_flags = 0; /* pkzip -es, -en or -ex equivalent */
43     ulg  stamp;
44
45     ifd = in;
46     ofd = out;
47     outcnt = 0;
48
49     /* Write the header to the gzip file. See algorithm.doc for the format */
50
51     method = DEFLATED;
52     put_byte(GZIP_MAGIC[0]); /* magic header */
53     put_byte(GZIP_MAGIC[1]);
54     put_byte(DEFLATED);      /* compression method */
55
56     if (save_orig_name) {
57         flags |= ORIG_NAME;
58     }
59     put_byte(flags);         /* general flags */
60     stamp = (0 <= time_stamp.tv_sec && time_stamp.tv_sec <= 0xffffffff
61              ? (ulg) time_stamp.tv_sec
62              : (ulg) 0);
63     put_long (stamp);
64
65     /* Write deflated file to zip file */
66     crc = updcrc(0, 0);
67
68     bi_init(out);
69     ct_init(&attr, &method);
70     lm_init(level, &deflate_flags);
71
72     put_byte((uch)deflate_flags); /* extra flags */
73     put_byte(OS_CODE);            /* OS identifier */
74
75     if (save_orig_name) {
76         char *p = gzip_base_name (ifname); /* Don't save the directory part. */
77         do {
78             put_byte (*p);
79         } while (*p++);
80     }
81     header_bytes = (off_t)outcnt;
82
83     (void)deflate();
84
85 #ifndef NO_SIZE_CHECK
86   /* Check input size (but not in VMS -- variable record lengths mess it up)
87    * and not on MSDOS -- diet in TSR mode reports an incorrect file size)
88    */
89     if (ifile_size != -1L && bytes_in != ifile_size) {
90         fprintf(stderr, "%s: %s: file size changed while zipping\n",
91                 program_name, ifname);
92     }
93 #endif
94
95     /* Write the crc and uncompressed size */
96     put_long(crc);
97     put_long((ulg)bytes_in);
98     header_bytes += 2*4;
99
100     flush_outbuf();
101     return OK;
102 }
103
104
105 /* ===========================================================================
106  * Read a new buffer from the current input file, perform end-of-line
107  * translation, and update the crc and input file size.
108  * IN assertion: size >= 2 (for end-of-line translation)
109  */
110 int file_read(buf, size)
111     char *buf;
112     unsigned size;
113 {
114     unsigned len;
115
116     Assert(insize == 0, "inbuf not empty");
117
118     len = read_buffer (ifd, buf, size);
119     if (len == 0) return (int)len;
120     if (len == (unsigned)-1) {
121         read_error();
122         return EOF;
123     }
124
125     crc = updcrc((uch*)buf, len);
126     bytes_in += (off_t)len;
127     return (int)len;
128 }