UNPKG

4.78 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.compare = exports.equal = exports.readUInt32BE = exports.readUInt16BE = exports.writeUInt32BE = exports.writeUInt16BE = exports.writeUInt8 = void 0;
4/**
5 * Writes value to array at the specified offset. The value must be a valid unsigned 8-bit integer.
6 * @param array Uint8Array to be written to
7 * @param value Number to be written to array.
8 * @param offset plus the number of bytes written.
9 */
10function writeUInt8(array, value, offset) {
11 value = Number(value);
12 array[offset] = value;
13}
14exports.writeUInt8 = writeUInt8;
15/**
16 * Writes value to array at the specified offset as big-endian. The value must be a valid unsigned 16-bit integer.
17 * @param array Uint8Array to be written to
18 * @param value Number to be written to array.
19 * @param offset plus the number of bytes written.
20 */
21function writeUInt16BE(array, value, offset) {
22 value = Number(value);
23 array[offset] = value >>> 8;
24 array[offset + 1] = value;
25}
26exports.writeUInt16BE = writeUInt16BE;
27/**
28 * Writes value to array at the specified offset as big-endian. The value must be a valid unsigned 32-bit integer.
29 * @param array Uint8Array to be written to
30 * @param value Number to be written to array.
31 * @param offset plus the number of bytes written.
32 */
33function writeUInt32BE(array, value, offset) {
34 array[offset] = (value >>> 24) & 0xff;
35 array[offset + 1] = (value >>> 16) & 0xff;
36 array[offset + 2] = (value >>> 8) & 0xff;
37 array[offset + 3] = value & 0xff;
38}
39exports.writeUInt32BE = writeUInt32BE;
40/**
41 * Reads an unsigned, big-endian 16-bit integer from the array at the specified offset.
42 * @param array Uint8Array to read
43 * @param offset Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 2
44 */
45function readUInt16BE(array, offset) {
46 return new DataView(array.buffer).getUint16(offset, false).toString(10);
47}
48exports.readUInt16BE = readUInt16BE;
49/**
50 * Reads an unsigned, big-endian 16-bit integer from the array at the specified offset.
51 * @param array Uint8Array to read
52 * @param offset Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 4
53 */
54function readUInt32BE(array, offset) {
55 return new DataView(array.buffer).getUint32(offset, false).toString(10);
56}
57exports.readUInt32BE = readUInt32BE;
58/**
59 * Compares two Uint8Array or ArrayBuffers
60 * @param a first array to compare
61 * @param b second array to compare
62 */
63function equal(a, b) {
64 const aUInt = a instanceof ArrayBuffer ? new Uint8Array(a, 0) : a;
65 const bUInt = b instanceof ArrayBuffer ? new Uint8Array(b, 0) : b;
66 if (aUInt.byteLength != bUInt.byteLength)
67 return false;
68 if (aligned32(aUInt) && aligned32(bUInt))
69 return compare32(aUInt, bUInt) === 0;
70 if (aligned16(aUInt) && aligned16(bUInt))
71 return compare16(aUInt, bUInt) === 0;
72 return compare8(aUInt, bUInt) === 0;
73}
74exports.equal = equal;
75/**
76 * Compares two 8 bit aligned arrays
77 * @param a first array to compare
78 * @param b second array to compare
79 */
80function compare8(a, b) {
81 const ua = new Uint8Array(a.buffer, a.byteOffset, a.byteLength);
82 const ub = new Uint8Array(b.buffer, b.byteOffset, b.byteLength);
83 return compare(ua, ub);
84}
85/**
86 * Compares two 16 bit aligned arrays
87 * @param a first array to compare
88 * @param b second array to compare
89 */
90function compare16(a, b) {
91 const ua = new Uint16Array(a.buffer, a.byteOffset, a.byteLength / 2);
92 const ub = new Uint16Array(b.buffer, b.byteOffset, b.byteLength / 2);
93 return compare(ua, ub);
94}
95/**
96 * Compares two 32 bit aligned arrays
97 * @param a first array to compare
98 * @param b second array to compare
99 */
100function compare32(a, b) {
101 const ua = new Uint32Array(a.buffer, a.byteOffset, a.byteLength / 4);
102 const ub = new Uint32Array(b.buffer, b.byteOffset, b.byteLength / 4);
103 return compare(ua, ub);
104}
105/**
106 * Compare two TypedArrays
107 * @param a first array to compare
108 * @param b second array to compare
109 */
110function compare(a, b) {
111 if (a.byteLength !== b.byteLength) {
112 throw new Error('Cannot compare arrays of different length');
113 }
114 for (let i = 0; i < a.length - 1; i += 1) {
115 if (a[i] > b[i])
116 return 1;
117 if (a[i] < b[i])
118 return -1;
119 }
120 return 0;
121}
122exports.compare = compare;
123/**
124 * Determine if TypedArray is 16 bit aligned
125 * @param array The array to check
126 */
127function aligned16(array) {
128 return array.byteOffset % 2 === 0 && array.byteLength % 2 === 0;
129}
130/**
131 * Determine if TypedArray is 32 bit aligned
132 * @param array The array to check
133 */
134function aligned32(array) {
135 return array.byteOffset % 4 === 0 && array.byteLength % 4 === 0;
136}
137//# sourceMappingURL=utils.js.map
\No newline at end of file