Skip to content

Latest commit

 

History

History
430 lines (336 loc) · 11.6 KB

physfs_archiver_7z.c

File metadata and controls

430 lines (336 loc) · 11.6 KB
 
Jul 17, 2017
Jul 17, 2017
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* 7zip support routines for PhysicsFS.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file was written by Ryan C. Gordon.
*/
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
#if PHYSFS_SUPPORTS_7Z
#include "physfs_lzmasdk.h"
typedef struct
{
ISeekInStream seekStream; /* lzma sdk i/o interface (lower level). */
PHYSFS_Io *io; /* physfs i/o interface for this archive. */
CLookToRead lookStream; /* lzma sdk i/o interface (higher level). */
} SZIPLookToRead;
/* One SZIPentry is kept for each file in an open 7zip archive. */
typedef struct
{
__PHYSFS_DirTreeEntry tree; /* manages directory tree */
PHYSFS_uint32 dbidx; /* index into lzma sdk database */
} SZIPentry;
/* One SZIPinfo is kept for each open 7zip archive. */
typedef struct
{
__PHYSFS_DirTree tree; /* manages directory tree. */
PHYSFS_Io *io; /* physfs i/o interface for this archive. */
CSzArEx db; /* lzma sdk archive database object. */
} SZIPinfo;
static PHYSFS_ErrorCode szipErrorCode(const SRes rc)
{
switch (rc)
{
case SZ_OK: return PHYSFS_ERR_OK;
case SZ_ERROR_DATA: return PHYSFS_ERR_CORRUPT;
case SZ_ERROR_MEM: return PHYSFS_ERR_OUT_OF_MEMORY;
case SZ_ERROR_CRC: return PHYSFS_ERR_CORRUPT;
case SZ_ERROR_UNSUPPORTED: return PHYSFS_ERR_UNSUPPORTED;
case SZ_ERROR_INPUT_EOF: return PHYSFS_ERR_CORRUPT;
case SZ_ERROR_OUTPUT_EOF: return PHYSFS_ERR_IO;
case SZ_ERROR_READ: return PHYSFS_ERR_IO;
case SZ_ERROR_WRITE: return PHYSFS_ERR_IO;
case SZ_ERROR_ARCHIVE: return PHYSFS_ERR_CORRUPT;
case SZ_ERROR_NO_ARCHIVE: return PHYSFS_ERR_UNSUPPORTED;
default: break;
} /* switch */
return PHYSFS_ERR_OTHER_ERROR;
} /* szipErrorCode */
/* LZMA SDK's ISzAlloc interface ... */
static void *SZIP_ISzAlloc_Alloc(void *p, size_t size)
{
return allocator.Malloc(size ? size : 1);
} /* SZIP_ISzAlloc_Alloc */
static void SZIP_ISzAlloc_Free(void *p, void *address)
{
if (address)
allocator.Free(address);
} /* SZIP_ISzAlloc_Free */
static ISzAlloc SZIP_SzAlloc = {
SZIP_ISzAlloc_Alloc, SZIP_ISzAlloc_Free
};
/* we implement ISeekInStream, and then wrap that in LZMA SDK's CLookToRead,
which implements the higher-level ILookInStream on top of that, handling
buffering and such for us. */
/* LZMA SDK's ISeekInStream interface ... */
static SRes SZIP_ISeekInStream_Read(void *p, void *buf, size_t *size)
{
SZIPLookToRead *stream = (SZIPLookToRead *) p;
PHYSFS_Io *io = stream->io;
const PHYSFS_uint64 len = (PHYSFS_uint64) *size;
const PHYSFS_sint64 rc = (len == 0) ? 0 : io->read(io, buf, len);
if (rc < 0)
{
*size = 0;
return SZ_ERROR_READ;
} /* if */
*size = (size_t) rc;
return SZ_OK;
} /* SZIP_ISeekInStream_Read */
static SRes SZIP_ISeekInStream_Seek(void *p, Int64 *pos, ESzSeek origin)
{
SZIPLookToRead *stream = (SZIPLookToRead *) p;
PHYSFS_Io *io = stream->io;
PHYSFS_sint64 base;
PHYSFS_uint64 newpos;
switch (origin)
{
case SZ_SEEK_SET:
base = 0;
break;
case SZ_SEEK_CUR:
base = io->tell(io);
break;
case SZ_SEEK_END:
base = io->length(io);
break;
default:
return SZ_ERROR_FAIL;
} /* switch */
if (base < 0)
return SZ_ERROR_FAIL;
else if ((*pos < 0) && (((Int64) base) < -*pos))
return SZ_ERROR_FAIL;
newpos = (PHYSFS_uint64) (((Int64) base) + *pos);
if (!io->seek(io, newpos))
return SZ_ERROR_FAIL;
*pos = (Int64) newpos;
return SZ_OK;
} /* SZIP_ISeekInStream_Seek */
static void szipInitStream(SZIPLookToRead *stream, PHYSFS_Io *io)
{
stream->seekStream.Read = SZIP_ISeekInStream_Read;
stream->seekStream.Seek = SZIP_ISeekInStream_Seek;
stream->io = io;
/* !!! FIXME: can we use lookahead? Is there value to it? */
LookToRead_Init(&stream->lookStream);
LookToRead_CreateVTable(&stream->lookStream, False);
stream->lookStream.realStream = &stream->seekStream;
} /* szipInitStream */
/* Do this in a separate function so we can smallAlloc without looping. */
static int szipLoadEntry(SZIPinfo *info, const PHYSFS_uint32 idx)
{
const size_t utf16len = SzArEx_GetFileNameUtf16(&info->db, idx, NULL);
const size_t utf16buflen = utf16len * 2;
PHYSFS_uint16 *utf16 = (PHYSFS_uint16 *) __PHYSFS_smallAlloc(utf16buflen);
const size_t utf8buflen = utf16len * 4;
char *utf8 = (char *) __PHYSFS_smallAlloc(utf8buflen);
int retval = 0;
if (utf16 && utf8)
{
const int isdir = SzArEx_IsDir(&info->db, idx) != 0;
SZIPentry *entry;
SzArEx_GetFileNameUtf16(&info->db, idx, (UInt16 *) utf16);
PHYSFS_utf8FromUtf16(utf16, utf8, utf8buflen);
entry = (SZIPentry*) __PHYSFS_DirTreeAdd(&info->tree, utf8, isdir);
retval = (entry != NULL);
if (retval)
entry->dbidx = idx;
} /* if */
__PHYSFS_smallFree(utf8);
__PHYSFS_smallFree(utf16);
return retval;
} /* szipLoadEntry */
static int szipLoadEntries(SZIPinfo *info)
{
int retval = 0;
Jul 21, 2017
Jul 21, 2017
188
if (__PHYSFS_DirTreeInit(&info->tree, sizeof (SZIPentry)))
Jul 17, 2017
Jul 17, 2017
189
{
Jul 21, 2017
Jul 21, 2017
190
const PHYSFS_uint32 count = info->db.NumFiles;
Jul 17, 2017
Jul 17, 2017
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
PHYSFS_uint32 i;
for (i = 0; i < count; i++)
BAIL_IF_ERRPASS(!szipLoadEntry(info, i), 0);
retval = 1;
} /* if */
return retval;
} /* szipLoadEntries */
static void SZIP_closeArchive(void *opaque)
{
SZIPinfo *info = (SZIPinfo *) opaque;
if (info)
{
Mar 8, 2018
Mar 8, 2018
206
207
if (info->io)
info->io->destroy(info->io);
Jul 17, 2017
Jul 17, 2017
208
209
210
211
212
213
214
SzArEx_Free(&info->db, &SZIP_SzAlloc);
__PHYSFS_DirTreeDeinit(&info->tree);
allocator.Free(info);
} /* if */
} /* SZIP_closeArchive */
Aug 14, 2017
Aug 14, 2017
215
216
static void *SZIP_openArchive(PHYSFS_Io *io, const char *name,
int forWriting, int *claimed)
Jul 17, 2017
Jul 17, 2017
217
{
Aug 14, 2017
Aug 14, 2017
218
static const PHYSFS_uint8 wantedsig[] = { '7','z',0xBC,0xAF,0x27,0x1C };
Jul 17, 2017
Jul 17, 2017
219
220
221
222
SZIPLookToRead stream;
ISzAlloc *alloc = &SZIP_SzAlloc;
SZIPinfo *info = NULL;
SRes rc;
Aug 14, 2017
Aug 14, 2017
223
224
PHYSFS_uint8 sig[6];
PHYSFS_sint64 pos;
Jul 17, 2017
Jul 17, 2017
225
226
BAIL_IF(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
Aug 14, 2017
Aug 14, 2017
227
228
229
230
231
pos = io->tell(io);
BAIL_IF_ERRPASS(pos == -1, NULL);
BAIL_IF_ERRPASS(io->read(io, sig, 6) != 6, NULL);
*claimed = (memcmp(sig, wantedsig, 6) == 0);
BAIL_IF_ERRPASS(!io->seek(io, pos), NULL);
Jul 17, 2017
Jul 17, 2017
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
info = (SZIPinfo *) allocator.Malloc(sizeof (SZIPinfo));
BAIL_IF(!info, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
memset(info, '\0', sizeof (*info));
SzArEx_Init(&info->db);
info->io = io;
szipInitStream(&stream, io);
rc = SzArEx_Open(&info->db, &stream.lookStream.s, alloc, alloc);
GOTO_IF(rc != SZ_OK, szipErrorCode(rc), failed);
GOTO_IF_ERRPASS(!szipLoadEntries(info), failed);
return info;
failed:
info->io = NULL; /* don't let cleanup destroy the PHYSFS_Io. */
SZIP_closeArchive(info);
return NULL;
} /* SZIP_openArchive */
static PHYSFS_Io *SZIP_openRead(void *opaque, const char *path)
{
/* !!! FIXME: the current lzma sdk C API only allows you to decompress
!!! FIXME: the entire file at once, which isn't ideal. Fix this in the
!!! FIXME: SDK and then convert this all to a streaming interface. */
SZIPinfo *info = (SZIPinfo *) opaque;
SZIPentry *entry = (SZIPentry *) __PHYSFS_DirTreeFind(&info->tree, path);
ISzAlloc *alloc = &SZIP_SzAlloc;
SZIPLookToRead stream;
PHYSFS_Io *retval = NULL;
PHYSFS_Io *io = NULL;
UInt32 blockIndex = 0xFFFFFFFF;
Byte *outBuffer = NULL;
size_t outBufferSize = 0;
size_t offset = 0;
size_t outSizeProcessed = 0;
void *buf = NULL;
SRes rc;
BAIL_IF_ERRPASS(!entry, NULL);
BAIL_IF(entry->tree.isdir, PHYSFS_ERR_NOT_A_FILE, NULL);
io = info->io->duplicate(info->io);
GOTO_IF_ERRPASS(!io, SZIP_openRead_failed);
szipInitStream(&stream, io);
rc = SzArEx_Extract(&info->db, &stream.lookStream.s, entry->dbidx,
&blockIndex, &outBuffer, &outBufferSize, &offset,
&outSizeProcessed, alloc, alloc);
GOTO_IF(rc != SZ_OK, szipErrorCode(rc), SZIP_openRead_failed);
io->destroy(io);
io = NULL;
buf = allocator.Malloc(outSizeProcessed);
GOTO_IF(rc != SZ_OK, PHYSFS_ERR_OUT_OF_MEMORY, SZIP_openRead_failed);
memcpy(buf, outBuffer + offset, outSizeProcessed);
alloc->Free(alloc, outBuffer);
outBuffer = NULL;
retval = __PHYSFS_createMemoryIo(buf, outSizeProcessed, allocator.Free);
GOTO_IF_ERRPASS(!retval, SZIP_openRead_failed);
return retval;
SZIP_openRead_failed:
if (io != NULL)
io->destroy(io);
if (buf)
allocator.Free(buf);
if (outBuffer)
alloc->Free(alloc, outBuffer);
return NULL;
} /* SZIP_openRead */
static PHYSFS_Io *SZIP_openWrite(void *opaque, const char *filename)
{
BAIL(PHYSFS_ERR_READ_ONLY, NULL);
} /* SZIP_openWrite */
static PHYSFS_Io *SZIP_openAppend(void *opaque, const char *filename)
{
BAIL(PHYSFS_ERR_READ_ONLY, NULL);
} /* SZIP_openAppend */
static int SZIP_remove(void *opaque, const char *name)
{
BAIL(PHYSFS_ERR_READ_ONLY, 0);
} /* SZIP_remove */
static int SZIP_mkdir(void *opaque, const char *name)
{
BAIL(PHYSFS_ERR_READ_ONLY, 0);
} /* SZIP_mkdir */
static inline PHYSFS_uint64 lzmasdkTimeToPhysfsTime(const CNtfsFileTime *t)
{
const PHYSFS_uint64 winEpochToUnixEpoch = __PHYSFS_UI64(0x019DB1DED53E8000);
const PHYSFS_uint64 nanosecToMillisec = __PHYSFS_UI64(10000000);
const PHYSFS_uint64 quad = (((PHYSFS_uint64) t->High) << 32) | t->Low;
return (quad - winEpochToUnixEpoch) / nanosecToMillisec;
} /* lzmasdkTimeToPhysfsTime */
static int SZIP_stat(void *opaque, const char *path, PHYSFS_Stat *stat)
{
SZIPinfo *info = (SZIPinfo *) opaque;
SZIPentry *entry;
PHYSFS_uint32 idx;
entry = (SZIPentry *) __PHYSFS_DirTreeFind(&info->tree, path);
BAIL_IF_ERRPASS(!entry, 0);
idx = entry->dbidx;
if (entry->tree.isdir)
{
stat->filesize = -1;
stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
} /* if */
else
{
stat->filesize = (PHYSFS_sint64) SzArEx_GetFileSize(&info->db, idx);
stat->filetype = PHYSFS_FILETYPE_REGULAR;
} /* else */
if (info->db.MTime.Vals != NULL)
stat->modtime = lzmasdkTimeToPhysfsTime(&info->db.MTime.Vals[idx]);
else if (info->db.CTime.Vals != NULL)
stat->modtime = lzmasdkTimeToPhysfsTime(&info->db.CTime.Vals[idx]);
else
stat->modtime = -1;
if (info->db.CTime.Vals != NULL)
stat->createtime = lzmasdkTimeToPhysfsTime(&info->db.CTime.Vals[idx]);
else if (info->db.MTime.Vals != NULL)
stat->createtime = lzmasdkTimeToPhysfsTime(&info->db.MTime.Vals[idx]);
else
stat->createtime = -1;
stat->accesstime = -1;
stat->readonly = 1;
return 1;
} /* SZIP_stat */
Aug 14, 2017
Aug 14, 2017
393
394
395
396
397
398
399
400
401
402
403
404
405
void SZIP_global_init(void)
{
/* this just needs to calculate some things, so it only ever
has to run once, even after a deinit. */
static int generatedTable = 0;
if (!generatedTable)
{
generatedTable = 1;
CrcGenerateTable();
} /* if */
} /* SZIP_global_init */
Jul 22, 2017
Jul 22, 2017
406
const PHYSFS_Archiver __PHYSFS_Archiver_7Z =
Jul 17, 2017
Jul 17, 2017
407
408
409
410
411
412
413
414
415
416
{
CURRENT_PHYSFS_ARCHIVER_API_VERSION,
{
"7Z",
"7zip archives",
"Ryan C. Gordon <icculus@icculus.org>",
"https://icculus.org/physfs/",
0, /* supportsSymlinks */
},
SZIP_openArchive,
Aug 12, 2017
Aug 12, 2017
417
__PHYSFS_DirTreeEnumerate,
Jul 17, 2017
Jul 17, 2017
418
419
420
421
422
423
424
425
426
427
428
SZIP_openRead,
SZIP_openWrite,
SZIP_openAppend,
SZIP_remove,
SZIP_mkdir,
SZIP_stat,
SZIP_closeArchive
};
#endif /* defined PHYSFS_SUPPORTS_7Z */
Jul 22, 2017
Jul 22, 2017
429
/* end of physfs_archiver_7z.c ... */