grp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. * As it was never a concern in the DOS-based Duke Nukem days, I treat these
  23. * archives as CASE-INSENSITIVE. Opening "myfile.txt" and "MYFILE.TXT" both
  24. * work, and get the same file.
  25. *
  26. * Please see the file LICENSE in the source's root directory.
  27. *
  28. * This file written by Ryan C. Gordon.
  29. */
  30. #if HAVE_CONFIG_H
  31. # include <config.h>
  32. #endif
  33. #if (defined PHYSFS_SUPPORTS_GRP)
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <errno.h>
  38. #include <fcntl.h>
  39. #include <assert.h>
  40. #include "physfs.h"
  41. #define __PHYSICSFS_INTERNAL__
  42. #include "physfs_internal.h"
  43. typedef struct
  44. {
  45. void *handle;
  46. char *filename;
  47. PHYSFS_uint32 totalEntries;
  48. } GRPinfo;
  49. typedef struct
  50. {
  51. void *handle;
  52. PHYSFS_uint64 startPos;
  53. PHYSFS_uint64 size;
  54. } GRPfileinfo;
  55. static void GRP_dirClose(DirHandle *h);
  56. static PHYSFS_sint64 GRP_read(FileHandle *handle, void *buffer,
  57. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
  58. static int GRP_eof(FileHandle *handle);
  59. static PHYSFS_sint64 GRP_tell(FileHandle *handle);
  60. static int GRP_seek(FileHandle *handle, PHYSFS_uint64 offset);
  61. static PHYSFS_sint64 GRP_fileLength(FileHandle *handle);
  62. static int GRP_fileClose(FileHandle *handle);
  63. static int GRP_isArchive(const char *filename, int forWriting);
  64. static DirHandle *GRP_openArchive(const char *name, int forWriting);
  65. static LinkedStringList *GRP_enumerateFiles(DirHandle *h,
  66. const char *dirname,
  67. int omitSymLinks);
  68. static int GRP_exists(DirHandle *h, const char *name);
  69. static int GRP_isDirectory(DirHandle *h, const char *name);
  70. static int GRP_isSymLink(DirHandle *h, const char *name);
  71. static PHYSFS_sint64 GRP_getLastModTime(DirHandle *h, const char *name);
  72. static FileHandle *GRP_openRead(DirHandle *h, const char *name);
  73. static const FileFunctions __PHYSFS_FileFunctions_GRP =
  74. {
  75. GRP_read, /* read() method */
  76. NULL, /* write() method */
  77. GRP_eof, /* eof() method */
  78. GRP_tell, /* tell() method */
  79. GRP_seek, /* seek() method */
  80. GRP_fileLength, /* fileLength() method */
  81. GRP_fileClose /* fileClose() method */
  82. };
  83. const DirFunctions __PHYSFS_DirFunctions_GRP =
  84. {
  85. GRP_isArchive, /* isArchive() method */
  86. GRP_openArchive, /* openArchive() method */
  87. GRP_enumerateFiles, /* enumerateFiles() method */
  88. GRP_exists, /* exists() method */
  89. GRP_isDirectory, /* isDirectory() method */
  90. GRP_isSymLink, /* isSymLink() method */
  91. GRP_getLastModTime, /* getLastModTime() method */
  92. GRP_openRead, /* openRead() method */
  93. NULL, /* openWrite() method */
  94. NULL, /* openAppend() method */
  95. NULL, /* remove() method */
  96. NULL, /* mkdir() method */
  97. GRP_dirClose /* dirClose() method */
  98. };
  99. const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_GRP =
  100. {
  101. "GRP",
  102. "Build engine Groupfile format",
  103. "Ryan C. Gordon <icculus@clutteredmind.org>",
  104. "http://www.icculus.org/physfs/",
  105. };
  106. static void GRP_dirClose(DirHandle *h)
  107. {
  108. __PHYSFS_platformClose( ((GRPinfo *) h->opaque)->handle );
  109. free(((GRPinfo *) h->opaque)->filename);
  110. free(h->opaque);
  111. free(h);
  112. } /* GRP_dirClose */
  113. static PHYSFS_sint64 GRP_read(FileHandle *handle, void *buffer,
  114. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  115. {
  116. GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
  117. void *fh = finfo->handle;
  118. PHYSFS_sint64 curPos = __PHYSFS_platformTell(fh);
  119. PHYSFS_uint64 bytesLeft = (finfo->startPos + finfo->size) - curPos;
  120. PHYSFS_uint64 objsLeft = (bytesLeft / objSize);
  121. if (objsLeft < objCount)
  122. objCount = (PHYSFS_uint32) objsLeft;
  123. return(__PHYSFS_platformRead(fh, buffer, objSize, objCount));
  124. } /* GRP_read */
  125. static int GRP_eof(FileHandle *handle)
  126. {
  127. GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
  128. void *fh = finfo->handle;
  129. PHYSFS_sint64 pos = __PHYSFS_platformTell(fh);
  130. BAIL_IF_MACRO(pos < 0, NULL, 1); /* (*shrug*) */
  131. return(pos >= (PHYSFS_sint64) (finfo->startPos + finfo->size));
  132. } /* GRP_eof */
  133. static PHYSFS_sint64 GRP_tell(FileHandle *handle)
  134. {
  135. GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
  136. return(__PHYSFS_platformTell(finfo->handle) - finfo->startPos);
  137. } /* GRP_tell */
  138. static int GRP_seek(FileHandle *handle, PHYSFS_uint64 offset)
  139. {
  140. GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
  141. PHYSFS_uint64 newPos = (finfo->startPos + offset);
  142. BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
  143. BAIL_IF_MACRO(newPos > finfo->startPos + finfo->size, ERR_PAST_EOF, 0);
  144. return(__PHYSFS_platformSeek(finfo->handle, newPos));
  145. } /* GRP_seek */
  146. static PHYSFS_sint64 GRP_fileLength(FileHandle *handle)
  147. {
  148. GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
  149. return(finfo->size);
  150. } /* GRP_fileLength */
  151. static int GRP_fileClose(FileHandle *handle)
  152. {
  153. GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
  154. BAIL_IF_MACRO(__PHYSFS_platformClose(finfo->handle), NULL, 0);
  155. free(handle->opaque);
  156. free(handle);
  157. return(1);
  158. } /* GRP_fileClose */
  159. static int openGrp(const char *filename, int forWriting,
  160. void **fh, PHYSFS_sint32 *count)
  161. {
  162. PHYSFS_uint8 buf[12];
  163. *fh = NULL;
  164. BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
  165. *fh = __PHYSFS_platformOpenRead(filename);
  166. BAIL_IF_MACRO(*fh == NULL, NULL, 0);
  167. if (__PHYSFS_platformRead(*fh, buf, 12, 1) != 1)
  168. goto openGrp_failed;
  169. if (memcmp(buf, "KenSilverman", 12) != 0)
  170. {
  171. __PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE);
  172. goto openGrp_failed;
  173. } /* if */
  174. if (__PHYSFS_platformRead(*fh, count, sizeof (PHYSFS_sint32), 1) != 1)
  175. goto openGrp_failed;
  176. *count = PHYSFS_swapSLE32(*count);
  177. return(1);
  178. openGrp_failed:
  179. if (*fh != NULL)
  180. __PHYSFS_platformClose(*fh);
  181. *count = -1;
  182. *fh = NULL;
  183. return(0);
  184. } /* openGrp */
  185. static int GRP_isArchive(const char *filename, int forWriting)
  186. {
  187. void *fh;
  188. int fileCount;
  189. int retval = openGrp(filename, forWriting, &fh, &fileCount);
  190. if (fh != NULL)
  191. __PHYSFS_platformClose(fh);
  192. return(retval);
  193. } /* GRP_isArchive */
  194. static DirHandle *GRP_openArchive(const char *name, int forWriting)
  195. {
  196. void *fh = NULL;
  197. int fileCount;
  198. DirHandle *retval = malloc(sizeof (DirHandle));
  199. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  200. retval->opaque = malloc(sizeof (GRPinfo));
  201. if (retval->opaque == NULL)
  202. {
  203. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  204. goto GRP_openArchive_failed;
  205. } /* if */
  206. ((GRPinfo *) retval->opaque)->filename = (char *) malloc(strlen(name) + 1);
  207. if (((GRPinfo *) retval->opaque)->filename == NULL)
  208. {
  209. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  210. goto GRP_openArchive_failed;
  211. } /* if */
  212. if (!openGrp(name, forWriting, &fh, &fileCount))
  213. goto GRP_openArchive_failed;
  214. strcpy(((GRPinfo *) retval->opaque)->filename, name);
  215. ((GRPinfo *) retval->opaque)->handle = fh;
  216. ((GRPinfo *) retval->opaque)->totalEntries = fileCount;
  217. retval->funcs = &__PHYSFS_DirFunctions_GRP;
  218. return(retval);
  219. GRP_openArchive_failed:
  220. if (retval != NULL)
  221. {
  222. if (retval->opaque != NULL)
  223. {
  224. if ( ((GRPinfo *) retval->opaque)->filename != NULL )
  225. free( ((GRPinfo *) retval->opaque)->filename );
  226. free(retval->opaque);
  227. } /* if */
  228. free(retval);
  229. } /* if */
  230. if (fh != NULL)
  231. __PHYSFS_platformClose(fh);
  232. return(NULL);
  233. } /* GRP_openArchive */
  234. static LinkedStringList *GRP_enumerateFiles(DirHandle *h,
  235. const char *dirname,
  236. int omitSymLinks)
  237. {
  238. PHYSFS_uint8 buf[16];
  239. GRPinfo *g = (GRPinfo *) (h->opaque);
  240. void *fh = g->handle;
  241. PHYSFS_uint32 i;
  242. LinkedStringList *retval = NULL;
  243. LinkedStringList *l = NULL;
  244. LinkedStringList *prev = NULL;
  245. /* !!! FIXME: Does this consider "/" ? */
  246. if (*dirname != '\0') /* no directories in GRP files. */
  247. return(NULL);
  248. /* jump to first file entry... */
  249. BAIL_IF_MACRO(!__PHYSFS_platformSeek(fh, 16), NULL, NULL);
  250. for (i = 0; i < g->totalEntries; i++)
  251. {
  252. BAIL_IF_MACRO(__PHYSFS_platformRead(fh, buf, 16, 1) != 1, NULL, retval);
  253. buf[12] = '\0'; /* FILENAME.EXT is all you get. */
  254. l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
  255. if (l == NULL)
  256. break;
  257. l->str = (char *) malloc(strlen((const char *) buf) + 1);
  258. if (l->str == NULL)
  259. {
  260. free(l);
  261. break;
  262. } /* if */
  263. strcpy(l->str, (const char *) buf);
  264. if (retval == NULL)
  265. retval = l;
  266. else
  267. prev->next = l;
  268. prev = l;
  269. l->next = NULL;
  270. } /* for */
  271. return(retval);
  272. } /* GRP_enumerateFiles */
  273. static PHYSFS_sint32 getFileEntry(DirHandle *h, const char *name,
  274. PHYSFS_uint32 *size)
  275. {
  276. PHYSFS_uint8 buf[16];
  277. GRPinfo *g = (GRPinfo *) (h->opaque);
  278. void *fh = g->handle;
  279. PHYSFS_uint32 i;
  280. char *ptr;
  281. int retval = (g->totalEntries + 1) * 16; /* offset of raw file data */
  282. /* Rule out filenames to avoid unneeded file i/o... */
  283. if (strchr(name, '/') != NULL) /* no directories in groupfiles. */
  284. return(-1);
  285. ptr = strchr(name, '.');
  286. if ((ptr) && (strlen(ptr) > 4)) /* 3 char extension at most. */
  287. return(-1);
  288. if (strlen(name) > 12)
  289. return(-1);
  290. /* jump to first file entry... */
  291. BAIL_IF_MACRO(!__PHYSFS_platformSeek(fh, 16), NULL, -1);
  292. for (i = 0; i < g->totalEntries; i++)
  293. {
  294. PHYSFS_sint32 l = 0;
  295. BAIL_IF_MACRO(__PHYSFS_platformRead(fh, buf, 12, 1) != 1, NULL, -1);
  296. BAIL_IF_MACRO(__PHYSFS_platformRead(fh, &l, sizeof (l), 1) != 1, NULL, -1);
  297. l = PHYSFS_swapSLE32(l);
  298. buf[12] = '\0'; /* FILENAME.EXT is all you get. */
  299. if (__PHYSFS_platformStricmp((const char *) buf, name) == 0)
  300. {
  301. if (size != NULL)
  302. *size = l;
  303. return(retval);
  304. } /* if */
  305. retval += l;
  306. } /* for */
  307. return(-1); /* not found. */
  308. } /* getFileEntry */
  309. static int GRP_exists(DirHandle *h, const char *name)
  310. {
  311. return(getFileEntry(h, name, NULL) != -1);
  312. } /* GRP_exists */
  313. static int GRP_isDirectory(DirHandle *h, const char *name)
  314. {
  315. return(0); /* never directories in a groupfile. */
  316. } /* GRP_isDirectory */
  317. static int GRP_isSymLink(DirHandle *h, const char *name)
  318. {
  319. return(0); /* never symlinks in a groupfile. */
  320. } /* GRP_isSymLink */
  321. static PHYSFS_sint64 GRP_getLastModTime(DirHandle *h, const char *name)
  322. {
  323. /* Just return the time of the GRP itself in the physical filesystem. */
  324. return(__PHYSFS_platformGetLastModTime(((GRPinfo *) h->opaque)->filename));
  325. } /* GRP_getLastModTime */
  326. static FileHandle *GRP_openRead(DirHandle *h, const char *name)
  327. {
  328. const char *filename = ((GRPinfo *) h->opaque)->filename;
  329. FileHandle *retval;
  330. GRPfileinfo *finfo;
  331. PHYSFS_uint32 size;
  332. PHYSFS_sint32 offset;
  333. offset = getFileEntry(h, name, &size);
  334. BAIL_IF_MACRO(offset == -1, ERR_NO_SUCH_FILE, NULL);
  335. retval = (FileHandle *) malloc(sizeof (FileHandle));
  336. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  337. finfo = (GRPfileinfo *) malloc(sizeof (GRPfileinfo));
  338. if (finfo == NULL)
  339. {
  340. free(retval);
  341. BAIL_IF_MACRO(1, ERR_OUT_OF_MEMORY, NULL);
  342. } /* if */
  343. finfo->handle = __PHYSFS_platformOpenRead(filename);
  344. if ( (finfo->handle == NULL) ||
  345. (!__PHYSFS_platformSeek(finfo->handle, offset)) )
  346. {
  347. free(finfo);
  348. free(retval);
  349. return(NULL);
  350. } /* if */
  351. finfo->startPos = offset;
  352. finfo->size = size;
  353. retval->opaque = (void *) finfo;
  354. retval->funcs = &__PHYSFS_FileFunctions_GRP;
  355. retval->dirHandle = h;
  356. return(retval);
  357. } /* GRP_openRead */
  358. #endif /* defined PHYSFS_SUPPORTS_GRP */
  359. /* end of grp.c ... */