]> wimlib.net Git - wimlib/blob - src/decompress.c
unmount_timed_out_cb(): Do not check errno if kill() succeeds
[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 };
49
50 static bool
51 decompressor_ctype_valid(int ctype)
52 {
53         return (ctype >= 0 &&
54                 ctype < ARRAY_LEN(decompressor_ops) &&
55                 decompressor_ops[ctype] != NULL);
56 }
57
58 WIMLIBAPI int
59 wimlib_set_default_decompressor_params(enum wimlib_compression_type ctype,
60                                        const struct wimlib_decompressor_params_header *params)
61 {
62         struct wimlib_decompressor_params_header *dup;
63
64         if (!decompressor_ctype_valid(ctype))
65                 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
66
67         dup = NULL;
68         if (params) {
69                 dup = memdup(params, params->size);
70                 if (dup == NULL)
71                         return WIMLIB_ERR_NOMEM;
72         }
73
74         FREE(decompressor_default_params[ctype]);
75         decompressor_default_params[ctype] = dup;
76         return 0;
77 }
78
79 void
80 cleanup_decompressor_params(void)
81 {
82         for (size_t i = 0; i < ARRAY_LEN(decompressor_default_params); i++) {
83                 FREE(decompressor_default_params[i]);
84                 decompressor_default_params[i] = NULL;
85         }
86 }
87
88 WIMLIBAPI int
89 wimlib_create_decompressor(enum wimlib_compression_type ctype,
90                            size_t max_block_size,
91                            const struct wimlib_decompressor_params_header *extra_params,
92                            struct wimlib_decompressor **dec_ret)
93 {
94         struct wimlib_decompressor *dec;
95
96         if (dec_ret == NULL)
97                 return WIMLIB_ERR_INVALID_PARAM;
98
99         if (!decompressor_ctype_valid(ctype))
100                 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
101
102         dec = MALLOC(sizeof(*dec));
103         if (dec == NULL)
104                 return WIMLIB_ERR_NOMEM;
105         dec->ops = decompressor_ops[ctype];
106         dec->private = NULL;
107         if (dec->ops->create_decompressor) {
108                 const struct wimlib_decompressor_params_header *params;
109                 int ret;
110
111                 if (extra_params)
112                         params = extra_params;
113                 else
114                         params = decompressor_default_params[ctype];
115                 ret = dec->ops->create_decompressor(max_block_size,
116                                                     params,
117                                                     &dec->private);
118                 if (ret) {
119                         FREE(dec);
120                         return ret;
121                 }
122         }
123         *dec_ret = dec;
124         return 0;
125 }
126
127 WIMLIBAPI int
128 wimlib_decompress(const void *compressed_data, size_t compressed_size,
129                   void *uncompressed_data, size_t uncompressed_size,
130                   struct wimlib_decompressor *dec)
131 {
132         return dec->ops->decompress(compressed_data, compressed_size,
133                                     uncompressed_data, uncompressed_size,
134                                     dec->private);
135 }
136
137 WIMLIBAPI void
138 wimlib_free_decompressor(struct wimlib_decompressor *dec)
139 {
140         if (dec) {
141                 if (dec->ops->free_decompressor)
142                         dec->ops->free_decompressor(dec->private);
143                 FREE(dec);
144         }
145 }