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 (!file)
{
if (complain)
- Con_Printf("W_LoadTextureWadFile: couldn't find %s\n", filename);
+ Con_Printf(CON_ERROR "W_LoadTextureWadFile: couldn't find %s\n", filename);
return;
}
if (FS_Read(file, &header, sizeof(wadinfo_t)) != sizeof(wadinfo_t))
- {Con_Print("W_LoadTextureWadFile: unable to read wad header\n");FS_Close(file);file = NULL;return;}
+ {Con_Print(CON_ERROR "W_LoadTextureWadFile: unable to read wad header\n");FS_Close(file);file = NULL;return;}
if(memcmp(header.identification, "WAD3", 4))
- {Con_Printf("W_LoadTextureWadFile: Wad file %s doesn't have WAD3 id\n",filename);FS_Close(file);file = NULL;return;}
+ {Con_Printf(CON_ERROR "W_LoadTextureWadFile: Wad file %s doesn't have WAD3 id\n",filename);FS_Close(file);file = NULL;return;}
numlumps = LittleLong(header.numlumps);
if (numlumps < 1 || numlumps > 65536)
- {Con_Printf("W_LoadTextureWadFile: invalid number of lumps (%i)\n", numlumps);FS_Close(file);file = NULL;return;}
+ {Con_Printf(CON_ERROR "W_LoadTextureWadFile: invalid number of lumps (%i)\n", numlumps);FS_Close(file);file = NULL;return;}
infotableofs = LittleLong(header.infotableofs);
if (FS_Seek (file, infotableofs, SEEK_SET))
- {Con_Print("W_LoadTextureWadFile: unable to seek to lump table\n");FS_Close(file);file = NULL;return;}
+ {Con_Print(CON_ERROR "W_LoadTextureWadFile: unable to seek to lump table\n");FS_Close(file);file = NULL;return;}
if (!wad.hlwads.mempool)
Mem_ExpandableArray_NewArray(&wad.hlwads, cls.permanentmempool, sizeof(mwad_t), 16);
if (!w->lumps)
{
- Con_Print("W_LoadTextureWadFile: unable to allocate temporary memory for lump table\n");
+ Con_Print(CON_ERROR "W_LoadTextureWadFile: unable to allocate temporary memory for lump table\n");
FS_Close(w->file);
w->file = NULL;
w->numlumps = 0;
if (FS_Read(file, w->lumps, sizeof(lumpinfo_t) * w->numlumps) != (fs_offset_t)sizeof(lumpinfo_t) * numlumps)
{
- Con_Print("W_LoadTextureWadFile: unable to read lump table\n");
+ Con_Print(CON_ERROR "W_LoadTextureWadFile: unable to read lump table\n");
FS_Close(w->file);
w->file = NULL;
w->numlumps = 0;
// 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;
}
}