grp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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. static const FileFunctions __PHYSFS_FileFunctions_GRP =
  83. {
  84. GRP_read, /* read() method */
  85. NULL, /* write() method */
  86. GRP_eof, /* eof() method */
  87. GRP_tell, /* tell() method */
  88. GRP_seek, /* seek() method */
  89. GRP_fileLength, /* fileLength() method */
  90. GRP_fileClose /* fileClose() method */
  91. };
  92. const DirFunctions __PHYSFS_DirFunctions_GRP =
  93. {
  94. GRP_isArchive, /* isArchive() method */
  95. GRP_openArchive, /* openArchive() method */
  96. GRP_enumerateFiles, /* enumerateFiles() method */
  97. GRP_exists, /* exists() method */
  98. GRP_isDirectory, /* isDirectory() method */
  99. GRP_isSymLink, /* isSymLink() method */
  100. GRP_getLastModTime, /* getLastModTime() method */
  101. GRP_openRead, /* openRead() method */
  102. NULL, /* openWrite() method */
  103. NULL, /* openAppend() method */
  104. NULL, /* remove() method */
  105. NULL, /* mkdir() method */
  106. GRP_dirClose /* dirClose() method */
  107. };
  108. const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_GRP =
  109. {
  110. "GRP",
  111. "Build engine Groupfile format",
  112. "Ryan C. Gordon <icculus@clutteredmind.org>",
  113. "http://www.icculus.org/physfs/",
  114. };
  115. static void GRP_dirClose(DirHandle *h)
  116. {
  117. GRPinfo *info = ((GRPinfo *) h->opaque);
  118. free(info->filename);
  119. free(info->entries);
  120. free(info);
  121. free(h);
  122. } /* GRP_dirClose */
  123. static PHYSFS_sint64 GRP_read(FileHandle *handle, void *buffer,
  124. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  125. {
  126. GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
  127. GRPentry *entry = finfo->entry;
  128. PHYSFS_uint64 bytesLeft = entry->size - finfo->curPos;
  129. PHYSFS_uint64 objsLeft = (bytesLeft / objSize);
  130. PHYSFS_sint64 rc;
  131. if (objsLeft < objCount)
  132. objCount = (PHYSFS_uint32) objsLeft;
  133. rc = __PHYSFS_platformRead(finfo->handle, buffer, objSize, objCount);
  134. if (rc > 0)
  135. finfo->curPos += (rc * objSize);
  136. return(rc);
  137. } /* GRP_read */
  138. static int GRP_eof(FileHandle *handle)
  139. {
  140. GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
  141. GRPentry *entry = finfo->entry;
  142. return(finfo->curPos >= (PHYSFS_sint64) entry->size);
  143. } /* GRP_eof */
  144. static PHYSFS_sint64 GRP_tell(FileHandle *handle)
  145. {
  146. return(((GRPfileinfo *) (handle->opaque))->curPos);
  147. } /* GRP_tell */
  148. static int GRP_seek(FileHandle *handle, PHYSFS_uint64 offset)
  149. {
  150. GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
  151. GRPentry *entry = finfo->entry;
  152. PHYSFS_uint64 newPos = (entry->startPos + offset);
  153. int rc;
  154. BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
  155. BAIL_IF_MACRO(newPos > entry->startPos + entry->size, ERR_PAST_EOF, 0);
  156. rc = __PHYSFS_platformSeek(finfo->handle, newPos);
  157. if (rc)
  158. finfo->curPos = offset;
  159. return(rc);
  160. } /* GRP_seek */
  161. static PHYSFS_sint64 GRP_fileLength(FileHandle *handle)
  162. {
  163. return(((GRPfileinfo *) handle->opaque)->entry->size);
  164. } /* GRP_fileLength */
  165. static int GRP_fileClose(FileHandle *handle)
  166. {
  167. GRPfileinfo *finfo = ((GRPfileinfo *) handle->opaque);
  168. BAIL_IF_MACRO(!__PHYSFS_platformClose(finfo->handle), NULL, 0);
  169. free(finfo);
  170. free(handle);
  171. return(1);
  172. } /* GRP_fileClose */
  173. static int grp_open(const char *filename, int forWriting,
  174. void **fh, PHYSFS_uint32 *count)
  175. {
  176. PHYSFS_uint8 buf[12];
  177. *fh = NULL;
  178. BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
  179. *fh = __PHYSFS_platformOpenRead(filename);
  180. BAIL_IF_MACRO(*fh == NULL, NULL, 0);
  181. if (__PHYSFS_platformRead(*fh, buf, 12, 1) != 1)
  182. goto openGrp_failed;
  183. if (memcmp(buf, "KenSilverman", 12) != 0)
  184. {
  185. __PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE);
  186. goto openGrp_failed;
  187. } /* if */
  188. if (__PHYSFS_platformRead(*fh, count, sizeof (PHYSFS_uint32), 1) != 1)
  189. goto openGrp_failed;
  190. *count = PHYSFS_swapULE32(*count);
  191. return(1);
  192. openGrp_failed:
  193. if (*fh != NULL)
  194. __PHYSFS_platformClose(*fh);
  195. *count = -1;
  196. *fh = NULL;
  197. return(0);
  198. } /* grp_open */
  199. static int GRP_isArchive(const char *filename, int forWriting)
  200. {
  201. void *fh;
  202. PHYSFS_uint32 fileCount;
  203. int retval = grp_open(filename, forWriting, &fh, &fileCount);
  204. if (fh != NULL)
  205. __PHYSFS_platformClose(fh);
  206. return(retval);
  207. } /* GRP_isArchive */
  208. static void grp_entry_swap(GRPentry *a, PHYSFS_uint32 one, PHYSFS_uint32 two)
  209. {
  210. GRPentry tmp;
  211. memcpy(&tmp, &a[one], sizeof (GRPentry));
  212. memcpy(&a[one], &a[two], sizeof (GRPentry));
  213. memcpy(&a[two], &tmp, sizeof (GRPentry));
  214. } /* grp_entry_swap */
  215. static void grp_quick_sort(GRPentry *a, PHYSFS_uint32 lo, PHYSFS_uint32 hi)
  216. {
  217. PHYSFS_uint32 i;
  218. PHYSFS_uint32 j;
  219. GRPentry *v;
  220. if ((hi - lo) > GRP_QUICKSORT_THRESHOLD)
  221. {
  222. i = (hi + lo) / 2;
  223. if (strcmp(a[lo].name, a[i].name) > 0) grp_entry_swap(a, lo, i);
  224. if (strcmp(a[lo].name, a[hi].name) > 0) grp_entry_swap(a, lo, hi);
  225. if (strcmp(a[i].name, a[hi].name) > 0) grp_entry_swap(a, i, hi);
  226. j = hi - 1;
  227. grp_entry_swap(a, i, j);
  228. i = lo;
  229. v = &a[j];
  230. while (1)
  231. {
  232. while(strcmp(a[++i].name, v->name) < 0) {}
  233. while(strcmp(a[--j].name, v->name) > 0) {}
  234. if (j < i)
  235. break;
  236. grp_entry_swap(a, i, j);
  237. } /* while */
  238. grp_entry_swap(a, i, hi-1);
  239. grp_quick_sort(a, lo, j);
  240. grp_quick_sort(a, i+1, hi);
  241. } /* if */
  242. } /* grp_quick_sort */
  243. static void grp_insertion_sort(GRPentry *a, PHYSFS_uint32 lo, PHYSFS_uint32 hi)
  244. {
  245. PHYSFS_uint32 i;
  246. PHYSFS_uint32 j;
  247. GRPentry tmp;
  248. for (i = lo + 1; i <= hi; i++)
  249. {
  250. memcpy(&tmp, &a[i], sizeof (GRPentry));
  251. j = i;
  252. while ((j > lo) && (strcmp(a[j - 1].name, tmp.name) > 0))
  253. {
  254. memcpy(&a[j], &a[j - 1], sizeof (GRPentry));
  255. j--;
  256. } /* while */
  257. memcpy(&a[j], &tmp, sizeof (GRPentry));
  258. } /* for */
  259. } /* grp_insertion_sort */
  260. static void grp_sort_entries(GRPentry *entries, PHYSFS_uint32 max)
  261. {
  262. /*
  263. * Fast Quicksort algorithm inspired by code from here:
  264. * http://www.cs.ubc.ca/spider/harrison/Java/sorting-demo.html
  265. */
  266. if (max <= GRP_QUICKSORT_THRESHOLD)
  267. grp_quick_sort(entries, 0, max - 1);
  268. grp_insertion_sort(entries, 0, max - 1);
  269. } /* grp_sort_entries */
  270. static int grp_load_entries(const char *name, int forWriting, GRPinfo *info)
  271. {
  272. void *fh = NULL;
  273. PHYSFS_uint32 fileCount;
  274. PHYSFS_uint32 location = 32; /* sizeof sig + sizeof 1st file header. */
  275. GRPentry *entry;
  276. char *ptr;
  277. BAIL_IF_MACRO(!grp_open(name, forWriting, &fh, &fileCount), NULL, 0);
  278. info->entryCount = fileCount;
  279. info->entries = (GRPentry *) malloc(sizeof (GRPentry) * fileCount);
  280. if (info->entries == NULL)
  281. {
  282. __PHYSFS_platformClose(fh);
  283. BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
  284. } /* if */
  285. for (entry = info->entries; fileCount > 0; fileCount--, entry++)
  286. {
  287. if (__PHYSFS_platformRead(fh, &entry->name, 12, 1) != 1)
  288. {
  289. __PHYSFS_platformClose(fh);
  290. return(0);
  291. } /* if */
  292. entry->name[12] = '\0'; /* name isn't null-terminated in file. */
  293. if ((ptr = strchr(entry->name, ' ')) != NULL)
  294. *ptr = '\0'; /* trim extra spaces. */
  295. if (__PHYSFS_platformRead(fh, &entry->size, 4, 1) != 1)
  296. {
  297. __PHYSFS_platformClose(fh);
  298. return(0);
  299. } /* if */
  300. entry->size = PHYSFS_swapULE32(entry->size);
  301. entry->startPos = location;
  302. location += entry->size + 16;
  303. } /* for */
  304. __PHYSFS_platformClose(fh);
  305. grp_sort_entries(info->entries, info->entryCount);
  306. return(1);
  307. } /* grp_load_entries */
  308. static DirHandle *GRP_openArchive(const char *name, int forWriting)
  309. {
  310. GRPinfo *info;
  311. DirHandle *retval = malloc(sizeof (DirHandle));
  312. PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
  313. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  314. info = retval->opaque = malloc(sizeof (GRPinfo));
  315. if (info == NULL)
  316. {
  317. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  318. goto GRP_openArchive_failed;
  319. } /* if */
  320. memset(info, '\0', sizeof (GRPinfo));
  321. info->filename = (char *) malloc(strlen(name) + 1);
  322. if (info->filename == NULL)
  323. {
  324. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  325. goto GRP_openArchive_failed;
  326. } /* if */
  327. if (!grp_load_entries(name, forWriting, info))
  328. goto GRP_openArchive_failed;
  329. strcpy(info->filename, name);
  330. info->last_mod_time = modtime;
  331. retval->funcs = &__PHYSFS_DirFunctions_GRP;
  332. return(retval);
  333. GRP_openArchive_failed:
  334. if (retval != NULL)
  335. {
  336. if (retval->opaque != NULL)
  337. {
  338. if (info->filename != NULL)
  339. free(info->filename);
  340. if (info->entries != NULL)
  341. free(info->entries);
  342. free(info);
  343. } /* if */
  344. free(retval);
  345. } /* if */
  346. return(NULL);
  347. } /* GRP_openArchive */
  348. static LinkedStringList *GRP_enumerateFiles(DirHandle *h,
  349. const char *dirname,
  350. int omitSymLinks)
  351. {
  352. GRPinfo *info = ((GRPinfo *) h->opaque);
  353. GRPentry *entry = info->entries;
  354. LinkedStringList *retval = NULL, *p = NULL;
  355. PHYSFS_uint32 max = info->entryCount;
  356. PHYSFS_uint32 i;
  357. /* no directories in GRP files. */
  358. BAIL_IF_MACRO(*dirname != '\0', ERR_NOT_A_DIR, NULL);
  359. for (i = 0; i < max; i++, entry++)
  360. retval = __PHYSFS_addToLinkedStringList(retval, &p, entry->name, -1);
  361. return(retval);
  362. } /* GRP_enumerateFiles */
  363. static GRPentry *grp_find_entry(GRPinfo *info, const char *name)
  364. {
  365. char *ptr = strchr(name, '.');
  366. GRPentry *a = info->entries;
  367. PHYSFS_sint32 lo = 0;
  368. PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
  369. PHYSFS_sint32 middle;
  370. int rc;
  371. /*
  372. * Rule out filenames to avoid unneeded processing...no dirs,
  373. * big filenames, or extensions > 3 chars.
  374. */
  375. BAIL_IF_MACRO((ptr) && (strlen(ptr) > 4), ERR_NO_SUCH_FILE, NULL);
  376. BAIL_IF_MACRO(strlen(name) > 12, ERR_NO_SUCH_FILE, NULL);
  377. BAIL_IF_MACRO(strchr(name, '/') != NULL, ERR_NO_SUCH_FILE, NULL);
  378. while (lo <= hi)
  379. {
  380. middle = lo + ((hi - lo) / 2);
  381. rc = strcmp(name, a[middle].name);
  382. if (rc == 0) /* found it! */
  383. return(&a[middle]);
  384. else if (rc > 0)
  385. lo = middle + 1;
  386. else
  387. hi = middle - 1;
  388. } /* while */
  389. BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
  390. } /* grp_find_entry */
  391. static int GRP_exists(DirHandle *h, const char *name)
  392. {
  393. return(grp_find_entry(((GRPinfo *) h->opaque), name) != NULL);
  394. } /* GRP_exists */
  395. static int GRP_isDirectory(DirHandle *h, const char *name)
  396. {
  397. return(0); /* never directories in a groupfile. */
  398. } /* GRP_isDirectory */
  399. static int GRP_isSymLink(DirHandle *h, const char *name)
  400. {
  401. return(0); /* never symlinks in a groupfile. */
  402. } /* GRP_isSymLink */
  403. static PHYSFS_sint64 GRP_getLastModTime(DirHandle *h, const char *name)
  404. {
  405. /* Just return the time of the GRP itself in the physical filesystem. */
  406. return(((GRPinfo *) h->opaque)->last_mod_time);
  407. } /* GRP_getLastModTime */
  408. static FileHandle *GRP_openRead(DirHandle *h, const char *name)
  409. {
  410. GRPinfo *info = ((GRPinfo *) h->opaque);
  411. FileHandle *retval;
  412. GRPfileinfo *finfo;
  413. GRPentry *entry;
  414. entry = grp_find_entry(info, name);
  415. BAIL_IF_MACRO(entry == NULL, NULL, NULL);
  416. retval = (FileHandle *) malloc(sizeof (FileHandle));
  417. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  418. finfo = (GRPfileinfo *) malloc(sizeof (GRPfileinfo));
  419. if (finfo == NULL)
  420. {
  421. free(retval);
  422. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  423. } /* if */
  424. finfo->handle = __PHYSFS_platformOpenRead(info->filename);
  425. if ( (finfo->handle == NULL) ||
  426. (!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
  427. {
  428. free(finfo);
  429. free(retval);
  430. return(NULL);
  431. } /* if */
  432. finfo->curPos = 0;
  433. finfo->entry = entry;
  434. retval->opaque = (void *) finfo;
  435. retval->funcs = &__PHYSFS_FileFunctions_GRP;
  436. retval->dirHandle = h;
  437. return(retval);
  438. } /* GRP_openRead */
  439. #endif /* defined PHYSFS_SUPPORTS_GRP */
  440. /* end of grp.c ... */