all repos — mgba @ 3bded6d039240472107d98ff0e95d4c17cc62cbc

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 <mgba-util/vfs.h>
  7
  8#ifdef USE_LZMA
  9
 10#include <mgba-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
 19#define BUFFER_SIZE 0x2000
 20
 21struct VDirEntry7z {
 22	struct VDirEntry d;
 23
 24	struct VDir7z* vd;
 25	UInt32 index;
 26	char* utf8;
 27};
 28
 29struct VDir7z {
 30	struct VDir d;
 31	struct VDirEntry7z dirent;
 32
 33	// What is all this garbage?
 34	CFileInStream archiveStream;
 35	CLookToRead2 lookStream;
 36	CSzArEx db;
 37	ISzAlloc allocImp;
 38	ISzAlloc allocTempImp;
 39};
 40
 41struct VFile7z {
 42	struct VFile d;
 43
 44	struct VDir7z* vd;
 45
 46	size_t offset;
 47
 48	Byte* outBuffer;
 49	size_t bufferOffset;
 50	size_t size;
 51};
 52
 53static bool _vf7zClose(struct VFile* vf);
 54static off_t _vf7zSeek(struct VFile* vf, off_t offset, int whence);
 55static ssize_t _vf7zRead(struct VFile* vf, void* buffer, size_t size);
 56static ssize_t _vf7zWrite(struct VFile* vf, const void* buffer, size_t size);
 57static void* _vf7zMap(struct VFile* vf, size_t size, int flags);
 58static void _vf7zUnmap(struct VFile* vf, void* memory, size_t size);
 59static void _vf7zTruncate(struct VFile* vf, size_t size);
 60static ssize_t _vf7zSize(struct VFile* vf);
 61static bool _vf7zSync(struct VFile* vf, const void* buffer, size_t size);
 62
 63static bool _vd7zClose(struct VDir* vd);
 64static void _vd7zRewind(struct VDir* vd);
 65static struct VDirEntry* _vd7zListNext(struct VDir* vd);
 66static struct VFile* _vd7zOpenFile(struct VDir* vd, const char* path, int mode);
 67static struct VDir* _vd7zOpenDir(struct VDir* vd, const char* path);
 68static bool _vd7zDeleteFile(struct VDir* vd, const char* path);
 69
 70static const char* _vde7zName(struct VDirEntry* vde);
 71static enum VFSType _vde7zType(struct VDirEntry* vde);
 72
 73struct VDir* VDirOpen7z(const char* path, int flags) {
 74	if (flags & O_WRONLY || flags & O_CREAT) {
 75		return 0;
 76	}
 77
 78	struct VDir7z* vd = malloc(sizeof(struct VDir7z));
 79
 80	// What does any of this mean, Igor?
 81	if (InFile_Open(&vd->archiveStream.file, path)) {
 82		free(vd);
 83		return 0;
 84	}
 85
 86	vd->allocImp.Alloc = SzAlloc;
 87	vd->allocImp.Free = SzFree;
 88
 89	vd->allocTempImp.Alloc = SzAllocTemp;
 90	vd->allocTempImp.Free = SzFreeTemp;
 91
 92	FileInStream_CreateVTable(&vd->archiveStream);
 93	LookToRead2_CreateVTable(&vd->lookStream, False);
 94
 95	vd->lookStream.realStream = &vd->archiveStream.vt;
 96	vd->lookStream.buf = malloc(BUFFER_SIZE);
 97	vd->lookStream.bufSize = BUFFER_SIZE;
 98
 99	LookToRead2_Init(&vd->lookStream);
100
101	CrcGenerateTable();
102
103	SzArEx_Init(&vd->db);
104	SRes res = SzArEx_Open(&vd->db, &vd->lookStream.vt, &vd->allocImp, &vd->allocTempImp);
105	if (res != SZ_OK) {
106		SzArEx_Free(&vd->db, &vd->allocImp);
107		File_Close(&vd->archiveStream.file);
108		free(vd);
109		return 0;
110	}
111
112	vd->dirent.index = -1;
113	vd->dirent.utf8 = 0;
114	vd->dirent.vd = vd;
115	vd->dirent.d.name = _vde7zName;
116	vd->dirent.d.type = _vde7zType;
117
118	vd->d.close = _vd7zClose;
119	vd->d.rewind = _vd7zRewind;
120	vd->d.listNext = _vd7zListNext;
121	vd->d.openFile = _vd7zOpenFile;
122	vd->d.openDir = _vd7zOpenDir;
123	vd->d.deleteFile = _vd7zDeleteFile;
124
125	return &vd->d;
126}
127
128bool _vf7zClose(struct VFile* vf) {
129	struct VFile7z* vf7z = (struct VFile7z*) vf;
130	IAlloc_Free(&vf7z->vd->allocImp, vf7z->outBuffer);
131	free(vf7z);
132	return true;
133}
134
135off_t _vf7zSeek(struct VFile* vf, off_t offset, int whence) {
136	struct VFile7z* vf7z = (struct VFile7z*) vf;
137
138	size_t position;
139	switch (whence) {
140	case SEEK_SET:
141		position = offset;
142		break;
143	case SEEK_CUR:
144		if (offset < 0 && ((vf7z->offset < (size_t) -offset) || (offset == INT_MIN))) {
145			return -1;
146		}
147		position = vf7z->offset + offset;
148		break;
149	case SEEK_END:
150		if (offset < 0 && ((vf7z->size < (size_t) -offset) || (offset == INT_MIN))) {
151			return -1;
152		}
153		position = vf7z->size + offset;
154		break;
155	default:
156		return -1;
157	}
158
159	if (position > vf7z->size) {
160		return -1;
161	}
162
163	vf7z->offset = position;
164	return position;
165}
166
167ssize_t _vf7zRead(struct VFile* vf, void* buffer, size_t size) {
168	struct VFile7z* vf7z = (struct VFile7z*) vf;
169
170	if (size + vf7z->offset >= vf7z->size) {
171		size = vf7z->size - vf7z->offset;
172	}
173
174	memcpy(buffer, vf7z->outBuffer + vf7z->offset + vf7z->bufferOffset, size);
175	vf7z->offset += size;
176	return size;
177}
178
179ssize_t _vf7zWrite(struct VFile* vf, const void* buffer, size_t size) {
180	// TODO
181	UNUSED(vf);
182	UNUSED(buffer);
183	UNUSED(size);
184	return -1;
185}
186
187void* _vf7zMap(struct VFile* vf, size_t size, int flags) {
188	struct VFile7z* vf7z = (struct VFile7z*) vf;
189
190	UNUSED(flags);
191	if (size > vf7z->size) {
192		return 0;
193	}
194
195	return vf7z->outBuffer + vf7z->bufferOffset;
196}
197
198void _vf7zUnmap(struct VFile* vf, void* memory, size_t size) {
199	UNUSED(vf);
200	UNUSED(memory);
201	UNUSED(size);
202}
203
204void _vf7zTruncate(struct VFile* vf, size_t size) {
205	// TODO
206	UNUSED(vf);
207	UNUSED(size);
208}
209
210ssize_t _vf7zSize(struct VFile* vf) {
211	struct VFile7z* vf7z = (struct VFile7z*) vf;
212	return vf7z->size;
213}
214
215bool _vd7zClose(struct VDir* vd) {
216	struct VDir7z* vd7z = (struct VDir7z*) vd;
217	SzArEx_Free(&vd7z->db, &vd7z->allocImp);
218	File_Close(&vd7z->archiveStream.file);
219
220	free(vd7z->lookStream.buf);
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.vt, 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 = VFileReadline;
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	vf->d.sync = _vf7zSync;
311	vf->offset = 0;
312
313	return &vf->d;
314}
315
316struct VDir* _vd7zOpenDir(struct VDir* vd, const char* path) {
317	UNUSED(vd);
318	UNUSED(path);
319	return 0;
320}
321
322bool _vd7zDeleteFile(struct VDir* vd, const char* path) {
323	UNUSED(vd);
324	UNUSED(path);
325	// TODO
326	return false;
327}
328
329bool _vf7zSync(struct VFile* vf, const void* memory, size_t size) {
330	UNUSED(vf);
331	UNUSED(memory);
332	UNUSED(size);
333	return false;
334}
335
336const char* _vde7zName(struct VDirEntry* vde) {
337	struct VDirEntry7z* vde7z = (struct VDirEntry7z*) vde;
338	if (!vde7z->utf8) {
339		size_t nameLength = SzArEx_GetFileNameUtf16(&vde7z->vd->db, vde7z->index, 0) * sizeof(UInt16);
340		UInt16* name = malloc(nameLength);
341		SzArEx_GetFileNameUtf16(&vde7z->vd->db, vde7z->index, name);
342		vde7z->utf8 = utf16to8(name, nameLength - sizeof(UInt16));
343		free(name);
344	}
345
346	return vde7z->utf8;
347}
348
349static enum VFSType _vde7zType(struct VDirEntry* vde) {
350	struct VDirEntry7z* vde7z = (struct VDirEntry7z*) vde;
351	if (SzArEx_IsDir(&vde7z->vd->db, vde7z->index)) {
352		return VFS_DIRECTORY;
353	}
354	return VFS_FILE;
355}
356
357#endif