forked from vanielf/pdf-lib
-
Notifications
You must be signed in to change notification settings - Fork 27
/
AsciiHexStream.ts
77 lines (67 loc) · 2.01 KB
/
AsciiHexStream.ts
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
/*
* Copyright 2012 Mozilla Foundation
*
* The AsciiHexStream class contained in this file is a TypeScript port of the
* JavaScript AsciiHexStream class in Mozilla's pdf.js project, made available
* under the Apache 2.0 open source license.
*/
import DecodeStream from './DecodeStream';
import { StreamType } from './Stream';
class AsciiHexStream extends DecodeStream {
private stream: StreamType;
private firstDigit: number;
constructor(stream: StreamType, maybeLength?: number) {
super(maybeLength);
this.stream = stream;
this.firstDigit = -1;
// Most streams increase in size when decoded, but AsciiHex streams shrink
// by 50%.
if (maybeLength) {
maybeLength = 0.5 * maybeLength;
}
}
protected readBlock() {
const UPSTREAM_BLOCK_SIZE = 8000;
const bytes = this.stream.getBytes(UPSTREAM_BLOCK_SIZE);
if (!bytes.length) {
this.eof = true;
return;
}
const maxDecodeLength = (bytes.length + 1) >> 1;
const buffer = this.ensureBuffer(this.bufferLength + maxDecodeLength);
let bufferLength = this.bufferLength;
let firstDigit = this.firstDigit;
for (let i = 0, ii = bytes.length; i < ii; i++) {
const ch = bytes[i];
let digit;
if (ch >= 0x30 && ch <= 0x39) {
// '0'-'9'
digit = ch & 0x0f;
} else if ((ch >= 0x41 && ch <= 0x46) || (ch >= 0x61 && ch <= 0x66)) {
// 'A'-'Z', 'a'-'z'
digit = (ch & 0x0f) + 9;
} else if (ch === 0x3e) {
// '>'
this.eof = true;
break;
} else {
// probably whitespace
continue; // ignoring
}
if (firstDigit < 0) {
firstDigit = digit;
} else {
buffer[bufferLength++] = (firstDigit << 4) | digit;
firstDigit = -1;
}
}
if (firstDigit >= 0 && this.eof) {
// incomplete byte
buffer[bufferLength++] = firstDigit << 4;
firstDigit = -1;
}
this.firstDigit = firstDigit;
this.bufferLength = bufferLength;
}
}
export default AsciiHexStream;