X-Git-Url: http://git.xonotic.org/?p=xonotic%2Fdarkplaces.git;a=blobdiff_plain;f=wad.c;h=55da49963bd731d96f612fa426a0173e5c7f2cbf;hp=25dd95cf53bc0c30b22904b705193a7afc8c3bbd;hb=4d33dcb245a32cf4c44807e8b53e4fccea944633;hpb=32432e964a2096d912e7500738d9e1bf1d7925db diff --git a/wad.c b/wad.c index 25dd95cf..55da4996 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) @@ -111,7 +111,7 @@ void W_UnloadAll(void) 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; @@ -146,8 +146,14 @@ 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)) + { + if (returnfilesize) + *returnfilesize = lump->size; return (wad.gfx_base + lump->filepos); + } + } return NULL; } @@ -164,37 +170,37 @@ 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) - 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); - 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) { - 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; @@ -203,7 +209,7 @@ void W_LoadTextureWadFile (char *filename, int complain) 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; @@ -217,23 +223,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 { @@ -250,8 +276,8 @@ 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]; @@ -274,18 +300,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; } }