Skip to content

Instantly share code, notes, and snippets.

@SHyx0rmZ
Created May 2, 2016 20:23
Show Gist options
  • Select an option

  • Save SHyx0rmZ/5e838d2adfe730ec76b1ffb75a5a989f to your computer and use it in GitHub Desktop.

Select an option

Save SHyx0rmZ/5e838d2adfe730ec76b1ffb75a5a989f to your computer and use it in GitHub Desktop.
Provides support for Bencode
function bencode_parse(d){var x={pv:function(d){var s=d.slice(1);switch(d[0]){case'd':return x.pd(s,{});case'i':return x.pi(s,'e');case'l':return x.pl(s,[]);default:return x.ps(d);}},pd:function(d,o){if(d[0]=='e'){return[o,d.slice(1)];}var k=x.ps(d);var v=x.pv(k[1]);o[k[0]]=v[0];return x.pd(v[1],o);},pi:function(d,p){var o=d.indexOf(p);return[parseInt(d.slice(0,o)),d.slice(o+1)];},pl:function(d,a){if(d[0]=='e'){return[a,d.slice(1)];}var e=x.pv(d);a.push(e[0]);return x.pl(e[1],a);},ps:function(d){var l=x.pi(d,':');return[l[1].slice(0,l[0]),l[1].slice(l[0])];}};return x.pv(d)[0];}function bencode_generate(d){var x={gv:function(d){switch(typeof(d)){case'object':if(Array.isArray(d)){return x.gl(d);}else{return x.gd(d);}case'number':return x.gi(d);default:return x.gs(d);}},gd:function(d){return 'd'+Object.keys(d).reduce(function(r,e){return r+x.gs(e)+x.gv(d[e]);},'')+'e';},gi:function(d){return 'i'+d+'e';},gl:function(d){return 'l'+d.reduce(function(r,e){return r+x.gv(e);},'')+'e';},gs:function(d){return d.length+':'+d;}};return x.gv(d);}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment