-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBytes.cpp
305 lines (265 loc) · 6.32 KB
/
Bytes.cpp
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
/*
* ByteBuffer.cpp
*
* Created on: 24-aug.-2012
* Author: lieven
*/
#include "Bytes.h"
#include <stdlib.h>
#if defined(__linux__) || defined(__CYGWIN__)
#include <stdlib.h>
#else
//#include "new.h"
#endif
void myMemcpy(uint8_t* dst, uint8_t* src, int length) {
for (int i = 0; i < length; i++)
dst[i] = src[i];
}
Bytes::Bytes() {
_start = 0;
_offset = 0;
_limit = 0;
_capacity = 0;
isMemoryOwner = false;
}
Bytes::Bytes(uint8_t* st, uint32_t length) {
_start = st;
_offset = 0;
_limit = length;
_capacity = length;
isMemoryOwner = false;
}
Bytes& Bytes::map(uint8_t* st, uint32_t length) {
_start = st;
_offset = 0;
_limit = length;
_capacity = length;
isMemoryOwner = false;
return *this;
}
/* Bytes::Bytes() {
_start = (uint8_t*) 0;
_offset = 0;
_limit = 0;
_capacity = 0;
isMemoryOwner = false;
}*/
Bytes::Bytes(uint32_t size) {
_start = 0;
_offset = 0;
_limit = 0;
_capacity = size;
if (size > 0) {
// INFO("calling malloc : %d ",size);
_start = (uint8_t*)malloc(
size); // (uint8_t*) Sys::malloc(size);
// INFO("malloc result : %x",_start);
_capacity = size;
}
isMemoryOwner = true;
}
Bytes::~Bytes() {
if (isMemoryOwner)
if (_start)
free(_start);
}
Bytes::Bytes(const Bytes& src) {
_start = (uint8_t*)malloc(src._capacity);
_offset = 0;
_limit = src._limit;
_capacity = src._capacity;
myMemcpy(_start, src._start, src._limit);
isMemoryOwner = true;
}
Bytes& Bytes::clone(Bytes& src) {
myMemcpy(_start, src._start, _capacity);
_offset = 0;
_limit = (_capacity > src._limit) ? src._limit : _capacity;
return *this;
}
Bytes& Bytes::sub(Bytes* parent, uint32_t length) {
_start = parent->_start + parent->_offset;
_offset = 0;
if (length <= (parent->_capacity - parent->_offset))
_limit = length;
else
_limit = parent->_capacity - parent->_offset;
_capacity = _limit;
isMemoryOwner = false;
return *this;
}
Bytes& Bytes::append(Bytes& b) {
b.offset(0);
while (b.hasData())
write(b.read());
return *this;
}
Bytes& Bytes::append(const char s[]) {
while (*s != '\0') {
write((uint8_t)(*s));
s++;
}
return *this;
}
Bytes& Bytes::operator=( Bytes& s) {
clear();
return append(s);
}
Bytes& Bytes::operator=( const Bytes& s) {
clear();
return append((Bytes&)s);
}
Bytes& Bytes::operator=(const char* s) {
clear();
return append(s);
}
Bytes& Bytes::append(uint8_t* data, uint32_t length) {
for (uint32_t i = 0; i < length; i++)
write(data[i]);
return *this;
}
Bytes& Bytes::move(int32_t dist) {
if ((_offset + dist) > _limit)
_offset = _limit;
else
_offset += dist;
return *this;
}
/* ByteBuffer::ByteBuffer(ByteBuffer& in) {
start = in.start;
_offset = 0;
_limit = in._limit;
_capacity = in._capacity;
isMemoryOwner = false;
}*/
uint8_t* Bytes::data() const { return _start; }
int Bytes::capacity() { return _capacity; }
uint32_t Bytes::length() const { return _limit; }
int Bytes::offset(int32_t pos) {
if (pos < 0)
_offset = _limit;
else if ((uint32_t)pos < _capacity)
_offset = pos;
return 0;
}
Erc Bytes::insert(uint32_t offset, uint8_t* data, uint32_t length) {
if (length + _limit > _capacity)
return E_LACK_RESOURCE;
if (offset > _limit)
return E_INVAL;
// move last part further
uint32_t delta = length;
uint32_t i;
for (i = _limit; i >= offset; i--)
_start[i + delta] = _start[i];
// insert data
for (i = 0; i < delta; i++)
_start[offset + i] = data[i];
return E_OK;
}
Erc Bytes::insert(uint32_t offset, Bytes& data) {
return insert(offset, data.data(), data.length());
}
int Bytes::offset() { return _offset; }
int Bytes::length(int l) {
_offset = l;
_limit = l;
return 0;
}
int Bytes::used() { return _limit; }
int Bytes::available() {
if (_offset < _limit)
return _limit - _offset;
else
return 0;
}
/*
Erc Bytes::write(uint8_t value) {
if (_offset < _capacity) {
_start[_offset++] = value;
_limit = _offset;
} else
return E_LACK_RESOURCE;
return 0;
}
*/
Erc Bytes::write(uint8_t* data, int offset, int length) {
for (int i = 0; i < length; i++) {
int erc;
erc = write(data[offset + i]);
if (erc)
return erc;
}
return 0;
}
Erc Bytes::write(Bytes* data) { return write(data->_start, 0, data->_limit); }
Erc Bytes::read(uint8_t* data) {
if (_offset < _limit)
*data = _start[_offset++];
else
return E_AGAIN;
return 0;
}
uint8_t Bytes::read() {
if (_offset < _limit)
return _start[_offset++];
return '-';
}
Bytes& Bytes::clear() {
_offset = 0;
_limit = 0;
return *this;
}
bool Bytes::equals(const uint8_t* pb, uint32_t length) {
if (length != _limit)
return false;
for (uint32_t i = 0; i < length; i++) {
if (_start[i] != pb[i])
return false;
}
return true;
}
int Bytes::poke(uint32_t idx, uint8_t b) {
if (idx > _limit)
return E_LACK_RESOURCE;
_start[idx] = b;
return 0;
}
int Bytes::peek() { return _start[_offset]; }
bool Bytes::seek(uint8_t b) {
for (uint32_t i = _offset; i < _limit; i++)
if (_start[i] == b) {
_offset = i;
return true;
}
return false;
}
int Bytes::peek(int32_t offset) { return _start[offset]; }
bool Bytes::hasData() { return _offset < _limit; }
bool Bytes::hasData(uint32_t size) { return _offset + size - 1 < _limit; }
bool Bytes::hasSpace(uint32_t size) { return (_capacity - _limit) >= size; }
const char* HEX_VALUES = "0123456789ABCDEF";
#include "Str.h"
const char* Bytes::toHex(Str& str) {
uint32_t i;
uint8_t b;
for (i = 0; i < _limit; i++) {
b = *(_start + i);
str.append(HEX_VALUES[b >> 4]);
str.append(HEX_VALUES[b & 0xF]);
str.append(' ');
}
return str.c_str();
}
const char* Bytes::toString(Str& str) {
uint32_t i;
uint8_t b;
for (i = 0; i < _limit; i++) {
b = *(_start + i);
if (b >= 0x20 && b < 0x7F)
str.append((char)b);
else
str.append('.');
}
return str.c_str();
}