Debian Codegraph @

src/org/tukaani/xz/index/IndexDecoder.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
5 package org.tukaani.xz.index;
6
7 import java.io.IOException;
8 import java.io.EOFException;
9 import java.util.zip.CheckedInputStream;
10 import org.tukaani.xz.common.DecoderUtil;
11 import org.tukaani.xz.common.StreamFlags;
12 import org.tukaani.xz.SeekableInputStream;
13 import org.tukaani.xz.CorruptedInputException;
14 import org.tukaani.xz.MemoryLimitException;
15 import org.tukaani.xz.UnsupportedOptionsException;
16
17 public final class IndexDecoder extends IndexBase {
18 private final StreamFlags streamFlags;
19 private final long streamPadding;
20 private final int memoryUsage;
21
22 // Unpadded Size and Uncompressed Size fields
23 private final long[] unpadded;
24 private final long[] uncompressed;
25
26 // Uncompressed size of the largest Block. It is used by
27 // SeekableXZInputStream to find out the largest Block of the .xz file.
28 private long largestBlockSize = 0;
29
30 // Offsets relative to the beginning of the .xz file. These are all zero
31 // for the first Stream in the file.
32 private int recordOffset = 0;
33 private long compressedOffset = 0;
34 private long uncompressedOffset = 0;
35
36 public IndexDecoder(SeekableInputStream in, StreamFlags streamFooterFlags,
37 long streamPadding, int memoryLimit)
38 throws IOException {
39 super(new CorruptedInputException("XZ Index is corrupt"));
40 this.streamFlags = streamFooterFlags;
41 this.streamPadding = streamPadding;
42
43 // If endPos is exceeded before the CRC32 field has been decoded,
44 // the Index is corrupt.
45 long endPos = in.position() + streamFooterFlags.backwardSize - 4;
46
47 java.util.zip.CRC32 crc32 = new java.util.zip.CRC32();
48 CheckedInputStream inChecked = new CheckedInputStream(in, crc32);
49
50 // Index Indicator
51 if (inChecked.read() != 0x00)
52 throw new CorruptedInputException("XZ Index is corrupt");
53
54 try {
55 // Number of Records
56 long count = DecoderUtil.decodeVLI(inChecked);
57
58 // Catch Record counts that obviously too high to be valid.
59 // This test isn't exact because it ignores Index Indicator,
60 // Number of Records, and CRC32 fields, but this is good enough
61 // to catch the most obvious problems.
62 if (count >= streamFooterFlags.backwardSize / 2)
63 throw new CorruptedInputException("XZ Index is corrupt");
64
65 // If the Record count doesn't fit into an int, we cannot
66 // allocate the arrays to hold the Records.
67 if (count > Integer.MAX_VALUE)
68 throw new UnsupportedOptionsException("XZ Index has over "
69 + Integer.MAX_VALUE + " Records");
70
71 // Calculate approximate memory requirements and check the
72 // memory usage limit.
73 memoryUsage = 1 + (int)((16L * count + 1023) / 1024);
74 if (memoryLimit >= 0 && memoryUsage > memoryLimit)
75 throw new MemoryLimitException(memoryUsage, memoryLimit);
76
77 // Allocate the arrays for the Records.
78 unpadded = new long[(int)count];
79 uncompressed = new long[(int)count];
80 int record = 0;
81
82 // Decode the Records.
83 for (int i = (int)count; i > 0; --i) {
84 // Get the next Record.
85 long unpaddedSize = DecoderUtil.decodeVLI(inChecked);
86 long uncompressedSize = DecoderUtil.decodeVLI(inChecked);
87
88 // Check that the input position stays sane. Since this is
89 // checked only once per loop iteration instead of for
90 // every input byte read, it's still possible that
91 // EOFException gets thrown with corrupt input.
92 if (in.position() > endPos)
93 throw new CorruptedInputException("XZ Index is corrupt");
94
95 // Add the new Record.
96 unpadded[record] = blocksSum + unpaddedSize;
97 uncompressed[record] = uncompressedSum + uncompressedSize;
98 ++record;
99 super.add(unpaddedSize, uncompressedSize);
100 assert record == recordCount;
101
102 // Remember the uncompressed size of the largest Block.
103 if (largestBlockSize < uncompressedSize)
104 largestBlockSize = uncompressedSize;
105 }
106 } catch (EOFException e) {
107 // EOFException is caught just in case a corrupt input causes
108 // DecoderUtil.decodeVLI to read too much at once.
109 throw new CorruptedInputException("XZ Index is corrupt");
110 }
111
112 // Validate that the size of the Index field matches
113 // Backward Size.
114 int indexPaddingSize = getIndexPaddingSize();
115 if (in.position() + indexPaddingSize != endPos)
116 throw new CorruptedInputException("XZ Index is corrupt");
117
118 // Index Padding
119 while (indexPaddingSize-- > 0)
120 if (inChecked.read() != 0x00)
121 throw new CorruptedInputException("XZ Index is corrupt");
122
123 // CRC32
124 long value = crc32.getValue();
125 for (int i = 0; i < 4; ++i)
126 if (((value >>> (i * 8)) & 0xFF) != in.read())
127 throw new CorruptedInputException("XZ Index is corrupt");
128 }
129
130 public void setOffsets(IndexDecoder prev) {
131 // NOTE: SeekableXZInputStream checks that the total number of Blocks
132 // in concatenated Streams fits into an int.
133 recordOffset = prev.recordOffset + (int)prev.recordCount;
134 compressedOffset = prev.compressedOffset
135 + prev.getStreamSize() + prev.streamPadding;
136 assert (compressedOffset & 3) == 0;
137 uncompressedOffset = prev.uncompressedOffset + prev.uncompressedSum;
138 }
139
140 public int getMemoryUsage() {
141 return memoryUsage;
142 }
143
144 public StreamFlags getStreamFlags() {
145 return streamFlags;
146 }
147
148 public int getRecordCount() {
149 // It was already checked in the constructor that it fits into an int.
150 // Otherwise we couldn't have allocated the arrays.
151 return (int)recordCount;
152 }
153
154 public long getUncompressedSize() {
155 return uncompressedSum;
156 }
157
158 public long getLargestBlockSize() {
159 return largestBlockSize;
160 }
161
162 public boolean hasUncompressedOffset(long pos) {
163 return pos >= uncompressedOffset
164 && pos < uncompressedOffset + uncompressedSum;
165 }
166
167 public boolean hasRecord(int blockNumber) {
168 return blockNumber >= recordOffset
169 && blockNumber < recordOffset + recordCount;
170 }
171
172 public void locateBlock(BlockInfo info, long target) {
173 assert target >= uncompressedOffset;
174 target -= uncompressedOffset;
175 assert target < uncompressedSum;
176
177 int left = 0;
178 int right = unpadded.length - 1;
179
180 while (left < right) {
181 int i = left + (right - left) / 2;
182
183 if (uncompressed[i] <= target)
184 left = i + 1;
185 else
186 right = i;
187 }
188
189 setBlockInfo(info, recordOffset + left);
190 }
191
192 public void setBlockInfo(BlockInfo info, int blockNumber) {
193 // The caller has checked that the given Block number is inside
194 // this Index.
195 assert blockNumber >= recordOffset;
196 assert blockNumber - recordOffset < recordCount;
197
198 info.index = this;
199 info.blockNumber = blockNumber;
200
201 int pos = blockNumber - recordOffset;
202
203 if (pos == 0) {
204 info.compressedOffset = 0;
205 info.uncompressedOffset = 0;
206 } else {
207 info.compressedOffset = (unpadded[pos - 1] + 3) & ~3;
208 info.uncompressedOffset = uncompressed[pos - 1];
209 }
210
211 info.unpaddedSize = unpadded[pos] - info.compressedOffset;
212 info.uncompressedSize = uncompressed[pos] - info.uncompressedOffset;
213
214 info.compressedOffset += compressedOffset
215 + DecoderUtil.STREAM_HEADER_SIZE;
216 info.uncompressedOffset += uncompressedOffset;
217 }
218 }