1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.alloc = alloc;
|
7 | exports.free = free;
|
8 | exports.resize = resize;
|
9 | exports.readInt = readInt;
|
10 | exports.readUInt = readUInt;
|
11 | exports.writeInt64 = writeInt64;
|
12 | exports.writeUInt64 = writeUInt64;
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | var bufPool = [];
|
25 |
|
26 |
|
27 | var TEMP_BUF_MAXIMUM_LENGTH = 20;
|
28 |
|
29 |
|
30 | var MIN_EXACT_INT64 = -0x8000000000000000;
|
31 |
|
32 |
|
33 | var MAX_EXACT_INT64 = 0x7ffffffffffffc00;
|
34 |
|
35 |
|
36 | var MAX_EXACT_UINT64 = 0xfffffffffffff800;
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | var BIT_32 = 0x100000000;
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | var BIT_64 = 0x10000000000000000;
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | function lowestBit(num) {
|
58 | return num & -num;
|
59 | }
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | function isLossyToAdd(accum, num) {
|
67 | if (num === 0) {
|
68 | return false;
|
69 | }
|
70 |
|
71 | var lowBit = lowestBit(num);
|
72 | var added = accum + lowBit;
|
73 |
|
74 | if (added === accum) {
|
75 | return true;
|
76 | }
|
77 |
|
78 | if (added - lowBit !== accum) {
|
79 | return true;
|
80 | }
|
81 |
|
82 | return false;
|
83 | }
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | function alloc(length) {
|
96 | var result = bufPool[length];
|
97 |
|
98 | if (result) {
|
99 | bufPool[length] = undefined;
|
100 | } else {
|
101 | result = new Uint8Array(length);
|
102 | }
|
103 |
|
104 | result.fill(0);
|
105 | return result;
|
106 | }
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | function free(buffer) {
|
113 | var length = buffer.length;
|
114 |
|
115 | if (length < TEMP_BUF_MAXIMUM_LENGTH) {
|
116 | bufPool[length] = buffer;
|
117 | }
|
118 | }
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 | function resize(buffer, length) {
|
128 | if (length === buffer.length) {
|
129 | return buffer;
|
130 | }
|
131 |
|
132 | var newBuf = alloc(length);
|
133 |
|
134 | for (var i = 0; i <= buffer.length; i++) {
|
135 | newBuf[i] = buffer[i];
|
136 | }
|
137 |
|
138 | free(buffer);
|
139 | return newBuf;
|
140 | }
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 | function readInt(buffer) {
|
147 | var length = buffer.length;
|
148 | var positive = buffer[length - 1] < 0x80;
|
149 | var result = positive ? 0 : -1;
|
150 | var lossy = false;
|
151 |
|
152 |
|
153 | if (length < 7) {
|
154 |
|
155 |
|
156 | for (var i = length - 1; i >= 0; i--) {
|
157 | result = result * 0x100 + buffer[i];
|
158 | }
|
159 | } else {
|
160 | for (var _i = length - 1; _i >= 0; _i--) {
|
161 | var one = buffer[_i];
|
162 | result *= 0x100;
|
163 |
|
164 | if (isLossyToAdd(result, one)) {
|
165 | lossy = true;
|
166 | }
|
167 |
|
168 | result += one;
|
169 | }
|
170 | }
|
171 |
|
172 | return {
|
173 | value: result,
|
174 | lossy: lossy
|
175 | };
|
176 | }
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 | function readUInt(buffer) {
|
183 | var length = buffer.length;
|
184 | var result = 0;
|
185 | var lossy = false;
|
186 |
|
187 | if (length < 7) {
|
188 |
|
189 | for (var i = length - 1; i >= 0; i--) {
|
190 | result = result * 0x100 + buffer[i];
|
191 | }
|
192 | } else {
|
193 | for (var _i2 = length - 1; _i2 >= 0; _i2--) {
|
194 | var one = buffer[_i2];
|
195 | result *= 0x100;
|
196 |
|
197 | if (isLossyToAdd(result, one)) {
|
198 | lossy = true;
|
199 | }
|
200 |
|
201 | result += one;
|
202 | }
|
203 | }
|
204 |
|
205 | return {
|
206 | value: result,
|
207 | lossy: lossy
|
208 | };
|
209 | }
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 | function writeInt64(value, buffer) {
|
216 | if (value < MIN_EXACT_INT64 || value > MAX_EXACT_INT64) {
|
217 | throw new Error("Value out of range.");
|
218 | }
|
219 |
|
220 | if (value < 0) {
|
221 | value += BIT_64;
|
222 | }
|
223 |
|
224 | writeUInt64(value, buffer);
|
225 | }
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 | function writeUInt64(value, buffer) {
|
232 | if (value < 0 || value > MAX_EXACT_UINT64) {
|
233 | throw new Error("Value out of range.");
|
234 | }
|
235 |
|
236 | var lowWord = value % BIT_32;
|
237 | var highWord = Math.floor(value / BIT_32);
|
238 | buffer[0] = lowWord & 0xff;
|
239 | buffer[1] = lowWord >> 8 & 0xff;
|
240 | buffer[2] = lowWord >> 16 & 0xff;
|
241 | buffer[3] = lowWord >> 24 & 0xff;
|
242 | buffer[4] = highWord & 0xff;
|
243 | buffer[5] = highWord >> 8 & 0xff;
|
244 | buffer[6] = highWord >> 16 & 0xff;
|
245 | buffer[7] = highWord >> 24 & 0xff;
|
246 | } |
\ | No newline at end of file |