X-Git-Url: http://de.git.xonotic.org/?a=blobdiff_plain;ds=sidebyside;f=wad.c;h=4e714b0b2712cfad3b6d9433abf5e8db5a58f929;hb=f91720d77fb7394c56a48993594ac5d93f7d0b29;hp=8e4c767bea1a0ba7a378bf44b187f241e48252c0;hpb=fd091d66e2673b19eb9c7d73d95160ef874de5e6;p=xonotic%2Fdarkplaces.git diff --git a/wad.c b/wad.c index 8e4c767b..4e714b0b 100644 --- a/wad.c +++ b/wad.c @@ -95,7 +95,7 @@ void W_UnloadAll(void) // 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) @@ -148,11 +148,6 @@ unsigned char *W_GetLumpName(const char *name) for (lump = wad.gfx.lumps, i = 0;i < wad.gfx.numlumps;i++, lump++) if (!strcmp(clean, lump->name)) return (wad.gfx_base + lump->filepos); - - if (wad.gfx_base) - Con_DPrintf("W_GetLumpByName(\"%s\"): couldn't find file in gfx.wad\n", name); - else - Con_DPrintf("W_GetLumpByName(\"%s\"): couldn't load gfx.wad\n", name); return NULL; } @@ -169,7 +164,7 @@ void W_LoadTextureWadFile (char *filename, int complain) int numlumps; mwad_t *w; - file = FS_Open (filename, "rb", false, false); + file = FS_OpenVirtualFile(filename, false); if (!file) { if (complain) @@ -192,10 +187,10 @@ void W_LoadTextureWadFile (char *filename, int complain) 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) { @@ -222,23 +217,43 @@ void W_LoadTextureWadFile (char *filename, int complain) // 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 { @@ -255,17 +270,19 @@ unsigned char *W_ConvertWAD3TextureBGRA(miptex_t *tex) 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]; + size_t range; texname[16] = 0; W_CleanupName(name, texname); if (!wad.hlwads.mempool) Mem_ExpandableArray_NewArray(&wad.hlwads, cls.permanentmempool, sizeof(mwad_t), 16); - for (k = 0;k < Mem_ExpandableArray_IndexRange(&wad.hlwads);k++) + range = Mem_ExpandableArray_IndexRange(&wad.hlwads); + for (k = 0;k < range;k++) { w = (mwad_t *)Mem_ExpandableArray_RecordAtIndex(&wad.hlwads, k); if (!w) @@ -277,18 +294,14 @@ unsigned char *W_GetTextureBGRA(char *name) 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; } }