// close all hlwad files and free their lumps data
for (i = 0;i < Mem_ExpandableArray_IndexRange(&wad.hlwads);i++)
{
- w = Mem_ExpandableArray_RecordAtIndex(&wad.hlwads, i);
+ w = (mwad_t *) Mem_ExpandableArray_RecordAtIndex(&wad.hlwads, i);
if (!w)
continue;
if (w->file)
memset(&wad, 0, sizeof(wad));
}
-unsigned char *W_GetLumpName(const char *name)
+unsigned char *W_GetLumpName(const char *name, fs_offset_t *returnfilesize)
{
int i;
fs_offset_t filesize;
}
for (lump = wad.gfx.lumps, i = 0;i < wad.gfx.numlumps;i++, lump++)
+ {
if (!strcmp(clean, lump->name))
+ {
+ if (returnfilesize)
+ *returnfilesize = lump->size;
return (wad.gfx_base + lump->filepos);
+ }
+ }
return NULL;
}
if (!wad.hlwads.mempool)
Mem_ExpandableArray_NewArray(&wad.hlwads, cls.permanentmempool, sizeof(mwad_t), 16);
- w = Mem_ExpandableArray_AllocRecord(&wad.hlwads);
+ w = (mwad_t *) Mem_ExpandableArray_AllocRecord(&wad.hlwads);
w->file = file;
w->numlumps = numlumps;
- w->lumps = Mem_Alloc(cls.permanentmempool, w->numlumps * sizeof(lumpinfo_t));
+ w->lumps = (lumpinfo_t *) Mem_Alloc(cls.permanentmempool, w->numlumps * sizeof(lumpinfo_t));
if (!w->lumps)
{
// leaves the file open
}
-unsigned char *W_ConvertWAD3TextureBGRA(miptex_t *tex)
+unsigned char *W_ConvertWAD3TextureBGRA(sizebuf_t *sb)
{
unsigned char *in, *data, *out, *pal;
int d, p;
+ unsigned char name[16];
+ unsigned int mipoffset[4];
+
+ MSG_BeginReading(sb);
+ MSG_ReadBytes(sb, 16, name);
+ image_width = MSG_ReadLittleLong(sb);
+ image_height = MSG_ReadLittleLong(sb);
+ mipoffset[0] = MSG_ReadLittleLong(sb);
+ mipoffset[1] = MSG_ReadLittleLong(sb); // should be mipoffset[0] + image_width*image_height
+ mipoffset[2] = MSG_ReadLittleLong(sb); // should be mipoffset[1] + image_width*image_height/4
+ mipoffset[3] = MSG_ReadLittleLong(sb); // should be mipoffset[2] + image_width*image_height/16
+ pal = sb->data + mipoffset[3] + (image_width / 8 * image_height / 8) + 2;
+
+ // bail if any data looks wrong
+ if (image_width < 0
+ || image_width > 4096
+ || image_height < 0
+ || image_height > 4096
+ || mipoffset[0] != 40
+ || mipoffset[1] != mipoffset[0] + image_width * image_height
+ || mipoffset[2] != mipoffset[1] + image_width / 2 * image_height / 2
+ || mipoffset[3] != mipoffset[2] + image_width / 4 * image_height / 4
+ || (unsigned int)sb->cursize < (mipoffset[3] + image_width / 8 * image_height / 8 + 2 + 768))
+ return NULL;
- in = (unsigned char *)tex + tex->offsets[0];
- data = out = (unsigned char *)Mem_Alloc(tempmempool, tex->width * tex->height * 4);
+ in = (unsigned char *)sb->data + mipoffset[0];
+ data = out = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
if (!data)
return NULL;
- image_width = tex->width;
- image_height = tex->height;
- pal = in + (((image_width * image_height) * 85) >> 6);
- pal += 2;
for (d = 0;d < image_width * image_height;d++)
{
p = *in++;
- if (tex->name[0] == '{' && p == 255)
+ if (name[0] == '{' && p == 255)
out[0] = out[1] = out[2] = out[3] = 0;
else
{
unsigned char *W_GetTextureBGRA(char *name)
{
- unsigned int i, j, k;
- miptex_t *tex;
+ unsigned int i, k;
+ sizebuf_t sb;
unsigned char *data;
mwad_t *w;
char texname[17];
if (FS_Seek(w->file, w->lumps[i].filepos, SEEK_SET))
{Con_Print("W_GetTexture: corrupt WAD3 file\n");return NULL;}
- tex = (miptex_t *)Mem_Alloc(tempmempool, w->lumps[i].disksize);
- if (!tex)
+ MSG_InitReadBuffer(&sb, (unsigned char *)Mem_Alloc(tempmempool, w->lumps[i].disksize), w->lumps[i].disksize);
+ if (!sb.data)
return NULL;
- if (FS_Read(w->file, tex, w->lumps[i].size) < w->lumps[i].disksize)
+ if (FS_Read(w->file, sb.data, w->lumps[i].size) < w->lumps[i].disksize)
{Con_Print("W_GetTexture: corrupt WAD3 file\n");return NULL;}
- tex->width = LittleLong(tex->width);
- tex->height = LittleLong(tex->height);
- for (j = 0;j < MIPLEVELS;j++)
- tex->offsets[j] = LittleLong(tex->offsets[j]);
- data = W_ConvertWAD3TextureBGRA(tex);
- Mem_Free(tex);
+ data = W_ConvertWAD3TextureBGRA(&sb);
+ Mem_Free(sb.data);
return data;
}
}