author  Ryan C. Gordon <icculus@icculus.org> 
Mon, 16 Jul 2001 14:36:02 +0000  
changeset 39  bc29e1ee7ef6 
parent 37  768c8484b0cd 
child 41  ff60d39d0862 
permissions  rwrr 
24
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

1 
/* 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

2 
* GRP support routines for PhysicsFS. 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

3 
* 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

4 
* This driver handles BUILD engine archives ("groupfiles"). This format 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

5 
* (but not this driver) was put together by Ken Silverman. 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

6 
* 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

7 
* The format is simple enough. In Ken's words: 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

8 
* 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

9 
* What's the .GRP file format? 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

10 
* 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

11 
* The ".grp" file format is just a collection of a lot of files stored 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

12 
* into 1 big one. I tried to make the format as simple as possible: The 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

13 
* first 12 bytes contains my name, "KenSilverman". The next 4 bytes is 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

14 
* the number of files that were compacted into the group file. Then for 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

15 
* each file, there is a 16 byte structure, where the first 12 bytes are 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

16 
* the filename, and the last 4 bytes are the file's size. The rest of 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

17 
* the group file is just the raw data packed one after the other in the 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

18 
* same order as the list of files. 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

19 
* 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

20 
* (That info is from http://www.advsys.net/ken/build.htm ...) 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

21 
* 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

22 
* As it was never a concern in the DOSbased Duke Nukem days, I treat these 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

23 
* archives as CASEINSENSITIVE. Opening "myfile.txt" and "MYFILE.TXT" both 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

24 
* work, and get the same file. 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

25 
* 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

26 
* Please see the file LICENSE in the source's root directory. 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

27 
* 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

28 
* This file written by Ryan C. Gordon. 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

29 
*/ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

30 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

31 
#include <stdio.h> 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

32 
#include <stdlib.h> 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

33 
#include <string.h> 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

34 
#include <errno.h> 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

35 
#include <sys/stat.h> 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

36 
#include <sys/types.h> 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

37 
#include <fcntl.h> 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

38 
#include <unistd.h> 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

39 
#include <assert.h> 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

40 
#include "physfs.h" 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

41 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

42 
#define __PHYSICSFS_INTERNAL__ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

43 
#include "physfs_internal.h" 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

44 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

45 
#if (!defined PHYSFS_SUPPORTS_GRP) 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

46 
#error PHYSFS_SUPPORTS_GRP must be defined. 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

47 
#endif 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

48 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

49 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

50 
typedef struct 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

51 
{ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

52 
FILE *handle; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

53 
int totalEntries; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

54 
} GRPinfo; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

55 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

56 
typedef struct 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

57 
{ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

58 
int startPos; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

59 
int curPos; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

60 
int size; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

61 
} GRPfileinfo; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

62 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

63 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

64 
extern const DirFunctions __PHYSFS_DirFunctions_GRP; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

65 
static const FileFunctions __PHYSFS_FileFunctions_GRP; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

66 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

67 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

68 
static int GRP_read(FileHandle *handle, void *buffer, 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

69 
unsigned int objSize, unsigned int objCount) 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

70 
{ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

71 
GRPfileinfo *finfo = (GRPfileinfo *) (handle>opaque); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

72 
FILE *fh = (FILE *) (((GRPinfo *) (handle>dirHandle>opaque))>handle); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

73 
int bytesLeft = (finfo>startPos + finfo>size)  finfo>curPos; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

74 
int objsLeft = bytesLeft / objSize; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

75 
int retval = 0; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

76 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

77 
if (objsLeft < objCount) 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

78 
objCount = objsLeft; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

79 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

80 
errno = 0; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

81 
BAIL_IF_MACRO(fseek(fh,finfo>curPos,SEEK_SET) == 1,strerror(errno),1); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

82 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

83 
errno = 0; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

84 
retval = fread(buffer, objSize, objCount, fh); 
26
575e03541d5e
Logic fixes; seems to work rather well with KenBuild, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
24
diff
changeset

85 
finfo>curPos += (retval * objSize); 
24
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

86 
BAIL_IF_MACRO((retval < objCount) && (ferror(fh)),strerror(errno),retval); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

87 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

88 
return(retval); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

89 
} /* GRP_read */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

90 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

91 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

92 
static int GRP_eof(FileHandle *handle) 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

93 
{ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

94 
GRPfileinfo *finfo = (GRPfileinfo *) (handle>opaque); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

95 
return(finfo>curPos >= finfo>startPos + finfo>size); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

96 
} /* GRP_eof */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

97 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

98 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

99 
static int GRP_tell(FileHandle *handle) 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

100 
{ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

101 
GRPfileinfo *finfo = (GRPfileinfo *) (handle>opaque); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

102 
return(finfo>curPos  finfo>startPos); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

103 
} /* GRP_tell */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

104 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

105 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

106 
static int GRP_seek(FileHandle *handle, int offset) 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

107 
{ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

108 
GRPfileinfo *finfo = (GRPfileinfo *) (handle>opaque); 
26
575e03541d5e
Logic fixes; seems to work rather well with KenBuild, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
24
diff
changeset

109 
int newPos = finfo>startPos + offset; 
24
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

110 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

111 
BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

112 
BAIL_IF_MACRO(newPos > finfo>startPos + finfo>size, ERR_PAST_EOF, 0); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

113 
finfo>curPos = newPos; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

114 
return(1); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

115 
} /* GRP_seek */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

116 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

117 

28
529214f57d1b
Added PHYSFS_fileLength(). Bleh.
Ryan C. Gordon <icculus@icculus.org>
parents:
26
diff
changeset

118 
static int GRP_fileLength(FileHandle *handle) 
529214f57d1b
Added PHYSFS_fileLength(). Bleh.
Ryan C. Gordon <icculus@icculus.org>
parents:
26
diff
changeset

119 
{ 
529214f57d1b
Added PHYSFS_fileLength(). Bleh.
Ryan C. Gordon <icculus@icculus.org>
parents:
26
diff
changeset

120 
GRPfileinfo *finfo = (GRPfileinfo *) (handle>opaque); 
529214f57d1b
Added PHYSFS_fileLength(). Bleh.
Ryan C. Gordon <icculus@icculus.org>
parents:
26
diff
changeset

121 
return(finfo>size); 
529214f57d1b
Added PHYSFS_fileLength(). Bleh.
Ryan C. Gordon <icculus@icculus.org>
parents:
26
diff
changeset

122 
} /* GRP_fileLength */ 
529214f57d1b
Added PHYSFS_fileLength(). Bleh.
Ryan C. Gordon <icculus@icculus.org>
parents:
26
diff
changeset

123 

529214f57d1b
Added PHYSFS_fileLength(). Bleh.
Ryan C. Gordon <icculus@icculus.org>
parents:
26
diff
changeset

124 

24
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

125 
static int GRP_fileClose(FileHandle *handle) 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

126 
{ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

127 
free(handle>opaque); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

128 
free(handle); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

129 
return(1); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

130 
} /* GRP_fileClose */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

131 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

132 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

133 
static int openGrp(const char *filename, int forWriting, FILE **fh, int *count) 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

134 
{ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

135 
char buf[12]; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

136 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

137 
assert(sizeof (int) == 4); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

138 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

139 
*fh = NULL; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

140 
BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

141 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

142 
errno = 0; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

143 
*fh = fopen(filename, "rb"); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

144 
BAIL_IF_MACRO(*fh == NULL, strerror(errno), 0); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

145 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

146 
errno = 0; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

147 
BAIL_IF_MACRO(fread(buf, 12, 1, *fh) != 1, strerror(errno), 0); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

148 
BAIL_IF_MACRO(strncmp(buf, "KenSilverman", 12) != 0, 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

149 
ERR_UNSUPPORTED_ARCHIVE, 0); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

150 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

151 
if (fread(count, 4, 1, *fh) != 1) 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

152 
*count = 0; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

153 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

154 
return(1); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

155 
} /* openGrp */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

156 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

157 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

158 
static int GRP_isArchive(const char *filename, int forWriting) 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

159 
{ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

160 
FILE *fh; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

161 
int fileCount; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

162 
int retval = openGrp(filename, forWriting, &fh, &fileCount); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

163 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

164 
if (fh != NULL) 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

165 
fclose(fh); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

166 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

167 
return(retval); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

168 
} /* GRP_isArchive */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

169 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

170 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

171 
static DirHandle *GRP_openArchive(const char *name, int forWriting) 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

172 
{ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

173 
FILE *fh; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

174 
int fileCount; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

175 
DirHandle *retval = malloc(sizeof (DirHandle)); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

176 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

177 
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

178 
retval>opaque = malloc(sizeof (GRPinfo)); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

179 
if (retval>opaque == NULL) 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

180 
{ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

181 
free(retval); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

182 
BAIL_IF_MACRO(1, ERR_OUT_OF_MEMORY, NULL); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

183 
} /* if */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

184 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

185 
if (!openGrp(name, forWriting, &fh, &fileCount)) 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

186 
{ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

187 
if (fh != NULL) 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

188 
fclose(fh); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

189 
free(retval>opaque); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

190 
free(retval); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

191 
} /* if */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

192 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

193 
((GRPinfo *) retval>opaque)>handle = fh; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

194 
((GRPinfo *) retval>opaque)>totalEntries = fileCount; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

195 
retval>funcs = &__PHYSFS_DirFunctions_GRP; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

196 
return(retval); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

197 
} /* GRP_openArchive */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

198 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

199 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

200 
static LinkedStringList *GRP_enumerateFiles(DirHandle *h, const char *dirname) 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

201 
{ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

202 
char buf[16]; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

203 
GRPinfo *g = (GRPinfo *) (h>opaque); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

204 
FILE *fh = g>handle; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

205 
int i; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

206 
LinkedStringList *retval = NULL; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

207 
LinkedStringList *l = NULL; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

208 
LinkedStringList *prev = NULL; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

209 

31
41c23ac35615
Fixed GRP_enumerateFiles() so that it will only list files in the root of
Ryan C. Gordon <icculus@icculus.org>
parents:
29
diff
changeset

210 
if (*dirname != '\0') /* no directories in GRP files. */ 
41c23ac35615
Fixed GRP_enumerateFiles() so that it will only list files in the root of
Ryan C. Gordon <icculus@icculus.org>
parents:
29
diff
changeset

211 
return(NULL); 
41c23ac35615
Fixed GRP_enumerateFiles() so that it will only list files in the root of
Ryan C. Gordon <icculus@icculus.org>
parents:
29
diff
changeset

212 

24
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

213 
/* jump to first file entry... */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

214 
errno = 0; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

215 
BAIL_IF_MACRO(fseek(fh, 16, SEEK_SET) == 1, strerror(errno), NULL); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

216 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

217 
for (i = 0; i < g>totalEntries; i++) 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

218 
{ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

219 
errno = 0; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

220 
BAIL_IF_MACRO(fread(buf, 16, 1, fh) != 1, strerror(errno), retval); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

221 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

222 
buf[12] = '\0'; /* FILENAME.EXT is all you get. */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

223 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

224 
l = (LinkedStringList *) malloc(sizeof (LinkedStringList)); 
39
bc29e1ee7ef6
Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset

225 
if (l == NULL) 
24
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

226 
break; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

227 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

228 
l>str = (char *) malloc(strlen(buf) + 1); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

229 
if (l>str == NULL) 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

230 
{ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

231 
free(l); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

232 
break; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

233 
} /* if */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

234 

39
bc29e1ee7ef6
Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset

235 
strcpy(l>str, buf); 
bc29e1ee7ef6
Lots of bugfixes, enhancements, and corrections due to the work on
Ryan C. Gordon <icculus@icculus.org>
parents:
37
diff
changeset

236 

24
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

237 
if (retval == NULL) 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

238 
retval = l; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

239 
else 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

240 
prev>next = l; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

241 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

242 
prev = l; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

243 
l>next = NULL; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

244 
} /* for */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

245 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

246 
return(retval); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

247 
} /* GRP_enumerateFiles */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

248 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

249 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

250 
static int getFileEntry(DirHandle *h, const char *name, int *size) 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

251 
{ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

252 
char buf[16]; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

253 
GRPinfo *g = (GRPinfo *) (h>opaque); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

254 
FILE *fh = g>handle; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

255 
int i; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

256 
char *ptr; 
26
575e03541d5e
Logic fixes; seems to work rather well with KenBuild, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
24
diff
changeset

257 
int retval = (g>totalEntries + 1) * 16; /* offset of raw file data */ 
24
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

258 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

259 
/* Rule out filenames to avoid unneeded file i/o... */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

260 
if (strchr(name, '/') != NULL) /* no directories in groupfiles. */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

261 
return(1); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

262 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

263 
ptr = strchr(name, '.'); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

264 
if ((ptr) && (strlen(ptr) > 4)) /* 3 char extension at most. */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

265 
return(1); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

266 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

267 
if (strlen(name) > 12) 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

268 
return(1); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

269 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

270 
/* jump to first file entry... */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

271 
errno = 0; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

272 
BAIL_IF_MACRO(fseek(fh, 16, SEEK_SET) == 1, strerror(errno), 1); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

273 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

274 
for (i = 0; i < g>totalEntries; i++) 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

275 
{ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

276 
int fsize; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

277 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

278 
errno = 0; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

279 
BAIL_IF_MACRO(fread(buf, 16, 1, fh) != 1, strerror(errno), 1); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

280 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

281 
fsize = *((int *) (buf + 12)); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

282 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

283 
buf[12] = '\0'; /* FILENAME.EXT is all you get. */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

284 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

285 
if (__PHYSFS_platformStricmp(buf, name) == 0) 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

286 
{ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

287 
if (size != NULL) 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

288 
*size = fsize; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

289 
return(retval); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

290 
} /* if */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

291 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

292 
retval += fsize; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

293 
} /* for */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

294 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

295 
return(1); /* not found. */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

296 
} /* getFileEntry */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

297 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

298 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

299 
static int GRP_exists(DirHandle *h, const char *name) 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

300 
{ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

301 
return(getFileEntry(h, name, NULL) != 1); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

302 
} /* GRP_exists */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

303 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

304 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

305 
static int GRP_isDirectory(DirHandle *h, const char *name) 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

306 
{ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

307 
return(0); /* never directories in a groupfile. */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

308 
} /* GRP_isDirectory */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

309 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

310 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

311 
static int GRP_isSymLink(DirHandle *h, const char *name) 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

312 
{ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

313 
return(0); /* never symlinks in a groupfile. */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

314 
} /* GRP_isSymLink */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

315 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

316 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

317 
static FileHandle *GRP_openRead(DirHandle *h, const char *name) 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

318 
{ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

319 
FileHandle *retval; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

320 
GRPfileinfo *finfo; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

321 
int size, offset; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

322 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

323 
offset = getFileEntry(h, name, &size); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

324 
BAIL_IF_MACRO(offset == 1, ERR_NO_SUCH_FILE, NULL); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

325 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

326 
retval = (FileHandle *) malloc(sizeof (FileHandle)); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

327 
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

328 
finfo = (GRPfileinfo *) malloc(sizeof (GRPfileinfo)); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

329 
if (finfo == NULL) 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

330 
{ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

331 
free(retval); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

332 
BAIL_IF_MACRO(1, ERR_OUT_OF_MEMORY, NULL); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

333 
} /* if */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

334 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

335 
finfo>startPos = offset; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

336 
finfo>curPos = offset; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

337 
finfo>size = size; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

338 
retval>opaque = (void *) finfo; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

339 
retval>funcs = &__PHYSFS_FileFunctions_GRP; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

340 
retval>dirHandle = h; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

341 
return(retval); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

342 
} /* GRP_openRead */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

343 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

344 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

345 
static void GRP_dirClose(DirHandle *h) 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

346 
{ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

347 
fclose( ((GRPinfo *) h>opaque)>handle ); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

348 
free(h>opaque); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

349 
free(h); 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

350 
} /* GRP_dirClose */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

351 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

352 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

353 
static const FileFunctions __PHYSFS_FileFunctions_GRP = 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

354 
{ 
28
529214f57d1b
Added PHYSFS_fileLength(). Bleh.
Ryan C. Gordon <icculus@icculus.org>
parents:
26
diff
changeset

355 
GRP_read, /* read() method */ 
529214f57d1b
Added PHYSFS_fileLength(). Bleh.
Ryan C. Gordon <icculus@icculus.org>
parents:
26
diff
changeset

356 
NULL, /* write() method */ 
529214f57d1b
Added PHYSFS_fileLength(). Bleh.
Ryan C. Gordon <icculus@icculus.org>
parents:
26
diff
changeset

357 
GRP_eof, /* eof() method */ 
529214f57d1b
Added PHYSFS_fileLength(). Bleh.
Ryan C. Gordon <icculus@icculus.org>
parents:
26
diff
changeset

358 
GRP_tell, /* tell() method */ 
529214f57d1b
Added PHYSFS_fileLength(). Bleh.
Ryan C. Gordon <icculus@icculus.org>
parents:
26
diff
changeset

359 
GRP_seek, /* seek() method */ 
529214f57d1b
Added PHYSFS_fileLength(). Bleh.
Ryan C. Gordon <icculus@icculus.org>
parents:
26
diff
changeset

360 
GRP_fileLength, /* fileLength() method */ 
529214f57d1b
Added PHYSFS_fileLength(). Bleh.
Ryan C. Gordon <icculus@icculus.org>
parents:
26
diff
changeset

361 
GRP_fileClose /* fileClose() method */ 
24
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

362 
}; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

363 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

364 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

365 
const DirFunctions __PHYSFS_DirFunctions_GRP = 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

366 
{ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

367 
GRP_isArchive, /* isArchive() method */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

368 
GRP_openArchive, /* openArchive() method */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

369 
GRP_enumerateFiles, /* enumerateFiles() method */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

370 
GRP_exists, /* exists() method */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

371 
GRP_isDirectory, /* isDirectory() method */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

372 
GRP_isSymLink, /* isSymLink() method */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

373 
GRP_openRead, /* openRead() method */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

374 
NULL, /* openWrite() method */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

375 
NULL, /* openAppend() method */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

376 
NULL, /* remove() method */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

377 
NULL, /* mkdir() method */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

378 
GRP_dirClose /* dirClose() method */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

379 
}; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

380 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

381 
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_GRP = 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

382 
{ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

383 
"GRP", 
29
91b45664ec85
Added more fields to the ArchiveInfo struct: author and url.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset

384 
"Build engine Groupfile format", 
37
768c8484b0cd
Updated archiver info to format I think is best for text.
Ryan C. Gordon <icculus@icculus.org>
parents:
31
diff
changeset

385 
"Ryan C. Gordon (icculus@linuxgames.com)", 
768c8484b0cd
Updated archiver info to format I think is best for text.
Ryan C. Gordon <icculus@icculus.org>
parents:
31
diff
changeset

386 
"http://www.icculus.org/~icculus/", 
24
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

387 
}; 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

388 

b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

389 
/* end of grp.c ... */ 
b050804123a3
Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

390 