aboutsummaryrefslogtreecommitdiff
path: root/lib/sqfs/comp/gzip.c
blob: 351d733e253d4a1ac706e28f14733899266ef54b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/*
 * gzip.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#define SQFS_BUILDING_DLL
#include "config.h"

#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <zlib.h>

#include "internal.h"

typedef struct {
	sqfs_u32 level;
	sqfs_u16 window;
	sqfs_u16 strategies;
} gzip_options_t;

typedef struct {
	sqfs_compressor_t base;

	z_stream strm;
	bool compress;

	size_t block_size;
	gzip_options_t opt;
} gzip_compressor_t;

static void gzip_destroy(sqfs_object_t *base)
{
	gzip_compressor_t *gzip = (gzip_compressor_t *)base;

	if (gzip->compress) {
		deflateEnd(&gzip->strm);
	} else {
		inflateEnd(&gzip->strm);
	}

	free(gzip);
}

static void gzip_get_configuration(const sqfs_compressor_t *base,
				   sqfs_compressor_config_t *cfg)
{
	const gzip_compressor_t *gzip = (const gzip_compressor_t *)base;

	memset(cfg, 0, sizeof(*cfg));
	cfg->id = SQFS_COMP_GZIP;
	cfg->flags = gzip->opt.strategies;
	cfg->block_size = gzip->block_size;
	cfg->opt.gzip.level = gzip->opt.level;
	cfg->opt.gzip.window_size = gzip->opt.window;

	if (!gzip->compress)
		cfg->flags |= SQFS_COMP_FLAG_UNCOMPRESS;
}

static int gzip_write_options(sqfs_compressor_t *base, sqfs_file_t *file)
{
	gzip_compressor_t *gzip = (gzip_compressor_t *)base;
	gzip_options_t opt;

	if (gzip->opt.level == SQFS_GZIP_DEFAULT_LEVEL &&
	    gzip->opt.window == SQFS_GZIP_DEFAULT_WINDOW &&
	    gzip->opt.strategies == 0) {
		return 0;
	}

	opt.level = htole32(gzip->opt.level);
	opt.window = htole16(gzip->opt.window);
	opt.strategies = htole16(gzip->opt.strategies);

	return sqfs_generic_write_options(file, &opt, sizeof(opt));
}

static int gzip_read_options(sqfs_compressor_t *base, sqfs_file_t *file)
{
	gzip_compressor_t *gzip = (gzip_compressor_t *)base;
	gzip_options_t opt;
	int ret;

	ret = sqfs_generic_read_options(file, &opt, sizeof(opt));
	if (ret)
		return ret;

	gzip->opt.level = le32toh(opt.level);
	gzip->opt.window = le16toh(opt.window);
	gzip->opt.strategies = le16toh(opt.strategies);

	if (gzip->opt.level < 1 || gzip->opt.level > 9)
		return SQFS_ERROR_UNSUPPORTED;

	if (gzip->opt.window < 8 || gzip->opt.window > 15)
		return SQFS_ERROR_UNSUPPORTED;

	if (gzip->opt.strategies & ~SQFS_COMP_FLAG_GZIP_ALL)
		return SQFS_ERROR_UNSUPPORTED;

	return 0;
}

static int flag_to_zlib_strategy(int flag)
{
	switch (flag) {
	case SQFS_COMP_FLAG_GZIP_DEFAULT:
		return Z_DEFAULT_STRATEGY;
	case SQFS_COMP_FLAG_GZIP_FILTERED:
		return Z_FILTERED;
	case SQFS_COMP_FLAG_GZIP_HUFFMAN:
		return Z_HUFFMAN_ONLY;
	case SQFS_COMP_FLAG_GZIP_RLE:
		return Z_RLE;
	case SQFS_COMP_FLAG_GZIP_FIXED:
		return Z_FIXED;
	}

	return 0;
}

static int find_strategy(gzip_compressor_t *gzip, const sqfs_u8 *in,
			 sqfs_u32 size, sqfs_u8 *out, sqfs_u32 outsize)
{
	int ret, strategy, selected = Z_DEFAULT_STRATEGY;
	size_t i, length, minlength = 0;

	for (i = 0x01; i & SQFS_COMP_FLAG_GZIP_ALL; i <<= 1) {
		if ((gzip->opt.strategies & i) == 0)
			continue;

		ret = deflateReset(&gzip->strm);
		if (ret != Z_OK)
			return SQFS_ERROR_COMPRESSOR;

		strategy = flag_to_zlib_strategy(i);

		gzip->strm.next_in = (void *)in;
		gzip->strm.avail_in = size;
		gzip->strm.next_out = out;
		gzip->strm.avail_out = outsize;

		ret = deflateParams(&gzip->strm, gzip->opt.level, strategy);
		if (ret != Z_OK)
			return SQFS_ERROR_COMPRESSOR;

		ret = deflate(&gzip->strm, Z_FINISH);

		if (ret == Z_STREAM_END) {
			length = gzip->strm.total_out;

			if (minlength == 0 || length < minlength) {
				minlength = length;
				selected = strategy;
			}
		} else if (ret != Z_OK && ret != Z_BUF_ERROR) {
			return SQFS_ERROR_COMPRESSOR;
		}
	}

	return selected;
}

static sqfs_s32 gzip_do_block(sqfs_compressor_t *base, const sqfs_u8 *in,
			      sqfs_u32 size, sqfs_u8 *out, sqfs_u32 outsize)
{
	gzip_compressor_t *gzip = (gzip_compressor_t *)base;
	int ret, strategy = 0;
	size_t written;

	if (size >= 0x7FFFFFFF)
		return SQFS_ERROR_ARG_INVALID;

	if (gzip->compress && gzip->opt.strategies != 0) {
		strategy = find_strategy(gzip, in, size, out, outsize);
		if (strategy < 0)
			return strategy;
	}

	if (gzip->compress) {
		ret = deflateReset(&gzip->strm);
	} else {
		ret = inflateReset(&gzip->strm);
	}

	if (ret != Z_OK)
		return SQFS_ERROR_COMPRESSOR;

	gzip->strm.next_in = (void *)in;
	gzip->strm.avail_in = size;
	gzip->strm.next_out = out;
	gzip->strm.avail_out = outsize;

	if (gzip->compress && gzip->opt.strategies != 0) {
		ret = deflateParams(&gzip->strm, gzip->opt.level, strategy);
		if (ret != Z_OK)
			return SQFS_ERROR_COMPRESSOR;
	}

	if (gzip->compress) {
		ret = deflate(&gzip->strm, Z_FINISH);
	} else {
		ret = inflate(&gzip->strm, Z_FINISH);
	}

	if (ret == Z_STREAM_END) {
		written = gzip->strm.total_out;

		if (gzip->compress && written >= size)
			return 0;

		return written;
	}

	if (ret != Z_OK && ret != Z_BUF_ERROR)
		return SQFS_ERROR_COMPRESSOR;

	return 0;
}

static sqfs_object_t *gzip_create_copy(const sqfs_object_t *cmp)
{
	gzip_compressor_t *gzip = malloc(sizeof(*gzip));
	int ret;

	if (gzip == NULL)
		return NULL;

	memcpy(gzip, cmp, sizeof(*gzip));
	memset(&gzip->strm, 0, sizeof(gzip->strm));

	if (gzip->compress) {
		ret = deflateInit2(&gzip->strm, gzip->opt.level, Z_DEFLATED,
				   gzip->opt.window, 8, Z_DEFAULT_STRATEGY);
	} else {
		ret = inflateInit(&gzip->strm);
	}

	if (ret != Z_OK) {
		free(gzip);
		return NULL;
	}

	return (sqfs_object_t *)gzip;
}

sqfs_compressor_t *gzip_compressor_create(const sqfs_compressor_config_t *cfg)
{
	gzip_compressor_t *gzip;
	sqfs_compressor_t *base;
	int ret;

	if (cfg->flags & ~(SQFS_COMP_FLAG_GZIP_ALL |
			   SQFS_COMP_FLAG_GENERIC_ALL)) {
		return NULL;
	}

	if (cfg->opt.gzip.level < SQFS_GZIP_MIN_LEVEL ||
	    cfg->opt.gzip.level > SQFS_GZIP_MAX_LEVEL) {
		return NULL;
	}

	if (cfg->opt.gzip.window_size < SQFS_GZIP_MIN_WINDOW ||
	    cfg->opt.gzip.window_size > SQFS_GZIP_MAX_WINDOW) {
		return NULL;
	}

	gzip = calloc(1, sizeof(*gzip));
	base = (sqfs_compressor_t *)gzip;

	if (gzip == NULL)
		return NULL;

	gzip->opt.level = cfg->opt.gzip.level;
	gzip->opt.window = cfg->opt.gzip.window_size;
	gzip->opt.strategies = cfg->flags & SQFS_COMP_FLAG_GZIP_ALL;
	gzip->compress = (cfg->flags & SQFS_COMP_FLAG_UNCOMPRESS) == 0;
	gzip->block_size = cfg->block_size;
	base->get_configuration = gzip_get_configuration;
	base->do_block = gzip_do_block;
	base->write_options = gzip_write_options;
	base->read_options = gzip_read_options;
	((sqfs_object_t *)base)->copy = gzip_create_copy;
	((sqfs_object_t *)base)->destroy = gzip_destroy;

	if (gzip->compress) {
		ret = deflateInit2(&gzip->strm, cfg->opt.gzip.level,
				   Z_DEFLATED, cfg->opt.gzip.window_size, 8,
				   Z_DEFAULT_STRATEGY);
	} else {
		ret = inflateInit(&gzip->strm);
	}

	if (ret != Z_OK) {
		free(gzip);
		return NULL;
	}

	return base;
}