UNPKG

mp4box

Version:

JavaScript version of GPAC's MP4Box tool

1,664 lines (1,475 loc) 284 kB
// file:src/log.js /* * Copyright (c) 2012-2013. Telecom ParisTech/TSI/MM/GPAC Cyril Concolato * License: BSD-3-Clause (see LICENSE file) */ var Log = (function (){ var start = new Date(); var LOG_LEVEL_ERROR = 4; var LOG_LEVEL_WARNING = 3; var LOG_LEVEL_INFO = 2; var LOG_LEVEL_DEBUG = 1; var log_level = LOG_LEVEL_ERROR; var logObject = { setLogLevel : function(level) { if (level == this.debug) log_level = LOG_LEVEL_DEBUG; else if (level == this.info) log_level = LOG_LEVEL_INFO; else if (level == this.warn) log_level = LOG_LEVEL_WARNING; else if (level == this.error) log_level = LOG_LEVEL_ERROR; else log_level = LOG_LEVEL_ERROR; }, debug : function(module, msg) { if (console.debug === undefined) { console.debug = console.log; } if (LOG_LEVEL_DEBUG >= log_level) { console.debug("["+Log.getDurationString(new Date()-start,1000)+"]","["+module+"]",msg); } }, log : function(module, msg) { this.debug(module.msg) }, info : function(module, msg) { if (LOG_LEVEL_INFO >= log_level) { console.info("["+Log.getDurationString(new Date()-start,1000)+"]","["+module+"]",msg); } }, warn : function(module, msg) { if (LOG_LEVEL_WARNING >= log_level) { console.warn("["+Log.getDurationString(new Date()-start,1000)+"]","["+module+"]",msg); } }, error : function(module, msg) { if (LOG_LEVEL_ERROR >= log_level) { console.error("["+Log.getDurationString(new Date()-start,1000)+"]","["+module+"]",msg); } } }; return logObject; })(); /* Helper function to print a duration value in the form H:MM:SS.MS */ Log.getDurationString = function(duration, _timescale) { var neg; /* Helper function to print a number on a fixed number of digits */ function pad(number, length) { var str = '' + number; var a = str.split('.'); while (a[0].length < length) { a[0] = '0' + a[0]; } return a.join('.'); } if (duration < 0) { neg = true; duration = -duration; } else { neg = false; } var timescale = _timescale || 1; var duration_sec = duration/timescale; var hours = Math.floor(duration_sec/3600); duration_sec -= hours * 3600; var minutes = Math.floor(duration_sec/60); duration_sec -= minutes * 60; var msec = duration_sec*1000; duration_sec = Math.floor(duration_sec); msec -= duration_sec*1000; msec = Math.floor(msec); return (neg ? "-": "")+hours+":"+pad(minutes,2)+":"+pad(duration_sec,2)+"."+pad(msec,3); } /* Helper function to stringify HTML5 TimeRanges objects */ Log.printRanges = function(ranges) { var length = ranges.length; if (length > 0) { var str = ""; for (var i = 0; i < length; i++) { if (i > 0) str += ","; str += "["+Log.getDurationString(ranges.start(i))+ ","+Log.getDurationString(ranges.end(i))+"]"; } return str; } else { return "(empty)"; } } if (typeof exports !== 'undefined') { exports.Log = Log; } // file:src/stream.js var MP4BoxStream = function(arrayBuffer) { if (arrayBuffer instanceof ArrayBuffer) { this.buffer = arrayBuffer; this.dataview = new DataView(arrayBuffer); } else { throw ("Needs an array buffer"); } this.position = 0; }; /************************************************************************* Common API between MultiBufferStream and SimpleStream *************************************************************************/ MP4BoxStream.prototype.getPosition = function() { return this.position; } MP4BoxStream.prototype.getEndPosition = function() { return this.buffer.byteLength; } MP4BoxStream.prototype.getLength = function() { return this.buffer.byteLength; } MP4BoxStream.prototype.seek = function (pos) { var npos = Math.max(0, Math.min(this.buffer.byteLength, pos)); this.position = (isNaN(npos) || !isFinite(npos)) ? 0 : npos; return true; } MP4BoxStream.prototype.isEos = function () { return this.getPosition() >= this.getEndPosition(); } /************************************************************************* Read methods, simimar to DataStream but simpler *************************************************************************/ MP4BoxStream.prototype.readAnyInt = function(size, signed) { var res = 0; if (this.position + size <= this.buffer.byteLength) { switch (size) { case 1: if (signed) { res = this.dataview.getInt8(this.position); } else { res = this.dataview.getUint8(this.position); } break; case 2: if (signed) { res = this.dataview.getInt16(this.position); } else { res = this.dataview.getUint16(this.position); } break; case 3: if (signed) { throw ("No method for reading signed 24 bits values"); } else { res = this.dataview.getUint8(this.position) << 16; res |= this.dataview.getUint8(this.position+1) << 8; res |= this.dataview.getUint8(this.position+2); } break; case 4: if (signed) { res = this.dataview.getInt32(this.position); } else { res = this.dataview.getUint32(this.position); } break; case 8: if (signed) { throw ("No method for reading signed 64 bits values"); } else { res = this.dataview.getUint32(this.position) << 32; res |= this.dataview.getUint32(this.position+4); } break; default: throw ("readInt method not implemented for size: "+size); } this.position+= size; return res; } else { throw ("Not enough bytes in buffer"); } } MP4BoxStream.prototype.readUint8 = function() { return this.readAnyInt(1, false); } MP4BoxStream.prototype.readUint16 = function() { return this.readAnyInt(2, false); } MP4BoxStream.prototype.readUint24 = function() { return this.readAnyInt(3, false); } MP4BoxStream.prototype.readUint32 = function() { return this.readAnyInt(4, false); } MP4BoxStream.prototype.readUint64 = function() { return this.readAnyInt(8, false); } MP4BoxStream.prototype.readString = function(length) { if (this.position + length <= this.buffer.byteLength) { var s = ""; for (var i = 0; i < length; i++) { s += String.fromCharCode(this.readUint8()); } return s; } else { throw ("Not enough bytes in buffer"); } } MP4BoxStream.prototype.readCString = function() { var arr = []; while(true) { var b = this.readUint8(); if (b !== 0) { arr.push(b); } else { break; } } return String.fromCharCode.apply(null, arr); } MP4BoxStream.prototype.readInt8 = function() { return this.readAnyInt(1, true); } MP4BoxStream.prototype.readInt16 = function() { return this.readAnyInt(2, true); } MP4BoxStream.prototype.readInt32 = function() { return this.readAnyInt(4, true); } MP4BoxStream.prototype.readInt64 = function() { return this.readAnyInt(8, false); } MP4BoxStream.prototype.readUint8Array = function(length) { var arr = new Uint8Array(length); for (var i = 0; i < length; i++) { arr[i] = this.readUint8(); } return arr; } MP4BoxStream.prototype.readInt16Array = function(length) { var arr = new Int16Array(length); for (var i = 0; i < length; i++) { arr[i] = this.readInt16(); } return arr; } MP4BoxStream.prototype.readUint16Array = function(length) { var arr = new Int16Array(length); for (var i = 0; i < length; i++) { arr[i] = this.readUint16(); } return arr; } MP4BoxStream.prototype.readUint32Array = function(length) { var arr = new Uint32Array(length); for (var i = 0; i < length; i++) { arr[i] = this.readUint32(); } return arr; } MP4BoxStream.prototype.readInt32Array = function(length) { var arr = new Int32Array(length); for (var i = 0; i < length; i++) { arr[i] = this.readInt32(); } return arr; } if (typeof exports !== 'undefined') { exports.MP4BoxStream = MP4BoxStream; }// file:src/DataStream.js /** DataStream reads scalars, arrays and structs of data from an ArrayBuffer. It's like a file-like DataView on steroids. @param {ArrayBuffer} arrayBuffer ArrayBuffer to read from. @param {?Number} byteOffset Offset from arrayBuffer beginning for the DataStream. @param {?Boolean} endianness DataStream.BIG_ENDIAN or DataStream.LITTLE_ENDIAN (the default). */ var DataStream = function(arrayBuffer, byteOffset, endianness) { this._byteOffset = byteOffset || 0; if (arrayBuffer instanceof ArrayBuffer) { this.buffer = arrayBuffer; } else if (typeof arrayBuffer == "object") { this.dataView = arrayBuffer; if (byteOffset) { this._byteOffset += byteOffset; } } else { this.buffer = new ArrayBuffer(arrayBuffer || 0); } this.position = 0; this.endianness = endianness == null ? DataStream.LITTLE_ENDIAN : endianness; }; DataStream.prototype = {}; DataStream.prototype.getPosition = function() { return this.position; } /** Internal function to resize the DataStream buffer when required. @param {number} extra Number of bytes to add to the buffer allocation. @return {null} */ DataStream.prototype._realloc = function(extra) { if (!this._dynamicSize) { return; } var req = this._byteOffset + this.position + extra; var blen = this._buffer.byteLength; if (req <= blen) { if (req > this._byteLength) { this._byteLength = req; } return; } if (blen < 1) { blen = 1; } while (req > blen) { blen *= 2; } var buf = new ArrayBuffer(blen); var src = new Uint8Array(this._buffer); var dst = new Uint8Array(buf, 0, src.length); dst.set(src); this.buffer = buf; this._byteLength = req; }; /** Internal function to trim the DataStream buffer when required. Used for stripping out the extra bytes from the backing buffer when the virtual byteLength is smaller than the buffer byteLength (happens after growing the buffer with writes and not filling the extra space completely). @return {null} */ DataStream.prototype._trimAlloc = function() { if (this._byteLength == this._buffer.byteLength) { return; } var buf = new ArrayBuffer(this._byteLength); var dst = new Uint8Array(buf); var src = new Uint8Array(this._buffer, 0, dst.length); dst.set(src); this.buffer = buf; }; /** Big-endian const to use as default endianness. @type {boolean} */ DataStream.BIG_ENDIAN = false; /** Little-endian const to use as default endianness. @type {boolean} */ DataStream.LITTLE_ENDIAN = true; /** Virtual byte length of the DataStream backing buffer. Updated to be max of original buffer size and last written size. If dynamicSize is false is set to buffer size. @type {number} */ DataStream.prototype._byteLength = 0; /** Returns the byte length of the DataStream object. @type {number} */ Object.defineProperty(DataStream.prototype, 'byteLength', { get: function() { return this._byteLength - this._byteOffset; }}); /** Set/get the backing ArrayBuffer of the DataStream object. The setter updates the DataView to point to the new buffer. @type {Object} */ Object.defineProperty(DataStream.prototype, 'buffer', { get: function() { this._trimAlloc(); return this._buffer; }, set: function(v) { this._buffer = v; this._dataView = new DataView(this._buffer, this._byteOffset); this._byteLength = this._buffer.byteLength; } }); /** Set/get the byteOffset of the DataStream object. The setter updates the DataView to point to the new byteOffset. @type {number} */ Object.defineProperty(DataStream.prototype, 'byteOffset', { get: function() { return this._byteOffset; }, set: function(v) { this._byteOffset = v; this._dataView = new DataView(this._buffer, this._byteOffset); this._byteLength = this._buffer.byteLength; } }); /** Set/get the backing DataView of the DataStream object. The setter updates the buffer and byteOffset to point to the DataView values. @type {Object} */ Object.defineProperty(DataStream.prototype, 'dataView', { get: function() { return this._dataView; }, set: function(v) { this._byteOffset = v.byteOffset; this._buffer = v.buffer; this._dataView = new DataView(this._buffer, this._byteOffset); this._byteLength = this._byteOffset + v.byteLength; } }); /** Sets the DataStream read/write position to given position. Clamps between 0 and DataStream length. @param {number} pos Position to seek to. @return {null} */ DataStream.prototype.seek = function(pos) { var npos = Math.max(0, Math.min(this.byteLength, pos)); this.position = (isNaN(npos) || !isFinite(npos)) ? 0 : npos; }; /** Returns true if the DataStream seek pointer is at the end of buffer and there's no more data to read. @return {boolean} True if the seek pointer is at the end of the buffer. */ DataStream.prototype.isEof = function() { return (this.position >= this._byteLength); }; /** Maps a Uint8Array into the DataStream buffer. Nice for quickly reading in data. @param {number} length Number of elements to map. @param {?boolean} e Endianness of the data to read. @return {Object} Uint8Array to the DataStream backing buffer. */ DataStream.prototype.mapUint8Array = function(length) { this._realloc(length * 1); var arr = new Uint8Array(this._buffer, this.byteOffset+this.position, length); this.position += length * 1; return arr; }; /** Reads an Int32Array of desired length and endianness from the DataStream. @param {number} length Number of elements to map. @param {?boolean} e Endianness of the data to read. @return {Object} The read Int32Array. */ DataStream.prototype.readInt32Array = function(length, e) { length = length == null ? (this.byteLength-this.position / 4) : length; var arr = new Int32Array(length); DataStream.memcpy(arr.buffer, 0, this.buffer, this.byteOffset+this.position, length*arr.BYTES_PER_ELEMENT); DataStream.arrayToNative(arr, e == null ? this.endianness : e); this.position += arr.byteLength; return arr; }; /** Reads an Int16Array of desired length and endianness from the DataStream. @param {number} length Number of elements to map. @param {?boolean} e Endianness of the data to read. @return {Object} The read Int16Array. */ DataStream.prototype.readInt16Array = function(length, e) { length = length == null ? (this.byteLength-this.position / 2) : length; var arr = new Int16Array(length); DataStream.memcpy(arr.buffer, 0, this.buffer, this.byteOffset+this.position, length*arr.BYTES_PER_ELEMENT); DataStream.arrayToNative(arr, e == null ? this.endianness : e); this.position += arr.byteLength; return arr; }; /** Reads an Int8Array of desired length from the DataStream. @param {number} length Number of elements to map. @param {?boolean} e Endianness of the data to read. @return {Object} The read Int8Array. */ DataStream.prototype.readInt8Array = function(length) { length = length == null ? (this.byteLength-this.position) : length; var arr = new Int8Array(length); DataStream.memcpy(arr.buffer, 0, this.buffer, this.byteOffset+this.position, length*arr.BYTES_PER_ELEMENT); this.position += arr.byteLength; return arr; }; /** Reads a Uint32Array of desired length and endianness from the DataStream. @param {number} length Number of elements to map. @param {?boolean} e Endianness of the data to read. @return {Object} The read Uint32Array. */ DataStream.prototype.readUint32Array = function(length, e) { length = length == null ? (this.byteLength-this.position / 4) : length; var arr = new Uint32Array(length); DataStream.memcpy(arr.buffer, 0, this.buffer, this.byteOffset+this.position, length*arr.BYTES_PER_ELEMENT); DataStream.arrayToNative(arr, e == null ? this.endianness : e); this.position += arr.byteLength; return arr; }; /** Reads a Uint16Array of desired length and endianness from the DataStream. @param {number} length Number of elements to map. @param {?boolean} e Endianness of the data to read. @return {Object} The read Uint16Array. */ DataStream.prototype.readUint16Array = function(length, e) { length = length == null ? (this.byteLength-this.position / 2) : length; var arr = new Uint16Array(length); DataStream.memcpy(arr.buffer, 0, this.buffer, this.byteOffset+this.position, length*arr.BYTES_PER_ELEMENT); DataStream.arrayToNative(arr, e == null ? this.endianness : e); this.position += arr.byteLength; return arr; }; /** Reads a Uint8Array of desired length from the DataStream. @param {number} length Number of elements to map. @param {?boolean} e Endianness of the data to read. @return {Object} The read Uint8Array. */ DataStream.prototype.readUint8Array = function(length) { length = length == null ? (this.byteLength-this.position) : length; var arr = new Uint8Array(length); DataStream.memcpy(arr.buffer, 0, this.buffer, this.byteOffset+this.position, length*arr.BYTES_PER_ELEMENT); this.position += arr.byteLength; return arr; }; /** Reads a Float64Array of desired length and endianness from the DataStream. @param {number} length Number of elements to map. @param {?boolean} e Endianness of the data to read. @return {Object} The read Float64Array. */ DataStream.prototype.readFloat64Array = function(length, e) { length = length == null ? (this.byteLength-this.position / 8) : length; var arr = new Float64Array(length); DataStream.memcpy(arr.buffer, 0, this.buffer, this.byteOffset+this.position, length*arr.BYTES_PER_ELEMENT); DataStream.arrayToNative(arr, e == null ? this.endianness : e); this.position += arr.byteLength; return arr; }; /** Reads a Float32Array of desired length and endianness from the DataStream. @param {number} length Number of elements to map. @param {?boolean} e Endianness of the data to read. @return {Object} The read Float32Array. */ DataStream.prototype.readFloat32Array = function(length, e) { length = length == null ? (this.byteLength-this.position / 4) : length; var arr = new Float32Array(length); DataStream.memcpy(arr.buffer, 0, this.buffer, this.byteOffset+this.position, length*arr.BYTES_PER_ELEMENT); DataStream.arrayToNative(arr, e == null ? this.endianness : e); this.position += arr.byteLength; return arr; }; /** Reads a 32-bit int from the DataStream with the desired endianness. @param {?boolean} e Endianness of the number. @return {number} The read number. */ DataStream.prototype.readInt32 = function(e) { var v = this._dataView.getInt32(this.position, e == null ? this.endianness : e); this.position += 4; return v; }; /** Reads a 16-bit int from the DataStream with the desired endianness. @param {?boolean} e Endianness of the number. @return {number} The read number. */ DataStream.prototype.readInt16 = function(e) { var v = this._dataView.getInt16(this.position, e == null ? this.endianness : e); this.position += 2; return v; }; /** Reads an 8-bit int from the DataStream. @return {number} The read number. */ DataStream.prototype.readInt8 = function() { var v = this._dataView.getInt8(this.position); this.position += 1; return v; }; /** Reads a 32-bit unsigned int from the DataStream with the desired endianness. @param {?boolean} e Endianness of the number. @return {number} The read number. */ DataStream.prototype.readUint32 = function(e) { var v = this._dataView.getUint32(this.position, e == null ? this.endianness : e); this.position += 4; return v; }; /** Reads a 16-bit unsigned int from the DataStream with the desired endianness. @param {?boolean} e Endianness of the number. @return {number} The read number. */ DataStream.prototype.readUint16 = function(e) { var v = this._dataView.getUint16(this.position, e == null ? this.endianness : e); this.position += 2; return v; }; /** Reads an 8-bit unsigned int from the DataStream. @return {number} The read number. */ DataStream.prototype.readUint8 = function() { var v = this._dataView.getUint8(this.position); this.position += 1; return v; }; /** Reads a 32-bit float from the DataStream with the desired endianness. @param {?boolean} e Endianness of the number. @return {number} The read number. */ DataStream.prototype.readFloat32 = function(e) { var v = this._dataView.getFloat32(this.position, e == null ? this.endianness : e); this.position += 4; return v; }; /** Reads a 64-bit float from the DataStream with the desired endianness. @param {?boolean} e Endianness of the number. @return {number} The read number. */ DataStream.prototype.readFloat64 = function(e) { var v = this._dataView.getFloat64(this.position, e == null ? this.endianness : e); this.position += 8; return v; }; /** Native endianness. Either DataStream.BIG_ENDIAN or DataStream.LITTLE_ENDIAN depending on the platform endianness. @type {boolean} */ DataStream.endianness = new Int8Array(new Int16Array([1]).buffer)[0] > 0; /** Copies byteLength bytes from the src buffer at srcOffset to the dst buffer at dstOffset. @param {Object} dst Destination ArrayBuffer to write to. @param {number} dstOffset Offset to the destination ArrayBuffer. @param {Object} src Source ArrayBuffer to read from. @param {number} srcOffset Offset to the source ArrayBuffer. @param {number} byteLength Number of bytes to copy. */ DataStream.memcpy = function(dst, dstOffset, src, srcOffset, byteLength) { var dstU8 = new Uint8Array(dst, dstOffset, byteLength); var srcU8 = new Uint8Array(src, srcOffset, byteLength); dstU8.set(srcU8); }; /** Converts array to native endianness in-place. @param {Object} array Typed array to convert. @param {boolean} arrayIsLittleEndian True if the data in the array is little-endian. Set false for big-endian. @return {Object} The converted typed array. */ DataStream.arrayToNative = function(array, arrayIsLittleEndian) { if (arrayIsLittleEndian == this.endianness) { return array; } else { return this.flipArrayEndianness(array); } }; /** Converts native endianness array to desired endianness in-place. @param {Object} array Typed array to convert. @param {boolean} littleEndian True if the converted array should be little-endian. Set false for big-endian. @return {Object} The converted typed array. */ DataStream.nativeToEndian = function(array, littleEndian) { if (this.endianness == littleEndian) { return array; } else { return this.flipArrayEndianness(array); } }; /** Flips typed array endianness in-place. @param {Object} array Typed array to flip. @return {Object} The converted typed array. */ DataStream.flipArrayEndianness = function(array) { var u8 = new Uint8Array(array.buffer, array.byteOffset, array.byteLength); for (var i=0; i<array.byteLength; i+=array.BYTES_PER_ELEMENT) { for (var j=i+array.BYTES_PER_ELEMENT-1, k=i; j>k; j--, k++) { var tmp = u8[k]; u8[k] = u8[j]; u8[j] = tmp; } } return array; }; /** Seek position where DataStream#readStruct ran into a problem. Useful for debugging struct parsing. @type {number} */ DataStream.prototype.failurePosition = 0; String.fromCharCodeUint8 = function(uint8arr) { var arr = []; for (var i = 0; i < uint8arr.length; i++) { arr[i] = uint8arr[i]; } return String.fromCharCode.apply(null, arr); } /** Read a string of desired length and encoding from the DataStream. @param {number} length The length of the string to read in bytes. @param {?string} encoding The encoding of the string data in the DataStream. Defaults to ASCII. @return {string} The read string. */ DataStream.prototype.readString = function(length, encoding) { if (encoding == null || encoding == "ASCII") { return String.fromCharCodeUint8.apply(null, [this.mapUint8Array(length == null ? this.byteLength-this.position : length)]); } else { return (new TextDecoder(encoding)).decode(this.mapUint8Array(length)); } }; /** Read null-terminated string of desired length from the DataStream. Truncates the returned string so that the null byte is not a part of it. @param {?number} length The length of the string to read. @return {string} The read string. */ DataStream.prototype.readCString = function(length) { var blen = this.byteLength-this.position; var u8 = new Uint8Array(this._buffer, this._byteOffset + this.position); var len = blen; if (length != null) { len = Math.min(length, blen); } for (var i = 0; i < len && u8[i] !== 0; i++); // find first zero byte var s = String.fromCharCodeUint8.apply(null, [this.mapUint8Array(i)]); if (length != null) { this.position += len-i; } else if (i != blen) { this.position += 1; // trailing zero if not at end of buffer } return s; }; /* TODO: fix endianness for 24/64-bit fields TODO: check range/support for 64-bits numbers in JavaScript */ var MAX_SIZE = Math.pow(2, 32); DataStream.prototype.readInt64 = function () { return (this.readInt32()*MAX_SIZE)+this.readUint32(); } DataStream.prototype.readUint64 = function () { return (this.readUint32()*MAX_SIZE)+this.readUint32(); } DataStream.prototype.readInt64 = function () { return (this.readUint32()*MAX_SIZE)+this.readUint32(); } DataStream.prototype.readUint24 = function () { return (this.readUint8()<<16)+(this.readUint8()<<8)+this.readUint8(); } if (typeof exports !== 'undefined') { exports.DataStream = DataStream; } // file:src/DataStream-write.js /** Saves the DataStream contents to the given filename. Uses Chrome's anchor download property to initiate download. @param {string} filename Filename to save as. @return {null} */ DataStream.prototype.save = function(filename) { var blob = new Blob([this.buffer]); if (window.URL && URL.createObjectURL) { var url = window.URL.createObjectURL(blob); var a = document.createElement('a'); // Required in Firefox: document.body.appendChild(a); a.setAttribute('href', url); a.setAttribute('download', filename); // Required in Firefox: a.setAttribute('target', '_self'); a.click(); window.URL.revokeObjectURL(url); } else { throw("DataStream.save: Can't create object URL."); } }; /** Whether to extend DataStream buffer when trying to write beyond its size. If set, the buffer is reallocated to twice its current size until the requested write fits the buffer. @type {boolean} */ DataStream.prototype._dynamicSize = true; Object.defineProperty(DataStream.prototype, 'dynamicSize', { get: function() { return this._dynamicSize; }, set: function(v) { if (!v) { this._trimAlloc(); } this._dynamicSize = v; } }); /** Internal function to trim the DataStream buffer when required. Used for stripping out the first bytes when not needed anymore. @return {null} */ DataStream.prototype.shift = function(offset) { var buf = new ArrayBuffer(this._byteLength-offset); var dst = new Uint8Array(buf); var src = new Uint8Array(this._buffer, offset, dst.length); dst.set(src); this.buffer = buf; this.position -= offset; }; /** Writes an Int32Array of specified endianness to the DataStream. @param {Object} arr The array to write. @param {?boolean} e Endianness of the data to write. */ DataStream.prototype.writeInt32Array = function(arr, e) { this._realloc(arr.length * 4); if (arr instanceof Int32Array && this.byteOffset+this.position % arr.BYTES_PER_ELEMENT === 0) { DataStream.memcpy(this._buffer, this.byteOffset+this.position, arr.buffer, 0, arr.byteLength); this.mapInt32Array(arr.length, e); } else { for (var i=0; i<arr.length; i++) { this.writeInt32(arr[i], e); } } }; /** Writes an Int16Array of specified endianness to the DataStream. @param {Object} arr The array to write. @param {?boolean} e Endianness of the data to write. */ DataStream.prototype.writeInt16Array = function(arr, e) { this._realloc(arr.length * 2); if (arr instanceof Int16Array && this.byteOffset+this.position % arr.BYTES_PER_ELEMENT === 0) { DataStream.memcpy(this._buffer, this.byteOffset+this.position, arr.buffer, 0, arr.byteLength); this.mapInt16Array(arr.length, e); } else { for (var i=0; i<arr.length; i++) { this.writeInt16(arr[i], e); } } }; /** Writes an Int8Array to the DataStream. @param {Object} arr The array to write. */ DataStream.prototype.writeInt8Array = function(arr) { this._realloc(arr.length * 1); if (arr instanceof Int8Array && this.byteOffset+this.position % arr.BYTES_PER_ELEMENT === 0) { DataStream.memcpy(this._buffer, this.byteOffset+this.position, arr.buffer, 0, arr.byteLength); this.mapInt8Array(arr.length); } else { for (var i=0; i<arr.length; i++) { this.writeInt8(arr[i]); } } }; /** Writes a Uint32Array of specified endianness to the DataStream. @param {Object} arr The array to write. @param {?boolean} e Endianness of the data to write. */ DataStream.prototype.writeUint32Array = function(arr, e) { this._realloc(arr.length * 4); if (arr instanceof Uint32Array && this.byteOffset+this.position % arr.BYTES_PER_ELEMENT === 0) { DataStream.memcpy(this._buffer, this.byteOffset+this.position, arr.buffer, 0, arr.byteLength); this.mapUint32Array(arr.length, e); } else { for (var i=0; i<arr.length; i++) { this.writeUint32(arr[i], e); } } }; /** Writes a Uint16Array of specified endianness to the DataStream. @param {Object} arr The array to write. @param {?boolean} e Endianness of the data to write. */ DataStream.prototype.writeUint16Array = function(arr, e) { this._realloc(arr.length * 2); if (arr instanceof Uint16Array && this.byteOffset+this.position % arr.BYTES_PER_ELEMENT === 0) { DataStream.memcpy(this._buffer, this.byteOffset+this.position, arr.buffer, 0, arr.byteLength); this.mapUint16Array(arr.length, e); } else { for (var i=0; i<arr.length; i++) { this.writeUint16(arr[i], e); } } }; /** Writes a Uint8Array to the DataStream. @param {Object} arr The array to write. */ DataStream.prototype.writeUint8Array = function(arr) { this._realloc(arr.length * 1); if (arr instanceof Uint8Array && this.byteOffset+this.position % arr.BYTES_PER_ELEMENT === 0) { DataStream.memcpy(this._buffer, this.byteOffset+this.position, arr.buffer, 0, arr.byteLength); this.mapUint8Array(arr.length); } else { for (var i=0; i<arr.length; i++) { this.writeUint8(arr[i]); } } }; /** Writes a Float64Array of specified endianness to the DataStream. @param {Object} arr The array to write. @param {?boolean} e Endianness of the data to write. */ DataStream.prototype.writeFloat64Array = function(arr, e) { this._realloc(arr.length * 8); if (arr instanceof Float64Array && this.byteOffset+this.position % arr.BYTES_PER_ELEMENT === 0) { DataStream.memcpy(this._buffer, this.byteOffset+this.position, arr.buffer, 0, arr.byteLength); this.mapFloat64Array(arr.length, e); } else { for (var i=0; i<arr.length; i++) { this.writeFloat64(arr[i], e); } } }; /** Writes a Float32Array of specified endianness to the DataStream. @param {Object} arr The array to write. @param {?boolean} e Endianness of the data to write. */ DataStream.prototype.writeFloat32Array = function(arr, e) { this._realloc(arr.length * 4); if (arr instanceof Float32Array && this.byteOffset+this.position % arr.BYTES_PER_ELEMENT === 0) { DataStream.memcpy(this._buffer, this.byteOffset+this.position, arr.buffer, 0, arr.byteLength); this.mapFloat32Array(arr.length, e); } else { for (var i=0; i<arr.length; i++) { this.writeFloat32(arr[i], e); } } }; /** Writes a 32-bit int to the DataStream with the desired endianness. @param {number} v Number to write. @param {?boolean} e Endianness of the number. */ DataStream.prototype.writeInt32 = function(v, e) { this._realloc(4); this._dataView.setInt32(this.position, v, e == null ? this.endianness : e); this.position += 4; }; /** Writes a 16-bit int to the DataStream with the desired endianness. @param {number} v Number to write. @param {?boolean} e Endianness of the number. */ DataStream.prototype.writeInt16 = function(v, e) { this._realloc(2); this._dataView.setInt16(this.position, v, e == null ? this.endianness : e); this.position += 2; }; /** Writes an 8-bit int to the DataStream. @param {number} v Number to write. */ DataStream.prototype.writeInt8 = function(v) { this._realloc(1); this._dataView.setInt8(this.position, v); this.position += 1; }; /** Writes a 32-bit unsigned int to the DataStream with the desired endianness. @param {number} v Number to write. @param {?boolean} e Endianness of the number. */ DataStream.prototype.writeUint32 = function(v, e) { this._realloc(4); this._dataView.setUint32(this.position, v, e == null ? this.endianness : e); this.position += 4; }; /** Writes a 16-bit unsigned int to the DataStream with the desired endianness. @param {number} v Number to write. @param {?boolean} e Endianness of the number. */ DataStream.prototype.writeUint16 = function(v, e) { this._realloc(2); this._dataView.setUint16(this.position, v, e == null ? this.endianness : e); this.position += 2; }; /** Writes an 8-bit unsigned int to the DataStream. @param {number} v Number to write. */ DataStream.prototype.writeUint8 = function(v) { this._realloc(1); this._dataView.setUint8(this.position, v); this.position += 1; }; /** Writes a 32-bit float to the DataStream with the desired endianness. @param {number} v Number to write. @param {?boolean} e Endianness of the number. */ DataStream.prototype.writeFloat32 = function(v, e) { this._realloc(4); this._dataView.setFloat32(this.position, v, e == null ? this.endianness : e); this.position += 4; }; /** Writes a 64-bit float to the DataStream with the desired endianness. @param {number} v Number to write. @param {?boolean} e Endianness of the number. */ DataStream.prototype.writeFloat64 = function(v, e) { this._realloc(8); this._dataView.setFloat64(this.position, v, e == null ? this.endianness : e); this.position += 8; }; /** Write a UCS-2 string of desired endianness to the DataStream. The lengthOverride argument lets you define the number of characters to write. If the string is shorter than lengthOverride, the extra space is padded with zeroes. @param {string} str The string to write. @param {?boolean} endianness The endianness to use for the written string data. @param {?number} lengthOverride The number of characters to write. */ DataStream.prototype.writeUCS2String = function(str, endianness, lengthOverride) { if (lengthOverride == null) { lengthOverride = str.length; } for (var i = 0; i < str.length && i < lengthOverride; i++) { this.writeUint16(str.charCodeAt(i), endianness); } for (; i<lengthOverride; i++) { this.writeUint16(0); } }; /** Writes a string of desired length and encoding to the DataStream. @param {string} s The string to write. @param {?string} encoding The encoding for the written string data. Defaults to ASCII. @param {?number} length The number of characters to write. */ DataStream.prototype.writeString = function(s, encoding, length) { var i = 0; if (encoding == null || encoding == "ASCII") { if (length != null) { var len = Math.min(s.length, length); for (i=0; i<len; i++) { this.writeUint8(s.charCodeAt(i)); } for (; i<length; i++) { this.writeUint8(0); } } else { for (i=0; i<s.length; i++) { this.writeUint8(s.charCodeAt(i)); } } } else { this.writeUint8Array((new TextEncoder(encoding)).encode(s.substring(0, length))); } }; /** Writes a null-terminated string to DataStream and zero-pads it to length bytes. If length is not given, writes the string followed by a zero. If string is longer than length, the written part of the string does not have a trailing zero. @param {string} s The string to write. @param {?number} length The number of characters to write. */ DataStream.prototype.writeCString = function(s, length) { var i = 0; if (length != null) { var len = Math.min(s.length, length); for (i=0; i<len; i++) { this.writeUint8(s.charCodeAt(i)); } for (; i<length; i++) { this.writeUint8(0); } } else { for (i=0; i<s.length; i++) { this.writeUint8(s.charCodeAt(i)); } this.writeUint8(0); } }; /** Writes a struct to the DataStream. Takes a structDefinition that gives the types and a struct object that gives the values. Refer to readStruct for the structure of structDefinition. @param {Object} structDefinition Type definition of the struct. @param {Object} struct The struct data object. */ DataStream.prototype.writeStruct = function(structDefinition, struct) { for (var i = 0; i < structDefinition.length; i+=2) { var t = structDefinition[i+1]; this.writeType(t, struct[structDefinition[i]], struct); } }; /** Writes object v of type t to the DataStream. @param {Object} t Type of data to write. @param {Object} v Value of data to write. @param {Object} struct Struct to pass to write callback functions. */ DataStream.prototype.writeType = function(t, v, struct) { var tp; if (typeof t == "function") { return t(this, v); } else if (typeof t == "object" && !(t instanceof Array)) { return t.set(this, v, struct); } var lengthOverride = null; var charset = "ASCII"; var pos = this.position; if (typeof(t) == 'string' && /:/.test(t)) { tp = t.split(":"); t = tp[0]; lengthOverride = parseInt(tp[1]); } if (typeof t == 'string' && /,/.test(t)) { tp = t.split(","); t = tp[0]; charset = parseInt(tp[1]); } switch(t) { case 'uint8': this.writeUint8(v); break; case 'int8': this.writeInt8(v); break; case 'uint16': this.writeUint16(v, this.endianness); break; case 'int16': this.writeInt16(v, this.endianness); break; case 'uint32': this.writeUint32(v, this.endianness); break; case 'int32': this.writeInt32(v, this.endianness); break; case 'float32': this.writeFloat32(v, this.endianness); break; case 'float64': this.writeFloat64(v, this.endianness); break; case 'uint16be': this.writeUint16(v, DataStream.BIG_ENDIAN); break; case 'int16be': this.writeInt16(v, DataStream.BIG_ENDIAN); break; case 'uint32be': this.writeUint32(v, DataStream.BIG_ENDIAN); break; case 'int32be': this.writeInt32(v, DataStream.BIG_ENDIAN); break; case 'float32be': this.writeFloat32(v, DataStream.BIG_ENDIAN); break; case 'float64be': this.writeFloat64(v, DataStream.BIG_ENDIAN); break; case 'uint16le': this.writeUint16(v, DataStream.LITTLE_ENDIAN); break; case 'int16le': this.writeInt16(v, DataStream.LITTLE_ENDIAN); break; case 'uint32le': this.writeUint32(v, DataStream.LITTLE_ENDIAN); break; case 'int32le': this.writeInt32(v, DataStream.LITTLE_ENDIAN); break; case 'float32le': this.writeFloat32(v, DataStream.LITTLE_ENDIAN); break; case 'float64le': this.writeFloat64(v, DataStream.LITTLE_ENDIAN); break; case 'cstring': this.writeCString(v, lengthOverride); break; case 'string': this.writeString(v, charset, lengthOverride); break; case 'u16string': this.writeUCS2String(v, this.endianness, lengthOverride); break; case 'u16stringle': this.writeUCS2String(v, DataStream.LITTLE_ENDIAN, lengthOverride); break; case 'u16stringbe': this.writeUCS2String(v, DataStream.BIG_ENDIAN, lengthOverride); break; default: if (t.length == 3) { var ta = t[1]; for (var i=0; i<v.length; i++) { this.writeType(ta, v[i]); } break; } else { this.writeStruct(t, v); break; } } if (lengthOverride != null) { this.position = pos; this._realloc(lengthOverride); this.position = pos + lengthOverride; } }; DataStream.prototype.writeUint64 = function (v) { var h = Math.floor(v / MAX_SIZE); this.writeUint32(h); this.writeUint32(v & 0xFFFFFFFF); } DataStream.prototype.writeUint24 = function (v) { this.writeUint8((v & 0x00FF0000)>>16); this.writeUint8((v & 0x0000FF00)>>8); this.writeUint8((v & 0x000000FF)); } DataStream.prototype.adjustUint32 = function(position, value) { var pos = this.position; this.seek(position); this.writeUint32(value); this.seek(pos); } // file:src/DataStream-map.js /** Maps an Int32Array into the DataStream buffer, swizzling it to native endianness in-place. The current offset from the start of the buffer needs to be a multiple of element size, just like with typed array views. Nice for quickly reading in data. Warning: potentially modifies the buffer contents. @param {number} length Number of elements to map. @param {?boolean} e Endianness of the data to read. @return {Object} Int32Array to the DataStream backing buffer. */ DataStream.prototype.mapInt32Array = function(length, e) { this._realloc(length * 4); var arr = new Int32Array(this._buffer, this.byteOffset+this.position, length); DataStream.arrayToNative(arr, e == null ? this.endianness : e); this.position += length * 4; return arr; }; /** Maps an Int16Array into the DataStream buffer, swizzling it to native endianness in-place. The current offset from the start of the buffer needs to be a multiple of element size, just like with typed array views. Nice for quickly reading in data. Warning: potentially modifies the buffer contents. @param {number} length Number of elements to map. @param {?boolean} e Endianness of the data to read. @return {Object} Int16Array to the DataStream backing buffer. */ DataStream.prototype.mapInt16Array = function(length, e) { this._realloc(length * 2); var arr = new Int16Array(this._buffer, this.byteOffset+this.position, length); DataStream.arrayToNative(arr, e == null ? this.endianness : e); this.position += length * 2; return arr; }; /** Maps an Int8Array into the DataStream buffer. Nice for quickly reading in data. @param {number} length Number of elements to map. @param {?boolean} e Endianness of the data to read. @return {Object} Int8Array to the DataStream backing buffer. */ DataStream.prototype.mapInt8Array = function(length) { this._realloc(length * 1); var arr = new Int8Array(this._buffer, this.byteOffset+this.position, length); this.position += length * 1; return arr; }; /** Maps a Uint32Array into the DataStream buffer, swizzling it to native endianness in-place. The current offset from the start of the buffer needs to be a multiple of element size, just like with typed array views. Nice for quickly reading in data. Warning: potentially modifies the buffer contents. @param {number} length Number of elements to map. @param {?boolean} e Endianness of the data to read. @return {Object} Uint32Array to the DataStream backing buffer. */ DataStream.prototype.mapUint32Array = function(length, e) { this._realloc(length * 4); var arr = new Uint32Array(this._buffer, this.byteOffset+this.position, length); DataStream.arrayToNative(arr, e == null ? this.endianness : e); this.position += length * 4; return arr; }; /** Maps a Uint16Array into the DataStream buffer, swizzling it to native endianness in-place. The current offset from the start of the buffer needs to be a multiple of element size, just like with typed array views. Nice for quickly reading in data. Warning: potentially modifies the buffer contents. @param {number} length Number of elements to map. @param {?boolean} e Endianness of the data to read. @return {Object} Uint16Array to the DataStream backing buffer. */ DataStream.prototype.mapUint16Array = function(length, e) { this._realloc(length * 2); var arr = new Uint16Array(this._buffer, this.byteOffset+this.position, length); DataStream.arrayToNative(arr, e == null ? this.endianness : e); this.position += length * 2; return arr; }; /** Maps a Float64Array into the DataStream buffer, swizzling it to native endianness in-place. The current offset from the start of the buffer needs to be a multiple of element size, just like with typed array views. Nice for quickly reading in data. Warning: potentially modifies the buffer contents. @param {number} length Number of elements to map. @param {?boolean} e Endianness of the data to read. @return {Object} Float64Array to the DataStream backing buffer. */ DataStream.prototype.mapFloat64Array = function(length, e) { this._realloc(length * 8); var arr = new Float64Array(this._buffer, this.byteOffset+this.position, length); DataStream.arrayToNative(arr, e == null ? this.endianness : e); this.position += length * 8; return arr; }; /** Maps a Float32Array into the DataStream buffer, swizzling it to native endianness in-place. The current offset from the start of the buffer needs to be a multiple of element size, just like with typed array views. Nice for quickly reading in data. Warning: potentially modifies the buffer contents. @param {number} length Number of elements to map. @param {?boolean} e Endianness of the data to read. @return {Object} Float32Array to the DataStream backing buffer. */ DataStream.prototype.mapFloat32Array = function(length, e) { this._realloc(length * 4); var arr = new Float32Array(this._buffer, this.byteOffset+this.position, length); DataStream.arrayToNative(arr, e == null ? this.endianness : e); this.position += length * 4; return arr; }; // file:src/buffer.js /** * MultiBufferStream is a class that acts as a SimpleStream for parsing * It holds several, possibly non-contiguous ArrayBuffer objects, each with a fileStart property * containing the offset for the buffer data in an original/virtual file * * It inherits also from DataStream for all read/write/alloc operations */ /** * Constructor */ var MultiBufferStream = function(buffer) { /* List of ArrayBuffers, with a fileStart property, sorted in fileStart order and non overlapping */ this.buffers = []; this.bufferIndex = -1; if (buffer) { this.insertBuffer(buffer); this.bufferIndex = 0; } } MultiBufferStream.prototype = new DataStream(new ArrayBuffer(), 0, DataStream.BIG_ENDIAN); /************************************************************************************ Methods for the managnement of the buffers (insertion, removal, concatenation, ...) ***********************************************************************************/ MultiBufferStream.prototype.initialized = function() { var firstBuffer; if (this.bufferIndex > -1) { return true; } else if (this.buffers.length > 0) { firstBuffer = this.buffers[0]; if (firstBuffer.fileStart === 0) { this.buffer = firstBuffer; this.bufferIndex = 0; Log.debug("MultiBufferStream", "Stream ready for parsing"); return true; } else { Log.warn("MultiBufferStream", "The first buffer should have a fileStart of 0"); this.logBufferLevel(); return false; } } else { Log.warn("MultiBufferStream", "No buffer to start parsing from"); this.logBufferLevel(); return false; } } /** * helper functions to concatenate two ArrayBuffer objects * @param {ArrayBuffer} buffer1 * @param {ArrayBuffer} buffer2 * @return {ArrayBuffer} the concatenation of buffer1 and buffer2 in that order */ ArrayBuffer.concat = function(buffer1, buffer2) { Log.debug("ArrayBuffer", "Trying to create a new buffer of size: "+(buffer1.byteLength + buffer2.byteLength)); var tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength); tmp.set(new Uint8Array(buffer1), 0); tmp.set(new Uint8Array(buffer2), buffer1.byteLength); return tmp.buffer; }; /** * Reduces the size of a given buffer, but taking the part between offset and offset+newlength * @param {ArrayBuffer} buffer * @param {Number} offset the start of new buffer * @param {Number} newLength the length of the new buffer * @return {ArrayBuffer} the new buffer */ MultiBufferStream.prototype.reduceBuffer = function(buffer, offset, newLength) { var smallB; smallB = new Uint8Array(newLength); smallB.set(new Uint8Array(buffer, offset, newLength)); smallB.buffer.fileStart = buffer.fileStart+offset; smallB.buffer.usedBytes = 0; return smallB.buffer; } /** * Inserts the new buffer in the sorted list of buffers, * making sure, it is not overlapping with existing ones (possibly reducing its size). * if the new buffer overrides/replaces