]> wimlib.net Git - wimlib/blob - src/decompress.c
wimlib_decompress(): Check uncompressed_size
[wimlib] / src / decompress.c
1 /*
2  * decompress.c
3  *
4  * Generic functions for decompression, wrapping around actual decompression
5  * implementations.
6  */
7
8 /*
9  * Copyright (C) 2013, 2014 Eric Biggers
10  *
11  * This file is part of wimlib, a library for working with WIM files.
12  *
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)
16  * any later version.
17  *
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
21  * details.
22  *
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/.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #  include "config.h"
29 #endif
30
31 #include "wimlib.h"
32 #include "wimlib/decompressor_ops.h"
33 #include "wimlib/util.h"
34
35 struct wimlib_decompressor {
36         const struct decompressor_ops *ops;
37         size_t max_block_size;
38         void *private;
39 };
40
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,
45 };
46
47 static bool
48 decompressor_ctype_valid(int ctype)
49 {
50         return (ctype >= 0 &&
51                 ctype < ARRAY_LEN(decompressor_ops) &&
52                 decompressor_ops[ctype] != NULL);
53 }
54
55 WIMLIBAPI int
56 wimlib_create_decompressor(enum wimlib_compression_type ctype,
57                            size_t max_block_size,
58                            struct wimlib_decompressor **dec_ret)
59 {
60         struct wimlib_decompressor *dec;
61
62         if (dec_ret == NULL)
63                 return WIMLIB_ERR_INVALID_PARAM;
64
65         if (!decompressor_ctype_valid(ctype))
66                 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
67
68         dec = MALLOC(sizeof(*dec));
69         if (dec == NULL)
70                 return WIMLIB_ERR_NOMEM;
71         dec->ops = decompressor_ops[ctype];
72         dec->max_block_size = max_block_size;
73         dec->private = NULL;
74         if (dec->ops->create_decompressor) {
75                 int ret;
76
77                 ret = dec->ops->create_decompressor(max_block_size,
78                                                     &dec->private);
79                 if (ret) {
80                         FREE(dec);
81                         return ret;
82                 }
83         }
84         *dec_ret = dec;
85         return 0;
86 }
87
88 WIMLIBAPI int
89 wimlib_decompress(const void *compressed_data, size_t compressed_size,
90                   void *uncompressed_data, size_t uncompressed_size,
91                   struct wimlib_decompressor *dec)
92 {
93         if (unlikely(uncompressed_size > dec->max_block_size))
94                 return -2;
95
96         return dec->ops->decompress(compressed_data, compressed_size,
97                                     uncompressed_data, uncompressed_size,
98                                     dec->private);
99 }
100
101 WIMLIBAPI void
102 wimlib_free_decompressor(struct wimlib_decompressor *dec)
103 {
104         if (dec) {
105                 if (dec->ops->free_decompressor)
106                         dec->ops->free_decompressor(dec->private);
107                 FREE(dec);
108         }
109 }