aboutsummaryrefslogtreecommitdiffstats
path: root/tools/squashfs4/patches/110-lzma.patch
blob: 9a924b74b9c9d786d50c8c20d8190cd50579ff65 (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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
--- a/squashfs-tools/mksquashfs.c
+++ b/squashfs-tools/mksquashfs.c
@@ -64,6 +64,18 @@
 #include "global.h"
 #include "sort.h"
 #include "pseudo.h"
+#include "uncompress.h"
+
+#ifdef USE_LZMA
+#include <LzmaEnc.h>
+#include <LzmaDec.h>
+#define LZMA_DEFAULT_LEVEL	5
+#define LZMA_DEFAULT_DICT	0
+#define LZMA_DEFAULT_LC	1
+#define LZMA_DEFAULT_LP	2
+#define LZMA_DEFAULT_PB 2
+#define LZMA_DEFAULT_FB 32
+#endif
 
 #ifdef SQUASHFS_TRACE
 #define TRACE(s, args...)	do { \
@@ -830,6 +842,19 @@ void sigalrm_handler()
 	rotate = (rotate + 1) % 4;
 }
 
+#ifdef USE_LZMA
+static void *lzma_malloc(void *p, size_t size)
+{
+	(void)p;
+	return malloc(size);
+}
+static void lzma_free(void *p, void *addr)
+{
+	(void)p;
+	free(addr);
+}
+static ISzAlloc lzma_alloc = { lzma_malloc, lzma_free };
+#endif
 
 unsigned int mangle2(z_stream **strm, char *d, char *s, int size,
 	int block_size, int uncompressed, int data_block)
@@ -841,6 +866,48 @@ unsigned int mangle2(z_stream **strm, ch
 	if(uncompressed)
 		goto notcompressed;
 
+#ifdef USE_LZMA
+	if (compression == LZMA_COMPRESSION) {
+		size_t outsize = block_size - LZMA_PROPS_SIZE;
+		size_t propsize = LZMA_PROPS_SIZE;
+		CLzmaEncProps props;
+
+		LzmaEncProps_Init(&props);
+		props.level = LZMA_DEFAULT_LEVEL;
+		props.dictSize = LZMA_DEFAULT_DICT;
+		props.lc = LZMA_DEFAULT_LC;
+		props.lp = LZMA_DEFAULT_LP;
+		props.pb = LZMA_DEFAULT_PB;
+		props.fb = LZMA_DEFAULT_FB;
+		props.numThreads = 1;
+
+		res = LzmaEncode((unsigned char *) d + LZMA_PROPS_SIZE, &outsize,
+			(unsigned char *) s, size,
+			&props, (unsigned char *) d, &propsize,
+			1, NULL, &lzma_alloc, &lzma_alloc);
+		switch(res) {
+		case SZ_OK:
+			outsize += LZMA_PROPS_SIZE;
+			break;
+		case SZ_ERROR_DATA:
+			BAD_ERROR("lzma::compress failed, data error\n");
+			break;
+		case SZ_ERROR_MEM:
+			BAD_ERROR("lzma::compress failed, memory allocation error\n");
+			break;
+		case SZ_ERROR_PARAM:
+			BAD_ERROR("lzma::compress failed, invalid parameters\n");
+			break;
+		/* should not happen */
+		default:
+			BAD_ERROR("lzma::compress failed, unknown error\n");
+			break;
+		}
+
+		return outsize;
+	}
+#endif
+
 	if(stream == NULL) {
 		if((stream = *strm = malloc(sizeof(z_stream))) == NULL)
 			BAD_ERROR("mangle::compress failed, not enough "
@@ -1669,17 +1736,17 @@ struct file_buffer *get_fragment(struct 
 		else
 			data = read_from_disk(start_block, size);
 
-		res = uncompress((unsigned char *) buffer->data, &bytes,
+		res = uncompress_wrapper((unsigned char *) buffer->data, &bytes,
 			(const unsigned char *) data, size);
 		if(res != Z_OK) {
 			if(res == Z_MEM_ERROR)
-				BAD_ERROR("zlib::uncompress failed, not enough "
+				BAD_ERROR("uncompress failed, not enough "
 					"memory\n");
 			else if(res == Z_BUF_ERROR)
-				BAD_ERROR("zlib::uncompress failed, not enough "
+				BAD_ERROR("uncompress failed, not enough "
 					"room in output buffer\n");
 			else
-				BAD_ERROR("zlib::uncompress failed,"
+				BAD_ERROR("uncompress failed,"
 					"  unknown error %d\n", res);
 		}
 	} else if(compressed_buffer)
@@ -4282,6 +4349,10 @@ int main(int argc, char *argv[])
 					argv[0]);
 				exit(1);
 			}
+#ifdef USE_LZMA
+		} else if(strcmp(argv[i], "-lzma") == 0) {
+			compression = LZMA_COMPRESSION;
+#endif
 		} else if(strcmp(argv[i], "-ef") == 0) {
 			if(++i == argc) {
 				ERROR("%s: -ef missing filename\n", argv[0]);
@@ -4410,6 +4481,9 @@ printOptions:
 			ERROR("-b <block_size>\t\tset data block to "
 				"<block_size>.  Default %d bytes\n",
 				SQUASHFS_FILE_SIZE);
+#ifdef USE_LZMA
+			ERROR("-lzma Enable LZMA compression\n");
+#endif
 			ERROR("-processors <number>\tUse <number> processors."
 				"  By default will use number of\n");
 			ERROR("\t\t\tprocessors available\n");
@@ -4804,7 +4878,7 @@ restore_filesystem:
 	sBlk.bytes_used = bytes;
 
 	/* Only compression supported */
-	sBlk.compression = ZLIB_COMPRESSION;
+	sBlk.compression = compression;
 
 	/* Xattrs are not currently supported */
 	sBlk.xattr_table_start = SQUASHFS_INVALID_BLK;
--- a/squashfs-tools/squashfs_fs.h
+++ b/squashfs-tools/squashfs_fs.h
@@ -229,6 +229,7 @@ typedef long long		squashfs_block_t;
 typedef long long		squashfs_inode_t;
 
 #define ZLIB_COMPRESSION	1
+#define LZMA_COMPRESSION	2
 
 struct squashfs_super_block {
 	unsigned int		s_magic;
--- a/squashfs-tools/Makefile
+++ b/squashfs-tools/Makefile
@@ -4,14 +4,20 @@ INCLUDEDIR = .
 
 CFLAGS := -I$(INCLUDEDIR) -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_GNU_SOURCE -O2
 
+ifdef USE_LZMA
+  LZMA_CFLAGS = -DUSE_LZMA
+  LZMA_LIB = -llzma
+  CFLAGS += $(LZMA_CFLAGS)
+endif
+
 all: mksquashfs unsquashfs
 
-mksquashfs: mksquashfs.o read_fs.o sort.o swap.o pseudo.o
-	$(CC) mksquashfs.o read_fs.o sort.o swap.o pseudo.o -lz -lpthread -lm -o $@
+mksquashfs: mksquashfs.o read_fs.o sort.o swap.o pseudo.o uncompress.o
+	$(CC) mksquashfs.o read_fs.o sort.o swap.o pseudo.o uncompress.o -lz -lpthread -lm $(LZMA_LIB) -o $@
 
-mksquashfs.o: mksquashfs.c squashfs_fs.h mksquashfs.h global.h sort.h squashfs_swap.h Makefile
+mksquashfs.o: mksquashfs.c squashfs_fs.h mksquashfs.h global.h sort.h squashfs_swap.h uncompress.h Makefile
 
-read_fs.o: read_fs.c squashfs_fs.h read_fs.h global.h squashfs_swap.h Makefile
+read_fs.o: read_fs.c squashfs_fs.h read_fs.h global.h squashfs_swap.h uncompress.h Makefile
 
 sort.o: sort.c squashfs_fs.h global.h sort.h Makefile
 
@@ -19,18 +25,20 @@ swap.o: swap.c Makefile
 
 pseudo.o: pseudo.c pseudo.h Makefile
 
-unsquashfs: unsquashfs.o unsquash-1.o unsquash-2.o unsquash-3.o unsquash-4.o swap.o
-	$(CC) unsquashfs.o unsquash-1.o unsquash-2.o unsquash-3.o unsquash-4.o swap.o -lz -lpthread -lm -o $@
+uncompress.o: uncompress.c uncompress.h
+
+unsquashfs: unsquashfs.o unsquash-1.o unsquash-2.o unsquash-3.o unsquash-4.o swap.o uncompress.o
+	$(CC) unsquashfs.o unsquash-1.o unsquash-2.o unsquash-3.o unsquash-4.o swap.o uncompress.o -lz -lpthread -lm $(LZMA_LIB) -o $@
 
-unsquashfs.o: unsquashfs.h unsquashfs.c squashfs_fs.h squashfs_swap.h squashfs_compat.h global.h Makefile
+unsquashfs.o: unsquashfs.h unsquashfs.c squashfs_fs.h squashfs_swap.h squashfs_compat.h global.h uncompress.h Makefile
 
-unsquash-1.o: unsquashfs.h unsquash-1.c squashfs_fs.h squashfs_compat.h global.h Makefile
+unsquash-1.o: unsquashfs.h unsquash-1.c squashfs_fs.h squashfs_compat.h global.h uncompress.h Makefile
 
-unsquash-2.o: unsquashfs.h unsquash-2.c unsquashfs.h squashfs_fs.h squashfs_compat.h global.h Makefile
+unsquash-2.o: unsquashfs.h unsquash-2.c unsquashfs.h squashfs_fs.h squashfs_compat.h global.h uncompress.h Makefile
 
-unsquash-3.o: unsquashfs.h unsquash-3.c squashfs_fs.h squashfs_compat.h global.h Makefile
+unsquash-3.o: unsquashfs.h unsquash-3.c squashfs_fs.h squashfs_compat.h global.h uncompress.h Makefile
 
-unsquash-4.o: unsquashfs.h unsquash-4.c squashfs_fs.h squashfs_swap.h global.h Makefile
+unsquash-4.o: unsquashfs.h unsquash-4.c squashfs_fs.h squashfs_swap.h global.h uncompress.h Makefile
 
 clean:
 	-rm -f *.o mksquashfs unsquashfs
--- a/squashfs-tools/read_fs.c
+++ b/squashfs-tools/read_fs.c
@@ -51,6 +51,7 @@ extern unsigned int get_guid(unsigned in
 #include "squashfs_swap.h"
 #include "read_fs.h"
 #include "global.h"
+#include "uncompress.h"
 
 #include <stdlib.h>
 
@@ -83,17 +84,17 @@ int read_block(int fd, long long start, 
 		c_byte = SQUASHFS_COMPRESSED_SIZE(c_byte);
 		read_destination(fd, start + offset, c_byte, buffer);
 
-		res = uncompress(block, &bytes, (const unsigned char *) buffer,
-			c_byte);
+		res = uncompress_wrapper(block, &bytes,
+			(const unsigned char *) buffer, c_byte);
 		if(res != Z_OK) {
 			if(res == Z_MEM_ERROR)
-				ERROR("zlib::uncompress failed, not enough "
+				ERROR("uncompress failed, not enough "
 					"memory\n");
 			else if(res == Z_BUF_ERROR)
-				ERROR("zlib::uncompress failed, not enough "
+				ERROR("uncompress failed, not enough "
 					"room in output buffer\n");
 			else
-				ERROR("zlib::uncompress failed, unknown error "
+				ERROR("uncompress failed, unknown error "
 					"%d\n", res);
 			return 0;
 		}
--- a/squashfs-tools/unsquashfs.c
+++ b/squashfs-tools/unsquashfs.c
@@ -24,6 +24,7 @@
 #include "unsquashfs.h"
 #include "squashfs_swap.h"
 #include "squashfs_compat.h"
+#include "uncompress.h"
 #include "read_fs.h"
 
 struct cache *fragment_cache, *data_cache;
@@ -597,18 +598,17 @@ int read_block(long long start, long lon
 		if(read_bytes(start + offset, c_byte, buffer) == FALSE)
 			goto failed;
 
-		res = uncompress((unsigned char *) block, &bytes,
+		res = uncompress_wrapper((unsigned char *) block, &bytes,
 			(const unsigned char *) buffer, c_byte);
-
 		if(res != Z_OK) {
 			if(res == Z_MEM_ERROR)
-				ERROR("zlib::uncompress failed, not enough "
+				ERROR("uncompress failed, not enough "
 					"memory\n");
 			else if(res == Z_BUF_ERROR)
-				ERROR("zlib::uncompress failed, not enough "
+				ERROR("uncompress failed, not enough "
 					"room in output buffer\n");
 			else
-				ERROR("zlib::uncompress failed, unknown error "
+				ERROR("uncompress failed, unknown error "
 					"%d\n", res);
 			goto failed;
 		}
@@ -645,18 +645,17 @@ int read_data_block(long long start, uns
 		if(read_bytes(start, c_byte, data) == FALSE)
 			goto failed;
 
-		res = uncompress((unsigned char *) block, &bytes,
+		res = uncompress_wrapper((unsigned char *) block, &bytes,
 			(const unsigned char *) data, c_byte);
-
 		if(res != Z_OK) {
 			if(res == Z_MEM_ERROR)
-				ERROR("zlib::uncompress failed, not enough "
+				ERROR("uncompress failed, not enough "
 					"memory\n");
 			else if(res == Z_BUF_ERROR)
-				ERROR("zlib::uncompress failed, not enough "
+				ERROR("uncompress failed, not enough "
 					"room in output buffer\n");
 			else
-				ERROR("zlib::uncompress failed, unknown error "
+				ERROR("uncompress failed, unknown error "
 					"%d\n", res);
 			goto failed;
 		}
@@ -1459,7 +1458,7 @@ int read_super(char *source)
 		s_ops.read_inode = read_inode_4;
 		s_ops.read_uids_guids = read_uids_guids_4;
 		memcpy(&sBlk, &sBlk_4, sizeof(sBlk_4));
-		return TRUE;
+		goto done;
 	}
 
 	/*
@@ -1548,6 +1547,9 @@ int read_super(char *source)
 		goto failed_mount;
 	}
 
+done:
+	compression = sBlk.compression;
+
 	return TRUE;
 
 failed_mount:
@@ -1710,19 +1712,19 @@ void *deflator(void *arg)
 		int res;
 		unsigned long bytes = block_size;
 
-		res = uncompress((unsigned char *) tmp, &bytes,
+		res = uncompress_wrapper((unsigned char *) tmp, &bytes,
 			(const unsigned char *) entry->data,
 			SQUASHFS_COMPRESSED_SIZE_BLOCK(entry->size));
 
 		if(res != Z_OK) {
 			if(res == Z_MEM_ERROR)
-				ERROR("zlib::uncompress failed, not enough"
+				ERROR("uncompress failed, not enough"
 					"memory\n");
 			else if(res == Z_BUF_ERROR)
-				ERROR("zlib::uncompress failed, not enough "
+				ERROR("uncompress failed, not enough "
 					"room in output buffer\n");
 			else
-				ERROR("zlib::uncompress failed, unknown error "
+				ERROR("uncompress failed, unknown error "
 					"%d\n", res);
 		} else
 			memcpy(entry->data, tmp, bytes);
--- a/squashfs-tools/mksquashfs.h
+++ b/squashfs-tools/mksquashfs.h
@@ -41,4 +41,9 @@
 #define SQUASHFS_SWAP_LONG_LONGS(s, d, n) \
 					memcpy(d, s, n * sizeof(long long))
 #endif
+
+extern int uncompress_wrapper(unsigned char *dest, unsigned long *dest_len,
+    const unsigned char *src, unsigned long src_len);
+
+
 #endif
--- /dev/null
+++ b/squashfs-tools/uncompress.c
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2009  Felix Fietkau <nbd@openwrt.org>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2,
+ * or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * uncompress.c
+ */
+
+
+
+#ifdef USE_LZMA
+#include <LzmaLib.h>
+#endif
+#include <zlib.h>
+#include "squashfs_fs.h"
+
+/* compression algorithm */
+int compression = ZLIB_COMPRESSION;
+
+
+int uncompress_wrapper(unsigned char *dest, unsigned long *dest_len,
+	const unsigned char *src, unsigned long src_len)
+{
+	int res;
+
+#ifdef USE_LZMA
+	if (compression == LZMA_COMPRESSION) {
+		size_t slen = src_len - LZMA_PROPS_SIZE;
+		res = LzmaUncompress((unsigned char *)dest, dest_len,
+			(const unsigned char *) src + LZMA_PROPS_SIZE, &slen,
+			(const unsigned char *) src, LZMA_PROPS_SIZE);
+		switch(res) {
+		case SZ_OK:
+			res = Z_OK;
+			break;
+		case SZ_ERROR_MEM:
+			res = Z_MEM_ERROR;
+			break;
+		}
+	} else
+#endif
+	res = uncompress(dest, dest_len, src, src_len);
+	return res;
+}
+
+
--- /dev/null
+++ b/squashfs-tools/uncompress.h
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2009  Felix Fietkau <nbd@openwrt.org>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2,
+ * or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * uncompress.h
+ */
+
+#ifdef USE_LZMA
+#include <LzmaLib.h>
+#endif
+
+extern int compression;
+extern int uncompress_wrapper(unsigned char *dest, unsigned long *dest_len,
+    const unsigned char *src, unsigned long src_len);
+
+