From 342b0c1bbcfdcda300c66d9a9987b950594d45f9 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 27 Apr 2021 13:04:07 +0200 Subject: [PATCH] Move the `RunLengthStream` from `src/core/stream.js` and into its own file --- src/core/parser.js | 2 +- src/core/run_length_stream.js | 67 +++++++++++++++++++++++++++++++++++ src/core/stream.js | 49 ------------------------- 3 files changed, 68 insertions(+), 50 deletions(-) create mode 100644 src/core/run_length_stream.js diff --git a/src/core/parser.js b/src/core/parser.js index 5c79654b0..a925aa947 100644 --- a/src/core/parser.js +++ b/src/core/parser.js @@ -19,7 +19,6 @@ import { FlateStream, NullStream, PredictorStream, - RunLengthStream, } from "./stream.js"; import { assert, @@ -47,6 +46,7 @@ import { Jbig2Stream } from "./jbig2_stream.js"; import { JpegStream } from "./jpeg_stream.js"; import { JpxStream } from "./jpx_stream.js"; import { LZWStream } from "./lzw_stream.js"; +import { RunLengthStream } from "./run_length_stream.js"; const MAX_LENGTH_TO_CACHE = 1000; const MAX_ADLER32_LENGTH = 5552; diff --git a/src/core/run_length_stream.js b/src/core/run_length_stream.js new file mode 100644 index 000000000..22eb8d769 --- /dev/null +++ b/src/core/run_length_stream.js @@ -0,0 +1,67 @@ +/* Copyright 2012 Mozilla Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* eslint-disable no-var */ + +import { DecodeStream } from "./stream.js"; + +var RunLengthStream = (function RunLengthStreamClosure() { + // eslint-disable-next-line no-shadow + function RunLengthStream(str, maybeLength) { + this.str = str; + this.dict = str.dict; + + DecodeStream.call(this, maybeLength); + } + + RunLengthStream.prototype = Object.create(DecodeStream.prototype); + + RunLengthStream.prototype.readBlock = function RunLengthStream_readBlock() { + // The repeatHeader has following format. The first byte defines type of run + // and amount of bytes to repeat/copy: n = 0 through 127 - copy next n bytes + // (in addition to the second byte from the header), n = 129 through 255 - + // duplicate the second byte from the header (257 - n) times, n = 128 - end. + var repeatHeader = this.str.getBytes(2); + if (!repeatHeader || repeatHeader.length < 2 || repeatHeader[0] === 128) { + this.eof = true; + return; + } + + var buffer; + var bufferLength = this.bufferLength; + var n = repeatHeader[0]; + if (n < 128) { + // copy n bytes + buffer = this.ensureBuffer(bufferLength + n + 1); + buffer[bufferLength++] = repeatHeader[1]; + if (n > 0) { + var source = this.str.getBytes(n); + buffer.set(source, bufferLength); + bufferLength += n; + } + } else { + n = 257 - n; + var b = repeatHeader[1]; + buffer = this.ensureBuffer(bufferLength + n + 1); + for (var i = 0; i < n; i++) { + buffer[bufferLength++] = b; + } + } + this.bufferLength = bufferLength; + }; + + return RunLengthStream; +})(); + +export { RunLengthStream }; diff --git a/src/core/stream.js b/src/core/stream.js index 258f0205e..54321d23a 100644 --- a/src/core/stream.js +++ b/src/core/stream.js @@ -1148,54 +1148,6 @@ var AsciiHexStream = (function AsciiHexStreamClosure() { return AsciiHexStream; })(); -var RunLengthStream = (function RunLengthStreamClosure() { - // eslint-disable-next-line no-shadow - function RunLengthStream(str, maybeLength) { - this.str = str; - this.dict = str.dict; - - DecodeStream.call(this, maybeLength); - } - - RunLengthStream.prototype = Object.create(DecodeStream.prototype); - - RunLengthStream.prototype.readBlock = function RunLengthStream_readBlock() { - // The repeatHeader has following format. The first byte defines type of run - // and amount of bytes to repeat/copy: n = 0 through 127 - copy next n bytes - // (in addition to the second byte from the header), n = 129 through 255 - - // duplicate the second byte from the header (257 - n) times, n = 128 - end. - var repeatHeader = this.str.getBytes(2); - if (!repeatHeader || repeatHeader.length < 2 || repeatHeader[0] === 128) { - this.eof = true; - return; - } - - var buffer; - var bufferLength = this.bufferLength; - var n = repeatHeader[0]; - if (n < 128) { - // copy n bytes - buffer = this.ensureBuffer(bufferLength + n + 1); - buffer[bufferLength++] = repeatHeader[1]; - if (n > 0) { - var source = this.str.getBytes(n); - buffer.set(source, bufferLength); - bufferLength += n; - } - } else { - n = 257 - n; - var b = repeatHeader[1]; - buffer = this.ensureBuffer(bufferLength + n + 1); - for (var i = 0; i < n; i++) { - buffer[bufferLength++] = b; - } - } - this.bufferLength = bufferLength; - }; - - return RunLengthStream; -})(); - var NullStream = (function NullStreamClosure() { // eslint-disable-next-line no-shadow function NullStream() { @@ -1215,7 +1167,6 @@ export { FlateStream, NullStream, PredictorStream, - RunLengthStream, Stream, StreamsSequenceStream, StringStream,