src/util/vfs/vfs-lzma.c (view raw)
1/* Copyright (c) 2013-2015 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "util/vfs.h"
7
8#ifdef USE_LZMA
9
10#include "util/string.h"
11
12#include "third-party/lzma/7z.h"
13#include "third-party/lzma/7zAlloc.h"
14#include "third-party/lzma/7zBuf.h"
15#include "third-party/lzma/7zCrc.h"
16#include "third-party/lzma/7zFile.h"
17#include "third-party/lzma/7zVersion.h"
18
19struct VDirEntry7z {
20 struct VDirEntry d;
21
22 struct VDir7z* vd;
23 UInt32 index;
24 char* utf8;
25};
26
27struct VDir7z {
28 struct VDir d;
29 struct VDirEntry7z dirent;
30
31 // What is all this garbage?
32 CFileInStream archiveStream;
33 CLookToRead lookStream;
34 CSzArEx db;
35 ISzAlloc allocImp;
36 ISzAlloc allocTempImp;
37};
38
39struct VFile7z {
40 struct VFile d;
41
42 struct VDir7z* vd;
43
44 size_t offset;
45
46 Byte* outBuffer;
47 size_t bufferOffset;
48 size_t size;
49};
50
51static bool _vf7zClose(struct VFile* vf);
52static off_t _vf7zSeek(struct VFile* vf, off_t offset, int whence);
53static ssize_t _vf7zRead(struct VFile* vf, void* buffer, size_t size);
54static ssize_t _vf7zWrite(struct VFile* vf, const void* buffer, size_t size);
55static void* _vf7zMap(struct VFile* vf, size_t size, int flags);
56static void _vf7zUnmap(struct VFile* vf, void* memory, size_t size);
57static void _vf7zTruncate(struct VFile* vf, size_t size);
58static ssize_t _vf7zSize(struct VFile* vf);
59static bool _vf7zSync(struct VFile* vf, const void* buffer, size_t size);
60
61static bool _vd7zClose(struct VDir* vd);
62static void _vd7zRewind(struct VDir* vd);
63static struct VDirEntry* _vd7zListNext(struct VDir* vd);
64static struct VFile* _vd7zOpenFile(struct VDir* vd, const char* path, int mode);
65
66static const char* _vde7zName(struct VDirEntry* vde);
67
68struct VDir* VDirOpen7z(const char* path, int flags) {
69 if (flags & O_WRONLY || flags & O_CREAT) {
70 return 0;
71 }
72
73 struct VDir7z* vd = malloc(sizeof(struct VDir7z));
74
75 // What does any of this mean, Igor?
76 if (InFile_Open(&vd->archiveStream.file, path)) {
77 free(vd);
78 return 0;
79 }
80
81 vd->allocImp.Alloc = SzAlloc;
82 vd->allocImp.Free = SzFree;
83
84 vd->allocTempImp.Alloc = SzAllocTemp;
85 vd->allocTempImp.Free = SzFreeTemp;
86
87 FileInStream_CreateVTable(&vd->archiveStream);
88 LookToRead_CreateVTable(&vd->lookStream, False);
89
90 vd->lookStream.realStream = &vd->archiveStream.s;
91 LookToRead_Init(&vd->lookStream);
92
93 CrcGenerateTable();
94
95 SzArEx_Init(&vd->db);
96 SRes res = SzArEx_Open(&vd->db, &vd->lookStream.s, &vd->allocImp, &vd->allocTempImp);
97 if (res != SZ_OK) {
98 free(vd);
99 return 0;
100 }
101
102 vd->dirent.index = 0;
103 vd->dirent.utf8 = 0;
104 vd->dirent.vd = vd;
105 vd->dirent.d.name = _vde7zName;
106
107 vd->d.close = _vd7zClose;
108 vd->d.rewind = _vd7zRewind;
109 vd->d.listNext = _vd7zListNext;
110 vd->d.openFile = _vd7zOpenFile;
111
112 return &vd->d;
113}
114
115bool _vf7zClose(struct VFile* vf) {
116 struct VFile7z* vf7z = (struct VFile7z*) vf;
117 IAlloc_Free(&vf7z->vd->allocImp, vf7z->outBuffer);
118 return true;
119}
120
121off_t _vf7zSeek(struct VFile* vf, off_t offset, int whence) {
122 struct VFile7z* vf7z = (struct VFile7z*) vf;
123
124 size_t position;
125 switch (whence) {
126 case SEEK_SET:
127 position = offset;
128 break;
129 case SEEK_CUR:
130 if (offset < 0 && ((vf7z->offset < (size_t) -offset) || (offset == INT_MIN))) {
131 return -1;
132 }
133 position = vf7z->offset + offset;
134 break;
135 case SEEK_END:
136 if (offset < 0 && ((vf7z->size < (size_t) -offset) || (offset == INT_MIN))) {
137 return -1;
138 }
139 position = vf7z->size + offset;
140 break;
141 default:
142 return -1;
143 }
144
145 if (position > vf7z->size) {
146 return -1;
147 }
148
149 vf7z->offset = position;
150 return position;
151}
152
153ssize_t _vf7zRead(struct VFile* vf, void* buffer, size_t size) {
154 struct VFile7z* vf7z = (struct VFile7z*) vf;
155
156 if (size + vf7z->offset >= vf7z->size) {
157 size = vf7z->size - vf7z->offset;
158 }
159
160 memcpy(buffer, vf7z->outBuffer + vf7z->offset + vf7z->bufferOffset, size);
161 vf7z->offset += size;
162 return size;
163}
164
165ssize_t _vf7zWrite(struct VFile* vf, const void* buffer, size_t size) {
166 // TODO
167 UNUSED(vf);
168 UNUSED(buffer);
169 UNUSED(size);
170 return -1;
171}
172
173void* _vf7zMap(struct VFile* vf, size_t size, int flags) {
174 struct VFile7z* vf7z = (struct VFile7z*) vf;
175
176 UNUSED(flags);
177 if (size > vf7z->size) {
178 return 0;
179 }
180
181 return vf7z->outBuffer + vf7z->bufferOffset;
182}
183
184void _vf7zUnmap(struct VFile* vf, void* memory, size_t size) {
185 UNUSED(vf);
186 UNUSED(memory);
187 UNUSED(size);
188}
189
190void _vf7zTruncate(struct VFile* vf, size_t size) {
191 // TODO
192 UNUSED(vf);
193 UNUSED(size);
194}
195
196ssize_t _vf7zSize(struct VFile* vf) {
197 struct VFile7z* vf7z = (struct VFile7z*) vf;
198 return vf7z->size;
199}
200
201bool _vd7zClose(struct VDir* vd) {
202 struct VDir7z* vd7z = (struct VDir7z*) vd;
203 SzArEx_Free(&vd7z->db, &vd7z->allocImp);
204 File_Close(&vd7z->archiveStream.file);
205
206 free(vd7z->dirent.utf8);
207 vd7z->dirent.utf8 = 0;
208
209 free(vd7z);
210 return true;
211}
212
213void _vd7zRewind(struct VDir* vd) {
214 struct VDir7z* vd7z = (struct VDir7z*) vd;
215 free(vd7z->dirent.utf8);
216 vd7z->dirent.utf8 = 0;
217 vd7z->dirent.index = -1;
218}
219
220struct VDirEntry* _vd7zListNext(struct VDir* vd) {
221 struct VDir7z* vd7z = (struct VDir7z*) vd;
222 if (vd7z->db.NumFiles <= vd7z->dirent.index + 1) {
223 return 0;
224 }
225 free(vd7z->dirent.utf8);
226 vd7z->dirent.utf8 = 0;
227 ++vd7z->dirent.index;
228 return &vd7z->dirent.d;
229}
230
231struct VFile* _vd7zOpenFile(struct VDir* vd, const char* path, int mode) {
232 UNUSED(mode);
233 // TODO: support truncating, appending and creating, and write
234 struct VDir7z* vd7z = (struct VDir7z*) vd;
235
236 if ((mode & O_RDWR) == O_RDWR) {
237 // Read/Write support is not yet implemented.
238 return 0;
239 }
240
241 if (mode & O_WRONLY) {
242 // Write support is not yet implemented.
243 return 0;
244 }
245
246 size_t pathLength = strlen(path);
247
248 UInt32 i;
249 for (i = 0; i < vd7z->db.NumFiles; ++i) {
250 if (SzArEx_IsDir(&vd7z->db, i)) {
251 continue;
252 }
253 size_t nameLength = SzArEx_GetFileNameUtf16(&vd7z->db, i, 0) * sizeof(UInt16);
254 UInt16* name = malloc(nameLength);
255 SzArEx_GetFileNameUtf16(&vd7z->db, i, name);
256
257 if (utfcmp(name, path, nameLength - sizeof(UInt16), pathLength) == 0) {
258 free(name);
259 break;
260 }
261
262 free(name);
263 }
264
265 if (i == vd7z->db.NumFiles) {
266 return 0; // No file found
267 }
268
269 struct VFile7z* vf = malloc(sizeof(struct VFile7z));
270 vf->vd = vd7z;
271
272 size_t outBufferSize;
273 UInt32 blockIndex;
274
275 vf->outBuffer = 0;
276 SRes res = SzArEx_Extract(&vd7z->db, &vd7z->lookStream.s, i, &blockIndex,
277 &vf->outBuffer, &outBufferSize,
278 &vf->bufferOffset, &vf->size,
279 &vd7z->allocImp, &vd7z->allocTempImp);
280
281 if (res != SZ_OK) {
282 free(vf);
283 return 0;
284 }
285
286 vf->d.close = _vf7zClose;
287 vf->d.seek = _vf7zSeek;
288 vf->d.read = _vf7zRead;
289 vf->d.readline = VFileReadline;
290 vf->d.write = _vf7zWrite;
291 vf->d.map = _vf7zMap;
292 vf->d.unmap = _vf7zUnmap;
293 vf->d.truncate = _vf7zTruncate;
294 vf->d.size = _vf7zSize;
295 vf->d.sync = _vf7zSync;
296
297 return &vf->d;
298}
299
300const char* _vde7zName(struct VDirEntry* vde) {
301 struct VDirEntry7z* vde7z = (struct VDirEntry7z*) vde;
302 if (!vde7z->utf8) {
303 size_t nameLength = SzArEx_GetFileNameUtf16(&vde7z->vd->db, vde7z->index, 0) * sizeof(UInt16);
304 UInt16* name = malloc(nameLength);
305 SzArEx_GetFileNameUtf16(&vde7z->vd->db, vde7z->index, name);
306 vde7z->utf8 = utf16to8(name, nameLength - sizeof(UInt16));
307 free(name);
308 }
309
310 return vde7z->utf8;
311}
312
313bool _vf7zSync(struct VFile* vf, const void* memory, size_t size) {
314 UNUSED(vf);
315 UNUSED(memory);
316 UNUSED(size);
317 return false;
318}
319
320#endif