forked from xerub/img4lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvfs_lzss.c
241 lines (213 loc) · 5.27 KB
/
vfs_lzss.c
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
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "lzss.h"
#include "vfs.h"
#include "vfs_internal.h"
struct file_ops_lzss {
struct file_ops_memory ops;
FHANDLE other;
void *watchtower;
size_t watchsize;
};
static int
lzss_fsync(FHANDLE fd)
{
FHANDLE other;
struct file_ops_lzss *ctx = (struct file_ops_lzss *)fd;
uint32_t csize;
uint32_t adler;
size_t total, written;
uint8_t *end, *buf;
if (!fd) {
return -1;
}
other = ctx->other;
if (other->flags == O_RDONLY) {
return 0;
}
if (!MEMFD(fd)->dirty) {
goto next;
}
total = MEMFD(fd)->size;
adler = lzadler32(MEMFD(fd)->buf, total);
buf = malloc(0x180 + (total + 256));
if (!buf) {
return -1;
}
end = compress_lzss(buf + 0x180, total + 256, MEMFD(fd)->buf, total);
csize = end - (buf + 0x180);
PUT_DWORD_BE(buf, 0, 'comp');
PUT_DWORD_BE(buf, 4, 'lzss');
PUT_DWORD_BE(buf, 8, adler);
PUT_DWORD_BE(buf, 12, total);
PUT_DWORD_BE(buf, 16, csize);
PUT_DWORD_BE(buf, 20, 1);
memset(buf + 24, 0, 0x180 - 24);
other->lseek(other, 0, SEEK_SET);
written = other->write(other, buf, end - buf);
free(buf);
if (buf + written != end) {
return -1;
}
written = other->write(other, ctx->watchtower, ctx->watchsize);
if (written != ctx->watchsize) {
return -1;
}
other->ftruncate(other, end - buf + written);
next:
MEMFD(fd)->dirty = 0;
return other->fsync(other);
}
static int
lzss_close(FHANDLE fd)
{
int rv, rc;
FHANDLE other;
struct file_ops_lzss *ctx = (struct file_ops_lzss *)fd;
if (!fd) {
return -1;
}
other = ctx->other;
rv = fd->fsync(fd);
free(ctx->watchtower);
memory_close(fd);
rc = other->close(other);
return rv ? rv : rc;
}
static int
lzss_ioctl(FHANDLE fd, unsigned long req, ...)
{
struct file_ops_lzss *ctx = (struct file_ops_lzss *)fd;
int rv = -1;
va_list ap;
if (!fd) {
return -1;
}
va_start(ap, req);
switch (req) {
case IOCTL_MEM_GET_DATAPTR: {
void **dst = va_arg(ap, void **);
size_t *sz = va_arg(ap, size_t *);
*dst = MEMFD(fd)->buf;
*sz = MEMFD(fd)->size;
rv = 0;
break;
}
case IOCTL_LZSS_GET_WTOWER: {
void **dst = va_arg(ap, void **);
size_t *sz = va_arg(ap, size_t *);
*dst = ctx->watchtower;
*sz = ctx->watchsize;
rv = 0;
break;
}
case IOCTL_LZSS_SET_WTOWER: {
void *old = ctx->watchtower;
void *src = va_arg(ap, void *);
size_t sz = va_arg(ap, size_t);
ctx->watchtower = src;
ctx->watchsize = sz;
free(old);
rv = 0;
break;
}
default: {
void *a = va_arg(ap, void *);
void *b = va_arg(ap, void *);
FHANDLE other = ctx->other;
rv = other->ioctl(other, req, a, b); /* XXX varargs */
}
}
va_end(ap);
return rv;
}
FHANDLE
lzss_reopen(FHANDLE other)
{
FHANDLE fd;
size_t outlen;
uint32_t csize;
uint32_t usize;
uint32_t adler;
unsigned char hdr[20];
unsigned char *buf, *dec;
struct file_ops_lzss *ctx;
off_t where;
size_t tail;
if (!other) {
return NULL;
}
if (other->flags == O_WRONLY) {
goto closeit;
}
where = other->lseek(other, 0, SEEK_CUR);
outlen = other->read(other, hdr, sizeof(hdr));
if (outlen != sizeof(hdr) || GET_DWORD_BE(hdr, 0) != 'comp' || GET_DWORD_BE(hdr, 4) != 'lzss') {
other->lseek(other, where, SEEK_SET);
return other;
}
csize = GET_DWORD_BE(hdr, 16);
usize = GET_DWORD_BE(hdr, 12);
buf = malloc(csize);
if (!buf) {
goto closeit;
}
outlen = other->lseek(other, 0x180, SEEK_SET);
if (outlen != 0x180) {
goto freebuf;
}
outlen = other->read(other, buf, csize);
if (outlen != csize) {
goto freebuf;
}
dec = malloc(usize);
if (!dec) {
goto freebuf;
}
outlen = decompress_lzss(dec, buf, csize);
free(buf);
buf = dec;
if (outlen != usize) {
goto freebuf;
}
adler = lzadler32(dec, usize);
if (GET_DWORD_BE(hdr, 8) != adler) {
fprintf(stderr, "adler mismatch: stored=%08x calculated=%08x\n", GET_DWORD_BE(hdr, 8), adler);
}
fd = memory_openex(malloc(sizeof(*ctx)), other->flags, buf, usize);
if (!fd) {
goto freebuf;
}
ctx = (struct file_ops_lzss *)fd;
ctx->other = other;
tail = other->length(other);
if ((ssize_t)tail < 0 || tail < csize + 0x180) {
goto error;
}
tail -= csize + 0x180;
ctx->watchtower = malloc(tail);
if (!ctx->watchtower) {
goto error;
}
outlen = other->read(other, ctx->watchtower, tail);
if (outlen != tail) {
free(ctx->watchtower);
goto error;
}
ctx->watchsize = tail;
fd->ioctl = lzss_ioctl;
fd->fsync = lzss_fsync;
fd->close = lzss_close;
return fd;
error:
fd->close(fd);
freebuf:
free(buf);
closeit:
other->close(other);
return NULL;
}