]> wimlib.net Git - wimlib/blob - src/compress.c
inode_fixup.c: Don't check ADS count when all have zero hashes
[wimlib] / src / compress.c
1 /*
2  * compress.c
3  *
4  * Generic functions for compression, wrapping around actual compression
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/compressor_ops.h"
33 #include "wimlib/util.h"
34
35 struct wimlib_compressor {
36         const struct compressor_ops *ops;
37         void *private;
38 };
39
40 static const struct compressor_ops *compressor_ops[] = {
41         [WIMLIB_COMPRESSION_TYPE_LZX]    = &lzx_compressor_ops,
42         [WIMLIB_COMPRESSION_TYPE_XPRESS] = &xpress_compressor_ops,
43         [WIMLIB_COMPRESSION_TYPE_LZMS]   = &lzms_compressor_ops,
44 };
45
46 static struct wimlib_compressor_params_header *
47 compressor_default_params[ARRAY_LEN(compressor_ops)] = {
48 };
49
50 static bool
51 compressor_ctype_valid(int ctype)
52 {
53         return (ctype >= 0 &&
54                 ctype < ARRAY_LEN(compressor_ops) &&
55                 compressor_ops[ctype] != NULL);
56 }
57
58 WIMLIBAPI int
59 wimlib_set_default_compressor_params(enum wimlib_compression_type ctype,
60                                      const struct wimlib_compressor_params_header *params)
61 {
62         struct wimlib_compressor_params_header *dup;
63
64         if (!compressor_ctype_valid(ctype))
65                 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
66
67         if (params != NULL &&
68             compressor_ops[ctype]->params_valid != NULL &&
69             !compressor_ops[ctype]->params_valid(params))
70                 return WIMLIB_ERR_INVALID_PARAM;
71
72         dup = NULL;
73         if (params) {
74                 dup = memdup(params, params->size);
75                 if (dup == NULL)
76                         return WIMLIB_ERR_NOMEM;
77         }
78
79         FREE(compressor_default_params[ctype]);
80         compressor_default_params[ctype] = dup;
81         return 0;
82 }
83
84 void
85 cleanup_compressor_params(void)
86 {
87         for (size_t i = 0; i < ARRAY_LEN(compressor_default_params); i++) {
88                 FREE(compressor_default_params[i]);
89                 compressor_default_params[i] = NULL;
90         }
91 }
92
93 WIMLIBAPI u64
94 wimlib_get_compressor_needed_memory(enum wimlib_compression_type ctype,
95                                     size_t max_block_size,
96                                     const struct wimlib_compressor_params_header *extra_params)
97 {
98         const struct compressor_ops *ops;
99         const struct wimlib_compressor_params_header *params;
100
101         if (!compressor_ctype_valid(ctype))
102                 return 0;
103
104         ops = compressor_ops[ctype];
105         if (ops->get_needed_memory == NULL)
106                 return 0;
107
108         if (extra_params) {
109                 params = extra_params;
110                 if (ops->params_valid && !ops->params_valid(params))
111                         return 0;
112         } else {
113                 params = compressor_default_params[ctype];
114         }
115
116         return ops->get_needed_memory(max_block_size, params);
117 }
118
119
120 WIMLIBAPI int
121 wimlib_create_compressor(enum wimlib_compression_type ctype,
122                          size_t max_block_size,
123                          const struct wimlib_compressor_params_header *extra_params,
124                          struct wimlib_compressor **c_ret)
125 {
126         struct wimlib_compressor *c;
127
128         if (c_ret == NULL)
129                 return WIMLIB_ERR_INVALID_PARAM;
130
131         if (!compressor_ctype_valid(ctype))
132                 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
133
134         c = MALLOC(sizeof(*c));
135         if (c == NULL)
136                 return WIMLIB_ERR_NOMEM;
137         c->ops = compressor_ops[ctype];
138         c->private = NULL;
139         if (c->ops->create_compressor) {
140                 const struct wimlib_compressor_params_header *params;
141                 int ret;
142
143                 if (extra_params) {
144                         params = extra_params;
145                         if (c->ops->params_valid && !c->ops->params_valid(params)) {
146                                 FREE(c);
147                                 return WIMLIB_ERR_INVALID_PARAM;
148                         }
149                 } else {
150                         params = compressor_default_params[ctype];
151                 }
152                 ret = c->ops->create_compressor(max_block_size,
153                                                 params, &c->private);
154                 if (ret) {
155                         FREE(c);
156                         return ret;
157                 }
158         }
159         *c_ret = c;
160         return 0;
161 }
162
163 WIMLIBAPI size_t
164 wimlib_compress(const void *uncompressed_data, size_t uncompressed_size,
165                 void *compressed_data, size_t compressed_size_avail,
166                 struct wimlib_compressor *c)
167 {
168         return c->ops->compress(uncompressed_data, uncompressed_size,
169                                 compressed_data, compressed_size_avail,
170                                 c->private);
171 }
172
173 WIMLIBAPI void
174 wimlib_free_compressor(struct wimlib_compressor *c)
175 {
176         if (c) {
177                 if (c->ops->free_compressor)
178                         c->ops->free_compressor(c->private);
179                 FREE(c);
180         }
181 }