11 lines
8.7 KiB
JavaScript
11 lines
8.7 KiB
JavaScript
/**
|
||
* byte-buffer v2.0.0
|
||
* Copyright (c) 2012-2020 Tim Kurvers <tim@moonsphere.net>
|
||
* @license MIT
|
||
*
|
||
* Wrapper for JavaScript's ArrayBuffer/DataView.
|
||
*
|
||
* https://github.com/timkurvers/byte-buffer
|
||
*/
|
||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t=t||self).ByteBuffer=e()}(this,(function(){"use strict";function t(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function e(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}var n=function(t){var e=arguments.length>1&&void 0!==arguments[1]&&arguments[1];if(t&&void 0!==t.byteLength)return void 0!==t.buffer?e?t.buffer.slice(0):t.buffer:e?t.slice(0):t;if(t&&void 0!==t.length){if(t.constructor===String)return null;try{return new Uint8Array(t).buffer}catch(t){return null}}return null},i=function(){function i(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:this.constructor.BIG_ENDIAN,o=arguments.length>2&&void 0!==arguments[2]&&arguments[2];t(this,i),this._buffer=null,this._raw=null,this._view=null,this._order=!!r,this._implicitGrowth=!!o,this._index=0;var a=n(e,!0);a||(a=new ArrayBuffer(e)),this.buffer=a}var r,o,a;return r=i,(o=[{key:"_sanitizeIndex",value:function(){this._index<0&&(this._index=0),this._index>this.length&&(this._index=this.length)}},{key:"front",value:function(){return this._index=0,this}},{key:"end",value:function(){return this._index=this.length,this}},{key:"seek",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:1;return this.index+=t,this}},{key:"read",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.available;if(t>this.available)throw new Error("Cannot read ".concat(t," byte(s), ").concat(this.available," available"));if(t<=0)throw new RangeError("Invalid number of bytes ".concat(t));var e=new i(this._buffer.slice(this._index,this._index+t),this.order);return this._index+=t,e}},{key:"write",value:function(t){var e;if(t instanceof Uint8Array)e=t;else{var i=n(t);if(!i)throw new TypeError("Cannot write ".concat(t,", not a sequence"));e=new Uint8Array(i)}var r=this.available;if(e.byteLength>r){if(!this._implicitGrowth)throw new Error("Cannot write ".concat(t," using ").concat(e.byteLength," byte(s), ").concat(this.available," available"));this.append(e.byteLength-r)}return this._raw.set(e,this._index),this._index+=e.byteLength,this}},{key:"readString",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.available;if(t>this.available)throw new Error("Cannot read ".concat(t," byte(s), ").concat(this.available," available"));if(t<=0)throw new RangeError("Invalid number of bytes ".concat(t));for(var e=this._raw,n=[],i=0,r=null,o=null,a=null,h=null,s=this._index+t;this._index<s;)if((r=e[this._index])<128)n[i++]=r,this._index++;else{if(r<194)throw new Error("Unexpected continuation byte");if(r<224){if((o=e[this._index+1])<128||o>191)throw new Error("Bad continuation byte");n[i++]=((31&r)<<6)+(63&o),this._index+=2}else if(r<240){if((o=e[this._index+1])<128||o>191)throw new Error("Bad continuation byte");if((a=e[this._index+2])<128||a>191)throw new Error("Bad continuation byte");n[i++]=((15&r)<<12)+((63&o)<<6)+(63&a),this._index+=3}else{if(!(r<245))throw new Error("Illegal byte");if((o=e[this._index+1])<128||o>191)throw new Error("Bad continuation byte");if((a=e[this._index+2])<128||a>191)throw new Error("Bad continuation byte");if((h=e[this._index+3])<128||h>191)throw new Error("Bad continuation byte");var l=((7&r)<<18)+((63&o)<<12)+((63&a)<<6)+(63&h);l-=65536,n[i++]=55296+((1047552&l)>>>10),n[i++]=56320+(1023&l),this._index+=4}}var u=65536,f=n.length;if(f<u)return String.fromCharCode.apply(String,n);for(var c=[],d=0;d<f;)c.push(String.fromCharCode.apply(String,n.slice(d,d+u))),d+=u;return c.join("")}},{key:"writeString",value:function(t){for(var e=[],n=t.length,i=0,r=0;i<n;){var o=t.charCodeAt(i);if(o<=127)e[r++]=o;else if(o<=2047)e[r++]=192|(1984&o)>>>6,e[r++]=128|63&o;else if(o<=55295||o>=57344&&o<=65535)e[r++]=224|(61440&o)>>>12,e[r++]=128|(4032&o)>>>6,e[r++]=128|63&o;else{if(i===n-1)throw new Error("Unpaired surrogate ".concat(t[i]," (index ").concat(i,")"));var a=t.charCodeAt(++i);if(o<55296||o>56319||a<56320||a>57343)throw new Error("Unpaired surrogate ".concat(t[i]," (index ").concat(i,")"));var h=((1023&o)<<10)+(1023&a)+65536;e[r++]=240|(1835008&h)>>>18,e[r++]=128|(258048&h)>>>12,e[r++]=128|(4032&h)>>>6,e[r++]=128|63&h}++i}return this.write(e),e.length}},{key:"readCString",value:function(){for(var t=this._raw,e=t.length,n=this._index;0!==t[n]&&n<e;)++n;if((e=n-this._index)>0){var i=this.readString(e);return this.readByte(),i}return null}},{key:"writeCString",value:function(t){var e=this.writeString(t);return this.writeByte(0),++e}},{key:"prepend",value:function(t){if(t<=0)throw new RangeError("Invalid number of bytes ".concat(t));var e=new Uint8Array(this.length+t);return e.set(this._raw,t),this._index+=t,this.buffer=e.buffer,this}},{key:"append",value:function(t){if(t<=0)throw new RangeError("Invalid number of bytes ".concat(t));var e=new Uint8Array(this.length+t);return e.set(this._raw,0),this.buffer=e.buffer,this}},{key:"clip",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this._index,e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:this.length;t<0&&(t=this.length+t);var n=this._buffer.slice(t,e);return this._index-=t,this.buffer=n,this}},{key:"slice",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:this.length,n=new i(this._buffer.slice(t,e),this.order);return n}},{key:"clone",value:function(){var t=new i(this._buffer.slice(0),this.order,this.implicitGrowth);return t.index=this._index,t}},{key:"reverse",value:function(){return Array.prototype.reverse.call(this._raw),this._index=0,this}},{key:"toArray",value:function(){return Array.prototype.slice.call(this._raw,0)}},{key:"toHex",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:" ";return Array.prototype.map.call(this._raw,(function(t){return"00".concat(t.toString(16).toUpperCase()).slice(-2)})).join(t)}},{key:"toASCII",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:" ",e=!(arguments.length>1&&void 0!==arguments[1])||arguments[1],n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"<22>",i=e?" ":"";return Array.prototype.map.call(this._raw,(function(t){return t<32||t>126?i+n:i+String.fromCharCode(t)})).join(t)}},{key:"buffer",get:function(){return this._buffer},set:function(t){this._buffer=t,this._raw=new Uint8Array(this._buffer),this._view=new DataView(this._buffer),this._sanitizeIndex()}},{key:"raw",get:function(){return this._raw}},{key:"view",get:function(){return this._view}},{key:"length",get:function(){return this._buffer.byteLength}},{key:"byteLength",get:function(){return this.length}},{key:"order",get:function(){return this._order},set:function(t){this._order=!!t}},{key:"implicitGrowth",get:function(){return this._implicitGrowth},set:function(t){this._implicitGrowth=!!t}},{key:"index",get:function(){return this._index},set:function(t){if(t<0||t>this.length)throw new RangeError("Invalid index ".concat(t,", should be between 0 and ").concat(this.length));this._index=t}},{key:"available",get:function(){return this.length-this._index}}])&&e(r.prototype,o),a&&e(r,a),i}(),r=function(t,e){return function(){var n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this._order;if(e>this.available)throw new Error("Cannot read ".concat(e," byte(s), ").concat(this.available," available"));var i=this._view[t](this._index,n);return this._index+=e,i}},o=function(t,e){return function(n){var i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:this._order,r=this.available;if(e>r){if(!this._implicitGrowth)throw new Error("Cannot write ".concat(n," using ").concat(e," byte(s), ").concat(r," available"));this.append(e-r)}return this._view[t](this._index,n,i),this._index+=e,this}};return i.LITTLE_ENDIAN=!0,i.BIG_ENDIAN=!1,i.prototype.readByte=r("getInt8",1),i.prototype.readUnsignedByte=r("getUint8",1),i.prototype.readShort=r("getInt16",2),i.prototype.readUnsignedShort=r("getUint16",2),i.prototype.readInt=r("getInt32",4),i.prototype.readUnsignedInt=r("getUint32",4),i.prototype.readFloat=r("getFloat32",4),i.prototype.readDouble=r("getFloat64",8),i.prototype.writeByte=o("setInt8",1),i.prototype.writeUnsignedByte=o("setUint8",1),i.prototype.writeShort=o("setInt16",2),i.prototype.writeUnsignedShort=o("setUint16",2),i.prototype.writeInt=o("setInt32",4),i.prototype.writeUnsignedInt=o("setUint32",4),i.prototype.writeFloat=o("setFloat32",4),i.prototype.writeDouble=o("setFloat64",8),i}));
|