mirror of
https://github.com/google/brotli.git
synced 2024-11-09 13:40:06 +00:00
Merge branch 'master' into dependabot/github_actions/ossf/scorecard-action-2.3.1
This commit is contained in:
commit
a1851fe3f7
@ -68,7 +68,7 @@ final class BitReader {
|
|||||||
s.halfOffset = 0;
|
s.halfOffset = 0;
|
||||||
while (bytesInBuffer < CAPACITY) {
|
while (bytesInBuffer < CAPACITY) {
|
||||||
final int spaceLeft = CAPACITY - bytesInBuffer;
|
final int spaceLeft = CAPACITY - bytesInBuffer;
|
||||||
final int len = Utils.readInput(s.input, s.byteBuffer, bytesInBuffer, spaceLeft);
|
final int len = Utils.readInput(s, s.byteBuffer, bytesInBuffer, spaceLeft);
|
||||||
// EOF is -1 in Java, but 0 in C#.
|
// EOF is -1 in Java, but 0 in C#.
|
||||||
if (len <= 0) {
|
if (len <= 0) {
|
||||||
s.endOfStreamReached = 1;
|
s.endOfStreamReached = 1;
|
||||||
@ -255,7 +255,7 @@ final class BitReader {
|
|||||||
|
|
||||||
// Now it is possible to copy bytes directly.
|
// Now it is possible to copy bytes directly.
|
||||||
while (length > 0) {
|
while (length > 0) {
|
||||||
final int len = Utils.readInput(s.input, data, offset, length);
|
final int len = Utils.readInput(s, data, offset, length);
|
||||||
if (len == -1) {
|
if (len == -1) {
|
||||||
throw new BrotliRuntimeException("Unexpected end of input");
|
throw new BrotliRuntimeException("Unexpected end of input");
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,8 @@ public class BitReaderTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testReadAfterEos() {
|
public void testReadAfterEos() {
|
||||||
State reader = new State();
|
State reader = new State();
|
||||||
Decode.initState(reader, new ByteArrayInputStream(new byte[1]));
|
reader.input = new ByteArrayInputStream(new byte[1]);
|
||||||
|
Decode.initState(reader);
|
||||||
BitReader.readBits(reader, 9);
|
BitReader.readBits(reader, 9);
|
||||||
try {
|
try {
|
||||||
BitReader.checkHealth(reader, 0);
|
BitReader.checkHealth(reader, 0);
|
||||||
@ -36,7 +37,8 @@ public class BitReaderTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testAccumulatorUnderflowDetected() {
|
public void testAccumulatorUnderflowDetected() {
|
||||||
State reader = new State();
|
State reader = new State();
|
||||||
Decode.initState(reader, new ByteArrayInputStream(new byte[8]));
|
reader.input = new ByteArrayInputStream(new byte[8]);
|
||||||
|
Decode.initState(reader);
|
||||||
// 65 bits is enough for both 32 and 64 bit systems.
|
// 65 bits is enough for both 32 and 64 bit systems.
|
||||||
BitReader.readBits(reader, 13);
|
BitReader.readBits(reader, 13);
|
||||||
BitReader.readBits(reader, 13);
|
BitReader.readBits(reader, 13);
|
||||||
|
@ -86,7 +86,8 @@ public class BrotliInputStream extends InputStream {
|
|||||||
this.remainingBufferBytes = 0;
|
this.remainingBufferBytes = 0;
|
||||||
this.bufferOffset = 0;
|
this.bufferOffset = 0;
|
||||||
try {
|
try {
|
||||||
Decode.initState(state, source);
|
state.input = source;
|
||||||
|
Decode.initState(state);
|
||||||
} catch (BrotliRuntimeException ex) {
|
} catch (BrotliRuntimeException ex) {
|
||||||
throw new IOException("Brotli decoder initialization failed", ex);
|
throw new IOException("Brotli decoder initialization failed", ex);
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,6 @@
|
|||||||
package org.brotli.dec;
|
package org.brotli.dec;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -290,7 +289,7 @@ final class Decode {
|
|||||||
* @param s uninitialized state without associated input
|
* @param s uninitialized state without associated input
|
||||||
* @param input compressed data source
|
* @param input compressed data source
|
||||||
*/
|
*/
|
||||||
static void initState(State s, InputStream input) {
|
static void initState(State s) {
|
||||||
if (s.runningState != UNINITIALIZED) {
|
if (s.runningState != UNINITIALIZED) {
|
||||||
throw new IllegalStateException("State MUST be uninitialized");
|
throw new IllegalStateException("State MUST be uninitialized");
|
||||||
}
|
}
|
||||||
@ -302,7 +301,6 @@ final class Decode {
|
|||||||
calculateDistanceAlphabetLimit(MAX_ALLOWED_DISTANCE, 3, 15 << 3);
|
calculateDistanceAlphabetLimit(MAX_ALLOWED_DISTANCE, 3, 15 << 3);
|
||||||
s.distExtraBits = new byte[maxDistanceAlphabetLimit];
|
s.distExtraBits = new byte[maxDistanceAlphabetLimit];
|
||||||
s.distOffset = new int[maxDistanceAlphabetLimit];
|
s.distOffset = new int[maxDistanceAlphabetLimit];
|
||||||
s.input = input;
|
|
||||||
BitReader.initBitReader(s);
|
BitReader.initBitReader(s);
|
||||||
s.runningState = INITIALIZED;
|
s.runningState = INITIALIZED;
|
||||||
}
|
}
|
||||||
@ -315,10 +313,7 @@ final class Decode {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
s.runningState = CLOSED;
|
s.runningState = CLOSED;
|
||||||
if (s.input != null) {
|
Utils.closeInput(s);
|
||||||
Utils.closeInput(s.input);
|
|
||||||
s.input = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -7,7 +7,6 @@
|
|||||||
package org.brotli.dec;
|
package org.brotli.dec;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.nio.Buffer;
|
import java.nio.Buffer;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
@ -67,16 +66,19 @@ final class Utils {
|
|||||||
System.arraycopy(bytes, start, bytes, target, end - start);
|
System.arraycopy(bytes, start, bytes, target, end - start);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int readInput(InputStream src, byte[] dst, int offset, int length) {
|
static int readInput(State s, byte[] dst, int offset, int length) {
|
||||||
try {
|
try {
|
||||||
return src.read(dst, offset, length);
|
return s.input.read(dst, offset, length);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new BrotliRuntimeException("Failed to read input", e);
|
throw new BrotliRuntimeException("Failed to read input", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void closeInput(InputStream src) throws IOException {
|
static void closeInput(State s) throws IOException {
|
||||||
src.close();
|
if (s.input != null) {
|
||||||
|
s.input.close();
|
||||||
|
s.input = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static byte[] toUsAsciiBytes(String src) {
|
static byte[] toUsAsciiBytes(String src) {
|
||||||
|
22
js/decode.js
22
js/decode.js
@ -193,10 +193,9 @@ let makeBrotliDecode = () => {
|
|||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* @param {!State} s
|
* @param {!State} s
|
||||||
* @param {!InputStream} input
|
|
||||||
* @return {void}
|
* @return {void}
|
||||||
*/
|
*/
|
||||||
function initState(s, input) {
|
function initState(s) {
|
||||||
if (s.runningState !== 0) {
|
if (s.runningState !== 0) {
|
||||||
throw new Error("State MUST be uninitialized");
|
throw new Error("State MUST be uninitialized");
|
||||||
}
|
}
|
||||||
@ -206,7 +205,6 @@ let makeBrotliDecode = () => {
|
|||||||
const /** @type {number} */ maxDistanceAlphabetLimit = calculateDistanceAlphabetLimit(0x7FFFFFFC, 3, 120);
|
const /** @type {number} */ maxDistanceAlphabetLimit = calculateDistanceAlphabetLimit(0x7FFFFFFC, 3, 120);
|
||||||
s.distExtraBits = new Int8Array(maxDistanceAlphabetLimit);
|
s.distExtraBits = new Int8Array(maxDistanceAlphabetLimit);
|
||||||
s.distOffset = new Int32Array(maxDistanceAlphabetLimit);
|
s.distOffset = new Int32Array(maxDistanceAlphabetLimit);
|
||||||
s.input = input;
|
|
||||||
initBitReader(s);
|
initBitReader(s);
|
||||||
s.runningState = 1;
|
s.runningState = 1;
|
||||||
}
|
}
|
||||||
@ -222,9 +220,7 @@ let makeBrotliDecode = () => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
s.runningState = 11;
|
s.runningState = 11;
|
||||||
if (s.input !== null) {
|
s.input = null;
|
||||||
s.input = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* @param {!State} s
|
* @param {!State} s
|
||||||
@ -1666,7 +1662,7 @@ let makeBrotliDecode = () => {
|
|||||||
s.halfOffset = 0;
|
s.halfOffset = 0;
|
||||||
while (bytesInBuffer < 4096) {
|
while (bytesInBuffer < 4096) {
|
||||||
const /** @type {number} */ spaceLeft = 4096 - bytesInBuffer;
|
const /** @type {number} */ spaceLeft = 4096 - bytesInBuffer;
|
||||||
const /** @type {number} */ len = readInput(s.input, s.byteBuffer, bytesInBuffer, spaceLeft);
|
const /** @type {number} */ len = readInput(s, s.byteBuffer, bytesInBuffer, spaceLeft);
|
||||||
if (len <= 0) {
|
if (len <= 0) {
|
||||||
s.endOfStreamReached = 1;
|
s.endOfStreamReached = 1;
|
||||||
s.tailBytes = bytesInBuffer;
|
s.tailBytes = bytesInBuffer;
|
||||||
@ -1820,7 +1816,7 @@ let makeBrotliDecode = () => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
while (length > 0) {
|
while (length > 0) {
|
||||||
const /** @type {number} */ len = readInput(s.input, data, offset, length);
|
const /** @type {number} */ len = readInput(s, data, offset, length);
|
||||||
if (len === -1) {
|
if (len === -1) {
|
||||||
throw new Error("Unexpected end of input");
|
throw new Error("Unexpected end of input");
|
||||||
}
|
}
|
||||||
@ -2151,16 +2147,17 @@ let makeBrotliDecode = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {?InputStream} src
|
* @param {!State} s
|
||||||
* @param {!Int8Array} dst
|
* @param {!Int8Array} dst
|
||||||
* @param {number} offset
|
* @param {number} offset
|
||||||
* @param {number} length
|
* @param {number} length
|
||||||
* @return {number}
|
* @return {number}
|
||||||
*/
|
*/
|
||||||
function readInput(src, dst, offset, length) {
|
function readInput(s, dst, offset, length) {
|
||||||
if (src === null) {
|
if (s.input === null) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
const /** @type {!InputStream} */ src = s.input;
|
||||||
let /** @type {number} */ end = Math.min(src.offset + length, src.data.length);
|
let /** @type {number} */ end = Math.min(src.offset + length, src.data.length);
|
||||||
let /** @type {number} */ bytesRead = end - src.offset;
|
let /** @type {number} */ bytesRead = end - src.offset;
|
||||||
dst.set(src.data.subarray(src.offset, end), offset);
|
dst.set(src.data.subarray(src.offset, end), offset);
|
||||||
@ -2192,7 +2189,8 @@ let makeBrotliDecode = () => {
|
|||||||
*/
|
*/
|
||||||
function decode(bytes, options) {
|
function decode(bytes, options) {
|
||||||
let /** @type {!State} */ s = new State();
|
let /** @type {!State} */ s = new State();
|
||||||
initState(s, new InputStream(bytes));
|
s.input = new InputStream(bytes);
|
||||||
|
initState(s);
|
||||||
if (options) {
|
if (options) {
|
||||||
let customDictionary =
|
let customDictionary =
|
||||||
/** @type {?Int8Array} */ (options["customDictionary"]);
|
/** @type {?Int8Array} */ (options["customDictionary"]);
|
||||||
|
2
js/decode.min.js
vendored
2
js/decode.min.js
vendored
File diff suppressed because one or more lines are too long
19
js/decode.ts
19
js/decode.ts
@ -136,7 +136,7 @@ function attachDictionaryChunk(s: State, data: Int8Array): void {
|
|||||||
s.cdTotalSize += data.length;
|
s.cdTotalSize += data.length;
|
||||||
s.cdChunkOffsets[s.cdNumChunks] = s.cdTotalSize;
|
s.cdChunkOffsets[s.cdNumChunks] = s.cdTotalSize;
|
||||||
}
|
}
|
||||||
function initState(s: State, input: InputStream): void {
|
function initState(s: State): void {
|
||||||
if (s.runningState !== 0) {
|
if (s.runningState !== 0) {
|
||||||
throw new Error("State MUST be uninitialized");
|
throw new Error("State MUST be uninitialized");
|
||||||
}
|
}
|
||||||
@ -146,7 +146,6 @@ function initState(s: State, input: InputStream): void {
|
|||||||
const maxDistanceAlphabetLimit: number = calculateDistanceAlphabetLimit(0x7FFFFFFC, 3, 120);
|
const maxDistanceAlphabetLimit: number = calculateDistanceAlphabetLimit(0x7FFFFFFC, 3, 120);
|
||||||
s.distExtraBits = new Int8Array(maxDistanceAlphabetLimit);
|
s.distExtraBits = new Int8Array(maxDistanceAlphabetLimit);
|
||||||
s.distOffset = new Int32Array(maxDistanceAlphabetLimit);
|
s.distOffset = new Int32Array(maxDistanceAlphabetLimit);
|
||||||
s.input = input;
|
|
||||||
initBitReader(s);
|
initBitReader(s);
|
||||||
s.runningState = 1;
|
s.runningState = 1;
|
||||||
}
|
}
|
||||||
@ -158,9 +157,7 @@ function close(s: State): void {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
s.runningState = 11;
|
s.runningState = 11;
|
||||||
if (s.input !== null) {
|
s.input = null;
|
||||||
s.input = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
function decodeVarLenUnsignedByte(s: State): number {
|
function decodeVarLenUnsignedByte(s: State): number {
|
||||||
if (s.bitOffset >= 16) {
|
if (s.bitOffset >= 16) {
|
||||||
@ -1398,7 +1395,7 @@ function doReadMoreInput(s: State): void {
|
|||||||
s.halfOffset = 0;
|
s.halfOffset = 0;
|
||||||
while (bytesInBuffer < 4096) {
|
while (bytesInBuffer < 4096) {
|
||||||
const spaceLeft: number = 4096 - bytesInBuffer;
|
const spaceLeft: number = 4096 - bytesInBuffer;
|
||||||
const len: number = readInput(s.input, s.byteBuffer, bytesInBuffer, spaceLeft);
|
const len: number = readInput(s, s.byteBuffer, bytesInBuffer, spaceLeft);
|
||||||
if (len <= 0) {
|
if (len <= 0) {
|
||||||
s.endOfStreamReached = 1;
|
s.endOfStreamReached = 1;
|
||||||
s.tailBytes = bytesInBuffer;
|
s.tailBytes = bytesInBuffer;
|
||||||
@ -1510,7 +1507,7 @@ function copyRawBytes(s: State, data: Int8Array, offset: number, length: number)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
while (length > 0) {
|
while (length > 0) {
|
||||||
const len: number = readInput(s.input, data, offset, length);
|
const len: number = readInput(s, data, offset, length);
|
||||||
if (len === -1) {
|
if (len === -1) {
|
||||||
throw new Error("Unexpected end of input");
|
throw new Error("Unexpected end of input");
|
||||||
}
|
}
|
||||||
@ -1728,10 +1725,11 @@ function valueOf(x: number): string {
|
|||||||
return x.toString();
|
return x.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
function readInput(src: InputStream|null, dst: Int8Array, offset: number, length: number): number {
|
function readInput(s: State, dst: Int8Array, offset: number, length: number): number {
|
||||||
if (src === null) {
|
if (s.input === null) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
const src: InputStream = s.input;
|
||||||
const end: number = Math.min(src.offset + length, src.data.length);
|
const end: number = Math.min(src.offset + length, src.data.length);
|
||||||
const bytesRead: number = end - src.offset;
|
const bytesRead: number = end - src.offset;
|
||||||
dst.set(src.data.subarray(src.offset, end), offset);
|
dst.set(src.data.subarray(src.offset, end), offset);
|
||||||
@ -1757,7 +1755,8 @@ type ByteBuffer = Int8Array;
|
|||||||
export function brotliDecode(
|
export function brotliDecode(
|
||||||
bytes: Int8Array, options?: BrotliDecodeOptions): Int8Array {
|
bytes: Int8Array, options?: BrotliDecodeOptions): Int8Array {
|
||||||
const s = new State();
|
const s = new State();
|
||||||
initState(s, new InputStream(bytes));
|
s.input = new InputStream(bytes);
|
||||||
|
initState(s);
|
||||||
if (options) {
|
if (options) {
|
||||||
const customDictionary: Int8Array|null = options.customDictionary;
|
const customDictionary: Int8Array|null = options.customDictionary;
|
||||||
if (customDictionary) attachDictionaryChunk(s, customDictionary);
|
if (customDictionary) attachDictionaryChunk(s, customDictionary);
|
||||||
|
Loading…
Reference in New Issue
Block a user