@zip.js/zip.js 2.6.32 → 2.6.34

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.
Files changed (47) hide show
  1. package/dist/z-worker-fflate.js +1 -1
  2. package/dist/z-worker-pako.js +1 -1
  3. package/dist/z-worker.js +1 -1
  4. package/dist/zip-fs-full.js +2441 -2287
  5. package/dist/zip-fs-full.min.js +1 -1
  6. package/dist/zip-fs.js +1970 -1816
  7. package/dist/zip-fs.min.js +1 -1
  8. package/dist/zip-full.js +1969 -1815
  9. package/dist/zip-full.min.js +1 -1
  10. package/dist/zip-no-worker-deflate.min.js +1 -1
  11. package/dist/zip-no-worker-inflate.min.js +1 -1
  12. package/dist/zip-no-worker.min.js +1 -1
  13. package/dist/zip.js +1971 -1817
  14. package/dist/zip.min.js +1 -1
  15. package/index-fflate.js +3 -1
  16. package/index.cjs +2441 -2287
  17. package/index.d.ts +36 -7
  18. package/index.js +3 -1
  19. package/lib/core/codec-pool.js +6 -5
  20. package/lib/core/codec-worker.js +321 -0
  21. package/lib/core/configuration.js +11 -8
  22. package/lib/core/constants.js +11 -5
  23. package/lib/core/io.js +150 -51
  24. package/lib/core/streams/aes-crypto-stream.js +2 -0
  25. package/lib/core/streams/codec-stream.js +60 -13
  26. package/lib/core/streams/codecs/sjcl.js +1 -1
  27. package/lib/core/streams/stream-adapter.js +55 -0
  28. package/lib/core/streams/zip-crypto-stream.js +1 -0
  29. package/lib/core/streams/zip-entry-stream.js +1 -0
  30. package/lib/core/util/stream-codec-shim.js +2 -0
  31. package/lib/core/z-worker-core.js +23 -12
  32. package/lib/core/zip-entry.js +2 -1
  33. package/lib/core/zip-fs-core.js +1 -0
  34. package/lib/core/zip-reader.js +16 -12
  35. package/lib/core/zip-writer.js +102 -54
  36. package/lib/z-worker-inline.js +1 -1
  37. package/lib/zip.js +1 -1
  38. package/package.json +5 -5
  39. package/rollup-es5.config.dev.js +199 -0
  40. package/rollup-es5.config.js +243 -0
  41. package/tests/all/test-http-split-zip.js +37 -0
  42. package/tests/all/test-readable-stream.js +1 -2
  43. package/tests/all/test-readable-zip-stream.js +1 -1
  44. package/tests/all/test-split-zip.js +25 -22
  45. package/tests/tests-data.js +2 -1
  46. package/lib/core/codec-interface.js +0 -115
  47. package/lib/core/codec-pool-worker.js +0 -289
package/dist/zip-fs.js CHANGED
@@ -34,37 +34,50 @@
34
34
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
35
  */
36
36
 
37
- const table = [];
38
- for (let i = 0; i < 256; i++) {
39
- let t = i;
40
- for (let j = 0; j < 8; j++) {
41
- if (t & 1) {
42
- t = (t >>> 1) ^ 0xEDB88320;
43
- } else {
44
- t = t >>> 1;
45
- }
46
- }
47
- table[i] = t;
48
- }
37
+ const MAX_32_BITS = 0xffffffff;
38
+ const MAX_16_BITS = 0xffff;
39
+ const COMPRESSION_METHOD_DEFLATE = 0x08;
40
+ const COMPRESSION_METHOD_STORE = 0x00;
41
+ const COMPRESSION_METHOD_AES = 0x63;
49
42
 
50
- class Crc32 {
43
+ const LOCAL_FILE_HEADER_SIGNATURE = 0x04034b50;
44
+ const SPLIT_ZIP_FILE_SIGNATURE = 0x08074b50;
45
+ const DATA_DESCRIPTOR_RECORD_SIGNATURE = SPLIT_ZIP_FILE_SIGNATURE;
46
+ const CENTRAL_FILE_HEADER_SIGNATURE = 0x02014b50;
47
+ const END_OF_CENTRAL_DIR_SIGNATURE = 0x06054b50;
48
+ const ZIP64_END_OF_CENTRAL_DIR_SIGNATURE = 0x06064b50;
49
+ const ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIGNATURE = 0x07064b50;
50
+ const END_OF_CENTRAL_DIR_LENGTH = 22;
51
+ const ZIP64_END_OF_CENTRAL_DIR_LOCATOR_LENGTH = 20;
52
+ const ZIP64_END_OF_CENTRAL_DIR_LENGTH = 56;
53
+ const ZIP64_END_OF_CENTRAL_DIR_TOTAL_LENGTH = END_OF_CENTRAL_DIR_LENGTH + ZIP64_END_OF_CENTRAL_DIR_LOCATOR_LENGTH + ZIP64_END_OF_CENTRAL_DIR_LENGTH;
51
54
 
52
- constructor(crc) {
53
- this.crc = crc || -1;
54
- }
55
+ const EXTRAFIELD_TYPE_ZIP64 = 0x0001;
56
+ const EXTRAFIELD_TYPE_AES = 0x9901;
57
+ const EXTRAFIELD_TYPE_NTFS = 0x000a;
58
+ const EXTRAFIELD_TYPE_NTFS_TAG1 = 0x0001;
59
+ const EXTRAFIELD_TYPE_EXTENDED_TIMESTAMP = 0x5455;
60
+ const EXTRAFIELD_TYPE_UNICODE_PATH = 0x7075;
61
+ const EXTRAFIELD_TYPE_UNICODE_COMMENT = 0x6375;
55
62
 
56
- append(data) {
57
- let crc = this.crc | 0;
58
- for (let offset = 0, length = data.length | 0; offset < length; offset++) {
59
- crc = (crc >>> 8) ^ table[(crc ^ data[offset]) & 0xFF];
60
- }
61
- this.crc = crc;
62
- }
63
+ const BITFLAG_ENCRYPTED = 0x01;
64
+ const BITFLAG_LEVEL = 0x06;
65
+ const BITFLAG_DATA_DESCRIPTOR = 0x0008;
66
+ const BITFLAG_LANG_ENCODING_FLAG = 0x0800;
67
+ const FILE_ATTR_MSDOS_DIR_MASK = 0x10;
63
68
 
64
- get() {
65
- return ~this.crc;
66
- }
67
- }
69
+ const VERSION_DEFLATE = 0x14;
70
+ const VERSION_ZIP64 = 0x2D;
71
+ const VERSION_AES = 0x33;
72
+
73
+ const DIRECTORY_SIGNATURE = "/";
74
+
75
+ const MAX_DATE = new Date(2107, 11, 31);
76
+ const MIN_DATE = new Date(1980, 0, 1);
77
+
78
+ const UNDEFINED_VALUE = undefined;
79
+ const UNDEFINED_TYPE$1 = "undefined";
80
+ const FUNCTION_TYPE$1 = "function";
68
81
 
69
82
  /*
70
83
  Copyright (c) 2022 Gildas Lormeau. All rights reserved.
@@ -94,21 +107,25 @@
94
107
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
95
108
  */
96
109
 
97
- class Crc32Stream extends TransformStream {
110
+ class StreamAdapter {
98
111
 
99
- constructor() {
100
- const crc32 = new Crc32();
101
- super({
102
- transform(chunk) {
103
- crc32.append(chunk);
104
- },
105
- flush(controller) {
106
- const value = new Uint8Array(4);
107
- const dataView = new DataView(value.buffer);
108
- dataView.setUint32(0, crc32.get());
109
- controller.enqueue(value);
112
+ constructor(Codec) {
113
+ return class extends TransformStream {
114
+ constructor(options) {
115
+ const codec = new Codec(options);
116
+ super({
117
+ transform(chunk, controller) {
118
+ controller.enqueue(codec.append(chunk));
119
+ },
120
+ flush(controller) {
121
+ const chunk = codec.flush();
122
+ if (chunk) {
123
+ controller.enqueue(chunk);
124
+ }
125
+ }
126
+ });
110
127
  }
111
- });
128
+ };
112
129
  }
113
130
  }
114
131
 
@@ -140,1302 +157,1185 @@
140
157
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
141
158
  */
142
159
 
143
- function encodeText(value) {
144
- if (typeof TextEncoder == "undefined") {
145
- value = unescape(encodeURIComponent(value));
146
- const result = new Uint8Array(value.length);
147
- for (let i = 0; i < result.length; i++) {
148
- result[i] = value.charCodeAt(i);
160
+ const MINIMUM_CHUNK_SIZE = 64;
161
+ let maxWorkers = 2;
162
+ try {
163
+ if (typeof navigator != UNDEFINED_TYPE$1 && navigator.hardwareConcurrency) {
164
+ maxWorkers = navigator.hardwareConcurrency;
165
+ }
166
+ } catch (_error) {
167
+ // ignored
168
+ }
169
+ const DEFAULT_CONFIGURATION = {
170
+ chunkSize: 512 * 1024,
171
+ maxWorkers,
172
+ terminateWorkerTimeout: 5000,
173
+ useWebWorkers: true,
174
+ useCompressionStream: true,
175
+ workerScripts: UNDEFINED_VALUE,
176
+ CompressionStreamNative: typeof CompressionStream != UNDEFINED_TYPE$1 && CompressionStream,
177
+ DecompressionStreamNative: typeof DecompressionStream != UNDEFINED_TYPE$1 && DecompressionStream
178
+ };
179
+
180
+ const config = Object.assign({}, DEFAULT_CONFIGURATION);
181
+
182
+ function getConfiguration() {
183
+ return config;
184
+ }
185
+
186
+ function getChunkSize(config) {
187
+ return Math.max(config.chunkSize, MINIMUM_CHUNK_SIZE);
188
+ }
189
+
190
+ function configure(configuration) {
191
+ const {
192
+ baseURL,
193
+ chunkSize,
194
+ maxWorkers,
195
+ terminateWorkerTimeout,
196
+ useCompressionStream,
197
+ useWebWorkers,
198
+ Deflate,
199
+ Inflate,
200
+ CompressionStream,
201
+ DecompressionStream,
202
+ workerScripts
203
+ } = configuration;
204
+ setIfDefined("baseURL", baseURL);
205
+ setIfDefined("chunkSize", chunkSize);
206
+ setIfDefined("maxWorkers", maxWorkers);
207
+ setIfDefined("terminateWorkerTimeout", terminateWorkerTimeout);
208
+ setIfDefined("useCompressionStream", useCompressionStream);
209
+ setIfDefined("useWebWorkers", useWebWorkers);
210
+ if (Deflate) {
211
+ config.CompressionStream = new StreamAdapter(Deflate);
212
+ }
213
+ if (Inflate) {
214
+ config.DecompressionStream = new StreamAdapter(Inflate);
215
+ }
216
+ setIfDefined("CompressionStream", CompressionStream);
217
+ setIfDefined("DecompressionStream", DecompressionStream);
218
+ if (workerScripts !== UNDEFINED_VALUE) {
219
+ const { deflate, inflate } = workerScripts;
220
+ if (deflate || inflate) {
221
+ if (!config.workerScripts) {
222
+ config.workerScripts = {};
223
+ }
224
+ }
225
+ if (deflate) {
226
+ if (!Array.isArray(deflate)) {
227
+ throw new Error("workerScripts.deflate must be an array");
228
+ }
229
+ config.workerScripts.deflate = deflate;
230
+ }
231
+ if (inflate) {
232
+ if (!Array.isArray(inflate)) {
233
+ throw new Error("workerScripts.inflate must be an array");
234
+ }
235
+ config.workerScripts.inflate = inflate;
149
236
  }
150
- return result;
151
- } else {
152
- return new TextEncoder().encode(value);
153
237
  }
154
238
  }
155
239
 
156
- // Derived from https://github.com/xqdoo00o/jszip/blob/master/lib/sjcl.js and https://github.com/bitwiseshiftleft/sjcl
240
+ function setIfDefined(propertyName, propertyValue) {
241
+ if (propertyValue !== UNDEFINED_VALUE) {
242
+ config[propertyName] = propertyValue;
243
+ }
244
+ }
157
245
 
158
- /*// deno-lint-ignore-file no-this-alias *
246
+ function e(e){const t=()=>URL.createObjectURL(new Blob(['const{Array:e,Object:t,Math:n,Error:r,Uint8Array:s,Uint16Array:i,Uint32Array:o,Int32Array:c,Map:f,DataView:a,Promise:l,TextEncoder:u,crypto:w,postMessage:h,TransformStream:d,ReadableStream:p,WritableStream:y,CompressionStream:m,DecompressionStream:b}=self;class g{constructor(e){return class extends d{constructor(t){const n=new e(t);super({transform(e,t){t.enqueue(n.append(e))},flush(e){const t=n.flush();t&&e.enqueue(t)}})}}}}const k=[];for(let e=0;256>e;e++){let t=e;for(let e=0;8>e;e++)1&t?t=t>>>1^3988292384:t>>>=1;k[e]=t}class v{constructor(e){this.t=e||-1}append(e){let t=0|this.t;for(let n=0,r=0|e.length;r>n;n++)t=t>>>8^k[255&(t^e[n])];this.t=t}get(){return~this.t}}class S extends d{constructor(){const e=new v;super({transform(t){e.append(t)},flush(t){const n=new s(4);new a(n.buffer).setUint32(0,e.get()),t.enqueue(n)}})}}const z={concat(e,t){if(0===e.length||0===t.length)return e.concat(t);const n=e[e.length-1],r=z.i(n);return 32===r?e.concat(t):z.o(t,r,0|n,e.slice(0,e.length-1))},l(e){const t=e.length;if(0===t)return 0;const n=e[t-1];return 32*(t-1)+z.i(n)},u(e,t){if(32*e.length<t)return e;const r=(e=e.slice(0,n.ceil(t/32))).length;return t&=31,r>0&&t&&(e[r-1]=z.h(t,e[r-1]&2147483648>>t-1,1)),e},h:(e,t,n)=>32===e?t:(n?0|t:t<<32-e)+1099511627776*e,i:e=>n.round(e/1099511627776)||32,o(e,t,n,r){for(void 0===r&&(r=[]);t>=32;t-=32)r.push(n),n=0;if(0===t)return r.concat(e);for(let s=0;s<e.length;s++)r.push(n|e[s]>>>t),n=e[s]<<32-t;const s=e.length?e[e.length-1]:0,i=z.i(s);return r.push(z.h(t+i&31,t+i>32?n:r.pop(),1)),r}},_={p:{m(e){const t=z.l(e)/8,n=new s(t);let r;for(let s=0;t>s;s++)0==(3&s)&&(r=e[s/4]),n[s]=r>>>24,r<<=8;return n},g(e){const t=[];let n,r=0;for(n=0;n<e.length;n++)r=r<<8|e[n],3==(3&n)&&(t.push(r),r=0);return 3&n&&t.push(z.h(8*(3&n),r)),t}}},C={k:function(e){e?(this.v=e.v.slice(0),this.S=e.S.slice(0),this._=e._):this.reset()}};C.k.prototype={blockSize:512,reset(){const e=this;return e.v=this.C.slice(0),e.S=[],e._=0,e},update(e){const t=this;"string"==typeof e&&(e=_.I.g(e));const n=t.S=z.concat(t.S,e),s=t._,i=t._=s+z.l(e);if(i>9007199254740991)throw new r("Cannot hash more than 2^53 - 1 bits");const c=new o(n);let f=0;for(let e=t.blockSize+s-(t.blockSize+s&t.blockSize-1);i>=e;e+=t.blockSize)t.A(c.subarray(16*f,16*(f+1))),f+=1;return n.splice(0,16*f),t},D(){const e=this;let t=e.S;const r=e.v;t=z.concat(t,[z.h(1,1)]);for(let e=t.length+2;15&e;e++)t.push(0);for(t.push(n.floor(e._/4294967296)),t.push(0|e._);t.length;)e.A(t.splice(0,16));return e.reset(),r},C:[1732584193,4023233417,2562383102,271733878,3285377520],V:[1518500249,1859775393,2400959708,3395469782],R:(e,t,n,r)=>e>19?e>39?e>59?e>79?void 0:t^n^r:t&n|t&r|n&r:t^n^r:t&n|~t&r,B:(e,t)=>t<<e|t>>>32-e,A(t){const r=this,s=r.v,i=e(80);for(let e=0;16>e;e++)i[e]=t[e];let o=s[0],c=s[1],f=s[2],a=s[3],l=s[4];for(let e=0;79>=e;e++){16>e||(i[e]=r.B(1,i[e-3]^i[e-8]^i[e-14]^i[e-16]));const t=r.B(5,o)+r.R(e,c,f,a)+l+i[e]+r.V[n.floor(e/20)]|0;l=a,a=f,f=r.B(30,c),c=o,o=t}s[0]=s[0]+o|0,s[1]=s[1]+c|0,s[2]=s[2]+f|0,s[3]=s[3]+a|0,s[4]=s[4]+l|0}};const I={getRandomValues(e){const t=new o(e.buffer),r=e=>{let t=987654321;const r=4294967295;return()=>(t=36969*(65535&t)+(t>>16)&r,(((t<<16)+(e=18e3*(65535&e)+(e>>16)&r)&r)/4294967296+.5)*(n.random()>.5?1:-1))};for(let s,i=0;i<e.length;i+=4){const e=r(4294967296*(s||n.random()));s=987654071*e(),t[i/4]=4294967296*e()|0}return e}},x={importKey:e=>new x.M(_.p.g(e)),K(e,t,n,s){if(n=n||1e4,0>s||0>n)throw new r("invalid params to pbkdf2");const i=1+(s>>5)<<2;let o,c,f,l,u;const w=new ArrayBuffer(i),h=new a(w);let d=0;const p=z;for(t=_.p.g(t),u=1;(i||1)>d;u++){for(o=c=e.encrypt(p.concat(t,[u])),f=1;n>f;f++)for(c=e.encrypt(c),l=0;l<c.length;l++)o[l]^=c[l];for(f=0;(i||1)>d&&f<o.length;f++)h.setInt32(d,o[f]),d+=4}return w.slice(0,s/8)},M:class{constructor(e){const t=this,n=t.N=C.k,r=[[],[]],s=n.prototype.blockSize/32;t.P=[new n,new n],e.length>s&&(e=n.hash(e));for(let t=0;s>t;t++)r[0][t]=909522486^e[t],r[1][t]=1549556828^e[t];t.P[0].update(r[0]),t.P[1].update(r[1]),t.U=new n(t.P[0])}reset(){const e=this;e.U=new e.N(e.P[0]),e.T=!1}update(e){this.T=!0,this.U.update(e)}digest(){const e=this,t=e.U.D(),n=new e.N(e.P[1]).update(t).D();return e.reset(),n}encrypt(e){if(this.T)throw new r("encrypt on already updated hmac called!");return this.update(e),this.digest(e)}}},A=void 0!==w&&"function"==typeof w.getRandomValues;function D(e){return A?w.getRandomValues(e):I.getRandomValues(e)}const V={name:"PBKDF2"},R=t.assign({hash:{name:"HMAC"}},V),B=t.assign({iterations:1e3,hash:{name:"SHA-1"}},V),E=["deriveBits"],M=[8,12,16],K=[16,24,32],N=[0,0,0,0],P=void 0!==w,U=P&&w.subtle,T=P&&void 0!==U,W=P&&T&&"function"==typeof U.importKey,H=P&&T&&"function"==typeof U.deriveBits,L=_.p,j=class{constructor(e){const t=this;t.W=[[[],[],[],[],[]],[[],[],[],[],[]]],t.W[0][0][0]||t.H();const n=t.W[0][4],s=t.W[1],i=e.length;let o,c,f,a=1;if(4!==i&&6!==i&&8!==i)throw new r("invalid aes key size");for(t.V=[c=e.slice(0),f=[]],o=i;4*i+28>o;o++){let e=c[o-1];(o%i==0||8===i&&o%i==4)&&(e=n[e>>>24]<<24^n[e>>16&255]<<16^n[e>>8&255]<<8^n[255&e],o%i==0&&(e=e<<8^e>>>24^a<<24,a=a<<1^283*(a>>7))),c[o]=c[o-i]^e}for(let e=0;o;e++,o--){const t=c[3&e?o:o-4];f[e]=4>=o||4>e?t:s[0][n[t>>>24]]^s[1][n[t>>16&255]]^s[2][n[t>>8&255]]^s[3][n[255&t]]}}encrypt(e){return this.L(e,0)}decrypt(e){return this.L(e,1)}H(){const e=this.W[0],t=this.W[1],n=e[4],r=t[4],s=[],i=[];let o,c,f,a;for(let e=0;256>e;e++)i[(s[e]=e<<1^283*(e>>7))^e]=e;for(let l=o=0;!n[l];l^=c||1,o=i[o]||1){let i=o^o<<1^o<<2^o<<3^o<<4;i=i>>8^255&i^99,n[l]=i,r[i]=l,a=s[f=s[c=s[l]]];let u=16843009*a^65537*f^257*c^16843008*l,w=257*s[i]^16843008*i;for(let n=0;4>n;n++)e[n][l]=w=w<<24^w>>>8,t[n][i]=u=u<<24^u>>>8}for(let n=0;5>n;n++)e[n]=e[n].slice(0),t[n]=t[n].slice(0)}L(e,t){if(4!==e.length)throw new r("invalid aes block size");const n=this.V[t],s=n.length/4-2,i=[0,0,0,0],o=this.W[t],c=o[0],f=o[1],a=o[2],l=o[3],u=o[4];let w,h,d,p=e[0]^n[0],y=e[t?3:1]^n[1],m=e[2]^n[2],b=e[t?1:3]^n[3],g=4;for(let e=0;s>e;e++)w=c[p>>>24]^f[y>>16&255]^a[m>>8&255]^l[255&b]^n[g],h=c[y>>>24]^f[m>>16&255]^a[b>>8&255]^l[255&p]^n[g+1],d=c[m>>>24]^f[b>>16&255]^a[p>>8&255]^l[255&y]^n[g+2],b=c[b>>>24]^f[p>>16&255]^a[y>>8&255]^l[255&m]^n[g+3],g+=4,p=w,y=h,m=d;for(let e=0;4>e;e++)i[t?3&-e:e]=u[p>>>24]<<24^u[y>>16&255]<<16^u[m>>8&255]<<8^u[255&b]^n[g++],w=p,p=y,y=m,m=b,b=w;return i}},F=class{constructor(e,t){this.j=e,this.F=t,this.O=t}reset(){this.O=this.F}update(e){return this.q(this.j,e,this.O)}G(e){if(255==(e>>24&255)){let t=e>>16&255,n=e>>8&255,r=255&e;255===t?(t=0,255===n?(n=0,255===r?r=0:++r):++n):++t,e=0,e+=t<<16,e+=n<<8,e+=r}else e+=1<<24;return e}J(e){0===(e[0]=this.G(e[0]))&&(e[1]=this.G(e[1]))}q(e,t,n){let r;if(!(r=t.length))return[];const s=z.l(t);for(let s=0;r>s;s+=4){this.J(n);const r=e.encrypt(n);t[s]^=r[0],t[s+1]^=r[1],t[s+2]^=r[2],t[s+3]^=r[3]}return z.u(t,s)}},O=x.M;class q extends d{constructor(e,n,i){super({start(){t.assign(this,{ready:new l((e=>this.X=e)),password:e,signed:n,Y:i-1,pending:new s})},async transform(e,t){const n=this,{password:i,Y:o,X:c,ready:f}=n;i?(await(async(e,t,n,s)=>{const i=await Q(e,t,n,Y(s,0,M[t])),o=Y(s,M[t]);if(i[0]!=o[0]||i[1]!=o[1])throw new r("Invalid password")})(n,o,i,Y(e,0,M[o]+2)),e=Y(e,M[o]+2),c()):await f;const a=new s(e.length-10-(e.length-10)%16);t.enqueue(J(n,e,a,0,10,!0))},async flush(e){const{signed:t,Z:n,$:i,pending:o,ready:c}=this;await c;const f=Y(o,0,o.length-10),a=Y(o,o.length-10);let l=new s;if(f.length){const e=$(L,f);i.update(e);const t=n.update(e);l=Z(L,t)}if(t){const e=Y(Z(L,i.digest()),0,10);for(let t=0;10>t;t++)if(e[t]!=a[t])throw new r("Invalid signature")}e.enqueue(l)}})}}class G extends d{constructor(e,n){let r;super({start(){t.assign(this,{ready:new l((e=>this.X=e)),password:e,Y:n-1,pending:new s})},async transform(e,t){const n=this,{password:r,Y:i,X:o,ready:c}=n;let f=new s;r?(f=await(async(e,t,n)=>{const r=D(new s(M[t]));return X(r,await Q(e,t,n,r))})(n,i,r),o()):await c;const a=new s(f.length+e.length-e.length%16);a.set(f,0),t.enqueue(J(n,e,a,f.length,0))},async flush(e){const{Z:t,$:n,pending:i,ready:o}=this;await o;let c=new s;if(i.length){const e=t.update($(L,i));n.update(e),c=Z(L,e)}r.signature=Z(L,n.digest()).slice(0,10),e.enqueue(X(c,r.signature))}}),r=this}}function J(e,t,n,r,i,o){const{Z:c,$:f,pending:a}=e,l=t.length-i;let u;for(a.length&&(t=X(a,t),n=((e,t)=>{if(t&&t>e.length){const n=e;(e=new s(t)).set(n,0)}return e})(n,l-l%16)),u=0;l-16>=u;u+=16){const e=$(L,Y(t,u,u+16));o&&f.update(e);const s=c.update(e);o||f.update(s),n.set(Z(L,s),u+r)}return e.pending=Y(t,u),n}async function Q(n,r,i,o){n.password=null;const c=(e=>{if(void 0===u){const t=new s((e=unescape(encodeURIComponent(e))).length);for(let n=0;n<t.length;n++)t[n]=e.charCodeAt(n);return t}return(new u).encode(e)})(i),f=await((e,t,n,r,s)=>W?U.importKey("raw",t,n,!1,s):x.importKey(t))(0,c,R,0,E),a=await(async(e,t,n)=>H?await U.deriveBits(e,t,n):x.K(t,e.salt,B.iterations,n))(t.assign({salt:o},B),f,8*(2*K[r]+2)),l=new s(a),w=$(L,Y(l,0,K[r])),h=$(L,Y(l,K[r],2*K[r])),d=Y(l,2*K[r]);return t.assign(n,{keys:{key:w,ee:h,passwordVerification:d},Z:new F(new j(w),e.from(N)),$:new O(h)}),d}function X(e,t){let n=e;return e.length+t.length&&(n=new s(e.length+t.length),n.set(e,0),n.set(t,e.length)),n}function Y(e,t,n){return e.subarray(t,n)}function Z(e,t){return e.m(t)}function $(e,t){return e.g(t)}class ee extends d{constructor(e,n){super({start(){t.assign(this,{password:e,passwordVerification:n}),se(this,e)},transform(e,t){const n=this;if(n.password){const t=ne(n,e.subarray(0,12));if(n.password=null,t[11]!=n.passwordVerification)throw new r("Invalid password");e=e.subarray(12)}t.enqueue(ne(n,e))}})}}class te extends d{constructor(e,n){super({start(){t.assign(this,{password:e,passwordVerification:n}),se(this,e)},transform(e,t){const n=this;let r,i;if(n.password){n.password=null;const t=D(new s(12));t[11]=n.passwordVerification,r=new s(e.length+t.length),r.set(re(n,t),0),i=12}else r=new s(e.length),i=0;r.set(re(n,e),i),t.enqueue(r)}})}}function ne(e,t){const n=new s(t.length);for(let r=0;r<t.length;r++)n[r]=oe(e)^t[r],ie(e,n[r]);return n}function re(e,t){const n=new s(t.length);for(let r=0;r<t.length;r++)n[r]=oe(e)^t[r],ie(e,t[r]);return n}function se(e,n){const r=[305419896,591751049,878082192];t.assign(e,{keys:r,te:new v(r[0]),ne:new v(r[2])});for(let t=0;t<n.length;t++)ie(e,n.charCodeAt(t))}function ie(e,t){let[r,s,i]=e.keys;e.te.append([t]),r=~e.te.get(),s=fe(n.imul(fe(s+ce(r)),134775813)+1),e.ne.append([s>>>24]),i=~e.ne.get(),e.keys=[r,s,i]}function oe(e){const t=2|e.keys[2];return ce(n.imul(t,1^t)>>>8)}function ce(e){return 255&e}function fe(e){return 4294967295&e}class ae extends d{constructor(e,{chunkSize:t,CompressionStream:n,re:r}){super({});const{compressed:s,encrypted:i,useCompressionStream:o,password:c,passwordVerification:f,encryptionStrength:l,zipCrypto:u,signed:w,level:h}=e,d=this;let p,y,m=ue(super.readable);i&&!u||!w||([m,p]=m.tee(),p=de(p,new S)),s&&(m=he(m,o,{level:h,chunkSize:t},r,n)),i&&(u?m=de(m,new te(c,f)):(y=new G(c,l),m=de(m,y))),we(d,m,(async()=>{let e;i&&!u&&(e=y.signature),i&&!u||!w||(e=await p.getReader().read(),e=new a(e.value.buffer).getUint32(0)),d.signature=e}))}}class le extends d{constructor(e,{chunkSize:t,DecompressionStream:n,se:s}){super({});const{zipCrypto:i,encrypted:o,password:c,passwordVerification:f,signed:l,signature:u,encryptionStrength:w,compressed:h,useCompressionStream:d}=e;let p,y,m=ue(super.readable);o&&(i?m=de(m,new ee(c,f)):(y=new q(c,l,w),m=de(m,y))),h&&(m=he(m,d,{chunkSize:t},s,n)),o&&!i||!l||([m,p]=m.tee(),p=de(p,new S)),we(this,m,(async()=>{if((!o||i)&&l){const e=await p.getReader().read(),t=new a(e.value.buffer);if(u!=t.getUint32(0,!1))throw new r("Invalid signature")}}))}}function ue(e){return de(e,new d({transform(e,t){e&&e.length&&t.enqueue(e)}}))}function we(e,n,r){n=de(n,new d({flush:r})),t.defineProperty(e,"readable",{get:()=>n})}function he(e,t,n,r,s){try{e=de(e,new(t&&r?r:s)("deflate-raw",n))}catch(r){if(!t)throw r;e=de(e,new s("deflate-raw",n))}return e}function de(e,t){return e.pipeThrough(t)}class pe extends d{constructor(e,n){super({});const r=this,{codecType:s}=e;let i;s.startsWith("deflate")?i=ae:s.startsWith("inflate")&&(i=le);let o=0;const c=new i(e,n),f=super.readable,a=new d({transform(e,t){e&&e.length&&(o+=e.length,t.enqueue(e))},flush(){const{signature:e}=c;t.assign(r,{signature:e,size:o})}});t.defineProperty(r,"readable",{get:()=>f.pipeThrough(c).pipeThrough(a)})}}const ye=new f,me=new f;let be=0;async function ge(e){try{const{options:t,scripts:n,config:r}=e;n&&n.length&&importScripts.apply(void 0,n),self.initCodec&&self.initCodec(),r.re=self.CompressionStream,r.se=self.DecompressionStream,self.Deflate&&(r.CompressionStream=new g(self.Deflate)),self.Inflate&&(r.DecompressionStream=new g(self.Inflate));const s={highWaterMark:1,size:()=>r.chunkSize},i=e.readable||new p({async pull(e){const t=new l((e=>ye.set(be,e)));ke({type:"pull",messageId:be}),be=(be+1)%Number.MAX_SAFE_INTEGER;const{value:n,done:r}=await t;e.enqueue(n),r&&e.close()}},s),o=e.writable||new y({async write(e){let t;const n=new l((e=>t=e));me.set(be,t),ke({type:"data",data:e,messageId:be}),be=(be+1)%Number.MAX_SAFE_INTEGER,await n}},s),c=new pe(t,r);await i.pipeThrough(c).pipeTo(o,{ie:!0});try{await o.close()}catch(e){}const{signature:f,size:a}=c;ke({type:"close",result:{signature:f,size:a}})}catch(e){ve(e)}}function ke(e){if(e.data){let{data:t}=e;if(t&&t.length)try{t=new s(t),e.data=t.buffer,h(e,[e.data])}catch(t){h(e)}else h(e)}else h(e)}function ve(e){const{message:t,stack:n,code:r,name:s}=e;h({error:{message:t,stack:n,code:r,name:s}})}function Se(t){return ze(t.map((([t,n])=>new e(t).fill(n,0,t))))}function ze(t){return t.reduce(((t,n)=>t.concat(e.isArray(n)?ze(n):n)),[])}addEventListener("message",(e=>{const t=e.data,{type:n,messageId:r,data:i,done:o}=t;try{if("start"==n&&ge(t),"data"==n){const e=ye.get(r);ye.delete(r),e({value:new s(i),done:o})}if("ack"==n){const e=me.get(r);me.delete(r),e()}}catch(e){ve(e)}}));const _e=[0,1,2,3].concat(...Se([[2,4],[2,5],[4,6],[4,7],[8,8],[8,9],[16,10],[16,11],[32,12],[32,13],[64,14],[64,15],[2,0],[1,16],[1,17],[2,18],[2,19],[4,20],[4,21],[8,22],[8,23],[16,24],[16,25],[32,26],[32,27],[64,28],[64,29]]));function Ce(){const e=this;function t(e,t){let n=0;do{n|=1&e,e>>>=1,n<<=1}while(--t>0);return n>>>1}e.oe=r=>{const s=e.ce,i=e.ae.fe,o=e.ae.le;let c,f,a,l=-1;for(r.ue=0,r.we=573,c=0;o>c;c++)0!==s[2*c]?(r.he[++r.ue]=l=c,r.de[c]=0):s[2*c+1]=0;for(;2>r.ue;)a=r.he[++r.ue]=2>l?++l:0,s[2*a]=1,r.de[a]=0,r.pe--,i&&(r.ye-=i[2*a+1]);for(e.me=l,c=n.floor(r.ue/2);c>=1;c--)r.be(s,c);a=o;do{c=r.he[1],r.he[1]=r.he[r.ue--],r.be(s,1),f=r.he[1],r.he[--r.we]=c,r.he[--r.we]=f,s[2*a]=s[2*c]+s[2*f],r.de[a]=n.max(r.de[c],r.de[f])+1,s[2*c+1]=s[2*f+1]=a,r.he[1]=a++,r.be(s,1)}while(r.ue>=2);r.he[--r.we]=r.he[1],(t=>{const n=e.ce,r=e.ae.fe,s=e.ae.ge,i=e.ae.ke,o=e.ae.ve;let c,f,a,l,u,w,h=0;for(l=0;15>=l;l++)t.Se[l]=0;for(n[2*t.he[t.we]+1]=0,c=t.we+1;573>c;c++)f=t.he[c],l=n[2*n[2*f+1]+1]+1,l>o&&(l=o,h++),n[2*f+1]=l,f>e.me||(t.Se[l]++,u=0,i>f||(u=s[f-i]),w=n[2*f],t.pe+=w*(l+u),r&&(t.ye+=w*(r[2*f+1]+u)));if(0!==h){do{for(l=o-1;0===t.Se[l];)l--;t.Se[l]--,t.Se[l+1]+=2,t.Se[o]--,h-=2}while(h>0);for(l=o;0!==l;l--)for(f=t.Se[l];0!==f;)a=t.he[--c],a>e.me||(n[2*a+1]!=l&&(t.pe+=(l-n[2*a+1])*n[2*a],n[2*a+1]=l),f--)}})(r),((e,n,r)=>{const s=[];let i,o,c,f=0;for(i=1;15>=i;i++)s[i]=f=f+r[i-1]<<1;for(o=0;n>=o;o++)c=e[2*o+1],0!==c&&(e[2*o]=t(s[c]++,c))})(s,e.me,r.Se)}}function Ie(e,t,n,r,s){const i=this;i.fe=e,i.ge=t,i.ke=n,i.le=r,i.ve=s}Ce.ze=[0,1,2,3,4,5,6,7].concat(...Se([[2,8],[2,9],[2,10],[2,11],[4,12],[4,13],[4,14],[4,15],[8,16],[8,17],[8,18],[8,19],[16,20],[16,21],[16,22],[16,23],[32,24],[32,25],[32,26],[31,27],[1,28]])),Ce._e=[0,1,2,3,4,5,6,7,8,10,12,14,16,20,24,28,32,40,48,56,64,80,96,112,128,160,192,224,0],Ce.Ce=[0,1,2,3,4,6,8,12,16,24,32,48,64,96,128,192,256,384,512,768,1024,1536,2048,3072,4096,6144,8192,12288,16384,24576],Ce.Ie=e=>256>e?_e[e]:_e[256+(e>>>7)],Ce.xe=[0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0],Ce.Ae=[0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13],Ce.De=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7],Ce.Ve=[16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15];const xe=Se([[144,8],[112,9],[24,7],[8,8]]);Ie.Re=ze([12,140,76,204,44,172,108,236,28,156,92,220,60,188,124,252,2,130,66,194,34,162,98,226,18,146,82,210,50,178,114,242,10,138,74,202,42,170,106,234,26,154,90,218,58,186,122,250,6,134,70,198,38,166,102,230,22,150,86,214,54,182,118,246,14,142,78,206,46,174,110,238,30,158,94,222,62,190,126,254,1,129,65,193,33,161,97,225,17,145,81,209,49,177,113,241,9,137,73,201,41,169,105,233,25,153,89,217,57,185,121,249,5,133,69,197,37,165,101,229,21,149,85,213,53,181,117,245,13,141,77,205,45,173,109,237,29,157,93,221,61,189,125,253,19,275,147,403,83,339,211,467,51,307,179,435,115,371,243,499,11,267,139,395,75,331,203,459,43,299,171,427,107,363,235,491,27,283,155,411,91,347,219,475,59,315,187,443,123,379,251,507,7,263,135,391,71,327,199,455,39,295,167,423,103,359,231,487,23,279,151,407,87,343,215,471,55,311,183,439,119,375,247,503,15,271,143,399,79,335,207,463,47,303,175,431,111,367,239,495,31,287,159,415,95,351,223,479,63,319,191,447,127,383,255,511,0,64,32,96,16,80,48,112,8,72,40,104,24,88,56,120,4,68,36,100,20,84,52,116,3,131,67,195,35,163,99,227].map(((e,t)=>[e,xe[t]])));const Ae=Se([[30,5]]);function De(e,t,n,r,s){const i=this;i.Be=e,i.Ee=t,i.Me=n,i.Ke=r,i.Ne=s}Ie.Pe=ze([0,16,8,24,4,20,12,28,2,18,10,26,6,22,14,30,1,17,9,25,5,21,13,29,3,19,11,27,7,23].map(((e,t)=>[e,Ae[t]]))),Ie.Ue=new Ie(Ie.Re,Ce.xe,257,286,15),Ie.Te=new Ie(Ie.Pe,Ce.Ae,0,30,15),Ie.We=new Ie(null,Ce.De,0,19,7);const Ve=[new De(0,0,0,0,0),new De(4,4,8,4,1),new De(4,5,16,8,1),new De(4,6,32,32,1),new De(4,4,16,16,2),new De(8,16,32,32,2),new De(8,16,128,128,2),new De(8,32,128,256,2),new De(32,128,258,1024,2),new De(32,258,258,4096,2)],Re=["need dictionary","stream end","","","stream error","data error","","buffer error","",""];function Be(e,t,n,r){const s=e[2*t],i=e[2*n];return i>s||s==i&&r[t]<=r[n]}function Ee(){const e=this;let t,r,o,c,f,a,l,u,w,h,d,p,y,m,b,g,k,v,S,z,_,C,I,x,A,D,V,R,B,E,M,K,N;const P=new Ce,U=new Ce,T=new Ce;let W,H,L,j,F,O;function q(){let t;for(t=0;286>t;t++)M[2*t]=0;for(t=0;30>t;t++)K[2*t]=0;for(t=0;19>t;t++)N[2*t]=0;M[512]=1,e.pe=e.ye=0,H=L=0}function G(e,t){let n,r=-1,s=e[1],i=0,o=7,c=4;0===s&&(o=138,c=3),e[2*(t+1)+1]=65535;for(let f=0;t>=f;f++)n=s,s=e[2*(f+1)+1],++i<o&&n==s||(c>i?N[2*n]+=i:0!==n?(n!=r&&N[2*n]++,N[32]++):i>10?N[36]++:N[34]++,i=0,r=n,0===s?(o=138,c=3):n==s?(o=6,c=3):(o=7,c=4))}function J(t){e.He[e.pending++]=t}function Q(e){J(255&e),J(e>>>8&255)}function X(e,t){let n;const r=t;O>16-r?(n=e,F|=n<<O&65535,Q(F),F=n>>>16-O,O+=r-16):(F|=e<<O&65535,O+=r)}function Y(e,t){const n=2*e;X(65535&t[n],65535&t[n+1])}function Z(e,t){let n,r,s=-1,i=e[1],o=0,c=7,f=4;for(0===i&&(c=138,f=3),n=0;t>=n;n++)if(r=i,i=e[2*(n+1)+1],++o>=c||r!=i){if(f>o)do{Y(r,N)}while(0!=--o);else 0!==r?(r!=s&&(Y(r,N),o--),Y(16,N),X(o-3,2)):o>10?(Y(18,N),X(o-11,7)):(Y(17,N),X(o-3,3));o=0,s=r,0===i?(c=138,f=3):r==i?(c=6,f=3):(c=7,f=4)}}function $(){16==O?(Q(F),F=0,O=0):8>O||(J(255&F),F>>>=8,O-=8)}function ee(t,r){let s,i,o;if(e.Le[H]=t,e.je[H]=255&r,H++,0===t?M[2*r]++:(L++,t--,M[2*(Ce.ze[r]+256+1)]++,K[2*Ce.Ie(t)]++),0==(8191&H)&&V>2){for(s=8*H,i=_-k,o=0;30>o;o++)s+=K[2*o]*(5+Ce.Ae[o]);if(s>>>=3,L<n.floor(H/2)&&s<n.floor(i/2))return!0}return H==W-1}function te(t,n){let r,s,i,o,c=0;if(0!==H)do{r=e.Le[c],s=e.je[c],c++,0===r?Y(s,t):(i=Ce.ze[s],Y(i+256+1,t),o=Ce.xe[i],0!==o&&(s-=Ce._e[i],X(s,o)),r--,i=Ce.Ie(r),Y(i,n),o=Ce.Ae[i],0!==o&&(r-=Ce.Ce[i],X(r,o)))}while(H>c);Y(256,t),j=t[513]}function ne(){O>8?Q(F):O>0&&J(255&F),F=0,O=0}function re(t,n,r){X(0+(r?1:0),3),((t,n)=>{ne(),j=8,Q(n),Q(~n),e.He.set(u.subarray(t,t+n),e.pending),e.pending+=n})(t,n)}function se(n){((t,n,r)=>{let s,i,o=0;V>0?(P.oe(e),U.oe(e),o=(()=>{let t;for(G(M,P.me),G(K,U.me),T.oe(e),t=18;t>=3&&0===N[2*Ce.Ve[t]+1];t--);return e.pe+=14+3*(t+1),t})(),s=e.pe+3+7>>>3,i=e.ye+3+7>>>3,i>s||(s=i)):s=i=n+5,n+4>s||-1==t?i==s?(X(2+(r?1:0),3),te(Ie.Re,Ie.Pe)):(X(4+(r?1:0),3),((e,t,n)=>{let r;for(X(e-257,5),X(t-1,5),X(n-4,4),r=0;n>r;r++)X(N[2*Ce.Ve[r]+1],3);Z(M,e-1),Z(K,t-1)})(P.me+1,U.me+1,o+1),te(M,K)):re(t,n,r),q(),r&&ne()})(0>k?-1:k,_-k,n),k=_,t.Fe()}function ie(){let e,n,r,s;do{if(s=w-I-_,0===s&&0===_&&0===I)s=f;else if(-1==s)s--;else if(_>=f+f-262){u.set(u.subarray(f,f+f),0),C-=f,_-=f,k-=f,e=y,r=e;do{n=65535&d[--r],d[r]=f>n?0:n-f}while(0!=--e);e=f,r=e;do{n=65535&h[--r],h[r]=f>n?0:n-f}while(0!=--e);s+=f}if(0===t.Oe)return;e=t.qe(u,_+I,s),I+=e,3>I||(p=255&u[_],p=(p<<g^255&u[_+1])&b)}while(262>I&&0!==t.Oe)}function oe(e){let t,n,r=A,s=_,i=x;const o=_>f-262?_-(f-262):0;let c=E;const a=l,w=_+258;let d=u[s+i-1],p=u[s+i];B>x||(r>>=2),c>I&&(c=I);do{if(t=e,u[t+i]==p&&u[t+i-1]==d&&u[t]==u[s]&&u[++t]==u[s+1]){s+=2,t++;do{}while(u[++s]==u[++t]&&u[++s]==u[++t]&&u[++s]==u[++t]&&u[++s]==u[++t]&&u[++s]==u[++t]&&u[++s]==u[++t]&&u[++s]==u[++t]&&u[++s]==u[++t]&&w>s);if(n=258-(w-s),s=w-258,n>i){if(C=e,i=n,n>=c)break;d=u[s+i-1],p=u[s+i]}}}while((e=65535&h[e&a])>o&&0!=--r);return i>I?I:i}e.de=[],e.Se=[],e.he=[],M=[],K=[],N=[],e.be=(t,n)=>{const r=e.he,s=r[n];let i=n<<1;for(;i<=e.ue&&(i<e.ue&&Be(t,r[i+1],r[i],e.de)&&i++,!Be(t,s,r[i],e.de));)r[n]=r[i],n=i,i<<=1;r[n]=s},e.Ge=(t,S,C,H,L,G)=>(H||(H=8),L||(L=8),G||(G=0),t.Je=null,-1==S&&(S=6),1>L||L>9||8!=H||9>C||C>15||0>S||S>9||0>G||G>2?-2:(t.Qe=e,a=C,f=1<<a,l=f-1,m=L+7,y=1<<m,b=y-1,g=n.floor((m+3-1)/3),u=new s(2*f),h=[],d=[],W=1<<L+6,e.He=new s(4*W),o=4*W,e.Le=new i(W),e.je=new s(W),V=S,R=G,(t=>(t.Xe=t.Ye=0,t.Je=null,e.pending=0,e.Ze=0,r=113,c=0,P.ce=M,P.ae=Ie.Ue,U.ce=K,U.ae=Ie.Te,T.ce=N,T.ae=Ie.We,F=0,O=0,j=8,q(),(()=>{w=2*f,d[y-1]=0;for(let e=0;y-1>e;e++)d[e]=0;D=Ve[V].Ee,B=Ve[V].Be,E=Ve[V].Me,A=Ve[V].Ke,_=0,k=0,I=0,v=x=2,z=0,p=0})(),0))(t))),e.$e=()=>42!=r&&113!=r&&666!=r?-2:(e.je=null,e.Le=null,e.He=null,d=null,h=null,u=null,e.Qe=null,113==r?-3:0),e.et=(e,t,n)=>{let r=0;return-1==t&&(t=6),0>t||t>9||0>n||n>2?-2:(Ve[V].Ne!=Ve[t].Ne&&0!==e.Xe&&(r=e.tt(1)),V!=t&&(V=t,D=Ve[V].Ee,B=Ve[V].Be,E=Ve[V].Me,A=Ve[V].Ke),R=n,r)},e.nt=(e,t,n)=>{let s,i=n,o=0;if(!t||42!=r)return-2;if(3>i)return 0;for(i>f-262&&(i=f-262,o=n-i),u.set(t.subarray(o,o+i),0),_=i,k=i,p=255&u[0],p=(p<<g^255&u[1])&b,s=0;i-3>=s;s++)p=(p<<g^255&u[s+2])&b,h[s&l]=d[p],d[p]=s;return 0},e.tt=(n,s)=>{let i,w,m,A,B;if(s>4||0>s)return-2;if(!n.rt||!n.st&&0!==n.Oe||666==r&&4!=s)return n.Je=Re[4],-2;if(0===n.it)return n.Je=Re[7],-5;var E;if(t=n,A=c,c=s,42==r&&(w=8+(a-8<<4)<<8,m=(V-1&255)>>1,m>3&&(m=3),w|=m<<6,0!==_&&(w|=32),w+=31-w%31,r=113,J((E=w)>>8&255),J(255&E)),0!==e.pending){if(t.Fe(),0===t.it)return c=-1,0}else if(0===t.Oe&&A>=s&&4!=s)return t.Je=Re[7],-5;if(666==r&&0!==t.Oe)return n.Je=Re[7],-5;if(0!==t.Oe||0!==I||0!=s&&666!=r){switch(B=-1,Ve[V].Ne){case 0:B=(e=>{let n,r=65535;for(r>o-5&&(r=o-5);;){if(1>=I){if(ie(),0===I&&0==e)return 0;if(0===I)break}if(_+=I,I=0,n=k+r,(0===_||_>=n)&&(I=_-n,_=n,se(!1),0===t.it))return 0;if(_-k>=f-262&&(se(!1),0===t.it))return 0}return se(4==e),0===t.it?4==e?2:0:4==e?3:1})(s);break;case 1:B=(e=>{let n,r=0;for(;;){if(262>I){if(ie(),262>I&&0==e)return 0;if(0===I)break}if(3>I||(p=(p<<g^255&u[_+2])&b,r=65535&d[p],h[_&l]=d[p],d[p]=_),0===r||(_-r&65535)>f-262||2!=R&&(v=oe(r)),3>v)n=ee(0,255&u[_]),I--,_++;else if(n=ee(_-C,v-3),I-=v,v>D||3>I)_+=v,v=0,p=255&u[_],p=(p<<g^255&u[_+1])&b;else{v--;do{_++,p=(p<<g^255&u[_+2])&b,r=65535&d[p],h[_&l]=d[p],d[p]=_}while(0!=--v);_++}if(n&&(se(!1),0===t.it))return 0}return se(4==e),0===t.it?4==e?2:0:4==e?3:1})(s);break;case 2:B=(e=>{let n,r,s=0;for(;;){if(262>I){if(ie(),262>I&&0==e)return 0;if(0===I)break}if(3>I||(p=(p<<g^255&u[_+2])&b,s=65535&d[p],h[_&l]=d[p],d[p]=_),x=v,S=C,v=2,0!==s&&D>x&&f-262>=(_-s&65535)&&(2!=R&&(v=oe(s)),5>=v&&(1==R||3==v&&_-C>4096)&&(v=2)),3>x||v>x)if(0!==z){if(n=ee(0,255&u[_-1]),n&&se(!1),_++,I--,0===t.it)return 0}else z=1,_++,I--;else{r=_+I-3,n=ee(_-1-S,x-3),I-=x-1,x-=2;do{++_>r||(p=(p<<g^255&u[_+2])&b,s=65535&d[p],h[_&l]=d[p],d[p]=_)}while(0!=--x);if(z=0,v=2,_++,n&&(se(!1),0===t.it))return 0}}return 0!==z&&(n=ee(0,255&u[_-1]),z=0),se(4==e),0===t.it?4==e?2:0:4==e?3:1})(s)}if(2!=B&&3!=B||(r=666),0==B||2==B)return 0===t.it&&(c=-1),0;if(1==B){if(1==s)X(2,3),Y(256,Ie.Re),$(),9>1+j+10-O&&(X(2,3),Y(256,Ie.Re),$()),j=7;else if(re(0,0,!1),3==s)for(i=0;y>i;i++)d[i]=0;if(t.Fe(),0===t.it)return c=-1,0}}return 4!=s?0:1}}function Me(){const e=this;e.ot=0,e.ct=0,e.Oe=0,e.Xe=0,e.it=0,e.Ye=0}function Ke(e){const t=new Me,i=(o=e&&e.chunkSize?e.chunkSize:65536)+5*(n.floor(o/16383)+1);var o;const c=new s(i);let f=e?e.level:-1;void 0===f&&(f=-1),t.Ge(f),t.rt=c,this.append=(e,n)=>{let o,f,a=0,l=0,u=0;const w=[];if(e.length){t.ot=0,t.st=e,t.Oe=e.length;do{if(t.ct=0,t.it=i,o=t.tt(0),0!=o)throw new r("deflating: "+t.Je);t.ct&&(t.ct==i?w.push(new s(c)):w.push(c.slice(0,t.ct))),u+=t.ct,n&&t.ot>0&&t.ot!=a&&(n(t.ot),a=t.ot)}while(t.Oe>0||0===t.it);return w.length>1?(f=new s(u),w.forEach((e=>{f.set(e,l),l+=e.length}))):f=w[0]||new s,f}},this.flush=()=>{let e,n,o=0,f=0;const a=[];do{if(t.ct=0,t.it=i,e=t.tt(4),1!=e&&0!=e)throw new r("deflating: "+t.Je);i-t.it>0&&a.push(c.slice(0,t.ct)),f+=t.ct}while(t.Oe>0||0===t.it);return t.$e(),n=new s(f),a.forEach((e=>{n.set(e,o),o+=e.length})),n}}Me.prototype={Ge(e,t){const n=this;return n.Qe=new Ee,t||(t=15),n.Qe.Ge(n,e,t)},tt(e){const t=this;return t.Qe?t.Qe.tt(t,e):-2},$e(){const e=this;if(!e.Qe)return-2;const t=e.Qe.$e();return e.Qe=null,t},et(e,t){const n=this;return n.Qe?n.Qe.et(n,e,t):-2},nt(e,t){const n=this;return n.Qe?n.Qe.nt(n,e,t):-2},qe(e,t,n){const r=this;let s=r.Oe;return s>n&&(s=n),0===s?0:(r.Oe-=s,e.set(r.st.subarray(r.ot,r.ot+s),t),r.ot+=s,r.Xe+=s,s)},Fe(){const e=this;let t=e.Qe.pending;t>e.it&&(t=e.it),0!==t&&(e.rt.set(e.Qe.He.subarray(e.Qe.Ze,e.Qe.Ze+t),e.ct),e.ct+=t,e.Qe.Ze+=t,e.Ye+=t,e.it-=t,e.Qe.pending-=t,0===e.Qe.pending&&(e.Qe.Ze=0))}};const Ne=[0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535],Pe=[96,7,256,0,8,80,0,8,16,84,8,115,82,7,31,0,8,112,0,8,48,0,9,192,80,7,10,0,8,96,0,8,32,0,9,160,0,8,0,0,8,128,0,8,64,0,9,224,80,7,6,0,8,88,0,8,24,0,9,144,83,7,59,0,8,120,0,8,56,0,9,208,81,7,17,0,8,104,0,8,40,0,9,176,0,8,8,0,8,136,0,8,72,0,9,240,80,7,4,0,8,84,0,8,20,85,8,227,83,7,43,0,8,116,0,8,52,0,9,200,81,7,13,0,8,100,0,8,36,0,9,168,0,8,4,0,8,132,0,8,68,0,9,232,80,7,8,0,8,92,0,8,28,0,9,152,84,7,83,0,8,124,0,8,60,0,9,216,82,7,23,0,8,108,0,8,44,0,9,184,0,8,12,0,8,140,0,8,76,0,9,248,80,7,3,0,8,82,0,8,18,85,8,163,83,7,35,0,8,114,0,8,50,0,9,196,81,7,11,0,8,98,0,8,34,0,9,164,0,8,2,0,8,130,0,8,66,0,9,228,80,7,7,0,8,90,0,8,26,0,9,148,84,7,67,0,8,122,0,8,58,0,9,212,82,7,19,0,8,106,0,8,42,0,9,180,0,8,10,0,8,138,0,8,74,0,9,244,80,7,5,0,8,86,0,8,22,192,8,0,83,7,51,0,8,118,0,8,54,0,9,204,81,7,15,0,8,102,0,8,38,0,9,172,0,8,6,0,8,134,0,8,70,0,9,236,80,7,9,0,8,94,0,8,30,0,9,156,84,7,99,0,8,126,0,8,62,0,9,220,82,7,27,0,8,110,0,8,46,0,9,188,0,8,14,0,8,142,0,8,78,0,9,252,96,7,256,0,8,81,0,8,17,85,8,131,82,7,31,0,8,113,0,8,49,0,9,194,80,7,10,0,8,97,0,8,33,0,9,162,0,8,1,0,8,129,0,8,65,0,9,226,80,7,6,0,8,89,0,8,25,0,9,146,83,7,59,0,8,121,0,8,57,0,9,210,81,7,17,0,8,105,0,8,41,0,9,178,0,8,9,0,8,137,0,8,73,0,9,242,80,7,4,0,8,85,0,8,21,80,8,258,83,7,43,0,8,117,0,8,53,0,9,202,81,7,13,0,8,101,0,8,37,0,9,170,0,8,5,0,8,133,0,8,69,0,9,234,80,7,8,0,8,93,0,8,29,0,9,154,84,7,83,0,8,125,0,8,61,0,9,218,82,7,23,0,8,109,0,8,45,0,9,186,0,8,13,0,8,141,0,8,77,0,9,250,80,7,3,0,8,83,0,8,19,85,8,195,83,7,35,0,8,115,0,8,51,0,9,198,81,7,11,0,8,99,0,8,35,0,9,166,0,8,3,0,8,131,0,8,67,0,9,230,80,7,7,0,8,91,0,8,27,0,9,150,84,7,67,0,8,123,0,8,59,0,9,214,82,7,19,0,8,107,0,8,43,0,9,182,0,8,11,0,8,139,0,8,75,0,9,246,80,7,5,0,8,87,0,8,23,192,8,0,83,7,51,0,8,119,0,8,55,0,9,206,81,7,15,0,8,103,0,8,39,0,9,174,0,8,7,0,8,135,0,8,71,0,9,238,80,7,9,0,8,95,0,8,31,0,9,158,84,7,99,0,8,127,0,8,63,0,9,222,82,7,27,0,8,111,0,8,47,0,9,190,0,8,15,0,8,143,0,8,79,0,9,254,96,7,256,0,8,80,0,8,16,84,8,115,82,7,31,0,8,112,0,8,48,0,9,193,80,7,10,0,8,96,0,8,32,0,9,161,0,8,0,0,8,128,0,8,64,0,9,225,80,7,6,0,8,88,0,8,24,0,9,145,83,7,59,0,8,120,0,8,56,0,9,209,81,7,17,0,8,104,0,8,40,0,9,177,0,8,8,0,8,136,0,8,72,0,9,241,80,7,4,0,8,84,0,8,20,85,8,227,83,7,43,0,8,116,0,8,52,0,9,201,81,7,13,0,8,100,0,8,36,0,9,169,0,8,4,0,8,132,0,8,68,0,9,233,80,7,8,0,8,92,0,8,28,0,9,153,84,7,83,0,8,124,0,8,60,0,9,217,82,7,23,0,8,108,0,8,44,0,9,185,0,8,12,0,8,140,0,8,76,0,9,249,80,7,3,0,8,82,0,8,18,85,8,163,83,7,35,0,8,114,0,8,50,0,9,197,81,7,11,0,8,98,0,8,34,0,9,165,0,8,2,0,8,130,0,8,66,0,9,229,80,7,7,0,8,90,0,8,26,0,9,149,84,7,67,0,8,122,0,8,58,0,9,213,82,7,19,0,8,106,0,8,42,0,9,181,0,8,10,0,8,138,0,8,74,0,9,245,80,7,5,0,8,86,0,8,22,192,8,0,83,7,51,0,8,118,0,8,54,0,9,205,81,7,15,0,8,102,0,8,38,0,9,173,0,8,6,0,8,134,0,8,70,0,9,237,80,7,9,0,8,94,0,8,30,0,9,157,84,7,99,0,8,126,0,8,62,0,9,221,82,7,27,0,8,110,0,8,46,0,9,189,0,8,14,0,8,142,0,8,78,0,9,253,96,7,256,0,8,81,0,8,17,85,8,131,82,7,31,0,8,113,0,8,49,0,9,195,80,7,10,0,8,97,0,8,33,0,9,163,0,8,1,0,8,129,0,8,65,0,9,227,80,7,6,0,8,89,0,8,25,0,9,147,83,7,59,0,8,121,0,8,57,0,9,211,81,7,17,0,8,105,0,8,41,0,9,179,0,8,9,0,8,137,0,8,73,0,9,243,80,7,4,0,8,85,0,8,21,80,8,258,83,7,43,0,8,117,0,8,53,0,9,203,81,7,13,0,8,101,0,8,37,0,9,171,0,8,5,0,8,133,0,8,69,0,9,235,80,7,8,0,8,93,0,8,29,0,9,155,84,7,83,0,8,125,0,8,61,0,9,219,82,7,23,0,8,109,0,8,45,0,9,187,0,8,13,0,8,141,0,8,77,0,9,251,80,7,3,0,8,83,0,8,19,85,8,195,83,7,35,0,8,115,0,8,51,0,9,199,81,7,11,0,8,99,0,8,35,0,9,167,0,8,3,0,8,131,0,8,67,0,9,231,80,7,7,0,8,91,0,8,27,0,9,151,84,7,67,0,8,123,0,8,59,0,9,215,82,7,19,0,8,107,0,8,43,0,9,183,0,8,11,0,8,139,0,8,75,0,9,247,80,7,5,0,8,87,0,8,23,192,8,0,83,7,51,0,8,119,0,8,55,0,9,207,81,7,15,0,8,103,0,8,39,0,9,175,0,8,7,0,8,135,0,8,71,0,9,239,80,7,9,0,8,95,0,8,31,0,9,159,84,7,99,0,8,127,0,8,63,0,9,223,82,7,27,0,8,111,0,8,47,0,9,191,0,8,15,0,8,143,0,8,79,0,9,255],Ue=[80,5,1,87,5,257,83,5,17,91,5,4097,81,5,5,89,5,1025,85,5,65,93,5,16385,80,5,3,88,5,513,84,5,33,92,5,8193,82,5,9,90,5,2049,86,5,129,192,5,24577,80,5,2,87,5,385,83,5,25,91,5,6145,81,5,7,89,5,1537,85,5,97,93,5,24577,80,5,4,88,5,769,84,5,49,92,5,12289,82,5,13,90,5,3073,86,5,193,192,5,24577],Te=[3,4,5,6,7,8,9,10,11,13,15,17,19,23,27,31,35,43,51,59,67,83,99,115,131,163,195,227,258,0,0],We=[0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,112,112],He=[1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577],Le=[0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13];function je(){let e,t,n,r,s,i;function o(e,t,o,c,f,a,l,u,w,h,d){let p,y,m,b,g,k,v,S,z,_,C,I,x,A,D;_=0,g=o;do{n[e[t+_]]++,_++,g--}while(0!==g);if(n[0]==o)return l[0]=-1,u[0]=0,0;for(S=u[0],k=1;15>=k&&0===n[k];k++);for(v=k,k>S&&(S=k),g=15;0!==g&&0===n[g];g--);for(m=g,S>g&&(S=g),u[0]=S,A=1<<k;g>k;k++,A<<=1)if(0>(A-=n[k]))return-3;if(0>(A-=n[g]))return-3;for(n[g]+=A,i[1]=k=0,_=1,x=2;0!=--g;)i[x]=k+=n[_],x++,_++;g=0,_=0;do{0!==(k=e[t+_])&&(d[i[k]++]=g),_++}while(++g<o);for(o=i[m],i[0]=g=0,_=0,b=-1,I=-S,s[0]=0,C=0,D=0;m>=v;v++)for(p=n[v];0!=p--;){for(;v>I+S;){if(b++,I+=S,D=m-I,D=D>S?S:D,(y=1<<(k=v-I))>p+1&&(y-=p+1,x=v,D>k))for(;++k<D&&(y<<=1)>n[++x];)y-=n[x];if(D=1<<k,h[0]+D>1440)return-3;s[b]=C=h[0],h[0]+=D,0!==b?(i[b]=g,r[0]=k,r[1]=S,k=g>>>I-S,r[2]=C-s[b-1]-k,w.set(r,3*(s[b-1]+k))):l[0]=C}for(r[1]=v-I,o>_?d[_]<c?(r[0]=256>d[_]?0:96,r[2]=d[_++]):(r[0]=a[d[_]-c]+16+64,r[2]=f[d[_++]-c]):r[0]=192,y=1<<v-I,k=g>>>I;D>k;k+=y)w.set(r,3*(C+k));for(k=1<<v-1;0!=(g&k);k>>>=1)g^=k;for(g^=k,z=(1<<I)-1;(g&z)!=i[b];)b--,I-=S,z=(1<<I)-1}return 0!==A&&1!=m?-5:0}function f(o){let f;for(e||(e=[],t=[],n=new c(16),r=[],s=new c(15),i=new c(16)),t.length<o&&(t=[]),f=0;o>f;f++)t[f]=0;for(f=0;16>f;f++)n[f]=0;for(f=0;3>f;f++)r[f]=0;s.set(n.subarray(0,15),0),i.set(n.subarray(0,16),0)}this.ft=(n,r,s,i,c)=>{let a;return f(19),e[0]=0,a=o(n,0,19,19,null,null,s,r,i,e,t),-3==a?c.Je="oversubscribed dynamic bit lengths tree":-5!=a&&0!==r[0]||(c.Je="incomplete dynamic bit lengths tree",a=-3),a},this.lt=(n,r,s,i,c,a,l,u,w)=>{let h;return f(288),e[0]=0,h=o(s,0,n,257,Te,We,a,i,u,e,t),0!=h||0===i[0]?(-3==h?w.Je="oversubscribed literal/length tree":-4!=h&&(w.Je="incomplete literal/length tree",h=-3),h):(f(288),h=o(s,n,r,0,He,Le,l,c,u,e,t),0!=h||0===c[0]&&n>257?(-3==h?w.Je="oversubscribed distance tree":-5==h?(w.Je="incomplete distance tree",h=-3):-4!=h&&(w.Je="empty distance tree with lengths",h=-3),h):0)}}function Fe(){const e=this;let t,n,r,s,i=0,o=0,c=0,f=0,a=0,l=0,u=0,w=0,h=0,d=0;function p(e,t,n,r,s,i,o,c){let f,a,l,u,w,h,d,p,y,m,b,g,k,v,S,z;d=c.ot,p=c.Oe,w=o.ut,h=o.wt,y=o.write,m=y<o.read?o.read-y-1:o.end-y,b=Ne[e],g=Ne[t];do{for(;20>h;)p--,w|=(255&c.ht(d++))<<h,h+=8;if(f=w&b,a=n,l=r,z=3*(l+f),0!==(u=a[z]))for(;;){if(w>>=a[z+1],h-=a[z+1],0!=(16&u)){for(u&=15,k=a[z+2]+(w&Ne[u]),w>>=u,h-=u;15>h;)p--,w|=(255&c.ht(d++))<<h,h+=8;for(f=w&g,a=s,l=i,z=3*(l+f),u=a[z];;){if(w>>=a[z+1],h-=a[z+1],0!=(16&u)){for(u&=15;u>h;)p--,w|=(255&c.ht(d++))<<h,h+=8;if(v=a[z+2]+(w&Ne[u]),w>>=u,h-=u,m-=k,v>y){S=y-v;do{S+=o.end}while(0>S);if(u=o.end-S,k>u){if(k-=u,y-S>0&&u>y-S)do{o.dt[y++]=o.dt[S++]}while(0!=--u);else o.dt.set(o.dt.subarray(S,S+u),y),y+=u,S+=u,u=0;S=0}}else S=y-v,y-S>0&&2>y-S?(o.dt[y++]=o.dt[S++],o.dt[y++]=o.dt[S++],k-=2):(o.dt.set(o.dt.subarray(S,S+2),y),y+=2,S+=2,k-=2);if(y-S>0&&k>y-S)do{o.dt[y++]=o.dt[S++]}while(0!=--k);else o.dt.set(o.dt.subarray(S,S+k),y),y+=k,S+=k,k=0;break}if(0!=(64&u))return c.Je="invalid distance code",k=c.Oe-p,k=k>h>>3?h>>3:k,p+=k,d-=k,h-=k<<3,o.ut=w,o.wt=h,c.Oe=p,c.Xe+=d-c.ot,c.ot=d,o.write=y,-3;f+=a[z+2],f+=w&Ne[u],z=3*(l+f),u=a[z]}break}if(0!=(64&u))return 0!=(32&u)?(k=c.Oe-p,k=k>h>>3?h>>3:k,p+=k,d-=k,h-=k<<3,o.ut=w,o.wt=h,c.Oe=p,c.Xe+=d-c.ot,c.ot=d,o.write=y,1):(c.Je="invalid literal/length code",k=c.Oe-p,k=k>h>>3?h>>3:k,p+=k,d-=k,h-=k<<3,o.ut=w,o.wt=h,c.Oe=p,c.Xe+=d-c.ot,c.ot=d,o.write=y,-3);if(f+=a[z+2],f+=w&Ne[u],z=3*(l+f),0===(u=a[z])){w>>=a[z+1],h-=a[z+1],o.dt[y++]=a[z+2],m--;break}}else w>>=a[z+1],h-=a[z+1],o.dt[y++]=a[z+2],m--}while(m>=258&&p>=10);return k=c.Oe-p,k=k>h>>3?h>>3:k,p+=k,d-=k,h-=k<<3,o.ut=w,o.wt=h,c.Oe=p,c.Xe+=d-c.ot,c.ot=d,o.write=y,0}e.init=(e,i,o,c,f,a)=>{t=0,u=e,w=i,r=o,h=c,s=f,d=a,n=null},e.yt=(e,y,m)=>{let b,g,k,v,S,z,_,C=0,I=0,x=0;for(x=y.ot,v=y.Oe,C=e.ut,I=e.wt,S=e.write,z=S<e.read?e.read-S-1:e.end-S;;)switch(t){case 0:if(z>=258&&v>=10&&(e.ut=C,e.wt=I,y.Oe=v,y.Xe+=x-y.ot,y.ot=x,e.write=S,m=p(u,w,r,h,s,d,e,y),x=y.ot,v=y.Oe,C=e.ut,I=e.wt,S=e.write,z=S<e.read?e.read-S-1:e.end-S,0!=m)){t=1==m?7:9;break}c=u,n=r,o=h,t=1;case 1:for(b=c;b>I;){if(0===v)return e.ut=C,e.wt=I,y.Oe=v,y.Xe+=x-y.ot,y.ot=x,e.write=S,e.bt(y,m);m=0,v--,C|=(255&y.ht(x++))<<I,I+=8}if(g=3*(o+(C&Ne[b])),C>>>=n[g+1],I-=n[g+1],k=n[g],0===k){f=n[g+2],t=6;break}if(0!=(16&k)){a=15&k,i=n[g+2],t=2;break}if(0==(64&k)){c=k,o=g/3+n[g+2];break}if(0!=(32&k)){t=7;break}return t=9,y.Je="invalid literal/length code",m=-3,e.ut=C,e.wt=I,y.Oe=v,y.Xe+=x-y.ot,y.ot=x,e.write=S,e.bt(y,m);case 2:for(b=a;b>I;){if(0===v)return e.ut=C,e.wt=I,y.Oe=v,y.Xe+=x-y.ot,y.ot=x,e.write=S,e.bt(y,m);m=0,v--,C|=(255&y.ht(x++))<<I,I+=8}i+=C&Ne[b],C>>=b,I-=b,c=w,n=s,o=d,t=3;case 3:for(b=c;b>I;){if(0===v)return e.ut=C,e.wt=I,y.Oe=v,y.Xe+=x-y.ot,y.ot=x,e.write=S,e.bt(y,m);m=0,v--,C|=(255&y.ht(x++))<<I,I+=8}if(g=3*(o+(C&Ne[b])),C>>=n[g+1],I-=n[g+1],k=n[g],0!=(16&k)){a=15&k,l=n[g+2],t=4;break}if(0==(64&k)){c=k,o=g/3+n[g+2];break}return t=9,y.Je="invalid distance code",m=-3,e.ut=C,e.wt=I,y.Oe=v,y.Xe+=x-y.ot,y.ot=x,e.write=S,e.bt(y,m);case 4:for(b=a;b>I;){if(0===v)return e.ut=C,e.wt=I,y.Oe=v,y.Xe+=x-y.ot,y.ot=x,e.write=S,e.bt(y,m);m=0,v--,C|=(255&y.ht(x++))<<I,I+=8}l+=C&Ne[b],C>>=b,I-=b,t=5;case 5:for(_=S-l;0>_;)_+=e.end;for(;0!==i;){if(0===z&&(S==e.end&&0!==e.read&&(S=0,z=S<e.read?e.read-S-1:e.end-S),0===z&&(e.write=S,m=e.bt(y,m),S=e.write,z=S<e.read?e.read-S-1:e.end-S,S==e.end&&0!==e.read&&(S=0,z=S<e.read?e.read-S-1:e.end-S),0===z)))return e.ut=C,e.wt=I,y.Oe=v,y.Xe+=x-y.ot,y.ot=x,e.write=S,e.bt(y,m);e.dt[S++]=e.dt[_++],z--,_==e.end&&(_=0),i--}t=0;break;case 6:if(0===z&&(S==e.end&&0!==e.read&&(S=0,z=S<e.read?e.read-S-1:e.end-S),0===z&&(e.write=S,m=e.bt(y,m),S=e.write,z=S<e.read?e.read-S-1:e.end-S,S==e.end&&0!==e.read&&(S=0,z=S<e.read?e.read-S-1:e.end-S),0===z)))return e.ut=C,e.wt=I,y.Oe=v,y.Xe+=x-y.ot,y.ot=x,e.write=S,e.bt(y,m);m=0,e.dt[S++]=f,z--,t=0;break;case 7:if(I>7&&(I-=8,v++,x--),e.write=S,m=e.bt(y,m),S=e.write,z=S<e.read?e.read-S-1:e.end-S,e.read!=e.write)return e.ut=C,e.wt=I,y.Oe=v,y.Xe+=x-y.ot,y.ot=x,e.write=S,e.bt(y,m);t=8;case 8:return m=1,e.ut=C,e.wt=I,y.Oe=v,y.Xe+=x-y.ot,y.ot=x,e.write=S,e.bt(y,m);case 9:return m=-3,e.ut=C,e.wt=I,y.Oe=v,y.Xe+=x-y.ot,y.ot=x,e.write=S,e.bt(y,m);default:return m=-2,e.ut=C,e.wt=I,y.Oe=v,y.Xe+=x-y.ot,y.ot=x,e.write=S,e.bt(y,m)}},e.gt=()=>{}}je.kt=(e,t,n,r)=>(e[0]=9,t[0]=5,n[0]=Pe,r[0]=Ue,0);const Oe=[16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15];function qe(e,t){const n=this;let r,i=0,o=0,f=0,a=0;const l=[0],u=[0],w=new Fe;let h=0,d=new c(4320);const p=new je;n.wt=0,n.ut=0,n.dt=new s(t),n.end=t,n.read=0,n.write=0,n.reset=(e,t)=>{t&&(t[0]=0),6==i&&w.gt(e),i=0,n.wt=0,n.ut=0,n.read=n.write=0},n.reset(e,null),n.bt=(e,t)=>{let r,s,i;return s=e.ct,i=n.read,r=(i>n.write?n.end:n.write)-i,r>e.it&&(r=e.it),0!==r&&-5==t&&(t=0),e.it-=r,e.Ye+=r,e.rt.set(n.dt.subarray(i,i+r),s),s+=r,i+=r,i==n.end&&(i=0,n.write==n.end&&(n.write=0),r=n.write-i,r>e.it&&(r=e.it),0!==r&&-5==t&&(t=0),e.it-=r,e.Ye+=r,e.rt.set(n.dt.subarray(i,i+r),s),s+=r,i+=r),e.ct=s,n.read=i,t},n.yt=(e,t)=>{let s,c,y,m,b,g,k,v;for(m=e.ot,b=e.Oe,c=n.ut,y=n.wt,g=n.write,k=g<n.read?n.read-g-1:n.end-g;;){let S,z,_,C,I,x,A,D;switch(i){case 0:for(;3>y;){if(0===b)return n.ut=c,n.wt=y,e.Oe=b,e.Xe+=m-e.ot,e.ot=m,n.write=g,n.bt(e,t);t=0,b--,c|=(255&e.ht(m++))<<y,y+=8}switch(s=7&c,h=1&s,s>>>1){case 0:c>>>=3,y-=3,s=7&y,c>>>=s,y-=s,i=1;break;case 1:S=[],z=[],_=[[]],C=[[]],je.kt(S,z,_,C),w.init(S[0],z[0],_[0],0,C[0],0),c>>>=3,y-=3,i=6;break;case 2:c>>>=3,y-=3,i=3;break;case 3:return c>>>=3,y-=3,i=9,e.Je="invalid block type",t=-3,n.ut=c,n.wt=y,e.Oe=b,e.Xe+=m-e.ot,e.ot=m,n.write=g,n.bt(e,t)}break;case 1:for(;32>y;){if(0===b)return n.ut=c,n.wt=y,e.Oe=b,e.Xe+=m-e.ot,e.ot=m,n.write=g,n.bt(e,t);t=0,b--,c|=(255&e.ht(m++))<<y,y+=8}if((~c>>>16&65535)!=(65535&c))return i=9,e.Je="invalid stored block lengths",t=-3,n.ut=c,n.wt=y,e.Oe=b,e.Xe+=m-e.ot,e.ot=m,n.write=g,n.bt(e,t);o=65535&c,c=y=0,i=0!==o?2:0!==h?7:0;break;case 2:if(0===b)return n.ut=c,n.wt=y,e.Oe=b,e.Xe+=m-e.ot,e.ot=m,n.write=g,n.bt(e,t);if(0===k&&(g==n.end&&0!==n.read&&(g=0,k=g<n.read?n.read-g-1:n.end-g),0===k&&(n.write=g,t=n.bt(e,t),g=n.write,k=g<n.read?n.read-g-1:n.end-g,g==n.end&&0!==n.read&&(g=0,k=g<n.read?n.read-g-1:n.end-g),0===k)))return n.ut=c,n.wt=y,e.Oe=b,e.Xe+=m-e.ot,e.ot=m,n.write=g,n.bt(e,t);if(t=0,s=o,s>b&&(s=b),s>k&&(s=k),n.dt.set(e.qe(m,s),g),m+=s,b-=s,g+=s,k-=s,0!=(o-=s))break;i=0!==h?7:0;break;case 3:for(;14>y;){if(0===b)return n.ut=c,n.wt=y,e.Oe=b,e.Xe+=m-e.ot,e.ot=m,n.write=g,n.bt(e,t);t=0,b--,c|=(255&e.ht(m++))<<y,y+=8}if(f=s=16383&c,(31&s)>29||(s>>5&31)>29)return i=9,e.Je="too many length or distance symbols",t=-3,n.ut=c,n.wt=y,e.Oe=b,e.Xe+=m-e.ot,e.ot=m,n.write=g,n.bt(e,t);if(s=258+(31&s)+(s>>5&31),!r||r.length<s)r=[];else for(v=0;s>v;v++)r[v]=0;c>>>=14,y-=14,a=0,i=4;case 4:for(;4+(f>>>10)>a;){for(;3>y;){if(0===b)return n.ut=c,n.wt=y,e.Oe=b,e.Xe+=m-e.ot,e.ot=m,n.write=g,n.bt(e,t);t=0,b--,c|=(255&e.ht(m++))<<y,y+=8}r[Oe[a++]]=7&c,c>>>=3,y-=3}for(;19>a;)r[Oe[a++]]=0;if(l[0]=7,s=p.ft(r,l,u,d,e),0!=s)return-3==(t=s)&&(r=null,i=9),n.ut=c,n.wt=y,e.Oe=b,e.Xe+=m-e.ot,e.ot=m,n.write=g,n.bt(e,t);a=0,i=5;case 5:for(;s=f,258+(31&s)+(s>>5&31)>a;){let o,w;for(s=l[0];s>y;){if(0===b)return n.ut=c,n.wt=y,e.Oe=b,e.Xe+=m-e.ot,e.ot=m,n.write=g,n.bt(e,t);t=0,b--,c|=(255&e.ht(m++))<<y,y+=8}if(s=d[3*(u[0]+(c&Ne[s]))+1],w=d[3*(u[0]+(c&Ne[s]))+2],16>w)c>>>=s,y-=s,r[a++]=w;else{for(v=18==w?7:w-14,o=18==w?11:3;s+v>y;){if(0===b)return n.ut=c,n.wt=y,e.Oe=b,e.Xe+=m-e.ot,e.ot=m,n.write=g,n.bt(e,t);t=0,b--,c|=(255&e.ht(m++))<<y,y+=8}if(c>>>=s,y-=s,o+=c&Ne[v],c>>>=v,y-=v,v=a,s=f,v+o>258+(31&s)+(s>>5&31)||16==w&&1>v)return r=null,i=9,e.Je="invalid bit length repeat",t=-3,n.ut=c,n.wt=y,e.Oe=b,e.Xe+=m-e.ot,e.ot=m,n.write=g,n.bt(e,t);w=16==w?r[v-1]:0;do{r[v++]=w}while(0!=--o);a=v}}if(u[0]=-1,I=[],x=[],A=[],D=[],I[0]=9,x[0]=6,s=f,s=p.lt(257+(31&s),1+(s>>5&31),r,I,x,A,D,d,e),0!=s)return-3==s&&(r=null,i=9),t=s,n.ut=c,n.wt=y,e.Oe=b,e.Xe+=m-e.ot,e.ot=m,n.write=g,n.bt(e,t);w.init(I[0],x[0],d,A[0],d,D[0]),i=6;case 6:if(n.ut=c,n.wt=y,e.Oe=b,e.Xe+=m-e.ot,e.ot=m,n.write=g,1!=(t=w.yt(n,e,t)))return n.bt(e,t);if(t=0,w.gt(e),m=e.ot,b=e.Oe,c=n.ut,y=n.wt,g=n.write,k=g<n.read?n.read-g-1:n.end-g,0===h){i=0;break}i=7;case 7:if(n.write=g,t=n.bt(e,t),g=n.write,k=g<n.read?n.read-g-1:n.end-g,n.read!=n.write)return n.ut=c,n.wt=y,e.Oe=b,e.Xe+=m-e.ot,e.ot=m,n.write=g,n.bt(e,t);i=8;case 8:return t=1,n.ut=c,n.wt=y,e.Oe=b,e.Xe+=m-e.ot,e.ot=m,n.write=g,n.bt(e,t);case 9:return t=-3,n.ut=c,n.wt=y,e.Oe=b,e.Xe+=m-e.ot,e.ot=m,n.write=g,n.bt(e,t);default:return t=-2,n.ut=c,n.wt=y,e.Oe=b,e.Xe+=m-e.ot,e.ot=m,n.write=g,n.bt(e,t)}}},n.gt=e=>{n.reset(e,null),n.dt=null,d=null},n.vt=(e,t,r)=>{n.dt.set(e.subarray(t,t+r),0),n.read=n.write=r},n.St=()=>1==i?1:0}const Ge=[0,0,255,255];function Je(){const e=this;function t(e){return e&&e.zt?(e.Xe=e.Ye=0,e.Je=null,e.zt.mode=7,e.zt._t.reset(e,null),0):-2}e.mode=0,e.method=0,e.Ct=[0],e.It=0,e.marker=0,e.xt=0,e.At=t=>(e._t&&e._t.gt(t),e._t=null,0),e.Dt=(n,r)=>(n.Je=null,e._t=null,8>r||r>15?(e.At(n),-2):(e.xt=r,n.zt._t=new qe(n,1<<r),t(n),0)),e.Vt=(e,t)=>{let n,r;if(!e||!e.zt||!e.st)return-2;const s=e.zt;for(t=4==t?-5:0,n=-5;;)switch(s.mode){case 0:if(0===e.Oe)return n;if(n=t,e.Oe--,e.Xe++,8!=(15&(s.method=e.ht(e.ot++)))){s.mode=13,e.Je="unknown compression method",s.marker=5;break}if(8+(s.method>>4)>s.xt){s.mode=13,e.Je="invalid win size",s.marker=5;break}s.mode=1;case 1:if(0===e.Oe)return n;if(n=t,e.Oe--,e.Xe++,r=255&e.ht(e.ot++),((s.method<<8)+r)%31!=0){s.mode=13,e.Je="incorrect header check",s.marker=5;break}if(0==(32&r)){s.mode=7;break}s.mode=2;case 2:if(0===e.Oe)return n;n=t,e.Oe--,e.Xe++,s.It=(255&e.ht(e.ot++))<<24&4278190080,s.mode=3;case 3:if(0===e.Oe)return n;n=t,e.Oe--,e.Xe++,s.It+=(255&e.ht(e.ot++))<<16&16711680,s.mode=4;case 4:if(0===e.Oe)return n;n=t,e.Oe--,e.Xe++,s.It+=(255&e.ht(e.ot++))<<8&65280,s.mode=5;case 5:return 0===e.Oe?n:(n=t,e.Oe--,e.Xe++,s.It+=255&e.ht(e.ot++),s.mode=6,2);case 6:return s.mode=13,e.Je="need dictionary",s.marker=0,-2;case 7:if(n=s._t.yt(e,n),-3==n){s.mode=13,s.marker=0;break}if(0==n&&(n=t),1!=n)return n;n=t,s._t.reset(e,s.Ct),s.mode=12;case 12:return e.Oe=0,1;case 13:return-3;default:return-2}},e.Rt=(e,t,n)=>{let r=0,s=n;if(!e||!e.zt||6!=e.zt.mode)return-2;const i=e.zt;return s<1<<i.xt||(s=(1<<i.xt)-1,r=n-s),i._t.vt(t,r,s),i.mode=7,0},e.Bt=e=>{let n,r,s,i,o;if(!e||!e.zt)return-2;const c=e.zt;if(13!=c.mode&&(c.mode=13,c.marker=0),0===(n=e.Oe))return-5;for(r=e.ot,s=c.marker;0!==n&&4>s;)e.ht(r)==Ge[s]?s++:s=0!==e.ht(r)?0:4-s,r++,n--;return e.Xe+=r-e.ot,e.ot=r,e.Oe=n,c.marker=s,4!=s?-3:(i=e.Xe,o=e.Ye,t(e),e.Xe=i,e.Ye=o,c.mode=7,0)},e.Et=e=>e&&e.zt&&e.zt._t?e.zt._t.St():-2}function Qe(){}function Xe(e){const t=new Qe,i=e&&e.chunkSize?n.floor(2*e.chunkSize):131072,o=new s(i);let c=!1;t.Dt(),t.rt=o,this.append=(e,n)=>{const f=[];let a,l,u=0,w=0,h=0;if(0!==e.length){t.ot=0,t.st=e,t.Oe=e.length;do{if(t.ct=0,t.it=i,0!==t.Oe||c||(t.ot=0,c=!0),a=t.Vt(0),c&&-5===a){if(0!==t.Oe)throw new r("inflating: bad input")}else if(0!==a&&1!==a)throw new r("inflating: "+t.Je);if((c||1===a)&&t.Oe===e.length)throw new r("inflating: bad input");t.ct&&(t.ct===i?f.push(new s(o)):f.push(o.slice(0,t.ct))),h+=t.ct,n&&t.ot>0&&t.ot!=u&&(n(t.ot),u=t.ot)}while(t.Oe>0||0===t.it);return f.length>1?(l=new s(h),f.forEach((e=>{l.set(e,w),w+=e.length}))):l=f[0]||new s,l}},this.flush=()=>{t.At()}}Qe.prototype={Dt(e){const t=this;return t.zt=new Je,e||(e=15),t.zt.Dt(t,e)},Vt(e){const t=this;return t.zt?t.zt.Vt(t,e):-2},At(){const e=this;if(!e.zt)return-2;const t=e.zt.At(e);return e.zt=null,t},Bt(){const e=this;return e.zt?e.zt.Bt(e):-2},Rt(e,t){const n=this;return n.zt?n.zt.Rt(n,e,t):-2},ht(e){return this.st[e]},qe(e,t){return this.st.subarray(e,e+t)}},self.initCodec=()=>{self.Deflate=Ke,self.Inflate=Xe};\n'],{type:"text/javascript"}));e({workerScripts:{inflate:[t],deflate:[t]}});}
159
247
 
160
248
  /*
161
- * SJCL is open. You can use, modify and redistribute it under a BSD
162
- * license or under the GNU GPL, version 2.0.
163
- */
249
+ Copyright (c) 2022 Gildas Lormeau. All rights reserved.
164
250
 
165
- /** @fileOverview Javascript cryptography implementation.
166
- *
167
- * Crush to remove comments, shorten variable names and
168
- * generally reduce transmission size.
169
- *
170
- * @author Emily Stark
171
- * @author Mike Hamburg
172
- * @author Dan Boneh
173
- */
251
+ Redistribution and use in source and binary forms, with or without
252
+ modification, are permitted provided that the following conditions are met:
174
253
 
175
- /*jslint indent: 2, bitwise: false, nomen: false, plusplus: false, white: false, regexp: false */
254
+ 1. Redistributions of source code must retain the above copyright notice,
255
+ this list of conditions and the following disclaimer.
176
256
 
177
- /** @fileOverview Arrays of bits, encoded as arrays of Numbers.
178
- *
179
- * @author Emily Stark
180
- * @author Mike Hamburg
181
- * @author Dan Boneh
182
- */
257
+ 2. Redistributions in binary form must reproduce the above copyright
258
+ notice, this list of conditions and the following disclaimer in
259
+ the documentation and/or other materials provided with the distribution.
183
260
 
184
- /**
185
- * Arrays of bits, encoded as arrays of Numbers.
186
- * @namespace
187
- * @description
188
- * <p>
189
- * These objects are the currency accepted by SJCL's crypto functions.
190
- * </p>
191
- *
192
- * <p>
193
- * Most of our crypto primitives operate on arrays of 4-byte words internally,
194
- * but many of them can take arguments that are not a multiple of 4 bytes.
195
- * This library encodes arrays of bits (whose size need not be a multiple of 8
196
- * bits) as arrays of 32-bit words. The bits are packed, big-endian, into an
197
- * array of words, 32 bits at a time. Since the words are double-precision
198
- * floating point numbers, they fit some extra data. We use this (in a private,
199
- * possibly-changing manner) to encode the number of bits actually present
200
- * in the last word of the array.
201
- * </p>
202
- *
203
- * <p>
204
- * Because bitwise ops clear this out-of-band data, these arrays can be passed
205
- * to ciphers like AES which want arrays of words.
206
- * </p>
261
+ 3. The names of the authors may not be used to endorse or promote products
262
+ derived from this software without specific prior written permission.
263
+
264
+ THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
265
+ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
266
+ FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
267
+ INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
268
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
269
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
270
+ OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
271
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
272
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
273
+ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
207
274
  */
208
- const bitArray = {
209
- /**
210
- * Concatenate two bit arrays.
211
- * @param {bitArray} a1 The first array.
212
- * @param {bitArray} a2 The second array.
213
- * @return {bitArray} The concatenation of a1 and a2.
214
- */
215
- concat(a1, a2) {
216
- if (a1.length === 0 || a2.length === 0) {
217
- return a1.concat(a2);
218
- }
219
275
 
220
- const last = a1[a1.length - 1], shift = bitArray.getPartial(last);
221
- if (shift === 32) {
222
- return a1.concat(a2);
223
- } else {
224
- return bitArray._shiftRight(a2, shift, last | 0, a1.slice(0, a1.length - 1));
225
- }
226
- },
276
+ function getMimeType() {
277
+ return "application/octet-stream";
278
+ }
227
279
 
228
- /**
229
- * Find the length of an array of bits.
230
- * @param {bitArray} a The array.
231
- * @return {Number} The length of a, in bits.
232
- */
233
- bitLength(a) {
234
- const l = a.length;
235
- if (l === 0) {
236
- return 0;
237
- }
238
- const x = a[l - 1];
239
- return (l - 1) * 32 + bitArray.getPartial(x);
240
- },
280
+ function initShimAsyncCodec(library, options = {}, registerDataHandler) {
281
+ return {
282
+ Deflate: createCodecClass(library.Deflate, options.deflate, registerDataHandler),
283
+ Inflate: createCodecClass(library.Inflate, options.inflate, registerDataHandler)
284
+ };
285
+ }
241
286
 
242
- /**
243
- * Truncate an array.
244
- * @param {bitArray} a The array.
245
- * @param {Number} len The length to truncate to, in bits.
246
- * @return {bitArray} A new array, truncated to len bits.
247
- */
248
- clamp(a, len) {
249
- if (a.length * 32 < len) {
250
- return a;
287
+ function createCodecClass(constructor, constructorOptions, registerDataHandler) {
288
+ return class {
289
+
290
+ constructor(options) {
291
+ const codecAdapter = this;
292
+ const onData = data => {
293
+ if (codecAdapter.pendingData) {
294
+ const { pendingData } = codecAdapter;
295
+ codecAdapter.pendingData = new Uint8Array(pendingData.length + data.length);
296
+ pendingData.set(pendingData, 0);
297
+ pendingData.set(data, pendingData.length);
298
+ } else {
299
+ codecAdapter.pendingData = new Uint8Array(data);
300
+ }
301
+ };
302
+ if (Object.hasOwn(options, "level") && options.level === undefined) {
303
+ delete options.level;
304
+ }
305
+ codecAdapter.codec = new constructor(Object.assign({}, constructorOptions, options));
306
+ registerDataHandler(codecAdapter.codec, onData);
251
307
  }
252
- a = a.slice(0, Math.ceil(len / 32));
253
- const l = a.length;
254
- len = len & 31;
255
- if (l > 0 && len) {
256
- a[l - 1] = bitArray.partial(len, a[l - 1] & 0x80000000 >> (len - 1), 1);
308
+ append(data) {
309
+ this.codec.push(data);
310
+ return getResponse(this);
257
311
  }
258
- return a;
259
- },
312
+ flush() {
313
+ this.codec.push(new Uint8Array(), true);
314
+ return getResponse(this);
315
+ }
316
+ };
260
317
 
261
- /**
262
- * Make a partial word for a bit array.
263
- * @param {Number} len The number of bits in the word.
264
- * @param {Number} x The bits.
265
- * @param {Number} [_end=0] Pass 1 if x has already been shifted to the high side.
266
- * @return {Number} The partial word.
267
- */
268
- partial(len, x, _end) {
269
- if (len === 32) {
270
- return x;
318
+ function getResponse(codec) {
319
+ if (codec.pendingData) {
320
+ const output = codec.pendingData;
321
+ codec.pendingData = null;
322
+ return output;
323
+ } else {
324
+ return new Uint8Array();
271
325
  }
272
- return (_end ? x | 0 : x << (32 - len)) + len * 0x10000000000;
273
- },
326
+ }
327
+ }
274
328
 
275
- /**
276
- * Get the number of bits used by a partial word.
277
- * @param {Number} x The partial word.
278
- * @return {Number} The number of bits used by the partial word.
279
- */
280
- getPartial(x) {
281
- return Math.round(x / 0x10000000000) || 32;
282
- },
329
+ /*
330
+ Copyright (c) 2022 Gildas Lormeau. All rights reserved.
283
331
 
284
- /** Shift an array right.
285
- * @param {bitArray} a The array to shift.
286
- * @param {Number} shift The number of bits to shift.
287
- * @param {Number} [carry=0] A byte to carry in
288
- * @param {bitArray} [out=[]] An array to prepend to the output.
289
- * @private
290
- */
291
- _shiftRight(a, shift, carry, out) {
292
- if (out === undefined) {
293
- out = [];
294
- }
332
+ Redistribution and use in source and binary forms, with or without
333
+ modification, are permitted provided that the following conditions are met:
295
334
 
296
- for (; shift >= 32; shift -= 32) {
297
- out.push(carry);
298
- carry = 0;
299
- }
300
- if (shift === 0) {
301
- return out.concat(a);
302
- }
335
+ 1. Redistributions of source code must retain the above copyright notice,
336
+ this list of conditions and the following disclaimer.
303
337
 
304
- for (let i = 0; i < a.length; i++) {
305
- out.push(carry | a[i] >>> shift);
306
- carry = a[i] << (32 - shift);
307
- }
308
- const last2 = a.length ? a[a.length - 1] : 0;
309
- const shift2 = bitArray.getPartial(last2);
310
- out.push(bitArray.partial(shift + shift2 & 31, (shift + shift2 > 32) ? carry : out.pop(), 1));
311
- return out;
312
- }
313
- };
338
+ 2. Redistributions in binary form must reproduce the above copyright
339
+ notice, this list of conditions and the following disclaimer in
340
+ the documentation and/or other materials provided with the distribution.
314
341
 
315
- /** @fileOverview Bit array codec implementations.
316
- *
317
- * @author Emily Stark
318
- * @author Mike Hamburg
319
- * @author Dan Boneh
320
- */
342
+ 3. The names of the authors may not be used to endorse or promote products
343
+ derived from this software without specific prior written permission.
321
344
 
322
- /**
323
- * Arrays of bytes
324
- * @namespace
345
+ THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
346
+ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
347
+ FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
348
+ INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
349
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
350
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
351
+ OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
352
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
353
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
354
+ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
325
355
  */
326
- const codec = {
327
- bytes: {
328
- /** Convert from a bitArray to an array of bytes. */
329
- fromBits(arr) {
330
- const bl = bitArray.bitLength(arr);
331
- const byteLength = bl / 8;
332
- const out = new Uint8Array(byteLength);
333
- let tmp;
334
- for (let i = 0; i < byteLength; i++) {
335
- if ((i & 3) === 0) {
336
- tmp = arr[i / 4];
337
- }
338
- out[i] = tmp >>> 24;
339
- tmp <<= 8;
340
- }
341
- return out;
342
- },
343
- /** Convert from an array of bytes to a bitArray. */
344
- toBits(bytes) {
345
- const out = [];
346
- let i;
347
- let tmp = 0;
348
- for (i = 0; i < bytes.length; i++) {
349
- tmp = tmp << 8 | bytes[i];
350
- if ((i & 3) === 3) {
351
- out.push(tmp);
352
- tmp = 0;
353
- }
354
- }
355
- if (i & 3) {
356
- out.push(bitArray.partial(8 * (i & 3), tmp));
357
- }
358
- return out;
356
+
357
+ const table = [];
358
+ for (let i = 0; i < 256; i++) {
359
+ let t = i;
360
+ for (let j = 0; j < 8; j++) {
361
+ if (t & 1) {
362
+ t = (t >>> 1) ^ 0xEDB88320;
363
+ } else {
364
+ t = t >>> 1;
359
365
  }
360
366
  }
361
- };
367
+ table[i] = t;
368
+ }
362
369
 
363
- const hash = {};
370
+ class Crc32 {
364
371
 
365
- /**
366
- * Context for a SHA-1 operation in progress.
367
- * @constructor
368
- */
369
- hash.sha1 = function (hash) {
370
- if (hash) {
371
- this._h = hash._h.slice(0);
372
- this._buffer = hash._buffer.slice(0);
373
- this._length = hash._length;
374
- } else {
375
- this.reset();
372
+ constructor(crc) {
373
+ this.crc = crc || -1;
376
374
  }
377
- };
378
-
379
- hash.sha1.prototype = {
380
- /**
381
- * The hash's block size, in bits.
382
- * @constant
383
- */
384
- blockSize: 512,
385
-
386
- /**
387
- * Reset the hash state.
388
- * @return this
389
- */
390
- reset() {
391
- const sha1 = this;
392
- sha1._h = this._init.slice(0);
393
- sha1._buffer = [];
394
- sha1._length = 0;
395
- return sha1;
396
- },
397
375
 
398
- /**
399
- * Input several words to the hash.
400
- * @param {bitArray|String} data the data to hash.
401
- * @return this
402
- */
403
- update(data) {
404
- const sha1 = this;
405
- if (typeof data === "string") {
406
- data = codec.utf8String.toBits(data);
407
- }
408
- const b = sha1._buffer = bitArray.concat(sha1._buffer, data);
409
- const ol = sha1._length;
410
- const nl = sha1._length = ol + bitArray.bitLength(data);
411
- if (nl > 9007199254740991) {
412
- throw new Error("Cannot hash more than 2^53 - 1 bits");
413
- }
414
- const c = new Uint32Array(b);
415
- let j = 0;
416
- for (let i = sha1.blockSize + ol - ((sha1.blockSize + ol) & (sha1.blockSize - 1)); i <= nl;
417
- i += sha1.blockSize) {
418
- sha1._block(c.subarray(16 * j, 16 * (j + 1)));
419
- j += 1;
376
+ append(data) {
377
+ let crc = this.crc | 0;
378
+ for (let offset = 0, length = data.length | 0; offset < length; offset++) {
379
+ crc = (crc >>> 8) ^ table[(crc ^ data[offset]) & 0xFF];
420
380
  }
421
- b.splice(0, 16 * j);
422
- return sha1;
423
- },
381
+ this.crc = crc;
382
+ }
424
383
 
425
- /**
426
- * Complete hashing and output the hash value.
427
- * @return {bitArray} The hash value, an array of 5 big-endian words. TODO
428
- */
429
- finalize() {
430
- const sha1 = this;
431
- let b = sha1._buffer;
432
- const h = sha1._h;
384
+ get() {
385
+ return ~this.crc;
386
+ }
387
+ }
433
388
 
434
- // Round out and push the buffer
435
- b = bitArray.concat(b, [bitArray.partial(1, 1)]);
436
- // Round out the buffer to a multiple of 16 words, less the 2 length words.
437
- for (let i = b.length + 2; i & 15; i++) {
438
- b.push(0);
439
- }
389
+ /*
390
+ Copyright (c) 2022 Gildas Lormeau. All rights reserved.
440
391
 
441
- // append the length
442
- b.push(Math.floor(sha1._length / 0x100000000));
443
- b.push(sha1._length | 0);
392
+ Redistribution and use in source and binary forms, with or without
393
+ modification, are permitted provided that the following conditions are met:
444
394
 
445
- while (b.length) {
446
- sha1._block(b.splice(0, 16));
447
- }
395
+ 1. Redistributions of source code must retain the above copyright notice,
396
+ this list of conditions and the following disclaimer.
448
397
 
449
- sha1.reset();
450
- return h;
451
- },
398
+ 2. Redistributions in binary form must reproduce the above copyright
399
+ notice, this list of conditions and the following disclaimer in
400
+ the documentation and/or other materials provided with the distribution.
452
401
 
453
- /**
454
- * The SHA-1 initialization vector.
455
- * @private
456
- */
457
- _init: [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0],
402
+ 3. The names of the authors may not be used to endorse or promote products
403
+ derived from this software without specific prior written permission.
458
404
 
459
- /**
460
- * The SHA-1 hash key.
461
- * @private
462
- */
463
- _key: [0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6],
405
+ THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
406
+ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
407
+ FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
408
+ INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
409
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
410
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
411
+ OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
412
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
413
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
414
+ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
415
+ */
464
416
 
465
- /**
466
- * The SHA-1 logical functions f(0), f(1), ..., f(79).
467
- * @private
468
- */
469
- _f(t, b, c, d) {
470
- if (t <= 19) {
471
- return (b & c) | (~b & d);
472
- } else if (t <= 39) {
473
- return b ^ c ^ d;
474
- } else if (t <= 59) {
475
- return (b & c) | (b & d) | (c & d);
476
- } else if (t <= 79) {
477
- return b ^ c ^ d;
478
- }
479
- },
417
+ class Crc32Stream extends TransformStream {
480
418
 
481
- /**
482
- * Circular left-shift operator.
483
- * @private
484
- */
485
- _S(n, x) {
486
- return (x << n) | (x >>> 32 - n);
487
- },
419
+ constructor() {
420
+ const crc32 = new Crc32();
421
+ super({
422
+ transform(chunk) {
423
+ crc32.append(chunk);
424
+ },
425
+ flush(controller) {
426
+ const value = new Uint8Array(4);
427
+ const dataView = new DataView(value.buffer);
428
+ dataView.setUint32(0, crc32.get());
429
+ controller.enqueue(value);
430
+ }
431
+ });
432
+ }
433
+ }
488
434
 
489
- /**
490
- * Perform one cycle of SHA-1.
491
- * @param {Uint32Array|bitArray} words one block of words.
492
- * @private
493
- */
494
- _block(words) {
495
- const sha1 = this;
496
- const h = sha1._h;
497
- // When words is passed to _block, it has 16 elements. SHA1 _block
498
- // function extends words with new elements (at the end there are 80 elements).
499
- // The problem is that if we use Uint32Array instead of Array,
500
- // the length of Uint32Array cannot be changed. Thus, we replace words with a
501
- // normal Array here.
502
- const w = Array(80); // do not use Uint32Array here as the instantiation is slower
503
- for (let j = 0; j < 16; j++) {
504
- w[j] = words[j];
505
- }
435
+ /*
436
+ Copyright (c) 2022 Gildas Lormeau. All rights reserved.
506
437
 
507
- let a = h[0];
508
- let b = h[1];
509
- let c = h[2];
510
- let d = h[3];
511
- let e = h[4];
438
+ Redistribution and use in source and binary forms, with or without
439
+ modification, are permitted provided that the following conditions are met:
512
440
 
513
- for (let t = 0; t <= 79; t++) {
514
- if (t >= 16) {
515
- w[t] = sha1._S(1, w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16]);
516
- }
517
- const tmp = (sha1._S(5, a) + sha1._f(t, b, c, d) + e + w[t] +
518
- sha1._key[Math.floor(t / 20)]) | 0;
519
- e = d;
520
- d = c;
521
- c = sha1._S(30, b);
522
- b = a;
523
- a = tmp;
524
- }
441
+ 1. Redistributions of source code must retain the above copyright notice,
442
+ this list of conditions and the following disclaimer.
525
443
 
526
- h[0] = (h[0] + a) | 0;
527
- h[1] = (h[1] + b) | 0;
528
- h[2] = (h[2] + c) | 0;
529
- h[3] = (h[3] + d) | 0;
530
- h[4] = (h[4] + e) | 0;
444
+ 2. Redistributions in binary form must reproduce the above copyright
445
+ notice, this list of conditions and the following disclaimer in
446
+ the documentation and/or other materials provided with the distribution.
447
+
448
+ 3. The names of the authors may not be used to endorse or promote products
449
+ derived from this software without specific prior written permission.
450
+
451
+ THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
452
+ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
453
+ FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
454
+ INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
455
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
456
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
457
+ OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
458
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
459
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
460
+ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
461
+ */
462
+
463
+ function encodeText(value) {
464
+ if (typeof TextEncoder == "undefined") {
465
+ value = unescape(encodeURIComponent(value));
466
+ const result = new Uint8Array(value.length);
467
+ for (let i = 0; i < result.length; i++) {
468
+ result[i] = value.charCodeAt(i);
469
+ }
470
+ return result;
471
+ } else {
472
+ return new TextEncoder().encode(value);
531
473
  }
532
- };
474
+ }
533
475
 
534
- /** @fileOverview Low-level AES implementation.
535
- *
536
- * This file contains a low-level implementation of AES, optimized for
537
- * size and for efficiency on several browsers. It is based on
538
- * OpenSSL's aes_core.c, a public-domain implementation by Vincent
539
- * Rijmen, Antoon Bosselaers and Paulo Barreto.
476
+ // Derived from https://github.com/xqdoo00o/jszip/blob/master/lib/sjcl.js and https://github.com/bitwiseshiftleft/sjcl
477
+
478
+ // deno-lint-ignore-file no-this-alias
479
+
480
+ /*
481
+ * SJCL is open. You can use, modify and redistribute it under a BSD
482
+ * license or under the GNU GPL, version 2.0.
483
+ */
484
+
485
+ /** @fileOverview Javascript cryptography implementation.
540
486
  *
541
- * An older version of this implementation is available in the public
542
- * domain, but this one is (c) Emily Stark, Mike Hamburg, Dan Boneh,
543
- * Stanford University 2008-2010 and BSD-licensed for liability
544
- * reasons.
487
+ * Crush to remove comments, shorten variable names and
488
+ * generally reduce transmission size.
545
489
  *
546
490
  * @author Emily Stark
547
491
  * @author Mike Hamburg
548
492
  * @author Dan Boneh
549
493
  */
550
494
 
551
- const cipher = {};
495
+ /*jslint indent: 2, bitwise: false, nomen: false, plusplus: false, white: false, regexp: false */
496
+
497
+ /** @fileOverview Arrays of bits, encoded as arrays of Numbers.
498
+ *
499
+ * @author Emily Stark
500
+ * @author Mike Hamburg
501
+ * @author Dan Boneh
502
+ */
552
503
 
553
504
  /**
554
- * Schedule out an AES key for both encryption and decryption. This
555
- * is a low-level class. Use a cipher mode to do bulk encryption.
505
+ * Arrays of bits, encoded as arrays of Numbers.
506
+ * @namespace
507
+ * @description
508
+ * <p>
509
+ * These objects are the currency accepted by SJCL's crypto functions.
510
+ * </p>
556
511
  *
557
- * @constructor
558
- * @param {Array} key The key as an array of 4, 6 or 8 words.
512
+ * <p>
513
+ * Most of our crypto primitives operate on arrays of 4-byte words internally,
514
+ * but many of them can take arguments that are not a multiple of 4 bytes.
515
+ * This library encodes arrays of bits (whose size need not be a multiple of 8
516
+ * bits) as arrays of 32-bit words. The bits are packed, big-endian, into an
517
+ * array of words, 32 bits at a time. Since the words are double-precision
518
+ * floating point numbers, they fit some extra data. We use this (in a private,
519
+ * possibly-changing manner) to encode the number of bits actually present
520
+ * in the last word of the array.
521
+ * </p>
522
+ *
523
+ * <p>
524
+ * Because bitwise ops clear this out-of-band data, these arrays can be passed
525
+ * to ciphers like AES which want arrays of words.
526
+ * </p>
559
527
  */
560
- cipher.aes = class {
561
- constructor(key) {
562
- /**
563
- * The expanded S-box and inverse S-box tables. These will be computed
564
- * on the client so that we don't have to send them down the wire.
565
- *
566
- * There are two tables, _tables[0] is for encryption and
567
- * _tables[1] is for decryption.
568
- *
569
- * The first 4 sub-tables are the expanded S-box with MixColumns. The
570
- * last (_tables[01][4]) is the S-box itself.
571
- *
572
- * @private
573
- */
574
- const aes = this;
575
- aes._tables = [[[], [], [], [], []], [[], [], [], [], []]];
528
+ const bitArray = {
529
+ /**
530
+ * Concatenate two bit arrays.
531
+ * @param {bitArray} a1 The first array.
532
+ * @param {bitArray} a2 The second array.
533
+ * @return {bitArray} The concatenation of a1 and a2.
534
+ */
535
+ concat(a1, a2) {
536
+ if (a1.length === 0 || a2.length === 0) {
537
+ return a1.concat(a2);
538
+ }
576
539
 
577
- if (!aes._tables[0][0][0]) {
578
- aes._precompute();
540
+ const last = a1[a1.length - 1], shift = bitArray.getPartial(last);
541
+ if (shift === 32) {
542
+ return a1.concat(a2);
543
+ } else {
544
+ return bitArray._shiftRight(a2, shift, last | 0, a1.slice(0, a1.length - 1));
579
545
  }
546
+ },
580
547
 
581
- const sbox = aes._tables[0][4];
582
- const decTable = aes._tables[1];
583
- const keyLen = key.length;
584
-
585
- let i, encKey, decKey, rcon = 1;
586
-
587
- if (keyLen !== 4 && keyLen !== 6 && keyLen !== 8) {
588
- throw new Error("invalid aes key size");
548
+ /**
549
+ * Find the length of an array of bits.
550
+ * @param {bitArray} a The array.
551
+ * @return {Number} The length of a, in bits.
552
+ */
553
+ bitLength(a) {
554
+ const l = a.length;
555
+ if (l === 0) {
556
+ return 0;
589
557
  }
558
+ const x = a[l - 1];
559
+ return (l - 1) * 32 + bitArray.getPartial(x);
560
+ },
590
561
 
591
- aes._key = [encKey = key.slice(0), decKey = []];
592
-
593
- // schedule encryption keys
594
- for (i = keyLen; i < 4 * keyLen + 28; i++) {
595
- let tmp = encKey[i - 1];
596
-
597
- // apply sbox
598
- if (i % keyLen === 0 || (keyLen === 8 && i % keyLen === 4)) {
599
- tmp = sbox[tmp >>> 24] << 24 ^ sbox[tmp >> 16 & 255] << 16 ^ sbox[tmp >> 8 & 255] << 8 ^ sbox[tmp & 255];
600
-
601
- // shift rows and add rcon
602
- if (i % keyLen === 0) {
603
- tmp = tmp << 8 ^ tmp >>> 24 ^ rcon << 24;
604
- rcon = rcon << 1 ^ (rcon >> 7) * 283;
605
- }
606
- }
607
-
608
- encKey[i] = encKey[i - keyLen] ^ tmp;
562
+ /**
563
+ * Truncate an array.
564
+ * @param {bitArray} a The array.
565
+ * @param {Number} len The length to truncate to, in bits.
566
+ * @return {bitArray} A new array, truncated to len bits.
567
+ */
568
+ clamp(a, len) {
569
+ if (a.length * 32 < len) {
570
+ return a;
609
571
  }
610
-
611
- // schedule decryption keys
612
- for (let j = 0; i; j++, i--) {
613
- const tmp = encKey[j & 3 ? i : i - 4];
614
- if (i <= 4 || j < 4) {
615
- decKey[j] = tmp;
616
- } else {
617
- decKey[j] = decTable[0][sbox[tmp >>> 24]] ^
618
- decTable[1][sbox[tmp >> 16 & 255]] ^
619
- decTable[2][sbox[tmp >> 8 & 255]] ^
620
- decTable[3][sbox[tmp & 255]];
621
- }
572
+ a = a.slice(0, Math.ceil(len / 32));
573
+ const l = a.length;
574
+ len = len & 31;
575
+ if (l > 0 && len) {
576
+ a[l - 1] = bitArray.partial(len, a[l - 1] & 0x80000000 >> (len - 1), 1);
622
577
  }
623
- }
624
- // public
625
- /* Something like this might appear here eventually
626
- name: "AES",
627
- blockSize: 4,
628
- keySizes: [4,6,8],
629
- */
578
+ return a;
579
+ },
630
580
 
631
581
  /**
632
- * Encrypt an array of 4 big-endian words.
633
- * @param {Array} data The plaintext.
634
- * @return {Array} The ciphertext.
582
+ * Make a partial word for a bit array.
583
+ * @param {Number} len The number of bits in the word.
584
+ * @param {Number} x The bits.
585
+ * @param {Number} [_end=0] Pass 1 if x has already been shifted to the high side.
586
+ * @return {Number} The partial word.
635
587
  */
636
- encrypt(data) {
637
- return this._crypt(data, 0);
638
- }
588
+ partial(len, x, _end) {
589
+ if (len === 32) {
590
+ return x;
591
+ }
592
+ return (_end ? x | 0 : x << (32 - len)) + len * 0x10000000000;
593
+ },
639
594
 
640
595
  /**
641
- * Decrypt an array of 4 big-endian words.
642
- * @param {Array} data The ciphertext.
643
- * @return {Array} The plaintext.
596
+ * Get the number of bits used by a partial word.
597
+ * @param {Number} x The partial word.
598
+ * @return {Number} The number of bits used by the partial word.
644
599
  */
645
- decrypt(data) {
646
- return this._crypt(data, 1);
647
- }
600
+ getPartial(x) {
601
+ return Math.round(x / 0x10000000000) || 32;
602
+ },
648
603
 
649
- /**
650
- * Expand the S-box tables.
651
- *
604
+ /** Shift an array right.
605
+ * @param {bitArray} a The array to shift.
606
+ * @param {Number} shift The number of bits to shift.
607
+ * @param {Number} [carry=0] A byte to carry in
608
+ * @param {bitArray} [out=[]] An array to prepend to the output.
652
609
  * @private
653
610
  */
654
- _precompute() {
655
- const encTable = this._tables[0];
656
- const decTable = this._tables[1];
657
- const sbox = encTable[4];
658
- const sboxInv = decTable[4];
659
- const d = [];
660
- const th = [];
661
- let xInv, x2, x4, x8;
662
-
663
- // Compute double and third tables
664
- for (let i = 0; i < 256; i++) {
665
- th[(d[i] = i << 1 ^ (i >> 7) * 283) ^ i] = i;
666
- }
667
-
668
- for (let x = xInv = 0; !sbox[x]; x ^= x2 || 1, xInv = th[xInv] || 1) {
669
- // Compute sbox
670
- let s = xInv ^ xInv << 1 ^ xInv << 2 ^ xInv << 3 ^ xInv << 4;
671
- s = s >> 8 ^ s & 255 ^ 99;
672
- sbox[x] = s;
673
- sboxInv[s] = x;
674
-
675
- // Compute MixColumns
676
- x8 = d[x4 = d[x2 = d[x]]];
677
- let tDec = x8 * 0x1010101 ^ x4 * 0x10001 ^ x2 * 0x101 ^ x * 0x1010100;
678
- let tEnc = d[s] * 0x101 ^ s * 0x1010100;
679
-
680
- for (let i = 0; i < 4; i++) {
681
- encTable[i][x] = tEnc = tEnc << 24 ^ tEnc >>> 8;
682
- decTable[i][s] = tDec = tDec << 24 ^ tDec >>> 8;
683
- }
684
- }
685
-
686
- // Compactify. Considerable speedup on Firefox.
687
- for (let i = 0; i < 5; i++) {
688
- encTable[i] = encTable[i].slice(0);
689
- decTable[i] = decTable[i].slice(0);
611
+ _shiftRight(a, shift, carry, out) {
612
+ if (out === undefined) {
613
+ out = [];
690
614
  }
691
- }
692
615
 
693
- /**
694
- * Encryption and decryption core.
695
- * @param {Array} input Four words to be encrypted or decrypted.
696
- * @param dir The direction, 0 for encrypt and 1 for decrypt.
697
- * @return {Array} The four encrypted or decrypted words.
698
- * @private
699
- */
700
- _crypt(input, dir) {
701
- if (input.length !== 4) {
702
- throw new Error("invalid aes block size");
616
+ for (; shift >= 32; shift -= 32) {
617
+ out.push(carry);
618
+ carry = 0;
703
619
  }
704
-
705
- const key = this._key[dir];
706
-
707
- const nInnerRounds = key.length / 4 - 2;
708
- const out = [0, 0, 0, 0];
709
- const table = this._tables[dir];
710
-
711
- // load up the tables
712
- const t0 = table[0];
713
- const t1 = table[1];
714
- const t2 = table[2];
715
- const t3 = table[3];
716
- const sbox = table[4];
717
-
718
- // state variables a,b,c,d are loaded with pre-whitened data
719
- let a = input[0] ^ key[0];
720
- let b = input[dir ? 3 : 1] ^ key[1];
721
- let c = input[2] ^ key[2];
722
- let d = input[dir ? 1 : 3] ^ key[3];
723
- let kIndex = 4;
724
- let a2, b2, c2;
725
-
726
- // Inner rounds. Cribbed from OpenSSL.
727
- for (let i = 0; i < nInnerRounds; i++) {
728
- a2 = t0[a >>> 24] ^ t1[b >> 16 & 255] ^ t2[c >> 8 & 255] ^ t3[d & 255] ^ key[kIndex];
729
- b2 = t0[b >>> 24] ^ t1[c >> 16 & 255] ^ t2[d >> 8 & 255] ^ t3[a & 255] ^ key[kIndex + 1];
730
- c2 = t0[c >>> 24] ^ t1[d >> 16 & 255] ^ t2[a >> 8 & 255] ^ t3[b & 255] ^ key[kIndex + 2];
731
- d = t0[d >>> 24] ^ t1[a >> 16 & 255] ^ t2[b >> 8 & 255] ^ t3[c & 255] ^ key[kIndex + 3];
732
- kIndex += 4;
733
- a = a2; b = b2; c = c2;
620
+ if (shift === 0) {
621
+ return out.concat(a);
734
622
  }
735
623
 
736
- // Last round.
737
- for (let i = 0; i < 4; i++) {
738
- out[dir ? 3 & -i : i] =
739
- sbox[a >>> 24] << 24 ^
740
- sbox[b >> 16 & 255] << 16 ^
741
- sbox[c >> 8 & 255] << 8 ^
742
- sbox[d & 255] ^
743
- key[kIndex++];
744
- a2 = a; a = b; b = c; c = d; d = a2;
624
+ for (let i = 0; i < a.length; i++) {
625
+ out.push(carry | a[i] >>> shift);
626
+ carry = a[i] << (32 - shift);
745
627
  }
746
-
628
+ const last2 = a.length ? a[a.length - 1] : 0;
629
+ const shift2 = bitArray.getPartial(last2);
630
+ out.push(bitArray.partial(shift + shift2 & 31, (shift + shift2 > 32) ? carry : out.pop(), 1));
747
631
  return out;
748
632
  }
749
633
  };
750
634
 
751
- /**
752
- * Random values
753
- * @namespace
754
- */
755
- const random = {
756
- /**
757
- * Generate random words with pure js, cryptographically not as strong & safe as native implementation.
758
- * @param {TypedArray} typedArray The array to fill.
759
- * @return {TypedArray} The random values.
760
- */
761
- getRandomValues(typedArray) {
762
- const words = new Uint32Array(typedArray.buffer);
763
- const r = (m_w) => {
764
- let m_z = 0x3ade68b1;
765
- const mask = 0xffffffff;
766
- return function () {
767
- m_z = (0x9069 * (m_z & 0xFFFF) + (m_z >> 0x10)) & mask;
768
- m_w = (0x4650 * (m_w & 0xFFFF) + (m_w >> 0x10)) & mask;
769
- const result = ((((m_z << 0x10) + m_w) & mask) / 0x100000000) + .5;
770
- return result * (Math.random() > .5 ? 1 : -1);
771
- };
772
- };
773
- for (let i = 0, rcache; i < typedArray.length; i += 4) {
774
- const _r = r((rcache || Math.random()) * 0x100000000);
775
- rcache = _r() * 0x3ade67b7;
776
- words[i / 4] = (_r() * 0x100000000) | 0;
777
- }
778
- return typedArray;
779
- }
780
- };
781
-
782
- /** @fileOverview CTR mode implementation.
783
- *
784
- * Special thanks to Roy Nicholson for pointing out a bug in our
785
- * implementation.
635
+ /** @fileOverview Bit array codec implementations.
786
636
  *
787
637
  * @author Emily Stark
788
638
  * @author Mike Hamburg
789
639
  * @author Dan Boneh
790
640
  */
791
641
 
792
- /** Brian Gladman's CTR Mode.
793
- * @constructor
794
- * @param {Object} _prf The aes instance to generate key.
795
- * @param {bitArray} _iv The iv for ctr mode, it must be 128 bits.
796
- */
797
-
798
- const mode = {};
799
-
800
642
  /**
801
- * Brian Gladman's CTR Mode.
643
+ * Arrays of bytes
802
644
  * @namespace
803
645
  */
804
- mode.ctrGladman = class {
805
- constructor(prf, iv) {
806
- this._prf = prf;
807
- this._initIv = iv;
808
- this._iv = iv;
809
- }
810
-
811
- reset() {
812
- this._iv = this._initIv;
813
- }
814
-
815
- /** Input some data to calculate.
816
- * @param {bitArray} data the data to process, it must be intergral multiple of 128 bits unless it's the last.
817
- */
818
- update(data) {
819
- return this.calculate(this._prf, data, this._iv);
820
- }
821
-
822
- incWord(word) {
823
- if (((word >> 24) & 0xff) === 0xff) { //overflow
824
- let b1 = (word >> 16) & 0xff;
825
- let b2 = (word >> 8) & 0xff;
826
- let b3 = word & 0xff;
827
-
828
- if (b1 === 0xff) { // overflow b1
829
- b1 = 0;
830
- if (b2 === 0xff) {
831
- b2 = 0;
832
- if (b3 === 0xff) {
833
- b3 = 0;
834
- } else {
835
- ++b3;
836
- }
837
- } else {
838
- ++b2;
646
+ const codec = {
647
+ bytes: {
648
+ /** Convert from a bitArray to an array of bytes. */
649
+ fromBits(arr) {
650
+ const bl = bitArray.bitLength(arr);
651
+ const byteLength = bl / 8;
652
+ const out = new Uint8Array(byteLength);
653
+ let tmp;
654
+ for (let i = 0; i < byteLength; i++) {
655
+ if ((i & 3) === 0) {
656
+ tmp = arr[i / 4];
839
657
  }
840
- } else {
841
- ++b1;
658
+ out[i] = tmp >>> 24;
659
+ tmp <<= 8;
842
660
  }
843
-
844
- word = 0;
845
- word += (b1 << 16);
846
- word += (b2 << 8);
847
- word += b3;
848
- } else {
849
- word += (0x01 << 24);
850
- }
851
- return word;
852
- }
853
-
854
- incCounter(counter) {
855
- if ((counter[0] = this.incWord(counter[0])) === 0) {
856
- // encr_data in fileenc.c from Dr Brian Gladman's counts only with DWORD j < 8
857
- counter[1] = this.incWord(counter[1]);
858
- }
859
- }
860
-
861
- calculate(prf, data, iv) {
862
- let l;
863
- if (!(l = data.length)) {
864
- return [];
865
- }
866
- const bl = bitArray.bitLength(data);
867
- for (let i = 0; i < l; i += 4) {
868
- this.incCounter(iv);
869
- const e = prf.encrypt(iv);
870
- data[i] ^= e[0];
871
- data[i + 1] ^= e[1];
872
- data[i + 2] ^= e[2];
873
- data[i + 3] ^= e[3];
874
- }
875
- return bitArray.clamp(data, bl);
876
- }
877
- };
878
-
879
- const misc = {
880
- importKey(password) {
881
- return new misc.hmacSha1(codec.bytes.toBits(password));
882
- },
883
- pbkdf2(prf, salt, count, length) {
884
- count = count || 10000;
885
- if (length < 0 || count < 0) {
886
- throw new Error("invalid params to pbkdf2");
887
- }
888
- const byteLength = ((length >> 5) + 1) << 2;
889
- let u, ui, i, j, k;
890
- const arrayBuffer = new ArrayBuffer(byteLength);
891
- const out = new DataView(arrayBuffer);
892
- let outLength = 0;
893
- const b = bitArray;
894
- salt = codec.bytes.toBits(salt);
895
- for (k = 1; outLength < (byteLength || 1); k++) {
896
- u = ui = prf.encrypt(b.concat(salt, [k]));
897
- for (i = 1; i < count; i++) {
898
- ui = prf.encrypt(ui);
899
- for (j = 0; j < ui.length; j++) {
900
- u[j] ^= ui[j];
661
+ return out;
662
+ },
663
+ /** Convert from an array of bytes to a bitArray. */
664
+ toBits(bytes) {
665
+ const out = [];
666
+ let i;
667
+ let tmp = 0;
668
+ for (i = 0; i < bytes.length; i++) {
669
+ tmp = tmp << 8 | bytes[i];
670
+ if ((i & 3) === 3) {
671
+ out.push(tmp);
672
+ tmp = 0;
901
673
  }
902
674
  }
903
- for (i = 0; outLength < (byteLength || 1) && i < u.length; i++) {
904
- out.setInt32(outLength, u[i]);
905
- outLength += 4;
675
+ if (i & 3) {
676
+ out.push(bitArray.partial(8 * (i & 3), tmp));
906
677
  }
678
+ return out;
907
679
  }
908
- return arrayBuffer.slice(0, length / 8);
909
680
  }
910
681
  };
911
682
 
912
- /** @fileOverview HMAC implementation.
913
- *
914
- * @author Emily Stark
915
- * @author Mike Hamburg
916
- * @author Dan Boneh
917
- */
683
+ const hash = {};
918
684
 
919
- /** HMAC with the specified hash function.
685
+ /**
686
+ * Context for a SHA-1 operation in progress.
920
687
  * @constructor
921
- * @param {bitArray} key the key for HMAC.
922
- * @param {Object} [Hash=hash.sha1] The hash function to use.
923
688
  */
924
- misc.hmacSha1 = class {
689
+ hash.sha1 = function (hash) {
690
+ if (hash) {
691
+ this._h = hash._h.slice(0);
692
+ this._buffer = hash._buffer.slice(0);
693
+ this._length = hash._length;
694
+ } else {
695
+ this.reset();
696
+ }
697
+ };
925
698
 
926
- constructor(key) {
927
- const hmac = this;
928
- const Hash = hmac._hash = hash.sha1;
929
- const exKey = [[], []];
930
- const bs = Hash.prototype.blockSize / 32;
931
- hmac._baseHash = [new Hash(), new Hash()];
699
+ hash.sha1.prototype = {
700
+ /**
701
+ * The hash's block size, in bits.
702
+ * @constant
703
+ */
704
+ blockSize: 512,
932
705
 
933
- if (key.length > bs) {
934
- key = Hash.hash(key);
935
- }
706
+ /**
707
+ * Reset the hash state.
708
+ * @return this
709
+ */
710
+ reset() {
711
+ const sha1 = this;
712
+ sha1._h = this._init.slice(0);
713
+ sha1._buffer = [];
714
+ sha1._length = 0;
715
+ return sha1;
716
+ },
936
717
 
937
- for (let i = 0; i < bs; i++) {
938
- exKey[0][i] = key[i] ^ 0x36363636;
939
- exKey[1][i] = key[i] ^ 0x5C5C5C5C;
718
+ /**
719
+ * Input several words to the hash.
720
+ * @param {bitArray|String} data the data to hash.
721
+ * @return this
722
+ */
723
+ update(data) {
724
+ const sha1 = this;
725
+ if (typeof data === "string") {
726
+ data = codec.utf8String.toBits(data);
727
+ }
728
+ const b = sha1._buffer = bitArray.concat(sha1._buffer, data);
729
+ const ol = sha1._length;
730
+ const nl = sha1._length = ol + bitArray.bitLength(data);
731
+ if (nl > 9007199254740991) {
732
+ throw new Error("Cannot hash more than 2^53 - 1 bits");
733
+ }
734
+ const c = new Uint32Array(b);
735
+ let j = 0;
736
+ for (let i = sha1.blockSize + ol - ((sha1.blockSize + ol) & (sha1.blockSize - 1)); i <= nl;
737
+ i += sha1.blockSize) {
738
+ sha1._block(c.subarray(16 * j, 16 * (j + 1)));
739
+ j += 1;
940
740
  }
741
+ b.splice(0, 16 * j);
742
+ return sha1;
743
+ },
941
744
 
942
- hmac._baseHash[0].update(exKey[0]);
943
- hmac._baseHash[1].update(exKey[1]);
944
- hmac._resultHash = new Hash(hmac._baseHash[0]);
945
- }
946
- reset() {
947
- const hmac = this;
948
- hmac._resultHash = new hmac._hash(hmac._baseHash[0]);
949
- hmac._updated = false;
950
- }
745
+ /**
746
+ * Complete hashing and output the hash value.
747
+ * @return {bitArray} The hash value, an array of 5 big-endian words. TODO
748
+ */
749
+ finalize() {
750
+ const sha1 = this;
751
+ let b = sha1._buffer;
752
+ const h = sha1._h;
951
753
 
952
- update(data) {
953
- const hmac = this;
954
- hmac._updated = true;
955
- hmac._resultHash.update(data);
956
- }
754
+ // Round out and push the buffer
755
+ b = bitArray.concat(b, [bitArray.partial(1, 1)]);
756
+ // Round out the buffer to a multiple of 16 words, less the 2 length words.
757
+ for (let i = b.length + 2; i & 15; i++) {
758
+ b.push(0);
759
+ }
957
760
 
958
- digest() {
959
- const hmac = this;
960
- const w = hmac._resultHash.finalize();
961
- const result = new (hmac._hash)(hmac._baseHash[1]).update(w).finalize();
761
+ // append the length
762
+ b.push(Math.floor(sha1._length / 0x100000000));
763
+ b.push(sha1._length | 0);
962
764
 
963
- hmac.reset();
765
+ while (b.length) {
766
+ sha1._block(b.splice(0, 16));
767
+ }
964
768
 
965
- return result;
966
- }
769
+ sha1.reset();
770
+ return h;
771
+ },
967
772
 
968
- encrypt(data) {
969
- if (!this._updated) {
970
- this.update(data);
971
- return this.digest(data);
972
- } else {
973
- throw new Error("encrypt on already updated hmac called!");
773
+ /**
774
+ * The SHA-1 initialization vector.
775
+ * @private
776
+ */
777
+ _init: [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0],
778
+
779
+ /**
780
+ * The SHA-1 hash key.
781
+ * @private
782
+ */
783
+ _key: [0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6],
784
+
785
+ /**
786
+ * The SHA-1 logical functions f(0), f(1), ..., f(79).
787
+ * @private
788
+ */
789
+ _f(t, b, c, d) {
790
+ if (t <= 19) {
791
+ return (b & c) | (~b & d);
792
+ } else if (t <= 39) {
793
+ return b ^ c ^ d;
794
+ } else if (t <= 59) {
795
+ return (b & c) | (b & d) | (c & d);
796
+ } else if (t <= 79) {
797
+ return b ^ c ^ d;
974
798
  }
975
- }
976
- };
799
+ },
977
800
 
978
- /*
979
- Copyright (c) 2022 Gildas Lormeau. All rights reserved.
801
+ /**
802
+ * Circular left-shift operator.
803
+ * @private
804
+ */
805
+ _S(n, x) {
806
+ return (x << n) | (x >>> 32 - n);
807
+ },
980
808
 
981
- Redistribution and use in source and binary forms, with or without
982
- modification, are permitted provided that the following conditions are met:
809
+ /**
810
+ * Perform one cycle of SHA-1.
811
+ * @param {Uint32Array|bitArray} words one block of words.
812
+ * @private
813
+ */
814
+ _block(words) {
815
+ const sha1 = this;
816
+ const h = sha1._h;
817
+ // When words is passed to _block, it has 16 elements. SHA1 _block
818
+ // function extends words with new elements (at the end there are 80 elements).
819
+ // The problem is that if we use Uint32Array instead of Array,
820
+ // the length of Uint32Array cannot be changed. Thus, we replace words with a
821
+ // normal Array here.
822
+ const w = Array(80); // do not use Uint32Array here as the instantiation is slower
823
+ for (let j = 0; j < 16; j++) {
824
+ w[j] = words[j];
825
+ }
983
826
 
984
- 1. Redistributions of source code must retain the above copyright notice,
985
- this list of conditions and the following disclaimer.
827
+ let a = h[0];
828
+ let b = h[1];
829
+ let c = h[2];
830
+ let d = h[3];
831
+ let e = h[4];
986
832
 
987
- 2. Redistributions in binary form must reproduce the above copyright
988
- notice, this list of conditions and the following disclaimer in
989
- the documentation and/or other materials provided with the distribution.
833
+ for (let t = 0; t <= 79; t++) {
834
+ if (t >= 16) {
835
+ w[t] = sha1._S(1, w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16]);
836
+ }
837
+ const tmp = (sha1._S(5, a) + sha1._f(t, b, c, d) + e + w[t] +
838
+ sha1._key[Math.floor(t / 20)]) | 0;
839
+ e = d;
840
+ d = c;
841
+ c = sha1._S(30, b);
842
+ b = a;
843
+ a = tmp;
844
+ }
990
845
 
991
- 3. The names of the authors may not be used to endorse or promote products
992
- derived from this software without specific prior written permission.
846
+ h[0] = (h[0] + a) | 0;
847
+ h[1] = (h[1] + b) | 0;
848
+ h[2] = (h[2] + c) | 0;
849
+ h[3] = (h[3] + d) | 0;
850
+ h[4] = (h[4] + e) | 0;
851
+ }
852
+ };
993
853
 
994
- THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
995
- INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
996
- FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
997
- INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
998
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
999
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
1000
- OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1001
- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
1002
- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
1003
- EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
854
+ /** @fileOverview Low-level AES implementation.
855
+ *
856
+ * This file contains a low-level implementation of AES, optimized for
857
+ * size and for efficiency on several browsers. It is based on
858
+ * OpenSSL's aes_core.c, a public-domain implementation by Vincent
859
+ * Rijmen, Antoon Bosselaers and Paulo Barreto.
860
+ *
861
+ * An older version of this implementation is available in the public
862
+ * domain, but this one is (c) Emily Stark, Mike Hamburg, Dan Boneh,
863
+ * Stanford University 2008-2010 and BSD-licensed for liability
864
+ * reasons.
865
+ *
866
+ * @author Emily Stark
867
+ * @author Mike Hamburg
868
+ * @author Dan Boneh
1004
869
  */
1005
870
 
1006
- /* global crypto */
871
+ const cipher = {};
1007
872
 
1008
- const GET_RANDOM_VALUES_SUPPORTED = typeof crypto != "undefined" && typeof crypto.getRandomValues == "function";
873
+ /**
874
+ * Schedule out an AES key for both encryption and decryption. This
875
+ * is a low-level class. Use a cipher mode to do bulk encryption.
876
+ *
877
+ * @constructor
878
+ * @param {Array} key The key as an array of 4, 6 or 8 words.
879
+ */
880
+ cipher.aes = class {
881
+ constructor(key) {
882
+ /**
883
+ * The expanded S-box and inverse S-box tables. These will be computed
884
+ * on the client so that we don't have to send them down the wire.
885
+ *
886
+ * There are two tables, _tables[0] is for encryption and
887
+ * _tables[1] is for decryption.
888
+ *
889
+ * The first 4 sub-tables are the expanded S-box with MixColumns. The
890
+ * last (_tables[01][4]) is the S-box itself.
891
+ *
892
+ * @private
893
+ */
894
+ const aes = this;
895
+ aes._tables = [[[], [], [], [], []], [[], [], [], [], []]];
1009
896
 
1010
- const ERR_INVALID_PASSWORD = "Invalid password";
1011
- const ERR_INVALID_SIGNATURE = "Invalid signature";
897
+ if (!aes._tables[0][0][0]) {
898
+ aes._precompute();
899
+ }
1012
900
 
1013
- function getRandomValues(array) {
1014
- if (GET_RANDOM_VALUES_SUPPORTED) {
1015
- return crypto.getRandomValues(array);
1016
- } else {
1017
- return random.getRandomValues(array);
1018
- }
1019
- }
901
+ const sbox = aes._tables[0][4];
902
+ const decTable = aes._tables[1];
903
+ const keyLen = key.length;
1020
904
 
1021
- /*
1022
- Copyright (c) 2022 Gildas Lormeau. All rights reserved.
905
+ let i, encKey, decKey, rcon = 1;
1023
906
 
1024
- Redistribution and use in source and binary forms, with or without
1025
- modification, are permitted provided that the following conditions are met:
907
+ if (keyLen !== 4 && keyLen !== 6 && keyLen !== 8) {
908
+ throw new Error("invalid aes key size");
909
+ }
1026
910
 
1027
- 1. Redistributions of source code must retain the above copyright notice,
1028
- this list of conditions and the following disclaimer.
911
+ aes._key = [encKey = key.slice(0), decKey = []];
1029
912
 
1030
- 2. Redistributions in binary form must reproduce the above copyright
1031
- notice, this list of conditions and the following disclaimer in
1032
- the documentation and/or other materials provided with the distribution.
913
+ // schedule encryption keys
914
+ for (i = keyLen; i < 4 * keyLen + 28; i++) {
915
+ let tmp = encKey[i - 1];
1033
916
 
1034
- 3. The names of the authors may not be used to endorse or promote products
1035
- derived from this software without specific prior written permission.
917
+ // apply sbox
918
+ if (i % keyLen === 0 || (keyLen === 8 && i % keyLen === 4)) {
919
+ tmp = sbox[tmp >>> 24] << 24 ^ sbox[tmp >> 16 & 255] << 16 ^ sbox[tmp >> 8 & 255] << 8 ^ sbox[tmp & 255];
1036
920
 
1037
- THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
1038
- INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
1039
- FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
1040
- INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
1041
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1042
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
1043
- OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1044
- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
1045
- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
1046
- EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1047
- */
921
+ // shift rows and add rcon
922
+ if (i % keyLen === 0) {
923
+ tmp = tmp << 8 ^ tmp >>> 24 ^ rcon << 24;
924
+ rcon = rcon << 1 ^ (rcon >> 7) * 283;
925
+ }
926
+ }
1048
927
 
1049
- const BLOCK_LENGTH = 16;
1050
- const RAW_FORMAT = "raw";
1051
- const PBKDF2_ALGORITHM = { name: "PBKDF2" };
1052
- const HASH_ALGORITHM = { name: "HMAC" };
1053
- const HASH_FUNCTION = "SHA-1";
1054
- const BASE_KEY_ALGORITHM = Object.assign({ hash: HASH_ALGORITHM }, PBKDF2_ALGORITHM);
1055
- const DERIVED_BITS_ALGORITHM = Object.assign({ iterations: 1000, hash: { name: HASH_FUNCTION } }, PBKDF2_ALGORITHM);
1056
- const DERIVED_BITS_USAGE = ["deriveBits"];
1057
- const SALT_LENGTH = [8, 12, 16];
1058
- const KEY_LENGTH = [16, 24, 32];
1059
- const SIGNATURE_LENGTH = 10;
1060
- const COUNTER_DEFAULT_VALUE = [0, 0, 0, 0];
1061
- const UNDEFINED_TYPE = "undefined";
1062
- const FUNCTION_TYPE = "function";
1063
- const CRYPTO_API_SUPPORTED = typeof crypto != UNDEFINED_TYPE;
1064
- const subtle = CRYPTO_API_SUPPORTED && crypto.subtle;
1065
- const SUBTLE_API_SUPPORTED = CRYPTO_API_SUPPORTED && typeof subtle != UNDEFINED_TYPE;
1066
- const IMPORT_KEY_SUPPORTED = CRYPTO_API_SUPPORTED && SUBTLE_API_SUPPORTED && typeof subtle.importKey == FUNCTION_TYPE;
1067
- const DERIVE_BITS_SUPPORTED = CRYPTO_API_SUPPORTED && SUBTLE_API_SUPPORTED && typeof subtle.deriveBits == FUNCTION_TYPE;
1068
- const codecBytes = codec.bytes;
1069
- const Aes = cipher.aes;
1070
- const CtrGladman = mode.ctrGladman;
1071
- const HmacSha1 = misc.hmacSha1;
928
+ encKey[i] = encKey[i - keyLen] ^ tmp;
929
+ }
1072
930
 
1073
- class AESDecryptionStream extends TransformStream {
931
+ // schedule decryption keys
932
+ for (let j = 0; i; j++, i--) {
933
+ const tmp = encKey[j & 3 ? i : i - 4];
934
+ if (i <= 4 || j < 4) {
935
+ decKey[j] = tmp;
936
+ } else {
937
+ decKey[j] = decTable[0][sbox[tmp >>> 24]] ^
938
+ decTable[1][sbox[tmp >> 16 & 255]] ^
939
+ decTable[2][sbox[tmp >> 8 & 255]] ^
940
+ decTable[3][sbox[tmp & 255]];
941
+ }
942
+ }
943
+ }
944
+ // public
945
+ /* Something like this might appear here eventually
946
+ name: "AES",
947
+ blockSize: 4,
948
+ keySizes: [4,6,8],
949
+ */
1074
950
 
1075
- constructor(password, signed, strength) {
1076
- super({
1077
- start() {
1078
- Object.assign(this, {
1079
- ready: new Promise(resolve => this.resolveReady = resolve),
1080
- password,
1081
- signed,
1082
- strength: strength - 1,
1083
- pending: new Uint8Array()
1084
- });
1085
- },
1086
- async transform(chunk, controller) {
1087
- const aesCrypto = this;
1088
- const {
1089
- password,
1090
- strength,
1091
- resolveReady,
1092
- ready
1093
- } = aesCrypto;
1094
- if (password) {
1095
- await createDecryptionKeys(aesCrypto, strength, password, subarray(chunk, 0, SALT_LENGTH[strength] + 2));
1096
- chunk = subarray(chunk, SALT_LENGTH[strength] + 2);
1097
- resolveReady();
1098
- } else {
1099
- await ready;
1100
- }
1101
- const output = new Uint8Array(chunk.length - SIGNATURE_LENGTH - ((chunk.length - SIGNATURE_LENGTH) % BLOCK_LENGTH));
1102
- controller.enqueue(append(aesCrypto, chunk, output, 0, SIGNATURE_LENGTH, true));
1103
- },
1104
- async flush(controller) {
1105
- const {
1106
- signed,
1107
- ctr,
1108
- hmac,
1109
- pending,
1110
- ready
1111
- } = this;
1112
- await ready;
1113
- const chunkToDecrypt = subarray(pending, 0, pending.length - SIGNATURE_LENGTH);
1114
- const originalSignature = subarray(pending, pending.length - SIGNATURE_LENGTH);
1115
- let decryptedChunkArray = new Uint8Array();
1116
- if (chunkToDecrypt.length) {
1117
- const encryptedChunk = toBits(codecBytes, chunkToDecrypt);
1118
- hmac.update(encryptedChunk);
1119
- const decryptedChunk = ctr.update(encryptedChunk);
1120
- decryptedChunkArray = fromBits(codecBytes, decryptedChunk);
1121
- }
1122
- if (signed) {
1123
- const signature = subarray(fromBits(codecBytes, hmac.digest()), 0, SIGNATURE_LENGTH);
1124
- for (let indexSignature = 0; indexSignature < SIGNATURE_LENGTH; indexSignature++) {
1125
- if (signature[indexSignature] != originalSignature[indexSignature]) {
1126
- throw new Error(ERR_INVALID_SIGNATURE);
1127
- }
1128
- }
1129
- }
1130
- controller.enqueue(decryptedChunkArray);
1131
- }
1132
- });
951
+ /**
952
+ * Encrypt an array of 4 big-endian words.
953
+ * @param {Array} data The plaintext.
954
+ * @return {Array} The ciphertext.
955
+ */
956
+ encrypt(data) {
957
+ return this._crypt(data, 0);
1133
958
  }
1134
- }
1135
959
 
1136
- class AESEncryptionStream extends TransformStream {
1137
-
1138
- constructor(password, strength) {
1139
- let stream;
1140
- super({
1141
- start() {
1142
- Object.assign(this, {
1143
- ready: new Promise(resolve => this.resolveReady = resolve),
1144
- password,
1145
- strength: strength - 1,
1146
- pending: new Uint8Array()
1147
- });
1148
- },
1149
- async transform(chunk, controller) {
1150
- const aesCrypto = this;
1151
- const {
1152
- password,
1153
- strength,
1154
- resolveReady,
1155
- ready
1156
- } = aesCrypto;
1157
- let preamble = new Uint8Array();
1158
- if (password) {
1159
- preamble = await createEncryptionKeys(aesCrypto, strength, password);
1160
- resolveReady();
1161
- } else {
1162
- await ready;
1163
- }
1164
- const output = new Uint8Array(preamble.length + chunk.length - (chunk.length % BLOCK_LENGTH));
1165
- output.set(preamble, 0);
1166
- controller.enqueue(append(aesCrypto, chunk, output, preamble.length, 0));
1167
- },
1168
- async flush(controller) {
1169
- const {
1170
- ctr,
1171
- hmac,
1172
- pending,
1173
- ready
1174
- } = this;
1175
- await ready;
1176
- let encryptedChunkArray = new Uint8Array();
1177
- if (pending.length) {
1178
- const encryptedChunk = ctr.update(toBits(codecBytes, pending));
1179
- hmac.update(encryptedChunk);
1180
- encryptedChunkArray = fromBits(codecBytes, encryptedChunk);
1181
- }
1182
- stream.signature = fromBits(codecBytes, hmac.digest()).slice(0, SIGNATURE_LENGTH);
1183
- controller.enqueue(concat(encryptedChunkArray, stream.signature));
1184
- }
1185
- });
1186
- stream = this;
960
+ /**
961
+ * Decrypt an array of 4 big-endian words.
962
+ * @param {Array} data The ciphertext.
963
+ * @return {Array} The plaintext.
964
+ */
965
+ decrypt(data) {
966
+ return this._crypt(data, 1);
1187
967
  }
1188
- }
1189
968
 
1190
- function append(aesCrypto, input, output, paddingStart, paddingEnd, verifySignature) {
1191
- const {
1192
- ctr,
1193
- hmac,
1194
- pending
1195
- } = aesCrypto;
1196
- const inputLength = input.length - paddingEnd;
1197
- if (pending.length) {
1198
- input = concat(pending, input);
1199
- output = expand(output, inputLength - (inputLength % BLOCK_LENGTH));
1200
- }
1201
- let offset;
1202
- for (offset = 0; offset <= inputLength - BLOCK_LENGTH; offset += BLOCK_LENGTH) {
1203
- const inputChunk = toBits(codecBytes, subarray(input, offset, offset + BLOCK_LENGTH));
1204
- if (verifySignature) {
1205
- hmac.update(inputChunk);
969
+ /**
970
+ * Expand the S-box tables.
971
+ *
972
+ * @private
973
+ */
974
+ _precompute() {
975
+ const encTable = this._tables[0];
976
+ const decTable = this._tables[1];
977
+ const sbox = encTable[4];
978
+ const sboxInv = decTable[4];
979
+ const d = [];
980
+ const th = [];
981
+ let xInv, x2, x4, x8;
982
+
983
+ // Compute double and third tables
984
+ for (let i = 0; i < 256; i++) {
985
+ th[(d[i] = i << 1 ^ (i >> 7) * 283) ^ i] = i;
1206
986
  }
1207
- const outputChunk = ctr.update(inputChunk);
1208
- if (!verifySignature) {
1209
- hmac.update(outputChunk);
987
+
988
+ for (let x = xInv = 0; !sbox[x]; x ^= x2 || 1, xInv = th[xInv] || 1) {
989
+ // Compute sbox
990
+ let s = xInv ^ xInv << 1 ^ xInv << 2 ^ xInv << 3 ^ xInv << 4;
991
+ s = s >> 8 ^ s & 255 ^ 99;
992
+ sbox[x] = s;
993
+ sboxInv[s] = x;
994
+
995
+ // Compute MixColumns
996
+ x8 = d[x4 = d[x2 = d[x]]];
997
+ let tDec = x8 * 0x1010101 ^ x4 * 0x10001 ^ x2 * 0x101 ^ x * 0x1010100;
998
+ let tEnc = d[s] * 0x101 ^ s * 0x1010100;
999
+
1000
+ for (let i = 0; i < 4; i++) {
1001
+ encTable[i][x] = tEnc = tEnc << 24 ^ tEnc >>> 8;
1002
+ decTable[i][s] = tDec = tDec << 24 ^ tDec >>> 8;
1003
+ }
1210
1004
  }
1211
- output.set(fromBits(codecBytes, outputChunk), offset + paddingStart);
1212
- }
1213
- aesCrypto.pending = subarray(input, offset);
1214
- return output;
1215
- }
1216
1005
 
1217
- async function createDecryptionKeys(decrypt, strength, password, preamble) {
1218
- const passwordVerificationKey = await createKeys$1(decrypt, strength, password, subarray(preamble, 0, SALT_LENGTH[strength]));
1219
- const passwordVerification = subarray(preamble, SALT_LENGTH[strength]);
1220
- if (passwordVerificationKey[0] != passwordVerification[0] || passwordVerificationKey[1] != passwordVerification[1]) {
1221
- throw new Error(ERR_INVALID_PASSWORD);
1006
+ // Compactify. Considerable speedup on Firefox.
1007
+ for (let i = 0; i < 5; i++) {
1008
+ encTable[i] = encTable[i].slice(0);
1009
+ decTable[i] = decTable[i].slice(0);
1010
+ }
1222
1011
  }
1223
- }
1224
1012
 
1225
- async function createEncryptionKeys(encrypt, strength, password) {
1226
- const salt = getRandomValues(new Uint8Array(SALT_LENGTH[strength]));
1227
- const passwordVerification = await createKeys$1(encrypt, strength, password, salt);
1228
- return concat(salt, passwordVerification);
1229
- }
1013
+ /**
1014
+ * Encryption and decryption core.
1015
+ * @param {Array} input Four words to be encrypted or decrypted.
1016
+ * @param dir The direction, 0 for encrypt and 1 for decrypt.
1017
+ * @return {Array} The four encrypted or decrypted words.
1018
+ * @private
1019
+ */
1020
+ _crypt(input, dir) {
1021
+ if (input.length !== 4) {
1022
+ throw new Error("invalid aes block size");
1023
+ }
1230
1024
 
1231
- async function createKeys$1(aesCrypto, strength, password, salt) {
1232
- aesCrypto.password = null;
1233
- const encodedPassword = encodeText(password);
1234
- const baseKey = await importKey(RAW_FORMAT, encodedPassword, BASE_KEY_ALGORITHM, false, DERIVED_BITS_USAGE);
1235
- const derivedBits = await deriveBits(Object.assign({ salt }, DERIVED_BITS_ALGORITHM), baseKey, 8 * ((KEY_LENGTH[strength] * 2) + 2));
1236
- const compositeKey = new Uint8Array(derivedBits);
1237
- const key = toBits(codecBytes, subarray(compositeKey, 0, KEY_LENGTH[strength]));
1238
- const authentication = toBits(codecBytes, subarray(compositeKey, KEY_LENGTH[strength], KEY_LENGTH[strength] * 2));
1239
- const passwordVerification = subarray(compositeKey, KEY_LENGTH[strength] * 2);
1240
- Object.assign(aesCrypto, {
1241
- keys: {
1242
- key,
1243
- authentication,
1244
- passwordVerification
1245
- },
1246
- ctr: new CtrGladman(new Aes(key), Array.from(COUNTER_DEFAULT_VALUE)),
1247
- hmac: new HmacSha1(authentication)
1248
- });
1249
- return passwordVerification;
1250
- }
1025
+ const key = this._key[dir];
1251
1026
 
1252
- function importKey(format, password, algorithm, extractable, keyUsages) {
1253
- if (IMPORT_KEY_SUPPORTED) {
1254
- return subtle.importKey(format, password, algorithm, extractable, keyUsages);
1255
- } else {
1256
- return misc.importKey(password);
1027
+ const nInnerRounds = key.length / 4 - 2;
1028
+ const out = [0, 0, 0, 0];
1029
+ const table = this._tables[dir];
1030
+
1031
+ // load up the tables
1032
+ const t0 = table[0];
1033
+ const t1 = table[1];
1034
+ const t2 = table[2];
1035
+ const t3 = table[3];
1036
+ const sbox = table[4];
1037
+
1038
+ // state variables a,b,c,d are loaded with pre-whitened data
1039
+ let a = input[0] ^ key[0];
1040
+ let b = input[dir ? 3 : 1] ^ key[1];
1041
+ let c = input[2] ^ key[2];
1042
+ let d = input[dir ? 1 : 3] ^ key[3];
1043
+ let kIndex = 4;
1044
+ let a2, b2, c2;
1045
+
1046
+ // Inner rounds. Cribbed from OpenSSL.
1047
+ for (let i = 0; i < nInnerRounds; i++) {
1048
+ a2 = t0[a >>> 24] ^ t1[b >> 16 & 255] ^ t2[c >> 8 & 255] ^ t3[d & 255] ^ key[kIndex];
1049
+ b2 = t0[b >>> 24] ^ t1[c >> 16 & 255] ^ t2[d >> 8 & 255] ^ t3[a & 255] ^ key[kIndex + 1];
1050
+ c2 = t0[c >>> 24] ^ t1[d >> 16 & 255] ^ t2[a >> 8 & 255] ^ t3[b & 255] ^ key[kIndex + 2];
1051
+ d = t0[d >>> 24] ^ t1[a >> 16 & 255] ^ t2[b >> 8 & 255] ^ t3[c & 255] ^ key[kIndex + 3];
1052
+ kIndex += 4;
1053
+ a = a2; b = b2; c = c2;
1054
+ }
1055
+
1056
+ // Last round.
1057
+ for (let i = 0; i < 4; i++) {
1058
+ out[dir ? 3 & -i : i] =
1059
+ sbox[a >>> 24] << 24 ^
1060
+ sbox[b >> 16 & 255] << 16 ^
1061
+ sbox[c >> 8 & 255] << 8 ^
1062
+ sbox[d & 255] ^
1063
+ key[kIndex++];
1064
+ a2 = a; a = b; b = c; c = d; d = a2;
1065
+ }
1066
+
1067
+ return out;
1257
1068
  }
1258
- }
1069
+ };
1259
1070
 
1260
- async function deriveBits(algorithm, baseKey, length) {
1261
- if (DERIVE_BITS_SUPPORTED) {
1262
- return await subtle.deriveBits(algorithm, baseKey, length);
1263
- } else {
1264
- return misc.pbkdf2(baseKey, algorithm.salt, DERIVED_BITS_ALGORITHM.iterations, length);
1071
+ /**
1072
+ * Random values
1073
+ * @namespace
1074
+ */
1075
+ const random = {
1076
+ /**
1077
+ * Generate random words with pure js, cryptographically not as strong & safe as native implementation.
1078
+ * @param {TypedArray} typedArray The array to fill.
1079
+ * @return {TypedArray} The random values.
1080
+ */
1081
+ getRandomValues(typedArray) {
1082
+ const words = new Uint32Array(typedArray.buffer);
1083
+ const r = (m_w) => {
1084
+ let m_z = 0x3ade68b1;
1085
+ const mask = 0xffffffff;
1086
+ return function () {
1087
+ m_z = (0x9069 * (m_z & 0xFFFF) + (m_z >> 0x10)) & mask;
1088
+ m_w = (0x4650 * (m_w & 0xFFFF) + (m_w >> 0x10)) & mask;
1089
+ const result = ((((m_z << 0x10) + m_w) & mask) / 0x100000000) + .5;
1090
+ return result * (Math.random() > .5 ? 1 : -1);
1091
+ };
1092
+ };
1093
+ for (let i = 0, rcache; i < typedArray.length; i += 4) {
1094
+ const _r = r((rcache || Math.random()) * 0x100000000);
1095
+ rcache = _r() * 0x3ade67b7;
1096
+ words[i / 4] = (_r() * 0x100000000) | 0;
1097
+ }
1098
+ return typedArray;
1265
1099
  }
1266
- }
1100
+ };
1101
+
1102
+ /** @fileOverview CTR mode implementation.
1103
+ *
1104
+ * Special thanks to Roy Nicholson for pointing out a bug in our
1105
+ * implementation.
1106
+ *
1107
+ * @author Emily Stark
1108
+ * @author Mike Hamburg
1109
+ * @author Dan Boneh
1110
+ */
1111
+
1112
+ /** Brian Gladman's CTR Mode.
1113
+ * @constructor
1114
+ * @param {Object} _prf The aes instance to generate key.
1115
+ * @param {bitArray} _iv The iv for ctr mode, it must be 128 bits.
1116
+ */
1267
1117
 
1268
- function concat(leftArray, rightArray) {
1269
- let array = leftArray;
1270
- if (leftArray.length + rightArray.length) {
1271
- array = new Uint8Array(leftArray.length + rightArray.length);
1272
- array.set(leftArray, 0);
1273
- array.set(rightArray, leftArray.length);
1118
+ const mode = {};
1119
+
1120
+ /**
1121
+ * Brian Gladman's CTR Mode.
1122
+ * @namespace
1123
+ */
1124
+ mode.ctrGladman = class {
1125
+ constructor(prf, iv) {
1126
+ this._prf = prf;
1127
+ this._initIv = iv;
1128
+ this._iv = iv;
1274
1129
  }
1275
- return array;
1276
- }
1277
1130
 
1278
- function expand(inputArray, length) {
1279
- if (length && length > inputArray.length) {
1280
- const array = inputArray;
1281
- inputArray = new Uint8Array(length);
1282
- inputArray.set(array, 0);
1131
+ reset() {
1132
+ this._iv = this._initIv;
1283
1133
  }
1284
- return inputArray;
1285
- }
1286
1134
 
1287
- function subarray(array, begin, end) {
1288
- return array.subarray(begin, end);
1289
- }
1135
+ /** Input some data to calculate.
1136
+ * @param {bitArray} data the data to process, it must be intergral multiple of 128 bits unless it's the last.
1137
+ */
1138
+ update(data) {
1139
+ return this.calculate(this._prf, data, this._iv);
1140
+ }
1290
1141
 
1291
- function fromBits(codecBytes, chunk) {
1292
- return codecBytes.fromBits(chunk);
1293
- }
1294
- function toBits(codecBytes, chunk) {
1295
- return codecBytes.toBits(chunk);
1296
- }
1142
+ incWord(word) {
1143
+ if (((word >> 24) & 0xff) === 0xff) { //overflow
1144
+ let b1 = (word >> 16) & 0xff;
1145
+ let b2 = (word >> 8) & 0xff;
1146
+ let b3 = word & 0xff;
1297
1147
 
1298
- /*
1299
- Copyright (c) 2022 Gildas Lormeau. All rights reserved.
1148
+ if (b1 === 0xff) { // overflow b1
1149
+ b1 = 0;
1150
+ if (b2 === 0xff) {
1151
+ b2 = 0;
1152
+ if (b3 === 0xff) {
1153
+ b3 = 0;
1154
+ } else {
1155
+ ++b3;
1156
+ }
1157
+ } else {
1158
+ ++b2;
1159
+ }
1160
+ } else {
1161
+ ++b1;
1162
+ }
1300
1163
 
1301
- Redistribution and use in source and binary forms, with or without
1302
- modification, are permitted provided that the following conditions are met:
1164
+ word = 0;
1165
+ word += (b1 << 16);
1166
+ word += (b2 << 8);
1167
+ word += b3;
1168
+ } else {
1169
+ word += (0x01 << 24);
1170
+ }
1171
+ return word;
1172
+ }
1303
1173
 
1304
- 1. Redistributions of source code must retain the above copyright notice,
1305
- this list of conditions and the following disclaimer.
1174
+ incCounter(counter) {
1175
+ if ((counter[0] = this.incWord(counter[0])) === 0) {
1176
+ // encr_data in fileenc.c from Dr Brian Gladman's counts only with DWORD j < 8
1177
+ counter[1] = this.incWord(counter[1]);
1178
+ }
1179
+ }
1306
1180
 
1307
- 2. Redistributions in binary form must reproduce the above copyright
1308
- notice, this list of conditions and the following disclaimer in
1309
- the documentation and/or other materials provided with the distribution.
1181
+ calculate(prf, data, iv) {
1182
+ let l;
1183
+ if (!(l = data.length)) {
1184
+ return [];
1185
+ }
1186
+ const bl = bitArray.bitLength(data);
1187
+ for (let i = 0; i < l; i += 4) {
1188
+ this.incCounter(iv);
1189
+ const e = prf.encrypt(iv);
1190
+ data[i] ^= e[0];
1191
+ data[i + 1] ^= e[1];
1192
+ data[i + 2] ^= e[2];
1193
+ data[i + 3] ^= e[3];
1194
+ }
1195
+ return bitArray.clamp(data, bl);
1196
+ }
1197
+ };
1310
1198
 
1311
- 3. The names of the authors may not be used to endorse or promote products
1312
- derived from this software without specific prior written permission.
1199
+ const misc = {
1200
+ importKey(password) {
1201
+ return new misc.hmacSha1(codec.bytes.toBits(password));
1202
+ },
1203
+ pbkdf2(prf, salt, count, length) {
1204
+ count = count || 10000;
1205
+ if (length < 0 || count < 0) {
1206
+ throw new Error("invalid params to pbkdf2");
1207
+ }
1208
+ const byteLength = ((length >> 5) + 1) << 2;
1209
+ let u, ui, i, j, k;
1210
+ const arrayBuffer = new ArrayBuffer(byteLength);
1211
+ const out = new DataView(arrayBuffer);
1212
+ let outLength = 0;
1213
+ const b = bitArray;
1214
+ salt = codec.bytes.toBits(salt);
1215
+ for (k = 1; outLength < (byteLength || 1); k++) {
1216
+ u = ui = prf.encrypt(b.concat(salt, [k]));
1217
+ for (i = 1; i < count; i++) {
1218
+ ui = prf.encrypt(ui);
1219
+ for (j = 0; j < ui.length; j++) {
1220
+ u[j] ^= ui[j];
1221
+ }
1222
+ }
1223
+ for (i = 0; outLength < (byteLength || 1) && i < u.length; i++) {
1224
+ out.setInt32(outLength, u[i]);
1225
+ outLength += 4;
1226
+ }
1227
+ }
1228
+ return arrayBuffer.slice(0, length / 8);
1229
+ }
1230
+ };
1313
1231
 
1314
- THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
1315
- INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
1316
- FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
1317
- INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
1318
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1319
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
1320
- OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1321
- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
1322
- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
1323
- EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1232
+ /** @fileOverview HMAC implementation.
1233
+ *
1234
+ * @author Emily Stark
1235
+ * @author Mike Hamburg
1236
+ * @author Dan Boneh
1324
1237
  */
1325
1238
 
1326
- const HEADER_LENGTH = 12;
1239
+ /** HMAC with the specified hash function.
1240
+ * @constructor
1241
+ * @param {bitArray} key the key for HMAC.
1242
+ * @param {Object} [Hash=hash.sha1] The hash function to use.
1243
+ */
1244
+ misc.hmacSha1 = class {
1327
1245
 
1328
- class ZipCryptoDecryptionStream extends TransformStream {
1246
+ constructor(key) {
1247
+ const hmac = this;
1248
+ const Hash = hmac._hash = hash.sha1;
1249
+ const exKey = [[], []];
1250
+ const bs = Hash.prototype.blockSize / 32;
1251
+ hmac._baseHash = [new Hash(), new Hash()];
1329
1252
 
1330
- constructor(password, passwordVerification) {
1331
- super({
1332
- start() {
1333
- Object.assign(this, {
1334
- password,
1335
- passwordVerification
1336
- });
1337
- createKeys(this, password);
1338
- },
1339
- transform(chunk, controller) {
1340
- const zipCrypto = this;
1341
- if (zipCrypto.password) {
1342
- const decryptedHeader = decrypt(zipCrypto, chunk.subarray(0, HEADER_LENGTH));
1343
- zipCrypto.password = null;
1344
- if (decryptedHeader[HEADER_LENGTH - 1] != zipCrypto.passwordVerification) {
1345
- throw new Error(ERR_INVALID_PASSWORD);
1346
- }
1347
- chunk = chunk.subarray(HEADER_LENGTH);
1348
- }
1349
- controller.enqueue(decrypt(zipCrypto, chunk));
1350
- }
1351
- });
1352
- }
1353
- }
1253
+ if (key.length > bs) {
1254
+ key = Hash.hash(key);
1255
+ }
1354
1256
 
1355
- class ZipCryptoEncryptionStream extends TransformStream {
1257
+ for (let i = 0; i < bs; i++) {
1258
+ exKey[0][i] = key[i] ^ 0x36363636;
1259
+ exKey[1][i] = key[i] ^ 0x5C5C5C5C;
1260
+ }
1356
1261
 
1357
- constructor(password, passwordVerification) {
1358
- super({
1359
- start() {
1360
- Object.assign(this, {
1361
- password,
1362
- passwordVerification
1363
- });
1364
- createKeys(this, password);
1365
- },
1366
- transform(chunk, controller) {
1367
- const zipCrypto = this;
1368
- let output;
1369
- let offset;
1370
- if (zipCrypto.password) {
1371
- zipCrypto.password = null;
1372
- const header = getRandomValues(new Uint8Array(HEADER_LENGTH));
1373
- header[HEADER_LENGTH - 1] = zipCrypto.passwordVerification;
1374
- output = new Uint8Array(chunk.length + header.length);
1375
- output.set(encrypt(zipCrypto, header), 0);
1376
- offset = HEADER_LENGTH;
1377
- } else {
1378
- output = new Uint8Array(chunk.length);
1379
- offset = 0;
1380
- }
1381
- output.set(encrypt(zipCrypto, chunk), offset);
1382
- controller.enqueue(output);
1383
- }
1384
- });
1262
+ hmac._baseHash[0].update(exKey[0]);
1263
+ hmac._baseHash[1].update(exKey[1]);
1264
+ hmac._resultHash = new Hash(hmac._baseHash[0]);
1265
+ }
1266
+ reset() {
1267
+ const hmac = this;
1268
+ hmac._resultHash = new hmac._hash(hmac._baseHash[0]);
1269
+ hmac._updated = false;
1385
1270
  }
1386
- }
1387
1271
 
1388
- function decrypt(target, input) {
1389
- const output = new Uint8Array(input.length);
1390
- for (let index = 0; index < input.length; index++) {
1391
- output[index] = getByte(target) ^ input[index];
1392
- updateKeys(target, output[index]);
1272
+ update(data) {
1273
+ const hmac = this;
1274
+ hmac._updated = true;
1275
+ hmac._resultHash.update(data);
1393
1276
  }
1394
- return output;
1395
- }
1396
1277
 
1397
- function encrypt(target, input) {
1398
- const output = new Uint8Array(input.length);
1399
- for (let index = 0; index < input.length; index++) {
1400
- output[index] = getByte(target) ^ input[index];
1401
- updateKeys(target, input[index]);
1278
+ digest() {
1279
+ const hmac = this;
1280
+ const w = hmac._resultHash.finalize();
1281
+ const result = new (hmac._hash)(hmac._baseHash[1]).update(w).finalize();
1282
+
1283
+ hmac.reset();
1284
+
1285
+ return result;
1402
1286
  }
1403
- return output;
1404
- }
1405
1287
 
1406
- function createKeys(target, password) {
1407
- const keys = [0x12345678, 0x23456789, 0x34567890];
1408
- Object.assign(target, {
1409
- keys,
1410
- crcKey0: new Crc32(keys[0]),
1411
- crcKey2: new Crc32(keys[2]),
1412
- });
1413
- for (let index = 0; index < password.length; index++) {
1414
- updateKeys(target, password.charCodeAt(index));
1288
+ encrypt(data) {
1289
+ if (!this._updated) {
1290
+ this.update(data);
1291
+ return this.digest(data);
1292
+ } else {
1293
+ throw new Error("encrypt on already updated hmac called!");
1294
+ }
1415
1295
  }
1416
- }
1296
+ };
1417
1297
 
1418
- function updateKeys(target, byte) {
1419
- let [key0, key1, key2] = target.keys;
1420
- target.crcKey0.append([byte]);
1421
- key0 = ~target.crcKey0.get();
1422
- key1 = getInt32(Math.imul(getInt32(key1 + getInt8(key0)), 134775813) + 1);
1423
- target.crcKey2.append([key1 >>> 24]);
1424
- key2 = ~target.crcKey2.get();
1425
- target.keys = [key0, key1, key2];
1426
- }
1298
+ /*
1299
+ Copyright (c) 2022 Gildas Lormeau. All rights reserved.
1427
1300
 
1428
- function getByte(target) {
1429
- const temp = target.keys[2] | 2;
1430
- return getInt8(Math.imul(temp, (temp ^ 1)) >>> 8);
1431
- }
1301
+ Redistribution and use in source and binary forms, with or without
1302
+ modification, are permitted provided that the following conditions are met:
1432
1303
 
1433
- function getInt8(number) {
1434
- return number & 0xFF;
1435
- }
1304
+ 1. Redistributions of source code must retain the above copyright notice,
1305
+ this list of conditions and the following disclaimer.
1436
1306
 
1437
- function getInt32(number) {
1438
- return number & 0xFFFFFFFF;
1307
+ 2. Redistributions in binary form must reproduce the above copyright
1308
+ notice, this list of conditions and the following disclaimer in
1309
+ the documentation and/or other materials provided with the distribution.
1310
+
1311
+ 3. The names of the authors may not be used to endorse or promote products
1312
+ derived from this software without specific prior written permission.
1313
+
1314
+ THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
1315
+ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
1316
+ FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
1317
+ INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
1318
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1319
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
1320
+ OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1321
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
1322
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
1323
+ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1324
+ */
1325
+
1326
+ /* global crypto */
1327
+
1328
+ const GET_RANDOM_VALUES_SUPPORTED = typeof crypto != "undefined" && typeof crypto.getRandomValues == "function";
1329
+
1330
+ const ERR_INVALID_PASSWORD = "Invalid password";
1331
+ const ERR_INVALID_SIGNATURE = "Invalid signature";
1332
+
1333
+ function getRandomValues(array) {
1334
+ if (GET_RANDOM_VALUES_SUPPORTED) {
1335
+ return crypto.getRandomValues(array);
1336
+ } else {
1337
+ return random.getRandomValues(array);
1338
+ }
1439
1339
  }
1440
1340
 
1441
1341
  /*
@@ -1466,114 +1366,255 @@
1466
1366
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1467
1367
  */
1468
1368
 
1469
- const COMPRESSION_FORMAT = "deflate-raw";
1369
+ const BLOCK_LENGTH = 16;
1370
+ const RAW_FORMAT = "raw";
1371
+ const PBKDF2_ALGORITHM = { name: "PBKDF2" };
1372
+ const HASH_ALGORITHM = { name: "HMAC" };
1373
+ const HASH_FUNCTION = "SHA-1";
1374
+ const BASE_KEY_ALGORITHM = Object.assign({ hash: HASH_ALGORITHM }, PBKDF2_ALGORITHM);
1375
+ const DERIVED_BITS_ALGORITHM = Object.assign({ iterations: 1000, hash: { name: HASH_FUNCTION } }, PBKDF2_ALGORITHM);
1376
+ const DERIVED_BITS_USAGE = ["deriveBits"];
1377
+ const SALT_LENGTH = [8, 12, 16];
1378
+ const KEY_LENGTH = [16, 24, 32];
1379
+ const SIGNATURE_LENGTH = 10;
1380
+ const COUNTER_DEFAULT_VALUE = [0, 0, 0, 0];
1381
+ const UNDEFINED_TYPE = "undefined";
1382
+ const FUNCTION_TYPE = "function";
1383
+ // deno-lint-ignore valid-typeof
1384
+ const CRYPTO_API_SUPPORTED = typeof crypto != UNDEFINED_TYPE;
1385
+ const subtle = CRYPTO_API_SUPPORTED && crypto.subtle;
1386
+ const SUBTLE_API_SUPPORTED = CRYPTO_API_SUPPORTED && typeof subtle != UNDEFINED_TYPE;
1387
+ const IMPORT_KEY_SUPPORTED = CRYPTO_API_SUPPORTED && SUBTLE_API_SUPPORTED && typeof subtle.importKey == FUNCTION_TYPE;
1388
+ const DERIVE_BITS_SUPPORTED = CRYPTO_API_SUPPORTED && SUBTLE_API_SUPPORTED && typeof subtle.deriveBits == FUNCTION_TYPE;
1389
+ const codecBytes = codec.bytes;
1390
+ const Aes = cipher.aes;
1391
+ const CtrGladman = mode.ctrGladman;
1392
+ const HmacSha1 = misc.hmacSha1;
1470
1393
 
1471
- class DeflateStream extends TransformStream {
1394
+ class AESDecryptionStream extends TransformStream {
1472
1395
 
1473
- constructor(options, { chunkSize, CompressionStream, CompressionStreamNative }) {
1474
- super({});
1475
- const { compressed, encrypted, useCompressionStream, password, passwordVerification, encryptionStrength, zipCrypto, signed, level } = options;
1476
- const stream = this;
1477
- let crc32Stream, encryptionStream;
1478
- let readable = filterEmptyChunks(super.readable);
1479
- if ((!encrypted || zipCrypto) && signed) {
1480
- [readable, crc32Stream] = readable.tee();
1481
- crc32Stream = pipeThrough(crc32Stream, new Crc32Stream());
1482
- }
1483
- if (compressed) {
1484
- readable = pipeThroughCommpressionStream(readable, useCompressionStream, { level, chunkSize }, CompressionStreamNative, CompressionStream);
1485
- }
1486
- if (encrypted) {
1487
- if (zipCrypto) {
1488
- readable = pipeThrough(readable, new ZipCryptoEncryptionStream(password, passwordVerification));
1489
- } else {
1490
- encryptionStream = new AESEncryptionStream(password, encryptionStrength);
1491
- readable = pipeThrough(readable, encryptionStream);
1492
- }
1493
- }
1494
- setReadable(stream, readable, async () => {
1495
- let signature;
1496
- if (encrypted && !zipCrypto) {
1497
- signature = encryptionStream.signature;
1498
- }
1499
- if ((!encrypted || zipCrypto) && signed) {
1500
- signature = await crc32Stream.getReader().read();
1501
- signature = new DataView(signature.value.buffer).getUint32(0);
1396
+ constructor(password, signed, strength) {
1397
+ super({
1398
+ start() {
1399
+ Object.assign(this, {
1400
+ ready: new Promise(resolve => this.resolveReady = resolve),
1401
+ password,
1402
+ signed,
1403
+ strength: strength - 1,
1404
+ pending: new Uint8Array()
1405
+ });
1406
+ },
1407
+ async transform(chunk, controller) {
1408
+ const aesCrypto = this;
1409
+ const {
1410
+ password,
1411
+ strength,
1412
+ resolveReady,
1413
+ ready
1414
+ } = aesCrypto;
1415
+ if (password) {
1416
+ await createDecryptionKeys(aesCrypto, strength, password, subarray(chunk, 0, SALT_LENGTH[strength] + 2));
1417
+ chunk = subarray(chunk, SALT_LENGTH[strength] + 2);
1418
+ resolveReady();
1419
+ } else {
1420
+ await ready;
1421
+ }
1422
+ const output = new Uint8Array(chunk.length - SIGNATURE_LENGTH - ((chunk.length - SIGNATURE_LENGTH) % BLOCK_LENGTH));
1423
+ controller.enqueue(append(aesCrypto, chunk, output, 0, SIGNATURE_LENGTH, true));
1424
+ },
1425
+ async flush(controller) {
1426
+ const {
1427
+ signed,
1428
+ ctr,
1429
+ hmac,
1430
+ pending,
1431
+ ready
1432
+ } = this;
1433
+ await ready;
1434
+ const chunkToDecrypt = subarray(pending, 0, pending.length - SIGNATURE_LENGTH);
1435
+ const originalSignature = subarray(pending, pending.length - SIGNATURE_LENGTH);
1436
+ let decryptedChunkArray = new Uint8Array();
1437
+ if (chunkToDecrypt.length) {
1438
+ const encryptedChunk = toBits(codecBytes, chunkToDecrypt);
1439
+ hmac.update(encryptedChunk);
1440
+ const decryptedChunk = ctr.update(encryptedChunk);
1441
+ decryptedChunkArray = fromBits(codecBytes, decryptedChunk);
1442
+ }
1443
+ if (signed) {
1444
+ const signature = subarray(fromBits(codecBytes, hmac.digest()), 0, SIGNATURE_LENGTH);
1445
+ for (let indexSignature = 0; indexSignature < SIGNATURE_LENGTH; indexSignature++) {
1446
+ if (signature[indexSignature] != originalSignature[indexSignature]) {
1447
+ throw new Error(ERR_INVALID_SIGNATURE);
1448
+ }
1449
+ }
1450
+ }
1451
+ controller.enqueue(decryptedChunkArray);
1502
1452
  }
1503
- stream.signature = signature;
1504
1453
  });
1505
1454
  }
1506
1455
  }
1507
1456
 
1508
- class InflateStream extends TransformStream {
1457
+ class AESEncryptionStream extends TransformStream {
1509
1458
 
1510
- constructor(options, { chunkSize, DecompressionStream, DecompressionStreamNative }) {
1511
- super({});
1512
- const { zipCrypto, encrypted, password, passwordVerification, signed, signature, encryptionStrength, compressed, useCompressionStream } = options;
1513
- let crc32Stream, decryptionStream;
1514
- let readable = filterEmptyChunks(super.readable);
1515
- if (encrypted) {
1516
- if (zipCrypto) {
1517
- readable = pipeThrough(readable, new ZipCryptoDecryptionStream(password, passwordVerification));
1518
- } else {
1519
- decryptionStream = new AESDecryptionStream(password, signed, encryptionStrength);
1520
- readable = pipeThrough(readable, decryptionStream);
1521
- }
1522
- }
1523
- if (compressed) {
1524
- readable = pipeThroughCommpressionStream(readable, useCompressionStream, { chunkSize }, DecompressionStreamNative, DecompressionStream);
1525
- }
1526
- if ((!encrypted || zipCrypto) && signed) {
1527
- [readable, crc32Stream] = readable.tee();
1528
- crc32Stream = pipeThrough(crc32Stream, new Crc32Stream());
1529
- }
1530
- setReadable(this, readable, async () => {
1531
- if ((!encrypted || zipCrypto) && signed) {
1532
- const streamSignature = await crc32Stream.getReader().read();
1533
- const dataViewSignature = new DataView(streamSignature.value.buffer);
1534
- if (signature != dataViewSignature.getUint32(0, false)) {
1535
- throw new Error(ERR_INVALID_SIGNATURE);
1459
+ constructor(password, strength) {
1460
+ // deno-lint-ignore prefer-const
1461
+ let stream;
1462
+ super({
1463
+ start() {
1464
+ Object.assign(this, {
1465
+ ready: new Promise(resolve => this.resolveReady = resolve),
1466
+ password,
1467
+ strength: strength - 1,
1468
+ pending: new Uint8Array()
1469
+ });
1470
+ },
1471
+ async transform(chunk, controller) {
1472
+ const aesCrypto = this;
1473
+ const {
1474
+ password,
1475
+ strength,
1476
+ resolveReady,
1477
+ ready
1478
+ } = aesCrypto;
1479
+ let preamble = new Uint8Array();
1480
+ if (password) {
1481
+ preamble = await createEncryptionKeys(aesCrypto, strength, password);
1482
+ resolveReady();
1483
+ } else {
1484
+ await ready;
1485
+ }
1486
+ const output = new Uint8Array(preamble.length + chunk.length - (chunk.length % BLOCK_LENGTH));
1487
+ output.set(preamble, 0);
1488
+ controller.enqueue(append(aesCrypto, chunk, output, preamble.length, 0));
1489
+ },
1490
+ async flush(controller) {
1491
+ const {
1492
+ ctr,
1493
+ hmac,
1494
+ pending,
1495
+ ready
1496
+ } = this;
1497
+ await ready;
1498
+ let encryptedChunkArray = new Uint8Array();
1499
+ if (pending.length) {
1500
+ const encryptedChunk = ctr.update(toBits(codecBytes, pending));
1501
+ hmac.update(encryptedChunk);
1502
+ encryptedChunkArray = fromBits(codecBytes, encryptedChunk);
1536
1503
  }
1504
+ stream.signature = fromBits(codecBytes, hmac.digest()).slice(0, SIGNATURE_LENGTH);
1505
+ controller.enqueue(concat(encryptedChunkArray, stream.signature));
1537
1506
  }
1538
1507
  });
1508
+ stream = this;
1539
1509
  }
1540
1510
  }
1541
1511
 
1542
- function filterEmptyChunks(readable) {
1543
- return pipeThrough(readable, new TransformStream({
1544
- transform(chunk, controller) {
1545
- if (chunk && chunk.length) {
1546
- controller.enqueue(chunk);
1547
- }
1512
+ function append(aesCrypto, input, output, paddingStart, paddingEnd, verifySignature) {
1513
+ const {
1514
+ ctr,
1515
+ hmac,
1516
+ pending
1517
+ } = aesCrypto;
1518
+ const inputLength = input.length - paddingEnd;
1519
+ if (pending.length) {
1520
+ input = concat(pending, input);
1521
+ output = expand(output, inputLength - (inputLength % BLOCK_LENGTH));
1522
+ }
1523
+ let offset;
1524
+ for (offset = 0; offset <= inputLength - BLOCK_LENGTH; offset += BLOCK_LENGTH) {
1525
+ const inputChunk = toBits(codecBytes, subarray(input, offset, offset + BLOCK_LENGTH));
1526
+ if (verifySignature) {
1527
+ hmac.update(inputChunk);
1548
1528
  }
1549
- }));
1529
+ const outputChunk = ctr.update(inputChunk);
1530
+ if (!verifySignature) {
1531
+ hmac.update(outputChunk);
1532
+ }
1533
+ output.set(fromBits(codecBytes, outputChunk), offset + paddingStart);
1534
+ }
1535
+ aesCrypto.pending = subarray(input, offset);
1536
+ return output;
1550
1537
  }
1551
1538
 
1552
- function setReadable(stream, readable, flush) {
1553
- readable = pipeThrough(readable, new TransformStream({ flush }));
1554
- Object.defineProperty(stream, "readable", {
1555
- get() {
1556
- return readable;
1557
- }
1539
+ async function createDecryptionKeys(decrypt, strength, password, preamble) {
1540
+ const passwordVerificationKey = await createKeys$1(decrypt, strength, password, subarray(preamble, 0, SALT_LENGTH[strength]));
1541
+ const passwordVerification = subarray(preamble, SALT_LENGTH[strength]);
1542
+ if (passwordVerificationKey[0] != passwordVerification[0] || passwordVerificationKey[1] != passwordVerification[1]) {
1543
+ throw new Error(ERR_INVALID_PASSWORD);
1544
+ }
1545
+ }
1546
+
1547
+ async function createEncryptionKeys(encrypt, strength, password) {
1548
+ const salt = getRandomValues(new Uint8Array(SALT_LENGTH[strength]));
1549
+ const passwordVerification = await createKeys$1(encrypt, strength, password, salt);
1550
+ return concat(salt, passwordVerification);
1551
+ }
1552
+
1553
+ async function createKeys$1(aesCrypto, strength, password, salt) {
1554
+ aesCrypto.password = null;
1555
+ const encodedPassword = encodeText(password);
1556
+ const baseKey = await importKey(RAW_FORMAT, encodedPassword, BASE_KEY_ALGORITHM, false, DERIVED_BITS_USAGE);
1557
+ const derivedBits = await deriveBits(Object.assign({ salt }, DERIVED_BITS_ALGORITHM), baseKey, 8 * ((KEY_LENGTH[strength] * 2) + 2));
1558
+ const compositeKey = new Uint8Array(derivedBits);
1559
+ const key = toBits(codecBytes, subarray(compositeKey, 0, KEY_LENGTH[strength]));
1560
+ const authentication = toBits(codecBytes, subarray(compositeKey, KEY_LENGTH[strength], KEY_LENGTH[strength] * 2));
1561
+ const passwordVerification = subarray(compositeKey, KEY_LENGTH[strength] * 2);
1562
+ Object.assign(aesCrypto, {
1563
+ keys: {
1564
+ key,
1565
+ authentication,
1566
+ passwordVerification
1567
+ },
1568
+ ctr: new CtrGladman(new Aes(key), Array.from(COUNTER_DEFAULT_VALUE)),
1569
+ hmac: new HmacSha1(authentication)
1558
1570
  });
1571
+ return passwordVerification;
1559
1572
  }
1560
1573
 
1561
- function pipeThroughCommpressionStream(readable, useCompressionStream, options, CodecStreamNative, CodecStream) {
1562
- try {
1563
- const CompressionStream = useCompressionStream && CodecStreamNative ? CodecStreamNative : CodecStream;
1564
- readable = pipeThrough(readable, new CompressionStream(COMPRESSION_FORMAT, options));
1565
- } catch (error) {
1566
- if (useCompressionStream) {
1567
- readable = pipeThrough(readable, new CodecStream(COMPRESSION_FORMAT, options));
1568
- } else {
1569
- throw error;
1570
- }
1574
+ function importKey(format, password, algorithm, extractable, keyUsages) {
1575
+ if (IMPORT_KEY_SUPPORTED) {
1576
+ return subtle.importKey(format, password, algorithm, extractable, keyUsages);
1577
+ } else {
1578
+ return misc.importKey(password);
1571
1579
  }
1572
- return readable;
1573
1580
  }
1574
1581
 
1575
- function pipeThrough(readable, transformStream) {
1576
- return readable.pipeThrough(transformStream);
1582
+ async function deriveBits(algorithm, baseKey, length) {
1583
+ if (DERIVE_BITS_SUPPORTED) {
1584
+ return await subtle.deriveBits(algorithm, baseKey, length);
1585
+ } else {
1586
+ return misc.pbkdf2(baseKey, algorithm.salt, DERIVED_BITS_ALGORITHM.iterations, length);
1587
+ }
1588
+ }
1589
+
1590
+ function concat(leftArray, rightArray) {
1591
+ let array = leftArray;
1592
+ if (leftArray.length + rightArray.length) {
1593
+ array = new Uint8Array(leftArray.length + rightArray.length);
1594
+ array.set(leftArray, 0);
1595
+ array.set(rightArray, leftArray.length);
1596
+ }
1597
+ return array;
1598
+ }
1599
+
1600
+ function expand(inputArray, length) {
1601
+ if (length && length > inputArray.length) {
1602
+ const array = inputArray;
1603
+ inputArray = new Uint8Array(length);
1604
+ inputArray.set(array, 0);
1605
+ }
1606
+ return inputArray;
1607
+ }
1608
+
1609
+ function subarray(array, begin, end) {
1610
+ return array.subarray(begin, end);
1611
+ }
1612
+
1613
+ function fromBits(codecBytes, chunk) {
1614
+ return codecBytes.fromBits(chunk);
1615
+ }
1616
+ function toBits(codecBytes, chunk) {
1617
+ return codecBytes.toBits(chunk);
1577
1618
  }
1578
1619
 
1579
1620
  /*
@@ -1604,121 +1645,119 @@
1604
1645
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1605
1646
  */
1606
1647
 
1607
- /*
1608
- * This program is based on JZlib 1.0.2 ymnk, JCraft,Inc.
1609
- * JZlib is based on zlib-1.1.3, so all credit should go authors
1610
- * Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
1611
- * and contributors of zlib.
1612
- */
1613
-
1614
- /* global TransformStream */
1648
+ const HEADER_LENGTH = 12;
1615
1649
 
1616
- class CodecStream extends TransformStream {
1650
+ class ZipCryptoDecryptionStream extends TransformStream {
1617
1651
 
1618
- constructor(Codec, options) {
1619
- const codec = new Codec(options);
1652
+ constructor(password, passwordVerification) {
1620
1653
  super({
1621
- transform(chunk, controller) {
1622
- controller.enqueue(codec.append(chunk));
1654
+ start() {
1655
+ Object.assign(this, {
1656
+ password,
1657
+ passwordVerification
1658
+ });
1659
+ createKeys(this, password);
1623
1660
  },
1624
- flush(controller) {
1625
- const chunk = codec.flush();
1626
- if (chunk) {
1627
- controller.enqueue(chunk);
1661
+ transform(chunk, controller) {
1662
+ const zipCrypto = this;
1663
+ if (zipCrypto.password) {
1664
+ const decryptedHeader = decrypt(zipCrypto, chunk.subarray(0, HEADER_LENGTH));
1665
+ zipCrypto.password = null;
1666
+ if (decryptedHeader[HEADER_LENGTH - 1] != zipCrypto.passwordVerification) {
1667
+ throw new Error(ERR_INVALID_PASSWORD);
1668
+ }
1669
+ chunk = chunk.subarray(HEADER_LENGTH);
1628
1670
  }
1671
+ controller.enqueue(decrypt(zipCrypto, chunk));
1629
1672
  }
1630
1673
  });
1631
1674
  }
1632
1675
  }
1633
1676
 
1634
- /*
1635
- Copyright (c) 2022 Gildas Lormeau. All rights reserved.
1636
-
1637
- Redistribution and use in source and binary forms, with or without
1638
- modification, are permitted provided that the following conditions are met:
1639
-
1640
- 1. Redistributions of source code must retain the above copyright notice,
1641
- this list of conditions and the following disclaimer.
1642
-
1643
- 2. Redistributions in binary form must reproduce the above copyright
1644
- notice, this list of conditions and the following disclaimer in
1645
- the documentation and/or other materials provided with the distribution.
1646
-
1647
- 3. The names of the authors may not be used to endorse or promote products
1648
- derived from this software without specific prior written permission.
1677
+ class ZipCryptoEncryptionStream extends TransformStream {
1649
1678
 
1650
- THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
1651
- INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
1652
- FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
1653
- INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
1654
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1655
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
1656
- OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1657
- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
1658
- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
1659
- EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1660
- */
1679
+ constructor(password, passwordVerification) {
1680
+ super({
1681
+ start() {
1682
+ Object.assign(this, {
1683
+ password,
1684
+ passwordVerification
1685
+ });
1686
+ createKeys(this, password);
1687
+ },
1688
+ transform(chunk, controller) {
1689
+ const zipCrypto = this;
1690
+ let output;
1691
+ let offset;
1692
+ if (zipCrypto.password) {
1693
+ zipCrypto.password = null;
1694
+ const header = getRandomValues(new Uint8Array(HEADER_LENGTH));
1695
+ header[HEADER_LENGTH - 1] = zipCrypto.passwordVerification;
1696
+ output = new Uint8Array(chunk.length + header.length);
1697
+ output.set(encrypt(zipCrypto, header), 0);
1698
+ offset = HEADER_LENGTH;
1699
+ } else {
1700
+ output = new Uint8Array(chunk.length);
1701
+ offset = 0;
1702
+ }
1703
+ output.set(encrypt(zipCrypto, chunk), offset);
1704
+ controller.enqueue(output);
1705
+ }
1706
+ });
1707
+ }
1708
+ }
1661
1709
 
1662
- const CODEC_DEFLATE = "deflate";
1663
- const CODEC_INFLATE = "inflate";
1664
- const MESSAGE_EVENT_TYPE = "message";
1665
- const MESSAGE_START = "start";
1666
- const MESSAGE_PULL = "pull";
1667
- const MESSAGE_DATA = "data";
1668
- const MESSAGE_ACK_DATA = "ack";
1669
- const MESSAGE_CLOSE = "close";
1710
+ function decrypt(target, input) {
1711
+ const output = new Uint8Array(input.length);
1712
+ for (let index = 0; index < input.length; index++) {
1713
+ output[index] = getByte(target) ^ input[index];
1714
+ updateKeys(target, output[index]);
1715
+ }
1716
+ return output;
1717
+ }
1670
1718
 
1671
- class Codec {
1719
+ function encrypt(target, input) {
1720
+ const output = new Uint8Array(input.length);
1721
+ for (let index = 0; index < input.length; index++) {
1722
+ output[index] = getByte(target) ^ input[index];
1723
+ updateKeys(target, input[index]);
1724
+ }
1725
+ return output;
1726
+ }
1672
1727
 
1673
- constructor(readable, writable, options, config) {
1674
- const { codecType } = options;
1675
- let CodecStream;
1676
- if (codecType.startsWith(CODEC_DEFLATE)) {
1677
- CodecStream = DeflateStream;
1678
- } else if (codecType.startsWith(CODEC_INFLATE)) {
1679
- CodecStream = InflateStream;
1680
- }
1681
- Object.assign(this, {
1682
- CodecStream,
1683
- readable,
1684
- writable,
1685
- options,
1686
- config
1687
- });
1728
+ function createKeys(target, password) {
1729
+ const keys = [0x12345678, 0x23456789, 0x34567890];
1730
+ Object.assign(target, {
1731
+ keys,
1732
+ crcKey0: new Crc32(keys[0]),
1733
+ crcKey2: new Crc32(keys[2]),
1734
+ });
1735
+ for (let index = 0; index < password.length; index++) {
1736
+ updateKeys(target, password.charCodeAt(index));
1688
1737
  }
1738
+ }
1689
1739
 
1690
- async run() {
1691
- const {
1692
- CodecStream,
1693
- readable,
1694
- writable,
1695
- options,
1696
- config
1697
- } = this;
1698
- const stream = new CodecStream(options, config);
1699
- let size = 0;
1700
- await readable
1701
- .pipeThrough(stream)
1702
- .pipeThrough(new TransformStream({
1703
- transform(chunk, controller) {
1704
- if (chunk && chunk.length) {
1705
- size += chunk.length;
1706
- controller.enqueue(chunk);
1707
- }
1708
- }
1709
- }))
1710
- .pipeTo(writable, { preventClose: true, preventAbort: true });
1711
- const { signature } = stream;
1712
- return { size, signature };
1713
- }
1740
+ function updateKeys(target, byte) {
1741
+ let [key0, key1, key2] = target.keys;
1742
+ target.crcKey0.append([byte]);
1743
+ key0 = ~target.crcKey0.get();
1744
+ key1 = getInt32(Math.imul(getInt32(key1 + getInt8(key0)), 134775813) + 1);
1745
+ target.crcKey2.append([key1 >>> 24]);
1746
+ key2 = ~target.crcKey2.get();
1747
+ target.keys = [key0, key1, key2];
1714
1748
  }
1715
1749
 
1716
- function getCodecStreamClass(Codec) {
1717
- return class extends CodecStream {
1718
- constructor(algorithm, options) {
1719
- super(Codec, options);
1720
- }
1721
- };
1750
+ function getByte(target) {
1751
+ const temp = target.keys[2] | 2;
1752
+ return getInt8(Math.imul(temp, (temp ^ 1)) >>> 8);
1753
+ }
1754
+
1755
+ function getInt8(number) {
1756
+ return number & 0xFF;
1757
+ }
1758
+
1759
+ function getInt32(number) {
1760
+ return number & 0xFFFFFFFF;
1722
1761
  }
1723
1762
 
1724
1763
  /*
@@ -1749,94 +1788,115 @@
1749
1788
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1750
1789
  */
1751
1790
 
1752
- const MINIMUM_CHUNK_SIZE = 64;
1753
- const UNDEFINED_VALUE = undefined;
1754
- let maxWorkers = 2;
1755
- try {
1756
- if (typeof navigator != "undefined" && navigator.hardwareConcurrency) {
1757
- maxWorkers = navigator.hardwareConcurrency;
1758
- }
1759
- } catch (_error) {
1760
- // ignored
1761
- }
1762
- const DEFAULT_CONFIGURATION = {
1763
- chunkSize: 512 * 1024,
1764
- maxWorkers,
1765
- terminateWorkerTimeout: 5000,
1766
- useWebWorkers: true,
1767
- useCompressionStream: true,
1768
- workerScripts: undefined,
1769
- CompressionStreamNative: typeof CompressionStream != "undefined" && CompressionStream,
1770
- DecompressionStreamNative: typeof DecompressionStream != "undefined" && DecompressionStream
1771
- };
1791
+ const COMPRESSION_FORMAT = "deflate-raw";
1772
1792
 
1773
- const config = Object.assign({}, DEFAULT_CONFIGURATION);
1793
+ class DeflateStream extends TransformStream {
1774
1794
 
1775
- function getConfiguration() {
1776
- return config;
1795
+ constructor(options, { chunkSize, CompressionStream, CompressionStreamNative }) {
1796
+ super({});
1797
+ const { compressed, encrypted, useCompressionStream, password, passwordVerification, encryptionStrength, zipCrypto, signed, level } = options;
1798
+ const stream = this;
1799
+ let crc32Stream, encryptionStream;
1800
+ let readable = filterEmptyChunks(super.readable);
1801
+ if ((!encrypted || zipCrypto) && signed) {
1802
+ [readable, crc32Stream] = readable.tee();
1803
+ crc32Stream = pipeThrough(crc32Stream, new Crc32Stream());
1804
+ }
1805
+ if (compressed) {
1806
+ readable = pipeThroughCommpressionStream(readable, useCompressionStream, { level, chunkSize }, CompressionStreamNative, CompressionStream);
1807
+ }
1808
+ if (encrypted) {
1809
+ if (zipCrypto) {
1810
+ readable = pipeThrough(readable, new ZipCryptoEncryptionStream(password, passwordVerification));
1811
+ } else {
1812
+ encryptionStream = new AESEncryptionStream(password, encryptionStrength);
1813
+ readable = pipeThrough(readable, encryptionStream);
1814
+ }
1815
+ }
1816
+ setReadable(stream, readable, async () => {
1817
+ let signature;
1818
+ if (encrypted && !zipCrypto) {
1819
+ signature = encryptionStream.signature;
1820
+ }
1821
+ if ((!encrypted || zipCrypto) && signed) {
1822
+ signature = await crc32Stream.getReader().read();
1823
+ signature = new DataView(signature.value.buffer).getUint32(0);
1824
+ }
1825
+ stream.signature = signature;
1826
+ });
1827
+ }
1777
1828
  }
1778
1829
 
1779
- function getChunkSize(config) {
1780
- return Math.max(config.chunkSize, MINIMUM_CHUNK_SIZE);
1781
- }
1830
+ class InflateStream extends TransformStream {
1782
1831
 
1783
- function configure(configuration) {
1784
- const {
1785
- baseURL,
1786
- chunkSize,
1787
- maxWorkers,
1788
- terminateWorkerTimeout,
1789
- useCompressionStream,
1790
- useWebWorkers,
1791
- Deflate,
1792
- Inflate,
1793
- CompressionStream,
1794
- DecompressionStream,
1795
- workerScripts
1796
- } = configuration;
1797
- setIfDefined("baseURL", baseURL);
1798
- setIfDefined("chunkSize", chunkSize);
1799
- setIfDefined("maxWorkers", maxWorkers);
1800
- setIfDefined("terminateWorkerTimeout", terminateWorkerTimeout);
1801
- setIfDefined("useCompressionStream", useCompressionStream);
1802
- setIfDefined("useWebWorkers", useWebWorkers);
1803
- if (Deflate) {
1804
- config.CompressionStream = getCodecStreamClass(Deflate);
1805
- }
1806
- if (Inflate) {
1807
- config.DecompressionStream = getCodecStreamClass(Inflate);
1808
- }
1809
- setIfDefined("CompressionStream", CompressionStream);
1810
- setIfDefined("DecompressionStream", DecompressionStream);
1811
- if (workerScripts !== UNDEFINED_VALUE) {
1812
- const { deflate, inflate } = workerScripts;
1813
- if (deflate || inflate) {
1814
- if (!config.workerScripts) {
1815
- config.workerScripts = {};
1832
+ constructor(options, { chunkSize, DecompressionStream, DecompressionStreamNative }) {
1833
+ super({});
1834
+ const { zipCrypto, encrypted, password, passwordVerification, signed, signature, encryptionStrength, compressed, useCompressionStream } = options;
1835
+ let crc32Stream, decryptionStream;
1836
+ let readable = filterEmptyChunks(super.readable);
1837
+ if (encrypted) {
1838
+ if (zipCrypto) {
1839
+ readable = pipeThrough(readable, new ZipCryptoDecryptionStream(password, passwordVerification));
1840
+ } else {
1841
+ decryptionStream = new AESDecryptionStream(password, signed, encryptionStrength);
1842
+ readable = pipeThrough(readable, decryptionStream);
1816
1843
  }
1817
1844
  }
1818
- if (deflate) {
1819
- if (!Array.isArray(deflate)) {
1820
- throw new Error("workerScripts.deflate must be an array");
1821
- }
1822
- config.workerScripts.deflate = deflate;
1845
+ if (compressed) {
1846
+ readable = pipeThroughCommpressionStream(readable, useCompressionStream, { chunkSize }, DecompressionStreamNative, DecompressionStream);
1823
1847
  }
1824
- if (inflate) {
1825
- if (!Array.isArray(inflate)) {
1826
- throw new Error("workerScripts.inflate must be an array");
1827
- }
1828
- config.workerScripts.inflate = inflate;
1848
+ if ((!encrypted || zipCrypto) && signed) {
1849
+ [readable, crc32Stream] = readable.tee();
1850
+ crc32Stream = pipeThrough(crc32Stream, new Crc32Stream());
1829
1851
  }
1852
+ setReadable(this, readable, async () => {
1853
+ if ((!encrypted || zipCrypto) && signed) {
1854
+ const streamSignature = await crc32Stream.getReader().read();
1855
+ const dataViewSignature = new DataView(streamSignature.value.buffer);
1856
+ if (signature != dataViewSignature.getUint32(0, false)) {
1857
+ throw new Error(ERR_INVALID_SIGNATURE);
1858
+ }
1859
+ }
1860
+ });
1830
1861
  }
1831
1862
  }
1832
1863
 
1833
- function setIfDefined(propertyName, propertyValue) {
1834
- if (propertyValue !== UNDEFINED_VALUE) {
1835
- config[propertyName] = propertyValue;
1864
+ function filterEmptyChunks(readable) {
1865
+ return pipeThrough(readable, new TransformStream({
1866
+ transform(chunk, controller) {
1867
+ if (chunk && chunk.length) {
1868
+ controller.enqueue(chunk);
1869
+ }
1870
+ }
1871
+ }));
1872
+ }
1873
+
1874
+ function setReadable(stream, readable, flush) {
1875
+ readable = pipeThrough(readable, new TransformStream({ flush }));
1876
+ Object.defineProperty(stream, "readable", {
1877
+ get() {
1878
+ return readable;
1879
+ }
1880
+ });
1881
+ }
1882
+
1883
+ function pipeThroughCommpressionStream(readable, useCompressionStream, options, CodecStreamNative, CodecStream) {
1884
+ try {
1885
+ const CompressionStream = useCompressionStream && CodecStreamNative ? CodecStreamNative : CodecStream;
1886
+ readable = pipeThrough(readable, new CompressionStream(COMPRESSION_FORMAT, options));
1887
+ } catch (error) {
1888
+ if (useCompressionStream) {
1889
+ readable = pipeThrough(readable, new CodecStream(COMPRESSION_FORMAT, options));
1890
+ } else {
1891
+ throw error;
1892
+ }
1836
1893
  }
1894
+ return readable;
1837
1895
  }
1838
1896
 
1839
- function e(e){const t=()=>URL.createObjectURL(new Blob(['const{Array:e,Object:t,Math:n,Error:r,Uint8Array:s,Uint16Array:i,Uint32Array:o,Int32Array:c,Map:f,DataView:a,Promise:l,TextEncoder:u,crypto:w,postMessage:h,TransformStream:d,ReadableStream:p,WritableStream:y,CompressionStream:m,DecompressionStream:b}=self,g=[];for(let e=0;256>e;e++){let t=e;for(let e=0;8>e;e++)1&t?t=t>>>1^3988292384:t>>>=1;g[e]=t}class k{constructor(e){this.t=e||-1}append(e){let t=0|this.t;for(let n=0,r=0|e.length;r>n;n++)t=t>>>8^g[255&(t^e[n])];this.t=t}get(){return~this.t}}class v extends d{constructor(){const e=new k;super({transform(t){e.append(t)},flush(t){const n=new s(4);new a(n.buffer).setUint32(0,e.get()),t.enqueue(n)}})}}const S={concat(e,t){if(0===e.length||0===t.length)return e.concat(t);const n=e[e.length-1],r=S.i(n);return 32===r?e.concat(t):S.o(t,r,0|n,e.slice(0,e.length-1))},l(e){const t=e.length;if(0===t)return 0;const n=e[t-1];return 32*(t-1)+S.i(n)},u(e,t){if(32*e.length<t)return e;const r=(e=e.slice(0,n.ceil(t/32))).length;return t&=31,r>0&&t&&(e[r-1]=S.h(t,e[r-1]&2147483648>>t-1,1)),e},h:(e,t,n)=>32===e?t:(n?0|t:t<<32-e)+1099511627776*e,i:e=>n.round(e/1099511627776)||32,o(e,t,n,r){for(void 0===r&&(r=[]);t>=32;t-=32)r.push(n),n=0;if(0===t)return r.concat(e);for(let s=0;s<e.length;s++)r.push(n|e[s]>>>t),n=e[s]<<32-t;const s=e.length?e[e.length-1]:0,i=S.i(s);return r.push(S.h(t+i&31,t+i>32?n:r.pop(),1)),r}},C={p:{m(e){const t=S.l(e)/8,n=new s(t);let r;for(let s=0;t>s;s++)0==(3&s)&&(r=e[s/4]),n[s]=r>>>24,r<<=8;return n},g(e){const t=[];let n,r=0;for(n=0;n<e.length;n++)r=r<<8|e[n],3==(3&n)&&(t.push(r),r=0);return 3&n&&t.push(S.h(8*(3&n),r)),t}}},z={k:function(e){e?(this.v=e.v.slice(0),this.S=e.S.slice(0),this.C=e.C):this.reset()}};z.k.prototype={blockSize:512,reset(){const e=this;return e.v=this._.slice(0),e.S=[],e.C=0,e},update(e){const t=this;"string"==typeof e&&(e=C.I.g(e));const n=t.S=S.concat(t.S,e),s=t.C,i=t.C=s+S.l(e);if(i>9007199254740991)throw new r("Cannot hash more than 2^53 - 1 bits");const c=new o(n);let f=0;for(let e=t.blockSize+s-(t.blockSize+s&t.blockSize-1);i>=e;e+=t.blockSize)t.A(c.subarray(16*f,16*(f+1))),f+=1;return n.splice(0,16*f),t},D(){const e=this;let t=e.S;const r=e.v;t=S.concat(t,[S.h(1,1)]);for(let e=t.length+2;15&e;e++)t.push(0);for(t.push(n.floor(e.C/4294967296)),t.push(0|e.C);t.length;)e.A(t.splice(0,16));return e.reset(),r},_:[1732584193,4023233417,2562383102,271733878,3285377520],V:[1518500249,1859775393,2400959708,3395469782],R:(e,t,n,r)=>e>19?e>39?e>59?e>79?void 0:t^n^r:t&n|t&r|n&r:t^n^r:t&n|~t&r,B:(e,t)=>t<<e|t>>>32-e,A(t){const r=this,s=r.v,i=e(80);for(let e=0;16>e;e++)i[e]=t[e];let o=s[0],c=s[1],f=s[2],a=s[3],l=s[4];for(let e=0;79>=e;e++){16>e||(i[e]=r.B(1,i[e-3]^i[e-8]^i[e-14]^i[e-16]));const t=r.B(5,o)+r.R(e,c,f,a)+l+i[e]+r.V[n.floor(e/20)]|0;l=a,a=f,f=r.B(30,c),c=o,o=t}s[0]=s[0]+o|0,s[1]=s[1]+c|0,s[2]=s[2]+f|0,s[3]=s[3]+a|0,s[4]=s[4]+l|0}};const _={getRandomValues(e){const t=new o(e.buffer),r=e=>{let t=987654321;const r=4294967295;return()=>(t=36969*(65535&t)+(t>>16)&r,(((t<<16)+(e=18e3*(65535&e)+(e>>16)&r)&r)/4294967296+.5)*(n.random()>.5?1:-1))};for(let s,i=0;i<e.length;i+=4){const e=r(4294967296*(s||n.random()));s=987654071*e(),t[i/4]=4294967296*e()|0}return e}},I={importKey:e=>new I.M(C.p.g(e)),K(e,t,n,s){if(n=n||1e4,0>s||0>n)throw new r("invalid params to pbkdf2");const i=1+(s>>5)<<2;let o,c,f,l,u;const w=new ArrayBuffer(i),h=new a(w);let d=0;const p=S;for(t=C.p.g(t),u=1;(i||1)>d;u++){for(o=c=e.encrypt(p.concat(t,[u])),f=1;n>f;f++)for(c=e.encrypt(c),l=0;l<c.length;l++)o[l]^=c[l];for(f=0;(i||1)>d&&f<o.length;f++)h.setInt32(d,o[f]),d+=4}return w.slice(0,s/8)},M:class{constructor(e){const t=this,n=t.N=z.k,r=[[],[]],s=n.prototype.blockSize/32;t.P=[new n,new n],e.length>s&&(e=n.hash(e));for(let t=0;s>t;t++)r[0][t]=909522486^e[t],r[1][t]=1549556828^e[t];t.P[0].update(r[0]),t.P[1].update(r[1]),t.U=new n(t.P[0])}reset(){const e=this;e.U=new e.N(e.P[0]),e.T=!1}update(e){this.T=!0,this.U.update(e)}digest(){const e=this,t=e.U.D(),n=new e.N(e.P[1]).update(t).D();return e.reset(),n}encrypt(e){if(this.T)throw new r("encrypt on already updated hmac called!");return this.update(e),this.digest(e)}}},x=void 0!==w&&"function"==typeof w.getRandomValues;function A(e){return x?w.getRandomValues(e):_.getRandomValues(e)}const D={name:"PBKDF2"},V=t.assign({hash:{name:"HMAC"}},D),R=t.assign({iterations:1e3,hash:{name:"SHA-1"}},D),B=["deriveBits"],E=[8,12,16],M=[16,24,32],K=[0,0,0,0],N=void 0!==w,P=N&&w.subtle,U=N&&void 0!==P,T=N&&U&&"function"==typeof P.importKey,W=N&&U&&"function"==typeof P.deriveBits,H=C.p,L=class{constructor(e){const t=this;t.W=[[[],[],[],[],[]],[[],[],[],[],[]]],t.W[0][0][0]||t.H();const n=t.W[0][4],s=t.W[1],i=e.length;let o,c,f,a=1;if(4!==i&&6!==i&&8!==i)throw new r("invalid aes key size");for(t.V=[c=e.slice(0),f=[]],o=i;4*i+28>o;o++){let e=c[o-1];(o%i==0||8===i&&o%i==4)&&(e=n[e>>>24]<<24^n[e>>16&255]<<16^n[e>>8&255]<<8^n[255&e],o%i==0&&(e=e<<8^e>>>24^a<<24,a=a<<1^283*(a>>7))),c[o]=c[o-i]^e}for(let e=0;o;e++,o--){const t=c[3&e?o:o-4];f[e]=4>=o||4>e?t:s[0][n[t>>>24]]^s[1][n[t>>16&255]]^s[2][n[t>>8&255]]^s[3][n[255&t]]}}encrypt(e){return this.L(e,0)}decrypt(e){return this.L(e,1)}H(){const e=this.W[0],t=this.W[1],n=e[4],r=t[4],s=[],i=[];let o,c,f,a;for(let e=0;256>e;e++)i[(s[e]=e<<1^283*(e>>7))^e]=e;for(let l=o=0;!n[l];l^=c||1,o=i[o]||1){let i=o^o<<1^o<<2^o<<3^o<<4;i=i>>8^255&i^99,n[l]=i,r[i]=l,a=s[f=s[c=s[l]]];let u=16843009*a^65537*f^257*c^16843008*l,w=257*s[i]^16843008*i;for(let n=0;4>n;n++)e[n][l]=w=w<<24^w>>>8,t[n][i]=u=u<<24^u>>>8}for(let n=0;5>n;n++)e[n]=e[n].slice(0),t[n]=t[n].slice(0)}L(e,t){if(4!==e.length)throw new r("invalid aes block size");const n=this.V[t],s=n.length/4-2,i=[0,0,0,0],o=this.W[t],c=o[0],f=o[1],a=o[2],l=o[3],u=o[4];let w,h,d,p=e[0]^n[0],y=e[t?3:1]^n[1],m=e[2]^n[2],b=e[t?1:3]^n[3],g=4;for(let e=0;s>e;e++)w=c[p>>>24]^f[y>>16&255]^a[m>>8&255]^l[255&b]^n[g],h=c[y>>>24]^f[m>>16&255]^a[b>>8&255]^l[255&p]^n[g+1],d=c[m>>>24]^f[b>>16&255]^a[p>>8&255]^l[255&y]^n[g+2],b=c[b>>>24]^f[p>>16&255]^a[y>>8&255]^l[255&m]^n[g+3],g+=4,p=w,y=h,m=d;for(let e=0;4>e;e++)i[t?3&-e:e]=u[p>>>24]<<24^u[y>>16&255]<<16^u[m>>8&255]<<8^u[255&b]^n[g++],w=p,p=y,y=m,m=b,b=w;return i}},j=class{constructor(e,t){this.j=e,this.F=t,this.O=t}reset(){this.O=this.F}update(e){return this.q(this.j,e,this.O)}G(e){if(255==(e>>24&255)){let t=e>>16&255,n=e>>8&255,r=255&e;255===t?(t=0,255===n?(n=0,255===r?r=0:++r):++n):++t,e=0,e+=t<<16,e+=n<<8,e+=r}else e+=1<<24;return e}J(e){0===(e[0]=this.G(e[0]))&&(e[1]=this.G(e[1]))}q(e,t,n){let r;if(!(r=t.length))return[];const s=S.l(t);for(let s=0;r>s;s+=4){this.J(n);const r=e.encrypt(n);t[s]^=r[0],t[s+1]^=r[1],t[s+2]^=r[2],t[s+3]^=r[3]}return S.u(t,s)}},F=I.M;class O extends d{constructor(e,n,i){super({start(){t.assign(this,{ready:new l((e=>this.X=e)),password:e,signed:n,Y:i-1,pending:new s})},async transform(e,t){const n=this,{password:i,Y:o,X:c,ready:f}=n;i?(await(async(e,t,n,s)=>{const i=await J(e,t,n,X(s,0,E[t])),o=X(s,E[t]);if(i[0]!=o[0]||i[1]!=o[1])throw new r("Invalid password")})(n,o,i,X(e,0,E[o]+2)),e=X(e,E[o]+2),c()):await f;const a=new s(e.length-10-(e.length-10)%16);t.enqueue(G(n,e,a,0,10,!0))},async flush(e){const{signed:t,Z:n,$:i,pending:o,ready:c}=this;await c;const f=X(o,0,o.length-10),a=X(o,o.length-10);let l=new s;if(f.length){const e=Z(H,f);i.update(e);const t=n.update(e);l=Y(H,t)}if(t){const e=X(Y(H,i.digest()),0,10);for(let t=0;10>t;t++)if(e[t]!=a[t])throw new r("Invalid signature")}e.enqueue(l)}})}}class q extends d{constructor(e,n){let r;super({start(){t.assign(this,{ready:new l((e=>this.X=e)),password:e,Y:n-1,pending:new s})},async transform(e,t){const n=this,{password:r,Y:i,X:o,ready:c}=n;let f=new s;r?(f=await(async(e,t,n)=>{const r=A(new s(E[t]));return Q(r,await J(e,t,n,r))})(n,i,r),o()):await c;const a=new s(f.length+e.length-e.length%16);a.set(f,0),t.enqueue(G(n,e,a,f.length,0))},async flush(e){const{Z:t,$:n,pending:i,ready:o}=this;await o;let c=new s;if(i.length){const e=t.update(Z(H,i));n.update(e),c=Y(H,e)}r.signature=Y(H,n.digest()).slice(0,10),e.enqueue(Q(c,r.signature))}}),r=this}}function G(e,t,n,r,i,o){const{Z:c,$:f,pending:a}=e,l=t.length-i;let u;for(a.length&&(t=Q(a,t),n=((e,t)=>{if(t&&t>e.length){const n=e;(e=new s(t)).set(n,0)}return e})(n,l-l%16)),u=0;l-16>=u;u+=16){const e=Z(H,X(t,u,u+16));o&&f.update(e);const s=c.update(e);o||f.update(s),n.set(Y(H,s),u+r)}return e.pending=X(t,u),n}async function J(n,r,i,o){n.password=null;const c=(e=>{if(void 0===u){const t=new s((e=unescape(encodeURIComponent(e))).length);for(let n=0;n<t.length;n++)t[n]=e.charCodeAt(n);return t}return(new u).encode(e)})(i),f=await((e,t,n,r,s)=>T?P.importKey("raw",t,n,!1,s):I.importKey(t))(0,c,V,0,B),a=await(async(e,t,n)=>W?await P.deriveBits(e,t,n):I.K(t,e.salt,R.iterations,n))(t.assign({salt:o},R),f,8*(2*M[r]+2)),l=new s(a),w=Z(H,X(l,0,M[r])),h=Z(H,X(l,M[r],2*M[r])),d=X(l,2*M[r]);return t.assign(n,{keys:{key:w,ee:h,passwordVerification:d},Z:new j(new L(w),e.from(K)),$:new F(h)}),d}function Q(e,t){let n=e;return e.length+t.length&&(n=new s(e.length+t.length),n.set(e,0),n.set(t,e.length)),n}function X(e,t,n){return e.subarray(t,n)}function Y(e,t){return e.m(t)}function Z(e,t){return e.g(t)}class $ extends d{constructor(e,n){super({start(){t.assign(this,{password:e,passwordVerification:n}),re(this,e)},transform(e,t){const n=this;if(n.password){const t=te(n,e.subarray(0,12));if(n.password=null,t[11]!=n.passwordVerification)throw new r("Invalid password");e=e.subarray(12)}t.enqueue(te(n,e))}})}}class ee extends d{constructor(e,n){super({start(){t.assign(this,{password:e,passwordVerification:n}),re(this,e)},transform(e,t){const n=this;let r,i;if(n.password){n.password=null;const t=A(new s(12));t[11]=n.passwordVerification,r=new s(e.length+t.length),r.set(ne(n,t),0),i=12}else r=new s(e.length),i=0;r.set(ne(n,e),i),t.enqueue(r)}})}}function te(e,t){const n=new s(t.length);for(let r=0;r<t.length;r++)n[r]=ie(e)^t[r],se(e,n[r]);return n}function ne(e,t){const n=new s(t.length);for(let r=0;r<t.length;r++)n[r]=ie(e)^t[r],se(e,t[r]);return n}function re(e,n){const r=[305419896,591751049,878082192];t.assign(e,{keys:r,te:new k(r[0]),ne:new k(r[2])});for(let t=0;t<n.length;t++)se(e,n.charCodeAt(t))}function se(e,t){let[r,s,i]=e.keys;e.te.append([t]),r=~e.te.get(),s=ce(n.imul(ce(s+oe(r)),134775813)+1),e.ne.append([s>>>24]),i=~e.ne.get(),e.keys=[r,s,i]}function ie(e){const t=2|e.keys[2];return oe(n.imul(t,1^t)>>>8)}function oe(e){return 255&e}function ce(e){return 4294967295&e}class fe extends d{constructor(e,{chunkSize:t,CompressionStream:n,re:r}){super({});const{compressed:s,encrypted:i,useCompressionStream:o,password:c,passwordVerification:f,encryptionStrength:l,zipCrypto:u,signed:w,level:h}=e,d=this;let p,y,m=le(super.readable);i&&!u||!w||([m,p]=m.tee(),p=he(p,new v)),s&&(m=we(m,o,{level:h,chunkSize:t},r,n)),i&&(u?m=he(m,new ee(c,f)):(y=new q(c,l),m=he(m,y))),ue(d,m,(async()=>{let e;i&&!u&&(e=y.signature),i&&!u||!w||(e=await p.getReader().read(),e=new a(e.value.buffer).getUint32(0)),d.signature=e}))}}class ae extends d{constructor(e,{chunkSize:t,DecompressionStream:n,se:s}){super({});const{zipCrypto:i,encrypted:o,password:c,passwordVerification:f,signed:l,signature:u,encryptionStrength:w,compressed:h,useCompressionStream:d}=e;let p,y,m=le(super.readable);o&&(i?m=he(m,new $(c,f)):(y=new O(c,l,w),m=he(m,y))),h&&(m=we(m,d,{chunkSize:t},s,n)),o&&!i||!l||([m,p]=m.tee(),p=he(p,new v)),ue(this,m,(async()=>{if((!o||i)&&l){const e=await p.getReader().read(),t=new a(e.value.buffer);if(u!=t.getUint32(0,!1))throw new r("Invalid signature")}}))}}function le(e){return he(e,new d({transform(e,t){e&&e.length&&t.enqueue(e)}}))}function ue(e,n,r){n=he(n,new d({flush:r})),t.defineProperty(e,"readable",{get:()=>n})}function we(e,t,n,r,s){try{e=he(e,new(t&&r?r:s)("deflate-raw",n))}catch(r){if(!t)throw r;e=he(e,new s("deflate-raw",n))}return e}function he(e,t){return e.pipeThrough(t)}class de extends d{constructor(e,t){const n=new e(t);super({transform(e,t){t.enqueue(n.append(e))},flush(e){const t=n.flush();t&&e.enqueue(t)}})}}class pe{constructor(e,n,r,s){const{codecType:i}=r;let o;i.startsWith("deflate")?o=fe:i.startsWith("inflate")&&(o=ae),t.assign(this,{ie:o,readable:e,writable:n,options:r,config:s})}async oe(){const{ie:e,readable:t,writable:n,options:r,config:s}=this,i=new e(r,s);let o=0;await t.pipeThrough(i).pipeThrough(new d({transform(e,t){e&&e.length&&(o+=e.length,t.enqueue(e))}})).pipeTo(n,{ce:!0,fe:!0});const{signature:c}=i;return{size:o,signature:c}}}function ye(e){return class extends de{constructor(t,n){super(e,n)}}}const me=new f,be=new f;let ge,ke=0;async function ve(e){try{const{options:t,scripts:n,config:r}=e;n&&n.length&&importScripts.apply(void 0,n),self.initCodec&&self.initCodec(),r.re=self.CompressionStream,r.se=self.DecompressionStream,self.Deflate&&(r.CompressionStream=ye(self.Deflate)),self.Inflate&&(r.DecompressionStream=ye(self.Inflate));const s={highWaterMark:1,size:()=>r.chunkSize},i=e.readable||new p({async pull(e){let t=new l((e=>me.set(ke,e)));Se({type:"pull",messageId:ke}),ke=(ke+1)%Number.MAX_SAFE_INTEGER;const{value:n,done:r}=await t;e.enqueue(n),r&&e.close()}},s),o=e.writable||new y({async write(e){let t;const n=new l((e=>t=e));be.set(ke,t),Se({type:"data",data:e,messageId:ke}),ke=(ke+1)%Number.MAX_SAFE_INTEGER,await n}},s);ge=new pe(i,o,t,r);const c=await ge.oe();try{await o.close()}catch(e){}Se({type:"close",result:c})}catch(e){Ce(e)}}function Se(e){if(e.data){let{data:t}=e;if(t&&t.length)try{t=new s(t),e.data=t.buffer,h(e,[e.data])}catch(t){h(e)}else h(e)}else h(e)}function Ce(e){const{message:t,stack:n,code:r,name:s}=e;h({error:{message:t,stack:n,code:r,name:s}})}function ze(t){return _e(t.map((([t,n])=>new e(t).fill(n,0,t))))}function _e(t){return t.reduce(((t,n)=>t.concat(e.isArray(n)?_e(n):n)),[])}addEventListener("message",(async e=>{const t=e.data,{type:n,messageId:r,data:i,done:o}=t;try{if("start"==n&&ve(t),"data"==n){const e=me.get(r);me.delete(r),e({value:new s(i),done:o})}if("ack"==n){const e=be.get(r);be.delete(r),e()}}catch(e){Ce(e)}}));const Ie=[0,1,2,3].concat(...ze([[2,4],[2,5],[4,6],[4,7],[8,8],[8,9],[16,10],[16,11],[32,12],[32,13],[64,14],[64,15],[2,0],[1,16],[1,17],[2,18],[2,19],[4,20],[4,21],[8,22],[8,23],[16,24],[16,25],[32,26],[32,27],[64,28],[64,29]]));function xe(){const e=this;function t(e,t){let n=0;do{n|=1&e,e>>>=1,n<<=1}while(--t>0);return n>>>1}e.ae=r=>{const s=e.le,i=e.we.ue,o=e.we.he;let c,f,a,l=-1;for(r.de=0,r.pe=573,c=0;o>c;c++)0!==s[2*c]?(r.ye[++r.de]=l=c,r.me[c]=0):s[2*c+1]=0;for(;2>r.de;)a=r.ye[++r.de]=2>l?++l:0,s[2*a]=1,r.me[a]=0,r.be--,i&&(r.ge-=i[2*a+1]);for(e.ke=l,c=n.floor(r.de/2);c>=1;c--)r.ve(s,c);a=o;do{c=r.ye[1],r.ye[1]=r.ye[r.de--],r.ve(s,1),f=r.ye[1],r.ye[--r.pe]=c,r.ye[--r.pe]=f,s[2*a]=s[2*c]+s[2*f],r.me[a]=n.max(r.me[c],r.me[f])+1,s[2*c+1]=s[2*f+1]=a,r.ye[1]=a++,r.ve(s,1)}while(r.de>=2);r.ye[--r.pe]=r.ye[1],(t=>{const n=e.le,r=e.we.ue,s=e.we.Se,i=e.we.Ce,o=e.we.ze;let c,f,a,l,u,w,h=0;for(l=0;15>=l;l++)t._e[l]=0;for(n[2*t.ye[t.pe]+1]=0,c=t.pe+1;573>c;c++)f=t.ye[c],l=n[2*n[2*f+1]+1]+1,l>o&&(l=o,h++),n[2*f+1]=l,f>e.ke||(t._e[l]++,u=0,i>f||(u=s[f-i]),w=n[2*f],t.be+=w*(l+u),r&&(t.ge+=w*(r[2*f+1]+u)));if(0!==h){do{for(l=o-1;0===t._e[l];)l--;t._e[l]--,t._e[l+1]+=2,t._e[o]--,h-=2}while(h>0);for(l=o;0!==l;l--)for(f=t._e[l];0!==f;)a=t.ye[--c],a>e.ke||(n[2*a+1]!=l&&(t.be+=(l-n[2*a+1])*n[2*a],n[2*a+1]=l),f--)}})(r),((e,n,r)=>{const s=[];let i,o,c,f=0;for(i=1;15>=i;i++)s[i]=f=f+r[i-1]<<1;for(o=0;n>=o;o++)c=e[2*o+1],0!==c&&(e[2*o]=t(s[c]++,c))})(s,e.ke,r._e)}}function Ae(e,t,n,r,s){const i=this;i.ue=e,i.Se=t,i.Ce=n,i.he=r,i.ze=s}xe.Ie=[0,1,2,3,4,5,6,7].concat(...ze([[2,8],[2,9],[2,10],[2,11],[4,12],[4,13],[4,14],[4,15],[8,16],[8,17],[8,18],[8,19],[16,20],[16,21],[16,22],[16,23],[32,24],[32,25],[32,26],[31,27],[1,28]])),xe.xe=[0,1,2,3,4,5,6,7,8,10,12,14,16,20,24,28,32,40,48,56,64,80,96,112,128,160,192,224,0],xe.Ae=[0,1,2,3,4,6,8,12,16,24,32,48,64,96,128,192,256,384,512,768,1024,1536,2048,3072,4096,6144,8192,12288,16384,24576],xe.De=e=>256>e?Ie[e]:Ie[256+(e>>>7)],xe.Ve=[0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0],xe.Re=[0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13],xe.Be=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7],xe.Ee=[16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15];const De=ze([[144,8],[112,9],[24,7],[8,8]]);Ae.Me=_e([12,140,76,204,44,172,108,236,28,156,92,220,60,188,124,252,2,130,66,194,34,162,98,226,18,146,82,210,50,178,114,242,10,138,74,202,42,170,106,234,26,154,90,218,58,186,122,250,6,134,70,198,38,166,102,230,22,150,86,214,54,182,118,246,14,142,78,206,46,174,110,238,30,158,94,222,62,190,126,254,1,129,65,193,33,161,97,225,17,145,81,209,49,177,113,241,9,137,73,201,41,169,105,233,25,153,89,217,57,185,121,249,5,133,69,197,37,165,101,229,21,149,85,213,53,181,117,245,13,141,77,205,45,173,109,237,29,157,93,221,61,189,125,253,19,275,147,403,83,339,211,467,51,307,179,435,115,371,243,499,11,267,139,395,75,331,203,459,43,299,171,427,107,363,235,491,27,283,155,411,91,347,219,475,59,315,187,443,123,379,251,507,7,263,135,391,71,327,199,455,39,295,167,423,103,359,231,487,23,279,151,407,87,343,215,471,55,311,183,439,119,375,247,503,15,271,143,399,79,335,207,463,47,303,175,431,111,367,239,495,31,287,159,415,95,351,223,479,63,319,191,447,127,383,255,511,0,64,32,96,16,80,48,112,8,72,40,104,24,88,56,120,4,68,36,100,20,84,52,116,3,131,67,195,35,163,99,227].map(((e,t)=>[e,De[t]])));const Ve=ze([[30,5]]);function Re(e,t,n,r,s){const i=this;i.Ke=e,i.Ne=t,i.Pe=n,i.Ue=r,i.Te=s}Ae.We=_e([0,16,8,24,4,20,12,28,2,18,10,26,6,22,14,30,1,17,9,25,5,21,13,29,3,19,11,27,7,23].map(((e,t)=>[e,Ve[t]]))),Ae.He=new Ae(Ae.Me,xe.Ve,257,286,15),Ae.Le=new Ae(Ae.We,xe.Re,0,30,15),Ae.je=new Ae(null,xe.Be,0,19,7);const Be=[new Re(0,0,0,0,0),new Re(4,4,8,4,1),new Re(4,5,16,8,1),new Re(4,6,32,32,1),new Re(4,4,16,16,2),new Re(8,16,32,32,2),new Re(8,16,128,128,2),new Re(8,32,128,256,2),new Re(32,128,258,1024,2),new Re(32,258,258,4096,2)],Ee=["need dictionary","stream end","","","stream error","data error","","buffer error","",""];function Me(e,t,n,r){const s=e[2*t],i=e[2*n];return i>s||s==i&&r[t]<=r[n]}function Ke(){const e=this;let t,r,o,c,f,a,l,u,w,h,d,p,y,m,b,g,k,v,S,C,z,_,I,x,A,D,V,R,B,E,M,K,N;const P=new xe,U=new xe,T=new xe;let W,H,L,j,F,O;function q(){let t;for(t=0;286>t;t++)M[2*t]=0;for(t=0;30>t;t++)K[2*t]=0;for(t=0;19>t;t++)N[2*t]=0;M[512]=1,e.be=e.ge=0,H=L=0}function G(e,t){let n,r=-1,s=e[1],i=0,o=7,c=4;0===s&&(o=138,c=3),e[2*(t+1)+1]=65535;for(let f=0;t>=f;f++)n=s,s=e[2*(f+1)+1],++i<o&&n==s||(c>i?N[2*n]+=i:0!==n?(n!=r&&N[2*n]++,N[32]++):i>10?N[36]++:N[34]++,i=0,r=n,0===s?(o=138,c=3):n==s?(o=6,c=3):(o=7,c=4))}function J(t){e.Fe[e.pending++]=t}function Q(e){J(255&e),J(e>>>8&255)}function X(e,t){let n;const r=t;O>16-r?(n=e,F|=n<<O&65535,Q(F),F=n>>>16-O,O+=r-16):(F|=e<<O&65535,O+=r)}function Y(e,t){const n=2*e;X(65535&t[n],65535&t[n+1])}function Z(e,t){let n,r,s=-1,i=e[1],o=0,c=7,f=4;for(0===i&&(c=138,f=3),n=0;t>=n;n++)if(r=i,i=e[2*(n+1)+1],++o>=c||r!=i){if(f>o)do{Y(r,N)}while(0!=--o);else 0!==r?(r!=s&&(Y(r,N),o--),Y(16,N),X(o-3,2)):o>10?(Y(18,N),X(o-11,7)):(Y(17,N),X(o-3,3));o=0,s=r,0===i?(c=138,f=3):r==i?(c=6,f=3):(c=7,f=4)}}function $(){16==O?(Q(F),F=0,O=0):8>O||(J(255&F),F>>>=8,O-=8)}function ee(t,r){let s,i,o;if(e.Oe[H]=t,e.qe[H]=255&r,H++,0===t?M[2*r]++:(L++,t--,M[2*(xe.Ie[r]+256+1)]++,K[2*xe.De(t)]++),0==(8191&H)&&V>2){for(s=8*H,i=z-k,o=0;30>o;o++)s+=K[2*o]*(5+xe.Re[o]);if(s>>>=3,L<n.floor(H/2)&&s<n.floor(i/2))return!0}return H==W-1}function te(t,n){let r,s,i,o,c=0;if(0!==H)do{r=e.Oe[c],s=e.qe[c],c++,0===r?Y(s,t):(i=xe.Ie[s],Y(i+256+1,t),o=xe.Ve[i],0!==o&&(s-=xe.xe[i],X(s,o)),r--,i=xe.De(r),Y(i,n),o=xe.Re[i],0!==o&&(r-=xe.Ae[i],X(r,o)))}while(H>c);Y(256,t),j=t[513]}function ne(){O>8?Q(F):O>0&&J(255&F),F=0,O=0}function re(t,n,r){X(0+(r?1:0),3),((t,n)=>{ne(),j=8,Q(n),Q(~n),e.Fe.set(u.subarray(t,t+n),e.pending),e.pending+=n})(t,n)}function se(n){((t,n,r)=>{let s,i,o=0;V>0?(P.ae(e),U.ae(e),o=(()=>{let t;for(G(M,P.ke),G(K,U.ke),T.ae(e),t=18;t>=3&&0===N[2*xe.Ee[t]+1];t--);return e.be+=14+3*(t+1),t})(),s=e.be+3+7>>>3,i=e.ge+3+7>>>3,i>s||(s=i)):s=i=n+5,n+4>s||-1==t?i==s?(X(2+(r?1:0),3),te(Ae.Me,Ae.We)):(X(4+(r?1:0),3),((e,t,n)=>{let r;for(X(e-257,5),X(t-1,5),X(n-4,4),r=0;n>r;r++)X(N[2*xe.Ee[r]+1],3);Z(M,e-1),Z(K,t-1)})(P.ke+1,U.ke+1,o+1),te(M,K)):re(t,n,r),q(),r&&ne()})(0>k?-1:k,z-k,n),k=z,t.Ge()}function ie(){let e,n,r,s;do{if(s=w-I-z,0===s&&0===z&&0===I)s=f;else if(-1==s)s--;else if(z>=f+f-262){u.set(u.subarray(f,f+f),0),_-=f,z-=f,k-=f,e=y,r=e;do{n=65535&d[--r],d[r]=f>n?0:n-f}while(0!=--e);e=f,r=e;do{n=65535&h[--r],h[r]=f>n?0:n-f}while(0!=--e);s+=f}if(0===t.Je)return;e=t.Qe(u,z+I,s),I+=e,3>I||(p=255&u[z],p=(p<<g^255&u[z+1])&b)}while(262>I&&0!==t.Je)}function oe(e){let t,n,r=A,s=z,i=x;const o=z>f-262?z-(f-262):0;let c=E;const a=l,w=z+258;let d=u[s+i-1],p=u[s+i];B>x||(r>>=2),c>I&&(c=I);do{if(t=e,u[t+i]==p&&u[t+i-1]==d&&u[t]==u[s]&&u[++t]==u[s+1]){s+=2,t++;do{}while(u[++s]==u[++t]&&u[++s]==u[++t]&&u[++s]==u[++t]&&u[++s]==u[++t]&&u[++s]==u[++t]&&u[++s]==u[++t]&&u[++s]==u[++t]&&u[++s]==u[++t]&&w>s);if(n=258-(w-s),s=w-258,n>i){if(_=e,i=n,n>=c)break;d=u[s+i-1],p=u[s+i]}}}while((e=65535&h[e&a])>o&&0!=--r);return i>I?I:i}e.me=[],e._e=[],e.ye=[],M=[],K=[],N=[],e.ve=(t,n)=>{const r=e.ye,s=r[n];let i=n<<1;for(;i<=e.de&&(i<e.de&&Me(t,r[i+1],r[i],e.me)&&i++,!Me(t,s,r[i],e.me));)r[n]=r[i],n=i,i<<=1;r[n]=s},e.Xe=(t,S,_,H,L,G)=>(H||(H=8),L||(L=8),G||(G=0),t.Ye=null,-1==S&&(S=6),1>L||L>9||8!=H||9>_||_>15||0>S||S>9||0>G||G>2?-2:(t.Ze=e,a=_,f=1<<a,l=f-1,m=L+7,y=1<<m,b=y-1,g=n.floor((m+3-1)/3),u=new s(2*f),h=[],d=[],W=1<<L+6,e.Fe=new s(4*W),o=4*W,e.Oe=new i(W),e.qe=new s(W),V=S,R=G,(t=>(t.$e=t.et=0,t.Ye=null,e.pending=0,e.tt=0,r=113,c=0,P.le=M,P.we=Ae.He,U.le=K,U.we=Ae.Le,T.le=N,T.we=Ae.je,F=0,O=0,j=8,q(),(()=>{w=2*f,d[y-1]=0;for(let e=0;y-1>e;e++)d[e]=0;D=Be[V].Ne,B=Be[V].Ke,E=Be[V].Pe,A=Be[V].Ue,z=0,k=0,I=0,v=x=2,C=0,p=0})(),0))(t))),e.nt=()=>42!=r&&113!=r&&666!=r?-2:(e.qe=null,e.Oe=null,e.Fe=null,d=null,h=null,u=null,e.Ze=null,113==r?-3:0),e.rt=(e,t,n)=>{let r=0;return-1==t&&(t=6),0>t||t>9||0>n||n>2?-2:(Be[V].Te!=Be[t].Te&&0!==e.$e&&(r=e.st(1)),V!=t&&(V=t,D=Be[V].Ne,B=Be[V].Ke,E=Be[V].Pe,A=Be[V].Ue),R=n,r)},e.it=(e,t,n)=>{let s,i=n,o=0;if(!t||42!=r)return-2;if(3>i)return 0;for(i>f-262&&(i=f-262,o=n-i),u.set(t.subarray(o,o+i),0),z=i,k=i,p=255&u[0],p=(p<<g^255&u[1])&b,s=0;i-3>=s;s++)p=(p<<g^255&u[s+2])&b,h[s&l]=d[p],d[p]=s;return 0},e.st=(n,s)=>{let i,w,m,A,B;if(s>4||0>s)return-2;if(!n.ot||!n.ct&&0!==n.Je||666==r&&4!=s)return n.Ye=Ee[4],-2;if(0===n.ft)return n.Ye=Ee[7],-5;var E;if(t=n,A=c,c=s,42==r&&(w=8+(a-8<<4)<<8,m=(V-1&255)>>1,m>3&&(m=3),w|=m<<6,0!==z&&(w|=32),w+=31-w%31,r=113,J((E=w)>>8&255),J(255&E)),0!==e.pending){if(t.Ge(),0===t.ft)return c=-1,0}else if(0===t.Je&&A>=s&&4!=s)return t.Ye=Ee[7],-5;if(666==r&&0!==t.Je)return n.Ye=Ee[7],-5;if(0!==t.Je||0!==I||0!=s&&666!=r){switch(B=-1,Be[V].Te){case 0:B=(e=>{let n,r=65535;for(r>o-5&&(r=o-5);;){if(1>=I){if(ie(),0===I&&0==e)return 0;if(0===I)break}if(z+=I,I=0,n=k+r,(0===z||z>=n)&&(I=z-n,z=n,se(!1),0===t.ft))return 0;if(z-k>=f-262&&(se(!1),0===t.ft))return 0}return se(4==e),0===t.ft?4==e?2:0:4==e?3:1})(s);break;case 1:B=(e=>{let n,r=0;for(;;){if(262>I){if(ie(),262>I&&0==e)return 0;if(0===I)break}if(3>I||(p=(p<<g^255&u[z+2])&b,r=65535&d[p],h[z&l]=d[p],d[p]=z),0===r||(z-r&65535)>f-262||2!=R&&(v=oe(r)),3>v)n=ee(0,255&u[z]),I--,z++;else if(n=ee(z-_,v-3),I-=v,v>D||3>I)z+=v,v=0,p=255&u[z],p=(p<<g^255&u[z+1])&b;else{v--;do{z++,p=(p<<g^255&u[z+2])&b,r=65535&d[p],h[z&l]=d[p],d[p]=z}while(0!=--v);z++}if(n&&(se(!1),0===t.ft))return 0}return se(4==e),0===t.ft?4==e?2:0:4==e?3:1})(s);break;case 2:B=(e=>{let n,r,s=0;for(;;){if(262>I){if(ie(),262>I&&0==e)return 0;if(0===I)break}if(3>I||(p=(p<<g^255&u[z+2])&b,s=65535&d[p],h[z&l]=d[p],d[p]=z),x=v,S=_,v=2,0!==s&&D>x&&f-262>=(z-s&65535)&&(2!=R&&(v=oe(s)),5>=v&&(1==R||3==v&&z-_>4096)&&(v=2)),3>x||v>x)if(0!==C){if(n=ee(0,255&u[z-1]),n&&se(!1),z++,I--,0===t.ft)return 0}else C=1,z++,I--;else{r=z+I-3,n=ee(z-1-S,x-3),I-=x-1,x-=2;do{++z>r||(p=(p<<g^255&u[z+2])&b,s=65535&d[p],h[z&l]=d[p],d[p]=z)}while(0!=--x);if(C=0,v=2,z++,n&&(se(!1),0===t.ft))return 0}}return 0!==C&&(n=ee(0,255&u[z-1]),C=0),se(4==e),0===t.ft?4==e?2:0:4==e?3:1})(s)}if(2!=B&&3!=B||(r=666),0==B||2==B)return 0===t.ft&&(c=-1),0;if(1==B){if(1==s)X(2,3),Y(256,Ae.Me),$(),9>1+j+10-O&&(X(2,3),Y(256,Ae.Me),$()),j=7;else if(re(0,0,!1),3==s)for(i=0;y>i;i++)d[i]=0;if(t.Ge(),0===t.ft)return c=-1,0}}return 4!=s?0:1}}function Ne(){const e=this;e.lt=0,e.ut=0,e.Je=0,e.$e=0,e.ft=0,e.et=0}function Pe(e){const t=new Ne,i=(o=e&&e.chunkSize?e.chunkSize:65536)+5*(n.floor(o/16383)+1);var o;const c=new s(i);let f=e?e.level:-1;void 0===f&&(f=-1),t.Xe(f),t.ot=c,this.append=(e,n)=>{let o,f,a=0,l=0,u=0;const w=[];if(e.length){t.lt=0,t.ct=e,t.Je=e.length;do{if(t.ut=0,t.ft=i,o=t.st(0),0!=o)throw new r("deflating: "+t.Ye);t.ut&&(t.ut==i?w.push(new s(c)):w.push(c.slice(0,t.ut))),u+=t.ut,n&&t.lt>0&&t.lt!=a&&(n(t.lt),a=t.lt)}while(t.Je>0||0===t.ft);return w.length>1?(f=new s(u),w.forEach((e=>{f.set(e,l),l+=e.length}))):f=w[0]||new s,f}},this.flush=()=>{let e,n,o=0,f=0;const a=[];do{if(t.ut=0,t.ft=i,e=t.st(4),1!=e&&0!=e)throw new r("deflating: "+t.Ye);i-t.ft>0&&a.push(c.slice(0,t.ut)),f+=t.ut}while(t.Je>0||0===t.ft);return t.nt(),n=new s(f),a.forEach((e=>{n.set(e,o),o+=e.length})),n}}Ne.prototype={Xe(e,t){const n=this;return n.Ze=new Ke,t||(t=15),n.Ze.Xe(n,e,t)},st(e){const t=this;return t.Ze?t.Ze.st(t,e):-2},nt(){const e=this;if(!e.Ze)return-2;const t=e.Ze.nt();return e.Ze=null,t},rt(e,t){const n=this;return n.Ze?n.Ze.rt(n,e,t):-2},it(e,t){const n=this;return n.Ze?n.Ze.it(n,e,t):-2},Qe(e,t,n){const r=this;let s=r.Je;return s>n&&(s=n),0===s?0:(r.Je-=s,e.set(r.ct.subarray(r.lt,r.lt+s),t),r.lt+=s,r.$e+=s,s)},Ge(){const e=this;let t=e.Ze.pending;t>e.ft&&(t=e.ft),0!==t&&(e.ot.set(e.Ze.Fe.subarray(e.Ze.tt,e.Ze.tt+t),e.ut),e.ut+=t,e.Ze.tt+=t,e.et+=t,e.ft-=t,e.Ze.pending-=t,0===e.Ze.pending&&(e.Ze.tt=0))}};const Ue=[0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535],Te=[96,7,256,0,8,80,0,8,16,84,8,115,82,7,31,0,8,112,0,8,48,0,9,192,80,7,10,0,8,96,0,8,32,0,9,160,0,8,0,0,8,128,0,8,64,0,9,224,80,7,6,0,8,88,0,8,24,0,9,144,83,7,59,0,8,120,0,8,56,0,9,208,81,7,17,0,8,104,0,8,40,0,9,176,0,8,8,0,8,136,0,8,72,0,9,240,80,7,4,0,8,84,0,8,20,85,8,227,83,7,43,0,8,116,0,8,52,0,9,200,81,7,13,0,8,100,0,8,36,0,9,168,0,8,4,0,8,132,0,8,68,0,9,232,80,7,8,0,8,92,0,8,28,0,9,152,84,7,83,0,8,124,0,8,60,0,9,216,82,7,23,0,8,108,0,8,44,0,9,184,0,8,12,0,8,140,0,8,76,0,9,248,80,7,3,0,8,82,0,8,18,85,8,163,83,7,35,0,8,114,0,8,50,0,9,196,81,7,11,0,8,98,0,8,34,0,9,164,0,8,2,0,8,130,0,8,66,0,9,228,80,7,7,0,8,90,0,8,26,0,9,148,84,7,67,0,8,122,0,8,58,0,9,212,82,7,19,0,8,106,0,8,42,0,9,180,0,8,10,0,8,138,0,8,74,0,9,244,80,7,5,0,8,86,0,8,22,192,8,0,83,7,51,0,8,118,0,8,54,0,9,204,81,7,15,0,8,102,0,8,38,0,9,172,0,8,6,0,8,134,0,8,70,0,9,236,80,7,9,0,8,94,0,8,30,0,9,156,84,7,99,0,8,126,0,8,62,0,9,220,82,7,27,0,8,110,0,8,46,0,9,188,0,8,14,0,8,142,0,8,78,0,9,252,96,7,256,0,8,81,0,8,17,85,8,131,82,7,31,0,8,113,0,8,49,0,9,194,80,7,10,0,8,97,0,8,33,0,9,162,0,8,1,0,8,129,0,8,65,0,9,226,80,7,6,0,8,89,0,8,25,0,9,146,83,7,59,0,8,121,0,8,57,0,9,210,81,7,17,0,8,105,0,8,41,0,9,178,0,8,9,0,8,137,0,8,73,0,9,242,80,7,4,0,8,85,0,8,21,80,8,258,83,7,43,0,8,117,0,8,53,0,9,202,81,7,13,0,8,101,0,8,37,0,9,170,0,8,5,0,8,133,0,8,69,0,9,234,80,7,8,0,8,93,0,8,29,0,9,154,84,7,83,0,8,125,0,8,61,0,9,218,82,7,23,0,8,109,0,8,45,0,9,186,0,8,13,0,8,141,0,8,77,0,9,250,80,7,3,0,8,83,0,8,19,85,8,195,83,7,35,0,8,115,0,8,51,0,9,198,81,7,11,0,8,99,0,8,35,0,9,166,0,8,3,0,8,131,0,8,67,0,9,230,80,7,7,0,8,91,0,8,27,0,9,150,84,7,67,0,8,123,0,8,59,0,9,214,82,7,19,0,8,107,0,8,43,0,9,182,0,8,11,0,8,139,0,8,75,0,9,246,80,7,5,0,8,87,0,8,23,192,8,0,83,7,51,0,8,119,0,8,55,0,9,206,81,7,15,0,8,103,0,8,39,0,9,174,0,8,7,0,8,135,0,8,71,0,9,238,80,7,9,0,8,95,0,8,31,0,9,158,84,7,99,0,8,127,0,8,63,0,9,222,82,7,27,0,8,111,0,8,47,0,9,190,0,8,15,0,8,143,0,8,79,0,9,254,96,7,256,0,8,80,0,8,16,84,8,115,82,7,31,0,8,112,0,8,48,0,9,193,80,7,10,0,8,96,0,8,32,0,9,161,0,8,0,0,8,128,0,8,64,0,9,225,80,7,6,0,8,88,0,8,24,0,9,145,83,7,59,0,8,120,0,8,56,0,9,209,81,7,17,0,8,104,0,8,40,0,9,177,0,8,8,0,8,136,0,8,72,0,9,241,80,7,4,0,8,84,0,8,20,85,8,227,83,7,43,0,8,116,0,8,52,0,9,201,81,7,13,0,8,100,0,8,36,0,9,169,0,8,4,0,8,132,0,8,68,0,9,233,80,7,8,0,8,92,0,8,28,0,9,153,84,7,83,0,8,124,0,8,60,0,9,217,82,7,23,0,8,108,0,8,44,0,9,185,0,8,12,0,8,140,0,8,76,0,9,249,80,7,3,0,8,82,0,8,18,85,8,163,83,7,35,0,8,114,0,8,50,0,9,197,81,7,11,0,8,98,0,8,34,0,9,165,0,8,2,0,8,130,0,8,66,0,9,229,80,7,7,0,8,90,0,8,26,0,9,149,84,7,67,0,8,122,0,8,58,0,9,213,82,7,19,0,8,106,0,8,42,0,9,181,0,8,10,0,8,138,0,8,74,0,9,245,80,7,5,0,8,86,0,8,22,192,8,0,83,7,51,0,8,118,0,8,54,0,9,205,81,7,15,0,8,102,0,8,38,0,9,173,0,8,6,0,8,134,0,8,70,0,9,237,80,7,9,0,8,94,0,8,30,0,9,157,84,7,99,0,8,126,0,8,62,0,9,221,82,7,27,0,8,110,0,8,46,0,9,189,0,8,14,0,8,142,0,8,78,0,9,253,96,7,256,0,8,81,0,8,17,85,8,131,82,7,31,0,8,113,0,8,49,0,9,195,80,7,10,0,8,97,0,8,33,0,9,163,0,8,1,0,8,129,0,8,65,0,9,227,80,7,6,0,8,89,0,8,25,0,9,147,83,7,59,0,8,121,0,8,57,0,9,211,81,7,17,0,8,105,0,8,41,0,9,179,0,8,9,0,8,137,0,8,73,0,9,243,80,7,4,0,8,85,0,8,21,80,8,258,83,7,43,0,8,117,0,8,53,0,9,203,81,7,13,0,8,101,0,8,37,0,9,171,0,8,5,0,8,133,0,8,69,0,9,235,80,7,8,0,8,93,0,8,29,0,9,155,84,7,83,0,8,125,0,8,61,0,9,219,82,7,23,0,8,109,0,8,45,0,9,187,0,8,13,0,8,141,0,8,77,0,9,251,80,7,3,0,8,83,0,8,19,85,8,195,83,7,35,0,8,115,0,8,51,0,9,199,81,7,11,0,8,99,0,8,35,0,9,167,0,8,3,0,8,131,0,8,67,0,9,231,80,7,7,0,8,91,0,8,27,0,9,151,84,7,67,0,8,123,0,8,59,0,9,215,82,7,19,0,8,107,0,8,43,0,9,183,0,8,11,0,8,139,0,8,75,0,9,247,80,7,5,0,8,87,0,8,23,192,8,0,83,7,51,0,8,119,0,8,55,0,9,207,81,7,15,0,8,103,0,8,39,0,9,175,0,8,7,0,8,135,0,8,71,0,9,239,80,7,9,0,8,95,0,8,31,0,9,159,84,7,99,0,8,127,0,8,63,0,9,223,82,7,27,0,8,111,0,8,47,0,9,191,0,8,15,0,8,143,0,8,79,0,9,255],We=[80,5,1,87,5,257,83,5,17,91,5,4097,81,5,5,89,5,1025,85,5,65,93,5,16385,80,5,3,88,5,513,84,5,33,92,5,8193,82,5,9,90,5,2049,86,5,129,192,5,24577,80,5,2,87,5,385,83,5,25,91,5,6145,81,5,7,89,5,1537,85,5,97,93,5,24577,80,5,4,88,5,769,84,5,49,92,5,12289,82,5,13,90,5,3073,86,5,193,192,5,24577],He=[3,4,5,6,7,8,9,10,11,13,15,17,19,23,27,31,35,43,51,59,67,83,99,115,131,163,195,227,258,0,0],Le=[0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,112,112],je=[1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577],Fe=[0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13];function Oe(){let e,t,n,r,s,i;function o(e,t,o,c,f,a,l,u,w,h,d){let p,y,m,b,g,k,v,S,C,z,_,I,x,A,D;z=0,g=o;do{n[e[t+z]]++,z++,g--}while(0!==g);if(n[0]==o)return l[0]=-1,u[0]=0,0;for(S=u[0],k=1;15>=k&&0===n[k];k++);for(v=k,k>S&&(S=k),g=15;0!==g&&0===n[g];g--);for(m=g,S>g&&(S=g),u[0]=S,A=1<<k;g>k;k++,A<<=1)if(0>(A-=n[k]))return-3;if(0>(A-=n[g]))return-3;for(n[g]+=A,i[1]=k=0,z=1,x=2;0!=--g;)i[x]=k+=n[z],x++,z++;g=0,z=0;do{0!==(k=e[t+z])&&(d[i[k]++]=g),z++}while(++g<o);for(o=i[m],i[0]=g=0,z=0,b=-1,I=-S,s[0]=0,_=0,D=0;m>=v;v++)for(p=n[v];0!=p--;){for(;v>I+S;){if(b++,I+=S,D=m-I,D=D>S?S:D,(y=1<<(k=v-I))>p+1&&(y-=p+1,x=v,D>k))for(;++k<D&&(y<<=1)>n[++x];)y-=n[x];if(D=1<<k,h[0]+D>1440)return-3;s[b]=_=h[0],h[0]+=D,0!==b?(i[b]=g,r[0]=k,r[1]=S,k=g>>>I-S,r[2]=_-s[b-1]-k,w.set(r,3*(s[b-1]+k))):l[0]=_}for(r[1]=v-I,o>z?d[z]<c?(r[0]=256>d[z]?0:96,r[2]=d[z++]):(r[0]=a[d[z]-c]+16+64,r[2]=f[d[z++]-c]):r[0]=192,y=1<<v-I,k=g>>>I;D>k;k+=y)w.set(r,3*(_+k));for(k=1<<v-1;0!=(g&k);k>>>=1)g^=k;for(g^=k,C=(1<<I)-1;(g&C)!=i[b];)b--,I-=S,C=(1<<I)-1}return 0!==A&&1!=m?-5:0}function f(o){let f;for(e||(e=[],t=[],n=new c(16),r=[],s=new c(15),i=new c(16)),t.length<o&&(t=[]),f=0;o>f;f++)t[f]=0;for(f=0;16>f;f++)n[f]=0;for(f=0;3>f;f++)r[f]=0;s.set(n.subarray(0,15),0),i.set(n.subarray(0,16),0)}this.wt=(n,r,s,i,c)=>{let a;return f(19),e[0]=0,a=o(n,0,19,19,null,null,s,r,i,e,t),-3==a?c.Ye="oversubscribed dynamic bit lengths tree":-5!=a&&0!==r[0]||(c.Ye="incomplete dynamic bit lengths tree",a=-3),a},this.ht=(n,r,s,i,c,a,l,u,w)=>{let h;return f(288),e[0]=0,h=o(s,0,n,257,He,Le,a,i,u,e,t),0!=h||0===i[0]?(-3==h?w.Ye="oversubscribed literal/length tree":-4!=h&&(w.Ye="incomplete literal/length tree",h=-3),h):(f(288),h=o(s,n,r,0,je,Fe,l,c,u,e,t),0!=h||0===c[0]&&n>257?(-3==h?w.Ye="oversubscribed distance tree":-5==h?(w.Ye="incomplete distance tree",h=-3):-4!=h&&(w.Ye="empty distance tree with lengths",h=-3),h):0)}}function qe(){const e=this;let t,n,r,s,i=0,o=0,c=0,f=0,a=0,l=0,u=0,w=0,h=0,d=0;function p(e,t,n,r,s,i,o,c){let f,a,l,u,w,h,d,p,y,m,b,g,k,v,S,C;d=c.lt,p=c.Je,w=o.dt,h=o.yt,y=o.write,m=y<o.read?o.read-y-1:o.end-y,b=Ue[e],g=Ue[t];do{for(;20>h;)p--,w|=(255&c.bt(d++))<<h,h+=8;if(f=w&b,a=n,l=r,C=3*(l+f),0!==(u=a[C]))for(;;){if(w>>=a[C+1],h-=a[C+1],0!=(16&u)){for(u&=15,k=a[C+2]+(w&Ue[u]),w>>=u,h-=u;15>h;)p--,w|=(255&c.bt(d++))<<h,h+=8;for(f=w&g,a=s,l=i,C=3*(l+f),u=a[C];;){if(w>>=a[C+1],h-=a[C+1],0!=(16&u)){for(u&=15;u>h;)p--,w|=(255&c.bt(d++))<<h,h+=8;if(v=a[C+2]+(w&Ue[u]),w>>=u,h-=u,m-=k,v>y){S=y-v;do{S+=o.end}while(0>S);if(u=o.end-S,k>u){if(k-=u,y-S>0&&u>y-S)do{o.gt[y++]=o.gt[S++]}while(0!=--u);else o.gt.set(o.gt.subarray(S,S+u),y),y+=u,S+=u,u=0;S=0}}else S=y-v,y-S>0&&2>y-S?(o.gt[y++]=o.gt[S++],o.gt[y++]=o.gt[S++],k-=2):(o.gt.set(o.gt.subarray(S,S+2),y),y+=2,S+=2,k-=2);if(y-S>0&&k>y-S)do{o.gt[y++]=o.gt[S++]}while(0!=--k);else o.gt.set(o.gt.subarray(S,S+k),y),y+=k,S+=k,k=0;break}if(0!=(64&u))return c.Ye="invalid distance code",k=c.Je-p,k=k>h>>3?h>>3:k,p+=k,d-=k,h-=k<<3,o.dt=w,o.yt=h,c.Je=p,c.$e+=d-c.lt,c.lt=d,o.write=y,-3;f+=a[C+2],f+=w&Ue[u],C=3*(l+f),u=a[C]}break}if(0!=(64&u))return 0!=(32&u)?(k=c.Je-p,k=k>h>>3?h>>3:k,p+=k,d-=k,h-=k<<3,o.dt=w,o.yt=h,c.Je=p,c.$e+=d-c.lt,c.lt=d,o.write=y,1):(c.Ye="invalid literal/length code",k=c.Je-p,k=k>h>>3?h>>3:k,p+=k,d-=k,h-=k<<3,o.dt=w,o.yt=h,c.Je=p,c.$e+=d-c.lt,c.lt=d,o.write=y,-3);if(f+=a[C+2],f+=w&Ue[u],C=3*(l+f),0===(u=a[C])){w>>=a[C+1],h-=a[C+1],o.gt[y++]=a[C+2],m--;break}}else w>>=a[C+1],h-=a[C+1],o.gt[y++]=a[C+2],m--}while(m>=258&&p>=10);return k=c.Je-p,k=k>h>>3?h>>3:k,p+=k,d-=k,h-=k<<3,o.dt=w,o.yt=h,c.Je=p,c.$e+=d-c.lt,c.lt=d,o.write=y,0}e.init=(e,i,o,c,f,a)=>{t=0,u=e,w=i,r=o,h=c,s=f,d=a,n=null},e.kt=(e,y,m)=>{let b,g,k,v,S,C,z,_=0,I=0,x=0;for(x=y.lt,v=y.Je,_=e.dt,I=e.yt,S=e.write,C=S<e.read?e.read-S-1:e.end-S;;)switch(t){case 0:if(C>=258&&v>=10&&(e.dt=_,e.yt=I,y.Je=v,y.$e+=x-y.lt,y.lt=x,e.write=S,m=p(u,w,r,h,s,d,e,y),x=y.lt,v=y.Je,_=e.dt,I=e.yt,S=e.write,C=S<e.read?e.read-S-1:e.end-S,0!=m)){t=1==m?7:9;break}c=u,n=r,o=h,t=1;case 1:for(b=c;b>I;){if(0===v)return e.dt=_,e.yt=I,y.Je=v,y.$e+=x-y.lt,y.lt=x,e.write=S,e.vt(y,m);m=0,v--,_|=(255&y.bt(x++))<<I,I+=8}if(g=3*(o+(_&Ue[b])),_>>>=n[g+1],I-=n[g+1],k=n[g],0===k){f=n[g+2],t=6;break}if(0!=(16&k)){a=15&k,i=n[g+2],t=2;break}if(0==(64&k)){c=k,o=g/3+n[g+2];break}if(0!=(32&k)){t=7;break}return t=9,y.Ye="invalid literal/length code",m=-3,e.dt=_,e.yt=I,y.Je=v,y.$e+=x-y.lt,y.lt=x,e.write=S,e.vt(y,m);case 2:for(b=a;b>I;){if(0===v)return e.dt=_,e.yt=I,y.Je=v,y.$e+=x-y.lt,y.lt=x,e.write=S,e.vt(y,m);m=0,v--,_|=(255&y.bt(x++))<<I,I+=8}i+=_&Ue[b],_>>=b,I-=b,c=w,n=s,o=d,t=3;case 3:for(b=c;b>I;){if(0===v)return e.dt=_,e.yt=I,y.Je=v,y.$e+=x-y.lt,y.lt=x,e.write=S,e.vt(y,m);m=0,v--,_|=(255&y.bt(x++))<<I,I+=8}if(g=3*(o+(_&Ue[b])),_>>=n[g+1],I-=n[g+1],k=n[g],0!=(16&k)){a=15&k,l=n[g+2],t=4;break}if(0==(64&k)){c=k,o=g/3+n[g+2];break}return t=9,y.Ye="invalid distance code",m=-3,e.dt=_,e.yt=I,y.Je=v,y.$e+=x-y.lt,y.lt=x,e.write=S,e.vt(y,m);case 4:for(b=a;b>I;){if(0===v)return e.dt=_,e.yt=I,y.Je=v,y.$e+=x-y.lt,y.lt=x,e.write=S,e.vt(y,m);m=0,v--,_|=(255&y.bt(x++))<<I,I+=8}l+=_&Ue[b],_>>=b,I-=b,t=5;case 5:for(z=S-l;0>z;)z+=e.end;for(;0!==i;){if(0===C&&(S==e.end&&0!==e.read&&(S=0,C=S<e.read?e.read-S-1:e.end-S),0===C&&(e.write=S,m=e.vt(y,m),S=e.write,C=S<e.read?e.read-S-1:e.end-S,S==e.end&&0!==e.read&&(S=0,C=S<e.read?e.read-S-1:e.end-S),0===C)))return e.dt=_,e.yt=I,y.Je=v,y.$e+=x-y.lt,y.lt=x,e.write=S,e.vt(y,m);e.gt[S++]=e.gt[z++],C--,z==e.end&&(z=0),i--}t=0;break;case 6:if(0===C&&(S==e.end&&0!==e.read&&(S=0,C=S<e.read?e.read-S-1:e.end-S),0===C&&(e.write=S,m=e.vt(y,m),S=e.write,C=S<e.read?e.read-S-1:e.end-S,S==e.end&&0!==e.read&&(S=0,C=S<e.read?e.read-S-1:e.end-S),0===C)))return e.dt=_,e.yt=I,y.Je=v,y.$e+=x-y.lt,y.lt=x,e.write=S,e.vt(y,m);m=0,e.gt[S++]=f,C--,t=0;break;case 7:if(I>7&&(I-=8,v++,x--),e.write=S,m=e.vt(y,m),S=e.write,C=S<e.read?e.read-S-1:e.end-S,e.read!=e.write)return e.dt=_,e.yt=I,y.Je=v,y.$e+=x-y.lt,y.lt=x,e.write=S,e.vt(y,m);t=8;case 8:return m=1,e.dt=_,e.yt=I,y.Je=v,y.$e+=x-y.lt,y.lt=x,e.write=S,e.vt(y,m);case 9:return m=-3,e.dt=_,e.yt=I,y.Je=v,y.$e+=x-y.lt,y.lt=x,e.write=S,e.vt(y,m);default:return m=-2,e.dt=_,e.yt=I,y.Je=v,y.$e+=x-y.lt,y.lt=x,e.write=S,e.vt(y,m)}},e.St=()=>{}}Oe.Ct=(e,t,n,r)=>(e[0]=9,t[0]=5,n[0]=Te,r[0]=We,0);const Ge=[16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15];function Je(e,t){const n=this;let r,i=0,o=0,f=0,a=0;const l=[0],u=[0],w=new qe;let h=0,d=new c(4320);const p=new Oe;n.yt=0,n.dt=0,n.gt=new s(t),n.end=t,n.read=0,n.write=0,n.reset=(e,t)=>{t&&(t[0]=0),6==i&&w.St(e),i=0,n.yt=0,n.dt=0,n.read=n.write=0},n.reset(e,null),n.vt=(e,t)=>{let r,s,i;return s=e.ut,i=n.read,r=(i>n.write?n.end:n.write)-i,r>e.ft&&(r=e.ft),0!==r&&-5==t&&(t=0),e.ft-=r,e.et+=r,e.ot.set(n.gt.subarray(i,i+r),s),s+=r,i+=r,i==n.end&&(i=0,n.write==n.end&&(n.write=0),r=n.write-i,r>e.ft&&(r=e.ft),0!==r&&-5==t&&(t=0),e.ft-=r,e.et+=r,e.ot.set(n.gt.subarray(i,i+r),s),s+=r,i+=r),e.ut=s,n.read=i,t},n.kt=(e,t)=>{let s,c,y,m,b,g,k,v;for(m=e.lt,b=e.Je,c=n.dt,y=n.yt,g=n.write,k=g<n.read?n.read-g-1:n.end-g;;){let S,C,z,_,I,x,A,D;switch(i){case 0:for(;3>y;){if(0===b)return n.dt=c,n.yt=y,e.Je=b,e.$e+=m-e.lt,e.lt=m,n.write=g,n.vt(e,t);t=0,b--,c|=(255&e.bt(m++))<<y,y+=8}switch(s=7&c,h=1&s,s>>>1){case 0:c>>>=3,y-=3,s=7&y,c>>>=s,y-=s,i=1;break;case 1:S=[],C=[],z=[[]],_=[[]],Oe.Ct(S,C,z,_),w.init(S[0],C[0],z[0],0,_[0],0),c>>>=3,y-=3,i=6;break;case 2:c>>>=3,y-=3,i=3;break;case 3:return c>>>=3,y-=3,i=9,e.Ye="invalid block type",t=-3,n.dt=c,n.yt=y,e.Je=b,e.$e+=m-e.lt,e.lt=m,n.write=g,n.vt(e,t)}break;case 1:for(;32>y;){if(0===b)return n.dt=c,n.yt=y,e.Je=b,e.$e+=m-e.lt,e.lt=m,n.write=g,n.vt(e,t);t=0,b--,c|=(255&e.bt(m++))<<y,y+=8}if((~c>>>16&65535)!=(65535&c))return i=9,e.Ye="invalid stored block lengths",t=-3,n.dt=c,n.yt=y,e.Je=b,e.$e+=m-e.lt,e.lt=m,n.write=g,n.vt(e,t);o=65535&c,c=y=0,i=0!==o?2:0!==h?7:0;break;case 2:if(0===b)return n.dt=c,n.yt=y,e.Je=b,e.$e+=m-e.lt,e.lt=m,n.write=g,n.vt(e,t);if(0===k&&(g==n.end&&0!==n.read&&(g=0,k=g<n.read?n.read-g-1:n.end-g),0===k&&(n.write=g,t=n.vt(e,t),g=n.write,k=g<n.read?n.read-g-1:n.end-g,g==n.end&&0!==n.read&&(g=0,k=g<n.read?n.read-g-1:n.end-g),0===k)))return n.dt=c,n.yt=y,e.Je=b,e.$e+=m-e.lt,e.lt=m,n.write=g,n.vt(e,t);if(t=0,s=o,s>b&&(s=b),s>k&&(s=k),n.gt.set(e.Qe(m,s),g),m+=s,b-=s,g+=s,k-=s,0!=(o-=s))break;i=0!==h?7:0;break;case 3:for(;14>y;){if(0===b)return n.dt=c,n.yt=y,e.Je=b,e.$e+=m-e.lt,e.lt=m,n.write=g,n.vt(e,t);t=0,b--,c|=(255&e.bt(m++))<<y,y+=8}if(f=s=16383&c,(31&s)>29||(s>>5&31)>29)return i=9,e.Ye="too many length or distance symbols",t=-3,n.dt=c,n.yt=y,e.Je=b,e.$e+=m-e.lt,e.lt=m,n.write=g,n.vt(e,t);if(s=258+(31&s)+(s>>5&31),!r||r.length<s)r=[];else for(v=0;s>v;v++)r[v]=0;c>>>=14,y-=14,a=0,i=4;case 4:for(;4+(f>>>10)>a;){for(;3>y;){if(0===b)return n.dt=c,n.yt=y,e.Je=b,e.$e+=m-e.lt,e.lt=m,n.write=g,n.vt(e,t);t=0,b--,c|=(255&e.bt(m++))<<y,y+=8}r[Ge[a++]]=7&c,c>>>=3,y-=3}for(;19>a;)r[Ge[a++]]=0;if(l[0]=7,s=p.wt(r,l,u,d,e),0!=s)return-3==(t=s)&&(r=null,i=9),n.dt=c,n.yt=y,e.Je=b,e.$e+=m-e.lt,e.lt=m,n.write=g,n.vt(e,t);a=0,i=5;case 5:for(;s=f,258+(31&s)+(s>>5&31)>a;){let o,w;for(s=l[0];s>y;){if(0===b)return n.dt=c,n.yt=y,e.Je=b,e.$e+=m-e.lt,e.lt=m,n.write=g,n.vt(e,t);t=0,b--,c|=(255&e.bt(m++))<<y,y+=8}if(s=d[3*(u[0]+(c&Ue[s]))+1],w=d[3*(u[0]+(c&Ue[s]))+2],16>w)c>>>=s,y-=s,r[a++]=w;else{for(v=18==w?7:w-14,o=18==w?11:3;s+v>y;){if(0===b)return n.dt=c,n.yt=y,e.Je=b,e.$e+=m-e.lt,e.lt=m,n.write=g,n.vt(e,t);t=0,b--,c|=(255&e.bt(m++))<<y,y+=8}if(c>>>=s,y-=s,o+=c&Ue[v],c>>>=v,y-=v,v=a,s=f,v+o>258+(31&s)+(s>>5&31)||16==w&&1>v)return r=null,i=9,e.Ye="invalid bit length repeat",t=-3,n.dt=c,n.yt=y,e.Je=b,e.$e+=m-e.lt,e.lt=m,n.write=g,n.vt(e,t);w=16==w?r[v-1]:0;do{r[v++]=w}while(0!=--o);a=v}}if(u[0]=-1,I=[],x=[],A=[],D=[],I[0]=9,x[0]=6,s=f,s=p.ht(257+(31&s),1+(s>>5&31),r,I,x,A,D,d,e),0!=s)return-3==s&&(r=null,i=9),t=s,n.dt=c,n.yt=y,e.Je=b,e.$e+=m-e.lt,e.lt=m,n.write=g,n.vt(e,t);w.init(I[0],x[0],d,A[0],d,D[0]),i=6;case 6:if(n.dt=c,n.yt=y,e.Je=b,e.$e+=m-e.lt,e.lt=m,n.write=g,1!=(t=w.kt(n,e,t)))return n.vt(e,t);if(t=0,w.St(e),m=e.lt,b=e.Je,c=n.dt,y=n.yt,g=n.write,k=g<n.read?n.read-g-1:n.end-g,0===h){i=0;break}i=7;case 7:if(n.write=g,t=n.vt(e,t),g=n.write,k=g<n.read?n.read-g-1:n.end-g,n.read!=n.write)return n.dt=c,n.yt=y,e.Je=b,e.$e+=m-e.lt,e.lt=m,n.write=g,n.vt(e,t);i=8;case 8:return t=1,n.dt=c,n.yt=y,e.Je=b,e.$e+=m-e.lt,e.lt=m,n.write=g,n.vt(e,t);case 9:return t=-3,n.dt=c,n.yt=y,e.Je=b,e.$e+=m-e.lt,e.lt=m,n.write=g,n.vt(e,t);default:return t=-2,n.dt=c,n.yt=y,e.Je=b,e.$e+=m-e.lt,e.lt=m,n.write=g,n.vt(e,t)}}},n.St=e=>{n.reset(e,null),n.gt=null,d=null},n.zt=(e,t,r)=>{n.gt.set(e.subarray(t,t+r),0),n.read=n.write=r},n._t=()=>1==i?1:0}const Qe=[0,0,255,255];function Xe(){const e=this;function t(e){return e&&e.It?(e.$e=e.et=0,e.Ye=null,e.It.mode=7,e.It.xt.reset(e,null),0):-2}e.mode=0,e.method=0,e.At=[0],e.Dt=0,e.marker=0,e.Vt=0,e.Rt=t=>(e.xt&&e.xt.St(t),e.xt=null,0),e.Bt=(n,r)=>(n.Ye=null,e.xt=null,8>r||r>15?(e.Rt(n),-2):(e.Vt=r,n.It.xt=new Je(n,1<<r),t(n),0)),e.Et=(e,t)=>{let n,r;if(!e||!e.It||!e.ct)return-2;const s=e.It;for(t=4==t?-5:0,n=-5;;)switch(s.mode){case 0:if(0===e.Je)return n;if(n=t,e.Je--,e.$e++,8!=(15&(s.method=e.bt(e.lt++)))){s.mode=13,e.Ye="unknown compression method",s.marker=5;break}if(8+(s.method>>4)>s.Vt){s.mode=13,e.Ye="invalid win size",s.marker=5;break}s.mode=1;case 1:if(0===e.Je)return n;if(n=t,e.Je--,e.$e++,r=255&e.bt(e.lt++),((s.method<<8)+r)%31!=0){s.mode=13,e.Ye="incorrect header check",s.marker=5;break}if(0==(32&r)){s.mode=7;break}s.mode=2;case 2:if(0===e.Je)return n;n=t,e.Je--,e.$e++,s.Dt=(255&e.bt(e.lt++))<<24&4278190080,s.mode=3;case 3:if(0===e.Je)return n;n=t,e.Je--,e.$e++,s.Dt+=(255&e.bt(e.lt++))<<16&16711680,s.mode=4;case 4:if(0===e.Je)return n;n=t,e.Je--,e.$e++,s.Dt+=(255&e.bt(e.lt++))<<8&65280,s.mode=5;case 5:return 0===e.Je?n:(n=t,e.Je--,e.$e++,s.Dt+=255&e.bt(e.lt++),s.mode=6,2);case 6:return s.mode=13,e.Ye="need dictionary",s.marker=0,-2;case 7:if(n=s.xt.kt(e,n),-3==n){s.mode=13,s.marker=0;break}if(0==n&&(n=t),1!=n)return n;n=t,s.xt.reset(e,s.At),s.mode=12;case 12:return e.Je=0,1;case 13:return-3;default:return-2}},e.Mt=(e,t,n)=>{let r=0,s=n;if(!e||!e.It||6!=e.It.mode)return-2;const i=e.It;return s<1<<i.Vt||(s=(1<<i.Vt)-1,r=n-s),i.xt.zt(t,r,s),i.mode=7,0},e.Kt=e=>{let n,r,s,i,o;if(!e||!e.It)return-2;const c=e.It;if(13!=c.mode&&(c.mode=13,c.marker=0),0===(n=e.Je))return-5;for(r=e.lt,s=c.marker;0!==n&&4>s;)e.bt(r)==Qe[s]?s++:s=0!==e.bt(r)?0:4-s,r++,n--;return e.$e+=r-e.lt,e.lt=r,e.Je=n,c.marker=s,4!=s?-3:(i=e.$e,o=e.et,t(e),e.$e=i,e.et=o,c.mode=7,0)},e.Nt=e=>e&&e.It&&e.It.xt?e.It.xt._t():-2}function Ye(){}function Ze(e){const t=new Ye,i=e&&e.chunkSize?n.floor(2*e.chunkSize):131072,o=new s(i);let c=!1;t.Bt(),t.ot=o,this.append=(e,n)=>{const f=[];let a,l,u=0,w=0,h=0;if(0!==e.length){t.lt=0,t.ct=e,t.Je=e.length;do{if(t.ut=0,t.ft=i,0!==t.Je||c||(t.lt=0,c=!0),a=t.Et(0),c&&-5===a){if(0!==t.Je)throw new r("inflating: bad input")}else if(0!==a&&1!==a)throw new r("inflating: "+t.Ye);if((c||1===a)&&t.Je===e.length)throw new r("inflating: bad input");t.ut&&(t.ut===i?f.push(new s(o)):f.push(o.slice(0,t.ut))),h+=t.ut,n&&t.lt>0&&t.lt!=u&&(n(t.lt),u=t.lt)}while(t.Je>0||0===t.ft);return f.length>1?(l=new s(h),f.forEach((e=>{l.set(e,w),w+=e.length}))):l=f[0]||new s,l}},this.flush=()=>{t.Rt()}}Ye.prototype={Bt(e){const t=this;return t.It=new Xe,e||(e=15),t.It.Bt(t,e)},Et(e){const t=this;return t.It?t.It.Et(t,e):-2},Rt(){const e=this;if(!e.It)return-2;const t=e.It.Rt(e);return e.It=null,t},Kt(){const e=this;return e.It?e.It.Kt(e):-2},Mt(e,t){const n=this;return n.It?n.It.Mt(n,e,t):-2},bt(e){return this.ct[e]},Qe(e,t){return this.ct.subarray(e,e+t)}},self.initCodec=()=>{self.Deflate=Pe,self.Inflate=Ze};\n'],{type:"text/javascript"}));e({workerScripts:{inflate:[t],deflate:[t]}});}
1897
+ function pipeThrough(readable, transformStream) {
1898
+ return readable.pipeThrough(transformStream);
1899
+ }
1840
1900
 
1841
1901
  /*
1842
1902
  Copyright (c) 2022 Gildas Lormeau. All rights reserved.
@@ -1866,56 +1926,50 @@
1866
1926
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1867
1927
  */
1868
1928
 
1869
- function getMimeType() {
1870
- return "application/octet-stream";
1871
- }
1872
-
1873
- function initShimAsyncCodec(library, options = {}, registerDataHandler) {
1874
- return {
1875
- Deflate: createCodecClass(library.Deflate, options.deflate, registerDataHandler),
1876
- Inflate: createCodecClass(library.Inflate, options.inflate, registerDataHandler)
1877
- };
1878
- }
1879
-
1880
- function createCodecClass(constructor, constructorOptions, registerDataHandler) {
1881
- return class {
1929
+ const MESSAGE_EVENT_TYPE = "message";
1930
+ const MESSAGE_START = "start";
1931
+ const MESSAGE_PULL = "pull";
1932
+ const MESSAGE_DATA = "data";
1933
+ const MESSAGE_ACK_DATA = "ack";
1934
+ const MESSAGE_CLOSE = "close";
1935
+ const CODEC_DEFLATE = "deflate";
1936
+ const CODEC_INFLATE = "inflate";
1882
1937
 
1883
- constructor(options) {
1884
- const codecAdapter = this;
1885
- const onData = data => {
1886
- if (codecAdapter.pendingData) {
1887
- const { pendingData } = codecAdapter;
1888
- codecAdapter.pendingData = new Uint8Array(pendingData.length + data.length);
1889
- pendingData.set(pendingData, 0);
1890
- pendingData.set(data, pendingData.length);
1891
- } else {
1892
- codecAdapter.pendingData = new Uint8Array(data);
1893
- }
1894
- };
1895
- if (Object.hasOwn(options, "level") && options.level === undefined) {
1896
- delete options.level;
1897
- }
1898
- codecAdapter.codec = new constructor(Object.assign({}, constructorOptions, options));
1899
- registerDataHandler(codecAdapter.codec, onData);
1900
- }
1901
- append(data) {
1902
- this.codec.push(data);
1903
- return getResponse(this);
1904
- }
1905
- flush() {
1906
- this.codec.push(new Uint8Array(), true);
1907
- return getResponse(this);
1908
- }
1909
- };
1938
+ class CodecStream extends TransformStream {
1910
1939
 
1911
- function getResponse(codec) {
1912
- if (codec.pendingData) {
1913
- const output = codec.pendingData;
1914
- codec.pendingData = null;
1915
- return output;
1916
- } else {
1917
- return new Uint8Array();
1940
+ constructor(options, config) {
1941
+ super({});
1942
+ const codec = this;
1943
+ const { codecType } = options;
1944
+ let Stream;
1945
+ if (codecType.startsWith(CODEC_DEFLATE)) {
1946
+ Stream = DeflateStream;
1947
+ } else if (codecType.startsWith(CODEC_INFLATE)) {
1948
+ Stream = InflateStream;
1918
1949
  }
1950
+ let size = 0;
1951
+ const stream = new Stream(options, config);
1952
+ const readable = super.readable;
1953
+ const transformStream = new TransformStream({
1954
+ transform(chunk, controller) {
1955
+ if (chunk && chunk.length) {
1956
+ size += chunk.length;
1957
+ controller.enqueue(chunk);
1958
+ }
1959
+ },
1960
+ flush() {
1961
+ const { signature } = stream;
1962
+ Object.assign(codec, {
1963
+ signature,
1964
+ size
1965
+ });
1966
+ }
1967
+ });
1968
+ Object.defineProperty(codec, "readable", {
1969
+ get() {
1970
+ return readable.pipeThrough(stream).pipeThrough(transformStream);
1971
+ }
1972
+ });
1919
1973
  }
1920
1974
  }
1921
1975
 
@@ -1947,25 +2001,26 @@
1947
2001
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1948
2002
  */
1949
2003
 
1950
- let workersSupported;
1951
- let classicWorkersSupported = true;
1952
- let transferStreamsSupported = true;
2004
+ // deno-lint-ignore valid-typeof
2005
+ const WEB_WORKERS_SUPPORTED = typeof Worker != UNDEFINED_TYPE$1;
1953
2006
 
1954
2007
  class CodecWorker {
1955
2008
 
1956
2009
  constructor(workerData, stream, workerOptions, onTaskFinished) {
1957
2010
  const { readable, writable } = stream;
1958
2011
  const { options, config, streamOptions, useWebWorkers, transferStreams, scripts } = workerOptions;
2012
+ const { signal } = streamOptions;
1959
2013
  Object.assign(workerData, {
1960
2014
  busy: true,
1961
- readable,
2015
+ readable: readable.pipeThrough(new ReadableStreamHooks(readable, streamOptions, config), { signal }),
1962
2016
  writable,
1963
2017
  options: Object.assign({}, options),
1964
2018
  scripts,
1965
2019
  transferStreams,
1966
2020
  terminate() {
1967
- if (workerData.worker && !workerData.busy) {
1968
- workerData.worker.terminate();
2021
+ const { worker, busy } = workerData;
2022
+ if (worker && !busy) {
2023
+ worker.terminate();
1969
2024
  workerData.interface = null;
1970
2025
  }
1971
2026
  },
@@ -1974,30 +2029,34 @@
1974
2029
  onTaskFinished(workerData);
1975
2030
  }
1976
2031
  });
1977
- const { signal, onstart, onprogress, size, onend } = streamOptions;
2032
+ return (useWebWorkers && WEB_WORKERS_SUPPORTED ? createWebWorkerInterface : createWorkerInterface)(workerData, config);
2033
+ }
2034
+ }
2035
+
2036
+ class ReadableStreamHooks extends TransformStream {
2037
+
2038
+ constructor(readableSource, { onstart, onprogress, size, onend }, { chunkSize }) {
1978
2039
  let chunkOffset = 0;
1979
- const transformer = {};
1980
- if (onstart) {
1981
- transformer.start = () => callHandler(onstart, size);
1982
- }
1983
- transformer.transform = async (chunk, controller) => {
1984
- chunkOffset += chunk.length;
1985
- if (onprogress) {
1986
- await callHandler(onprogress, chunkOffset, size);
1987
- }
1988
- controller.enqueue(chunk);
1989
- };
1990
- transformer.flush = () => {
1991
- readable.size = chunkOffset;
1992
- if (onend) {
1993
- callHandler(onend, chunkOffset);
2040
+ super({
2041
+ start() {
2042
+ if (onstart) {
2043
+ callHandler(onstart, size);
2044
+ }
2045
+ },
2046
+ async transform(chunk, controller) {
2047
+ chunkOffset += chunk.length;
2048
+ if (onprogress) {
2049
+ await callHandler(onprogress, chunkOffset, size);
2050
+ }
2051
+ controller.enqueue(chunk);
2052
+ },
2053
+ flush() {
2054
+ readableSource.size = chunkOffset;
2055
+ if (onend) {
2056
+ callHandler(onend, chunkOffset);
2057
+ }
1994
2058
  }
1995
- };
1996
- workerData.readable = readable.pipeThrough(new TransformStream(transformer, { highWaterMark: 1, size: () => config.chunkSize }), { signal });
1997
- if (workersSupported === undefined) {
1998
- workersSupported = typeof Worker != "undefined";
1999
- }
2000
- return useWebWorkers && workersSupported ? createWebWorkerInterface(workerData, config) : createWorkerInterface(workerData, config);
2059
+ }, { highWaterMark: 1, size: () => chunkSize });
2001
2060
  }
2002
2061
  }
2003
2062
 
@@ -2010,186 +2069,209 @@
2010
2069
  }
2011
2070
 
2012
2071
  function createWorkerInterface(workerData, config) {
2013
- const interfaceCodec = new Codec(workerData.readable, workerData.writable, workerData.options, config);
2014
- const { onTaskFinished } = workerData;
2015
- const codec = {
2016
- async run() {
2017
- try {
2018
- return await interfaceCodec.run();
2019
- } finally {
2020
- onTaskFinished();
2021
- }
2022
- }
2072
+ return {
2073
+ run: () => runWorker$1(workerData, config)
2023
2074
  };
2024
- return codec;
2025
2075
  }
2026
2076
 
2027
2077
  function createWebWorkerInterface(workerData, { baseURL, chunkSize }) {
2028
- const workerOptions = { type: "module" };
2029
- Object.assign(workerData, {
2030
- streams: {},
2031
- result: new Promise((resolve, reject) => {
2032
- workerData.resolveResult = resolve;
2033
- workerData.rejectResult = reject;
2034
- })
2035
- });
2036
2078
  if (!workerData.interface) {
2037
- if (!classicWorkersSupported) {
2038
- workerData.worker = getWorker(workerOptions);
2039
- } else {
2040
- try {
2041
- workerData.worker = getWorker({});
2042
- } catch (_error) {
2043
- classicWorkersSupported = false;
2044
- workerData.worker = getWorker(workerOptions);
2045
- }
2046
- }
2047
- workerData.worker.addEventListener(MESSAGE_EVENT_TYPE, onMessage, false);
2048
- workerData.interface = {
2049
- async run() {
2050
- const { readable, options } = workerData;
2051
- const writer = workerData.writable.getWriter();
2052
- let resolveStreamClosed, streamClosed;
2053
- streamClosed = new Promise(resolve => resolveStreamClosed = resolve);
2054
- const writable = new WritableStream({
2055
- async write(chunk) {
2056
- await writer.ready;
2057
- await writer.write(chunk);
2058
- },
2059
- async close() {
2060
- writer.releaseLock();
2061
- resolveStreamClosed();
2062
- },
2063
- abort(reason) {
2064
- writer.abort(reason);
2065
- }
2066
- });
2067
- const scripts = workerData.scripts.slice(1);
2068
- const message = { type: MESSAGE_START, scripts, options, config: { chunkSize } };
2069
- if (workerData.transferStreams && transferStreamsSupported) {
2070
- const streamsTransferred = sendMessage({ ...message, readable, writable });
2071
- if (!streamsTransferred) {
2072
- transferStreamsSupported = false;
2073
- initStreams(readable, writable);
2074
- }
2075
- } else {
2076
- sendMessage(message);
2077
- initStreams(readable, writable);
2078
- }
2079
- const result = await workerData.result;
2080
- try {
2081
- await writable.close();
2082
- } catch (_error) {
2083
- // ignored
2084
- }
2085
- await streamClosed;
2086
- return result;
2079
+ Object.assign(workerData, {
2080
+ worker: getWebWorker(workerData.scripts[0], baseURL, workerData),
2081
+ interface: {
2082
+ run: () => runWebWorker(workerData, { chunkSize })
2087
2083
  }
2088
- };
2084
+ });
2089
2085
  }
2090
2086
  return workerData.interface;
2087
+ }
2088
+
2089
+ async function runWorker$1({ options, readable, writable, onTaskFinished }, config) {
2090
+ const codecStream = new CodecStream(options, config);
2091
+ try {
2092
+ await readable.pipeThrough(codecStream).pipeTo(writable, { preventClose: true, preventAbort: true });
2093
+ const {
2094
+ signature,
2095
+ size
2096
+ } = codecStream;
2097
+ return {
2098
+ signature,
2099
+ size
2100
+ };
2101
+ } finally {
2102
+ onTaskFinished();
2103
+ }
2104
+ }
2091
2105
 
2092
- function initStreams(readable, writable) {
2093
- Object.assign(workerData.streams, {
2106
+ async function runWebWorker(workerData, config) {
2107
+ let resolveResult, rejectResult;
2108
+ const result = new Promise((resolve, reject) => {
2109
+ resolveResult = resolve;
2110
+ rejectResult = reject;
2111
+ });
2112
+ Object.assign(workerData, {
2113
+ reader: null,
2114
+ writer: null,
2115
+ resolveResult,
2116
+ rejectResult,
2117
+ result
2118
+ });
2119
+ const { readable, options } = workerData;
2120
+ const { writable, closed } = detectClosedStream(workerData.writable);
2121
+ const streamsTransferred = sendMessage({
2122
+ type: MESSAGE_START,
2123
+ scripts: workerData.scripts.slice(1),
2124
+ options,
2125
+ config,
2126
+ readable,
2127
+ writable
2128
+ }, workerData);
2129
+ if (!streamsTransferred) {
2130
+ Object.assign(workerData, {
2094
2131
  reader: readable.getReader(),
2095
2132
  writer: writable.getWriter()
2096
2133
  });
2097
2134
  }
2135
+ const resultValue = await result;
2136
+ try {
2137
+ await writable.close();
2138
+ } catch (_error) {
2139
+ // ignored
2140
+ }
2141
+ await closed;
2142
+ return resultValue;
2143
+ }
2098
2144
 
2099
- function getWorker(options) {
2100
- let url, scriptUrl;
2101
- url = workerData.scripts[0];
2102
- if (typeof url == "function") {
2103
- url = url();
2145
+ function detectClosedStream(writableSource) {
2146
+ const writer = writableSource.getWriter();
2147
+ let resolveStreamClosed;
2148
+ const closed = new Promise(resolve => resolveStreamClosed = resolve);
2149
+ const writable = new WritableStream({
2150
+ async write(chunk) {
2151
+ await writer.ready;
2152
+ await writer.write(chunk);
2153
+ },
2154
+ close() {
2155
+ writer.releaseLock();
2156
+ resolveStreamClosed();
2157
+ },
2158
+ abort(reason) {
2159
+ writer.abort(reason);
2104
2160
  }
2161
+ });
2162
+ return { writable, closed };
2163
+ }
2164
+
2165
+ let classicWorkersSupported = true;
2166
+ let transferStreamsSupported = true;
2167
+
2168
+ function getWebWorker(url, baseURL, workerData) {
2169
+ const workerOptions = { type: "module" };
2170
+ let scriptUrl, worker;
2171
+ if (typeof url == FUNCTION_TYPE$1) {
2172
+ url = url();
2173
+ }
2174
+ try {
2175
+ scriptUrl = new URL(url, baseURL);
2176
+ } catch (_error) {
2177
+ scriptUrl = url;
2178
+ }
2179
+ if (classicWorkersSupported) {
2105
2180
  try {
2106
- scriptUrl = new URL(url, baseURL);
2181
+ worker = new Worker(scriptUrl);
2107
2182
  } catch (_error) {
2108
- scriptUrl = url;
2183
+ classicWorkersSupported = false;
2184
+ worker = new Worker(scriptUrl, workerOptions);
2109
2185
  }
2110
- return new Worker(scriptUrl, options);
2186
+ } else {
2187
+ worker = new Worker(scriptUrl, workerOptions);
2111
2188
  }
2189
+ worker.addEventListener(MESSAGE_EVENT_TYPE, event => onMessage(event, workerData));
2190
+ return worker;
2191
+ }
2112
2192
 
2113
- function sendMessage(message) {
2114
- const { worker, writer, onTaskFinished } = workerData;
2115
- try {
2116
- let { data, readable, writable } = message;
2117
- const transferrables = [];
2118
- if (data) {
2119
- const { buffer, length } = data;
2120
- if (length != buffer.byteLength) {
2121
- data = new Uint8Array(data);
2122
- }
2123
- message.data = data.buffer;
2124
- transferrables.push(message.data);
2193
+ function sendMessage(message, { worker, writer, onTaskFinished, transferStreams }) {
2194
+ try {
2195
+ let { data, readable, writable } = message;
2196
+ const transferrables = [];
2197
+ if (data) {
2198
+ const { buffer, length } = data;
2199
+ if (length != buffer.byteLength) {
2200
+ data = new Uint8Array(data);
2125
2201
  }
2202
+ message.data = data.buffer;
2203
+ transferrables.push(message.data);
2204
+ }
2205
+ if (transferStreams && transferStreamsSupported) {
2126
2206
  if (readable) {
2127
2207
  transferrables.push(readable);
2128
2208
  }
2129
2209
  if (writable) {
2130
2210
  transferrables.push(writable);
2131
2211
  }
2132
- if (transferrables.length) {
2133
- try {
2134
- worker.postMessage(message, transferrables);
2135
- return true;
2136
- } catch (error) {
2137
- message.readable = message.writable = null;
2138
- worker.postMessage(message);
2139
- }
2140
- } else {
2212
+ } else {
2213
+ message.readable = message.writable = null;
2214
+ }
2215
+ if (transferrables.length) {
2216
+ try {
2217
+ worker.postMessage(message, transferrables);
2218
+ return true;
2219
+ } catch (_error) {
2220
+ transferStreamsSupported = false;
2221
+ message.readable = message.writable = null;
2141
2222
  worker.postMessage(message);
2142
2223
  }
2143
- } catch (error) {
2144
- if (writer) {
2145
- writer.releaseLock();
2146
- }
2147
- onTaskFinished();
2148
- throw error;
2224
+ } else {
2225
+ worker.postMessage(message);
2226
+ }
2227
+ } catch (error) {
2228
+ if (writer) {
2229
+ writer.releaseLock();
2149
2230
  }
2231
+ onTaskFinished();
2232
+ throw error;
2150
2233
  }
2234
+ }
2151
2235
 
2152
- async function onMessage(event) {
2153
- const message = event.data;
2154
- const { resolveResult, rejectResult, onTaskFinished } = workerData;
2155
- const { reader, writer } = workerData.streams;
2156
- const { type, data, messageId, result } = message;
2157
- const reponseError = message.error;
2158
- try {
2159
- if (reponseError) {
2160
- const { message, stack, code, name } = reponseError;
2161
- const error = new Error(message);
2162
- Object.assign(error, { stack, code, name });
2163
- close(error);
2164
- } else {
2165
- if (type == MESSAGE_PULL) {
2166
- const { value, done } = await reader.read();
2167
- sendMessage({ type: MESSAGE_DATA, data: value, done, messageId });
2168
- }
2169
- if (type == MESSAGE_DATA) {
2170
- await writer.ready;
2171
- await writer.write(new Uint8Array(data));
2172
- sendMessage({ type: MESSAGE_ACK_DATA, messageId });
2173
- }
2174
- if (type == MESSAGE_CLOSE) {
2175
- close(null, result);
2176
- }
2177
- }
2178
- } catch (error) {
2236
+ async function onMessage(event, workerData) {
2237
+ const message = event.data;
2238
+ const { reader, writer, resolveResult, rejectResult, onTaskFinished } = workerData;
2239
+ const { type, data, messageId, result } = message;
2240
+ const reponseError = message.error;
2241
+ try {
2242
+ if (reponseError) {
2243
+ const { message, stack, code, name } = reponseError;
2244
+ const error = new Error(message);
2245
+ Object.assign(error, { stack, code, name });
2179
2246
  close(error);
2180
- }
2181
-
2182
- function close(error, result) {
2183
- if (error) {
2184
- rejectResult(error);
2185
- } else {
2186
- resolveResult(result);
2247
+ } else {
2248
+ if (type == MESSAGE_PULL) {
2249
+ const { value, done } = await reader.read();
2250
+ sendMessage({ type: MESSAGE_DATA, data: value, done, messageId }, workerData);
2187
2251
  }
2188
- if (writer) {
2189
- writer.releaseLock();
2252
+ if (type == MESSAGE_DATA) {
2253
+ await writer.ready;
2254
+ await writer.write(new Uint8Array(data));
2255
+ sendMessage({ type: MESSAGE_ACK_DATA, messageId }, workerData);
2190
2256
  }
2191
- onTaskFinished();
2257
+ if (type == MESSAGE_CLOSE) {
2258
+ close(null, result);
2259
+ }
2260
+ }
2261
+ } catch (error) {
2262
+ close(error);
2263
+ }
2264
+
2265
+ function close(error, result) {
2266
+ if (error) {
2267
+ rejectResult(error);
2268
+ } else {
2269
+ resolveResult(result);
2192
2270
  }
2271
+ if (writer) {
2272
+ writer.releaseLock();
2273
+ }
2274
+ onTaskFinished();
2193
2275
  }
2194
2276
  }
2195
2277
 
@@ -2230,11 +2312,11 @@
2230
2312
  const { options, config } = workerOptions;
2231
2313
  const { transferStreams, useWebWorkers, useCompressionStream, codecType, compressed, signed, encrypted } = options;
2232
2314
  const { workerScripts } = config;
2233
- workerOptions.transferStreams = transferStreams || transferStreams === undefined;
2315
+ workerOptions.transferStreams = transferStreams || transferStreams === UNDEFINED_VALUE;
2234
2316
  const streamCopy = !compressed && !signed && !encrypted && !workerOptions.transferStreams;
2235
- workerOptions.useWebWorkers = !streamCopy && (useWebWorkers || (useWebWorkers === undefined && config.useWebWorkers));
2317
+ workerOptions.useWebWorkers = !streamCopy && (useWebWorkers || (useWebWorkers === UNDEFINED_VALUE && config.useWebWorkers));
2236
2318
  workerOptions.scripts = workerOptions.useWebWorkers && workerScripts ? workerScripts[codecType] : [];
2237
- options.useCompressionStream = useCompressionStream || (useCompressionStream === undefined && config.useCompressionStream);
2319
+ options.useCompressionStream = useCompressionStream || (useCompressionStream === UNDEFINED_VALUE && config.useCompressionStream);
2238
2320
  let worker;
2239
2321
  if (pool.length < config.maxWorkers) {
2240
2322
  const workerData = { indexWorker };
@@ -2315,20 +2397,25 @@
2315
2397
  */
2316
2398
 
2317
2399
  /* global Blob, atob, btoa, XMLHttpRequest, URL, fetch, ReadableStream, WritableStream, FileReader, TransformStream, Response */
2400
+ // deno-lint-ignore-file no-this-alias
2318
2401
 
2319
2402
  const ERR_HTTP_STATUS = "HTTP error ";
2320
2403
  const ERR_HTTP_RANGE = "HTTP Range not supported";
2404
+ const ERR_ITERATOR_COMPLETED_TOO_SOON = "Writer iterator completed too soon";
2321
2405
 
2322
2406
  const CONTENT_TYPE_TEXT_PLAIN = "text/plain";
2323
2407
  const HTTP_HEADER_CONTENT_LENGTH = "Content-Length";
2324
2408
  const HTTP_HEADER_CONTENT_RANGE = "Content-Range";
2325
2409
  const HTTP_HEADER_ACCEPT_RANGES = "Accept-Ranges";
2326
2410
  const HTTP_HEADER_RANGE = "Range";
2411
+ const HTTP_HEADER_CONTENT_TYPE = "Content-Type";
2327
2412
  const HTTP_METHOD_HEAD = "HEAD";
2328
2413
  const HTTP_METHOD_GET = "GET";
2329
2414
  const HTTP_RANGE_UNIT = "bytes";
2330
2415
  const DEFAULT_CHUNK_SIZE = 64 * 1024;
2331
2416
 
2417
+ const PROPERTY_NAME_WRITABLE = "writable";
2418
+
2332
2419
  class Stream {
2333
2420
 
2334
2421
  constructor() {
@@ -2374,7 +2461,7 @@
2374
2461
  return writer.writeUint8Array(chunk);
2375
2462
  }
2376
2463
  });
2377
- Object.defineProperty(writer, "writable", {
2464
+ Object.defineProperty(writer, PROPERTY_NAME_WRITABLE, {
2378
2465
  get() {
2379
2466
  return writable;
2380
2467
  }
@@ -2390,21 +2477,26 @@
2390
2477
 
2391
2478
  constructor(dataURI) {
2392
2479
  super();
2393
- const reader = this;
2394
- reader.dataURI = dataURI;
2395
2480
  let dataEnd = dataURI.length;
2396
2481
  while (dataURI.charAt(dataEnd - 1) == "=") {
2397
2482
  dataEnd--;
2398
2483
  }
2399
- reader.dataStart = dataURI.indexOf(",") + 1;
2400
- reader.size = Math.floor((dataEnd - reader.dataStart) * 0.75);
2484
+ const dataStart = dataURI.indexOf(",") + 1;
2485
+ Object.assign(this, {
2486
+ dataURI,
2487
+ dataStart,
2488
+ size: Math.floor((dataEnd - dataStart) * 0.75)
2489
+ });
2401
2490
  }
2402
2491
 
2403
2492
  readUint8Array(offset, length) {
2493
+ const {
2494
+ dataStart,
2495
+ dataURI
2496
+ } = this;
2404
2497
  const dataArray = new Uint8Array(length);
2405
2498
  const start = Math.floor(offset / 3) * 4;
2406
- const dataStart = this.dataStart;
2407
- const bytes = atob(this.dataURI.substring(start + dataStart, Math.ceil((offset + length) / 3) * 4 + dataStart));
2499
+ const bytes = atob(dataURI.substring(start + dataStart, Math.ceil((offset + length) / 3) * 4 + dataStart));
2408
2500
  const delta = offset - Math.floor(start / 4) * 3;
2409
2501
  for (let indexByte = delta; indexByte < delta + length; indexByte++) {
2410
2502
  dataArray[indexByte - delta] = bytes.charCodeAt(indexByte);
@@ -2417,8 +2509,10 @@
2417
2509
 
2418
2510
  constructor(contentType) {
2419
2511
  super();
2420
- this.data = "data:" + (contentType || "") + ";base64,";
2421
- this.pending = [];
2512
+ Object.assign(this, {
2513
+ data: "data:" + (contentType || "") + ";base64,",
2514
+ pending: []
2515
+ });
2422
2516
  }
2423
2517
 
2424
2518
  writeUint8Array(array) {
@@ -2449,8 +2543,10 @@
2449
2543
 
2450
2544
  constructor(blob) {
2451
2545
  super();
2452
- this.blob = blob;
2453
- this.size = blob.size;
2546
+ Object.assign(this, {
2547
+ blob,
2548
+ size: blob.size
2549
+ });
2454
2550
  }
2455
2551
 
2456
2552
  async readUint8Array(offset, length) {
@@ -2467,9 +2563,9 @@
2467
2563
  const transformStream = new TransformStream();
2468
2564
  const headers = [];
2469
2565
  if (contentType) {
2470
- headers.push(["content-type", contentType]);
2566
+ headers.push([HTTP_HEADER_CONTENT_TYPE, contentType]);
2471
2567
  }
2472
- Object.defineProperty(writer, "writable", {
2568
+ Object.defineProperty(writer, PROPERTY_NAME_WRITABLE, {
2473
2569
  get() {
2474
2570
  return transformStream.writable;
2475
2571
  }
@@ -2510,8 +2606,10 @@
2510
2606
  } else {
2511
2607
  const reader = new FileReader();
2512
2608
  return new Promise((resolve, reject) => {
2513
- reader.onload = ({ target }) => resolve(target.result);
2514
- reader.onerror = () => reject(reader.error);
2609
+ Object.assign(reader, {
2610
+ onload: ({ target }) => resolve(target.result),
2611
+ onerror: () => reject(reader.error)
2612
+ });
2515
2613
  reader.readAsText(blob, encoding);
2516
2614
  });
2517
2615
  }
@@ -2522,16 +2620,7 @@
2522
2620
 
2523
2621
  constructor(url, options) {
2524
2622
  super();
2525
- const reader = this;
2526
- reader.url = url;
2527
- reader.preventHeadRequest = options.preventHeadRequest;
2528
- reader.useRangeHeader = options.useRangeHeader;
2529
- reader.forceRangeRequests = options.forceRangeRequests;
2530
- reader.options = options = Object.assign({}, options);
2531
- delete options.preventHeadRequest;
2532
- delete options.useRangeHeader;
2533
- delete options.forceRangeRequests;
2534
- delete options.useXHR;
2623
+ createHtpReader(this, url, options);
2535
2624
  }
2536
2625
 
2537
2626
  async init() {
@@ -2548,12 +2637,7 @@
2548
2637
 
2549
2638
  constructor(url, options) {
2550
2639
  super();
2551
- const reader = this;
2552
- reader.url = url;
2553
- reader.preventHeadRequest = options.preventHeadRequest;
2554
- reader.useRangeHeader = options.useRangeHeader;
2555
- reader.forceRangeRequests = options.forceRangeRequests;
2556
- reader.options = options;
2640
+ createHtpReader(this, url, options);
2557
2641
  }
2558
2642
 
2559
2643
  async init() {
@@ -2566,14 +2650,39 @@
2566
2650
  }
2567
2651
  }
2568
2652
 
2653
+ function createHtpReader(httpReader, url, options) {
2654
+ const {
2655
+ preventHeadRequest,
2656
+ useRangeHeader,
2657
+ forceRangeRequests
2658
+ } = options;
2659
+ options = Object.assign({}, options);
2660
+ delete options.preventHeadRequest;
2661
+ delete options.useRangeHeader;
2662
+ delete options.forceRangeRequests;
2663
+ delete options.useXHR;
2664
+ Object.assign(httpReader, {
2665
+ url,
2666
+ options,
2667
+ preventHeadRequest,
2668
+ useRangeHeader,
2669
+ forceRangeRequests
2670
+ });
2671
+ }
2672
+
2569
2673
  async function initHttpReader(httpReader, sendRequest, getRequestData) {
2570
- if (isHttpFamily(httpReader.url) && (httpReader.useRangeHeader || httpReader.forceRangeRequests)) {
2571
- const response = await sendRequest(HTTP_METHOD_GET, httpReader, getRangeHeaders(httpReader));
2572
- if (!httpReader.forceRangeRequests && response.headers.get(HTTP_HEADER_ACCEPT_RANGES) != HTTP_RANGE_UNIT) {
2674
+ const {
2675
+ url,
2676
+ useRangeHeader,
2677
+ forceRangeRequests
2678
+ } = httpReader;
2679
+ if (isHttpFamily(url) && (useRangeHeader || forceRangeRequests)) {
2680
+ const { headers } = await sendRequest(HTTP_METHOD_GET, httpReader, getRangeHeaders(httpReader));
2681
+ if (!forceRangeRequests && headers.get(HTTP_HEADER_ACCEPT_RANGES) != HTTP_RANGE_UNIT) {
2573
2682
  throw new Error(ERR_HTTP_RANGE);
2574
2683
  } else {
2575
2684
  let contentSize;
2576
- const contentRangeHeader = response.headers.get(HTTP_HEADER_CONTENT_RANGE);
2685
+ const contentRangeHeader = headers.get(HTTP_HEADER_CONTENT_RANGE);
2577
2686
  if (contentRangeHeader) {
2578
2687
  const splitHeader = contentRangeHeader.trim().split(/\s*\/\s*/);
2579
2688
  if (splitHeader.length) {
@@ -2583,7 +2692,7 @@
2583
2692
  }
2584
2693
  }
2585
2694
  }
2586
- if (contentSize === undefined) {
2695
+ if (contentSize === UNDEFINED_VALUE) {
2587
2696
  await getContentLength(httpReader, sendRequest, getRequestData);
2588
2697
  } else {
2589
2698
  httpReader.size = contentSize;
@@ -2595,17 +2704,23 @@
2595
2704
  }
2596
2705
 
2597
2706
  async function readUint8ArrayHttpReader(httpReader, index, length, sendRequest, getRequestData) {
2598
- if (httpReader.useRangeHeader || httpReader.forceRangeRequests) {
2707
+ const {
2708
+ data,
2709
+ useRangeHeader,
2710
+ forceRangeRequests,
2711
+ options
2712
+ } = httpReader;
2713
+ if (useRangeHeader || forceRangeRequests) {
2599
2714
  const response = await sendRequest(HTTP_METHOD_GET, httpReader, getRangeHeaders(httpReader, index, length));
2600
2715
  if (response.status != 206) {
2601
2716
  throw new Error(ERR_HTTP_RANGE);
2602
2717
  }
2603
2718
  return new Uint8Array(await response.arrayBuffer());
2604
2719
  } else {
2605
- if (!httpReader.data) {
2606
- await getRequestData(httpReader, httpReader.options);
2720
+ if (!data) {
2721
+ await getRequestData(httpReader, options);
2607
2722
  }
2608
- return new Uint8Array(httpReader.data.subarray(index, index + length));
2723
+ return new Uint8Array(data.subarray(index, index + length));
2609
2724
  }
2610
2725
  }
2611
2726
 
@@ -2613,8 +2728,8 @@
2613
2728
  return Object.assign({}, getHeaders(httpReader), { [HTTP_HEADER_RANGE]: HTTP_RANGE_UNIT + "=" + index + "-" + (index + length - 1) });
2614
2729
  }
2615
2730
 
2616
- function getHeaders(httpReader) {
2617
- const headers = httpReader.options.headers;
2731
+ function getHeaders({ options }) {
2732
+ const { headers } = options;
2618
2733
  if (headers) {
2619
2734
  if (Symbol.iterator in headers) {
2620
2735
  return Object.fromEntries(headers);
@@ -2699,9 +2814,10 @@
2699
2814
 
2700
2815
  constructor(url, options = {}) {
2701
2816
  super();
2702
- const reader = this;
2703
- reader.url = url;
2704
- reader.reader = options.useXHR ? new XHRReader(url, options) : new FetchReader(url, options);
2817
+ Object.assign(this, {
2818
+ url,
2819
+ reader: options.useXHR ? new XHRReader(url, options) : new FetchReader(url, options)
2820
+ });
2705
2821
  }
2706
2822
 
2707
2823
  set size(value) {
@@ -2735,8 +2851,10 @@
2735
2851
 
2736
2852
  constructor(array) {
2737
2853
  super();
2738
- this.array = array;
2739
- this.size = array.length;
2854
+ Object.assign(this, {
2855
+ array,
2856
+ size: array.length
2857
+ });
2740
2858
  }
2741
2859
 
2742
2860
  readUint8Array(index, length) {
@@ -2748,9 +2866,10 @@
2748
2866
 
2749
2867
  init(initSize = 0) {
2750
2868
  super.init();
2751
- const writer = this;
2752
- writer.offset = 0;
2753
- writer.array = new Uint8Array(initSize);
2869
+ Object.assign(this, {
2870
+ offset: 0,
2871
+ array: new Uint8Array(initSize)
2872
+ });
2754
2873
  }
2755
2874
 
2756
2875
  writeUint8Array(array) {
@@ -2815,6 +2934,65 @@
2815
2934
  }
2816
2935
  }
2817
2936
 
2937
+ class SplitZipWriter extends Stream {
2938
+
2939
+ constructor(writerGenerator, maxSize = 4294967296) {
2940
+ super();
2941
+ const writer = this;
2942
+ Object.assign(writer, {
2943
+ diskNumber: 0,
2944
+ diskOffset: 0,
2945
+ size: 0,
2946
+ availableSize: maxSize
2947
+ });
2948
+ let currentWriter, diskWritable, diskWriter;
2949
+ const writable = new WritableStream({
2950
+ async write(chunk) {
2951
+ if (!diskWriter) {
2952
+ const { value, done } = await writerGenerator.next();
2953
+ if (done && !value) {
2954
+ throw new Error(ERR_ITERATOR_COMPLETED_TOO_SOON);
2955
+ } else {
2956
+ currentWriter = value;
2957
+ currentWriter.size = 0;
2958
+ diskWritable = value.writable;
2959
+ diskWriter = diskWritable.getWriter();
2960
+ }
2961
+ await this.write(chunk);
2962
+ } else if (diskWriter && currentWriter.size + chunk.length > maxSize) {
2963
+ await diskWriter.ready;
2964
+ const chunkLength = maxSize - currentWriter.size;
2965
+ await diskWriter.write(chunk.slice(0, chunkLength));
2966
+ chunk = chunk.slice(chunkLength);
2967
+ currentWriter.size += chunkLength;
2968
+ writer.size += chunkLength;
2969
+ writer.diskOffset += diskWritable.size = currentWriter.size;
2970
+ writer.diskNumber++;
2971
+ await diskWriter.close();
2972
+ diskWriter = null;
2973
+ await this.write(chunk);
2974
+ } else {
2975
+ await diskWriter.ready;
2976
+ await diskWriter.write(chunk);
2977
+ currentWriter.size += chunk.length;
2978
+ writer.size += chunk.length;
2979
+ }
2980
+ writer.availableSize = maxSize - currentWriter.size;
2981
+ },
2982
+ async close() {
2983
+ await diskWriter.ready;
2984
+ diskWritable.size = currentWriter.size;
2985
+ await diskWriter.close();
2986
+ }
2987
+ });
2988
+ Object.defineProperty(writer, PROPERTY_NAME_WRITABLE, {
2989
+ get() {
2990
+ return writable;
2991
+ }
2992
+ });
2993
+ }
2994
+ }
2995
+
2818
2996
  function isHttpFamily(url) {
2819
2997
  const { baseURL } = getConfiguration();
2820
2998
  const { protocol } = new URL(url, baseURL);
@@ -2831,76 +3009,6 @@
2831
3009
  return reader.readUint8Array(offset, size, diskNumber);
2832
3010
  }
2833
3011
 
2834
- /*
2835
- Copyright (c) 2022 Gildas Lormeau. All rights reserved.
2836
-
2837
- Redistribution and use in source and binary forms, with or without
2838
- modification, are permitted provided that the following conditions are met:
2839
-
2840
- 1. Redistributions of source code must retain the above copyright notice,
2841
- this list of conditions and the following disclaimer.
2842
-
2843
- 2. Redistributions in binary form must reproduce the above copyright
2844
- notice, this list of conditions and the following disclaimer in
2845
- the documentation and/or other materials provided with the distribution.
2846
-
2847
- 3. The names of the authors may not be used to endorse or promote products
2848
- derived from this software without specific prior written permission.
2849
-
2850
- THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
2851
- INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
2852
- FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
2853
- INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
2854
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2855
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
2856
- OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
2857
- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2858
- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
2859
- EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2860
- */
2861
-
2862
- const MAX_32_BITS = 0xffffffff;
2863
- const MAX_16_BITS = 0xffff;
2864
- const COMPRESSION_METHOD_DEFLATE = 0x08;
2865
- const COMPRESSION_METHOD_STORE = 0x00;
2866
- const COMPRESSION_METHOD_AES = 0x63;
2867
-
2868
- const LOCAL_FILE_HEADER_SIGNATURE = 0x04034b50;
2869
- const DATA_DESCRIPTOR_RECORD_SIGNATURE = 0x08074b50;
2870
- const CENTRAL_FILE_HEADER_SIGNATURE = 0x02014b50;
2871
- const END_OF_CENTRAL_DIR_SIGNATURE = 0x06054b50;
2872
- const ZIP64_END_OF_CENTRAL_DIR_SIGNATURE = 0x06064b50;
2873
- const ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIGNATURE = 0x07064b50;
2874
- const END_OF_CENTRAL_DIR_LENGTH = 22;
2875
- const ZIP64_END_OF_CENTRAL_DIR_LOCATOR_LENGTH = 20;
2876
- const ZIP64_END_OF_CENTRAL_DIR_LENGTH = 56;
2877
- const ZIP64_END_OF_CENTRAL_DIR_TOTAL_LENGTH = END_OF_CENTRAL_DIR_LENGTH + ZIP64_END_OF_CENTRAL_DIR_LOCATOR_LENGTH + ZIP64_END_OF_CENTRAL_DIR_LENGTH;
2878
-
2879
- const ZIP64_TOTAL_NUMBER_OF_DISKS = 1;
2880
-
2881
- const EXTRAFIELD_TYPE_ZIP64 = 0x0001;
2882
- const EXTRAFIELD_TYPE_AES = 0x9901;
2883
- const EXTRAFIELD_TYPE_NTFS = 0x000a;
2884
- const EXTRAFIELD_TYPE_NTFS_TAG1 = 0x0001;
2885
- const EXTRAFIELD_TYPE_EXTENDED_TIMESTAMP = 0x5455;
2886
- const EXTRAFIELD_TYPE_UNICODE_PATH = 0x7075;
2887
- const EXTRAFIELD_TYPE_UNICODE_COMMENT = 0x6375;
2888
-
2889
- const BITFLAG_ENCRYPTED = 0x01;
2890
- const BITFLAG_LEVEL = 0x06;
2891
- const BITFLAG_DATA_DESCRIPTOR = 0x0008;
2892
- const BITFLAG_LANG_ENCODING_FLAG = 0x0800;
2893
- const FILE_ATTR_MSDOS_DIR_MASK = 0x10;
2894
-
2895
- const VERSION_DEFLATE = 0x14;
2896
- const VERSION_ZIP64 = 0x2D;
2897
- const VERSION_AES = 0x33;
2898
-
2899
- const DIRECTORY_SIGNATURE = "/";
2900
-
2901
- const MAX_DATE = new Date(2107, 11, 31);
2902
- const MIN_DATE = new Date(1980, 0, 1);
2903
-
2904
3012
  /*
2905
3013
  Copyright (c) 2022 Gildas Lormeau. All rights reserved.
2906
3014
 
@@ -3016,7 +3124,8 @@
3016
3124
  "rawExtraField", "bitFlag", "extraFieldZip64", "extraFieldUnicodePath", "extraFieldUnicodeComment",
3017
3125
  "extraFieldAES", "filenameUTF8", "commentUTF8", "offset", "zip64", "compressionMethod",
3018
3126
  "extraFieldNTFS", "lastAccessDate", "creationDate", "extraFieldExtendedTimestamp",
3019
- "version", "versionMadeBy", "msDosCompatible", "internalFileAttribute", "externalFileAttribute"];
3127
+ "version", "versionMadeBy", "msDosCompatible", "internalFileAttribute", "externalFileAttribute",
3128
+ "diskNumberStart"];
3020
3129
 
3021
3130
  class Entry {
3022
3131
 
@@ -3054,7 +3163,6 @@
3054
3163
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3055
3164
  */
3056
3165
 
3057
- const SPLIT_ZIP_FILE_SIGNATURE = 0x08074b50;
3058
3166
  const ERR_BAD_FORMAT = "File format is not recognized";
3059
3167
  const ERR_EOCDR_NOT_FOUND = "End of central directory not found";
3060
3168
  const ERR_EOCDR_ZIP64_NOT_FOUND = "End of Zip64 central directory not found";
@@ -3105,7 +3213,7 @@
3105
3213
  let { reader } = zipReader;
3106
3214
  const { config } = zipReader;
3107
3215
  await initStream(reader);
3108
- if (reader.size === undefined || !reader.readUint8Array) {
3216
+ if (reader.size === UNDEFINED_VALUE || !reader.readUint8Array) {
3109
3217
  if (Array.isArray(reader)) {
3110
3218
  reader = new SplitZipReader(reader);
3111
3219
  } else {
@@ -3127,15 +3235,16 @@
3127
3235
  throw new Error(ERR_EOCDR_NOT_FOUND);
3128
3236
  }
3129
3237
  }
3130
- const { lastDiskNumber } = reader;
3131
3238
  const endOfDirectoryView = getDataView$1(endOfDirectoryInfo);
3132
3239
  let directoryDataLength = getUint32(endOfDirectoryView, 12);
3133
3240
  let directoryDataOffset = getUint32(endOfDirectoryView, 16);
3134
3241
  const commentOffset = endOfDirectoryInfo.offset;
3135
3242
  const commentLength = getUint16(endOfDirectoryView, 20);
3136
3243
  const appendedDataOffset = commentOffset + END_OF_CENTRAL_DIR_LENGTH + commentLength;
3137
- let diskNumber = getUint16(endOfDirectoryView, 4);
3138
- if (lastDiskNumber !== undefined && diskNumber != lastDiskNumber) {
3244
+ const lastDiskNumber = getUint16(endOfDirectoryView, 4);
3245
+ const expectedLastDiskNumber = reader.lastDiskNumber || 0;
3246
+ let diskNumber = getUint16(endOfDirectoryView, 6);
3247
+ if (expectedLastDiskNumber != lastDiskNumber) {
3139
3248
  throw new Error(ERR_SPLIT_ZIP_FILE);
3140
3249
  }
3141
3250
  let filesLength = getUint16(endOfDirectoryView, 8);
@@ -3304,7 +3413,7 @@
3304
3413
  compressedSize
3305
3414
  } = zipEntry;
3306
3415
  const localDirectory = zipEntry.localDirectory = {};
3307
- let dataArray = await readUint8Array(reader, offset, 30, diskNumberStart);
3416
+ const dataArray = await readUint8Array(reader, offset, 30, diskNumberStart);
3308
3417
  const dataView = getDataView$1(dataArray);
3309
3418
  let password = getOptionValue$1(zipEntry, options, "password");
3310
3419
  password = password && password.length && password;
@@ -3320,8 +3429,9 @@
3320
3429
  throw new Error(ERR_LOCAL_FILE_HEADER_NOT_FOUND);
3321
3430
  }
3322
3431
  readCommonHeader(localDirectory, dataView, 4);
3323
- dataArray = await readUint8Array(reader, offset, 30 + localDirectory.filenameLength + localDirectory.extraFieldLength, diskNumberStart);
3324
- localDirectory.rawExtraField = dataArray.subarray(30 + localDirectory.filenameLength);
3432
+ localDirectory.rawExtraField = localDirectory.extraFieldLength ?
3433
+ await readUint8Array(reader, offset + 30 + localDirectory.filenameLength, localDirectory.extraFieldLength, diskNumberStart) :
3434
+ new Uint8Array();
3325
3435
  await readCommonFooter(zipEntry, localDirectory, dataView, 4);
3326
3436
  Object.assign(fileEntry, {
3327
3437
  lastAccessDate: localDirectory.lastAccessDate,
@@ -3330,7 +3440,7 @@
3330
3440
  const encrypted = zipEntry.encrypted && localDirectory.encrypted;
3331
3441
  const zipCrypto = encrypted && !extraFieldAES;
3332
3442
  if (encrypted) {
3333
- if (!zipCrypto && extraFieldAES.strength === undefined) {
3443
+ if (!zipCrypto && extraFieldAES.strength === UNDEFINED_VALUE) {
3334
3444
  throw new Error(ERR_UNSUPPORTED_ENCRYPTION);
3335
3445
  } else if (!password) {
3336
3446
  throw new Error(ERR_ENCRYPTED);
@@ -3347,7 +3457,7 @@
3347
3457
  };
3348
3458
  }
3349
3459
  const { writable } = writer;
3350
- if (writable.size === undefined) {
3460
+ if (writable.size === UNDEFINED_VALUE) {
3351
3461
  writable.size = 0;
3352
3462
  }
3353
3463
  const signal = getOptionValue$1(zipEntry, options, "signal");
@@ -3595,7 +3705,7 @@
3595
3705
  }
3596
3706
 
3597
3707
  function getOptionValue$1(zipReader, options, name) {
3598
- return options[name] === undefined ? zipReader.options[name] : options[name];
3708
+ return options[name] === UNDEFINED_VALUE ? zipReader.options[name] : options[name];
3599
3709
  }
3600
3710
 
3601
3711
  function getDate(timeRaw) {
@@ -3674,7 +3784,7 @@
3674
3784
  const ERR_UNSUPPORTED_FORMAT = "Zip64 is not supported (make sure 'keepOrder' is set to 'true')";
3675
3785
 
3676
3786
  const EXTRAFIELD_DATA_AES = new Uint8Array([0x07, 0x00, 0x02, 0x00, 0x41, 0x45, 0x03, 0x00, 0x00]);
3677
- const EXTRAFIELD_LENGTH_ZIP64 = 24;
3787
+ const EXTRAFIELD_LENGTH_ZIP64 = 28;
3678
3788
 
3679
3789
  let workers = 0;
3680
3790
  const pendingEntries = [];
@@ -3682,14 +3792,26 @@
3682
3792
  class ZipWriter {
3683
3793
 
3684
3794
  constructor(writer, options = {}) {
3795
+ if (writer.writable === UNDEFINED_VALUE && typeof writer.next == FUNCTION_TYPE$1) {
3796
+ writer = new SplitZipWriter(writer);
3797
+ }
3685
3798
  if (writer instanceof WritableStream) {
3686
3799
  writer = { writable: writer };
3687
3800
  }
3688
- if (writer.writable.size === undefined) {
3801
+ if (writer.writable.size === UNDEFINED_VALUE) {
3689
3802
  writer.writable.size = 0;
3690
3803
  }
3804
+ const splitZipFile = writer instanceof SplitZipWriter;
3805
+ if (!splitZipFile) {
3806
+ Object.assign(writer, {
3807
+ diskNumber: 0,
3808
+ diskOffset: 0,
3809
+ availableSize: Infinity
3810
+ });
3811
+ }
3691
3812
  Object.assign(this, {
3692
3813
  writer,
3814
+ addSplitZipSignature: splitZipFile,
3693
3815
  options,
3694
3816
  config: getConfiguration(),
3695
3817
  files: new Map(),
@@ -3779,7 +3901,7 @@
3779
3901
  const password = getOptionValue(zipWriter, options, "password");
3780
3902
  const encryptionStrength = getOptionValue(zipWriter, options, "encryptionStrength") || 3;
3781
3903
  const zipCrypto = getOptionValue(zipWriter, options, "zipCrypto");
3782
- if (password !== undefined && encryptionStrength !== undefined && (encryptionStrength < 1 || encryptionStrength > 3)) {
3904
+ if (password !== UNDEFINED_VALUE && encryptionStrength !== UNDEFINED_VALUE && (encryptionStrength < 1 || encryptionStrength > 3)) {
3783
3905
  throw new Error(ERR_INVALID_ENCRYPTION_STRENGTH);
3784
3906
  }
3785
3907
  let rawExtraField = new Uint8Array();
@@ -3803,18 +3925,18 @@
3803
3925
  });
3804
3926
  }
3805
3927
  let extendedTimestamp = getOptionValue(zipWriter, options, "extendedTimestamp");
3806
- if (extendedTimestamp === undefined) {
3928
+ if (extendedTimestamp === UNDEFINED_VALUE) {
3807
3929
  extendedTimestamp = true;
3808
3930
  }
3809
3931
  let maximumCompressedSize = 0;
3810
3932
  let maximumEntrySize = 0;
3811
3933
  let keepOrder = getOptionValue(zipWriter, options, "keepOrder");
3812
- if (keepOrder === undefined) {
3934
+ if (keepOrder === UNDEFINED_VALUE) {
3813
3935
  keepOrder = true;
3814
3936
  }
3815
3937
  let uncompressedSize = 0;
3816
3938
  let msDosCompatible = getOptionValue(zipWriter, options, "msDosCompatible");
3817
- if (msDosCompatible === undefined) {
3939
+ if (msDosCompatible === UNDEFINED_VALUE) {
3818
3940
  msDosCompatible = true;
3819
3941
  }
3820
3942
  const internalFileAttribute = getOptionValue(zipWriter, options, "internalFileAttribute") || 0;
@@ -3827,15 +3949,16 @@
3827
3949
  };
3828
3950
  }
3829
3951
  await initStream(reader);
3830
- if (reader.size === undefined) {
3952
+ if (reader.size === UNDEFINED_VALUE) {
3831
3953
  options.dataDescriptor = true;
3832
- if (zip64 === undefined) {
3954
+ if (zip64 || zip64 === UNDEFINED_VALUE) {
3833
3955
  zip64 = true;
3956
+ maximumCompressedSize = MAX_32_BITS;
3834
3957
  }
3835
3958
  } else {
3836
3959
  uncompressedSize = reader.size;
3960
+ maximumCompressedSize = getMaximumCompressedSize(uncompressedSize);
3837
3961
  }
3838
- maximumCompressedSize = getMaximumCompressedSize(uncompressedSize);
3839
3962
  }
3840
3963
  if (zipWriter.offset + zipWriter.pendingEntriesSize >= MAX_32_BITS ||
3841
3964
  uncompressedSize >= MAX_32_BITS ||
@@ -3854,10 +3977,10 @@
3854
3977
  let dataDescriptorSignature = getOptionValue(zipWriter, options, "dataDescriptorSignature");
3855
3978
  const signal = getOptionValue(zipWriter, options, "signal");
3856
3979
  const useCompressionStream = getOptionValue(zipWriter, options, "useCompressionStream");
3857
- if (dataDescriptor === undefined) {
3980
+ if (dataDescriptor === UNDEFINED_VALUE) {
3858
3981
  dataDescriptor = true;
3859
3982
  }
3860
- if (dataDescriptor && dataDescriptorSignature === undefined) {
3983
+ if (dataDescriptor && dataDescriptorSignature === UNDEFINED_VALUE) {
3861
3984
  dataDescriptorSignature = false;
3862
3985
  }
3863
3986
  options = Object.assign({}, options, {
@@ -3940,10 +4063,21 @@
3940
4063
  releaseLockWriter = () => delete zipWriter.lockWriter;
3941
4064
  }
3942
4065
  await initStream(fileWriter);
4066
+ const { writable, diskNumber } = writer;
4067
+ let { diskOffset } = writer;
4068
+ if (zipWriter.addSplitZipSignature) {
4069
+ delete zipWriter.addSplitZipSignature;
4070
+ const signatureArray = new Uint8Array(4);
4071
+ const signatureArrayView = new DataView(signatureArray.buffer);
4072
+ setUint32(signatureArrayView, 0, SPLIT_ZIP_FILE_SIGNATURE);
4073
+ await writeData(writable, signatureArray);
4074
+ zipWriter.offset += 4;
4075
+ }
3943
4076
  if (!bufferedWrite) {
3944
4077
  await lockPreviousFileEntry;
3945
4078
  }
3946
4079
  writingEntryData = true;
4080
+ fileEntry.diskNumberStart = diskNumber;
3947
4081
  fileEntry = await createFileEntry(reader, fileWriter, fileEntry, entryInfo, zipWriter.config, options);
3948
4082
  writingEntryData = false;
3949
4083
  files.set(name, fileEntry);
@@ -3954,7 +4088,6 @@
3954
4088
  await lockPreviousFileEntry;
3955
4089
  await requestLockWriter();
3956
4090
  writingBufferedEntryData = true;
3957
- const { writable } = writer;
3958
4091
  if (!dataDescriptor) {
3959
4092
  const arrayBuffer = await sliceAsArrayBuffer(blob, 0, 26);
3960
4093
  const arrayBufferView = new DataView(arrayBuffer);
@@ -3971,13 +4104,18 @@
3971
4104
  await writeData(writable, new Uint8Array(arrayBuffer));
3972
4105
  blob = blob.slice(arrayBuffer.byteLength);
3973
4106
  }
4107
+ fileEntry.diskNumberStart = writer.diskNumber;
4108
+ diskOffset = writer.diskOffset;
3974
4109
  await blob.stream().pipeTo(writable, { preventClose: true, signal });
4110
+ writable.size += blob.size;
3975
4111
  writingBufferedEntryData = false;
3976
4112
  }
3977
- fileEntry.offset = zipWriter.offset;
4113
+ fileEntry.offset = zipWriter.offset - diskOffset;
3978
4114
  if (fileEntry.zip64) {
3979
- const rawExtraFieldZip64View = getDataView(fileEntry.rawExtraFieldZip64);
3980
- setBigUint64(rawExtraFieldZip64View, 20, BigInt(fileEntry.offset));
4115
+ const { rawExtraFieldZip64, offset, diskNumberStart } = fileEntry;
4116
+ const rawExtraFieldZip64View = getDataView(rawExtraFieldZip64);
4117
+ setBigUint64(rawExtraFieldZip64View, 20, BigInt(offset));
4118
+ setUint32(rawExtraFieldZip64View, 28, diskNumberStart);
3981
4119
  } else if (fileEntry.offset >= MAX_32_BITS) {
3982
4120
  throw new Error(ERR_UNSUPPORTED_FORMAT);
3983
4121
  }
@@ -4021,7 +4159,7 @@
4021
4159
  }
4022
4160
  }
4023
4161
 
4024
- async function createFileEntry(reader, writer, pendingFileEntry, entryInfo, config, options) {
4162
+ async function createFileEntry(reader, writer, { diskNumberStart, lock }, entryInfo, config, options) {
4025
4163
  const {
4026
4164
  headerInfo,
4027
4165
  dataDescriptorInfo
@@ -4066,7 +4204,7 @@
4066
4204
  useCompressionStream
4067
4205
  } = options;
4068
4206
  const fileEntry = {
4069
- lock: pendingFileEntry.lock,
4207
+ lock,
4070
4208
  versionMadeBy,
4071
4209
  zip64,
4072
4210
  directory: Boolean(directory),
@@ -4081,7 +4219,8 @@
4081
4219
  extendedTimestamp,
4082
4220
  msDosCompatible,
4083
4221
  internalFileAttribute,
4084
- externalFileAttribute
4222
+ externalFileAttribute,
4223
+ diskNumberStart
4085
4224
  };
4086
4225
  let uncompressedSize = 0;
4087
4226
  let signature;
@@ -4324,7 +4463,7 @@
4324
4463
  zipCrypto,
4325
4464
  dataDescriptor
4326
4465
  } = options;
4327
- if ((!encrypted || zipCrypto) && signature !== undefined) {
4466
+ if ((!encrypted || zipCrypto) && signature !== UNDEFINED_VALUE) {
4328
4467
  setUint32(headerView, 10, signature);
4329
4468
  if (dataDescriptor) {
4330
4469
  setUint32(dataDescriptorView, dataDescriptorOffset, signature);
@@ -4353,10 +4492,12 @@
4353
4492
  }
4354
4493
 
4355
4494
  async function closeFile(zipWriter, comment, options) {
4356
- const { files } = zipWriter;
4495
+ const { files, writer } = zipWriter;
4357
4496
  let offset = 0;
4358
4497
  let directoryDataLength = 0;
4359
- let directoryOffset = zipWriter.offset;
4498
+ const { diskOffset, writable } = writer;
4499
+ let { diskNumber } = writer;
4500
+ let directoryOffset = zipWriter.offset - diskOffset;
4360
4501
  let filesLength = files.size;
4361
4502
  for (const [, {
4362
4503
  rawFilename,
@@ -4385,7 +4526,7 @@
4385
4526
  zip64 = true;
4386
4527
  }
4387
4528
  }
4388
- const directoryArray = new Uint8Array(directoryDataLength + (zip64 ? ZIP64_END_OF_CENTRAL_DIR_TOTAL_LENGTH : END_OF_CENTRAL_DIR_LENGTH));
4529
+ const directoryArray = new Uint8Array(directoryDataLength);
4389
4530
  const directoryView = getDataView(directoryArray);
4390
4531
  for (const [indexFileEntry, fileEntry] of Array.from(files.values()).entries()) {
4391
4532
  const {
@@ -4404,7 +4545,8 @@
4404
4545
  internalFileAttribute,
4405
4546
  externalFileAttribute,
4406
4547
  extendedTimestamp,
4407
- lastModDate
4548
+ lastModDate,
4549
+ diskNumberStart
4408
4550
  } = fileEntry;
4409
4551
  let rawExtraFieldExtendedTimestamp;
4410
4552
  if (extendedTimestamp) {
@@ -4423,17 +4565,14 @@
4423
4565
  arraySet(directoryArray, headerArray, offset + 6);
4424
4566
  setUint16(directoryView, offset + 30, extraFieldLength);
4425
4567
  setUint16(directoryView, offset + 32, getLength(rawComment));
4568
+ setUint16(directoryView, offset + 34, zip64 ? MAX_16_BITS : diskNumberStart);
4426
4569
  setUint16(directoryView, offset + 36, internalFileAttribute);
4427
4570
  if (externalFileAttribute) {
4428
4571
  setUint32(directoryView, offset + 38, externalFileAttribute);
4429
4572
  } else if (directory && msDosCompatible) {
4430
4573
  setUint8(directoryView, offset + 38, FILE_ATTR_MSDOS_DIR_MASK);
4431
4574
  }
4432
- if (zip64) {
4433
- setUint32(directoryView, offset + 42, MAX_32_BITS);
4434
- } else {
4435
- setUint32(directoryView, offset + 42, fileEntryOffset);
4436
- }
4575
+ setUint32(directoryView, offset + 42, zip64 ? MAX_32_BITS : fileEntryOffset);
4437
4576
  arraySet(directoryArray, rawFilename, offset + 46);
4438
4577
  arraySet(directoryArray, rawExtraFieldZip64, offset + 46 + getLength(rawFilename));
4439
4578
  arraySet(directoryArray, rawExtraFieldAES, offset + 46 + getLength(rawFilename, rawExtraFieldZip64));
@@ -4450,39 +4589,52 @@
4450
4589
  }
4451
4590
  }
4452
4591
  }
4592
+ await initStream(writer);
4593
+ await writeData(writable, directoryArray);
4594
+ const endOfdirectoryArray = new Uint8Array(zip64 ? ZIP64_END_OF_CENTRAL_DIR_TOTAL_LENGTH : END_OF_CENTRAL_DIR_LENGTH);
4595
+ const endOfdirectoryView = getDataView(endOfdirectoryArray);
4596
+ let lastDiskNumber = writer.diskNumber;
4597
+ const { availableSize } = writer;
4598
+ if (availableSize < endOfdirectoryArray.length) {
4599
+ lastDiskNumber++;
4600
+ }
4601
+ offset = 0;
4453
4602
  if (zip64) {
4454
- setUint32(directoryView, offset, ZIP64_END_OF_CENTRAL_DIR_SIGNATURE);
4455
- setBigUint64(directoryView, offset + 4, BigInt(44));
4456
- setUint16(directoryView, offset + 12, 45);
4457
- setUint16(directoryView, offset + 14, 45);
4458
- setBigUint64(directoryView, offset + 24, BigInt(filesLength));
4459
- setBigUint64(directoryView, offset + 32, BigInt(filesLength));
4460
- setBigUint64(directoryView, offset + 40, BigInt(directoryDataLength));
4461
- setBigUint64(directoryView, offset + 48, BigInt(directoryOffset));
4462
- setUint32(directoryView, offset + 56, ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIGNATURE);
4463
- setBigUint64(directoryView, offset + 64, BigInt(directoryOffset) + BigInt(directoryDataLength));
4464
- setUint32(directoryView, offset + 72, ZIP64_TOTAL_NUMBER_OF_DISKS);
4603
+ setUint32(endOfdirectoryView, 0, ZIP64_END_OF_CENTRAL_DIR_SIGNATURE);
4604
+ setBigUint64(endOfdirectoryView, 4, BigInt(44));
4605
+ setUint16(endOfdirectoryView, 12, 45);
4606
+ setUint16(endOfdirectoryView, 14, 45);
4607
+ setUint32(endOfdirectoryView, 16, lastDiskNumber);
4608
+ setUint32(endOfdirectoryView, 20, diskNumber);
4609
+ setBigUint64(endOfdirectoryView, 24, BigInt(filesLength));
4610
+ setBigUint64(endOfdirectoryView, 32, BigInt(filesLength));
4611
+ setBigUint64(endOfdirectoryView, 40, BigInt(directoryDataLength));
4612
+ setBigUint64(endOfdirectoryView, 48, BigInt(directoryOffset));
4613
+ setUint32(endOfdirectoryView, 56, ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIGNATURE);
4614
+ setBigUint64(endOfdirectoryView, 64, BigInt(directoryOffset) + BigInt(directoryDataLength));
4615
+ setUint32(endOfdirectoryView, 72, lastDiskNumber + 1);
4616
+ diskNumber = MAX_16_BITS;
4465
4617
  filesLength = MAX_16_BITS;
4466
4618
  directoryOffset = MAX_32_BITS;
4467
4619
  directoryDataLength = MAX_32_BITS;
4468
- offset += 76;
4469
- }
4470
- setUint32(directoryView, offset, END_OF_CENTRAL_DIR_SIGNATURE);
4471
- setUint16(directoryView, offset + 8, filesLength);
4472
- setUint16(directoryView, offset + 10, filesLength);
4473
- setUint32(directoryView, offset + 12, directoryDataLength);
4474
- setUint32(directoryView, offset + 16, directoryOffset);
4620
+ offset += ZIP64_END_OF_CENTRAL_DIR_LENGTH + ZIP64_END_OF_CENTRAL_DIR_LOCATOR_LENGTH;
4621
+ }
4622
+ setUint32(endOfdirectoryView, offset, END_OF_CENTRAL_DIR_SIGNATURE);
4623
+ setUint16(endOfdirectoryView, offset + 4, lastDiskNumber);
4624
+ setUint16(endOfdirectoryView, offset + 6, diskNumber);
4625
+ setUint16(endOfdirectoryView, offset + 8, filesLength);
4626
+ setUint16(endOfdirectoryView, offset + 10, filesLength);
4627
+ setUint32(endOfdirectoryView, offset + 12, directoryDataLength);
4628
+ setUint32(endOfdirectoryView, offset + 16, directoryOffset);
4475
4629
  const commentLength = getLength(comment);
4476
4630
  if (commentLength) {
4477
4631
  if (commentLength <= MAX_16_BITS) {
4478
- setUint16(directoryView, offset + 20, commentLength);
4632
+ setUint16(endOfdirectoryView, offset + 20, commentLength);
4479
4633
  } else {
4480
4634
  throw new Error(ERR_INVALID_COMMENT);
4481
4635
  }
4482
4636
  }
4483
- const { writable } = zipWriter.writer;
4484
- await initStream(zipWriter.writer);
4485
- await writeData(writable, directoryArray);
4637
+ await writeData(writable, endOfdirectoryArray);
4486
4638
  if (commentLength) {
4487
4639
  await writeData(writable, comment);
4488
4640
  }
@@ -4511,7 +4663,7 @@
4511
4663
  }
4512
4664
 
4513
4665
  function getOptionValue(zipWriter, options, name) {
4514
- return options[name] === undefined ? zipWriter.options[name] : options[name];
4666
+ return options[name] === UNDEFINED_VALUE ? zipWriter.options[name] : options[name];
4515
4667
  }
4516
4668
 
4517
4669
  function getMaximumCompressedSize(uncompressedSize) {
@@ -5219,6 +5371,7 @@
5219
5371
  exports.ERR_INVALID_PASSWORD = ERR_INVALID_PASSWORD;
5220
5372
  exports.ERR_INVALID_SIGNATURE = ERR_INVALID_SIGNATURE;
5221
5373
  exports.ERR_INVALID_VERSION = ERR_INVALID_VERSION;
5374
+ exports.ERR_ITERATOR_COMPLETED_TOO_SOON = ERR_ITERATOR_COMPLETED_TOO_SOON;
5222
5375
  exports.ERR_LOCAL_FILE_HEADER_NOT_FOUND = ERR_LOCAL_FILE_HEADER_NOT_FOUND;
5223
5376
  exports.ERR_SPLIT_ZIP_FILE = ERR_SPLIT_ZIP_FILE;
5224
5377
  exports.ERR_UNSUPPORTED_COMPRESSION = ERR_UNSUPPORTED_COMPRESSION;
@@ -5228,6 +5381,7 @@
5228
5381
  exports.HttpReader = HttpReader;
5229
5382
  exports.Reader = Reader;
5230
5383
  exports.SplitZipReader = SplitZipReader;
5384
+ exports.SplitZipWriter = SplitZipWriter;
5231
5385
  exports.TextReader = TextReader;
5232
5386
  exports.TextWriter = TextWriter;
5233
5387
  exports.Uint8ArrayReader = Uint8ArrayReader;