grp.c 11 KB

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