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
|
/**
* @file
* @author 2012-2013 Stefan Radomski (stefan.radomski@cs.tu-darmstadt.de)
* @copyright Simplified BSD
*
* @cond
* This program is free software: you can redistribute it and/or modify
* it under the terms of the FreeBSD license as published by the FreeBSD
* project.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the FreeBSD license along with this
* program. If not, see <http://www.opensource.org/licenses/bsd-license>.
* @endcond
*/
#include "TypedArray.h"
#include <iostream>
#include <boost/detail/endian.hpp>
#define DATAVIEW_TYPED_GET(type) \
type retVal;\
if (index + _start + sizeof(type) > _buffer->size)\
return 0;\
memcpy(&retVal, _buffer->data + (_start + index), sizeof(type));
#define DATAVIEW_TYPED_SET(type) \
if (index + _start + sizeof(type) > _buffer->size)\
return;\
memcpy(_buffer->data + (_start + index), &value, sizeof(type));
namespace uscxml {
ArrayBuffer::ArrayBuffer(unsigned long length) {
_buffer = boost::shared_ptr<Blob>(new Blob(length));
}
ArrayBuffer::ArrayBuffer(boost::shared_ptr<Blob> buffer) : _buffer(buffer) {
}
ArrayBuffer::ArrayBuffer(void* data, unsigned int size) {
_buffer = boost::shared_ptr<Blob>(new Blob(data, size, "application/octet-stream"));
}
unsigned long ArrayBuffer::getByteLength() {
if (!_buffer)
return 0;
return _buffer->size;
}
ArrayBuffer ArrayBuffer::slice(long begin, long end) {
if (!_buffer) {
return ArrayBuffer(0);
}
unsigned int realBegin = (begin + _buffer->size) % _buffer->size;
unsigned int realEnd = (end + _buffer->size) % _buffer->size;
if (realEnd < realBegin) {
return ArrayBuffer(0);
}
ArrayBuffer arrBuffer(realEnd - realBegin);
memcpy(arrBuffer._buffer->data, _buffer->data + realBegin, realEnd - realBegin);
return arrBuffer;
}
ArrayBuffer ArrayBuffer::slice(long begin) {
return slice(begin, _buffer->size);
}
bool ArrayBuffer::isView(void*) {
return true;
}
ArrayBuffer::operator bool() {
return _buffer;
}
ArrayBuffer ArrayBufferView::getBuffer() {
return ArrayBuffer(_buffer);
}
DataView::DataView(ArrayBuffer* buffer, unsigned long byteOffset, unsigned long byteLength) {
_start = byteOffset;
if (_start > buffer->_buffer->size)
return;
_end = _start + byteLength;
if (_end > buffer->_buffer->size)
return;
_buffer = buffer->_buffer;
}
DataView::DataView(ArrayBuffer* buffer , unsigned long byteOffset) {
_start = byteOffset;
_end = buffer->_buffer->size;
if (_start > buffer->_buffer->size)
return;
_buffer = buffer->_buffer;
}
DataView::DataView(ArrayBuffer* buffer) {
_start = 0;
_end = (buffer->_buffer->size);
_buffer = buffer->_buffer;
}
unsigned long DataView::getByteOffset() {
return _start;
}
unsigned long DataView::getByteLength() {
return _end - _start;
}
unsigned long DataView::getLength() {
return _end - _start;
}
int8_t DataView::getInt8(unsigned long index) {
DATAVIEW_TYPED_GET(int8_t);
return retVal;
}
uint8_t DataView::getUint8(unsigned long index) {
DATAVIEW_TYPED_GET(uint8_t);
return retVal;
}
int16_t DataView::getInt16(unsigned long index, bool littleEndian) {
DATAVIEW_TYPED_GET(int16_t);
#ifdef BOOST_LITTLE_ENDIAN
if (littleEndian)
return retVal;
return byte_swap<little_endian, big_endian>(retVal);
#else
if (littleEndian)
return byte_swap<big_endian, little_endian>(retVal);
return retVal;
#endif
}
uint16_t DataView::getUint16(unsigned long index, bool littleEndian) {
DATAVIEW_TYPED_GET(uint16_t);
#ifdef BOOST_LITTLE_ENDIAN
if (littleEndian)
return retVal;
return byte_swap<little_endian, big_endian>(retVal);
#else
if (littleEndian)
return byte_swap<big_endian, little_endian>(retVal);
return retVal;
#endif
}
int32_t DataView::getInt32(unsigned long index, bool littleEndian) {
DATAVIEW_TYPED_GET(int32_t);
#ifdef BOOST_LITTLE_ENDIAN
if (littleEndian)
return retVal;
return byte_swap<little_endian, big_endian>(retVal);
#else
if (littleEndian)
return byte_swap<big_endian, little_endian>(retVal);
return retVal;
#endif
}
uint32_t DataView::getUint32(unsigned long index, bool littleEndian) {
DATAVIEW_TYPED_GET(uint32_t);
#ifdef BOOST_LITTLE_ENDIAN
if (littleEndian)
return retVal;
return byte_swap<little_endian, big_endian>(retVal);
#else
if (littleEndian)
return byte_swap<big_endian, little_endian>(retVal);
return retVal;
#endif
}
float DataView::getFloat32(unsigned long index, bool littleEndian) {
DATAVIEW_TYPED_GET(float);
#ifdef BOOST_LITTLE_ENDIAN
if (littleEndian)
return retVal;
return byte_swap<little_endian, big_endian>(retVal);
#else
if (littleEndian)
return byte_swap<big_endian, little_endian>(retVal);
return retVal;
#endif
}
double DataView::getFloat64(unsigned long index, bool littleEndian) {
DATAVIEW_TYPED_GET(double);
#ifdef BOOST_LITTLE_ENDIAN
if (littleEndian)
return retVal;
return byte_swap<little_endian, big_endian>(retVal);
#else
if (littleEndian)
return byte_swap<big_endian, little_endian>(retVal);
return retVal;
#endif
}
void DataView::setInt8(long index, int8_t value) {
DATAVIEW_TYPED_SET(int8_t);
}
void DataView::setUint8(long index, uint8_t value) {
DATAVIEW_TYPED_SET(uint8_t);
}
void DataView::setInt16(long index, int16_t value, bool littleEndian) {
#ifdef BOOST_LITTLE_ENDIAN
if (!littleEndian)
value = byte_swap<little_endian, big_endian>(value);
#else
if (littleEndian)
value = byte_swap<big_endian, little_endian>(value);
#endif
DATAVIEW_TYPED_SET(int16_t);
}
void DataView::setUint16(long index, uint16_t value, bool littleEndian) {
#ifdef BOOST_LITTLE_ENDIAN
if (!littleEndian)
value = byte_swap<little_endian, big_endian>(value);
#else
if (littleEndian)
value = byte_swap<big_endian, little_endian>(value);
#endif
DATAVIEW_TYPED_SET(uint16_t);
}
void DataView::setInt32(long index, int32_t value, bool littleEndian) {
#ifdef BOOST_LITTLE_ENDIAN
if (!littleEndian)
value = byte_swap<little_endian, big_endian>(value);
#else
if (littleEndian)
value = byte_swap<big_endian, little_endian>(value);
#endif
DATAVIEW_TYPED_SET(int32_t);
}
void DataView::setUint32(long index, uint32_t value, bool littleEndian) {
#ifdef BOOST_LITTLE_ENDIAN
if (!littleEndian)
value = byte_swap<little_endian, big_endian>(value);
#else
if (littleEndian)
value = byte_swap<big_endian, little_endian>(value);
#endif
DATAVIEW_TYPED_SET(uint32_t);
}
void DataView::setFloat32(long index, float value, bool littleEndian) {
#ifdef BOOST_LITTLE_ENDIAN
if (!littleEndian)
value = byte_swap<little_endian, big_endian>(value);
#else
if (littleEndian)
value = byte_swap<big_endian, little_endian>(value);
#endif
DATAVIEW_TYPED_SET(float);
}
void DataView::setFloat64(long index, double value, bool littleEndian) {
#ifdef BOOST_LITTLE_ENDIAN
if (!littleEndian)
value = byte_swap<little_endian, big_endian>(value);
#else
if (littleEndian)
value = byte_swap<big_endian, little_endian>(value);
#endif
DATAVIEW_TYPED_SET(double);
}
}
|