grp.c 11 KB

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