Changeset View
Changeset View
Standalone View
Standalone View
sys/kern/subr_compressor.c
Show First 20 Lines • Show All 60 Lines • ▼ Show 20 Lines | struct compressor { | ||||
void *priv; | void *priv; | ||||
void *arg; | void *arg; | ||||
}; | }; | ||||
SET_DECLARE(compressors, struct compressor_methods); | SET_DECLARE(compressors, struct compressor_methods); | ||||
#ifdef GZIO | #ifdef GZIO | ||||
#include <sys/zutil.h> | #include <sys/crc32.h> | ||||
#include <zlib.h> | |||||
sobomax: Do we really need that comment? It's pretty clear (to me anyway), why do we need to include… | |||||
Done Inline ActionsI will clean up later. I had to keep my note while trying to replace from old header to new header and one file to another. ota_j.email.ne.jp: I will clean up later. I had to keep my note while trying to replace from old header to new… | |||||
struct gz_stream { | struct gz_stream { | ||||
uint8_t *gz_buffer; /* output buffer */ | uint8_t *gz_buffer; /* output buffer */ | ||||
size_t gz_bufsz; /* output buffer size */ | size_t gz_bufsz; /* output buffer size */ | ||||
off_t gz_off; /* offset into the output stream */ | off_t gz_off; /* offset into the output stream */ | ||||
uint32_t gz_crc; /* stream CRC32 */ | uint32_t gz_crc; /* stream CRC32 */ | ||||
z_stream gz_stream; /* zlib state */ | z_stream gz_stream; /* zlib state */ | ||||
}; | }; | ||||
Show All 34 Lines | gz_init(size_t maxiosize, int level) | ||||
s->gz_bufsz = maxiosize; | s->gz_bufsz = maxiosize; | ||||
s->gz_stream.zalloc = gz_alloc; | s->gz_stream.zalloc = gz_alloc; | ||||
s->gz_stream.zfree = gz_free; | s->gz_stream.zfree = gz_free; | ||||
s->gz_stream.opaque = NULL; | s->gz_stream.opaque = NULL; | ||||
s->gz_stream.next_in = Z_NULL; | s->gz_stream.next_in = Z_NULL; | ||||
s->gz_stream.avail_in = 0; | s->gz_stream.avail_in = 0; | ||||
error = deflateInit2(&s->gz_stream, level, Z_DEFLATED, -MAX_WBITS, | error = z_deflateInit2(&s->gz_stream, level, Z_DEFLATED, -MAX_WBITS, | ||||
DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY); | 8 /* as DEF_MEM_LEVEL */, Z_DEFAULT_STRATEGY); | ||||
Not Done Inline ActionsOptional: personally I think it's fine to just use 8 here - there is little reason to tweak it anyways, and the example application shipped with zlib use a hardcoded 8 too. delphij: Optional: personally I think it's fine to just use 8 here - there is little reason to tweak it… | |||||
Done Inline Actionszlib.h doesn't expose this DEF_MEM_LEVEL and wondered how other people would think by changing to const value. Should I keep the comment or remove it? ota_j.email.ne.jp: zlib.h doesn't expose this DEF_MEM_LEVEL and wondered how other people would think by changing… | |||||
if (error != 0) | if (error != 0) | ||||
goto fail; | goto fail; | ||||
gz_reset(s); | gz_reset(s); | ||||
return (s); | return (s); | ||||
fail: | fail: | ||||
gz_free(NULL, s); | gz_free(NULL, s); | ||||
return (NULL); | return (NULL); | ||||
} | } | ||||
static void | static void | ||||
gz_reset(void *stream) | gz_reset(void *stream) | ||||
{ | { | ||||
struct gz_stream *s; | struct gz_stream *s; | ||||
uint8_t *hdr; | uint8_t *hdr; | ||||
const size_t hdrlen = 10; | const size_t hdrlen = 10; | ||||
s = stream; | s = stream; | ||||
s->gz_off = 0; | s->gz_off = 0; | ||||
s->gz_crc = ~0U; | s->gz_crc = ~0U; | ||||
(void)deflateReset(&s->gz_stream); | (void)z_deflateReset(&s->gz_stream); | ||||
s->gz_stream.avail_out = s->gz_bufsz; | s->gz_stream.avail_out = s->gz_bufsz; | ||||
s->gz_stream.next_out = s->gz_buffer; | s->gz_stream.next_out = s->gz_buffer; | ||||
/* Write the gzip header to the output buffer. */ | /* Write the gzip header to the output buffer. */ | ||||
hdr = s->gz_buffer; | hdr = s->gz_buffer; | ||||
memset(hdr, 0, hdrlen); | memset(hdr, 0, hdrlen); | ||||
hdr[0] = 0x1f; | hdr[0] = 0x1f; | ||||
hdr[1] = 0x8b; | hdr[1] = 0x8b; | ||||
hdr[2] = Z_DEFLATED; | hdr[2] = Z_DEFLATED; | ||||
hdr[9] = OS_CODE; | hdr[9] = 0x03; /* zlib's OS_CODE Unix */ | ||||
s->gz_stream.next_out += hdrlen; | s->gz_stream.next_out += hdrlen; | ||||
s->gz_stream.avail_out -= hdrlen; | s->gz_stream.avail_out -= hdrlen; | ||||
} | } | ||||
static int | static int | ||||
gz_write(void *stream, void *data, size_t len, compressor_cb_t cb, | gz_write(void *stream, void *data, size_t len, compressor_cb_t cb, | ||||
void *arg) | void *arg) | ||||
{ | { | ||||
Show All 9 Lines | if (len > 0) { | ||||
s->gz_stream.avail_in = len; | s->gz_stream.avail_in = len; | ||||
s->gz_stream.next_in = data; | s->gz_stream.next_in = data; | ||||
s->gz_crc = crc32_raw(data, len, s->gz_crc); | s->gz_crc = crc32_raw(data, len, s->gz_crc); | ||||
} else | } else | ||||
s->gz_crc ^= ~0U; | s->gz_crc ^= ~0U; | ||||
error = 0; | error = 0; | ||||
do { | do { | ||||
zerror = deflate(&s->gz_stream, zflag); | zerror = z_deflate(&s->gz_stream, zflag); | ||||
if (zerror != Z_OK && zerror != Z_STREAM_END) { | if (zerror != Z_OK && zerror != Z_STREAM_END) { | ||||
error = EIO; | error = EIO; | ||||
break; | break; | ||||
} | } | ||||
if (s->gz_stream.avail_out == 0 || zerror == Z_STREAM_END) { | if (s->gz_stream.avail_out == 0 || zerror == Z_STREAM_END) { | ||||
/* | /* | ||||
* Our output buffer is full or there's nothing left | * Our output buffer is full or there's nothing left | ||||
Show All 37 Lines | |||||
} | } | ||||
static void | static void | ||||
gz_fini(void *stream) | gz_fini(void *stream) | ||||
{ | { | ||||
struct gz_stream *s; | struct gz_stream *s; | ||||
s = stream; | s = stream; | ||||
(void)deflateEnd(&s->gz_stream); | (void)z_deflateEnd(&s->gz_stream); | ||||
gz_free(NULL, s->gz_buffer); | gz_free(NULL, s->gz_buffer); | ||||
gz_free(NULL, s); | gz_free(NULL, s); | ||||
} | } | ||||
struct compressor_methods gzip_methods = { | struct compressor_methods gzip_methods = { | ||||
.format = COMPRESS_GZIP, | .format = COMPRESS_GZIP, | ||||
.init = gz_init, | .init = gz_init, | ||||
.reset = gz_reset, | .reset = gz_reset, | ||||
▲ Show 20 Lines • Show All 316 Lines • Show Last 20 Lines |
Do we really need that comment? It's pretty clear (to me anyway), why do we need to include zlib.h. I have not seen us doing this for any other headers used in the kernel.