| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package org.tukaani.xz.common; |
| 6 |
|
| 7 |
import java.io.InputStream; |
| 8 |
import java.io.IOException; |
| 9 |
import java.io.EOFException; |
| 10 |
import java.util.zip.CRC32; |
| 11 |
import org.tukaani.xz.XZ; |
| 12 |
import org.tukaani.xz.XZFormatException; |
| 13 |
import org.tukaani.xz.CorruptedInputException; |
| 14 |
import org.tukaani.xz.UnsupportedOptionsException; |
| 15 |
|
| 16 |
public class DecoderUtil extends Util { |
| 17 |
public static boolean isCRC32Valid(byte[] buf, int off, int len, |
| 18 |
int ref_off) { |
| 19 |
CRC32 crc32 = new CRC32(); |
| 20 |
crc32.update(buf, off, len); |
| 21 |
long value = crc32.getValue(); |
| 22 |
|
| 23 |
for (int i = 0; i < 4; ++i) |
| 24 |
if ((byte)(value >>> (i * 8)) != buf[ref_off + i]) |
| 25 |
return false; |
| 26 |
|
| 27 |
return true; |
| 28 |
} |
| 29 |
|
| 30 |
public static StreamFlags decodeStreamHeader(byte[] buf) |
| 31 |
throws IOException { |
| 32 |
for (int i = 0; i < XZ.HEADER_MAGIC.length; ++i) |
| 33 |
if (buf[i] != XZ.HEADER_MAGIC[i]) |
| 34 |
throw new XZFormatException(); |
| 35 |
|
| 36 |
if (!isCRC32Valid(buf, XZ.HEADER_MAGIC.length, 2, |
| 37 |
XZ.HEADER_MAGIC.length + 2)) |
| 38 |
throw new CorruptedInputException("XZ Stream Header is corrupt"); |
| 39 |
|
| 40 |
try { |
| 41 |
return decodeStreamFlags(buf, XZ.HEADER_MAGIC.length); |
| 42 |
} catch (UnsupportedOptionsException e) { |
| 43 |
throw new UnsupportedOptionsException( |
| 44 |
"Unsupported options in XZ Stream Header"); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
public static StreamFlags decodeStreamFooter(byte[] buf) |
| 49 |
throws IOException { |
| 50 |
if (buf[10] != XZ.FOOTER_MAGIC[0] || buf[11] != XZ.FOOTER_MAGIC[1]) { |
| 51 |
|
| 52 |
|
| 53 |
throw new CorruptedInputException("XZ Stream Footer is corrupt"); |
| 54 |
} |
| 55 |
|
| 56 |
if (!isCRC32Valid(buf, 4, 6, 0)) |
| 57 |
throw new CorruptedInputException("XZ Stream Footer is corrupt"); |
| 58 |
|
| 59 |
StreamFlags streamFlags; |
| 60 |
try { |
| 61 |
streamFlags = decodeStreamFlags(buf, 8); |
| 62 |
} catch (UnsupportedOptionsException e) { |
| 63 |
throw new UnsupportedOptionsException( |
| 64 |
"Unsupported options in XZ Stream Footer"); |
| 65 |
} |
| 66 |
|
| 67 |
streamFlags.backwardSize = 0; |
| 68 |
for (int i = 0; i < 4; ++i) |
| 69 |
streamFlags.backwardSize |= (buf[i + 4] & 0xFF) << (i * 8); |
| 70 |
|
| 71 |
streamFlags.backwardSize = (streamFlags.backwardSize + 1) * 4; |
| 72 |
|
| 73 |
return streamFlags; |
| 74 |
} |
| 75 |
|
| 76 |
private static StreamFlags decodeStreamFlags(byte[] buf, int off) |
| 77 |
throws UnsupportedOptionsException { |
| 78 |
if (buf[off] != 0x00 || (buf[off + 1] & 0xFF) >= 0x10) |
| 79 |
throw new UnsupportedOptionsException(); |
| 80 |
|
| 81 |
StreamFlags streamFlags = new StreamFlags(); |
| 82 |
streamFlags.checkType = buf[off + 1]; |
| 83 |
|
| 84 |
return streamFlags; |
| 85 |
} |
| 86 |
|
| 87 |
public static boolean areStreamFlagsEqual(StreamFlags a, StreamFlags b) { |
| 88 |
|
| 89 |
return a.checkType == b.checkType; |
| 90 |
} |
| 91 |
|
| 92 |
public static long decodeVLI(InputStream in) throws IOException { |
| 93 |
int b = in.read(); |
| 94 |
if (b == -1) |
| 95 |
throw new EOFException(); |
| 96 |
|
| 97 |
long num = b & 0x7F; |
| 98 |
int i = 0; |
| 99 |
|
| 100 |
while ((b & 0x80) != 0x00) { |
| 101 |
if (++i >= VLI_SIZE_MAX) |
| 102 |
throw new CorruptedInputException(); |
| 103 |
|
| 104 |
b = in.read(); |
| 105 |
if (b == -1) |
| 106 |
throw new EOFException(); |
| 107 |
|
| 108 |
if (b == 0x00) |
| 109 |
throw new CorruptedInputException(); |
| 110 |
|
| 111 |
num |= (long)(b & 0x7F) << (i * 7); |
| 112 |
} |
| 113 |
|
| 114 |
return num; |
| 115 |
} |
| 116 |
} |