grp.c 10 KB

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