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