grp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /*
  2. * GRP support routines for PhysicsFS.
  3. *
  4. * This driver handles BUILD engine archives ("groupfiles"). This format
  5. * (but not this driver) was put together by Ken Silverman.
  6. *
  7. * The format is simple enough. In Ken's words:
  8. *
  9. * What's the .GRP file format?
  10. *
  11. * The ".grp" file format is just a collection of a lot of files stored
  12. * into 1 big one. I tried to make the format as simple as possible: The
  13. * first 12 bytes contains my name, "KenSilverman". The next 4 bytes is
  14. * the number of files that were compacted into the group file. Then for
  15. * each file, there is a 16 byte structure, where the first 12 bytes are
  16. * the filename, and the last 4 bytes are the file's size. The rest of
  17. * the group file is just the raw data packed one after the other in the
  18. * same order as the list of files.
  19. *
  20. * (That info is from http://www.advsys.net/ken/build.htm ...)
  21. *
  22. * Please see the file LICENSE in the source's root directory.
  23. *
  24. * This file written by Ryan C. Gordon.
  25. */
  26. #if HAVE_CONFIG_H
  27. # include <config.h>
  28. #endif
  29. #if (defined PHYSFS_SUPPORTS_GRP)
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <errno.h>
  34. #include <fcntl.h>
  35. #include <assert.h>
  36. #include "physfs.h"
  37. #define __PHYSICSFS_INTERNAL__
  38. #include "physfs_internal.h"
  39. /*
  40. * When sorting the grp entries in an archive, we use a modified QuickSort.
  41. * When there are less then GRP_QUICKSORT_THRESHOLD entries left to sort,
  42. * we switch over to an InsertionSort for the remainder. Tweak to taste.
  43. */
  44. #define GRP_QUICKSORT_THRESHOLD 4
  45. typedef struct
  46. {
  47. char name[13];
  48. PHYSFS_uint64 startPos;
  49. PHYSFS_uint64 size;
  50. } GRPentry;
  51. typedef struct
  52. {
  53. char *filename;
  54. PHYSFS_sint64 last_mod_time;
  55. PHYSFS_uint32 entryCount;
  56. GRPentry *entries;
  57. } GRPinfo;
  58. typedef struct
  59. {
  60. void *handle;
  61. GRPentry *entry;
  62. PHYSFS_sint64 curPos;
  63. } GRPfileinfo;
  64. static void GRP_dirClose(DirHandle *h);
  65. static PHYSFS_sint64 GRP_read(FileHandle *handle, void *buffer,
  66. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
  67. static int GRP_eof(FileHandle *handle);
  68. static PHYSFS_sint64 GRP_tell(FileHandle *handle);
  69. static int GRP_seek(FileHandle *handle, PHYSFS_uint64 offset);
  70. static PHYSFS_sint64 GRP_fileLength(FileHandle *handle);
  71. static int GRP_fileClose(FileHandle *handle);
  72. static int GRP_isArchive(const char *filename, int forWriting);
  73. static DirHandle *GRP_openArchive(const char *name, int forWriting);
  74. static LinkedStringList *GRP_enumerateFiles(DirHandle *h,
  75. const char *dirname,
  76. int omitSymLinks);
  77. static int GRP_exists(DirHandle *h, const char *name);
  78. static int GRP_isDirectory(DirHandle *h, const char *name);
  79. static int GRP_isSymLink(DirHandle *h, const char *name);
  80. static PHYSFS_sint64 GRP_getLastModTime(DirHandle *h, const char *name);
  81. static FileHandle *GRP_openRead(DirHandle *h, const char *name);
  82. const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_GRP =
  83. {
  84. "GRP",
  85. "Build engine Groupfile format",
  86. "Ryan C. Gordon <icculus@clutteredmind.org>",
  87. "http://icculus.org/physfs/",
  88. };
  89. static const FileFunctions __PHYSFS_FileFunctions_GRP =
  90. {
  91. GRP_read, /* read() method */
  92. NULL, /* write() method */
  93. GRP_eof, /* eof() method */
  94. GRP_tell, /* tell() method */
  95. GRP_seek, /* seek() method */
  96. GRP_fileLength, /* fileLength() method */
  97. GRP_fileClose /* fileClose() method */
  98. };
  99. const DirFunctions __PHYSFS_DirFunctions_GRP =
  100. {
  101. &__PHYSFS_ArchiveInfo_GRP,
  102. GRP_isArchive, /* isArchive() method */
  103. GRP_openArchive, /* openArchive() method */
  104. GRP_enumerateFiles, /* enumerateFiles() method */
  105. GRP_exists, /* exists() method */
  106. GRP_isDirectory, /* isDirectory() method */
  107. GRP_isSymLink, /* isSymLink() method */
  108. GRP_getLastModTime, /* getLastModTime() method */
  109. GRP_openRead, /* openRead() method */
  110. NULL, /* openWrite() method */
  111. NULL, /* openAppend() method */
  112. NULL, /* remove() method */
  113. NULL, /* mkdir() method */
  114. GRP_dirClose /* dirClose() method */
  115. };
  116. static void GRP_dirClose(DirHandle *h)
  117. {
  118. GRPinfo *info = ((GRPinfo *) h->opaque);
  119. free(info->filename);
  120. free(info->entries);
  121. free(info);
  122. free(h);
  123. } /* GRP_dirClose */
  124. static PHYSFS_sint64 GRP_read(FileHandle *handle, void *buffer,
  125. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  126. {
  127. GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
  128. GRPentry *entry = finfo->entry;
  129. PHYSFS_uint64 bytesLeft = entry->size - finfo->curPos;
  130. PHYSFS_uint64 objsLeft = (bytesLeft / objSize);
  131. PHYSFS_sint64 rc;
  132. if (objsLeft < objCount)
  133. objCount = (PHYSFS_uint32) objsLeft;
  134. rc = __PHYSFS_platformRead(finfo->handle, buffer, objSize, objCount);
  135. if (rc > 0)
  136. finfo->curPos += (rc * objSize);
  137. return(rc);
  138. } /* GRP_read */
  139. static int GRP_eof(FileHandle *handle)
  140. {
  141. GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
  142. GRPentry *entry = finfo->entry;
  143. return(finfo->curPos >= (PHYSFS_sint64) entry->size);
  144. } /* GRP_eof */
  145. static PHYSFS_sint64 GRP_tell(FileHandle *handle)
  146. {
  147. return(((GRPfileinfo *) (handle->opaque))->curPos);
  148. } /* GRP_tell */
  149. static int GRP_seek(FileHandle *handle, PHYSFS_uint64 offset)
  150. {
  151. GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
  152. GRPentry *entry = finfo->entry;
  153. PHYSFS_uint64 newPos = (entry->startPos + offset);
  154. int rc;
  155. BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
  156. BAIL_IF_MACRO(newPos > entry->startPos + entry->size, ERR_PAST_EOF, 0);
  157. rc = __PHYSFS_platformSeek(finfo->handle, newPos);
  158. if (rc)
  159. finfo->curPos = offset;
  160. return(rc);
  161. } /* GRP_seek */
  162. static PHYSFS_sint64 GRP_fileLength(FileHandle *handle)
  163. {
  164. return(((GRPfileinfo *) handle->opaque)->entry->size);
  165. } /* GRP_fileLength */
  166. static int GRP_fileClose(FileHandle *handle)
  167. {
  168. GRPfileinfo *finfo = ((GRPfileinfo *) handle->opaque);
  169. BAIL_IF_MACRO(!__PHYSFS_platformClose(finfo->handle), NULL, 0);
  170. free(finfo);
  171. free(handle);
  172. return(1);
  173. } /* GRP_fileClose */
  174. static int grp_open(const char *filename, int forWriting,
  175. void **fh, PHYSFS_uint32 *count)
  176. {
  177. PHYSFS_uint8 buf[12];
  178. *fh = NULL;
  179. BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
  180. *fh = __PHYSFS_platformOpenRead(filename);
  181. BAIL_IF_MACRO(*fh == NULL, NULL, 0);
  182. if (__PHYSFS_platformRead(*fh, buf, 12, 1) != 1)
  183. goto openGrp_failed;
  184. if (memcmp(buf, "KenSilverman", 12) != 0)
  185. {
  186. __PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE);
  187. goto openGrp_failed;
  188. } /* if */
  189. if (__PHYSFS_platformRead(*fh, count, sizeof (PHYSFS_uint32), 1) != 1)
  190. goto openGrp_failed;
  191. *count = PHYSFS_swapULE32(*count);
  192. return(1);
  193. openGrp_failed:
  194. if (*fh != NULL)
  195. __PHYSFS_platformClose(*fh);
  196. *count = -1;
  197. *fh = NULL;
  198. return(0);
  199. } /* grp_open */
  200. static int GRP_isArchive(const char *filename, int forWriting)
  201. {
  202. void *fh;
  203. PHYSFS_uint32 fileCount;
  204. int retval = grp_open(filename, forWriting, &fh, &fileCount);
  205. if (fh != NULL)
  206. __PHYSFS_platformClose(fh);
  207. return(retval);
  208. } /* GRP_isArchive */
  209. static void grp_entry_swap(GRPentry *a, PHYSFS_uint32 one, PHYSFS_uint32 two)
  210. {
  211. GRPentry tmp;
  212. memcpy(&tmp, &a[one], sizeof (GRPentry));
  213. memcpy(&a[one], &a[two], sizeof (GRPentry));
  214. memcpy(&a[two], &tmp, sizeof (GRPentry));
  215. } /* grp_entry_swap */
  216. static void grp_quick_sort(GRPentry *a, PHYSFS_uint32 lo, PHYSFS_uint32 hi)
  217. {
  218. PHYSFS_uint32 i;
  219. PHYSFS_uint32 j;
  220. GRPentry *v;
  221. if ((hi - lo) > GRP_QUICKSORT_THRESHOLD)
  222. {
  223. i = (hi + lo) / 2;
  224. if (strcmp(a[lo].name, a[i].name) > 0) grp_entry_swap(a, lo, i);
  225. if (strcmp(a[lo].name, a[hi].name) > 0) grp_entry_swap(a, lo, hi);
  226. if (strcmp(a[i].name, a[hi].name) > 0) grp_entry_swap(a, i, hi);
  227. j = hi - 1;
  228. grp_entry_swap(a, i, j);
  229. i = lo;
  230. v = &a[j];
  231. while (1)
  232. {
  233. while(strcmp(a[++i].name, v->name) < 0) {}
  234. while(strcmp(a[--j].name, v->name) > 0) {}
  235. if (j < i)
  236. break;
  237. grp_entry_swap(a, i, j);
  238. } /* while */
  239. grp_entry_swap(a, i, hi-1);
  240. grp_quick_sort(a, lo, j);
  241. grp_quick_sort(a, i+1, hi);
  242. } /* if */
  243. } /* grp_quick_sort */
  244. static void grp_insertion_sort(GRPentry *a, PHYSFS_uint32 lo, PHYSFS_uint32 hi)
  245. {
  246. PHYSFS_uint32 i;
  247. PHYSFS_uint32 j;
  248. GRPentry tmp;
  249. for (i = lo + 1; i <= hi; i++)
  250. {
  251. memcpy(&tmp, &a[i], sizeof (GRPentry));
  252. j = i;
  253. while ((j > lo) && (strcmp(a[j - 1].name, tmp.name) > 0))
  254. {
  255. memcpy(&a[j], &a[j - 1], sizeof (GRPentry));
  256. j--;
  257. } /* while */
  258. memcpy(&a[j], &tmp, sizeof (GRPentry));
  259. } /* for */
  260. } /* grp_insertion_sort */
  261. static void grp_sort_entries(GRPentry *entries, PHYSFS_uint32 max)
  262. {
  263. /*
  264. * Fast Quicksort algorithm inspired by code from here:
  265. * http://www.cs.ubc.ca/spider/harrison/Java/sorting-demo.html
  266. */
  267. if (max <= GRP_QUICKSORT_THRESHOLD)
  268. grp_quick_sort(entries, 0, max - 1);
  269. grp_insertion_sort(entries, 0, max - 1);
  270. } /* grp_sort_entries */
  271. static int grp_load_entries(const char *name, int forWriting, GRPinfo *info)
  272. {
  273. void *fh = NULL;
  274. PHYSFS_uint32 fileCount;
  275. PHYSFS_uint32 location = 16; /* sizeof sig. */
  276. GRPentry *entry;
  277. char *ptr;
  278. BAIL_IF_MACRO(!grp_open(name, forWriting, &fh, &fileCount), NULL, 0);
  279. info->entryCount = fileCount;
  280. info->entries = (GRPentry *) malloc(sizeof (GRPentry) * fileCount);
  281. if (info->entries == NULL)
  282. {
  283. __PHYSFS_platformClose(fh);
  284. BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
  285. } /* if */
  286. location += (16 * fileCount);
  287. for (entry = info->entries; fileCount > 0; fileCount--, entry++)
  288. {
  289. if (__PHYSFS_platformRead(fh, &entry->name, 12, 1) != 1)
  290. {
  291. __PHYSFS_platformClose(fh);
  292. return(0);
  293. } /* if */
  294. entry->name[12] = '\0'; /* name isn't null-terminated in file. */
  295. if ((ptr = strchr(entry->name, ' ')) != NULL)
  296. *ptr = '\0'; /* trim extra spaces. */
  297. if (__PHYSFS_platformRead(fh, &entry->size, 4, 1) != 1)
  298. {
  299. __PHYSFS_platformClose(fh);
  300. return(0);
  301. } /* if */
  302. entry->size = PHYSFS_swapULE32(entry->size);
  303. entry->startPos = location;
  304. location += entry->size;
  305. } /* for */
  306. __PHYSFS_platformClose(fh);
  307. grp_sort_entries(info->entries, info->entryCount);
  308. return(1);
  309. } /* grp_load_entries */
  310. static DirHandle *GRP_openArchive(const char *name, int forWriting)
  311. {
  312. GRPinfo *info;
  313. DirHandle *retval = malloc(sizeof (DirHandle));
  314. PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
  315. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  316. info = retval->opaque = malloc(sizeof (GRPinfo));
  317. if (info == NULL)
  318. {
  319. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  320. goto GRP_openArchive_failed;
  321. } /* if */
  322. memset(info, '\0', sizeof (GRPinfo));
  323. info->filename = (char *) malloc(strlen(name) + 1);
  324. if (info->filename == NULL)
  325. {
  326. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  327. goto GRP_openArchive_failed;
  328. } /* if */
  329. if (!grp_load_entries(name, forWriting, info))
  330. goto GRP_openArchive_failed;
  331. strcpy(info->filename, name);
  332. info->last_mod_time = modtime;
  333. retval->funcs = &__PHYSFS_DirFunctions_GRP;
  334. return(retval);
  335. GRP_openArchive_failed:
  336. if (retval != NULL)
  337. {
  338. if (retval->opaque != NULL)
  339. {
  340. if (info->filename != NULL)
  341. free(info->filename);
  342. if (info->entries != NULL)
  343. free(info->entries);
  344. free(info);
  345. } /* if */
  346. free(retval);
  347. } /* if */
  348. return(NULL);
  349. } /* GRP_openArchive */
  350. static LinkedStringList *GRP_enumerateFiles(DirHandle *h,
  351. const char *dirname,
  352. int omitSymLinks)
  353. {
  354. GRPinfo *info = ((GRPinfo *) h->opaque);
  355. GRPentry *entry = info->entries;
  356. LinkedStringList *retval = NULL, *p = NULL;
  357. PHYSFS_uint32 max = info->entryCount;
  358. PHYSFS_uint32 i;
  359. /* no directories in GRP files. */
  360. BAIL_IF_MACRO(*dirname != '\0', ERR_NOT_A_DIR, NULL);
  361. for (i = 0; i < max; i++, entry++)
  362. retval = __PHYSFS_addToLinkedStringList(retval, &p, entry->name, -1);
  363. return(retval);
  364. } /* GRP_enumerateFiles */
  365. static GRPentry *grp_find_entry(GRPinfo *info, const char *name)
  366. {
  367. char *ptr = strchr(name, '.');
  368. GRPentry *a = info->entries;
  369. PHYSFS_sint32 lo = 0;
  370. PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
  371. PHYSFS_sint32 middle;
  372. int rc;
  373. /*
  374. * Rule out filenames to avoid unneeded processing...no dirs,
  375. * big filenames, or extensions > 3 chars.
  376. */
  377. BAIL_IF_MACRO((ptr) && (strlen(ptr) > 4), ERR_NO_SUCH_FILE, NULL);
  378. BAIL_IF_MACRO(strlen(name) > 12, ERR_NO_SUCH_FILE, NULL);
  379. BAIL_IF_MACRO(strchr(name, '/') != NULL, ERR_NO_SUCH_FILE, NULL);
  380. while (lo <= hi)
  381. {
  382. middle = lo + ((hi - lo) / 2);
  383. rc = strcmp(name, a[middle].name);
  384. if (rc == 0) /* found it! */
  385. return(&a[middle]);
  386. else if (rc > 0)
  387. lo = middle + 1;
  388. else
  389. hi = middle - 1;
  390. } /* while */
  391. BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
  392. } /* grp_find_entry */
  393. static int GRP_exists(DirHandle *h, const char *name)
  394. {
  395. return(grp_find_entry(((GRPinfo *) h->opaque), name) != NULL);
  396. } /* GRP_exists */
  397. static int GRP_isDirectory(DirHandle *h, const char *name)
  398. {
  399. return(0); /* never directories in a groupfile. */
  400. } /* GRP_isDirectory */
  401. static int GRP_isSymLink(DirHandle *h, const char *name)
  402. {
  403. return(0); /* never symlinks in a groupfile. */
  404. } /* GRP_isSymLink */
  405. static PHYSFS_sint64 GRP_getLastModTime(DirHandle *h, const char *name)
  406. {
  407. GRPinfo *info = ((GRPinfo *) h->opaque);
  408. if (grp_find_entry(info, name) == NULL)
  409. return(-1); /* no such entry. */
  410. /* Just return the time of the GRP itself in the physical filesystem. */
  411. return(((GRPinfo *) h->opaque)->last_mod_time);
  412. } /* GRP_getLastModTime */
  413. static FileHandle *GRP_openRead(DirHandle *h, const char *name)
  414. {
  415. GRPinfo *info = ((GRPinfo *) h->opaque);
  416. FileHandle *retval;
  417. GRPfileinfo *finfo;
  418. GRPentry *entry;
  419. entry = grp_find_entry(info, name);
  420. BAIL_IF_MACRO(entry == NULL, NULL, NULL);
  421. retval = (FileHandle *) malloc(sizeof (FileHandle));
  422. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  423. finfo = (GRPfileinfo *) malloc(sizeof (GRPfileinfo));
  424. if (finfo == NULL)
  425. {
  426. free(retval);
  427. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  428. } /* if */
  429. finfo->handle = __PHYSFS_platformOpenRead(info->filename);
  430. if ( (finfo->handle == NULL) ||
  431. (!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
  432. {
  433. free(finfo);
  434. free(retval);
  435. return(NULL);
  436. } /* if */
  437. finfo->curPos = 0;
  438. finfo->entry = entry;
  439. retval->opaque = (void *) finfo;
  440. retval->funcs = &__PHYSFS_FileFunctions_GRP;
  441. retval->dirHandle = h;
  442. return(retval);
  443. } /* GRP_openRead */
  444. #endif /* defined PHYSFS_SUPPORTS_GRP */
  445. /* end of grp.c ... */