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
322
323
324
|
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/*
* xz.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 <lzma.h>
#include "internal.h"
typedef struct {
sqfs_compressor_t base;
size_t block_size;
size_t dict_size;
sqfs_u8 level;
sqfs_u8 lc;
sqfs_u8 lp;
sqfs_u8 pb;
int flags;
} xz_compressor_t;
typedef struct {
sqfs_u32 dict_size;
sqfs_u32 flags;
} xz_options_t;
static bool is_dict_size_valid(size_t size)
{
size_t x = size & (size - 1);
if (x == 0)
return true;
return size == (x | (x >> 1));
}
static int xz_write_options(sqfs_compressor_t *base, sqfs_file_t *file)
{
xz_compressor_t *xz = (xz_compressor_t *)base;
xz_options_t opt;
sqfs_u32 flags;
if (xz->flags == 0 && xz->dict_size == xz->block_size)
return 0;
flags = xz->flags & SQFS_COMP_FLAG_XZ_ALL;
flags &= ~SQFS_COMP_FLAG_XZ_EXTREME;
opt.dict_size = htole32(xz->dict_size);
opt.flags = htole32(flags);
return sqfs_generic_write_options(file, &opt, sizeof(opt));
}
static int xz_read_options(sqfs_compressor_t *base, sqfs_file_t *file)
{
xz_compressor_t *xz = (xz_compressor_t *)base;
xz_options_t opt;
int ret;
ret = sqfs_generic_read_options(file, &opt, sizeof(opt));
if (ret)
return ret;
opt.dict_size = le32toh(opt.dict_size);
opt.flags = le32toh(opt.flags);
if (!is_dict_size_valid(opt.dict_size))
return SQFS_ERROR_CORRUPTED;
if (opt.flags & ~SQFS_COMP_FLAG_XZ_ALL)
return SQFS_ERROR_UNSUPPORTED;
xz->flags = opt.flags;
xz->dict_size = opt.dict_size;
return 0;
}
static sqfs_s32 compress(xz_compressor_t *xz, lzma_vli filter,
const sqfs_u8 *in, sqfs_u32 size,
sqfs_u8 *out, sqfs_u32 outsize,
sqfs_u32 presets)
{
lzma_filter filters[5];
lzma_options_lzma opt;
size_t written = 0;
lzma_ret ret;
int i = 0;
if (lzma_lzma_preset(&opt, presets))
return SQFS_ERROR_COMPRESSOR;
opt.lc = xz->lc;
opt.lp = xz->lp;
opt.pb = xz->pb;
opt.dict_size = xz->dict_size;
if (filter != LZMA_VLI_UNKNOWN) {
filters[i].id = filter;
filters[i].options = NULL;
++i;
}
filters[i].id = LZMA_FILTER_LZMA2;
filters[i].options = &opt;
++i;
filters[i].id = LZMA_VLI_UNKNOWN;
filters[i].options = NULL;
++i;
ret = lzma_stream_buffer_encode(filters, LZMA_CHECK_CRC32, NULL,
in, size, out, &written, outsize);
if (ret == LZMA_OK)
return (written >= size) ? 0 : written;
if (ret != LZMA_BUF_ERROR)
return SQFS_ERROR_COMPRESSOR;
return 0;
}
static lzma_vli flag_to_vli(int flag)
{
switch (flag) {
case SQFS_COMP_FLAG_XZ_X86:
return LZMA_FILTER_X86;
case SQFS_COMP_FLAG_XZ_POWERPC:
return LZMA_FILTER_POWERPC;
case SQFS_COMP_FLAG_XZ_IA64:
return LZMA_FILTER_IA64;
case SQFS_COMP_FLAG_XZ_ARM:
return LZMA_FILTER_ARM;
case SQFS_COMP_FLAG_XZ_ARMTHUMB:
return LZMA_FILTER_ARMTHUMB;
case SQFS_COMP_FLAG_XZ_SPARC:
return LZMA_FILTER_SPARC;
default:
break;
}
return LZMA_VLI_UNKNOWN;
}
static sqfs_s32 xz_comp_block(sqfs_compressor_t *base, const sqfs_u8 *in,
sqfs_u32 size, sqfs_u8 *out, sqfs_u32 outsize)
{
xz_compressor_t *xz = (xz_compressor_t *)base;
lzma_vli filter, selected = LZMA_VLI_UNKNOWN;
sqfs_s32 ret, smallest;
bool extreme;
size_t i;
if (size >= 0x7FFFFFFF)
return SQFS_ERROR_ARG_INVALID;
ret = compress(xz, LZMA_VLI_UNKNOWN, in, size, out,
outsize, xz->level);
if (ret < 0 || xz->flags == 0)
return ret;
smallest = ret;
extreme = false;
if (xz->flags & SQFS_COMP_FLAG_XZ_EXTREME) {
ret = compress(xz, LZMA_VLI_UNKNOWN, in, size, out, outsize,
xz->level | LZMA_PRESET_EXTREME);
if (ret > 0 && (smallest == 0 || ret < smallest)) {
smallest = ret;
extreme = true;
}
}
for (i = 1; i & SQFS_COMP_FLAG_XZ_ALL; i <<= 1) {
if ((i & SQFS_COMP_FLAG_XZ_EXTREME) || (xz->flags & i) == 0)
continue;
filter = flag_to_vli(i);
ret = compress(xz, filter, in, size, out, outsize, xz->level);
if (ret > 0 && (smallest == 0 || ret < smallest)) {
smallest = ret;
selected = filter;
extreme = false;
}
if (xz->flags & SQFS_COMP_FLAG_XZ_EXTREME) {
ret = compress(xz, filter, in, size, out, outsize,
xz->level | LZMA_PRESET_EXTREME);
if (ret > 0 && (smallest == 0 || ret < smallest)) {
smallest = ret;
selected = filter;
extreme = true;
}
}
}
if (smallest == 0)
return 0;
return compress(xz, selected, in, size, out, outsize,
xz->level | (extreme ? LZMA_PRESET_EXTREME : 0));
}
static sqfs_s32 xz_uncomp_block(sqfs_compressor_t *base, const sqfs_u8 *in,
sqfs_u32 size, sqfs_u8 *out, sqfs_u32 outsize)
{
sqfs_u64 memlimit = 65 * 1024 * 1024;
size_t dest_pos = 0;
size_t src_pos = 0;
lzma_ret ret;
(void)base;
if (outsize >= 0x7FFFFFFF)
return SQFS_ERROR_ARG_INVALID;
ret = lzma_stream_buffer_decode(&memlimit, 0, NULL,
in, &src_pos, size,
out, &dest_pos, outsize);
if (ret == LZMA_OK && size == src_pos)
return dest_pos;
return SQFS_ERROR_COMPRESSOR;
}
static void xz_get_configuration(const sqfs_compressor_t *base,
sqfs_compressor_config_t *cfg)
{
const xz_compressor_t *xz = (const xz_compressor_t *)base;
memset(cfg, 0, sizeof(*cfg));
cfg->id = SQFS_COMP_XZ;
cfg->flags = xz->flags;
cfg->block_size = xz->block_size;
cfg->level = xz->level;
cfg->opt.xz.dict_size = xz->dict_size;
cfg->opt.xz.lc = xz->lc;
cfg->opt.xz.lp = xz->lp;
cfg->opt.xz.pb = xz->pb;
if (base->do_block == xz_uncomp_block)
cfg->flags |= SQFS_COMP_FLAG_UNCOMPRESS;
}
static sqfs_object_t *xz_create_copy(const sqfs_object_t *cmp)
{
xz_compressor_t *xz = malloc(sizeof(*xz));
if (xz == NULL)
return NULL;
memcpy(xz, cmp, sizeof(*xz));
return (sqfs_object_t *)xz;
}
static void xz_destroy(sqfs_object_t *base)
{
free(base);
}
int xz_compressor_create(const sqfs_compressor_config_t *cfg,
sqfs_compressor_t **out)
{
sqfs_compressor_t *base;
xz_compressor_t *xz;
if (cfg->flags & ~(SQFS_COMP_FLAG_GENERIC_ALL |
SQFS_COMP_FLAG_XZ_ALL)) {
return SQFS_ERROR_UNSUPPORTED;
}
if (!is_dict_size_valid(cfg->opt.xz.dict_size))
return SQFS_ERROR_UNSUPPORTED;
if (cfg->opt.xz.lc + cfg->opt.xz.lp > 4)
return SQFS_ERROR_UNSUPPORTED;
if (cfg->opt.xz.pb > SQFS_XZ_MAX_PB)
return SQFS_ERROR_UNSUPPORTED;
if (cfg->level > SQFS_XZ_MAX_LEVEL)
return SQFS_ERROR_UNSUPPORTED;
if (cfg->opt.xz.dict_size < SQFS_XZ_MIN_DICT_SIZE)
return SQFS_ERROR_UNSUPPORTED;
if (cfg->opt.xz.dict_size > SQFS_XZ_MAX_DICT_SIZE)
return SQFS_ERROR_UNSUPPORTED;
xz = calloc(1, sizeof(*xz));
base = (sqfs_compressor_t *)xz;
if (xz == NULL)
return SQFS_ERROR_ALLOC;
sqfs_object_init(xz, xz_destroy, xz_create_copy);
xz->flags = cfg->flags;
xz->dict_size = cfg->opt.xz.dict_size;
xz->block_size = cfg->block_size;
xz->lc = cfg->opt.xz.lc;
xz->lp = cfg->opt.xz.lp;
xz->pb = cfg->opt.xz.pb;
xz->level = cfg->level;
base->get_configuration = xz_get_configuration;
base->do_block = (cfg->flags & SQFS_COMP_FLAG_UNCOMPRESS) ?
xz_uncomp_block : xz_comp_block;
base->write_options = xz_write_options;
base->read_options = xz_read_options;
*out = base;
return 0;
}
|