grp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. typedef struct
  40. {
  41. char name[13];
  42. PHYSFS_uint32 startPos;
  43. PHYSFS_uint32 size;
  44. } GRPentry;
  45. typedef struct
  46. {
  47. char *filename;
  48. PHYSFS_sint64 last_mod_time;
  49. PHYSFS_uint32 entryCount;
  50. GRPentry *entries;
  51. } GRPinfo;
  52. typedef struct
  53. {
  54. void *handle;
  55. GRPentry *entry;
  56. PHYSFS_uint32 curPos;
  57. } GRPfileinfo;
  58. static void GRP_dirClose(DirHandle *h);
  59. static PHYSFS_sint64 GRP_read(FileHandle *handle, void *buffer,
  60. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
  61. static PHYSFS_sint64 GRP_write(FileHandle *handle, const void *buffer,
  62. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
  63. static int GRP_eof(FileHandle *handle);
  64. static PHYSFS_sint64 GRP_tell(FileHandle *handle);
  65. static int GRP_seek(FileHandle *handle, PHYSFS_uint64 offset);
  66. static PHYSFS_sint64 GRP_fileLength(FileHandle *handle);
  67. static int GRP_fileClose(FileHandle *handle);
  68. static int GRP_isArchive(const char *filename, int forWriting);
  69. static DirHandle *GRP_openArchive(const char *name, int forWriting);
  70. static LinkedStringList *GRP_enumerateFiles(DirHandle *h,
  71. const char *dirname,
  72. int omitSymLinks);
  73. static int GRP_exists(DirHandle *h, const char *name);
  74. static int GRP_isDirectory(DirHandle *h, const char *name, int *fileExists);
  75. static int GRP_isSymLink(DirHandle *h, const char *name, int *fileExists);
  76. static PHYSFS_sint64 GRP_getLastModTime(DirHandle *h, const char *n, int *e);
  77. static FileHandle *GRP_openRead(DirHandle *h, const char *name, int *exist);
  78. static FileHandle *GRP_openWrite(DirHandle *h, const char *name);
  79. static FileHandle *GRP_openAppend(DirHandle *h, const char *name);
  80. static int GRP_remove(DirHandle *h, const char *name);
  81. static int GRP_mkdir(DirHandle *h, const char *name);
  82. const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_GRP =
  83. {
  84. "GRP",
  85. GRP_ARCHIVE_DESCRIPTION,
  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. GRP_write, /* 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. GRP_openWrite, /* openWrite() method */
  111. GRP_openAppend, /* openAppend() method */
  112. GRP_remove, /* remove() method */
  113. GRP_mkdir, /* 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_uint32 bytesLeft = entry->size - finfo->curPos;
  130. PHYSFS_uint32 objsLeft = (bytesLeft / objSize);
  131. PHYSFS_sint64 rc;
  132. if (objsLeft < objCount)
  133. objCount = objsLeft;
  134. rc = __PHYSFS_platformRead(finfo->handle, buffer, objSize, objCount);
  135. if (rc > 0)
  136. finfo->curPos += (PHYSFS_uint32) (rc * objSize);
  137. return(rc);
  138. } /* GRP_read */
  139. static PHYSFS_sint64 GRP_write(FileHandle *handle, const void *buffer,
  140. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  141. {
  142. BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
  143. } /* GRP_write */
  144. static int GRP_eof(FileHandle *handle)
  145. {
  146. GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
  147. GRPentry *entry = finfo->entry;
  148. return(finfo->curPos >= entry->size);
  149. } /* GRP_eof */
  150. static PHYSFS_sint64 GRP_tell(FileHandle *handle)
  151. {
  152. return(((GRPfileinfo *) (handle->opaque))->curPos);
  153. } /* GRP_tell */
  154. static int GRP_seek(FileHandle *handle, PHYSFS_uint64 offset)
  155. {
  156. GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
  157. GRPentry *entry = finfo->entry;
  158. int rc;
  159. BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
  160. BAIL_IF_MACRO(offset >= entry->size, ERR_PAST_EOF, 0);
  161. rc = __PHYSFS_platformSeek(finfo->handle, entry->startPos + offset);
  162. if (rc)
  163. finfo->curPos = (PHYSFS_uint32) offset;
  164. return(rc);
  165. } /* GRP_seek */
  166. static PHYSFS_sint64 GRP_fileLength(FileHandle *handle)
  167. {
  168. GRPfileinfo *finfo = ((GRPfileinfo *) handle->opaque);
  169. return((PHYSFS_sint64) finfo->entry->size);
  170. } /* GRP_fileLength */
  171. static int GRP_fileClose(FileHandle *handle)
  172. {
  173. GRPfileinfo *finfo = ((GRPfileinfo *) handle->opaque);
  174. BAIL_IF_MACRO(!__PHYSFS_platformClose(finfo->handle), NULL, 0);
  175. free(finfo);
  176. free(handle);
  177. return(1);
  178. } /* GRP_fileClose */
  179. static int grp_open(const char *filename, int forWriting,
  180. void **fh, PHYSFS_uint32 *count)
  181. {
  182. PHYSFS_uint8 buf[12];
  183. *fh = NULL;
  184. BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
  185. *fh = __PHYSFS_platformOpenRead(filename);
  186. BAIL_IF_MACRO(*fh == NULL, NULL, 0);
  187. if (__PHYSFS_platformRead(*fh, buf, 12, 1) != 1)
  188. goto openGrp_failed;
  189. if (memcmp(buf, "KenSilverman", 12) != 0)
  190. {
  191. __PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE);
  192. goto openGrp_failed;
  193. } /* if */
  194. if (__PHYSFS_platformRead(*fh, count, sizeof (PHYSFS_uint32), 1) != 1)
  195. goto openGrp_failed;
  196. *count = PHYSFS_swapULE32(*count);
  197. return(1);
  198. openGrp_failed:
  199. if (*fh != NULL)
  200. __PHYSFS_platformClose(*fh);
  201. *count = -1;
  202. *fh = NULL;
  203. return(0);
  204. } /* grp_open */
  205. static int GRP_isArchive(const char *filename, int forWriting)
  206. {
  207. void *fh;
  208. PHYSFS_uint32 fileCount;
  209. int retval = grp_open(filename, forWriting, &fh, &fileCount);
  210. if (fh != NULL)
  211. __PHYSFS_platformClose(fh);
  212. return(retval);
  213. } /* GRP_isArchive */
  214. static int grp_entry_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
  215. {
  216. GRPentry *a = (GRPentry *) _a;
  217. return(strcmp(a[one].name, a[two].name));
  218. } /* grp_entry_cmp */
  219. static void grp_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
  220. {
  221. GRPentry tmp;
  222. GRPentry *first = &(((GRPentry *) _a)[one]);
  223. GRPentry *second = &(((GRPentry *) _a)[two]);
  224. memcpy(&tmp, first, sizeof (GRPentry));
  225. memcpy(first, second, sizeof (GRPentry));
  226. memcpy(second, &tmp, sizeof (GRPentry));
  227. } /* grp_entry_swap */
  228. static int grp_load_entries(const char *name, int forWriting, GRPinfo *info)
  229. {
  230. void *fh = NULL;
  231. PHYSFS_uint32 fileCount;
  232. PHYSFS_uint32 location = 16; /* sizeof sig. */
  233. GRPentry *entry;
  234. char *ptr;
  235. BAIL_IF_MACRO(!grp_open(name, forWriting, &fh, &fileCount), NULL, 0);
  236. info->entryCount = fileCount;
  237. info->entries = (GRPentry *) malloc(sizeof (GRPentry) * fileCount);
  238. if (info->entries == NULL)
  239. {
  240. __PHYSFS_platformClose(fh);
  241. BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
  242. } /* if */
  243. location += (16 * fileCount);
  244. for (entry = info->entries; fileCount > 0; fileCount--, entry++)
  245. {
  246. if (__PHYSFS_platformRead(fh, &entry->name, 12, 1) != 1)
  247. {
  248. __PHYSFS_platformClose(fh);
  249. return(0);
  250. } /* if */
  251. entry->name[12] = '\0'; /* name isn't null-terminated in file. */
  252. if ((ptr = strchr(entry->name, ' ')) != NULL)
  253. *ptr = '\0'; /* trim extra spaces. */
  254. if (__PHYSFS_platformRead(fh, &entry->size, 4, 1) != 1)
  255. {
  256. __PHYSFS_platformClose(fh);
  257. return(0);
  258. } /* if */
  259. entry->size = PHYSFS_swapULE32(entry->size);
  260. entry->startPos = location;
  261. location += entry->size;
  262. } /* for */
  263. __PHYSFS_platformClose(fh);
  264. __PHYSFS_sort(info->entries, info->entryCount,
  265. grp_entry_cmp, grp_entry_swap);
  266. return(1);
  267. } /* grp_load_entries */
  268. static DirHandle *GRP_openArchive(const char *name, int forWriting)
  269. {
  270. GRPinfo *info;
  271. DirHandle *retval = malloc(sizeof (DirHandle));
  272. PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
  273. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  274. info = retval->opaque = malloc(sizeof (GRPinfo));
  275. if (info == NULL)
  276. {
  277. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  278. goto GRP_openArchive_failed;
  279. } /* if */
  280. memset(info, '\0', sizeof (GRPinfo));
  281. info->filename = (char *) malloc(strlen(name) + 1);
  282. if (info->filename == NULL)
  283. {
  284. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  285. goto GRP_openArchive_failed;
  286. } /* if */
  287. if (!grp_load_entries(name, forWriting, info))
  288. goto GRP_openArchive_failed;
  289. strcpy(info->filename, name);
  290. info->last_mod_time = modtime;
  291. retval->funcs = &__PHYSFS_DirFunctions_GRP;
  292. return(retval);
  293. GRP_openArchive_failed:
  294. if (retval != NULL)
  295. {
  296. if (retval->opaque != NULL)
  297. {
  298. if (info->filename != NULL)
  299. free(info->filename);
  300. if (info->entries != NULL)
  301. free(info->entries);
  302. free(info);
  303. } /* if */
  304. free(retval);
  305. } /* if */
  306. return(NULL);
  307. } /* GRP_openArchive */
  308. static LinkedStringList *GRP_enumerateFiles(DirHandle *h,
  309. const char *dirname,
  310. int omitSymLinks)
  311. {
  312. GRPinfo *info = ((GRPinfo *) h->opaque);
  313. GRPentry *entry = info->entries;
  314. LinkedStringList *retval = NULL, *p = NULL;
  315. PHYSFS_uint32 max = info->entryCount;
  316. PHYSFS_uint32 i;
  317. /* no directories in GRP files. */
  318. BAIL_IF_MACRO(*dirname != '\0', ERR_NOT_A_DIR, NULL);
  319. for (i = 0; i < max; i++, entry++)
  320. retval = __PHYSFS_addToLinkedStringList(retval, &p, entry->name, -1);
  321. return(retval);
  322. } /* GRP_enumerateFiles */
  323. static GRPentry *grp_find_entry(GRPinfo *info, const char *name)
  324. {
  325. char *ptr = strchr(name, '.');
  326. GRPentry *a = info->entries;
  327. PHYSFS_sint32 lo = 0;
  328. PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
  329. PHYSFS_sint32 middle;
  330. int rc;
  331. /*
  332. * Rule out filenames to avoid unneeded processing...no dirs,
  333. * big filenames, or extensions > 3 chars.
  334. */
  335. BAIL_IF_MACRO((ptr) && (strlen(ptr) > 4), ERR_NO_SUCH_FILE, NULL);
  336. BAIL_IF_MACRO(strlen(name) > 12, ERR_NO_SUCH_FILE, NULL);
  337. BAIL_IF_MACRO(strchr(name, '/') != NULL, ERR_NO_SUCH_FILE, NULL);
  338. while (lo <= hi)
  339. {
  340. middle = lo + ((hi - lo) / 2);
  341. rc = strcmp(name, a[middle].name);
  342. if (rc == 0) /* found it! */
  343. return(&a[middle]);
  344. else if (rc > 0)
  345. lo = middle + 1;
  346. else
  347. hi = middle - 1;
  348. } /* while */
  349. BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
  350. } /* grp_find_entry */
  351. static int GRP_exists(DirHandle *h, const char *name)
  352. {
  353. return(grp_find_entry(((GRPinfo *) h->opaque), name) != NULL);
  354. } /* GRP_exists */
  355. static int GRP_isDirectory(DirHandle *h, const char *name, int *fileExists)
  356. {
  357. *fileExists = GRP_exists(h, name);
  358. return(0); /* never directories in a groupfile. */
  359. } /* GRP_isDirectory */
  360. static int GRP_isSymLink(DirHandle *h, const char *name, int *fileExists)
  361. {
  362. *fileExists = GRP_exists(h, name);
  363. return(0); /* never symlinks in a groupfile. */
  364. } /* GRP_isSymLink */
  365. static PHYSFS_sint64 GRP_getLastModTime(DirHandle *h,
  366. const char *name,
  367. int *fileExists)
  368. {
  369. GRPinfo *info = ((GRPinfo *) h->opaque);
  370. PHYSFS_sint64 retval = -1;
  371. *fileExists = (grp_find_entry(info, name) != NULL);
  372. if (*fileExists) /* use time of GRP itself in the physical filesystem. */
  373. retval = ((GRPinfo *) h->opaque)->last_mod_time;
  374. return(retval);
  375. } /* GRP_getLastModTime */
  376. static FileHandle *GRP_openRead(DirHandle *h, const char *fnm, int *fileExists)
  377. {
  378. GRPinfo *info = ((GRPinfo *) h->opaque);
  379. FileHandle *retval;
  380. GRPfileinfo *finfo;
  381. GRPentry *entry;
  382. entry = grp_find_entry(info, fnm);
  383. *fileExists = (entry != NULL);
  384. BAIL_IF_MACRO(entry == NULL, NULL, NULL);
  385. retval = (FileHandle *) malloc(sizeof (FileHandle));
  386. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  387. finfo = (GRPfileinfo *) malloc(sizeof (GRPfileinfo));
  388. if (finfo == NULL)
  389. {
  390. free(retval);
  391. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  392. } /* if */
  393. finfo->handle = __PHYSFS_platformOpenRead(info->filename);
  394. if ( (finfo->handle == NULL) ||
  395. (!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
  396. {
  397. free(finfo);
  398. free(retval);
  399. return(NULL);
  400. } /* if */
  401. finfo->curPos = 0;
  402. finfo->entry = entry;
  403. retval->opaque = (void *) finfo;
  404. retval->funcs = &__PHYSFS_FileFunctions_GRP;
  405. retval->dirHandle = h;
  406. return(retval);
  407. } /* GRP_openRead */
  408. static FileHandle *GRP_openWrite(DirHandle *h, const char *name)
  409. {
  410. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  411. } /* GRP_openWrite */
  412. static FileHandle *GRP_openAppend(DirHandle *h, const char *name)
  413. {
  414. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  415. } /* GRP_openAppend */
  416. static int GRP_remove(DirHandle *h, const char *name)
  417. {
  418. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  419. } /* GRP_remove */
  420. static int GRP_mkdir(DirHandle *h, const char *name)
  421. {
  422. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  423. } /* GRP_mkdir */
  424. #endif /* defined PHYSFS_SUPPORTS_GRP */
  425. /* end of grp.c ... */