8 wadFile_t *wadCleanup( wadFile_t *wf ){
22 if ( wf->wadfilename ) {
23 free( wf->wadfilename );
31 int wadGetCurrentFileInfo( wadFile_t *wf, char *szFileName, unsigned long fileNameBufferSize, unsigned long *filesize ){
32 /* returns 0 if error, or 1 for sucess */
33 // if this fails you'll need to re-position the fileposition
34 // before attempting any other calls. e.g. call wadGoToFirstFile()
36 if ( fread( wf->lpLump,sizeof( WAD3_LUMP ),1,wf->fin ) != 1 ) {
39 strncpy( szFileName, wf->lpLump->name, fileNameBufferSize );
40 szFileName[fileNameBufferSize - 1] = 0; // null terminate
42 *filesize = wf->lpLump->size;
47 int wadGoToFile( wadFile_t *wf, unsigned long filenum ){
56 if ( filenum >= wf->lpHeader->numlumps ) {
60 if ( fseek( wf->fin,wf->lpHeader->infotableofs + ( filenum * sizeof( WAD3_LUMP ) ),SEEK_SET ) != 0 ) {
64 wf->currentfile = filenum;
69 int wadGoToNextFile( wadFile_t *wf ){
70 return( wadGoToFile( wf, wf->currentfile + 1 ) );
73 int wadGoToFirstFile( wadFile_t *wf ){
74 /* returns 0 if error, or 1 for sucess */
84 if ( fseek( wf->fin,wf->lpHeader->infotableofs,SEEK_SET ) != 0 ) {
93 wadFile_t *wadOpen( const char* path ){
102 memset( wf, 0, sizeof( *wf ) );
108 wf->fin = fopen( path,"rb" );
109 if ( wf->fin == NULL ) {
110 return wadCleanup( wf );
114 if ( fseek( wf->fin,0,SEEK_END ) != 0 ) {
115 return wadCleanup( wf );
118 wf->FileSize = ftell( wf->fin );
120 // Make sure it's at least big enough to manipulate the header
121 if ( wf->FileSize < sizeof( WAD3_HEADER ) ) {
122 // WAD3 file is malformed.
123 return wadCleanup( wf );
126 // go back to the start
127 if ( fseek( wf->fin,0,SEEK_SET ) != 0 ) {
128 return wadCleanup( wf );
132 wf->lpHeader = (LPWAD3_HEADER) malloc( sizeof( WAD3_HEADER ) );
133 wf->lpLump = (LPWAD3_LUMP) malloc( sizeof( WAD3_LUMP ) );
134 wf->lpMip = (LPWAD3_MIP) malloc( sizeof( WAD3_MIP ) );
136 if ( !( wf->lpHeader ) || !( wf->lpLump ) || !( wf->lpMip ) ) {
137 return wadCleanup( wf );
141 if ( fread( wf->lpHeader,sizeof( WAD3_HEADER ),1,wf->fin ) != 1 ) {
142 return wadCleanup( wf );
145 if ( wf->lpHeader->identification != WAD2_ID && wf->lpHeader->identification != WAD3_ID ) {
146 // Invalid WAD3 header id.
147 return wadCleanup( wf );
150 // Make sure our table is really there
151 if ( ( ( wf->lpHeader->numlumps * sizeof( WAD3_LUMP ) ) + wf->lpHeader->infotableofs ) > wf->FileSize ) {
152 // WAD3 file is malformed.
153 return wadCleanup( wf );
156 // Store the name of the wadfile
157 if ( !( wf->wadfilename = strdup( path ) ) ) {
158 return wadCleanup( wf );
164 int wadOpenCurrentFileByNum( wadFile_t *wf, unsigned long filenumber ){
165 /* returns 0 if error, or 1 for sucess */
166 return( wadGoToFile( wf, filenumber ) );
169 void wadCloseCurrentFile( wadFile_t *wf ){
170 // nothing to do really...
173 unsigned long wadReadCurrentFile( wadFile_t *wf, char *bufferptr, unsigned long size ){
174 // returns 0 if error, or the amount of data read into the buffer
175 if ( fread( wf->lpLump,sizeof( WAD3_LUMP ),1,wf->fin ) != 1 ) {
179 // dunno how to handle any other image types but this (yet)
180 if ( wf->lpLump->type != WAD2_TYPE_MIP && wf->lpLump->type != WAD3_TYPE_MIP ) {
185 if ( fseek( wf->fin, wf->lpLump->filepos, SEEK_SET ) != 0 ) {
189 if ( fread( bufferptr,size,1,wf->fin ) == 1 ) {
199 .. or we could do it the long way, and process the file as we go..
204 unsigned long wadReadCurrentFile (wadFile_t *wf , char *bufferptr, unsigned long size)
206 // returns 0 if error, or the amount of data read into the buffer
207 unsigned long bufferpos;
208 unsigned long mipdatasize;
211 if (fread(wf->lpLump,sizeof(WAD3_LUMP),1,wf->fin)!=1)
214 if (wf->lpLump->type == WAD3_TYPE_MIP) // can we handle it ?
218 if (wf->lpLump->filepos >= wf->FileSize)
219 return 0; // malformed wad3
222 if (fseek(wf->fin, wf->lpLump->filepos, SEEK_SET) != 0)
226 if (fread(wf->lpMip,sizeof(WAD3_MIP),1,wf->fin)!=1)
230 memcpy(bufferptr, wf->lpMip, sizeof(WAD3_MIP));
231 bufferpos = sizeof(WAD3_MIP);
233 // now read the MIP data.
235 if (fseek(wf->fin, wf->lpLump->filepos + wf->lpMip->offsets[0], SEEK_SET) != 0)
238 mipdatasize = GET_MIP_DATA_SIZE(wf->lpMip->width,wf->lpMip->height);
240 if (fread(bufferptr+bufferpos, mipdatasize, 1, wf->fin)!=1)
243 bufferpos += mipdatasize;
245 // ok, that's the mip data itself, now grab the palette size.
246 if (fread(bufferptr+bufferpos,sizeof(WORD),1,wf->fin)!=1)
249 palettesize = *(WORD *)(bufferptr+bufferpos);
251 bufferpos += sizeof(WORD);
253 // grab the palette itself
254 if (fread(bufferptr+bufferpos,palettesize*3,1,wf->fin)!=1)
257 bufferpos += palettesize*3;
259 // and finally the one-word padding.
260 if (fread(bufferptr+bufferpos,sizeof(WORD),1,wf->fin)!=1)
263 bufferpos += sizeof(WORD);
265 return(bufferpos); // return the amount of bytes read.