2 * Copyright (c) 2014 Mark Johnston <markj@FreeBSD.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in
11 * the documentation and/or other materials provided with the
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/zutil.h>
37 #define KERN_GZ_HDRLEN 10 /* gzip header length */
38 #define KERN_GZ_TRAILERLEN 8 /* gzip trailer length */
39 #define KERN_GZ_MAGIC1 0x1f /* first magic byte */
40 #define KERN_GZ_MAGIC2 0x8b /* second magic byte */
42 MALLOC_DEFINE(M_GZIO, "gzio", "zlib state");
45 uint8_t * gz_buffer; /* output buffer */
46 size_t gz_bufsz; /* total buffer size */
47 off_t gz_off; /* offset into the output stream */
48 enum gzio_mode gz_mode; /* stream mode */
49 uint32_t gz_crc; /* stream CRC32 */
50 gzio_cb gz_cb; /* output callback */
51 void * gz_arg; /* private callback arg */
52 z_stream gz_stream; /* zlib state */
55 static void * gz_alloc(void *, u_int, u_int);
56 static void gz_free(void *, void *);
57 static int gz_write(struct gzio_stream *, void *, u_int, int);
60 gzio_init(gzio_cb cb, enum gzio_mode mode, size_t bufsz, int level, void *arg)
62 struct gzio_stream *s;
65 if (bufsz < KERN_GZ_HDRLEN)
67 if (mode != GZIO_DEFLATE)
70 s = gz_alloc(NULL, 1, sizeof(*s));
72 s->gz_buffer = gz_alloc(NULL, 1, s->gz_bufsz);
77 s->gz_stream.zalloc = gz_alloc;
78 s->gz_stream.zfree = gz_free;
79 s->gz_stream.opaque = NULL;
80 s->gz_stream.next_in = Z_NULL;
81 s->gz_stream.avail_in = 0;
83 error = deflateInit2(&s->gz_stream, level, Z_DEFLATED, -MAX_WBITS,
84 DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
93 gz_free(NULL, s->gz_buffer);
99 gzio_reset(struct gzio_stream *s)
103 (void)deflateReset(&s->gz_stream);
108 s->gz_stream.avail_out = s->gz_bufsz;
109 s->gz_stream.next_out = s->gz_buffer;
111 /* Write the gzip header to the output buffer. */
113 memset(hdr, 0, KERN_GZ_HDRLEN);
114 hdr[0] = KERN_GZ_MAGIC1;
115 hdr[1] = KERN_GZ_MAGIC2;
118 s->gz_stream.next_out += KERN_GZ_HDRLEN;
119 s->gz_stream.avail_out -= KERN_GZ_HDRLEN;
123 gzio_write(struct gzio_stream *s, void *data, u_int len)
126 return (gz_write(s, data, len, Z_NO_FLUSH));
130 gzio_flush(struct gzio_stream *s)
133 return (gz_write(s, NULL, 0, Z_FINISH));
137 gzio_fini(struct gzio_stream *s)
140 (void)deflateEnd(&s->gz_stream);
141 gz_free(NULL, s->gz_buffer);
146 gz_alloc(void *arg __unused, u_int n, u_int sz)
150 * Memory for zlib state is allocated using M_NODUMP since it may be
151 * used to compress a kernel dump, and we don't want zlib to attempt to
152 * compress its own state.
154 return (malloc(n * sz, M_GZIO, M_WAITOK | M_ZERO | M_NODUMP));
158 gz_free(void *arg __unused, void *ptr)
165 gz_write(struct gzio_stream *s, void *buf, u_int len, int zflag)
167 uint8_t trailer[KERN_GZ_TRAILERLEN];
171 KASSERT(zflag == Z_FINISH || zflag == Z_NO_FLUSH,
172 ("unexpected flag %d", zflag));
173 KASSERT(s->gz_mode == GZIO_DEFLATE,
174 ("invalid stream mode %d", s->gz_mode));
177 s->gz_stream.avail_in = len;
178 s->gz_stream.next_in = buf;
179 s->gz_crc = crc32_raw(buf, len, s->gz_crc);
185 zerror = deflate(&s->gz_stream, zflag);
186 if (zerror != Z_OK && zerror != Z_STREAM_END) {
191 if (s->gz_stream.avail_out == 0 || zerror == Z_STREAM_END) {
193 * Our output buffer is full or there's nothing left
194 * to produce, so we're flushing the buffer.
196 len = s->gz_bufsz - s->gz_stream.avail_out;
197 if (zerror == Z_STREAM_END) {
199 * Try to pack as much of the trailer into the
200 * output buffer as we can.
202 ((uint32_t *)trailer)[0] = s->gz_crc;
203 ((uint32_t *)trailer)[1] =
204 s->gz_stream.total_in;
205 room = MIN(KERN_GZ_TRAILERLEN,
207 memcpy(s->gz_buffer + len, trailer, room);
211 error = s->gz_cb(s->gz_buffer, len, s->gz_off,
217 s->gz_stream.next_out = s->gz_buffer;
218 s->gz_stream.avail_out = s->gz_bufsz;
221 * If we couldn't pack the trailer into the output
222 * buffer, write it out now.
224 if (zerror == Z_STREAM_END && room < KERN_GZ_TRAILERLEN)
225 error = s->gz_cb(trailer + room,
226 KERN_GZ_TRAILERLEN - room, s->gz_off,
229 } while (zerror != Z_STREAM_END &&
230 (zflag == Z_FINISH || s->gz_stream.avail_in > 0));