Files
codeql-action/node_modules/@protobuf-ts/runtime/build/commonjs/binary-reader.js
Angela P Wen a196a714b8 Bump artifact dependencies if CODEQL_ACTION_ARTIFACT_V2_UPGRADE enabled (#2482)
Co-authored-by: Andrew Eisenberg <aeisenberg@github.com>
Co-authored-by: Henry Mercer <henrymercer@github.com>
2024-10-01 09:59:05 -07:00

184 lines
5.8 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BinaryReader = exports.binaryReadOptions = void 0;
const binary_format_contract_1 = require("./binary-format-contract");
const pb_long_1 = require("./pb-long");
const goog_varint_1 = require("./goog-varint");
const defaultsRead = {
readUnknownField: true,
readerFactory: bytes => new BinaryReader(bytes),
};
/**
* Make options for reading binary data form partial options.
*/
function binaryReadOptions(options) {
return options ? Object.assign(Object.assign({}, defaultsRead), options) : defaultsRead;
}
exports.binaryReadOptions = binaryReadOptions;
class BinaryReader {
constructor(buf, textDecoder) {
this.varint64 = goog_varint_1.varint64read; // dirty cast for `this`
/**
* Read a `uint32` field, an unsigned 32 bit varint.
*/
this.uint32 = goog_varint_1.varint32read; // dirty cast for `this` and access to protected `buf`
this.buf = buf;
this.len = buf.length;
this.pos = 0;
this.view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
this.textDecoder = textDecoder !== null && textDecoder !== void 0 ? textDecoder : new TextDecoder("utf-8", {
fatal: true,
ignoreBOM: true,
});
}
/**
* Reads a tag - field number and wire type.
*/
tag() {
let tag = this.uint32(), fieldNo = tag >>> 3, wireType = tag & 7;
if (fieldNo <= 0 || wireType < 0 || wireType > 5)
throw new Error("illegal tag: field no " + fieldNo + " wire type " + wireType);
return [fieldNo, wireType];
}
/**
* Skip one element on the wire and return the skipped data.
* Supports WireType.StartGroup since v2.0.0-alpha.23.
*/
skip(wireType) {
let start = this.pos;
// noinspection FallThroughInSwitchStatementJS
switch (wireType) {
case binary_format_contract_1.WireType.Varint:
while (this.buf[this.pos++] & 0x80) {
// ignore
}
break;
case binary_format_contract_1.WireType.Bit64:
this.pos += 4;
case binary_format_contract_1.WireType.Bit32:
this.pos += 4;
break;
case binary_format_contract_1.WireType.LengthDelimited:
let len = this.uint32();
this.pos += len;
break;
case binary_format_contract_1.WireType.StartGroup:
// From descriptor.proto: Group type is deprecated, not supported in proto3.
// But we must still be able to parse and treat as unknown.
let t;
while ((t = this.tag()[1]) !== binary_format_contract_1.WireType.EndGroup) {
this.skip(t);
}
break;
default:
throw new Error("cant skip wire type " + wireType);
}
this.assertBounds();
return this.buf.subarray(start, this.pos);
}
/**
* Throws error if position in byte array is out of range.
*/
assertBounds() {
if (this.pos > this.len)
throw new RangeError("premature EOF");
}
/**
* Read a `int32` field, a signed 32 bit varint.
*/
int32() {
return this.uint32() | 0;
}
/**
* Read a `sint32` field, a signed, zigzag-encoded 32-bit varint.
*/
sint32() {
let zze = this.uint32();
// decode zigzag
return (zze >>> 1) ^ -(zze & 1);
}
/**
* Read a `int64` field, a signed 64-bit varint.
*/
int64() {
return new pb_long_1.PbLong(...this.varint64());
}
/**
* Read a `uint64` field, an unsigned 64-bit varint.
*/
uint64() {
return new pb_long_1.PbULong(...this.varint64());
}
/**
* Read a `sint64` field, a signed, zig-zag-encoded 64-bit varint.
*/
sint64() {
let [lo, hi] = this.varint64();
// decode zig zag
let s = -(lo & 1);
lo = ((lo >>> 1 | (hi & 1) << 31) ^ s);
hi = (hi >>> 1 ^ s);
return new pb_long_1.PbLong(lo, hi);
}
/**
* Read a `bool` field, a variant.
*/
bool() {
let [lo, hi] = this.varint64();
return lo !== 0 || hi !== 0;
}
/**
* Read a `fixed32` field, an unsigned, fixed-length 32-bit integer.
*/
fixed32() {
return this.view.getUint32((this.pos += 4) - 4, true);
}
/**
* Read a `sfixed32` field, a signed, fixed-length 32-bit integer.
*/
sfixed32() {
return this.view.getInt32((this.pos += 4) - 4, true);
}
/**
* Read a `fixed64` field, an unsigned, fixed-length 64 bit integer.
*/
fixed64() {
return new pb_long_1.PbULong(this.sfixed32(), this.sfixed32());
}
/**
* Read a `fixed64` field, a signed, fixed-length 64-bit integer.
*/
sfixed64() {
return new pb_long_1.PbLong(this.sfixed32(), this.sfixed32());
}
/**
* Read a `float` field, 32-bit floating point number.
*/
float() {
return this.view.getFloat32((this.pos += 4) - 4, true);
}
/**
* Read a `double` field, a 64-bit floating point number.
*/
double() {
return this.view.getFloat64((this.pos += 8) - 8, true);
}
/**
* Read a `bytes` field, length-delimited arbitrary data.
*/
bytes() {
let len = this.uint32();
let start = this.pos;
this.pos += len;
this.assertBounds();
return this.buf.subarray(start, start + len);
}
/**
* Read a `string` field, length-delimited data converted to UTF-8 text.
*/
string() {
return this.textDecoder.decode(this.bytes());
}
}
exports.BinaryReader = BinaryReader;