1 /* 2 * Copyright (c) 2004-2016 Maxim Sobolev <sobomax@FreeBSD.org> 3 * Copyright (c) 2011 Aleksandr Rybalko <ray@ddteam.net> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/param.h> 32 #include <err.h> 33 #include <stdint.h> 34 35 #include <lzma.h> 36 37 #include "mkuzip.h" 38 #include "mkuz_lzma.h" 39 #include "mkuz_blk.h" 40 41 #define USED_BLOCKSIZE DEV_BSIZE 42 43 struct mkuz_lzma { 44 lzma_filter filters[2]; 45 lzma_options_lzma opt_lzma; 46 lzma_stream strm; 47 uint32_t blksz; 48 }; 49 50 static const lzma_stream lzma_stream_init = LZMA_STREAM_INIT; 51 52 void * 53 mkuz_lzma_init(uint32_t blksz) 54 { 55 struct mkuz_lzma *ulp; 56 57 if (blksz % USED_BLOCKSIZE != 0) { 58 errx(1, "cluster size should be multiple of %d", 59 USED_BLOCKSIZE); 60 /* Not reached */ 61 } 62 if (blksz > MAXPHYS) { 63 errx(1, "cluster size is too large"); 64 /* Not reached */ 65 } 66 ulp = mkuz_safe_zmalloc(sizeof(struct mkuz_lzma)); 67 68 /* Init lzma encoder */ 69 ulp->strm = lzma_stream_init; 70 if (lzma_lzma_preset(&ulp->opt_lzma, LZMA_PRESET_DEFAULT)) 71 errx(1, "Error loading LZMA preset"); 72 73 ulp->filters[0].id = LZMA_FILTER_LZMA2; 74 ulp->filters[0].options = &ulp->opt_lzma; 75 ulp->filters[1].id = LZMA_VLI_UNKNOWN; 76 77 ulp->blksz = blksz; 78 79 return (void *)ulp; 80 } 81 82 struct mkuz_blk * 83 mkuz_lzma_compress(void *p, const struct mkuz_blk *iblk) 84 { 85 lzma_ret ret; 86 struct mkuz_blk *rval; 87 struct mkuz_lzma *ulp; 88 89 ulp = (struct mkuz_lzma *)p; 90 91 rval = mkuz_blk_ctor(ulp->blksz * 2); 92 93 ret = lzma_stream_encoder(&ulp->strm, ulp->filters, LZMA_CHECK_CRC32); 94 if (ret != LZMA_OK) { 95 if (ret == LZMA_MEMLIMIT_ERROR) 96 errx(1, "can't compress data: LZMA_MEMLIMIT_ERROR"); 97 98 errx(1, "can't compress data: LZMA compressor ERROR"); 99 } 100 101 ulp->strm.next_in = iblk->data; 102 ulp->strm.avail_in = ulp->blksz; 103 ulp->strm.next_out = rval->data; 104 ulp->strm.avail_out = rval->alen; 105 106 ret = lzma_code(&ulp->strm, LZMA_FINISH); 107 108 if (ret != LZMA_STREAM_END) { 109 /* Error */ 110 errx(1, "lzma_code FINISH failed, code=%d, pos(in=%zd, " 111 "out=%zd)", ret, (ulp->blksz - ulp->strm.avail_in), 112 (ulp->blksz * 2 - ulp->strm.avail_out)); 113 } 114 115 #if 0 116 lzma_end(&ulp->strm); 117 #endif 118 119 rval->info.len = rval->alen - ulp->strm.avail_out; 120 return (rval); 121 } 122