1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | "use strict";
|
11 |
|
12 | function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
13 |
|
14 | Object.defineProperty(exports, "__esModule", {
|
15 | value: true
|
16 | });
|
17 | exports["default"] = void 0;
|
18 |
|
19 | var _long = _interopRequireDefault(require("@xtuc/long"));
|
20 |
|
21 | var bits = _interopRequireWildcard(require("./bits"));
|
22 |
|
23 | var bufs = _interopRequireWildcard(require("./bufs"));
|
24 |
|
25 | function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
26 |
|
27 | function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
28 |
|
29 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | var MIN_INT32 = -0x80000000;
|
37 |
|
38 |
|
39 | var MAX_INT32 = 0x7fffffff;
|
40 |
|
41 |
|
42 | var MAX_UINT32 = 0xffffffff;
|
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 | function signedBitCount(buffer) {
|
81 | return bits.highOrder(bits.getSign(buffer) ^ 1, buffer) + 2;
|
82 | }
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | function unsignedBitCount(buffer) {
|
102 | var result = bits.highOrder(1, buffer) + 1;
|
103 | return result ? result : 1;
|
104 | }
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 | function encodeBufferCommon(buffer, signed) {
|
112 | var signBit;
|
113 | var bitCount;
|
114 |
|
115 | if (signed) {
|
116 | signBit = bits.getSign(buffer);
|
117 | bitCount = signedBitCount(buffer);
|
118 | } else {
|
119 | signBit = 0;
|
120 | bitCount = unsignedBitCount(buffer);
|
121 | }
|
122 |
|
123 | var byteCount = Math.ceil(bitCount / 7);
|
124 | var result = bufs.alloc(byteCount);
|
125 |
|
126 | for (var i = 0; i < byteCount; i++) {
|
127 | var payload = bits.extract(buffer, i * 7, 7, signBit);
|
128 | result[i] = payload | 0x80;
|
129 | }
|
130 |
|
131 |
|
132 |
|
133 | result[byteCount - 1] &= 0x7f;
|
134 | return result;
|
135 | }
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 | function encodedLength(encodedBuffer, index) {
|
143 | var result = 0;
|
144 |
|
145 | while (encodedBuffer[index + result] >= 0x80) {
|
146 | result++;
|
147 | }
|
148 |
|
149 | result++;
|
150 |
|
151 | if (index + result > encodedBuffer.length) {
|
152 |
|
153 | }
|
154 |
|
155 | return result;
|
156 | }
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 | function decodeBufferCommon(encodedBuffer, index, signed) {
|
164 | index = index === undefined ? 0 : index;
|
165 | var length = encodedLength(encodedBuffer, index);
|
166 | var bitLength = length * 7;
|
167 | var byteLength = Math.ceil(bitLength / 8);
|
168 | var result = bufs.alloc(byteLength);
|
169 | var outIndex = 0;
|
170 |
|
171 | while (length > 0) {
|
172 | bits.inject(result, outIndex, 7, encodedBuffer[index]);
|
173 | outIndex += 7;
|
174 | index++;
|
175 | length--;
|
176 | }
|
177 |
|
178 | var signBit;
|
179 | var signByte;
|
180 |
|
181 | if (signed) {
|
182 |
|
183 | var lastByte = result[byteLength - 1];
|
184 | var endBit = outIndex % 8;
|
185 |
|
186 | if (endBit !== 0) {
|
187 | var shift = 32 - endBit;
|
188 |
|
189 | lastByte = result[byteLength - 1] = lastByte << shift >> shift & 0xff;
|
190 | }
|
191 |
|
192 | signBit = lastByte >> 7;
|
193 | signByte = signBit * 0xff;
|
194 | } else {
|
195 | signBit = 0;
|
196 | signByte = 0;
|
197 | }
|
198 |
|
199 |
|
200 |
|
201 | while (byteLength > 1 && result[byteLength - 1] === signByte && (!signed || result[byteLength - 2] >> 7 === signBit)) {
|
202 | byteLength--;
|
203 | }
|
204 |
|
205 | result = bufs.resize(result, byteLength);
|
206 | return {
|
207 | value: result,
|
208 | nextIndex: index
|
209 | };
|
210 | }
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 | function encodeIntBuffer(buffer) {
|
217 | return encodeBufferCommon(buffer, true);
|
218 | }
|
219 |
|
220 | function decodeIntBuffer(encodedBuffer, index) {
|
221 | return decodeBufferCommon(encodedBuffer, index, true);
|
222 | }
|
223 |
|
224 | function encodeInt32(num) {
|
225 | var buf = new Uint8Array(4);
|
226 | buf[0] = num & 0xff;
|
227 | buf[1] = num >> 8 & 0xff;
|
228 | buf[2] = num >> 16 & 0xff;
|
229 | buf[3] = num >> 24 & 0xff;
|
230 | var result = encodeIntBuffer(buf);
|
231 | return result;
|
232 | }
|
233 |
|
234 | function decodeInt32(encodedBuffer, index) {
|
235 | var result = decodeIntBuffer(encodedBuffer, index);
|
236 | var parsed = bufs.readInt(result.value);
|
237 | var value = parsed.value;
|
238 | bufs.free(result.value);
|
239 |
|
240 | if (value < MIN_INT32 || value > MAX_INT32) {
|
241 | throw new Error("integer too large");
|
242 | }
|
243 |
|
244 | return {
|
245 | value: value,
|
246 | nextIndex: result.nextIndex
|
247 | };
|
248 | }
|
249 |
|
250 | function encodeInt64(num) {
|
251 | var buf = bufs.alloc(8);
|
252 | bufs.writeInt64(num, buf);
|
253 | var result = encodeIntBuffer(buf);
|
254 | bufs.free(buf);
|
255 | return result;
|
256 | }
|
257 |
|
258 | function decodeInt64(encodedBuffer, index) {
|
259 | var result = decodeIntBuffer(encodedBuffer, index);
|
260 |
|
261 | var length = result.value.length;
|
262 |
|
263 | if (result.value[length - 1] >> 7) {
|
264 | result.value = bufs.resize(result.value, 8);
|
265 | result.value.fill(255, length);
|
266 | }
|
267 |
|
268 | var value = _long["default"].fromBytesLE(result.value, false);
|
269 |
|
270 | bufs.free(result.value);
|
271 | return {
|
272 | value: value,
|
273 | nextIndex: result.nextIndex,
|
274 | lossy: false
|
275 | };
|
276 | }
|
277 |
|
278 | function encodeUIntBuffer(buffer) {
|
279 | return encodeBufferCommon(buffer, false);
|
280 | }
|
281 |
|
282 | function decodeUIntBuffer(encodedBuffer, index) {
|
283 | return decodeBufferCommon(encodedBuffer, index, false);
|
284 | }
|
285 |
|
286 | function encodeUInt32(num) {
|
287 | var buf = new Uint8Array(4);
|
288 | buf[0] = num & 0xff;
|
289 | buf[1] = num >> 8 & 0xff;
|
290 | buf[2] = num >> 16 & 0xff;
|
291 | buf[3] = num >> 24 & 0xff;
|
292 | var result = encodeUIntBuffer(buf);
|
293 | return result;
|
294 | }
|
295 |
|
296 | function decodeUInt32(encodedBuffer, index) {
|
297 | var result = decodeUIntBuffer(encodedBuffer, index);
|
298 | var parsed = bufs.readUInt(result.value);
|
299 | var value = parsed.value;
|
300 | bufs.free(result.value);
|
301 |
|
302 | if (value > MAX_UINT32) {
|
303 | throw new Error("integer too large");
|
304 | }
|
305 |
|
306 | return {
|
307 | value: value,
|
308 | nextIndex: result.nextIndex
|
309 | };
|
310 | }
|
311 |
|
312 | function encodeUInt64(num) {
|
313 | var buf = bufs.alloc(8);
|
314 | bufs.writeUInt64(num, buf);
|
315 | var result = encodeUIntBuffer(buf);
|
316 | bufs.free(buf);
|
317 | return result;
|
318 | }
|
319 |
|
320 | function decodeUInt64(encodedBuffer, index) {
|
321 | var result = decodeUIntBuffer(encodedBuffer, index);
|
322 |
|
323 | var value = _long["default"].fromBytesLE(result.value, true);
|
324 |
|
325 | bufs.free(result.value);
|
326 | return {
|
327 | value: value,
|
328 | nextIndex: result.nextIndex,
|
329 | lossy: false
|
330 | };
|
331 | }
|
332 |
|
333 | var _default = {
|
334 | decodeInt32: decodeInt32,
|
335 | decodeInt64: decodeInt64,
|
336 | decodeIntBuffer: decodeIntBuffer,
|
337 | decodeUInt32: decodeUInt32,
|
338 | decodeUInt64: decodeUInt64,
|
339 | decodeUIntBuffer: decodeUIntBuffer,
|
340 | encodeInt32: encodeInt32,
|
341 | encodeInt64: encodeInt64,
|
342 | encodeIntBuffer: encodeIntBuffer,
|
343 | encodeUInt32: encodeUInt32,
|
344 | encodeUInt64: encodeUInt64,
|
345 | encodeUIntBuffer: encodeUIntBuffer
|
346 | };
|
347 | exports["default"] = _default; |
\ | No newline at end of file |