1 /* zip.c -- compress files to the gzip or pkzip format
3 Copyright (C) 1997-1999, 2006-2007, 2009-2018 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)
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. */
26 local ulg crc
; /* crc on uncompressed file data */
27 off_t header_bytes
; /* number of bytes in gzip header */
29 /* ===========================================================================
31 * IN assertions: the input and output buffers are cleared.
32 * The variables time_stamp and save_orig_name are initialized.
35 int in
, out
; /* input and output file descriptors */
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 */
46 /* Write the header to the gzip file. See algorithm.doc for the format */
49 put_byte(GZIP_MAGIC
[0]); /* magic header */
50 put_byte(GZIP_MAGIC
[1]);
51 put_byte(DEFLATED
); /* compression method */
56 put_byte(flags
); /* general flags */
57 if (time_stamp
.tv_nsec
< 0)
59 else if (0 < time_stamp
.tv_sec
&& time_stamp
.tv_sec
<= 0xffffffff)
60 stamp
= time_stamp
.tv_sec
;
63 /* It's intended that timestamp 0 generates this warning,
64 since gzip format reserves 0 for something else. */
65 warning ("file timestamp out of range for gzip format");
70 /* Write deflated file to zip file */
74 ct_init(&attr
, &method
);
75 lm_init(level
, &deflate_flags
);
77 put_byte((uch
)deflate_flags
); /* extra flags */
78 put_byte(OS_CODE
); /* OS identifier */
81 char *p
= gzip_base_name (ifname
); /* Don't save the directory part. */
86 header_bytes
= (off_t
)outcnt
;
92 * (but not on MSDOS -- diet in TSR mode reports an incorrect file size)
94 if (ifile_size
!= -1L && bytes_in
!= ifile_size
) {
95 fprintf(stderr
, "%s: %s: file size changed while zipping\n",
96 program_name
, ifname
);
100 /* Write the crc and uncompressed size */
102 put_long((ulg
)bytes_in
);
110 /* ===========================================================================
111 * Read a new buffer from the current input file, perform end-of-line
112 * translation, and update the crc and input file size.
113 * IN assertion: size >= 2 (for end-of-line translation)
115 int file_read(buf
, size
)
121 Assert(insize
== 0, "inbuf not empty");
123 len
= read_buffer (ifd
, buf
, size
);
124 if (len
== 0) return (int)len
;
125 if (len
== (unsigned)-1) {
130 crc
= updcrc((uch
*)buf
, len
);
131 bytes_in
+= (off_t
)len
;