2019-04-02 17:27:14 +01:00
/**
* @author GCHQ Contributor [3]
* @copyright Crown Copyright 2019
* @license Apache-2.0
*/
2019-07-09 12:23:59 +01:00
import Operation from "../Operation.mjs" ;
import OperationError from "../errors/OperationError.mjs" ;
import Protobuf from "../lib/Protobuf.mjs" ;
2019-04-02 17:27:14 +01:00
/**
* VarInt Decode operation
*/
class VarIntDecode extends Operation {
/**
* VarIntDecode constructor
*/
constructor () {
super ();
this . name = "VarInt Decode" ;
this . module = "Default" ;
this . description = "Decodes a VarInt encoded integer. VarInt is an efficient way of encoding variable length integers and is commonly used with Protobuf." ;
this . infoURL = "https://developers.google.com/protocol-buffers/docs/encoding#varints" ;
this . inputType = "byteArray" ;
2025-02-16 19:54:55 +00:00
this . outputType = "string" ;
2019-04-02 17:27:14 +01:00
this . args = [];
}
/**
* @param {byteArray} input
* @param {Object[]} args
* @returns {number}
*/
run ( input , args ) {
try {
2025-02-16 19:54:55 +00:00
if ( typeof BigInt === "function" ) {
let result = BigInt ( 0 );
let offset = BigInt ( 0 );
for ( let i = 0 ; i < input . length ; i ++ ) {
result |= BigInt ( input [ i ] & 0x7f ) << offset ;
if ( ! ( input [ i ] & 0x80 )) break ;
offset += BigInt ( 7 );
}
return result . toString ();
} else {
return Protobuf . varIntDecode ( input ). toString ();
}
2019-04-02 17:27:14 +01:00
} catch ( err ) {
throw new OperationError ( err );
}
}
}
export default VarIntDecode ;