Created
November 30, 2019 20:05
-
-
Save IanSSenne/791106a051cb65d14fae0ea08b1b5a5f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class nt{static r(a){return a.z()}static w(a,b){a.y(b)}}class it{static r(a){const b=!!a.x(),c=a.z();let d="";for(let b=0;b<c;b++)d+=a.x().toString(2);return b?BigInt(-1)*BigInt("0b0"+d):BigInt("0b0"+d)}static w(a,b){b<BigInt(0)?(b=BigInt(-1)*b,a.w(1)):a.w(0);let c=b.toString(2).split(""),d=[],e=0;for(;c.length;)e++,"0"===c[0]?(d.push(0),c.shift()):d.push(parseInt([c.shift(),c.shift(),c.shift(),c.shift(),c.shift(),c.shift(),c.shift(),c.shift()].join(""),2));a.y(e),a.w(...d)}}class ot{constructor(a){this.z=a,this.y=Object.keys(this.z)}r(a){let b={};for(let c=0;c<this.y.length;c++)b[this.y[c]]=this.z[this.y[c]].r(a);return b}w(a,b){for(let c=0;c<this.y.length;c++)this.z[this.y[c]].w(a,b[this.y[c]])}}class at{constructor(a){this.s=a}r(a){let b=[],c=a.z();for(let d=0;d<c;d++)b.push(this.s.r(a));return b}w(a,b){a.y(b.length);for(let c=0;c<b.length;c++)this.s.w(a,b[c])}}class st{static r(a){let b=a.z(),c="";for(let d=0;d<b;d++)c+=String.fromCharCode(a.x());return c}static w(a,b){a.y(b.length),a.w(...b.split("").map(a=>a.charCodeAt(0)))}}class bt{static r(a){return!!a.x()}static w(a,b){a.w(+b)}}function otg(a){return new ot(a)}function atg(a){return new at(a)}export const Types={NUMBER:nt,BIGINT:it,STRING:st,OBJECT:otg,ARRAY:atg,BOOLEAN:bt};class ib{constructor(){this.v=[],this.r=0}w(...a){this.v.push(...a.map(a=>a%=256))}y(a){const b=0>a;a=Math.abs(a);let c=0;b&&(c|=2);let d=Math.floor(a).toString(2).split("");0==a%1?this.w(c):(c|=1,this.w(c),this.y(a%1*Math.pow(10,(a%1).toString().length-1)));let e=[],f=0;for(;d.length;)f++,"0"===d[0]?(e.push(0),d.shift()):e.push(parseInt([d.shift(),d.shift(),d.shift(),d.shift(),d.shift(),d.shift(),d.shift(),d.shift()].join(""),2));e.unshift(f),this.w(...e)}x(){return this.v[this.r++]}z(){const a=this.x();let b=0;!(1&a)||(b=this.z(),b/=Math.pow(10,b.toString().length));const c=this.x();let d="";for(let a=0;a<c;a++)d+=this.x().toString(2);let e=1;return!!(2&a)&&(e=-1),e*(+("0b0"+d)+b)}toUInt8Array(){return new Uint8Array(this.v)}}export class Packer{constructor(a){this.s=Types.OBJECT(a)}toUint8Array(a){const b=new ib;return this.s.w(b,a),b.toUInt8Array()}toBuffer(a){return this.toUint8Array(a).buffer}toString(a){return[...this.toUint8Array(a)].map(a=>String.fromCharCode(a)).join("")}toHex(a){return[...this.toUint8Array(a)].map(a=>(a.toString(16)+"").padStart(2,"0")).join("")}readUint8(a){const b=new ib;return b.w(...a),this.s.r(b)}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment