| 1 |
|
| 2 |
|
| 3 |
|
| 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 |
|
| 23 |
private final long[] unpadded; |
| 24 |
private final long[] uncompressed; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
private long largestBlockSize = 0; |
| 29 |
|
| 30 |
|
| 31 |
|
| 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 |
|
| 44 |
|
| 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 |
|
| 51 |
if (inChecked.read() != 0x00) |
| 52 |
throw new CorruptedInputException("XZ Index is corrupt"); |
| 53 |
|
| 54 |
try { |
| 55 |
|
| 56 |
long count = DecoderUtil.decodeVLI(inChecked); |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
if (count >= streamFooterFlags.backwardSize / 2) |
| 63 |
throw new CorruptedInputException("XZ Index is corrupt"); |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
if (count > Integer.MAX_VALUE) |
| 68 |
throw new UnsupportedOptionsException("XZ Index has over " |
| 69 |
+ Integer.MAX_VALUE + " Records"); |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
memoryUsage = 1 + (int)((16L * count + 1023) / 1024); |
| 74 |
if (memoryLimit >= 0 && memoryUsage > memoryLimit) |
| 75 |
throw new MemoryLimitException(memoryUsage, memoryLimit); |
| 76 |
|
| 77 |
|
| 78 |
unpadded = new long[(int)count]; |
| 79 |
uncompressed = new long[(int)count]; |
| 80 |
int record = 0; |
| 81 |
|
| 82 |
|
| 83 |
for (int i = (int)count; i > 0; --i) { |
| 84 |
|
| 85 |
long unpaddedSize = DecoderUtil.decodeVLI(inChecked); |
| 86 |
long uncompressedSize = DecoderUtil.decodeVLI(inChecked); |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
if (in.position() > endPos) |
| 93 |
throw new CorruptedInputException("XZ Index is corrupt"); |
| 94 |
|
| 95 |
|
| 96 |
unpadded[record] = blocksSum + unpaddedSize; |
| 97 |
uncompressed[record] = uncompressedSum + uncompressedSize; |
| 98 |
++record; |
| 99 |
super.add(unpaddedSize, uncompressedSize); |
| 100 |
assert record == recordCount; |
| 101 |
|
| 102 |
|
| 103 |
if (largestBlockSize < uncompressedSize) |
| 104 |
largestBlockSize = uncompressedSize; |
| 105 |
} |
| 106 |
} catch (EOFException e) { |
| 107 |
|
| 108 |
|
| 109 |
throw new CorruptedInputException("XZ Index is corrupt"); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
int indexPaddingSize = getIndexPaddingSize(); |
| 115 |
if (in.position() + indexPaddingSize != endPos) |
| 116 |
throw new CorruptedInputException("XZ Index is corrupt"); |
| 117 |
|
| 118 |
|
| 119 |
while (indexPaddingSize-- > 0) |
| 120 |
if (inChecked.read() != 0x00) |
| 121 |
throw new CorruptedInputException("XZ Index is corrupt"); |
| 122 |
|
| 123 |
|
| 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 |
|
| 132 |
|
| 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 |
|
| 150 |
|
| 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 |
|
| 194 |
|
| 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 |
} |