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,
46 decompressor_ctype_valid(int ctype)
49 ctype < ARRAY_LEN(decompressor_ops) &&
50 decompressor_ops[ctype] != NULL);
54 wimlib_create_decompressor(enum wimlib_compression_type ctype,
55 size_t max_block_size,
56 struct wimlib_decompressor **dec_ret)
58 struct wimlib_decompressor *dec;
60 if (!decompressor_ctype_valid(ctype))
61 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
64 return WIMLIB_ERR_INVALID_PARAM;
66 if (max_block_size == 0)
67 return WIMLIB_ERR_INVALID_PARAM;
69 dec = MALLOC(sizeof(*dec));
71 return WIMLIB_ERR_NOMEM;
72 dec->ops = decompressor_ops[ctype];
73 dec->max_block_size = max_block_size;
75 if (dec->ops->create_decompressor) {
78 ret = dec->ops->create_decompressor(max_block_size,
90 wimlib_decompress(const void *compressed_data, size_t compressed_size,
91 void *uncompressed_data, size_t uncompressed_size,
92 struct wimlib_decompressor *dec)
94 if (unlikely(uncompressed_size > dec->max_block_size))
97 return dec->ops->decompress(compressed_data, compressed_size,
98 uncompressed_data, uncompressed_size,
103 wimlib_free_decompressor(struct wimlib_decompressor *dec)
106 if (dec->ops->free_decompressor)
107 dec->ops->free_decompressor(dec->private);