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