grp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. /* !!! FIXME: Using the same file handle for all reads is a RACE CONDITION! */
  43. typedef struct
  44. {
  45. FILE *handle;
  46. PHYSFS_uint32 totalEntries;
  47. } GRPinfo;
  48. typedef struct
  49. {
  50. PHYSFS_uint32 startPos;
  51. PHYSFS_uint32 curPos;
  52. PHYSFS_uint32 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. fclose( ((GRPinfo *) h->opaque)->handle );
  106. free(h->opaque);
  107. free(h);
  108. } /* GRP_dirClose */
  109. static PHYSFS_sint64 GRP_read(FileHandle *handle, void *buffer,
  110. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  111. {
  112. GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
  113. FILE *fh = (FILE *) (((GRPinfo *) (handle->dirHandle->opaque))->handle);
  114. PHYSFS_uint32 bytesLeft = (finfo->startPos + finfo->size) - finfo->curPos;
  115. PHYSFS_uint32 objsLeft = bytesLeft / objSize;
  116. size_t retval = 0;
  117. if (objsLeft < objCount)
  118. objCount = objsLeft;
  119. errno = 0;
  120. BAIL_IF_MACRO(fseek(fh,finfo->curPos,SEEK_SET) == -1,strerror(errno),-1);
  121. errno = 0;
  122. retval = fread(buffer, objSize, objCount, fh);
  123. finfo->curPos += (retval * objSize);
  124. BAIL_IF_MACRO((retval < (size_t) objCount) && (ferror(fh)),
  125. strerror(errno), (PHYSFS_sint64) retval);
  126. return((PHYSFS_sint64) retval);
  127. } /* GRP_read */
  128. static int GRP_eof(FileHandle *handle)
  129. {
  130. GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
  131. return(finfo->curPos >= finfo->startPos + finfo->size);
  132. } /* GRP_eof */
  133. static PHYSFS_sint64 GRP_tell(FileHandle *handle)
  134. {
  135. GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
  136. return(finfo->curPos - finfo->startPos);
  137. } /* GRP_tell */
  138. static int GRP_seek(FileHandle *handle, PHYSFS_uint64 offset)
  139. {
  140. GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
  141. int 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. finfo->curPos = newPos;
  145. return(1);
  146. } /* GRP_seek */
  147. static PHYSFS_sint64 GRP_fileLength(FileHandle *handle)
  148. {
  149. GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
  150. return(finfo->size);
  151. } /* GRP_fileLength */
  152. static int GRP_fileClose(FileHandle *handle)
  153. {
  154. free(handle->opaque);
  155. free(handle);
  156. return(1);
  157. } /* GRP_fileClose */
  158. static int openGrp(const char *filename, int forWriting, FILE **fh, PHYSFS_sint32 *count)
  159. {
  160. char buf[12];
  161. *fh = NULL;
  162. BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
  163. errno = 0;
  164. *fh = fopen(filename, "rb");
  165. BAIL_IF_MACRO(*fh == NULL, strerror(errno), 0);
  166. errno = 0;
  167. BAIL_IF_MACRO(fread(buf, 12, 1, *fh) != 1, strerror(errno), 0);
  168. BAIL_IF_MACRO(memcmp(buf, "KenSilverman", 12) != 0,
  169. ERR_UNSUPPORTED_ARCHIVE, 0);
  170. if (fread(count, sizeof (PHYSFS_sint32), 1, *fh) != 1)
  171. *count = 0;
  172. return(1);
  173. } /* openGrp */
  174. static int GRP_isArchive(const char *filename, int forWriting)
  175. {
  176. FILE *fh;
  177. int fileCount;
  178. int retval = openGrp(filename, forWriting, &fh, &fileCount);
  179. if (fh != NULL)
  180. fclose(fh);
  181. return(retval);
  182. } /* GRP_isArchive */
  183. static DirHandle *GRP_openArchive(const char *name, int forWriting)
  184. {
  185. FILE *fh;
  186. int fileCount;
  187. DirHandle *retval = malloc(sizeof (DirHandle));
  188. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  189. retval->opaque = malloc(sizeof (GRPinfo));
  190. if (retval->opaque == NULL)
  191. {
  192. free(retval);
  193. BAIL_IF_MACRO(1, ERR_OUT_OF_MEMORY, NULL);
  194. } /* if */
  195. if (!openGrp(name, forWriting, &fh, &fileCount))
  196. {
  197. if (fh != NULL)
  198. fclose(fh);
  199. free(retval->opaque);
  200. free(retval);
  201. } /* if */
  202. ((GRPinfo *) retval->opaque)->handle = fh;
  203. ((GRPinfo *) retval->opaque)->totalEntries = fileCount;
  204. retval->funcs = &__PHYSFS_DirFunctions_GRP;
  205. return(retval);
  206. } /* GRP_openArchive */
  207. static LinkedStringList *GRP_enumerateFiles(DirHandle *h,
  208. const char *dirname,
  209. int omitSymLinks)
  210. {
  211. char buf[16];
  212. GRPinfo *g = (GRPinfo *) (h->opaque);
  213. FILE *fh = g->handle;
  214. int i;
  215. LinkedStringList *retval = NULL;
  216. LinkedStringList *l = NULL;
  217. LinkedStringList *prev = NULL;
  218. if (*dirname != '\0') /* no directories in GRP files. */
  219. return(NULL);
  220. /* jump to first file entry... */
  221. errno = 0;
  222. BAIL_IF_MACRO(fseek(fh, 16, SEEK_SET) == -1, strerror(errno), NULL);
  223. for (i = 0; i < g->totalEntries; i++)
  224. {
  225. errno = 0;
  226. BAIL_IF_MACRO(fread(buf, 16, 1, fh) != 1, strerror(errno), retval);
  227. buf[12] = '\0'; /* FILENAME.EXT is all you get. */
  228. l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
  229. if (l == NULL)
  230. break;
  231. l->str = (char *) malloc(strlen(buf) + 1);
  232. if (l->str == NULL)
  233. {
  234. free(l);
  235. break;
  236. } /* if */
  237. strcpy(l->str, buf);
  238. if (retval == NULL)
  239. retval = l;
  240. else
  241. prev->next = l;
  242. prev = l;
  243. l->next = NULL;
  244. } /* for */
  245. return(retval);
  246. } /* GRP_enumerateFiles */
  247. static PHYSFS_sint32 getFileEntry(DirHandle *h, const char *name,
  248. PHYSFS_sint32 *size)
  249. {
  250. char buf[16];
  251. GRPinfo *g = (GRPinfo *) (h->opaque);
  252. FILE *fh = g->handle;
  253. int i;
  254. char *ptr;
  255. int retval = (g->totalEntries + 1) * 16; /* offset of raw file data */
  256. /* Rule out filenames to avoid unneeded file i/o... */
  257. if (strchr(name, '/') != NULL) /* no directories in groupfiles. */
  258. return(-1);
  259. ptr = strchr(name, '.');
  260. if ((ptr) && (strlen(ptr) > 4)) /* 3 char extension at most. */
  261. return(-1);
  262. if (strlen(name) > 12)
  263. return(-1);
  264. /* jump to first file entry... */
  265. errno = 0;
  266. BAIL_IF_MACRO(fseek(fh, 16, SEEK_SET) == -1, strerror(errno), -1);
  267. for (i = 0; i < g->totalEntries; i++)
  268. {
  269. PHYSFS_sint32 l = 0;
  270. errno = 0;
  271. BAIL_IF_MACRO(fread(buf, 12, 1, fh) != 1, strerror(errno), -1);
  272. errno = 0;
  273. BAIL_IF_MACRO(fread(&l, sizeof (l), 1, fh) != 1, strerror(errno), -1);
  274. buf[12] = '\0'; /* FILENAME.EXT is all you get. */
  275. if (__PHYSFS_platformStricmp(buf, name) == 0)
  276. {
  277. if (size != NULL)
  278. *size = l;
  279. return(retval);
  280. } /* if */
  281. retval += l;
  282. } /* for */
  283. return(-1); /* not found. */
  284. } /* getFileEntry */
  285. static int GRP_exists(DirHandle *h, const char *name)
  286. {
  287. return(getFileEntry(h, name, NULL) != -1);
  288. } /* GRP_exists */
  289. static int GRP_isDirectory(DirHandle *h, const char *name)
  290. {
  291. return(0); /* never directories in a groupfile. */
  292. } /* GRP_isDirectory */
  293. static int GRP_isSymLink(DirHandle *h, const char *name)
  294. {
  295. return(0); /* never symlinks in a groupfile. */
  296. } /* GRP_isSymLink */
  297. static FileHandle *GRP_openRead(DirHandle *h, const char *name)
  298. {
  299. FileHandle *retval;
  300. GRPfileinfo *finfo;
  301. PHYSFS_sint32 size;
  302. PHYSFS_sint32 offset;
  303. offset = getFileEntry(h, name, &size);
  304. BAIL_IF_MACRO(offset == -1, ERR_NO_SUCH_FILE, NULL);
  305. retval = (FileHandle *) malloc(sizeof (FileHandle));
  306. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  307. finfo = (GRPfileinfo *) malloc(sizeof (GRPfileinfo));
  308. if (finfo == NULL)
  309. {
  310. free(retval);
  311. BAIL_IF_MACRO(1, ERR_OUT_OF_MEMORY, NULL);
  312. } /* if */
  313. finfo->startPos = offset;
  314. finfo->curPos = offset;
  315. finfo->size = size;
  316. retval->opaque = (void *) finfo;
  317. retval->funcs = &__PHYSFS_FileFunctions_GRP;
  318. retval->dirHandle = h;
  319. return(retval);
  320. } /* GRP_openRead */
  321. /* end of grp.c ... */