Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | 1x 1x 3x 3x 3x 14x 3x 3x 14x 4x 4x 4x 4x 4x 4x 4x 10x 10x 5x | const fromCharCode = String.fromCharCode;
/**
* A streaming reader which internally manages multiple chunks of
* Uint8Array instances. For performance it does not merge the chunks into
* a single Uint8Array instance. Instead it keeps track of the chunks and
* reads across chunk boundaries as needed.
*/
export class StreamingOctetReader {
protected readonly chunks: Uint8Array[] = [];
/** Total size of all chunks. */
protected chunkSize: number = 0;
protected x: number = 0;
public size(): number {
return this.chunkSize - this.x;
}
public push(chunk: Uint8Array): void {
this.chunks.push(chunk);
this.chunkSize += chunk.length;
}
protected assertSize(size: number): void {
Iif (size > this.size()) throw new RangeError('OUT_OF_BOUNDS');
}
public u8(): number {
this.assertSize(1);
const chunk = this.chunks[0]!;
let x = this.x;
const octet = chunk[x++];
Iif (x === chunk.length) {
this.chunks.shift();
this.chunkSize -= chunk.length;
x = 0;
}
this.x = x;
return octet;
}
public u32(): number {
const octet0 = this.u8();
const octet1 = this.u8();
const octet2 = this.u8();
const octet3 = this.u8();
return (octet0 * 0x1000000 + (octet1 << 16) + (octet2 << 8)) | octet3;
}
public copy(size: number, dst: Uint8Array, pos: number): void {
Iif (!size) return;
this.assertSize(size);
const chunk0 = this.chunks[0]!;
const size0 = Math.min(chunk0.length - this.x, size);
dst.set(chunk0.subarray(this.x, this.x + size0), pos);
size -= size0;
Iif (size <= 0) {
this.skipUnsafe(size0);
return;
}
let chunkIndex = 1;
while (size > 0) {
const chunk1 = this.chunks[chunkIndex]!;
const size1 = Math.min(chunk1.length, size);
dst.set(chunk1.subarray(0, size1), pos + size0);
size -= size1;
chunkIndex++;
}
this.skipUnsafe(size);
}
public copyXor(
size: number,
dst: Uint8Array,
pos: number,
mask: [number, number, number, number],
maskIndex: number,
): void {
Iif (!size) return;
this.assertSize(size);
const chunk0 = this.chunks[0]!;
let x = this.x;
const size0 = Math.min(chunk0.length - x, size);
const end = x + size0;
for (; x < end; ) dst[pos++] = chunk0[x++] ^ mask[maskIndex++ % 4];
size -= size0;
Iif (size <= 0) {
this.skipUnsafe(size0);
return;
}
let chunkIndex = 1;
while (size > 0) {
const chunk1 = this.chunks[chunkIndex++]!;
const size1 = Math.min(chunk1.length, size);
for (let x = 0; x < size1; ) dst[pos++] = chunk1[x++] ^ mask[maskIndex++ % 4];
size -= size1;
}
this.skipUnsafe(size);
}
public buf(size: number): Uint8Array {
this.assertSize(size);
const buf = new Uint8Array(size);
this.copy(size, buf, 0);
return buf;
}
public bufXor(size: number, mask: [number, number, number, number], maskIndex: number): Uint8Array {
this.assertSize(size);
const buf = new Uint8Array(size);
this.copyXor(size, buf, 0, mask, maskIndex);
return buf;
}
public skipUnsafe(n: number): void {
Iif (!n) return;
const chunk = this.chunks[0]!;
const chunkLength = chunk.length;
const remaining = chunkLength - this.x;
Iif (remaining > n) {
this.x = this.x + n;
return;
}
this.x = 0;
this.chunks.shift();
this.chunkSize -= chunkLength;
n -= remaining;
this.skipUnsafe(n);
}
public skip(n: number): void {
this.assertSize(n);
this.skipUnsafe(n);
}
public peek(): number {
this.assertSize(1);
return this.chunks[0]![this.x];
}
/**
* Get current byte value without advancing the cursor.
* @deprecated Use peek() instead.
*/
public peak(): number {
return this.peek();
}
public utf8(length: number, mask: [number, number, number, number], maskIndex: number): string {
this.assertSize(length);
let i = 0;
const points: number[] = [];
while (i < length) {
let code = this.u8() ^ mask[maskIndex++ % 4];
i++;
Iif ((code & 0x80) !== 0) {
const octet2 = (this.u8() ^ mask[maskIndex++ % 4]) & 0x3f;
i++;
if ((code & 0xe0) === 0xc0) {
code = ((code & 0x1f) << 6) | octet2;
} else {
const octet3 = (this.u8() ^ mask[maskIndex++ % 4]) & 0x3f;
i++;
if ((code & 0xf0) === 0xe0) {
code = ((code & 0x1f) << 12) | (octet2 << 6) | octet3;
} else {
Iif ((code & 0xf8) === 0xf0) {
const octet4 = (this.u8() ^ mask[maskIndex++ % 4]) & 0x3f;
i++;
let unit = ((code & 0x07) << 0x12) | (octet2 << 0x0c) | (octet3 << 0x06) | octet4;
if (unit > 0xffff) {
unit -= 0x10000;
const unit0 = ((unit >>> 10) & 0x3ff) | 0xd800;
code = 0xdc00 | (unit & 0x3ff);
points.push(unit0);
} else {
code = unit;
}
}
}
}
}
points.push(code);
}
return fromCharCode.apply(String, points);
}
}
|