1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Decompression module for stand alone file systems.
*/
#include <sys/param.h>
#include <sys/sysmacros.h>
#include <sys/vnode.h>
#include <sys/bootvfs.h>
#include <sys/filep.h>
#include <zmod/zlib.h>
#ifdef _BOOT
#include "../common/util.h"
#else
#include <sys/sunddi.h>
#endif
#define MAX_DECOMP_BUFS 8
#define GZIP_ID_BYTE_1 0x1f
#define GZIP_ID_BYTE_2 0x8b
#define GZIP_CM_DEFLATE 0x08
#define SEEKBUFSIZE 8192
#ifdef _BOOT
#define dprintf if (cf_debug) printf
#else
#define dprintf if (cf_debug) printf
#endif
extern int bootrd_debug;
extern void *bkmem_alloc(size_t);
extern void bkmem_free(void *, size_t);
caddr_t scratch_bufs[MAX_DECOMP_BUFS]; /* array of free scratch mem bufs */
int decomp_bufcnt; /* total no, of allocated decomp bufs */
int free_dcomp_bufs; /* no. of free decomp bufs */
char seek_scrbuf[SEEKBUFSIZE]; /* buffer for seeking */
int cf_debug; /* non-zero enables debug prints */
void *
cf_alloc(void *opaque, unsigned int items, unsigned int size)
{
fileid_t *filep;
unsigned int nbytes;
caddr_t ptr;
filep = (fileid_t *)opaque;
nbytes = roundup(items * size, sizeof (long));
if (nbytes > (DECOMP_BUFSIZE - filep->fi_dcscrused)) {
ptr = bkmem_alloc(nbytes);
} else {
ptr = &filep->fi_dcscrbuf[filep->fi_dcscrused];
filep->fi_dcscrused += nbytes;
}
bzero(ptr, nbytes);
return (ptr);
}
/*
* Decompression scratch memory free routine, does nothing since we free
* the entire scratch area all at once on file close.
*/
/* ARGSUSED */
void
cf_free(void *opaque, void *addr)
{
}
/*
* Read the first block of the file described by filep and determine if
* the file is gzip-compressed. If so, the compressed flag will be set
* in the fileid_t struct pointed to by filep and it will be initialized
* for doing decompression on reads to the file.
*/
int
cf_check_compressed(fileid_t *filep)
{
unsigned char *filebytes;
z_stream *zsp;
/*
* If the file is not long enough to check for a decompression header
* then return not compressed.
*/
if (filep->fi_inode->i_size < 3)
return (0);
filep->fi_offset = 0;
if ((filep->fi_getblock)(filep) == -1)
return (-1);
filep->fi_offset = 0;
filep->fi_count = 0;
filep->fi_cfoff = 0;
filebytes = (unsigned char *)filep->fi_memp;
if (filebytes[0] != GZIP_ID_BYTE_1 || filebytes[1] != GZIP_ID_BYTE_2 ||
filebytes[2] != GZIP_CM_DEFLATE)
return (0); /* not compressed */
filep->fi_flags |= FI_COMPRESSED;
dprintf("file %s is compressed\n", filep->fi_path);
/*
* Allocate decompress scratch buffer
*/
if (free_dcomp_bufs) {
filep->fi_dcscrbuf = scratch_bufs[--free_dcomp_bufs];
} else {
filep->fi_dcscrbuf = bkmem_alloc(DECOMP_BUFSIZE);
decomp_bufcnt++;
}
filep->fi_dcscrused = 0;
zsp = bkmem_alloc(sizeof (*zsp));
filep->fi_dcstream = zsp;
/*
* Initialize the decompression stream. Adding 16 to the window size
* indicates that zlib should expect a gzip header.
*/
bzero(zsp, sizeof (*zsp));
zsp->opaque = filep;
zsp->zalloc = cf_alloc;
zsp->zfree = cf_free;
zsp->avail_in = 0;
zsp->next_in = NULL;
zsp->avail_out = 0;
zsp->next_out = NULL;
if (inflateInit2(zsp, MAX_WBITS + 16) != Z_OK) {
dprintf("inflateInit2() failed\n");
return (-1);
}
return (0);
}
/*
* If the file described by fileid_t struct at *filep is compressed
* free any resources associated with the decompression. (decompression
* buffer, etc.).
*/
void
cf_close(fileid_t *filep)
{
if ((filep->fi_flags & FI_COMPRESSED) == 0)
return;
dprintf("cf_close: %s\n", filep->fi_path);
(void) inflateEnd(filep->fi_dcstream);
bkmem_free(filep->fi_dcstream, sizeof (z_stream));
if (free_dcomp_bufs == MAX_DECOMP_BUFS) {
bkmem_free(filep->fi_dcscrbuf, DECOMP_BUFSIZE);
} else {
scratch_bufs[free_dcomp_bufs++] = filep->fi_dcscrbuf;
}
}
void
cf_rewind(fileid_t *filep)
{
z_stream *zsp;
dprintf("cf_rewind: %s\n", filep->fi_path);
zsp = filep->fi_dcstream;
zsp->avail_in = 0;
zsp->next_in = NULL;
(void) inflateReset(zsp);
filep->fi_cfoff = 0;
}
#define FLG_FHCRC 0x02 /* crc field present */
#define FLG_FEXTRA 0x04 /* "extra" field present */
#define FLG_FNAME 0x08 /* file name field present */
#define FLG_FCOMMENT 0x10 /* comment field present */
/*
* Read at the current uncompressed offset from the compressed file described
* by *filep. Will return decompressed data.
*/
int
cf_read(fileid_t *filep, caddr_t buf, size_t count)
{
z_stream *zsp;
struct inode *ip;
int err = Z_OK;
int infbytes;
off_t soff;
caddr_t smemp;
dprintf("cf_read: %s ", filep->fi_path);
dprintf("%lx bytes\n", count);
zsp = filep->fi_dcstream;
ip = filep->fi_inode;
dprintf(" reading at offset %lx\n", zsp->total_out);
zsp->next_out = (unsigned char *)buf;
zsp->avail_out = count;
while (zsp->avail_out != 0) {
if (zsp->avail_in == 0 && filep->fi_cfoff < ip->i_size) {
/*
* read a block of the file to inflate
*/
soff = filep->fi_offset;
smemp = filep->fi_memp;
filep->fi_memp = NULL;
filep->fi_offset = filep->fi_cfoff;
filep->fi_count = 0;
if ((*filep->fi_getblock)(filep) == -1)
return (-1);
filep->fi_offset = soff;
zsp->next_in = (unsigned char *)filep->fi_memp;
zsp->avail_in = filep->fi_count;
filep->fi_memp = smemp;
filep->fi_cfoff += filep->fi_count;
}
infbytes = zsp->avail_out;
dprintf("attempting inflate of %x bytes to buf at: %lx\n",
zsp->avail_out, (unsigned long)zsp->next_out);
err = inflate(zsp, Z_NO_FLUSH);
infbytes -= zsp->avail_out;
dprintf("inflated %x bytes, errcode=%d\n", infbytes, err);
/*
* break out if we hit end of the compressed file
* or the end of the compressed byte stream
*/
if (filep->fi_cfoff >= ip->i_size || err == Z_STREAM_END)
break;
}
dprintf("cf_read: returned %lx bytes\n", count - zsp->avail_out);
return (count - zsp->avail_out);
}
/*
* Seek to the location specified by addr
*/
void
cf_seek(fileid_t *filep, off_t addr, int whence)
{
z_stream *zsp;
int readsz;
dprintf("cf_seek: %s ", filep->fi_path);
dprintf("to %lx\n", addr);
zsp = filep->fi_dcstream;
if (whence == SEEK_CUR)
addr += zsp->total_out;
/*
* To seek backwards, must rewind and seek forwards
*/
if (addr < zsp->total_out) {
cf_rewind(filep);
filep->fi_offset = 0;
} else {
addr -= zsp->total_out;
}
while (addr > 0) {
readsz = MIN(addr, SEEKBUFSIZE);
(void) cf_read(filep, seek_scrbuf, readsz);
addr -= readsz;
}
}
|