8 wadFile_t *wadCleanup(wadFile_t *wf)
12 if (wf->fin) fclose(wf->fin);
13 if (wf->lpHeader) free(wf->lpHeader);
14 if (wf->lpLump) free(wf->lpLump);
15 if (wf->lpMip) free(wf->lpMip);
16 if (wf->wadfilename) free(wf->wadfilename);
23 int wadGetCurrentFileInfo ( wadFile_t *wf, char *szFileName, unsigned long fileNameBufferSize, unsigned long *filesize)
25 /* returns 0 if error, or 1 for sucess */
26 // if this fails you'll need to re-position the fileposition
27 // before attempting any other calls. e.g. call wadGoToFirstFile()
29 if (fread(wf->lpLump,sizeof(WAD3_LUMP),1,wf->fin)!=1)
31 strncpy(szFileName, wf->lpLump->name, fileNameBufferSize);
32 szFileName[fileNameBufferSize-1] = 0; // null terminate
34 *filesize = wf->lpLump->size;
39 int wadGoToFile(wadFile_t *wf, unsigned long filenum)
47 if (filenum >= wf->lpHeader->numlumps)
50 if (fseek(wf->fin,wf->lpHeader->infotableofs + (filenum * sizeof(WAD3_LUMP)),SEEK_SET) != 0)
53 wf->currentfile = filenum;
58 int wadGoToNextFile(wadFile_t *wf)
60 return(wadGoToFile(wf, wf->currentfile + 1));
63 int wadGoToFirstFile(wadFile_t *wf)
65 /* returns 0 if error, or 1 for sucess */
73 if (fseek(wf->fin,wf->lpHeader->infotableofs,SEEK_SET) != 0)
81 wadFile_t *wadOpen(const char* path)
90 memset (wf, 0, sizeof(*wf));
95 wf->fin=fopen(path,"rb");
97 return wadCleanup(wf);
100 if (fseek(wf->fin,0,SEEK_END) != 0)
101 return wadCleanup(wf);
103 wf->FileSize = ftell( wf->fin );
105 // Make sure it's at least big enough to manipulate the header
106 if (wf->FileSize < sizeof(WAD3_HEADER))
108 // WAD3 file is malformed.
109 return wadCleanup(wf);
112 // go back to the start
113 if (fseek(wf->fin,0,SEEK_SET)!=0)
114 return wadCleanup(wf);
117 wf->lpHeader = (LPWAD3_HEADER) malloc(sizeof(WAD3_HEADER));
118 wf->lpLump = (LPWAD3_LUMP) malloc(sizeof(WAD3_LUMP));
119 wf->lpMip = (LPWAD3_MIP) malloc(sizeof(WAD3_MIP));
121 if (!(wf->lpHeader) || !(wf->lpLump) || !(wf->lpMip))
122 return wadCleanup(wf);
125 if (fread(wf->lpHeader,sizeof(WAD3_HEADER),1,wf->fin)!=1)
126 return wadCleanup(wf);
128 if (wf->lpHeader->identification != WAD2_ID && wf->lpHeader->identification != WAD3_ID)
130 // Invalid WAD3 header id.
131 return wadCleanup(wf);
134 // Make sure our table is really there
135 if ( ((wf->lpHeader->numlumps * sizeof(WAD3_LUMP)) + wf->lpHeader->infotableofs) > wf->FileSize)
137 // WAD3 file is malformed.
138 return wadCleanup(wf);
141 // Store the name of the wadfile
142 if (!(wf->wadfilename = strdup(path)))
143 return wadCleanup(wf);
148 int wadOpenCurrentFileByNum (wadFile_t *wf, unsigned long filenumber)
150 /* returns 0 if error, or 1 for sucess */
151 return(wadGoToFile(wf, filenumber));
154 void wadCloseCurrentFile (wadFile_t *wf)
156 // nothing to do really...
159 unsigned long wadReadCurrentFile (wadFile_t *wf , char *bufferptr, unsigned long size)
161 // returns 0 if error, or the amount of data read into the buffer
162 if (fread(wf->lpLump,sizeof(WAD3_LUMP),1,wf->fin)!=1)
165 // dunno how to handle any other image types but this (yet)
166 if (wf->lpLump->type != WAD2_TYPE_MIP && wf->lpLump->type != WAD3_TYPE_MIP)
170 if (fseek(wf->fin, wf->lpLump->filepos, SEEK_SET) != 0)
173 if (fread(bufferptr,size,1,wf->fin) == 1)
181 .. or we could do it the long way, and process the file as we go..
186 unsigned long wadReadCurrentFile (wadFile_t *wf , char *bufferptr, unsigned long size)
188 // returns 0 if error, or the amount of data read into the buffer
189 unsigned long bufferpos;
190 unsigned long mipdatasize;
193 if (fread(wf->lpLump,sizeof(WAD3_LUMP),1,wf->fin)!=1)
196 if (wf->lpLump->type == WAD3_TYPE_MIP) // can we handle it ?
200 if (wf->lpLump->filepos >= wf->FileSize)
201 return 0; // malformed wad3
204 if (fseek(wf->fin, wf->lpLump->filepos, SEEK_SET) != 0)
208 if (fread(wf->lpMip,sizeof(WAD3_MIP),1,wf->fin)!=1)
212 memcpy(bufferptr, wf->lpMip, sizeof(WAD3_MIP));
213 bufferpos = sizeof(WAD3_MIP);
215 // now read the MIP data.
217 if (fseek(wf->fin, wf->lpLump->filepos + wf->lpMip->offsets[0], SEEK_SET) != 0)
220 mipdatasize = GET_MIP_DATA_SIZE(wf->lpMip->width,wf->lpMip->height);
222 if (fread(bufferptr+bufferpos, mipdatasize, 1, wf->fin)!=1)
225 bufferpos += mipdatasize;
227 // ok, that's the mip data itself, now grab the palette size.
228 if (fread(bufferptr+bufferpos,sizeof(WORD),1,wf->fin)!=1)
231 palettesize = *(WORD *)(bufferptr+bufferpos);
233 bufferpos += sizeof(WORD);
235 // grab the palette itself
236 if (fread(bufferptr+bufferpos,palettesize*3,1,wf->fin)!=1)
239 bufferpos += palettesize*3;
241 // and finally the one-word padding.
242 if (fread(bufferptr+bufferpos,sizeof(WORD),1,wf->fin)!=1)
245 bufferpos += sizeof(WORD);
247 return(bufferpos); // return the amount of bytes read.