2014-01-28 16:38:02 +00:00
|
|
|
function readIEEE754(buf, idx, isLE, nl, ml) {
|
|
|
|
if(isLE === undefined) isLE = true;
|
|
|
|
if(!nl) nl = 8;
|
|
|
|
if(!ml && nl === 8) ml = 52;
|
|
|
|
var e, m, el = nl * 8 - ml - 1, eMax = (1 << el) - 1, eBias = eMax >> 1;
|
|
|
|
var bits = -7, d = isLE ? -1 : 1, i = isLE ? (nl - 1) : 0, s = buf[idx + i];
|
|
|
|
|
|
|
|
i += d;
|
|
|
|
e = s & ((1 << (-bits)) - 1); s >>>= (-bits); bits += el;
|
|
|
|
for (; bits > 0; e = e * 256 + buf[idx + i], i += d, bits -= 8);
|
|
|
|
m = e & ((1 << (-bits)) - 1); e >>>= (-bits); bits += ml;
|
|
|
|
for (; bits > 0; m = m * 256 + buf[idx + i], i += d, bits -= 8);
|
|
|
|
if (e === eMax) return m ? NaN : ((s ? -1 : 1) * Infinity);
|
|
|
|
else if (e === 0) e = 1 - eBias;
|
|
|
|
else { m = m + Math.pow(2, ml); e = e - eBias; }
|
|
|
|
return (s ? -1 : 1) * m * Math.pow(2, e - ml);
|
|
|
|
}
|
|
|
|
|
2014-05-29 22:30:03 +00:00
|
|
|
var __toBuffer, ___toBuffer;
|
|
|
|
__toBuffer = ___toBuffer = function(bufs) {
|
|
|
|
var x = [];
|
|
|
|
for(var i = 0; i != bufs[0].length; ++i) { x = x.concat(bufs[0][i]); }
|
|
|
|
return x;
|
|
|
|
};
|
2014-01-28 16:38:02 +00:00
|
|
|
if(typeof Buffer !== "undefined") {
|
|
|
|
Buffer.prototype.hexlify= function() { return this.toString('hex'); };
|
2014-05-29 22:30:03 +00:00
|
|
|
__toBuffer = function(bufs) { try { return Buffer.concat(bufs[0]); } catch(e) { return ___toBuffer(bufs);} };
|
2014-01-28 16:38:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var __readUInt8 = function(b, idx) { return b.readUInt8 ? b.readUInt8(idx) : b[idx]; };
|
|
|
|
var __readUInt16LE = function(b, idx) { return b.readUInt16LE ? b.readUInt16LE(idx) : b[idx+1]*(1<<8)+b[idx]; };
|
|
|
|
var __readInt16LE = function(b, idx) { var u = __readUInt16LE(b,idx); if(!(u & 0x8000)) return u; return (0xffff - u + 1) * -1; };
|
|
|
|
var __readUInt32LE = function(b, idx) { return b.readUInt32LE ? b.readUInt32LE(idx) : b[idx+3]*(1<<24)+b[idx+2]*(1<<16)+b[idx+1]*(1<<8)+b[idx]; };
|
|
|
|
var __readInt32LE = function(b, idx) { if(b.readInt32LE) return b.readInt32LE(idx); var u = __readUInt32LE(b,idx); if(!(u & 0x80000000)) return u; return (0xffffffff - u + 1) * -1; };
|
|
|
|
var __readDoubleLE = function(b, idx) { return b.readDoubleLE ? b.readDoubleLE(idx) : readIEEE754(b, idx||0);};
|
|
|
|
|
2014-05-29 22:30:03 +00:00
|
|
|
var __hexlify = function(b,l) { if(b.hexlify) return b.hexlify((b.l||0), (b.l||0)+l); return b.slice(b.l||0,(b.l||0)+16).map(function(x){return (x<16?"0":"") + x.toString(16);}).join(""); };
|
2014-01-28 16:38:02 +00:00
|
|
|
|
|
|
|
function ReadShift(size, t) {
|
2014-05-29 22:30:03 +00:00
|
|
|
var o="", oo=[], w, vv, i, loc; t = t || 'u';
|
2014-01-28 16:38:02 +00:00
|
|
|
if(size === 'ieee754') { size = 8; t = 'f'; }
|
|
|
|
switch(size) {
|
|
|
|
case 1: o = __readUInt8(this, this.l); break;
|
|
|
|
case 2: o=(t==='u' ? __readUInt16LE : __readInt16LE)(this, this.l); break;
|
|
|
|
case 4: o = __readUInt32LE(this, this.l); break;
|
|
|
|
case 8: if(t === 'f') { o = __readDoubleLE(this, this.l); break; }
|
|
|
|
/* falls through */
|
2014-05-29 22:30:03 +00:00
|
|
|
case 16: o = __hexlify(this, 16); break;
|
2014-01-28 16:38:02 +00:00
|
|
|
|
2014-02-22 21:36:28 +00:00
|
|
|
case 'dbcs': size = 2*t; loc = this.l;
|
2014-01-28 16:38:02 +00:00
|
|
|
for(i = 0; i != t; ++i) {
|
2014-02-22 21:36:28 +00:00
|
|
|
oo.push(_getchar(__readUInt16LE(this, loc)));
|
2014-01-28 16:38:02 +00:00
|
|
|
loc+=2;
|
2014-02-22 21:36:28 +00:00
|
|
|
} o = oo.join(""); break;
|
2014-01-28 16:38:02 +00:00
|
|
|
}
|
|
|
|
this.l+=size; return o;
|
|
|
|
}
|
|
|
|
|
2014-05-29 22:30:03 +00:00
|
|
|
function WriteShift(t, val, f) {
|
|
|
|
var size, i;
|
|
|
|
if(t === 'ieee754') { f = 'f'; t = 8; }
|
|
|
|
switch(t) {
|
|
|
|
case 1: size = 1; this.writeUInt8(val, this.l); break;
|
|
|
|
case 4: size = 4; this.writeUInt32LE(val, this.l); break;
|
|
|
|
case 8: size = 8; if(f === 'f') { this.writeDoubleLE(val, this.l); break; }
|
|
|
|
/* falls through */
|
|
|
|
case 16: break;
|
|
|
|
case -4: size = 4; this.writeInt32LE(val, this.l); break;
|
|
|
|
case 'dbcs':
|
|
|
|
for(i = 0; i != val.length; ++i) this.writeUInt16LE(val.charCodeAt(i), this.l + 2 * i);
|
|
|
|
size = 2 * val.length;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
this.l += size; return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
function prep_blob(blob, pos, w) {
|
2014-01-28 16:38:02 +00:00
|
|
|
blob.l = pos || 0;
|
2014-05-29 22:30:03 +00:00
|
|
|
if(w) {
|
|
|
|
var write = WriteShift.bind(blob);
|
|
|
|
blob.write_shift = write;
|
|
|
|
return [write];
|
|
|
|
} else {
|
|
|
|
var read = ReadShift.bind(blob);
|
|
|
|
blob.read_shift = read;
|
|
|
|
return [read];
|
|
|
|
}
|
2014-01-28 16:38:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function parsenoop(blob, length) { blob.l += length; }
|
2014-05-29 22:30:03 +00:00
|
|
|
|
|
|
|
function writenoop(blob, length) { blob.l += length; }
|
|
|
|
|
|
|
|
var new_buf = function(sz) {
|
|
|
|
var o = typeof Buffer !== 'undefined' ? new Buffer(sz) : new Array(sz);
|
|
|
|
prep_blob(o, 0, true);
|
|
|
|
return o;
|
|
|
|
};
|
|
|
|
|
|
|
|
var is_buf = function(a) { return (typeof Buffer !== 'undefined' && a instanceof Buffer) || Array.isArray(a); };
|