@protontech/openpgp 6.1.1-patch.5 → 6.2.2

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 (49) hide show
  1. package/README.md +13 -2
  2. package/dist/lightweight/argon2id.min.mjs +2 -2
  3. package/dist/lightweight/argon2id.min.mjs.map +1 -1
  4. package/dist/lightweight/argon2id.mjs +4 -4
  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 +10 -10
  8. package/dist/lightweight/nacl-fast.min.mjs +3 -0
  9. package/dist/lightweight/nacl-fast.min.mjs.map +1 -0
  10. package/dist/lightweight/nacl-fast.mjs +1382 -0
  11. package/dist/lightweight/noble_curves.min.mjs +11 -12
  12. package/dist/lightweight/noble_curves.min.mjs.map +1 -1
  13. package/dist/lightweight/noble_curves.mjs +2175 -1752
  14. package/dist/lightweight/noble_hashes.min.mjs +2 -2
  15. package/dist/lightweight/noble_hashes.min.mjs.map +1 -1
  16. package/dist/lightweight/noble_hashes.mjs +80 -51
  17. package/dist/lightweight/noble_post_quantum.min.mjs +3 -4
  18. package/dist/lightweight/noble_post_quantum.min.mjs.map +1 -1
  19. package/dist/lightweight/noble_post_quantum.mjs +352 -10
  20. package/dist/lightweight/openpgp.min.mjs +3 -4
  21. package/dist/lightweight/openpgp.min.mjs.map +1 -1
  22. package/dist/lightweight/openpgp.mjs +1004 -2822
  23. package/dist/lightweight/seek-bzip.min.mjs +2 -2
  24. package/dist/lightweight/seek-bzip.min.mjs.map +1 -1
  25. package/dist/lightweight/seek-bzip.mjs +780 -746
  26. package/dist/lightweight/sha512.min.mjs +4 -2
  27. package/dist/lightweight/sha512.min.mjs.map +1 -1
  28. package/dist/lightweight/sha512.mjs +672 -130
  29. package/dist/node/openpgp.cjs +10691 -10143
  30. package/dist/node/openpgp.min.cjs +14 -17
  31. package/dist/node/openpgp.min.cjs.map +1 -1
  32. package/dist/node/openpgp.min.mjs +14 -17
  33. package/dist/node/openpgp.min.mjs.map +1 -1
  34. package/dist/node/openpgp.mjs +10691 -10142
  35. package/dist/openpgp.js +11739 -11195
  36. package/dist/openpgp.min.js +14 -17
  37. package/dist/openpgp.min.js.map +1 -1
  38. package/dist/openpgp.min.mjs +14 -17
  39. package/dist/openpgp.min.mjs.map +1 -1
  40. package/dist/openpgp.mjs +11739 -11195
  41. package/{src → dist/types}/config/config.d.ts +1 -21
  42. package/{openpgp.d.ts → dist/types/index.d.ts} +26 -8
  43. package/dist/types/packet/grammar.d.ts +33 -0
  44. package/package.json +38 -38
  45. package/dist/lightweight/sha3.min.mjs +0 -4
  46. package/dist/lightweight/sha3.min.mjs.map +0 -1
  47. package/dist/lightweight/sha3.mjs +0 -401
  48. /package/{src → dist/types}/config/index.d.ts +0 -0
  49. /package/{src → dist/types}/enums.d.ts +0 -0
@@ -1,4 +1,4 @@
1
- /*! OpenPGP.js v6.1.1-patch.5 - 2025-08-13 - this is LGPL licensed code, see LICENSE/our website https://openpgpjs.org/ for more information. */
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
2
  const globalThis = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
3
3
 
4
4
  function _mergeNamespaces(n, m) {
@@ -45,114 +45,130 @@ Peter Fenwick, Alistair Moffat, Radford Neal, Ian H. Witten,
45
45
  Robert Sedgewick, and Jon L. Bentley.
46
46
  */
47
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('');
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;
109
119
  }
110
120
 
111
- var bitreader = BitReader$1;
112
-
113
121
  /* very simple input/output stream interface */
114
122
 
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;
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
+ }
156
172
 
157
173
  /* CRC32, used in Bzip2 implementation.
158
174
  * This is a port of CRC32.java from the jbzip2 implementation at
@@ -185,80 +201,88 @@ var stream = Stream$1;
185
201
  * with the same licensing terms as Matthew Francis' original implementation.
186
202
  */
187
203
 
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
- })();
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
+ }
262
286
 
263
287
  /*
264
288
  seek-bzip - a pure-javascript module for seeking within bzip2 data
@@ -292,575 +316,585 @@ Peter Fenwick, Alistair Moffat, Radford Neal, Ian H. Witten,
292
316
  Robert Sedgewick, and Jon L. Bentley.
293
317
  */
294
318
 
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
- };
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();
861
895
 
862
896
  var index = /*#__PURE__*/_mergeNamespaces({
863
897
  __proto__: null
864
- }, [lib]);
898
+ }, [libExports]);
865
899
 
866
900
  export { index as i };