1/*- 2 * Copyright (c) 2004 Max Khon 3 * Copyright (c) 2014 Juniper Networks, Inc. 4 * Copyright (c) 2006-2016 Maxim Sobolev <sobomax@FreeBSD.org> 5 * Copyright (c) 2010-2012 Aleksandr Rybalko 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30#include <sys/types.h> 31#include <sys/malloc.h> 32#include <sys/systm.h> 33 34#include <contrib/xz-embedded/linux/include/linux/xz.h> 35 36#include <geom/uzip/g_uzip.h> 37#include <geom/uzip/g_uzip_dapi.h> 38#include <geom/uzip/g_uzip_lzma.h> 39 40struct g_uzip_lzma { 41 struct g_uzip_dapi pub; 42 uint32_t blksz; 43 /* XZ decoder structs */ 44 struct xz_buf b; 45 struct xz_dec *s; 46}; 47 48static int g_uzip_lzma_nop(struct g_uzip_dapi *, const char *); 49 50static void 51g_uzip_lzma_free(struct g_uzip_dapi *lzpp) 52{ 53 struct g_uzip_lzma *lzp; 54 55 lzp = (struct g_uzip_lzma *)lzpp->pvt; 56 if (lzp->s != NULL) { 57 xz_dec_end(lzp->s); 58 lzp->s = NULL; 59 } 60 61 free(lzp, M_GEOM_UZIP); 62} 63 64static int 65g_uzip_lzma_decompress(struct g_uzip_dapi *lzpp, const char *gp_name, 66 void *ibp, size_t ilen, void *obp) 67{ 68 struct g_uzip_lzma *lzp; 69 int err; 70 71 lzp = (struct g_uzip_lzma *)lzpp->pvt; 72 73 lzp->b.in = ibp; 74 lzp->b.out = obp; 75 lzp->b.in_pos = lzp->b.out_pos = 0; 76 lzp->b.in_size = ilen; 77 lzp->b.out_size = lzp->blksz; 78 err = (xz_dec_run(lzp->s, &lzp->b) != XZ_STREAM_END) ? 1 : 0; 79 /* TODO decoder recovery, if needed */ 80 if (err != 0) { 81 printf("%s: ibp=%p, obp=%p, in_pos=%jd, out_pos=%jd, " 82 "in_size=%jd, out_size=%jd\n", __func__, ibp, obp, 83 (intmax_t)lzp->b.in_pos, (intmax_t)lzp->b.out_pos, 84 (intmax_t)lzp->b.in_size, (intmax_t)lzp->b.out_size); 85 } 86 87 return (err); 88} 89 90static int 91LZ4_compressBound(int isize) 92{ 93 94 return (isize + (isize / 255) + 16); 95} 96 97struct g_uzip_dapi * 98g_uzip_lzma_ctor(uint32_t blksz) 99{ 100 struct g_uzip_lzma *lzp; 101 102 lzp = malloc(sizeof(struct g_uzip_lzma), M_GEOM_UZIP, M_WAITOK); 103 lzp->s = xz_dec_init(XZ_SINGLE, 0); 104 if (lzp->s == NULL) { 105 goto e1; 106 } 107 lzp->blksz = blksz; 108 lzp->pub.max_blen = LZ4_compressBound(blksz); 109 lzp->pub.decompress = &g_uzip_lzma_decompress; 110 lzp->pub.free = &g_uzip_lzma_free; 111 lzp->pub.rewind = &g_uzip_lzma_nop; 112 lzp->pub.pvt = lzp; 113 return (&lzp->pub); 114e1: 115 free(lzp, M_GEOM_UZIP); 116 return (NULL); 117} 118 119static int 120g_uzip_lzma_nop(struct g_uzip_dapi *zpp, const char *gp_name) 121{ 122 123 return (0); 124} 125