OverviewFilesCommitsIssuesDocsReleasesPeers

holepunchto-deps / @holepunchto / legacy-keet-core-schemas / node_modules / compact-encoding / lexint.js

1 lines · javascript ref:
module.exports={preencode:function t(r,f){if(f<251)r.end++;else if(f<256)r.end+=2;else if(f<65536)r.end+=3;else if(f<16777216)r.end+=4;else if(f<4294967296)r.end+=5;else{r.end++;t(r,Math.floor(Math.log(f)/Math.log(2))-32),r.end+=6}},encode:function t(r,f){const e=f-251;if(f<251)r.buffer[r.start++]=f;else if(f<256)r.buffer[r.start++]=251,r.buffer[r.start++]=e;else if(f<65536)r.buffer[r.start++]=252,r.buffer[r.start++]=e>>8&255,r.buffer[r.start++]=255&e;else if(f<16777216)r.buffer[r.start++]=253,r.buffer[r.start++]=e>>16,r.buffer[r.start++]=e>>8&255,r.buffer[r.start++]=255&e;else if(f<4294967296)r.buffer[r.start++]=254,r.buffer[r.start++]=e>>24,r.buffer[r.start++]=e>>16&255,r.buffer[r.start++]=e>>8&255,r.buffer[r.start++]=255&e;else{const f=Math.floor(Math.log(e)/Math.log(2))-32;r.buffer[r.start++]=255,t(r,f);const s=e/Math.pow(2,f-11);for(let t=5;t>=0;t--)r.buffer[r.start++]=s/Math.pow(2,8*t)&255}},decode:function t(r){const f=251;if(r.end-r.start<1)throw new Error("Out of bounds");const e=r.buffer[r.start++];if(e<f)return e;if(r.end-r.start<e-f+1)throw new Error("Out of bounds.");if(e<252)return r.buffer[r.start++]+f;if(e<253)return(r.buffer[r.start++]<<8)+r.buffer[r.start++]+f;if(e<254)return(r.buffer[r.start++]<<16)+(r.buffer[r.start++]<<8)+r.buffer[r.start++]+f;if(e<255)return 16777216*r.buffer[r.start++]+(r.buffer[r.start++]<<16)+(r.buffer[r.start++]<<8)+r.buffer[r.start++]+f;const s=t(r);if(r.end-r.start<6)throw new Error("Out of bounds");let u=0;for(let t=5;t>=0;t--)u+=r.buffer[r.start++]*Math.pow(2,8*t);return u*Math.pow(2,s-11)+f}};