Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update getMatroskaUint to handle larger integer values #4136

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions src/streaming/utils/EBMLParser.js
Original file line number Diff line number Diff line change
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