grp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. GRPentry *a = (GRPentry *) _a;
  151. return(strcmp(a[one].name, a[two].name));
  152. } /* grp_entry_cmp */
  153. static void grp_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
  154. {
  155. GRPentry tmp;
  156. GRPentry *first = &(((GRPentry *) _a)[one]);
  157. GRPentry *second = &(((GRPentry *) _a)[two]);
  158. memcpy(&tmp, first, sizeof (GRPentry));
  159. memcpy(first, second, sizeof (GRPentry));
  160. memcpy(second, &tmp, sizeof (GRPentry));
  161. } /* grp_entry_swap */
  162. static int grp_load_entries(const char *name, int forWriting, GRPinfo *info)
  163. {
  164. void *fh = NULL;
  165. PHYSFS_uint32 fileCount;
  166. PHYSFS_uint32 location = 16; /* sizeof sig. */
  167. GRPentry *entry;
  168. char *ptr;
  169. BAIL_IF_MACRO(!grp_open(name, forWriting, &fh, &fileCount), NULL, 0);
  170. info->entryCount = fileCount;
  171. info->entries = (GRPentry *) allocator.Malloc(sizeof(GRPentry)*fileCount);
  172. if (info->entries == NULL)
  173. {
  174. __PHYSFS_platformClose(fh);
  175. BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
  176. } /* if */
  177. location += (16 * fileCount);
  178. for (entry = info->entries; fileCount > 0; fileCount--, entry++)
  179. {
  180. if (__PHYSFS_platformRead(fh, &entry->name, 12, 1) != 1)
  181. {
  182. __PHYSFS_platformClose(fh);
  183. return(0);
  184. } /* if */
  185. entry->name[12] = '\0'; /* name isn't null-terminated in file. */
  186. if ((ptr = strchr(entry->name, ' ')) != NULL)
  187. *ptr = '\0'; /* trim extra spaces. */
  188. if (__PHYSFS_platformRead(fh, &entry->size, 4, 1) != 1)
  189. {
  190. __PHYSFS_platformClose(fh);
  191. return(0);
  192. } /* if */
  193. entry->size = PHYSFS_swapULE32(entry->size);
  194. entry->startPos = location;
  195. location += entry->size;
  196. } /* for */
  197. __PHYSFS_platformClose(fh);
  198. __PHYSFS_sort(info->entries, info->entryCount,
  199. grp_entry_cmp, grp_entry_swap);
  200. return(1);
  201. } /* grp_load_entries */
  202. static void *GRP_openArchive(const char *name, int forWriting)
  203. {
  204. PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
  205. GRPinfo *info = (GRPinfo *) allocator.Malloc(sizeof (GRPinfo));
  206. BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, 0);
  207. memset(info, '\0', sizeof (GRPinfo));
  208. info->filename = (char *) allocator.Malloc(strlen(name) + 1);
  209. GOTO_IF_MACRO(!info->filename, ERR_OUT_OF_MEMORY, GRP_openArchive_failed);
  210. if (!grp_load_entries(name, forWriting, info))
  211. goto GRP_openArchive_failed;
  212. strcpy(info->filename, name);
  213. info->last_mod_time = modtime;
  214. return(info);
  215. GRP_openArchive_failed:
  216. if (info != NULL)
  217. {
  218. if (info->filename != NULL)
  219. allocator.Free(info->filename);
  220. if (info->entries != NULL)
  221. allocator.Free(info->entries);
  222. allocator.Free(info);
  223. } /* if */
  224. return(NULL);
  225. } /* GRP_openArchive */
  226. static void GRP_enumerateFiles(dvoid *opaque, const char *dname,
  227. int omitSymLinks, PHYSFS_EnumFilesCallback cb,
  228. const char *origdir, void *callbackdata)
  229. {
  230. /* no directories in GRP files. */
  231. if (*dname != '\0')
  232. {
  233. GRPinfo *info = (GRPinfo *) opaque;
  234. GRPentry *entry = info->entries;
  235. PHYSFS_uint32 max = info->entryCount;
  236. PHYSFS_uint32 i;
  237. for (i = 0; i < max; i++, entry++)
  238. cb(callbackdata, origdir, entry->name);
  239. } /* if */
  240. } /* GRP_enumerateFiles */
  241. static GRPentry *grp_find_entry(GRPinfo *info, const char *name)
  242. {
  243. char *ptr = strchr(name, '.');
  244. GRPentry *a = info->entries;
  245. PHYSFS_sint32 lo = 0;
  246. PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
  247. PHYSFS_sint32 middle;
  248. int rc;
  249. /*
  250. * Rule out filenames to avoid unneeded processing...no dirs,
  251. * big filenames, or extensions > 3 chars.
  252. */
  253. BAIL_IF_MACRO((ptr) && (strlen(ptr) > 4), ERR_NO_SUCH_FILE, NULL);
  254. BAIL_IF_MACRO(strlen(name) > 12, ERR_NO_SUCH_FILE, NULL);
  255. BAIL_IF_MACRO(strchr(name, '/') != NULL, ERR_NO_SUCH_FILE, NULL);
  256. while (lo <= hi)
  257. {
  258. middle = lo + ((hi - lo) / 2);
  259. rc = strcmp(name, a[middle].name);
  260. if (rc == 0) /* found it! */
  261. return(&a[middle]);
  262. else if (rc > 0)
  263. lo = middle + 1;
  264. else
  265. hi = middle - 1;
  266. } /* while */
  267. BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
  268. } /* grp_find_entry */
  269. static int GRP_exists(dvoid *opaque, const char *name)
  270. {
  271. return(grp_find_entry((GRPinfo *) opaque, name) != NULL);
  272. } /* GRP_exists */
  273. static int GRP_isDirectory(dvoid *opaque, const char *name, int *fileExists)
  274. {
  275. *fileExists = GRP_exists(opaque, name);
  276. return(0); /* never directories in a groupfile. */
  277. } /* GRP_isDirectory */
  278. static int GRP_isSymLink(dvoid *opaque, const char *name, int *fileExists)
  279. {
  280. *fileExists = GRP_exists(opaque, name);
  281. return(0); /* never symlinks in a groupfile. */
  282. } /* GRP_isSymLink */
  283. static PHYSFS_sint64 GRP_getLastModTime(dvoid *opaque,
  284. const char *name,
  285. int *fileExists)
  286. {
  287. GRPinfo *info = (GRPinfo *) opaque;
  288. PHYSFS_sint64 retval = -1;
  289. *fileExists = (grp_find_entry(info, name) != NULL);
  290. if (*fileExists) /* use time of GRP itself in the physical filesystem. */
  291. retval = info->last_mod_time;
  292. return(retval);
  293. } /* GRP_getLastModTime */
  294. static fvoid *GRP_openRead(dvoid *opaque, const char *fnm, int *fileExists)
  295. {
  296. GRPinfo *info = (GRPinfo *) opaque;
  297. GRPfileinfo *finfo;
  298. GRPentry *entry;
  299. entry = grp_find_entry(info, fnm);
  300. *fileExists = (entry != NULL);
  301. BAIL_IF_MACRO(entry == NULL, NULL, NULL);
  302. finfo = (GRPfileinfo *) allocator.Malloc(sizeof (GRPfileinfo));
  303. BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
  304. finfo->handle = __PHYSFS_platformOpenRead(info->filename);
  305. if ( (finfo->handle == NULL) ||
  306. (!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
  307. {
  308. allocator.Free(finfo);
  309. return(NULL);
  310. } /* if */
  311. finfo->curPos = 0;
  312. finfo->entry = entry;
  313. return(finfo);
  314. } /* GRP_openRead */
  315. static fvoid *GRP_openWrite(dvoid *opaque, const char *name)
  316. {
  317. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  318. } /* GRP_openWrite */
  319. static fvoid *GRP_openAppend(dvoid *opaque, const char *name)
  320. {
  321. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  322. } /* GRP_openAppend */
  323. static int GRP_remove(dvoid *opaque, const char *name)
  324. {
  325. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  326. } /* GRP_remove */
  327. static int GRP_mkdir(dvoid *opaque, const char *name)
  328. {
  329. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  330. } /* GRP_mkdir */
  331. const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_GRP =
  332. {
  333. "GRP",
  334. GRP_ARCHIVE_DESCRIPTION,
  335. "Ryan C. Gordon <icculus@icculus.org>",
  336. "http://icculus.org/physfs/",
  337. };
  338. const PHYSFS_Archiver __PHYSFS_Archiver_GRP =
  339. {
  340. &__PHYSFS_ArchiveInfo_GRP,
  341. GRP_isArchive, /* isArchive() method */
  342. GRP_openArchive, /* openArchive() method */
  343. GRP_enumerateFiles, /* enumerateFiles() method */
  344. GRP_exists, /* exists() method */
  345. GRP_isDirectory, /* isDirectory() method */
  346. GRP_isSymLink, /* isSymLink() method */
  347. GRP_getLastModTime, /* getLastModTime() method */
  348. GRP_openRead, /* openRead() method */
  349. GRP_openWrite, /* openWrite() method */
  350. GRP_openAppend, /* openAppend() method */
  351. GRP_remove, /* remove() method */
  352. GRP_mkdir, /* mkdir() method */
  353. GRP_dirClose, /* dirClose() method */
  354. GRP_read, /* read() method */
  355. GRP_write, /* write() method */
  356. GRP_eof, /* eof() method */
  357. GRP_tell, /* tell() method */
  358. GRP_seek, /* seek() method */
  359. GRP_fileLength, /* fileLength() method */
  360. GRP_fileClose /* fileClose() method */
  361. };
  362. #endif /* defined PHYSFS_SUPPORTS_GRP */
  363. /* end of grp.c ... */