grp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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. if (one != two)
  214. {
  215. const GRPentry *a = (const GRPentry *) _a;
  216. return(strcmp(a[one].name, a[two].name));
  217. } /* if */
  218. return 0;
  219. } /* grp_entry_cmp */
  220. static void grp_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
  221. {
  222. if (one != two)
  223. {
  224. GRPentry tmp;
  225. GRPentry *first = &(((GRPentry *) _a)[one]);
  226. GRPentry *second = &(((GRPentry *) _a)[two]);
  227. memcpy(&tmp, first, sizeof (GRPentry));
  228. memcpy(first, second, sizeof (GRPentry));
  229. memcpy(second, &tmp, sizeof (GRPentry));
  230. } /* if */
  231. } /* grp_entry_swap */
  232. static int grp_load_entries(const char *name, int forWriting, GRPinfo *info)
  233. {
  234. void *fh = NULL;
  235. PHYSFS_uint32 fileCount;
  236. PHYSFS_uint32 location = 16; /* sizeof sig. */
  237. GRPentry *entry;
  238. char *ptr;
  239. BAIL_IF_MACRO(!grp_open(name, forWriting, &fh, &fileCount), NULL, 0);
  240. info->entryCount = fileCount;
  241. info->entries = (GRPentry *) malloc(sizeof (GRPentry) * fileCount);
  242. if (info->entries == NULL)
  243. {
  244. __PHYSFS_platformClose(fh);
  245. BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
  246. } /* if */
  247. location += (16 * fileCount);
  248. for (entry = info->entries; fileCount > 0; fileCount--, entry++)
  249. {
  250. if (__PHYSFS_platformRead(fh, &entry->name, 12, 1) != 1)
  251. {
  252. __PHYSFS_platformClose(fh);
  253. return(0);
  254. } /* if */
  255. entry->name[12] = '\0'; /* name isn't null-terminated in file. */
  256. if ((ptr = strchr(entry->name, ' ')) != NULL)
  257. *ptr = '\0'; /* trim extra spaces. */
  258. if (__PHYSFS_platformRead(fh, &entry->size, 4, 1) != 1)
  259. {
  260. __PHYSFS_platformClose(fh);
  261. return(0);
  262. } /* if */
  263. entry->size = PHYSFS_swapULE32(entry->size);
  264. entry->startPos = location;
  265. location += entry->size;
  266. } /* for */
  267. __PHYSFS_platformClose(fh);
  268. __PHYSFS_sort(info->entries, info->entryCount,
  269. grp_entry_cmp, grp_entry_swap);
  270. return(1);
  271. } /* grp_load_entries */
  272. static DirHandle *GRP_openArchive(const char *name, int forWriting)
  273. {
  274. GRPinfo *info;
  275. DirHandle *retval = malloc(sizeof (DirHandle));
  276. PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
  277. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  278. info = retval->opaque = malloc(sizeof (GRPinfo));
  279. if (info == NULL)
  280. {
  281. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  282. goto GRP_openArchive_failed;
  283. } /* if */
  284. memset(info, '\0', sizeof (GRPinfo));
  285. info->filename = (char *) malloc(strlen(name) + 1);
  286. if (info->filename == NULL)
  287. {
  288. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  289. goto GRP_openArchive_failed;
  290. } /* if */
  291. if (!grp_load_entries(name, forWriting, info))
  292. goto GRP_openArchive_failed;
  293. strcpy(info->filename, name);
  294. info->last_mod_time = modtime;
  295. retval->funcs = &__PHYSFS_DirFunctions_GRP;
  296. return(retval);
  297. GRP_openArchive_failed:
  298. if (retval != NULL)
  299. {
  300. if (retval->opaque != NULL)
  301. {
  302. if (info->filename != NULL)
  303. free(info->filename);
  304. if (info->entries != NULL)
  305. free(info->entries);
  306. free(info);
  307. } /* if */
  308. free(retval);
  309. } /* if */
  310. return(NULL);
  311. } /* GRP_openArchive */
  312. static LinkedStringList *GRP_enumerateFiles(DirHandle *h,
  313. const char *dirname,
  314. int omitSymLinks)
  315. {
  316. GRPinfo *info = ((GRPinfo *) h->opaque);
  317. GRPentry *entry = info->entries;
  318. LinkedStringList *retval = NULL, *p = NULL;
  319. PHYSFS_uint32 max = info->entryCount;
  320. PHYSFS_uint32 i;
  321. /* no directories in GRP files. */
  322. BAIL_IF_MACRO(*dirname != '\0', ERR_NOT_A_DIR, NULL);
  323. for (i = 0; i < max; i++, entry++)
  324. retval = __PHYSFS_addToLinkedStringList(retval, &p, entry->name, -1);
  325. return(retval);
  326. } /* GRP_enumerateFiles */
  327. static GRPentry *grp_find_entry(GRPinfo *info, const char *name)
  328. {
  329. char *ptr = strchr(name, '.');
  330. GRPentry *a = info->entries;
  331. PHYSFS_sint32 lo = 0;
  332. PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
  333. PHYSFS_sint32 middle;
  334. int rc;
  335. /*
  336. * Rule out filenames to avoid unneeded processing...no dirs,
  337. * big filenames, or extensions > 3 chars.
  338. */
  339. BAIL_IF_MACRO((ptr) && (strlen(ptr) > 4), ERR_NO_SUCH_FILE, NULL);
  340. BAIL_IF_MACRO(strlen(name) > 12, ERR_NO_SUCH_FILE, NULL);
  341. BAIL_IF_MACRO(strchr(name, '/') != NULL, ERR_NO_SUCH_FILE, NULL);
  342. while (lo <= hi)
  343. {
  344. middle = lo + ((hi - lo) / 2);
  345. rc = strcmp(name, a[middle].name);
  346. if (rc == 0) /* found it! */
  347. return(&a[middle]);
  348. else if (rc > 0)
  349. lo = middle + 1;
  350. else
  351. hi = middle - 1;
  352. } /* while */
  353. BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
  354. } /* grp_find_entry */
  355. static int GRP_exists(DirHandle *h, const char *name)
  356. {
  357. return(grp_find_entry(((GRPinfo *) h->opaque), name) != NULL);
  358. } /* GRP_exists */
  359. static int GRP_isDirectory(DirHandle *h, const char *name, int *fileExists)
  360. {
  361. *fileExists = GRP_exists(h, name);
  362. return(0); /* never directories in a groupfile. */
  363. } /* GRP_isDirectory */
  364. static int GRP_isSymLink(DirHandle *h, const char *name, int *fileExists)
  365. {
  366. *fileExists = GRP_exists(h, name);
  367. return(0); /* never symlinks in a groupfile. */
  368. } /* GRP_isSymLink */
  369. static PHYSFS_sint64 GRP_getLastModTime(DirHandle *h,
  370. const char *name,
  371. int *fileExists)
  372. {
  373. GRPinfo *info = ((GRPinfo *) h->opaque);
  374. PHYSFS_sint64 retval = -1;
  375. *fileExists = (grp_find_entry(info, name) != NULL);
  376. if (*fileExists) /* use time of GRP itself in the physical filesystem. */
  377. retval = ((GRPinfo *) h->opaque)->last_mod_time;
  378. return(retval);
  379. } /* GRP_getLastModTime */
  380. static FileHandle *GRP_openRead(DirHandle *h, const char *fnm, int *fileExists)
  381. {
  382. GRPinfo *info = ((GRPinfo *) h->opaque);
  383. FileHandle *retval;
  384. GRPfileinfo *finfo;
  385. GRPentry *entry;
  386. entry = grp_find_entry(info, fnm);
  387. *fileExists = (entry != NULL);
  388. BAIL_IF_MACRO(entry == NULL, NULL, NULL);
  389. retval = (FileHandle *) malloc(sizeof (FileHandle));
  390. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  391. finfo = (GRPfileinfo *) malloc(sizeof (GRPfileinfo));
  392. if (finfo == NULL)
  393. {
  394. free(retval);
  395. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  396. } /* if */
  397. finfo->handle = __PHYSFS_platformOpenRead(info->filename);
  398. if ( (finfo->handle == NULL) ||
  399. (!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
  400. {
  401. free(finfo);
  402. free(retval);
  403. return(NULL);
  404. } /* if */
  405. finfo->curPos = 0;
  406. finfo->entry = entry;
  407. retval->opaque = (void *) finfo;
  408. retval->funcs = &__PHYSFS_FileFunctions_GRP;
  409. retval->dirHandle = h;
  410. return(retval);
  411. } /* GRP_openRead */
  412. static FileHandle *GRP_openWrite(DirHandle *h, const char *name)
  413. {
  414. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  415. } /* GRP_openWrite */
  416. static FileHandle *GRP_openAppend(DirHandle *h, const char *name)
  417. {
  418. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  419. } /* GRP_openAppend */
  420. static int GRP_remove(DirHandle *h, const char *name)
  421. {
  422. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  423. } /* GRP_remove */
  424. static int GRP_mkdir(DirHandle *h, const char *name)
  425. {
  426. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  427. } /* GRP_mkdir */
  428. #endif /* defined PHYSFS_SUPPORTS_GRP */
  429. /* end of grp.c ... */