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;
61 ret = wimlib_global_init(0);
65 if (!decompressor_ctype_valid(ctype))
66 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
69 return WIMLIB_ERR_INVALID_PARAM;
71 if (max_block_size == 0)
72 return WIMLIB_ERR_INVALID_PARAM;
74 dec = MALLOC(sizeof(*dec));
76 return WIMLIB_ERR_NOMEM;
77 dec->ops = decompressor_ops[ctype];
78 dec->max_block_size = max_block_size;
80 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);