Wed, 27 May 2015 21:15:52 +0300
Merged with default
9
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
1 | /* |
73
07dda51a7a8e
Update license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
61
diff
changeset
|
2 | Copyright 2014, 2015 Teemu Piippo |
9
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 | // ------------------------------------------------------------------------------------------------- | |
35 | // | |
36 | Bytestream::Bytestream (unsigned long length) : | |
37 | m_data (nullptr) | |
38 | { | |
39 | resize (length); | |
40 | clear(); | |
41 | } | |
42 | ||
43 | // ------------------------------------------------------------------------------------------------- | |
44 | // | |
45 | Bytestream::Bytestream (const unsigned char* data, unsigned long length) : | |
46 | m_data (nullptr) | |
47 | { | |
48 | init (data, length); | |
49 | } | |
50 | ||
51 | // ------------------------------------------------------------------------------------------------- | |
52 | // | |
53 | Bytestream::Bytestream (const Vector<unsigned char>& bytes) : | |
54 | m_data (nullptr) | |
55 | { | |
56 | init (bytes.data(), bytes.size()); | |
57 | } | |
58 | ||
59 | // ------------------------------------------------------------------------------------------------- | |
60 | // | |
11 | 61 | Bytestream::Bytestream (const Bytestream& other) : |
62 | m_data (nullptr) | |
63 | { | |
64 | init (other.data(), other.written_length()); | |
65 | } | |
66 | ||
67 | // ------------------------------------------------------------------------------------------------- | |
18
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
68 | // |
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
69 | Bytestream::~Bytestream() |
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
70 | { |
58
d175243ad169
- rcon sessions are no longer allocated on the heap
Teemu Piippo <crimsondusk64@gmail.com>
parents:
18
diff
changeset
|
71 | delete[] m_data; |
18
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
72 | } |
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
73 | |
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
74 | // ------------------------------------------------------------------------------------------------- |
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
75 | METHOD |
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
76 | Bytestream::operator= (const Bytestream& other) -> Bytestream& |
11 | 77 | { |
78 | init (other.data(), other.written_length()); | |
79 | return *this; | |
80 | } | |
81 | ||
82 | // ------------------------------------------------------------------------------------------------- | |
83 | // | |
18
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
84 | METHOD |
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
85 | Bytestream::resize (unsigned long newsize) -> void |
5 | 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 | // | |
18
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
107 | METHOD |
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
108 | Bytestream::init (const unsigned char* data, unsigned long length) -> void |
5 | 109 | { |
110 | resize (length); | |
111 | memcpy (m_data, data, length); | |
112 | m_cursor = &m_data[0]; | |
113 | m_writtenLength = length; | |
114 | } | |
115 | ||
116 | // ------------------------------------------------------------------------------------------------- | |
117 | // | |
18
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
118 | METHOD |
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
119 | Bytestream::clear() -> void |
5 | 120 | { |
121 | m_cursor = &m_data[0]; | |
122 | m_writtenLength = 0; | |
123 | } | |
124 | ||
125 | // ------------------------------------------------------------------------------------------------- | |
126 | // | |
13
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
127 | METHOD |
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
128 | Bytestream::ensure_read_space (unsigned int bytes) -> void |
5 | 129 | { |
13
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
130 | if (bytes_left() < bytes) |
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
131 | { |
83
08bfc3d9d2ae
Removed format.cpp and format.h, use built-in formatting instead
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
132 | int bytesPast = bytes - bytes_left(); |
08bfc3d9d2ae
Removed format.cpp and format.h, use built-in formatting instead
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
133 | |
08bfc3d9d2ae
Removed format.cpp and format.h, use built-in formatting instead
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
134 | String message; |
08bfc3d9d2ae
Removed format.cpp and format.h, use built-in formatting instead
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
135 | message.sprintf ("attempted to read %d byte%s past the end of bytestream", |
08bfc3d9d2ae
Removed format.cpp and format.h, use built-in formatting instead
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
136 | bytesPast, bytesPast != -1 ? "s" : ""); |
08bfc3d9d2ae
Removed format.cpp and format.h, use built-in formatting instead
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
137 | throw IOError (message); |
13
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
138 | } |
5 | 139 | } |
140 | ||
141 | // ------------------------------------------------------------------------------------------------- | |
142 | // | |
18
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
143 | METHOD |
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
144 | Bytestream::read_byte() -> char |
5 | 145 | { |
13
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
146 | ensure_read_space (1); |
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
147 | return *m_cursor++; |
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
148 | } |
5 | 149 | |
13
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
150 | // ------------------------------------------------------------------------------------------------- |
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
151 | // |
18
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
152 | METHOD |
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
153 | Bytestream::read_short() -> short int |
13
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
154 | { |
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
155 | ensure_read_space (2); |
10
3874575d924d
- begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
156 | short int result = 0; |
5 | 157 | |
158 | for (int i = 0; i < 2; ++i) | |
10
3874575d924d
- begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
159 | result |= m_cursor[i] << (i * 8); |
5 | 160 | |
10
3874575d924d
- begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
161 | m_cursor += 2; |
3874575d924d
- begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
162 | return result; |
5 | 163 | } |
164 | ||
165 | // ------------------------------------------------------------------------------------------------- | |
166 | // | |
18
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
167 | METHOD |
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
168 | Bytestream::read_long() -> long int |
5 | 169 | { |
13
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
170 | ensure_read_space (4); |
10
3874575d924d
- begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
171 | long int result = 0; |
5 | 172 | |
173 | for (int i = 0; i < 4; ++i) | |
10
3874575d924d
- begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
174 | result |= m_cursor[i] << (i * 8); |
5 | 175 | |
10
3874575d924d
- begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
176 | m_cursor += 4; |
3874575d924d
- begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
177 | return result; |
5 | 178 | } |
179 | ||
180 | // ------------------------------------------------------------------------------------------------- | |
181 | // | |
18
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
182 | METHOD |
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
183 | Bytestream::read_float() -> float |
5 | 184 | { |
61
cdf3c8af1545
- changed write_float/read_float to use memcpy
Teemu Piippo <crimsondusk64@gmail.com>
parents:
58
diff
changeset
|
185 | float value; |
cdf3c8af1545
- changed write_float/read_float to use memcpy
Teemu Piippo <crimsondusk64@gmail.com>
parents:
58
diff
changeset
|
186 | int intvalue = read_long(); |
cdf3c8af1545
- changed write_float/read_float to use memcpy
Teemu Piippo <crimsondusk64@gmail.com>
parents:
58
diff
changeset
|
187 | memcpy (&value, &intvalue, sizeof intvalue); |
cdf3c8af1545
- changed write_float/read_float to use memcpy
Teemu Piippo <crimsondusk64@gmail.com>
parents:
58
diff
changeset
|
188 | return value; |
5 | 189 | } |
190 | ||
191 | // ------------------------------------------------------------------------------------------------- | |
192 | // | |
18
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
193 | METHOD |
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
194 | Bytestream::read_string() -> String |
5 | 195 | { |
196 | // Zandronum sends strings of maximum 2048 characters, though it only | |
197 | // reads 2047-character long ones so I guess we can follow up and do | |
198 | // the same :-) | |
199 | static char buffer[MAX_NETWORK_STRING]; | |
200 | unsigned char* stringEnd; | |
201 | unsigned char* stringBegin = m_cursor; | |
202 | unsigned char* end = m_data + allocated_size(); | |
203 | ||
204 | // where's the end of the string? | |
205 | for (stringEnd = m_cursor; *stringEnd != '\0'; ++stringEnd) | |
206 | { | |
207 | if (stringEnd == end) | |
208 | // past the end of the buffer! Argh! | |
13
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
209 | throw IOError ("unterminated string in packet"); |
5 | 210 | } |
211 | ||
212 | m_cursor = stringEnd + 1; | |
213 | unsigned int length = stringEnd - m_cursor; | |
214 | ||
215 | // ensure we won't write past the buffer (note: we still moved | |
216 | // past the excess bytes in the above statement, those are ignored) | |
217 | if (length >= MAX_NETWORK_STRING) | |
218 | length = MAX_NETWORK_STRING - 1; | |
219 | ||
220 | memcpy (buffer, stringBegin, length); | |
221 | buffer[length] = '\0'; | |
222 | return String (buffer); | |
223 | } | |
224 | ||
225 | // ------------------------------------------------------------------------------------------------- | |
226 | // | |
227 | METHOD | |
13
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
228 | Bytestream::read (unsigned char* buffer, unsigned long length) -> void |
5 | 229 | { |
13
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
230 | ensure_read_space (length); |
5 | 231 | memcpy (buffer, m_cursor, length); |
232 | m_cursor += length; | |
233 | } | |
234 | ||
235 | // ------------------------------------------------------------------------------------------------- | |
236 | // | |
18
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
237 | METHOD |
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
238 | Bytestream::write (unsigned char val) -> void |
5 | 239 | { |
240 | *m_cursor++ = val; | |
241 | m_writtenLength++; | |
242 | } | |
243 | ||
244 | // ------------------------------------------------------------------------------------------------- | |
245 | // | |
18
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
246 | METHOD |
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
247 | Bytestream::write (const unsigned char* val, unsigned int length) -> void |
5 | 248 | { |
249 | grow_to_fit (length); | |
250 | memcpy (m_cursor, val, length); | |
251 | m_cursor += length; | |
252 | m_writtenLength += length; | |
253 | } | |
254 | ||
255 | // ------------------------------------------------------------------------------------------------- | |
256 | // | |
18
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
257 | METHOD |
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
258 | Bytestream::grow_to_fit (unsigned long bytes) -> void |
5 | 259 | { |
260 | if (space_left() < bytes) | |
261 | resize (allocated_size() + bytes + 128); | |
262 | } | |
263 | ||
264 | // ------------------------------------------------------------------------------------------------- | |
265 | // | |
18
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
266 | METHOD |
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
267 | Bytestream::write_byte (char val) -> void |
5 | 268 | { |
269 | grow_to_fit (1); | |
270 | write (val); | |
271 | } | |
272 | ||
273 | // ------------------------------------------------------------------------------------------------- | |
274 | // | |
18
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
275 | METHOD |
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
276 | Bytestream::write_short (short int val) -> void |
5 | 277 | { |
278 | grow_to_fit (2); | |
279 | ||
280 | for (int i = 0; i < 2; ++i) | |
281 | write ((val >> (i * 8)) & 0xFF); | |
282 | } | |
283 | ||
284 | // ------------------------------------------------------------------------------------------------- | |
285 | // | |
18
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
286 | METHOD |
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
287 | Bytestream::write_long (long int val) -> void |
5 | 288 | { |
289 | grow_to_fit (4); | |
290 | ||
291 | for (int i = 0; i < 4; ++i) | |
292 | write ((val >> (i * 8)) & 0xFF); | |
293 | } | |
294 | ||
295 | // ------------------------------------------------------------------------------------------------- | |
296 | // | |
18
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
297 | METHOD |
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
298 | Bytestream::write_float (float val) -> void |
5 | 299 | { |
300 | // I know this is probably dangerous but this is what Zandronum does so yeah | |
61
cdf3c8af1545
- changed write_float/read_float to use memcpy
Teemu Piippo <crimsondusk64@gmail.com>
parents:
58
diff
changeset
|
301 | int intvalue; |
cdf3c8af1545
- changed write_float/read_float to use memcpy
Teemu Piippo <crimsondusk64@gmail.com>
parents:
58
diff
changeset
|
302 | memcpy (&intvalue, &val, sizeof val); |
cdf3c8af1545
- changed write_float/read_float to use memcpy
Teemu Piippo <crimsondusk64@gmail.com>
parents:
58
diff
changeset
|
303 | write_long (intvalue); |
5 | 304 | } |
305 | ||
306 | // ------------------------------------------------------------------------------------------------- | |
307 | // | |
18
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
308 | METHOD |
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
309 | Bytestream::write_string (const String& val) -> void |
5 | 310 | { |
311 | grow_to_fit (val.length() + 1); | |
312 | write (reinterpret_cast<const unsigned char*> (val.chars()), val.length()); | |
313 | write (0); | |
314 | } | |
315 | ||
316 | // ------------------------------------------------------------------------------------------------- | |
317 | // | |
18
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
318 | METHOD |
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
319 | Bytestream::write_buffer (const Bytestream& other) -> void |
5 | 320 | { |
321 | write (other.data(), other.written_length()); | |
322 | } |