xref: /freebsd/usr.bin/mkuzip/mkuz_lzma.c (revision 81ad6265)
1 /*
2  * Copyright (c) 2004-2016 Maxim Sobolev <sobomax@FreeBSD.org>
3  * Copyright (c) 2011 Aleksandr Rybalko <ray@ddteam.net>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <err.h>
33 #include <stdint.h>
34 
35 #include <lzma.h>
36 
37 #include "mkuzip.h"
38 #include "mkuz_blk.h"
39 #include "mkuz_lzma.h"
40 
41 struct mkuz_lzma {
42 	lzma_filter filters[2];
43 	lzma_options_lzma opt_lzma;
44 	lzma_stream strm;
45 };
46 
47 size_t
48 mkuz_lzma_cbound(size_t blksz)
49 {
50 	return (lzma_stream_buffer_bound(blksz));
51 }
52 
53 void *
54 mkuz_lzma_init(int *comp_level)
55 {
56 	struct mkuz_lzma *ulp;
57 
58 	if (*comp_level == USE_DEFAULT_LEVEL)
59 		*comp_level = LZMA_PRESET_DEFAULT;
60 	if (*comp_level < 0 || *comp_level > 9)
61 		errx(1, "provided compression level %d is invalid",
62 		    *comp_level);
63 		/* Not reached */
64 
65 	ulp = mkuz_safe_zmalloc(sizeof(struct mkuz_lzma));
66 
67 	/* Init lzma encoder */
68 	ulp->strm = (lzma_stream)LZMA_STREAM_INIT;
69 	if (lzma_lzma_preset(&ulp->opt_lzma, *comp_level))
70 		errx(1, "Error loading LZMA preset");
71 
72 	ulp->filters[0].id = LZMA_FILTER_LZMA2;
73 	ulp->filters[0].options = &ulp->opt_lzma;
74 	ulp->filters[1].id = LZMA_VLI_UNKNOWN;
75 
76 	return (void *)ulp;
77 }
78 
79 void
80 mkuz_lzma_compress(void *p, const struct mkuz_blk *iblk, struct mkuz_blk *oblk)
81 {
82 	lzma_ret ret;
83 	struct mkuz_lzma *ulp;
84 
85 	ulp = (struct mkuz_lzma *)p;
86 
87 	ret = lzma_stream_encoder(&ulp->strm, ulp->filters, LZMA_CHECK_CRC32);
88 	if (ret != LZMA_OK) {
89 		if (ret == LZMA_MEMLIMIT_ERROR)
90 			errx(1, "can't compress data: LZMA_MEMLIMIT_ERROR");
91 
92 		errx(1, "can't compress data: LZMA compressor ERROR");
93 	}
94 
95 	ulp->strm.next_in = iblk->data;
96 	ulp->strm.avail_in = iblk->info.len;
97 	ulp->strm.next_out = oblk->data;
98 	ulp->strm.avail_out = oblk->alen;
99 
100 	ret = lzma_code(&ulp->strm, LZMA_FINISH);
101 
102 	if (ret != LZMA_STREAM_END)
103 		errx(1, "lzma_code FINISH failed, code=%d, pos(in=%zd, "
104 		    "out=%zd)", ret, (iblk->info.len - ulp->strm.avail_in),
105 		    (oblk->alen - ulp->strm.avail_out));
106 
107 #if 0
108 	lzma_end(&ulp->strm);
109 #endif
110 
111 	oblk->info.len = oblk->alen - ulp->strm.avail_out;
112 }
113