1210284Sjmallett/* 2210284Sjmallett * CDDL HEADER START 3210284Sjmallett * 4210284Sjmallett * The contents of this file are subject to the terms of the 5210284Sjmallett * Common Development and Distribution License (the "License"). 6210284Sjmallett * You may not use this file except in compliance with the License. 7210284Sjmallett * 8210284Sjmallett * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9210284Sjmallett * or http://www.opensolaris.org/os/licensing. 10210284Sjmallett * See the License for the specific language governing permissions 11210284Sjmallett * and limitations under the License. 12210284Sjmallett * 13210284Sjmallett * When distributing Covered Code, include this CDDL HEADER in each 14210284Sjmallett * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15210284Sjmallett * If applicable, add the following below this CDDL HEADER, with the 16210284Sjmallett * fields enclosed by brackets "[]" replaced with your own identifying 17210284Sjmallett * information: Portions Copyright [yyyy] [name of copyright owner] 18210284Sjmallett * 19210284Sjmallett * CDDL HEADER END 20210284Sjmallett */ 21210284Sjmallett 22210284Sjmallett/* 23210284Sjmallett * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24210284Sjmallett * Use is subject to license terms. 25210284Sjmallett */ 26210284Sjmallett 27210284Sjmallett#include <sys/types.h> 28210284Sjmallett#include <sys/zmod.h> 29210284Sjmallett 30210284Sjmallett#include "zlib.h" 31210284Sjmallett#include "zutil.h" 32210284Sjmallett 33210284Sjmallett/* 34210284Sjmallett * Uncompress the buffer 'src' into the buffer 'dst'. The caller must store 35210284Sjmallett * the expected decompressed data size externally so it can be passed in. 36210284Sjmallett * The resulting decompressed size is then returned through dstlen. This 37210284Sjmallett * function return Z_OK on success, or another error code on failure. 38210284Sjmallett */ 39210284Sjmallettint 40210284Sjmallettz_uncompress(void *dst, size_t *dstlen, const void *src, size_t srclen) 41210284Sjmallett{ 42210284Sjmallett z_stream zs; 43210284Sjmallett int err; 44210284Sjmallett 45210284Sjmallett bzero(&zs, sizeof (zs)); 46210284Sjmallett zs.next_in = (uchar_t *)src; 47210284Sjmallett zs.avail_in = srclen; 48210284Sjmallett zs.next_out = dst; 49210284Sjmallett zs.avail_out = *dstlen; 50210284Sjmallett 51210284Sjmallett /* 52210284Sjmallett * Call inflateInit2() specifying a window size of DEF_WBITS 53210284Sjmallett * with the 6th bit set to indicate that the compression format 54210284Sjmallett * type (zlib or gzip) should be automatically detected. 55210284Sjmallett */ 56210284Sjmallett if ((err = inflateInit2(&zs, DEF_WBITS | 0x20)) != Z_OK) 57210284Sjmallett return (err); 58210284Sjmallett 59210284Sjmallett if ((err = inflate(&zs, Z_FINISH)) != Z_STREAM_END) { 60210284Sjmallett (void) inflateEnd(&zs); 61210284Sjmallett return (err == Z_OK ? Z_BUF_ERROR : err); 62210284Sjmallett } 63210284Sjmallett 64210284Sjmallett *dstlen = zs.total_out; 65210284Sjmallett return (inflateEnd(&zs)); 66210284Sjmallett} 67210284Sjmallett 68210284Sjmallettint 69210284Sjmallettz_compress_level(void *dst, size_t *dstlen, const void *src, size_t srclen, 70210284Sjmallett int level) 71210284Sjmallett{ 72210284Sjmallett 73210284Sjmallett z_stream zs; 74210284Sjmallett int err; 75210284Sjmallett 76210284Sjmallett bzero(&zs, sizeof (zs)); 77210284Sjmallett zs.next_in = (uchar_t *)src; 78210284Sjmallett zs.avail_in = srclen; 79210284Sjmallett zs.next_out = dst; 80210284Sjmallett zs.avail_out = *dstlen; 81210284Sjmallett 82210284Sjmallett if ((err = deflateInit(&zs, level)) != Z_OK) 83210284Sjmallett return (err); 84210284Sjmallett 85210284Sjmallett if ((err = deflate(&zs, Z_FINISH)) != Z_STREAM_END) { 86210284Sjmallett (void) deflateEnd(&zs); 87210284Sjmallett return (err == Z_OK ? Z_BUF_ERROR : err); 88210284Sjmallett } 89210284Sjmallett 90210284Sjmallett *dstlen = zs.total_out; 91210284Sjmallett return (deflateEnd(&zs)); 92210284Sjmallett} 93210284Sjmallett 94210284Sjmallettint 95210284Sjmallettz_compress(void *dst, size_t *dstlen, const void *src, size_t srclen) 96210284Sjmallett{ 97210284Sjmallett return (z_compress_level(dst, dstlen, src, srclen, 98210284Sjmallett Z_DEFAULT_COMPRESSION)); 99210284Sjmallett} 100210284Sjmallett 101210284Sjmallett/* 102210284Sjmallett * Convert a zlib error code into a string error message. 103210284Sjmallett */ 104210284Sjmallettconst char * 105210284Sjmallettz_strerror(int err) 106210284Sjmallett{ 107210284Sjmallett int i = Z_NEED_DICT - err; 108210284Sjmallett 109210284Sjmallett if (i < 0 || i > Z_NEED_DICT - Z_VERSION_ERROR) 110210284Sjmallett return ("unknown error"); 111210284Sjmallett 112210284Sjmallett return (zError(err)); 113210284Sjmallett} 114210284Sjmallett