grp.c 13 KB

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