diff --git a/README.markdown b/README.markdown index f194422..ba8d0bc 100644 --- a/README.markdown +++ b/README.markdown @@ -1,5 +1,18 @@ # 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 + + + +``` ## (lightly) Tested in: * Chrome 18 (beta) (Ubuntu) @@ -10,4 +23,9 @@ 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 + +## Online Tests from /test/ folder +* [test](https://username1565.github.io/browser-buffer/test/test.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) diff --git a/browser-buffer.min.js b/browser-buffer.min.js index 6b3cc33..dd63bdc 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 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 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 91af07d..00e0985 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 / 2) < 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; @@ -295,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 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] = atob( data.charCodeAt(i) ); } this._charsWritten = i; return le; @@ -953,6 +969,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);