grp.c 14 KB

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