-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbrowser-buffer.min.js
More file actions
2 lines (2 loc) · 10.1 KB
/
browser-buffer.min.js
File metadata and controls
2 lines (2 loc) · 10.1 KB
1
2
// Based off of buffer.js in the Node project(copyright Joyent, Inc. and other Node contributors.)
(function(){"use strict";function n(n){return M.isBuffer(n)||n&&"object"==typeof n&&"number"==typeof n.length}function t(n){return n=~~Math.ceil(+n),0>n?0:n}function i(n,t){if(!n)throw Error(t)}function e(n){return 16>n?"0"+n.toString(16):n.toString(16)}function r(n,t,e,r){var o=0;return r||(i("boolean"==typeof e,"missing or invalid endian"),i(void 0!==t&&null!==t,"missing offset"),i(n.length>t+1,"Trying to read beyond buffer length")),e?(o=n[t]<<8,o|=n[t+1]):(o=n[t],o|=n[t+1]<<8),o}function o(n,t,e,r){var o=0;return r||(i("boolean"==typeof e,"missing or invalid endian"),i(void 0!==t&&null!==t,"missing offset"),i(n.length>t+3,"Trying to read beyond buffer length")),e?(o=n[t+1]<<16,o|=n[t+2]<<8,o|=n[t+3],o+=n[t]<<24>>>0):(o=n[t+2]<<16,o|=n[t+1]<<8,o|=n[t],o+=n[t+3]<<24>>>0),o}function u(n,t,e,o){var u,s;return o||(i("boolean"==typeof e,"missing or invalid endian"),i(void 0!==t&&null!==t,"missing offset"),i(n.length>t+1,"Trying to read beyond buffer length")),s=r(n,t,e,o),u=32768&s,u?-1*(65535-s+1):s}function s(n,t,e,r){var u,s;return r||(i("boolean"==typeof e,"missing or invalid endian"),i(void 0!==t&&null!==t,"missing offset"),i(n.length>t+3,"Trying to read beyond buffer length")),s=o(n,t,e,r),u=2147483648&s,u?-1*(4294967295-s+1):s}function a(n,t,i,e,r){var o,u,s=8*r-e-1,a=(1<<s)-1,f=a>>1,l=-7,h=i?0:r-1,c=i?1:-1,d=n[t+h];for(h+=c,o=d&(1<<-l)-1,d>>=-l,l+=s;l>0;o=256*o+n[t+h],h+=c,l-=8);for(u=o&(1<<-l)-1,o>>=-l,l+=e;l>0;u=256*u+n[t+h],h+=c,l-=8);if(0===o)o=1-f;else{if(o===a)return u?0/0:1/0*(d?-1:1);u+=Math.pow(2,e),o-=f}return(d?-1:1)*u*Math.pow(2,o-e)}function f(n,t,e,r){return r||(i("boolean"==typeof e,"missing or invalid endian"),i(n.length>t+3,"Trying to read beyond buffer length")),a(n,t,e,23,4)}function l(n,t,e,r){return r||(i("boolean"==typeof e,"missing or invalid endian"),i(n.length>t+7,"Trying to read beyond buffer length")),a(n,t,e,52,8)}function h(n,t){i("number"==typeof n,"cannot write a non-number as a number"),i(n>=0,"specified a negative value for writing an unsigned value"),i(t>=n,"value is larger than maximum value for type"),i(Math.floor(n)===n,"value has a fractional component")}function c(n,t,e,r,o){o||(i(void 0!==t&&null!==t,"missing value"),i("boolean"==typeof r,"missing or invalid endian"),i(void 0!==e&&null!==e,"missing offset"),i(n.length>e+1,"trying to write beyond buffer length"),h(t,65535)),r?(n[e]=(65280&t)>>>8,n[e+1]=255&t):(n[e+1]=(65280&t)>>>8,n[e]=255&t)}function d(n,t,e,r,o){o||(i(void 0!==t&&null!==t,"missing value"),i("boolean"==typeof r,"missing or invalid endian"),i(void 0!==e&&null!==e,"missing offset"),i(n.length>e+3,"trying to write beyond buffer length"),h(t,4294967295)),r?(n[e]=255&t>>>24,n[e+1]=255&t>>>16,n[e+2]=255&t>>>8,n[e+3]=255&t):(n[e+3]=255&t>>>24,n[e+2]=255&t>>>16,n[e+1]=255&t>>>8,n[e]=255&t)}function g(n,t,e){i("number"==typeof n,"cannot write a non-number as a number"),i(t>=n,"value larger than maximum allowed value"),i(n>=e,"value smaller than minimum allowed value"),i(Math.floor(n)===n,"value has a fractional component")}function b(n,t,e){i("number"==typeof n,"cannot write a non-number as a number"),i(t>=n,"value larger than maximum allowed value"),i(n>=e,"value smaller than minimum allowed value")}function v(n,t,e,r,o){o||(i(void 0!==t&&null!==t,"missing value"),i("boolean"==typeof r,"missing or invalid endian"),i(void 0!==e&&null!==e,"missing offset"),i(n.length>e+1,"Trying to write beyond buffer length"),g(t,32767,-32768)),t>=0?c(n,t,e,r,o):c(n,65535+t+1,e,r,o)}function w(n,t,e,r,o){o||(i(void 0!==t&&null!==t,"missing value"),i("boolean"==typeof r,"missing or invalid endian"),i(void 0!==e&&null!==e,"missing offset"),i(n.length>e+3,"Trying to write beyond buffer length"),g(t,2147483647,-2147483648)),t>=0?d(n,t,e,r,o):d(n,4294967295+t+1,e,r,o)}function m(n,t,i,e,r,o){var u,s,a,f=8*o-r-1,l=(1<<f)-1,h=l>>1,c=23===r?Math.pow(2,-24)-Math.pow(2,-77):0,d=e?o-1:0,g=e?-1:1,b=0>t||0===t&&0>1/t?1:0;for(t=Math.abs(t),isNaN(t)||1/0===t?(s=isNaN(t)?1:0,u=l):(u=Math.floor(Math.log(t)/Math.LN2),1>t*(a=Math.pow(2,-u))&&(u--,a*=2),t+=u+h>=1?c/a:c*Math.pow(2,1-h),t*a>=2&&(u++,a/=2),u+h>=l?(s=0,u=l):u+h>=1?(s=(t*a-1)*Math.pow(2,r),u+=h):(s=t*Math.pow(2,h-1)*Math.pow(2,r),u=0));r>=8;n[i+d]=255&s,d+=g,s/=256,r-=8);for(u=u<<r|s,f+=r;f>0;n[i+d]=255&u,d+=g,u/=256,f-=8);n[i+d-g]|=128*b}function y(n,t,e,r,o){o||(i(void 0!==t&&null!==t,"missing value"),i("boolean"==typeof r,"missing or invalid endian"),i(void 0!==e&&null!==e,"missing offset"),i(n.length>e+3,"Trying to write beyond buffer length"),b(t,3.4028234663852886e38,-3.4028234663852886e38)),m(n,t,e,r,23,4)}function p(n,t,e,r,o){o||(i(void 0!==t&&null!==t,"missing value"),i("boolean"==typeof r,"missing or invalid endian"),i(void 0!==e&&null!==e,"missing offset"),i(n.length>e+7,"Trying to write beyond buffer length"),b(t,1.7976931348623157e308,-1.7976931348623157e308)),m(n,t,e,r,52,8)}function E(){T=new B(M.poolSize),T.used=0}var B=window.ArrayBuffer||function(n){for(this.length=n;n--;)this[n]=0},I=window.Uint8Array||function(n,t,i){this.buffer=n,this.offset=t,this.length=i};"Uint8Array"in window&&I.prototype.set,window.Uint8Array||(I.prototype={get:function(n){return this.buffer[n+this.offset]},set:function(n,t){this.buffer[n+this.offset]=t}});var L=function(n,t,i){var e=new I(n,t,i);return e.parent=n,e.offset=t,e},M=function(i,e,r){var o,u,s,a;if("number"==typeof r)u=t(e),s=i;else{switch(o=typeof i){case"number":u=t(i);break;case"string":u=M.byteLength(i,e);break;case"object":u=t(i.length);break;default:throw Error("First argument needs to be a number, array or string.")}if(this.length>M.poolSize?(s=new B(u),r=0):((!T||u>T.byteLength-T.used)&&E(),s=T,r=T.used,T.used+=u),n(i)){a=L(s,r,u);for(var f=u;f--;)a[f]=i[f]}else"string"==o&&(a=L(s,r,u),u=a.write(i,0,e))}return a||L(s,r,u)},U=M.prototype=I.prototype;U.toString=function(n,t,i){if(n=((n||"utf8")+"").toLowerCase(),t=+t||0,i===void 0&&(i=this.length),+i==t)return"";switch(n){case"hex":return this.hexSlice(t,i);case"utf8":case"utf-8":return this.utf8Slice(t,i);case"ascii":return this.asciiSlice(t,i);case"base64":return this.base64Slice(t,i);default:throw Error("Unknown encoding")}},U.write=function(n,t,i,e){if(isFinite(t))isFinite(i)||(e=i,i=void 0);else{var r=e;e=t,t=i,i=r}t=+t||0;var o=this.length-t;switch(i?(i=+i,i>o&&(i=o)):i=o,e=((e||"utf8")+"").toLowerCase()){case"utf8":case"utf-8":return this.utf8Write(n,t,i);case"ascii":return this.asciiWrite(n,t,i);case"base64":return this.base64Write(n,t,i);default:throw Error("Unknown encoding")}};var S=String.fromCharCode;U.utf8Write=function(n,t,i){for(var e=0,r=t,o=n.length,u=i-t;u>e&&o>e;e++){var s=n.charCodeAt(e);128>s?this[r++]=s:s>127&&2048>s?(this[r++]=192|s>>6,this[r++]=128|63&s):(this[r++]=224|s>>12,this[r++]=128|63&s>>6,this[r++]=128|63&s)}return this._charsWritten=r,o},U.utf8Slice=function(n,t){for(var i,e,r,o="",u=n,s=0;t>s&&(i=this[u]);u++)s++,128>i?o+=S(i):i>191&&224>i?(e=this[u+1],o+=S((31&i)<<6|63&e),u++):(e=this[u+1],r=this[u+2],o+=S((15&i)<<12|(63&e)<<6|63&r),u+=2);return o},U.asciiWrite=function(n,t,i){for(var e=0,r=n.length;i>e&&r>e;e++)this[e+t]=n.charCodeAt(e);return this._charsWritten=e,r},U.asciiSlice=function(n,t){for(var i="",e=n;t>e;e++)i+=S(this[e]);return i},U.hexSlice=function(n,t){var i=this.length;(!n||0>n)&&(n=0),(!t||0>t||t>i)&&(t=i);for(var r="",o=n;t>o;o++)r+=e(this[o]);return r},U.copy=function(n,t,i,e){for(var r=0,o=e-i;o>r;r++)n[r+t]=this[r+i]},U.base64Slice=function(n,t){var i=this.length;(!n||0>n)&&(n=0),(!t||0>t||t>i)&&(t=i);for(var e="",r=n;t>r;r++)e+=window.btoa(this[r]);return e},U.base64Write=function(n,t,i){for(var e=0,r=n.length;i>e&&r>e;e++)this[e+t]=window.atob(n.charCodeAt(e));return this._charsWritten=e,r},U.slice=function(n,t){if(void 0===t&&(t=this.length),t>this.length)throw Error("oob");if(n>t)throw Error("oob");return L(this.buffer,+n,t-n)},U.toBlob=function(){var n=new(window.BlobBuilder||window.WebKitBlobBuilder||window.MozBlobBuilder);return this.offset?n.append(this.toString("utf8")):n.append(this.buffer),n.getBlob()},U.readUInt8=function(n,t){var e=this;return t||(i(void 0!==n&&null!==n,"missing offset"),i(e.length>n,"Trying to read beyond buffer length")),e[n]},U.readUInt16LE=function(n,t){return r(this,n,!1,t)},U.readUInt16BE=function(n,t){return r(this,n,!0,t)},U.readUInt32LE=function(n,t){return o(this,n,!1,t)},U.readUInt32BE=function(n,t){return o(this,n,!0,t)},U.readInt8=function(n,t){var e,r=this;return t||(i(void 0!==n&&null!==n,"missing offset"),i(r.length>n,"Trying to read beyond buffer length")),e=128&r[n],e?-1*(255-r[n]+1):r[n]},U.readInt16LE=function(n,t){return u(this,n,!1,t)},U.readInt16BE=function(n,t){return u(this,n,!0,t)},U.readInt32LE=function(n,t){return s(this,n,!1,t)},U.readInt32BE=function(n,t){return s(this,n,!0,t)},U.readFloatLE=function(n,t){return f(this,n,!1,t)},U.readFloatBE=function(n,t){return f(this,n,!0,t)},U.readDoubleLE=function(n,t){return l(this,n,!1,t)},U.readDoubleBE=function(n,t){return l(this,n,!0,t)},U.writeUInt8=function(n,t,e){var r=this;e||(i(void 0!==n&&null!==n,"missing value"),i(void 0!==t&&null!==t,"missing offset"),i(r.length>t,"trying to write beyond buffer length"),h(n,255)),r[t]=n},U.writeUInt16LE=function(n,t,i){c(this,n,t,!1,i)},U.writeUInt16BE=function(n,t,i){c(this,n,t,!0,i)},U.writeUInt32LE=function(n,t,i){d(this,n,t,!1,i)},U.writeUInt32BE=function(n,t,i){d(this,n,t,!0,i)},U.writeInt8=function(n,t,e){var r=this;e||(i(void 0!==n&&null!==n,"missing value"),i(void 0!==t&&null!==t,"missing offset"),i(r.length>t,"Trying to write beyond buffer length"),g(n,127,-128)),n>=0?r.writeUInt8(n,t,e):r.writeUInt8(255+n+1,t,e)},U.writeInt16LE=function(n,t,i){v(this,n,t,!1,i)},U.writeInt16BE=function(n,t,i){v(this,n,t,!0,i)},U.writeInt32LE=function(n,t,i){w(this,n,t,!1,i)},U.writeInt32BE=function(n,t,i){w(this,n,t,!0,i)},U.writeFloatLE=function(n,t,i){y(this,n,t,!1,i)},U.writeFloatBE=function(n,t,i){y(this,n,t,!0,i)},U.writeDoubleLE=function(n,t,i){p(this,n,t,!1,i)},U.writeDoubleBE=function(n,t,i){p(this,n,t,!0,i)};var T;M.poolSize=8192,M.isBuffer=function(n){return n instanceof M||n instanceof B},M.byteLength=function(n,t){switch(t){case"ascii":return n.length}for(var i,e=0,r=0,o=n.length;o>e;e++)i=n.charCodeAt(e),128>i?r++:r+=i>127&&2048>i?2:3;return r},window.Buffer=M})();