Skip to content

Instantly share code, notes, and snippets.

@5HT
Last active September 9, 2015 07:52
Show Gist options
  • Save 5HT/39f4c65c6920ab3a4481 to your computer and use it in GitHub Desktop.
Save 5HT/39f4c65c6920ab3a4481 to your computer and use it in GitHub Desktop.
var $tab=[['INTE', 97,dint,1],['LINT', 98,dint,4],['FLOA', 99,dflo,0],['ATOM',100,dstr,2],
['REFE',101,dnop,0],['PORT',102,dnop,0],['PID0',103,dnop,0],['TUPL',104,drun,1],
['TUPL',105,drun,4],['NIL0',106,dnil,4],['STRI',107,dstr,2],['LIST',108,drun,4],
['BINA',109,dstr,4],['BIGI',110,dnop,1],['LBIG',111,dnop,4],['NFUN',112,dnop,0],
['EXPO',113,dnop,0],['NREF',114,dnop,0],['SATO',115,dnop,0],['MAPS',116,dnop,0],
['FUNC',117,dnop,0],['AUT8',118,dnop,0],['SAU8',119,dnop,0]];
function decode(b,ix,d) { var s = new DataView(d);
if (s.getUint8(ix++)!==131) throw("BERT"); return din(0,ix,s); };
function din(b,ix,s) { var code=s.getUint8(ix++); var r;
var x=(code > 96 && code < 120)?$tab[code-97]:['U',0,dnop,0];
r=x[2](x[3],ix,s); r.type=x[0]; return r; }
function dflo(b,ix,s) { return { value: [], size: ix+b}; }
function dint(b,ix,s) { return { value:b==1?s.getUint8(ix):s.getInt32(ix), size:ix+b }; }
function dnil(b,ix,s) { return { value:[],size:ix+1}; }
function dnop(b,ix,s) { return { }; }
function dstr(b,ix,s) {
var sz = b==2?s.getUint16(ix):s.getUint32(ix),r=[]; ix+=b;
return { value: utf8_dec(new DataView(s.buffer.slice(ix,ix+sz))), size: ix+sz }; };
function drun(b,ix,s) {
var sz = b==1?s.getUint8(ix):s.getUint32(ix),r=[]; ix+=b;
for (var i=0;i<sz;i++) { e=din(0,ix,s); r.push(e); ix=e.size; }
if (b==4 && s.getUint8(ix++) != 106) throw ("NIL");
return { value: r, size: ix }; };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment