Debian Codegraph @

src/org/tukaani/xz/lz/LZDecoder.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.lz;
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 LZDecoder {
14 private final byte[] buf;
15 private final int bufSize; // To avoid buf.length with an array-cached buf.
16 private int start = 0;
17 private int pos = 0;
18 private int full = 0;
19 private int limit = 0;
20 private int pendingLen = 0;
21 private int pendingDist = 0;
22
23 public LZDecoder(int dictSize, byte[] presetDict, ArrayCache arrayCache) {
24 bufSize = dictSize;
25 buf = arrayCache.getByteArray(bufSize, false);
26
27 if (presetDict != null) {
28 pos = Math.min(presetDict.length, dictSize);
29 full = pos;
30 start = pos;
31 System.arraycopy(presetDict, presetDict.length - pos, buf, 0, pos);
32 }
33 }
34
35 public void putArraysToCache(ArrayCache arrayCache) {
36 arrayCache.putArray(buf);
37 }
38
39 public void reset() {
40 start = 0;
41 pos = 0;
42 full = 0;
43 limit = 0;
44 buf[bufSize - 1] = 0x00;
45 }
46
47 public void setLimit(int outMax) {
48 if (bufSize - pos <= outMax)
49 limit = bufSize;
50 else
51 limit = pos + outMax;
52 }
53
54 public boolean hasSpace() {
55 return pos < limit;
56 }
57
58 public boolean hasPending() {
59 return pendingLen > 0;
60 }
61
62 public int getPos() {
63 return pos;
64 }
65
66 public int getByte(int dist) {
67 int offset = pos - dist - 1;
68 if (dist >= pos)
69 offset += bufSize;
70
71 return buf[offset] & 0xFF;
72 }
73
74 public void putByte(byte b) {
75 buf[pos++] = b;
76
77 if (full < pos)
78 full = pos;
79 }
80
81 public void repeat(int dist, int len) throws IOException {
82 if (dist < 0 || dist >= full)
83 throw new CorruptedInputException();
84
85 int left = Math.min(limit - pos, len);
86 pendingLen = len - left;
87 pendingDist = dist;
88
89 int back = pos - dist - 1;
90 if (back < 0) {
91 // The distance wraps around to the end of the cyclic dictionary
92 // buffer. We cannot get here if the dictionary isn't full.
93 assert full == bufSize;
94 back += bufSize;
95
96 // Here we will never copy more than dist + 1 bytes and
97 // so the copying won't repeat from its own output.
98 // Thus, we can always use arraycopy safely.
99 int copySize = Math.min(bufSize - back, left);
100 assert copySize <= dist + 1;
101
102 System.arraycopy(buf, back, buf, pos, copySize);
103 pos += copySize;
104 back = 0;
105 left -= copySize;
106
107 if (left == 0)
108 return;
109 }
110
111 assert back < pos;
112 assert left > 0;
113
114 do {
115 // Determine the number of bytes to copy on this loop iteration:
116 // copySize is set so that the source and destination ranges
117 // don't overlap. If "left" is large enough, the destination
118 // range will start right after the last byte of the source
119 // range. This way we don't need to advance "back" which
120 // allows the next iteration of this loop to copy (up to)
121 // twice the number of bytes.
122 int copySize = Math.min(left, pos - back);
123 System.arraycopy(buf, back, buf, pos, copySize);
124 pos += copySize;
125 left -= copySize;
126 } while (left > 0);
127
128 if (full < pos)
129 full = pos;
130 }
131
132 public void repeatPending() throws IOException {
133 if (pendingLen > 0)
134 repeat(pendingDist, pendingLen);
135 }
136
137 public void copyUncompressed(DataInputStream inData, int len)
138 throws IOException {
139 int copySize = Math.min(bufSize - pos, len);
140 inData.readFully(buf, pos, copySize);
141 pos += copySize;
142
143 if (full < pos)
144 full = pos;
145 }
146
147 public int flush(byte[] out, int outOff) {
148 int copySize = pos - start;
149 if (pos == bufSize)
150 pos = 0;
151
152 System.arraycopy(buf, start, out, outOff, copySize);
153 start = pos;
154
155 return copySize;
156 }
157 }