grp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. PHYSFS_uint64 objsLeft = (bytesLeft / objSize);
  118. if (objsLeft < objCount)
  119. objCount = objsLeft;
  120. return(__PHYSFS_platformRead(fh, buffer, objSize, objCount));
  121. } /* GRP_read */
  122. static int GRP_eof(FileHandle *handle)
  123. {
  124. GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
  125. void *fh = finfo->handle;
  126. return(__PHYSFS_platformTell(fh) >= finfo->startPos + finfo->size);
  127. } /* GRP_eof */
  128. static PHYSFS_sint64 GRP_tell(FileHandle *handle)
  129. {
  130. GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
  131. return(__PHYSFS_platformTell(finfo->handle) - finfo->startPos);
  132. } /* GRP_tell */
  133. static int GRP_seek(FileHandle *handle, PHYSFS_uint64 offset)
  134. {
  135. GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
  136. PHYSFS_uint64 newPos = (finfo->startPos + offset);
  137. BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
  138. BAIL_IF_MACRO(newPos > finfo->startPos + finfo->size, ERR_PAST_EOF, 0);
  139. return(__PHYSFS_platformSeek(finfo->handle, newPos));
  140. } /* GRP_seek */
  141. static PHYSFS_sint64 GRP_fileLength(FileHandle *handle)
  142. {
  143. GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
  144. return(finfo->size);
  145. } /* GRP_fileLength */
  146. static int GRP_fileClose(FileHandle *handle)
  147. {
  148. GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
  149. BAIL_IF_MACRO(__PHYSFS_platformClose(finfo->handle), NULL, 0);
  150. free(handle->opaque);
  151. free(handle);
  152. return(1);
  153. } /* GRP_fileClose */
  154. static int openGrp(const char *filename, int forWriting,
  155. void **fh, PHYSFS_sint32 *count)
  156. {
  157. PHYSFS_uint8 buf[12];
  158. *fh = NULL;
  159. BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
  160. *fh = __PHYSFS_platformOpenRead(filename);
  161. BAIL_IF_MACRO(*fh == NULL, NULL, 0);
  162. if (__PHYSFS_platformRead(*fh, buf, 12, 1) != 1)
  163. goto openGrp_failed;
  164. if (memcmp(buf, "KenSilverman", 12) != 0)
  165. {
  166. __PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE);
  167. goto openGrp_failed;
  168. } /* if */
  169. if (__PHYSFS_platformRead(*fh, count, sizeof (PHYSFS_sint32), 1) != 1)
  170. goto openGrp_failed;
  171. *count = PHYSFS_swapSLE32(*count);
  172. return(1);
  173. openGrp_failed:
  174. if (*fh != NULL)
  175. __PHYSFS_platformClose(*fh);
  176. *count = -1;
  177. *fh = NULL;
  178. return(0);
  179. } /* openGrp */
  180. static int GRP_isArchive(const char *filename, int forWriting)
  181. {
  182. void *fh;
  183. int fileCount;
  184. int retval = openGrp(filename, forWriting, &fh, &fileCount);
  185. if (fh != NULL)
  186. __PHYSFS_platformClose(fh);
  187. return(retval);
  188. } /* GRP_isArchive */
  189. static DirHandle *GRP_openArchive(const char *name, int forWriting)
  190. {
  191. void *fh = NULL;
  192. int fileCount;
  193. DirHandle *retval = malloc(sizeof (DirHandle));
  194. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  195. retval->opaque = malloc(sizeof (GRPinfo));
  196. if (retval->opaque == NULL)
  197. {
  198. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  199. goto GRP_openArchive_failed;
  200. } /* if */
  201. ((GRPinfo *) retval->opaque)->filename = (char *) malloc(strlen(name) + 1);
  202. if (((GRPinfo *) retval->opaque)->filename == NULL)
  203. {
  204. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  205. goto GRP_openArchive_failed;
  206. } /* if */
  207. if (!openGrp(name, forWriting, &fh, &fileCount))
  208. goto GRP_openArchive_failed;
  209. strcpy(((GRPinfo *) retval->opaque)->filename, name);
  210. ((GRPinfo *) retval->opaque)->handle = fh;
  211. ((GRPinfo *) retval->opaque)->totalEntries = fileCount;
  212. retval->funcs = &__PHYSFS_DirFunctions_GRP;
  213. return(retval);
  214. GRP_openArchive_failed:
  215. if (retval != NULL)
  216. {
  217. if (retval->opaque != NULL)
  218. {
  219. if ( ((GRPinfo *) retval->opaque)->filename != NULL )
  220. free( ((GRPinfo *) retval->opaque)->filename );
  221. free(retval->opaque);
  222. } /* if */
  223. free(retval);
  224. } /* if */
  225. if (fh != NULL)
  226. __PHYSFS_platformClose(fh);
  227. return(NULL);
  228. } /* GRP_openArchive */
  229. static LinkedStringList *GRP_enumerateFiles(DirHandle *h,
  230. const char *dirname,
  231. int omitSymLinks)
  232. {
  233. PHYSFS_uint8 buf[16];
  234. GRPinfo *g = (GRPinfo *) (h->opaque);
  235. void *fh = g->handle;
  236. PHYSFS_uint32 i;
  237. LinkedStringList *retval = NULL;
  238. LinkedStringList *l = NULL;
  239. LinkedStringList *prev = NULL;
  240. /* !!! FIXME: Does this consider "/" ? */
  241. if (*dirname != '\0') /* no directories in GRP files. */
  242. return(NULL);
  243. /* jump to first file entry... */
  244. BAIL_IF_MACRO(!__PHYSFS_platformSeek(fh, 16), NULL, NULL);
  245. for (i = 0; i < g->totalEntries; i++)
  246. {
  247. BAIL_IF_MACRO(__PHYSFS_platformRead(fh, buf, 16, 1) != 1, NULL, retval);
  248. buf[12] = '\0'; /* FILENAME.EXT is all you get. */
  249. l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
  250. if (l == NULL)
  251. break;
  252. l->str = (char *) malloc(strlen((const char *) buf) + 1);
  253. if (l->str == NULL)
  254. {
  255. free(l);
  256. break;
  257. } /* if */
  258. strcpy(l->str, (const char *) buf);
  259. if (retval == NULL)
  260. retval = l;
  261. else
  262. prev->next = l;
  263. prev = l;
  264. l->next = NULL;
  265. } /* for */
  266. return(retval);
  267. } /* GRP_enumerateFiles */
  268. static PHYSFS_sint32 getFileEntry(DirHandle *h, const char *name,
  269. PHYSFS_uint32 *size)
  270. {
  271. PHYSFS_uint8 buf[16];
  272. GRPinfo *g = (GRPinfo *) (h->opaque);
  273. void *fh = g->handle;
  274. PHYSFS_uint32 i;
  275. char *ptr;
  276. int retval = (g->totalEntries + 1) * 16; /* offset of raw file data */
  277. /* Rule out filenames to avoid unneeded file i/o... */
  278. if (strchr(name, '/') != NULL) /* no directories in groupfiles. */
  279. return(-1);
  280. ptr = strchr(name, '.');
  281. if ((ptr) && (strlen(ptr) > 4)) /* 3 char extension at most. */
  282. return(-1);
  283. if (strlen(name) > 12)
  284. return(-1);
  285. /* jump to first file entry... */
  286. BAIL_IF_MACRO(!__PHYSFS_platformSeek(fh, 16), NULL, -1);
  287. for (i = 0; i < g->totalEntries; i++)
  288. {
  289. PHYSFS_sint32 l = 0;
  290. BAIL_IF_MACRO(__PHYSFS_platformRead(fh, buf, 12, 1) != 1, NULL, -1);
  291. BAIL_IF_MACRO(__PHYSFS_platformRead(fh, &l, sizeof (l), 1) != 1, NULL, -1);
  292. l = PHYSFS_swapSLE32(l);
  293. buf[12] = '\0'; /* FILENAME.EXT is all you get. */
  294. if (__PHYSFS_platformStricmp((const char *) buf, name) == 0)
  295. {
  296. if (size != NULL)
  297. *size = l;
  298. return(retval);
  299. } /* if */
  300. retval += l;
  301. } /* for */
  302. return(-1); /* not found. */
  303. } /* getFileEntry */
  304. static int GRP_exists(DirHandle *h, const char *name)
  305. {
  306. return(getFileEntry(h, name, NULL) != -1);
  307. } /* GRP_exists */
  308. static int GRP_isDirectory(DirHandle *h, const char *name)
  309. {
  310. return(0); /* never directories in a groupfile. */
  311. } /* GRP_isDirectory */
  312. static int GRP_isSymLink(DirHandle *h, const char *name)
  313. {
  314. return(0); /* never symlinks in a groupfile. */
  315. } /* GRP_isSymLink */
  316. static FileHandle *GRP_openRead(DirHandle *h, const char *name)
  317. {
  318. const char *filename = ((GRPinfo *) h->opaque)->filename;
  319. FileHandle *retval;
  320. GRPfileinfo *finfo;
  321. PHYSFS_uint32 size;
  322. PHYSFS_sint32 offset;
  323. offset = getFileEntry(h, name, &size);
  324. BAIL_IF_MACRO(offset == -1, ERR_NO_SUCH_FILE, NULL);
  325. retval = (FileHandle *) malloc(sizeof (FileHandle));
  326. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  327. finfo = (GRPfileinfo *) malloc(sizeof (GRPfileinfo));
  328. if (finfo == NULL)
  329. {
  330. free(retval);
  331. BAIL_IF_MACRO(1, ERR_OUT_OF_MEMORY, NULL);
  332. } /* if */
  333. finfo->handle = __PHYSFS_platformOpenRead(filename);
  334. if ( (finfo->handle == NULL) ||
  335. (!__PHYSFS_platformSeek(finfo->handle, offset)) )
  336. {
  337. free(finfo);
  338. free(retval);
  339. return(NULL);
  340. } /* if */
  341. finfo->startPos = offset;
  342. finfo->size = size;
  343. retval->opaque = (void *) finfo;
  344. retval->funcs = &__PHYSFS_FileFunctions_GRP;
  345. retval->dirHandle = h;
  346. return(retval);
  347. } /* GRP_openRead */
  348. /* end of grp.c ... */