-
Notifications
You must be signed in to change notification settings - Fork 0
/
endianness.ts
327 lines (323 loc) · 8.87 KB
/
endianness.ts
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
326
327
import { ENDIAN_TEST } from "./endianness_internal.ts";
import type { BufferType, TypedArray } from "./internal.ts";
import { bufferToDataView } from "./internal.ts";
/**
* Inspect if the byte ordering of this process is big endian.
*
* @returns true if the process uses big endian memory order
*/
export function hostIsBigEndian(): boolean {
return ENDIAN_TEST[1] != 0;
}
/**
* Inspect if the byte ordering of this process is little endian.
*
* @returns true if the process uses little endian memory order
*/
export function hostIsLittleEndian(): boolean {
return ENDIAN_TEST[0] != 0;
}
/**
* Inspect if the byte ordering of this process is big or little endian.
*
* @returns "big" if the system is big endian, otherwise "little".
*/
export function hostEndianness(): "little" | "big" {
return hostIsBigEndian() ? "big" : "little";
}
function internalArrayTo(
array: TypedArray,
endianness: "little" | "big",
width: 2 | 4 | 8,
fun: (v: DataView, i: number, n: number | bigint, l: boolean) => void,
): Uint8Array {
const out = new Uint8Array(array.byteLength);
const view = new DataView(out.buffer);
const length = array.length;
const little = endianness == "little";
for (let i = 0, outIndex = 0; i < length; i++, outIndex += width) {
fun(view, outIndex, array[i], little);
}
return out;
}
function internalArrayFrom(
view: DataView,
littleEndian: boolean,
width: 1 | 2 | 4 | 8,
output: TypedArray,
fun: (v: DataView, i: number, l: boolean) => number | bigint,
): TypedArray {
for (let i = 0, j = 0; i < view.byteLength; j++, i += width) {
output[j] = fun(view, i, littleEndian);
}
return output;
}
/**
* This function takes a typed input array and encodes all elements in the
* requested endianness order, so that the input array may be communicated
* in a reliable way to another recipient.
*
* Protocols may specify that numbers are encoded in "big" endian, while the
* host uses "little" endian byte ordering in memory. This function may help
* in ensuring that sequences of multi-byte numbers are portable between
* processes.
*
* @param array Input array to encode
* @param endianness what endianness to encode the input array as
* @returns an output Uint8Array with bytes set in the requested endianness
* order which represents the multi-byte numbers in the input array.
*/
export function arrayToEndian(
array: TypedArray,
endianness: "little" | "big",
): Uint8Array {
if (array instanceof Uint8Array) {
return array;
} else if (array instanceof Uint16Array) {
return internalArrayTo(
array,
endianness,
2,
(v, i, n, l) => v.setUint16(i, n as number, l),
);
} else if (array instanceof Int16Array) {
return internalArrayTo(
array,
endianness,
2,
(v, i, n, l) => v.setInt16(i, n as number, l),
);
} else if (array instanceof Uint32Array) {
return internalArrayTo(
array,
endianness,
4,
(v, i, n, l) => v.setUint32(i, n as number, l),
);
} else if (array instanceof Int32Array) {
return internalArrayTo(
array,
endianness,
4,
(v, i, n, l) => v.setInt32(i, n as number, l),
);
} else if (array instanceof BigUint64Array) {
return internalArrayTo(
array,
endianness,
8,
(v, i, n, l) => v.setBigUint64(i, n as bigint, l),
);
} else if (array instanceof BigInt64Array) {
return internalArrayTo(
array,
endianness,
8,
(v, i, n, l) => v.setBigInt64(i, n as bigint, l),
);
} else if (array instanceof Float32Array) {
return internalArrayTo(
array,
endianness,
4,
(v, i, n, l) => v.setFloat32(i, n as number, l),
);
} else if (array instanceof Float64Array) {
return internalArrayTo(
array,
endianness,
8,
(v, i, n, l) => v.setFloat64(i, n as number, l),
);
}
// Int8Array
return new Uint8Array(array.buffer);
}
/**
* This function receives a sequence of bytes, which is known to contain
* multi-byte numbers encoded with a known endianness, and outputs a typed
* array of those multi-byte numbers in the requested format / type.
*
* Will throw if the input array is not sized for the type that is asked for.
* If you are processing a section of data in a larger sequence of bytes then
* use a DataView.
*
* @param array Source bytes to read from, various types are supported, the
* bytes will be read as they are stored in memory. While it can
* take a Uint32Array type, this is not advised.
* @param endianness the endianness of bytes, most protocols use "big",
* while most systems use "little".
* @param type the output type desired, for example "uint32" will
* return a Uint32Array.
* @returns a typed array populated with the numbers sourced from the input
* array with the endianness taken into account
*/
export function arrayFromEndian(
view: BufferType,
endianness: "little" | "big",
type: "uint8",
): Uint8Array;
export function arrayFromEndian(
view: BufferType,
endianness: "little" | "big",
type: "int8",
): Int8Array;
export function arrayFromEndian(
view: BufferType,
endianness: "little" | "big",
type: "uint16",
): Uint16Array;
export function arrayFromEndian(
view: BufferType,
endianness: "little" | "big",
type: "int16",
): Int16Array;
export function arrayFromEndian(
view: BufferType,
endianness: "little" | "big",
type: "uint32",
): Uint32Array;
export function arrayFromEndian(
view: BufferType,
endianness: "little" | "big",
type: "int32",
): Int32Array;
export function arrayFromEndian(
view: BufferType,
endianness: "little" | "big",
type: "uint64",
): BigUint64Array;
export function arrayFromEndian(
view: BufferType,
endianness: "little" | "big",
type: "int64",
): BigInt64Array;
export function arrayFromEndian(
view: BufferType,
endianness: "little" | "big",
type: "float32",
): Float32Array;
export function arrayFromEndian(
view: BufferType,
endianness: "little" | "big",
type: "float64",
): Float64Array;
export function arrayFromEndian(
array: BufferType,
endianness: "little" | "big",
type:
| "uint8"
| "int8"
| "uint16"
| "int16"
| "uint32"
| "int32"
| "uint64"
| "int64"
| "float32"
| "float64",
): TypedArray {
const view = bufferToDataView(array);
const littleEndian = endianness == "little";
if (type == "int8") {
return internalArrayFrom(
view,
littleEndian,
1,
new Int8Array(view.byteLength),
(v, i, _l) => v.getInt8(i),
);
} else if (type == "uint16") {
if (view.byteLength & 1) {
throw new Error("Incomplete byte sequence");
}
return internalArrayFrom(
view,
littleEndian,
2,
new Uint16Array(view.byteLength >>> 1),
(v, i, l) => v.getUint16(i, l),
);
} else if (type == "int16") {
if (view.byteLength & 1) {
throw new Error("Incomplete byte sequence");
}
return internalArrayFrom(
view,
littleEndian,
2,
new Int16Array(view.byteLength >>> 1),
(v, i, l) => v.getInt16(i, l),
);
} else if (type == "uint32") {
if (view.byteLength & 3) {
throw new Error("Incomplete byte sequence");
}
return internalArrayFrom(
view,
littleEndian,
4,
new Uint32Array(view.byteLength >>> 2),
(v, i, l) => v.getUint32(i, l),
);
} else if (type == "int32") {
if (view.byteLength & 3) {
throw new Error("Incomplete byte sequence");
}
return internalArrayFrom(
view,
littleEndian,
4,
new Int32Array(view.byteLength >>> 2),
(v, i, l) => v.getInt32(i, l),
);
} else if (type == "uint64") {
if (view.byteLength & 7) {
throw new Error("Incomplete byte sequence");
}
return internalArrayFrom(
view,
littleEndian,
8,
new BigUint64Array(view.byteLength >>> 3),
(v, i, l) => v.getBigUint64(i, l),
);
} else if (type == "int64") {
if (view.byteLength & 7) {
throw new Error("Incomplete byte sequence");
}
return internalArrayFrom(
view,
littleEndian,
8,
new BigInt64Array(view.byteLength >>> 3),
(v, i, l) => v.getBigInt64(i, l),
);
} else if (type == "float32") {
if (view.byteLength & 3) {
throw new Error("Incomplete byte sequence");
}
return internalArrayFrom(
view,
littleEndian,
4,
new Float32Array(view.byteLength >>> 2),
(v, i, l) => v.getFloat32(i, l),
);
} else if (type == "float64") {
if (view.byteLength & 7) {
throw new Error("Incomplete byte sequence");
}
return internalArrayFrom(
view,
littleEndian,
8,
new Float64Array(view.byteLength >>> 3),
(v, i, l) => v.getFloat64(i, l),
);
}
// Last is uint8
return new Uint8Array(
view.buffer.slice(view.byteOffset, view.byteOffset + view.byteLength),
);
}