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);
65static struct VDir* _vd7zOpenDir(struct VDir* vd, const char* path);
66static bool _vd7zDeleteFile(struct VDir* vd, const char* path);
67
68static const char* _vde7zName(struct VDirEntry* vde);
69static enum VFSType _vde7zType(struct VDirEntry* vde);
70
71struct VDir* VDirOpen7z(const char* path, int flags) {
72 if (flags & O_WRONLY || flags & O_CREAT) {
73 return 0;
74 }
75
76 struct VDir7z* vd = malloc(sizeof(struct VDir7z));
77
78 // What does any of this mean, Igor?
79 if (InFile_Open(&vd->archiveStream.file, path)) {
80 free(vd);
81 return 0;
82 }
83
84 vd->allocImp.Alloc = SzAlloc;
85 vd->allocImp.Free = SzFree;
86
87 vd->allocTempImp.Alloc = SzAllocTemp;
88 vd->allocTempImp.Free = SzFreeTemp;
89
90 FileInStream_CreateVTable(&vd->archiveStream);
91 LookToRead_CreateVTable(&vd->lookStream, False);
92
93 vd->lookStream.realStream = &vd->archiveStream.s;
94 LookToRead_Init(&vd->lookStream);
95
96 CrcGenerateTable();
97
98 SzArEx_Init(&vd->db);
99 SRes res = SzArEx_Open(&vd->db, &vd->lookStream.s, &vd->allocImp, &vd->allocTempImp);
100 if (res != SZ_OK) {
101 File_Close(&vd->archiveStream.file);
102 free(vd);
103 return 0;
104 }
105
106 vd->dirent.index = -1;
107 vd->dirent.utf8 = 0;
108 vd->dirent.vd = vd;
109 vd->dirent.d.name = _vde7zName;
110 vd->dirent.d.type = _vde7zType;
111
112 vd->d.close = _vd7zClose;
113 vd->d.rewind = _vd7zRewind;
114 vd->d.listNext = _vd7zListNext;
115 vd->d.openFile = _vd7zOpenFile;
116 vd->d.openDir = _vd7zOpenDir;
117 vd->d.deleteFile = _vd7zDeleteFile;
118
119 return &vd->d;
120}
121
122bool _vf7zClose(struct VFile* vf) {
123 struct VFile7z* vf7z = (struct VFile7z*) vf;
124 IAlloc_Free(&vf7z->vd->allocImp, vf7z->outBuffer);
125 return true;
126}
127
128off_t _vf7zSeek(struct VFile* vf, off_t offset, int whence) {
129 struct VFile7z* vf7z = (struct VFile7z*) vf;
130
131 size_t position;
132 switch (whence) {
133 case SEEK_SET:
134 position = offset;
135 break;
136 case SEEK_CUR:
137 if (offset < 0 && ((vf7z->offset < (size_t) -offset) || (offset == INT_MIN))) {
138 return -1;
139 }
140 position = vf7z->offset + offset;
141 break;
142 case SEEK_END:
143 if (offset < 0 && ((vf7z->size < (size_t) -offset) || (offset == INT_MIN))) {
144 return -1;
145 }
146 position = vf7z->size + offset;
147 break;
148 default:
149 return -1;
150 }
151
152 if (position > vf7z->size) {
153 return -1;
154 }
155
156 vf7z->offset = position;
157 return position;
158}
159
160ssize_t _vf7zRead(struct VFile* vf, void* buffer, size_t size) {
161 struct VFile7z* vf7z = (struct VFile7z*) vf;
162
163 if (size + vf7z->offset >= vf7z->size) {
164 size = vf7z->size - vf7z->offset;
165 }
166
167 memcpy(buffer, vf7z->outBuffer + vf7z->offset + vf7z->bufferOffset, size);
168 vf7z->offset += size;
169 return size;
170}
171
172ssize_t _vf7zWrite(struct VFile* vf, const void* buffer, size_t size) {
173 // TODO
174 UNUSED(vf);
175 UNUSED(buffer);
176 UNUSED(size);
177 return -1;
178}
179
180void* _vf7zMap(struct VFile* vf, size_t size, int flags) {
181 struct VFile7z* vf7z = (struct VFile7z*) vf;
182
183 UNUSED(flags);
184 if (size > vf7z->size) {
185 return 0;
186 }
187
188 return vf7z->outBuffer + vf7z->bufferOffset;
189}
190
191void _vf7zUnmap(struct VFile* vf, void* memory, size_t size) {
192 UNUSED(vf);
193 UNUSED(memory);
194 UNUSED(size);
195}
196
197void _vf7zTruncate(struct VFile* vf, size_t size) {
198 // TODO
199 UNUSED(vf);
200 UNUSED(size);
201}
202
203ssize_t _vf7zSize(struct VFile* vf) {
204 struct VFile7z* vf7z = (struct VFile7z*) vf;
205 return vf7z->size;
206}
207
208bool _vd7zClose(struct VDir* vd) {
209 struct VDir7z* vd7z = (struct VDir7z*) vd;
210 SzArEx_Free(&vd7z->db, &vd7z->allocImp);
211 File_Close(&vd7z->archiveStream.file);
212
213 free(vd7z->dirent.utf8);
214 vd7z->dirent.utf8 = 0;
215
216 free(vd7z);
217 return true;
218}
219
220void _vd7zRewind(struct VDir* vd) {
221 struct VDir7z* vd7z = (struct VDir7z*) vd;
222 free(vd7z->dirent.utf8);
223 vd7z->dirent.utf8 = 0;
224 vd7z->dirent.index = -1;
225}
226
227struct VDirEntry* _vd7zListNext(struct VDir* vd) {
228 struct VDir7z* vd7z = (struct VDir7z*) vd;
229 if (vd7z->db.NumFiles <= vd7z->dirent.index + 1) {
230 return 0;
231 }
232 free(vd7z->dirent.utf8);
233 vd7z->dirent.utf8 = 0;
234 ++vd7z->dirent.index;
235 return &vd7z->dirent.d;
236}
237
238struct VFile* _vd7zOpenFile(struct VDir* vd, const char* path, int mode) {
239 UNUSED(mode);
240 // TODO: support truncating, appending and creating, and write
241 struct VDir7z* vd7z = (struct VDir7z*) vd;
242
243 if ((mode & O_RDWR) == O_RDWR) {
244 // Read/Write support is not yet implemented.
245 return 0;
246 }
247
248 if (mode & O_WRONLY) {
249 // Write support is not yet implemented.
250 return 0;
251 }
252
253 size_t pathLength = strlen(path);
254
255 UInt32 i;
256 for (i = 0; i < vd7z->db.NumFiles; ++i) {
257 if (SzArEx_IsDir(&vd7z->db, i)) {
258 continue;
259 }
260 size_t nameLength = SzArEx_GetFileNameUtf16(&vd7z->db, i, 0) * sizeof(UInt16);
261 UInt16* name = malloc(nameLength);
262 SzArEx_GetFileNameUtf16(&vd7z->db, i, name);
263
264 if (utfcmp(name, path, nameLength - sizeof(UInt16), pathLength) == 0) {
265 free(name);
266 break;
267 }
268
269 free(name);
270 }
271
272 if (i == vd7z->db.NumFiles) {
273 return 0; // No file found
274 }
275
276 struct VFile7z* vf = malloc(sizeof(struct VFile7z));
277 vf->vd = vd7z;
278
279 size_t outBufferSize;
280 UInt32 blockIndex;
281
282 vf->outBuffer = 0;
283 SRes res = SzArEx_Extract(&vd7z->db, &vd7z->lookStream.s, i, &blockIndex,
284 &vf->outBuffer, &outBufferSize,
285 &vf->bufferOffset, &vf->size,
286 &vd7z->allocImp, &vd7z->allocTempImp);
287
288 if (res != SZ_OK) {
289 free(vf);
290 return 0;
291 }
292
293 vf->d.close = _vf7zClose;
294 vf->d.seek = _vf7zSeek;
295 vf->d.read = _vf7zRead;
296 vf->d.readline = VFileReadline;
297 vf->d.write = _vf7zWrite;
298 vf->d.map = _vf7zMap;
299 vf->d.unmap = _vf7zUnmap;
300 vf->d.truncate = _vf7zTruncate;
301 vf->d.size = _vf7zSize;
302 vf->d.sync = _vf7zSync;
303 vf->offset = 0;
304
305 return &vf->d;
306}
307
308struct VDir* _vd7zOpenDir(struct VDir* vd, const char* path) {
309 UNUSED(vd);
310 UNUSED(path);
311 return 0;
312}
313
314bool _vd7zDeleteFile(struct VDir* vd, const char* path) {
315 UNUSED(vd);
316 UNUSED(path);
317 // TODO
318 return false;
319}
320
321bool _vf7zSync(struct VFile* vf, const void* memory, size_t size) {
322 UNUSED(vf);
323 UNUSED(memory);
324 UNUSED(size);
325 return false;
326}
327
328const char* _vde7zName(struct VDirEntry* vde) {
329 struct VDirEntry7z* vde7z = (struct VDirEntry7z*) vde;
330 if (!vde7z->utf8) {
331 size_t nameLength = SzArEx_GetFileNameUtf16(&vde7z->vd->db, vde7z->index, 0) * sizeof(UInt16);
332 UInt16* name = malloc(nameLength);
333 SzArEx_GetFileNameUtf16(&vde7z->vd->db, vde7z->index, name);
334 vde7z->utf8 = utf16to8(name, nameLength - sizeof(UInt16));
335 free(name);
336 }
337
338 return vde7z->utf8;
339}
340
341static enum VFSType _vde7zType(struct VDirEntry* vde) {
342 struct VDirEntry7z* vde7z = (struct VDirEntry7z*) vde;
343 if (SzArEx_IsDir(&vde7z->vd->db, vde7z->index)) {
344 return VFS_DIRECTORY;
345 }
346 return VFS_FILE;
347}
348
349#endif