]> wimlib.net Git - wimlib/blob - src/decompress.c
New compression/decompression API
[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 Eric Biggers
10  *
11  * This file is part of wimlib, a library for working with WIM files.
12  *
13  * wimlib is free software; you can redistribute it and/or modify it under the
14  * terms of the GNU General Public License as published by the Free
15  * Software Foundation; either version 3 of the License, or (at your option)
16  * any later version.
17  *
18  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
19  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20  * A PARTICULAR PURPOSE. See the GNU General Public License for more
21  * details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with wimlib; if not, see http://www.gnu.org/licenses/.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #  include "config.h"
29 #endif
30
31 #include "wimlib.h"
32 #include "wimlib/decompressor_ops.h"
33 #include "wimlib/util.h"
34
35 struct wimlib_decompressor {
36         const struct decompressor_ops *ops;
37         void *private;
38 };
39
40 static const struct decompressor_ops *decompressor_ops[] = {
41         [WIMLIB_COMPRESSION_TYPE_LZX]    = &lzx_decompressor_ops,
42         [WIMLIB_COMPRESSION_TYPE_XPRESS] = &xpress_decompressor_ops,
43         [WIMLIB_COMPRESSION_TYPE_LZMS]   = &lzms_decompressor_ops,
44 };
45
46 static struct wimlib_decompressor_params_header *
47 decompressor_default_params[ARRAY_LEN(decompressor_ops)] = {
48         [WIMLIB_COMPRESSION_TYPE_LZX]    = NULL,
49         [WIMLIB_COMPRESSION_TYPE_XPRESS] = NULL,
50         [WIMLIB_COMPRESSION_TYPE_LZMS]   = NULL,
51 };
52
53 static bool
54 decompressor_ctype_valid(int ctype)
55 {
56         return (ctype >= 0 &&
57                 ctype < ARRAY_LEN(decompressor_ops) &&
58                 decompressor_ops[ctype] != NULL);
59 }
60
61 WIMLIBAPI int
62 wimlib_set_default_decompressor_params(enum wimlib_compression_type ctype,
63                                        const struct wimlib_decompressor_params_header *params)
64 {
65         struct wimlib_decompressor_params_header *dup;
66
67         if (!decompressor_ctype_valid(ctype))
68                 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
69
70         dup = NULL;
71         if (params) {
72                 dup = memdup(params, params->size);
73                 if (dup == NULL)
74                         return WIMLIB_ERR_NOMEM;
75         }
76
77         FREE(decompressor_default_params[ctype]);
78         decompressor_default_params[ctype] = dup;
79         return 0;
80 }
81
82 void
83 cleanup_decompressor_params(void)
84 {
85         for (size_t i = 0; i < ARRAY_LEN(decompressor_default_params); i++) {
86                 FREE(decompressor_default_params[i]);
87                 decompressor_default_params[i] = NULL;
88         }
89 }
90
91 WIMLIBAPI int
92 wimlib_create_decompressor(enum wimlib_compression_type ctype,
93                            size_t max_block_size,
94                            const struct wimlib_decompressor_params_header *extra_params,
95                            struct wimlib_decompressor **dec_ret)
96 {
97         struct wimlib_decompressor *dec;
98
99         if (dec_ret == NULL)
100                 return WIMLIB_ERR_INVALID_PARAM;
101
102         if (!decompressor_ctype_valid(ctype))
103                 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
104
105         dec = MALLOC(sizeof(*dec));
106         if (dec == NULL)
107                 return WIMLIB_ERR_NOMEM;
108         dec->ops = decompressor_ops[ctype];
109         dec->private = NULL;
110         if (dec->ops->create_decompressor) {
111                 const struct wimlib_decompressor_params_header *params;
112                 int ret;
113
114                 if (extra_params)
115                         params = extra_params;
116                 else
117                         params = decompressor_default_params[ctype];
118                 ret = dec->ops->create_decompressor(max_block_size,
119                                                     params,
120                                                     &dec->private);
121                 if (ret) {
122                         FREE(dec);
123                         return ret;
124                 }
125         }
126         *dec_ret = dec;
127         return 0;
128 }
129
130 WIMLIBAPI int
131 wimlib_decompress(const void *compressed_data, size_t compressed_size,
132                   void *uncompressed_data, size_t uncompressed_size,
133                   struct wimlib_decompressor *dec)
134 {
135         return dec->ops->decompress(compressed_data, compressed_size,
136                                     uncompressed_data, uncompressed_size,
137                                     dec->private);
138 }
139
140 WIMLIBAPI void
141 wimlib_free_decompressor(struct wimlib_decompressor *dec)
142 {
143         if (dec) {
144                 if (dec->ops->free_decompressor)
145                         dec->ops->free_decompressor(dec->private);
146                 FREE(dec);
147         }
148 }