grp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. NULL, /* getLastModTime() method */
  91. GRP_openRead, /* openRead() method */
  92. NULL, /* openWrite() method */
  93. NULL, /* openAppend() method */
  94. NULL, /* remove() method */
  95. NULL, /* mkdir() method */
  96. GRP_dirClose /* dirClose() method */
  97. };
  98. const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_GRP =
  99. {
  100. "GRP",
  101. "Build engine Groupfile format",
  102. "Ryan C. Gordon <icculus@clutteredmind.org>",
  103. "http://www.icculus.org/physfs/",
  104. };
  105. static void GRP_dirClose(DirHandle *h)
  106. {
  107. __PHYSFS_platformClose( ((GRPinfo *) h->opaque)->handle );
  108. free(((GRPinfo *) h->opaque)->filename);
  109. free(h->opaque);
  110. free(h);
  111. } /* GRP_dirClose */
  112. static PHYSFS_sint64 GRP_read(FileHandle *handle, void *buffer,
  113. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  114. {
  115. GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
  116. void *fh = finfo->handle;
  117. PHYSFS_sint64 curPos = __PHYSFS_platformTell(fh);
  118. PHYSFS_uint64 bytesLeft = (finfo->startPos + finfo->size) - curPos;
  119. PHYSFS_uint64 objsLeft = (bytesLeft / objSize);
  120. if (objsLeft < objCount)
  121. objCount = (PHYSFS_uint32) objsLeft;
  122. return(__PHYSFS_platformRead(fh, buffer, objSize, objCount));
  123. } /* GRP_read */
  124. static int GRP_eof(FileHandle *handle)
  125. {
  126. GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
  127. void *fh = finfo->handle;
  128. PHYSFS_sint64 pos = __PHYSFS_platformTell(fh);
  129. BAIL_IF_MACRO(pos < 0, NULL, 1); /* (*shrug*) */
  130. return(pos >= (PHYSFS_sint64) (finfo->startPos + finfo->size));
  131. } /* GRP_eof */
  132. static PHYSFS_sint64 GRP_tell(FileHandle *handle)
  133. {
  134. GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
  135. return(__PHYSFS_platformTell(finfo->handle) - finfo->startPos);
  136. } /* GRP_tell */
  137. static int GRP_seek(FileHandle *handle, PHYSFS_uint64 offset)
  138. {
  139. GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
  140. PHYSFS_uint64 newPos = (finfo->startPos + offset);
  141. BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
  142. BAIL_IF_MACRO(newPos > finfo->startPos + finfo->size, ERR_PAST_EOF, 0);
  143. return(__PHYSFS_platformSeek(finfo->handle, newPos));
  144. } /* GRP_seek */
  145. static PHYSFS_sint64 GRP_fileLength(FileHandle *handle)
  146. {
  147. GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
  148. return(finfo->size);
  149. } /* GRP_fileLength */
  150. static int GRP_fileClose(FileHandle *handle)
  151. {
  152. GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
  153. BAIL_IF_MACRO(__PHYSFS_platformClose(finfo->handle), NULL, 0);
  154. free(handle->opaque);
  155. free(handle);
  156. return(1);
  157. } /* GRP_fileClose */
  158. static int openGrp(const char *filename, int forWriting,
  159. void **fh, PHYSFS_sint32 *count)
  160. {
  161. PHYSFS_uint8 buf[12];
  162. *fh = NULL;
  163. BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
  164. *fh = __PHYSFS_platformOpenRead(filename);
  165. BAIL_IF_MACRO(*fh == NULL, NULL, 0);
  166. if (__PHYSFS_platformRead(*fh, buf, 12, 1) != 1)
  167. goto openGrp_failed;
  168. if (memcmp(buf, "KenSilverman", 12) != 0)
  169. {
  170. __PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE);
  171. goto openGrp_failed;
  172. } /* if */
  173. if (__PHYSFS_platformRead(*fh, count, sizeof (PHYSFS_sint32), 1) != 1)
  174. goto openGrp_failed;
  175. *count = PHYSFS_swapSLE32(*count);
  176. return(1);
  177. openGrp_failed:
  178. if (*fh != NULL)
  179. __PHYSFS_platformClose(*fh);
  180. *count = -1;
  181. *fh = NULL;
  182. return(0);
  183. } /* openGrp */
  184. static int GRP_isArchive(const char *filename, int forWriting)
  185. {
  186. void *fh;
  187. int fileCount;
  188. int retval = openGrp(filename, forWriting, &fh, &fileCount);
  189. if (fh != NULL)
  190. __PHYSFS_platformClose(fh);
  191. return(retval);
  192. } /* GRP_isArchive */
  193. static DirHandle *GRP_openArchive(const char *name, int forWriting)
  194. {
  195. void *fh = NULL;
  196. int fileCount;
  197. DirHandle *retval = malloc(sizeof (DirHandle));
  198. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  199. retval->opaque = malloc(sizeof (GRPinfo));
  200. if (retval->opaque == NULL)
  201. {
  202. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  203. goto GRP_openArchive_failed;
  204. } /* if */
  205. ((GRPinfo *) retval->opaque)->filename = (char *) malloc(strlen(name) + 1);
  206. if (((GRPinfo *) retval->opaque)->filename == NULL)
  207. {
  208. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  209. goto GRP_openArchive_failed;
  210. } /* if */
  211. if (!openGrp(name, forWriting, &fh, &fileCount))
  212. goto GRP_openArchive_failed;
  213. strcpy(((GRPinfo *) retval->opaque)->filename, name);
  214. ((GRPinfo *) retval->opaque)->handle = fh;
  215. ((GRPinfo *) retval->opaque)->totalEntries = fileCount;
  216. retval->funcs = &__PHYSFS_DirFunctions_GRP;
  217. return(retval);
  218. GRP_openArchive_failed:
  219. if (retval != NULL)
  220. {
  221. if (retval->opaque != NULL)
  222. {
  223. if ( ((GRPinfo *) retval->opaque)->filename != NULL )
  224. free( ((GRPinfo *) retval->opaque)->filename );
  225. free(retval->opaque);
  226. } /* if */
  227. free(retval);
  228. } /* if */
  229. if (fh != NULL)
  230. __PHYSFS_platformClose(fh);
  231. return(NULL);
  232. } /* GRP_openArchive */
  233. static LinkedStringList *GRP_enumerateFiles(DirHandle *h,
  234. const char *dirname,
  235. int omitSymLinks)
  236. {
  237. PHYSFS_uint8 buf[16];
  238. GRPinfo *g = (GRPinfo *) (h->opaque);
  239. void *fh = g->handle;
  240. PHYSFS_uint32 i;
  241. LinkedStringList *retval = NULL;
  242. LinkedStringList *l = NULL;
  243. LinkedStringList *prev = NULL;
  244. /* !!! FIXME: Does this consider "/" ? */
  245. if (*dirname != '\0') /* no directories in GRP files. */
  246. return(NULL);
  247. /* jump to first file entry... */
  248. BAIL_IF_MACRO(!__PHYSFS_platformSeek(fh, 16), NULL, NULL);
  249. for (i = 0; i < g->totalEntries; i++)
  250. {
  251. BAIL_IF_MACRO(__PHYSFS_platformRead(fh, buf, 16, 1) != 1, NULL, retval);
  252. buf[12] = '\0'; /* FILENAME.EXT is all you get. */
  253. l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
  254. if (l == NULL)
  255. break;
  256. l->str = (char *) malloc(strlen((const char *) buf) + 1);
  257. if (l->str == NULL)
  258. {
  259. free(l);
  260. break;
  261. } /* if */
  262. strcpy(l->str, (const char *) buf);
  263. if (retval == NULL)
  264. retval = l;
  265. else
  266. prev->next = l;
  267. prev = l;
  268. l->next = NULL;
  269. } /* for */
  270. return(retval);
  271. } /* GRP_enumerateFiles */
  272. static PHYSFS_sint32 getFileEntry(DirHandle *h, const char *name,
  273. PHYSFS_uint32 *size)
  274. {
  275. PHYSFS_uint8 buf[16];
  276. GRPinfo *g = (GRPinfo *) (h->opaque);
  277. void *fh = g->handle;
  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. l = PHYSFS_swapSLE32(l);
  297. buf[12] = '\0'; /* FILENAME.EXT is all you get. */
  298. if (__PHYSFS_platformStricmp((const char *) buf, name) == 0)
  299. {
  300. if (size != NULL)
  301. *size = l;
  302. return(retval);
  303. } /* if */
  304. retval += l;
  305. } /* for */
  306. return(-1); /* not found. */
  307. } /* getFileEntry */
  308. static int GRP_exists(DirHandle *h, const char *name)
  309. {
  310. return(getFileEntry(h, name, NULL) != -1);
  311. } /* GRP_exists */
  312. static int GRP_isDirectory(DirHandle *h, const char *name)
  313. {
  314. return(0); /* never directories in a groupfile. */
  315. } /* GRP_isDirectory */
  316. static int GRP_isSymLink(DirHandle *h, const char *name)
  317. {
  318. return(0); /* never symlinks in a groupfile. */
  319. } /* GRP_isSymLink */
  320. static FileHandle *GRP_openRead(DirHandle *h, const char *name)
  321. {
  322. const char *filename = ((GRPinfo *) h->opaque)->filename;
  323. FileHandle *retval;
  324. GRPfileinfo *finfo;
  325. PHYSFS_uint32 size;
  326. PHYSFS_sint32 offset;
  327. offset = getFileEntry(h, name, &size);
  328. BAIL_IF_MACRO(offset == -1, ERR_NO_SUCH_FILE, NULL);
  329. retval = (FileHandle *) malloc(sizeof (FileHandle));
  330. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  331. finfo = (GRPfileinfo *) malloc(sizeof (GRPfileinfo));
  332. if (finfo == NULL)
  333. {
  334. free(retval);
  335. BAIL_IF_MACRO(1, ERR_OUT_OF_MEMORY, NULL);
  336. } /* if */
  337. finfo->handle = __PHYSFS_platformOpenRead(filename);
  338. if ( (finfo->handle == NULL) ||
  339. (!__PHYSFS_platformSeek(finfo->handle, offset)) )
  340. {
  341. free(finfo);
  342. free(retval);
  343. return(NULL);
  344. } /* if */
  345. finfo->startPos = offset;
  346. finfo->size = size;
  347. retval->opaque = (void *) finfo;
  348. retval->funcs = &__PHYSFS_FileFunctions_GRP;
  349. retval->dirHandle = h;
  350. return(retval);
  351. } /* GRP_openRead */
  352. #endif /* defined PHYSFS_SUPPORTS_GRP */
  353. /* end of grp.c ... */