@protontech/openpgp 6.2.2 → 6.3.0

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