feat: adding protobuf decoder for parse of google auth migrations
This commit is contained in:
27
lib/protobuf-decoder/intUtils.js
Normal file
27
lib/protobuf-decoder/intUtils.js
Normal file
@@ -0,0 +1,27 @@
|
||||
import JSBI from "jsbi";
|
||||
|
||||
export function interpretAsSignedType(n) {
|
||||
// see https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/wire_format_lite.h#L857-L876
|
||||
// however, this is a simpler equivalent formula
|
||||
const isEven = JSBI.equal(JSBI.bitwiseAnd(n, JSBI.BigInt(1)), JSBI.BigInt(0));
|
||||
if (isEven) {
|
||||
return JSBI.divide(n, JSBI.BigInt(2));
|
||||
} else {
|
||||
return JSBI.multiply(
|
||||
JSBI.BigInt(-1),
|
||||
JSBI.divide(JSBI.add(n, JSBI.BigInt(1)), JSBI.BigInt(2))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export function interpretAsTwosComplement(n, bits) {
|
||||
const isTwosComplement = JSBI.equal(
|
||||
JSBI.signedRightShift(n, JSBI.BigInt(bits - 1)),
|
||||
JSBI.BigInt(1)
|
||||
);
|
||||
if (isTwosComplement) {
|
||||
return JSBI.subtract(n, JSBI.leftShift(JSBI.BigInt(1), JSBI.BigInt(bits)));
|
||||
} else {
|
||||
return n;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user