]> wimlib.net Git - wimlib/blob - src/decompress.c
v1.14.4
[wimlib] / src / decompress.c
1 /*
2  * decompress.c
3  *
4  * Generic functions for decompression, wrapping around actual decompression
5  * implementations.
6  */
7
8 /*
9  * Copyright (C) 2013, 2014 Eric Biggers
10  *
11  * This file is free software; you can redistribute it and/or modify it under
12  * the terms of the GNU Lesser General Public License as published by the Free
13  * Software Foundation; either version 3 of the License, or (at your option) any
14  * later version.
15  *
16  * This file is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with this file; if not, see https://www.gnu.org/licenses/.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #  include "config.h"
27 #endif
28
29 #include "wimlib.h"
30 #include "wimlib/decompressor_ops.h"
31 #include "wimlib/util.h"
32
33 struct wimlib_decompressor {
34         const struct decompressor_ops *ops;
35         size_t max_block_size;
36         void *private;
37 };
38
39 static const struct decompressor_ops * const decompressor_ops[] = {
40         [WIMLIB_COMPRESSION_TYPE_XPRESS] = &xpress_decompressor_ops,
41         [WIMLIB_COMPRESSION_TYPE_LZX]    = &lzx_decompressor_ops,
42         [WIMLIB_COMPRESSION_TYPE_LZMS]   = &lzms_decompressor_ops,
43 };
44
45 static bool
46 decompressor_ctype_valid(int ctype)
47 {
48         return (ctype >= 0 &&
49                 ctype < ARRAY_LEN(decompressor_ops) &&
50                 decompressor_ops[ctype] != NULL);
51 }
52
53 WIMLIBAPI int
54 wimlib_create_decompressor(enum wimlib_compression_type ctype,
55                            size_t max_block_size,
56                            struct wimlib_decompressor **dec_ret)
57 {
58         struct wimlib_decompressor *dec;
59         int ret;
60
61         ret = wimlib_global_init(0);
62         if (ret)
63                 return ret;
64
65         if (!decompressor_ctype_valid(ctype))
66                 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
67
68         if (dec_ret == NULL)
69                 return WIMLIB_ERR_INVALID_PARAM;
70
71         if (max_block_size == 0)
72                 return WIMLIB_ERR_INVALID_PARAM;
73
74         dec = MALLOC(sizeof(*dec));
75         if (dec == NULL)
76                 return WIMLIB_ERR_NOMEM;
77         dec->ops = decompressor_ops[ctype];
78         dec->max_block_size = max_block_size;
79         dec->private = NULL;
80         if (dec->ops->create_decompressor) {
81                 ret = dec->ops->create_decompressor(max_block_size,
82                                                     &dec->private);
83                 if (ret) {
84                         FREE(dec);
85                         return ret;
86                 }
87         }
88         *dec_ret = dec;
89         return 0;
90 }
91
92 WIMLIBAPI int
93 wimlib_decompress(const void *compressed_data, size_t compressed_size,
94                   void *uncompressed_data, size_t uncompressed_size,
95                   struct wimlib_decompressor *dec)
96 {
97         if (unlikely(uncompressed_size > dec->max_block_size))
98                 return -2;
99
100         return dec->ops->decompress(compressed_data, compressed_size,
101                                     uncompressed_data, uncompressed_size,
102                                     dec->private);
103 }
104
105 WIMLIBAPI void
106 wimlib_free_decompressor(struct wimlib_decompressor *dec)
107 {
108         if (dec) {
109                 if (dec->ops->free_decompressor)
110                         dec->ops->free_decompressor(dec->private);
111                 FREE(dec);
112         }
113 }