grp.c 13 KB

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