Annotation of src/usr.bin/compress/gzopen.c, Revision 1.10
1.10 ! mickey 1: /* $OpenBSD: gzopen.c,v 1.9 2003/06/29 21:14:37 millert Exp $ */
1.1 mickey 2:
3: /*
4: * Copyright (c) 1997 Michael Shalayeff
5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: *
1.5 mickey 16: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1.1 mickey 18: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26: * SUCH DAMAGE.
27: *
28: */
29: /* this is partially derived from the zlib's gzio.c file, so the notice: */
30: /*
31: zlib.h -- interface of the 'zlib' general purpose compression library
32: version 1.0.4, Jul 24th, 1996.
33:
34: Copyright (C) 1995-1996 Jean-loup Gailly and Mark Adler
35:
36: This software is provided 'as-is', without any express or implied
37: warranty. In no event will the authors be held liable for any damages
38: arising from the use of this software.
39:
40: Permission is granted to anyone to use this software for any purpose,
41: including commercial applications, and to alter it and redistribute it
42: freely, subject to the following restrictions:
43:
44: 1. The origin of this software must not be misrepresented; you must not
45: claim that you wrote the original software. If you use this software
46: in a product, an acknowledgment in the product documentation would be
47: appreciated but is not required.
48: 2. Altered source versions must be plainly marked as such, and must not be
49: misrepresented as being the original software.
50: 3. This notice may not be removed or altered from any source distribution.
51:
52: Jean-loup Gailly Mark Adler
53: gzip@prep.ai.mit.edu madler@alumni.caltech.edu
54:
55:
56: The data format used by the zlib library is described by RFCs (Request for
57: Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt
58: (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
59: */
60:
1.5 mickey 61: const char gz_rcsid[] =
1.10 ! mickey 62: "$OpenBSD: gzopen.c,v 1.9 2003/06/29 21:14:37 millert Exp $";
1.5 mickey 63:
1.1 mickey 64: #include <sys/types.h>
65: #include <sys/stat.h>
66: #include <stdio.h>
67: #include <stdlib.h>
68: #include <errno.h>
69: #include <unistd.h>
70: #include <zlib.h>
71: #include "compress.h"
72:
73: /* gzip flag byte */
74: #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
75: #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
76: #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
77: #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
78: #define COMMENT 0x10 /* bit 4 set: file comment present */
79: #define RESERVED 0xE0 /* bits 5..7: reserved */
80:
81: #define DEF_MEM_LEVEL 8
82: #define OS_CODE 0x03 /* unix */
83:
84: typedef
85: struct gz_stream {
86: int z_fd; /* .gz file */
87: z_stream z_stream; /* libz stream */
88: int z_eof; /* set if end of input file */
89: u_char z_buf[Z_BUFSIZE]; /* i/o buffer */
90: u_int32_t z_crc; /* crc32 of uncompressed data */
91: char z_mode; /* 'w' or 'r' */
92:
93: } gz_stream;
94:
1.5 mickey 95: static const u_char gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
1.1 mickey 96:
1.3 millert 97: static int put_int32(gz_stream *, u_int32_t);
98: static u_int32_t get_int32(gz_stream *);
99: static int get_header(gz_stream *);
100: static int get_byte(gz_stream *);
1.1 mickey 101:
102: int
1.7 deraadt 103: gz_check_header(int fd, struct stat *sb, const char *ofn)
1.1 mickey 104: {
105: int f;
106: u_char buf[sizeof(gz_magic)];
107: off_t off = lseek(fd, 0, SEEK_CUR);
108:
109: f = (read(fd, buf, sizeof(buf)) == sizeof(buf) &&
1.7 deraadt 110: !memcmp(buf, gz_magic, sizeof(buf)));
1.1 mickey 111:
112: lseek (fd, off, SEEK_SET);
113:
114: return f;
115: }
116:
117: void *
1.7 deraadt 118: gz_open(int fd, const char *mode, int bits)
1.1 mickey 119: {
120: gz_stream *s;
121:
122: if (fd < 0 || !mode)
123: return NULL;
124:
125: if ((mode[0] != 'r' && mode[0] != 'w') || mode[1] != '\0' ||
126: bits < 0 || bits > Z_BEST_COMPRESSION) {
127: errno = EINVAL;
128: return NULL;
129: }
1.4 aaron 130: if ((s = (gz_stream *)calloc(1, sizeof(gz_stream))) == NULL)
1.1 mickey 131: return NULL;
132:
133: s->z_stream.zalloc = (alloc_func)0;
134: s->z_stream.zfree = (free_func)0;
135: s->z_stream.opaque = (voidpf)0;
136: s->z_stream.next_in = Z_NULL;
137: s->z_stream.next_out = Z_NULL;
138: s->z_stream.avail_in = s->z_stream.avail_out = 0;
139: s->z_fd = 0;
140: s->z_eof = 0;
141: s->z_crc = crc32(0L, Z_NULL, 0);
142: s->z_mode = mode[0];
143:
144: if (s->z_mode == 'w') {
145: /* windowBits is passed < 0 to suppress zlib header */
146: if (deflateInit2(&(s->z_stream), bits, Z_DEFLATED,
147: -MAX_WBITS, DEF_MEM_LEVEL, 0) != Z_OK) {
148: free (s);
149: return NULL;
150: }
151: s->z_stream.next_out = s->z_buf;
152: } else {
153: if (inflateInit2(&(s->z_stream), -MAX_WBITS) != Z_OK) {
154: free (s);
155: return NULL;
156: }
157: s->z_stream.next_in = s->z_buf;
158: }
159: s->z_stream.avail_out = Z_BUFSIZE;
160:
161: errno = 0;
162: s->z_fd = fd;
163:
164: if (s->z_mode == 'w') {
165: u_char buf[10];
166: /* Write a very simple .gz header: */
167: buf[0] = gz_magic[0];
168: buf[1] = gz_magic[1];
169: buf[2] = Z_DEFLATED;
170: buf[3] = 0 /*flags*/;
171: buf[4] = buf[5] = buf[6] = buf[7] = 0 /*time*/;
172: buf[8] = 0 /*xflags*/;
173: buf[9] = OS_CODE;
174: if (write(fd, buf, sizeof(buf)) != sizeof(buf)) {
175: gz_close(s);
176: s = NULL;
177: }
178: } else {
179: if (get_header(s) != 0) { /* skip the .gz header */
180: gz_close (s);
181: s = NULL;
182: }
183: }
184:
185: return s;
186: }
187:
188: int
1.7 deraadt 189: gz_close(void *cookie)
1.1 mickey 190: {
1.2 mpech 191: gz_stream *s = (gz_stream*)cookie;
1.1 mickey 192: int err = 0;
193:
194: if (s == NULL)
195: return -1;
196:
197: if (s->z_mode == 'w' && (err = gz_flush (s, Z_FINISH)) == Z_OK) {
198: if ((err = put_int32 (s, s->z_crc)) == Z_OK)
199: err = put_int32 (s, s->z_stream.total_in);
200: }
201:
202: if (!err && s->z_stream.state != NULL) {
203: if (s->z_mode == 'w')
204: err = deflateEnd(&s->z_stream);
205: else if (s->z_mode == 'r')
206: err = inflateEnd(&s->z_stream);
207: }
1.10 ! mickey 208:
! 209: if (!err)
! 210: err = close(s->z_fd);
! 211: else
! 212: (void)close(s->z_fd);
1.1 mickey 213:
214: free(s);
215:
216: return err;
217: }
218:
219: int
1.7 deraadt 220: gz_flush(void *cookie, int flush)
1.1 mickey 221: {
1.2 mpech 222: gz_stream *s = (gz_stream*)cookie;
1.1 mickey 223: size_t len;
224: int done = 0;
225: int err;
226:
227: if (s == NULL || s->z_mode != 'w') {
228: errno = EBADF;
229: return Z_ERRNO;
230: }
231:
232: s->z_stream.avail_in = 0; /* should be zero already anyway */
233:
234: for (;;) {
235: len = Z_BUFSIZE - s->z_stream.avail_out;
236:
237: if (len != 0) {
238: if (write(s->z_fd, s->z_buf, len) != len)
239: return Z_ERRNO;
240: s->z_stream.next_out = s->z_buf;
241: s->z_stream.avail_out = Z_BUFSIZE;
242: }
243: if (done)
244: break;
245: if ((err = deflate(&(s->z_stream), flush)) != Z_OK &&
246: err != Z_STREAM_END)
247: return err;
248:
249: /* deflate has finished flushing only when it hasn't
250: * used up all the available space in the output buffer
251: */
252: done = (s->z_stream.avail_out != 0 || err == Z_STREAM_END);
253: }
254: return 0;
255: }
256:
257: static int
1.7 deraadt 258: put_int32(gz_stream *s, u_int32_t x)
1.1 mickey 259: {
1.8 millert 260: u_int32_t y = htole32(x);
261:
262: if (write(s->z_fd, &y, sizeof(y)) != sizeof(y))
1.1 mickey 263: return Z_ERRNO;
264: return 0;
265: }
266:
267: static int
1.7 deraadt 268: get_byte(gz_stream *s)
1.1 mickey 269: {
270: if (s->z_eof)
271: return EOF;
1.5 mickey 272:
1.1 mickey 273: if (s->z_stream.avail_in == 0) {
274: errno = 0;
275: s->z_stream.avail_in = read(s->z_fd, s->z_buf, Z_BUFSIZE);
276: if (s->z_stream.avail_in <= 0) {
277: s->z_eof = 1;
278: return EOF;
279: }
280: s->z_stream.next_in = s->z_buf;
281: }
282: s->z_stream.avail_in--;
283: return *s->z_stream.next_in++;
284: }
285:
1.5 mickey 286: static u_int32_t
1.7 deraadt 287: get_int32(gz_stream *s)
1.1 mickey 288: {
1.2 mpech 289: u_int32_t x;
1.1 mickey 290:
291: x = ((u_int32_t)(get_byte(s) & 0xff));
292: x |= ((u_int32_t)(get_byte(s) & 0xff))<<8;
293: x |= ((u_int32_t)(get_byte(s) & 0xff))<<16;
294: x |= ((u_int32_t)(get_byte(s) & 0xff))<<24;
295: return x;
296: }
297:
298: static int
1.7 deraadt 299: get_header(gz_stream *s)
1.1 mickey 300: {
301: int method; /* method byte */
302: int flags; /* flags byte */
303: uInt len;
304: int c;
305:
306: /* Check the gzip magic header */
307: for (len = 0; len < 2; len++) {
308: c = get_byte(s);
309: if (c != gz_magic[len]) {
310: errno = EFTYPE;
311: return -1;
312: }
313: }
314:
315: method = get_byte(s);
316: flags = get_byte(s);
317: if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
318: errno = EFTYPE;
319: return -1;
320: }
321:
322: /* Discard time, xflags and OS code: */
323: for (len = 0; len < 6; len++)
324: (void)get_byte(s);
325:
326: if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
327: len = (uInt)get_byte(s);
328: len += ((uInt)get_byte(s))<<8;
329: /* len is garbage if EOF but the loop below will quit anyway */
330: while (len-- != 0 && get_byte(s) != EOF)
331: ;
332: }
333:
334: if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
335: while ((c = get_byte(s)) != 0 && c != EOF) ;
336: }
337:
338: if ((flags & COMMENT) != 0) { /* skip the .gz file comment */
339: while ((c = get_byte(s)) != 0 && c != EOF) ;
340: }
341:
342: if ((flags & HEAD_CRC) != 0) { /* skip the header crc */
343: for (len = 0; len < 2; len++) (void)get_byte(s);
344: }
345:
346: if (s->z_eof) {
347: errno = EFTYPE;
348: return -1;
349: }
350:
351: return 0;
352: }
353:
354: int
1.7 deraadt 355: gz_read(void *cookie, char *buf, int len)
1.1 mickey 356: {
1.2 mpech 357: gz_stream *s = (gz_stream*)cookie;
1.1 mickey 358: u_char *start = buf; /* starting point for crc computation */
359:
360: s->z_stream.next_out = buf;
361: s->z_stream.avail_out = len;
362:
363: while (s->z_stream.avail_out != 0 && !s->z_eof) {
364:
365: if (s->z_stream.avail_in == 0) {
366:
367: errno = 0;
368: if ((s->z_stream.avail_in =
1.7 deraadt 369: read(s->z_fd, s->z_buf, Z_BUFSIZE)) == 0)
1.1 mickey 370: s->z_eof = 1;
371: s->z_stream.next_in = s->z_buf;
372: }
373:
374: if (inflate(&(s->z_stream), Z_NO_FLUSH) == Z_STREAM_END) {
375: /* Check CRC and original size */
376: s->z_crc = crc32(s->z_crc, start,
1.7 deraadt 377: (uInt)(s->z_stream.next_out - start));
1.1 mickey 378: start = s->z_stream.next_out;
379:
1.9 millert 380: if (get_int32(s) != s->z_crc) {
381: errno = EINVAL;
382: return -1;
383: }
384: if (get_int32(s) != s->z_stream.total_out) {
1.7 deraadt 385: errno = EIO;
1.1 mickey 386: return -1;
387: }
388: s->z_eof = 1;
389: break;
390: }
391: }
392: s->z_crc = crc32(s->z_crc, start,
1.7 deraadt 393: (uInt)(s->z_stream.next_out - start));
1.1 mickey 394:
395: return (int)(len - s->z_stream.avail_out);
396: }
397:
398: int
1.7 deraadt 399: gz_write(void *cookie, const char *buf, int len)
1.1 mickey 400: {
1.2 mpech 401: gz_stream *s = (gz_stream*)cookie;
1.1 mickey 402:
403: s->z_stream.next_in = (char *)buf;
404: s->z_stream.avail_in = len;
405:
406: while (s->z_stream.avail_in != 0) {
407: if (s->z_stream.avail_out == 0) {
408: if (write(s->z_fd, s->z_buf, Z_BUFSIZE) != Z_BUFSIZE)
409: break;
410: s->z_stream.next_out = s->z_buf;
411: s->z_stream.avail_out = Z_BUFSIZE;
412: }
413: if (deflate(&(s->z_stream), Z_NO_FLUSH) != Z_OK)
414: break;
415: }
416: s->z_crc = crc32(s->z_crc, buf, len);
417:
418: return (int)(len - s->z_stream.avail_in);
419: }
420: