Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "precompiled.h"
19 :
20 : #include "XMBData.h"
21 :
22 : #include "lib/debug.h"
23 : #include "lib/posix/posix.h"
24 : #include "lib/secure_crt.h"
25 : #include "ps/XMB/XMBStorage.h"
26 :
27 : #include <cstring>
28 : #include <memory>
29 :
30 : template<typename T>
31 10351030 : static inline T read(const void* ptr)
32 : {
33 : T ret;
34 8009800 : memcpy(&ret, ptr, sizeof(T));
35 : return ret;
36 : }
37 :
38 14636 : bool XMBData::Initialise(const XMBStorage& doc)
39 : {
40 14636 : const char* start = reinterpret_cast<const char*>(doc.m_Buffer.get());
41 14636 : m_Pointer = start;
42 14636 : char Header[5] = { 0 };
43 14636 : strncpy_s(Header, 5, m_Pointer, 4);
44 14636 : m_Pointer += 4;
45 :
46 14636 : if (strcmp(Header, XMBStorage::UnfinishedHeaderMagicStr) == 0)
47 : return false;
48 14636 : ENSURE(strcmp(Header, XMBStorage::HeaderMagicStr) == 0 && "Invalid XMB header!");
49 :
50 14636 : std::uint32_t Version = read<std::uint32_t>(m_Pointer);
51 14636 : m_Pointer += 4;
52 14636 : if (Version != XMBStorage::XMBVersion)
53 : return false;
54 :
55 : // FIXME Check that m_Pointer doesn't end up past the end of the buffer
56 : // (it shouldn't be all that dangerous since we're only doing read-only
57 : // access, but it might crash on an invalid file, reading a couple of
58 : // billion random element names from RAM)
59 :
60 14636 : m_ElementPointer = start + read<std::uint32_t>(m_Pointer); m_Pointer += 4;
61 14636 : m_ElementNameCount = read<int>(m_Pointer); m_Pointer += 4;
62 14636 : m_AttributePointer = start + read<std::uint32_t>(m_Pointer); m_Pointer += 4;
63 14636 : m_AttributeNameCount = read<int>(m_Pointer); m_Pointer += 4;
64 : // At this point m_Pointer points to the element start, as expected.
65 14636 : return true; // success
66 : }
67 :
68 23113 : XMBElement XMBData::GetRoot() const
69 : {
70 23113 : return XMBElement(m_Pointer);
71 : }
72 :
73 275 : int XMBData::GetElementID(const char* Name) const
74 : {
75 275 : const char* Pos = m_ElementPointer;
76 :
77 275 : int len = (int)strlen(Name)+1; // count bytes, including null terminator
78 :
79 : // Loop through each string to find a match
80 3503 : for (int i = 0; i < m_ElementNameCount; ++i)
81 : {
82 : // See if this could be the right string, checking its
83 : // length and then its contents
84 3419 : if (read<int>(Pos) == len && strncasecmp(Pos+4, Name, len) == 0)
85 191 : return static_cast<int>(Pos - m_ElementPointer);
86 : // If not, jump to the next string
87 3228 : Pos += 4 + read<int>(Pos);
88 : }
89 : // Failed
90 : return -1;
91 : }
92 :
93 2662476 : int XMBData::GetAttributeID(const char* Name) const
94 : {
95 2662476 : const char* Pos = m_AttributePointer;
96 :
97 2662476 : int len = (int)strlen(Name)+1; // count bytes, including null terminator
98 :
99 : // Loop through each string to find a match
100 9184363 : for (int i = 0; i < m_AttributeNameCount; ++i)
101 : {
102 : // See if this could be the right string, checking its
103 : // length and then its contents
104 6987348 : if (read<int>(Pos) == len && strncasecmp(Pos+4, Name, len) == 0)
105 465461 : return static_cast<int>(Pos - m_AttributePointer);
106 : // If not, jump to the next string
107 6521887 : Pos += 4 + read<int>(Pos);
108 : }
109 : // Failed
110 : return -1;
111 : }
112 :
113 442390 : const char* XMBData::GetElementString(const int ID) const
114 : {
115 442390 : return reinterpret_cast<const char*>(m_ElementPointer + ID + 4);
116 : }
117 :
118 38085 : const char* XMBData::GetAttributeString(const int ID) const
119 : {
120 38085 : return reinterpret_cast<const char*>(m_AttributePointer + ID + 4);
121 : }
122 :
123 31 : std::string_view XMBData::GetElementStringView(const int ID) const
124 : {
125 31 : return std::string_view(reinterpret_cast<const char*>(m_ElementPointer + ID + 4), read<int>(m_ElementPointer + ID) - 1);
126 : }
127 :
128 10 : std::string_view XMBData::GetAttributeStringView(const int ID) const
129 : {
130 10 : return std::string_view(reinterpret_cast<const char*>(m_AttributePointer + ID + 4), read<int>(m_AttributePointer + ID) - 1);
131 : }
132 :
133 442615 : int XMBElement::GetNodeName() const
134 : {
135 442615 : if (m_Pointer == NULL)
136 : return -1;
137 :
138 442612 : return read<int>(m_Pointer + 4); // == ElementName
139 : }
140 :
141 440770 : XMBElementList XMBElement::GetChildNodes() const
142 : {
143 440770 : if (m_Pointer == NULL)
144 3 : return XMBElementList(NULL, 0, NULL);
145 :
146 440767 : return XMBElementList(
147 440767 : m_Pointer + 20 + read<int>(m_Pointer + 16), // == Children[]
148 440767 : read<int>(m_Pointer + 12), // == ChildCount
149 440767 : m_Pointer + read<int>(m_Pointer) // == &Children[ChildCount]
150 440767 : );
151 : }
152 :
153 1332430 : XMBAttributeList XMBElement::GetAttributes() const
154 : {
155 1332430 : if (m_Pointer == NULL)
156 3 : return XMBAttributeList(NULL, 0, NULL);
157 :
158 1332427 : return XMBAttributeList(
159 1332427 : m_Pointer + 24 + read<int>(m_Pointer + 20), // == Attributes[]
160 1332427 : read<int>(m_Pointer + 8), // == AttributeCount
161 1332427 : m_Pointer + 20 + read<int>(m_Pointer + 16) // == &Attributes[AttributeCount] ( == &Children[])
162 1332427 : );
163 : }
164 :
165 442367 : CStr8 XMBElement::GetText() const
166 : {
167 : // Return empty string if there's no text
168 442367 : if (m_Pointer == NULL || read<int>(m_Pointer + 20) == 0)
169 187303 : return CStr8();
170 :
171 255064 : return CStr8(m_Pointer + 28);
172 : }
173 :
174 4 : int XMBElement::GetLineNumber() const
175 : {
176 : // Make sure there actually was some text to record the line of
177 4 : if (m_Pointer == NULL || read<int>(m_Pointer + 20) == 0)
178 : return -1;
179 : else
180 2 : return read<int>(m_Pointer + 24);
181 : }
182 :
183 12 : XMBElement XMBElementList::GetFirstNamedItem(const int ElementName) const
184 : {
185 12 : const char* Pos = m_Pointer;
186 :
187 : // Maybe not the cleverest algorithm, but it should be
188 : // fast enough with half a dozen attributes:
189 42 : for (size_t i = 0; i < m_Size; ++i)
190 : {
191 39 : int Length = read<int>(Pos);
192 39 : int Name = read<int>(Pos+4);
193 39 : if (Name == ElementName)
194 9 : return XMBElement(Pos);
195 30 : Pos += Length;
196 : }
197 :
198 : // Can't find element
199 3 : return XMBElement();
200 : }
201 :
202 428198 : XMBElementList::iterator& XMBElementList::iterator::operator++()
203 : {
204 428198 : m_CurPointer += read<int>(m_CurPointer);
205 428198 : ++m_CurItemID;
206 428198 : return (*this);
207 : }
208 :
209 4 : XMBElement XMBElementList::operator[](size_t id)
210 : {
211 4 : ENSURE(id < m_Size && "Element ID out of range");
212 4 : const char* Pos;
213 4 : size_t i;
214 :
215 4 : if (id < m_CurItemID)
216 : {
217 0 : Pos = m_Pointer;
218 0 : i = 0;
219 : }
220 : else
221 : {
222 : // If access is sequential, don't bother scanning
223 : // through all the nodes to find the next one
224 4 : Pos = m_CurPointer;
225 4 : i = m_CurItemID;
226 : }
227 :
228 : // Skip over each preceding node
229 5 : for (; i < id; ++i)
230 1 : Pos += read<int>(Pos);
231 :
232 : // Cache information about this node
233 4 : m_CurItemID = id;
234 4 : m_CurPointer = Pos;
235 :
236 4 : return XMBElement(Pos);
237 : }
238 :
239 8893 : CStr8 XMBAttributeList::GetNamedItem(const int AttributeName) const
240 : {
241 8893 : const char* Pos = m_Pointer;
242 :
243 : // Maybe not the cleverest algorithm, but it should be
244 : // fast enough with half a dozen attributes:
245 9283 : for (size_t i = 0; i < m_Size; ++i)
246 : {
247 6895 : if (read<int>(Pos) == AttributeName)
248 6505 : return CStr8(Pos+8);
249 390 : Pos += 8 + read<int>(Pos+4); // Skip over the string
250 : }
251 :
252 : // Can't find attribute
253 2388 : return CStr8();
254 : }
255 :
256 127154 : XMBAttribute XMBAttributeList::iterator::operator*() const
257 : {
258 270079 : return XMBAttribute(read<int>(m_CurPointer), CStr8(m_CurPointer+8));
259 : }
260 :
261 110093 : XMBAttributeList::iterator& XMBAttributeList::iterator::operator++()
262 : {
263 110093 : m_CurPointer += 8 + read<int>(m_CurPointer+4); // skip ID, length, and string data
264 110093 : ++m_CurItemID;
265 110093 : return (*this);
266 : }
267 :
268 5 : XMBAttribute XMBAttributeList::operator[](size_t id)
269 : {
270 5 : ENSURE(id < m_Size && "Attribute ID out of range");
271 5 : const char* Pos;
272 5 : size_t i;
273 :
274 5 : if (id < m_CurItemID)
275 : {
276 0 : Pos = m_Pointer;
277 0 : i = 0;
278 : }
279 : else
280 : {
281 : // If access is sequential, don't bother scanning
282 : // through all the nodes to find the next one
283 5 : Pos = m_CurPointer;
284 5 : i = m_CurItemID;
285 : }
286 :
287 : // Skip over each preceding attribute
288 5 : for (; i < id; ++i)
289 0 : Pos += 8 + read<int>(Pos+4); // skip ID, length, and string data
290 :
291 : // Cache information about this attribute
292 5 : m_CurItemID = id;
293 5 : m_CurPointer = Pos;
294 :
295 10 : return XMBAttribute(read<int>(Pos), CStr8(Pos+8));
296 : }
|