sources/network/bytestream.cpp

Mon, 11 Jan 2016 02:42:34 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Mon, 11 Jan 2016 02:42:34 +0200
changeset 117
39e05a6ba8d7
parent 109
e4966d7e615d
child 137
485cb6d6b98c
permissions
-rw-r--r--

Improved the color handling a tad

9
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
1 /*
109
e4966d7e615d Happy new year 2016
Teemu Piippo <crimsondusk64@gmail.com>
parents: 99
diff changeset
2 Copyright 2014 - 2016 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
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 #include "bytestream.h"
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 #include <string.h>
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 83
diff changeset
33 BEGIN_ZFC_NAMESPACE
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 // -------------------------------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36 //
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 Bytestream::Bytestream (unsigned long length) :
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 m_data (nullptr)
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40 resize (length);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 clear();
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44 // -------------------------------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 //
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 Bytestream::Bytestream (const unsigned char* data, unsigned long length) :
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 m_data (nullptr)
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49 init (data, length);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 // -------------------------------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 //
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 Bytestream::Bytestream (const Vector<unsigned char>& bytes) :
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 m_data (nullptr)
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 init (bytes.data(), bytes.size());
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60 // -------------------------------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 //
11
cffa2777d917 - now able to login
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
62 Bytestream::Bytestream (const Bytestream& other) :
cffa2777d917 - now able to login
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
63 m_data (nullptr)
cffa2777d917 - now able to login
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
64 {
cffa2777d917 - now able to login
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
65 init (other.data(), other.written_length());
cffa2777d917 - now able to login
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
66 }
cffa2777d917 - now able to login
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
67
cffa2777d917 - now able to login
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
68 // -------------------------------------------------------------------------------------------------
18
56a1ac7d931b - converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents: 13
diff changeset
69 //
56a1ac7d931b - converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents: 13
diff changeset
70 Bytestream::~Bytestream()
56a1ac7d931b - converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents: 13
diff changeset
71 {
58
d175243ad169 - rcon sessions are no longer allocated on the heap
Teemu Piippo <crimsondusk64@gmail.com>
parents: 18
diff changeset
72 delete[] m_data;
18
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 // -------------------------------------------------------------------------------------------------
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 83
diff changeset
76 Bytestream& Bytestream::operator= (const Bytestream& other)
11
cffa2777d917 - now able to login
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
77 {
cffa2777d917 - now able to login
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
78 init (other.data(), other.written_length());
cffa2777d917 - now able to login
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
79 return *this;
cffa2777d917 - now able to login
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
80 }
cffa2777d917 - now able to login
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
81
cffa2777d917 - now able to login
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
82 // -------------------------------------------------------------------------------------------------
cffa2777d917 - now able to login
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
83 //
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 83
diff changeset
84 void Bytestream::resize (unsigned long newsize)
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 Vector<unsigned char> olddata;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87 unsigned long oldsize = 0L;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89 if (m_data != nullptr)
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 oldsize = allocated_size();
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 olddata.resize (oldsize);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93 memcpy (olddata.data(), m_data, oldsize);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96 delete[] m_data;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97 m_allocatedSize = newsize;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98 m_data = new unsigned char[newsize];
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
99
94
294803d4ab5d Fix bad comparision in Bytestream::resize
Teemu Piippo <tsapii@utu.fi>
parents: 88
diff changeset
100 if (oldsize > 0L)
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101 memcpy (m_data, olddata, min (oldsize, newsize));
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104 // -------------------------------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
105 //
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 83
diff changeset
106 void Bytestream::init (const unsigned char* data, unsigned long length)
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
107 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
108 resize (length);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
109 memcpy (m_data, data, length);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
110 m_cursor = &m_data[0];
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
111 m_writtenLength = length;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
112 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
113
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
114 // -------------------------------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
115 //
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 83
diff changeset
116 void Bytestream::clear()
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
117 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
118 m_cursor = &m_data[0];
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
119 m_writtenLength = 0;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
120 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
121
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
122 // -------------------------------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
123 //
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 83
diff changeset
124 void Bytestream::ensure_read_space (unsigned int bytes)
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
125 {
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
126 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
127 {
83
08bfc3d9d2ae Removed format.cpp and format.h, use built-in formatting instead
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
128 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
129
08bfc3d9d2ae Removed format.cpp and format.h, use built-in formatting instead
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
130 String message;
08bfc3d9d2ae Removed format.cpp and format.h, use built-in formatting instead
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
131 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
132 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
133 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
134 }
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
135 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
136
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
137 // -------------------------------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
138 //
98
4f0f0b1b8e0b Use stdint.h types for the bytestream
Teemu Piippo <tsapii@utu.fi>
parents: 94
diff changeset
139 int8_t Bytestream::read_byte()
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
140 {
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
141 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
142 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
143 }
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
144
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
145 // -------------------------------------------------------------------------------------------------
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 //
98
4f0f0b1b8e0b Use stdint.h types for the bytestream
Teemu Piippo <tsapii@utu.fi>
parents: 94
diff changeset
147 int16_t Bytestream::read_short()
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
148 {
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
149 ensure_read_space (2);
10
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 9
diff changeset
150 short int result = 0;
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
151
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
152 for (int i = 0; i < 2; ++i)
10
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 9
diff changeset
153 result |= m_cursor[i] << (i * 8);
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
154
10
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 9
diff changeset
155 m_cursor += 2;
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 9
diff changeset
156 return result;
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
157 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
158
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
159 // -------------------------------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
160 //
98
4f0f0b1b8e0b Use stdint.h types for the bytestream
Teemu Piippo <tsapii@utu.fi>
parents: 94
diff changeset
161 int32_t Bytestream::read_long()
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
162 {
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
163 ensure_read_space (4);
10
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 9
diff changeset
164 long int result = 0;
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
165
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
166 for (int i = 0; i < 4; ++i)
10
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 9
diff changeset
167 result |= m_cursor[i] << (i * 8);
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
168
10
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 9
diff changeset
169 m_cursor += 4;
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 9
diff changeset
170 return result;
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
171 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
172
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
173 // -------------------------------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
174 //
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 83
diff changeset
175 float Bytestream::read_float()
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
176 {
61
cdf3c8af1545 - changed write_float/read_float to use memcpy
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
177 float value;
cdf3c8af1545 - changed write_float/read_float to use memcpy
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
178 int intvalue = read_long();
cdf3c8af1545 - changed write_float/read_float to use memcpy
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
179 memcpy (&value, &intvalue, sizeof intvalue);
cdf3c8af1545 - changed write_float/read_float to use memcpy
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
180 return value;
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
181 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
182
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
183 // -------------------------------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
184 //
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 83
diff changeset
185 String Bytestream::read_string()
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
186 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
187 // Zandronum sends strings of maximum 2048 characters, though it only
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
188 // reads 2047-character long ones so I guess we can follow up and do
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
189 // the same :-)
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
190 static char buffer[MAX_NETWORK_STRING];
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
191 unsigned char* stringEnd;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
192 unsigned char* stringBegin = m_cursor;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
193 unsigned char* end = m_data + allocated_size();
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
194
99
f9f73eeba3b7 Fixed crashing problems: min and max had their logic inverted and Bytestream::read_string moved the cursor too early
Teemu Piippo <tsapii@utu.fi>
parents: 98
diff changeset
195 // Where's the end of the string?
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
196 for (stringEnd = m_cursor; *stringEnd != '\0'; ++stringEnd)
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
197 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
198 if (stringEnd == end)
99
f9f73eeba3b7 Fixed crashing problems: min and max had their logic inverted and Bytestream::read_string moved the cursor too early
Teemu Piippo <tsapii@utu.fi>
parents: 98
diff changeset
199 {
f9f73eeba3b7 Fixed crashing problems: min and max had their logic inverted and Bytestream::read_string moved the cursor too early
Teemu Piippo <tsapii@utu.fi>
parents: 98
diff changeset
200 // Past the end of the buffer
f9f73eeba3b7 Fixed crashing problems: min and max had their logic inverted and Bytestream::read_string moved the cursor too early
Teemu Piippo <tsapii@utu.fi>
parents: 98
diff changeset
201 throw IOError ("unterminated or too long string in packet");
f9f73eeba3b7 Fixed crashing problems: min and max had their logic inverted and Bytestream::read_string moved the cursor too early
Teemu Piippo <tsapii@utu.fi>
parents: 98
diff changeset
202 }
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
203 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
204
99
f9f73eeba3b7 Fixed crashing problems: min and max had their logic inverted and Bytestream::read_string moved the cursor too early
Teemu Piippo <tsapii@utu.fi>
parents: 98
diff changeset
205 unsigned int length = stringEnd - m_cursor;
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
206 m_cursor = stringEnd + 1;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
207 memcpy (buffer, stringBegin, length);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
208 buffer[length] = '\0';
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
209 return String (buffer);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
210 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
211
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
212 // -------------------------------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
213 //
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 83
diff changeset
214 void Bytestream::read (unsigned char* buffer, unsigned long length)
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
215 {
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
216 ensure_read_space (length);
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
217 memcpy (buffer, m_cursor, length);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
218 m_cursor += length;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
219 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
220
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
221 // -------------------------------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
222 //
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 83
diff changeset
223 void Bytestream::write (unsigned char val)
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
224 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
225 *m_cursor++ = val;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
226 m_writtenLength++;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
227 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
228
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
229 // -------------------------------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
230 //
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 83
diff changeset
231 void Bytestream::write (const unsigned char* val, unsigned int length)
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
232 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
233 grow_to_fit (length);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
234 memcpy (m_cursor, val, length);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
235 m_cursor += length;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
236 m_writtenLength += length;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
237 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
238
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
239 // -------------------------------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
240 //
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 83
diff changeset
241 void Bytestream::grow_to_fit (unsigned long bytes)
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
242 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
243 if (space_left() < bytes)
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
244 resize (allocated_size() + bytes + 128);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
245 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
246
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
247 // -------------------------------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
248 //
98
4f0f0b1b8e0b Use stdint.h types for the bytestream
Teemu Piippo <tsapii@utu.fi>
parents: 94
diff changeset
249 void Bytestream::write_byte (int8_t val)
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
250 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
251 grow_to_fit (1);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
252 write (val);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
253 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
254
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
255 // -------------------------------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
256 //
98
4f0f0b1b8e0b Use stdint.h types for the bytestream
Teemu Piippo <tsapii@utu.fi>
parents: 94
diff changeset
257 void Bytestream::write_short (int16_t val)
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
258 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
259 grow_to_fit (2);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
260
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
261 for (int i = 0; i < 2; ++i)
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
262 write ((val >> (i * 8)) & 0xFF);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
263 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
264
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
265 // -------------------------------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
266 //
98
4f0f0b1b8e0b Use stdint.h types for the bytestream
Teemu Piippo <tsapii@utu.fi>
parents: 94
diff changeset
267 void Bytestream::write_long (int32_t val)
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
268 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
269 grow_to_fit (4);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
270
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
271 for (int i = 0; i < 4; ++i)
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
272 write ((val >> (i * 8)) & 0xFF);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
273 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
274
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
275 // -------------------------------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
276 //
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 83
diff changeset
277 void Bytestream::write_float (float val)
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
278 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
279 // 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
280 int intvalue;
cdf3c8af1545 - changed write_float/read_float to use memcpy
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
281 memcpy (&intvalue, &val, sizeof val);
cdf3c8af1545 - changed write_float/read_float to use memcpy
Teemu Piippo <crimsondusk64@gmail.com>
parents: 58
diff changeset
282 write_long (intvalue);
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
283 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
284
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
285 // -------------------------------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
286 //
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 83
diff changeset
287 void Bytestream::write_string (const String& val)
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
288 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
289 grow_to_fit (val.length() + 1);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
290 write (reinterpret_cast<const unsigned char*> (val.chars()), val.length());
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
291 write (0);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
292 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
293
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
294 // -------------------------------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
295 //
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 83
diff changeset
296 void Bytestream::write_buffer (const Bytestream& other)
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
297 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
298 write (other.data(), other.written_length());
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
299 }
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 83
diff changeset
300
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 83
diff changeset
301 END_ZFC_NAMESPACE

mercurial