physfs_archiver_unpacked.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * High-level PhysicsFS archiver for simple unpacked file formats.
  3. *
  4. * This is a framework that basic archivers build on top of. It's for simple
  5. * formats that can just hand back a list of files and the offsets of their
  6. * uncompressed data. There are an alarming number of formats like this.
  7. *
  8. * RULES: Archive entries must be uncompressed. Dirs and files allowed, but no
  9. * symlinks, etc. We can relax some of these rules as necessary.
  10. *
  11. * Please see the file LICENSE.txt in the source's root directory.
  12. *
  13. * This file written by Ryan C. Gordon.
  14. */
  15. #define __PHYSICSFS_INTERNAL__
  16. #include "physfs_internal.h"
  17. typedef struct
  18. {
  19. __PHYSFS_DirTree tree;
  20. PHYSFS_Io *io;
  21. } UNPKinfo;
  22. typedef struct
  23. {
  24. __PHYSFS_DirTreeEntry tree;
  25. PHYSFS_uint64 startPos;
  26. PHYSFS_uint64 size;
  27. PHYSFS_sint64 ctime;
  28. PHYSFS_sint64 mtime;
  29. } UNPKentry;
  30. typedef struct
  31. {
  32. PHYSFS_Io *io;
  33. UNPKentry *entry;
  34. PHYSFS_uint32 curPos;
  35. } UNPKfileinfo;
  36. void UNPK_closeArchive(void *opaque)
  37. {
  38. UNPKinfo *info = ((UNPKinfo *) opaque);
  39. if (info)
  40. {
  41. __PHYSFS_DirTreeDeinit(&info->tree);
  42. if (info->io)
  43. info->io->destroy(info->io);
  44. allocator.Free(info);
  45. } /* if */
  46. } /* UNPK_closeArchive */
  47. void UNPK_abandonArchive(void *opaque)
  48. {
  49. UNPKinfo *info = ((UNPKinfo *) opaque);
  50. if (info)
  51. {
  52. info->io = NULL;
  53. UNPK_closeArchive(info);
  54. } /* if */
  55. } /* UNPK_abandonArchive */
  56. static PHYSFS_sint64 UNPK_read(PHYSFS_Io *io, void *buffer, PHYSFS_uint64 len)
  57. {
  58. UNPKfileinfo *finfo = (UNPKfileinfo *) io->opaque;
  59. const UNPKentry *entry = finfo->entry;
  60. const PHYSFS_uint64 bytesLeft = (PHYSFS_uint64)(entry->size-finfo->curPos);
  61. PHYSFS_sint64 rc;
  62. if (bytesLeft < len)
  63. len = bytesLeft;
  64. rc = finfo->io->read(finfo->io, buffer, len);
  65. if (rc > 0)
  66. finfo->curPos += (PHYSFS_uint32) rc;
  67. return rc;
  68. } /* UNPK_read */
  69. static PHYSFS_sint64 UNPK_write(PHYSFS_Io *io, const void *b, PHYSFS_uint64 len)
  70. {
  71. BAIL(PHYSFS_ERR_READ_ONLY, -1);
  72. } /* UNPK_write */
  73. static PHYSFS_sint64 UNPK_tell(PHYSFS_Io *io)
  74. {
  75. return ((UNPKfileinfo *) io->opaque)->curPos;
  76. } /* UNPK_tell */
  77. static int UNPK_seek(PHYSFS_Io *io, PHYSFS_uint64 offset)
  78. {
  79. UNPKfileinfo *finfo = (UNPKfileinfo *) io->opaque;
  80. const UNPKentry *entry = finfo->entry;
  81. int rc;
  82. BAIL_IF(offset >= entry->size, PHYSFS_ERR_PAST_EOF, 0);
  83. rc = finfo->io->seek(finfo->io, entry->startPos + offset);
  84. if (rc)
  85. finfo->curPos = (PHYSFS_uint32) offset;
  86. return rc;
  87. } /* UNPK_seek */
  88. static PHYSFS_sint64 UNPK_length(PHYSFS_Io *io)
  89. {
  90. const UNPKfileinfo *finfo = (UNPKfileinfo *) io->opaque;
  91. return ((PHYSFS_sint64) finfo->entry->size);
  92. } /* UNPK_length */
  93. static PHYSFS_Io *UNPK_duplicate(PHYSFS_Io *_io)
  94. {
  95. UNPKfileinfo *origfinfo = (UNPKfileinfo *) _io->opaque;
  96. PHYSFS_Io *io = NULL;
  97. PHYSFS_Io *retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
  98. UNPKfileinfo *finfo = (UNPKfileinfo *) allocator.Malloc(sizeof (UNPKfileinfo));
  99. GOTO_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, UNPK_duplicate_failed);
  100. GOTO_IF(!finfo, PHYSFS_ERR_OUT_OF_MEMORY, UNPK_duplicate_failed);
  101. io = origfinfo->io->duplicate(origfinfo->io);
  102. if (!io) goto UNPK_duplicate_failed;
  103. finfo->io = io;
  104. finfo->entry = origfinfo->entry;
  105. finfo->curPos = 0;
  106. memcpy(retval, _io, sizeof (PHYSFS_Io));
  107. retval->opaque = finfo;
  108. return retval;
  109. UNPK_duplicate_failed:
  110. if (finfo != NULL) allocator.Free(finfo);
  111. if (retval != NULL) allocator.Free(retval);
  112. if (io != NULL) io->destroy(io);
  113. return NULL;
  114. } /* UNPK_duplicate */
  115. static int UNPK_flush(PHYSFS_Io *io) { return 1; /* no write support. */ }
  116. static void UNPK_destroy(PHYSFS_Io *io)
  117. {
  118. UNPKfileinfo *finfo = (UNPKfileinfo *) io->opaque;
  119. finfo->io->destroy(finfo->io);
  120. allocator.Free(finfo);
  121. allocator.Free(io);
  122. } /* UNPK_destroy */
  123. static const PHYSFS_Io UNPK_Io =
  124. {
  125. CURRENT_PHYSFS_IO_API_VERSION, NULL,
  126. UNPK_read,
  127. UNPK_write,
  128. UNPK_seek,
  129. UNPK_tell,
  130. UNPK_length,
  131. UNPK_duplicate,
  132. UNPK_flush,
  133. UNPK_destroy
  134. };
  135. static inline UNPKentry *findEntry(UNPKinfo *info, const char *path)
  136. {
  137. return (UNPKentry *) __PHYSFS_DirTreeFind(&info->tree, path);
  138. } /* findEntry */
  139. PHYSFS_Io *UNPK_openRead(void *opaque, const char *name)
  140. {
  141. PHYSFS_Io *retval = NULL;
  142. UNPKinfo *info = (UNPKinfo *) opaque;
  143. UNPKfileinfo *finfo = NULL;
  144. UNPKentry *entry = findEntry(info, name);
  145. BAIL_IF_ERRPASS(!entry, NULL);
  146. BAIL_IF(entry->tree.isdir, PHYSFS_ERR_NOT_A_FILE, NULL);
  147. retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
  148. GOTO_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, UNPK_openRead_failed);
  149. finfo = (UNPKfileinfo *) allocator.Malloc(sizeof (UNPKfileinfo));
  150. GOTO_IF(!finfo, PHYSFS_ERR_OUT_OF_MEMORY, UNPK_openRead_failed);
  151. finfo->io = info->io->duplicate(info->io);
  152. GOTO_IF_ERRPASS(!finfo->io, UNPK_openRead_failed);
  153. if (!finfo->io->seek(finfo->io, entry->startPos))
  154. goto UNPK_openRead_failed;
  155. finfo->curPos = 0;
  156. finfo->entry = entry;
  157. memcpy(retval, &UNPK_Io, sizeof (*retval));
  158. retval->opaque = finfo;
  159. return retval;
  160. UNPK_openRead_failed:
  161. if (finfo != NULL)
  162. {
  163. if (finfo->io != NULL)
  164. finfo->io->destroy(finfo->io);
  165. allocator.Free(finfo);
  166. } /* if */
  167. if (retval != NULL)
  168. allocator.Free(retval);
  169. return NULL;
  170. } /* UNPK_openRead */
  171. PHYSFS_Io *UNPK_openWrite(void *opaque, const char *name)
  172. {
  173. BAIL(PHYSFS_ERR_READ_ONLY, NULL);
  174. } /* UNPK_openWrite */
  175. PHYSFS_Io *UNPK_openAppend(void *opaque, const char *name)
  176. {
  177. BAIL(PHYSFS_ERR_READ_ONLY, NULL);
  178. } /* UNPK_openAppend */
  179. int UNPK_remove(void *opaque, const char *name)
  180. {
  181. BAIL(PHYSFS_ERR_READ_ONLY, 0);
  182. } /* UNPK_remove */
  183. int UNPK_mkdir(void *opaque, const char *name)
  184. {
  185. BAIL(PHYSFS_ERR_READ_ONLY, 0);
  186. } /* UNPK_mkdir */
  187. int UNPK_stat(void *opaque, const char *path, PHYSFS_Stat *stat)
  188. {
  189. UNPKinfo *info = (UNPKinfo *) opaque;
  190. const UNPKentry *entry = findEntry(info, path);
  191. BAIL_IF_ERRPASS(!entry, 0);
  192. if (entry->tree.isdir)
  193. {
  194. stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
  195. stat->filesize = 0;
  196. } /* if */
  197. else
  198. {
  199. stat->filetype = PHYSFS_FILETYPE_REGULAR;
  200. stat->filesize = entry->size;
  201. } /* else */
  202. stat->modtime = entry->mtime;
  203. stat->createtime = entry->ctime;
  204. stat->accesstime = -1;
  205. stat->readonly = 1;
  206. return 1;
  207. } /* UNPK_stat */
  208. void *UNPK_addEntry(void *opaque, char *name, const int isdir,
  209. const PHYSFS_sint64 ctime, const PHYSFS_sint64 mtime,
  210. const PHYSFS_uint64 pos, const PHYSFS_uint64 len)
  211. {
  212. UNPKinfo *info = (UNPKinfo *) opaque;
  213. UNPKentry *entry;
  214. entry = (UNPKentry *) __PHYSFS_DirTreeAdd(&info->tree, name, isdir);
  215. BAIL_IF_ERRPASS(!entry, NULL);
  216. entry->startPos = isdir ? 0 : pos;
  217. entry->size = isdir ? 0 : len;
  218. entry->ctime = ctime;
  219. entry->mtime = mtime;
  220. return entry;
  221. } /* UNPK_addEntry */
  222. void *UNPK_openArchive(PHYSFS_Io *io)
  223. {
  224. UNPKinfo *info = (UNPKinfo *) allocator.Malloc(sizeof (UNPKinfo));
  225. BAIL_IF(!info, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  226. if (!__PHYSFS_DirTreeInit(&info->tree, sizeof (UNPKentry)))
  227. {
  228. allocator.Free(info);
  229. return NULL;
  230. } /* if */
  231. info->io = io;
  232. return info;
  233. } /* UNPK_openArchive */
  234. /* end of physfs_archiver_unpacked.c ... */