@protontech/openpgp 6.0.0-beta.3.patch.1 → 6.0.1

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 (42) hide show
  1. package/README.md +34 -37
  2. package/dist/lightweight/argon2id.min.mjs +1 -1
  3. package/dist/lightweight/argon2id.min.mjs.map +1 -1
  4. package/dist/lightweight/argon2id.mjs +1 -1
  5. package/dist/lightweight/legacy_ciphers.min.mjs +1 -1
  6. package/dist/lightweight/legacy_ciphers.min.mjs.map +1 -1
  7. package/dist/lightweight/legacy_ciphers.mjs +1 -1
  8. package/dist/lightweight/noble_curves.min.mjs +11 -11
  9. package/dist/lightweight/noble_curves.min.mjs.map +1 -1
  10. package/dist/lightweight/noble_curves.mjs +260 -158
  11. package/dist/lightweight/noble_hashes.min.mjs +2 -2
  12. package/dist/lightweight/noble_hashes.min.mjs.map +1 -1
  13. package/dist/lightweight/noble_hashes.mjs +3 -2
  14. package/dist/lightweight/noble_post_quantum.min.mjs +5 -0
  15. package/dist/lightweight/noble_post_quantum.min.mjs.map +1 -0
  16. package/dist/lightweight/noble_post_quantum.mjs +1002 -0
  17. package/dist/lightweight/openpgp.min.mjs +4 -4
  18. package/dist/lightweight/openpgp.min.mjs.map +1 -1
  19. package/dist/lightweight/openpgp.mjs +863 -1056
  20. package/dist/lightweight/seek-bzip.min.mjs +3 -0
  21. package/dist/lightweight/seek-bzip.min.mjs.map +1 -0
  22. package/dist/lightweight/seek-bzip.mjs +866 -0
  23. package/dist/lightweight/sha3.min.mjs +3 -3
  24. package/dist/lightweight/sha3.min.mjs.map +1 -1
  25. package/dist/lightweight/sha3.mjs +27 -456
  26. package/dist/lightweight/sha512.min.mjs +3 -0
  27. package/dist/lightweight/sha512.min.mjs.map +1 -0
  28. package/dist/lightweight/sha512.mjs +436 -0
  29. package/dist/node/openpgp.cjs +14499 -12719
  30. package/dist/node/openpgp.min.cjs +16 -14
  31. package/dist/node/openpgp.min.cjs.map +1 -1
  32. package/dist/node/openpgp.min.mjs +16 -14
  33. package/dist/node/openpgp.min.mjs.map +1 -1
  34. package/dist/node/openpgp.mjs +11878 -10098
  35. package/dist/openpgp.js +11909 -10129
  36. package/dist/openpgp.min.js +16 -14
  37. package/dist/openpgp.min.js.map +1 -1
  38. package/dist/openpgp.min.mjs +16 -14
  39. package/dist/openpgp.min.mjs.map +1 -1
  40. package/dist/openpgp.mjs +11909 -10129
  41. package/openpgp.d.ts +3 -9
  42. package/package.json +27 -26
@@ -0,0 +1,866 @@
1
+ /*! OpenPGP.js v6.0.1 - 2024-11-25 - this is LGPL licensed code, see LICENSE/our website https://openpgpjs.org/ for more information. */
2
+ const globalThis = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
3
+
4
+ function _mergeNamespaces(n, m) {
5
+ m.forEach(function (e) {
6
+ e && typeof e !== 'string' && !Array.isArray(e) && Object.keys(e).forEach(function (k) {
7
+ if (k !== 'default' && !(k in n)) {
8
+ var d = Object.getOwnPropertyDescriptor(e, k);
9
+ Object.defineProperty(n, k, d.get ? d : {
10
+ enumerable: true,
11
+ get: function () { return e[k]; }
12
+ });
13
+ }
14
+ });
15
+ });
16
+ return Object.freeze(n);
17
+ }
18
+
19
+ /*
20
+ node-bzip - a pure-javascript Node.JS module for decoding bzip2 data
21
+
22
+ Copyright (C) 2012 Eli Skeggs
23
+
24
+ This library is free software; you can redistribute it and/or
25
+ modify it under the terms of the GNU Lesser General Public
26
+ License as published by the Free Software Foundation; either
27
+ version 2.1 of the License, or (at your option) any later version.
28
+
29
+ This library is distributed in the hope that it will be useful,
30
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
31
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32
+ Lesser General Public License for more details.
33
+
34
+ You should have received a copy of the GNU Lesser General Public
35
+ License along with this library; if not, see
36
+ http://www.gnu.org/licenses/lgpl-2.1.html
37
+
38
+ Adapted from bzip2.js, copyright 2011 antimatter15 (antimatter15@gmail.com).
39
+
40
+ Based on micro-bunzip by Rob Landley (rob@landley.net).
41
+
42
+ Based on bzip2 decompression code by Julian R Seward (jseward@acm.org),
43
+ which also acknowledges contributions by Mike Burrows, David Wheeler,
44
+ Peter Fenwick, Alistair Moffat, Radford Neal, Ian H. Witten,
45
+ Robert Sedgewick, and Jon L. Bentley.
46
+ */
47
+
48
+ var BITMASK = [0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF];
49
+
50
+ // offset in bytes
51
+ var BitReader$1 = function(stream) {
52
+ this.stream = stream;
53
+ this.bitOffset = 0;
54
+ this.curByte = 0;
55
+ this.hasByte = false;
56
+ };
57
+
58
+ BitReader$1.prototype._ensureByte = function() {
59
+ if (!this.hasByte) {
60
+ this.curByte = this.stream.readByte();
61
+ this.hasByte = true;
62
+ }
63
+ };
64
+
65
+ // reads bits from the buffer
66
+ BitReader$1.prototype.read = function(bits) {
67
+ var result = 0;
68
+ while (bits > 0) {
69
+ this._ensureByte();
70
+ var remaining = 8 - this.bitOffset;
71
+ // if we're in a byte
72
+ if (bits >= remaining) {
73
+ result <<= remaining;
74
+ result |= BITMASK[remaining] & this.curByte;
75
+ this.hasByte = false;
76
+ this.bitOffset = 0;
77
+ bits -= remaining;
78
+ } else {
79
+ result <<= bits;
80
+ var shift = remaining - bits;
81
+ result |= (this.curByte & (BITMASK[bits] << shift)) >> shift;
82
+ this.bitOffset += bits;
83
+ bits = 0;
84
+ }
85
+ }
86
+ return result;
87
+ };
88
+
89
+ // seek to an arbitrary point in the buffer (expressed in bits)
90
+ BitReader$1.prototype.seek = function(pos) {
91
+ var n_bit = pos % 8;
92
+ var n_byte = (pos - n_bit) / 8;
93
+ this.bitOffset = n_bit;
94
+ this.stream.seek(n_byte);
95
+ this.hasByte = false;
96
+ };
97
+
98
+ // reads 6 bytes worth of data using the read method
99
+ BitReader$1.prototype.pi = function() {
100
+ var buf = new Uint8Array(6), i;
101
+ for (i = 0; i < buf.length; i++) {
102
+ buf[i] = this.read(8);
103
+ }
104
+ return bufToHex(buf);
105
+ };
106
+
107
+ function bufToHex(buf) {
108
+ return Array.prototype.map.call(buf, x => ('00' + x.toString(16)).slice(-2)).join('');
109
+ }
110
+
111
+ var bitreader = BitReader$1;
112
+
113
+ /* very simple input/output stream interface */
114
+
115
+ var Stream$1 = function() {
116
+ };
117
+
118
+ // input streams //////////////
119
+ /** Returns the next byte, or -1 for EOF. */
120
+ Stream$1.prototype.readByte = function() {
121
+ throw new Error("abstract method readByte() not implemented");
122
+ };
123
+ /** Attempts to fill the buffer; returns number of bytes read, or
124
+ * -1 for EOF. */
125
+ Stream$1.prototype.read = function(buffer, bufOffset, length) {
126
+ var bytesRead = 0;
127
+ while (bytesRead < length) {
128
+ var c = this.readByte();
129
+ if (c < 0) { // EOF
130
+ return (bytesRead===0) ? -1 : bytesRead;
131
+ }
132
+ buffer[bufOffset++] = c;
133
+ bytesRead++;
134
+ }
135
+ return bytesRead;
136
+ };
137
+ Stream$1.prototype.seek = function(new_pos) {
138
+ throw new Error("abstract method seek() not implemented");
139
+ };
140
+
141
+ // output streams ///////////
142
+ Stream$1.prototype.writeByte = function(_byte) {
143
+ throw new Error("abstract method readByte() not implemented");
144
+ };
145
+ Stream$1.prototype.write = function(buffer, bufOffset, length) {
146
+ var i;
147
+ for (i=0; i<length; i++) {
148
+ this.writeByte(buffer[bufOffset++]);
149
+ }
150
+ return length;
151
+ };
152
+ Stream$1.prototype.flush = function() {
153
+ };
154
+
155
+ var stream = Stream$1;
156
+
157
+ /* CRC32, used in Bzip2 implementation.
158
+ * This is a port of CRC32.java from the jbzip2 implementation at
159
+ * https://code.google.com/p/jbzip2
160
+ * which is:
161
+ * Copyright (c) 2011 Matthew Francis
162
+ *
163
+ * Permission is hereby granted, free of charge, to any person
164
+ * obtaining a copy of this software and associated documentation
165
+ * files (the "Software"), to deal in the Software without
166
+ * restriction, including without limitation the rights to use,
167
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
168
+ * copies of the Software, and to permit persons to whom the
169
+ * Software is furnished to do so, subject to the following
170
+ * conditions:
171
+ *
172
+ * The above copyright notice and this permission notice shall be
173
+ * included in all copies or substantial portions of the Software.
174
+ *
175
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
176
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
177
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
178
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
179
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
180
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
181
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
182
+ * OTHER DEALINGS IN THE SOFTWARE.
183
+ * This JavaScript implementation is:
184
+ * Copyright (c) 2013 C. Scott Ananian
185
+ * with the same licensing terms as Matthew Francis' original implementation.
186
+ */
187
+
188
+ var crc32 = (function() {
189
+
190
+ /**
191
+ * A static CRC lookup table
192
+ */
193
+ var crc32Lookup = new Uint32Array([
194
+ 0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9, 0x130476dc, 0x17c56b6b, 0x1a864db2, 0x1e475005,
195
+ 0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61, 0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd,
196
+ 0x4c11db70, 0x48d0c6c7, 0x4593e01e, 0x4152fda9, 0x5f15adac, 0x5bd4b01b, 0x569796c2, 0x52568b75,
197
+ 0x6a1936c8, 0x6ed82b7f, 0x639b0da6, 0x675a1011, 0x791d4014, 0x7ddc5da3, 0x709f7b7a, 0x745e66cd,
198
+ 0x9823b6e0, 0x9ce2ab57, 0x91a18d8e, 0x95609039, 0x8b27c03c, 0x8fe6dd8b, 0x82a5fb52, 0x8664e6e5,
199
+ 0xbe2b5b58, 0xbaea46ef, 0xb7a96036, 0xb3687d81, 0xad2f2d84, 0xa9ee3033, 0xa4ad16ea, 0xa06c0b5d,
200
+ 0xd4326d90, 0xd0f37027, 0xddb056fe, 0xd9714b49, 0xc7361b4c, 0xc3f706fb, 0xceb42022, 0xca753d95,
201
+ 0xf23a8028, 0xf6fb9d9f, 0xfbb8bb46, 0xff79a6f1, 0xe13ef6f4, 0xe5ffeb43, 0xe8bccd9a, 0xec7dd02d,
202
+ 0x34867077, 0x30476dc0, 0x3d044b19, 0x39c556ae, 0x278206ab, 0x23431b1c, 0x2e003dc5, 0x2ac12072,
203
+ 0x128e9dcf, 0x164f8078, 0x1b0ca6a1, 0x1fcdbb16, 0x018aeb13, 0x054bf6a4, 0x0808d07d, 0x0cc9cdca,
204
+ 0x7897ab07, 0x7c56b6b0, 0x71159069, 0x75d48dde, 0x6b93dddb, 0x6f52c06c, 0x6211e6b5, 0x66d0fb02,
205
+ 0x5e9f46bf, 0x5a5e5b08, 0x571d7dd1, 0x53dc6066, 0x4d9b3063, 0x495a2dd4, 0x44190b0d, 0x40d816ba,
206
+ 0xaca5c697, 0xa864db20, 0xa527fdf9, 0xa1e6e04e, 0xbfa1b04b, 0xbb60adfc, 0xb6238b25, 0xb2e29692,
207
+ 0x8aad2b2f, 0x8e6c3698, 0x832f1041, 0x87ee0df6, 0x99a95df3, 0x9d684044, 0x902b669d, 0x94ea7b2a,
208
+ 0xe0b41de7, 0xe4750050, 0xe9362689, 0xedf73b3e, 0xf3b06b3b, 0xf771768c, 0xfa325055, 0xfef34de2,
209
+ 0xc6bcf05f, 0xc27dede8, 0xcf3ecb31, 0xcbffd686, 0xd5b88683, 0xd1799b34, 0xdc3abded, 0xd8fba05a,
210
+ 0x690ce0ee, 0x6dcdfd59, 0x608edb80, 0x644fc637, 0x7a089632, 0x7ec98b85, 0x738aad5c, 0x774bb0eb,
211
+ 0x4f040d56, 0x4bc510e1, 0x46863638, 0x42472b8f, 0x5c007b8a, 0x58c1663d, 0x558240e4, 0x51435d53,
212
+ 0x251d3b9e, 0x21dc2629, 0x2c9f00f0, 0x285e1d47, 0x36194d42, 0x32d850f5, 0x3f9b762c, 0x3b5a6b9b,
213
+ 0x0315d626, 0x07d4cb91, 0x0a97ed48, 0x0e56f0ff, 0x1011a0fa, 0x14d0bd4d, 0x19939b94, 0x1d528623,
214
+ 0xf12f560e, 0xf5ee4bb9, 0xf8ad6d60, 0xfc6c70d7, 0xe22b20d2, 0xe6ea3d65, 0xeba91bbc, 0xef68060b,
215
+ 0xd727bbb6, 0xd3e6a601, 0xdea580d8, 0xda649d6f, 0xc423cd6a, 0xc0e2d0dd, 0xcda1f604, 0xc960ebb3,
216
+ 0xbd3e8d7e, 0xb9ff90c9, 0xb4bcb610, 0xb07daba7, 0xae3afba2, 0xaafbe615, 0xa7b8c0cc, 0xa379dd7b,
217
+ 0x9b3660c6, 0x9ff77d71, 0x92b45ba8, 0x9675461f, 0x8832161a, 0x8cf30bad, 0x81b02d74, 0x857130c3,
218
+ 0x5d8a9099, 0x594b8d2e, 0x5408abf7, 0x50c9b640, 0x4e8ee645, 0x4a4ffbf2, 0x470cdd2b, 0x43cdc09c,
219
+ 0x7b827d21, 0x7f436096, 0x7200464f, 0x76c15bf8, 0x68860bfd, 0x6c47164a, 0x61043093, 0x65c52d24,
220
+ 0x119b4be9, 0x155a565e, 0x18197087, 0x1cd86d30, 0x029f3d35, 0x065e2082, 0x0b1d065b, 0x0fdc1bec,
221
+ 0x3793a651, 0x3352bbe6, 0x3e119d3f, 0x3ad08088, 0x2497d08d, 0x2056cd3a, 0x2d15ebe3, 0x29d4f654,
222
+ 0xc5a92679, 0xc1683bce, 0xcc2b1d17, 0xc8ea00a0, 0xd6ad50a5, 0xd26c4d12, 0xdf2f6bcb, 0xdbee767c,
223
+ 0xe3a1cbc1, 0xe760d676, 0xea23f0af, 0xeee2ed18, 0xf0a5bd1d, 0xf464a0aa, 0xf9278673, 0xfde69bc4,
224
+ 0x89b8fd09, 0x8d79e0be, 0x803ac667, 0x84fbdbd0, 0x9abc8bd5, 0x9e7d9662, 0x933eb0bb, 0x97ffad0c,
225
+ 0xafb010b1, 0xab710d06, 0xa6322bdf, 0xa2f33668, 0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4
226
+ ]);
227
+
228
+ var CRC32 = function() {
229
+ /**
230
+ * The current CRC
231
+ */
232
+ var crc = 0xffffffff;
233
+
234
+ /**
235
+ * @return The current CRC
236
+ */
237
+ this.getCRC = function() {
238
+ return (~crc) >>> 0; // return an unsigned value
239
+ };
240
+
241
+ /**
242
+ * Update the CRC with a single byte
243
+ * @param value The value to update the CRC with
244
+ */
245
+ this.updateCRC = function(value) {
246
+ crc = (crc << 8) ^ crc32Lookup[((crc >>> 24) ^ value) & 0xff];
247
+ };
248
+
249
+ /**
250
+ * Update the CRC with a sequence of identical bytes
251
+ * @param value The value to update the CRC with
252
+ * @param count The number of bytes
253
+ */
254
+ this.updateCRCRun = function(value, count) {
255
+ while (count-- > 0) {
256
+ crc = (crc << 8) ^ crc32Lookup[((crc >>> 24) ^ value) & 0xff];
257
+ }
258
+ };
259
+ };
260
+ return CRC32;
261
+ })();
262
+
263
+ /*
264
+ seek-bzip - a pure-javascript module for seeking within bzip2 data
265
+
266
+ Copyright (C) 2013 C. Scott Ananian
267
+ Copyright (C) 2012 Eli Skeggs
268
+ Copyright (C) 2011 Kevin Kwok
269
+
270
+ This library is free software; you can redistribute it and/or
271
+ modify it under the terms of the GNU Lesser General Public
272
+ License as published by the Free Software Foundation; either
273
+ version 2.1 of the License, or (at your option) any later version.
274
+
275
+ This library is distributed in the hope that it will be useful,
276
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
277
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
278
+ Lesser General Public License for more details.
279
+
280
+ You should have received a copy of the GNU Lesser General Public
281
+ License along with this library; if not, see
282
+ http://www.gnu.org/licenses/lgpl-2.1.html
283
+
284
+ Adapted from node-bzip, copyright 2012 Eli Skeggs.
285
+ Adapted from bzip2.js, copyright 2011 Kevin Kwok (antimatter15@gmail.com).
286
+
287
+ Based on micro-bunzip by Rob Landley (rob@landley.net).
288
+
289
+ Based on bzip2 decompression code by Julian R Seward (jseward@acm.org),
290
+ which also acknowledges contributions by Mike Burrows, David Wheeler,
291
+ Peter Fenwick, Alistair Moffat, Radford Neal, Ian H. Witten,
292
+ Robert Sedgewick, and Jon L. Bentley.
293
+ */
294
+
295
+ var BitReader = bitreader;
296
+ var Stream = stream;
297
+ var CRC32 = crc32;
298
+
299
+ var MAX_HUFCODE_BITS = 20;
300
+ var MAX_SYMBOLS = 258;
301
+ var SYMBOL_RUNA = 0;
302
+ var SYMBOL_RUNB = 1;
303
+ var MIN_GROUPS = 2;
304
+ var MAX_GROUPS = 6;
305
+ var GROUP_SIZE = 50;
306
+
307
+ var WHOLEPI = "314159265359";
308
+ var SQRTPI = "177245385090";
309
+
310
+ var mtf = function(array, index) {
311
+ var src = array[index], i;
312
+ for (i = index; i > 0; i--) {
313
+ array[i] = array[i-1];
314
+ }
315
+ array[0] = src;
316
+ return src;
317
+ };
318
+
319
+ var Err = {
320
+ OK: 0,
321
+ LAST_BLOCK: -1,
322
+ NOT_BZIP_DATA: -2,
323
+ UNEXPECTED_INPUT_EOF: -3,
324
+ UNEXPECTED_OUTPUT_EOF: -4,
325
+ DATA_ERROR: -5,
326
+ OUT_OF_MEMORY: -6,
327
+ OBSOLETE_INPUT: -7,
328
+ END_OF_BLOCK: -8
329
+ };
330
+ var ErrorMessages = {};
331
+ ErrorMessages[Err.LAST_BLOCK] = "Bad file checksum";
332
+ ErrorMessages[Err.NOT_BZIP_DATA] = "Not bzip data";
333
+ ErrorMessages[Err.UNEXPECTED_INPUT_EOF] = "Unexpected input EOF";
334
+ ErrorMessages[Err.UNEXPECTED_OUTPUT_EOF] = "Unexpected output EOF";
335
+ ErrorMessages[Err.DATA_ERROR] = "Data error";
336
+ ErrorMessages[Err.OUT_OF_MEMORY] = "Out of memory";
337
+ ErrorMessages[Err.OBSOLETE_INPUT] = "Obsolete (pre 0.9.5) bzip format not supported.";
338
+
339
+ var _throw = function(status, optDetail) {
340
+ var msg = ErrorMessages[status] || 'unknown error';
341
+ if (optDetail) { msg += ': '+optDetail; }
342
+ var e = new TypeError(msg);
343
+ e.errorCode = status;
344
+ throw e;
345
+ };
346
+
347
+ var Bunzip = function(inputStream, outputStream) {
348
+ this.writePos = this.writeCurrent = this.writeCount = 0;
349
+
350
+ this._start_bunzip(inputStream, outputStream);
351
+ };
352
+ Bunzip.prototype._init_block = function() {
353
+ var moreBlocks = this._get_next_block();
354
+ if ( !moreBlocks ) {
355
+ this.writeCount = -1;
356
+ return false; /* no more blocks */
357
+ }
358
+ this.blockCRC = new CRC32();
359
+ return true;
360
+ };
361
+ /* XXX micro-bunzip uses (inputStream, inputBuffer, len) as arguments */
362
+ Bunzip.prototype._start_bunzip = function(inputStream, outputStream) {
363
+ /* Ensure that file starts with "BZh['1'-'9']." */
364
+ var buf = new Uint8Array(4);
365
+ if (inputStream.read(buf, 0, 4) !== 4 ||
366
+ String.fromCharCode(buf[0], buf[1], buf[2]) !== 'BZh')
367
+ _throw(Err.NOT_BZIP_DATA, 'bad magic');
368
+
369
+ var level = buf[3] - 0x30;
370
+ if (level < 1 || level > 9)
371
+ _throw(Err.NOT_BZIP_DATA, 'level out of range');
372
+
373
+ this.reader = new BitReader(inputStream);
374
+
375
+ /* Fourth byte (ascii '1'-'9'), indicates block size in units of 100k of
376
+ uncompressed data. Allocate intermediate buffer for block. */
377
+ this.dbufSize = 100000 * level;
378
+ this.nextoutput = 0;
379
+ this.outputStream = outputStream;
380
+ this.streamCRC = 0;
381
+ };
382
+ Bunzip.prototype._get_next_block = function() {
383
+ var i, j, k;
384
+ var reader = this.reader;
385
+ // this is get_next_block() function from micro-bunzip:
386
+ /* Read in header signature and CRC, then validate signature.
387
+ (last block signature means CRC is for whole file, return now) */
388
+ var h = reader.pi();
389
+ if (h === SQRTPI) { // last block
390
+ return false; /* no more blocks */
391
+ }
392
+ if (h !== WHOLEPI)
393
+ _throw(Err.NOT_BZIP_DATA);
394
+ this.targetBlockCRC = reader.read(32) >>> 0; // (convert to unsigned)
395
+ this.streamCRC = (this.targetBlockCRC ^
396
+ ((this.streamCRC << 1) | (this.streamCRC>>>31))) >>> 0;
397
+ /* We can add support for blockRandomised if anybody complains. There was
398
+ some code for this in busybox 1.0.0-pre3, but nobody ever noticed that
399
+ it didn't actually work. */
400
+ if (reader.read(1))
401
+ _throw(Err.OBSOLETE_INPUT);
402
+ var origPointer = reader.read(24);
403
+ if (origPointer > this.dbufSize)
404
+ _throw(Err.DATA_ERROR, 'initial position out of bounds');
405
+ /* mapping table: if some byte values are never used (encoding things
406
+ like ascii text), the compression code removes the gaps to have fewer
407
+ symbols to deal with, and writes a sparse bitfield indicating which
408
+ values were present. We make a translation table to convert the symbols
409
+ back to the corresponding bytes. */
410
+ var t = reader.read(16);
411
+ var symToByte = new Uint8Array(256), symTotal = 0;
412
+ for (i = 0; i < 16; i++) {
413
+ if (t & (1 << (0xF - i))) {
414
+ var o = i * 16;
415
+ k = reader.read(16);
416
+ for (j = 0; j < 16; j++)
417
+ if (k & (1 << (0xF - j)))
418
+ symToByte[symTotal++] = o + j;
419
+ }
420
+ }
421
+
422
+ /* How many different huffman coding groups does this block use? */
423
+ var groupCount = reader.read(3);
424
+ if (groupCount < MIN_GROUPS || groupCount > MAX_GROUPS)
425
+ _throw(Err.DATA_ERROR);
426
+ /* nSelectors: Every GROUP_SIZE many symbols we select a new huffman coding
427
+ group. Read in the group selector list, which is stored as MTF encoded
428
+ bit runs. (MTF=Move To Front, as each value is used it's moved to the
429
+ start of the list.) */
430
+ var nSelectors = reader.read(15);
431
+ if (nSelectors === 0)
432
+ _throw(Err.DATA_ERROR);
433
+
434
+ var mtfSymbol = new Uint8Array(256);
435
+ for (i = 0; i < groupCount; i++)
436
+ mtfSymbol[i] = i;
437
+
438
+ var selectors = new Uint8Array(nSelectors); // was 32768...
439
+
440
+ for (i = 0; i < nSelectors; i++) {
441
+ /* Get next value */
442
+ for (j = 0; reader.read(1); j++)
443
+ if (j >= groupCount) _throw(Err.DATA_ERROR);
444
+ /* Decode MTF to get the next selector */
445
+ selectors[i] = mtf(mtfSymbol, j);
446
+ }
447
+
448
+ /* Read the huffman coding tables for each group, which code for symTotal
449
+ literal symbols, plus two run symbols (RUNA, RUNB) */
450
+ var symCount = symTotal + 2;
451
+ var groups = [], hufGroup;
452
+ for (j = 0; j < groupCount; j++) {
453
+ var length = new Uint8Array(symCount), temp = new Uint16Array(MAX_HUFCODE_BITS + 1);
454
+ /* Read huffman code lengths for each symbol. They're stored in
455
+ a way similar to mtf; record a starting value for the first symbol,
456
+ and an offset from the previous value for everys symbol after that. */
457
+ t = reader.read(5); // lengths
458
+ for (i = 0; i < symCount; i++) {
459
+ for (;;) {
460
+ if (t < 1 || t > MAX_HUFCODE_BITS) _throw(Err.DATA_ERROR);
461
+ /* If first bit is 0, stop. Else second bit indicates whether
462
+ to increment or decrement the value. */
463
+ if(!reader.read(1))
464
+ break;
465
+ if(!reader.read(1))
466
+ t++;
467
+ else
468
+ t--;
469
+ }
470
+ length[i] = t;
471
+ }
472
+
473
+ /* Find largest and smallest lengths in this group */
474
+ var minLen, maxLen;
475
+ minLen = maxLen = length[0];
476
+ for (i = 1; i < symCount; i++) {
477
+ if (length[i] > maxLen)
478
+ maxLen = length[i];
479
+ else if (length[i] < minLen)
480
+ minLen = length[i];
481
+ }
482
+
483
+ /* Calculate permute[], base[], and limit[] tables from length[].
484
+ *
485
+ * permute[] is the lookup table for converting huffman coded symbols
486
+ * into decoded symbols. base[] is the amount to subtract from the
487
+ * value of a huffman symbol of a given length when using permute[].
488
+ *
489
+ * limit[] indicates the largest numerical value a symbol with a given
490
+ * number of bits can have. This is how the huffman codes can vary in
491
+ * length: each code with a value>limit[length] needs another bit.
492
+ */
493
+ hufGroup = {};
494
+ groups.push(hufGroup);
495
+ hufGroup.permute = new Uint16Array(MAX_SYMBOLS);
496
+ hufGroup.limit = new Uint32Array(MAX_HUFCODE_BITS + 2);
497
+ hufGroup.base = new Uint32Array(MAX_HUFCODE_BITS + 1);
498
+ hufGroup.minLen = minLen;
499
+ hufGroup.maxLen = maxLen;
500
+ /* Calculate permute[]. Concurently, initialize temp[] and limit[]. */
501
+ var pp = 0;
502
+ for (i = minLen; i <= maxLen; i++) {
503
+ temp[i] = hufGroup.limit[i] = 0;
504
+ for (t = 0; t < symCount; t++)
505
+ if (length[t] === i)
506
+ hufGroup.permute[pp++] = t;
507
+ }
508
+ /* Count symbols coded for at each bit length */
509
+ for (i = 0; i < symCount; i++)
510
+ temp[length[i]]++;
511
+ /* Calculate limit[] (the largest symbol-coding value at each bit
512
+ * length, which is (previous limit<<1)+symbols at this level), and
513
+ * base[] (number of symbols to ignore at each bit length, which is
514
+ * limit minus the cumulative count of symbols coded for already). */
515
+ pp = t = 0;
516
+ for (i = minLen; i < maxLen; i++) {
517
+ pp += temp[i];
518
+ /* We read the largest possible symbol size and then unget bits
519
+ after determining how many we need, and those extra bits could
520
+ be set to anything. (They're noise from future symbols.) At
521
+ each level we're really only interested in the first few bits,
522
+ so here we set all the trailing to-be-ignored bits to 1 so they
523
+ don't affect the value>limit[length] comparison. */
524
+ hufGroup.limit[i] = pp - 1;
525
+ pp <<= 1;
526
+ t += temp[i];
527
+ hufGroup.base[i + 1] = pp - t;
528
+ }
529
+ hufGroup.limit[maxLen + 1] = Number.MAX_VALUE; /* Sentinal value for reading next sym. */
530
+ hufGroup.limit[maxLen] = pp + temp[maxLen] - 1;
531
+ hufGroup.base[minLen] = 0;
532
+ }
533
+ /* We've finished reading and digesting the block header. Now read this
534
+ block's huffman coded symbols from the file and undo the huffman coding
535
+ and run length encoding, saving the result into dbuf[dbufCount++]=uc */
536
+
537
+ /* Initialize symbol occurrence counters and symbol Move To Front table */
538
+ var byteCount = new Uint32Array(256);
539
+ for (i = 0; i < 256; i++)
540
+ mtfSymbol[i] = i;
541
+ /* Loop through compressed symbols. */
542
+ var runPos = 0, dbufCount = 0, selector = 0, uc;
543
+ var dbuf = this.dbuf = new Uint32Array(this.dbufSize);
544
+ symCount = 0;
545
+ for (;;) {
546
+ /* Determine which huffman coding group to use. */
547
+ if (!(symCount--)) {
548
+ symCount = GROUP_SIZE - 1;
549
+ if (selector >= nSelectors) { _throw(Err.DATA_ERROR); }
550
+ hufGroup = groups[selectors[selector++]];
551
+ }
552
+ /* Read next huffman-coded symbol. */
553
+ i = hufGroup.minLen;
554
+ j = reader.read(i);
555
+ for (;;i++) {
556
+ if (i > hufGroup.maxLen) { _throw(Err.DATA_ERROR); }
557
+ if (j <= hufGroup.limit[i])
558
+ break;
559
+ j = (j << 1) | reader.read(1);
560
+ }
561
+ /* Huffman decode value to get nextSym (with bounds checking) */
562
+ j -= hufGroup.base[i];
563
+ if (j < 0 || j >= MAX_SYMBOLS) { _throw(Err.DATA_ERROR); }
564
+ var nextSym = hufGroup.permute[j];
565
+ /* We have now decoded the symbol, which indicates either a new literal
566
+ byte, or a repeated run of the most recent literal byte. First,
567
+ check if nextSym indicates a repeated run, and if so loop collecting
568
+ how many times to repeat the last literal. */
569
+ if (nextSym === SYMBOL_RUNA || nextSym === SYMBOL_RUNB) {
570
+ /* If this is the start of a new run, zero out counter */
571
+ if (!runPos){
572
+ runPos = 1;
573
+ t = 0;
574
+ }
575
+ /* Neat trick that saves 1 symbol: instead of or-ing 0 or 1 at
576
+ each bit position, add 1 or 2 instead. For example,
577
+ 1011 is 1<<0 + 1<<1 + 2<<2. 1010 is 2<<0 + 2<<1 + 1<<2.
578
+ You can make any bit pattern that way using 1 less symbol than
579
+ the basic or 0/1 method (except all bits 0, which would use no
580
+ symbols, but a run of length 0 doesn't mean anything in this
581
+ context). Thus space is saved. */
582
+ if (nextSym === SYMBOL_RUNA)
583
+ t += runPos;
584
+ else
585
+ t += 2 * runPos;
586
+ runPos <<= 1;
587
+ continue;
588
+ }
589
+ /* When we hit the first non-run symbol after a run, we now know
590
+ how many times to repeat the last literal, so append that many
591
+ copies to our buffer of decoded symbols (dbuf) now. (The last
592
+ literal used is the one at the head of the mtfSymbol array.) */
593
+ if (runPos){
594
+ runPos = 0;
595
+ if (dbufCount + t > this.dbufSize) { _throw(Err.DATA_ERROR); }
596
+ uc = symToByte[mtfSymbol[0]];
597
+ byteCount[uc] += t;
598
+ while (t--)
599
+ dbuf[dbufCount++] = uc;
600
+ }
601
+ /* Is this the terminating symbol? */
602
+ if (nextSym > symTotal)
603
+ break;
604
+ /* At this point, nextSym indicates a new literal character. Subtract
605
+ one to get the position in the MTF array at which this literal is
606
+ currently to be found. (Note that the result can't be -1 or 0,
607
+ because 0 and 1 are RUNA and RUNB. But another instance of the
608
+ first symbol in the mtf array, position 0, would have been handled
609
+ as part of a run above. Therefore 1 unused mtf position minus
610
+ 2 non-literal nextSym values equals -1.) */
611
+ if (dbufCount >= this.dbufSize) { _throw(Err.DATA_ERROR); }
612
+ i = nextSym - 1;
613
+ uc = mtf(mtfSymbol, i);
614
+ uc = symToByte[uc];
615
+ /* We have our literal byte. Save it into dbuf. */
616
+ byteCount[uc]++;
617
+ dbuf[dbufCount++] = uc;
618
+ }
619
+ /* At this point, we've read all the huffman-coded symbols (and repeated
620
+ runs) for this block from the input stream, and decoded them into the
621
+ intermediate buffer. There are dbufCount many decoded bytes in dbuf[].
622
+ Now undo the Burrows-Wheeler transform on dbuf.
623
+ See http://dogma.net/markn/articles/bwt/bwt.htm
624
+ */
625
+ if (origPointer < 0 || origPointer >= dbufCount) { _throw(Err.DATA_ERROR); }
626
+ /* Turn byteCount into cumulative occurrence counts of 0 to n-1. */
627
+ j = 0;
628
+ for (i = 0; i < 256; i++) {
629
+ k = j + byteCount[i];
630
+ byteCount[i] = j;
631
+ j = k;
632
+ }
633
+ /* Figure out what order dbuf would be in if we sorted it. */
634
+ for (i = 0; i < dbufCount; i++) {
635
+ uc = dbuf[i] & 0xff;
636
+ dbuf[byteCount[uc]] |= (i << 8);
637
+ byteCount[uc]++;
638
+ }
639
+ /* Decode first byte by hand to initialize "previous" byte. Note that it
640
+ doesn't get output, and if the first three characters are identical
641
+ it doesn't qualify as a run (hence writeRunCountdown=5). */
642
+ var pos = 0, current = 0, run = 0;
643
+ if (dbufCount) {
644
+ pos = dbuf[origPointer];
645
+ current = (pos & 0xff);
646
+ pos >>= 8;
647
+ run = -1;
648
+ }
649
+ this.writePos = pos;
650
+ this.writeCurrent = current;
651
+ this.writeCount = dbufCount;
652
+ this.writeRun = run;
653
+
654
+ return true; /* more blocks to come */
655
+ };
656
+ /* Undo burrows-wheeler transform on intermediate buffer to produce output.
657
+ If start_bunzip was initialized with out_fd=-1, then up to len bytes of
658
+ data are written to outbuf. Return value is number of bytes written or
659
+ error (all errors are negative numbers). If out_fd!=-1, outbuf and len
660
+ are ignored, data is written to out_fd and return is RETVAL_OK or error.
661
+ */
662
+ Bunzip.prototype._read_bunzip = function(outputBuffer, len) {
663
+ var copies, previous, outbyte;
664
+ /* james@jamestaylor.org: writeCount goes to -1 when the buffer is fully
665
+ decoded, which results in this returning RETVAL_LAST_BLOCK, also
666
+ equal to -1... Confusing, I'm returning 0 here to indicate no
667
+ bytes written into the buffer */
668
+ if (this.writeCount < 0) { return 0; }
669
+ var dbuf = this.dbuf, pos = this.writePos, current = this.writeCurrent;
670
+ var dbufCount = this.writeCount; this.outputsize;
671
+ var run = this.writeRun;
672
+
673
+ while (dbufCount) {
674
+ dbufCount--;
675
+ previous = current;
676
+ pos = dbuf[pos];
677
+ current = pos & 0xff;
678
+ pos >>= 8;
679
+ if (run++ === 3){
680
+ copies = current;
681
+ outbyte = previous;
682
+ current = -1;
683
+ } else {
684
+ copies = 1;
685
+ outbyte = current;
686
+ }
687
+ this.blockCRC.updateCRCRun(outbyte, copies);
688
+ while (copies--) {
689
+ this.outputStream.writeByte(outbyte);
690
+ this.nextoutput++;
691
+ }
692
+ if (current != previous)
693
+ run = 0;
694
+ }
695
+ this.writeCount = dbufCount;
696
+ // check CRC
697
+ if (this.blockCRC.getCRC() !== this.targetBlockCRC) {
698
+ _throw(Err.DATA_ERROR, "Bad block CRC "+
699
+ "(got "+this.blockCRC.getCRC().toString(16)+
700
+ " expected "+this.targetBlockCRC.toString(16)+")");
701
+ }
702
+ return this.nextoutput;
703
+ };
704
+
705
+ var coerceInputStream = function(input) {
706
+ if ('readByte' in input) { return input; }
707
+ var inputStream = new Stream();
708
+ inputStream.pos = 0;
709
+ inputStream.readByte = function() { return input[this.pos++]; };
710
+ inputStream.seek = function(pos) { this.pos = pos; };
711
+ inputStream.eof = function() { return this.pos >= input.length; };
712
+ return inputStream;
713
+ };
714
+ var coerceOutputStream = function(output) {
715
+ var outputStream = new Stream();
716
+ var resizeOk = true;
717
+ if (output) {
718
+ if (typeof(output)==='number') {
719
+ outputStream.buffer = new Uint8Array(output);
720
+ resizeOk = false;
721
+ } else if ('writeByte' in output) {
722
+ return output;
723
+ } else {
724
+ outputStream.buffer = output;
725
+ resizeOk = false;
726
+ }
727
+ } else {
728
+ outputStream.buffer = new Uint8Array(16384);
729
+ }
730
+ outputStream.pos = 0;
731
+ outputStream.writeByte = function(_byte) {
732
+ if (resizeOk && this.pos >= this.buffer.length) {
733
+ var newBuffer = new Uint8Array(this.buffer.length*2);
734
+ newBuffer.set(this.buffer);
735
+ this.buffer = newBuffer;
736
+ }
737
+ this.buffer[this.pos++] = _byte;
738
+ };
739
+ outputStream.getBuffer = function() {
740
+ // trim buffer
741
+ if (this.pos !== this.buffer.length) {
742
+ if (!resizeOk)
743
+ throw new TypeError('outputsize does not match decoded input');
744
+ var newBuffer = new Uint8Array(this.pos);
745
+ newBuffer.set(this.buffer.subarray(0, this.pos));
746
+ this.buffer = newBuffer;
747
+ }
748
+ return this.buffer;
749
+ };
750
+ outputStream._coerced = true;
751
+ return outputStream;
752
+ };
753
+
754
+ /* Static helper functions */
755
+ // 'input' can be a stream or a buffer
756
+ // 'output' can be a stream or a buffer or a number (buffer size)
757
+ const decode = function(input, output, multistream) {
758
+ // make a stream from a buffer, if necessary
759
+ var inputStream = coerceInputStream(input);
760
+ var outputStream = coerceOutputStream(output);
761
+
762
+ var bz = new Bunzip(inputStream, outputStream);
763
+ while (true) {
764
+ if ('eof' in inputStream && inputStream.eof()) break;
765
+ if (bz._init_block()) {
766
+ bz._read_bunzip();
767
+ } else {
768
+ var targetStreamCRC = bz.reader.read(32) >>> 0; // (convert to unsigned)
769
+ if (targetStreamCRC !== bz.streamCRC) {
770
+ _throw(Err.DATA_ERROR, "Bad stream CRC "+
771
+ "(got "+bz.streamCRC.toString(16)+
772
+ " expected "+targetStreamCRC.toString(16)+")");
773
+ }
774
+ if (multistream &&
775
+ 'eof' in inputStream &&
776
+ !inputStream.eof()) {
777
+ // note that start_bunzip will also resync the bit reader to next byte
778
+ bz._start_bunzip(inputStream, outputStream);
779
+ } else break;
780
+ }
781
+ }
782
+ if ('getBuffer' in outputStream)
783
+ return outputStream.getBuffer();
784
+ };
785
+ const decodeBlock = function(input, pos, output) {
786
+ // make a stream from a buffer, if necessary
787
+ var inputStream = coerceInputStream(input);
788
+ var outputStream = coerceOutputStream(output);
789
+ var bz = new Bunzip(inputStream, outputStream);
790
+ bz.reader.seek(pos);
791
+ /* Fill the decode buffer for the block */
792
+ var moreBlocks = bz._get_next_block();
793
+ if (moreBlocks) {
794
+ /* Init the CRC for writing */
795
+ bz.blockCRC = new CRC32();
796
+
797
+ /* Zero this so the current byte from before the seek is not written */
798
+ bz.writeCopies = 0;
799
+
800
+ /* Decompress the block and write to stdout */
801
+ bz._read_bunzip();
802
+ // XXX keep writing?
803
+ }
804
+ if ('getBuffer' in outputStream)
805
+ return outputStream.getBuffer();
806
+ };
807
+ /* Reads bzip2 file from stream or buffer `input`, and invoke
808
+ * `callback(position, size)` once for each bzip2 block,
809
+ * where position gives the starting position (in *bits*)
810
+ * and size gives uncompressed size of the block (in *bytes*). */
811
+ const table = function(input, callback, multistream) {
812
+ // make a stream from a buffer, if necessary
813
+ var inputStream = new Stream();
814
+ inputStream.delegate = coerceInputStream(input);
815
+ inputStream.pos = 0;
816
+ inputStream.readByte = function() {
817
+ this.pos++;
818
+ return this.delegate.readByte();
819
+ };
820
+ if (inputStream.delegate.eof) {
821
+ inputStream.eof = inputStream.delegate.eof.bind(inputStream.delegate);
822
+ }
823
+ var outputStream = new Stream();
824
+ outputStream.pos = 0;
825
+ outputStream.writeByte = function() { this.pos++; };
826
+
827
+ var bz = new Bunzip(inputStream, outputStream);
828
+ var blockSize = bz.dbufSize;
829
+ while (true) {
830
+ if ('eof' in inputStream && inputStream.eof()) break;
831
+
832
+ var position = inputStream.pos*8 + bz.reader.bitOffset;
833
+ if (bz.reader.hasByte) { position -= 8; }
834
+
835
+ if (bz._init_block()) {
836
+ var start = outputStream.pos;
837
+ bz._read_bunzip();
838
+ callback(position, outputStream.pos - start);
839
+ } else {
840
+ bz.reader.read(32); // (but we ignore the crc)
841
+ if (multistream &&
842
+ 'eof' in inputStream &&
843
+ !inputStream.eof()) {
844
+ // note that start_bunzip will also resync the bit reader to next byte
845
+ bz._start_bunzip(inputStream, outputStream);
846
+ console.assert(bz.dbufSize === blockSize,
847
+ "shouldn't change block size within multistream file");
848
+ } else break;
849
+ }
850
+ }
851
+ };
852
+
853
+ var lib = {
854
+ Bunzip,
855
+ Stream,
856
+ Err,
857
+ decode,
858
+ decodeBlock,
859
+ table
860
+ };
861
+
862
+ var index = /*#__PURE__*/_mergeNamespaces({
863
+ __proto__: null
864
+ }, [lib]);
865
+
866
+ export { index as i };