Fri, 12 Dec 2014 01:37:04 +0200
- now able to login
| 
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 | #pragma once | 
| 32 | #include "../main.h" | |
| 33 | ||
| 34 | class String; | |
| 35 | ||
| 36 | enum { MAX_NETWORK_STRING = 0x800 }; | |
| 37 | ||
| 38 | // TODO: Make able to handle big-endian too | |
| 39 | class Bytestream | |
| 40 | { | |
| 41 | public: | |
| 42 | static bool sink; | |
| 43 | ||
| 44 | Bytestream (unsigned long length = 0x800); | |
| 45 | Bytestream (const unsigned char* data, unsigned long length); | |
| 46 | Bytestream (const Vector<unsigned char>& bytes); | |
| 11 | 47 | Bytestream (const Bytestream& other); | 
| 5 | 48 | ~Bytestream(); | 
| 49 | ||
| 50 | inline METHOD allocated_size() const -> unsigned long; | |
| 51 | inline METHOD bytes_left() const -> unsigned long; | |
| 52 | METHOD clear() -> void; | |
| 53 | inline METHOD data() -> unsigned char*; | |
| 54 | inline METHOD data() const -> const unsigned char*; | |
| 55 | METHOD grow_to_fit (unsigned long bytes) -> void; | |
| 56 | inline METHOD position() const -> unsigned long; | |
| 57 | METHOD read (unsigned char* buffer, unsigned long length, bool* ok = &sink) -> void; | |
| 58 | METHOD read_byte (bool* ok = &sink) -> char; | |
| 59 | METHOD read_short (bool* ok = &sink) -> short int; | |
| 60 | METHOD read_long (bool* ok = &sink) -> long int; | |
| 61 | METHOD read_string (bool* ok = &sink) -> String; | |
| 62 | METHOD read_float (bool* ok = &sink) -> float; | |
| 63 | METHOD resize (unsigned long length) -> void; | |
| 64 | inline METHOD rewind() -> void; | |
| 65 | inline METHOD seek (unsigned long pos) -> void; | |
| 11 | 66 | inline METHOD to_vector() const -> Vector<unsigned char>; | 
| 5 | 67 | METHOD write (const unsigned char* val, unsigned int length) -> void; | 
| 68 | METHOD write_buffer (const Bytestream& other) -> void; | |
| 69 | METHOD write_buffer (const Vector<unsigned char>& other) -> void; | |
| 70 | METHOD write_byte (char val) -> void; | |
| 71 | METHOD write_double (double val) -> void; | |
| 72 | METHOD write_float (float val) -> void; | |
| 73 | METHOD write_long (long int val) -> void; | |
| 74 | METHOD write_short (short int val) -> void; | |
| 75 | METHOD write_string (const String& val) -> void; | |
| 76 | inline METHOD written_length() const -> unsigned long; | |
| 77 | ||
| 78 | inline METHOD operator[] (unsigned long idx) -> unsigned char&; | |
| 79 | inline METHOD operator[] (unsigned long idx) const -> unsigned char; | |
| 11 | 80 | METHOD operator= (const Bytestream& other) -> Bytestream&; | 
| 5 | 81 | |
| 82 | private: | |
| 83 | unsigned char* m_data; | |
| 84 | unsigned char* m_cursor; | |
| 85 | unsigned long m_allocatedSize; | |
| 86 | unsigned long m_writtenLength; | |
| 87 | ||
| 88 | METHOD init (const unsigned char* data, unsigned long length) -> void; | |
| 89 | METHOD write (unsigned char val) -> void; | |
| 90 | inline METHOD space_left() const -> unsigned long; | |
| 91 | }; | |
| 92 | ||
| 93 | // ------------------------------------------------------------------------------------------------- | |
| 94 | // | |
| 95 | inline METHOD | |
| 96 | Bytestream::allocated_size() const -> unsigned long | |
| 97 | { | |
| 98 | return m_allocatedSize; | |
| 99 | } | |
| 100 | ||
| 101 | // ------------------------------------------------------------------------------------------------- | |
| 102 | // | |
| 103 | inline METHOD | |
| 104 | Bytestream::written_length() const -> unsigned long | |
| 105 | { | |
| 106 | return m_writtenLength; | |
| 107 | } | |
| 108 | ||
| 109 | // ------------------------------------------------------------------------------------------------- | |
| 110 | // | |
| 111 | inline METHOD | |
| 112 | Bytestream::operator[] (unsigned long idx) -> unsigned char& | |
| 113 | { | |
| 114 | return m_data[idx]; | |
| 115 | } | |
| 116 | ||
| 117 | // ------------------------------------------------------------------------------------------------- | |
| 118 | // | |
| 119 | inline METHOD | |
| 120 | Bytestream::operator[] (unsigned long idx) const -> unsigned char | |
| 121 | { | |
| 122 | return m_data[idx]; | |
| 123 | } | |
| 124 | ||
| 125 | // ------------------------------------------------------------------------------------------------- | |
| 126 | // | |
| 127 | inline METHOD | |
| 128 | Bytestream::position() const -> unsigned long | |
| 129 | { | |
| 130 | return m_cursor - m_data; | |
| 131 | } | |
| 132 | ||
| 133 | // ------------------------------------------------------------------------------------------------- | |
| 134 | // | |
| 135 | inline METHOD | |
| 136 | Bytestream::seek (unsigned long pos) -> void | |
| 137 | { | |
| 138 | m_cursor = m_data + pos; | |
| 139 | } | |
| 140 | ||
| 141 | // ------------------------------------------------------------------------------------------------- | |
| 142 | // | |
| 143 | inline METHOD | |
| 144 | Bytestream::rewind() -> void | |
| 145 | { | |
| 146 | m_cursor = m_data; | |
| 147 | } | |
| 148 | ||
| 149 | // ------------------------------------------------------------------------------------------------- | |
| 150 | // | |
| 151 | inline METHOD | |
| 152 | Bytestream::bytes_left() const -> unsigned long | |
| 153 | { | |
| 154 | return (m_writtenLength - (m_cursor - &m_data[0])); | |
| 155 | } | |
| 156 | ||
| 157 | // ------------------------------------------------------------------------------------------------- | |
| 158 | // | |
| 159 | inline METHOD | |
| 160 | Bytestream::space_left() const -> unsigned long | |
| 161 | { | |
| 162 | return (m_allocatedSize - m_writtenLength); | |
| 163 | } | |
| 164 | ||
| 165 | // ------------------------------------------------------------------------------------------------- | |
| 166 | // | |
| 167 | inline METHOD | |
| 168 | Bytestream::data() -> unsigned char* | |
| 169 | { | |
| 170 | return m_data; | |
| 171 | } | |
| 172 | ||
| 173 | // ------------------------------------------------------------------------------------------------- | |
| 174 | // | |
| 175 | inline METHOD | |
| 176 | Bytestream::data() const -> const unsigned char* | |
| 177 | { | |
| 178 | return m_data; | |
| 179 | } | |
| 11 | 180 | |
| 181 | // ------------------------------------------------------------------------------------------------- | |
| 182 | // | |
| 183 | inline METHOD | |
| 184 | Bytestream::to_vector() const -> Vector<unsigned char> | |
| 185 | { | |
| 186 | return Vector<unsigned char> (m_data, m_writtenLength); | |
| 187 | } |