4 * Generic functions for decompression, wrapping around actual decompression
9 * Copyright (C) 2013, 2014 Eric Biggers
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
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
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/.
30 #include "wimlib/decompressor_ops.h"
31 #include "wimlib/util.h"
33 struct wimlib_decompressor {
34 const struct decompressor_ops *ops;
35 size_t max_block_size;
39 static const struct decompressor_ops * const decompressor_ops[] = {
40 [WIMLIB_COMPRESSION_TYPE_XPRESS] = &xpress_decompressor_ops,
41 [WIMLIB_COMPRESSION_TYPE_LZX] = &lzx_decompressor_ops,
42 [WIMLIB_COMPRESSION_TYPE_LZMS] = &lzms_decompressor_ops,
44 [WIMLIB_COMPRESSION_TYPE_ZSTD] = &zstd_decompressor_ops,
49 decompressor_ctype_valid(int ctype)
52 ctype < ARRAY_LEN(decompressor_ops) &&
53 decompressor_ops[ctype] != NULL);
57 wimlib_create_decompressor(enum wimlib_compression_type ctype,
58 size_t max_block_size,
59 struct wimlib_decompressor **dec_ret)
61 struct wimlib_decompressor *dec;
63 if (!decompressor_ctype_valid(ctype))
64 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
67 return WIMLIB_ERR_INVALID_PARAM;
69 if (max_block_size == 0)
70 return WIMLIB_ERR_INVALID_PARAM;
72 dec = MALLOC(sizeof(*dec));
74 return WIMLIB_ERR_NOMEM;
75 dec->ops = decompressor_ops[ctype];
76 dec->max_block_size = max_block_size;
78 if (dec->ops->create_decompressor) {
81 ret = dec->ops->create_decompressor(max_block_size,
93 wimlib_decompress(const void *compressed_data, size_t compressed_size,
94 void *uncompressed_data, size_t uncompressed_size,
95 struct wimlib_decompressor *dec)
97 if (unlikely(uncompressed_size > dec->max_block_size))
100 return dec->ops->decompress(compressed_data, compressed_size,
101 uncompressed_data, uncompressed_size,
106 wimlib_free_decompressor(struct wimlib_decompressor *dec)
109 if (dec->ops->free_decompressor)
110 dec->ops->free_decompressor(dec->private);