xref: /freebsd/sys/geom/uzip/g_uzip_zlib.c (revision acc1a9ef)
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  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/types.h>
33 #include <sys/systm.h>
34 #include <sys/malloc.h>
35 
36 #include <sys/zlib.h>
37 
38 #include <geom/uzip/g_uzip.h>
39 #include <geom/uzip/g_uzip_dapi.h>
40 #include <geom/uzip/g_uzip_zlib.h>
41 
42 struct g_uzip_zlib {
43 	uint32_t blksz;
44 	struct g_uzip_dapi pub;
45 	/* Zlib decoder structs */
46 	z_stream zs;
47 };
48 
49 static void *z_alloc(void *, u_int, u_int);
50 static void z_free(void *, void *);
51 static int g_uzip_zlib_rewind(struct g_uzip_dapi *, const char *);
52 
53 static void
54 g_uzip_zlib_free(struct g_uzip_dapi *zpp)
55 {
56 	struct g_uzip_zlib *zp;
57 
58 	zp = (struct g_uzip_zlib *)zpp->pvt;
59 	inflateEnd(&zp->zs);
60 	free(zp, M_GEOM_UZIP);
61 }
62 
63 static int
64 g_uzip_zlib_decompress(struct g_uzip_dapi *zpp, const char *gp_name, void *ibp,
65     size_t ilen, void *obp)
66 {
67 	int err;
68 	struct g_uzip_zlib *zp;
69 
70 	zp = (struct g_uzip_zlib *)zpp->pvt;
71 
72 	zp->zs.next_in = ibp;
73 	zp->zs.avail_in = ilen;
74 	zp->zs.next_out = obp;
75 	zp->zs.avail_out = zp->blksz;
76 
77 	err = (inflate(&zp->zs, Z_FINISH) != Z_STREAM_END) ? 1 : 0;
78 	if (err != 0) {
79 		printf("%s: UZIP(zlib) inflate() failed\n", gp_name);
80 	}
81 	return (err);
82 }
83 
84 static int
85 g_uzip_zlib_rewind(struct g_uzip_dapi *zpp, const char *gp_name)
86 {
87 	int err;
88 	struct g_uzip_zlib *zp;
89 
90 	zp = (struct g_uzip_zlib *)zpp->pvt;
91 
92 	err = 0;
93 	if (inflateReset(&zp->zs) != Z_OK) {
94 		printf("%s: UZIP(zlib) decoder reset failed\n", gp_name);
95 		err = 1;
96 	}
97 	return (err);
98 }
99 
100 struct g_uzip_dapi *
101 g_uzip_zlib_ctor(uint32_t blksz)
102 {
103 	struct g_uzip_zlib *zp;
104 
105 	zp = malloc(sizeof(struct g_uzip_zlib), M_GEOM_UZIP, M_WAITOK);
106 	zp->zs.zalloc = z_alloc;
107 	zp->zs.zfree = z_free;
108 	if (inflateInit(&zp->zs) != Z_OK) {
109 		goto e1;
110 	}
111 	zp->blksz = blksz;
112 	zp->pub.decompress = &g_uzip_zlib_decompress;
113 	zp->pub.free = &g_uzip_zlib_free;
114 	zp->pub.rewind = &g_uzip_zlib_rewind;
115 	zp->pub.pvt = (void *)zp;
116 	return (&zp->pub);
117 e1:
118 	free(zp, M_GEOM_UZIP);
119 	return (NULL);
120 }
121 
122 static void *
123 z_alloc(void *nil, u_int type, u_int size)
124 {
125         void *ptr;
126 
127         ptr = malloc(type * size, M_GEOM_UZIP, M_NOWAIT);
128 
129         return (ptr);
130 }
131 
132 static void
133 z_free(void *nil, void *ptr)
134 {
135 
136         free(ptr, M_GEOM_UZIP);
137 }
138