Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | 1x | import FactoryMaker from '../../core/FactoryMaker.js';
/**
* Creates an instance of an EBMLParser class which implements a large subset
* of the functionality required to parse Matroska EBML
*
* @param {Object} config object with data member which is the buffer to parse
* @ignore
*/
function EBMLParser(config) {
config = config || {};
let instance;
let data = new DataView(config.data);
let pos = 0;
function getPos() {
return pos;
}
function setPos(value) {
pos = value;
}
/**
* Consumes an EBML tag from the data stream.
*
* @param {Object} tag to parse, A tag is an object with at least a {number} tag and
* {boolean} required flag.
* @param {boolean} test whether or not the function should throw if a required
* tag is not found
* @return {boolean} whether or not the tag was found
* @throws will throw an exception if a required tag is not found and test
* param is false or undefined, or if the stream is malformed.
* @memberof EBMLParser
*/
function consumeTag(tag, test) {
let found = true;
let bytesConsumed = 0;
let p1,
p2;
if (test === undefined) {
test = false;
}
if (tag.tag > 0xFFFFFF) {
if (data.getUint32(pos) !== tag.tag) {
found = false;
}
bytesConsumed = 4;
} else if (tag.tag > 0xFFFF) {
// 3 bytes
p1 = data.getUint16(pos);
p2 = data.getUint8(pos + 2);
// shift p1 over a byte and add p2
if (p1 * 256 + p2 !== tag.tag) {
found = false;
}
bytesConsumed = 3;
} else if (tag.tag > 0xFF) {
if (data.getUint16(pos) !== tag.tag) {
found = false;
}
bytesConsumed = 2;
} else {
if (data.getUint8(pos) !== tag.tag) {
found = false;
}
bytesConsumed = 1;
}
if (!found && tag.required && !test) {
throw new Error('required tag not found');
}
if (found) {
pos += bytesConsumed;
}
return found;
}
/**
* Consumes an EBML tag from the data stream. If the tag is found then this
* function will also remove the size field which follows the tag from the
* data stream.
*
* @param {Object} tag to parse, A tag is an object with at least a {number} tag and
* {boolean} required flag.
* @param {boolean} test whether or not the function should throw if a required
* tag is not found
* @return {boolean} whether or not the tag was found
* @throws will throw an exception if a required tag is not found and test
* param is false or undefined, or if the stream is malformedata.
* @memberof EBMLParser
*/
function consumeTagAndSize(tag, test) {
let found = consumeTag(tag, test);
if (found) {
getMatroskaCodedNum();
}
return found;
}
/**
* Consumes an EBML tag from the data stream. If the tag is found then this
* function will also remove the size field which follows the tag from the
* data stream. It will use the value of the size field to parse a binary
* field, using a parser defined in the tag itself
*
* @param {Object} tag to parse, A tag is an object with at least a {number} tag,
* {boolean} required flag, and a parse function which takes a size parameter
* @return {boolean} whether or not the tag was found
* @throws will throw an exception if a required tag is not found,
* or if the stream is malformed
* @memberof EBMLParser
*/
function parseTag(tag) {
let size;
consumeTag(tag);
size = getMatroskaCodedNum();
return instance[tag.parse](size);
}
/**
* Consumes an EBML tag from the data stream. If the tag is found then this
* function will also remove the size field which follows the tag from the
* data stream. It will use the value of the size field to skip over the
* entire section of EBML encapsulated by the tag.
*
* @param {Object} tag to parse, A tag is an object with at least a {number} tag, and
* {boolean} required flag
* @param {boolean} test a flag to indicate if an exception should be thrown
* if a required tag is not found
* @return {boolean} whether or not the tag was found
* @throws will throw an exception if a required tag is not found and test is
* false or undefined or if the stream is malformed
* @memberof EBMLParser
*/
function skipOverElement(tag, test) {
let found = consumeTag(tag, test);
let headerSize;
if (found) {
headerSize = getMatroskaCodedNum();
pos += headerSize;
}
return found;
}
/**
* Returns and consumes a number encoded according to the Matroska EBML
* specification from the bitstream.
*
* @param {boolean} retainMSB whether or not to retain the Most Significant Bit (the
* first 1). this is usually true when reading Tag IDs.
* @return {number} the decoded number
* @throws will throw an exception if the bit stream is malformed or there is
* not enough data
* @memberof EBMLParser
*/
function getMatroskaCodedNum(retainMSB) {
let bytesUsed = 1;
let mask = 0x80;
let maxBytes = 8;
let extraBytes = -1;
let num = 0;
let ch = data.getUint8(pos);
let i = 0;
for (i = 0; i < maxBytes; i += 1) {
if ((ch & mask) === mask) {
num = (retainMSB === undefined) ? ch & ~mask : ch;
extraBytes = i;
break;
}
mask >>= 1;
}
for (i = 0; i < extraBytes; i += 1, bytesUsed += 1) {
num = (num << 8) | (0xff & data.getUint8(pos + bytesUsed));
}
pos += bytesUsed;
return num;
}
/**
* Returns and consumes a float from the bitstream.
*
* @param {number} size 4 or 8 byte floats are supported
* @return {number} the decoded number
* @throws will throw an exception if the bit stream is malformed or there is
* not enough data
* @memberof EBMLParser
*/
function getMatroskaFloat(size) {
let outFloat;
switch (size) {
case 4:
outFloat = data.getFloat32(pos);
pos += 4;
break;
case 8:
outFloat = data.getFloat64(pos);
pos += 8;
break;
}
return outFloat;
}
/**
* 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
* @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;
}
/**
* Tests whether there is more data in the bitstream for parsing
*
* @return {boolean} whether there is more data to parse
* @memberof EBMLParser
*/
function moreData() {
return pos < data.byteLength;
}
instance = {
getPos: getPos,
setPos: setPos,
consumeTag: consumeTag,
consumeTagAndSize: consumeTagAndSize,
parseTag: parseTag,
skipOverElement: skipOverElement,
getMatroskaCodedNum: getMatroskaCodedNum,
getMatroskaFloat: getMatroskaFloat,
getMatroskaUint: getMatroskaUint,
moreData: moreData
};
return instance;
}
EBMLParser.__dashjs_factory_name = 'EBMLParser';
export default FactoryMaker.getClassFactory(EBMLParser);
|