Debian Codegraph @

prboom2/src/wadtbl.c

raw
1 /* Emacs style mode select -*- C -*-
2 *-----------------------------------------------------------------------------
3 *
4 *
5 * PrBoom: a Doom port merged with LxDoom and LSDLDoom
6 * based on BOOM, a modified and improved DOOM engine
7 * Copyright (C) 1999 by
8 * id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
9 * Copyright (C) 1999-2000 by
10 * Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze, Andrey Budko
11 * Copyright 2005, 2006 by
12 * Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 * 02111-1307, USA.
28 *
29 * DESCRIPTION:
30 * Ex Demo Wad Table
31 *
32 *---------------------------------------------------------------------
33 */
34
35 #include <string.h>
36
37 #include "lprintf.h"
38 #include "m_swap.h"
39 #include "z_zone.h"
40
41 #include "wadtbl.h"
42
43 void InitPWADTable(wadtbl_t *wadtbl)
44 {
45 //init header signature and lookup table offset and size
46 memcpy(wadtbl->header.identification, PWAD_SIGNATURE, 4);
47 wadtbl->header.infotableofs = sizeof(wadtbl->header);
48 wadtbl->header.numlumps = 0;
49
50 //clear PWAD lookup table
51 wadtbl->lumps = NULL;
52
53 //clear PWAD data
54 wadtbl->data = NULL;
55 wadtbl->datasize = 0;
56 }
57
58 void FreePWADTable(wadtbl_t *wadtbl)
59 {
60 //clear PWAD lookup table
61 Z_Free(wadtbl->lumps);
62
63 //clear PWAD data
64 Z_Free(wadtbl->data);
65 }
66
67 void AddPWADTableLump(wadtbl_t *wadtbl, const char *name, const byte* data, size_t size)
68 {
69 int lumpnum;
70
71 if (!wadtbl || (name && strlen(name) > 8))
72 {
73 I_Error("W_AddLump: wrong parameters.");
74 return;
75 }
76
77 lumpnum = wadtbl->header.numlumps;
78
79 if (name)
80 {
81 wadtbl->lumps = Z_Realloc(wadtbl->lumps, (lumpnum + 1) * sizeof(wadtbl->lumps[0]));
82
83 memcpy(wadtbl->lumps[lumpnum].name, name, 8);
84 wadtbl->lumps[lumpnum].size = size;
85 wadtbl->lumps[lumpnum].filepos = wadtbl->header.infotableofs;
86
87 wadtbl->header.numlumps++;
88 }
89
90 if (data && size > 0)
91 {
92 wadtbl->data = Z_Realloc(wadtbl->data, wadtbl->datasize + size);
93
94 memcpy(wadtbl->data + wadtbl->datasize, data, size);
95 wadtbl->datasize += size;
96
97 wadtbl->header.infotableofs += size;
98 }
99 }
100
101 wadinfo_t *ReadPWADTable(byte *buffer, size_t size)
102 {
103 int i;
104 unsigned int length;
105 wadinfo_t *header;
106 const filelump_t *fileinfo;
107
108 if (buffer && size > sizeof(*header))
109 {
110 header = (wadinfo_t*)buffer;
111 if (strncmp(header->identification, "IWAD", 4) == 0 ||
112 strncmp(header->identification, "PWAD", 4) == 0)
113 {
114 header->numlumps = LittleLong(header->numlumps);
115 header->infotableofs = LittleLong(header->infotableofs);
116 length = header->numlumps * sizeof(filelump_t);
117
118 if (header->infotableofs + length <= size)
119 {
120 fileinfo = (const filelump_t*)(buffer + header->infotableofs);
121 for (i = 0; i < header->numlumps; i++, fileinfo++)
122 {
123 if (fileinfo->filepos < 0 ||
124 fileinfo->filepos > header->infotableofs ||
125 fileinfo->filepos + fileinfo->size > header->infotableofs)
126 {
127 break;
128 }
129 }
130 if (i == header->numlumps)
131 return header;
132 }
133 }
134 }
135
136 return NULL;
137 }