@gmod/cram 4.0.9 → 5.0.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 (246) hide show
  1. package/README.md +6 -6
  2. package/dist/craiIndex.d.ts +1 -1
  3. package/dist/craiIndex.js +7 -7
  4. package/dist/craiIndex.js.map +1 -1
  5. package/dist/cram-bundle.js +1 -2
  6. package/dist/cramFile/codecs/_base.d.ts +3 -3
  7. package/dist/cramFile/codecs/beta.d.ts +5 -5
  8. package/dist/cramFile/codecs/beta.js +7 -7
  9. package/dist/cramFile/codecs/beta.js.map +1 -1
  10. package/dist/cramFile/codecs/byteArrayLength.d.ts +5 -5
  11. package/dist/cramFile/codecs/byteArrayLength.js +4 -4
  12. package/dist/cramFile/codecs/byteArrayLength.js.map +1 -1
  13. package/dist/cramFile/codecs/byteArrayStop.d.ts +5 -5
  14. package/dist/cramFile/codecs/byteArrayStop.js +7 -7
  15. package/dist/cramFile/codecs/byteArrayStop.js.map +1 -1
  16. package/dist/cramFile/codecs/dataSeriesTypes.d.ts +1 -1
  17. package/dist/cramFile/codecs/external.d.ts +5 -5
  18. package/dist/cramFile/codecs/external.js +12 -11
  19. package/dist/cramFile/codecs/external.js.map +1 -1
  20. package/dist/cramFile/codecs/gamma.d.ts +5 -5
  21. package/dist/cramFile/codecs/gamma.js +8 -8
  22. package/dist/cramFile/codecs/gamma.js.map +1 -1
  23. package/dist/cramFile/codecs/huffman.d.ts +6 -6
  24. package/dist/cramFile/codecs/huffman.js +9 -9
  25. package/dist/cramFile/codecs/huffman.js.map +1 -1
  26. package/dist/cramFile/codecs/index.d.ts +3 -3
  27. package/dist/cramFile/codecs/index.js +16 -16
  28. package/dist/cramFile/codecs/index.js.map +1 -1
  29. package/dist/cramFile/codecs/subexp.d.ts +5 -5
  30. package/dist/cramFile/codecs/subexp.js +9 -9
  31. package/dist/cramFile/codecs/subexp.js.map +1 -1
  32. package/dist/cramFile/container/compressionScheme.d.ts +4 -4
  33. package/dist/cramFile/container/compressionScheme.js +5 -5
  34. package/dist/cramFile/container/compressionScheme.js.map +1 -1
  35. package/dist/cramFile/container/index.d.ts +8 -8
  36. package/dist/cramFile/container/index.js +15 -16
  37. package/dist/cramFile/container/index.js.map +1 -1
  38. package/dist/cramFile/file.d.ts +3 -3
  39. package/dist/cramFile/file.js +29 -38
  40. package/dist/cramFile/file.js.map +1 -1
  41. package/dist/cramFile/index.d.ts +2 -2
  42. package/dist/cramFile/index.js +4 -4
  43. package/dist/cramFile/index.js.map +1 -1
  44. package/dist/cramFile/record.d.ts +2 -2
  45. package/dist/cramFile/record.js +17 -17
  46. package/dist/cramFile/record.js.map +1 -1
  47. package/dist/cramFile/sectionParsers.d.ts +3 -3
  48. package/dist/cramFile/sectionParsers.js +56 -56
  49. package/dist/cramFile/sectionParsers.js.map +1 -1
  50. package/dist/cramFile/slice/decodeRecord.d.ts +6 -6
  51. package/dist/cramFile/slice/decodeRecord.js +17 -17
  52. package/dist/cramFile/slice/decodeRecord.js.map +1 -1
  53. package/dist/cramFile/slice/index.d.ts +5 -5
  54. package/dist/cramFile/slice/index.js +46 -52
  55. package/dist/cramFile/slice/index.js.map +1 -1
  56. package/dist/htscodecs/arith_gen.d.ts +1 -1
  57. package/dist/htscodecs/arith_gen.js +17 -17
  58. package/dist/htscodecs/arith_gen.js.map +1 -1
  59. package/dist/htscodecs/fqzcomp.js +10 -10
  60. package/dist/htscodecs/fqzcomp.js.map +1 -1
  61. package/dist/htscodecs/index.js +6 -6
  62. package/dist/htscodecs/index.js.map +1 -1
  63. package/dist/htscodecs/rans.js +2 -2
  64. package/dist/htscodecs/rans.js.map +1 -1
  65. package/dist/htscodecs/rans4x16.js +7 -7
  66. package/dist/htscodecs/rans4x16.js.map +1 -1
  67. package/dist/htscodecs/tok3.js +9 -9
  68. package/dist/htscodecs/tok3.js.map +1 -1
  69. package/dist/index.d.ts +3 -3
  70. package/dist/index.js +7 -7
  71. package/dist/index.js.map +1 -1
  72. package/dist/indexedCramFile.d.ts +4 -4
  73. package/dist/indexedCramFile.js +5 -5
  74. package/dist/indexedCramFile.js.map +1 -1
  75. package/dist/{io/index.js → io.js} +1 -1
  76. package/dist/io.js.map +1 -0
  77. package/dist/rans/d04.js +28 -28
  78. package/dist/rans/d04.js.map +1 -1
  79. package/dist/rans/d14.js +16 -16
  80. package/dist/rans/d14.js.map +1 -1
  81. package/dist/rans/decoding.js +8 -8
  82. package/dist/rans/decoding.js.map +1 -1
  83. package/dist/rans/frequencies.js +15 -15
  84. package/dist/rans/frequencies.js.map +1 -1
  85. package/dist/rans/index.js +17 -17
  86. package/dist/rans/index.js.map +1 -1
  87. package/dist/seek-bzip/bitreader.d.ts +27 -0
  88. package/dist/seek-bzip/bitreader.js +101 -0
  89. package/dist/seek-bzip/bitreader.js.map +1 -0
  90. package/dist/seek-bzip/crc32.d.ts +0 -0
  91. package/dist/seek-bzip/crc32.js +113 -0
  92. package/dist/seek-bzip/crc32.js.map +1 -0
  93. package/dist/seek-bzip/index.d.ts +9 -0
  94. package/dist/seek-bzip/index.js +585 -0
  95. package/dist/seek-bzip/index.js.map +1 -0
  96. package/dist/seek-bzip/stream.d.ts +1 -0
  97. package/dist/seek-bzip/stream.js +41 -0
  98. package/dist/seek-bzip/stream.js.map +1 -0
  99. package/dist/seek-bzip/toHex.d.ts +1 -0
  100. package/dist/seek-bzip/toHex.js +36 -0
  101. package/dist/seek-bzip/toHex.js.map +1 -0
  102. package/esm/craiIndex.d.ts +1 -1
  103. package/esm/craiIndex.js +3 -3
  104. package/esm/craiIndex.js.map +1 -1
  105. package/esm/cramFile/codecs/_base.d.ts +3 -3
  106. package/esm/cramFile/codecs/beta.d.ts +5 -5
  107. package/esm/cramFile/codecs/beta.js +4 -4
  108. package/esm/cramFile/codecs/beta.js.map +1 -1
  109. package/esm/cramFile/codecs/byteArrayLength.d.ts +5 -5
  110. package/esm/cramFile/codecs/byteArrayLength.js +2 -2
  111. package/esm/cramFile/codecs/byteArrayLength.js.map +1 -1
  112. package/esm/cramFile/codecs/byteArrayStop.d.ts +5 -5
  113. package/esm/cramFile/codecs/byteArrayStop.js +4 -4
  114. package/esm/cramFile/codecs/byteArrayStop.js.map +1 -1
  115. package/esm/cramFile/codecs/dataSeriesTypes.d.ts +1 -1
  116. package/esm/cramFile/codecs/external.d.ts +5 -5
  117. package/esm/cramFile/codecs/external.js +8 -7
  118. package/esm/cramFile/codecs/external.js.map +1 -1
  119. package/esm/cramFile/codecs/gamma.d.ts +5 -5
  120. package/esm/cramFile/codecs/gamma.js +4 -4
  121. package/esm/cramFile/codecs/gamma.js.map +1 -1
  122. package/esm/cramFile/codecs/huffman.d.ts +6 -6
  123. package/esm/cramFile/codecs/huffman.js +5 -5
  124. package/esm/cramFile/codecs/huffman.js.map +1 -1
  125. package/esm/cramFile/codecs/index.d.ts +3 -3
  126. package/esm/cramFile/codecs/index.js +8 -8
  127. package/esm/cramFile/codecs/index.js.map +1 -1
  128. package/esm/cramFile/codecs/subexp.d.ts +5 -5
  129. package/esm/cramFile/codecs/subexp.js +4 -4
  130. package/esm/cramFile/codecs/subexp.js.map +1 -1
  131. package/esm/cramFile/container/compressionScheme.d.ts +4 -4
  132. package/esm/cramFile/container/compressionScheme.js +2 -2
  133. package/esm/cramFile/container/compressionScheme.js.map +1 -1
  134. package/esm/cramFile/container/index.d.ts +8 -8
  135. package/esm/cramFile/container/index.js +5 -6
  136. package/esm/cramFile/container/index.js.map +1 -1
  137. package/esm/cramFile/file.d.ts +3 -3
  138. package/esm/cramFile/file.js +12 -21
  139. package/esm/cramFile/file.js.map +1 -1
  140. package/esm/cramFile/index.d.ts +2 -2
  141. package/esm/cramFile/index.js +2 -2
  142. package/esm/cramFile/index.js.map +1 -1
  143. package/esm/cramFile/record.d.ts +2 -2
  144. package/esm/cramFile/record.js +1 -1
  145. package/esm/cramFile/record.js.map +1 -1
  146. package/esm/cramFile/sectionParsers.d.ts +3 -3
  147. package/esm/cramFile/sectionParsers.js +1 -1
  148. package/esm/cramFile/sectionParsers.js.map +1 -1
  149. package/esm/cramFile/slice/decodeRecord.d.ts +6 -6
  150. package/esm/cramFile/slice/decodeRecord.js +3 -3
  151. package/esm/cramFile/slice/decodeRecord.js.map +1 -1
  152. package/esm/cramFile/slice/index.d.ts +5 -5
  153. package/esm/cramFile/slice/index.js +18 -24
  154. package/esm/cramFile/slice/index.js.map +1 -1
  155. package/esm/htscodecs/arith_gen.d.ts +1 -1
  156. package/esm/htscodecs/arith_gen.js +5 -5
  157. package/esm/htscodecs/arith_gen.js.map +1 -1
  158. package/esm/htscodecs/fqzcomp.js +3 -3
  159. package/esm/htscodecs/fqzcomp.js.map +1 -1
  160. package/esm/htscodecs/index.js +5 -5
  161. package/esm/htscodecs/index.js.map +1 -1
  162. package/esm/htscodecs/rans.js +1 -1
  163. package/esm/htscodecs/rans.js.map +1 -1
  164. package/esm/htscodecs/rans4x16.js +1 -1
  165. package/esm/htscodecs/rans4x16.js.map +1 -1
  166. package/esm/htscodecs/tok3.js +4 -4
  167. package/esm/htscodecs/tok3.js.map +1 -1
  168. package/esm/index.d.ts +3 -3
  169. package/esm/index.js +3 -3
  170. package/esm/index.js.map +1 -1
  171. package/esm/indexedCramFile.d.ts +4 -4
  172. package/esm/indexedCramFile.js +2 -2
  173. package/esm/indexedCramFile.js.map +1 -1
  174. package/esm/{io/index.js → io.js} +1 -1
  175. package/esm/io.js.map +1 -0
  176. package/esm/rans/d04.js +3 -3
  177. package/esm/rans/d04.js.map +1 -1
  178. package/esm/rans/d14.js +2 -2
  179. package/esm/rans/d14.js.map +1 -1
  180. package/esm/rans/decoding.js +2 -2
  181. package/esm/rans/decoding.js.map +1 -1
  182. package/esm/rans/frequencies.js +3 -3
  183. package/esm/rans/frequencies.js.map +1 -1
  184. package/esm/rans/index.js +5 -5
  185. package/esm/rans/index.js.map +1 -1
  186. package/esm/seek-bzip/bitreader.d.ts +27 -0
  187. package/esm/seek-bzip/bitreader.js +98 -0
  188. package/esm/seek-bzip/bitreader.js.map +1 -0
  189. package/esm/seek-bzip/crc32.d.ts +0 -0
  190. package/esm/seek-bzip/crc32.js +113 -0
  191. package/esm/seek-bzip/crc32.js.map +1 -0
  192. package/esm/seek-bzip/index.d.ts +9 -0
  193. package/esm/seek-bzip/index.js +579 -0
  194. package/esm/seek-bzip/index.js.map +1 -0
  195. package/esm/seek-bzip/stream.d.ts +1 -0
  196. package/esm/seek-bzip/stream.js +41 -0
  197. package/esm/seek-bzip/stream.js.map +1 -0
  198. package/esm/seek-bzip/toHex.d.ts +1 -0
  199. package/esm/seek-bzip/toHex.js +33 -0
  200. package/esm/seek-bzip/toHex.js.map +1 -0
  201. package/package.json +15 -10
  202. package/src/craiIndex.ts +4 -4
  203. package/src/cramFile/codecs/_base.ts +3 -3
  204. package/src/cramFile/codecs/beta.ts +8 -8
  205. package/src/cramFile/codecs/byteArrayLength.ts +6 -6
  206. package/src/cramFile/codecs/byteArrayStop.ts +8 -8
  207. package/src/cramFile/codecs/dataSeriesTypes.ts +1 -1
  208. package/src/cramFile/codecs/external.ts +15 -12
  209. package/src/cramFile/codecs/gamma.ts +8 -8
  210. package/src/cramFile/codecs/huffman.ts +8 -8
  211. package/src/cramFile/codecs/index.ts +11 -11
  212. package/src/cramFile/codecs/subexp.ts +8 -8
  213. package/src/cramFile/container/compressionScheme.ts +6 -6
  214. package/src/cramFile/container/index.ts +6 -7
  215. package/src/cramFile/file.ts +13 -24
  216. package/src/cramFile/index.ts +2 -2
  217. package/src/cramFile/record.ts +3 -3
  218. package/src/cramFile/sectionParsers.ts +4 -4
  219. package/src/cramFile/slice/decodeRecord.ts +8 -8
  220. package/src/cramFile/slice/index.ts +23 -29
  221. package/src/htscodecs/arith_gen.ts +6 -6
  222. package/src/htscodecs/fqzcomp.ts +3 -3
  223. package/src/htscodecs/index.ts +5 -5
  224. package/src/htscodecs/rans.ts +1 -1
  225. package/src/htscodecs/rans4x16.ts +1 -1
  226. package/src/htscodecs/tok3.ts +4 -4
  227. package/src/index.ts +3 -3
  228. package/src/indexedCramFile.ts +5 -5
  229. package/src/rans/d04.ts +3 -3
  230. package/src/rans/d14.ts +2 -2
  231. package/src/rans/decoding.ts +2 -2
  232. package/src/rans/frequencies.ts +3 -3
  233. package/src/rans/index.ts +5 -5
  234. package/src/seek-bzip/README.md +6 -0
  235. package/src/seek-bzip/bitreader.ts +113 -0
  236. package/src/seek-bzip/crc32.ts +116 -0
  237. package/src/seek-bzip/index.ts +615 -0
  238. package/src/seek-bzip/stream.ts +42 -0
  239. package/src/seek-bzip/toHex.ts +33 -0
  240. package/dist/cram-bundle.js.LICENSE.txt +0 -12
  241. package/dist/io/index.js.map +0 -1
  242. package/esm/io/index.js.map +0 -1
  243. package/src/cramFile/declare.d.ts +0 -1
  244. /package/dist/{io/index.d.ts → io.d.ts} +0 -0
  245. /package/esm/{io/index.d.ts → io.d.ts} +0 -0
  246. /package/src/{io/index.ts → io.ts} +0 -0
@@ -0,0 +1,615 @@
1
+ // @ts-nocheck
2
+ /*
3
+ seek-bzip - a pure-javascript module for seeking within bzip2 data
4
+
5
+ Copyright (C) 2013 C. Scott Ananian
6
+ Copyright (C) 2012 Eli Skeggs
7
+ Copyright (C) 2011 Kevin Kwok
8
+
9
+ Permission is hereby granted, free of charge, to any person obtaining
10
+ a copy of this software and associated documentation files (the
11
+ "Software"), to deal in the Software without restriction, including
12
+ without limitation the rights to use, copy, modify, merge, publish,
13
+ distribute, sublicense, and/or sell copies of the Software, and to
14
+ permit persons to whom the Software is furnished to do so, subject to
15
+ the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be
18
+ included in all copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
+
28
+ Adapted from node-bzip, copyright 2012 Eli Skeggs.
29
+ Adapted from bzip2.js, copyright 2011 Kevin Kwok (antimatter15@gmail.com).
30
+
31
+ Based on micro-bunzip by Rob Landley (rob@landley.net).
32
+
33
+ Based on bzip2 decompression code by Julian R Seward (jseward@acm.org),
34
+ which also acknowledges contributions by Mike Burrows, David Wheeler,
35
+ Peter Fenwick, Alistair Moffat, Radford Neal, Ian H. Witten,
36
+ Robert Sedgewick, and Jon L. Bentley.
37
+ */
38
+
39
+ import BitReader from './bitreader'
40
+ import CRC32 from './crc32'
41
+ import Stream from './stream'
42
+
43
+ const MAX_HUFCODE_BITS = 20
44
+ const MAX_SYMBOLS = 258
45
+ const SYMBOL_RUNA = 0
46
+ const SYMBOL_RUNB = 1
47
+ const MIN_GROUPS = 2
48
+ const MAX_GROUPS = 6
49
+ const GROUP_SIZE = 50
50
+
51
+ const WHOLEPI = '314159265359'
52
+ const SQRTPI = '177245385090'
53
+
54
+ const mtf = function (array, index) {
55
+ const src = array[index]
56
+ for (let i = index; i > 0; i--) {
57
+ array[i] = array[i - 1]
58
+ }
59
+ array[0] = src
60
+ return src
61
+ }
62
+
63
+ const Err = {
64
+ OK: 0,
65
+ LAST_BLOCK: -1,
66
+ NOT_BZIP_DATA: -2,
67
+ UNEXPECTED_INPUT_EOF: -3,
68
+ UNEXPECTED_OUTPUT_EOF: -4,
69
+ DATA_ERROR: -5,
70
+ OUT_OF_MEMORY: -6,
71
+ OBSOLETE_INPUT: -7,
72
+ END_OF_BLOCK: -8,
73
+ }
74
+ const ErrorMessages = {}
75
+ ErrorMessages[Err.LAST_BLOCK] = 'Bad file checksum'
76
+ ErrorMessages[Err.NOT_BZIP_DATA] = 'Not bzip data'
77
+ ErrorMessages[Err.UNEXPECTED_INPUT_EOF] = 'Unexpected input EOF'
78
+ ErrorMessages[Err.UNEXPECTED_OUTPUT_EOF] = 'Unexpected output EOF'
79
+ ErrorMessages[Err.DATA_ERROR] = 'Data error'
80
+ ErrorMessages[Err.OUT_OF_MEMORY] = 'Out of memory'
81
+ ErrorMessages[Err.OBSOLETE_INPUT] =
82
+ 'Obsolete (pre 0.9.5) bzip format not supported.'
83
+
84
+ const _throw = function (status, optDetail) {
85
+ let msg = ErrorMessages[status] || 'unknown error'
86
+ if (optDetail) {
87
+ msg += ': ' + optDetail
88
+ }
89
+ const e = new TypeError(msg)
90
+ e.errorCode = status
91
+ throw e
92
+ }
93
+
94
+ class Bunzip {
95
+ constructor(inputStream, outputStream) {
96
+ this.writePos = this.writeCurrent = this.writeCount = 0
97
+
98
+ this._start_bunzip(inputStream, outputStream)
99
+ }
100
+ _init_block() {
101
+ const moreBlocks = this._get_next_block()
102
+ if (!moreBlocks) {
103
+ this.writeCount = -1
104
+ return false /* no more blocks */
105
+ }
106
+ this.blockCRC = new CRC32()
107
+ return true
108
+ }
109
+ /* XXX micro-bunzip uses (inputStream, inputBuffer, len) as arguments */
110
+ _start_bunzip(inputStream, outputStream) {
111
+ /* Ensure that file starts with "BZh['1'-'9']." */
112
+ const buf = new Uint8Array(4)
113
+ if (
114
+ inputStream.read(buf, 0, 4) !== 4 ||
115
+ String.fromCharCode(buf[0], buf[1], buf[2]) !== 'BZh'
116
+ ) {
117
+ _throw(Err.NOT_BZIP_DATA, 'bad magic')
118
+ }
119
+
120
+ const level = buf[3] - 0x30
121
+ if (level < 1 || level > 9) {
122
+ _throw(Err.NOT_BZIP_DATA, 'level out of range')
123
+ }
124
+
125
+ this.reader = new BitReader(inputStream)
126
+
127
+ /* Fourth byte (ascii '1'-'9'), indicates block size in units of 100k of
128
+ uncompressed data. Allocate intermediate buffer for block. */
129
+ this.dbufSize = 100000 * level
130
+ this.nextoutput = 0
131
+ this.outputStream = outputStream
132
+ this.streamCRC = 0
133
+ }
134
+ _get_next_block() {
135
+ let i, j, k
136
+ const reader = this.reader
137
+ // this is get_next_block() function from micro-bunzip:
138
+ /* Read in header signature and CRC, then validate signature.
139
+ (last block signature means CRC is for whole file, return now) */
140
+ const h = reader.pi()
141
+ if (h === SQRTPI) {
142
+ // last block
143
+ return false /* no more blocks */
144
+ }
145
+ if (h !== WHOLEPI) {
146
+ _throw(Err.NOT_BZIP_DATA)
147
+ }
148
+ this.targetBlockCRC = reader.read(32) >>> 0 // (convert to unsigned)
149
+ this.streamCRC =
150
+ (this.targetBlockCRC ^
151
+ ((this.streamCRC << 1) | (this.streamCRC >>> 31))) >>>
152
+ 0
153
+ /* We can add support for blockRandomised if anybody complains. There was
154
+ some code for this in busybox 1.0.0-pre3, but nobody ever noticed that
155
+ it didn't actually work. */
156
+ if (reader.read(1)) {
157
+ _throw(Err.OBSOLETE_INPUT)
158
+ }
159
+ const origPointer = reader.read(24)
160
+ if (origPointer > this.dbufSize) {
161
+ _throw(Err.DATA_ERROR, 'initial position out of bounds')
162
+ }
163
+ /* mapping table: if some byte values are never used (encoding things
164
+ like ascii text), the compression code removes the gaps to have fewer
165
+ symbols to deal with, and writes a sparse bitfield indicating which
166
+ values were present. We make a translation table to convert the symbols
167
+ back to the corresponding bytes. */
168
+ let t = reader.read(16)
169
+ let symToByte = new Uint8Array(256),
170
+ symTotal = 0
171
+ for (i = 0; i < 16; i++) {
172
+ if (t & (1 << (0xf - i))) {
173
+ const o = i * 16
174
+ k = reader.read(16)
175
+ for (j = 0; j < 16; j++) {
176
+ if (k & (1 << (0xf - j))) {
177
+ symToByte[symTotal++] = o + j
178
+ }
179
+ }
180
+ }
181
+ }
182
+
183
+ /* How many different huffman coding groups does this block use? */
184
+ const groupCount = reader.read(3)
185
+ if (groupCount < MIN_GROUPS || groupCount > MAX_GROUPS) {
186
+ _throw(Err.DATA_ERROR)
187
+ }
188
+ /* nSelectors: Every GROUP_SIZE many symbols we select a new huffman coding
189
+ group. Read in the group selector list, which is stored as MTF encoded
190
+ bit runs. (MTF=Move To Front, as each value is used it's moved to the
191
+ start of the list.) */
192
+ const nSelectors = reader.read(15)
193
+ if (nSelectors === 0) {
194
+ _throw(Err.DATA_ERROR)
195
+ }
196
+
197
+ const mtfSymbol = new Uint8Array(256)
198
+ for (i = 0; i < groupCount; i++) {
199
+ mtfSymbol[i] = i
200
+ }
201
+
202
+ const selectors = new Uint8Array(nSelectors) // was 32768...
203
+
204
+ for (i = 0; i < nSelectors; i++) {
205
+ /* Get next value */
206
+ for (j = 0; reader.read(1); j++) {
207
+ if (j >= groupCount) {
208
+ _throw(Err.DATA_ERROR)
209
+ }
210
+ }
211
+ /* Decode MTF to get the next selector */
212
+ selectors[i] = mtf(mtfSymbol, j)
213
+ }
214
+
215
+ /* Read the huffman coding tables for each group, which code for symTotal
216
+ literal symbols, plus two run symbols (RUNA, RUNB) */
217
+ let symCount = symTotal + 2
218
+ let groups = [],
219
+ hufGroup
220
+ for (j = 0; j < groupCount; j++) {
221
+ const length = new Uint8Array(symCount),
222
+ temp = new Uint16Array(MAX_HUFCODE_BITS + 1)
223
+ /* Read huffman code lengths for each symbol. They're stored in
224
+ a way similar to mtf; record a starting value for the first symbol,
225
+ and an offset from the previous value for everys symbol after that. */
226
+ t = reader.read(5) // lengths
227
+ for (i = 0; i < symCount; i++) {
228
+ for (;;) {
229
+ if (t < 1 || t > MAX_HUFCODE_BITS) {
230
+ _throw(Err.DATA_ERROR)
231
+ }
232
+ /* If first bit is 0, stop. Else second bit indicates whether
233
+ to increment or decrement the value. */
234
+ if (!reader.read(1)) {
235
+ break
236
+ }
237
+ if (!reader.read(1)) {
238
+ t++
239
+ } else {
240
+ t--
241
+ }
242
+ }
243
+ length[i] = t
244
+ }
245
+
246
+ /* Find largest and smallest lengths in this group */
247
+ var minLen, maxLen
248
+ minLen = maxLen = length[0]
249
+ for (i = 1; i < symCount; i++) {
250
+ if (length[i] > maxLen) {
251
+ maxLen = length[i]
252
+ } else if (length[i] < minLen) {
253
+ minLen = length[i]
254
+ }
255
+ }
256
+
257
+ /* Calculate permute[], base[], and limit[] tables from length[].
258
+ *
259
+ * permute[] is the lookup table for converting huffman coded symbols
260
+ * into decoded symbols. base[] is the amount to subtract from the
261
+ * value of a huffman symbol of a given length when using permute[].
262
+ *
263
+ * limit[] indicates the largest numerical value a symbol with a given
264
+ * number of bits can have. This is how the huffman codes can vary in
265
+ * length: each code with a value>limit[length] needs another bit.
266
+ */
267
+ hufGroup = {}
268
+ groups.push(hufGroup)
269
+ hufGroup.permute = new Uint16Array(MAX_SYMBOLS)
270
+ hufGroup.limit = new Uint32Array(MAX_HUFCODE_BITS + 2)
271
+ hufGroup.base = new Uint32Array(MAX_HUFCODE_BITS + 1)
272
+ hufGroup.minLen = minLen
273
+ hufGroup.maxLen = maxLen
274
+ /* Calculate permute[]. Concurently, initialize temp[] and limit[]. */
275
+ let pp = 0
276
+ for (i = minLen; i <= maxLen; i++) {
277
+ temp[i] = hufGroup.limit[i] = 0
278
+ for (t = 0; t < symCount; t++) {
279
+ if (length[t] === i) {
280
+ hufGroup.permute[pp++] = t
281
+ }
282
+ }
283
+ }
284
+ /* Count symbols coded for at each bit length */
285
+ for (i = 0; i < symCount; i++) {
286
+ temp[length[i]]++
287
+ }
288
+ /* Calculate limit[] (the largest symbol-coding value at each bit
289
+ * length, which is (previous limit<<1)+symbols at this level), and
290
+ * base[] (number of symbols to ignore at each bit length, which is
291
+ * limit minus the cumulative count of symbols coded for already). */
292
+ pp = t = 0
293
+ for (i = minLen; i < maxLen; i++) {
294
+ pp += temp[i]
295
+ /* We read the largest possible symbol size and then unget bits
296
+ after determining how many we need, and those extra bits could
297
+ be set to anything. (They're noise from future symbols.) At
298
+ each level we're really only interested in the first few bits,
299
+ so here we set all the trailing to-be-ignored bits to 1 so they
300
+ don't affect the value>limit[length] comparison. */
301
+ hufGroup.limit[i] = pp - 1
302
+ pp <<= 1
303
+ t += temp[i]
304
+ hufGroup.base[i + 1] = pp - t
305
+ }
306
+ hufGroup.limit[maxLen + 1] =
307
+ Number.MAX_VALUE /* Sentinal value for reading next sym. */
308
+ hufGroup.limit[maxLen] = pp + temp[maxLen] - 1
309
+ hufGroup.base[minLen] = 0
310
+ }
311
+ /* We've finished reading and digesting the block header. Now read this
312
+ block's huffman coded symbols from the file and undo the huffman coding
313
+ and run length encoding, saving the result into dbuf[dbufCount++]=uc */
314
+
315
+ /* Initialize symbol occurrence counters and symbol Move To Front table */
316
+ const byteCount = new Uint32Array(256)
317
+ for (i = 0; i < 256; i++) {
318
+ mtfSymbol[i] = i
319
+ }
320
+ /* Loop through compressed symbols. */
321
+ let runPos = 0,
322
+ dbufCount = 0,
323
+ selector = 0,
324
+ uc
325
+ const dbuf = (this.dbuf = new Uint32Array(this.dbufSize))
326
+ symCount = 0
327
+ for (;;) {
328
+ /* Determine which huffman coding group to use. */
329
+ if (!symCount--) {
330
+ symCount = GROUP_SIZE - 1
331
+ if (selector >= nSelectors) {
332
+ _throw(Err.DATA_ERROR)
333
+ }
334
+ hufGroup = groups[selectors[selector++]]
335
+ }
336
+ /* Read next huffman-coded symbol. */
337
+ i = hufGroup.minLen
338
+ j = reader.read(i)
339
+ for (; ; i++) {
340
+ if (i > hufGroup.maxLen) {
341
+ _throw(Err.DATA_ERROR)
342
+ }
343
+ if (j <= hufGroup.limit[i]) {
344
+ break
345
+ }
346
+ j = (j << 1) | reader.read(1)
347
+ }
348
+ /* Huffman decode value to get nextSym (with bounds checking) */
349
+ j -= hufGroup.base[i]
350
+ if (j < 0 || j >= MAX_SYMBOLS) {
351
+ _throw(Err.DATA_ERROR)
352
+ }
353
+ const nextSym = hufGroup.permute[j]
354
+ /* We have now decoded the symbol, which indicates either a new literal
355
+ byte, or a repeated run of the most recent literal byte. First,
356
+ check if nextSym indicates a repeated run, and if so loop collecting
357
+ how many times to repeat the last literal. */
358
+ if (nextSym === SYMBOL_RUNA || nextSym === SYMBOL_RUNB) {
359
+ /* If this is the start of a new run, zero out counter */
360
+ if (!runPos) {
361
+ runPos = 1
362
+ t = 0
363
+ }
364
+ /* Neat trick that saves 1 symbol: instead of or-ing 0 or 1 at
365
+ each bit position, add 1 or 2 instead. For example,
366
+ 1011 is 1<<0 + 1<<1 + 2<<2. 1010 is 2<<0 + 2<<1 + 1<<2.
367
+ You can make any bit pattern that way using 1 less symbol than
368
+ the basic or 0/1 method (except all bits 0, which would use no
369
+ symbols, but a run of length 0 doesn't mean anything in this
370
+ context). Thus space is saved. */
371
+ t += nextSym === SYMBOL_RUNA ? runPos : 2 * runPos
372
+ runPos <<= 1
373
+ continue
374
+ }
375
+ /* When we hit the first non-run symbol after a run, we now know
376
+ how many times to repeat the last literal, so append that many
377
+ copies to our buffer of decoded symbols (dbuf) now. (The last
378
+ literal used is the one at the head of the mtfSymbol array.) */
379
+ if (runPos) {
380
+ runPos = 0
381
+ if (dbufCount + t > this.dbufSize) {
382
+ _throw(Err.DATA_ERROR)
383
+ }
384
+ uc = symToByte[mtfSymbol[0]]
385
+ byteCount[uc] += t
386
+ while (t--) {
387
+ dbuf[dbufCount++] = uc
388
+ }
389
+ }
390
+ /* Is this the terminating symbol? */
391
+ if (nextSym > symTotal) {
392
+ break
393
+ }
394
+ /* At this point, nextSym indicates a new literal character. Subtract
395
+ one to get the position in the MTF array at which this literal is
396
+ currently to be found. (Note that the result can't be -1 or 0,
397
+ because 0 and 1 are RUNA and RUNB. But another instance of the
398
+ first symbol in the mtf array, position 0, would have been handled
399
+ as part of a run above. Therefore 1 unused mtf position minus
400
+ 2 non-literal nextSym values equals -1.) */
401
+ if (dbufCount >= this.dbufSize) {
402
+ _throw(Err.DATA_ERROR)
403
+ }
404
+ i = nextSym - 1
405
+ uc = mtf(mtfSymbol, i)
406
+ uc = symToByte[uc]
407
+ /* We have our literal byte. Save it into dbuf. */
408
+ byteCount[uc]++
409
+ dbuf[dbufCount++] = uc
410
+ }
411
+ /* At this point, we've read all the huffman-coded symbols (and repeated
412
+ runs) for this block from the input stream, and decoded them into the
413
+ intermediate buffer. There are dbufCount many decoded bytes in dbuf[].
414
+ Now undo the Burrows-Wheeler transform on dbuf.
415
+ See http://dogma.net/markn/articles/bwt/bwt.htm
416
+ */
417
+ if (origPointer < 0 || origPointer >= dbufCount) {
418
+ _throw(Err.DATA_ERROR)
419
+ }
420
+ /* Turn byteCount into cumulative occurrence counts of 0 to n-1. */
421
+ j = 0
422
+ for (i = 0; i < 256; i++) {
423
+ k = j + byteCount[i]
424
+ byteCount[i] = j
425
+ j = k
426
+ }
427
+ /* Figure out what order dbuf would be in if we sorted it. */
428
+ for (i = 0; i < dbufCount; i++) {
429
+ uc = dbuf[i] & 0xff
430
+ dbuf[byteCount[uc]] |= i << 8
431
+ byteCount[uc]++
432
+ }
433
+ /* Decode first byte by hand to initialize "previous" byte. Note that it
434
+ doesn't get output, and if the first three characters are identical
435
+ it doesn't qualify as a run (hence writeRunCountdown=5). */
436
+ let pos = 0,
437
+ current = 0,
438
+ run = 0
439
+ if (dbufCount) {
440
+ pos = dbuf[origPointer]
441
+ current = pos & 0xff
442
+ pos >>= 8
443
+ run = -1
444
+ }
445
+ this.writePos = pos
446
+ this.writeCurrent = current
447
+ this.writeCount = dbufCount
448
+ this.writeRun = run
449
+
450
+ return true /* more blocks to come */
451
+ }
452
+ /* Undo burrows-wheeler transform on intermediate buffer to produce output.
453
+ If start_bunzip was initialized with out_fd=-1, then up to len bytes of
454
+ data are written to outbuf. Return value is number of bytes written or
455
+ error (all errors are negative numbers). If out_fd!=-1, outbuf and len
456
+ are ignored, data is written to out_fd and return is RETVAL_OK or error.
457
+ */
458
+ _read_bunzip(outputBuffer, len) {
459
+ let copies, previous, outbyte
460
+ /* james@jamestaylor.org: writeCount goes to -1 when the buffer is fully
461
+ decoded, which results in this returning RETVAL_LAST_BLOCK, also
462
+ equal to -1... Confusing, I'm returning 0 here to indicate no
463
+ bytes written into the buffer */
464
+ if (this.writeCount < 0) {
465
+ return 0
466
+ }
467
+
468
+ const gotcount = 0
469
+ let dbuf = this.dbuf,
470
+ pos = this.writePos,
471
+ current = this.writeCurrent
472
+ let dbufCount = this.writeCount,
473
+ outputsize = this.outputsize
474
+ let run = this.writeRun
475
+
476
+ while (dbufCount) {
477
+ dbufCount--
478
+ previous = current
479
+ pos = dbuf[pos]
480
+ current = pos & 0xff
481
+ pos >>= 8
482
+ if (run++ === 3) {
483
+ copies = current
484
+ outbyte = previous
485
+ current = -1
486
+ } else {
487
+ copies = 1
488
+ outbyte = current
489
+ }
490
+ this.blockCRC.updateCRCRun(outbyte, copies)
491
+ while (copies--) {
492
+ this.outputStream.writeByte(outbyte)
493
+ this.nextoutput++
494
+ }
495
+ if (current != previous) {
496
+ run = 0
497
+ }
498
+ }
499
+ this.writeCount = dbufCount
500
+ // check CRC
501
+ if (this.blockCRC.getCRC() !== this.targetBlockCRC) {
502
+ _throw(
503
+ Err.DATA_ERROR,
504
+ 'Bad block CRC ' +
505
+ '(got ' +
506
+ this.blockCRC.getCRC().toString(16) +
507
+ ' expected ' +
508
+ this.targetBlockCRC.toString(16) +
509
+ ')',
510
+ )
511
+ }
512
+ return this.nextoutput
513
+ }
514
+ }
515
+
516
+ const coerceInputStream = function (input) {
517
+ if ('readByte' in input) {
518
+ return input
519
+ }
520
+ const inputStream = new Stream()
521
+ inputStream.pos = 0
522
+ inputStream.readByte = function () {
523
+ return input[this.pos++]
524
+ }
525
+ inputStream.seek = function (pos) {
526
+ this.pos = pos
527
+ }
528
+ inputStream.eof = function () {
529
+ return this.pos >= input.length
530
+ }
531
+ return inputStream
532
+ }
533
+ const coerceOutputStream = function (output) {
534
+ const outputStream = new Stream()
535
+ let resizeOk = true
536
+ if (output) {
537
+ if (typeof output === 'number') {
538
+ outputStream.buffer = new Uint8Array(output)
539
+ resizeOk = false
540
+ } else if ('writeByte' in output) {
541
+ return output
542
+ } else {
543
+ outputStream.buffer = output
544
+ resizeOk = false
545
+ }
546
+ } else {
547
+ outputStream.buffer = new Uint8Array(16384)
548
+ }
549
+ outputStream.pos = 0
550
+ outputStream.writeByte = function (_byte) {
551
+ if (resizeOk && this.pos >= this.buffer.length) {
552
+ const newBuffer = new Uint8Array(this.buffer.length * 2)
553
+ newBuffer.set(this.buffer)
554
+ this.buffer = newBuffer
555
+ }
556
+ this.buffer[this.pos++] = _byte
557
+ }
558
+ outputStream.getBuffer = function () {
559
+ // trim buffer
560
+ if (this.pos !== this.buffer.length) {
561
+ if (!resizeOk) {
562
+ throw new TypeError('outputsize does not match decoded input')
563
+ }
564
+ const newBuffer = new Uint8Array(this.pos)
565
+ newBuffer.set(this.buffer.slice(0, this.pos))
566
+ this.buffer = newBuffer
567
+ }
568
+ return this.buffer
569
+ }
570
+ outputStream._coerced = true
571
+ return outputStream
572
+ }
573
+
574
+ /* Static helper functions */
575
+ // 'input' can be a stream or a buffer
576
+ // 'output' can be a stream or a buffer or a number (buffer size)
577
+ export function decode(input: Uint8Array, output?: any, multistream?: any) {
578
+ // make a stream from a buffer, if necessary
579
+ const inputStream = coerceInputStream(input)
580
+ const outputStream = coerceOutputStream(output)
581
+
582
+ const bz = new Bunzip(inputStream, outputStream)
583
+ while (true) {
584
+ if ('eof' in inputStream && inputStream.eof()) {
585
+ break
586
+ }
587
+ if (bz._init_block()) {
588
+ bz._read_bunzip()
589
+ } else {
590
+ const targetStreamCRC = bz.reader.read(32) >>> 0 // (convert to unsigned)
591
+ if (targetStreamCRC !== bz.streamCRC) {
592
+ _throw(
593
+ Err.DATA_ERROR,
594
+ 'Bad stream CRC ' +
595
+ '(got ' +
596
+ bz.streamCRC.toString(16) +
597
+ ' expected ' +
598
+ targetStreamCRC.toString(16) +
599
+ ')',
600
+ )
601
+ }
602
+ if (multistream && 'eof' in inputStream && !inputStream.eof()) {
603
+ // note that start_bunzip will also resync the bit reader to next byte
604
+ bz._start_bunzip(inputStream, outputStream)
605
+ } else {
606
+ break
607
+ }
608
+ }
609
+ }
610
+ if ('getBuffer' in outputStream) {
611
+ return outputStream.getBuffer()
612
+ }
613
+ }
614
+
615
+ export default Bunzip
@@ -0,0 +1,42 @@
1
+ // @ts-nocheck
2
+ /// * very simple input/output stream interface */
3
+ const Stream = function () {}
4
+
5
+ // input streams //////////////
6
+ /** Returns the next byte, or -1 for EOF. */
7
+ Stream.prototype.readByte = function () {
8
+ throw new Error('abstract method readByte() not implemented')
9
+ }
10
+ /** Attempts to fill the buffer; returns number of bytes read, or
11
+ * -1 for EOF. */
12
+ Stream.prototype.read = function (buffer, bufOffset, length) {
13
+ let bytesRead = 0
14
+ while (bytesRead < length) {
15
+ const c = this.readByte()
16
+ if (c < 0) {
17
+ // EOF
18
+ return bytesRead === 0 ? -1 : bytesRead
19
+ }
20
+ buffer[bufOffset++] = c
21
+ bytesRead++
22
+ }
23
+ return bytesRead
24
+ }
25
+ Stream.prototype.seek = function (new_pos) {
26
+ throw new Error('abstract method seek() not implemented')
27
+ }
28
+
29
+ // output streams ///////////
30
+ Stream.prototype.writeByte = function (_byte) {
31
+ throw new Error('abstract method readByte() not implemented')
32
+ }
33
+ Stream.prototype.write = function (buffer, bufOffset, length) {
34
+ let i
35
+ for (i = 0; i < length; i++) {
36
+ this.writeByte(buffer[bufOffset++])
37
+ }
38
+ return length
39
+ }
40
+ Stream.prototype.flush = function () {}
41
+
42
+ module.exports = Stream
@@ -0,0 +1,33 @@
1
+ // from https://www.xaymar.com/articles/2020/12/08/fastest-uint8array-to-hex-string-conversion-in-javascript/
2
+
3
+ // Pre-Init
4
+ const LUT_HEX_4b = [
5
+ '0',
6
+ '1',
7
+ '2',
8
+ '3',
9
+ '4',
10
+ '5',
11
+ '6',
12
+ '7',
13
+ '8',
14
+ '9',
15
+ 'A',
16
+ 'B',
17
+ 'C',
18
+ 'D',
19
+ 'E',
20
+ 'F',
21
+ ]
22
+ const LUT_HEX_8b = new Array(0x100)
23
+ for (let n = 0; n < 0x100; n++) {
24
+ LUT_HEX_8b[n] = `${LUT_HEX_4b[(n >>> 4) & 0xf]}${LUT_HEX_4b[n & 0xf]}`
25
+ }
26
+ // End Pre-Init
27
+ export function toHex(buffer: Uint8Array) {
28
+ let out = ''
29
+ for (let idx = 0, edx = buffer.length; idx < edx; idx++) {
30
+ out += LUT_HEX_8b[buffer[idx]!]
31
+ }
32
+ return out
33
+ }
@@ -1,12 +0,0 @@
1
- /*!
2
- * Based on xzwasm (c) Steve Sanderson. License: MIT - https://github.com/SteveSanderson/xzwasm
3
- * Contains xz-embedded by Lasse Collin and Igor Pavlov. License: Public domain - https://tukaani.org/xz/embedded.html
4
- * and walloc (c) 2020 Igalia, S.L. License: MIT - https://github.com/wingo/walloc
5
- */
6
-
7
- /*!
8
- * Determine if an object is a Buffer
9
- *
10
- * @author Feross Aboukhadijeh <https://feross.org>
11
- * @license MIT
12
- */
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/io/index.ts"],"names":[],"mappings":";;;AAqBS,oBAAI;AArBb,6DAA2D;AAI3D,SAAS,IAAI,CACX,QAAiB,EACjB,SAAkB,EAClB,eAAmC;IAEnC,IAAI,eAAe,EAAE,CAAC;QACpB,OAAO,eAAe,CAAA;IACxB,CAAC;IACD,IAAI,QAAQ,EAAE,CAAC;QACb,OAAO,IAAI,gCAAU,CAAC,QAAQ,CAAC,CAAA;IACjC,CAAC;IACD,IAAI,SAAS,EAAE,CAAC;QACd,OAAO,IAAI,+BAAS,CAAC,SAAS,CAAC,CAAA;IACjC,CAAC;IACD,MAAM,IAAI,KAAK,CAAC,mDAAmD,CAAC,CAAA;AACtE,CAAC;AAID,2DAA2D;AAAlD,gHAAA,SAAS,OAAA;AAAE,iHAAA,UAAU,OAAA"}