js-cfb/bits/08_blob.js

133 lines
5.0 KiB
JavaScript
Raw Normal View History

/** Helper Functions */
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);
}
var Base64 = (function(){
var map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
return {
encode: function(input, utf8) {
var o = "";
var c1, c2, c3, e1, e2, e3, e4;
for(var i = 0; i < input.length; ) {
c1 = input.charCodeAt(i++);
c2 = input.charCodeAt(i++);
c3 = input.charCodeAt(i++);
e1 = c1 >> 2;
e2 = (c1 & 3) << 4 | c2 >> 4;
e3 = (c2 & 15) << 2 | c3 >> 6;
e4 = c3 & 63;
if (isNaN(c2)) { e3 = e4 = 64; }
else if (isNaN(c3)) { e4 = 64; }
o += map.charAt(e1) + map.charAt(e2) + map.charAt(e3) + map.charAt(e4);
}
return o;
},
decode: function(input, utf8) {
var o = "";
var c1, c2, c3;
var e1, e2, e3, e4;
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
for(var i = 0; i < input.length;) {
e1 = map.indexOf(input.charAt(i++));
e2 = map.indexOf(input.charAt(i++));
e3 = map.indexOf(input.charAt(i++));
e4 = map.indexOf(input.charAt(i++));
c1 = e1 << 2 | e2 >> 4;
c2 = (e2 & 15) << 4 | e3 >> 2;
c3 = (e3 & 3) << 6 | e4;
o += String.fromCharCode(c1);
if (e3 != 64) { o += String.fromCharCode(c2); }
if (e4 != 64) { o += String.fromCharCode(c3); }
}
return o;
}
};
})();
function s2a(s) {
if(typeof Buffer !== 'undefined') return new Buffer(s, "binary");
2013-11-29 07:09:46 +00:00
var w = s.split("").map(function(x){ return x.charCodeAt(0) & 0xff; });
return w;
}
if(typeof Buffer !== "undefined") {
Buffer.prototype.hexlify= function() { return this.toString('hex'); };
Buffer.prototype.utf16le= function(s,e){return this.toString('utf16le',s,e).replace(/\u0000/,'').replace(/[\u0001-\u0006]/,'!');};
Buffer.prototype.utf8 = function(s,e) { return this.toString('utf8',s,e); };
}
Array.prototype.readUInt8 = function(idx) { return this[idx]; };
Array.prototype.readUInt16LE = function(idx) { return this[idx+1]*(1<<8)+this[idx]; };
Array.prototype.readInt16LE = function(idx) { var u = this.readUInt16LE(idx); if(!(u & 0x8000)) return u; return (0xffff - u + 1) * -1; };
Array.prototype.readUInt32LE = function(idx) { return this[idx+3]*(1<<24)+this[idx+2]*(1<<16)+this[idx+1]*(1<<8)+this[idx]; };
Array.prototype.readDoubleLE = function(idx) { return readIEEE754(this, idx||0);};
Array.prototype.hexlify = function() { return this.map(function(x){return (x<16?"0":"") + x.toString(16);}).join(""); };
Array.prototype.utf16le = function(s,e) { var str = ""; for(var i=s; i<e; i+=2) str += String.fromCharCode(this.readUInt16LE(i)); return str.replace(/\u0000/,'').replace(/[\u0001-\u0006]/,'!'); };
Array.prototype.utf8 = function(s,e) { var str = ""; for(var i=s; i<e; i++) str += String.fromCharCode(this.readUInt8(i)); return str; };
function bconcat(bufs) { return (typeof Buffer !== 'undefined') ? Buffer.concat(bufs) : [].concat.apply([], bufs); }
/** Buffer helpers -- keep track of read location `.l` and move it */
function ReadShift(size, t) {
var o, w, vv; t = t || 'u';
if(size === 'ieee754') { size = 8; t = 'f'; }
switch(size) {
case 1: o = this.readUInt8(this.l); break;
case 2: o=t==='u'?this.readUInt16LE(this.l):this.readInt16LE(this.l);break;
case 4: o = this.readUInt32LE(this.l); break;
case 8: if(t === 'f') { o = this.readDoubleLE(this.l); break; }
/* falls through */
case 16: o = this.toString('hex', this.l,this.l+size); break;
case 'utf8': size = t; o = this.utf8(this.l, this.l + size); break;
case 'utf16le': size = 2*t; o = this.utf16le(this.l, this.l + size); break;
case 'cstr': size = 0; o = "";
while((w=this.readUInt8(this.l + size++))!==0) o+= String.fromCharCode(w);
break;
case 'wstr': size = 0; o = "";
while((w=this.readUInt16LE(this.l +size))!==0){o+= String.fromCharCode(w);size+=2;}
size+=2; break;
}
this.l+=size; return o;
}
function CheckField(hexstr, fld) {
var m = this.slice(this.l, this.l+hexstr.length/2).hexlify('hex');
if(m !== hexstr) throw (fld||"") + 'Expected ' + hexstr + ' saw ' + m;
this.l += hexstr.length/2;
}
function WarnField(hexstr, fld) {
var m = this.slice(this.l, this.l+hexstr.length/2).hexlify('hex');
if(m !== hexstr) console.error((fld||"") + 'Expected ' + hexstr +' saw ' + m);
this.l += hexstr.length/2;
}
function prep_blob(blob, pos) {
blob.read_shift = ReadShift.bind(blob);
blob.chk = CheckField;
blob.l = pos || 0;
var read = ReadShift.bind(blob), chk = CheckField.bind(blob);
return [read, chk];
}