forked from vanielf/pdf-lib
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Ascii85Stream.ts
98 lines (81 loc) · 2.2 KB
/
Ascii85Stream.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* Copyright 2012 Mozilla Foundation
*
* The Ascii85Stream class contained in this file is a TypeScript port of the
* JavaScript Ascii85Stream 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';
const isSpace = (ch: number) =>
ch === 0x20 || ch === 0x09 || ch === 0x0d || ch === 0x0a;
class Ascii85Stream extends DecodeStream {
private stream: StreamType;
private input: Uint8Array;
constructor(stream: StreamType, maybeLength?: number) {
super(maybeLength);
this.stream = stream;
this.input = new Uint8Array(5);
// Most streams increase in size when decoded, but Ascii85 streams
// typically shrink by ~20%.
if (maybeLength) {
maybeLength = 0.8 * maybeLength;
}
}
protected readBlock() {
const TILDA_CHAR = 0x7e; // '~'
const Z_LOWER_CHAR = 0x7a; // 'z'
const EOF = -1;
const stream = this.stream;
let c = stream.getByte();
while (isSpace(c)) {
c = stream.getByte();
}
if (c === EOF || c === TILDA_CHAR) {
this.eof = true;
return;
}
const bufferLength = this.bufferLength;
let buffer;
let i;
// special code for z
if (c === Z_LOWER_CHAR) {
buffer = this.ensureBuffer(bufferLength + 4);
for (i = 0; i < 4; ++i) {
buffer[bufferLength + i] = 0;
}
this.bufferLength += 4;
} else {
const input = this.input;
input[0] = c;
for (i = 1; i < 5; ++i) {
c = stream.getByte();
while (isSpace(c)) {
c = stream.getByte();
}
input[i] = c;
if (c === EOF || c === TILDA_CHAR) {
break;
}
}
buffer = this.ensureBuffer(bufferLength + i - 1);
this.bufferLength += i - 1;
// partial ending;
if (i < 5) {
for (; i < 5; ++i) {
input[i] = 0x21 + 84;
}
this.eof = true;
}
let t = 0;
for (i = 0; i < 5; ++i) {
t = t * 85 + (input[i] - 0x21);
}
for (i = 3; i >= 0; --i) {
buffer[bufferLength + i] = t & 0xff;
t >>= 8;
}
}
}
}
export default Ascii85Stream;