kasten-js 0.2.2 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -6
- package/dist/index.cjs +895 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +45 -7
- package/dist/index.js +875 -1
- package/dist/index.js.map +1 -0
- package/package.json +34 -34
package/dist/index.js
CHANGED
|
@@ -1 +1,875 @@
|
|
|
1
|
-
var e=class{},t=class{static CR=13;static LF=10;static SP=32;static HT=9;constructor(){}},n=class extends Error{},r=class extends n{},i=class extends r{};(class n extends e{static textDecoder=new TextDecoder;static TABLE=`0123456789ABCDEF`;static ENCODED_TABLE=new Uint8Array([...this.TABLE].map(e=>e.charCodeAt(0)));static DECODED_TABLE=new Uint8Array(256);static{this.DECODED_TABLE.fill(255);for(let e=0;e<this.TABLE.length;e++)this.DECODED_TABLE[this.TABLE.charCodeAt(e)]=e}encode(e){let t=n.ENCODED_TABLE,r=new Uint8Array(e.length*2),i=0;for(let n of e){let e=(n&240)>>4,a=n&15,o=t[e],s=t[a];r[i++]=o,r[i++]=s}return n.textDecoder.decode(r)}decode(e){let r=n.DECODED_TABLE,{CR:a,LF:o,SP:s,HT:c}=t,l=e.toUpperCase(),u=new Uint8Array(e.length),d=0;for(let t=0;t<e.length;t++){let n=l.charCodeAt(t);if(!(n===a||n===o||n===s||n===c)){if(!r[n]||r[n]==255)throw new i(`Invalid Base16 character: ${e.charAt(t)}`);u[d++]=n}}if(d%2!=0)throw new i(`Invalid Base16`);let f=new Uint8Array(Math.ceil(e.length/2)),p=0;for(let e=0;e<d;e+=2){let t=u[e],n=u[e+1],i=r[t],a=r[n],o=(i&15)<<4|a&15;f[p++]=o}return f.subarray(0,p)}});var a=class{static textDecoder=new TextDecoder},o=class extends n{},s=class extends o{};(class e extends a{static PADDING_CHAR_CODE=61;static TABLE=`ABCDEFGHIJKLMNOPQRSTUVWXYZ234567`;static ENCODED_TABLE=new Uint8Array([...this.TABLE].map(e=>e.charCodeAt(0)));static DECODED_TABLE=new Uint8Array(256);static{this.DECODED_TABLE.fill(255);for(let e=0;e<this.TABLE.length;e++)this.DECODED_TABLE[this.TABLE.charCodeAt(e)]=e}encode(t){let n=e.ENCODED_TABLE,r=new Uint8Array(Math.ceil(t.length/5)*8),i=0;for(let e=0;e<t.length;e+=5){let a=t[e],o=t[e+1]??0,s=t[e+2]??0,c=t[e+3]??0,l=t[e+4]??0,u=(a&255)>>3,d=(a&7)<<2|(o&192)>>6,f=(o&62)>>1,p=(o&1)<<4|(s&240)>>4,m=(s&15)<<1|(c&128)>>7,h=(c&124)>>2,g=(c&3)<<3|(l&224)>>5,_=l&31,v=n[u],y=n[d],b=n[f],x=n[p],S=n[m],C=n[h],w=n[g],T=n[_];r[i++]=v,r[i++]=y,r[i++]=b,r[i++]=x,r[i++]=S,r[i++]=C,r[i++]=w,r[i++]=T}let o=t.length%5,s=o===4?1:o===3?3:o===2?4:o===1?6:0;for(let t=s;t>0;t--)r[i-t]=e.PADDING_CHAR_CODE;return a.textDecoder.decode(r.subarray(0,i))}decode(n){let r=n.toUpperCase(),i=e.DECODED_TABLE,a=e.PADDING_CHAR_CODE,{CR:o,LF:c,SP:l,HT:u}=t,d=new Uint8Array(n.length),f=0;for(let e=0;e<n.length;e++){let t=r.charCodeAt(e);t!==o&&t!==c&&t!==l&&t!==u&&(d[f++]=t)}if(f%8!=0)throw new s(`Invalid length string.`);let p=-1;for(let e=0;e<f;e++)if(d[e]===a){p=e;break}let m=0;if(p!==-1){if(p<f-6)throw new s(`Invalid padding position.`);for(let e=p;e<f;e++)if(d[e]!==a)throw new s(`Invalid padding`);if(m=f-p,m!==1&&m!==3&&m!==4&&m!==6)throw new s(`Invalid padding`)}let h=new Uint8Array(Math.floor((f-m)*5/8)),g=0;for(let e=0;e<f;e+=8){let t=d[e],n=d[e+1],r=d[e+2],o=d[e+3],c=d[e+4],l=d[e+5],u=d[e+6],f=d[e+7],p=i[t],m=i[n],_=r===a?0:i[r],v=o===a?0:i[o],y=c===a?0:i[c],b=l===a?0:i[l],x=u===a?0:i[u],S=f===a?0:i[f];if(p===255||m===255||_===255||v===255||y===255||b===255||x===255||S===255)throw new s(`Invalid Base32`);let C=(p&31)<<3|(m&28)>>2,w=(m&3)<<6|(_&31)<<1|(v&16)>>4,T=(v&15)<<4|(y&30)>>1,E=(y&1)<<7|(b&31)<<2|(x&24)>>3,D=(x&7)<<5|S&31;h[g++]=C,g<h.length&&(h[g++]=w),g<h.length&&(h[g++]=T),g<h.length&&c!==a&&(h[g++]=E),g<h.length&&u!=a&&(h[g++]=D)}return h.subarray(0,g)}}),class e extends a{static PADDING_CHAR_CODE=61;static TABLE=`0123456789ABCDEFGHIJKLMNOPQRSTUV`;static ENCODED_TABLE=new Uint8Array([...this.TABLE].map(e=>e.charCodeAt(0)));static DECODED_TABLE=new Uint8Array(256);static{this.DECODED_TABLE.fill(255);for(let e=0;e<this.TABLE.length;e++)this.DECODED_TABLE[this.TABLE.charCodeAt(e)]=e}encode(t){let n=e.ENCODED_TABLE,r=new Uint8Array(Math.ceil(t.length/5)*8),i=0;for(let e=0;e<t.length;e+=5){let a=t[e],o=t[e+1]??0,s=t[e+2]??0,c=t[e+3]??0,l=t[e+4]??0,u=(a&255)>>3,d=(a&7)<<2|(o&192)>>6,f=(o&62)>>1,p=(o&1)<<4|(s&240)>>4,m=(s&15)<<1|(c&128)>>7,h=(c&124)>>2,g=(c&3)<<3|(l&224)>>5,_=l&31,v=n[u],y=n[d],b=n[f],x=n[p],S=n[m],C=n[h],w=n[g],T=n[_];r[i++]=v,r[i++]=y,r[i++]=b,r[i++]=x,r[i++]=S,r[i++]=C,r[i++]=w,r[i++]=T}let o=t.length%5,s=o===4?1:o===3?3:o===2?4:o===1?6:0;for(let t=s;t>0;t--)r[i-t]=e.PADDING_CHAR_CODE;return a.textDecoder.decode(r.subarray(0,i))}decode(n){let r=n.toUpperCase(),i=e.DECODED_TABLE,a=e.PADDING_CHAR_CODE,{CR:o,LF:c,SP:l,HT:u}=t,d=new Uint8Array(n.length),f=0;for(let e=0;e<n.length;e++){let t=r.charCodeAt(e);t!==o&&t!==c&&t!==l&&t!==u&&(d[f++]=t)}if(f%8!=0)throw new s(`Invalid length string.`);let p=-1;for(let e=0;e<f;e++)if(d[e]===a){p=e;break}let m=0;if(p!==-1){if(p<f-6)throw new s(`Invalid padding position.`);for(let e=p;e<f;e++)if(d[e]!==a)throw new s(`Invalid padding`);if(m=f-p,m!==1&&m!==3&&m!==4&&m!==6)throw new s(`Invalid padding`)}let h=new Uint8Array(Math.floor((f-m)*5/8)),g=0;for(let e=0;e<f;e+=8){let t=d[e],n=d[e+1],r=d[e+2],o=d[e+3],c=d[e+4],l=d[e+5],u=d[e+6],f=d[e+7],p=i[t],m=i[n],_=r===a?0:i[r],v=o===a?0:i[o],y=c===a?0:i[c],b=l===a?0:i[l],x=u===a?0:i[u],S=f===a?0:i[f];if(p===255||m===255||_===255||v===255||y===255||b===255||x===255||S===255)throw new s(`Invalid Base32`);let C=(p&31)<<3|(m&28)>>2,w=(m&3)<<6|(_&31)<<1|(v&16)>>4,T=(v&15)<<4|(y&30)>>1,E=(y&1)<<7|(b&31)<<2|(x&24)>>3,D=(x&7)<<5|S&31;h[g++]=C,g<h.length&&(h[g++]=w),g<h.length&&(h[g++]=T),g<h.length&&c!==a&&(h[g++]=E),g<h.length&&u!=a&&(h[g++]=D)}return h.subarray(0,g)}};var c=class{},l=class extends n{},u=class extends l{};(class e extends c{static PADDING_CHAR_CODE=61;static TABLE=`ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/`;static DECODE_TABLE=new Uint8Array(256);static ENCODE_TABLE=new Uint8Array([...this.TABLE].map(e=>e.charCodeAt(0)));static decoder=new TextDecoder;static{this.DECODE_TABLE.fill(255);for(let e=0;e<this.TABLE.length;e++)this.DECODE_TABLE[this.TABLE.charCodeAt(e)]=e}encode=n=>{if(n.length===0)return``;let r=e.ENCODE_TABLE,i=Math.ceil(n.length/3)*4,a=Math.floor((i-1)/76),o=new Uint8Array(i+a*2),s=0,c=0;for(let e=0;e<n.length;e+=3){let i=n[e],a=n[e+1]??0,l=n[e+2]??0,u=i>>2,d=(i&3)<<4|a>>4,f=(a&15)<<2|l>>6,p=l&63;o[s++]=r[u],o[s++]=r[d],o[s++]=r[f],o[s++]=r[p],c+=4,c===76&&s<o.length&&(c=0,o[s++]=t.CR,o[s++]=t.LF)}let l=n.length%3;return l===1?(o[s-2]=e.PADDING_CHAR_CODE,o[s-1]=e.PADDING_CHAR_CODE):l===2&&(o[s-1]=e.PADDING_CHAR_CODE),e.decoder.decode(o.subarray(0,s))};decode=n=>{let r=e.DECODE_TABLE,i=new Uint8Array(n.length),a=0;for(let e=0;e<n.length;e++){let r=n.charCodeAt(e);r!==t.CR&&r!==t.LF&&r!==t.SP&&r!==t.HT&&(i[a++]=r)}if(a%4!=0)throw new u(`Invalid length string`);let o=-1;for(let t=0;t<a;t++)if(i[t]===e.PADDING_CHAR_CODE){o=t;break}if(o!==-1){if(o<a-2)throw new u(`Invalid padding position`);for(let t=o;t<a;t++)if(i[t]!==e.PADDING_CHAR_CODE)throw new u(`Invalid padding`);let t=a-o;if(t!==1&&t!==2)throw new u(`Invalid padding`)}let s=a,c=i[s-1]===e.PADDING_CHAR_CODE?i[s-2]===e.PADDING_CHAR_CODE?2:1:0,l=new Uint8Array(a/4*3-c),d=0;for(let t=0;t<a;t+=4){let n=i[t],a=i[t+1],o=i[t+2],s=i[t+3],c=r[n],f=r[a],p=o===e.PADDING_CHAR_CODE?0:r[o],m=s===e.PADDING_CHAR_CODE?0:r[s];if(c===255||f===255||o!==e.PADDING_CHAR_CODE&&p===255||s!==e.PADDING_CHAR_CODE&&m===255)throw new u(`Invalid Base64`);let h=c<<2|f>>4,g=(f&15)<<4|p>>2,_=(p&3)<<6|m;l[d++]=h,o!==e.PADDING_CHAR_CODE&&(l[d++]=g),s!==e.PADDING_CHAR_CODE&&(l[d++]=_)}return l}}),class e extends c{static PADDING_CHAR_CODE=61;static TABLE=`ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/`;static DECODE_TABLE=new Uint8Array(256);static ENCODE_TABLE=new Uint8Array([...this.TABLE].map(e=>e.charCodeAt(0)));static decoder=new TextDecoder;static{this.DECODE_TABLE.fill(255);for(let e=0;e<this.TABLE.length;e++)this.DECODE_TABLE[this.TABLE.charCodeAt(e)]=e}encode=t=>{if(t.length===0)return``;let n=e.ENCODE_TABLE,r=Math.ceil(t.length/3)*4,i=Math.floor((r-1)/76),a=new Uint8Array(r+i*2),o=0;for(let e=0;e<t.length;e+=3){let r=t[e],i=t[e+1]??0,s=t[e+2]??0,c=r>>2,l=(r&3)<<4|i>>4,u=(i&15)<<2|s>>6,d=s&63;a[o++]=n[c],a[o++]=n[l],a[o++]=n[u],a[o++]=n[d]}let s=t.length%3;return s===1?(a[o-2]=e.PADDING_CHAR_CODE,a[o-1]=e.PADDING_CHAR_CODE):s===2&&(a[o-1]=e.PADDING_CHAR_CODE),e.decoder.decode(a.subarray(0,o))};decode=n=>{let r=e.DECODE_TABLE,i=new Uint8Array(n.length),a=0;for(let e=0;e<n.length;e++){let r=n.charCodeAt(e);r!==t.CR&&r!==t.LF&&r!==t.SP&&r!==t.HT&&(i[a++]=r)}if(a%4!=0)throw new u(`Invalid length string`);let o=-1;for(let t=0;t<a;t++)if(i[t]===e.PADDING_CHAR_CODE){o=t;break}if(o!==-1){if(o<a-2)throw new u(`Invalid padding position`);for(let t=o;t<a;t++)if(i[t]!==e.PADDING_CHAR_CODE)throw new u(`Invalid padding`);let t=a-o;if(t!==1&&t!==2)throw new u(`Invalid padding`)}let s=a,c=i[s-1]===e.PADDING_CHAR_CODE?i[s-2]===e.PADDING_CHAR_CODE?2:1:0,l=new Uint8Array(a/4*3-c),d=0;for(let t=0;t<a;t+=4){let n=i[t],a=i[t+1],o=i[t+2],s=i[t+3],c=r[n],f=r[a],p=o===e.PADDING_CHAR_CODE?0:r[o],m=s===e.PADDING_CHAR_CODE?0:r[s];if(c===255||f===255||o!==e.PADDING_CHAR_CODE&&p===255||s!==e.PADDING_CHAR_CODE&&m===255)throw new u(`Invalid Base64`);let h=c<<2|f>>4,g=(f&15)<<4|p>>2,_=(p&3)<<6|m;l[d++]=h,o!==e.PADDING_CHAR_CODE&&(l[d++]=g),s!==e.PADDING_CHAR_CODE&&(l[d++]=_)}return l}},class e extends c{static PADDING_CHAR_CODE=61;static TABLE=`ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_`;static DECODE_TABLE=new Uint8Array(256);static ENCODE_TABLE=new Uint8Array([...this.TABLE].map(e=>e.charCodeAt(0)));static decoder=new TextDecoder;static{this.DECODE_TABLE.fill(255);for(let e=0;e<this.TABLE.length;e++)this.DECODE_TABLE[this.TABLE.charCodeAt(e)]=e}encode=t=>{if(t.length===0)return``;let n=e.ENCODE_TABLE,r=Math.ceil(t.length/3)*4,i=Math.floor((r-1)/76),a=new Uint8Array(r+i*2),o=0;for(let e=0;e<t.length;e+=3){let r=t[e],i=t[e+1]??0,s=t[e+2]??0,c=r>>2,l=(r&3)<<4|i>>4,u=(i&15)<<2|s>>6,d=s&63;a[o++]=n[c],a[o++]=n[l],a[o++]=n[u],a[o++]=n[d]}let s=t.length%3;return s===1?(a[o-2]=e.PADDING_CHAR_CODE,a[o-1]=e.PADDING_CHAR_CODE):s===2&&(a[o-1]=e.PADDING_CHAR_CODE),e.decoder.decode(a.subarray(0,o))};decode=n=>{let r=e.DECODE_TABLE,i=new Uint8Array(n.length),a=0;for(let e=0;e<n.length;e++){let r=n.charCodeAt(e);r!==t.CR&&r!==t.LF&&r!==t.SP&&r!==t.HT&&(i[a++]=r)}if(a%4!=0)throw new u(`Invalid length string`);let o=-1;for(let t=0;t<a;t++)if(i[t]===e.PADDING_CHAR_CODE){o=t;break}if(o!==-1){if(o<a-2)throw new u(`Invalid padding position`);for(let t=o;t<a;t++)if(i[t]!==e.PADDING_CHAR_CODE)throw new u(`Invalid padding`);let t=a-o;if(t!==1&&t!==2)throw new u(`Invalid padding`)}let s=a,c=i[s-1]===e.PADDING_CHAR_CODE?i[s-2]===e.PADDING_CHAR_CODE?2:1:0,l=new Uint8Array(a/4*3-c),d=0;for(let t=0;t<a;t+=4){let n=i[t],a=i[t+1],o=i[t+2],s=i[t+3],c=r[n],f=r[a],p=o===e.PADDING_CHAR_CODE?0:r[o],m=s===e.PADDING_CHAR_CODE?0:r[s];if(c===255||f===255||o!==e.PADDING_CHAR_CODE&&p===255||s!==e.PADDING_CHAR_CODE&&m===255)throw new u(`Invalid Base64`);let h=c<<2|f>>4,g=(f&15)<<4|p>>2,_=(p&3)<<6|m;l[d++]=h,o!==e.PADDING_CHAR_CODE&&(l[d++]=g),s!==e.PADDING_CHAR_CODE&&(l[d++]=_)}return l}};
|
|
1
|
+
//#region src/base16/index.ts
|
|
2
|
+
/**
|
|
3
|
+
* Base16 codec.
|
|
4
|
+
*/
|
|
5
|
+
var Base16 = class {};
|
|
6
|
+
//#endregion
|
|
7
|
+
//#region src/utils/char-codes.ts
|
|
8
|
+
var CharCodes = class {
|
|
9
|
+
static HT = 9;
|
|
10
|
+
static LF = 10;
|
|
11
|
+
static VT = 11;
|
|
12
|
+
static FF = 12;
|
|
13
|
+
static CR = 13;
|
|
14
|
+
static SP = 32;
|
|
15
|
+
constructor() {}
|
|
16
|
+
static isWhitespace = (c) => {
|
|
17
|
+
const type = typeof c;
|
|
18
|
+
if (type !== "string" && type !== "number") throw new Error("invalid argument type.");
|
|
19
|
+
if (type === "string" && c.length !== 1) throw new Error("inalid argument: not a character");
|
|
20
|
+
const code = typeof c === "string" ? c.charCodeAt(0) : c;
|
|
21
|
+
return this.HT <= code && code <= this.CR || code === this.SP;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region src/errors.ts
|
|
26
|
+
var KastenError = class extends Error {};
|
|
27
|
+
//#endregion
|
|
28
|
+
//#region src/base16/errors.ts
|
|
29
|
+
var KastenBase16Error = class extends KastenError {};
|
|
30
|
+
var KastenBase16EncodeError = class extends KastenBase16Error {};
|
|
31
|
+
var KastenBase16DecodeError = class extends KastenBase16Error {};
|
|
32
|
+
//#endregion
|
|
33
|
+
//#region src/base16/rfc4648/rfc4648.ts
|
|
34
|
+
/**
|
|
35
|
+
* RFC 4648 Base 16 encoding.
|
|
36
|
+
*/
|
|
37
|
+
var RFC4648Base16 = class RFC4648Base16 extends Base16 {
|
|
38
|
+
static textDecoder = new TextDecoder();
|
|
39
|
+
static TABLE = "0123456789ABCDEF";
|
|
40
|
+
static ENCODED_TABLE = new Uint8Array([...this.TABLE].map((c) => c.charCodeAt(0)));
|
|
41
|
+
static DECODED_TABLE = new Uint8Array(256);
|
|
42
|
+
static {
|
|
43
|
+
this.DECODED_TABLE.fill(255);
|
|
44
|
+
for (let i = 0; i < this.TABLE.length; i++) this.DECODED_TABLE[this.TABLE.charCodeAt(i)] = i;
|
|
45
|
+
}
|
|
46
|
+
encode(bytes) {
|
|
47
|
+
const ENCODED_TABLE = RFC4648Base16.ENCODED_TABLE;
|
|
48
|
+
const chars = new Uint8Array(bytes.length * 2);
|
|
49
|
+
let charCount = 0;
|
|
50
|
+
for (const byte of bytes) {
|
|
51
|
+
const ci1 = (byte & 240) >> 4;
|
|
52
|
+
const ci2 = byte & 15;
|
|
53
|
+
const c1 = ENCODED_TABLE[ci1];
|
|
54
|
+
const c2 = ENCODED_TABLE[ci2];
|
|
55
|
+
chars[charCount++] = c1;
|
|
56
|
+
chars[charCount++] = c2;
|
|
57
|
+
}
|
|
58
|
+
return RFC4648Base16.textDecoder.decode(chars);
|
|
59
|
+
}
|
|
60
|
+
decode(base16) {
|
|
61
|
+
const DECODED_TABLE = RFC4648Base16.DECODED_TABLE;
|
|
62
|
+
const upper = base16.toUpperCase();
|
|
63
|
+
const chars = new Uint8Array(base16.length);
|
|
64
|
+
let charCount = 0;
|
|
65
|
+
for (let i = 0; i < base16.length; i++) {
|
|
66
|
+
const c = upper.charCodeAt(i);
|
|
67
|
+
if (CharCodes.isWhitespace(c)) continue;
|
|
68
|
+
if (!DECODED_TABLE[c] || DECODED_TABLE[c] == 255) throw new KastenBase16DecodeError(`Invalid Base16 character: ${base16.charAt(i)}`);
|
|
69
|
+
chars[charCount++] = c;
|
|
70
|
+
}
|
|
71
|
+
if (charCount % 2 !== 0) throw new KastenBase16DecodeError("Invalid Base16");
|
|
72
|
+
const bytes = new Uint8Array(Math.ceil(base16.length / 2));
|
|
73
|
+
let byteCount = 0;
|
|
74
|
+
for (let i = 0; i < charCount; i += 2) {
|
|
75
|
+
const c1 = chars[i];
|
|
76
|
+
const c2 = chars[i + 1];
|
|
77
|
+
const ci1 = DECODED_TABLE[c1];
|
|
78
|
+
const ci2 = DECODED_TABLE[c2];
|
|
79
|
+
const byte = (ci1 & 15) << 4 | ci2 & 15;
|
|
80
|
+
bytes[byteCount++] = byte;
|
|
81
|
+
}
|
|
82
|
+
return bytes.subarray(0, byteCount);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
//#endregion
|
|
86
|
+
//#region src/base16/ascii/ascii.ts
|
|
87
|
+
/**
|
|
88
|
+
* AsciiHexDecode codec of PDF1.7
|
|
89
|
+
*/
|
|
90
|
+
var AsciiHexDecode = class extends Base16 {
|
|
91
|
+
encode(bytes) {
|
|
92
|
+
if (bytes.length === 0) return ">";
|
|
93
|
+
const chars = [];
|
|
94
|
+
for (const byte of bytes) {
|
|
95
|
+
const char = byte.toString(16).padStart(2, "0");
|
|
96
|
+
chars.push(char);
|
|
97
|
+
}
|
|
98
|
+
chars.push(">");
|
|
99
|
+
return chars.join("");
|
|
100
|
+
}
|
|
101
|
+
decode(base16) {
|
|
102
|
+
const chars = [];
|
|
103
|
+
for (const c of base16) {
|
|
104
|
+
if (c === ">") break;
|
|
105
|
+
if (CharCodes.isWhitespace(c)) continue;
|
|
106
|
+
const code = c.charCodeAt(0);
|
|
107
|
+
if (!(48 <= code && code <= 57) && !(65 <= code && code <= 70) && !(97 <= code && code <= 102)) throw new KastenBase16DecodeError(`invalid char: ${c}`);
|
|
108
|
+
chars.push(c);
|
|
109
|
+
}
|
|
110
|
+
if (chars.length % 2 !== 0) chars.push("0");
|
|
111
|
+
const strLength = chars.length;
|
|
112
|
+
const bytes = new Uint8Array(Math.ceil(strLength / 2));
|
|
113
|
+
let byteCount = 0;
|
|
114
|
+
for (let i = 0, j = 0; i < strLength; i += 2, j++) {
|
|
115
|
+
const hex = chars[i] + chars[i + 1];
|
|
116
|
+
const byte = Number.parseInt(hex, 16);
|
|
117
|
+
bytes[byteCount++] = byte;
|
|
118
|
+
}
|
|
119
|
+
return bytes.subarray(0, byteCount);
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
//#endregion
|
|
123
|
+
//#region src/base32/index.ts
|
|
124
|
+
/**
|
|
125
|
+
* Base32 codec.
|
|
126
|
+
*/
|
|
127
|
+
var Base32 = class {
|
|
128
|
+
static textDecoder = new TextDecoder();
|
|
129
|
+
};
|
|
130
|
+
//#endregion
|
|
131
|
+
//#region src/base32/errors.ts
|
|
132
|
+
var KastenBase32Error = class extends KastenError {};
|
|
133
|
+
var KastenBase32EncodeError = class extends KastenBase32Error {};
|
|
134
|
+
var KastenBase32DecodeError = class extends KastenBase32Error {};
|
|
135
|
+
//#endregion
|
|
136
|
+
//#region src/base32/rfc4648/rfc4648.ts
|
|
137
|
+
var RFC4648Base32 = class RFC4648Base32 extends Base32 {
|
|
138
|
+
static PADDING_CHAR_CODE = "=".charCodeAt(0);
|
|
139
|
+
static TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
|
|
140
|
+
static ENCODED_TABLE = new Uint8Array([...this.TABLE].map((c) => c.charCodeAt(0)));
|
|
141
|
+
static DECODED_TABLE = new Uint8Array(256);
|
|
142
|
+
static {
|
|
143
|
+
this.DECODED_TABLE.fill(255);
|
|
144
|
+
for (let i = 0; i < this.TABLE.length; i++) this.DECODED_TABLE[this.TABLE.charCodeAt(i)] = i;
|
|
145
|
+
}
|
|
146
|
+
encode(bytes) {
|
|
147
|
+
const ENCODED_TABLE = RFC4648Base32.ENCODED_TABLE;
|
|
148
|
+
const chars = new Uint8Array(Math.ceil(bytes.length / 5) * 8);
|
|
149
|
+
let count = 0;
|
|
150
|
+
for (let i = 0; i < bytes.length; i += 5) {
|
|
151
|
+
const b1 = bytes[i];
|
|
152
|
+
const b2 = bytes[i + 1] ?? 0;
|
|
153
|
+
const b3 = bytes[i + 2] ?? 0;
|
|
154
|
+
const b4 = bytes[i + 3] ?? 0;
|
|
155
|
+
const b5 = bytes[i + 4] ?? 0;
|
|
156
|
+
const ci1 = (b1 & 255) >> 3;
|
|
157
|
+
const ci2 = (b1 & 7) << 2 | (b2 & 192) >> 6;
|
|
158
|
+
const ci3 = (b2 & 62) >> 1;
|
|
159
|
+
const ci4 = (b2 & 1) << 4 | (b3 & 240) >> 4;
|
|
160
|
+
const ci5 = (b3 & 15) << 1 | (b4 & 128) >> 7;
|
|
161
|
+
const ci6 = (b4 & 124) >> 2;
|
|
162
|
+
const ci7 = (b4 & 3) << 3 | (b5 & 224) >> 5;
|
|
163
|
+
const ci8 = b5 & 31;
|
|
164
|
+
const c1 = ENCODED_TABLE[ci1];
|
|
165
|
+
const c2 = ENCODED_TABLE[ci2];
|
|
166
|
+
const c3 = ENCODED_TABLE[ci3];
|
|
167
|
+
const c4 = ENCODED_TABLE[ci4];
|
|
168
|
+
const c5 = ENCODED_TABLE[ci5];
|
|
169
|
+
const c6 = ENCODED_TABLE[ci6];
|
|
170
|
+
const c7 = ENCODED_TABLE[ci7];
|
|
171
|
+
const c8 = ENCODED_TABLE[ci8];
|
|
172
|
+
chars[count++] = c1;
|
|
173
|
+
chars[count++] = c2;
|
|
174
|
+
chars[count++] = c3;
|
|
175
|
+
chars[count++] = c4;
|
|
176
|
+
chars[count++] = c5;
|
|
177
|
+
chars[count++] = c6;
|
|
178
|
+
chars[count++] = c7;
|
|
179
|
+
chars[count++] = c8;
|
|
180
|
+
}
|
|
181
|
+
const remain = bytes.length % 5;
|
|
182
|
+
const padding = remain === 4 ? 1 : remain === 3 ? 3 : remain === 2 ? 4 : remain === 1 ? 6 : 0;
|
|
183
|
+
for (let i = padding; i > 0; i--) chars[count - i] = RFC4648Base32.PADDING_CHAR_CODE;
|
|
184
|
+
return Base32.textDecoder.decode(chars.subarray(0, count));
|
|
185
|
+
}
|
|
186
|
+
decode(base32) {
|
|
187
|
+
const upper = base32.toUpperCase();
|
|
188
|
+
const DECODED_TABLE = RFC4648Base32.DECODED_TABLE;
|
|
189
|
+
const PADDING = RFC4648Base32.PADDING_CHAR_CODE;
|
|
190
|
+
const chars = new Uint8Array(base32.length);
|
|
191
|
+
let charCount = 0;
|
|
192
|
+
for (let i = 0; i < base32.length; i++) {
|
|
193
|
+
const c = upper.charCodeAt(i);
|
|
194
|
+
if (!CharCodes.isWhitespace(c)) chars[charCount++] = c;
|
|
195
|
+
}
|
|
196
|
+
if (charCount % 8 !== 0) throw new KastenBase32DecodeError("Invalid length string.");
|
|
197
|
+
let firstPad = -1;
|
|
198
|
+
for (let i = 0; i < charCount; i++) if (chars[i] === PADDING) {
|
|
199
|
+
firstPad = i;
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
let padCount = 0;
|
|
203
|
+
if (firstPad !== -1) {
|
|
204
|
+
if (firstPad < charCount - 6) throw new KastenBase32DecodeError("Invalid padding position.");
|
|
205
|
+
for (let i = firstPad; i < charCount; i++) if (chars[i] !== PADDING) throw new KastenBase32DecodeError("Invalid padding");
|
|
206
|
+
padCount = charCount - firstPad;
|
|
207
|
+
if (padCount !== 1 && padCount !== 3 && padCount !== 4 && padCount !== 6) throw new KastenBase32DecodeError("Invalid padding");
|
|
208
|
+
}
|
|
209
|
+
const bytes = new Uint8Array(Math.floor((charCount - padCount) * 5 / 8));
|
|
210
|
+
let byteCount = 0;
|
|
211
|
+
for (let i = 0; i < charCount; i += 8) {
|
|
212
|
+
const c1 = chars[i];
|
|
213
|
+
const c2 = chars[i + 1];
|
|
214
|
+
const c3 = chars[i + 2];
|
|
215
|
+
const c4 = chars[i + 3];
|
|
216
|
+
const c5 = chars[i + 4];
|
|
217
|
+
const c6 = chars[i + 5];
|
|
218
|
+
const c7 = chars[i + 6];
|
|
219
|
+
const c8 = chars[i + 7];
|
|
220
|
+
const ci1 = DECODED_TABLE[c1];
|
|
221
|
+
const ci2 = DECODED_TABLE[c2];
|
|
222
|
+
const ci3 = c3 !== PADDING ? DECODED_TABLE[c3] : 0;
|
|
223
|
+
const ci4 = c4 !== PADDING ? DECODED_TABLE[c4] : 0;
|
|
224
|
+
const ci5 = c5 !== PADDING ? DECODED_TABLE[c5] : 0;
|
|
225
|
+
const ci6 = c6 !== PADDING ? DECODED_TABLE[c6] : 0;
|
|
226
|
+
const ci7 = c7 !== PADDING ? DECODED_TABLE[c7] : 0;
|
|
227
|
+
const ci8 = c8 !== PADDING ? DECODED_TABLE[c8] : 0;
|
|
228
|
+
if (ci1 === 255 || ci2 === 255 || ci3 === 255 || ci4 === 255 || ci5 === 255 || ci6 === 255 || ci7 === 255 || ci8 === 255) throw new KastenBase32DecodeError("Invalid Base32");
|
|
229
|
+
const b1 = (ci1 & 31) << 3 | (ci2 & 28) >> 2;
|
|
230
|
+
const b2 = (ci2 & 3) << 6 | (ci3 & 31) << 1 | (ci4 & 16) >> 4;
|
|
231
|
+
const b3 = (ci4 & 15) << 4 | (ci5 & 30) >> 1;
|
|
232
|
+
const b4 = (ci5 & 1) << 7 | (ci6 & 31) << 2 | (ci7 & 24) >> 3;
|
|
233
|
+
const b5 = (ci7 & 7) << 5 | ci8 & 31;
|
|
234
|
+
bytes[byteCount++] = b1;
|
|
235
|
+
if (byteCount < bytes.length) bytes[byteCount++] = b2;
|
|
236
|
+
if (byteCount < bytes.length) bytes[byteCount++] = b3;
|
|
237
|
+
if (byteCount < bytes.length && c5 !== PADDING) bytes[byteCount++] = b4;
|
|
238
|
+
if (byteCount < bytes.length && c7 != PADDING) bytes[byteCount++] = b5;
|
|
239
|
+
}
|
|
240
|
+
return bytes.subarray(0, byteCount);
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
//#endregion
|
|
244
|
+
//#region src/base32/rfc4648/rfc4648hex.ts
|
|
245
|
+
var RFC4648Base32Hex = class RFC4648Base32Hex extends Base32 {
|
|
246
|
+
static PADDING_CHAR_CODE = "=".charCodeAt(0);
|
|
247
|
+
static TABLE = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
|
|
248
|
+
static ENCODED_TABLE = new Uint8Array([...this.TABLE].map((c) => c.charCodeAt(0)));
|
|
249
|
+
static DECODED_TABLE = new Uint8Array(256);
|
|
250
|
+
static {
|
|
251
|
+
this.DECODED_TABLE.fill(255);
|
|
252
|
+
for (let i = 0; i < this.TABLE.length; i++) this.DECODED_TABLE[this.TABLE.charCodeAt(i)] = i;
|
|
253
|
+
}
|
|
254
|
+
encode(bytes) {
|
|
255
|
+
const ENCODED_TABLE = RFC4648Base32Hex.ENCODED_TABLE;
|
|
256
|
+
const chars = new Uint8Array(Math.ceil(bytes.length / 5) * 8);
|
|
257
|
+
let count = 0;
|
|
258
|
+
for (let i = 0; i < bytes.length; i += 5) {
|
|
259
|
+
const b1 = bytes[i];
|
|
260
|
+
const b2 = bytes[i + 1] ?? 0;
|
|
261
|
+
const b3 = bytes[i + 2] ?? 0;
|
|
262
|
+
const b4 = bytes[i + 3] ?? 0;
|
|
263
|
+
const b5 = bytes[i + 4] ?? 0;
|
|
264
|
+
const ci1 = (b1 & 255) >> 3;
|
|
265
|
+
const ci2 = (b1 & 7) << 2 | (b2 & 192) >> 6;
|
|
266
|
+
const ci3 = (b2 & 62) >> 1;
|
|
267
|
+
const ci4 = (b2 & 1) << 4 | (b3 & 240) >> 4;
|
|
268
|
+
const ci5 = (b3 & 15) << 1 | (b4 & 128) >> 7;
|
|
269
|
+
const ci6 = (b4 & 124) >> 2;
|
|
270
|
+
const ci7 = (b4 & 3) << 3 | (b5 & 224) >> 5;
|
|
271
|
+
const ci8 = b5 & 31;
|
|
272
|
+
const c1 = ENCODED_TABLE[ci1];
|
|
273
|
+
const c2 = ENCODED_TABLE[ci2];
|
|
274
|
+
const c3 = ENCODED_TABLE[ci3];
|
|
275
|
+
const c4 = ENCODED_TABLE[ci4];
|
|
276
|
+
const c5 = ENCODED_TABLE[ci5];
|
|
277
|
+
const c6 = ENCODED_TABLE[ci6];
|
|
278
|
+
const c7 = ENCODED_TABLE[ci7];
|
|
279
|
+
const c8 = ENCODED_TABLE[ci8];
|
|
280
|
+
chars[count++] = c1;
|
|
281
|
+
chars[count++] = c2;
|
|
282
|
+
chars[count++] = c3;
|
|
283
|
+
chars[count++] = c4;
|
|
284
|
+
chars[count++] = c5;
|
|
285
|
+
chars[count++] = c6;
|
|
286
|
+
chars[count++] = c7;
|
|
287
|
+
chars[count++] = c8;
|
|
288
|
+
}
|
|
289
|
+
const remain = bytes.length % 5;
|
|
290
|
+
const padding = remain === 4 ? 1 : remain === 3 ? 3 : remain === 2 ? 4 : remain === 1 ? 6 : 0;
|
|
291
|
+
for (let i = padding; i > 0; i--) chars[count - i] = RFC4648Base32Hex.PADDING_CHAR_CODE;
|
|
292
|
+
return Base32.textDecoder.decode(chars.subarray(0, count));
|
|
293
|
+
}
|
|
294
|
+
decode(base32) {
|
|
295
|
+
const upper = base32.toUpperCase();
|
|
296
|
+
const DECODED_TABLE = RFC4648Base32Hex.DECODED_TABLE;
|
|
297
|
+
const PADDING = RFC4648Base32Hex.PADDING_CHAR_CODE;
|
|
298
|
+
const chars = new Uint8Array(base32.length);
|
|
299
|
+
let charCount = 0;
|
|
300
|
+
for (let i = 0; i < base32.length; i++) {
|
|
301
|
+
const c = upper.charCodeAt(i);
|
|
302
|
+
if (!CharCodes.isWhitespace(c)) chars[charCount++] = c;
|
|
303
|
+
}
|
|
304
|
+
if (charCount % 8 !== 0) throw new KastenBase32DecodeError("Invalid length string.");
|
|
305
|
+
let firstPad = -1;
|
|
306
|
+
for (let i = 0; i < charCount; i++) if (chars[i] === PADDING) {
|
|
307
|
+
firstPad = i;
|
|
308
|
+
break;
|
|
309
|
+
}
|
|
310
|
+
let padCount = 0;
|
|
311
|
+
if (firstPad !== -1) {
|
|
312
|
+
if (firstPad < charCount - 6) throw new KastenBase32DecodeError("Invalid padding position.");
|
|
313
|
+
for (let i = firstPad; i < charCount; i++) if (chars[i] !== PADDING) throw new KastenBase32DecodeError("Invalid padding");
|
|
314
|
+
padCount = charCount - firstPad;
|
|
315
|
+
if (padCount !== 1 && padCount !== 3 && padCount !== 4 && padCount !== 6) throw new KastenBase32DecodeError("Invalid padding");
|
|
316
|
+
}
|
|
317
|
+
const bytes = new Uint8Array(Math.floor((charCount - padCount) * 5 / 8));
|
|
318
|
+
let byteCount = 0;
|
|
319
|
+
for (let i = 0; i < charCount; i += 8) {
|
|
320
|
+
const c1 = chars[i];
|
|
321
|
+
const c2 = chars[i + 1];
|
|
322
|
+
const c3 = chars[i + 2];
|
|
323
|
+
const c4 = chars[i + 3];
|
|
324
|
+
const c5 = chars[i + 4];
|
|
325
|
+
const c6 = chars[i + 5];
|
|
326
|
+
const c7 = chars[i + 6];
|
|
327
|
+
const c8 = chars[i + 7];
|
|
328
|
+
const ci1 = DECODED_TABLE[c1];
|
|
329
|
+
const ci2 = DECODED_TABLE[c2];
|
|
330
|
+
const ci3 = c3 !== PADDING ? DECODED_TABLE[c3] : 0;
|
|
331
|
+
const ci4 = c4 !== PADDING ? DECODED_TABLE[c4] : 0;
|
|
332
|
+
const ci5 = c5 !== PADDING ? DECODED_TABLE[c5] : 0;
|
|
333
|
+
const ci6 = c6 !== PADDING ? DECODED_TABLE[c6] : 0;
|
|
334
|
+
const ci7 = c7 !== PADDING ? DECODED_TABLE[c7] : 0;
|
|
335
|
+
const ci8 = c8 !== PADDING ? DECODED_TABLE[c8] : 0;
|
|
336
|
+
if (ci1 === 255 || ci2 === 255 || ci3 === 255 || ci4 === 255 || ci5 === 255 || ci6 === 255 || ci7 === 255 || ci8 === 255) throw new KastenBase32DecodeError("Invalid Base32");
|
|
337
|
+
const b1 = (ci1 & 31) << 3 | (ci2 & 28) >> 2;
|
|
338
|
+
const b2 = (ci2 & 3) << 6 | (ci3 & 31) << 1 | (ci4 & 16) >> 4;
|
|
339
|
+
const b3 = (ci4 & 15) << 4 | (ci5 & 30) >> 1;
|
|
340
|
+
const b4 = (ci5 & 1) << 7 | (ci6 & 31) << 2 | (ci7 & 24) >> 3;
|
|
341
|
+
const b5 = (ci7 & 7) << 5 | ci8 & 31;
|
|
342
|
+
bytes[byteCount++] = b1;
|
|
343
|
+
if (byteCount < bytes.length) bytes[byteCount++] = b2;
|
|
344
|
+
if (byteCount < bytes.length) bytes[byteCount++] = b3;
|
|
345
|
+
if (byteCount < bytes.length && c5 !== PADDING) bytes[byteCount++] = b4;
|
|
346
|
+
if (byteCount < bytes.length && c7 != PADDING) bytes[byteCount++] = b5;
|
|
347
|
+
}
|
|
348
|
+
return bytes.subarray(0, byteCount);
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
//#endregion
|
|
352
|
+
//#region src/base64/index.ts
|
|
353
|
+
/**
|
|
354
|
+
* Base64 codec.
|
|
355
|
+
*/
|
|
356
|
+
var Base64 = class {};
|
|
357
|
+
//#endregion
|
|
358
|
+
//#region src/base64/errors.ts
|
|
359
|
+
/**
|
|
360
|
+
* Base error type of Base64.
|
|
361
|
+
*/
|
|
362
|
+
var KastenBase64Error = class extends KastenError {};
|
|
363
|
+
/**
|
|
364
|
+
* Thrown when error occurs on Base64 encoding.
|
|
365
|
+
*/
|
|
366
|
+
var KastenBase64EncodeError = class extends KastenBase64Error {};
|
|
367
|
+
/**
|
|
368
|
+
* Thrown when error occurs on Base64 decoding.
|
|
369
|
+
*/
|
|
370
|
+
var KastenBase64DecodeError = class extends KastenBase64Error {};
|
|
371
|
+
//#endregion
|
|
372
|
+
//#region src/base64/rfc2045/rfc2045.ts
|
|
373
|
+
var RFC2045Base64 = class RFC2045Base64 extends Base64 {
|
|
374
|
+
static PADDING_CHAR_CODE = "=".charCodeAt(0);
|
|
375
|
+
static TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
376
|
+
static DECODE_TABLE = new Uint8Array(256);
|
|
377
|
+
static ENCODE_TABLE = new Uint8Array([...this.TABLE].map((c) => c.charCodeAt(0)));
|
|
378
|
+
static decoder = new TextDecoder();
|
|
379
|
+
static {
|
|
380
|
+
this.DECODE_TABLE.fill(255);
|
|
381
|
+
for (let i = 0; i < this.TABLE.length; i++) this.DECODE_TABLE[this.TABLE.charCodeAt(i)] = i;
|
|
382
|
+
}
|
|
383
|
+
encode = (bytes) => {
|
|
384
|
+
if (bytes.length === 0) return "";
|
|
385
|
+
const ENCODE_TABLE = RFC2045Base64.ENCODE_TABLE;
|
|
386
|
+
const { CR, LF } = CharCodes;
|
|
387
|
+
const baseSize = Math.ceil(bytes.length / 3) * 4;
|
|
388
|
+
const linebreaks = Math.floor((baseSize - 1) / 76);
|
|
389
|
+
const chars = new Uint8Array(baseSize + linebreaks * 2);
|
|
390
|
+
let ci = 0;
|
|
391
|
+
let charCout = 0;
|
|
392
|
+
for (let i = 0; i < bytes.length; i += 3) {
|
|
393
|
+
const b1 = bytes[i];
|
|
394
|
+
const b2 = bytes[i + 1] ?? 0;
|
|
395
|
+
const b3 = bytes[i + 2] ?? 0;
|
|
396
|
+
const c1 = b1 >> 2;
|
|
397
|
+
const c2 = (b1 & 3) << 4 | b2 >> 4;
|
|
398
|
+
const c3 = (b2 & 15) << 2 | b3 >> 6;
|
|
399
|
+
const c4 = b3 & 63;
|
|
400
|
+
chars[ci++] = ENCODE_TABLE[c1];
|
|
401
|
+
chars[ci++] = ENCODE_TABLE[c2];
|
|
402
|
+
chars[ci++] = ENCODE_TABLE[c3];
|
|
403
|
+
chars[ci++] = ENCODE_TABLE[c4];
|
|
404
|
+
charCout += 4;
|
|
405
|
+
if (charCout === 76 && ci < chars.length) {
|
|
406
|
+
charCout = 0;
|
|
407
|
+
chars[ci++] = CR;
|
|
408
|
+
chars[ci++] = LF;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
const remain = bytes.length % 3;
|
|
412
|
+
if (remain === 1) {
|
|
413
|
+
chars[ci - 2] = RFC2045Base64.PADDING_CHAR_CODE;
|
|
414
|
+
chars[ci - 1] = RFC2045Base64.PADDING_CHAR_CODE;
|
|
415
|
+
} else if (remain === 2) chars[ci - 1] = RFC2045Base64.PADDING_CHAR_CODE;
|
|
416
|
+
return RFC2045Base64.decoder.decode(chars.subarray(0, ci));
|
|
417
|
+
};
|
|
418
|
+
decode = (base64) => {
|
|
419
|
+
const DECODE_TABLE = RFC2045Base64.DECODE_TABLE;
|
|
420
|
+
const line = new Uint8Array(base64.length);
|
|
421
|
+
let lineCharCount = 0;
|
|
422
|
+
for (let i = 0; i < base64.length; i++) {
|
|
423
|
+
const c = base64.charCodeAt(i);
|
|
424
|
+
if (c !== CharCodes.CR && c !== CharCodes.LF && c !== CharCodes.SP && c !== CharCodes.HT) line[lineCharCount++] = c;
|
|
425
|
+
}
|
|
426
|
+
if (lineCharCount % 4 !== 0) throw new KastenBase64DecodeError("Invalid length string");
|
|
427
|
+
let firstPad = -1;
|
|
428
|
+
for (let i = 0; i < lineCharCount; i++) if (line[i] === RFC2045Base64.PADDING_CHAR_CODE) {
|
|
429
|
+
firstPad = i;
|
|
430
|
+
break;
|
|
431
|
+
}
|
|
432
|
+
if (firstPad !== -1) {
|
|
433
|
+
if (firstPad < lineCharCount - 2) throw new KastenBase64DecodeError("Invalid padding position");
|
|
434
|
+
for (let i = firstPad; i < lineCharCount; i++) if (line[i] !== RFC2045Base64.PADDING_CHAR_CODE) throw new KastenBase64DecodeError("Invalid padding");
|
|
435
|
+
const padCount = lineCharCount - firstPad;
|
|
436
|
+
if (padCount !== 1 && padCount !== 2) throw new KastenBase64DecodeError("Invalid padding");
|
|
437
|
+
}
|
|
438
|
+
const lineLength = lineCharCount;
|
|
439
|
+
const padding = line[lineLength - 1] === RFC2045Base64.PADDING_CHAR_CODE ? line[lineLength - 2] === RFC2045Base64.PADDING_CHAR_CODE ? 2 : 1 : 0;
|
|
440
|
+
const bytes = new Uint8Array(lineCharCount / 4 * 3 - padding);
|
|
441
|
+
let writtenOffset = 0;
|
|
442
|
+
for (let i = 0; i < lineCharCount; i += 4) {
|
|
443
|
+
const code1 = line[i];
|
|
444
|
+
const code2 = line[i + 1];
|
|
445
|
+
const code3 = line[i + 2];
|
|
446
|
+
const code4 = line[i + 3];
|
|
447
|
+
const ci1 = DECODE_TABLE[code1];
|
|
448
|
+
const ci2 = DECODE_TABLE[code2];
|
|
449
|
+
const ci3 = code3 !== RFC2045Base64.PADDING_CHAR_CODE ? DECODE_TABLE[code3] : 0;
|
|
450
|
+
const ci4 = code4 !== RFC2045Base64.PADDING_CHAR_CODE ? DECODE_TABLE[code4] : 0;
|
|
451
|
+
if (ci1 === 255 || ci2 === 255 || code3 !== RFC2045Base64.PADDING_CHAR_CODE && ci3 === 255 || code4 !== RFC2045Base64.PADDING_CHAR_CODE && ci4 === 255) throw new KastenBase64DecodeError("Invalid Base64");
|
|
452
|
+
const b1 = ci1 << 2 | ci2 >> 4;
|
|
453
|
+
const b2 = (ci2 & 15) << 4 | ci3 >> 2;
|
|
454
|
+
const b3 = (ci3 & 3) << 6 | ci4;
|
|
455
|
+
bytes[writtenOffset++] = b1;
|
|
456
|
+
if (code3 !== RFC2045Base64.PADDING_CHAR_CODE) bytes[writtenOffset++] = b2;
|
|
457
|
+
if (code4 !== RFC2045Base64.PADDING_CHAR_CODE) bytes[writtenOffset++] = b3;
|
|
458
|
+
}
|
|
459
|
+
return bytes;
|
|
460
|
+
};
|
|
461
|
+
};
|
|
462
|
+
//#endregion
|
|
463
|
+
//#region src/base64/rfc4648/rfc4648.ts
|
|
464
|
+
var RFC4648Base64 = class RFC4648Base64 extends Base64 {
|
|
465
|
+
static PADDING_CHAR_CODE = "=".charCodeAt(0);
|
|
466
|
+
static TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
467
|
+
static DECODE_TABLE = new Uint8Array(256);
|
|
468
|
+
static ENCODE_TABLE = new Uint8Array([...this.TABLE].map((c) => c.charCodeAt(0)));
|
|
469
|
+
static decoder = new TextDecoder();
|
|
470
|
+
static {
|
|
471
|
+
this.DECODE_TABLE.fill(255);
|
|
472
|
+
for (let i = 0; i < this.TABLE.length; i++) this.DECODE_TABLE[this.TABLE.charCodeAt(i)] = i;
|
|
473
|
+
}
|
|
474
|
+
encode = (bytes) => {
|
|
475
|
+
if (bytes.length === 0) return "";
|
|
476
|
+
const ENCODE_TABLE = RFC4648Base64.ENCODE_TABLE;
|
|
477
|
+
const baseSize = Math.ceil(bytes.length / 3) * 4;
|
|
478
|
+
const linebreaks = Math.floor((baseSize - 1) / 76);
|
|
479
|
+
const chars = new Uint8Array(baseSize + linebreaks * 2);
|
|
480
|
+
let ci = 0;
|
|
481
|
+
for (let i = 0; i < bytes.length; i += 3) {
|
|
482
|
+
const b1 = bytes[i];
|
|
483
|
+
const b2 = bytes[i + 1] ?? 0;
|
|
484
|
+
const b3 = bytes[i + 2] ?? 0;
|
|
485
|
+
const c1 = b1 >> 2;
|
|
486
|
+
const c2 = (b1 & 3) << 4 | b2 >> 4;
|
|
487
|
+
const c3 = (b2 & 15) << 2 | b3 >> 6;
|
|
488
|
+
const c4 = b3 & 63;
|
|
489
|
+
chars[ci++] = ENCODE_TABLE[c1];
|
|
490
|
+
chars[ci++] = ENCODE_TABLE[c2];
|
|
491
|
+
chars[ci++] = ENCODE_TABLE[c3];
|
|
492
|
+
chars[ci++] = ENCODE_TABLE[c4];
|
|
493
|
+
}
|
|
494
|
+
const remain = bytes.length % 3;
|
|
495
|
+
if (remain === 1) {
|
|
496
|
+
chars[ci - 2] = RFC4648Base64.PADDING_CHAR_CODE;
|
|
497
|
+
chars[ci - 1] = RFC4648Base64.PADDING_CHAR_CODE;
|
|
498
|
+
} else if (remain === 2) chars[ci - 1] = RFC4648Base64.PADDING_CHAR_CODE;
|
|
499
|
+
return RFC4648Base64.decoder.decode(chars.subarray(0, ci));
|
|
500
|
+
};
|
|
501
|
+
decode = (base64) => {
|
|
502
|
+
const DECODE_TABLE = RFC4648Base64.DECODE_TABLE;
|
|
503
|
+
const line = new Uint8Array(base64.length);
|
|
504
|
+
let lineCharCount = 0;
|
|
505
|
+
for (let i = 0; i < base64.length; i++) {
|
|
506
|
+
const c = base64.charCodeAt(i);
|
|
507
|
+
if (!CharCodes.isWhitespace(c)) line[lineCharCount++] = c;
|
|
508
|
+
}
|
|
509
|
+
if (lineCharCount % 4 !== 0) throw new KastenBase64DecodeError("Invalid length string");
|
|
510
|
+
let firstPad = -1;
|
|
511
|
+
for (let i = 0; i < lineCharCount; i++) if (line[i] === RFC4648Base64.PADDING_CHAR_CODE) {
|
|
512
|
+
firstPad = i;
|
|
513
|
+
break;
|
|
514
|
+
}
|
|
515
|
+
if (firstPad !== -1) {
|
|
516
|
+
if (firstPad < lineCharCount - 2) throw new KastenBase64DecodeError("Invalid padding position");
|
|
517
|
+
for (let i = firstPad; i < lineCharCount; i++) if (line[i] !== RFC4648Base64.PADDING_CHAR_CODE) throw new KastenBase64DecodeError("Invalid padding");
|
|
518
|
+
const padCount = lineCharCount - firstPad;
|
|
519
|
+
if (padCount !== 1 && padCount !== 2) throw new KastenBase64DecodeError("Invalid padding");
|
|
520
|
+
}
|
|
521
|
+
const lineLength = lineCharCount;
|
|
522
|
+
const padding = line[lineLength - 1] === RFC4648Base64.PADDING_CHAR_CODE ? line[lineLength - 2] === RFC4648Base64.PADDING_CHAR_CODE ? 2 : 1 : 0;
|
|
523
|
+
const bytes = new Uint8Array(lineCharCount / 4 * 3 - padding);
|
|
524
|
+
let writtenOffset = 0;
|
|
525
|
+
for (let i = 0; i < lineCharCount; i += 4) {
|
|
526
|
+
const code1 = line[i];
|
|
527
|
+
const code2 = line[i + 1];
|
|
528
|
+
const code3 = line[i + 2];
|
|
529
|
+
const code4 = line[i + 3];
|
|
530
|
+
const ci1 = DECODE_TABLE[code1];
|
|
531
|
+
const ci2 = DECODE_TABLE[code2];
|
|
532
|
+
const ci3 = code3 !== RFC4648Base64.PADDING_CHAR_CODE ? DECODE_TABLE[code3] : 0;
|
|
533
|
+
const ci4 = code4 !== RFC4648Base64.PADDING_CHAR_CODE ? DECODE_TABLE[code4] : 0;
|
|
534
|
+
if (ci1 === 255 || ci2 === 255 || code3 !== RFC4648Base64.PADDING_CHAR_CODE && ci3 === 255 || code4 !== RFC4648Base64.PADDING_CHAR_CODE && ci4 === 255) throw new KastenBase64DecodeError("Invalid Base64");
|
|
535
|
+
const b1 = ci1 << 2 | ci2 >> 4;
|
|
536
|
+
const b2 = (ci2 & 15) << 4 | ci3 >> 2;
|
|
537
|
+
const b3 = (ci3 & 3) << 6 | ci4;
|
|
538
|
+
bytes[writtenOffset++] = b1;
|
|
539
|
+
if (code3 !== RFC4648Base64.PADDING_CHAR_CODE) bytes[writtenOffset++] = b2;
|
|
540
|
+
if (code4 !== RFC4648Base64.PADDING_CHAR_CODE) bytes[writtenOffset++] = b3;
|
|
541
|
+
}
|
|
542
|
+
return bytes;
|
|
543
|
+
};
|
|
544
|
+
};
|
|
545
|
+
//#endregion
|
|
546
|
+
//#region src/base64/rfc4648/rfc4648url.ts
|
|
547
|
+
var RFC4648Base64URL = class RFC4648Base64URL extends Base64 {
|
|
548
|
+
static PADDING_CHAR_CODE = "=".charCodeAt(0);
|
|
549
|
+
static TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
|
|
550
|
+
static DECODE_TABLE = new Uint8Array(256);
|
|
551
|
+
static ENCODE_TABLE = new Uint8Array([...this.TABLE].map((c) => c.charCodeAt(0)));
|
|
552
|
+
static decoder = new TextDecoder();
|
|
553
|
+
static {
|
|
554
|
+
this.DECODE_TABLE.fill(255);
|
|
555
|
+
for (let i = 0; i < this.TABLE.length; i++) this.DECODE_TABLE[this.TABLE.charCodeAt(i)] = i;
|
|
556
|
+
}
|
|
557
|
+
encode = (bytes) => {
|
|
558
|
+
if (bytes.length === 0) return "";
|
|
559
|
+
const ENCODE_TABLE = RFC4648Base64URL.ENCODE_TABLE;
|
|
560
|
+
const baseSize = Math.ceil(bytes.length / 3) * 4;
|
|
561
|
+
const linebreaks = Math.floor((baseSize - 1) / 76);
|
|
562
|
+
const chars = new Uint8Array(baseSize + linebreaks * 2);
|
|
563
|
+
let ci = 0;
|
|
564
|
+
for (let i = 0; i < bytes.length; i += 3) {
|
|
565
|
+
const b1 = bytes[i];
|
|
566
|
+
const b2 = bytes[i + 1] ?? 0;
|
|
567
|
+
const b3 = bytes[i + 2] ?? 0;
|
|
568
|
+
const c1 = b1 >> 2;
|
|
569
|
+
const c2 = (b1 & 3) << 4 | b2 >> 4;
|
|
570
|
+
const c3 = (b2 & 15) << 2 | b3 >> 6;
|
|
571
|
+
const c4 = b3 & 63;
|
|
572
|
+
chars[ci++] = ENCODE_TABLE[c1];
|
|
573
|
+
chars[ci++] = ENCODE_TABLE[c2];
|
|
574
|
+
chars[ci++] = ENCODE_TABLE[c3];
|
|
575
|
+
chars[ci++] = ENCODE_TABLE[c4];
|
|
576
|
+
}
|
|
577
|
+
const remain = bytes.length % 3;
|
|
578
|
+
if (remain === 1) {
|
|
579
|
+
chars[ci - 2] = RFC4648Base64URL.PADDING_CHAR_CODE;
|
|
580
|
+
chars[ci - 1] = RFC4648Base64URL.PADDING_CHAR_CODE;
|
|
581
|
+
} else if (remain === 2) chars[ci - 1] = RFC4648Base64URL.PADDING_CHAR_CODE;
|
|
582
|
+
return RFC4648Base64URL.decoder.decode(chars.subarray(0, ci));
|
|
583
|
+
};
|
|
584
|
+
decode = (base64) => {
|
|
585
|
+
const DECODE_TABLE = RFC4648Base64URL.DECODE_TABLE;
|
|
586
|
+
const line = new Uint8Array(base64.length);
|
|
587
|
+
let lineCharCount = 0;
|
|
588
|
+
for (let i = 0; i < base64.length; i++) {
|
|
589
|
+
const c = base64.charCodeAt(i);
|
|
590
|
+
if (!CharCodes.isWhitespace(c)) line[lineCharCount++] = c;
|
|
591
|
+
}
|
|
592
|
+
if (lineCharCount % 4 !== 0) throw new KastenBase64DecodeError("Invalid length string");
|
|
593
|
+
let firstPad = -1;
|
|
594
|
+
for (let i = 0; i < lineCharCount; i++) if (line[i] === RFC4648Base64URL.PADDING_CHAR_CODE) {
|
|
595
|
+
firstPad = i;
|
|
596
|
+
break;
|
|
597
|
+
}
|
|
598
|
+
if (firstPad !== -1) {
|
|
599
|
+
if (firstPad < lineCharCount - 2) throw new KastenBase64DecodeError("Invalid padding position");
|
|
600
|
+
for (let i = firstPad; i < lineCharCount; i++) if (line[i] !== RFC4648Base64URL.PADDING_CHAR_CODE) throw new KastenBase64DecodeError("Invalid padding");
|
|
601
|
+
const padCount = lineCharCount - firstPad;
|
|
602
|
+
if (padCount !== 1 && padCount !== 2) throw new KastenBase64DecodeError("Invalid padding");
|
|
603
|
+
}
|
|
604
|
+
const lineLength = lineCharCount;
|
|
605
|
+
const padding = line[lineLength - 1] === RFC4648Base64URL.PADDING_CHAR_CODE ? line[lineLength - 2] === RFC4648Base64URL.PADDING_CHAR_CODE ? 2 : 1 : 0;
|
|
606
|
+
const bytes = new Uint8Array(lineCharCount / 4 * 3 - padding);
|
|
607
|
+
let writtenOffset = 0;
|
|
608
|
+
for (let i = 0; i < lineCharCount; i += 4) {
|
|
609
|
+
const code1 = line[i];
|
|
610
|
+
const code2 = line[i + 1];
|
|
611
|
+
const code3 = line[i + 2];
|
|
612
|
+
const code4 = line[i + 3];
|
|
613
|
+
const ci1 = DECODE_TABLE[code1];
|
|
614
|
+
const ci2 = DECODE_TABLE[code2];
|
|
615
|
+
const ci3 = code3 !== RFC4648Base64URL.PADDING_CHAR_CODE ? DECODE_TABLE[code3] : 0;
|
|
616
|
+
const ci4 = code4 !== RFC4648Base64URL.PADDING_CHAR_CODE ? DECODE_TABLE[code4] : 0;
|
|
617
|
+
if (ci1 === 255 || ci2 === 255 || code3 !== RFC4648Base64URL.PADDING_CHAR_CODE && ci3 === 255 || code4 !== RFC4648Base64URL.PADDING_CHAR_CODE && ci4 === 255) throw new KastenBase64DecodeError("Invalid Base64");
|
|
618
|
+
const b1 = ci1 << 2 | ci2 >> 4;
|
|
619
|
+
const b2 = (ci2 & 15) << 4 | ci3 >> 2;
|
|
620
|
+
const b3 = (ci3 & 3) << 6 | ci4;
|
|
621
|
+
bytes[writtenOffset++] = b1;
|
|
622
|
+
if (code3 !== RFC4648Base64URL.PADDING_CHAR_CODE) bytes[writtenOffset++] = b2;
|
|
623
|
+
if (code4 !== RFC4648Base64URL.PADDING_CHAR_CODE) bytes[writtenOffset++] = b3;
|
|
624
|
+
}
|
|
625
|
+
return bytes;
|
|
626
|
+
};
|
|
627
|
+
};
|
|
628
|
+
//#endregion
|
|
629
|
+
//#region src/run-length/index.ts
|
|
630
|
+
/**
|
|
631
|
+
* Run-Length encoding and decoding.
|
|
632
|
+
*/
|
|
633
|
+
var RunLength = class {};
|
|
634
|
+
//#endregion
|
|
635
|
+
//#region src/run-length/basic/basic-run-length.ts
|
|
636
|
+
/**
|
|
637
|
+
* Basic Run-Length codec.
|
|
638
|
+
*/
|
|
639
|
+
var BasicRunLength = class extends RunLength {
|
|
640
|
+
encode(bytes) {
|
|
641
|
+
const encoded = new Uint8Array(bytes.length * 2);
|
|
642
|
+
let byteCount = 0;
|
|
643
|
+
let prev = null;
|
|
644
|
+
let count = 0;
|
|
645
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
646
|
+
const byte = bytes[i];
|
|
647
|
+
if (prev === null) {
|
|
648
|
+
prev = byte;
|
|
649
|
+
count = 1;
|
|
650
|
+
continue;
|
|
651
|
+
}
|
|
652
|
+
if (prev === byte) {
|
|
653
|
+
if (count < 255) {
|
|
654
|
+
count++;
|
|
655
|
+
continue;
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
encoded[byteCount++] = count;
|
|
659
|
+
encoded[byteCount++] = prev;
|
|
660
|
+
prev = byte;
|
|
661
|
+
count = 1;
|
|
662
|
+
}
|
|
663
|
+
if (prev !== null) {
|
|
664
|
+
encoded[byteCount++] = count;
|
|
665
|
+
encoded[byteCount++] = prev;
|
|
666
|
+
}
|
|
667
|
+
return encoded.subarray(0, byteCount);
|
|
668
|
+
}
|
|
669
|
+
decode(runLength) {
|
|
670
|
+
const decoded = [];
|
|
671
|
+
for (let i = 0; i < runLength.length; i += 2) {
|
|
672
|
+
const count = runLength[i];
|
|
673
|
+
const byte = runLength[i + 1];
|
|
674
|
+
for (let j = 0; j < count; j++) decoded.push(byte);
|
|
675
|
+
}
|
|
676
|
+
return new Uint8Array(decoded);
|
|
677
|
+
}
|
|
678
|
+
};
|
|
679
|
+
//#endregion
|
|
680
|
+
//#region src/run-length/errors.ts
|
|
681
|
+
var KastenRunLengthError = class extends KastenError {};
|
|
682
|
+
var KastenRunLengthEncodeError = class extends KastenRunLengthError {};
|
|
683
|
+
var KastenRunLengthDecodeError = class extends KastenRunLengthError {};
|
|
684
|
+
//#endregion
|
|
685
|
+
//#region src/run-length/pack-bits/pack-bits.ts
|
|
686
|
+
/**
|
|
687
|
+
* PackBits codec.
|
|
688
|
+
*/
|
|
689
|
+
var PackBits = class extends RunLength {
|
|
690
|
+
encode(bytes) {
|
|
691
|
+
if (bytes.length === 0) return new Uint8Array([]);
|
|
692
|
+
const byteLength = bytes.length;
|
|
693
|
+
const encoded = [];
|
|
694
|
+
let i = 0;
|
|
695
|
+
while (i < byteLength) {
|
|
696
|
+
const byte = bytes[i];
|
|
697
|
+
let j = 1;
|
|
698
|
+
while (i + j < byteLength && byte === bytes[i + j] && j < 128) j++;
|
|
699
|
+
if (j >= 3) {
|
|
700
|
+
encoded.push(256 - (j - 1));
|
|
701
|
+
encoded.push(byte);
|
|
702
|
+
i += j;
|
|
703
|
+
continue;
|
|
704
|
+
}
|
|
705
|
+
while (i + j < byteLength && j < 128) {
|
|
706
|
+
const pos = i + j;
|
|
707
|
+
const _byte = bytes[pos];
|
|
708
|
+
if (bytes[pos - 1] === _byte && _byte === bytes[pos + 1]) {
|
|
709
|
+
j--;
|
|
710
|
+
break;
|
|
711
|
+
}
|
|
712
|
+
j++;
|
|
713
|
+
}
|
|
714
|
+
encoded.push(j - 1);
|
|
715
|
+
for (let k = i; k < i + j; k++) encoded.push(bytes[k]);
|
|
716
|
+
i += j;
|
|
717
|
+
}
|
|
718
|
+
return new Uint8Array(encoded);
|
|
719
|
+
}
|
|
720
|
+
decode(runLength) {
|
|
721
|
+
const decoded = [];
|
|
722
|
+
const byteLength = runLength.length;
|
|
723
|
+
let i = 0;
|
|
724
|
+
while (i < byteLength) {
|
|
725
|
+
const header = runLength[i];
|
|
726
|
+
if (header === 128) continue;
|
|
727
|
+
if (0 <= header && header <= 127) {
|
|
728
|
+
const end = i + header + 2;
|
|
729
|
+
for (let j = i + 1; j < end; j++) {
|
|
730
|
+
const byte = runLength[j];
|
|
731
|
+
if (byte === void 0) throw new KastenRunLengthDecodeError("Invalid PackBits encoding");
|
|
732
|
+
decoded.push(byte);
|
|
733
|
+
}
|
|
734
|
+
i = end;
|
|
735
|
+
continue;
|
|
736
|
+
}
|
|
737
|
+
if (129 <= header && header <= 255) {
|
|
738
|
+
const count = 257 - header;
|
|
739
|
+
const byte = runLength[i + 1];
|
|
740
|
+
if (byte === void 0) throw new KastenRunLengthDecodeError("Invalid PackBits encoding");
|
|
741
|
+
for (let j = 0; j < count; j++) decoded.push(byte);
|
|
742
|
+
i += 2;
|
|
743
|
+
continue;
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
return new Uint8Array(decoded);
|
|
747
|
+
}
|
|
748
|
+
};
|
|
749
|
+
//#endregion
|
|
750
|
+
//#region src/run-length/pdf/pdf.ts
|
|
751
|
+
var Pdf = class extends RunLength {
|
|
752
|
+
encode(bytes) {
|
|
753
|
+
if (bytes.length === 0) return new Uint8Array([]);
|
|
754
|
+
const byteLength = bytes.length;
|
|
755
|
+
const encoded = [];
|
|
756
|
+
let i = 0;
|
|
757
|
+
while (i < byteLength) {
|
|
758
|
+
const byte = bytes[i];
|
|
759
|
+
let j = 1;
|
|
760
|
+
while (i + j < byteLength && byte === bytes[i + j] && j < 128) j++;
|
|
761
|
+
if (j >= 2) {
|
|
762
|
+
encoded.push(257 - j);
|
|
763
|
+
encoded.push(byte);
|
|
764
|
+
i += j;
|
|
765
|
+
continue;
|
|
766
|
+
}
|
|
767
|
+
while (i + j < byteLength && j < 128) {
|
|
768
|
+
const pos = i + j;
|
|
769
|
+
const _byte = bytes[pos];
|
|
770
|
+
if (bytes[pos - 1] === _byte && _byte === bytes[pos + 1]) {
|
|
771
|
+
j--;
|
|
772
|
+
break;
|
|
773
|
+
}
|
|
774
|
+
j++;
|
|
775
|
+
}
|
|
776
|
+
encoded.push(j - 1);
|
|
777
|
+
for (let k = i; k < i + j; k++) encoded.push(bytes[k]);
|
|
778
|
+
i += j;
|
|
779
|
+
}
|
|
780
|
+
return new Uint8Array(encoded);
|
|
781
|
+
}
|
|
782
|
+
decode(runLength) {
|
|
783
|
+
const decoded = [];
|
|
784
|
+
const byteLength = runLength.length;
|
|
785
|
+
let i = 0;
|
|
786
|
+
while (i < byteLength) {
|
|
787
|
+
const header = runLength[i];
|
|
788
|
+
if (header === 128) continue;
|
|
789
|
+
if (0 <= header && header <= 127) {
|
|
790
|
+
const end = i + header + 2;
|
|
791
|
+
for (let j = i + 1; j < end; j++) {
|
|
792
|
+
const byte = runLength[j];
|
|
793
|
+
if (byte === void 0) throw new KastenRunLengthDecodeError("Invalid PackBits encoding");
|
|
794
|
+
decoded.push(byte);
|
|
795
|
+
}
|
|
796
|
+
i = end;
|
|
797
|
+
continue;
|
|
798
|
+
}
|
|
799
|
+
if (129 <= header && header <= 255) {
|
|
800
|
+
const count = 257 - header;
|
|
801
|
+
const byte = runLength[i + 1];
|
|
802
|
+
if (byte === void 0) throw new KastenRunLengthDecodeError("Invalid PackBits encoding");
|
|
803
|
+
for (let j = 0; j < count; j++) decoded.push(byte);
|
|
804
|
+
i += 2;
|
|
805
|
+
continue;
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
return new Uint8Array(decoded);
|
|
809
|
+
}
|
|
810
|
+
};
|
|
811
|
+
//#endregion
|
|
812
|
+
//#region src/kasten.ts
|
|
813
|
+
/**
|
|
814
|
+
* Entry point of the Kasten library.
|
|
815
|
+
* All encoding/decoding feature implementations are created through this class.
|
|
816
|
+
*
|
|
817
|
+
* @example
|
|
818
|
+
* const base64 = Kasten.base64('rfc4648');
|
|
819
|
+
* const encoded = base64.encode(data);
|
|
820
|
+
*
|
|
821
|
+
* @example
|
|
822
|
+
* const base32 = Kasten.base32('rfc464hex');
|
|
823
|
+
* const decoded = base32.decode(text);
|
|
824
|
+
*
|
|
825
|
+
* @example
|
|
826
|
+
* const base16 = Kasten.base16();
|
|
827
|
+
*
|
|
828
|
+
* @example
|
|
829
|
+
* const runLength = Kasten.runLength('pack-bits');
|
|
830
|
+
*/
|
|
831
|
+
var Kasten = class {
|
|
832
|
+
constructor() {}
|
|
833
|
+
/**
|
|
834
|
+
* Creates Base 64 codec instance.
|
|
835
|
+
* @param category Base 64 category.
|
|
836
|
+
*/
|
|
837
|
+
static base64 = (category) => {
|
|
838
|
+
switch (category) {
|
|
839
|
+
case "rfc4648-url": return new RFC4648Base64URL();
|
|
840
|
+
case "rfc2025": return new RFC2045Base64();
|
|
841
|
+
default: return new RFC4648Base64();
|
|
842
|
+
}
|
|
843
|
+
};
|
|
844
|
+
/**
|
|
845
|
+
* Creates Base 32 codec instance.
|
|
846
|
+
* @param category Base 32 category.
|
|
847
|
+
*/
|
|
848
|
+
static base32 = (category) => {
|
|
849
|
+
switch (category) {
|
|
850
|
+
case "rfc468-hex": return new RFC4648Base32Hex();
|
|
851
|
+
default: return new RFC4648Base32();
|
|
852
|
+
}
|
|
853
|
+
};
|
|
854
|
+
/**
|
|
855
|
+
* Create Base 16 codec instance.
|
|
856
|
+
*/
|
|
857
|
+
static base16 = (category) => {
|
|
858
|
+
switch (category) {
|
|
859
|
+
case "ascii": return new AsciiHexDecode();
|
|
860
|
+
default: return new RFC4648Base16();
|
|
861
|
+
}
|
|
862
|
+
};
|
|
863
|
+
/**
|
|
864
|
+
* Creates Run-Length codec instance.
|
|
865
|
+
*/
|
|
866
|
+
static runLength = (category) => {
|
|
867
|
+
switch (category) {
|
|
868
|
+
case "pack-bits": return new PackBits();
|
|
869
|
+
case "pdf": return new Pdf();
|
|
870
|
+
default: return new BasicRunLength();
|
|
871
|
+
}
|
|
872
|
+
};
|
|
873
|
+
};
|
|
874
|
+
//#endregion
|
|
875
|
+
export { Base16, Base32, Base64, Kasten, KastenBase16DecodeError, KastenBase16EncodeError, KastenBase16Error, KastenBase32DecodeError, KastenBase32EncodeError, KastenBase32Error, KastenBase64DecodeError, KastenBase64EncodeError, KastenBase64Error, KastenError, KastenRunLengthDecodeError, KastenRunLengthEncodeError, KastenRunLengthError, RunLength };
|