1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 3*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 4*7c478bd9Sstevel@tonic-gate */ 5*7c478bd9Sstevel@tonic-gate 6*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 7*7c478bd9Sstevel@tonic-gate 8*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 9*7c478bd9Sstevel@tonic-gate #include <sys/zmod.h> 10*7c478bd9Sstevel@tonic-gate #include <sys/systm.h> 11*7c478bd9Sstevel@tonic-gate 12*7c478bd9Sstevel@tonic-gate #include "zlib.h" 13*7c478bd9Sstevel@tonic-gate 14*7c478bd9Sstevel@tonic-gate /* 15*7c478bd9Sstevel@tonic-gate * Uncompress the buffer 'src' into the buffer 'dst'. The caller must store 16*7c478bd9Sstevel@tonic-gate * the expected decompressed data size externally so it can be passed in. 17*7c478bd9Sstevel@tonic-gate * The resulting decompressed size is then returned through dstlen. This 18*7c478bd9Sstevel@tonic-gate * function return Z_OK on success, or another error code on failure. 19*7c478bd9Sstevel@tonic-gate */ 20*7c478bd9Sstevel@tonic-gate int 21*7c478bd9Sstevel@tonic-gate z_uncompress(void *dst, size_t *dstlen, const void *src, size_t srclen) 22*7c478bd9Sstevel@tonic-gate { 23*7c478bd9Sstevel@tonic-gate z_stream zs; 24*7c478bd9Sstevel@tonic-gate int err; 25*7c478bd9Sstevel@tonic-gate 26*7c478bd9Sstevel@tonic-gate bzero(&zs, sizeof (zs)); 27*7c478bd9Sstevel@tonic-gate zs.next_in = (uchar_t *)src; 28*7c478bd9Sstevel@tonic-gate zs.avail_in = srclen; 29*7c478bd9Sstevel@tonic-gate zs.next_out = dst; 30*7c478bd9Sstevel@tonic-gate zs.avail_out = *dstlen; 31*7c478bd9Sstevel@tonic-gate 32*7c478bd9Sstevel@tonic-gate if ((err = inflateInit(&zs)) != Z_OK) 33*7c478bd9Sstevel@tonic-gate return (err); 34*7c478bd9Sstevel@tonic-gate 35*7c478bd9Sstevel@tonic-gate if ((err = inflate(&zs, Z_FINISH)) != Z_STREAM_END) { 36*7c478bd9Sstevel@tonic-gate (void) inflateEnd(&zs); 37*7c478bd9Sstevel@tonic-gate return (err == Z_OK ? Z_BUF_ERROR : err); 38*7c478bd9Sstevel@tonic-gate } 39*7c478bd9Sstevel@tonic-gate 40*7c478bd9Sstevel@tonic-gate *dstlen = zs.total_out; 41*7c478bd9Sstevel@tonic-gate return (inflateEnd(&zs)); 42*7c478bd9Sstevel@tonic-gate } 43*7c478bd9Sstevel@tonic-gate 44*7c478bd9Sstevel@tonic-gate static const char *const z_errmsg[] = { 45*7c478bd9Sstevel@tonic-gate "need dictionary", /* Z_NEED_DICT 2 */ 46*7c478bd9Sstevel@tonic-gate "stream end", /* Z_STREAM_END 1 */ 47*7c478bd9Sstevel@tonic-gate "", /* Z_OK 0 */ 48*7c478bd9Sstevel@tonic-gate "file error", /* Z_ERRNO (-1) */ 49*7c478bd9Sstevel@tonic-gate "stream error", /* Z_STREAM_ERROR (-2) */ 50*7c478bd9Sstevel@tonic-gate "data error", /* Z_DATA_ERROR (-3) */ 51*7c478bd9Sstevel@tonic-gate "insufficient memory", /* Z_MEM_ERROR (-4) */ 52*7c478bd9Sstevel@tonic-gate "buffer error", /* Z_BUF_ERROR (-5) */ 53*7c478bd9Sstevel@tonic-gate "incompatible version" /* Z_VERSION_ERROR (-6) */ 54*7c478bd9Sstevel@tonic-gate }; 55*7c478bd9Sstevel@tonic-gate 56*7c478bd9Sstevel@tonic-gate /* 57*7c478bd9Sstevel@tonic-gate * Convert a zlib error code into a string error message. 58*7c478bd9Sstevel@tonic-gate */ 59*7c478bd9Sstevel@tonic-gate const char * 60*7c478bd9Sstevel@tonic-gate z_strerror(int err) 61*7c478bd9Sstevel@tonic-gate { 62*7c478bd9Sstevel@tonic-gate int i = Z_NEED_DICT - err; 63*7c478bd9Sstevel@tonic-gate 64*7c478bd9Sstevel@tonic-gate if (i < 0 || i >= sizeof (z_errmsg) / sizeof (z_errmsg[0])) 65*7c478bd9Sstevel@tonic-gate return ("unknown error"); 66*7c478bd9Sstevel@tonic-gate 67*7c478bd9Sstevel@tonic-gate return (z_errmsg[i]); 68*7c478bd9Sstevel@tonic-gate } 69*7c478bd9Sstevel@tonic-gate 70*7c478bd9Sstevel@tonic-gate static struct modlmisc modlmisc = { 71*7c478bd9Sstevel@tonic-gate &mod_miscops, "RFC 1950 decompression routines" 72*7c478bd9Sstevel@tonic-gate }; 73*7c478bd9Sstevel@tonic-gate 74*7c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = { 75*7c478bd9Sstevel@tonic-gate MODREV_1, &modlmisc, NULL 76*7c478bd9Sstevel@tonic-gate }; 77*7c478bd9Sstevel@tonic-gate 78*7c478bd9Sstevel@tonic-gate int 79*7c478bd9Sstevel@tonic-gate _init(void) 80*7c478bd9Sstevel@tonic-gate { 81*7c478bd9Sstevel@tonic-gate return (mod_install(&modlinkage)); 82*7c478bd9Sstevel@tonic-gate } 83*7c478bd9Sstevel@tonic-gate 84*7c478bd9Sstevel@tonic-gate int 85*7c478bd9Sstevel@tonic-gate _info(struct modinfo *mip) 86*7c478bd9Sstevel@tonic-gate { 87*7c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, mip)); 88*7c478bd9Sstevel@tonic-gate } 89*7c478bd9Sstevel@tonic-gate 90*7c478bd9Sstevel@tonic-gate int 91*7c478bd9Sstevel@tonic-gate _fini(void) 92*7c478bd9Sstevel@tonic-gate { 93*7c478bd9Sstevel@tonic-gate return (mod_remove(&modlinkage)); 94*7c478bd9Sstevel@tonic-gate } 95