xref: /illumos-gate/usr/src/boot/libsa/gzipfs.c (revision fd5b2b1f)
1734b3a42SToomas Soome /*
2199767f8SToomas Soome  * Copyright (c) 1998 Michael Smith.
3199767f8SToomas Soome  * All rights reserved.
4199767f8SToomas Soome  *
5199767f8SToomas Soome  * Redistribution and use in source and binary forms, with or without
6199767f8SToomas Soome  * modification, are permitted provided that the following conditions
7199767f8SToomas Soome  * are met:
8199767f8SToomas Soome  * 1. Redistributions of source code must retain the above copyright
9199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer.
10199767f8SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
11199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
12199767f8SToomas Soome  *    documentation and/or other materials provided with the distribution.
13199767f8SToomas Soome  *
14199767f8SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15199767f8SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16199767f8SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17199767f8SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18199767f8SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19199767f8SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20199767f8SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21199767f8SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22199767f8SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23199767f8SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24199767f8SToomas Soome  * SUCH DAMAGE.
25199767f8SToomas Soome  */
26199767f8SToomas Soome 
27199767f8SToomas Soome #include <sys/cdefs.h>
28199767f8SToomas Soome 
29199767f8SToomas Soome #include "stand.h"
30199767f8SToomas Soome 
31199767f8SToomas Soome #include <sys/stat.h>
32199767f8SToomas Soome #include <string.h>
33199767f8SToomas Soome #include <zlib.h>
34199767f8SToomas Soome 
35*fd5b2b1fSToomas Soome #define	Z_BUFSIZE 16384	/* match NFSREAD_MAX_SIZE */
36199767f8SToomas Soome 
37199767f8SToomas Soome struct z_file
38199767f8SToomas Soome {
39734b3a42SToomas Soome 	int		zf_rawfd;
40734b3a42SToomas Soome 	off_t		zf_dataoffset;
41734b3a42SToomas Soome 	z_stream	zf_zstream;
42734b3a42SToomas Soome 	int		zf_endseen;
43*fd5b2b1fSToomas Soome 	unsigned char	zf_buf[Z_BUFSIZE];
44199767f8SToomas Soome };
45199767f8SToomas Soome 
46734b3a42SToomas Soome static int zf_fill(struct z_file *z);
47734b3a42SToomas Soome static int zf_open(const char *path, struct open_file *f);
48734b3a42SToomas Soome static int zf_close(struct open_file *f);
49734b3a42SToomas Soome static int zf_read(struct open_file *f, void *buf, size_t size, size_t *resid);
50734b3a42SToomas Soome static off_t zf_seek(struct open_file *f, off_t offset, int where);
51734b3a42SToomas Soome static int zf_stat(struct open_file *f, struct stat *sb);
52199767f8SToomas Soome 
53199767f8SToomas Soome struct fs_ops gzipfs_fsops = {
54734b3a42SToomas Soome 	.fs_name = "zip",
55734b3a42SToomas Soome 	.fo_open = zf_open,
56734b3a42SToomas Soome 	.fo_close = zf_close,
57734b3a42SToomas Soome 	.fo_read = zf_read,
58734b3a42SToomas Soome 	.fo_write = null_write,
59734b3a42SToomas Soome 	.fo_seek = zf_seek,
60734b3a42SToomas Soome 	.fo_stat = zf_stat,
61734b3a42SToomas Soome 	.fo_readdir = null_readdir
62199767f8SToomas Soome };
63199767f8SToomas Soome 
64199767f8SToomas Soome static int
zf_fill(struct z_file * zf)65199767f8SToomas Soome zf_fill(struct z_file *zf)
66199767f8SToomas Soome {
67734b3a42SToomas Soome 	int	result;
68734b3a42SToomas Soome 	int	req;
69734b3a42SToomas Soome 
70734b3a42SToomas Soome 	req = Z_BUFSIZE - zf->zf_zstream.avail_in;
71734b3a42SToomas Soome 	result = 0;
72734b3a42SToomas Soome 
73734b3a42SToomas Soome 	/* If we need more */
74734b3a42SToomas Soome 	if (req > 0) {
75734b3a42SToomas Soome 		/* move old data to bottom of buffer */
76734b3a42SToomas Soome 		if (req < Z_BUFSIZE)
77734b3a42SToomas Soome 			bcopy(zf->zf_buf + req, zf->zf_buf, Z_BUFSIZE - req);
78734b3a42SToomas Soome 
79734b3a42SToomas Soome 		/* read to fill buffer and update availibility data */
80734b3a42SToomas Soome 		result = read(zf->zf_rawfd,
81734b3a42SToomas Soome 		    zf->zf_buf + zf->zf_zstream.avail_in, req);
82734b3a42SToomas Soome 		zf->zf_zstream.next_in = zf->zf_buf;
83734b3a42SToomas Soome 		if (result >= 0)
84734b3a42SToomas Soome 			zf->zf_zstream.avail_in += result;
85734b3a42SToomas Soome 	}
86734b3a42SToomas Soome 	return (result);
87199767f8SToomas Soome }
88199767f8SToomas Soome 
89199767f8SToomas Soome /*
90199767f8SToomas Soome  * Adapted from get_byte/check_header in libz
91199767f8SToomas Soome  *
92199767f8SToomas Soome  * Returns 0 if the header is OK, nonzero if not.
93199767f8SToomas Soome  */
94199767f8SToomas Soome static int
get_byte(struct z_file * zf,off_t * curoffp)95199767f8SToomas Soome get_byte(struct z_file *zf, off_t *curoffp)
96199767f8SToomas Soome {
97734b3a42SToomas Soome 	if ((zf->zf_zstream.avail_in == 0) && (zf_fill(zf) == -1))
98734b3a42SToomas Soome 		return (-1);
99734b3a42SToomas Soome 	zf->zf_zstream.avail_in--;
100734b3a42SToomas Soome 	++*curoffp;
101734b3a42SToomas Soome 	return (*(zf->zf_zstream.next_in)++);
102199767f8SToomas Soome }
103199767f8SToomas Soome 
104199767f8SToomas Soome static int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
105199767f8SToomas Soome 
106199767f8SToomas Soome /* gzip flag byte */
107734b3a42SToomas Soome #define	ASCII_FLAG	0x01 /* bit 0 set: file probably ascii text */
108734b3a42SToomas Soome #define	HEAD_CRC	0x02 /* bit 1 set: header CRC present */
109734b3a42SToomas Soome #define	EXTRA_FIELD	0x04 /* bit 2 set: extra field present */
110734b3a42SToomas Soome #define	ORIG_NAME	0x08 /* bit 3 set: original file name present */
111734b3a42SToomas Soome #define	COMMENT		0x10 /* bit 4 set: file comment present */
112734b3a42SToomas Soome #define	RESERVED	0xE0 /* bits 5..7: reserved */
113199767f8SToomas Soome 
114199767f8SToomas Soome static int
check_header(struct z_file * zf)115199767f8SToomas Soome check_header(struct z_file *zf)
116199767f8SToomas Soome {
117734b3a42SToomas Soome 	int	method; /* method byte */
118734b3a42SToomas Soome 	int	flags;  /* flags byte */
119734b3a42SToomas Soome 	uInt	len;
120734b3a42SToomas Soome 	int	c;
121734b3a42SToomas Soome 
122734b3a42SToomas Soome 	zf->zf_dataoffset = 0;
123734b3a42SToomas Soome 	/* Check the gzip magic header */
124734b3a42SToomas Soome 	for (len = 0; len < 2; len++) {
125734b3a42SToomas Soome 		c = get_byte(zf, &zf->zf_dataoffset);
126734b3a42SToomas Soome 		if (c != gz_magic[len]) {
127734b3a42SToomas Soome 			return (1);
128734b3a42SToomas Soome 		}
129734b3a42SToomas Soome 	}
130734b3a42SToomas Soome 	method = get_byte(zf, &zf->zf_dataoffset);
131734b3a42SToomas Soome 	flags = get_byte(zf, &zf->zf_dataoffset);
132734b3a42SToomas Soome 	if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
133734b3a42SToomas Soome 		return (1);
134734b3a42SToomas Soome 	}
135734b3a42SToomas Soome 
136734b3a42SToomas Soome 	/* Discard time, xflags and OS code: */
137734b3a42SToomas Soome 	for (len = 0; len < 6; len++)
138734b3a42SToomas Soome 		(void) get_byte(zf, &zf->zf_dataoffset);
139734b3a42SToomas Soome 
140734b3a42SToomas Soome 	if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
141734b3a42SToomas Soome 		len  =  (uInt)get_byte(zf, &zf->zf_dataoffset);
142734b3a42SToomas Soome 		len += ((uInt)get_byte(zf, &zf->zf_dataoffset))<<8;
143734b3a42SToomas Soome 		/* len is garbage if EOF but the loop below will quit anyway */
144734b3a42SToomas Soome 		while (len-- != 0 && get_byte(zf, &zf->zf_dataoffset) != -1)
145734b3a42SToomas Soome 			;
146734b3a42SToomas Soome 	}
147734b3a42SToomas Soome 	if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
148734b3a42SToomas Soome 		while ((c = get_byte(zf, &zf->zf_dataoffset)) != 0 && c != -1)
149734b3a42SToomas Soome 			;
150734b3a42SToomas Soome 	}
151734b3a42SToomas Soome 	if ((flags & COMMENT) != 0) {   /* skip the .gz file comment */
152734b3a42SToomas Soome 		while ((c = get_byte(zf, &zf->zf_dataoffset)) != 0 && c != -1)
153734b3a42SToomas Soome 			;
154199767f8SToomas Soome 	}
155734b3a42SToomas Soome 	if ((flags & HEAD_CRC) != 0) {  /* skip the header crc */
156734b3a42SToomas Soome 		for (len = 0; len < 2; len++)
157734b3a42SToomas Soome 			c = get_byte(zf, &zf->zf_dataoffset);
158734b3a42SToomas Soome 	}
159734b3a42SToomas Soome 	/* if there's data left, we're in business */
160734b3a42SToomas Soome 	return ((c == -1) ? 1 : 0);
161199767f8SToomas Soome }
162734b3a42SToomas Soome 
163199767f8SToomas Soome static int
zf_open(const char * fname,struct open_file * f)164199767f8SToomas Soome zf_open(const char *fname, struct open_file *f)
165199767f8SToomas Soome {
166734b3a42SToomas Soome 	char		*zfname;
167734b3a42SToomas Soome 	int		rawfd;
168734b3a42SToomas Soome 	struct z_file	*zf;
169734b3a42SToomas Soome 	char		*cp;
170734b3a42SToomas Soome 	int		error;
171734b3a42SToomas Soome 	struct stat	sb;
172734b3a42SToomas Soome 
173734b3a42SToomas Soome 	/* Have to be in "just read it" mode */
174734b3a42SToomas Soome 	if (f->f_flags != F_READ)
175734b3a42SToomas Soome 		return (EPERM);
176734b3a42SToomas Soome 
177734b3a42SToomas Soome 	/* If the name already ends in .gz or .bz2, ignore it */
178734b3a42SToomas Soome 	if ((cp = strrchr(fname, '.')) && (strcmp(cp, ".gz") == 0 ||
179734b3a42SToomas Soome 	    strcmp(cp, ".bz2") == 0 || strcmp(cp, ".split") == 0))
180734b3a42SToomas Soome 		return (ENOENT);
181734b3a42SToomas Soome 
182734b3a42SToomas Soome 	/* Try to open the compressed datafile */
183734b3a42SToomas Soome 	rawfd = open(fname, O_RDONLY | F_GZIP);
184734b3a42SToomas Soome 	if (rawfd == -1) {
185734b3a42SToomas Soome 		/* add .gz sufix and try again */
186734b3a42SToomas Soome 		zfname = malloc(strlen(fname) + 4);
187734b3a42SToomas Soome 		if (zfname == NULL)
188734b3a42SToomas Soome 			return (ENOMEM);
189734b3a42SToomas Soome 		sprintf(zfname, "%s.gz", fname);
190734b3a42SToomas Soome 		rawfd = open(zfname, O_RDONLY);
191734b3a42SToomas Soome 		free(zfname);
192734b3a42SToomas Soome 		if (rawfd == -1)
193734b3a42SToomas Soome 			return (ENOENT);
194734b3a42SToomas Soome 	}
195199767f8SToomas Soome 
196734b3a42SToomas Soome 	if (fstat(rawfd, &sb) < 0) {
197734b3a42SToomas Soome 		printf("zf_open: stat failed\n");
198734b3a42SToomas Soome 		close(rawfd);
199734b3a42SToomas Soome 		return (ENOENT);
200734b3a42SToomas Soome 	}
201734b3a42SToomas Soome 	if (!S_ISREG(sb.st_mode)) {
202734b3a42SToomas Soome 		close(rawfd);
203734b3a42SToomas Soome 		return (EISDIR);	/* best guess */
204734b3a42SToomas Soome 	}
205734b3a42SToomas Soome 
206734b3a42SToomas Soome 	/* Allocate a z_file structure, populate it */
207734b3a42SToomas Soome 	zf = malloc(sizeof (struct z_file));
208734b3a42SToomas Soome 	if (zf == NULL)
209734b3a42SToomas Soome 		return (ENOMEM);
210734b3a42SToomas Soome 	bzero(zf, sizeof (struct z_file));
211734b3a42SToomas Soome 	zf->zf_rawfd = rawfd;
212734b3a42SToomas Soome 
213734b3a42SToomas Soome 	/* Verify that the file is gzipped */
214734b3a42SToomas Soome 	if (check_header(zf)) {
215734b3a42SToomas Soome 		close(zf->zf_rawfd);
216734b3a42SToomas Soome 		free(zf);
217734b3a42SToomas Soome 		return (EFTYPE);
218734b3a42SToomas Soome 	}
219199767f8SToomas Soome 
220734b3a42SToomas Soome 	/* Initialise the inflation engine */
221734b3a42SToomas Soome 	if ((error = inflateInit2(&(zf->zf_zstream), -15)) != Z_OK) {
222734b3a42SToomas Soome 		printf("zf_open: inflateInit returned %d : %s\n", error,
223734b3a42SToomas Soome 		    zf->zf_zstream.msg);
224734b3a42SToomas Soome 		close(zf->zf_rawfd);
225734b3a42SToomas Soome 		free(zf);
226734b3a42SToomas Soome 		return (EIO);
227734b3a42SToomas Soome 	}
228734b3a42SToomas Soome 
229734b3a42SToomas Soome 	/* Looks OK, we'll take it */
230734b3a42SToomas Soome 	f->f_fsdata = zf;
231734b3a42SToomas Soome 	return (0);
232199767f8SToomas Soome }
233199767f8SToomas Soome 
234199767f8SToomas Soome static int
zf_close(struct open_file * f)235199767f8SToomas Soome zf_close(struct open_file *f)
236199767f8SToomas Soome {
237734b3a42SToomas Soome 	struct z_file	*zf = (struct z_file *)f->f_fsdata;
238734b3a42SToomas Soome 
239734b3a42SToomas Soome 	inflateEnd(&(zf->zf_zstream));
240734b3a42SToomas Soome 	close(zf->zf_rawfd);
241734b3a42SToomas Soome 	free(zf);
242734b3a42SToomas Soome 	return (0);
243199767f8SToomas Soome }
244734b3a42SToomas Soome 
245734b3a42SToomas Soome static int
zf_read(struct open_file * f,void * buf,size_t size,size_t * resid)246199767f8SToomas Soome zf_read(struct open_file *f, void *buf, size_t size, size_t *resid)
247199767f8SToomas Soome {
248734b3a42SToomas Soome 	struct z_file	*zf = (struct z_file *)f->f_fsdata;
249734b3a42SToomas Soome 	int		error;
250734b3a42SToomas Soome 
251734b3a42SToomas Soome 	zf->zf_zstream.next_out = buf;		/* where and how much */
252734b3a42SToomas Soome 	zf->zf_zstream.avail_out = size;
253734b3a42SToomas Soome 
254734b3a42SToomas Soome 	while (zf->zf_zstream.avail_out && zf->zf_endseen == 0) {
255734b3a42SToomas Soome 		if ((zf->zf_zstream.avail_in == 0) && (zf_fill(zf) == -1)) {
256734b3a42SToomas Soome 			printf("zf_read: fill error\n");
257734b3a42SToomas Soome 			return (EIO);
258734b3a42SToomas Soome 		}
259734b3a42SToomas Soome 		if (zf->zf_zstream.avail_in == 0) { /* oops, unexpected EOF */
260734b3a42SToomas Soome 			printf("zf_read: unexpected EOF\n");
261734b3a42SToomas Soome 			if (zf->zf_zstream.avail_out == size)
262734b3a42SToomas Soome 				return (EIO);
263734b3a42SToomas Soome 			break;
264734b3a42SToomas Soome 		}
265734b3a42SToomas Soome 
266734b3a42SToomas Soome 		/* decompression pass */
267734b3a42SToomas Soome 		error = inflate(&zf->zf_zstream, Z_SYNC_FLUSH);
268734b3a42SToomas Soome 		if (error == Z_STREAM_END) {	/* EOF, all done */
269734b3a42SToomas Soome 			zf->zf_endseen = 1;
270734b3a42SToomas Soome 			break;
271734b3a42SToomas Soome 		}
272734b3a42SToomas Soome 		if (error != Z_OK) {		/* argh, decompression error */
273734b3a42SToomas Soome 			printf("inflate: %s\n", zf->zf_zstream.msg);
274734b3a42SToomas Soome 			return (EIO);
275734b3a42SToomas Soome 		}
276199767f8SToomas Soome 	}
277734b3a42SToomas Soome 	if (resid != NULL)
278734b3a42SToomas Soome 		*resid = zf->zf_zstream.avail_out;
279734b3a42SToomas Soome 	return (0);
280199767f8SToomas Soome }
281199767f8SToomas Soome 
282199767f8SToomas Soome static int
zf_rewind(struct open_file * f)283199767f8SToomas Soome zf_rewind(struct open_file *f)
284199767f8SToomas Soome {
285734b3a42SToomas Soome 	struct z_file	*zf = (struct z_file *)f->f_fsdata;
286199767f8SToomas Soome 
287734b3a42SToomas Soome 	if (lseek(zf->zf_rawfd, zf->zf_dataoffset, SEEK_SET) == -1)
288734b3a42SToomas Soome 		return (-1);
289734b3a42SToomas Soome 	zf->zf_zstream.avail_in = 0;
290734b3a42SToomas Soome 	zf->zf_zstream.next_in = NULL;
291734b3a42SToomas Soome 	zf->zf_endseen = 0;
292734b3a42SToomas Soome 	(void) inflateReset(&zf->zf_zstream);
293199767f8SToomas Soome 
294734b3a42SToomas Soome 	return (0);
295199767f8SToomas Soome }
296199767f8SToomas Soome 
297199767f8SToomas Soome static off_t
zf_seek(struct open_file * f,off_t offset,int where)298199767f8SToomas Soome zf_seek(struct open_file *f, off_t offset, int where)
299199767f8SToomas Soome {
300734b3a42SToomas Soome 	struct z_file	*zf = (struct z_file *)f->f_fsdata;
301734b3a42SToomas Soome 	off_t		target;
302734b3a42SToomas Soome 	char		discard[16];
303734b3a42SToomas Soome 
304734b3a42SToomas Soome 	switch (where) {
305734b3a42SToomas Soome 	case SEEK_SET:
306734b3a42SToomas Soome 		target = offset;
307734b3a42SToomas Soome 		break;
308734b3a42SToomas Soome 	case SEEK_CUR:
309734b3a42SToomas Soome 		target = offset + zf->zf_zstream.total_out;
310734b3a42SToomas Soome 		break;
311734b3a42SToomas Soome 	default:
312734b3a42SToomas Soome 		errno = EINVAL;
313734b3a42SToomas Soome 		return (-1);
314734b3a42SToomas Soome 	}
315734b3a42SToomas Soome 
316734b3a42SToomas Soome 	/* rewind if required */
317734b3a42SToomas Soome 	if (target < zf->zf_zstream.total_out && zf_rewind(f) != 0)
318734b3a42SToomas Soome 		return (-1);
319734b3a42SToomas Soome 
320734b3a42SToomas Soome 	/* skip forwards if required */
321734b3a42SToomas Soome 	while (target > zf->zf_zstream.total_out) {
322734b3a42SToomas Soome 		errno = zf_read(f, discard, min(sizeof (discard),
323734b3a42SToomas Soome 		    target - zf->zf_zstream.total_out), NULL);
324734b3a42SToomas Soome 		if (errno != 0)
325734b3a42SToomas Soome 			return (-1);
326734b3a42SToomas Soome 	}
327734b3a42SToomas Soome 	/* This is where we are (be honest if we overshot) */
328734b3a42SToomas Soome 	return (zf->zf_zstream.total_out);
329199767f8SToomas Soome }
330199767f8SToomas Soome 
331199767f8SToomas Soome 
332199767f8SToomas Soome static int
zf_stat(struct open_file * f,struct stat * sb)333199767f8SToomas Soome zf_stat(struct open_file *f, struct stat *sb)
334199767f8SToomas Soome {
335734b3a42SToomas Soome 	struct z_file	*zf = (struct z_file *)f->f_fsdata;
336734b3a42SToomas Soome 	int		result;
337734b3a42SToomas Soome 	off_t		pos1, pos2;
338734b3a42SToomas Soome 	uint32_t	size;
339734b3a42SToomas Soome 
340734b3a42SToomas Soome 	/* stat as normal, but indicate that size is unknown */
341734b3a42SToomas Soome 	if ((result = fstat(zf->zf_rawfd, sb)) == 0) {
342734b3a42SToomas Soome 		if (sb->st_size == -1)
343734b3a42SToomas Soome 			return (result);
344734b3a42SToomas Soome 		pos1 = lseek(zf->zf_rawfd, 0, SEEK_CUR);
345734b3a42SToomas Soome 		pos2 = lseek(zf->zf_rawfd, sb->st_size - 4, SEEK_SET);
346734b3a42SToomas Soome 		if (pos2 != -1) {
347734b3a42SToomas Soome 			if (read(zf->zf_rawfd, &size, 4) == 4)
348734b3a42SToomas Soome 				sb->st_size = (off_t)size;
349734b3a42SToomas Soome 			else
350734b3a42SToomas Soome 				sb->st_size = -1;
351734b3a42SToomas Soome 		} else
352734b3a42SToomas Soome 			sb->st_size = -1;
353734b3a42SToomas Soome 
354734b3a42SToomas Soome 		pos1 = lseek(zf->zf_rawfd, pos1, SEEK_SET);
355734b3a42SToomas Soome 	}
356734b3a42SToomas Soome 	return (result);
357199767f8SToomas Soome }
358