Home | History | Annotate | Download | only in zmod
      1   420      stevel /*
      2   420      stevel  * CDDL HEADER START
      3   420      stevel  *
      4   420      stevel  * The contents of this file are subject to the terms of the
      5  3446         mrj  * Common Development and Distribution License (the "License").
      6  3446         mrj  * You may not use this file except in compliance with the License.
      7   420      stevel  *
      8   420      stevel  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9   420      stevel  * or http://www.opensolaris.org/os/licensing.
     10   420      stevel  * See the License for the specific language governing permissions
     11   420      stevel  * and limitations under the License.
     12   420      stevel  *
     13   420      stevel  * When distributing Covered Code, include this CDDL HEADER in each
     14   420      stevel  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15   420      stevel  * If applicable, add the following below this CDDL HEADER, with the
     16   420      stevel  * fields enclosed by brackets "[]" replaced with your own identifying
     17   420      stevel  * information: Portions Copyright [yyyy] [name of copyright owner]
     18   420      stevel  *
     19   420      stevel  * CDDL HEADER END
     20   420      stevel  */
     21   420      stevel 
     22     0      stevel /*
     23  7858  Krishnendu  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
     24     0      stevel  * Use is subject to license terms.
     25     0      stevel  */
     26     0      stevel 
     27     0      stevel #include <sys/modctl.h>
     28     0      stevel #include <sys/zmod.h>
     29     0      stevel 
     30     0      stevel #include "zlib.h"
     31  7858  Krishnendu #include "zutil.h"
     32     0      stevel 
     33     0      stevel /*
     34     0      stevel  * Uncompress the buffer 'src' into the buffer 'dst'.  The caller must store
     35     0      stevel  * the expected decompressed data size externally so it can be passed in.
     36     0      stevel  * The resulting decompressed size is then returned through dstlen.  This
     37     0      stevel  * function return Z_OK on success, or another error code on failure.
     38     0      stevel  */
     39     0      stevel int
     40     0      stevel z_uncompress(void *dst, size_t *dstlen, const void *src, size_t srclen)
     41     0      stevel {
     42     0      stevel 	z_stream zs;
     43     0      stevel 	int err;
     44     0      stevel 
     45     0      stevel 	bzero(&zs, sizeof (zs));
     46     0      stevel 	zs.next_in = (uchar_t *)src;
     47     0      stevel 	zs.avail_in = srclen;
     48     0      stevel 	zs.next_out = dst;
     49     0      stevel 	zs.avail_out = *dstlen;
     50     0      stevel 
     51  7858  Krishnendu 	/*
     52  7858  Krishnendu 	 * Call inflateInit2() specifying a window size of DEF_WBITS
     53  7858  Krishnendu 	 * with the 6th bit set to indicate that the compression format
     54  7858  Krishnendu 	 * type (zlib or gzip) should be automatically detected.
     55  7858  Krishnendu 	 */
     56  7858  Krishnendu 	if ((err = inflateInit2(&zs, DEF_WBITS | 0x20)) != Z_OK)
     57     0      stevel 		return (err);
     58     0      stevel 
     59     0      stevel 	if ((err = inflate(&zs, Z_FINISH)) != Z_STREAM_END) {
     60     0      stevel 		(void) inflateEnd(&zs);
     61     0      stevel 		return (err == Z_OK ? Z_BUF_ERROR : err);
     62     0      stevel 	}
     63     0      stevel 
     64     0      stevel 	*dstlen = zs.total_out;
     65     0      stevel 	return (inflateEnd(&zs));
     66     0      stevel }
     67     0      stevel 
     68  3886         ahl int
     69  3886         ahl z_compress_level(void *dst, size_t *dstlen, const void *src, size_t srclen,
     70  3886         ahl     int level)
     71  3886         ahl {
     72  3886         ahl 
     73  3886         ahl 	z_stream zs;
     74  3886         ahl 	int err;
     75  3886         ahl 
     76  3886         ahl 	bzero(&zs, sizeof (zs));
     77  3886         ahl 	zs.next_in = (uchar_t *)src;
     78  3886         ahl 	zs.avail_in = srclen;
     79  3886         ahl 	zs.next_out = dst;
     80  3886         ahl 	zs.avail_out = *dstlen;
     81  3886         ahl 
     82  3886         ahl 	if ((err = deflateInit(&zs, level)) != Z_OK)
     83  3886         ahl 		return (err);
     84  3886         ahl 
     85  3886         ahl 	if ((err = deflate(&zs, Z_FINISH)) != Z_STREAM_END) {
     86  3886         ahl 		(void) deflateEnd(&zs);
     87  3886         ahl 		return (err == Z_OK ? Z_BUF_ERROR : err);
     88  3886         ahl 	}
     89  3886         ahl 
     90  3886         ahl 	*dstlen = zs.total_out;
     91  3886         ahl 	return (deflateEnd(&zs));
     92  3886         ahl }
     93  3886         ahl 
     94  3886         ahl int
     95  3886         ahl z_compress(void *dst, size_t *dstlen, const void *src, size_t srclen)
     96  3886         ahl {
     97  3886         ahl 	return (z_compress_level(dst, dstlen, src, srclen,
     98  3886         ahl 	    Z_DEFAULT_COMPRESSION));
     99  3886         ahl }
    100     0      stevel 
    101     0      stevel /*
    102     0      stevel  * Convert a zlib error code into a string error message.
    103     0      stevel  */
    104     0      stevel const char *
    105     0      stevel z_strerror(int err)
    106     0      stevel {
    107     0      stevel 	int i = Z_NEED_DICT - err;
    108     0      stevel 
    109  3886         ahl 	if (i < 0 || i > Z_NEED_DICT - Z_VERSION_ERROR)
    110     0      stevel 		return ("unknown error");
    111     0      stevel 
    112  3886         ahl 	return (zError(err));
    113     0      stevel }
    114