-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmicrofs_decompressor_zlib.c
More file actions
276 lines (227 loc) · 7.98 KB
/
microfs_decompressor_zlib.c
File metadata and controls
276 lines (227 loc) · 7.98 KB
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
/* microfs - Minimally Improved Compressed Read Only File System
* Copyright (C) 2012, 2013, 2014, 2015, 2016, 2017, ..., +%Y
* Erik Edlund <erik.edlund@32767.se>
*
* 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 of the License, 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, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "microfs.h"
#include "libinfo_zlib.h"
#ifdef MICROFS_DECOMPRESSOR_ZLIB
#include <linux/zlib.h>
struct decompressor_zlib_data {
void* z_pageaddr;
struct z_stream_s z_strm;
};
static int decompressor_zlib_create(struct microfs_sb_info* sbi, void** dest)
{
struct decompressor_zlib_data* zdat = kmalloc(sizeof(*zdat), GFP_KERNEL);
if (!zdat)
goto err_mem_zdat;
zdat->z_strm.workspace = kmalloc(zlib_inflate_workspacesize(), GFP_KERNEL);
if (!zdat->z_strm.workspace)
goto err_mem_workspace;
zdat->z_strm.next_in = NULL;
zdat->z_strm.avail_in = 0;
zdat->z_strm.next_out = NULL;
zdat->z_strm.avail_out = 0;
zlib_inflateInit(&zdat->z_strm);
*dest = zdat;
return 0;
err_mem_workspace:
kfree(zdat);
err_mem_zdat:
return -ENOMEM;
}
static int decompressor_zlib_destroy(struct microfs_sb_info* sbi, void* data)
{
struct decompressor_zlib_data* zdat = data;
if (zdat) {
zlib_inflateEnd(&zdat->z_strm);
kfree(zdat->z_strm.workspace);
kfree(zdat);
}
return 0;
}
static int decompressor_zlib_reset(struct microfs_sb_info* sbi, void* data)
{
struct decompressor_zlib_data* zdat = data;
if (unlikely(zlib_inflateReset(&zdat->z_strm) != Z_OK)) {
pr_err("failed to reset the inflate stream: %s\n", zdat->z_strm.msg);
pr_err("reinitializing the stream\n");
zlib_inflateEnd(&zdat->z_strm);
zlib_inflateInit(&zdat->z_strm);
}
return 0;
}
static int decompressor_zlib_exceptionally_begin(struct microfs_sb_info* sbi,
void* data)
{
struct decompressor_zlib_data* zdat = data;
pr_spam("decompressor_zlib_exceptionally_begin: zdat=0x%p\n", zdat);
sbi->si_filedatabuf.d_offset = MICROFS_MAXIMGSIZE - 1;
sbi->si_filedatabuf.d_used = 0;
zdat->z_strm.avail_in = 0;
zdat->z_strm.next_in = NULL;
zdat->z_strm.avail_out = sbi->si_filedatabuf.d_size;
zdat->z_strm.next_out = sbi->si_filedatabuf.d_data;
return 0;
}
static int decompressor_zlib_nominally_begin(struct microfs_sb_info* sbi,
void* data, struct page** pages, __u32 npages)
{
struct decompressor_zlib_data* zdat = data;
pr_spam("decompressor_zlib_nominally_begin: zdat=0x%p\n", zdat);
(void)sbi;
(void)pages;
(void)npages;
zdat->z_strm.avail_in = 0;
zdat->z_strm.next_in = NULL;
zdat->z_strm.avail_out = 0;
zdat->z_strm.next_out = NULL;
return 0;
}
static int decompressor_zlib_copy_nominally_needpage(struct microfs_sb_info* sbi,
void* data)
{
struct decompressor_zlib_data* zdat = data;
(void)sbi;
return zdat->z_strm.avail_out == 0;
}
static int decompressor_zlib_copy_nominally_utilizepage(struct microfs_sb_info* sbi,
void* data, struct page* page)
{
struct decompressor_zlib_data* zdat = data;
(void)sbi;
if (page) {
zdat->z_pageaddr = kmap_atomic(page);
zdat->z_strm.next_out = zdat->z_pageaddr;
zdat->z_strm.avail_out = PAGE_SIZE;
} else {
zdat->z_strm.next_out = NULL;
}
return zdat->z_strm.next_out != NULL;
}
static int decompressor_zlib_copy_nominally_releasepage(struct microfs_sb_info* sbi,
void* data, struct page* page)
{
struct decompressor_zlib_data* zdat = data;
(void)sbi;
(void)page;
kunmap_atomic(zdat->z_pageaddr);
return 0;
}
static int decompressor_zlib_consumebhs(struct microfs_sb_info* sbi,
void* data, struct buffer_head** bhs, __u32 nbhs, __u32* length,
__u32* bh, __u32* bh_offset, __u32* inflated, int* implerr)
{
int err = 0;
struct decompressor_zlib_data* zdat = data;
pr_spam("decompressor_zlib_consumebhs: sbi=0x%p, bhs=0x%p, nbhs=%u\n", sbi, bhs, nbhs);
pr_spam("decompressor_zlib_consumebhs: zdat=0x%p\n", zdat);
pr_spam("decompressor_zlib_consumebhs: *length=%u, *bh=%u, *bh_offset=%u, *inflated=%u\n",
*length, *bh, *bh_offset, *inflated);
do {
if (zdat->z_strm.avail_in == 0) {
pr_spam("decompressor_zlib_consumebhs: *bh=%u, bhs[*bh]=0x%p\n", *bh, bhs[*bh]);
zdat->z_strm.avail_in = min_t(__u32, *length, PAGE_SIZE - *bh_offset);
zdat->z_strm.next_in = bhs[*bh]->b_data + *bh_offset;
*bh += 1;
*length -= zdat->z_strm.avail_in;
*bh_offset = 0;
}
pr_spam("decompressor_zlib_consumebhs: *length=%u\n", *length);
pr_spam("decompressor_zlib_consumebhs: pre; zstrm->avail_out=%u, zstrm->next_out=0x%p\n",
zdat->z_strm.avail_out, zdat->z_strm.next_out);
pr_spam("decompressor_zlib_consumebhs: pre; zstrm->avail_in=%u, zstrm->next_in=0x%p\n",
zdat->z_strm.avail_in, zdat->z_strm.next_in);
*implerr = zlib_inflate(&zdat->z_strm, Z_SYNC_FLUSH);
pr_spam("decompressor_zlib_consumebhs: post; zstrm->avail_out=%u, zstrm->next_out=0x%p\n",
zdat->z_strm.avail_out, zdat->z_strm.next_out);
pr_spam("decompressor_zlib_consumebhs: post; zstrm->avail_in=%u, zstrm->next_in=0x%p\n",
zdat->z_strm.avail_in, zdat->z_strm.next_in);
pr_spam("decompressor_zlib_consumebhs: *implerr=%d\n", *implerr);
if (zdat->z_strm.avail_out == 0 && zdat->z_strm.next_out != NULL) {
/* z_strm.avail_out can be zero when z_strm.next_out is NULL.
* If it is not, the output buffer must be refilled (it is
* also possible that everything is inflated, but let the
* caller worry about that).
*/
break;
}
} while (*implerr == Z_OK);
if (*implerr == Z_STREAM_END) {
*inflated += zdat->z_strm.total_out;
pr_spam("decompressor_zlib_consumebhs: at streams end, %u bytes inflated, %u bytes total",
(__u32)zdat->z_strm.total_out, *inflated);
sbi->si_decompressor->dc_reset(sbi, data);
} else if (*implerr != Z_OK) {
pr_err("decompressor_zlib_consumebhs: failed to inflate data: (%d) %s\n",
*implerr, zdat->z_strm.msg);
err = -EIO;
}
return err;
}
static int decompressor_zlib_continue(struct microfs_sb_info* sbi,
void* data, int err, int implerr, __u32 length, int more_avail_out)
{
struct decompressor_zlib_data* zdat = data;
return !err && (implerr == Z_OK || (
implerr == Z_STREAM_END && (
zdat->z_strm.avail_in > 0 || length > 0
) && (
zdat->z_strm.avail_out > 0 || more_avail_out > 0
)
));
}
static int decompressor_zlib_end(struct microfs_sb_info* sbi,
void* data, int* err, int* implerr, __u32* decompressed)
{
(void)sbi;
(void)data;
(void)decompressed;
if (*err) {
return -1;
} else if (!*err && *implerr != Z_STREAM_END) {
pr_err("decompressor_zlib_end: zlib not at streams end"
" but no error is reported by decompressor_zlib_consumebhs\n");
*err = -EIO;
return -1;
}
return 0;
}
const struct microfs_decompressor decompressor_zlib = {
.dc_info = &libinfo_zlib,
.dc_compiled = 1,
.dc_streamed = 1,
.dc_data_init = microfs_decompressor_data_init_noop,
.dc_data_exit = microfs_decompressor_data_exit_noop,
.dc_create = decompressor_zlib_create,
.dc_destroy = decompressor_zlib_destroy,
.dc_reset = decompressor_zlib_reset,
.dc_exceptionally_begin = decompressor_zlib_exceptionally_begin,
.dc_nominally_begin = decompressor_zlib_nominally_begin,
.dc_copy_nominally_needpage = decompressor_zlib_copy_nominally_needpage,
.dc_copy_nominally_utilizepage = decompressor_zlib_copy_nominally_utilizepage,
.dc_copy_nominally_releasepage = decompressor_zlib_copy_nominally_releasepage,
.dc_consumebhs = decompressor_zlib_consumebhs,
.dc_continue = decompressor_zlib_continue,
.dc_end = decompressor_zlib_end
};
#else
const struct microfs_decompressor decompressor_zlib = {
.dc_info = &libinfo_zlib,
.dc_compiled = 0
};
#endif