Debian Codegraph @

node/@bufbuild/protobuf/dist/commonjs/wire/varint.js

raw
1 "use strict";
2 // Copyright 2008 Google Inc. All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Code generated by the Protocol Buffer compiler is owned by the owner
31 // of the input file used when generating it. This code is not
32 // standalone and requires a support library to be linked with it. This
33 // support library is itself covered by the above license.
34 Object.defineProperty(exports, "__esModule", { value: true });
35 exports.varint64read = varint64read;
36 exports.varint64write = varint64write;
37 exports.int64FromString = int64FromString;
38 exports.int64ToString = int64ToString;
39 exports.uInt64ToString = uInt64ToString;
40 exports.varint32write = varint32write;
41 exports.varint32read = varint32read;
42 /**
43 * Read a 64 bit varint as two JS numbers.
44 *
45 * Stores the low and high words on the reader.
46 *
47 * Copyright 2008 Google Inc. All rights reserved.
48 *
49 * See https://github.com/protocolbuffers/protobuf/blob/8a71927d74a4ce34efe2d8769fda198f52d20d12/js/experimental/runtime/kernel/buffer_decoder.js#L175
50 */
51 function varint64read() {
52 const buf = this.buf;
53 let pos = this.pos;
54 let lo = 0;
55 let hi = 0;
56 for (let shift = 0; shift < 28; shift += 7) {
57 const b = buf[pos++];
58 lo |= (b & 0x7f) << shift;
59 if ((b & 0x80) == 0) {
60 this.pos = pos;
61 this.assertBounds();
62 this.varint64Lo = lo;
63 this.varint64Hi = hi;
64 return;
65 }
66 }
67 const middleByte = buf[pos++];
68 // last four bits of the first 32 bit number
69 lo |= (middleByte & 0x0f) << 28;
70 // 3 upper bits are part of the next 32 bit number
71 hi = (middleByte & 0x70) >> 4;
72 if ((middleByte & 0x80) == 0) {
73 this.pos = pos;
74 this.assertBounds();
75 this.varint64Lo = lo;
76 this.varint64Hi = hi;
77 return;
78 }
79 for (let shift = 3; shift <= 31; shift += 7) {
80 const b = buf[pos++];
81 hi |= (b & 0x7f) << shift;
82 if ((b & 0x80) == 0) {
83 this.pos = pos;
84 this.assertBounds();
85 this.varint64Lo = lo;
86 this.varint64Hi = hi;
87 return;
88 }
89 }
90 throw new Error("invalid varint");
91 }
92 /**
93 * Write a 64 bit varint, given as two JS numbers, to the given bytes array.
94 *
95 * Copyright 2008 Google Inc. All rights reserved.
96 *
97 * See https://github.com/protocolbuffers/protobuf/blob/8a71927d74a4ce34efe2d8769fda198f52d20d12/js/experimental/runtime/kernel/writer.js#L344
98 */
99 function varint64write(lo, hi, bytes) {
100 for (let i = 0; i < 28; i = i + 7) {
101 const shift = lo >>> i;
102 const hasNext = !(shift >>> 7 == 0 && hi == 0);
103 const byte = (hasNext ? shift | 0x80 : shift) & 0xff;
104 bytes.push(byte);
105 if (!hasNext) {
106 return;
107 }
108 }
109 const splitBits = ((lo >>> 28) & 0x0f) | ((hi & 0x07) << 4);
110 const hasMoreBits = !(hi >> 3 == 0);
111 bytes.push((hasMoreBits ? splitBits | 0x80 : splitBits) & 0xff);
112 if (!hasMoreBits) {
113 return;
114 }
115 for (let i = 3; i < 31; i = i + 7) {
116 const shift = hi >>> i;
117 const hasNext = !(shift >>> 7 == 0);
118 const byte = (hasNext ? shift | 0x80 : shift) & 0xff;
119 bytes.push(byte);
120 if (!hasNext) {
121 return;
122 }
123 }
124 bytes.push((hi >>> 31) & 0x01);
125 }
126 // constants for binary math
127 const TWO_PWR_32_DBL = 0x100000000;
128 /**
129 * Parse decimal string of 64 bit integer value as two JS numbers.
130 *
131 * Copyright 2008 Google Inc. All rights reserved.
132 *
133 * See https://github.com/protocolbuffers/protobuf-javascript/blob/a428c58273abad07c66071d9753bc4d1289de426/experimental/runtime/int64.js#L10
134 */
135 function int64FromString(dec) {
136 // Check for minus sign.
137 const minus = dec[0] === "-";
138 if (minus) {
139 dec = dec.slice(1);
140 }
141 // Work 6 decimal digits at a time, acting like we're converting base 1e6
142 // digits to binary. This is safe to do with floating point math because
143 // Number.isSafeInteger(ALL_32_BITS * 1e6) == true.
144 const base = 1e6;
145 let lowBits = 0;
146 let highBits = 0;
147 function add1e6digit(begin, end) {
148 // Note: Number('') is 0.
149 const digit1e6 = Number(dec.slice(begin, end));
150 highBits *= base;
151 lowBits = lowBits * base + digit1e6;
152 // Carry bits from lowBits to
153 if (lowBits >= TWO_PWR_32_DBL) {
154 highBits = highBits + ((lowBits / TWO_PWR_32_DBL) | 0);
155 lowBits = lowBits % TWO_PWR_32_DBL;
156 }
157 }
158 add1e6digit(-24, -18);
159 add1e6digit(-18, -12);
160 add1e6digit(-12, -6);
161 add1e6digit(-6);
162 return minus ? negate(lowBits, highBits) : newBits(lowBits, highBits);
163 }
164 /**
165 * Losslessly converts a 64-bit signed integer in 32:32 split representation
166 * into a decimal string.
167 *
168 * Copyright 2008 Google Inc. All rights reserved.
169 *
170 * See https://github.com/protocolbuffers/protobuf-javascript/blob/a428c58273abad07c66071d9753bc4d1289de426/experimental/runtime/int64.js#L10
171 */
172 function int64ToString(lo, hi) {
173 let bits = newBits(lo, hi);
174 // If we're treating the input as a signed value and the high bit is set, do
175 // a manual two's complement conversion before the decimal conversion.
176 const negative = bits.hi & 0x80000000;
177 if (negative) {
178 bits = negate(bits.lo, bits.hi);
179 }
180 const result = uInt64ToString(bits.lo, bits.hi);
181 return negative ? "-" + result : result;
182 }
183 /**
184 * Losslessly converts a 64-bit unsigned integer in 32:32 split representation
185 * into a decimal string.
186 *
187 * Copyright 2008 Google Inc. All rights reserved.
188 *
189 * See https://github.com/protocolbuffers/protobuf-javascript/blob/a428c58273abad07c66071d9753bc4d1289de426/experimental/runtime/int64.js#L10
190 */
191 function uInt64ToString(lo, hi) {
192 ({ lo, hi } = toUnsigned(lo, hi));
193 // Skip the expensive conversion if the number is small enough to use the
194 // built-in conversions.
195 // Number.MAX_SAFE_INTEGER = 0x001FFFFF FFFFFFFF, thus any number with
196 // highBits <= 0x1FFFFF can be safely expressed with a double and retain
197 // integer precision.
198 // Proven by: Number.isSafeInteger(0x1FFFFF * 2**32 + 0xFFFFFFFF) == true.
199 if (hi <= 0x1fffff) {
200 return String(TWO_PWR_32_DBL * hi + lo);
201 }
202 // What this code is doing is essentially converting the input number from
203 // base-2 to base-1e7, which allows us to represent the 64-bit range with
204 // only 3 (very large) digits. Those digits are then trivial to convert to
205 // a base-10 string.
206 // The magic numbers used here are -
207 // 2^24 = 16777216 = (1,6777216) in base-1e7.
208 // 2^48 = 281474976710656 = (2,8147497,6710656) in base-1e7.
209 // Split 32:32 representation into 16:24:24 representation so our
210 // intermediate digits don't overflow.
211 const low = lo & 0xffffff;
212 const mid = ((lo >>> 24) | (hi << 8)) & 0xffffff;
213 const high = (hi >> 16) & 0xffff;
214 // Assemble our three base-1e7 digits, ignoring carries. The maximum
215 // value in a digit at this step is representable as a 48-bit integer, which
216 // can be stored in a 64-bit floating point number.
217 let digitA = low + mid * 6777216 + high * 6710656;
218 let digitB = mid + high * 8147497;
219 let digitC = high * 2;
220 // Apply carries from A to B and from B to C.
221 const base = 10000000;
222 if (digitA >= base) {
223 digitB += Math.floor(digitA / base);
224 digitA %= base;
225 }
226 if (digitB >= base) {
227 digitC += Math.floor(digitB / base);
228 digitB %= base;
229 }
230 // If digitC is 0, then we should have returned in the trivial code path
231 // at the top for non-safe integers. Given this, we can assume both digitB
232 // and digitA need leading zeros.
233 return (digitC.toString() +
234 decimalFrom1e7WithLeadingZeros(digitB) +
235 decimalFrom1e7WithLeadingZeros(digitA));
236 }
237 function toUnsigned(lo, hi) {
238 return { lo: lo >>> 0, hi: hi >>> 0 };
239 }
240 function newBits(lo, hi) {
241 return { lo: lo | 0, hi: hi | 0 };
242 }
243 /**
244 * Returns two's compliment negation of input.
245 * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Signed_32-bit_integers
246 */
247 function negate(lowBits, highBits) {
248 highBits = ~highBits;
249 if (lowBits) {
250 lowBits = ~lowBits + 1;
251 }
252 else {
253 // If lowBits is 0, then bitwise-not is 0xFFFFFFFF,
254 // adding 1 to that, results in 0x100000000, which leaves
255 // the low bits 0x0 and simply adds one to the high bits.
256 highBits += 1;
257 }
258 return newBits(lowBits, highBits);
259 }
260 /**
261 * Returns decimal representation of digit1e7 with leading zeros.
262 */
263 const decimalFrom1e7WithLeadingZeros = (digit1e7) => {
264 const partial = String(digit1e7);
265 return "0000000".slice(partial.length) + partial;
266 };
267 /**
268 * Write a 32 bit varint, signed or unsigned. Same as `varint64write(0, value, bytes)`
269 *
270 * Copyright 2008 Google Inc. All rights reserved.
271 *
272 * See https://github.com/protocolbuffers/protobuf/blob/1b18833f4f2a2f681f4e4a25cdf3b0a43115ec26/js/binary/encoder.js#L144
273 */
274 function varint32write(value, bytes) {
275 if (value >>> 0 < 0x80) {
276 bytes.push(value);
277 return;
278 }
279 if (value >= 0) {
280 // write value as varint 32
281 while (value > 0x7f) {
282 bytes.push((value & 0x7f) | 0x80);
283 value = value >>> 7;
284 }
285 bytes.push(value);
286 }
287 else {
288 for (let i = 0; i < 9; i++) {
289 bytes.push((value & 127) | 128);
290 value = value >> 7;
291 }
292 bytes.push(1);
293 }
294 }
295 /**
296 * Read an unsigned 32 bit varint.
297 *
298 * See https://github.com/protocolbuffers/protobuf/blob/8a71927d74a4ce34efe2d8769fda198f52d20d12/js/experimental/runtime/kernel/buffer_decoder.js#L220
299 */
300 function varint32read() {
301 let b = this.buf[this.pos++];
302 if ((b & 0x80) === 0) {
303 this.assertBounds();
304 return b;
305 }
306 let result = b & 0x7f;
307 b = this.buf[this.pos++];
308 result |= (b & 0x7f) << 7;
309 if ((b & 0x80) === 0) {
310 this.assertBounds();
311 return result;
312 }
313 b = this.buf[this.pos++];
314 result |= (b & 0x7f) << 14;
315 if ((b & 0x80) === 0) {
316 this.assertBounds();
317 return result;
318 }
319 b = this.buf[this.pos++];
320 result |= (b & 0x7f) << 21;
321 if ((b & 0x80) === 0) {
322 this.assertBounds();
323 return result;
324 }
325 // Extract only last 4 bits
326 b = this.buf[this.pos++];
327 result |= (b & 0x0f) << 28;
328 for (let readBytes = 5; (b & 0x80) !== 0 && readBytes < 10; readBytes++)
329 b = this.buf[this.pos++];
330 if ((b & 0x80) !== 0)
331 throw new Error("invalid varint");
332 this.assertBounds();
333 return result >>> 0;
334 }