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