Path: blob/main/sys/cddl/contrib/opensolaris/uts/common/zmod/zmod.c
96395 views
/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License (the "License").5* You may not use this file except in compliance with the License.6*7* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE8* or http://www.opensolaris.org/os/licensing.9* See the License for the specific language governing permissions10* and limitations under the License.11*12* When distributing Covered Code, include this CDDL HEADER in each13* file and include the License file at usr/src/OPENSOLARIS.LICENSE.14* If applicable, add the following below this CDDL HEADER, with the15* fields enclosed by brackets "[]" replaced with your own identifying16* information: Portions Copyright [yyyy] [name of copyright owner]17*18* CDDL HEADER END19*/2021/*22* Copyright 2008 Sun Microsystems, Inc. All rights reserved.23* Use is subject to license terms.24*/2526#include <sys/types.h>27#include <sys/cmn_err.h>28#include <sys/systm.h>29#include <sys/kmem.h>30#include <sys/zmod.h>3132#include <contrib/zlib/zlib.h>33#include <contrib/zlib/zutil.h>3435/*ARGSUSED*/36static void *37zfs_zcalloc(void *opaque, uint_t items, uint_t size)38{39void *ptr;4041ptr = malloc((size_t)items * size, M_SOLARIS, M_NOWAIT);42return ptr;43}4445/*ARGSUSED*/46static void47zfs_zcfree(void *opaque, void *ptr)48{4950free(ptr, M_SOLARIS);51}5253/*54* Uncompress the buffer 'src' into the buffer 'dst'. The caller must store55* the expected decompressed data size externally so it can be passed in.56* The resulting decompressed size is then returned through dstlen. This57* function return Z_OK on success, or another error code on failure.58*/59int60z_uncompress(void *dst, size_t *dstlen, const void *src, size_t srclen)61{62z_stream zs;63int err;6465bzero(&zs, sizeof (zs));66zs.next_in = (uchar_t *)src;67zs.avail_in = srclen;68zs.next_out = dst;69zs.avail_out = *dstlen;70zs.zalloc = zfs_zcalloc;71zs.zfree = zfs_zcfree;7273/*74* Call inflateInit2() specifying a window size of DEF_WBITS75* with the 6th bit set to indicate that the compression format76* type (zlib or gzip) should be automatically detected.77*/78if ((err = inflateInit2(&zs, DEF_WBITS | 0x20)) != Z_OK)79return (err);8081if ((err = inflate(&zs, Z_FINISH)) != Z_STREAM_END) {82(void) inflateEnd(&zs);83return (err == Z_OK ? Z_BUF_ERROR : err);84}8586*dstlen = zs.total_out;87return (inflateEnd(&zs));88}8990int91z_compress_level(void *dst, size_t *dstlen, const void *src, size_t srclen,92int level)93{9495z_stream zs;96int err;9798bzero(&zs, sizeof (zs));99zs.next_in = (uchar_t *)src;100zs.avail_in = srclen;101zs.next_out = dst;102zs.avail_out = *dstlen;103zs.zalloc = zfs_zcalloc;104zs.zfree = zfs_zcfree;105106if ((err = deflateInit(&zs, level)) != Z_OK)107return (err);108109if ((err = deflate(&zs, Z_FINISH)) != Z_STREAM_END) {110(void) deflateEnd(&zs);111return (err == Z_OK ? Z_BUF_ERROR : err);112}113114*dstlen = zs.total_out;115return (deflateEnd(&zs));116}117118int119z_compress(void *dst, size_t *dstlen, const void *src, size_t srclen)120{121return (z_compress_level(dst, dstlen, src, srclen,122Z_DEFAULT_COMPRESSION));123}124125/*126* Convert a zlib error code into a string error message.127*/128const char *129z_strerror(int err)130{131int i = Z_NEED_DICT - err;132133if (i < 0 || i > Z_NEED_DICT - Z_VERSION_ERROR)134return ("unknown error");135136return (zError(err));137}138139140