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