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 "IDeserializer.h"
21 :
22 : #include "lib/byte_order.h"
23 : #include "lib/debug.h"
24 : #include "lib/status.h"
25 : #include "lib/utf8.h"
26 : #include "simulation2/system/Component.h"
27 :
28 96 : IDeserializer::~IDeserializer()
29 : {
30 96 : }
31 :
32 212 : void IDeserializer::NumberU8(const char* name, uint8_t& out, uint8_t lower, uint8_t upper)
33 : {
34 212 : uint8_t value;
35 212 : Get(name, static_cast<std::uint8_t*>(&value), sizeof(uint8_t));
36 :
37 212 : if (!(lower <= value && value <= upper))
38 0 : throw PSERROR_Deserialize_OutOfBounds(name);
39 :
40 212 : out = value;
41 212 : }
42 :
43 0 : void IDeserializer::NumberI8(const char* name, int8_t& out, int8_t lower, int8_t upper)
44 : {
45 0 : int8_t value;
46 0 : Get(name, reinterpret_cast<std::uint8_t*>(&value), sizeof(uint8_t));
47 :
48 0 : if (!(lower <= value && value <= upper))
49 0 : throw PSERROR_Deserialize_OutOfBounds(name);
50 :
51 0 : out = value;
52 0 : }
53 :
54 0 : void IDeserializer::NumberU16(const char* name, uint16_t& out, uint16_t lower, uint16_t upper)
55 : {
56 0 : uint16_t value;
57 0 : Get(name, reinterpret_cast<std::uint8_t*>(&value), sizeof(uint16_t));
58 0 : value = to_le16(value);
59 :
60 0 : if (!(lower <= value && value <= upper))
61 0 : throw PSERROR_Deserialize_OutOfBounds(name);
62 :
63 0 : out = value;
64 0 : }
65 :
66 0 : void IDeserializer::NumberI16(const char* name, int16_t& out, int16_t lower, int16_t upper)
67 : {
68 0 : int16_t value;
69 0 : Get(name, reinterpret_cast<std::uint8_t*>(&value), sizeof(uint16_t));
70 0 : value = static_cast<std::int16_t>(to_le16(static_cast<std::uint16_t>(value)));
71 :
72 0 : if (!(lower <= value && value <= upper))
73 0 : throw PSERROR_Deserialize_OutOfBounds(name);
74 :
75 0 : out = value;
76 0 : }
77 :
78 15 : void IDeserializer::NumberU32(const char* name, uint32_t& out, uint32_t lower, uint32_t upper)
79 : {
80 15 : uint32_t value;
81 15 : Get(name, reinterpret_cast<std::uint8_t*>(&value), sizeof(uint32_t));
82 15 : value = to_le32(value);
83 :
84 15 : if (!(lower <= value && value <= upper))
85 0 : throw PSERROR_Deserialize_OutOfBounds(name);
86 :
87 15 : out = value;
88 15 : }
89 :
90 101 : void IDeserializer::NumberI32(const char* name, int32_t& out, int32_t lower, int32_t upper)
91 : {
92 101 : int32_t value;
93 101 : Get(name, reinterpret_cast<std::uint8_t*>(&value), sizeof(uint32_t));
94 101 : value = static_cast<std::int32_t>(to_le32(static_cast<std::uint32_t>(value)));
95 :
96 101 : if (!(lower <= value && value <= upper))
97 0 : throw PSERROR_Deserialize_OutOfBounds(name);
98 :
99 101 : out = value;
100 101 : }
101 :
102 357 : void IDeserializer::NumberU8_Unbounded(const char* name, uint8_t& out)
103 : {
104 357 : Get(name, reinterpret_cast<std::uint8_t*>(&out), sizeof(uint8_t));
105 357 : }
106 :
107 1 : void IDeserializer::NumberI8_Unbounded(const char* name, int8_t& out)
108 : {
109 1 : Get(name, reinterpret_cast<std::uint8_t*>(&out), sizeof(int8_t));
110 1 : }
111 :
112 1 : void IDeserializer::NumberU16_Unbounded(const char* name, uint16_t& out)
113 : {
114 1 : uint16_t value;
115 1 : Get(name, reinterpret_cast<std::uint8_t*>(&value), sizeof(uint16_t));
116 1 : out = to_le16(value);
117 1 : }
118 :
119 1 : void IDeserializer::NumberI16_Unbounded(const char* name, int16_t& out)
120 : {
121 1 : int16_t value;
122 1 : Get(name, reinterpret_cast<std::uint8_t*>(&value), sizeof(int16_t));
123 1 : out = static_cast<std::int16_t>(to_le16(static_cast<std::uint16_t>(value)));
124 1 : }
125 :
126 449 : void IDeserializer::NumberU32_Unbounded(const char* name, uint32_t& out)
127 : {
128 449 : uint32_t value;
129 449 : Get(name, reinterpret_cast<std::uint8_t*>(&value), sizeof(uint32_t));
130 449 : out = to_le32(value);
131 449 : }
132 :
133 38 : void IDeserializer::NumberI32_Unbounded(const char* name, int32_t& out)
134 : {
135 38 : int32_t value;
136 38 : Get(name, reinterpret_cast<std::uint8_t*>(&value), sizeof(int32_t));
137 38 : out = static_cast<std::int32_t>(to_le32(static_cast<std::uint32_t>(value)));
138 38 : }
139 :
140 1 : void IDeserializer::NumberFloat_Unbounded(const char* name, float& out)
141 : {
142 1 : Get(name, reinterpret_cast<std::uint8_t*>(&out), sizeof(float));
143 1 : }
144 :
145 19 : void IDeserializer::NumberDouble_Unbounded(const char* name, double& out)
146 : {
147 19 : Get(name, reinterpret_cast<std::uint8_t*>(&out), sizeof(double));
148 19 : }
149 :
150 28 : void IDeserializer::NumberFixed_Unbounded(const char* name, fixed& out)
151 : {
152 28 : int32_t n;
153 28 : NumberI32_Unbounded(name, n);
154 28 : out.SetInternalValue(n);
155 28 : }
156 :
157 206 : void IDeserializer::Bool(const char* name, bool& out)
158 : {
159 206 : uint8_t i;
160 206 : NumberU8(name, i, 0, 1);
161 206 : out = (i != 0);
162 206 : }
163 :
164 15 : void IDeserializer::StringASCII(const char* name, std::string& out, uint32_t minlength, uint32_t maxlength)
165 : {
166 15 : uint32_t len;
167 15 : NumberU32("string length", len, minlength, maxlength);
168 :
169 15 : RequireBytesInStream(len);
170 15 : out.resize(len);
171 15 : Get(name, reinterpret_cast<std::uint8_t*>(out.data()), len);
172 :
173 196 : for (size_t i = 0; i < out.length(); ++i)
174 181 : if (out[i] == 0 || (unsigned char)out[i] >= 128)
175 0 : throw PSERROR_Deserialize_InvalidCharInString();
176 15 : }
177 :
178 17 : void IDeserializer::String(const char* name, std::wstring& out, uint32_t minlength, uint32_t maxlength)
179 : {
180 17 : std::string str;
181 17 : uint32_t len;
182 17 : NumberU32_Unbounded("string length", len);
183 :
184 17 : RequireBytesInStream(len);
185 17 : str.resize(len);
186 17 : Get(name, reinterpret_cast<std::uint8_t*>(str.data()), len);
187 :
188 17 : Status err;
189 17 : out = wstring_from_utf8(str, &err);
190 17 : if (err)
191 0 : throw PSERROR_Deserialize_InvalidCharInString();
192 :
193 17 : if (!(minlength <= out.length() && out.length() <= maxlength))
194 0 : throw PSERROR_Deserialize_OutOfBounds(name);
195 17 : }
196 :
197 25 : void IDeserializer::RawBytes(const char* name, std::uint8_t* data, size_t len)
198 : {
199 25 : Get(name, data, len);
200 25 : }
201 :
202 0 : int IDeserializer::GetVersion() const
203 : {
204 0 : debug_warn(L"GetVersion() not implemented in this subclass");
205 0 : return 0;
206 : }
|