Debian Codegraph @

zgz/gzip/gzip.c

raw
1 /* zip.c -- compress files to the gzip or pkzip format
2
3 Copyright (C) 1997, 1998, 1999, 2006, 2007 Free Software Foundation, Inc.
4 Copyright (C) 1992-1993 Jean-loup Gailly
5 Copyright (C) 2008 Josh Triplett
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20
21 #include <ctype.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24
25 #include "gzip.h"
26
27 uch outbuf[OUTBUFSIZ];
28 unsigned outcnt; /* bytes in output buffer */
29
30 static ulg crc; /* crc on uncompressed file data */
31
32 static int ifd; /* input file descriptor */
33 static int ofd; /* output file descriptor */
34
35 static off_t bytes_in; /* number of input bytes */
36
37 /* ===========================================================================
38 * Deflate in to out.
39 * IN assertions: the input and output buffers are cleared.
40 */
41 void gnuzip(int in, int out, char *origname, ulg timestamp, int level, int osflag, int rsync, int rsync14, int rsync16)
42 {
43 uch flags = 0; /* general purpose bit flags */
44 ush deflate_flags = 0; /* pkzip -es, -en or -ex equivalent */
45
46 ifd = in;
47 ofd = out;
48 outcnt = 0;
49 bytes_in = 0L;
50
51 /* Write the header to the gzip file. */
52
53 put_byte(GZIP_MAGIC[0]); /* magic header */
54 put_byte(GZIP_MAGIC[1]);
55 put_byte(DEFLATED); /* compression method */
56
57 if (origname)
58 flags |= ORIG_NAME;
59 put_byte(flags); /* general flags */
60 put_long(timestamp);
61
62 /* Write deflated file to zip file */
63 crc = updcrc(0, 0);
64
65 bi_init(out);
66 ct_init();
67 lm_init(level, &deflate_flags);
68
69 put_byte((uch)deflate_flags); /* extra flags */
70 put_byte(osflag); /* OS identifier */
71
72 if (origname) {
73 char *p = origname;
74 do {
75 put_byte(*p);
76 } while (*p++);
77 }
78
79 gnu_deflate(level, rsync, rsync14, rsync16);
80
81 /* Write the crc and uncompressed size */
82 put_long(crc);
83 put_long((ulg)bytes_in);
84
85 flush_outbuf();
86 }
87
88
89 /* ===========================================================================
90 * Read a new buffer from the current input file, perform end-of-line
91 * translation, and update the crc and input file size.
92 * IN assertion: size >= 2 (for end-of-line translation)
93 */
94 int file_read(char *buf, unsigned size)
95 {
96 unsigned len;
97
98 len = read_buffer (ifd, buf, size);
99 if (len == 0) return (int)len;
100 if (len == (unsigned)-1) {
101 read_error();
102 return EOF;
103 }
104
105 crc = updcrc((uch*)buf, len);
106 bytes_in += (off_t)len;
107 return (int)len;
108 }
109
110 /* ===========================================================================
111 * Write the output buffer outbuf[0..outcnt-1].
112 * (used for the compressed data only)
113 */
114 void flush_outbuf(void)
115 {
116 if (outcnt == 0) return;
117
118 write_buf(ofd, (char *)outbuf, outcnt);
119 outcnt = 0;
120 }