1303095Ssobomax/* 2303095Ssobomax * Copyright (c) 2004-2016 Maxim Sobolev <sobomax@FreeBSD.org> 3303095Ssobomax * All rights reserved. 4303095Ssobomax * 5303095Ssobomax * Redistribution and use in source and binary forms, with or without 6303095Ssobomax * modification, are permitted provided that the following conditions 7303095Ssobomax * are met: 8303095Ssobomax * 1. Redistributions of source code must retain the above copyright 9303095Ssobomax * notice, this list of conditions and the following disclaimer. 10303095Ssobomax * 2. Redistributions in binary form must reproduce the above copyright 11303095Ssobomax * notice, this list of conditions and the following disclaimer in the 12303095Ssobomax * documentation and/or other materials provided with the distribution. 13303095Ssobomax * 14303095Ssobomax * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15303095Ssobomax * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16303095Ssobomax * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17303095Ssobomax * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18303095Ssobomax * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19303095Ssobomax * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20303095Ssobomax * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21303095Ssobomax * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22303095Ssobomax * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23303095Ssobomax * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24303095Ssobomax * SUCH DAMAGE. 25303095Ssobomax */ 26303095Ssobomax 27303095Ssobomax#include <sys/cdefs.h> 28303095Ssobomax__FBSDID("$FreeBSD$"); 29303095Ssobomax 30303095Ssobomax#include <sys/param.h> 31303095Ssobomax#include <err.h> 32303095Ssobomax#include <stdint.h> 33303095Ssobomax 34303095Ssobomax#include <zlib.h> 35303095Ssobomax 36303095Ssobomax#include "mkuzip.h" 37303095Ssobomax#include "mkuz_zlib.h" 38303095Ssobomax#include "mkuz_blk.h" 39303095Ssobomax 40303095Ssobomaxstruct mkuz_zlib { 41303095Ssobomax uLongf oblen; 42303095Ssobomax uint32_t blksz; 43303095Ssobomax}; 44303095Ssobomax 45303095Ssobomaxvoid * 46303095Ssobomaxmkuz_zlib_init(uint32_t blksz) 47303095Ssobomax{ 48303095Ssobomax struct mkuz_zlib *zp; 49303095Ssobomax 50303095Ssobomax if (blksz % DEV_BSIZE != 0) { 51303095Ssobomax errx(1, "cluster size should be multiple of %d", 52303095Ssobomax DEV_BSIZE); 53303095Ssobomax /* Not reached */ 54303095Ssobomax } 55303095Ssobomax if (compressBound(blksz) > MAXPHYS) { 56303095Ssobomax errx(1, "cluster size is too large"); 57303095Ssobomax /* Not reached */ 58303095Ssobomax } 59303095Ssobomax zp = mkuz_safe_zmalloc(sizeof(struct mkuz_zlib)); 60303095Ssobomax zp->oblen = compressBound(blksz); 61303095Ssobomax zp->blksz = blksz; 62303095Ssobomax 63303095Ssobomax return (void *)zp; 64303095Ssobomax} 65303095Ssobomax 66303095Ssobomaxstruct mkuz_blk * 67303095Ssobomaxmkuz_zlib_compress(void *p, const struct mkuz_blk *iblk) 68303095Ssobomax{ 69303095Ssobomax uLongf destlen_z; 70303095Ssobomax struct mkuz_blk *rval; 71303095Ssobomax struct mkuz_zlib *zp; 72303095Ssobomax 73303095Ssobomax zp = (struct mkuz_zlib *)p; 74303095Ssobomax 75303095Ssobomax rval = mkuz_blk_ctor(zp->oblen); 76303095Ssobomax 77303095Ssobomax destlen_z = rval->alen; 78303095Ssobomax if (compress2(rval->data, &destlen_z, iblk->data, zp->blksz, 79303095Ssobomax Z_BEST_COMPRESSION) != Z_OK) { 80303095Ssobomax errx(1, "can't compress data: compress2() " 81303095Ssobomax "failed"); 82303095Ssobomax /* Not reached */ 83303095Ssobomax } 84303095Ssobomax 85303095Ssobomax rval->info.len = (uint32_t)destlen_z; 86303095Ssobomax return (rval); 87303095Ssobomax} 88