UNPKG

@nethesis/phone-island

Version:
3 lines (2 loc) 3.71 kB
"use strict";var t=require("../../../../_virtual/EBMLDecoder.js");require("../../../int64-buffer/int64-buffer.js");var e=require("./tools.js");require("./ebmlID.js");var r=require("../../../../_virtual/int64-buffer.js"),a=require("../../../../_virtual/ebmlID.js");Object.defineProperty(t.__exports,"__esModule",{value:!0});var s,i=r.__exports,n=e.__require(),u=e.__require(),_=a.__exports;!function(t){t[t.STATE_TAG=1]="STATE_TAG",t[t.STATE_SIZE=2]="STATE_SIZE",t[t.STATE_CONTENT=3]="STATE_CONTENT"}(s||(s={}));var h=function(){function t(){this._buffer=new n.Buffer(0),this._tag_stack=[],this._state=s.STATE_TAG,this._cursor=0,this._total=0,this._schema=_.byEbmlID,this._result=[]}return t.prototype.decode=function(t){this.readChunk(t);var e=this._result;return this._result=[],e},t.prototype.readChunk=function(t){for(this._buffer=u.concat([this._buffer,new n.Buffer(t)]);this._cursor<this._buffer.length&&(this._state!==s.STATE_TAG||this.readTag())&&(this._state!==s.STATE_SIZE||this.readSize())&&(this._state!==s.STATE_CONTENT||this.readContent()););},t.prototype.getSchemaInfo=function(t){return this._schema[t]||{name:"unknown",level:-1,type:"unknown",description:"unknown"}},t.prototype.readTag=function(){if(this._cursor>=this._buffer.length)return!1;var t=(0,n.readVint)(this._buffer,this._cursor);if(null==t)return!1;var e=this._buffer.slice(this._cursor,this._cursor+t.length).reduce((function(t,e,r,a){return t+e*Math.pow(16,2*(a.length-1-r))}),0),r=this.getSchemaInfo(e),a={EBML_ID:e.toString(16),schema:r,type:r.type,name:r.name,level:r.level,tagStart:this._total,tagEnd:this._total+t.length,sizeStart:this._total+t.length,sizeEnd:null,dataStart:null,dataEnd:null,dataSize:null,data:null};return this._tag_stack.push(a),this._cursor+=t.length,this._total+=t.length,this._state=s.STATE_SIZE,!0},t.prototype.readSize=function(){if(this._cursor>=this._buffer.length)return!1;var t=(0,n.readVint)(this._buffer,this._cursor);if(null==t)return!1;var e=this._tag_stack[this._tag_stack.length-1];return e.sizeEnd=e.sizeStart+t.length,e.dataStart=e.sizeEnd,e.dataSize=t.value,-1===t.value?(e.dataEnd=-1,"m"===e.type&&(e.unknownSize=!0)):e.dataEnd=e.sizeEnd+t.value,this._cursor+=t.length,this._total+=t.length,this._state=s.STATE_CONTENT,!0},t.prototype.readContent=function(){var t=this._tag_stack[this._tag_stack.length-1];if("m"===t.type){if(t.isEnd=!1,this._result.push(t),this._state=s.STATE_TAG,0===t.dataSize){var e=Object.assign({},t,{isEnd:!0});this._result.push(e),this._tag_stack.pop()}return!0}if(this._buffer.length<this._cursor+t.dataSize)return!1;var r=this._buffer.slice(this._cursor,this._cursor+t.dataSize);switch(this._buffer=this._buffer.slice(this._cursor+t.dataSize),t.data=r,t.type){case"u":t.value=r.readUIntBE(0,r.length);break;case"i":t.value=r.readIntBE(0,r.length);break;case"f":t.value=4===t.dataSize?r.readFloatBE(0):8===t.dataSize?r.readDoubleBE(0):(console.warn("cannot read ".concat(t.dataSize," octets float. failback to 0")),0);break;case"s":t.value=r.toString("ascii");break;case"8":t.value=r.toString("utf8");break;case"b":t.value=r;break;case"d":t.value=(0,n.convertEBMLDateToJSDate)(new i.Int64BE(r).toNumber())}if(null===t.value)throw new Error("unknown tag type:"+t.type);for(this._result.push(t),this._total+=t.dataSize,this._state=s.STATE_TAG,this._cursor=0,this._tag_stack.pop();this._tag_stack.length>0;){var a=this._tag_stack[this._tag_stack.length-1];if(a.dataEnd<0)return this._tag_stack.pop(),!0;if(this._total<a.dataEnd)break;if("m"!==a.type)throw new Error("parent element is not master element");e=Object.assign({},a,{isEnd:!0});this._result.push(e),this._tag_stack.pop()}return!0},t}();t.__exports.default=h; //# sourceMappingURL=EBMLDecoder.js.map