1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.compare = exports.equal = exports.readUInt32BE = exports.readUInt16BE = exports.writeUInt32BE = exports.writeUInt16BE = exports.writeUInt8 = void 0;
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | function writeUInt8(array, value, offset) {
|
11 | value = Number(value);
|
12 | array[offset] = value;
|
13 | }
|
14 | exports.writeUInt8 = writeUInt8;
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | function writeUInt16BE(array, value, offset) {
|
22 | value = Number(value);
|
23 | array[offset] = value >>> 8;
|
24 | array[offset + 1] = value;
|
25 | }
|
26 | exports.writeUInt16BE = writeUInt16BE;
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | function 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 | }
|
39 | exports.writeUInt32BE = writeUInt32BE;
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | function readUInt16BE(array, offset) {
|
46 | return new DataView(array.buffer).getUint16(offset, false).toString(10);
|
47 | }
|
48 | exports.readUInt16BE = readUInt16BE;
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | function readUInt32BE(array, offset) {
|
55 | return new DataView(array.buffer).getUint32(offset, false).toString(10);
|
56 | }
|
57 | exports.readUInt32BE = readUInt32BE;
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 | function 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 | }
|
74 | exports.equal = equal;
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | function 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 |
|
87 |
|
88 |
|
89 |
|
90 | function 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 |
|
97 |
|
98 |
|
99 |
|
100 | function 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 |
|
107 |
|
108 |
|
109 |
|
110 | function 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 | }
|
122 | exports.compare = compare;
|
123 |
|
124 |
|
125 |
|
126 |
|
127 | function aligned16(array) {
|
128 | return array.byteOffset % 2 === 0 && array.byteLength % 2 === 0;
|
129 | }
|
130 |
|
131 |
|
132 |
|
133 |
|
134 | function aligned32(array) {
|
135 | return array.byteOffset % 4 === 0 && array.byteLength % 4 === 0;
|
136 | }
|
137 |
|
\ | No newline at end of file |