all repos — mgba @ a268d462c0ed4d834cf1035bbb645cea2978c86e

mGBA Game Boy Advance Emulator

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 ENABLE_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 _vf7zReadline(struct VFile* vf, char* buffer, size_t size);
 55static ssize_t _vf7zWrite(struct VFile* vf, const void* buffer, size_t size);
 56static void* _vf7zMap(struct VFile* vf, size_t size, int flags);
 57static void _vf7zUnmap(struct VFile* vf, void* memory, size_t size);
 58static void _vf7zTruncate(struct VFile* vf, size_t size);
 59static ssize_t _vf7zSize(struct VFile* vf);
 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	struct VDir7z* vd = malloc(sizeof(struct VDir7z));
 70
 71	if (flags & O_WRONLY || flags & O_CREAT) {
 72		return 0;
 73	}
 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->offset) {
146		vf7z->offset = position;
147		return position;
148	}
149
150	if (position <= vf7z->size) {
151		return vf7z->offset;
152	}
153
154	return -1;
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	return size;
166}
167
168ssize_t _vf7zReadline(struct VFile* vf, char* buffer, size_t size) {
169	size_t bytesRead = 0;
170	while (bytesRead < size - 1) {
171		size_t newRead = vf->read(vf, &buffer[bytesRead], 1);
172		bytesRead += newRead;
173		if (!newRead || buffer[bytesRead] == '\n') {
174			break;
175		}
176	}
177	return buffer[bytesRead] = '\0';
178}
179
180ssize_t _vf7zWrite(struct VFile* vf, const void* buffer, size_t size) {
181	// TODO
182	UNUSED(vf);
183	UNUSED(buffer);
184	UNUSED(size);
185	return -1;
186}
187
188void* _vf7zMap(struct VFile* vf, size_t size, int flags) {
189	struct VFile7z* vf7z = (struct VFile7z*) vf;
190
191	UNUSED(flags);
192	if (size > vf7z->size) {
193		return 0;
194	}
195
196	return vf7z->outBuffer + vf7z->bufferOffset;
197}
198
199void _vf7zUnmap(struct VFile* vf, void* memory, size_t size) {
200	UNUSED(vf);
201	UNUSED(memory);
202	UNUSED(size);
203}
204
205void _vf7zTruncate(struct VFile* vf, size_t size) {
206	// TODO
207	UNUSED(vf);
208	UNUSED(size);
209}
210
211ssize_t _vf7zSize(struct VFile* vf) {
212	struct VFile7z* vf7z = (struct VFile7z*) vf;
213	return vf7z->size;
214}
215
216bool _vd7zClose(struct VDir* vd) {
217	struct VDir7z* vd7z = (struct VDir7z*) vd;
218	SzArEx_Free(&vd7z->db, &vd7z->allocImp);
219	File_Close(&vd7z->archiveStream.file);
220
221	free(vd7z->dirent.utf8);
222	vd7z->dirent.utf8 = 0;
223
224	free(vd7z);
225	return true;
226}
227
228void _vd7zRewind(struct VDir* vd) {
229	struct VDir7z* vd7z = (struct VDir7z*) vd;
230	free(vd7z->dirent.utf8);
231	vd7z->dirent.utf8 = 0;
232	vd7z->dirent.index = -1;
233}
234
235struct VDirEntry* _vd7zListNext(struct VDir* vd) {
236	struct VDir7z* vd7z = (struct VDir7z*) vd;
237	if (vd7z->db.NumFiles <= vd7z->dirent.index + 1) {
238		return 0;
239	}
240	free(vd7z->dirent.utf8);
241	vd7z->dirent.utf8 = 0;
242	++vd7z->dirent.index;
243	return &vd7z->dirent.d;
244}
245
246struct VFile* _vd7zOpenFile(struct VDir* vd, const char* path, int mode) {
247	UNUSED(mode);
248	// TODO: support truncating, appending and creating, and write
249	struct VDir7z* vd7z = (struct VDir7z*) vd;
250
251	if ((mode & O_RDWR) == O_RDWR) {
252		// Read/Write support is not yet implemented.
253		return 0;
254	}
255
256	if (mode & O_WRONLY) {
257		// Write support is not yet implemented.
258		return 0;
259	}
260
261	size_t pathLength = strlen(path);
262
263	UInt32 i;
264	for (i = 0; i < vd7z->db.NumFiles; ++i) {
265		if (SzArEx_IsDir(&vd7z->db, i)) {
266			continue;
267		}
268		size_t nameLength = SzArEx_GetFileNameUtf16(&vd7z->db, i, 0) * sizeof(UInt16);
269		UInt16* name = malloc(nameLength);
270		SzArEx_GetFileNameUtf16(&vd7z->db, i, name);
271
272		if (utfcmp(name, path, nameLength - sizeof(UInt16), pathLength) == 0) {
273			free(name);
274			break;
275		}
276
277		free(name);
278	}
279
280	if (i == vd7z->db.NumFiles) {
281		return 0; // No file found
282	}
283
284	struct VFile7z* vf = malloc(sizeof(struct VFile7z));
285	vf->vd = vd7z;
286
287	size_t outBufferSize;
288	UInt32 blockIndex;
289
290	vf->outBuffer = 0;
291	SRes res = SzArEx_Extract(&vd7z->db, &vd7z->lookStream.s, i, &blockIndex,
292		&vf->outBuffer, &outBufferSize,
293		&vf->bufferOffset, &vf->size,
294		&vd7z->allocImp, &vd7z->allocTempImp);
295
296	if (res != SZ_OK) {
297		free(vf);
298		return 0;
299	}
300
301	vf->d.close = _vf7zClose;
302	vf->d.seek = _vf7zSeek;
303	vf->d.read = _vf7zRead;
304	vf->d.readline = _vf7zReadline;
305	vf->d.write = _vf7zWrite;
306	vf->d.map = _vf7zMap;
307	vf->d.unmap = _vf7zUnmap;
308	vf->d.truncate = _vf7zTruncate;
309	vf->d.size = _vf7zSize;
310
311	return &vf->d;
312}
313
314const char* _vde7zName(struct VDirEntry* vde) {
315	struct VDirEntry7z* vde7z = (struct VDirEntry7z*) vde;
316	if (!vde7z->utf8) {
317		size_t nameLength = SzArEx_GetFileNameUtf16(&vde7z->vd->db, vde7z->index, 0) * sizeof(UInt16);
318		UInt16* name = malloc(nameLength);
319		SzArEx_GetFileNameUtf16(&vde7z->vd->db, vde7z->index, name);
320		vde7z->utf8 = utf16to8(name, nameLength - sizeof(UInt16));
321		free(name);
322	}
323
324	return vde7z->utf8;
325}
326
327#endif