added files.
This commit is contained in:
commit
b9a4fa717b
84 files changed
+7967
No files matched your search
@@ -0,0 +1,9 @@
|
||||
package dev.jnic.loader;
|
||||
|
||||
final class BitTreeDecoder extends LiteralModel {
|
||||
final DistanceDecoder parent;
|
||||
|
||||
BitTreeDecoder(DistanceDecoder parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package dev.jnic.loader;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public final class ByteBufferRangeSource extends RangeDecoder {
|
||||
public final byte[] buf = new byte[65531];
|
||||
public int pos = this.buf.length;
|
||||
|
||||
@Override
|
||||
public void normalize() throws IOException {
|
||||
if ((this.range & 0xFF000000) == 0) {
|
||||
try {
|
||||
this.code = (this.code << 8) | (this.buf[this.pos++] & 0xFF);
|
||||
this.range <<= 8;
|
||||
} catch (ArrayIndexOutOfBoundsException e) {
|
||||
throw new IOException();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package dev.jnic.loader;
|
||||
|
||||
final class DistanceDecoder extends DistanceParams {
|
||||
final BitTreeDecoder[] subcoders;
|
||||
final LzmaDecoder decoder;
|
||||
|
||||
DistanceDecoder(LzmaDecoder decoder, int lc, int lp) {
|
||||
super(lc, lp);
|
||||
this.decoder = decoder;
|
||||
this.subcoders = new BitTreeDecoder[1 << (lc + lp)];
|
||||
for (int i = 0; i < this.subcoders.length; ++i) {
|
||||
this.subcoders[i] = new BitTreeDecoder(this);
|
||||
}
|
||||
}
|
||||
|
||||
final void reset() {
|
||||
for (BitTreeDecoder subcoder : this.subcoders) {
|
||||
subcoder.reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package dev.jnic.loader;
|
||||
|
||||
abstract class DistanceParams {
|
||||
final int lc;
|
||||
final int posMask;
|
||||
|
||||
DistanceParams(int lc, int lp) {
|
||||
this.lc = lc;
|
||||
this.posMask = (1 << lp) - 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,358 @@
|
||||
package dev.jnic.loader;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class JNICLoader extends InputStream {
|
||||
public static ByteBuffer z;
|
||||
private String version = "3.7.0";
|
||||
private DataInputStream in;
|
||||
private LzDictionary lz;
|
||||
private ByteBufferRangeSource rc;
|
||||
private LzmaDecoder lzma;
|
||||
private int uncompressedSize = 0;
|
||||
private boolean isLZMAChunk = false;
|
||||
private boolean needDictReset = true;
|
||||
private boolean needProps = true;
|
||||
private boolean endReached = false;
|
||||
private IOException exception = null;
|
||||
private final byte[] tempBuf = new byte[1];
|
||||
|
||||
public JNICLoader(InputStream in) {
|
||||
this(in, 0x1000000, null);
|
||||
}
|
||||
|
||||
public JNICLoader(InputStream in, int dictSize, byte[] presetDict) {
|
||||
if (in == null) {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
if (dictSize < 4096 || dictSize > 0x7FFFFFF0) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
this.in = new DataInputStream(in);
|
||||
this.rc = new ByteBufferRangeSource();
|
||||
this.lz = new LzDictionary((dictSize + 15) & 0xFFFFFFF0, presetDict);
|
||||
if (presetDict != null && presetDict.length > 0) {
|
||||
this.needDictReset = false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
if (this.read(this.tempBuf, 0, 1) == -1) {
|
||||
return -1;
|
||||
}
|
||||
return this.tempBuf[0] & 0xFF;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(byte[] buf, int off, int len) throws IOException {
|
||||
if (off < 0 || len < 0 || off + len < 0 || off + len > buf.length) {
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
if (len == 0) {
|
||||
return 0;
|
||||
}
|
||||
if (this.in == null) {
|
||||
throw new IOException();
|
||||
}
|
||||
if (this.exception != null) {
|
||||
throw this.exception;
|
||||
}
|
||||
if (this.endReached) {
|
||||
return -1;
|
||||
}
|
||||
try {
|
||||
int size = 0;
|
||||
while (len > 0) {
|
||||
if (this.uncompressedSize == 0) {
|
||||
this.decodeChunkHeader();
|
||||
if (this.endReached) {
|
||||
return size == 0 ? -1 : size;
|
||||
}
|
||||
}
|
||||
int copySizeMax = Math.min(this.uncompressedSize, len);
|
||||
if (!this.isLZMAChunk) {
|
||||
int n = Math.min(this.lz.size - this.lz.pos, copySizeMax);
|
||||
this.in.readFully(this.lz.buf, this.lz.pos, n);
|
||||
this.lz.pos += n;
|
||||
if (this.lz.full < this.lz.pos) {
|
||||
this.lz.full = this.lz.pos;
|
||||
}
|
||||
} else {
|
||||
if (this.lz.size - this.lz.pos <= copySizeMax) {
|
||||
this.lz.limit = this.lz.size;
|
||||
} else {
|
||||
this.lz.limit = this.lz.pos + copySizeMax;
|
||||
}
|
||||
this.lzma.decode();
|
||||
}
|
||||
int copied = this.lz.pos - this.lz.start;
|
||||
if (this.lz.pos == this.lz.size) {
|
||||
this.lz.pos = 0;
|
||||
}
|
||||
System.arraycopy(this.lz.buf, this.lz.start, buf, off, copied);
|
||||
this.lz.start = this.lz.pos;
|
||||
off += copied;
|
||||
len -= copied;
|
||||
size += copied;
|
||||
this.uncompressedSize -= copied;
|
||||
if (this.uncompressedSize == 0) {
|
||||
if (this.rc.pos != this.rc.buf.length || this.rc.code != 0 || this.lz.pendingLen > 0) {
|
||||
throw new IOException();
|
||||
}
|
||||
}
|
||||
}
|
||||
return size;
|
||||
} catch (IOException e) {
|
||||
this.exception = e;
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
private void decodeChunkHeader() throws IOException {
|
||||
int control = this.in.readUnsignedByte();
|
||||
if (control == 0) {
|
||||
this.endReached = true;
|
||||
this.releaseResources();
|
||||
return;
|
||||
}
|
||||
if (control >= 224 || control == 1) {
|
||||
this.needProps = true;
|
||||
this.needDictReset = false;
|
||||
this.lz.start = 0;
|
||||
this.lz.pos = 0;
|
||||
this.lz.full = 0;
|
||||
this.lz.limit = 0;
|
||||
this.lz.buf[this.lz.size - 1] = 0;
|
||||
} else if (this.needDictReset) {
|
||||
throw new IOException();
|
||||
}
|
||||
if (control >= 128) {
|
||||
this.isLZMAChunk = true;
|
||||
this.uncompressedSize = (control & 0x1F) << 16;
|
||||
this.uncompressedSize += this.in.readUnsignedShort() + 1;
|
||||
int compressedSize = this.in.readUnsignedShort() + 1;
|
||||
if (control >= 192) {
|
||||
this.needProps = false;
|
||||
this.decodeProps();
|
||||
} else {
|
||||
if (this.needProps) {
|
||||
throw new IOException();
|
||||
}
|
||||
if (control >= 160) {
|
||||
this.lzma.reset();
|
||||
}
|
||||
}
|
||||
if (compressedSize < 5) {
|
||||
throw new IOException();
|
||||
}
|
||||
if (this.in.readUnsignedByte() != 0) {
|
||||
throw new IOException();
|
||||
}
|
||||
this.rc.code = this.in.readInt();
|
||||
this.rc.range = -1;
|
||||
int remaining = compressedSize - 5;
|
||||
this.rc.pos = this.rc.buf.length - remaining;
|
||||
this.in.readFully(this.rc.buf, this.rc.pos, remaining);
|
||||
} else {
|
||||
if (control > 2) {
|
||||
throw new IOException();
|
||||
}
|
||||
this.isLZMAChunk = false;
|
||||
this.uncompressedSize = this.in.readUnsignedShort() + 1;
|
||||
}
|
||||
}
|
||||
|
||||
private void decodeProps() throws IOException {
|
||||
int props = this.in.readUnsignedByte();
|
||||
if (props > 224) {
|
||||
throw new IOException();
|
||||
}
|
||||
int pb = props / 45;
|
||||
props -= pb * 9 * 5;
|
||||
int lp = props / 9;
|
||||
int lc = props - lp * 9;
|
||||
if (lc + lp > 4) {
|
||||
throw new IOException();
|
||||
}
|
||||
this.lzma = new LzmaDecoder(this.lz, this.rc, lc, lp, pb);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int available() throws IOException {
|
||||
if (this.in == null) {
|
||||
throw new IOException("closed");
|
||||
}
|
||||
if (this.exception != null) {
|
||||
throw this.exception;
|
||||
}
|
||||
return this.isLZMAChunk ? this.uncompressedSize : Math.min(this.uncompressedSize, this.in.available());
|
||||
}
|
||||
|
||||
private void releaseResources() {
|
||||
if (this.lz != null) {
|
||||
this.lz = null;
|
||||
this.rc = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
if (this.in != null) {
|
||||
this.releaseResources();
|
||||
try {
|
||||
this.in.close();
|
||||
} finally {
|
||||
this.in = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void init() {
|
||||
}
|
||||
|
||||
private static String platformKey(String os, String arch) {
|
||||
boolean x86 = arch.equals("x86_64") || arch.equals("amd64") || arch.equals("x64");
|
||||
boolean arm = arch.equals("aarch64") || arch.equals("arm64");
|
||||
if (os.contains("win") && x86) return "WINDOWS_X86_64";
|
||||
if (os.contains("win") && arm) return "WINDOWS_AARCH64";
|
||||
if (os.contains("mac") && x86) return "MACOS_X86_64";
|
||||
if (os.contains("mac") && arm) return "MACOS_AARCH64";
|
||||
if (os.contains("lin") && x86) return "LINUX_X86_64";
|
||||
if (os.contains("lin") && arm) return "LINUX_AARCH64";
|
||||
return null;
|
||||
}
|
||||
|
||||
private static long[] readLayout(String platform) {
|
||||
try (InputStream layout = JNICLoader.class.getResourceAsStream("/dev/jnic/lib/layout.txt")) {
|
||||
if (layout == null) return null;
|
||||
String text = new String(layout.readAllBytes(), StandardCharsets.UTF_8);
|
||||
for (String line : text.split("\\R")) {
|
||||
String[] parts = line.trim().split("\\s+");
|
||||
if (parts.length == 3 && parts[0].equals(platform)) {
|
||||
return new long[]{Long.parseLong(parts[1]), Long.parseLong(parts[2])};
|
||||
}
|
||||
}
|
||||
} catch (IOException | RuntimeException ignored) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static {
|
||||
Stream<String> stream = ManagementFactory.getRuntimeMXBean().getInputArguments().stream();
|
||||
List<String> list = Arrays.asList("-javaagent", "-agentlib");
|
||||
Objects.requireNonNull(list);
|
||||
if (!stream.anyMatch(list::contains)) {
|
||||
z = ByteBuffer.allocateDirect(64).order(ByteOrder.LITTLE_ENDIAN);
|
||||
String os = System.getProperty("os.name").toLowerCase();
|
||||
String arch = System.getProperty("os.arch").toLowerCase();
|
||||
long start = 0L;
|
||||
long end = 0L;
|
||||
String platform = platformKey(os, arch);
|
||||
long[] range = platform == null ? null : readLayout(platform);
|
||||
if (range != null) {
|
||||
start = range[0];
|
||||
end = range[1];
|
||||
}
|
||||
if (range == null && os.contains("lin") && (arch.equals("x86_64") || arch.equals("amd64"))) {
|
||||
start = 22947080L;
|
||||
end = 32608048L;
|
||||
z.putInt(-567463365);
|
||||
z.putInt(1401376760);
|
||||
z.putInt(997729761);
|
||||
z.putInt(1502854057);
|
||||
z.putInt(1456950269);
|
||||
z.putInt(2068781566);
|
||||
z.putInt(-2074230231);
|
||||
z.putInt(-877438771);
|
||||
}
|
||||
if (range == null && os.contains("mac") && arch.equals("aarch64")) {
|
||||
start = 16419456L;
|
||||
end = 22947080L;
|
||||
z.putInt(1014097172);
|
||||
z.putInt(-117690684);
|
||||
z.putInt(820061173);
|
||||
z.putInt(-1688620839);
|
||||
z.putInt(-1273992576);
|
||||
z.putInt(1914204887);
|
||||
z.putInt(-1152264584);
|
||||
z.putInt(-36053678);
|
||||
}
|
||||
if (range == null && os.contains("win") && (arch.equals("x86_64") || arch.equals("amd64"))) {
|
||||
start = 0L;
|
||||
end = 7558656L;
|
||||
z.putInt(-1114421519);
|
||||
z.putInt(457382690);
|
||||
z.putInt(1129719259);
|
||||
z.putInt(1518767796);
|
||||
z.putInt(1901767710);
|
||||
z.putInt(1310480474);
|
||||
z.putInt(1420822585);
|
||||
z.putInt(-1793329926);
|
||||
}
|
||||
if (range == null && os.contains("mac") && (arch.equals("x86_64") || arch.equals("amd64"))) {
|
||||
start = 7558656L;
|
||||
end = 16419456L;
|
||||
z.putInt(1314687260);
|
||||
z.putInt(-478689012);
|
||||
z.putInt(-2086494599);
|
||||
z.putInt(551956295);
|
||||
z.putInt(410610689);
|
||||
z.putInt(-1224114266);
|
||||
z.putInt(-1353435766);
|
||||
z.putInt(-1032801772);
|
||||
}
|
||||
if (start != end) {
|
||||
InputStream resourceStream = JNICLoader.class.getResourceAsStream("/dev/jnic/lib/3273ada3-3474-4e4e-9623-319038d7e4bb.dat");
|
||||
if (resourceStream != null) {
|
||||
File file;
|
||||
try {
|
||||
file = File.createTempFile("lib", null);
|
||||
file.deleteOnExit();
|
||||
if (!file.exists()) {
|
||||
throw new IOException();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new UnsatisfiedLinkError("Failed to create temp file");
|
||||
}
|
||||
byte[] buffer = new byte[2048];
|
||||
try (JNICLoader decoder = new JNICLoader(resourceStream);
|
||||
FileOutputStream out = new FileOutputStream(file)) {
|
||||
long pos = 0L;
|
||||
while (pos < start) {
|
||||
long skipped = decoder.skip(start - pos);
|
||||
if (skipped <= 0L) {
|
||||
throw new IOException("failed to skip: " + pos);
|
||||
}
|
||||
pos += skipped;
|
||||
}
|
||||
while (pos < end) {
|
||||
int n = decoder.read(buffer, 0, (int) Math.min(buffer.length, end - pos));
|
||||
if (n < 0) break;
|
||||
out.write(buffer, 0, n);
|
||||
pos += n;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new UnsatisfiedLinkError("Failed to extract file: " + e.getMessage());
|
||||
}
|
||||
z.putInt(-1731935906);
|
||||
z.putInt(-851835500);
|
||||
z.putInt(-880934708);
|
||||
System.load(file.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package dev.jnic.loader;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
final class LengthDecoder extends LengthModel {
|
||||
private final LzmaDecoder decoder;
|
||||
|
||||
LengthDecoder(LzmaDecoder decoder) {
|
||||
this.decoder = decoder;
|
||||
}
|
||||
|
||||
final int decode(int posState) throws IOException {
|
||||
if (this.decoder.rc.decodeBit(this.choice, 0) == 0) {
|
||||
return this.decoder.rc.decodeBitTree(this.low[posState]) + 2;
|
||||
}
|
||||
if (this.decoder.rc.decodeBit(this.choice, 1) == 0) {
|
||||
return this.decoder.rc.decodeBitTree(this.mid[posState]) + 2 + 8;
|
||||
}
|
||||
return this.decoder.rc.decodeBitTree(this.high) + 2 + 8 + 8;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package dev.jnic.loader;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
abstract class LengthModel {
|
||||
final short[] choice = new short[2];
|
||||
final short[][] low = new short[16][8];
|
||||
final short[][] mid = new short[16][8];
|
||||
final short[] high = new short[256];
|
||||
|
||||
LengthModel() {
|
||||
}
|
||||
|
||||
final void reset() {
|
||||
Arrays.fill(this.choice, (short) 1024);
|
||||
for (short[] row : this.low) {
|
||||
Arrays.fill(row, (short) 1024);
|
||||
}
|
||||
for (short[] row : this.mid) {
|
||||
Arrays.fill(row, (short) 1024);
|
||||
}
|
||||
Arrays.fill(this.high, (short) 1024);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package dev.jnic.loader;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
abstract class LiteralModel {
|
||||
final short[] probs = new short[768];
|
||||
|
||||
LiteralModel() {
|
||||
}
|
||||
|
||||
final void reset() {
|
||||
Arrays.fill(this.probs, (short) 1024);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package dev.jnic.loader;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public final class LzDictionary {
|
||||
public final byte[] buf;
|
||||
public final int size;
|
||||
public int start = 0;
|
||||
public int pos = 0;
|
||||
public int full = 0;
|
||||
public int limit = 0;
|
||||
public int pendingLen = 0;
|
||||
public int pendingDist = 0;
|
||||
|
||||
public LzDictionary(int dictSize, byte[] presetDict) {
|
||||
this.size = dictSize;
|
||||
this.buf = new byte[this.size];
|
||||
if (presetDict != null) {
|
||||
this.pos = Math.min(presetDict.length, dictSize);
|
||||
this.full = this.pos;
|
||||
this.start = this.pos;
|
||||
System.arraycopy(presetDict, presetDict.length - this.pos, this.buf, 0, this.pos);
|
||||
}
|
||||
}
|
||||
|
||||
public int getByte(int dist) {
|
||||
int offset = this.pos - dist - 1;
|
||||
if (dist >= this.pos) {
|
||||
offset += this.size;
|
||||
}
|
||||
return this.buf[offset] & 0xFF;
|
||||
}
|
||||
|
||||
public void repeat(int dist, int len) throws IOException {
|
||||
if (dist < 0 || dist >= this.full) {
|
||||
throw new IOException();
|
||||
}
|
||||
int left = Math.min(this.limit - this.pos, len);
|
||||
this.pendingLen = len - left;
|
||||
this.pendingDist = dist;
|
||||
int back = this.pos - dist - 1;
|
||||
if (back < 0) {
|
||||
assert this.full == this.size;
|
||||
back += this.size;
|
||||
int copySize = Math.min(this.size - back, left);
|
||||
assert copySize <= dist + 1;
|
||||
System.arraycopy(this.buf, back, this.buf, this.pos, copySize);
|
||||
this.pos += copySize;
|
||||
back = 0;
|
||||
left -= copySize;
|
||||
if (left == 0) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
assert back < this.pos;
|
||||
assert left > 0;
|
||||
do {
|
||||
int copySize = Math.min(left, this.pos - back);
|
||||
System.arraycopy(this.buf, back, this.buf, this.pos, copySize);
|
||||
this.pos += copySize;
|
||||
left -= copySize;
|
||||
} while (left > 0);
|
||||
if (this.full < this.pos) {
|
||||
this.full = this.pos;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
package dev.jnic.loader;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public final class LzmaDecoder extends LzmaState {
|
||||
final LzDictionary dict;
|
||||
final RangeDecoder rc;
|
||||
private final DistanceDecoder literalDecoder;
|
||||
private final LengthDecoder matchLenDecoder = new LengthDecoder(this);
|
||||
private final LengthDecoder repLenDecoder = new LengthDecoder(this);
|
||||
|
||||
public LzmaDecoder(LzDictionary dict, RangeDecoder rc, int lc, int lp, int pb) {
|
||||
super(pb);
|
||||
this.dict = dict;
|
||||
this.rc = rc;
|
||||
this.literalDecoder = new DistanceDecoder(this, lc, lp);
|
||||
this.reset();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset() {
|
||||
super.reset();
|
||||
this.literalDecoder.reset();
|
||||
this.matchLenDecoder.reset();
|
||||
this.repLenDecoder.reset();
|
||||
}
|
||||
|
||||
public void decode() throws IOException {
|
||||
if (this.dict.pendingLen > 0) {
|
||||
this.dict.repeat(this.dict.pendingDist, this.dict.pendingLen);
|
||||
}
|
||||
while (this.dict.pos < this.dict.limit) {
|
||||
int posState = this.dict.pos & this.posMask;
|
||||
if (this.rc.decodeBit(this.isMatch[this.state.state], posState) == 0) {
|
||||
this.decodeLiteral();
|
||||
continue;
|
||||
}
|
||||
int len;
|
||||
if (this.rc.decodeBit(this.isRep, this.state.state) == 0) {
|
||||
len = this.decodeMatch(posState);
|
||||
} else {
|
||||
len = this.decodeRepMatch(posState);
|
||||
}
|
||||
this.dict.repeat(this.reps[0], len);
|
||||
}
|
||||
this.rc.normalize();
|
||||
}
|
||||
|
||||
private void decodeLiteral() throws IOException {
|
||||
int i = ((this.dict.getByte(0) >> (8 - this.literalDecoder.lc))
|
||||
+ ((this.dict.pos & this.literalDecoder.posMask) << this.literalDecoder.lc));
|
||||
BitTreeDecoder subcoder = this.literalDecoder.subcoders[i];
|
||||
int symbol = 1;
|
||||
if (this.state.state < 7) {
|
||||
do {
|
||||
symbol = (symbol << 1) | this.rc.decodeBit(subcoder.probs, symbol);
|
||||
} while (symbol < 256);
|
||||
} else {
|
||||
int matchByte = this.dict.getByte(this.reps[0]);
|
||||
int offset = 256;
|
||||
do {
|
||||
matchByte <<= 1;
|
||||
int matchBit = matchByte & offset;
|
||||
int bit = this.rc.decodeBit(subcoder.probs, offset + matchBit + symbol);
|
||||
symbol = (symbol << 1) | bit;
|
||||
offset &= (0 - bit) ^ ~matchBit;
|
||||
} while (symbol < 256);
|
||||
}
|
||||
this.dict.buf[this.dict.pos++] = (byte) symbol;
|
||||
if (this.dict.full < this.dict.pos) {
|
||||
this.dict.full = this.dict.pos;
|
||||
}
|
||||
if (this.state.state <= 3) {
|
||||
this.state.state = 0;
|
||||
} else if (this.state.state <= 9) {
|
||||
this.state.state -= 3;
|
||||
} else {
|
||||
this.state.state -= 6;
|
||||
}
|
||||
}
|
||||
|
||||
private int decodeMatch(int posState) throws IOException {
|
||||
this.state.state = this.state.state < 7 ? 7 : 10;
|
||||
this.reps[3] = this.reps[2];
|
||||
this.reps[2] = this.reps[1];
|
||||
this.reps[1] = this.reps[0];
|
||||
int len = this.matchLenDecoder.decode(posState);
|
||||
int distSlot = this.rc.decodeBitTree(this.distSlots[len < 6 ? len - 2 : 3]);
|
||||
if (distSlot < 4) {
|
||||
this.reps[0] = distSlot;
|
||||
} else {
|
||||
int limit = (distSlot >> 1) - 1;
|
||||
this.reps[0] = (2 | (distSlot & 1)) << limit;
|
||||
if (distSlot < 14) {
|
||||
this.reps[0] |= this.rc.decodeReverseBitTree(this.distSpecial[distSlot - 4]);
|
||||
} else {
|
||||
int count = limit - 4;
|
||||
int directBits = 0;
|
||||
do {
|
||||
this.rc.normalize();
|
||||
this.rc.range >>>= 1;
|
||||
int t = (this.rc.code - this.rc.range) >>> 31;
|
||||
this.rc.code -= this.rc.range & (t - 1);
|
||||
directBits = (directBits << 1) | (1 - t);
|
||||
} while (--count != 0);
|
||||
this.reps[0] |= directBits << 4;
|
||||
this.reps[0] |= this.rc.decodeReverseBitTree(this.distAlign);
|
||||
}
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
private int decodeRepMatch(int posState) throws IOException {
|
||||
if (this.rc.decodeBit(this.isRep0, this.state.state) == 0) {
|
||||
if (this.rc.decodeBit(this.isRep0Long[this.state.state], posState) == 0) {
|
||||
this.state.state = this.state.state < 7 ? 9 : 11;
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
int dist;
|
||||
if (this.rc.decodeBit(this.isRep1, this.state.state) == 0) {
|
||||
dist = this.reps[1];
|
||||
} else {
|
||||
if (this.rc.decodeBit(this.isRep2, this.state.state) == 0) {
|
||||
dist = this.reps[2];
|
||||
} else {
|
||||
dist = this.reps[3];
|
||||
this.reps[3] = this.reps[2];
|
||||
}
|
||||
this.reps[2] = this.reps[1];
|
||||
}
|
||||
this.reps[1] = this.reps[0];
|
||||
this.reps[0] = dist;
|
||||
}
|
||||
this.state.state = this.state.state < 7 ? 8 : 11;
|
||||
return this.repLenDecoder.decode(posState);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package dev.jnic.loader;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
abstract class LzmaState {
|
||||
final int posMask;
|
||||
final int[] reps = new int[4];
|
||||
final LzmaStateBox state = new LzmaStateBox();
|
||||
final short[][] isMatch = new short[12][16];
|
||||
final short[] isRep = new short[12];
|
||||
final short[] isRep0 = new short[12];
|
||||
final short[] isRep1 = new short[12];
|
||||
final short[] isRep2 = new short[12];
|
||||
final short[][] isRep0Long = new short[12][16];
|
||||
final short[][] distSlots = new short[4][64];
|
||||
final short[][] distSpecial = new short[][]{
|
||||
new short[2], new short[2], new short[4], new short[4],
|
||||
new short[8], new short[8], new short[16], new short[16],
|
||||
new short[32], new short[32]
|
||||
};
|
||||
final short[] distAlign = new short[16];
|
||||
|
||||
LzmaState(int pb) {
|
||||
this.posMask = (1 << pb) - 1;
|
||||
}
|
||||
|
||||
void reset() {
|
||||
Arrays.fill(this.reps, 0);
|
||||
this.state.state = 0;
|
||||
for (short[] row : this.isMatch) {
|
||||
Arrays.fill(row, (short) 1024);
|
||||
}
|
||||
Arrays.fill(this.isRep, (short) 1024);
|
||||
Arrays.fill(this.isRep0, (short) 1024);
|
||||
Arrays.fill(this.isRep1, (short) 1024);
|
||||
Arrays.fill(this.isRep2, (short) 1024);
|
||||
for (short[] row : this.isRep0Long) {
|
||||
Arrays.fill(row, (short) 1024);
|
||||
}
|
||||
for (short[] row : this.distSlots) {
|
||||
Arrays.fill(row, (short) 1024);
|
||||
}
|
||||
for (short[] row : this.distSpecial) {
|
||||
Arrays.fill(row, (short) 1024);
|
||||
}
|
||||
Arrays.fill(this.distAlign, (short) 1024);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package dev.jnic.loader;
|
||||
|
||||
final class LzmaStateBox {
|
||||
int state;
|
||||
|
||||
LzmaStateBox() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package dev.jnic.loader;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public abstract class RangeDecoder {
|
||||
public int range = 0;
|
||||
public int code = 0;
|
||||
|
||||
public abstract void normalize() throws IOException;
|
||||
|
||||
public final int decodeBit(short[] probs, int index) throws IOException {
|
||||
this.normalize();
|
||||
short prob = probs[index];
|
||||
int bound = (this.range >>> 11) * prob;
|
||||
if ((this.code ^ Integer.MIN_VALUE) < (bound ^ Integer.MIN_VALUE)) {
|
||||
this.range = bound;
|
||||
probs[index] = (short) (prob + ((2048 - prob) >>> 5));
|
||||
return 0;
|
||||
}
|
||||
this.range -= bound;
|
||||
this.code -= bound;
|
||||
probs[index] = (short) (prob - (prob >>> 5));
|
||||
return 1;
|
||||
}
|
||||
|
||||
public final int decodeBitTree(short[] probs) throws IOException {
|
||||
int symbol = 1;
|
||||
do {
|
||||
symbol = (symbol << 1) | this.decodeBit(probs, symbol);
|
||||
} while (symbol < probs.length);
|
||||
return symbol - probs.length;
|
||||
}
|
||||
|
||||
public final int decodeReverseBitTree(short[] probs) throws IOException {
|
||||
int symbol = 1;
|
||||
int i = 0;
|
||||
int result = 0;
|
||||
do {
|
||||
int bit = this.decodeBit(probs, symbol);
|
||||
symbol = (symbol << 1) | bit;
|
||||
result |= bit << i++;
|
||||
} while (symbol < probs.length);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user