4 * Generic functions for decompression, wrapping around actual decompression
9 * Copyright (C) 2013, 2014 Eric Biggers
11 * This file is part of wimlib, a library for working with WIM files.
13 * wimlib is free software; you can redistribute it and/or modify it under the
14 * terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 3 of the License, or (at your option)
18 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
19 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20 * A PARTICULAR PURPOSE. See the GNU General Public License for more
23 * You should have received a copy of the GNU General Public License
24 * along with wimlib; if not, see http://www.gnu.org/licenses/.
32 #include "wimlib/decompressor_ops.h"
33 #include "wimlib/util.h"
35 struct wimlib_decompressor {
36 const struct decompressor_ops *ops;
37 size_t max_block_size;
41 static const struct decompressor_ops *decompressor_ops[] = {
42 [WIMLIB_COMPRESSION_TYPE_XPRESS] = &xpress_decompressor_ops,
43 [WIMLIB_COMPRESSION_TYPE_LZX] = &lzx_decompressor_ops,
44 [WIMLIB_COMPRESSION_TYPE_LZMS] = &lzms_decompressor_ops,
48 decompressor_ctype_valid(int ctype)
51 ctype < ARRAY_LEN(decompressor_ops) &&
52 decompressor_ops[ctype] != NULL);
56 wimlib_create_decompressor(enum wimlib_compression_type ctype,
57 size_t max_block_size,
58 struct wimlib_decompressor **dec_ret)
60 struct wimlib_decompressor *dec;
62 if (!decompressor_ctype_valid(ctype))
63 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
66 return WIMLIB_ERR_INVALID_PARAM;
68 if (max_block_size == 0)
69 return WIMLIB_ERR_INVALID_PARAM;
71 dec = MALLOC(sizeof(*dec));
73 return WIMLIB_ERR_NOMEM;
74 dec->ops = decompressor_ops[ctype];
75 dec->max_block_size = max_block_size;
77 if (dec->ops->create_decompressor) {
80 ret = dec->ops->create_decompressor(max_block_size,
92 wimlib_decompress(const void *compressed_data, size_t compressed_size,
93 void *uncompressed_data, size_t uncompressed_size,
94 struct wimlib_decompressor *dec)
96 if (unlikely(uncompressed_size > dec->max_block_size))
99 return dec->ops->decompress(compressed_data, compressed_size,
100 uncompressed_data, uncompressed_size,
105 wimlib_free_decompressor(struct wimlib_decompressor *dec)
108 if (dec->ops->free_decompressor)
109 dec->ops->free_decompressor(dec->private);