gnablib
Version:
A lean, zero dependency library to provide a useful base for your project.
2 lines • 1.18 kB
JavaScript
/*! Copyright 2023-2024 the gnablib contributors MPL-1.1 */
import{ContentError as t}from"../error/ContentError.js";import{sLen as n}from"../safe/safe.js";export const tbl=" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`";function o(t){return t.charCodeAt(0)-32&191}function r(t,o){n("bytes",t).atMost(45).throwNot();let r=o(t.length),e=0;const c=t.length-2;for(;e<c;){const n=t[e++],c=t[e++],s=t[e++],f=63&(n<<4|c>>4),h=63&(c<<2|s>>6),i=63&s;r+=o(n>>2)+o(f)+o(h)+o(i)}if(e<t.length){const n=t[e++],c=e<t.length?t[e++]:0,s=63&(n<<4|c>>4),f=c<<2&63;r+=o(n>>2)+o(s)+o(f)+o(0)}return r+"\n"}export const uucode={fromBytes:function(t,n){const o=(n=n||{}).cSpaceZero?t=>String.fromCharCode(t+32):t=>String.fromCharCode((t+63)%64+33);let e="",c=0;for(;c<t.length;)e+=r(t.slice(c,c+45),o),c+=45;return e+"`\n"},toBytes:function(n){const r=new Uint8Array(Math.ceil(3*n.length/4));let e=0,c=0;for(;e<n.length;){const s=c,f=o(n[e++]),h=e+4*Math.ceil(f/3);for(;e<h;){const t=o(n[e++]),s=o(n[e++]),f=o(n[e++]),h=o(n[e++]);r[c++]=t<<2|s>>4,r[c++]=s<<4|f>>2,r[c++]=f<<6|h}if(c=s+f,"\n"!=n[e++])throw new t("Unknown char @ pos "+e,"Uuencoded",n[e-1])}return r.slice(0,c)}};