xref: /freebsd/usr.bin/mkuzip/mkuz_zlib.c (revision 5e3934b15a2741b2de6b217e77dc9d798d740804)
18f8cb840SMaxim Sobolev /*
28f8cb840SMaxim Sobolev  * Copyright (c) 2004-2016 Maxim Sobolev <sobomax@FreeBSD.org>
38f8cb840SMaxim Sobolev  * All rights reserved.
48f8cb840SMaxim Sobolev  *
58f8cb840SMaxim Sobolev  * Redistribution and use in source and binary forms, with or without
68f8cb840SMaxim Sobolev  * modification, are permitted provided that the following conditions
78f8cb840SMaxim Sobolev  * are met:
88f8cb840SMaxim Sobolev  * 1. Redistributions of source code must retain the above copyright
98f8cb840SMaxim Sobolev  *    notice, this list of conditions and the following disclaimer.
108f8cb840SMaxim Sobolev  * 2. Redistributions in binary form must reproduce the above copyright
118f8cb840SMaxim Sobolev  *    notice, this list of conditions and the following disclaimer in the
128f8cb840SMaxim Sobolev  *    documentation and/or other materials provided with the distribution.
138f8cb840SMaxim Sobolev  *
148f8cb840SMaxim Sobolev  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
158f8cb840SMaxim Sobolev  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
168f8cb840SMaxim Sobolev  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
178f8cb840SMaxim Sobolev  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
188f8cb840SMaxim Sobolev  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
198f8cb840SMaxim Sobolev  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
208f8cb840SMaxim Sobolev  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
218f8cb840SMaxim Sobolev  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
228f8cb840SMaxim Sobolev  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
238f8cb840SMaxim Sobolev  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
248f8cb840SMaxim Sobolev  * SUCH DAMAGE.
258f8cb840SMaxim Sobolev  */
268f8cb840SMaxim Sobolev 
278f8cb840SMaxim Sobolev #include <sys/param.h>
288f8cb840SMaxim Sobolev #include <err.h>
298f8cb840SMaxim Sobolev #include <stdint.h>
308f8cb840SMaxim Sobolev 
318f8cb840SMaxim Sobolev #include <zlib.h>
328f8cb840SMaxim Sobolev 
338f8cb840SMaxim Sobolev #include "mkuzip.h"
344fc55e3eSMaxim Sobolev #include "mkuz_blk.h"
35*eefd8f96SConrad Meyer #include "mkuz_zlib.h"
368f8cb840SMaxim Sobolev 
378f8cb840SMaxim Sobolev struct mkuz_zlib {
38*eefd8f96SConrad Meyer 	int comp_level;
398f8cb840SMaxim Sobolev };
408f8cb840SMaxim Sobolev 
41*eefd8f96SConrad Meyer size_t
mkuz_zlib_cbound(size_t blksz)42*eefd8f96SConrad Meyer mkuz_zlib_cbound(size_t blksz)
43*eefd8f96SConrad Meyer {
44*eefd8f96SConrad Meyer 	return (compressBound(blksz));
45*eefd8f96SConrad Meyer }
46*eefd8f96SConrad Meyer 
478f8cb840SMaxim Sobolev void *
mkuz_zlib_init(int * comp_level)48*eefd8f96SConrad Meyer mkuz_zlib_init(int *comp_level)
498f8cb840SMaxim Sobolev {
504fc55e3eSMaxim Sobolev 	struct mkuz_zlib *zp;
514fc55e3eSMaxim Sobolev 
52*eefd8f96SConrad Meyer 	if (*comp_level == USE_DEFAULT_LEVEL)
53*eefd8f96SConrad Meyer 		*comp_level = Z_BEST_COMPRESSION;
54*eefd8f96SConrad Meyer 	if (*comp_level < Z_BEST_SPEED || *comp_level > Z_BEST_COMPRESSION)
55*eefd8f96SConrad Meyer 		errx(1, "provided compression level %d is invalid",
56*eefd8f96SConrad Meyer 		    *comp_level);
578f8cb840SMaxim Sobolev 		/* Not reached */
58*eefd8f96SConrad Meyer 
594fc55e3eSMaxim Sobolev 	zp = mkuz_safe_zmalloc(sizeof(struct mkuz_zlib));
60*eefd8f96SConrad Meyer 	zp->comp_level = *comp_level;
618f8cb840SMaxim Sobolev 
62*eefd8f96SConrad Meyer 	return (zp);
638f8cb840SMaxim Sobolev }
648f8cb840SMaxim Sobolev 
65*eefd8f96SConrad Meyer void
mkuz_zlib_compress(void * p,const struct mkuz_blk * iblk,struct mkuz_blk * oblk)66*eefd8f96SConrad Meyer mkuz_zlib_compress(void *p, const struct mkuz_blk *iblk, struct mkuz_blk *oblk)
678f8cb840SMaxim Sobolev {
688f8cb840SMaxim Sobolev 	uLongf destlen_z;
694fc55e3eSMaxim Sobolev 	struct mkuz_zlib *zp;
708f8cb840SMaxim Sobolev 
714fc55e3eSMaxim Sobolev 	zp = (struct mkuz_zlib *)p;
724fc55e3eSMaxim Sobolev 
73*eefd8f96SConrad Meyer 	destlen_z = oblk->alen;
74*eefd8f96SConrad Meyer 	if (compress2(oblk->data, &destlen_z, iblk->data, iblk->info.len,
75*eefd8f96SConrad Meyer 	    zp->comp_level) != Z_OK) {
76*eefd8f96SConrad Meyer 		errx(1, "can't compress data: compress2() failed");
778f8cb840SMaxim Sobolev 		/* Not reached */
788f8cb840SMaxim Sobolev 	}
798f8cb840SMaxim Sobolev 
80*eefd8f96SConrad Meyer 	oblk->info.len = (uint32_t)destlen_z;
818f8cb840SMaxim Sobolev }
82