forked from KonstantinChizhov/Mcucpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_buffer.h
325 lines (271 loc) · 6.93 KB
/
data_buffer.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//*****************************************************************************
//
// Author : Konstantin Chizhov
// Date : 2016
// All rights reserved.
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
// Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation and/or
// other materials provided with the distribution.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//*****************************************************************************
#pragma once
#include <stdint.h>
#include <stddef.h>
#include <array.h>
#include <binary_stream.h>
#include <net/net_addr.h>
namespace Mcucpp
{
class DataChunk
{
uint8_t *_data;
DataChunk * _next;
uint16_t _size;
uint16_t _storage;
friend class DataBufferBase;
friend class DataBufferIterator;
DataChunk(const DataChunk&)=delete;
DataChunk & operator=(const DataChunk &) = delete;
public:
DataChunk& operator=(DataChunk&& ) = default;
~DataChunk()
{
Release(this);
}
DataChunk(DataChunk&&)=default;
DataChunk(uint8_t *data, size_t size)
:_data(data), _next(0), _size(size), _storage(0)
{
}
DataChunk(uint8_t *data, size_t size, size_t storageSize)
:_data(data), _next(0), _size(size), _storage(storageSize)
{
}
uint8_t &operator[](size_t index)
{
return _data[index];
}
uint8_t operator[](size_t index)const
{
return _data[index];
}
bool Resize(size_t s)
{
if(s > _storage)
return false;
_size = s;
return true;
}
uint8_t* Data() const {return _data; }
size_t Size() const {return _size; }
size_t Capacity() const {return _storage ? _storage : _size; }
DataChunk* Next() const {return _next; }
DataChunk* Prev(DataChunk* first) const ;
static DataChunk* GetNew(size_t size);
static DataChunk* GetNew(const void *data, size_t size);
static void Release(DataChunk * data);
static void ReleaseRecursive(DataChunk * data);
static DataChunk *FindLast(DataChunk *first);
};
class DataBufferBase
{
DataChunk *_first;
DataChunk *_current;
size_t _pos;
static uint8_t _dummy;
friend class DataBufferIterator;
public:
DataBufferBase();
~DataBufferBase();
DataBufferBase(const DataBufferBase &) = delete;
DataBufferBase & operator=(const DataBufferBase &) = delete;
DataBufferBase& operator=(DataBufferBase&& ) noexcept;
DataBufferBase(DataBufferBase &&) noexcept;
DataBufferBase(DataChunk* chain)
:_first(chain), _current(nullptr), _pos(0)
{
}
DataChunk* MoveToBufferList()
{
DataChunk* result = _first;
_first = 0;
_current = 0;
_pos = 0;
return result;
}
DataChunk* BufferList(){ return _first;}
uint8_t operator*() const
{
if(!_current)
return 0;
return (*_current)[_pos];
}
uint8_t& operator*()
{
if(!_current)
return _dummy;
return (*_current)[_pos];
}
DataBufferBase& operator++()
{
if(_current)
{
if(++_pos >= _current->Size())
{
_pos = 0;
_current = _current->Next();
}
}
return *this;
}
DataBufferBase& operator--()
{
if(_current)
{
if(_pos == 0)
{
_current = _current->Prev(_first);
if(_current)
{
_pos = _current->Size();
}
}else
{
_pos--;
}
}
return *this;
}
uint8_t Read()
{
if(!_current)
return 0;
uint8_t value = (*_current)[_pos++];
if(_pos >= _current->Size())
{
_pos = 0;
_current = _current->Next();
}
return value;
}
void Write(uint8_t byte)
{
if(!_current)
return;
(*_current)[_pos++] = byte;
if(_pos >= _current->Size())
{
_pos = 0;
_current = _current->Next();
}
}
void WriteMac(const Net::MacAddr &addr)
{
for(size_t i = 0; i < addr.Length(); i++)
{
Write(addr[i]);
}
}
void WriteIp(const Net::IpAddr &addr)
{
for(size_t i = 0; i < addr.Length(); i++)
{
Write(addr[i]);
}
}
Net::MacAddr ReadMac()
{
Net::MacAddr addr;
for(size_t i = 0; i < addr.Length(); i++)
{
addr[i] = Read();
}
return addr;
}
Net::IpAddr ReadIp()
{
uint8_t a = Read();
uint8_t b = Read();
uint8_t c = Read();
uint8_t d = Read();
return Net::IpAddr(a, b, c, d);
}
void Clear();
bool InsertFront(size_t size);
bool InsertBack(size_t size);
bool Insert(size_t pos, size_t size);
void AttachBack(DataChunk* buffer);
void AttachFront(DataChunk* buffer);
DataChunk* DetachFront();
DataChunk* CurrentChunck(){ return _current; }
bool Seek(size_t pos);
size_t Tell();
size_t Size();
unsigned Parts();
};
typedef Mcucpp::BinaryStream<DataBufferBase> DataBuffer;
class DataBufferIterator
{
DataChunk *_current;
size_t _pos;
public:
DataBufferIterator(DataChunk *first)
:_current(first),
_pos(0)
{}
uint8_t operator*() const
{
if(!_current)
return 0;
return (*_current)[_pos];
}
uint8_t& operator*()
{
if(!_current)
return DataBufferBase::_dummy;
return (*_current)[_pos];
}
DataBufferIterator& operator++()
{
if(_current)
{
if(++_pos >= _current->Size())
{
_pos = 0;
_current = _current->Next();
}
}
return *this;
}
bool operator== (const DataBufferIterator & other)
{
return _current == other._current && _pos == other._pos;
}
bool operator!= (const DataBufferIterator & other)
{
return _current != other._current;
}
};
static inline DataBufferIterator begin(DataBuffer &buffer)
{
return DataBufferIterator(buffer.BufferList());
}
static inline DataBufferIterator end(DataBuffer &buffer)
{
(void)buffer;
return DataBufferIterator(nullptr);
}
}