1303095Ssobomax/* 2303095Ssobomax * Copyright (c) 2004-2016 Maxim Sobolev <sobomax@FreeBSD.org> 3303095Ssobomax * Copyright (c) 2011 Aleksandr Rybalko <ray@ddteam.net> 4303095Ssobomax * All rights reserved. 5303095Ssobomax * 6303095Ssobomax * Redistribution and use in source and binary forms, with or without 7303095Ssobomax * modification, are permitted provided that the following conditions 8303095Ssobomax * are met: 9303095Ssobomax * 1. Redistributions of source code must retain the above copyright 10303095Ssobomax * notice, this list of conditions and the following disclaimer. 11303095Ssobomax * 2. Redistributions in binary form must reproduce the above copyright 12303095Ssobomax * notice, this list of conditions and the following disclaimer in the 13303095Ssobomax * documentation and/or other materials provided with the distribution. 14303095Ssobomax * 15303095Ssobomax * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16303095Ssobomax * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17303095Ssobomax * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18303095Ssobomax * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19303095Ssobomax * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20303095Ssobomax * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21303095Ssobomax * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22303095Ssobomax * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23303095Ssobomax * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24303095Ssobomax * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25303095Ssobomax * SUCH DAMAGE. 26303095Ssobomax */ 27303095Ssobomax 28303095Ssobomax#include <sys/cdefs.h> 29303095Ssobomax__FBSDID("$FreeBSD$"); 30303095Ssobomax 31303095Ssobomax#include <sys/param.h> 32303095Ssobomax#include <err.h> 33303095Ssobomax#include <stdint.h> 34303095Ssobomax 35303095Ssobomax#include <lzma.h> 36303095Ssobomax 37303095Ssobomax#include "mkuzip.h" 38303095Ssobomax#include "mkuz_lzma.h" 39303095Ssobomax#include "mkuz_blk.h" 40303095Ssobomax 41303095Ssobomax#define USED_BLOCKSIZE DEV_BSIZE 42303095Ssobomax 43303095Ssobomaxstruct mkuz_lzma { 44303095Ssobomax lzma_filter filters[2]; 45303095Ssobomax lzma_options_lzma opt_lzma; 46303095Ssobomax lzma_stream strm; 47303095Ssobomax uint32_t blksz; 48303095Ssobomax}; 49303095Ssobomax 50303095Ssobomaxstatic const lzma_stream lzma_stream_init = LZMA_STREAM_INIT; 51303095Ssobomax 52303095Ssobomaxvoid * 53303095Ssobomaxmkuz_lzma_init(uint32_t blksz) 54303095Ssobomax{ 55303095Ssobomax struct mkuz_lzma *ulp; 56303095Ssobomax 57303095Ssobomax if (blksz % USED_BLOCKSIZE != 0) { 58303095Ssobomax errx(1, "cluster size should be multiple of %d", 59303095Ssobomax USED_BLOCKSIZE); 60303095Ssobomax /* Not reached */ 61303095Ssobomax } 62303095Ssobomax if (blksz > MAXPHYS) { 63303095Ssobomax errx(1, "cluster size is too large"); 64303095Ssobomax /* Not reached */ 65303095Ssobomax } 66303095Ssobomax ulp = mkuz_safe_zmalloc(sizeof(struct mkuz_lzma)); 67303095Ssobomax 68303095Ssobomax /* Init lzma encoder */ 69303095Ssobomax ulp->strm = lzma_stream_init; 70303095Ssobomax if (lzma_lzma_preset(&ulp->opt_lzma, LZMA_PRESET_DEFAULT)) 71303095Ssobomax errx(1, "Error loading LZMA preset"); 72303095Ssobomax 73303095Ssobomax ulp->filters[0].id = LZMA_FILTER_LZMA2; 74303095Ssobomax ulp->filters[0].options = &ulp->opt_lzma; 75303095Ssobomax ulp->filters[1].id = LZMA_VLI_UNKNOWN; 76303095Ssobomax 77303095Ssobomax ulp->blksz = blksz; 78303095Ssobomax 79303095Ssobomax return (void *)ulp; 80303095Ssobomax} 81303095Ssobomax 82303095Ssobomaxstruct mkuz_blk * 83303095Ssobomaxmkuz_lzma_compress(void *p, const struct mkuz_blk *iblk) 84303095Ssobomax{ 85303095Ssobomax lzma_ret ret; 86303095Ssobomax struct mkuz_blk *rval; 87303095Ssobomax struct mkuz_lzma *ulp; 88303095Ssobomax 89303095Ssobomax ulp = (struct mkuz_lzma *)p; 90303095Ssobomax 91303095Ssobomax rval = mkuz_blk_ctor(ulp->blksz * 2); 92303095Ssobomax 93303095Ssobomax ret = lzma_stream_encoder(&ulp->strm, ulp->filters, LZMA_CHECK_CRC32); 94303095Ssobomax if (ret != LZMA_OK) { 95303095Ssobomax if (ret == LZMA_MEMLIMIT_ERROR) 96303095Ssobomax errx(1, "can't compress data: LZMA_MEMLIMIT_ERROR"); 97303095Ssobomax 98303095Ssobomax errx(1, "can't compress data: LZMA compressor ERROR"); 99303095Ssobomax } 100303095Ssobomax 101303095Ssobomax ulp->strm.next_in = iblk->data; 102303095Ssobomax ulp->strm.avail_in = ulp->blksz; 103303095Ssobomax ulp->strm.next_out = rval->data; 104303095Ssobomax ulp->strm.avail_out = rval->alen; 105303095Ssobomax 106303095Ssobomax ret = lzma_code(&ulp->strm, LZMA_FINISH); 107303095Ssobomax 108303095Ssobomax if (ret != LZMA_STREAM_END) { 109303095Ssobomax /* Error */ 110303095Ssobomax errx(1, "lzma_code FINISH failed, code=%d, pos(in=%zd, " 111303095Ssobomax "out=%zd)", ret, (ulp->blksz - ulp->strm.avail_in), 112303095Ssobomax (ulp->blksz * 2 - ulp->strm.avail_out)); 113303095Ssobomax } 114303095Ssobomax 115303095Ssobomax#if 0 116303095Ssobomax lzma_end(&ulp->strm); 117303095Ssobomax#endif 118303095Ssobomax 119303095Ssobomax rval->info.len = rval->alen - ulp->strm.avail_out; 120303095Ssobomax return (rval); 121303095Ssobomax} 122