From a61319a27c86ff94a4222a1af07f2956ff045dbc Mon Sep 17 00:00:00 2001 From: Syrian Watermelon Date: Thu, 1 May 2014 15:53:12 +0200 Subject: [PATCH 01/11] Adding hex decoding/writing into buffer. Minified with uglifyjs --- browser-buffer.min.js | 2 +- src/browser-buffer.js | 22 ++++++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/browser-buffer.min.js b/browser-buffer.min.js index 6b3cc33..11c29bb 100644 --- a/browser-buffer.min.js +++ b/browser-buffer.min.js @@ -1,2 +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<>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<>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<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})(); +!function(){"use strict";function a(a){return h.isBuffer(a)||a&&"object"==typeof a&&"number"==typeof a.length}function b(a){return a=~~Math.ceil(+a),0>a?0:a}function c(a,b){if(!a)throw new Error(b)}function k(a){return 16>a?"0"+a.toString(16):a.toString(16)}function l(a,b,d,e){var f=0;return e||(c("boolean"==typeof d,"missing or invalid endian"),c(void 0!==b&&null!==b,"missing offset"),c(b+1>>0):(f=a[b+2]<<16,f|=a[b+1]<<8,f|=a[b],f+=a[b+3]<<24>>>0),f}function n(a,b,d,e){var f,g;return e||(c("boolean"==typeof d,"missing or invalid endian"),c(void 0!==b&&null!==b,"missing offset"),c(b+1>1,k=-7,l=c?0:e-1,m=c?1:-1,n=a[b+l];for(l+=m,f=n&(1<<-k)-1,n>>=-k,k+=h;k>0;f=256*f+a[b+l],l+=m,k-=8);for(g=f&(1<<-k)-1,f>>=-k,k+=d;k>0;g=256*g+a[b+l],l+=m,k-=8);if(0===f)f=1-j;else{if(f===i)return g?0/0:1/0*(n?-1:1);g+=Math.pow(2,d),f-=j}return(n?-1:1)*g*Math.pow(2,f-d)}function q(a,b,d,e){return e||(c("boolean"==typeof d,"missing or invalid endian"),c(b+3=0,"specified a negative value for writing an unsigned value"),c(b>=a,"value is larger than maximum value for type"),c(Math.floor(a)===a,"value has a fractional component")}function t(a,b,d,e,f){f||(c(void 0!==b&&null!==b,"missing value"),c("boolean"==typeof e,"missing or invalid endian"),c(void 0!==d&&null!==d,"missing offset"),c(d+1>>8,a[d+1]=255&b):(a[d+1]=(65280&b)>>>8,a[d]=255&b)}function u(a,b,d,e,f){f||(c(void 0!==b&&null!==b,"missing value"),c("boolean"==typeof e,"missing or invalid endian"),c(void 0!==d&&null!==d,"missing offset"),c(d+3>>24,a[d+1]=255&b>>>16,a[d+2]=255&b>>>8,a[d+3]=255&b):(a[d+3]=255&b>>>24,a[d+2]=255&b>>>16,a[d+1]=255&b>>>8,a[d]=255&b)}function v(a,b,d){c("number"==typeof a,"cannot write a non-number as a number"),c(b>=a,"value larger than maximum allowed value"),c(a>=d,"value smaller than minimum allowed value"),c(Math.floor(a)===a,"value has a fractional component")}function w(a,b,d){c("number"==typeof a,"cannot write a non-number as a number"),c(b>=a,"value larger than maximum allowed value"),c(a>=d,"value smaller than minimum allowed value")}function x(a,b,d,e,f){f||(c(void 0!==b&&null!==b,"missing value"),c("boolean"==typeof e,"missing or invalid endian"),c(void 0!==d&&null!==d,"missing offset"),c(d+1=0?t(a,b,d,e,f):t(a,65535+b+1,d,e,f)}function y(a,b,d,e,f){f||(c(void 0!==b&&null!==b,"missing value"),c("boolean"==typeof e,"missing or invalid endian"),c(void 0!==d&&null!==d,"missing offset"),c(d+3=0?u(a,b,d,e,f):u(a,4294967295+b+1,d,e,f)}function z(a,b,c,d,e,f){var g,h,i,j=8*f-e-1,k=(1<>1,m=23===e?Math.pow(2,-24)-Math.pow(2,-77):0,n=d?f-1:0,o=d?-1:1,p=0>b||0===b&&0>1/b?1:0;for(b=Math.abs(b),isNaN(b)||1/0===b?(h=isNaN(b)?1:0,g=k):(g=Math.floor(Math.log(b)/Math.LN2),b*(i=Math.pow(2,-g))<1&&(g--,i*=2),b+=g+l>=1?m/i:m*Math.pow(2,1-l),b*i>=2&&(g++,i/=2),g+l>=k?(h=0,g=k):g+l>=1?(h=(b*i-1)*Math.pow(2,e),g+=l):(h=b*Math.pow(2,l-1)*Math.pow(2,e),g=0));e>=8;a[c+n]=255&h,n+=o,h/=256,e-=8);for(g=g<0;a[c+n]=255&g,n+=o,g/=256,j-=8);a[c+n-o]|=128*p}function A(a,b,d,e,f){f||(c(void 0!==b&&null!==b,"missing value"),c("boolean"==typeof e,"missing or invalid endian"),c(void 0!==d&&null!==d,"missing offset"),c(d+3h.poolSize?(k=new d(j),f=0):((!C||C.byteLength-C.usedf&&(c=f)):c=f,d=String(d||"utf8").toLowerCase()){case"hex":return this.hexWrite(a,b,c);case"utf8":case"utf-8":return this.utf8Write(a,b,c);case"ascii":return this.asciiWrite(a,b,c);case"base64":return this.base64Write(a,b,c);default:throw new Error("Unknown encoding")}};var j=String.fromCharCode;i.utf8Write=function(a,b,c){for(var d=0,e=b,f=a.length,g=c-b;g>d&&f>d;d++){var h=a.charCodeAt(d);128>h?this[e++]=h:h>127&&2048>h?(this[e++]=192|h>>6,this[e++]=128|63&h):(this[e++]=224|h>>12,this[e++]=128|63&h>>6,this[e++]=128|63&h)}return this._charsWritten=e,f},i.utf8Slice=function(a,b){for(var d,g,h,c="",e=a,f=0;b>f&&(d=this[e]);e++)f++,128>d?c+=j(d):d>191&&224>d?(g=this[e+1],c+=j((31&d)<<6|63&g),e++):(g=this[e+1],h=this[e+2],c+=j((15&d)<<12|(63&g)<<6|63&h),e+=2);return c},i.asciiWrite=function(a,b,c){for(var d=0,e=a.length;c>d&&e>d;d++)this[d+b]=a.charCodeAt(d);return this._charsWritten=d,e},i.asciiSlice=function(a,b){for(var c="",d=a;b>d;d++)c+=j(this[d]);return c},i.hexWrite=function(a,b,c){function k(a){for(var b=0;be&&f>e;e+=2,g++){var h=a[e],i=a[e+1],j=k(h)<<4|k(i);this[g+b]=j}},i.hexSlice=function(a,b){var c=this.length;(!a||0>a)&&(a=0),(!b||0>b||b>c)&&(b=c);for(var d="",e=a;b>e;e++)d+=k(this[e]);return d},i.copy=function(a,b,c,d){for(var e=0,f=d-c;f>e;e++)a[e+b]=this[e+c]},i.base64Slice=function(a,b){var c=this.length;(!a||0>a)&&(a=0),(!b||0>b||b>c)&&(b=c);for(var d="",e=a;b>e;e++)d+=window.btoa(this[e]);return d},i.base64Write=function(a,b,c){for(var d=0,e=a.length;c>d&&e>d;d++)this[d+b]=window.atob(a.charCodeAt(d));return this._charsWritten=d,e},i.slice=function(a,b){if(void 0===b&&(b=this.length),b>this.length)throw new Error("oob");if(a>b)throw new Error("oob");return g(this.buffer,+a,b-a)},i.toBlob=function(){var a=new(window.BlobBuilder||window.WebKitBlobBuilder||window.MozBlobBuilder);return this.offset?a.append(this.toString("utf8")):a.append(this.buffer),a.getBlob()},i.readUInt8=function(a,b){var d=this;return b||(c(void 0!==a&&null!==a,"missing offset"),c(a=0?e.writeUInt8(a,b,d):e.writeUInt8(255+a+1,b,d)},i.writeInt16LE=function(a,b,c){x(this,a,b,!1,c)},i.writeInt16BE=function(a,b,c){x(this,a,b,!0,c)},i.writeInt32LE=function(a,b,c){y(this,a,b,!1,c)},i.writeInt32BE=function(a,b,c){y(this,a,b,!0,c)},i.writeFloatLE=function(a,b,c){A(this,a,b,!1,c)},i.writeFloatBE=function(a,b,c){A(this,a,b,!0,c)},i.writeDoubleLE=function(a,b,c){B(this,a,b,!1,c)},i.writeDoubleBE=function(a,b,c){B(this,a,b,!0,c)};var C;h.poolSize=8192,h.isBuffer=function(a){return a instanceof h||a instanceof d},h.byteLength=function(a,b){switch(b){case"ascii":return a.length}for(var f,c=0,d=0,e=a.length;e>c;c++)f=a.charCodeAt(c),128>f?d++:d+=f>127&&2048>f?2:3;return d},window.Buffer=h}(); diff --git a/src/browser-buffer.js b/src/browser-buffer.js index 91af07d..c621184 100644 --- a/src/browser-buffer.js +++ b/src/browser-buffer.js @@ -183,8 +183,8 @@ encoding = String(encoding || 'utf8').toLowerCase(); switch (encoding) { - /*case 'hex': - return this.hexWrite(string, offset, length);*/ + case 'hex': + return this.hexWrite(string, offset, length); case 'utf8': case 'utf-8': @@ -269,6 +269,24 @@ return n.toString(16); } + proto.hexWrite = function(string, start, end) { + var charset = '0123456789abcdef'; + string = string.toLowerCase(); + for (var i = 0, le = string.length, byteCount = 0; i < end && i < le; i += 2, byteCount++){ + var p1 = string[i]; + var p2 = string[i+1]; + var b = ((charVal(p1) << 4) | charVal(p2)); + this[byteCount + start] = b; + } + + function charVal(c){ + for (var i = 0; i < charset.length; i++){ + if (c == charset[i]) return i; + } + throw new Error('Invalid hex string'); + } + } + proto.hexSlice = function (start, end) { var len = this.length; From d2aca2e3bdabfd28f52322208cd3db1e616eac24 Mon Sep 17 00:00:00 2001 From: Syrian Watermelon Date: Wed, 14 May 2014 14:36:27 +0200 Subject: [PATCH 02/11] Fixing a bug in byteLength in case encoding == 'hex' --- browser-buffer.min.js | 2 +- src/browser-buffer.js | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/browser-buffer.min.js b/browser-buffer.min.js index 11c29bb..8ff0c5c 100644 --- a/browser-buffer.min.js +++ b/browser-buffer.min.js @@ -1,2 +1,2 @@ // Based off of buffer.js in the Node project(copyright Joyent, Inc. and other Node contributors.) -!function(){"use strict";function a(a){return h.isBuffer(a)||a&&"object"==typeof a&&"number"==typeof a.length}function b(a){return a=~~Math.ceil(+a),0>a?0:a}function c(a,b){if(!a)throw new Error(b)}function k(a){return 16>a?"0"+a.toString(16):a.toString(16)}function l(a,b,d,e){var f=0;return e||(c("boolean"==typeof d,"missing or invalid endian"),c(void 0!==b&&null!==b,"missing offset"),c(b+1>>0):(f=a[b+2]<<16,f|=a[b+1]<<8,f|=a[b],f+=a[b+3]<<24>>>0),f}function n(a,b,d,e){var f,g;return e||(c("boolean"==typeof d,"missing or invalid endian"),c(void 0!==b&&null!==b,"missing offset"),c(b+1>1,k=-7,l=c?0:e-1,m=c?1:-1,n=a[b+l];for(l+=m,f=n&(1<<-k)-1,n>>=-k,k+=h;k>0;f=256*f+a[b+l],l+=m,k-=8);for(g=f&(1<<-k)-1,f>>=-k,k+=d;k>0;g=256*g+a[b+l],l+=m,k-=8);if(0===f)f=1-j;else{if(f===i)return g?0/0:1/0*(n?-1:1);g+=Math.pow(2,d),f-=j}return(n?-1:1)*g*Math.pow(2,f-d)}function q(a,b,d,e){return e||(c("boolean"==typeof d,"missing or invalid endian"),c(b+3=0,"specified a negative value for writing an unsigned value"),c(b>=a,"value is larger than maximum value for type"),c(Math.floor(a)===a,"value has a fractional component")}function t(a,b,d,e,f){f||(c(void 0!==b&&null!==b,"missing value"),c("boolean"==typeof e,"missing or invalid endian"),c(void 0!==d&&null!==d,"missing offset"),c(d+1>>8,a[d+1]=255&b):(a[d+1]=(65280&b)>>>8,a[d]=255&b)}function u(a,b,d,e,f){f||(c(void 0!==b&&null!==b,"missing value"),c("boolean"==typeof e,"missing or invalid endian"),c(void 0!==d&&null!==d,"missing offset"),c(d+3>>24,a[d+1]=255&b>>>16,a[d+2]=255&b>>>8,a[d+3]=255&b):(a[d+3]=255&b>>>24,a[d+2]=255&b>>>16,a[d+1]=255&b>>>8,a[d]=255&b)}function v(a,b,d){c("number"==typeof a,"cannot write a non-number as a number"),c(b>=a,"value larger than maximum allowed value"),c(a>=d,"value smaller than minimum allowed value"),c(Math.floor(a)===a,"value has a fractional component")}function w(a,b,d){c("number"==typeof a,"cannot write a non-number as a number"),c(b>=a,"value larger than maximum allowed value"),c(a>=d,"value smaller than minimum allowed value")}function x(a,b,d,e,f){f||(c(void 0!==b&&null!==b,"missing value"),c("boolean"==typeof e,"missing or invalid endian"),c(void 0!==d&&null!==d,"missing offset"),c(d+1=0?t(a,b,d,e,f):t(a,65535+b+1,d,e,f)}function y(a,b,d,e,f){f||(c(void 0!==b&&null!==b,"missing value"),c("boolean"==typeof e,"missing or invalid endian"),c(void 0!==d&&null!==d,"missing offset"),c(d+3=0?u(a,b,d,e,f):u(a,4294967295+b+1,d,e,f)}function z(a,b,c,d,e,f){var g,h,i,j=8*f-e-1,k=(1<>1,m=23===e?Math.pow(2,-24)-Math.pow(2,-77):0,n=d?f-1:0,o=d?-1:1,p=0>b||0===b&&0>1/b?1:0;for(b=Math.abs(b),isNaN(b)||1/0===b?(h=isNaN(b)?1:0,g=k):(g=Math.floor(Math.log(b)/Math.LN2),b*(i=Math.pow(2,-g))<1&&(g--,i*=2),b+=g+l>=1?m/i:m*Math.pow(2,1-l),b*i>=2&&(g++,i/=2),g+l>=k?(h=0,g=k):g+l>=1?(h=(b*i-1)*Math.pow(2,e),g+=l):(h=b*Math.pow(2,l-1)*Math.pow(2,e),g=0));e>=8;a[c+n]=255&h,n+=o,h/=256,e-=8);for(g=g<0;a[c+n]=255&g,n+=o,g/=256,j-=8);a[c+n-o]|=128*p}function A(a,b,d,e,f){f||(c(void 0!==b&&null!==b,"missing value"),c("boolean"==typeof e,"missing or invalid endian"),c(void 0!==d&&null!==d,"missing offset"),c(d+3h.poolSize?(k=new d(j),f=0):((!C||C.byteLength-C.usedf&&(c=f)):c=f,d=String(d||"utf8").toLowerCase()){case"hex":return this.hexWrite(a,b,c);case"utf8":case"utf-8":return this.utf8Write(a,b,c);case"ascii":return this.asciiWrite(a,b,c);case"base64":return this.base64Write(a,b,c);default:throw new Error("Unknown encoding")}};var j=String.fromCharCode;i.utf8Write=function(a,b,c){for(var d=0,e=b,f=a.length,g=c-b;g>d&&f>d;d++){var h=a.charCodeAt(d);128>h?this[e++]=h:h>127&&2048>h?(this[e++]=192|h>>6,this[e++]=128|63&h):(this[e++]=224|h>>12,this[e++]=128|63&h>>6,this[e++]=128|63&h)}return this._charsWritten=e,f},i.utf8Slice=function(a,b){for(var d,g,h,c="",e=a,f=0;b>f&&(d=this[e]);e++)f++,128>d?c+=j(d):d>191&&224>d?(g=this[e+1],c+=j((31&d)<<6|63&g),e++):(g=this[e+1],h=this[e+2],c+=j((15&d)<<12|(63&g)<<6|63&h),e+=2);return c},i.asciiWrite=function(a,b,c){for(var d=0,e=a.length;c>d&&e>d;d++)this[d+b]=a.charCodeAt(d);return this._charsWritten=d,e},i.asciiSlice=function(a,b){for(var c="",d=a;b>d;d++)c+=j(this[d]);return c},i.hexWrite=function(a,b,c){function k(a){for(var b=0;be&&f>e;e+=2,g++){var h=a[e],i=a[e+1],j=k(h)<<4|k(i);this[g+b]=j}},i.hexSlice=function(a,b){var c=this.length;(!a||0>a)&&(a=0),(!b||0>b||b>c)&&(b=c);for(var d="",e=a;b>e;e++)d+=k(this[e]);return d},i.copy=function(a,b,c,d){for(var e=0,f=d-c;f>e;e++)a[e+b]=this[e+c]},i.base64Slice=function(a,b){var c=this.length;(!a||0>a)&&(a=0),(!b||0>b||b>c)&&(b=c);for(var d="",e=a;b>e;e++)d+=window.btoa(this[e]);return d},i.base64Write=function(a,b,c){for(var d=0,e=a.length;c>d&&e>d;d++)this[d+b]=window.atob(a.charCodeAt(d));return this._charsWritten=d,e},i.slice=function(a,b){if(void 0===b&&(b=this.length),b>this.length)throw new Error("oob");if(a>b)throw new Error("oob");return g(this.buffer,+a,b-a)},i.toBlob=function(){var a=new(window.BlobBuilder||window.WebKitBlobBuilder||window.MozBlobBuilder);return this.offset?a.append(this.toString("utf8")):a.append(this.buffer),a.getBlob()},i.readUInt8=function(a,b){var d=this;return b||(c(void 0!==a&&null!==a,"missing offset"),c(a=0?e.writeUInt8(a,b,d):e.writeUInt8(255+a+1,b,d)},i.writeInt16LE=function(a,b,c){x(this,a,b,!1,c)},i.writeInt16BE=function(a,b,c){x(this,a,b,!0,c)},i.writeInt32LE=function(a,b,c){y(this,a,b,!1,c)},i.writeInt32BE=function(a,b,c){y(this,a,b,!0,c)},i.writeFloatLE=function(a,b,c){A(this,a,b,!1,c)},i.writeFloatBE=function(a,b,c){A(this,a,b,!0,c)},i.writeDoubleLE=function(a,b,c){B(this,a,b,!1,c)},i.writeDoubleBE=function(a,b,c){B(this,a,b,!0,c)};var C;h.poolSize=8192,h.isBuffer=function(a){return a instanceof h||a instanceof d},h.byteLength=function(a,b){switch(b){case"ascii":return a.length}for(var f,c=0,d=0,e=a.length;e>c;c++)f=a.charCodeAt(c),128>f?d++:d+=f>127&&2048>f?2:3;return d},window.Buffer=h}(); +!function(){"use strict";function a(a){return h.isBuffer(a)||a&&"object"==typeof a&&"number"==typeof a.length}function b(a){return a=~~Math.ceil(+a),0>a?0:a}function c(a,b){if(!a)throw new Error(b)}function k(a){return 16>a?"0"+a.toString(16):a.toString(16)}function l(a,b,d,e){var f=0;return e||(c("boolean"==typeof d,"missing or invalid endian"),c(void 0!==b&&null!==b,"missing offset"),c(b+1>>0):(f=a[b+2]<<16,f|=a[b+1]<<8,f|=a[b],f+=a[b+3]<<24>>>0),f}function n(a,b,d,e){var f,g;return e||(c("boolean"==typeof d,"missing or invalid endian"),c(void 0!==b&&null!==b,"missing offset"),c(b+1>1,k=-7,l=c?0:e-1,m=c?1:-1,n=a[b+l];for(l+=m,f=n&(1<<-k)-1,n>>=-k,k+=h;k>0;f=256*f+a[b+l],l+=m,k-=8);for(g=f&(1<<-k)-1,f>>=-k,k+=d;k>0;g=256*g+a[b+l],l+=m,k-=8);if(0===f)f=1-j;else{if(f===i)return g?0/0:1/0*(n?-1:1);g+=Math.pow(2,d),f-=j}return(n?-1:1)*g*Math.pow(2,f-d)}function q(a,b,d,e){return e||(c("boolean"==typeof d,"missing or invalid endian"),c(b+3=0,"specified a negative value for writing an unsigned value"),c(b>=a,"value is larger than maximum value for type"),c(Math.floor(a)===a,"value has a fractional component")}function t(a,b,d,e,f){f||(c(void 0!==b&&null!==b,"missing value"),c("boolean"==typeof e,"missing or invalid endian"),c(void 0!==d&&null!==d,"missing offset"),c(d+1>>8,a[d+1]=255&b):(a[d+1]=(65280&b)>>>8,a[d]=255&b)}function u(a,b,d,e,f){f||(c(void 0!==b&&null!==b,"missing value"),c("boolean"==typeof e,"missing or invalid endian"),c(void 0!==d&&null!==d,"missing offset"),c(d+3>>24,a[d+1]=255&b>>>16,a[d+2]=255&b>>>8,a[d+3]=255&b):(a[d+3]=255&b>>>24,a[d+2]=255&b>>>16,a[d+1]=255&b>>>8,a[d]=255&b)}function v(a,b,d){c("number"==typeof a,"cannot write a non-number as a number"),c(b>=a,"value larger than maximum allowed value"),c(a>=d,"value smaller than minimum allowed value"),c(Math.floor(a)===a,"value has a fractional component")}function w(a,b,d){c("number"==typeof a,"cannot write a non-number as a number"),c(b>=a,"value larger than maximum allowed value"),c(a>=d,"value smaller than minimum allowed value")}function x(a,b,d,e,f){f||(c(void 0!==b&&null!==b,"missing value"),c("boolean"==typeof e,"missing or invalid endian"),c(void 0!==d&&null!==d,"missing offset"),c(d+1=0?t(a,b,d,e,f):t(a,65535+b+1,d,e,f)}function y(a,b,d,e,f){f||(c(void 0!==b&&null!==b,"missing value"),c("boolean"==typeof e,"missing or invalid endian"),c(void 0!==d&&null!==d,"missing offset"),c(d+3=0?u(a,b,d,e,f):u(a,4294967295+b+1,d,e,f)}function z(a,b,c,d,e,f){var g,h,i,j=8*f-e-1,k=(1<>1,m=23===e?Math.pow(2,-24)-Math.pow(2,-77):0,n=d?f-1:0,o=d?-1:1,p=0>b||0===b&&0>1/b?1:0;for(b=Math.abs(b),isNaN(b)||1/0===b?(h=isNaN(b)?1:0,g=k):(g=Math.floor(Math.log(b)/Math.LN2),b*(i=Math.pow(2,-g))<1&&(g--,i*=2),b+=g+l>=1?m/i:m*Math.pow(2,1-l),b*i>=2&&(g++,i/=2),g+l>=k?(h=0,g=k):g+l>=1?(h=(b*i-1)*Math.pow(2,e),g+=l):(h=b*Math.pow(2,l-1)*Math.pow(2,e),g=0));e>=8;a[c+n]=255&h,n+=o,h/=256,e-=8);for(g=g<0;a[c+n]=255&g,n+=o,g/=256,j-=8);a[c+n-o]|=128*p}function A(a,b,d,e,f){f||(c(void 0!==b&&null!==b,"missing value"),c("boolean"==typeof e,"missing or invalid endian"),c(void 0!==d&&null!==d,"missing offset"),c(d+3h.poolSize?(k=new d(j),f=0):((!C||C.byteLength-C.usedf&&(c=f)):c=f,d=String(d||"utf8").toLowerCase()){case"hex":return this.hexWrite(a,b,c);case"utf8":case"utf-8":return this.utf8Write(a,b,c);case"ascii":return this.asciiWrite(a,b,c);case"base64":return this.base64Write(a,b,c);default:throw new Error("Unknown encoding")}};var j=String.fromCharCode;i.utf8Write=function(a,b,c){for(var d=0,e=b,f=a.length,g=c-b;g>d&&f>d;d++){var h=a.charCodeAt(d);128>h?this[e++]=h:h>127&&2048>h?(this[e++]=192|h>>6,this[e++]=128|63&h):(this[e++]=224|h>>12,this[e++]=128|63&h>>6,this[e++]=128|63&h)}return this._charsWritten=e,f},i.utf8Slice=function(a,b){for(var d,g,h,c="",e=a,f=0;b>f&&(d=this[e]);e++)f++,128>d?c+=j(d):d>191&&224>d?(g=this[e+1],c+=j((31&d)<<6|63&g),e++):(g=this[e+1],h=this[e+2],c+=j((15&d)<<12|(63&g)<<6|63&h),e+=2);return c},i.asciiWrite=function(a,b,c){for(var d=0,e=a.length;c>d&&e>d;d++)this[d+b]=a.charCodeAt(d);return this._charsWritten=d,e},i.asciiSlice=function(a,b){for(var c="",d=a;b>d;d++)c+=j(this[d]);return c},i.hexWrite=function(a,b,c){function k(a){for(var b=0;be&&f>e;e+=2,g++){var h=a[e],i=a[e+1],j=k(h)<<4|k(i);this[g+b]=j}},i.hexSlice=function(a,b){var c=this.length;(!a||0>a)&&(a=0),(!b||0>b||b>c)&&(b=c);for(var d="",e=a;b>e;e++)d+=k(this[e]);return d},i.copy=function(a,b,c,d){for(var e=0,f=d-c;f>e;e++)a[e+b]=this[e+c]},i.base64Slice=function(a,b){var c=this.length;(!a||0>a)&&(a=0),(!b||0>b||b>c)&&(b=c);for(var d="",e=a;b>e;e++)d+=window.btoa(this[e]);return d},i.base64Write=function(a,b,c){for(var d=0,e=a.length;c>d&&e>d;d++)this[d+b]=window.atob(a.charCodeAt(d));return this._charsWritten=d,e},i.slice=function(a,b){if(void 0===b&&(b=this.length),b>this.length)throw new Error("oob");if(a>b)throw new Error("oob");return g(this.buffer,+a,b-a)},i.toBlob=function(){var a=new(window.BlobBuilder||window.WebKitBlobBuilder||window.MozBlobBuilder);return this.offset?a.append(this.toString("utf8")):a.append(this.buffer),a.getBlob()},i.readUInt8=function(a,b){var d=this;return b||(c(void 0!==a&&null!==a,"missing offset"),c(a=0?e.writeUInt8(a,b,d):e.writeUInt8(255+a+1,b,d)},i.writeInt16LE=function(a,b,c){x(this,a,b,!1,c)},i.writeInt16BE=function(a,b,c){x(this,a,b,!0,c)},i.writeInt32LE=function(a,b,c){y(this,a,b,!1,c)},i.writeInt32BE=function(a,b,c){y(this,a,b,!0,c)},i.writeFloatLE=function(a,b,c){A(this,a,b,!1,c)},i.writeFloatBE=function(a,b,c){A(this,a,b,!0,c)},i.writeDoubleLE=function(a,b,c){B(this,a,b,!1,c)},i.writeDoubleBE=function(a,b,c){B(this,a,b,!0,c)};var C;h.poolSize=8192,h.isBuffer=function(a){return a instanceof h||a instanceof d},h.byteLength=function(a,b){switch(b){case"ascii":return a.length;case"hex":return a.length/2}for(var f,c=0,d=0,e=a.length;e>c;c++)f=a.charCodeAt(c),128>f?d++:d+=f>127&&2048>f?2:3;return d},window.Buffer=h}(); diff --git a/src/browser-buffer.js b/src/browser-buffer.js index c621184..30e76be 100644 --- a/src/browser-buffer.js +++ b/src/browser-buffer.js @@ -971,6 +971,8 @@ switch (encoding) { case "ascii": return string.length; + case 'hex': + return string.length / 2; } for (var i = 0, l = 0, le = string.length, c; i < le; i++) { c = string.charCodeAt(i); From e2425bb69fe52a617ff01c286e50d418210a8850 Mon Sep 17 00:00:00 2001 From: Syrian Watermelon Date: Mon, 19 May 2014 11:54:16 +0200 Subject: [PATCH 03/11] fixed a bug in hexWrite that caused the method only half the buffer. Minified again. --- browser-buffer.min.js | 2 +- src/browser-buffer.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/browser-buffer.min.js b/browser-buffer.min.js index 8ff0c5c..15a3f58 100644 --- a/browser-buffer.min.js +++ b/browser-buffer.min.js @@ -1,2 +1,2 @@ // Based off of buffer.js in the Node project(copyright Joyent, Inc. and other Node contributors.) -!function(){"use strict";function a(a){return h.isBuffer(a)||a&&"object"==typeof a&&"number"==typeof a.length}function b(a){return a=~~Math.ceil(+a),0>a?0:a}function c(a,b){if(!a)throw new Error(b)}function k(a){return 16>a?"0"+a.toString(16):a.toString(16)}function l(a,b,d,e){var f=0;return e||(c("boolean"==typeof d,"missing or invalid endian"),c(void 0!==b&&null!==b,"missing offset"),c(b+1>>0):(f=a[b+2]<<16,f|=a[b+1]<<8,f|=a[b],f+=a[b+3]<<24>>>0),f}function n(a,b,d,e){var f,g;return e||(c("boolean"==typeof d,"missing or invalid endian"),c(void 0!==b&&null!==b,"missing offset"),c(b+1>1,k=-7,l=c?0:e-1,m=c?1:-1,n=a[b+l];for(l+=m,f=n&(1<<-k)-1,n>>=-k,k+=h;k>0;f=256*f+a[b+l],l+=m,k-=8);for(g=f&(1<<-k)-1,f>>=-k,k+=d;k>0;g=256*g+a[b+l],l+=m,k-=8);if(0===f)f=1-j;else{if(f===i)return g?0/0:1/0*(n?-1:1);g+=Math.pow(2,d),f-=j}return(n?-1:1)*g*Math.pow(2,f-d)}function q(a,b,d,e){return e||(c("boolean"==typeof d,"missing or invalid endian"),c(b+3=0,"specified a negative value for writing an unsigned value"),c(b>=a,"value is larger than maximum value for type"),c(Math.floor(a)===a,"value has a fractional component")}function t(a,b,d,e,f){f||(c(void 0!==b&&null!==b,"missing value"),c("boolean"==typeof e,"missing or invalid endian"),c(void 0!==d&&null!==d,"missing offset"),c(d+1>>8,a[d+1]=255&b):(a[d+1]=(65280&b)>>>8,a[d]=255&b)}function u(a,b,d,e,f){f||(c(void 0!==b&&null!==b,"missing value"),c("boolean"==typeof e,"missing or invalid endian"),c(void 0!==d&&null!==d,"missing offset"),c(d+3>>24,a[d+1]=255&b>>>16,a[d+2]=255&b>>>8,a[d+3]=255&b):(a[d+3]=255&b>>>24,a[d+2]=255&b>>>16,a[d+1]=255&b>>>8,a[d]=255&b)}function v(a,b,d){c("number"==typeof a,"cannot write a non-number as a number"),c(b>=a,"value larger than maximum allowed value"),c(a>=d,"value smaller than minimum allowed value"),c(Math.floor(a)===a,"value has a fractional component")}function w(a,b,d){c("number"==typeof a,"cannot write a non-number as a number"),c(b>=a,"value larger than maximum allowed value"),c(a>=d,"value smaller than minimum allowed value")}function x(a,b,d,e,f){f||(c(void 0!==b&&null!==b,"missing value"),c("boolean"==typeof e,"missing or invalid endian"),c(void 0!==d&&null!==d,"missing offset"),c(d+1=0?t(a,b,d,e,f):t(a,65535+b+1,d,e,f)}function y(a,b,d,e,f){f||(c(void 0!==b&&null!==b,"missing value"),c("boolean"==typeof e,"missing or invalid endian"),c(void 0!==d&&null!==d,"missing offset"),c(d+3=0?u(a,b,d,e,f):u(a,4294967295+b+1,d,e,f)}function z(a,b,c,d,e,f){var g,h,i,j=8*f-e-1,k=(1<>1,m=23===e?Math.pow(2,-24)-Math.pow(2,-77):0,n=d?f-1:0,o=d?-1:1,p=0>b||0===b&&0>1/b?1:0;for(b=Math.abs(b),isNaN(b)||1/0===b?(h=isNaN(b)?1:0,g=k):(g=Math.floor(Math.log(b)/Math.LN2),b*(i=Math.pow(2,-g))<1&&(g--,i*=2),b+=g+l>=1?m/i:m*Math.pow(2,1-l),b*i>=2&&(g++,i/=2),g+l>=k?(h=0,g=k):g+l>=1?(h=(b*i-1)*Math.pow(2,e),g+=l):(h=b*Math.pow(2,l-1)*Math.pow(2,e),g=0));e>=8;a[c+n]=255&h,n+=o,h/=256,e-=8);for(g=g<0;a[c+n]=255&g,n+=o,g/=256,j-=8);a[c+n-o]|=128*p}function A(a,b,d,e,f){f||(c(void 0!==b&&null!==b,"missing value"),c("boolean"==typeof e,"missing or invalid endian"),c(void 0!==d&&null!==d,"missing offset"),c(d+3h.poolSize?(k=new d(j),f=0):((!C||C.byteLength-C.usedf&&(c=f)):c=f,d=String(d||"utf8").toLowerCase()){case"hex":return this.hexWrite(a,b,c);case"utf8":case"utf-8":return this.utf8Write(a,b,c);case"ascii":return this.asciiWrite(a,b,c);case"base64":return this.base64Write(a,b,c);default:throw new Error("Unknown encoding")}};var j=String.fromCharCode;i.utf8Write=function(a,b,c){for(var d=0,e=b,f=a.length,g=c-b;g>d&&f>d;d++){var h=a.charCodeAt(d);128>h?this[e++]=h:h>127&&2048>h?(this[e++]=192|h>>6,this[e++]=128|63&h):(this[e++]=224|h>>12,this[e++]=128|63&h>>6,this[e++]=128|63&h)}return this._charsWritten=e,f},i.utf8Slice=function(a,b){for(var d,g,h,c="",e=a,f=0;b>f&&(d=this[e]);e++)f++,128>d?c+=j(d):d>191&&224>d?(g=this[e+1],c+=j((31&d)<<6|63&g),e++):(g=this[e+1],h=this[e+2],c+=j((15&d)<<12|(63&g)<<6|63&h),e+=2);return c},i.asciiWrite=function(a,b,c){for(var d=0,e=a.length;c>d&&e>d;d++)this[d+b]=a.charCodeAt(d);return this._charsWritten=d,e},i.asciiSlice=function(a,b){for(var c="",d=a;b>d;d++)c+=j(this[d]);return c},i.hexWrite=function(a,b,c){function k(a){for(var b=0;be&&f>e;e+=2,g++){var h=a[e],i=a[e+1],j=k(h)<<4|k(i);this[g+b]=j}},i.hexSlice=function(a,b){var c=this.length;(!a||0>a)&&(a=0),(!b||0>b||b>c)&&(b=c);for(var d="",e=a;b>e;e++)d+=k(this[e]);return d},i.copy=function(a,b,c,d){for(var e=0,f=d-c;f>e;e++)a[e+b]=this[e+c]},i.base64Slice=function(a,b){var c=this.length;(!a||0>a)&&(a=0),(!b||0>b||b>c)&&(b=c);for(var d="",e=a;b>e;e++)d+=window.btoa(this[e]);return d},i.base64Write=function(a,b,c){for(var d=0,e=a.length;c>d&&e>d;d++)this[d+b]=window.atob(a.charCodeAt(d));return this._charsWritten=d,e},i.slice=function(a,b){if(void 0===b&&(b=this.length),b>this.length)throw new Error("oob");if(a>b)throw new Error("oob");return g(this.buffer,+a,b-a)},i.toBlob=function(){var a=new(window.BlobBuilder||window.WebKitBlobBuilder||window.MozBlobBuilder);return this.offset?a.append(this.toString("utf8")):a.append(this.buffer),a.getBlob()},i.readUInt8=function(a,b){var d=this;return b||(c(void 0!==a&&null!==a,"missing offset"),c(a=0?e.writeUInt8(a,b,d):e.writeUInt8(255+a+1,b,d)},i.writeInt16LE=function(a,b,c){x(this,a,b,!1,c)},i.writeInt16BE=function(a,b,c){x(this,a,b,!0,c)},i.writeInt32LE=function(a,b,c){y(this,a,b,!1,c)},i.writeInt32BE=function(a,b,c){y(this,a,b,!0,c)},i.writeFloatLE=function(a,b,c){A(this,a,b,!1,c)},i.writeFloatBE=function(a,b,c){A(this,a,b,!0,c)},i.writeDoubleLE=function(a,b,c){B(this,a,b,!1,c)},i.writeDoubleBE=function(a,b,c){B(this,a,b,!0,c)};var C;h.poolSize=8192,h.isBuffer=function(a){return a instanceof h||a instanceof d},h.byteLength=function(a,b){switch(b){case"ascii":return a.length;case"hex":return a.length/2}for(var f,c=0,d=0,e=a.length;e>c;c++)f=a.charCodeAt(c),128>f?d++:d+=f>127&&2048>f?2:3;return d},window.Buffer=h}(); +!function(){"use strict";function a(a){return h.isBuffer(a)||a&&"object"==typeof a&&"number"==typeof a.length}function b(a){return a=~~Math.ceil(+a),0>a?0:a}function c(a,b){if(!a)throw new Error(b)}function k(a){return 16>a?"0"+a.toString(16):a.toString(16)}function l(a,b,d,e){var f=0;return e||(c("boolean"==typeof d,"missing or invalid endian"),c(void 0!==b&&null!==b,"missing offset"),c(b+1>>0):(f=a[b+2]<<16,f|=a[b+1]<<8,f|=a[b],f+=a[b+3]<<24>>>0),f}function n(a,b,d,e){var f,g;return e||(c("boolean"==typeof d,"missing or invalid endian"),c(void 0!==b&&null!==b,"missing offset"),c(b+1>1,k=-7,l=c?0:e-1,m=c?1:-1,n=a[b+l];for(l+=m,f=n&(1<<-k)-1,n>>=-k,k+=h;k>0;f=256*f+a[b+l],l+=m,k-=8);for(g=f&(1<<-k)-1,f>>=-k,k+=d;k>0;g=256*g+a[b+l],l+=m,k-=8);if(0===f)f=1-j;else{if(f===i)return g?0/0:1/0*(n?-1:1);g+=Math.pow(2,d),f-=j}return(n?-1:1)*g*Math.pow(2,f-d)}function q(a,b,d,e){return e||(c("boolean"==typeof d,"missing or invalid endian"),c(b+3=0,"specified a negative value for writing an unsigned value"),c(b>=a,"value is larger than maximum value for type"),c(Math.floor(a)===a,"value has a fractional component")}function t(a,b,d,e,f){f||(c(void 0!==b&&null!==b,"missing value"),c("boolean"==typeof e,"missing or invalid endian"),c(void 0!==d&&null!==d,"missing offset"),c(d+1>>8,a[d+1]=255&b):(a[d+1]=(65280&b)>>>8,a[d]=255&b)}function u(a,b,d,e,f){f||(c(void 0!==b&&null!==b,"missing value"),c("boolean"==typeof e,"missing or invalid endian"),c(void 0!==d&&null!==d,"missing offset"),c(d+3>>24,a[d+1]=255&b>>>16,a[d+2]=255&b>>>8,a[d+3]=255&b):(a[d+3]=255&b>>>24,a[d+2]=255&b>>>16,a[d+1]=255&b>>>8,a[d]=255&b)}function v(a,b,d){c("number"==typeof a,"cannot write a non-number as a number"),c(b>=a,"value larger than maximum allowed value"),c(a>=d,"value smaller than minimum allowed value"),c(Math.floor(a)===a,"value has a fractional component")}function w(a,b,d){c("number"==typeof a,"cannot write a non-number as a number"),c(b>=a,"value larger than maximum allowed value"),c(a>=d,"value smaller than minimum allowed value")}function x(a,b,d,e,f){f||(c(void 0!==b&&null!==b,"missing value"),c("boolean"==typeof e,"missing or invalid endian"),c(void 0!==d&&null!==d,"missing offset"),c(d+1=0?t(a,b,d,e,f):t(a,65535+b+1,d,e,f)}function y(a,b,d,e,f){f||(c(void 0!==b&&null!==b,"missing value"),c("boolean"==typeof e,"missing or invalid endian"),c(void 0!==d&&null!==d,"missing offset"),c(d+3=0?u(a,b,d,e,f):u(a,4294967295+b+1,d,e,f)}function z(a,b,c,d,e,f){var g,h,i,j=8*f-e-1,k=(1<>1,m=23===e?Math.pow(2,-24)-Math.pow(2,-77):0,n=d?f-1:0,o=d?-1:1,p=0>b||0===b&&0>1/b?1:0;for(b=Math.abs(b),isNaN(b)||1/0===b?(h=isNaN(b)?1:0,g=k):(g=Math.floor(Math.log(b)/Math.LN2),b*(i=Math.pow(2,-g))<1&&(g--,i*=2),b+=g+l>=1?m/i:m*Math.pow(2,1-l),b*i>=2&&(g++,i/=2),g+l>=k?(h=0,g=k):g+l>=1?(h=(b*i-1)*Math.pow(2,e),g+=l):(h=b*Math.pow(2,l-1)*Math.pow(2,e),g=0));e>=8;a[c+n]=255&h,n+=o,h/=256,e-=8);for(g=g<0;a[c+n]=255&g,n+=o,g/=256,j-=8);a[c+n-o]|=128*p}function A(a,b,d,e,f){f||(c(void 0!==b&&null!==b,"missing value"),c("boolean"==typeof e,"missing or invalid endian"),c(void 0!==d&&null!==d,"missing offset"),c(d+3h.poolSize?(k=new d(j),f=0):((!C||C.byteLength-C.usedf&&(c=f)):c=f,d=String(d||"utf8").toLowerCase()){case"hex":return this.hexWrite(a,b,c);case"utf8":case"utf-8":return this.utf8Write(a,b,c);case"ascii":return this.asciiWrite(a,b,c);case"base64":return this.base64Write(a,b,c);default:throw new Error("Unknown encoding")}};var j=String.fromCharCode;i.utf8Write=function(a,b,c){for(var d=0,e=b,f=a.length,g=c-b;g>d&&f>d;d++){var h=a.charCodeAt(d);128>h?this[e++]=h:h>127&&2048>h?(this[e++]=192|h>>6,this[e++]=128|63&h):(this[e++]=224|h>>12,this[e++]=128|63&h>>6,this[e++]=128|63&h)}return this._charsWritten=e,f},i.utf8Slice=function(a,b){for(var d,g,h,c="",e=a,f=0;b>f&&(d=this[e]);e++)f++,128>d?c+=j(d):d>191&&224>d?(g=this[e+1],c+=j((31&d)<<6|63&g),e++):(g=this[e+1],h=this[e+2],c+=j((15&d)<<12|(63&g)<<6|63&h),e+=2);return c},i.asciiWrite=function(a,b,c){for(var d=0,e=a.length;c>d&&e>d;d++)this[d+b]=a.charCodeAt(d);return this._charsWritten=d,e},i.asciiSlice=function(a,b){for(var c="",d=a;b>d;d++)c+=j(this[d]);return c},i.hexWrite=function(a,b,c){function k(a){for(var b=0;be/2&&f>e;e+=2,g++){var h=a[e],i=a[e+1],j=k(h)<<4|k(i);this[g+b]=j}},i.hexSlice=function(a,b){var c=this.length;(!a||0>a)&&(a=0),(!b||0>b||b>c)&&(b=c);for(var d="",e=a;b>e;e++)d+=k(this[e]);return d},i.copy=function(a,b,c,d){for(var e=0,f=d-c;f>e;e++)a[e+b]=this[e+c]},i.base64Slice=function(a,b){var c=this.length;(!a||0>a)&&(a=0),(!b||0>b||b>c)&&(b=c);for(var d="",e=a;b>e;e++)d+=window.btoa(this[e]);return d},i.base64Write=function(a,b,c){for(var d=0,e=a.length;c>d&&e>d;d++)this[d+b]=window.atob(a.charCodeAt(d));return this._charsWritten=d,e},i.slice=function(a,b){if(void 0===b&&(b=this.length),b>this.length)throw new Error("oob");if(a>b)throw new Error("oob");return g(this.buffer,+a,b-a)},i.toBlob=function(){var a=new(window.BlobBuilder||window.WebKitBlobBuilder||window.MozBlobBuilder);return this.offset?a.append(this.toString("utf8")):a.append(this.buffer),a.getBlob()},i.readUInt8=function(a,b){var d=this;return b||(c(void 0!==a&&null!==a,"missing offset"),c(a=0?e.writeUInt8(a,b,d):e.writeUInt8(255+a+1,b,d)},i.writeInt16LE=function(a,b,c){x(this,a,b,!1,c)},i.writeInt16BE=function(a,b,c){x(this,a,b,!0,c)},i.writeInt32LE=function(a,b,c){y(this,a,b,!1,c)},i.writeInt32BE=function(a,b,c){y(this,a,b,!0,c)},i.writeFloatLE=function(a,b,c){A(this,a,b,!1,c)},i.writeFloatBE=function(a,b,c){A(this,a,b,!0,c)},i.writeDoubleLE=function(a,b,c){B(this,a,b,!1,c)},i.writeDoubleBE=function(a,b,c){B(this,a,b,!0,c)};var C;h.poolSize=8192,h.isBuffer=function(a){return a instanceof h||a instanceof d},h.byteLength=function(a,b){switch(b){case"ascii":return a.length;case"hex":return a.length/2}for(var f,c=0,d=0,e=a.length;e>c;c++)f=a.charCodeAt(c),128>f?d++:d+=f>127&&2048>f?2:3;return d},window.Buffer=h}(); diff --git a/src/browser-buffer.js b/src/browser-buffer.js index 30e76be..5cbe525 100644 --- a/src/browser-buffer.js +++ b/src/browser-buffer.js @@ -272,7 +272,7 @@ proto.hexWrite = function(string, start, end) { var charset = '0123456789abcdef'; string = string.toLowerCase(); - for (var i = 0, le = string.length, byteCount = 0; i < end && i < le; i += 2, byteCount++){ + for (var i = 0, le = string.length, byteCount = 0; (i / 2) < end && i < le; i += 2, byteCount++){ var p1 = string[i]; var p2 = string[i+1]; var b = ((charVal(p1) << 4) | charVal(p2)); From ec23537a784aec80c2c00b8417e72f23d6e687e2 Mon Sep 17 00:00:00 2001 From: Syrian Watermelon Date: Tue, 9 Dec 2014 01:21:28 +0100 Subject: [PATCH 04/11] Potential fix for base64 --- browser-buffer.min.js | 3 +-- src/browser-buffer.js | 12 +++++------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/browser-buffer.min.js b/browser-buffer.min.js index 15a3f58..a072d9d 100644 --- a/browser-buffer.min.js +++ b/browser-buffer.min.js @@ -1,2 +1 @@ -// Based off of buffer.js in the Node project(copyright Joyent, Inc. and other Node contributors.) -!function(){"use strict";function a(a){return h.isBuffer(a)||a&&"object"==typeof a&&"number"==typeof a.length}function b(a){return a=~~Math.ceil(+a),0>a?0:a}function c(a,b){if(!a)throw new Error(b)}function k(a){return 16>a?"0"+a.toString(16):a.toString(16)}function l(a,b,d,e){var f=0;return e||(c("boolean"==typeof d,"missing or invalid endian"),c(void 0!==b&&null!==b,"missing offset"),c(b+1>>0):(f=a[b+2]<<16,f|=a[b+1]<<8,f|=a[b],f+=a[b+3]<<24>>>0),f}function n(a,b,d,e){var f,g;return e||(c("boolean"==typeof d,"missing or invalid endian"),c(void 0!==b&&null!==b,"missing offset"),c(b+1>1,k=-7,l=c?0:e-1,m=c?1:-1,n=a[b+l];for(l+=m,f=n&(1<<-k)-1,n>>=-k,k+=h;k>0;f=256*f+a[b+l],l+=m,k-=8);for(g=f&(1<<-k)-1,f>>=-k,k+=d;k>0;g=256*g+a[b+l],l+=m,k-=8);if(0===f)f=1-j;else{if(f===i)return g?0/0:1/0*(n?-1:1);g+=Math.pow(2,d),f-=j}return(n?-1:1)*g*Math.pow(2,f-d)}function q(a,b,d,e){return e||(c("boolean"==typeof d,"missing or invalid endian"),c(b+3=0,"specified a negative value for writing an unsigned value"),c(b>=a,"value is larger than maximum value for type"),c(Math.floor(a)===a,"value has a fractional component")}function t(a,b,d,e,f){f||(c(void 0!==b&&null!==b,"missing value"),c("boolean"==typeof e,"missing or invalid endian"),c(void 0!==d&&null!==d,"missing offset"),c(d+1>>8,a[d+1]=255&b):(a[d+1]=(65280&b)>>>8,a[d]=255&b)}function u(a,b,d,e,f){f||(c(void 0!==b&&null!==b,"missing value"),c("boolean"==typeof e,"missing or invalid endian"),c(void 0!==d&&null!==d,"missing offset"),c(d+3>>24,a[d+1]=255&b>>>16,a[d+2]=255&b>>>8,a[d+3]=255&b):(a[d+3]=255&b>>>24,a[d+2]=255&b>>>16,a[d+1]=255&b>>>8,a[d]=255&b)}function v(a,b,d){c("number"==typeof a,"cannot write a non-number as a number"),c(b>=a,"value larger than maximum allowed value"),c(a>=d,"value smaller than minimum allowed value"),c(Math.floor(a)===a,"value has a fractional component")}function w(a,b,d){c("number"==typeof a,"cannot write a non-number as a number"),c(b>=a,"value larger than maximum allowed value"),c(a>=d,"value smaller than minimum allowed value")}function x(a,b,d,e,f){f||(c(void 0!==b&&null!==b,"missing value"),c("boolean"==typeof e,"missing or invalid endian"),c(void 0!==d&&null!==d,"missing offset"),c(d+1=0?t(a,b,d,e,f):t(a,65535+b+1,d,e,f)}function y(a,b,d,e,f){f||(c(void 0!==b&&null!==b,"missing value"),c("boolean"==typeof e,"missing or invalid endian"),c(void 0!==d&&null!==d,"missing offset"),c(d+3=0?u(a,b,d,e,f):u(a,4294967295+b+1,d,e,f)}function z(a,b,c,d,e,f){var g,h,i,j=8*f-e-1,k=(1<>1,m=23===e?Math.pow(2,-24)-Math.pow(2,-77):0,n=d?f-1:0,o=d?-1:1,p=0>b||0===b&&0>1/b?1:0;for(b=Math.abs(b),isNaN(b)||1/0===b?(h=isNaN(b)?1:0,g=k):(g=Math.floor(Math.log(b)/Math.LN2),b*(i=Math.pow(2,-g))<1&&(g--,i*=2),b+=g+l>=1?m/i:m*Math.pow(2,1-l),b*i>=2&&(g++,i/=2),g+l>=k?(h=0,g=k):g+l>=1?(h=(b*i-1)*Math.pow(2,e),g+=l):(h=b*Math.pow(2,l-1)*Math.pow(2,e),g=0));e>=8;a[c+n]=255&h,n+=o,h/=256,e-=8);for(g=g<0;a[c+n]=255&g,n+=o,g/=256,j-=8);a[c+n-o]|=128*p}function A(a,b,d,e,f){f||(c(void 0!==b&&null!==b,"missing value"),c("boolean"==typeof e,"missing or invalid endian"),c(void 0!==d&&null!==d,"missing offset"),c(d+3h.poolSize?(k=new d(j),f=0):((!C||C.byteLength-C.usedf&&(c=f)):c=f,d=String(d||"utf8").toLowerCase()){case"hex":return this.hexWrite(a,b,c);case"utf8":case"utf-8":return this.utf8Write(a,b,c);case"ascii":return this.asciiWrite(a,b,c);case"base64":return this.base64Write(a,b,c);default:throw new Error("Unknown encoding")}};var j=String.fromCharCode;i.utf8Write=function(a,b,c){for(var d=0,e=b,f=a.length,g=c-b;g>d&&f>d;d++){var h=a.charCodeAt(d);128>h?this[e++]=h:h>127&&2048>h?(this[e++]=192|h>>6,this[e++]=128|63&h):(this[e++]=224|h>>12,this[e++]=128|63&h>>6,this[e++]=128|63&h)}return this._charsWritten=e,f},i.utf8Slice=function(a,b){for(var d,g,h,c="",e=a,f=0;b>f&&(d=this[e]);e++)f++,128>d?c+=j(d):d>191&&224>d?(g=this[e+1],c+=j((31&d)<<6|63&g),e++):(g=this[e+1],h=this[e+2],c+=j((15&d)<<12|(63&g)<<6|63&h),e+=2);return c},i.asciiWrite=function(a,b,c){for(var d=0,e=a.length;c>d&&e>d;d++)this[d+b]=a.charCodeAt(d);return this._charsWritten=d,e},i.asciiSlice=function(a,b){for(var c="",d=a;b>d;d++)c+=j(this[d]);return c},i.hexWrite=function(a,b,c){function k(a){for(var b=0;be/2&&f>e;e+=2,g++){var h=a[e],i=a[e+1],j=k(h)<<4|k(i);this[g+b]=j}},i.hexSlice=function(a,b){var c=this.length;(!a||0>a)&&(a=0),(!b||0>b||b>c)&&(b=c);for(var d="",e=a;b>e;e++)d+=k(this[e]);return d},i.copy=function(a,b,c,d){for(var e=0,f=d-c;f>e;e++)a[e+b]=this[e+c]},i.base64Slice=function(a,b){var c=this.length;(!a||0>a)&&(a=0),(!b||0>b||b>c)&&(b=c);for(var d="",e=a;b>e;e++)d+=window.btoa(this[e]);return d},i.base64Write=function(a,b,c){for(var d=0,e=a.length;c>d&&e>d;d++)this[d+b]=window.atob(a.charCodeAt(d));return this._charsWritten=d,e},i.slice=function(a,b){if(void 0===b&&(b=this.length),b>this.length)throw new Error("oob");if(a>b)throw new Error("oob");return g(this.buffer,+a,b-a)},i.toBlob=function(){var a=new(window.BlobBuilder||window.WebKitBlobBuilder||window.MozBlobBuilder);return this.offset?a.append(this.toString("utf8")):a.append(this.buffer),a.getBlob()},i.readUInt8=function(a,b){var d=this;return b||(c(void 0!==a&&null!==a,"missing offset"),c(a=0?e.writeUInt8(a,b,d):e.writeUInt8(255+a+1,b,d)},i.writeInt16LE=function(a,b,c){x(this,a,b,!1,c)},i.writeInt16BE=function(a,b,c){x(this,a,b,!0,c)},i.writeInt32LE=function(a,b,c){y(this,a,b,!1,c)},i.writeInt32BE=function(a,b,c){y(this,a,b,!0,c)},i.writeFloatLE=function(a,b,c){A(this,a,b,!1,c)},i.writeFloatBE=function(a,b,c){A(this,a,b,!0,c)},i.writeDoubleLE=function(a,b,c){B(this,a,b,!1,c)},i.writeDoubleBE=function(a,b,c){B(this,a,b,!0,c)};var C;h.poolSize=8192,h.isBuffer=function(a){return a instanceof h||a instanceof d},h.byteLength=function(a,b){switch(b){case"ascii":return a.length;case"hex":return a.length/2}for(var f,c=0,d=0,e=a.length;e>c;c++)f=a.charCodeAt(c),128>f?d++:d+=f>127&&2048>f?2:3;return d},window.Buffer=h}(); +(function(){"use strict";function isArrayIsh(subject){return Buffer.isBuffer(subject)||subject&&typeof subject==="object"&&typeof subject.length==="number"}function coerce(length){length=~~Math.ceil(+length);return length<0?0:length}function ok(cond,msg){if(!cond)throw new Error(msg)}var ArrayBuffer=window.ArrayBuffer||function(len){this.length=len;while(len--)this[len]=0},Uint8Array=window.Uint8Array||function(parent,offset,length){this.buffer=parent;this.offset=offset;this.length=length},__set="Uint8Array"in window&&Uint8Array.prototype.set;window.Uint8Array||(Uint8Array.prototype={get:function(ind){return this.buffer[ind+this.offset]},set:function(ind,value){this.buffer[ind+this.offset]=value}});var makeBuffer=function(parent,offset,length){var buf=new Uint8Array(parent,offset,length);buf.parent=parent;buf.offset=offset;return buf},Buffer=function(subject,encoding,offset){var type,length,parent,ret;if(typeof offset==="number"){length=coerce(encoding);parent=subject}else{switch(type=typeof subject){case"number":length=coerce(subject);break;case"string":length=Buffer.byteLength(subject,encoding);break;case"object":length=coerce(subject.length);break;default:throw new Error("First argument needs to be a number, "+"array or string.")}if(length>Buffer.poolSize){parent=new ArrayBuffer(length);offset=0}else{if(!pool||pool.byteLength-pool.usedremaining){length=remaining}}encoding=String(encoding||"utf8").toLowerCase();switch(encoding){case"hex":return this.hexWrite(string,offset,length);case"utf8":case"utf-8":return this.utf8Write(string,offset,length);case"ascii":return this.asciiWrite(string,offset,length);case"base64":return this.base64Write(string,offset,length);default:throw new Error("Unknown encoding")}};var fCC=String.fromCharCode;proto.utf8Write=function(string,start,end){for(var i=0,l=start,le=string.length,d=end-start;i127&&c<2048){this[l++]=c>>6|192;this[l++]=c&63|128}else{this[l++]=c>>12|224;this[l++]=c>>6&63|128;this[l++]=c&63|128}}this._charsWritten=l;return le};proto.utf8Slice=function(start,end){for(var string="",c,i=start,p=0,c2,c3;p191&&c<224){c2=this[i+1];string+=fCC((c&31)<<6|c2&63);i++}else{c2=this[i+1];c3=this[i+2];string+=fCC((c&15)<<12|(c2&63)<<6|c3&63);i+=2}}return string};proto.asciiWrite=function(string,start,end){for(var i=0,le=string.length;ilen)end=len;var out="";for(var i=start;ilen)end=len;var data=this.utf8Slice(start,end);return window.btoa(data)};proto.base64Write=function(string,start,end){var data=window.atob(string);for(var i=0,le=data.length;ithis.length){throw new Error("oob")}if(start>end){throw new Error("oob")}return makeBuffer(this.buffer,+start,end-start)};proto.toBlob=function(){var b=new(window.BlobBuilder||window.WebKitBlobBuilder||window.MozBlobBuilder);this.offset?b.append(this.toString("utf8")):b.append(this.buffer);return b.getBlob()};proto.readUInt8=function(offset,noAssert){var buffer=this;if(!noAssert){ok(offset!==undefined&&offset!==null,"missing offset");ok(offset>>0)}else{val=buffer[offset+2]<<16;val|=buffer[offset+1]<<8;val|=buffer[offset];val=val+(buffer[offset+3]<<24>>>0)}return val}proto.readUInt32LE=function(offset,noAssert){return readUInt32(this,offset,false,noAssert)};proto.readUInt32BE=function(offset,noAssert){return readUInt32(this,offset,true,noAssert)};proto.readInt8=function(offset,noAssert){var buffer=this;var neg;if(!noAssert){ok(offset!==undefined&&offset!==null,"missing offset");ok(offset>1,nBits=-7,i=isBE?0:nBytes-1,d=isBE?1:-1,s=buffer[offset+i];i+=d;e=s&(1<<-nBits)-1;s>>=-nBits;nBits+=eLen;for(;nBits>0;e=e*256+buffer[offset+i],i+=d,nBits-=8);m=e&(1<<-nBits)-1;e>>=-nBits;nBits+=mLen;for(;nBits>0;m=m*256+buffer[offset+i],i+=d,nBits-=8);if(e===0){e=1-eBias}else if(e===eMax){return m?NaN:(s?-1:1)*Infinity}else{m=m+Math.pow(2,mLen);e=e-eBias}return(s?-1:1)*m*Math.pow(2,e-mLen)}function readFloat(buffer,offset,isBigEndian,noAssert){if(!noAssert){ok(typeof isBigEndian==="boolean","missing or invalid endian");ok(offset+3=0,"specified a negative value for writing an unsigned value");ok(value<=max,"value is larger than maximum value for type");ok(Math.floor(value)===value,"value has a fractional component")}proto.writeUInt8=function(value,offset,noAssert){var buffer=this;if(!noAssert){ok(value!==undefined&&value!==null,"missing value");ok(offset!==undefined&&offset!==null,"missing offset");ok(offset>>8;buffer[offset+1]=value&255}else{buffer[offset+1]=(value&65280)>>>8;buffer[offset]=value&255}}proto.writeUInt16LE=function(value,offset,noAssert){writeUInt16(this,value,offset,false,noAssert)};proto.writeUInt16BE=function(value,offset,noAssert){writeUInt16(this,value,offset,true,noAssert)};function writeUInt32(buffer,value,offset,isBigEndian,noAssert){if(!noAssert){ok(value!==undefined&&value!==null,"missing value");ok(typeof isBigEndian==="boolean","missing or invalid endian");ok(offset!==undefined&&offset!==null,"missing offset");ok(offset+3>>24&255;buffer[offset+1]=value>>>16&255;buffer[offset+2]=value>>>8&255;buffer[offset+3]=value&255}else{buffer[offset+3]=value>>>24&255;buffer[offset+2]=value>>>16&255;buffer[offset+1]=value>>>8&255;buffer[offset]=value&255}}proto.writeUInt32LE=function(value,offset,noAssert){writeUInt32(this,value,offset,false,noAssert)};proto.writeUInt32BE=function(value,offset,noAssert){writeUInt32(this,value,offset,true,noAssert)};function verifsint(value,max,min){ok(typeof value=="number","cannot write a non-number as a number");ok(value<=max,"value larger than maximum allowed value");ok(value>=min,"value smaller than minimum allowed value");ok(Math.floor(value)===value,"value has a fractional component")}function verifIEEE754(value,max,min){ok(typeof value=="number","cannot write a non-number as a number");ok(value<=max,"value larger than maximum allowed value");ok(value>=min,"value smaller than minimum allowed value")}proto.writeInt8=function(value,offset,noAssert){var buffer=this;if(!noAssert){ok(value!==undefined&&value!==null,"missing value");ok(offset!==undefined&&offset!==null,"missing offset");ok(offset=0){buffer.writeUInt8(value,offset,noAssert)}else{buffer.writeUInt8(255+value+1,offset,noAssert)}};function writeInt16(buffer,value,offset,isBigEndian,noAssert){if(!noAssert){ok(value!==undefined&&value!==null,"missing value");ok(typeof isBigEndian==="boolean","missing or invalid endian");ok(offset!==undefined&&offset!==null,"missing offset");ok(offset+1=0){writeUInt16(buffer,value,offset,isBigEndian,noAssert)}else{writeUInt16(buffer,65535+value+1,offset,isBigEndian,noAssert)}}proto.writeInt16LE=function(value,offset,noAssert){writeInt16(this,value,offset,false,noAssert)};proto.writeInt16BE=function(value,offset,noAssert){writeInt16(this,value,offset,true,noAssert)};function writeInt32(buffer,value,offset,isBigEndian,noAssert){if(!noAssert){ok(value!==undefined&&value!==null,"missing value");ok(typeof isBigEndian==="boolean","missing or invalid endian");ok(offset!==undefined&&offset!==null,"missing offset");ok(offset+3=0){writeUInt32(buffer,value,offset,isBigEndian,noAssert)}else{writeUInt32(buffer,4294967295+value+1,offset,isBigEndian,noAssert)}}proto.writeInt32LE=function(value,offset,noAssert){writeInt32(this,value,offset,false,noAssert)};proto.writeInt32BE=function(value,offset,noAssert){writeInt32(this,value,offset,true,noAssert)};function writeIEEE754(buffer,value,offset,isBE,mLen,nBytes){var e,m,c,eLen=nBytes*8-mLen-1,eMax=(1<>1,rt=mLen===23?Math.pow(2,-24)-Math.pow(2,-77):0,i=isBE?nBytes-1:0,d=isBE?-1:1,s=value<0||value===0&&1/value<0?1:0;value=Math.abs(value);if(isNaN(value)||value===Infinity){m=isNaN(value)?1:0;e=eMax}else{e=Math.floor(Math.log(value)/Math.LN2);if(value*(c=Math.pow(2,-e))<1){e--;c*=2}if(e+eBias>=1){value+=rt/c}else{value+=rt*Math.pow(2,1-eBias)}if(value*c>=2){e++;c/=2}if(e+eBias>=eMax){m=0;e=eMax}else if(e+eBias>=1){m=(value*c-1)*Math.pow(2,mLen);e=e+eBias}else{m=value*Math.pow(2,eBias-1)*Math.pow(2,mLen);e=0}}for(;mLen>=8;buffer[offset+i]=m&255,i+=d,m/=256,mLen-=8);e=e<0;buffer[offset+i]=e&255,i+=d,e/=256,eLen-=8);buffer[offset+i-d]|=s*128}function writeFloat(buffer,value,offset,isBigEndian,noAssert){if(!noAssert){ok(value!==undefined&&value!==null,"missing value");ok(typeof isBigEndian==="boolean","missing or invalid endian");ok(offset!==undefined&&offset!==null,"missing offset");ok(offset+3127&&c<2048){l+=2}else{l+=3}}return l};window.Buffer=Buffer})(); \ No newline at end of file diff --git a/src/browser-buffer.js b/src/browser-buffer.js index 5cbe525..644867d 100644 --- a/src/browser-buffer.js +++ b/src/browser-buffer.js @@ -313,16 +313,14 @@ if (!start || start < 0) start = 0; if (!end || end < 0 || end > len) end = len; - var out = ''; - for (var i = start; i < end; i++) { - out += window.btoa(this[i]); - } - return out; + var data = this.utf8Slice(start, end); + return window.btoa(data); }; proto.base64Write = function (string, start, end) { - for (var i = 0, le = string.length; i < end && i < le; i++) { - this[i + start] = window.atob( string.charCodeAt(i) ); + var data = window.atob(string); + for (var i = 0, le = data.length; i < end && i < le; i++) { + this[i + start] = window.atob( data.charCodeAt(i) ); } this._charsWritten = i; return le; From f31316559425d2014d9d0718000159e4b9c2edca Mon Sep 17 00:00:00 2001 From: Syrian Watermelon Date: Tue, 9 Dec 2014 10:00:35 +0100 Subject: [PATCH 05/11] It didn't work... --- browser-buffer.min.js | 2 +- src/browser-buffer.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/browser-buffer.min.js b/browser-buffer.min.js index a072d9d..dd63bdc 100644 --- a/browser-buffer.min.js +++ b/browser-buffer.min.js @@ -1 +1 @@ -(function(){"use strict";function isArrayIsh(subject){return Buffer.isBuffer(subject)||subject&&typeof subject==="object"&&typeof subject.length==="number"}function coerce(length){length=~~Math.ceil(+length);return length<0?0:length}function ok(cond,msg){if(!cond)throw new Error(msg)}var ArrayBuffer=window.ArrayBuffer||function(len){this.length=len;while(len--)this[len]=0},Uint8Array=window.Uint8Array||function(parent,offset,length){this.buffer=parent;this.offset=offset;this.length=length},__set="Uint8Array"in window&&Uint8Array.prototype.set;window.Uint8Array||(Uint8Array.prototype={get:function(ind){return this.buffer[ind+this.offset]},set:function(ind,value){this.buffer[ind+this.offset]=value}});var makeBuffer=function(parent,offset,length){var buf=new Uint8Array(parent,offset,length);buf.parent=parent;buf.offset=offset;return buf},Buffer=function(subject,encoding,offset){var type,length,parent,ret;if(typeof offset==="number"){length=coerce(encoding);parent=subject}else{switch(type=typeof subject){case"number":length=coerce(subject);break;case"string":length=Buffer.byteLength(subject,encoding);break;case"object":length=coerce(subject.length);break;default:throw new Error("First argument needs to be a number, "+"array or string.")}if(length>Buffer.poolSize){parent=new ArrayBuffer(length);offset=0}else{if(!pool||pool.byteLength-pool.usedremaining){length=remaining}}encoding=String(encoding||"utf8").toLowerCase();switch(encoding){case"hex":return this.hexWrite(string,offset,length);case"utf8":case"utf-8":return this.utf8Write(string,offset,length);case"ascii":return this.asciiWrite(string,offset,length);case"base64":return this.base64Write(string,offset,length);default:throw new Error("Unknown encoding")}};var fCC=String.fromCharCode;proto.utf8Write=function(string,start,end){for(var i=0,l=start,le=string.length,d=end-start;i127&&c<2048){this[l++]=c>>6|192;this[l++]=c&63|128}else{this[l++]=c>>12|224;this[l++]=c>>6&63|128;this[l++]=c&63|128}}this._charsWritten=l;return le};proto.utf8Slice=function(start,end){for(var string="",c,i=start,p=0,c2,c3;p191&&c<224){c2=this[i+1];string+=fCC((c&31)<<6|c2&63);i++}else{c2=this[i+1];c3=this[i+2];string+=fCC((c&15)<<12|(c2&63)<<6|c3&63);i+=2}}return string};proto.asciiWrite=function(string,start,end){for(var i=0,le=string.length;ilen)end=len;var out="";for(var i=start;ilen)end=len;var data=this.utf8Slice(start,end);return window.btoa(data)};proto.base64Write=function(string,start,end){var data=window.atob(string);for(var i=0,le=data.length;ithis.length){throw new Error("oob")}if(start>end){throw new Error("oob")}return makeBuffer(this.buffer,+start,end-start)};proto.toBlob=function(){var b=new(window.BlobBuilder||window.WebKitBlobBuilder||window.MozBlobBuilder);this.offset?b.append(this.toString("utf8")):b.append(this.buffer);return b.getBlob()};proto.readUInt8=function(offset,noAssert){var buffer=this;if(!noAssert){ok(offset!==undefined&&offset!==null,"missing offset");ok(offset>>0)}else{val=buffer[offset+2]<<16;val|=buffer[offset+1]<<8;val|=buffer[offset];val=val+(buffer[offset+3]<<24>>>0)}return val}proto.readUInt32LE=function(offset,noAssert){return readUInt32(this,offset,false,noAssert)};proto.readUInt32BE=function(offset,noAssert){return readUInt32(this,offset,true,noAssert)};proto.readInt8=function(offset,noAssert){var buffer=this;var neg;if(!noAssert){ok(offset!==undefined&&offset!==null,"missing offset");ok(offset>1,nBits=-7,i=isBE?0:nBytes-1,d=isBE?1:-1,s=buffer[offset+i];i+=d;e=s&(1<<-nBits)-1;s>>=-nBits;nBits+=eLen;for(;nBits>0;e=e*256+buffer[offset+i],i+=d,nBits-=8);m=e&(1<<-nBits)-1;e>>=-nBits;nBits+=mLen;for(;nBits>0;m=m*256+buffer[offset+i],i+=d,nBits-=8);if(e===0){e=1-eBias}else if(e===eMax){return m?NaN:(s?-1:1)*Infinity}else{m=m+Math.pow(2,mLen);e=e-eBias}return(s?-1:1)*m*Math.pow(2,e-mLen)}function readFloat(buffer,offset,isBigEndian,noAssert){if(!noAssert){ok(typeof isBigEndian==="boolean","missing or invalid endian");ok(offset+3=0,"specified a negative value for writing an unsigned value");ok(value<=max,"value is larger than maximum value for type");ok(Math.floor(value)===value,"value has a fractional component")}proto.writeUInt8=function(value,offset,noAssert){var buffer=this;if(!noAssert){ok(value!==undefined&&value!==null,"missing value");ok(offset!==undefined&&offset!==null,"missing offset");ok(offset>>8;buffer[offset+1]=value&255}else{buffer[offset+1]=(value&65280)>>>8;buffer[offset]=value&255}}proto.writeUInt16LE=function(value,offset,noAssert){writeUInt16(this,value,offset,false,noAssert)};proto.writeUInt16BE=function(value,offset,noAssert){writeUInt16(this,value,offset,true,noAssert)};function writeUInt32(buffer,value,offset,isBigEndian,noAssert){if(!noAssert){ok(value!==undefined&&value!==null,"missing value");ok(typeof isBigEndian==="boolean","missing or invalid endian");ok(offset!==undefined&&offset!==null,"missing offset");ok(offset+3>>24&255;buffer[offset+1]=value>>>16&255;buffer[offset+2]=value>>>8&255;buffer[offset+3]=value&255}else{buffer[offset+3]=value>>>24&255;buffer[offset+2]=value>>>16&255;buffer[offset+1]=value>>>8&255;buffer[offset]=value&255}}proto.writeUInt32LE=function(value,offset,noAssert){writeUInt32(this,value,offset,false,noAssert)};proto.writeUInt32BE=function(value,offset,noAssert){writeUInt32(this,value,offset,true,noAssert)};function verifsint(value,max,min){ok(typeof value=="number","cannot write a non-number as a number");ok(value<=max,"value larger than maximum allowed value");ok(value>=min,"value smaller than minimum allowed value");ok(Math.floor(value)===value,"value has a fractional component")}function verifIEEE754(value,max,min){ok(typeof value=="number","cannot write a non-number as a number");ok(value<=max,"value larger than maximum allowed value");ok(value>=min,"value smaller than minimum allowed value")}proto.writeInt8=function(value,offset,noAssert){var buffer=this;if(!noAssert){ok(value!==undefined&&value!==null,"missing value");ok(offset!==undefined&&offset!==null,"missing offset");ok(offset=0){buffer.writeUInt8(value,offset,noAssert)}else{buffer.writeUInt8(255+value+1,offset,noAssert)}};function writeInt16(buffer,value,offset,isBigEndian,noAssert){if(!noAssert){ok(value!==undefined&&value!==null,"missing value");ok(typeof isBigEndian==="boolean","missing or invalid endian");ok(offset!==undefined&&offset!==null,"missing offset");ok(offset+1=0){writeUInt16(buffer,value,offset,isBigEndian,noAssert)}else{writeUInt16(buffer,65535+value+1,offset,isBigEndian,noAssert)}}proto.writeInt16LE=function(value,offset,noAssert){writeInt16(this,value,offset,false,noAssert)};proto.writeInt16BE=function(value,offset,noAssert){writeInt16(this,value,offset,true,noAssert)};function writeInt32(buffer,value,offset,isBigEndian,noAssert){if(!noAssert){ok(value!==undefined&&value!==null,"missing value");ok(typeof isBigEndian==="boolean","missing or invalid endian");ok(offset!==undefined&&offset!==null,"missing offset");ok(offset+3=0){writeUInt32(buffer,value,offset,isBigEndian,noAssert)}else{writeUInt32(buffer,4294967295+value+1,offset,isBigEndian,noAssert)}}proto.writeInt32LE=function(value,offset,noAssert){writeInt32(this,value,offset,false,noAssert)};proto.writeInt32BE=function(value,offset,noAssert){writeInt32(this,value,offset,true,noAssert)};function writeIEEE754(buffer,value,offset,isBE,mLen,nBytes){var e,m,c,eLen=nBytes*8-mLen-1,eMax=(1<>1,rt=mLen===23?Math.pow(2,-24)-Math.pow(2,-77):0,i=isBE?nBytes-1:0,d=isBE?-1:1,s=value<0||value===0&&1/value<0?1:0;value=Math.abs(value);if(isNaN(value)||value===Infinity){m=isNaN(value)?1:0;e=eMax}else{e=Math.floor(Math.log(value)/Math.LN2);if(value*(c=Math.pow(2,-e))<1){e--;c*=2}if(e+eBias>=1){value+=rt/c}else{value+=rt*Math.pow(2,1-eBias)}if(value*c>=2){e++;c/=2}if(e+eBias>=eMax){m=0;e=eMax}else if(e+eBias>=1){m=(value*c-1)*Math.pow(2,mLen);e=e+eBias}else{m=value*Math.pow(2,eBias-1)*Math.pow(2,mLen);e=0}}for(;mLen>=8;buffer[offset+i]=m&255,i+=d,m/=256,mLen-=8);e=e<0;buffer[offset+i]=e&255,i+=d,e/=256,eLen-=8);buffer[offset+i-d]|=s*128}function writeFloat(buffer,value,offset,isBigEndian,noAssert){if(!noAssert){ok(value!==undefined&&value!==null,"missing value");ok(typeof isBigEndian==="boolean","missing or invalid endian");ok(offset!==undefined&&offset!==null,"missing offset");ok(offset+3127&&c<2048){l+=2}else{l+=3}}return l};window.Buffer=Buffer})(); \ No newline at end of file +(function(){"use strict";function isArrayIsh(subject){return Buffer.isBuffer(subject)||subject&&typeof subject==="object"&&typeof subject.length==="number"}function coerce(length){length=~~Math.ceil(+length);return length<0?0:length}function ok(cond,msg){if(!cond)throw new Error(msg)}var ArrayBuffer=window.ArrayBuffer||function(len){this.length=len;while(len--)this[len]=0},Uint8Array=window.Uint8Array||function(parent,offset,length){this.buffer=parent;this.offset=offset;this.length=length},__set="Uint8Array"in window&&Uint8Array.prototype.set;window.Uint8Array||(Uint8Array.prototype={get:function(ind){return this.buffer[ind+this.offset]},set:function(ind,value){this.buffer[ind+this.offset]=value}});var makeBuffer=function(parent,offset,length){var buf=new Uint8Array(parent,offset,length);buf.parent=parent;buf.offset=offset;return buf},Buffer=function(subject,encoding,offset){var type,length,parent,ret;if(typeof offset==="number"){length=coerce(encoding);parent=subject}else{switch(type=typeof subject){case"number":length=coerce(subject);break;case"string":length=Buffer.byteLength(subject,encoding);break;case"object":length=coerce(subject.length);break;default:throw new Error("First argument needs to be a number, "+"array or string.")}if(length>Buffer.poolSize){parent=new ArrayBuffer(length);offset=0}else{if(!pool||pool.byteLength-pool.usedremaining){length=remaining}}encoding=String(encoding||"utf8").toLowerCase();switch(encoding){case"hex":return this.hexWrite(string,offset,length);case"utf8":case"utf-8":return this.utf8Write(string,offset,length);case"ascii":return this.asciiWrite(string,offset,length);case"base64":return this.base64Write(string,offset,length);default:throw new Error("Unknown encoding")}};var fCC=String.fromCharCode;proto.utf8Write=function(string,start,end){for(var i=0,l=start,le=string.length,d=end-start;i127&&c<2048){this[l++]=c>>6|192;this[l++]=c&63|128}else{this[l++]=c>>12|224;this[l++]=c>>6&63|128;this[l++]=c&63|128}}this._charsWritten=l;return le};proto.utf8Slice=function(start,end){for(var string="",c,i=start,p=0,c2,c3;p191&&c<224){c2=this[i+1];string+=fCC((c&31)<<6|c2&63);i++}else{c2=this[i+1];c3=this[i+2];string+=fCC((c&15)<<12|(c2&63)<<6|c3&63);i+=2}}return string};proto.asciiWrite=function(string,start,end){for(var i=0,le=string.length;ilen)end=len;var out="";for(var i=start;ilen)end=len;var data=this.utf8Slice(start,end);return btoa(data)};proto.base64Write=function(string,start,end){var data=window.atob(string);for(var i=0,le=data.length;ithis.length){throw new Error("oob")}if(start>end){throw new Error("oob")}return makeBuffer(this.buffer,+start,end-start)};proto.toBlob=function(){var b=new(window.BlobBuilder||window.WebKitBlobBuilder||window.MozBlobBuilder);this.offset?b.append(this.toString("utf8")):b.append(this.buffer);return b.getBlob()};proto.readUInt8=function(offset,noAssert){var buffer=this;if(!noAssert){ok(offset!==undefined&&offset!==null,"missing offset");ok(offset>>0)}else{val=buffer[offset+2]<<16;val|=buffer[offset+1]<<8;val|=buffer[offset];val=val+(buffer[offset+3]<<24>>>0)}return val}proto.readUInt32LE=function(offset,noAssert){return readUInt32(this,offset,false,noAssert)};proto.readUInt32BE=function(offset,noAssert){return readUInt32(this,offset,true,noAssert)};proto.readInt8=function(offset,noAssert){var buffer=this;var neg;if(!noAssert){ok(offset!==undefined&&offset!==null,"missing offset");ok(offset>1,nBits=-7,i=isBE?0:nBytes-1,d=isBE?1:-1,s=buffer[offset+i];i+=d;e=s&(1<<-nBits)-1;s>>=-nBits;nBits+=eLen;for(;nBits>0;e=e*256+buffer[offset+i],i+=d,nBits-=8);m=e&(1<<-nBits)-1;e>>=-nBits;nBits+=mLen;for(;nBits>0;m=m*256+buffer[offset+i],i+=d,nBits-=8);if(e===0){e=1-eBias}else if(e===eMax){return m?NaN:(s?-1:1)*Infinity}else{m=m+Math.pow(2,mLen);e=e-eBias}return(s?-1:1)*m*Math.pow(2,e-mLen)}function readFloat(buffer,offset,isBigEndian,noAssert){if(!noAssert){ok(typeof isBigEndian==="boolean","missing or invalid endian");ok(offset+3=0,"specified a negative value for writing an unsigned value");ok(value<=max,"value is larger than maximum value for type");ok(Math.floor(value)===value,"value has a fractional component")}proto.writeUInt8=function(value,offset,noAssert){var buffer=this;if(!noAssert){ok(value!==undefined&&value!==null,"missing value");ok(offset!==undefined&&offset!==null,"missing offset");ok(offset>>8;buffer[offset+1]=value&255}else{buffer[offset+1]=(value&65280)>>>8;buffer[offset]=value&255}}proto.writeUInt16LE=function(value,offset,noAssert){writeUInt16(this,value,offset,false,noAssert)};proto.writeUInt16BE=function(value,offset,noAssert){writeUInt16(this,value,offset,true,noAssert)};function writeUInt32(buffer,value,offset,isBigEndian,noAssert){if(!noAssert){ok(value!==undefined&&value!==null,"missing value");ok(typeof isBigEndian==="boolean","missing or invalid endian");ok(offset!==undefined&&offset!==null,"missing offset");ok(offset+3>>24&255;buffer[offset+1]=value>>>16&255;buffer[offset+2]=value>>>8&255;buffer[offset+3]=value&255}else{buffer[offset+3]=value>>>24&255;buffer[offset+2]=value>>>16&255;buffer[offset+1]=value>>>8&255;buffer[offset]=value&255}}proto.writeUInt32LE=function(value,offset,noAssert){writeUInt32(this,value,offset,false,noAssert)};proto.writeUInt32BE=function(value,offset,noAssert){writeUInt32(this,value,offset,true,noAssert)};function verifsint(value,max,min){ok(typeof value=="number","cannot write a non-number as a number");ok(value<=max,"value larger than maximum allowed value");ok(value>=min,"value smaller than minimum allowed value");ok(Math.floor(value)===value,"value has a fractional component")}function verifIEEE754(value,max,min){ok(typeof value=="number","cannot write a non-number as a number");ok(value<=max,"value larger than maximum allowed value");ok(value>=min,"value smaller than minimum allowed value")}proto.writeInt8=function(value,offset,noAssert){var buffer=this;if(!noAssert){ok(value!==undefined&&value!==null,"missing value");ok(offset!==undefined&&offset!==null,"missing offset");ok(offset=0){buffer.writeUInt8(value,offset,noAssert)}else{buffer.writeUInt8(255+value+1,offset,noAssert)}};function writeInt16(buffer,value,offset,isBigEndian,noAssert){if(!noAssert){ok(value!==undefined&&value!==null,"missing value");ok(typeof isBigEndian==="boolean","missing or invalid endian");ok(offset!==undefined&&offset!==null,"missing offset");ok(offset+1=0){writeUInt16(buffer,value,offset,isBigEndian,noAssert)}else{writeUInt16(buffer,65535+value+1,offset,isBigEndian,noAssert)}}proto.writeInt16LE=function(value,offset,noAssert){writeInt16(this,value,offset,false,noAssert)};proto.writeInt16BE=function(value,offset,noAssert){writeInt16(this,value,offset,true,noAssert)};function writeInt32(buffer,value,offset,isBigEndian,noAssert){if(!noAssert){ok(value!==undefined&&value!==null,"missing value");ok(typeof isBigEndian==="boolean","missing or invalid endian");ok(offset!==undefined&&offset!==null,"missing offset");ok(offset+3=0){writeUInt32(buffer,value,offset,isBigEndian,noAssert)}else{writeUInt32(buffer,4294967295+value+1,offset,isBigEndian,noAssert)}}proto.writeInt32LE=function(value,offset,noAssert){writeInt32(this,value,offset,false,noAssert)};proto.writeInt32BE=function(value,offset,noAssert){writeInt32(this,value,offset,true,noAssert)};function writeIEEE754(buffer,value,offset,isBE,mLen,nBytes){var e,m,c,eLen=nBytes*8-mLen-1,eMax=(1<>1,rt=mLen===23?Math.pow(2,-24)-Math.pow(2,-77):0,i=isBE?nBytes-1:0,d=isBE?-1:1,s=value<0||value===0&&1/value<0?1:0;value=Math.abs(value);if(isNaN(value)||value===Infinity){m=isNaN(value)?1:0;e=eMax}else{e=Math.floor(Math.log(value)/Math.LN2);if(value*(c=Math.pow(2,-e))<1){e--;c*=2}if(e+eBias>=1){value+=rt/c}else{value+=rt*Math.pow(2,1-eBias)}if(value*c>=2){e++;c/=2}if(e+eBias>=eMax){m=0;e=eMax}else if(e+eBias>=1){m=(value*c-1)*Math.pow(2,mLen);e=e+eBias}else{m=value*Math.pow(2,eBias-1)*Math.pow(2,mLen);e=0}}for(;mLen>=8;buffer[offset+i]=m&255,i+=d,m/=256,mLen-=8);e=e<0;buffer[offset+i]=e&255,i+=d,e/=256,eLen-=8);buffer[offset+i-d]|=s*128}function writeFloat(buffer,value,offset,isBigEndian,noAssert){if(!noAssert){ok(value!==undefined&&value!==null,"missing value");ok(typeof isBigEndian==="boolean","missing or invalid endian");ok(offset!==undefined&&offset!==null,"missing offset");ok(offset+3127&&c<2048){l+=2}else{l+=3}}return l};window.Buffer=Buffer})(); \ No newline at end of file diff --git a/src/browser-buffer.js b/src/browser-buffer.js index 644867d..00e0985 100644 --- a/src/browser-buffer.js +++ b/src/browser-buffer.js @@ -314,13 +314,13 @@ if (!end || end < 0 || end > len) end = len; var data = this.utf8Slice(start, end); - return window.btoa(data); + return btoa(data); }; proto.base64Write = function (string, start, end) { var data = window.atob(string); for (var i = 0, le = data.length; i < end && i < le; i++) { - this[i + start] = window.atob( data.charCodeAt(i) ); + this[i + start] = atob( data.charCodeAt(i) ); } this._charsWritten = i; return le; From a64a9ee53c1336941f52257fa14ae656ef79f230 Mon Sep 17 00:00:00 2001 From: username1565 <39200328+username1565@users.noreply.github.com> Date: Tue, 16 Oct 2018 17:04:58 +0300 Subject: [PATCH 06/11] Fix error 1. Fork after fixing this issue: https://github.com/arextar/browser-buffer/issues/2 2. Add "Usage" code --- README.markdown | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index f194422..e753809 100644 --- a/README.markdown +++ b/README.markdown @@ -1,6 +1,19 @@ # Browser-buffer.js An attempt at mimicing node.js's Buffer API in the browser using typed arrays. This project uses a lot of code from node.js's (lib/buffer.js)[https://github.com/joyent/node/blob/master/lib/buffer.js]. This library still needs a lot of work both to get it running across the board and to clean up some of the pollution it puts on the UInt8Array prototype. +# Usage: +```javascript + + + +``` + ## (lightly) Tested in: * Chrome 18 (beta) (Ubuntu) * Firefox 10.0.2 (Ubuntu) @@ -10,4 +23,4 @@ An attempt at mimicing node.js's Buffer API in the browser using typed arrays. T 1. hex 2. binary 3. base64 (possibly use btoa/atob?) - 4. ucs-2 \ No newline at end of file + 4. ucs-2 From dac9409f50a5ee9cf353dcfe8e1dc8ca6e5b4f8e Mon Sep 17 00:00:00 2001 From: username1565 <39200328+username1565@users.noreply.github.com> Date: Mon, 29 Oct 2018 14:42:00 +0200 Subject: [PATCH 07/11] Add links for online tests on my page. Add links for online tests on my page. --- README.markdown | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.markdown b/README.markdown index e753809..f4be876 100644 --- a/README.markdown +++ b/README.markdown @@ -24,3 +24,8 @@ An attempt at mimicing node.js's Buffer API in the browser using typed arrays. T 2. binary 3. base64 (possibly use btoa/atob?) 4. ucs-2 + +## Online Tests from /test/ folder +test: https://username1565.github.io/browser-buffer/test/test.htm +benchmark: https://username1565.github.io/browser-buffer/test/benchmark.htm +debug (no display something): https://username1565.github.io/browser-buffer/test/debug.htm From 03ce287065c4a42030b737cd6fd22a0418a50c03 Mon Sep 17 00:00:00 2001 From: username1565 <39200328+username1565@users.noreply.github.com> Date: Mon, 29 Oct 2018 14:42:38 +0200 Subject: [PATCH 08/11] fix display fix display --- README.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.markdown b/README.markdown index f4be876..64502e8 100644 --- a/README.markdown +++ b/README.markdown @@ -26,6 +26,6 @@ An attempt at mimicing node.js's Buffer API in the browser using typed arrays. T 4. ucs-2 ## Online Tests from /test/ folder -test: https://username1565.github.io/browser-buffer/test/test.htm -benchmark: https://username1565.github.io/browser-buffer/test/benchmark.htm -debug (no display something): https://username1565.github.io/browser-buffer/test/debug.htm +* test: https://username1565.github.io/browser-buffer/test/test.htm +* benchmark: https://username1565.github.io/browser-buffer/test/benchmark.htm +* debug (no display something): https://username1565.github.io/browser-buffer/test/debug.htm From da58994a3e305d0c520382383ec940e4ad63fece Mon Sep 17 00:00:00 2001 From: username1565 <39200328+username1565@users.noreply.github.com> Date: Mon, 29 Oct 2018 14:43:22 +0200 Subject: [PATCH 09/11] Fix link fix link --- README.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index 64502e8..ad34a51 100644 --- a/README.markdown +++ b/README.markdown @@ -1,5 +1,5 @@ # Browser-buffer.js -An attempt at mimicing node.js's Buffer API in the browser using typed arrays. This project uses a lot of code from node.js's (lib/buffer.js)[https://github.com/joyent/node/blob/master/lib/buffer.js]. This library still needs a lot of work both to get it running across the board and to clean up some of the pollution it puts on the UInt8Array prototype. +An attempt at mimicing node.js's Buffer API in the browser using typed arrays. This project uses a lot of code from node.js's [lib/buffer.js](https://github.com/joyent/node/blob/master/lib/buffer.js). This library still needs a lot of work both to get it running across the board and to clean up some of the pollution it puts on the UInt8Array prototype. # Usage: ```javascript From 5471890fdc94ec4f6b461aa59cebc50a7867a99a Mon Sep 17 00:00:00 2001 From: username1565 <39200328+username1565@users.noreply.github.com> Date: Mon, 29 Oct 2018 14:44:47 +0200 Subject: [PATCH 10/11] Fix my links Fix my links --- README.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.markdown b/README.markdown index ad34a51..30826a7 100644 --- a/README.markdown +++ b/README.markdown @@ -26,6 +26,6 @@ An attempt at mimicing node.js's Buffer API in the browser using typed arrays. T 4. ucs-2 ## Online Tests from /test/ folder -* test: https://username1565.github.io/browser-buffer/test/test.htm -* benchmark: https://username1565.github.io/browser-buffer/test/benchmark.htm -* debug (no display something): https://username1565.github.io/browser-buffer/test/debug.htm +* [test](https://username1565.github.io/browser-buffer/test/test.htm) +* [benchmark](https://username1565.github.io/browser-buffer/test/benchmark.htm) +* [debug (no display anything)](https://username1565.github.io/browser-buffer/test/debug.htm) From b2dea5b24c49c9c42efed1a0be92e28b02d72dac Mon Sep 17 00:00:00 2001 From: username1565 <39200328+username1565@users.noreply.github.com> Date: Mon, 29 Oct 2018 14:45:28 +0200 Subject: [PATCH 11/11] Add description Add description --- README.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index 30826a7..ba8d0bc 100644 --- a/README.markdown +++ b/README.markdown @@ -27,5 +27,5 @@ An attempt at mimicing node.js's Buffer API in the browser using typed arrays. T ## Online Tests from /test/ folder * [test](https://username1565.github.io/browser-buffer/test/test.htm) -* [benchmark](https://username1565.github.io/browser-buffer/test/benchmark.htm) +* [benchmark (wait some time)](https://username1565.github.io/browser-buffer/test/benchmark.htm) * [debug (no display anything)](https://username1565.github.io/browser-buffer/test/debug.htm)