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 #include <sys/types.h>
32 #include <sys/malloc.h>
33 #include <sys/systm.h>
34
35 #include <contrib/xz-embedded/linux/include/linux/xz.h>
36
37 #include <geom/uzip/g_uzip.h>
38 #include <geom/uzip/g_uzip_dapi.h>
39 #include <geom/uzip/g_uzip_lzma.h>
40
41 struct g_uzip_lzma {
42 struct g_uzip_dapi pub;
43 uint32_t blksz;
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
g_uzip_lzma_free(struct g_uzip_dapi * lzpp)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
g_uzip_lzma_decompress(struct g_uzip_dapi * lzpp,const char * gp_name,void * ibp,size_t ilen,void * obp)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 = lzp->blksz;
79 err = (xz_dec_run(lzp->s, &lzp->b) != XZ_STREAM_END) ? 1 : 0;
80 /* TODO decoder recovery, if needed */
81 if (err != 0) {
82 printf("%s: ibp=%p, obp=%p, in_pos=%jd, out_pos=%jd, "
83 "in_size=%jd, out_size=%jd\n", __func__, ibp, obp,
84 (intmax_t)lzp->b.in_pos, (intmax_t)lzp->b.out_pos,
85 (intmax_t)lzp->b.in_size, (intmax_t)lzp->b.out_size);
86 }
87
88 return (err);
89 }
90
91 static int
LZ4_compressBound(int isize)92 LZ4_compressBound(int isize)
93 {
94
95 return (isize + (isize / 255) + 16);
96 }
97
98 struct g_uzip_dapi *
g_uzip_lzma_ctor(uint32_t blksz)99 g_uzip_lzma_ctor(uint32_t blksz)
100 {
101 struct g_uzip_lzma *lzp;
102
103 lzp = malloc(sizeof(struct g_uzip_lzma), M_GEOM_UZIP, M_WAITOK);
104 lzp->s = xz_dec_init(XZ_SINGLE, 0);
105 if (lzp->s == NULL) {
106 goto e1;
107 }
108 lzp->blksz = blksz;
109 lzp->pub.max_blen = LZ4_compressBound(blksz);
110 lzp->pub.decompress = &g_uzip_lzma_decompress;
111 lzp->pub.free = &g_uzip_lzma_free;
112 lzp->pub.rewind = &g_uzip_lzma_nop;
113 lzp->pub.pvt = lzp;
114 return (&lzp->pub);
115 e1:
116 free(lzp, M_GEOM_UZIP);
117 return (NULL);
118 }
119
120 static int
g_uzip_lzma_nop(struct g_uzip_dapi * zpp,const char * gp_name)121 g_uzip_lzma_nop(struct g_uzip_dapi *zpp, const char *gp_name)
122 {
123
124 return (0);
125 }
126