Newer
Older
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include "buffered_binary_stream.h"
BufferedBinaryStream::BufferedBinaryStream()
{
this->isOpen = false;
this->underlayingSourceType = BufferSourceType_NoSource;
}
BufferedBinaryStream::BufferedBinaryStream(ByteArray *bytes)
{
this->isOpen = true;
this->underlayingSourceType = BufferSourceType_Memory;
this->memoryBuffer = bytes;
this->currentBufferPosition = 0;
}
BufferedBinaryStream::BufferedBinaryStream(BinaryStreamBase *stream, const long streamBufferSize)
{
this->isOpen = stream->is_open();
this->underlayingSourceType = BufferSourceType_Stream;
this->streamBufferSize = streamBufferSize;
this->underlayingStream = stream;
this->currentBufferPosition = 0;
}
BufferedBinaryStream::~BufferedBinaryStream()
{
// Do we want to close stream / clear buffer?
// Probably not, because someone may want to use them later.
/*
switch (this->underlayingSourceType)
{
case BufferSourceType_Memory:
{
this->buffer.clear();
}
break;
case BufferSourceType_Stream:
{
this->underlayingStream->close_stream();
}
break;
}
*/
}
long BufferedBinaryStream::get_size() const
{
switch (this->underlayingSourceType)
{
case BufferSourceType_Memory:
{
return this->memoryBuffer->size();
}
break;
case BufferSourceType_Stream:
{
return this->underlayingStream->get_size();
}
break;
default:
always_assert(false && "Invalid BufferSourceType.");
break;
}
return 0;
}
long BufferedBinaryStream::get_position()
{
switch (this->underlayingSourceType)
{
case BufferSourceType_Memory:
{
return this->currentBufferPosition;
}
break;
case BufferSourceType_Stream:
{
always_assert(this->currentBufferPosition <= this->streamBufferSize);
long pos = (this->readedFromSource > 0) ? this->readedFromSource - (this->streamBufferSize - this->currentBufferPosition) : 0;
return pos;
}
break;
default:
always_assert(false && "Invalid BufferSourceType.");
break;
}
return 0;
}
ulong BufferedBinaryStream::get_consume_size()
{
switch (this->underlayingSourceType)
{
case BufferSourceType_Memory:
{
always_assert(false && "This shouldn't be called ever.");
return 0;
}
break;
case BufferSourceType_Stream:
{
long totalSize = get_size();
ulong currPos = this->underlayingStream->get_position();
ulong avaible = totalSize - currPos;
ulong toConsume = (avaible > this->streamBufferSize) ? this->streamBufferSize : avaible;
return toConsume;
}
break;
default:
always_assert(false && "Invalid BufferSourceType.");
break;
}
return 0;
}
void BufferedBinaryStream::move_to(const long position)
{
always_assert(position < get_size());
switch (this->underlayingSourceType)
{
case BufferSourceType_Memory:
{
this->currentBufferPosition = position;
}
break;
case BufferSourceType_Stream:
{
// move_to will always reload the buffer.
this->underlayingStream->move_to(position);
ulong toConsume = get_consume_size();
this->streamBuffer = this->underlayingStream->consume_bytes(toConsume);
this->readedFromSource = this->underlayingStream->get_position();
// Just for sure.
//always_assert(this->readedFromSource == position + this->streamBufferSize);
this->currentBufferPosition = 0;
}
break;
default:
always_assert(false && "Invalid BufferSourceType.");
break;
}
}
void BufferedBinaryStream::move_to_beginning()
{
switch (this->underlayingSourceType)
{
case BufferSourceType_Memory:
{
this->currentBufferPosition = 0;
}
break;
case BufferSourceType_Stream:
{
// move_to_beginning will always reload the buffer.
this->underlayingStream->move_to_beginning();
this->streamBuffer = this->underlayingStream->consume_bytes(get_consume_size());
this->readedFromSource = this->streamBufferSize;
this->currentBufferPosition = 0;
}
break;
default:
always_assert(false && "Invalid BufferSourceType.");
break;
}
}
void BufferedBinaryStream::move_to_end()
{
this->currentBufferPosition = get_size();
switch (this->underlayingSourceType)
{
case BufferSourceType_Stream:
{
// move_to_beginning will always clear the buffer.
this->streamBuffer.clear();
this->readedFromSource = this->currentBufferPosition;
this->currentBufferPosition = 0;
}
break;
default:
always_assert(false && "Invalid BufferSourceType.");
break;
}
}
void BufferedBinaryStream::move_by(const long distance)
{
always_assert((get_position() + distance) < get_size());
switch (this->underlayingSourceType)
{
case BufferSourceType_Memory:
{
always_assert((this->currentBufferPosition + distance) < this->memoryBuffer->size());
this->currentBufferPosition += distance;
}
break;
case BufferSourceType_Stream:
{
// If atleast one byte can be read, just advance the buffer position.
if ((this->currentBufferPosition + distance + 1) < this->streamBufferSize)
{
this->currentBufferPosition += distance;
}
else
{
// If we are moving outside buffer, reload the buffer from desired location.
long currentPosInStreamRelativeToBufferPos = get_position();
always_assert((currentPosInStreamRelativeToBufferPos + distance) < this->underlayingStream->get_size());
this->underlayingStream->move_to((currentPosInStreamRelativeToBufferPos + distance));
ulong toConsume = get_consume_size();
this->streamBuffer = this->underlayingStream->consume_bytes(toConsume);
this->readedFromSource = this->underlayingStream->get_position();
this->currentBufferPosition = 0;
}
}
break;
default:
always_assert(false && "Invalid BufferSourceType.");
break;
}
}
ByteArray BufferedBinaryStream::consume_bytes(const ulong byteCount)
{
if (this->streamBufferSize < byteCount)
{
printf(RED "To parse this file, you need buffer of size atleast: %lu\n" RESET, byteCount);
}
// We won't allow consuming more bytes than is buffer size.
always_assert(byteCount <= this->streamBufferSize && "Buffer is too small.");
switch (this->underlayingSourceType)
{
case BufferSourceType_Memory:
{
// Check how many bytes are avaible in memory buffer.
ulong bytesAvaible = this->memoryBuffer->size() - this->currentBufferPosition;
printf("BA: %lu\n", bytesAvaible);
always_assert(bytesAvaible >= byteCount);
auto fromIt = this->memoryBuffer->begin() + this->currentBufferPosition;
auto toIt = fromIt + byteCount;
this->currentBufferPosition += byteCount;
ByteArray result(fromIt, toIt);
return result;
}
break;
case BufferSourceType_Stream:
{
ulong bytesAvaible = this->streamBuffer.size() - this->currentBufferPosition;
if ((bytesAvaible == 0) || (byteCount > bytesAvaible))
{
long currentPos = get_position();
// Moving to current position will ensure, that streamBufferSize of data will be read into buffer.
move_to(currentPos);
}
auto fromIt = this->streamBuffer.begin() + this->currentBufferPosition;
auto toIt = fromIt + byteCount;
this->currentBufferPosition += byteCount;
ByteArray result(fromIt, toIt);
return result;
}
break;
default:
always_assert(false && "Invalid BufferSourceType.");
break;
}
return ByteArray();