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/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <sys/types.h> 34 #include <sys/malloc.h> 35 36 #include <contrib/xz-embedded/linux/include/linux/xz.h> 37 38 #include <geom/uzip/g_uzip.h> 39 #include <geom/uzip/g_uzip_dapi.h> 40 #include <geom/uzip/g_uzip_lzma.h> 41 42 struct g_uzip_lzma { 43 struct g_uzip_dapi pub; 44 /* XZ decoder structs */ 45 struct xz_buf b; 46 struct xz_dec *s; 47 }; 48 49 static int g_uzip_lzma_nop(struct g_uzip_dapi *, const char *); 50 51 static void 52 g_uzip_lzma_free(struct g_uzip_dapi *lzpp) 53 { 54 struct g_uzip_lzma *lzp; 55 56 lzp = (struct g_uzip_lzma *)lzpp->pvt; 57 if (lzp->s != NULL) { 58 xz_dec_end(lzp->s); 59 lzp->s = NULL; 60 } 61 62 free(lzp, M_GEOM_UZIP); 63 } 64 65 static int 66 g_uzip_lzma_decompress(struct g_uzip_dapi *lzpp, const char *gp_name, 67 void *ibp, size_t ilen, void *obp) 68 { 69 struct g_uzip_lzma *lzp; 70 int err; 71 72 lzp = (struct g_uzip_lzma *)lzpp->pvt; 73 74 lzp->b.in = ibp; 75 lzp->b.out = obp; 76 lzp->b.in_pos = lzp->b.out_pos = 0; 77 lzp->b.in_size = ilen; 78 lzp->b.out_size = (size_t)-1; 79 err = (xz_dec_run(lzp->s, &lzp->b) != XZ_STREAM_END) ? 1 : 0; 80 /* TODO decoder recovery, if needed */ 81 82 return (err); 83 } 84 85 struct g_uzip_dapi * 86 g_uzip_lzma_ctor(uint32_t blksz) 87 { 88 struct g_uzip_lzma *lzp; 89 90 lzp = malloc(sizeof(struct g_uzip_lzma), M_GEOM_UZIP, M_WAITOK); 91 xz_crc32_init(); 92 lzp->s = xz_dec_init(XZ_SINGLE, 0); 93 if (lzp->s == NULL) { 94 goto e1; 95 } 96 lzp->pub.decompress = &g_uzip_lzma_decompress; 97 lzp->pub.free = &g_uzip_lzma_free; 98 lzp->pub.rewind = &g_uzip_lzma_nop; 99 lzp->pub.pvt = lzp; 100 return (&lzp->pub); 101 e1: 102 free(lzp, M_GEOM_UZIP); 103 return (NULL); 104 } 105 106 static int 107 g_uzip_lzma_nop(struct g_uzip_dapi *zpp, const char *gp_name) 108 { 109 110 return (0); 111 } 112