grp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. free(info->filename);
  59. free(info->entries);
  60. 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. 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 *) 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 = malloc(sizeof (GRPinfo));
  209. BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, 0);
  210. memset(info, '\0', sizeof (GRPinfo));
  211. info->filename = (char *) malloc(strlen(name) + 1);
  212. if (info->filename == NULL)
  213. {
  214. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  215. goto GRP_openArchive_failed;
  216. } /* if */
  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. free(info->filename);
  227. if (info->entries != NULL)
  228. free(info->entries);
  229. free(info);
  230. } /* if */
  231. return(NULL);
  232. } /* GRP_openArchive */
  233. static void GRP_enumerateFiles(dvoid *opaque, const char *dname,
  234. int omitSymLinks, PHYSFS_StringCallback cb,
  235. 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, 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 *) 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. 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@clutteredmind.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 ... */