grp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 FileHandle *GRP_openRead(DirHandle *h, const char *name);
  72. static const FileFunctions __PHYSFS_FileFunctions_GRP =
  73. {
  74. GRP_read, /* read() method */
  75. NULL, /* write() method */
  76. GRP_eof, /* eof() method */
  77. GRP_tell, /* tell() method */
  78. GRP_seek, /* seek() method */
  79. GRP_fileLength, /* fileLength() method */
  80. GRP_fileClose /* fileClose() method */
  81. };
  82. const DirFunctions __PHYSFS_DirFunctions_GRP =
  83. {
  84. GRP_isArchive, /* isArchive() method */
  85. GRP_openArchive, /* openArchive() method */
  86. GRP_enumerateFiles, /* enumerateFiles() method */
  87. GRP_exists, /* exists() method */
  88. GRP_isDirectory, /* isDirectory() method */
  89. GRP_isSymLink, /* isSymLink() method */
  90. GRP_openRead, /* openRead() method */
  91. NULL, /* openWrite() method */
  92. NULL, /* openAppend() method */
  93. NULL, /* remove() method */
  94. NULL, /* mkdir() method */
  95. GRP_dirClose /* dirClose() method */
  96. };
  97. const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_GRP =
  98. {
  99. "GRP",
  100. "Build engine Groupfile format",
  101. "Ryan C. Gordon <icculus@clutteredmind.org>",
  102. "http://www.icculus.org/physfs/",
  103. };
  104. static void GRP_dirClose(DirHandle *h)
  105. {
  106. __PHYSFS_platformClose( ((GRPinfo *) h->opaque)->handle );
  107. free(((GRPinfo *) h->opaque)->filename);
  108. free(h->opaque);
  109. free(h);
  110. } /* GRP_dirClose */
  111. static PHYSFS_sint64 GRP_read(FileHandle *handle, void *buffer,
  112. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  113. {
  114. GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
  115. void *fh = finfo->handle;
  116. PHYSFS_sint64 curPos = __PHYSFS_platformTell(fh);
  117. PHYSFS_uint64 bytesLeft = (finfo->startPos + finfo->size) - curPos;
  118. PHYSFS_uint64 objsLeft = (bytesLeft / objSize);
  119. if (objsLeft < objCount)
  120. objCount = (PHYSFS_uint32) 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. PHYSFS_sint64 pos = __PHYSFS_platformTell(fh);
  128. BAIL_IF_MACRO(pos < 0, NULL, 1); /* (*shrug*) */
  129. return(pos >= (PHYSFS_sint64) (finfo->startPos + finfo->size));
  130. } /* GRP_eof */
  131. static PHYSFS_sint64 GRP_tell(FileHandle *handle)
  132. {
  133. GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
  134. return(__PHYSFS_platformTell(finfo->handle) - finfo->startPos);
  135. } /* GRP_tell */
  136. static int GRP_seek(FileHandle *handle, PHYSFS_uint64 offset)
  137. {
  138. GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
  139. PHYSFS_uint64 newPos = (finfo->startPos + offset);
  140. BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
  141. BAIL_IF_MACRO(newPos > finfo->startPos + finfo->size, ERR_PAST_EOF, 0);
  142. return(__PHYSFS_platformSeek(finfo->handle, newPos));
  143. } /* GRP_seek */
  144. static PHYSFS_sint64 GRP_fileLength(FileHandle *handle)
  145. {
  146. GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
  147. return(finfo->size);
  148. } /* GRP_fileLength */
  149. static int GRP_fileClose(FileHandle *handle)
  150. {
  151. GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
  152. BAIL_IF_MACRO(__PHYSFS_platformClose(finfo->handle), NULL, 0);
  153. free(handle->opaque);
  154. free(handle);
  155. return(1);
  156. } /* GRP_fileClose */
  157. static int openGrp(const char *filename, int forWriting,
  158. void **fh, PHYSFS_sint32 *count)
  159. {
  160. PHYSFS_uint8 buf[12];
  161. *fh = NULL;
  162. BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
  163. *fh = __PHYSFS_platformOpenRead(filename);
  164. BAIL_IF_MACRO(*fh == NULL, NULL, 0);
  165. if (__PHYSFS_platformRead(*fh, buf, 12, 1) != 1)
  166. goto openGrp_failed;
  167. if (memcmp(buf, "KenSilverman", 12) != 0)
  168. {
  169. __PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE);
  170. goto openGrp_failed;
  171. } /* if */
  172. if (__PHYSFS_platformRead(*fh, count, sizeof (PHYSFS_sint32), 1) != 1)
  173. goto openGrp_failed;
  174. *count = PHYSFS_swapSLE32(*count);
  175. return(1);
  176. openGrp_failed:
  177. if (*fh != NULL)
  178. __PHYSFS_platformClose(*fh);
  179. *count = -1;
  180. *fh = NULL;
  181. return(0);
  182. } /* openGrp */
  183. static int GRP_isArchive(const char *filename, int forWriting)
  184. {
  185. void *fh;
  186. int fileCount;
  187. int retval = openGrp(filename, forWriting, &fh, &fileCount);
  188. if (fh != NULL)
  189. __PHYSFS_platformClose(fh);
  190. return(retval);
  191. } /* GRP_isArchive */
  192. static DirHandle *GRP_openArchive(const char *name, int forWriting)
  193. {
  194. void *fh = NULL;
  195. int fileCount;
  196. DirHandle *retval = malloc(sizeof (DirHandle));
  197. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  198. retval->opaque = malloc(sizeof (GRPinfo));
  199. if (retval->opaque == NULL)
  200. {
  201. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  202. goto GRP_openArchive_failed;
  203. } /* if */
  204. ((GRPinfo *) retval->opaque)->filename = (char *) malloc(strlen(name) + 1);
  205. if (((GRPinfo *) retval->opaque)->filename == NULL)
  206. {
  207. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  208. goto GRP_openArchive_failed;
  209. } /* if */
  210. if (!openGrp(name, forWriting, &fh, &fileCount))
  211. goto GRP_openArchive_failed;
  212. strcpy(((GRPinfo *) retval->opaque)->filename, name);
  213. ((GRPinfo *) retval->opaque)->handle = fh;
  214. ((GRPinfo *) retval->opaque)->totalEntries = fileCount;
  215. retval->funcs = &__PHYSFS_DirFunctions_GRP;
  216. return(retval);
  217. GRP_openArchive_failed:
  218. if (retval != NULL)
  219. {
  220. if (retval->opaque != NULL)
  221. {
  222. if ( ((GRPinfo *) retval->opaque)->filename != NULL )
  223. free( ((GRPinfo *) retval->opaque)->filename );
  224. free(retval->opaque);
  225. } /* if */
  226. free(retval);
  227. } /* if */
  228. if (fh != NULL)
  229. __PHYSFS_platformClose(fh);
  230. return(NULL);
  231. } /* GRP_openArchive */
  232. static LinkedStringList *GRP_enumerateFiles(DirHandle *h,
  233. const char *dirname,
  234. int omitSymLinks)
  235. {
  236. PHYSFS_uint8 buf[16];
  237. GRPinfo *g = (GRPinfo *) (h->opaque);
  238. void *fh = g->handle;
  239. PHYSFS_uint32 i;
  240. LinkedStringList *retval = NULL;
  241. LinkedStringList *l = NULL;
  242. LinkedStringList *prev = NULL;
  243. /* !!! FIXME: Does this consider "/" ? */
  244. if (*dirname != '\0') /* no directories in GRP files. */
  245. return(NULL);
  246. /* jump to first file entry... */
  247. BAIL_IF_MACRO(!__PHYSFS_platformSeek(fh, 16), NULL, NULL);
  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. PHYSFS_uint32 i;
  278. char *ptr;
  279. int retval = (g->totalEntries + 1) * 16; /* offset of raw file data */
  280. /* Rule out filenames to avoid unneeded file i/o... */
  281. if (strchr(name, '/') != NULL) /* no directories in groupfiles. */
  282. return(-1);
  283. ptr = strchr(name, '.');
  284. if ((ptr) && (strlen(ptr) > 4)) /* 3 char extension at most. */
  285. return(-1);
  286. if (strlen(name) > 12)
  287. return(-1);
  288. /* jump to first file entry... */
  289. BAIL_IF_MACRO(!__PHYSFS_platformSeek(fh, 16), NULL, -1);
  290. for (i = 0; i < g->totalEntries; i++)
  291. {
  292. PHYSFS_sint32 l = 0;
  293. BAIL_IF_MACRO(__PHYSFS_platformRead(fh, buf, 12, 1) != 1, NULL, -1);
  294. BAIL_IF_MACRO(__PHYSFS_platformRead(fh, &l, sizeof (l), 1) != 1, NULL, -1);
  295. l = PHYSFS_swapSLE32(l);
  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. #endif /* defined PHYSFS_SUPPORTS_GRP */
  352. /* end of grp.c ... */