-
Notifications
You must be signed in to change notification settings - Fork 59
/
CBytestream.h
224 lines (189 loc) · 5.06 KB
/
CBytestream.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/////////////////////////////////////////
//
// OpenLieroX
//
// Auxiliary Software class library
//
// based on the work of JasonB
// enhanced by Dark Charlie and Albert Zeyer
//
// code under LGPL
//
/////////////////////////////////////////
// Byte stream class
// Created 13/10/01
// Jason Boettcher
#ifndef __CBYTESTREAM_H__
#define __CBYTESTREAM_H__
#include <SDL.h> // for SInt16
#include <string>
#include <set>
#include "olx-types.h"
#include "Networking.h"
#include "SmartPointer.h"
#include "CodeAttributes.h"
class ScriptVar_t;
struct Logger;
struct PrintOutFct;
struct CustomVar;
class CBytestream {
public:
CBytestream() : pos(0), bitPos(0), Data("") {}
CBytestream(const std::string& rawData) : pos(0), bitPos(0), Data(rawData) {}
CBytestream(const CBytestream& bs) {
operator=(bs);
}
CBytestream& operator=(const CBytestream& bs) {
pos = bs.pos;
Data = bs.Data;
bitPos = bs.bitPos;
return *this;
}
private:
// Attributes
size_t pos;
size_t bitPos;
std::string Data;
public:
// Methods
// Debug
void Test();
// Generic data
void ResetBitPos() { bitPos = 0; }
void ResetPosToBegin() { pos = 0; bitPos = 0; }
size_t GetLength() const { return Data.size(); }
size_t GetPos() const { return pos; }
size_t GetRestLen() const { return isPosAtEnd() ? 0 : (Data.size() - pos); }
bool isPosAtEnd() const { return GetPos() >= GetLength(); }
void revertByte() { assert(pos > 0); pos--; }
void flushOld() { Data.erase(0, pos); pos = 0; }
std::string getRawData(size_t start, size_t end) { assert(start <= end); return Data.substr(start, end - start + 1); }
void Clear();
void Append(CBytestream *bs);
// Note: marks positions are relative to start; start=0 means from the very beginning of the stream (not from pos)
void Dump(const PrintOutFct& printer, const std::set<size_t>& marks = std::set<size_t>(), size_t start = 0, size_t count = (size_t)-1);
void Dump();
// Writes
bool writeByte(uchar byte);
bool writeBool(bool value);
bool writeInt(int value, uchar numbytes);
bool writeInt16(Sint16 value);
bool writeUInt64(Uint64 value);
bool writeFloat(float value);
bool writeString(const std::string& value);
bool write2Int12(short x, short y);
bool write2Int4(short x, short y);
bool writeBit(bool bit);
bool writeData(const std::string& value); // Do not append '\0' at the end, writes just the raw data
bool writeVar(const ScriptVar_t& var, const CustomVar* diffToOld = NULL);
// Reads
uchar readByte();
bool readBool();
int readInt(uchar numbytes); // readInt(2) will return 0:65535 range, not -32768:32767, so save it into Sint16
Sint16 readInt16();
Uint64 readUInt64();
float readFloat();
std::string readString();
std::string readString(size_t maxlen);
void read2Int12(short& x, short& y);
void read2Int4(short& x, short& y);
bool readBit();
std::string readData( size_t size = (size_t)(-1) );
bool readVar(ScriptVar_t& var);
// Peeks
uchar peekByte() const;
std::string peekData(size_t len) const;
const std::string& data() const { return Data; }
// Skips
// Folowing functions return true if we're at the end of stream after the skip
bool Skip(size_t num);
bool SkipInt() { return Skip(4); }
bool SkipFloat() { return Skip(4); }
bool SkipShort() { return Skip(2); }
bool SkipString();
void SkipAll() { pos = Data.size(); }
bool SkipRestBits() { if(isPosAtEnd()) return true; ResetBitPos(); pos++; return isPosAtEnd(); }
bool SkipVar();
// Networking stuff
bool Send(NetworkSocket* sock);
size_t Read(NetworkSocket* sock);
bool Send(const SmartPointer<NetworkSocket>& sock) { return Send(sock.get()); }
size_t Read(const SmartPointer<NetworkSocket>& sock) { return Read(sock.get()); }
};
// Inline class to operate with bits for dirt updates, hopefully optimized by compiler
// No bound-checking is made, be sure your data is large enough
class CBytestreamBitIterator
{
char * data;
size_t pos;
Uint8 bitMask;
public:
CBytestreamBitIterator(char * Data): data(Data)
{
resetPos();
}
size_t getPos() const
{
return pos;
}
void resetPos()
{
pos = 0;
bitMask = 1;
}
CBytestreamBitIterator& operator ++()
{
if( bitMask >= 128 )
{
bitMask = 1;
pos ++;
}
else
bitMask *= 2;
return *this;
}
bool getBit() const
{
return ( data[pos] & bitMask ) != 0;
}
void setBit()
{
data[pos] |= bitMask;
}
void clearBit()
{
data[pos] &= ~ bitMask;
}
void xorBit()
{
data[pos] ^= bitMask;
}
// Slower functions
bool readBit()
{
bool ret = getBit();
++(*this);
return ret;
}
void writeBit( bool bit )
{
if( bit )
setBit();
else
clearBit();
++(*this);
}
static size_t getSizeInBytes( size_t bits )
{
size_t bytes = bits / 8;
if( bits % 8 )
bytes++;
return bytes;
}
};
template< bool (*fct1) (CBytestream*), bool (*fct2) (CBytestream*) >
INLINE bool SkipMult(CBytestream* bs) { return (*fct1)(bs) && (*fct2)(bs); }
template< size_t NUM >
INLINE bool Skip(CBytestream* bs) { return bs->Skip(NUM); }
INLINE bool SkipString(CBytestream* bs) { return bs->SkipString(); }
#endif // __CBITSTREAM_H__