Sat, 13 Dec 2014 04:32:15 +0200
- rcon session now works! woo!
9
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
1 | /* |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
2 | Copyright 2014 Teemu Piippo |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
3 | All rights reserved. |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
4 | |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
5 | Redistribution and use in source and binary forms, with or without |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
6 | modification, are permitted provided that the following conditions |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
7 | are met: |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
8 | |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
9 | 1. Redistributions of source code must retain the above copyright |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
10 | notice, this list of conditions and the following disclaimer. |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
11 | 2. Redistributions in binary form must reproduce the above copyright |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
12 | notice, this list of conditions and the following disclaimer in the |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
13 | documentation and/or other materials provided with the distribution. |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
14 | 3. Neither the name of the copyright holder nor the names of its |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
15 | contributors may be used to endorse or promote products derived from |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
16 | this software without specific prior written permission. |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
17 | |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
19 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
20 | TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
21 | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
22 | OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
23 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
24 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
25 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
26 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
27 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
28 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
29 | */ |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
30 | |
5 | 31 | #include "bytestream.h" |
32 | #include <string.h> | |
33 | ||
34 | bool Bytestream::sink; | |
35 | ||
36 | // ------------------------------------------------------------------------------------------------- | |
37 | // | |
38 | Bytestream::Bytestream (unsigned long length) : | |
39 | m_data (nullptr) | |
40 | { | |
41 | resize (length); | |
42 | clear(); | |
43 | } | |
44 | ||
45 | // ------------------------------------------------------------------------------------------------- | |
46 | // | |
47 | Bytestream::Bytestream (const unsigned char* data, unsigned long length) : | |
48 | m_data (nullptr) | |
49 | { | |
50 | init (data, length); | |
51 | } | |
52 | ||
53 | // ------------------------------------------------------------------------------------------------- | |
54 | // | |
55 | Bytestream::Bytestream (const Vector<unsigned char>& bytes) : | |
56 | m_data (nullptr) | |
57 | { | |
58 | init (bytes.data(), bytes.size()); | |
59 | } | |
60 | ||
61 | // ------------------------------------------------------------------------------------------------- | |
62 | // | |
11 | 63 | Bytestream::Bytestream (const Bytestream& other) : |
64 | m_data (nullptr) | |
65 | { | |
66 | init (other.data(), other.written_length()); | |
67 | } | |
68 | ||
69 | // ------------------------------------------------------------------------------------------------- | |
70 | METHOD Bytestream::operator= (const Bytestream& other) -> Bytestream& | |
71 | { | |
72 | init (other.data(), other.written_length()); | |
73 | return *this; | |
74 | } | |
75 | ||
76 | // ------------------------------------------------------------------------------------------------- | |
77 | // | |
5 | 78 | Bytestream::~Bytestream() |
79 | { | |
80 | delete m_data; | |
81 | } | |
82 | ||
83 | // ------------------------------------------------------------------------------------------------- | |
84 | // | |
85 | void Bytestream::resize (unsigned long newsize) | |
86 | { | |
87 | Vector<unsigned char> olddata; | |
88 | unsigned long oldsize = 0L; | |
89 | ||
90 | if (m_data != nullptr) | |
91 | { | |
92 | oldsize = allocated_size(); | |
93 | olddata.resize (oldsize); | |
94 | memcpy (olddata.data(), m_data, oldsize); | |
95 | } | |
96 | ||
97 | delete[] m_data; | |
98 | m_allocatedSize = newsize; | |
99 | m_data = new unsigned char[newsize]; | |
100 | ||
101 | if (olddata > 0L) | |
102 | memcpy (m_data, olddata, min (oldsize, newsize)); | |
103 | } | |
104 | ||
105 | // ------------------------------------------------------------------------------------------------- | |
106 | // | |
107 | void Bytestream::init (const unsigned char* data, unsigned long length) | |
108 | { | |
109 | resize (length); | |
110 | memcpy (m_data, data, length); | |
111 | m_cursor = &m_data[0]; | |
112 | m_writtenLength = length; | |
113 | } | |
114 | ||
115 | // ------------------------------------------------------------------------------------------------- | |
116 | // | |
117 | void Bytestream::clear() | |
118 | { | |
119 | m_cursor = &m_data[0]; | |
120 | m_writtenLength = 0; | |
121 | } | |
122 | ||
123 | // ------------------------------------------------------------------------------------------------- | |
124 | // | |
125 | char Bytestream::read_byte (bool* ok) | |
126 | { | |
127 | *ok = bytes_left() > 0; | |
128 | return *ok ? *m_cursor++ : -1; | |
129 | } | |
130 | ||
131 | // ------------------------------------------------------------------------------------------------- | |
132 | // | |
133 | short int Bytestream::read_short (bool* ok) | |
134 | { | |
135 | if (bytes_left() < 2) | |
136 | { | |
137 | *ok = false; | |
138 | return false; | |
139 | } | |
140 | ||
10
3874575d924d
- begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
141 | short int result = 0; |
5 | 142 | |
143 | for (int i = 0; i < 2; ++i) | |
10
3874575d924d
- begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
144 | result |= m_cursor[i] << (i * 8); |
5 | 145 | |
10
3874575d924d
- begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
146 | m_cursor += 2; |
3874575d924d
- begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
147 | return result; |
5 | 148 | } |
149 | ||
150 | // ------------------------------------------------------------------------------------------------- | |
151 | // | |
152 | long int Bytestream::read_long (bool* ok) | |
153 | { | |
154 | if (bytes_left() < 4) | |
155 | { | |
156 | *ok = false; | |
157 | return -1; | |
158 | } | |
159 | ||
10
3874575d924d
- begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
160 | long int result = 0; |
5 | 161 | |
162 | for (int i = 0; i < 4; ++i) | |
10
3874575d924d
- begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
163 | result |= m_cursor[i] << (i * 8); |
5 | 164 | |
10
3874575d924d
- begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
165 | m_cursor += 4; |
3874575d924d
- begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
166 | return result; |
5 | 167 | } |
168 | ||
169 | // ------------------------------------------------------------------------------------------------- | |
170 | // | |
171 | float Bytestream::read_float (bool* ok) | |
172 | { | |
173 | int value = read_long (ok); | |
174 | ||
175 | if (*ok == false) | |
176 | return -1.0f; | |
177 | ||
178 | return reinterpret_cast<float&> (value); | |
179 | } | |
180 | ||
181 | // ------------------------------------------------------------------------------------------------- | |
182 | // | |
183 | String Bytestream::read_string (bool* ok) | |
184 | { | |
185 | // Zandronum sends strings of maximum 2048 characters, though it only | |
186 | // reads 2047-character long ones so I guess we can follow up and do | |
187 | // the same :-) | |
188 | static char buffer[MAX_NETWORK_STRING]; | |
189 | unsigned char* stringEnd; | |
190 | unsigned char* stringBegin = m_cursor; | |
191 | unsigned char* end = m_data + allocated_size(); | |
192 | ||
193 | // where's the end of the string? | |
194 | for (stringEnd = m_cursor; *stringEnd != '\0'; ++stringEnd) | |
195 | { | |
196 | if (stringEnd == end) | |
197 | { | |
198 | // past the end of the buffer! Argh! | |
199 | *ok = false; | |
200 | return ""; | |
201 | } | |
202 | } | |
203 | ||
204 | m_cursor = stringEnd + 1; | |
205 | unsigned int length = stringEnd - m_cursor; | |
206 | ||
207 | // ensure we won't write past the buffer (note: we still moved | |
208 | // past the excess bytes in the above statement, those are ignored) | |
209 | if (length >= MAX_NETWORK_STRING) | |
210 | length = MAX_NETWORK_STRING - 1; | |
211 | ||
212 | memcpy (buffer, stringBegin, length); | |
213 | buffer[length] = '\0'; | |
214 | return String (buffer); | |
215 | } | |
216 | ||
217 | // ------------------------------------------------------------------------------------------------- | |
218 | // | |
219 | METHOD | |
220 | Bytestream::read (unsigned char* buffer, unsigned long length, bool* ok) -> void | |
221 | { | |
222 | if (bytes_left() < length) | |
223 | { | |
224 | *ok = false; | |
225 | return; | |
226 | } | |
227 | ||
228 | memcpy (buffer, m_cursor, length); | |
229 | m_cursor += length; | |
230 | } | |
231 | ||
232 | // ------------------------------------------------------------------------------------------------- | |
233 | // | |
234 | void Bytestream::write (unsigned char val) | |
235 | { | |
236 | *m_cursor++ = val; | |
237 | m_writtenLength++; | |
238 | } | |
239 | ||
240 | // ------------------------------------------------------------------------------------------------- | |
241 | // | |
242 | void Bytestream::write (const unsigned char* val, unsigned int length) | |
243 | { | |
244 | grow_to_fit (length); | |
245 | memcpy (m_cursor, val, length); | |
246 | m_cursor += length; | |
247 | m_writtenLength += length; | |
248 | } | |
249 | ||
250 | // ------------------------------------------------------------------------------------------------- | |
251 | // | |
252 | void Bytestream::grow_to_fit (unsigned long bytes) | |
253 | { | |
254 | if (space_left() < bytes) | |
255 | resize (allocated_size() + bytes + 128); | |
256 | } | |
257 | ||
258 | // ------------------------------------------------------------------------------------------------- | |
259 | // | |
260 | void Bytestream::write_byte (char val) | |
261 | { | |
262 | grow_to_fit (1); | |
263 | write (val); | |
264 | } | |
265 | ||
266 | // ------------------------------------------------------------------------------------------------- | |
267 | // | |
268 | void Bytestream::write_short (short int val) | |
269 | { | |
270 | grow_to_fit (2); | |
271 | ||
272 | for (int i = 0; i < 2; ++i) | |
273 | write ((val >> (i * 8)) & 0xFF); | |
274 | } | |
275 | ||
276 | // ------------------------------------------------------------------------------------------------- | |
277 | // | |
278 | void Bytestream::write_long (long int val) | |
279 | { | |
280 | grow_to_fit (4); | |
281 | ||
282 | for (int i = 0; i < 4; ++i) | |
283 | write ((val >> (i * 8)) & 0xFF); | |
284 | } | |
285 | ||
286 | // ------------------------------------------------------------------------------------------------- | |
287 | // | |
288 | void Bytestream::write_float (float val) | |
289 | { | |
290 | // I know this is probably dangerous but this is what Zandronum does so yeah | |
291 | write_long (reinterpret_cast<int&> (val)); | |
292 | } | |
293 | ||
294 | // ------------------------------------------------------------------------------------------------- | |
295 | // | |
296 | void Bytestream::write_string (const String& val) | |
297 | { | |
298 | grow_to_fit (val.length() + 1); | |
299 | write (reinterpret_cast<const unsigned char*> (val.chars()), val.length()); | |
300 | write (0); | |
301 | } | |
302 | ||
303 | // ------------------------------------------------------------------------------------------------- | |
304 | // | |
305 | void Bytestream::write_buffer (const Bytestream& other) | |
306 | { | |
307 | write (other.data(), other.written_length()); | |
308 | } |