File: XeroXMB.cpp

package info (click to toggle)
0ad 0.0.23.1-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 78,412 kB
  • sloc: cpp: 245,162; ansic: 200,249; javascript: 19,244; python: 13,754; sh: 6,104; perl: 4,620; makefile: 977; xml: 810; java: 533; ruby: 229; erlang: 46; pascal: 30; sql: 21; tcl: 4
file content (342 lines) | stat: -rw-r--r-- 8,501 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* Copyright (C) 2015 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * 0 A.D. is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "precompiled.h"

#include "Xeromyces.h"

#include "lib/byte_order.h"	// FOURCC_LE

// external linkage (also used by Xeromyces.cpp)
const char* HeaderMagicStr = "XMB0";
const char* UnfinishedHeaderMagicStr = "XMBu";
// Arbitrary version number - change this if we update the code and
// need to invalidate old users' caches
const u32 XMBVersion = 3;

template<typename T>
static inline T read(const void* ptr)
{
	T ret;
	memcpy(&ret, ptr, sizeof(T));
	return ret;
}

bool XMBFile::Initialise(const char* FileData)
{
	m_Pointer = FileData;
	char Header[5] = { 0 };
	strncpy_s(Header, 5, m_Pointer, 4);
	m_Pointer += 4;
	// (c.f. @return documentation of this function)
	if(!strcmp(Header, UnfinishedHeaderMagicStr))
		return false;
	ENSURE(!strcmp(Header, HeaderMagicStr) && "Invalid XMB header!");

	u32 Version = read<u32>(m_Pointer);
	m_Pointer += 4;
	if (Version != XMBVersion)
		return false;

	int i;

	// FIXME Check that m_Pointer doesn't end up past the end of the buffer
	// (it shouldn't be all that dangerous since we're only doing read-only
	// access, but it might crash on an invalid file, reading a couple of
	// billion random element names from RAM)

#ifdef XERO_USEMAP
	// Build a std::map of all the names->ids
	u32 ElementNameCount = read<u32>(m_Pointer); m_Pointer += 4;
	for (i = 0; i < ElementNameCount; ++i)
		m_ElementNames[ReadZStr8()] = i;

	u32 AttributeNameCount = read<u32>(m_Pointer); m_Pointer += 4;
	for (i = 0; i < AttributeNameCount; ++i)
		m_AttributeNames[ReadZStr8()] = i;
#else
	// Ignore all the names for now, and skip over them
	// (remembering the position of the first)
	m_ElementNameCount = read<int>(m_Pointer); m_Pointer += 4;
	m_ElementPointer = m_Pointer;
	for (i = 0; i < m_ElementNameCount; ++i)
		m_Pointer += 4 + read<int>(m_Pointer); // skip over the string

	m_AttributeNameCount = read<int>(m_Pointer); m_Pointer += 4;
	m_AttributePointer = m_Pointer;
	for (i = 0; i < m_AttributeNameCount; ++i)
		m_Pointer += 4 + read<int>(m_Pointer); // skip over the string
#endif

	return true;	// success
}

std::string XMBFile::ReadZStr8()
{
	int Length = read<int>(m_Pointer);
	m_Pointer += 4;
	std::string String (m_Pointer); // reads up until the first NULL
	m_Pointer += Length;
	return String;
}

XMBElement XMBFile::GetRoot() const
{
	return XMBElement(m_Pointer);
}


#ifdef XERO_USEMAP

int XMBFile::GetElementID(const char* Name) const
{
	return m_ElementNames[Name];
}

int XMBFile::GetAttributeID(const char* Name) const
{
	return m_AttributeNames[Name];
}

#else // #ifdef XERO_USEMAP

int XMBFile::GetElementID(const char* Name) const
{
	const char* Pos = m_ElementPointer;

	int len = (int)strlen(Name)+1; // count bytes, including null terminator

	// Loop through each string to find a match
	for (int i = 0; i < m_ElementNameCount; ++i)
	{
		// See if this could be the right string, checking its
		// length and then its contents
		if (read<int>(Pos) == len && strncasecmp(Pos+4, Name, len) == 0)
			return i;
		// If not, jump to the next string
		Pos += 4 + read<int>(Pos);
	}
	// Failed
	return -1;
}

int XMBFile::GetAttributeID(const char* Name) const
{
	const char* Pos = m_AttributePointer;

	int len = (int)strlen(Name)+1; // count bytes, including null terminator

	// Loop through each string to find a match
	for (int i = 0; i < m_AttributeNameCount; ++i)
	{
		// See if this could be the right string, checking its
		// length and then its contents
		if (read<int>(Pos) == len && strncasecmp(Pos+4, Name, len) == 0)
			return i;
		// If not, jump to the next string
		Pos += 4 + read<int>(Pos);
	}
	// Failed
	return -1;
}
#endif // #ifdef XERO_USEMAP / #else


// Relatively inefficient, so only use when
// laziness overcomes the need for speed
std::string XMBFile::GetElementString(const int ID) const
{
	const char* Pos = m_ElementPointer;
	for (int i = 0; i < ID; ++i)
		Pos += 4 + read<int>(Pos);
	return std::string(Pos+4);
}

std::string XMBFile::GetAttributeString(const int ID) const
{
	const char* Pos = m_AttributePointer;
	for (int i = 0; i < ID; ++i)
		Pos += 4 + read<int>(Pos);
	return std::string(Pos+4);
}



int XMBElement::GetNodeName() const
{
	if (m_Pointer == NULL)
		return -1;

	return read<int>(m_Pointer + 4); // == ElementName
}

XMBElementList XMBElement::GetChildNodes() const
{
	if (m_Pointer == NULL)
		return XMBElementList(NULL, 0, NULL);

	return XMBElementList(
		m_Pointer + 20 + read<int>(m_Pointer + 16), // == Children[]
		read<int>(m_Pointer + 12), // == ChildCount
		m_Pointer + read<int>(m_Pointer) // == &Children[ChildCount]
	);
}

XMBAttributeList XMBElement::GetAttributes() const
{
	if (m_Pointer == NULL)
		return XMBAttributeList(NULL, 0, NULL);

	return XMBAttributeList(
		m_Pointer + 24 + read<int>(m_Pointer + 20), // == Attributes[]
		read<int>(m_Pointer + 8), // == AttributeCount
		m_Pointer + 20 + read<int>(m_Pointer + 16) // == &Attributes[AttributeCount] ( == &Children[])
	);
}

CStr8 XMBElement::GetText() const
{
	// Return empty string if there's no text
	if (m_Pointer == NULL || read<int>(m_Pointer + 20) == 0)
		return CStr8();

	return CStr8(m_Pointer + 28);
}

int XMBElement::GetLineNumber() const
{
	// Make sure there actually was some text to record the line of
	if (m_Pointer == NULL || read<int>(m_Pointer + 20) == 0)
		return -1;
	else
		return read<int>(m_Pointer + 24);
}

XMBElement XMBElementList::GetFirstNamedItem(const int ElementName) const
{
	const char* Pos = m_Pointer;

	// Maybe not the cleverest algorithm, but it should be
	// fast enough with half a dozen attributes:
	for (size_t i = 0; i < m_Size; ++i)
	{
		int Length = read<int>(Pos);
		int Name = read<int>(Pos+4);
		if (Name == ElementName)
			return XMBElement(Pos);
		Pos += Length;
	}

	// Can't find element
	return XMBElement();
}

XMBElementList::iterator& XMBElementList::iterator::operator++()
{
	m_CurPointer += read<int>(m_CurPointer);
	++m_CurItemID;
	return (*this);
}

XMBElement XMBElementList::operator[](size_t id)
{
	ENSURE(id < m_Size && "Element ID out of range");
	const char* Pos;
	size_t i;

	if (id < m_CurItemID)
	{
		Pos = m_Pointer;
		i = 0;
	}
	else
	{
		// If access is sequential, don't bother scanning
		// through all the nodes to find the next one
		Pos = m_CurPointer;
		i = m_CurItemID;
	}

	// Skip over each preceding node
	for (; i < id; ++i)
		Pos += read<int>(Pos);

	// Cache information about this node
	m_CurItemID = id;
	m_CurPointer = Pos;

	return XMBElement(Pos);
}

CStr8 XMBAttributeList::GetNamedItem(const int AttributeName) const
{
	const char* Pos = m_Pointer;

	// Maybe not the cleverest algorithm, but it should be
	// fast enough with half a dozen attributes:
	for (size_t i = 0; i < m_Size; ++i)
	{
		if (read<int>(Pos) == AttributeName)
			return CStr8(Pos+8);
		Pos += 8 + read<int>(Pos+4); // Skip over the string
	}

	// Can't find attribute
	return CStr8();
}

XMBAttribute XMBAttributeList::iterator::operator*() const
{
	return XMBAttribute(read<int>(m_CurPointer), CStr8(m_CurPointer+8));
}

XMBAttributeList::iterator& XMBAttributeList::iterator::operator++()
{
	m_CurPointer += 8 + read<int>(m_CurPointer+4); // skip ID, length, and string data
	++m_CurItemID;
	return (*this);
}

XMBAttribute XMBAttributeList::operator[](size_t id)
{
	ENSURE(id < m_Size && "Attribute ID out of range");
	const char* Pos;
	size_t i;

	if (id < m_CurItemID)
	{
		Pos = m_Pointer;
		i = 0;
	}
	else
	{
		// If access is sequential, don't bother scanning
		// through all the nodes to find the next one
		Pos = m_CurPointer;
		i = m_CurItemID;
	}

	// Skip over each preceding attribute
	for (; i < id; ++i)
		Pos += 8 + read<int>(Pos+4); // skip ID, length, and string data

	// Cache information about this attribute
	m_CurItemID = id;
	m_CurPointer = Pos;

	return XMBAttribute(read<int>(Pos), CStr8(Pos+8));
}