@gmod/cram 4.0.3 → 4.0.4
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.
- package/dist/cram-bundle.js +1 -1
- package/dist/cramFile/file.js +43 -9
- package/dist/cramFile/file.js.map +1 -1
- package/dist/cramFile/slice/decodeRecord.js +3 -3
- package/dist/cramFile/slice/decodeRecord.js.map +1 -1
- package/dist/cramFile/util.d.ts +0 -2
- package/dist/cramFile/util.js +3 -31
- package/dist/cramFile/util.js.map +1 -1
- package/dist/htscodecs/arith_gen.d.ts +5 -7
- package/dist/htscodecs/arith_gen.js +122 -105
- package/dist/htscodecs/arith_gen.js.map +1 -1
- package/dist/htscodecs/arith_sh.d.ts +1 -8
- package/dist/htscodecs/arith_sh.js +16 -10
- package/dist/htscodecs/arith_sh.js.map +1 -1
- package/dist/htscodecs/byte_model.d.ts +1 -6
- package/dist/htscodecs/byte_model.js +25 -17
- package/dist/htscodecs/byte_model.js.map +1 -1
- package/dist/htscodecs/fqzcomp.d.ts +1 -1
- package/dist/htscodecs/fqzcomp.js +98 -77
- package/dist/htscodecs/fqzcomp.js.map +1 -1
- package/dist/htscodecs/index.d.ts +5 -5
- package/dist/htscodecs/index.js +53 -16
- package/dist/htscodecs/index.js.map +1 -1
- package/dist/htscodecs/iostream.d.ts +9 -20
- package/dist/htscodecs/iostream.js +21 -116
- package/dist/htscodecs/iostream.js.map +1 -1
- package/dist/htscodecs/rans.d.ts +1 -1
- package/dist/htscodecs/rans.js +65 -54
- package/dist/htscodecs/rans.js.map +1 -1
- package/dist/htscodecs/rans4x16.d.ts +1 -1
- package/dist/htscodecs/rans4x16.js +151 -111
- package/dist/htscodecs/rans4x16.js.map +1 -1
- package/dist/htscodecs/tok3.d.ts +1 -2
- package/dist/htscodecs/tok3.js +82 -239
- package/dist/htscodecs/tok3.js.map +1 -1
- package/dist/util.d.ts +1 -0
- package/dist/util.js +20 -0
- package/dist/util.js.map +1 -0
- package/esm/cramFile/file.js +3 -2
- package/esm/cramFile/file.js.map +1 -1
- package/esm/cramFile/slice/decodeRecord.js +3 -3
- package/esm/cramFile/slice/decodeRecord.js.map +1 -1
- package/esm/cramFile/util.d.ts +0 -2
- package/esm/cramFile/util.js +3 -29
- package/esm/cramFile/util.js.map +1 -1
- package/esm/htscodecs/arith_gen.d.ts +5 -7
- package/esm/htscodecs/arith_gen.js +108 -97
- package/esm/htscodecs/arith_gen.js.map +1 -1
- package/esm/htscodecs/arith_sh.d.ts +1 -8
- package/esm/htscodecs/arith_sh.js +14 -11
- package/esm/htscodecs/arith_sh.js.map +1 -1
- package/esm/htscodecs/byte_model.d.ts +1 -6
- package/esm/htscodecs/byte_model.js +23 -18
- package/esm/htscodecs/byte_model.js.map +1 -1
- package/esm/htscodecs/fqzcomp.d.ts +1 -1
- package/esm/htscodecs/fqzcomp.js +91 -76
- package/esm/htscodecs/fqzcomp.js.map +1 -1
- package/esm/htscodecs/index.d.ts +5 -5
- package/esm/htscodecs/index.js +14 -20
- package/esm/htscodecs/index.js.map +1 -1
- package/esm/htscodecs/iostream.d.ts +9 -20
- package/esm/htscodecs/iostream.js +19 -117
- package/esm/htscodecs/iostream.js.map +1 -1
- package/esm/htscodecs/rans.d.ts +1 -1
- package/esm/htscodecs/rans.js +61 -56
- package/esm/htscodecs/rans.js.map +1 -1
- package/esm/htscodecs/rans4x16.d.ts +1 -1
- package/esm/htscodecs/rans4x16.js +143 -109
- package/esm/htscodecs/rans4x16.js.map +1 -1
- package/esm/htscodecs/tok3.d.ts +1 -2
- package/esm/htscodecs/tok3.js +41 -237
- package/esm/htscodecs/tok3.js.map +1 -1
- package/esm/util.d.ts +1 -0
- package/esm/util.js +17 -0
- package/esm/util.js.map +1 -0
- package/package.json +2 -3
- package/src/cramFile/file.ts +3 -2
- package/src/cramFile/slice/decodeRecord.ts +3 -5
- package/src/cramFile/util.ts +6 -39
- package/src/htscodecs/{arith_gen.js → arith_gen.ts} +133 -95
- package/src/htscodecs/{arith_sh.js → arith_sh.ts} +17 -9
- package/src/htscodecs/{byte_model.js → byte_model.ts} +26 -16
- package/src/htscodecs/{fqzcomp.js → fqzcomp.ts} +108 -74
- package/src/htscodecs/{index.js → index.ts} +14 -20
- package/src/htscodecs/iostream.ts +159 -0
- package/src/htscodecs/{rans.js → rans.ts} +73 -56
- package/src/htscodecs/{rans4x16.js → rans4x16.ts} +180 -111
- package/src/htscodecs/tok3.ts +197 -0
- package/src/util.ts +16 -0
- package/src/htscodecs/iostream.js +0 -257
- package/src/htscodecs/tok3.js +0 -413
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/* eslint-disable no-var */
|
|
2
|
+
// @ts-nocheck
|
|
3
|
+
|
|
4
|
+
/*
|
|
5
|
+
* Copyright (c) 2019 Genome Research Ltd.
|
|
6
|
+
* Author(s): James Bonfield
|
|
7
|
+
*
|
|
8
|
+
* Redistribution and use in source and binary forms, with or without
|
|
9
|
+
* modification, are permitted provided that the following conditions are met:
|
|
10
|
+
*
|
|
11
|
+
* 1. Redistributions of source code must retain the above copyright notice,
|
|
12
|
+
* this list of conditions and the following disclaimer.
|
|
13
|
+
*
|
|
14
|
+
* 2. Redistributions in binary form must reproduce the above
|
|
15
|
+
* copyright notice, this list of conditions and the following
|
|
16
|
+
* disclaimer in the documentation and/or other materials provided
|
|
17
|
+
* with the distribution.
|
|
18
|
+
*
|
|
19
|
+
* 3. Neither the names Genome Research Ltd and Wellcome Trust Sanger
|
|
20
|
+
* Institute nor the names of its contributors may be used to endorse
|
|
21
|
+
* or promote products derived from this software without specific
|
|
22
|
+
* prior written permission.
|
|
23
|
+
*
|
|
24
|
+
* THIS SOFTWARE IS PROVIDED BY GENOME RESEARCH LTD AND CONTRIBUTORS "AS
|
|
25
|
+
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
26
|
+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
|
27
|
+
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENOME RESEARCH
|
|
28
|
+
* LTD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
29
|
+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
30
|
+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
31
|
+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
32
|
+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
33
|
+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
34
|
+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
// Name tokeniser
|
|
38
|
+
//
|
|
39
|
+
// This is a reference implementation designed to match the
|
|
40
|
+
// written specification as closely as possible. It is *NOT*
|
|
41
|
+
// an efficient implementation, but see comments below.
|
|
42
|
+
|
|
43
|
+
import { concatUint8Array } from '../util'
|
|
44
|
+
import arith_gen from './arith_gen'
|
|
45
|
+
import IOStream from './iostream'
|
|
46
|
+
import * as rans from './rans4x16'
|
|
47
|
+
|
|
48
|
+
const arith = new arith_gen()
|
|
49
|
+
|
|
50
|
+
const TOK_TYPE = 0
|
|
51
|
+
const TOK_STRING = 1
|
|
52
|
+
const TOK_CHAR = 2
|
|
53
|
+
const TOK_DIGITS0 = 3
|
|
54
|
+
const TOK_DZLEN = 4
|
|
55
|
+
const TOK_DUP = 5
|
|
56
|
+
const TOK_DIFF = 6
|
|
57
|
+
const TOK_DIGITS = 7
|
|
58
|
+
const TOK_DELTA = 8
|
|
59
|
+
const TOK_DELTA0 = 9
|
|
60
|
+
const TOK_MATCH = 10
|
|
61
|
+
const TOK_NOP = 11
|
|
62
|
+
const TOK_END = 12
|
|
63
|
+
|
|
64
|
+
// ----------------------------------------------------------------------
|
|
65
|
+
// Token byte streams
|
|
66
|
+
function DecodeTokenByteStreams(src, in_size, use_arith, nnames) {
|
|
67
|
+
let t = -1
|
|
68
|
+
|
|
69
|
+
const B = new Array(256)
|
|
70
|
+
|
|
71
|
+
while (!src.EOF()) {
|
|
72
|
+
const ttype = src.ReadByte()
|
|
73
|
+
const tok_new = ttype & 128
|
|
74
|
+
const tok_dup = ttype & 64
|
|
75
|
+
const type = ttype & 63
|
|
76
|
+
|
|
77
|
+
if (tok_new) {
|
|
78
|
+
t++
|
|
79
|
+
B[t] = new Array(13)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (type != TOK_TYPE && tok_new) {
|
|
83
|
+
const M = new Array(nnames - 1).fill(TOK_MATCH)
|
|
84
|
+
B[t][TOK_TYPE] = new IOStream(concatUint8Array([new Uint8Array(type), M]))
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (tok_dup) {
|
|
88
|
+
const dup_pos = src.ReadByte()
|
|
89
|
+
const dup_type = src.ReadByte()
|
|
90
|
+
B[t][type] = new IOStream(B[dup_pos][dup_type].buf)
|
|
91
|
+
} else {
|
|
92
|
+
const clen = src.ReadUint7()
|
|
93
|
+
const data = src.ReadData(clen)
|
|
94
|
+
|
|
95
|
+
B[t][type] = use_arith ? arith.decode(data) : rans.decode(data)
|
|
96
|
+
B[t][type] = new IOStream(B[t][type])
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return B
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// ----------------------------------------------------------------------
|
|
104
|
+
// Token decode
|
|
105
|
+
function LeftPadNumber(val, len) {
|
|
106
|
+
let str = val + ''
|
|
107
|
+
while (str.length < len) {
|
|
108
|
+
str = '0' + str
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return str
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function DecodeSingleName(B, N, T, n) {
|
|
115
|
+
let type = B[0][TOK_TYPE].ReadByte()
|
|
116
|
+
const dist = B[0][type].ReadUint32()
|
|
117
|
+
const m = n - dist
|
|
118
|
+
|
|
119
|
+
if (type == TOK_DUP) {
|
|
120
|
+
N[n] = N[m]
|
|
121
|
+
T[n] = T[m]
|
|
122
|
+
return N[n]
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
let t = 1
|
|
126
|
+
N[n] = ''
|
|
127
|
+
T[n] = new Array(256)
|
|
128
|
+
do {
|
|
129
|
+
type = B[t][TOK_TYPE].ReadByte()
|
|
130
|
+
|
|
131
|
+
switch (type) {
|
|
132
|
+
case TOK_CHAR:
|
|
133
|
+
T[n][t] = B[t][TOK_CHAR].ReadChar()
|
|
134
|
+
break
|
|
135
|
+
|
|
136
|
+
case TOK_STRING:
|
|
137
|
+
T[n][t] = B[t][TOK_STRING].ReadString()
|
|
138
|
+
break
|
|
139
|
+
|
|
140
|
+
case TOK_DIGITS:
|
|
141
|
+
T[n][t] = B[t][TOK_DIGITS].ReadUint32()
|
|
142
|
+
break
|
|
143
|
+
|
|
144
|
+
case TOK_DIGITS0:
|
|
145
|
+
var d = B[t][TOK_DIGITS0].ReadUint32()
|
|
146
|
+
var l = B[t][TOK_DZLEN].ReadByte()
|
|
147
|
+
T[n][t] = LeftPadNumber(d, l)
|
|
148
|
+
break
|
|
149
|
+
|
|
150
|
+
case TOK_DELTA:
|
|
151
|
+
T[n][t] = (T[m][t] >> 0) + B[t][TOK_DELTA].ReadByte()
|
|
152
|
+
break
|
|
153
|
+
|
|
154
|
+
case TOK_DELTA0:
|
|
155
|
+
var d = (T[m][t] >> 0) + B[t][TOK_DELTA0].ReadByte()
|
|
156
|
+
var l = T[m][t].length
|
|
157
|
+
T[n][t] = LeftPadNumber(d, l)
|
|
158
|
+
break
|
|
159
|
+
|
|
160
|
+
case TOK_MATCH:
|
|
161
|
+
T[n][t] = T[m][t]
|
|
162
|
+
break
|
|
163
|
+
|
|
164
|
+
default:
|
|
165
|
+
T[n][t] = ''
|
|
166
|
+
break
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
N[n] += T[n][t++]
|
|
170
|
+
} while (type != TOK_END)
|
|
171
|
+
|
|
172
|
+
return N[n]
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// ----------------------------------------------------------------------
|
|
176
|
+
// Main tokeniser decode entry function: decodes a compressed src and
|
|
177
|
+
// returns the uncompressed buffer.
|
|
178
|
+
export function decode(src, len, separator) {
|
|
179
|
+
var src = new IOStream(src)
|
|
180
|
+
const ulen = src.ReadUint32()
|
|
181
|
+
const nnames = src.ReadUint32()
|
|
182
|
+
const use_arith = src.ReadByte()
|
|
183
|
+
|
|
184
|
+
const B = DecodeTokenByteStreams(src, len, use_arith, nnames)
|
|
185
|
+
const N = new Array(nnames)
|
|
186
|
+
const T = new Array(nnames)
|
|
187
|
+
|
|
188
|
+
let str = ''
|
|
189
|
+
if (separator === undefined) {
|
|
190
|
+
separator = '\n'
|
|
191
|
+
}
|
|
192
|
+
for (let i = 0; i < nnames; i++) {
|
|
193
|
+
str += DecodeSingleName(B, N, T, i) + separator
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return str
|
|
197
|
+
}
|
package/src/util.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
function sum(array: Uint8Array[]) {
|
|
2
|
+
let sum = 0
|
|
3
|
+
for (const entry of array) {
|
|
4
|
+
sum += entry.length
|
|
5
|
+
}
|
|
6
|
+
return sum
|
|
7
|
+
}
|
|
8
|
+
export function concatUint8Array(args: Uint8Array[]) {
|
|
9
|
+
const mergedArray = new Uint8Array(sum(args))
|
|
10
|
+
let offset = 0
|
|
11
|
+
for (const entry of args) {
|
|
12
|
+
mergedArray.set(entry, offset)
|
|
13
|
+
offset += entry.length
|
|
14
|
+
}
|
|
15
|
+
return mergedArray
|
|
16
|
+
}
|
|
@@ -1,257 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2019 Genome Research Ltd.
|
|
3
|
-
* Author(s): James Bonfield
|
|
4
|
-
*
|
|
5
|
-
* Redistribution and use in source and binary forms, with or without
|
|
6
|
-
* modification, are permitted provided that the following conditions are met:
|
|
7
|
-
*
|
|
8
|
-
* 1. Redistributions of source code must retain the above copyright notice,
|
|
9
|
-
* this list of conditions and the following disclaimer.
|
|
10
|
-
*
|
|
11
|
-
* 2. Redistributions in binary form must reproduce the above
|
|
12
|
-
* copyright notice, this list of conditions and the following
|
|
13
|
-
* disclaimer in the documentation and/or other materials provided
|
|
14
|
-
* with the distribution.
|
|
15
|
-
*
|
|
16
|
-
* 3. Neither the names Genome Research Ltd and Wellcome Trust Sanger
|
|
17
|
-
* Institute nor the names of its contributors may be used to endorse
|
|
18
|
-
* or promote products derived from this software without specific
|
|
19
|
-
* prior written permission.
|
|
20
|
-
*
|
|
21
|
-
* THIS SOFTWARE IS PROVIDED BY GENOME RESEARCH LTD AND CONTRIBUTORS "AS
|
|
22
|
-
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
23
|
-
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
|
24
|
-
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENOME RESEARCH
|
|
25
|
-
* LTD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
26
|
-
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
27
|
-
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
28
|
-
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
29
|
-
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
30
|
-
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
31
|
-
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
32
|
-
*/
|
|
33
|
-
|
|
34
|
-
// Turn a buffer into a fake stream with get / put commands.
|
|
35
|
-
// This enables up to closely match the published pseudocode.
|
|
36
|
-
module.exports = class IOStream {
|
|
37
|
-
constructor(buf, start_pos = 0, size = 0) {
|
|
38
|
-
if (size != 0) {
|
|
39
|
-
this.buf = new Uint8Array(size)
|
|
40
|
-
this.length = size
|
|
41
|
-
} else {
|
|
42
|
-
this.buf = buf
|
|
43
|
-
this.length = buf.length
|
|
44
|
-
}
|
|
45
|
-
this.dataView = new DataView(this.buf.buffer)
|
|
46
|
-
this.pos = start_pos
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// ----------
|
|
50
|
-
// Reading
|
|
51
|
-
EOF() {
|
|
52
|
-
return this.pos >= this.length
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
ReadData(len) {
|
|
56
|
-
var A = this.buf.slice(this.pos, this.pos + len)
|
|
57
|
-
this.pos += len
|
|
58
|
-
return A
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
ReadByte() {
|
|
62
|
-
const b = this.buf[this.pos]
|
|
63
|
-
this.pos++
|
|
64
|
-
return b
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
ReadChar() {
|
|
68
|
-
const b = this.buf[this.pos]
|
|
69
|
-
this.pos++
|
|
70
|
-
return String.fromCharCode(b)
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
ReadUint16() {
|
|
74
|
-
var i = this.ReadByte()
|
|
75
|
-
i |= this.ReadByte() << 8
|
|
76
|
-
return i
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
ReadUint32() {
|
|
80
|
-
const i = this.dataView.getInt32(this.pos, true)
|
|
81
|
-
this.pos += 4
|
|
82
|
-
return i
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// nul terminated string
|
|
86
|
-
ReadString() {
|
|
87
|
-
var s = ''
|
|
88
|
-
do {
|
|
89
|
-
var b = this.buf[this.pos++]
|
|
90
|
-
if (b) s += String.fromCharCode(b)
|
|
91
|
-
} while (b)
|
|
92
|
-
return s
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// ReadUint7() {
|
|
96
|
-
// // Variable sized unsigned integers
|
|
97
|
-
// var i = 0;
|
|
98
|
-
// var s = 0;
|
|
99
|
-
// do {
|
|
100
|
-
// var c = this.ReadByte();
|
|
101
|
-
// i = i | ((c & 0x7f)<<s);
|
|
102
|
-
// s += 7;
|
|
103
|
-
// } while ((c & 0x80))
|
|
104
|
-
//
|
|
105
|
-
// return i;
|
|
106
|
-
// }
|
|
107
|
-
|
|
108
|
-
ReadUint7() {
|
|
109
|
-
// Variable sized unsigned integers
|
|
110
|
-
var i = 0
|
|
111
|
-
do {
|
|
112
|
-
var c = this.ReadByte()
|
|
113
|
-
i = (i << 7) | (c & 0x7f)
|
|
114
|
-
} while (c & 0x80)
|
|
115
|
-
|
|
116
|
-
return i
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
ReadITF8() {
|
|
120
|
-
var i = this.buf[this.pos]
|
|
121
|
-
this.pos++
|
|
122
|
-
|
|
123
|
-
//process.stderr.write("i="+i+"\n");
|
|
124
|
-
|
|
125
|
-
if (i >= 0xf0) {
|
|
126
|
-
// 1111xxxx => +4 bytes
|
|
127
|
-
i = (i & 0x0f) << 28
|
|
128
|
-
i +=
|
|
129
|
-
(this.buf[this.pos + 0] << 20) +
|
|
130
|
-
(this.buf[this.pos + 1] << 12) +
|
|
131
|
-
(this.buf[this.pos + 2] << 4) +
|
|
132
|
-
(this.buf[this.pos + 3] >> 4)
|
|
133
|
-
this.pos += 4
|
|
134
|
-
//process.stderr.write(" 4i="+i+"\n");
|
|
135
|
-
} else if (i >= 0xe0) {
|
|
136
|
-
// 1110xxxx => +3 bytes
|
|
137
|
-
i = (i & 0x0f) << 24
|
|
138
|
-
i +=
|
|
139
|
-
(this.buf[this.pos + 0] << 16) +
|
|
140
|
-
(this.buf[this.pos + 1] << 8) +
|
|
141
|
-
(this.buf[this.pos + 2] << 0)
|
|
142
|
-
this.pos += 3
|
|
143
|
-
//process.stderr.write(" 3i="+i+"\n");
|
|
144
|
-
} else if (i >= 0xc0) {
|
|
145
|
-
// 110xxxxx => +2 bytes
|
|
146
|
-
i = (i & 0x1f) << 16
|
|
147
|
-
i += (this.buf[this.pos + 0] << 8) + (this.buf[this.pos + 1] << 0)
|
|
148
|
-
this.pos += 2
|
|
149
|
-
//process.stderr.write(" 2i="+i+"\n");
|
|
150
|
-
} else if (i >= 0x80) {
|
|
151
|
-
// 10xxxxxx => +1 bytes
|
|
152
|
-
i = (i & 0x3f) << 8
|
|
153
|
-
i += this.buf[this.pos]
|
|
154
|
-
this.pos++
|
|
155
|
-
//process.stderr.write(" 1i="+i+"\n");
|
|
156
|
-
} else {
|
|
157
|
-
// 0xxxxxxx => +0 bytes
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
return i
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
// ----------
|
|
164
|
-
// Writing
|
|
165
|
-
WriteByte(b) {
|
|
166
|
-
this.buf[this.pos++] = b
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
WriteChar(b) {
|
|
170
|
-
this.buf[this.pos++] = b.charCodeAt(0)
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
WriteString(str) {
|
|
174
|
-
for (var i = 0; i < str.length; i++)
|
|
175
|
-
this.buf[this.pos++] = str.charCodeAt(i)
|
|
176
|
-
this.buf[this.pos++] = 0
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
WriteData(buf, len) {
|
|
180
|
-
for (var i = 0; i < len; i++) this.buf[this.pos++] = buf[i]
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
WriteStream(stream) {
|
|
184
|
-
this.WriteData(stream.buf, stream.pos)
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
WriteUint16(u) {
|
|
188
|
-
//this.buf.writeInt16LE(u, this.pos);
|
|
189
|
-
this.WriteByte(u & 0xff)
|
|
190
|
-
this.WriteByte((u >> 8) & 0xff)
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
WriteUint32(u) {
|
|
194
|
-
this.buf.writeInt32LE(u, this.pos)
|
|
195
|
-
this.pos += 4
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
// WriteUint7(i) {
|
|
199
|
-
// do {
|
|
200
|
-
// this.WriteByte((i & 0x7f) | ((i > 0x80) << 7));
|
|
201
|
-
// i >>= 7;
|
|
202
|
-
// } while (i > 0);
|
|
203
|
-
// }
|
|
204
|
-
|
|
205
|
-
WriteUint7(i) {
|
|
206
|
-
var s = 0
|
|
207
|
-
var X = i
|
|
208
|
-
do {
|
|
209
|
-
s += 7
|
|
210
|
-
X >>= 7
|
|
211
|
-
} while (X > 0)
|
|
212
|
-
|
|
213
|
-
do {
|
|
214
|
-
s -= 7
|
|
215
|
-
this.WriteByte(((i >> s) & 0x7f) + ((s > 0) << 7))
|
|
216
|
-
} while (s > 0)
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
WriteITF8(i) {
|
|
220
|
-
// Horrid, ITF8 is unsigned, but we still write signed into it
|
|
221
|
-
if (i < 0) i = (1 << 32) + i
|
|
222
|
-
|
|
223
|
-
if (i <= 0x0000007f) {
|
|
224
|
-
// 1 byte
|
|
225
|
-
this.buf[this.pos++] = i
|
|
226
|
-
} else if (i <= 0x00003fff) {
|
|
227
|
-
// 2 bytes
|
|
228
|
-
this.buf[this.pos++] = 0x80 | Math.floor(i / 256)
|
|
229
|
-
this.buf[this.pos++] = i & 0xff
|
|
230
|
-
} else if (i < 0x0001ffff) {
|
|
231
|
-
// 3 bytes
|
|
232
|
-
this.buf[this.pos++] = 0xc0 | Math.floor(i / 65536)
|
|
233
|
-
this.buf[this.pos++] = Math.floor(i / 256) & 0xff
|
|
234
|
-
this.buf[this.pos++] = i & 0xff
|
|
235
|
-
} else if (i < 0x0fffffff) {
|
|
236
|
-
// 4 bytes
|
|
237
|
-
this.buf[this.pos++] = 0xe0 | Math.floor(i / 16777216)
|
|
238
|
-
this.buf[this.pos++] = Math.floor(i / 65536) & 0xff
|
|
239
|
-
this.buf[this.pos++] = Math.floor(i / 256) & 0xff
|
|
240
|
-
this.buf[this.pos++] = i & 0xff
|
|
241
|
-
} else {
|
|
242
|
-
// 5 bytes; oddly using 4.5 bytes
|
|
243
|
-
this.buf[this.pos++] = 0xf0 | Math.floor(i / 268435456)
|
|
244
|
-
this.buf[this.pos++] = Math.floor(i / 1048576) & 0xff
|
|
245
|
-
this.buf[this.pos++] = Math.floor(i / 4096) & 0xff
|
|
246
|
-
this.buf[this.pos++] = Math.floor(i / 4) & 0xff
|
|
247
|
-
this.buf[this.pos++] = i & 0x0f
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
// ----------
|
|
252
|
-
// Writing from end of buffer going backwards.
|
|
253
|
-
// Needed by rANS codec.
|
|
254
|
-
WriteByteNeg(b) {
|
|
255
|
-
this.buf[--this.pos] = b
|
|
256
|
-
}
|
|
257
|
-
}
|