grp.c 13 KB

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