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
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/types.h>
34 #include <sys/malloc.h>
35 #include <sys/systm.h>
37 #include <contrib/xz-embedded/linux/include/linux/xz.h>
39 #include <geom/uzip/g_uzip.h>
40 #include <geom/uzip/g_uzip_dapi.h>
41 #include <geom/uzip/g_uzip_lzma.h>
44 struct g_uzip_dapi pub;
46 /* XZ decoder structs */
51 static int g_uzip_lzma_nop(struct g_uzip_dapi *, const char *);
54 g_uzip_lzma_free(struct g_uzip_dapi *lzpp)
56 struct g_uzip_lzma *lzp;
58 lzp = (struct g_uzip_lzma *)lzpp->pvt;
64 free(lzp, M_GEOM_UZIP);
68 g_uzip_lzma_decompress(struct g_uzip_dapi *lzpp, const char *gp_name,
69 void *ibp, size_t ilen, void *obp)
71 struct g_uzip_lzma *lzp;
74 lzp = (struct g_uzip_lzma *)lzpp->pvt;
78 lzp->b.in_pos = lzp->b.out_pos = 0;
79 lzp->b.in_size = ilen;
80 lzp->b.out_size = lzp->blksz;
81 err = (xz_dec_run(lzp->s, &lzp->b) != XZ_STREAM_END) ? 1 : 0;
82 /* TODO decoder recovery, if needed */
84 printf("%s: ibp=%p, obp=%p, in_pos=%jd, out_pos=%jd, "
85 "in_size=%jd, out_size=%jd\n", __func__, ibp, obp,
86 (intmax_t)lzp->b.in_pos, (intmax_t)lzp->b.out_pos,
87 (intmax_t)lzp->b.in_size, (intmax_t)lzp->b.out_size);
94 LZ4_compressBound(int isize)
97 return (isize + (isize / 255) + 16);
101 g_uzip_lzma_ctor(uint32_t blksz)
103 struct g_uzip_lzma *lzp;
105 lzp = malloc(sizeof(struct g_uzip_lzma), M_GEOM_UZIP, M_WAITOK);
106 lzp->s = xz_dec_init(XZ_SINGLE, 0);
107 if (lzp->s == NULL) {
111 lzp->pub.max_blen = LZ4_compressBound(blksz);
112 lzp->pub.decompress = &g_uzip_lzma_decompress;
113 lzp->pub.free = &g_uzip_lzma_free;
114 lzp->pub.rewind = &g_uzip_lzma_nop;
118 free(lzp, M_GEOM_UZIP);
123 g_uzip_lzma_nop(struct g_uzip_dapi *zpp, const char *gp_name)