Permalink
Cannot retrieve contributors at this time
31 lines (24 sloc)
998 Bytes
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
codeql-action/node_modules/es-abstract/helpers/bytesAsInteger.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var GetIntrinsic = require('get-intrinsic'); | |
var $pow = GetIntrinsic('%Math.pow%'); | |
var $Number = GetIntrinsic('%Number%'); | |
var $BigInt = GetIntrinsic('%BigInt%', true); | |
module.exports = function bytesAsInteger(rawBytes, elementSize, isUnsigned, isBigInt) { | |
var Z = isBigInt ? $BigInt : $Number; | |
// this is common to both branches | |
var intValue = Z(0); | |
for (var i = 0; i < rawBytes.length; i++) { | |
intValue += Z(rawBytes[i]) * Z($pow(2, 8 * i)); | |
} | |
/* | |
Let intValue be the byte elements of rawBytes concatenated and interpreted as a bit string encoding of an unsigned little-endian binary number. | |
*/ | |
if (!isUnsigned) { // steps 5-6 | |
// Let intValue be the byte elements of rawBytes concatenated and interpreted as a bit string encoding of a binary little-endian 2's complement number of bit length elementSize × 8. | |
var bitLength = elementSize * 8; | |
if (rawBytes[elementSize - 1] & 0x80) { | |
intValue -= Z($pow(2, bitLength)); | |
} | |
} | |
return intValue; // step 7 | |
}; |