Debian Codegraph @

src/org/tukaani/xz/rangecoder/RangeDecoderFromBuffer.java

raw symbols SCIP
1 // SPDX-License-Identifier: 0BSD
2 // SPDX-FileCopyrightText: The XZ for Java authors and contributors
3 // SPDX-FileContributor: Lasse Collin <[email protected]>
4 // SPDX-FileContributor: Igor Pavlov <https://7-zip.org/>
5
6 package org.tukaani.xz.rangecoder;
7
8 import java.io.DataInputStream;
9 import java.io.IOException;
10 import org.tukaani.xz.ArrayCache;
11 import org.tukaani.xz.CorruptedInputException;
12
13 public final class RangeDecoderFromBuffer extends RangeDecoder {
14 private static final int INIT_SIZE = 5;
15
16 private final byte[] buf;
17 private int pos;
18
19 public RangeDecoderFromBuffer(int inputSizeMax, ArrayCache arrayCache) {
20 // We will use the *end* of the array so if the cache gives us
21 // a bigger-than-requested array, we still want to use buf.length.
22 buf = arrayCache.getByteArray(inputSizeMax - INIT_SIZE, false);
23 pos = buf.length;
24 }
25
26 public void putArraysToCache(ArrayCache arrayCache) {
27 arrayCache.putArray(buf);
28 }
29
30 public void prepareInputBuffer(DataInputStream in, int len)
31 throws IOException {
32 if (len < INIT_SIZE)
33 throw new CorruptedInputException();
34
35 if (in.readUnsignedByte() != 0x00)
36 throw new CorruptedInputException();
37
38 code = in.readInt();
39 range = 0xFFFFFFFF;
40
41 // Read the data to the end of the buffer. If the data is corrupt
42 // and the decoder, reading from buf, tries to read past the end of
43 // the data, ArrayIndexOutOfBoundsException will be thrown and
44 // the problem is detected immediately.
45 len -= INIT_SIZE;
46 pos = buf.length - len;
47 in.readFully(buf, pos, len);
48 }
49
50 public boolean isFinished() {
51 return pos == buf.length && code == 0;
52 }
53
54 @Override
55 public void normalize() throws IOException {
56 if ((range & TOP_MASK) == 0) {
57 try {
58 // If the input is corrupt, this might throw
59 // ArrayIndexOutOfBoundsException.
60 code = (code << SHIFT_BITS) | (buf[pos++] & 0xFF);
61 range <<= SHIFT_BITS;
62 } catch (ArrayIndexOutOfBoundsException e) {
63 throw new CorruptedInputException();
64 }
65 }
66 }
67 }