Skip to content

Commit

Permalink
Update getMatroskaUint to handle larger integer values (#4136)
Browse files Browse the repository at this point in the history
  • Loading branch information
kv-bh committed Mar 27, 2023
1 parent 1bda677 commit 533b074
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions src/streaming/utils/EBMLParser.js
Expand Up @@ -223,18 +223,50 @@ function EBMLParser(config) {
*
* @param {number} size 1 to 8 bytes
* @return {number} the decoded number
* @throws will throw an exception if the bit stream is malformed or there is
* not enough data
* @throws will throw an exception if the bit stream is malformed, there is
* not enough data, or if the value exceeds the maximum safe integer value
* @memberof EBMLParser
*/
function getMatroskaUint(size) {
if (size > 4) {
return getMatroskaUintLarge(size);
}

let val = 0;

for (let i = 0; i < size; i += 1) {
val <<= 8;
val |= data.getUint8(pos + i) & 0xff;
}

pos += size;
return val >>> 0;
}

/**
* Consumes and returns an unsigned int from the bitstream.
*
* @param {number} size 1 to 8 bytes
* @return {number} the decoded number
* @throws will throw an exception if the bit stream is malformed, there is
* not enough data, or if the value exceeds the maximum safe integer value
*/
function getMatroskaUintLarge(size) {
const limit = Math.floor(Number.MAX_SAFE_INTEGER / 256);
let val = 0;

for (let i = 0; i < size; i += 1) {
if (val > limit) {
throw new Error('Value exceeds safe integer limit');
}
val *= 256;
const n = data.getUint8(pos + i);
if (val > Number.MAX_SAFE_INTEGER - n) {
throw new Error('Value exceeds safe integer limit');
}
val += n;
}

pos += size;
return val;
}
Expand Down

0 comments on commit 533b074

Please sign in to comment.