aboutsummaryrefslogtreecommitdiff
path: root/lib/sqfs/comp/lzo.c
blob: 09ef75cf09628682cc58523d0b912a894e24ced0 (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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * lzo.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "config.h"

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

#include <lzo/lzo1x.h>

#include "internal.h"


typedef enum {
	LZO_ALGORITHM_LZO1X_1 = 0,
	LZO_ALGORITHM_LZO1X_1_11 = 1,
	LZO_ALGORITHM_LZO1X_1_12 = 2,
	LZO_ALGORITHM_LZO1X_1_15 = 3,
	LZO_ALGORITHM_LZO1X_999	= 4,
} LZO_ALGORITHM;

#define LZO_DEFAULT_ALG LZO_ALGORITHM_LZO1X_999
#define LZO_DEFAULT_LEVEL 8
#define LZO_NUM_ALGS (sizeof(lzo_algs) / sizeof(lzo_algs[0]))

typedef int (*lzo_cb_t)(const lzo_bytep src, lzo_uint src_len, lzo_bytep dst,
			lzo_uintp dst_len, lzo_voidp wrkmem);

static const struct {
	const char *name;
	lzo_cb_t compress;
	size_t bufsize;
} lzo_algs[] = {
	[LZO_ALGORITHM_LZO1X_1] = {
		.name = "lzo1x_1",
		.compress = lzo1x_1_compress,
		.bufsize = LZO1X_1_MEM_COMPRESS,
	},
	[LZO_ALGORITHM_LZO1X_1_11] = {
		.name = "lzo1x_1_11",
		.compress = lzo1x_1_11_compress,
		.bufsize = LZO1X_1_11_MEM_COMPRESS,
	},
	[LZO_ALGORITHM_LZO1X_1_12] = {
		.name = "lzo1x_1_12",
		.compress = lzo1x_1_12_compress,
		.bufsize = LZO1X_1_12_MEM_COMPRESS,
	},
	[LZO_ALGORITHM_LZO1X_1_15] = {
		.name = "lzo1x_1_15",
		.compress = lzo1x_1_15_compress,
		.bufsize = LZO1X_1_15_MEM_COMPRESS,
	},
	[LZO_ALGORITHM_LZO1X_999] = {
		.name = "lzo1x_999",
		.compress = lzo1x_999_compress,
		.bufsize = LZO1X_999_MEM_COMPRESS,
	},
};

typedef struct {
	compressor_t base;
	int algorithm;
	int level;

	uint8_t buffer[];
} lzo_compressor_t;

typedef struct {
	uint32_t algorithm;
	uint32_t level;
} lzo_options_t;

static int lzo_write_options(compressor_t *base, int fd)
{
	lzo_compressor_t *lzo = (lzo_compressor_t *)base;
	lzo_options_t opt;

	if (lzo->algorithm == LZO_DEFAULT_ALG &&
	    lzo->level == LZO_DEFAULT_LEVEL) {
		return 0;
	}

	opt.algorithm = htole32(lzo->algorithm);

	if (lzo->algorithm == LZO_ALGORITHM_LZO1X_999) {
		opt.level = htole32(lzo->level);
	} else {
		opt.level = 0;
	}

	return generic_write_options(fd, &opt, sizeof(opt));
}

static int lzo_read_options(compressor_t *base, int fd)
{
	lzo_compressor_t *lzo = (lzo_compressor_t *)base;
	lzo_options_t opt;

	if (generic_read_options(fd, &opt, sizeof(opt)))
		return -1;

	lzo->algorithm = le32toh(opt.algorithm);
	lzo->level = le32toh(opt.level);

	switch(lzo->algorithm) {
	case LZO_ALGORITHM_LZO1X_1:
	case LZO_ALGORITHM_LZO1X_1_11:
	case LZO_ALGORITHM_LZO1X_1_12:
	case LZO_ALGORITHM_LZO1X_1_15:
		if (lzo->level != 0)
			goto fail_level;
		break;
	case LZO_ALGORITHM_LZO1X_999:
		if (lzo->level < 1 || lzo->level > 9)
			goto fail_level;
		break;
	default:
		fputs("Unsupported LZO variant specified.\n", stderr);
		return -1;
	}

	return 0;
fail_level:
	fputs("Unsupported LZO compression level specified.\n", stderr);
	return -1;
}

static ssize_t lzo_comp_block(compressor_t *base, const uint8_t *in,
			      size_t size, uint8_t *out, size_t outsize)
{
	lzo_compressor_t *lzo = (lzo_compressor_t *)base;
	lzo_uint len = outsize;
	int ret;

	if (lzo->algorithm == LZO_ALGORITHM_LZO1X_999 &&
	    lzo->level != LZO_DEFAULT_LEVEL) {
		ret = lzo1x_999_compress_level(in, size, out, &len,
					       lzo->buffer, NULL, 0, 0,
					       lzo->level);
	} else {
		ret = lzo_algs[lzo->algorithm].compress(in, size, out,
							&len, lzo->buffer);
	}

	if (ret != LZO_E_OK) {
		fputs("LZO compression failed.\n", stderr);
		return -1;
	}

	if (len < size)
		return len;

	return 0;
}

static ssize_t lzo_uncomp_block(compressor_t *base, const uint8_t *in,
				size_t size, uint8_t *out, size_t outsize)
{
	lzo_compressor_t *lzo = (lzo_compressor_t *)base;
	lzo_uint len = outsize;
	int ret;

	ret = lzo1x_decompress_safe(in, size, out, &len, lzo->buffer);

	if (ret != LZO_E_OK) {
		fputs("lzo decompress: input data is corrupted\n", stderr);
		return -1;
	}

	return len;
}

static compressor_t *lzo_create_copy(compressor_t *cmp)
{
	lzo_compressor_t *other = (lzo_compressor_t *)cmp;
	lzo_compressor_t *lzo;

	lzo = alloc_flex(sizeof(*lzo), 1, lzo_algs[other->algorithm].bufsize);

	if (lzo == NULL) {
		perror("creating additional lzo compressor");
		return NULL;
	}

	memcpy(lzo, other, sizeof(*lzo));
	return (compressor_t *)lzo;
}

static void lzo_destroy(compressor_t *base)
{
	free(base);
}

static int process_options(char *options, int *algorithm, int *level)
{
	enum {
		OPT_ALG = 0,
		OPT_LEVEL,
	};
	char *const token[] = {
		[OPT_ALG] = (char *)"algorithm",
		[OPT_LEVEL] = (char *)"level",
		NULL
	};
	char *subopts, *value;
	size_t i;
	int opt;

	subopts = options;

	while (*subopts != '\0') {
		opt = getsubopt(&subopts, token, &value);

		switch (opt) {
		case OPT_ALG:
			if (value == NULL)
				goto fail_value;

			for (i = 0; i < LZO_NUM_ALGS; ++i) {
				if (strcmp(lzo_algs[i].name, value) == 0) {
					*algorithm = i;
					break;
				}
			}

			if (i == LZO_NUM_ALGS) {
				fprintf(stderr, "Unknown lzo variant '%s'.\n",
					value);
				return -1;
			}
			break;
		case OPT_LEVEL:
			if (value == NULL)
				goto fail_value;

			for (i = 0; isdigit(value[i]); ++i)
				;

			if (i < 1 || i > 3 || value[i] != '\0')
				goto fail_level;

			*level = atoi(value);

			if (*level < 1 || *level > 9)
				goto fail_level;
			break;
		default:
			goto fail_opt;
		}
	}

	return 0;
fail_level:
	fputs("Compression level must be a number between 1 and 9.\n", stderr);
	return -1;
fail_opt:
	fprintf(stderr, "Unknown option '%s'.\n", value);
	return -1;
fail_value:
	fprintf(stderr, "Missing value for '%s'.\n", token[opt]);
	return -1;
}

compressor_t *create_lzo_compressor(bool compress, size_t block_size,
				    char *options)
{
	lzo_compressor_t *lzo;
	compressor_t *base;
	int level, alg;
	(void)block_size;

	alg = LZO_DEFAULT_ALG;
	level = LZO_DEFAULT_LEVEL;

	if (options != NULL && process_options(options, &alg, &level) != 0)
		return NULL;

	lzo = alloc_flex(sizeof(*lzo), 1, lzo_algs[alg].bufsize);
	base = (compressor_t *)lzo;

	if (lzo == NULL) {
		perror("creating lzo compressor");
		return NULL;
	}

	lzo->algorithm = alg;
	lzo->level = level;

	base->destroy = lzo_destroy;
	base->do_block = compress ? lzo_comp_block : lzo_uncomp_block;
	base->write_options = lzo_write_options;
	base->read_options = lzo_read_options;
	base->create_copy = lzo_create_copy;
	return base;
}

void compressor_lzo_print_help(void)
{
	size_t i;

	fputs("Available options for lzo compressor:\n"
	      "\n"
	      "    algorithm=<name>  Specify the variant of lzo to use.\n"
	      "                      Defaults to 'lzo1x_999'.\n"
	      "    level=<value>     For lzo1x_999, the compression level.\n"
	      "                      Value from 1 to 9. Defaults to 8.\n"
	      "                      Ignored if algorithm is not lzo1x_999.\n"
	      "\n"
	      "Available algorithms:\n",
	      stdout);

	for (i = 0; i < LZO_NUM_ALGS; ++i)
		printf("\t%s\n", lzo_algs[i].name);
}