Remove --enable-verify-compression option
[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, 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 http://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/error.h"
31 #include "wimlib/compressor_ops.h"
32 #include "wimlib/util.h"
33
34 struct wimlib_compressor {
35         const struct compressor_ops *ops;
36         void *private;
37         enum wimlib_compression_type ctype;
38         size_t max_block_size;
39 };
40
41 static const struct compressor_ops *compressor_ops[] = {
42         [WIMLIB_COMPRESSION_TYPE_XPRESS] = &xpress_compressor_ops,
43         [WIMLIB_COMPRESSION_TYPE_LZX]    = &lzx_compressor_ops,
44         [WIMLIB_COMPRESSION_TYPE_LZMS]   = &lzms_compressor_ops,
45 };
46
47 /* Scale: 10 = low, 50 = medium, 100 = high */
48
49 #define DEFAULT_COMPRESSION_LEVEL 50
50
51 static unsigned int default_compression_levels[ARRAY_LEN(compressor_ops)];
52
53 static bool
54 compressor_ctype_valid(int ctype)
55 {
56         return (ctype >= 0 &&
57                 ctype < ARRAY_LEN(compressor_ops) &&
58                 compressor_ops[ctype] != NULL);
59 }
60
61 WIMLIBAPI int
62 wimlib_set_default_compression_level(int ctype, unsigned int compression_level)
63 {
64         if (ctype == -1) {
65                 for (int i = 0; i < ARRAY_LEN(default_compression_levels); i++)
66                         default_compression_levels[i] = compression_level;
67         } else {
68                 if (!compressor_ctype_valid(ctype))
69                         return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
70
71                 default_compression_levels[ctype] = compression_level;
72         }
73         return 0;
74 }
75
76 WIMLIBAPI u64
77 wimlib_get_compressor_needed_memory(enum wimlib_compression_type ctype,
78                                     size_t max_block_size,
79                                     unsigned int compression_level)
80 {
81         bool destructive;
82         const struct compressor_ops *ops;
83         u64 size;
84
85         destructive = (compression_level & WIMLIB_COMPRESSOR_FLAG_DESTRUCTIVE);
86         compression_level &= ~WIMLIB_COMPRESSOR_FLAG_DESTRUCTIVE;
87
88         if (!compressor_ctype_valid(ctype))
89                 return 0;
90
91         if (compression_level > 0xFFFFFF)
92                 return 0;
93
94         if (max_block_size == 0)
95                 return 0;
96
97         ops = compressor_ops[ctype];
98
99         if (compression_level == 0)
100                 compression_level = default_compression_levels[ctype];
101         if (compression_level == 0)
102                 compression_level = DEFAULT_COMPRESSION_LEVEL;
103
104         if (ops->get_needed_memory) {
105                 size = ops->get_needed_memory(max_block_size, compression_level,
106                                               destructive);
107
108                 /* 0 is never valid and indicates an invalid max_block_size.  */
109                 if (size == 0)
110                         return 0;
111         } else {
112                 size = 0;
113         }
114         return size + sizeof(struct wimlib_compressor);
115 }
116
117 WIMLIBAPI int
118 wimlib_create_compressor(enum wimlib_compression_type ctype,
119                          size_t max_block_size,
120                          unsigned int compression_level,
121                          struct wimlib_compressor **c_ret)
122 {
123         bool destructive;
124         struct wimlib_compressor *c;
125
126         destructive = (compression_level & WIMLIB_COMPRESSOR_FLAG_DESTRUCTIVE);
127         compression_level &= ~WIMLIB_COMPRESSOR_FLAG_DESTRUCTIVE;
128
129         if (!compressor_ctype_valid(ctype))
130                 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
131
132         if (compression_level > 0xFFFFFF)
133                 return WIMLIB_ERR_INVALID_PARAM;
134
135         if (c_ret == NULL)
136                 return WIMLIB_ERR_INVALID_PARAM;
137
138         if (max_block_size == 0)
139                 return WIMLIB_ERR_INVALID_PARAM;
140
141         c = MALLOC(sizeof(*c));
142         if (c == NULL)
143                 return WIMLIB_ERR_NOMEM;
144         c->ops = compressor_ops[ctype];
145         c->private = NULL;
146         c->ctype = ctype;
147         c->max_block_size = max_block_size;
148         if (c->ops->create_compressor) {
149                 int ret;
150
151                 if (compression_level == 0)
152                         compression_level = default_compression_levels[ctype];
153                 if (compression_level == 0)
154                         compression_level = DEFAULT_COMPRESSION_LEVEL;
155
156                 ret = c->ops->create_compressor(max_block_size,
157                                                 compression_level,
158                                                 destructive,
159                                                 &c->private);
160                 if (ret) {
161                         FREE(c);
162                         return ret;
163                 }
164         }
165         *c_ret = c;
166         return 0;
167 }
168
169 WIMLIBAPI size_t
170 wimlib_compress(const void *uncompressed_data, size_t uncompressed_size,
171                 void *compressed_data, size_t compressed_size_avail,
172                 struct wimlib_compressor *c)
173 {
174         if (unlikely(uncompressed_size == 0 || uncompressed_size > c->max_block_size))
175                 return 0;
176
177         return c->ops->compress(uncompressed_data, uncompressed_size,
178                                 compressed_data, compressed_size_avail,
179                                 c->private);
180 }
181
182 WIMLIBAPI void
183 wimlib_free_compressor(struct wimlib_compressor *c)
184 {
185         if (c) {
186                 if (c->ops->free_compressor)
187                         c->ops->free_compressor(c->private);
188                 FREE(c);
189         }
190 }