-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathbigEndianByteArrayParser.js
More file actions
173 lines (147 loc) · 5.33 KB
/
bigEndianByteArrayParser.js
File metadata and controls
173 lines (147 loc) · 5.33 KB
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
import { overflowErrorMessage, underflowErrorMessage } from './util/errors.js';
/**
* Internal helper functions for parsing different types from a big-endian byte array
*/
export default {
/**
*
* Parses an unsigned int 16 from a big-endian byte array
*
* @param byteArray the byte array to read from
* @param position the position in the byte array to read from
* @returns {number} the parsed unsigned int 16
* @throws {RangeError} tried to read outside the buffer
* @access private
*/
readUint16 (byteArray, position) {
if (position < 0) {
throw new RangeError(underflowErrorMessage);
}
if (position + 2 > byteArray.length) {
throw new RangeError(overflowErrorMessage);
}
return (byteArray[position] << 8) + byteArray[position + 1];
},
/**
*
* Parses a signed int 16 from a big-endian byte array
*
* @param byteArray the byte array to read from
* @param position the position in the byte array to read from
* @returns {number} the parsed signed int 16
* @throws {RangeError} tried to read outside the buffer
* @access private
*/
readInt16 (byteArray, position) {
if (position < 0) {
throw new RangeError(underflowErrorMessage);
}
if (position + 2 > byteArray.length) {
throw new RangeError(overflowErrorMessage);
}
var int16 = (byteArray[position] << 8) + byteArray[position + 1];
// fix sign
if (int16 & 0x8000) {
int16 = int16 - 0xFFFF - 1;
}
return int16;
},
/**
* Parses an unsigned int 32 from a big-endian byte array
*
* @param byteArray the byte array to read from
* @param position the position in the byte array to read from
* @returns {number} the parsed unsigned int 32
* @throws {RangeError} tried to read outside the buffer
* @access private
*/
readUint32 (byteArray, position) {
if (position < 0) {
throw new RangeError(underflowErrorMessage);
}
if (position + 4 > byteArray.length) {
throw new RangeError(overflowErrorMessage);
}
var uint32 = (256 * (256 * (256 * byteArray[position] +
byteArray[position + 1]) +
byteArray[position + 2]) +
byteArray[position + 3]);
return uint32;
},
/**
* Parses a signed int 32 from a big-endian byte array
*
* @param byteArray the byte array to read from
* @param position the position in the byte array to read from
* @returns {number} the parsed signed int 32
* @throws {RangeError} tried to read outside the buffer
* @access private
*/
readInt32 (byteArray, position) {
if (position < 0) {
throw new RangeError(underflowErrorMessage);
}
if (position + 4 > byteArray.length) {
throw new RangeError(overflowErrorMessage);
}
var int32 = ((byteArray[position] << 24) +
(byteArray[position + 1] << 16) +
(byteArray[position + 2] << 8) +
byteArray[position + 3]);
return int32;
},
/**
* Parses 32-bit float from a big-endian byte array
*
* @param byteArray the byte array to read from
* @param position the position in the byte array to read from
* @returns {number} the parsed 32-bit float
* @throws {RangeError} tried to read outside the buffer
* @access private
*/
readFloat (byteArray, position) {
if (position < 0) {
throw new RangeError(underflowErrorMessage);
}
if (position + 4 > byteArray.length) {
throw new RangeError(overflowErrorMessage);
}
// I am sure there is a better way than this but this should be safe
var byteArrayForParsingFloat = new Uint8Array(4);
byteArrayForParsingFloat[3] = byteArray[position];
byteArrayForParsingFloat[2] = byteArray[position + 1];
byteArrayForParsingFloat[1] = byteArray[position + 2];
byteArrayForParsingFloat[0] = byteArray[position + 3];
var floatArray = new Float32Array(byteArrayForParsingFloat.buffer);
return floatArray[0];
},
/**
* Parses 64-bit float from a big-endian byte array
*
* @param byteArray the byte array to read from
* @param position the position in the byte array to read from
* @returns {number} the parsed 64-bit float
* @throws {RangeError} tried to read outside the buffer
* @access private
*/
readDouble (byteArray, position) {
if (position < 0) {
throw new RangeError(underflowErrorMessage);
}
if (position + 8 > byteArray.length) {
throw new RangeError(overflowErrorMessage);
}
// I am sure there is a better way than this but this should be safe
var byteArrayForParsingFloat = new Uint8Array(8);
byteArrayForParsingFloat[7] = byteArray[position];
byteArrayForParsingFloat[6] = byteArray[position + 1];
byteArrayForParsingFloat[5] = byteArray[position + 2];
byteArrayForParsingFloat[4] = byteArray[position + 3];
byteArrayForParsingFloat[3] = byteArray[position + 4];
byteArrayForParsingFloat[2] = byteArray[position + 5];
byteArrayForParsingFloat[1] = byteArray[position + 6];
byteArrayForParsingFloat[0] = byteArray[position + 7];
var floatArray = new Float64Array(byteArrayForParsingFloat.buffer);
return floatArray[0];
}
};