lzma.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. /*
  2. * LZMA support routines for PhysicsFS.
  3. *
  4. * Please see the file lzma.txt in the lzma/ directory.
  5. *
  6. * This file was written by Dennis Schridde, with some peeking at "7zMain.c"
  7. * by Igor Pavlov.
  8. */
  9. #if (defined PHYSFS_SUPPORTS_7Z)
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <time.h>
  13. #include "physfs.h"
  14. #define __PHYSICSFS_INTERNAL__
  15. #include "physfs_internal.h"
  16. #include "lzma/C/7zCrc.h"
  17. #include "lzma/C/Archive/7z/7zIn.h"
  18. #include "lzma/C/Archive/7z/7zExtract.h"
  19. /* 7z internal from 7zIn.c */
  20. extern int TestSignatureCandidate(Byte *testBytes);
  21. #ifdef _LZMA_IN_CB
  22. # define BUFFER_SIZE (1 << 12)
  23. #endif /* _LZMA_IN_CB */
  24. /*
  25. * Carries filestream metadata through 7z
  26. */
  27. typedef struct _FileInputStream
  28. {
  29. ISzAlloc allocImp; /* Allocation implementation, used by 7z */
  30. ISzAlloc allocTempImp; /* Temporary allocation implementation, used by 7z */
  31. ISzInStream inStream; /* Input stream with read callbacks, used by 7z */
  32. void *file; /* Filehandle, used by read implementation */
  33. #ifdef _LZMA_IN_CB
  34. Byte buffer[BUFFER_SIZE]; /* Buffer, used by read implementation */
  35. #endif /* _LZMA_IN_CB */
  36. } FileInputStream;
  37. /*
  38. * In the 7z format archives are splited into blocks, those are called folders
  39. * Set by LZMA_read()
  40. */
  41. typedef struct _LZMAfolder
  42. {
  43. PHYSFS_uint32 index; /* Index of folder in archive */
  44. PHYSFS_uint32 references; /* Number of files using this block */
  45. PHYSFS_uint8 *cache; /* Cached folder */
  46. size_t size; /* Size of folder */
  47. } LZMAfolder;
  48. /*
  49. * Set by LZMA_openArchive(), except folder which gets it's values
  50. * in LZMA_read()
  51. */
  52. typedef struct _LZMAarchive
  53. {
  54. struct _LZMAfile *files; /* Array of files, size == archive->db.Database.NumFiles */
  55. LZMAfolder *folders; /* Array of folders, size == archive->db.Database.NumFolders */
  56. CArchiveDatabaseEx db; /* For 7z: Database */
  57. FileInputStream stream; /* For 7z: Input file incl. read and seek callbacks */
  58. } LZMAarchive;
  59. /* Set by LZMA_openArchive(), except offset which is set by LZMA_read() */
  60. typedef struct _LZMAfile
  61. {
  62. PHYSFS_uint32 index; /* Index of file in archive */
  63. LZMAarchive *archive; /* Link to corresponding archive */
  64. LZMAfolder *folder; /* Link to corresponding folder */
  65. CFileItem *item; /* For 7z: File info, eg. name, size */
  66. size_t offset; /* Offset in folder */
  67. size_t position; /* Current "virtual" position in file */
  68. } LZMAfile;
  69. /* Memory management implementations to be passed to 7z */
  70. static void *SzAllocPhysicsFS(size_t size)
  71. {
  72. return ((size == 0) ? NULL : allocator.Malloc(size));
  73. } /* SzAllocPhysicsFS */
  74. static void SzFreePhysicsFS(void *address)
  75. {
  76. if (address != NULL)
  77. allocator.Free(address);
  78. } /* SzFreePhysicsFS */
  79. /* Filesystem implementations to be passed to 7z */
  80. #ifdef _LZMA_IN_CB
  81. /*
  82. * Read implementation, to be passed to 7z
  83. * WARNING: If the ISzInStream in 'object' is not contained in a valid FileInputStream this _will_ break horribly!
  84. */
  85. SZ_RESULT SzFileReadImp(void *object, void **buffer, size_t maxReqSize,
  86. size_t *processedSize)
  87. {
  88. FileInputStream *s = (FileInputStream *)(object - offsetof(FileInputStream, inStream)); /* HACK! */
  89. PHYSFS_sint64 processedSizeLoc = 0;
  90. if (maxReqSize > BUFFER_SIZE)
  91. maxReqSize = BUFFER_SIZE;
  92. processedSizeLoc = __PHYSFS_platformRead(s->file, s->buffer, 1, maxReqSize);
  93. *buffer = s->buffer;
  94. if (processedSize != NULL)
  95. *processedSize = (size_t) processedSizeLoc;
  96. return SZ_OK;
  97. } /* SzFileReadImp */
  98. #else
  99. /*
  100. * Read implementation, to be passed to 7z
  101. * WARNING: If the ISzInStream in 'object' is not contained in a valid FileInputStream this _will_ break horribly!
  102. */
  103. SZ_RESULT SzFileReadImp(void *object, void *buffer, size_t size,
  104. size_t *processedSize)
  105. {
  106. FileInputStream *s = (FileInputStream *)((unsigned long)object - offsetof(FileInputStream, inStream)); /* HACK! */
  107. size_t processedSizeLoc = __PHYSFS_platformRead(s->file, buffer, 1, size);
  108. if (processedSize != 0)
  109. *processedSize = processedSizeLoc;
  110. return SZ_OK;
  111. } /* SzFileReadImp */
  112. #endif
  113. /*
  114. * Seek implementation, to be passed to 7z
  115. * WARNING: If the ISzInStream in 'object' is not contained in a valid FileInputStream this _will_ break horribly!
  116. */
  117. SZ_RESULT SzFileSeekImp(void *object, CFileSize pos)
  118. {
  119. FileInputStream *s = (FileInputStream *)((unsigned long)object - offsetof(FileInputStream, inStream)); /* HACK! */
  120. if (__PHYSFS_platformSeek(s->file, (PHYSFS_uint64) pos))
  121. return SZ_OK;
  122. return SZE_FAIL;
  123. } /* SzFileSeekImp */
  124. /*
  125. * Translate Microsoft FILETIME (used by 7zip) into UNIX timestamp
  126. */
  127. static PHYSFS_sint64 lzma_filetime_to_unix_timestamp(CArchiveFileTime *ft)
  128. {
  129. /* MS counts in nanoseconds ... */
  130. const PHYSFS_uint64 FILETIME_NANOTICKS_PER_SECOND = __PHYSFS_UI64(10000000);
  131. /* MS likes to count seconds since 01.01.1601 ... */
  132. const PHYSFS_uint64 FILETIME_UNIX_DIFF = __PHYSFS_UI64(11644473600);
  133. PHYSFS_uint64 filetime = ft->Low | ((PHYSFS_uint64)ft->High << 32);
  134. return filetime/FILETIME_NANOTICKS_PER_SECOND - FILETIME_UNIX_DIFF;
  135. } /* lzma_filetime_to_unix_timestamp */
  136. /*
  137. * Compare a file with a given name, C89 stdlib variant
  138. * Used for sorting
  139. */
  140. static int lzma_file_cmp_stdlib(const void *key, const void *object)
  141. {
  142. const char *name = (const char *) key;
  143. LZMAfile *file = (LZMAfile *) object;
  144. return(strcmp(name, file->item->Name));
  145. } /* lzma_file_cmp_posix */
  146. /*
  147. * Compare two files with each other based on the name
  148. * Used for sorting
  149. */
  150. static int lzma_file_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
  151. {
  152. LZMAfile *files = (LZMAfile *) _a;
  153. return(strcmp(files[one].item->Name, files[two].item->Name));
  154. } /* lzma_file_cmp */
  155. /*
  156. * Swap two entries in the file array
  157. */
  158. static void lzma_file_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
  159. {
  160. LZMAfile tmp;
  161. LZMAfile *first = &(((LZMAfile *) _a)[one]);
  162. LZMAfile *second = &(((LZMAfile *) _a)[two]);
  163. memcpy(&tmp, first, sizeof (LZMAfile));
  164. memcpy(first, second, sizeof (LZMAfile));
  165. memcpy(second, &tmp, sizeof (LZMAfile));
  166. } /* lzma_file_swap */
  167. /*
  168. * Find entry 'name' in 'archive'
  169. */
  170. static LZMAfile * lzma_find_file(LZMAarchive *archive, const char *name)
  171. {
  172. LZMAfile *file = bsearch(name, archive->files, archive->db.Database.NumFiles, sizeof(*archive->files), lzma_file_cmp_stdlib); /* FIXME: Should become __PHYSFS_search!!! */
  173. BAIL_IF_MACRO(file == NULL, ERR_NO_SUCH_FILE, NULL);
  174. return(file);
  175. } /* lzma_find_file */
  176. /*
  177. * Load metadata for the file at given index
  178. */
  179. static int lzma_file_init(LZMAarchive *archive, PHYSFS_uint32 fileIndex)
  180. {
  181. LZMAfile *file = &archive->files[fileIndex];
  182. PHYSFS_uint32 folderIndex = archive->db.FileIndexToFolderIndexMap[fileIndex];
  183. file->index = fileIndex; /* Store index into 7z array, since we sort our own. */
  184. file->archive = archive;
  185. file->folder = (folderIndex != (PHYSFS_uint32)-1 ? &archive->folders[folderIndex] : NULL); /* Directories don't have a folder (they contain no own data...) */
  186. file->item = &archive->db.Database.Files[fileIndex]; /* Holds crucial data and is often referenced -> Store link */
  187. file->position = 0;
  188. file->offset = 0; /* Offset will be set by LZMA_read() */
  189. return(1);
  190. } /* lzma_load_file */
  191. /*
  192. * Load metadata for all files
  193. */
  194. static int lzma_files_init(LZMAarchive *archive)
  195. {
  196. PHYSFS_uint32 fileIndex = 0, numFiles = archive->db.Database.NumFiles;
  197. for (fileIndex = 0; fileIndex < numFiles; fileIndex++ )
  198. {
  199. if (!lzma_file_init(archive, fileIndex))
  200. {
  201. return(0); /* FALSE on failure */
  202. }
  203. } /* for */
  204. __PHYSFS_sort(archive->files, numFiles, lzma_file_cmp, lzma_file_swap);
  205. return(1);
  206. } /* lzma_load_files */
  207. /*
  208. * Initialise specified archive
  209. */
  210. static void lzma_archive_init(LZMAarchive *archive)
  211. {
  212. memset(archive, 0, sizeof(*archive));
  213. /* Prepare callbacks for 7z */
  214. archive->stream.inStream.Read = SzFileReadImp;
  215. archive->stream.inStream.Seek = SzFileSeekImp;
  216. archive->stream.allocImp.Alloc = SzAllocPhysicsFS;
  217. archive->stream.allocImp.Free = SzFreePhysicsFS;
  218. archive->stream.allocTempImp.Alloc = SzAllocPhysicsFS;
  219. archive->stream.allocTempImp.Free = SzFreePhysicsFS;
  220. }
  221. /*
  222. * Deinitialise archive
  223. */
  224. static void lzma_archive_exit(LZMAarchive *archive)
  225. {
  226. /* Free arrays */
  227. allocator.Free(archive->folders);
  228. allocator.Free(archive->files);
  229. allocator.Free(archive);
  230. }
  231. /*
  232. * Wrap all 7z calls in this, so the physfs error state is set appropriately.
  233. */
  234. static int lzma_err(SZ_RESULT rc)
  235. {
  236. switch (rc)
  237. {
  238. case SZ_OK: /* Same as LZMA_RESULT_OK */
  239. break;
  240. case SZE_DATA_ERROR: /* Same as LZMA_RESULT_DATA_ERROR */
  241. __PHYSFS_setError(ERR_DATA_ERROR);
  242. break;
  243. case SZE_OUTOFMEMORY:
  244. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  245. break;
  246. case SZE_CRC_ERROR:
  247. __PHYSFS_setError(ERR_CORRUPTED);
  248. break;
  249. case SZE_NOTIMPL:
  250. __PHYSFS_setError(ERR_NOT_IMPLEMENTED);
  251. break;
  252. case SZE_FAIL:
  253. __PHYSFS_setError(ERR_UNKNOWN_ERROR); /* !!! FIXME: right? */
  254. break;
  255. case SZE_ARCHIVE_ERROR:
  256. __PHYSFS_setError(ERR_CORRUPTED); /* !!! FIXME: right? */
  257. break;
  258. default:
  259. __PHYSFS_setError(ERR_UNKNOWN_ERROR);
  260. } /* switch */
  261. return(rc);
  262. } /* lzma_err */
  263. static PHYSFS_sint64 LZMA_read(fvoid *opaque, void *outBuffer,
  264. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  265. {
  266. LZMAfile *file = (LZMAfile *) opaque;
  267. size_t wantedSize = objSize*objCount;
  268. size_t remainingSize = file->item->Size - file->position;
  269. size_t fileSize = 0;
  270. BAIL_IF_MACRO(wantedSize == 0, NULL, 0); /* quick rejection. */
  271. BAIL_IF_MACRO(remainingSize == 0, ERR_PAST_EOF, 0);
  272. if (remainingSize < wantedSize)
  273. {
  274. wantedSize = remainingSize - (remainingSize % objSize);
  275. objCount = (PHYSFS_uint32) (remainingSize / objSize);
  276. BAIL_IF_MACRO(objCount == 0, ERR_PAST_EOF, 0); /* quick rejection. */
  277. __PHYSFS_setError(ERR_PAST_EOF); /* this is always true here. */
  278. } /* if */
  279. /* Only decompress the folder if it is not allready cached */
  280. if (file->folder->cache == NULL)
  281. {
  282. int rc = lzma_err(SzExtract(
  283. &file->archive->stream.inStream, /* compressed data */
  284. &file->archive->db, /* 7z's database, containing everything */
  285. file->index, /* Index into database arrays */
  286. /* Index of cached folder, will be changed by SzExtract */
  287. &file->folder->index,
  288. /* Cache for decompressed folder, allocated/freed by SzExtract */
  289. &file->folder->cache,
  290. /* Size of cache, will be changed by SzExtract */
  291. &file->folder->size,
  292. /* Offset of this file inside the cache, set by SzExtract */
  293. &file->offset,
  294. &fileSize, /* Size of this file */
  295. &file->archive->stream.allocImp,
  296. &file->archive->stream.allocTempImp));
  297. if (rc != SZ_OK)
  298. return -1;
  299. } /* if */
  300. /* Copy wanted bytes over from cache to outBuffer */
  301. memcpy(outBuffer,
  302. (file->folder->cache +
  303. file->offset + file->position),
  304. wantedSize);
  305. file->position += wantedSize; /* Increase virtual position */
  306. return objCount;
  307. } /* LZMA_read */
  308. static PHYSFS_sint64 LZMA_write(fvoid *opaque, const void *buf,
  309. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  310. {
  311. BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
  312. } /* LZMA_write */
  313. static int LZMA_eof(fvoid *opaque)
  314. {
  315. LZMAfile *file = (LZMAfile *) opaque;
  316. return (file->position >= file->item->Size);
  317. } /* LZMA_eof */
  318. static PHYSFS_sint64 LZMA_tell(fvoid *opaque)
  319. {
  320. LZMAfile *file = (LZMAfile *) opaque;
  321. return (file->position);
  322. } /* LZMA_tell */
  323. static int LZMA_seek(fvoid *opaque, PHYSFS_uint64 offset)
  324. {
  325. LZMAfile *file = (LZMAfile *) opaque;
  326. BAIL_IF_MACRO(offset < 0, ERR_SEEK_OUT_OF_RANGE, 0);
  327. BAIL_IF_MACRO(offset > file->item->Size, ERR_PAST_EOF, 0);
  328. file->position = offset; /* We only use a virtual position... */
  329. return 1;
  330. } /* LZMA_seek */
  331. static PHYSFS_sint64 LZMA_fileLength(fvoid *opaque)
  332. {
  333. LZMAfile *file = (LZMAfile *) opaque;
  334. return (file->item->Size);
  335. } /* LZMA_fileLength */
  336. static int LZMA_fileClose(fvoid *opaque)
  337. {
  338. LZMAfile *file = (LZMAfile *) opaque;
  339. BAIL_IF_MACRO(file->folder == NULL, ERR_NOT_A_FILE, 0);
  340. /* Only decrease refcount if someone actually requested this file... Prevents from overflows and close-on-open... */
  341. if (file->folder->references > 0)
  342. file->folder->references--;
  343. if (file->folder->references == 0)
  344. {
  345. /* Free the cache which might have been allocated by LZMA_read() */
  346. allocator.Free(file->folder->cache);
  347. file->folder->cache = NULL;
  348. }
  349. return(1);
  350. } /* LZMA_fileClose */
  351. static int LZMA_isArchive(const char *filename, int forWriting)
  352. {
  353. PHYSFS_uint8 sig[k7zSignatureSize];
  354. void *in;
  355. BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
  356. in = __PHYSFS_platformOpenRead(filename);
  357. BAIL_IF_MACRO(in == NULL, NULL, 0);
  358. /* Read signature bytes */
  359. if (__PHYSFS_platformRead(in, sig, k7zSignatureSize, 1) != 1)
  360. {
  361. __PHYSFS_platformClose(in); /* Don't forget to close the file before returning... */
  362. BAIL_MACRO(NULL, 0);
  363. }
  364. __PHYSFS_platformClose(in);
  365. /* Test whether sig is the 7z signature */
  366. return(TestSignatureCandidate(sig));
  367. } /* LZMA_isArchive */
  368. static void *LZMA_openArchive(const char *name, int forWriting)
  369. {
  370. size_t len = 0;
  371. LZMAarchive *archive = NULL;
  372. BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, NULL);
  373. BAIL_IF_MACRO(!LZMA_isArchive(name,forWriting), ERR_UNSUPPORTED_ARCHIVE, 0);
  374. archive = (LZMAarchive *) allocator.Malloc(sizeof (LZMAarchive));
  375. BAIL_IF_MACRO(archive == NULL, ERR_OUT_OF_MEMORY, NULL);
  376. lzma_archive_init(archive);
  377. if ( (archive->stream.file = __PHYSFS_platformOpenRead(name)) == NULL )
  378. {
  379. __PHYSFS_platformClose(archive->stream.file);
  380. lzma_archive_exit(archive);
  381. return(NULL); /* Error is set by platformOpenRead! */
  382. }
  383. CrcGenerateTable();
  384. SzArDbExInit(&archive->db);
  385. if (lzma_err(SzArchiveOpen(&archive->stream.inStream,
  386. &archive->db,
  387. &archive->stream.allocImp,
  388. &archive->stream.allocTempImp)) != SZ_OK)
  389. {
  390. SzArDbExFree(&archive->db, SzFreePhysicsFS);
  391. __PHYSFS_platformClose(archive->stream.file);
  392. lzma_archive_exit(archive);
  393. return NULL; /* Error is set by lzma_err! */
  394. } /* if */
  395. len = archive->db.Database.NumFiles * sizeof (LZMAfile);
  396. archive->files = (LZMAfile *) allocator.Malloc(len);
  397. if (archive->files == NULL)
  398. {
  399. SzArDbExFree(&archive->db, SzFreePhysicsFS);
  400. __PHYSFS_platformClose(archive->stream.file);
  401. lzma_archive_exit(archive);
  402. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  403. }
  404. /*
  405. * Init with 0 so we know when a folder is already cached
  406. * Values will be set by LZMA_openRead()
  407. */
  408. memset(archive->files, 0, len);
  409. len = archive->db.Database.NumFolders * sizeof (LZMAfolder);
  410. archive->folders = (LZMAfolder *) allocator.Malloc(len);
  411. if (archive->folders == NULL)
  412. {
  413. SzArDbExFree(&archive->db, SzFreePhysicsFS);
  414. __PHYSFS_platformClose(archive->stream.file);
  415. lzma_archive_exit(archive);
  416. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  417. }
  418. /*
  419. * Init with 0 so we know when a folder is already cached
  420. * Values will be set by LZMA_read()
  421. */
  422. memset(archive->folders, 0, len);
  423. if(!lzma_files_init(archive))
  424. {
  425. SzArDbExFree(&archive->db, SzFreePhysicsFS);
  426. __PHYSFS_platformClose(archive->stream.file);
  427. lzma_archive_exit(archive);
  428. BAIL_MACRO(ERR_UNKNOWN_ERROR, NULL);
  429. }
  430. return(archive);
  431. } /* LZMA_openArchive */
  432. /*
  433. * Moved to seperate function so we can use alloca then immediately throw
  434. * away the allocated stack space...
  435. */
  436. static void doEnumCallback(PHYSFS_EnumFilesCallback cb, void *callbackdata,
  437. const char *odir, const char *str, size_t flen)
  438. {
  439. char *newstr = __PHYSFS_smallAlloc(flen + 1);
  440. if (newstr == NULL)
  441. return;
  442. memcpy(newstr, str, flen);
  443. newstr[flen] = '\0';
  444. cb(callbackdata, odir, newstr);
  445. __PHYSFS_smallFree(newstr);
  446. } /* doEnumCallback */
  447. static void LZMA_enumerateFiles(dvoid *opaque, const char *dname,
  448. int omitSymLinks, PHYSFS_EnumFilesCallback cb,
  449. const char *origdir, void *callbackdata)
  450. {
  451. size_t dlen = strlen(dname),
  452. dlen_inc = dlen + ((dlen > 0) ? 1 : 0);
  453. LZMAarchive *archive = (LZMAarchive *) opaque;
  454. LZMAfile *file = NULL,
  455. *lastFile = &archive->files[archive->db.Database.NumFiles];
  456. if (dlen)
  457. {
  458. file = lzma_find_file(archive, dname);
  459. if (file != NULL) /* if 'file' is NULL it should stay so, otherwise errors will not be handled */
  460. file += 1;
  461. }
  462. else
  463. {
  464. file = archive->files;
  465. }
  466. BAIL_IF_MACRO(file == NULL, ERR_NO_SUCH_FILE, );
  467. while (file < lastFile)
  468. {
  469. const char * fname = file->item->Name;
  470. const char * dirNameEnd = fname + dlen_inc;
  471. if (strncmp(dname, fname, dlen) != 0) /* Stop after mismatch, archive->files is sorted */
  472. break;
  473. if (strchr(dirNameEnd, '/')) /* Skip subdirs */
  474. {
  475. file++;
  476. continue;
  477. }
  478. /* Do the actual callback... */
  479. doEnumCallback(cb, callbackdata, origdir, dirNameEnd, strlen(dirNameEnd));
  480. file++;
  481. }
  482. } /* LZMA_enumerateFiles */
  483. static int LZMA_exists(dvoid *opaque, const char *name)
  484. {
  485. LZMAarchive *archive = (LZMAarchive *) opaque;
  486. return(lzma_find_file(archive, name) != NULL);
  487. } /* LZMA_exists */
  488. static PHYSFS_sint64 LZMA_getLastModTime(dvoid *opaque,
  489. const char *name,
  490. int *fileExists)
  491. {
  492. LZMAarchive *archive = (LZMAarchive *) opaque;
  493. LZMAfile *file = lzma_find_file(archive, name);
  494. *fileExists = (file != NULL);
  495. BAIL_IF_MACRO(file == NULL, NULL, -1);
  496. BAIL_IF_MACRO(!file->item->IsLastWriteTimeDefined, NULL, -1); /* write-time may not be defined for every file */
  497. return(lzma_filetime_to_unix_timestamp(&file->item->LastWriteTime));
  498. } /* LZMA_getLastModTime */
  499. static int LZMA_isDirectory(dvoid *opaque, const char *name, int *fileExists)
  500. {
  501. LZMAarchive *archive = (LZMAarchive *) opaque;
  502. LZMAfile *file = lzma_find_file(archive, name);
  503. *fileExists = (file != NULL);
  504. return(file == NULL ? 0 : file->item->IsDirectory);
  505. } /* LZMA_isDirectory */
  506. static int LZMA_isSymLink(dvoid *opaque, const char *name, int *fileExists)
  507. {
  508. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  509. } /* LZMA_isSymLink */
  510. static fvoid *LZMA_openRead(dvoid *opaque, const char *name, int *fileExists)
  511. {
  512. LZMAarchive *archive = (LZMAarchive *) opaque;
  513. LZMAfile *file = lzma_find_file(archive, name);
  514. *fileExists = (file != NULL);
  515. BAIL_IF_MACRO(file == NULL, ERR_NO_SUCH_FILE, NULL);
  516. BAIL_IF_MACRO(file->folder == NULL, ERR_NOT_A_FILE, NULL);
  517. file->position = 0;
  518. file->folder->references++; /* Increase refcount for automatic cleanup... */
  519. return(file);
  520. } /* LZMA_openRead */
  521. static fvoid *LZMA_openWrite(dvoid *opaque, const char *filename)
  522. {
  523. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  524. } /* LZMA_openWrite */
  525. static fvoid *LZMA_openAppend(dvoid *opaque, const char *filename)
  526. {
  527. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  528. } /* LZMA_openAppend */
  529. static void LZMA_dirClose(dvoid *opaque)
  530. {
  531. LZMAarchive *archive = (LZMAarchive *) opaque;
  532. PHYSFS_uint32 fileIndex = 0, numFiles = archive->db.Database.NumFiles;
  533. for (fileIndex = 0; fileIndex < numFiles; fileIndex++)
  534. {
  535. LZMA_fileClose(&archive->files[fileIndex]);
  536. } /* for */
  537. SzArDbExFree(&archive->db, SzFreePhysicsFS);
  538. __PHYSFS_platformClose(archive->stream.file);
  539. lzma_archive_exit(archive);
  540. } /* LZMA_dirClose */
  541. static int LZMA_remove(dvoid *opaque, const char *name)
  542. {
  543. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  544. } /* LZMA_remove */
  545. static int LZMA_mkdir(dvoid *opaque, const char *name)
  546. {
  547. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  548. } /* LZMA_mkdir */
  549. const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_LZMA =
  550. {
  551. "7Z",
  552. LZMA_ARCHIVE_DESCRIPTION,
  553. "Dennis Schridde <[email protected]>",
  554. "http://icculus.org/physfs/",
  555. };
  556. const PHYSFS_Archiver __PHYSFS_Archiver_LZMA =
  557. {
  558. &__PHYSFS_ArchiveInfo_LZMA,
  559. LZMA_isArchive, /* isArchive() method */
  560. LZMA_openArchive, /* openArchive() method */
  561. LZMA_enumerateFiles, /* enumerateFiles() method */
  562. LZMA_exists, /* exists() method */
  563. LZMA_isDirectory, /* isDirectory() method */
  564. LZMA_isSymLink, /* isSymLink() method */
  565. LZMA_getLastModTime, /* getLastModTime() method */
  566. LZMA_openRead, /* openRead() method */
  567. LZMA_openWrite, /* openWrite() method */
  568. LZMA_openAppend, /* openAppend() method */
  569. LZMA_remove, /* remove() method */
  570. LZMA_mkdir, /* mkdir() method */
  571. LZMA_dirClose, /* dirClose() method */
  572. LZMA_read, /* read() method */
  573. LZMA_write, /* write() method */
  574. LZMA_eof, /* eof() method */
  575. LZMA_tell, /* tell() method */
  576. LZMA_seek, /* seek() method */
  577. LZMA_fileLength, /* fileLength() method */
  578. LZMA_fileClose /* fileClose() method */
  579. };
  580. #endif /* defined PHYSFS_SUPPORTS_7Z */
  581. /* end of lzma.c ... */