grp.c 15 KB

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