xref: /freebsd/sys/geom/uzip/g_uzip_zlib.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
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/types.h>
30 #include <sys/systm.h>
31 #include <sys/malloc.h>
32 
33 #include <contrib/zlib/zlib.h>
34 #include <dev/zlib/zcalloc.h>
35 
36 #include <geom/uzip/g_uzip.h>
37 #include <geom/uzip/g_uzip_dapi.h>
38 #include <geom/uzip/g_uzip_zlib.h>
39 
40 struct g_uzip_zlib {
41 	uint32_t blksz;
42 	struct g_uzip_dapi pub;
43 	/* Zlib decoder structs */
44 	z_stream zs;
45 };
46 
47 static int g_uzip_zlib_rewind(struct g_uzip_dapi *, const char *);
48 
49 static void
g_uzip_zlib_free(struct g_uzip_dapi * zpp)50 g_uzip_zlib_free(struct g_uzip_dapi *zpp)
51 {
52 	struct g_uzip_zlib *zp;
53 
54 	zp = (struct g_uzip_zlib *)zpp->pvt;
55 	inflateEnd(&zp->zs);
56 	free(zp, M_GEOM_UZIP);
57 }
58 
59 static int
g_uzip_zlib_decompress(struct g_uzip_dapi * zpp,const char * gp_name,void * ibp,size_t ilen,void * obp)60 g_uzip_zlib_decompress(struct g_uzip_dapi *zpp, const char *gp_name, void *ibp,
61     size_t ilen, void *obp)
62 {
63 	int err;
64 	struct g_uzip_zlib *zp;
65 
66 	zp = (struct g_uzip_zlib *)zpp->pvt;
67 
68 	zp->zs.next_in = ibp;
69 	zp->zs.avail_in = ilen;
70 	zp->zs.next_out = obp;
71 	zp->zs.avail_out = zp->blksz;
72 
73 	err = (inflate(&zp->zs, Z_FINISH) != Z_STREAM_END) ? 1 : 0;
74 	if (err != 0) {
75 		printf("%s: UZIP(zlib) inflate() failed\n", gp_name);
76 	}
77 	return (err);
78 }
79 
80 static int
g_uzip_zlib_rewind(struct g_uzip_dapi * zpp,const char * gp_name)81 g_uzip_zlib_rewind(struct g_uzip_dapi *zpp, const char *gp_name)
82 {
83 	int err;
84 	struct g_uzip_zlib *zp;
85 
86 	zp = (struct g_uzip_zlib *)zpp->pvt;
87 
88 	err = 0;
89 	if (inflateReset(&zp->zs) != Z_OK) {
90 		printf("%s: UZIP(zlib) decoder reset failed\n", gp_name);
91 		err = 1;
92 	}
93 	return (err);
94 }
95 
96 struct g_uzip_dapi *
g_uzip_zlib_ctor(uint32_t blksz)97 g_uzip_zlib_ctor(uint32_t blksz)
98 {
99 	struct g_uzip_zlib *zp;
100 
101 	zp = malloc(sizeof(struct g_uzip_zlib), M_GEOM_UZIP, M_WAITOK | M_ZERO);
102 	if (inflateInit(&zp->zs) != Z_OK) {
103 		goto e1;
104 	}
105 	zp->blksz = blksz;
106 	zp->pub.max_blen = compressBound(blksz);
107 	zp->pub.decompress = &g_uzip_zlib_decompress;
108 	zp->pub.free = &g_uzip_zlib_free;
109 	zp->pub.rewind = &g_uzip_zlib_rewind;
110 	zp->pub.pvt = (void *)zp;
111 	return (&zp->pub);
112 e1:
113 	free(zp, M_GEOM_UZIP);
114 	return (NULL);
115 }
116