/
struct.min.js
22 lines (21 loc) · 6.03 KB
/
struct.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*!
* C-Like Data Structure for JavaScript.
*
* @see Struct.js on GitHub <https://github.com/firejune/struct.js>
* @author Joon Kyoung <firejune@gmail.com>
* @license MIT
* @version 0.9.1
*
*/
(function(E,r){function A(a){return!!a&&a.constructor===C}function D(a){return!!a&&a.constructor===Error}function x(a){return!!a&&(a===Array||a.constructor===Array)}function F(a){return!!a&&(a===Object||a.constructor===Object)}function t(a){return!!a&&(a===String||a.constructor===String)}function w(a){return!!a&&(a===ArrayBuffer||a.constructor===ArrayBuffer)}function B(a){return a.charAt(0).toUpperCase()+a.slice(1)}function u(a,b){var c={},e=null,l;for(l in a)if(a.hasOwnProperty(l))if(e=a[l],F(e))c[l]=
u(e,b);else if(e=b(e,l,c)){c=e;break}return c}function G(a,b){for(var c in b)a.hasOwnProperty(c)&&(F(a[c])?a[c]=G(a[c],b[c]):a[c][1]=b[c]);return a}function H(a){var b={},c;for(c in a)a.hasOwnProperty(c)&&(b[c]=a[c]);return b}function J(a,b){return u(a,function(a,e,l){var k=x(a)&&a[0]||a,k=A(k)&&k||k.toLowerCase(),m=t(a)?b:a[1],g=x(a)&&3<=a.length&&a[2]||(t(m)||x(m)||w(m))&&m.length||w(m)&&m.byteLength||1;a=x(a)&&(t(g)||1<g)?t(g)&&a[3]===r&&!0||a[3]:!1;if(m===r||null===m)m="";l[e]=[k,m,g,a]})}function I(a){var b=
0;u(a,function(a){var e=a[0],l=a[1],k=a[2];a=a[3];var m=y[e],g="uint8";m===r&&A(e)&&(m=e.byteLength);if(t(k)||t(l))g=k,k=l&&l.length||0;w(l)&&(k=l.byteLength||0);!0===a&&(b+=y[g]);b+=k*m});return b}var C=function(a,b,c){this.endianness=c===r&&!0||c;this.defaultValue=b||0;this.struct=J(a,this.defaultValue);this.byteLength=I(this.struct);this.emptyBuffer=new ArrayBuffer(this.byteLength);this.constructor=C;this._debug=!1;this._struct={};this._debug&&console.log("STRUCT.CREATE","defaultValue:",this.defaultValue,
"byteLength:",this.byteLength,"endianness:",this.endianness,"struct:",this.struct)};C.prototype={update:function(a){this.struct=G(this.struct,a||{});this.byteLength=I(this.struct);this.emptyBuffer=new ArrayBuffer(this.byteLength);return H(this.struct)},read:function(a,b){if(a===r&&b===r)return u(this.struct,function(a,c,b){b[c]=a[1]});var c=this,e=this.endianness,l=a instanceof DataView&&a||new DataView(a);if(0===a.byteLength)return Error("Uncaught IndexSizeError: Buffer size was zero byte.");this.offset=
b||0;this._debug&&console.info("STRUCT.READ","byteLength:",a.byteLength,"readOffset:",this.offset);var k=u(this.struct,function(b,g,k){var d=[],q=b[0],h=b[1],p=b[2],r=b[3];b=y[q]||1;var f="uint8",v=0;t(p)&&(f=p,p=h.byteLength||h.length);if(a.byteLength<=c.offset)return Error("Uncaught IndexSizeError: Index or size was negative.");if(!0===r){try{p=l["get"+B(f)](c.offset,e)/b}catch(n){return console.error(n,c.offset,c.offset+b,a.byteLength),Error(n)}c.offset+=y[f];c._struct[g+"Size"]=[f,p*b];c._debug&&
console.log(g+"Size",c._struct[g+"Size"],c.offset)}if(w(h)){d=c.offset+p*b;if(a.byteLength<d)return Error("Uncaught IndexSizeError: Index or size was negative.");d=a.slice(c.offset,d);c.offset+=p*b}else for(;v<p;){A(q)?(b=a.slice(c.offset),d[v]=q.read(b),b=q.byteLength):d[v]=c.offset+b>a.byteLength?Error("Uncaught IndexSizeError: Index or size was negative."):l["get"+B(q)](c.offset,e);if(D(d[v]))return d[v];c.offset+=b;v++}if(w(h)||t(h)||x(h)||1<p){if(t(h)){h=d;p=[];d=h.length;for(b=0;b<d;)p[b]=String.fromCharCode(h[b]),
b++;h=p.join("")}else h=d;k[g]=h}else k[g]=d[0];c._struct[g]=[q,k[g]];c._debug&&console.log(g,c._struct[g],c.offset)});D(k)||(this.byteLength=this.offset,this.offset!=a.byteLength&&200<=k.type&&6!=this.offset&&console.warn("Incorrect buffer size readed",this.offset,a.byteLength,k));return D(k)&&k||H(k)},write:function(a){var b=this,c=0,e=this.endianness;a!==r&&this.update(a);var l=new DataView(this.emptyBuffer);this._debug&&console.info("STRUCT.WRITE","byteLength:",this.byteLength);u(this.struct,
function(a,m){var g=[],f=a[0],d=a[1],q=a[2],h=a[3],p=y[f],n="uint8",u=0;p===r&&A(f)&&(n=q,q=d.length,p=1);if(t(q)||t(d))n=q,q=d.length;w(d)&&(q=d.byteLength);!0===h&&(l["set"+B(n)](c,q*p,e),c+=y[n],b._struct[m+"Size"]=[n,q*p],b._debug&&console.log(m+"Size",b._struct[m+"Size"],c));b._struct[m]=[f,d];if(w(d)||t(d)||x(d)||1<q){w(d)&&(g=Array.prototype.slice.call(new (E[B(f)+"Array"])(d)));!d||d!==Function&&d.constructor!==Function||(g=[b.defaultValue]);x(d)&&(g=d);if(h=d&&t(d))for(var h=[],n=d.length,
v=0;v<n;)h[v]=d.charCodeAt(v),v++;g=h||g}else g=[d];for(;u<q;){if(A(f)){d=f.write(g[u]);d=new Uint8Array(d);for(h=0;h<d.length;)l.setUint8(c+h,d[h]),h++;c+=d.length}else l["set"+B(f)](c,g[u],e),c+=p;u++}b._debug&&console.log(m,b._struct[m],c)});c!=this.emptyBuffer.byteLength&&console.warn("Incorrect buffer size writed");return l.buffer}};var y={int8:1,uint8:1,int16:2,uint16:2,int32:4,uint32:4,float32:4,int64:8,uint64:8,float64:8};if(DataView.prototype.getUint64===r&&DataView.prototype.setUint64===
r&&DataView.prototype.getInt64===r&&DataView.prototype.setInt64===r){var f=function(a){return 0<=a&&31>a?1<<a:f[a]||(f[a]=Math.pow(2,a)-1)},n=function(a,b){this.lo=a;this.hi=b};n.prototype={valueOf:function(){return this.lo+f(32)*this.hi},toString:function(){return Number.prototype.toString.apply(this.valueOf(),arguments)}};n.fromNumber=function(a){var b=Math.floor(a/f(32));a-=b*f(32);return new n(a,b)};var z=function(){n.apply(this,arguments)};z.prototype="create"in Object?Object.create(n.prototype):
new n;z.prototype.valueOf=function(){return this.hi<f(31)?n.prototype.valueOf.apply(this,arguments):-(f(32)-this.lo+f(32)*(f(32)-1-this.hi))};z.fromNumber=function(a){var b;0<=a?(b=n.fromNumber(a),a=b.lo,b=b.hi):(b=Math.floor(a/f(32)),a-=b*f(32),b+=f(32));return new z(a,b)};DataView.prototype.getUint64=function(a,b){for(var c=b?[0,4]:[4,0],e=0;2>e;e++)c[e]=this.getUint32(a+c[e],b);return(new n(c[0],c[1])).valueOf()};DataView.prototype.setUint64=function(a,b,c){b=n.fromNumber(b);var e=c?{lo:0,hi:4}:
{lo:4,hi:0},f;for(f in e)this.setUint32(a+e[f],b[f],c)};DataView.prototype.getInt64=function(a,b){for(var c=b?[0,4]:[4,0],e=0;2>e;e++)c[e]=this.getUint32(a+c[e],b);return(new z(c[0],c[1])).valueOf()};DataView.prototype.setInt64=function(a,b){value=z.fromNumber(value);var c=b?{lo:0,hi:4}:{lo:4,hi:0},e;for(e in c)this.setUint32(a+c[e],value[e],b)}}E.Struct=C})(this);