Skip to content

Commit f6f3351

Browse files
Merge pull request #13303 from Snuffleupagus/BaseStream
Add an abstract base-class, which all the various Stream implementations inherit from
2 parents af4dc55 + 2ac4ad3 commit f6f3351

23 files changed

+1481
-1425
lines changed

src/core/ascii_85_stream.js

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/* Copyright 2012 Mozilla Foundation
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
import { DecodeStream } from "./decode_stream.js";
17+
import { isWhiteSpace } from "./core_utils.js";
18+
19+
class Ascii85Stream extends DecodeStream {
20+
constructor(str, maybeLength) {
21+
// Most streams increase in size when decoded, but Ascii85 streams
22+
// typically shrink by ~20%.
23+
if (maybeLength) {
24+
maybeLength = 0.8 * maybeLength;
25+
}
26+
super(maybeLength);
27+
28+
this.str = str;
29+
this.dict = str.dict;
30+
this.input = new Uint8Array(5);
31+
}
32+
33+
readBlock() {
34+
const TILDA_CHAR = 0x7e; // '~'
35+
const Z_LOWER_CHAR = 0x7a; // 'z'
36+
const EOF = -1;
37+
38+
const str = this.str;
39+
40+
let c = str.getByte();
41+
while (isWhiteSpace(c)) {
42+
c = str.getByte();
43+
}
44+
45+
if (c === EOF || c === TILDA_CHAR) {
46+
this.eof = true;
47+
return;
48+
}
49+
50+
const bufferLength = this.bufferLength;
51+
let buffer, i;
52+
53+
// special code for z
54+
if (c === Z_LOWER_CHAR) {
55+
buffer = this.ensureBuffer(bufferLength + 4);
56+
for (i = 0; i < 4; ++i) {
57+
buffer[bufferLength + i] = 0;
58+
}
59+
this.bufferLength += 4;
60+
} else {
61+
const input = this.input;
62+
input[0] = c;
63+
for (i = 1; i < 5; ++i) {
64+
c = str.getByte();
65+
while (isWhiteSpace(c)) {
66+
c = str.getByte();
67+
}
68+
69+
input[i] = c;
70+
71+
if (c === EOF || c === TILDA_CHAR) {
72+
break;
73+
}
74+
}
75+
buffer = this.ensureBuffer(bufferLength + i - 1);
76+
this.bufferLength += i - 1;
77+
78+
// partial ending;
79+
if (i < 5) {
80+
for (; i < 5; ++i) {
81+
input[i] = 0x21 + 84;
82+
}
83+
this.eof = true;
84+
}
85+
let t = 0;
86+
for (i = 0; i < 5; ++i) {
87+
t = t * 85 + (input[i] - 0x21);
88+
}
89+
90+
for (i = 3; i >= 0; --i) {
91+
buffer[bufferLength + i] = t & 0xff;
92+
t >>= 8;
93+
}
94+
}
95+
}
96+
}
97+
98+
export { Ascii85Stream };

src/core/ascii_hex_stream.js

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/* Copyright 2012 Mozilla Foundation
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
import { DecodeStream } from "./decode_stream.js";
17+
18+
class AsciiHexStream extends DecodeStream {
19+
constructor(str, maybeLength) {
20+
// Most streams increase in size when decoded, but AsciiHex streams shrink
21+
// by 50%.
22+
if (maybeLength) {
23+
maybeLength = 0.5 * maybeLength;
24+
}
25+
super(maybeLength);
26+
27+
this.str = str;
28+
this.dict = str.dict;
29+
30+
this.firstDigit = -1;
31+
}
32+
33+
readBlock() {
34+
const UPSTREAM_BLOCK_SIZE = 8000;
35+
const bytes = this.str.getBytes(UPSTREAM_BLOCK_SIZE);
36+
if (!bytes.length) {
37+
this.eof = true;
38+
return;
39+
}
40+
41+
const maxDecodeLength = (bytes.length + 1) >> 1;
42+
const buffer = this.ensureBuffer(this.bufferLength + maxDecodeLength);
43+
let bufferLength = this.bufferLength;
44+
45+
let firstDigit = this.firstDigit;
46+
for (const ch of bytes) {
47+
let digit;
48+
if (ch >= /* '0' = */ 0x30 && ch <= /* '9' = */ 0x39) {
49+
digit = ch & 0x0f;
50+
} else if (
51+
(ch >= /* 'A' = */ 0x41 && ch <= /* 'Z' = */ 0x46) ||
52+
(ch >= /* 'a' = */ 0x61 && ch <= /* 'z' = */ 0x66)
53+
) {
54+
digit = (ch & 0x0f) + 9;
55+
} else if (ch === /* '>' = */ 0x3e) {
56+
this.eof = true;
57+
break;
58+
} else {
59+
// Probably whitespace, ignoring.
60+
continue;
61+
}
62+
if (firstDigit < 0) {
63+
firstDigit = digit;
64+
} else {
65+
buffer[bufferLength++] = (firstDigit << 4) | digit;
66+
firstDigit = -1;
67+
}
68+
}
69+
if (firstDigit >= 0 && this.eof) {
70+
// incomplete byte
71+
buffer[bufferLength++] = firstDigit << 4;
72+
firstDigit = -1;
73+
}
74+
this.firstDigit = firstDigit;
75+
this.bufferLength = bufferLength;
76+
}
77+
}
78+
79+
export { AsciiHexStream };

src/core/base_stream.js

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/* Copyright 2021 Mozilla Foundation
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
import { shadow, unreachable } from "../shared/util.js";
17+
18+
class BaseStream {
19+
constructor() {
20+
if (this.constructor === BaseStream) {
21+
unreachable("Cannot initialize BaseStream.");
22+
}
23+
}
24+
25+
// eslint-disable-next-line getter-return
26+
get length() {
27+
unreachable("Abstract getter `length` accessed");
28+
}
29+
30+
// eslint-disable-next-line getter-return
31+
get isEmpty() {
32+
unreachable("Abstract getter `isEmpty` accessed");
33+
}
34+
35+
get isDataLoaded() {
36+
return shadow(this, "isDataLoaded", true);
37+
}
38+
39+
getByte() {
40+
unreachable("Abstract method `getByte` called");
41+
}
42+
43+
getBytes(length, forceClamped = false) {
44+
unreachable("Abstract method `getBytes` called");
45+
}
46+
47+
peekByte() {
48+
const peekedByte = this.getByte();
49+
if (peekedByte !== -1) {
50+
this.pos--;
51+
}
52+
return peekedByte;
53+
}
54+
55+
peekBytes(length, forceClamped = false) {
56+
const bytes = this.getBytes(length, forceClamped);
57+
this.pos -= bytes.length;
58+
return bytes;
59+
}
60+
61+
getUint16() {
62+
const b0 = this.getByte();
63+
const b1 = this.getByte();
64+
if (b0 === -1 || b1 === -1) {
65+
return -1;
66+
}
67+
return (b0 << 8) + b1;
68+
}
69+
70+
getInt32() {
71+
const b0 = this.getByte();
72+
const b1 = this.getByte();
73+
const b2 = this.getByte();
74+
const b3 = this.getByte();
75+
return (b0 << 24) + (b1 << 16) + (b2 << 8) + b3;
76+
}
77+
78+
getByteRange(begin, end) {
79+
unreachable("Abstract method `getByteRange` called");
80+
}
81+
82+
skip(n) {
83+
this.pos += n || 1;
84+
}
85+
86+
reset() {
87+
unreachable("Abstract method `reset` called");
88+
}
89+
90+
moveStart() {
91+
unreachable("Abstract method `moveStart` called");
92+
}
93+
94+
makeSubStream(start, length, dict = null) {
95+
unreachable("Abstract method `makeSubStream` called");
96+
}
97+
98+
/**
99+
* @returns {Array | null}
100+
*/
101+
getBaseStreams() {
102+
return null;
103+
}
104+
}
105+
106+
export { BaseStream };

src/core/ccitt_stream.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
import { Dict, isDict } from "./primitives.js";
1717
import { CCITTFaxDecoder } from "./ccitt.js";
18-
import { DecodeStream } from "./stream.js";
18+
import { DecodeStream } from "./decode_stream.js";
1919

2020
class CCITTFaxStream extends DecodeStream {
2121
constructor(str, maybeLength, params) {

0 commit comments

Comments
 (0)