xref: /freebsd/usr.bin/mkuzip/mkuz_zlib.c (revision 8f8cb840b0d64bb227909d4866b7734d3a0d200d)
1*8f8cb840SMaxim Sobolev /*
2*8f8cb840SMaxim Sobolev  * Copyright (c) 2004-2016 Maxim Sobolev <sobomax@FreeBSD.org>
3*8f8cb840SMaxim Sobolev  * All rights reserved.
4*8f8cb840SMaxim Sobolev  *
5*8f8cb840SMaxim Sobolev  * Redistribution and use in source and binary forms, with or without
6*8f8cb840SMaxim Sobolev  * modification, are permitted provided that the following conditions
7*8f8cb840SMaxim Sobolev  * are met:
8*8f8cb840SMaxim Sobolev  * 1. Redistributions of source code must retain the above copyright
9*8f8cb840SMaxim Sobolev  *    notice, this list of conditions and the following disclaimer.
10*8f8cb840SMaxim Sobolev  * 2. Redistributions in binary form must reproduce the above copyright
11*8f8cb840SMaxim Sobolev  *    notice, this list of conditions and the following disclaimer in the
12*8f8cb840SMaxim Sobolev  *    documentation and/or other materials provided with the distribution.
13*8f8cb840SMaxim Sobolev  *
14*8f8cb840SMaxim Sobolev  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*8f8cb840SMaxim Sobolev  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*8f8cb840SMaxim Sobolev  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*8f8cb840SMaxim Sobolev  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*8f8cb840SMaxim Sobolev  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*8f8cb840SMaxim Sobolev  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*8f8cb840SMaxim Sobolev  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*8f8cb840SMaxim Sobolev  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*8f8cb840SMaxim Sobolev  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*8f8cb840SMaxim Sobolev  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*8f8cb840SMaxim Sobolev  * SUCH DAMAGE.
25*8f8cb840SMaxim Sobolev  *
26*8f8cb840SMaxim Sobolev  */
27*8f8cb840SMaxim Sobolev 
28*8f8cb840SMaxim Sobolev #include <sys/cdefs.h>
29*8f8cb840SMaxim Sobolev __FBSDID("$FreeBSD$");
30*8f8cb840SMaxim Sobolev 
31*8f8cb840SMaxim Sobolev #include <sys/param.h>
32*8f8cb840SMaxim Sobolev #include <err.h>
33*8f8cb840SMaxim Sobolev #include <stdint.h>
34*8f8cb840SMaxim Sobolev 
35*8f8cb840SMaxim Sobolev #include <zlib.h>
36*8f8cb840SMaxim Sobolev 
37*8f8cb840SMaxim Sobolev #include "mkuzip.h"
38*8f8cb840SMaxim Sobolev #include "mkuz_zlib.h"
39*8f8cb840SMaxim Sobolev 
40*8f8cb840SMaxim Sobolev struct mkuz_zlib {
41*8f8cb840SMaxim Sobolev 	char *obuf;
42*8f8cb840SMaxim Sobolev 	uLongf oblen;
43*8f8cb840SMaxim Sobolev 	uint32_t blksz;
44*8f8cb840SMaxim Sobolev };
45*8f8cb840SMaxim Sobolev 
46*8f8cb840SMaxim Sobolev static struct mkuz_zlib uzip;
47*8f8cb840SMaxim Sobolev 
48*8f8cb840SMaxim Sobolev void *
49*8f8cb840SMaxim Sobolev mkuz_zlib_init(uint32_t blksz)
50*8f8cb840SMaxim Sobolev {
51*8f8cb840SMaxim Sobolev 	if (blksz % DEV_BSIZE != 0) {
52*8f8cb840SMaxim Sobolev 		errx(1, "cluster size should be multiple of %d",
53*8f8cb840SMaxim Sobolev 		    DEV_BSIZE);
54*8f8cb840SMaxim Sobolev 		/* Not reached */
55*8f8cb840SMaxim Sobolev 	}
56*8f8cb840SMaxim Sobolev 	if (compressBound(blksz) > MAXPHYS) {
57*8f8cb840SMaxim Sobolev 		errx(1, "cluster size is too large");
58*8f8cb840SMaxim Sobolev 		/* Not reached */
59*8f8cb840SMaxim Sobolev 	}
60*8f8cb840SMaxim Sobolev 	uzip.oblen = compressBound(blksz);
61*8f8cb840SMaxim Sobolev 	uzip.obuf = mkuz_safe_malloc(uzip.oblen);
62*8f8cb840SMaxim Sobolev 	uzip.blksz = blksz;
63*8f8cb840SMaxim Sobolev 
64*8f8cb840SMaxim Sobolev 	return (uzip.obuf);
65*8f8cb840SMaxim Sobolev }
66*8f8cb840SMaxim Sobolev 
67*8f8cb840SMaxim Sobolev void
68*8f8cb840SMaxim Sobolev mkuz_zlib_compress(const char *ibuf, uint32_t *destlen)
69*8f8cb840SMaxim Sobolev {
70*8f8cb840SMaxim Sobolev 	uLongf destlen_z;
71*8f8cb840SMaxim Sobolev 
72*8f8cb840SMaxim Sobolev 	destlen_z = uzip.oblen;
73*8f8cb840SMaxim Sobolev 	if (compress2(uzip.obuf, &destlen_z, ibuf, uzip.blksz,
74*8f8cb840SMaxim Sobolev 	    Z_BEST_COMPRESSION) != Z_OK) {
75*8f8cb840SMaxim Sobolev 		errx(1, "can't compress data: compress2() "
76*8f8cb840SMaxim Sobolev 		    "failed");
77*8f8cb840SMaxim Sobolev 		/* Not reached */
78*8f8cb840SMaxim Sobolev 	}
79*8f8cb840SMaxim Sobolev 
80*8f8cb840SMaxim Sobolev 	*destlen = (uint32_t)destlen_z;
81*8f8cb840SMaxim Sobolev }
82