grp.c 15 KB

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