xref: /freebsd/sys/geom/uzip/g_uzip_lzma.c (revision c697fb7f)
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 #include <sys/systm.h>
36 
37 #include <contrib/xz-embedded/linux/include/linux/xz.h>
38 
39 #include <geom/uzip/g_uzip.h>
40 #include <geom/uzip/g_uzip_dapi.h>
41 #include <geom/uzip/g_uzip_lzma.h>
42 
43 struct g_uzip_lzma {
44 	struct g_uzip_dapi pub;
45 	uint32_t blksz;
46 	/* XZ decoder structs */
47 	struct xz_buf b;
48 	struct xz_dec *s;
49 };
50 
51 static int g_uzip_lzma_nop(struct g_uzip_dapi *, const char *);
52 
53 static void
54 g_uzip_lzma_free(struct g_uzip_dapi *lzpp)
55 {
56 	struct g_uzip_lzma *lzp;
57 
58 	lzp = (struct g_uzip_lzma *)lzpp->pvt;
59 	if (lzp->s != NULL) {
60 		xz_dec_end(lzp->s);
61 		lzp->s = NULL;
62 	}
63 
64 	free(lzp, M_GEOM_UZIP);
65 }
66 
67 static int
68 g_uzip_lzma_decompress(struct g_uzip_dapi *lzpp, const char *gp_name,
69     void *ibp, size_t ilen, void *obp)
70 {
71 	struct g_uzip_lzma *lzp;
72 	int err;
73 
74 	lzp = (struct g_uzip_lzma *)lzpp->pvt;
75 
76 	lzp->b.in = ibp;
77 	lzp->b.out = obp;
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 */
83 	if (err != 0) {
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);
88 	}
89 
90 	return (err);
91 }
92 
93 static int
94 LZ4_compressBound(int isize)
95 {
96 
97         return (isize + (isize / 255) + 16);
98 }
99 
100 struct g_uzip_dapi *
101 g_uzip_lzma_ctor(uint32_t blksz)
102 {
103 	struct g_uzip_lzma *lzp;
104 
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) {
108 		goto e1;
109 	}
110 	lzp->blksz = blksz;
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;
115 	lzp->pub.pvt = lzp;
116 	return (&lzp->pub);
117 e1:
118         free(lzp, M_GEOM_UZIP);
119         return (NULL);
120 }
121 
122 static int
123 g_uzip_lzma_nop(struct g_uzip_dapi *zpp, const char *gp_name)
124 {
125 
126         return (0);
127 }
128