grp.c 10 KB

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