maint: update bootstrap and m4/.gitignore to latest Gnulib
[gzip.git] / zip.c
blob5c95499e759853b43e3507b08c7163e3c9f4d665
1 /* zip.c -- compress files to the gzip or pkzip format
3 Copyright (C) 1997-1999, 2006-2007, 2009-2024 Free Software Foundation, Inc.
4 Copyright (C) 1992-1993 Jean-loup Gailly
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.
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.
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. */
20 #include <config.h>
21 #include <ctype.h>
23 #include "tailor.h"
24 #include "gzip.h"
26 off_t header_bytes; /* number of bytes in gzip header */
28 /* Speed options for the general purpose bit flag. */
29 enum { SLOW = 2, FAST = 4 };
31 /* ===========================================================================
32 * Deflate in to out.
33 * IN assertions: the input and output buffers are cleared.
34 * The variables time_stamp and save_orig_name are initialized.
35 * 'in' and 'out' are input and output file descriptors.
37 int
38 zip (int in, int out)
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;
45 ifd = in;
46 ofd = out;
47 outcnt = 0;
49 /* Write the header to the gzip file. See algorithm.doc for the format */
51 method = DEFLATED;
52 put_byte(GZIP_MAGIC[0]); /* magic header */
53 put_byte(GZIP_MAGIC[1]);
54 put_byte(DEFLATED); /* compression method */
56 if (save_orig_name) {
57 flags |= ORIG_NAME;
59 put_byte(flags); /* general flags */
60 if (time_stamp.tv_nsec < 0)
61 stamp = 0;
62 else if (0 < time_stamp.tv_sec && time_stamp.tv_sec <= 0xffffffff)
63 stamp = time_stamp.tv_sec;
64 else
66 /* It's intended that timestamp 0 generates this warning,
67 since gzip format reserves 0 for something else. */
68 warning ("file timestamp out of range for gzip format");
69 stamp = 0;
71 put_long (stamp);
73 /* Write deflated file to zip file */
74 updcrc (NULL, 0);
76 bi_init(out);
77 ct_init(&attr, &method);
78 if (level == 1)
79 deflate_flags |= FAST;
80 else if (level == 9)
81 deflate_flags |= SLOW;
83 put_byte((uch)deflate_flags); /* extra flags */
84 put_byte(OS_CODE); /* OS identifier */
86 if (save_orig_name) {
87 char *p = gzip_base_name (ifname); /* Don't save the directory part. */
88 do {
89 put_byte (*p);
90 } while (*p++);
92 header_bytes = (off_t)outcnt;
94 #ifdef IBM_Z_DFLTCC
95 dfltcc_deflate (level);
96 #else
97 deflate (level);
98 #endif
100 #ifndef NO_SIZE_CHECK
101 /* Check input size
102 * (but not on MSDOS -- diet in TSR mode reports an incorrect file size)
104 if (ifile_size != -1L && bytes_in != ifile_size) {
105 fprintf(stderr, "%s: %s: file size changed while zipping\n",
106 program_name, ifname);
108 #endif
110 /* Write the crc and uncompressed size */
111 put_long (getcrc ());
112 put_long((ulg)bytes_in);
113 header_bytes += 2*4;
115 flush_outbuf();
116 return OK;
120 /* ===========================================================================
121 * Read a new buffer from the current input file, perform end-of-line
122 * translation, and update the crc and input file size.
123 * IN assertion: size >= 2 (for end-of-line translation)
126 file_read (char *buf, unsigned size)
128 unsigned len;
130 Assert(insize == 0, "inbuf not empty");
132 len = read_buffer (ifd, buf, size);
133 if (len == 0) return (int)len;
134 if (len == (unsigned)-1) {
135 read_error();
138 updcrc ((uch *) buf, len);
139 bytes_in += (off_t)len;
140 return (int)len;