Root/
Source at commit 45c3204b42c8333664975ab15c3cd9438754fe94 created 13 years 6 months ago. By Phillip Lougher, Squashfs: add LZMA compression | |
---|---|
1 | /* |
2 | * Squashfs - a compressed read only filesystem for Linux |
3 | * |
4 | * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 |
5 | * Phillip Lougher <phillip@lougher.demon.co.uk> |
6 | * |
7 | * This program is free software; you can redistribute it and/or |
8 | * modify it under the terms of the GNU General Public License |
9 | * as published by the Free Software Foundation; either version 2, |
10 | * or (at your option) any later version. |
11 | * |
12 | * This program is distributed in the hope that it will be useful, |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | * GNU General Public License for more details. |
16 | * |
17 | * You should have received a copy of the GNU General Public License |
18 | * along with this program; if not, write to the Free Software |
19 | * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
20 | * |
21 | * lzma_wrapper.c |
22 | */ |
23 | |
24 | #include <asm/unaligned.h> |
25 | #include <linux/buffer_head.h> |
26 | #include <linux/mutex.h> |
27 | #include <linux/vmalloc.h> |
28 | #include <linux/decompress/unlzma.h> |
29 | #include <linux/slab.h> |
30 | |
31 | #include "squashfs_fs.h" |
32 | #include "squashfs_fs_sb.h" |
33 | #include "squashfs_fs_i.h" |
34 | #include "squashfs.h" |
35 | #include "decompressor.h" |
36 | |
37 | struct squashfs_lzma { |
38 | void *input; |
39 | void *output; |
40 | }; |
41 | |
42 | /* decompress_unlzma.c is currently non re-entrant... */ |
43 | DEFINE_MUTEX(lzma_mutex); |
44 | |
45 | /* decompress_unlzma.c doesn't provide any context in its callbacks... */ |
46 | static int lzma_error; |
47 | |
48 | static void error(char *m) |
49 | { |
50 | ERROR("unlzma error: %s\n", m); |
51 | lzma_error = 1; |
52 | } |
53 | |
54 | |
55 | static void *lzma_init(struct squashfs_sb_info *msblk) |
56 | { |
57 | struct squashfs_lzma *stream = kzalloc(sizeof(*stream), GFP_KERNEL); |
58 | if (stream == NULL) |
59 | goto failed; |
60 | stream->input = vmalloc(msblk->block_size); |
61 | if (stream->input == NULL) |
62 | goto failed; |
63 | stream->output = vmalloc(msblk->block_size); |
64 | if (stream->output == NULL) |
65 | goto failed2; |
66 | |
67 | return stream; |
68 | |
69 | failed2: |
70 | vfree(stream->input); |
71 | failed: |
72 | ERROR("failed to allocate lzma workspace\n"); |
73 | kfree(stream); |
74 | return NULL; |
75 | } |
76 | |
77 | |
78 | static void lzma_free(void *strm) |
79 | { |
80 | struct squashfs_lzma *stream = strm; |
81 | |
82 | if (stream) { |
83 | vfree(stream->input); |
84 | vfree(stream->output); |
85 | } |
86 | kfree(stream); |
87 | } |
88 | |
89 | |
90 | static int lzma_uncompress(struct squashfs_sb_info *msblk, void **buffer, |
91 | struct buffer_head **bh, int b, int offset, int length, int srclength, |
92 | int pages) |
93 | { |
94 | struct squashfs_lzma *stream = msblk->stream; |
95 | void *buff = stream->input; |
96 | int avail, i, bytes = length, res; |
97 | |
98 | mutex_lock(&lzma_mutex); |
99 | |
100 | for (i = 0; i < b; i++) { |
101 | wait_on_buffer(bh[i]); |
102 | if (!buffer_uptodate(bh[i])) |
103 | goto block_release; |
104 | |
105 | avail = min(bytes, msblk->devblksize - offset); |
106 | memcpy(buff, bh[i]->b_data + offset, avail); |
107 | buff += avail; |
108 | bytes -= avail; |
109 | offset = 0; |
110 | put_bh(bh[i]); |
111 | } |
112 | |
113 | lzma_error = 0; |
114 | res = unlzma(stream->input, length, NULL, NULL, stream->output, NULL, |
115 | error); |
116 | if (res || lzma_error) |
117 | goto failed; |
118 | |
119 | /* uncompressed size is stored in the LZMA header (5 byte offset) */ |
120 | res = bytes = get_unaligned_le32(stream->input + 5); |
121 | for (i = 0, buff = stream->output; bytes && i < pages; i++) { |
122 | avail = min_t(int, bytes, PAGE_CACHE_SIZE); |
123 | memcpy(buffer[i], buff, avail); |
124 | buff += avail; |
125 | bytes -= avail; |
126 | } |
127 | if (bytes) |
128 | goto failed; |
129 | |
130 | mutex_unlock(&lzma_mutex); |
131 | return res; |
132 | |
133 | block_release: |
134 | for (; i < b; i++) |
135 | put_bh(bh[i]); |
136 | |
137 | failed: |
138 | mutex_unlock(&lzma_mutex); |
139 | |
140 | ERROR("lzma decompression failed, data probably corrupt\n"); |
141 | return -EIO; |
142 | } |
143 | |
144 | const struct squashfs_decompressor squashfs_lzma_comp_ops = { |
145 | .init = lzma_init, |
146 | .free = lzma_free, |
147 | .decompress = lzma_uncompress, |
148 | .id = LZMA_COMPRESSION, |
149 | .name = "lzma", |
150 | .supported = 1 |
151 | }; |
152 | |
153 |
Branches:
ben-wpan
ben-wpan-stefan
javiroman/ks7010
jz-2.6.34
jz-2.6.34-rc5
jz-2.6.34-rc6
jz-2.6.34-rc7
jz-2.6.35
jz-2.6.36
jz-2.6.37
jz-2.6.38
jz-2.6.39
jz-3.0
jz-3.1
jz-3.11
jz-3.12
jz-3.13
jz-3.15
jz-3.16
jz-3.18-dt
jz-3.2
jz-3.3
jz-3.4
jz-3.5
jz-3.6
jz-3.6-rc2-pwm
jz-3.9
jz-3.9-clk
jz-3.9-rc8
jz47xx
jz47xx-2.6.38
master
Tags:
od-2011-09-04
od-2011-09-18
v2.6.34-rc5
v2.6.34-rc6
v2.6.34-rc7
v3.9