archiver_unpacked.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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, must not have separate subdir
  9. * entries (but can have subdirs), must be case insensitive LOW ASCII
  10. * filenames <= 64 bytes. No symlinks, etc. We can relax some of these rules
  11. * as necessary.
  12. *
  13. * Please see the file LICENSE.txt in the source's root directory.
  14. *
  15. * This file written by Ryan C. Gordon.
  16. */
  17. #define __PHYSICSFS_INTERNAL__
  18. #include "physfs_internal.h"
  19. typedef struct
  20. {
  21. PHYSFS_Io *io;
  22. PHYSFS_uint32 entryCount;
  23. UNPKentry *entries;
  24. } UNPKinfo;
  25. typedef struct
  26. {
  27. PHYSFS_Io *io;
  28. UNPKentry *entry;
  29. PHYSFS_uint32 curPos;
  30. } UNPKfileinfo;
  31. void UNPK_closeArchive(PHYSFS_Dir *opaque)
  32. {
  33. UNPKinfo *info = ((UNPKinfo *) opaque);
  34. info->io->destroy(info->io);
  35. allocator.Free(info->entries);
  36. allocator.Free(info);
  37. } /* UNPK_closeArchive */
  38. static PHYSFS_sint64 UNPK_read(PHYSFS_Io *io, void *buffer, PHYSFS_uint64 len)
  39. {
  40. UNPKfileinfo *finfo = (UNPKfileinfo *) io->opaque;
  41. const UNPKentry *entry = finfo->entry;
  42. const PHYSFS_uint64 bytesLeft = (PHYSFS_uint64)(entry->size-finfo->curPos);
  43. PHYSFS_sint64 rc;
  44. if (bytesLeft < len)
  45. len = bytesLeft;
  46. rc = finfo->io->read(finfo->io, buffer, len);
  47. if (rc > 0)
  48. finfo->curPos += (PHYSFS_uint32) rc;
  49. return rc;
  50. } /* UNPK_read */
  51. static PHYSFS_sint64 UNPK_write(PHYSFS_Io *io, const void *b, PHYSFS_uint64 len)
  52. {
  53. BAIL_MACRO(PHYSFS_ERR_READ_ONLY, -1);
  54. } /* UNPK_write */
  55. static PHYSFS_sint64 UNPK_tell(PHYSFS_Io *io)
  56. {
  57. return ((UNPKfileinfo *) io->opaque)->curPos;
  58. } /* UNPK_tell */
  59. static int UNPK_seek(PHYSFS_Io *io, PHYSFS_uint64 offset)
  60. {
  61. UNPKfileinfo *finfo = (UNPKfileinfo *) io->opaque;
  62. const UNPKentry *entry = finfo->entry;
  63. int rc;
  64. BAIL_IF_MACRO(offset >= entry->size, PHYSFS_ERR_PAST_EOF, 0);
  65. rc = finfo->io->seek(finfo->io, entry->startPos + offset);
  66. if (rc)
  67. finfo->curPos = (PHYSFS_uint32) offset;
  68. return rc;
  69. } /* UNPK_seek */
  70. static PHYSFS_sint64 UNPK_length(PHYSFS_Io *io)
  71. {
  72. const UNPKfileinfo *finfo = (UNPKfileinfo *) io->opaque;
  73. return ((PHYSFS_sint64) finfo->entry->size);
  74. } /* UNPK_length */
  75. static PHYSFS_Io *UNPK_duplicate(PHYSFS_Io *_io)
  76. {
  77. UNPKfileinfo *origfinfo = (UNPKfileinfo *) _io->opaque;
  78. PHYSFS_Io *io = NULL;
  79. PHYSFS_Io *retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
  80. UNPKfileinfo *finfo = (UNPKfileinfo *) allocator.Malloc(sizeof (UNPKfileinfo));
  81. GOTO_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, UNPK_duplicate_failed);
  82. GOTO_IF_MACRO(!finfo, PHYSFS_ERR_OUT_OF_MEMORY, UNPK_duplicate_failed);
  83. io = origfinfo->io->duplicate(origfinfo->io);
  84. if (!io) goto UNPK_duplicate_failed;
  85. finfo->io = io;
  86. finfo->entry = origfinfo->entry;
  87. finfo->curPos = 0;
  88. memcpy(retval, _io, sizeof (PHYSFS_Io));
  89. retval->opaque = finfo;
  90. return retval;
  91. UNPK_duplicate_failed:
  92. if (finfo != NULL) allocator.Free(finfo);
  93. if (retval != NULL) allocator.Free(retval);
  94. if (io != NULL) io->destroy(io);
  95. return NULL;
  96. } /* UNPK_duplicate */
  97. static int UNPK_flush(PHYSFS_Io *io) { return 1; /* no write support. */ }
  98. static void UNPK_destroy(PHYSFS_Io *io)
  99. {
  100. UNPKfileinfo *finfo = (UNPKfileinfo *) io->opaque;
  101. finfo->io->destroy(finfo->io);
  102. allocator.Free(finfo);
  103. allocator.Free(io);
  104. } /* UNPK_destroy */
  105. static const PHYSFS_Io UNPK_Io =
  106. {
  107. CURRENT_PHYSFS_IO_API_VERSION, NULL,
  108. UNPK_read,
  109. UNPK_write,
  110. UNPK_seek,
  111. UNPK_tell,
  112. UNPK_length,
  113. UNPK_duplicate,
  114. UNPK_flush,
  115. UNPK_destroy
  116. };
  117. static int entryCmp(void *_a, size_t one, size_t two)
  118. {
  119. if (one != two)
  120. {
  121. const UNPKentry *a = (const UNPKentry *) _a;
  122. return __PHYSFS_stricmpASCII(a[one].name, a[two].name);
  123. } /* if */
  124. return 0;
  125. } /* entryCmp */
  126. static void entrySwap(void *_a, size_t one, size_t two)
  127. {
  128. if (one != two)
  129. {
  130. UNPKentry tmp;
  131. UNPKentry *first = &(((UNPKentry *) _a)[one]);
  132. UNPKentry *second = &(((UNPKentry *) _a)[two]);
  133. memcpy(&tmp, first, sizeof (UNPKentry));
  134. memcpy(first, second, sizeof (UNPKentry));
  135. memcpy(second, &tmp, sizeof (UNPKentry));
  136. } /* if */
  137. } /* entrySwap */
  138. static PHYSFS_sint32 findStartOfDir(UNPKinfo *info, const char *path,
  139. int stop_on_first_find)
  140. {
  141. PHYSFS_sint32 lo = 0;
  142. PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
  143. PHYSFS_sint32 middle;
  144. PHYSFS_uint32 dlen = (PHYSFS_uint32) strlen(path);
  145. PHYSFS_sint32 retval = -1;
  146. const char *name;
  147. int rc;
  148. if (*path == '\0') /* root dir? */
  149. return 0;
  150. if ((dlen > 0) && (path[dlen - 1] == '/')) /* ignore trailing slash. */
  151. dlen--;
  152. while (lo <= hi)
  153. {
  154. middle = lo + ((hi - lo) / 2);
  155. name = info->entries[middle].name;
  156. rc = __PHYSFS_strnicmpASCII(path, name, dlen);
  157. if (rc == 0)
  158. {
  159. char ch = name[dlen];
  160. if (ch < '/') /* make sure this isn't just a substr match. */
  161. rc = -1;
  162. else if (ch > '/')
  163. rc = 1;
  164. else
  165. {
  166. if (stop_on_first_find) /* Just checking dir's existance? */
  167. return middle;
  168. if (name[dlen + 1] == '\0') /* Skip initial dir entry. */
  169. return (middle + 1);
  170. /* there might be more entries earlier in the list. */
  171. retval = middle;
  172. hi = middle - 1;
  173. } /* else */
  174. } /* if */
  175. if (rc > 0)
  176. lo = middle + 1;
  177. else
  178. hi = middle - 1;
  179. } /* while */
  180. return retval;
  181. } /* findStartOfDir */
  182. /*
  183. * Moved to seperate function so we can use alloca then immediately throw
  184. * away the allocated stack space...
  185. */
  186. static void doEnumCallback(PHYSFS_EnumFilesCallback cb, void *callbackdata,
  187. const char *odir, const char *str, PHYSFS_sint32 ln)
  188. {
  189. char *newstr = __PHYSFS_smallAlloc(ln + 1);
  190. if (newstr == NULL)
  191. return;
  192. memcpy(newstr, str, ln);
  193. newstr[ln] = '\0';
  194. cb(callbackdata, odir, newstr);
  195. __PHYSFS_smallFree(newstr);
  196. } /* doEnumCallback */
  197. void UNPK_enumerateFiles(PHYSFS_Dir *opaque, const char *dname,
  198. int omitSymLinks, PHYSFS_EnumFilesCallback cb,
  199. const char *origdir, void *callbackdata)
  200. {
  201. UNPKinfo *info = ((UNPKinfo *) opaque);
  202. PHYSFS_sint32 dlen, dlen_inc, max, i;
  203. i = findStartOfDir(info, dname, 0);
  204. if (i == -1) /* no such directory. */
  205. return;
  206. dlen = (PHYSFS_sint32) strlen(dname);
  207. if ((dlen > 0) && (dname[dlen - 1] == '/')) /* ignore trailing slash. */
  208. dlen--;
  209. dlen_inc = ((dlen > 0) ? 1 : 0) + dlen;
  210. max = (PHYSFS_sint32) info->entryCount;
  211. while (i < max)
  212. {
  213. char *add;
  214. char *ptr;
  215. PHYSFS_sint32 ln;
  216. char *e = info->entries[i].name;
  217. if ((dlen) &&
  218. ((__PHYSFS_strnicmpASCII(e, dname, dlen)) || (e[dlen] != '/')))
  219. {
  220. break; /* past end of this dir; we're done. */
  221. } /* if */
  222. add = e + dlen_inc;
  223. ptr = strchr(add, '/');
  224. ln = (PHYSFS_sint32) ((ptr) ? ptr-add : strlen(add));
  225. doEnumCallback(cb, callbackdata, origdir, add, ln);
  226. ln += dlen_inc; /* point past entry to children... */
  227. /* increment counter and skip children of subdirs... */
  228. while ((++i < max) && (ptr != NULL))
  229. {
  230. char *e_new = info->entries[i].name;
  231. if ((__PHYSFS_strnicmpASCII(e, e_new, ln) != 0) ||
  232. (e_new[ln] != '/'))
  233. {
  234. break;
  235. } /* if */
  236. } /* while */
  237. } /* while */
  238. } /* UNPK_enumerateFiles */
  239. /*
  240. * This will find the UNPKentry associated with a path in platform-independent
  241. * notation. Directories don't have UNPKentries associated with them, but
  242. * (*isDir) will be set to non-zero if a dir was hit.
  243. */
  244. static UNPKentry *findEntry(const UNPKinfo *info, const char *path, int *isDir)
  245. {
  246. UNPKentry *a = info->entries;
  247. PHYSFS_sint32 pathlen = (PHYSFS_sint32) strlen(path);
  248. PHYSFS_sint32 lo = 0;
  249. PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
  250. PHYSFS_sint32 middle;
  251. const char *thispath = NULL;
  252. int rc;
  253. while (lo <= hi)
  254. {
  255. middle = lo + ((hi - lo) / 2);
  256. thispath = a[middle].name;
  257. rc = __PHYSFS_strnicmpASCII(path, thispath, pathlen);
  258. if (rc > 0)
  259. lo = middle + 1;
  260. else if (rc < 0)
  261. hi = middle - 1;
  262. else /* substring match...might be dir or entry or nothing. */
  263. {
  264. if (isDir != NULL)
  265. {
  266. *isDir = (thispath[pathlen] == '/');
  267. if (*isDir)
  268. return NULL;
  269. } /* if */
  270. if (thispath[pathlen] == '\0') /* found entry? */
  271. return &a[middle];
  272. /* adjust search params, try again. */
  273. else if (thispath[pathlen] > '/')
  274. hi = middle - 1;
  275. else
  276. lo = middle + 1;
  277. } /* if */
  278. } /* while */
  279. if (isDir != NULL)
  280. *isDir = 0;
  281. BAIL_MACRO(PHYSFS_ERR_NO_SUCH_PATH, NULL);
  282. } /* findEntry */
  283. PHYSFS_Io *UNPK_openRead(PHYSFS_Dir *opaque, const char *fnm, int *fileExists)
  284. {
  285. PHYSFS_Io *retval = NULL;
  286. UNPKinfo *info = (UNPKinfo *) opaque;
  287. UNPKfileinfo *finfo = NULL;
  288. int isdir = 0;
  289. UNPKentry *entry = findEntry(info, fnm, &isdir);
  290. *fileExists = (entry != NULL);
  291. GOTO_IF_MACRO(isdir, PHYSFS_ERR_NOT_A_FILE, UNPK_openRead_failed);
  292. GOTO_IF_MACRO(!entry, ERRPASS, UNPK_openRead_failed);
  293. retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
  294. GOTO_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, UNPK_openRead_failed);
  295. finfo = (UNPKfileinfo *) allocator.Malloc(sizeof (UNPKfileinfo));
  296. GOTO_IF_MACRO(!finfo, PHYSFS_ERR_OUT_OF_MEMORY, UNPK_openRead_failed);
  297. finfo->io = info->io->duplicate(info->io);
  298. GOTO_IF_MACRO(!finfo->io, ERRPASS, UNPK_openRead_failed);
  299. if (!finfo->io->seek(finfo->io, entry->startPos))
  300. goto UNPK_openRead_failed;
  301. finfo->curPos = 0;
  302. finfo->entry = entry;
  303. memcpy(retval, &UNPK_Io, sizeof (*retval));
  304. retval->opaque = finfo;
  305. return retval;
  306. UNPK_openRead_failed:
  307. if (finfo != NULL)
  308. {
  309. if (finfo->io != NULL)
  310. finfo->io->destroy(finfo->io);
  311. allocator.Free(finfo);
  312. } /* if */
  313. if (retval != NULL)
  314. allocator.Free(retval);
  315. return NULL;
  316. } /* UNPK_openRead */
  317. PHYSFS_Io *UNPK_openWrite(PHYSFS_Dir *opaque, const char *name)
  318. {
  319. BAIL_MACRO(PHYSFS_ERR_READ_ONLY, NULL);
  320. } /* UNPK_openWrite */
  321. PHYSFS_Io *UNPK_openAppend(PHYSFS_Dir *opaque, const char *name)
  322. {
  323. BAIL_MACRO(PHYSFS_ERR_READ_ONLY, NULL);
  324. } /* UNPK_openAppend */
  325. int UNPK_remove(PHYSFS_Dir *opaque, const char *name)
  326. {
  327. BAIL_MACRO(PHYSFS_ERR_READ_ONLY, 0);
  328. } /* UNPK_remove */
  329. int UNPK_mkdir(PHYSFS_Dir *opaque, const char *name)
  330. {
  331. BAIL_MACRO(PHYSFS_ERR_READ_ONLY, 0);
  332. } /* UNPK_mkdir */
  333. int UNPK_stat(PHYSFS_Dir *opaque, const char *filename,
  334. int *exists, PHYSFS_Stat *stat)
  335. {
  336. int isDir = 0;
  337. const UNPKinfo *info = (const UNPKinfo *) opaque;
  338. const UNPKentry *entry = findEntry(info, filename, &isDir);
  339. if (isDir)
  340. {
  341. *exists = 1;
  342. stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
  343. stat->filesize = 0;
  344. } /* if */
  345. else if (entry != NULL)
  346. {
  347. *exists = 1;
  348. stat->filetype = PHYSFS_FILETYPE_REGULAR;
  349. stat->filesize = entry->size;
  350. } /* else if */
  351. else
  352. {
  353. *exists = 0;
  354. return 0;
  355. } /* else */
  356. stat->modtime = -1;
  357. stat->createtime = -1;
  358. stat->accesstime = -1;
  359. stat->readonly = 1;
  360. return 1;
  361. } /* UNPK_stat */
  362. PHYSFS_Dir *UNPK_openArchive(PHYSFS_Io *io, UNPKentry *e,
  363. const PHYSFS_uint32 num)
  364. {
  365. UNPKinfo *info = (UNPKinfo *) allocator.Malloc(sizeof (UNPKinfo));
  366. if (info == NULL)
  367. {
  368. allocator.Free(e);
  369. BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  370. } /* if */
  371. __PHYSFS_sort(e, (size_t) num, entryCmp, entrySwap);
  372. info->io = io;
  373. info->entryCount = num;
  374. info->entries = e;
  375. return info;
  376. } /* UNPK_openArchive */
  377. /* end of archiver_unpacked.c ... */