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