Code polish, cleanup

This commit is contained in:
nathan 2015-11-22 21:17:11 +01:00
parent c0c76c266f
commit d2fdd8865f
7 changed files with 66 additions and 51 deletions

View File

@ -15,12 +15,12 @@
*/ */
package dorkbox.util.cab; package dorkbox.util.cab;
import java.io.OutputStream;
import dorkbox.util.cab.structure.CabFileEntry; import dorkbox.util.cab.structure.CabFileEntry;
import java.io.OutputStream;
public interface CabStreamSaver { public interface CabStreamSaver {
public abstract OutputStream openOutputStream(CabFileEntry entry); OutputStream openOutputStream(CabFileEntry entry);
public abstract void closeOutputStream(OutputStream outputStream, CabFileEntry entry); void closeOutputStream(OutputStream outputStream, CabFileEntry entry);
public abstract boolean saveReservedAreaData(byte[] data, int dataLength); boolean saveReservedAreaData(byte[] data, int dataLength);
} }

View File

@ -20,8 +20,8 @@ import dorkbox.util.cab.structure.CabConstants;
public interface Decompressor extends CabConstants { public interface Decompressor extends CabConstants {
public abstract void init(int windowBits) throws CabException; void init(int windowBits) throws CabException;
public abstract void decompress(byte[] inputBytes, byte[] outputBytes, int inputLength, int outputLength) throws CabException; void decompress(byte[] inputBytes, byte[] outputBytes, int inputLength, int outputLength) throws CabException;
public abstract int getMaxGrowth(); int getMaxGrowth();
public abstract void reset(int windowBits) throws CabException; void reset(int windowBits) throws CabException;
} }

View File

@ -150,11 +150,11 @@ public final class DecompressLzx implements Decompressor, LZXConstants {
private int decompressLoop(int bytesToRead) throws CabException { private int decompressLoop(int bytesToRead) throws CabException {
int i = bytesToRead; int i = bytesToRead;
int lastWindowPosition = 0; int lastWindowPosition;
int k; int k;
int m; int m;
if (this.readHeader == true) { if (this.readHeader) {
// read header // read header
if (readBits(1) == 1) { if (readBits(1) == 1) {
k = readBits(16); k = readBits(16);
@ -266,6 +266,7 @@ public final class DecompressLzx implements Decompressor, LZXConstants {
return lastWindowPosition; return lastWindowPosition;
} }
@SuppressWarnings("NumericCastThatLosesPrecision")
private void decodeIntelBlock(byte[] bytes, int outLength) { private void decodeIntelBlock(byte[] bytes, int outLength) {
if (outLength <= 6 || !this.intelStarted) { if (outLength <= 6 || !this.intelStarted) {
this.intelCursorPos += outLength; this.intelCursorPos += outLength;
@ -281,7 +282,7 @@ public final class DecompressLzx implements Decompressor, LZXConstants {
// save bytes // save bytes
while (abs_off < 6) { while (abs_off < 6) {
this.savedBytes[abs_off] = bytes[adjustedOutLength + abs_off]; this.savedBytes[abs_off] = bytes[adjustedOutLength + abs_off];
bytes[adjustedOutLength + abs_off] = -24; bytes[adjustedOutLength + abs_off] = (byte) -24;
abs_off++; abs_off++;
} }
@ -383,6 +384,7 @@ public final class DecompressLzx implements Decompressor, LZXConstants {
} while (i < this.wndSize); } while (i < this.wndSize);
} }
@SuppressWarnings("NumericCastThatLosesPrecision")
private void verbatimAlgo(int this_run) throws CorruptCabException { private void verbatimAlgo(int this_run) throws CorruptCabException {
int i = this.windowPosition; int i = this.windowPosition;
int mask = this.windowMask; int mask = this.windowMask;
@ -480,6 +482,7 @@ public final class DecompressLzx implements Decompressor, LZXConstants {
return i; return i;
} }
@SuppressWarnings("NumericCastThatLosesPrecision")
private void alignedAlgo(int this_run) throws CorruptCabException { private void alignedAlgo(int this_run) throws CorruptCabException {
int windowPos = this.windowPosition; int windowPos = this.windowPosition;
int mask = this.windowMask; int mask = this.windowMask;

View File

@ -111,7 +111,8 @@ public final class DecompressZip implements Decompressor {
} }
} }
private void b() throws CabException { @SuppressWarnings({"NumericCastThatLosesPrecision", "Duplicates"})
private void bits() throws CabException {
int i = this.int3; int i = this.int3;
int j = this.outputLength; int j = this.outputLength;
byte[] arrayOfByte1 = this.outputBytes; byte[] arrayOfByte1 = this.outputBytes;
@ -125,6 +126,7 @@ public final class DecompressZip implements Decompressor {
byte[] arrayOfByte3 = this.state2.byteA; byte[] arrayOfByte3 = this.state2.byteA;
int k = this.int1; int k = this.int1;
int m = this.int2; int m = this.int2;
do { do {
if (this.index > this.inputPlus4) { if (this.index > this.inputPlus4) {
break; break;
@ -222,34 +224,39 @@ public final class DecompressZip implements Decompressor {
} }
private void readStuffcommon() throws CabException { private void readStuffcommon() throws CabException {
this.state1.c(); this.state1.main();
this.state2.c(); this.state2.main();
} }
private void c() { private void setup() {
int i = 0; int i = 0;
do { do {
this.state1.byteA[i] = 8; this.state1.byteA[i] = (byte) 8;
i++; i++;
} while (i <= 143); } while (i <= 143);
i = 144; i = 144;
do { do {
this.state1.byteA[i] = 9; this.state1.byteA[i] = (byte) 9;
i++; i++;
} while (i <= 255); } while (i <= 255);
i = 256; i = 256;
do { do {
this.state1.byteA[i] = 7; this.state1.byteA[i] = (byte) 7;
i++; i++;
} while (i <= 279); } while (i <= 279);
i = 280; i = 280;
do { do {
this.state1.byteA[i] = 8; this.state1.byteA[i] = (byte) 8;
i++; i++;
} while (i <= 287); } while (i <= 287);
i = 0; i = 0;
do { do {
this.state2.byteA[i] = 5; this.state2.byteA[i] = (byte) 5;
i++; i++;
} while (i < 32); } while (i < 32);
} }
@ -258,7 +265,8 @@ public final class DecompressZip implements Decompressor {
return byte1 & 0xFF | (byte2 & 0xFF) << 8; return byte1 & 0xFF | (byte2 & 0xFF) << 8;
} }
private void d() throws CabException { @SuppressWarnings("NumericCastThatLosesPrecision")
private void expand() throws CabException {
check(); check();
int i = calc(5) + 257; int i = calc(5) + 257;
int j = calc(5) + 1; int j = calc(5) + 1;
@ -268,9 +276,9 @@ public final class DecompressZip implements Decompressor {
check(); check();
} }
for (int n = k; n < ar3.length; n++) { for (int n = k; n < ar3.length; n++) {
this.state3.byteA[ar3[n]] = 0; this.state3.byteA[ar3[n]] = (byte) 0;
} }
this.state3.c(); this.state3.main();
int m = i + j; int m = i + j;
int n = 0; int n = 0;
while (n < m) { while (n < m) {
@ -299,7 +307,7 @@ public final class DecompressZip implements Decompressor {
throw new CorruptCabException(); throw new CorruptCabException();
} }
for (i3 = 0; i3 < i2; i3++) { for (i3 = 0; i3 < i2; i3++) {
this.bytes[n++] = 0; this.bytes[n++] = (byte) 0;
} }
} else { } else {
i2 = calc(7) + 11; i2 = calc(7) + 11;
@ -307,20 +315,21 @@ public final class DecompressZip implements Decompressor {
throw new CorruptCabException(); throw new CorruptCabException();
} }
for (i3 = 0; i3 < i2; i3++) { for (i3 = 0; i3 < i2; i3++) {
this.bytes[n++] = 0; this.bytes[n++] = (byte) 0;
} }
} }
} }
} }
System.arraycopy(this.bytes, 0, this.state1.byteA, 0, i); System.arraycopy(this.bytes, 0, this.state1.byteA, 0, i);
for (n = i; n < 288; n++) { for (n = i; n < 288; n++) {
this.state1.byteA[n] = 0; this.state1.byteA[n] = (byte) 0;
} }
for (n = 0; n < j; n++) { for (n = 0; n < j; n++) {
this.state2.byteA[n] = this.bytes[n + i]; this.state2.byteA[n] = this.bytes[n + i];
} }
for (n = j; n < 32; n++) { for (n = j; n < 32; n++) {
this.state2.byteA[n] = 0; this.state2.byteA[n] = (byte) 0;
} }
if (this.state1.byteA[256] == 0) { if (this.state1.byteA[256] == 0) {
throw new CorruptCabException(); throw new CorruptCabException();
@ -344,26 +353,26 @@ public final class DecompressZip implements Decompressor {
int i = calc(1); int i = calc(1);
int j = calc(2); int j = calc(2);
if (j == 2) { if (j == 2) {
d(); expand();
readStuffcommon(); readStuffcommon();
b(); bits();
return; return;
} }
if (j == 1) { if (j == 1) {
c(); setup();
readStuffcommon(); readStuffcommon();
b(); bits();
return; return;
} }
if (j == 0) { if (j == 0) {
e(); verify();
return; return;
} }
throw new CabException(); throw new CabException();
} }
private void e() throws CabException { private void verify() throws CabException {
mod(); mod();
if (this.index >= this.inputPlus4) { if (this.index >= this.inputPlus4) {
throw new CorruptCabException(); throw new CorruptCabException();
@ -371,7 +380,8 @@ public final class DecompressZip implements Decompressor {
int i = makeShort(this.inputBytes[this.index], this.inputBytes[this.index + 1]); int i = makeShort(this.inputBytes[this.index], this.inputBytes[this.index + 1]);
int j = makeShort(this.inputBytes[this.index + 2], this.inputBytes[this.index + 3]); int j = makeShort(this.inputBytes[this.index + 2], this.inputBytes[this.index + 3]);
if ((short) i != (short) (j ^ 0xFFFFFFFF)) { //noinspection NumericCastThatLosesPrecision
if ((short) i != (short) (~j)) {
throw new CorruptCabException(); throw new CorruptCabException();
} }

View File

@ -45,7 +45,7 @@ final class DecompressZipState {
this.intA4 = new int[this.intA * 2]; this.intA4 = new int[this.intA * 2];
} }
void c() throws CorruptCabException { void main() throws CorruptCabException {
int[] arrayOfInt1 = new int[17]; int[] arrayOfInt1 = new int[17];
int[] arrayOfInt2 = new int[17]; int[] arrayOfInt2 = new int[17];
int k = 0; int k = 0;

View File

@ -16,39 +16,39 @@
package dorkbox.util.cab.structure; package dorkbox.util.cab.structure;
public interface CabConstants { public interface CabConstants {
public static final int CAB_BLOCK_SIZE = 32768; int CAB_BLOCK_SIZE = 32768;
public static final int CAB_BLOCK_SIZE_THRESH = 32767; int CAB_BLOCK_SIZE_THRESH = 32767;
public static final int COMPRESSION_TYPE_NONE = 0; int COMPRESSION_TYPE_NONE = 0;
public static final int COMPRESSION_TYPE_MSZIP = 1; int COMPRESSION_TYPE_MSZIP = 1;
public static final int COMPRESSION_TYPE_QUANTUM = 2; int COMPRESSION_TYPE_QUANTUM = 2;
public static final int COMPRESSION_TYPE_LZX = 3; int COMPRESSION_TYPE_LZX = 3;
public static final int RESERVED_CFHEADER = 1; int RESERVED_CFHEADER = 1;
public static final int RESERVED_CFFOLDER = 2; int RESERVED_CFFOLDER = 2;
public static final int RESERVED_CFDATA = 3; int RESERVED_CFDATA = 3;
public static final int CAB_PROGRESS_INPUT = 1; int CAB_PROGRESS_INPUT = 1;
/** /**
* FLAG_PREV_CABINET is set if this cabinet file is not the first in a set * FLAG_PREV_CABINET is set if this cabinet file is not the first in a set
* of cabinet files. When this bit is set, the szCabinetPrev and szDiskPrev * of cabinet files. When this bit is set, the szCabinetPrev and szDiskPrev
* fields are present in this CFHEADER. * fields are present in this CFHEADER.
*/ */
static final int FLAG_PREV_CABINET = 0x0001; int FLAG_PREV_CABINET = 0x0001;
/** /**
* FLAG_NEXT_CABINET is set if this cabinet file is not the last in a set of * FLAG_NEXT_CABINET is set if this cabinet file is not the last in a set of
* cabinet files. When this bit is set, the szCabinetNext and szDiskNext * cabinet files. When this bit is set, the szCabinetNext and szDiskNext
* fields are present in this CFHEADER. * fields are present in this CFHEADER.
*/ */
static final int FLAG_NEXT_CABINET = 0x0002; int FLAG_NEXT_CABINET = 0x0002;
/** /**
* FLAG_RESERVE_PRESENT is set if this cabinet file contains any reserved * FLAG_RESERVE_PRESENT is set if this cabinet file contains any reserved
* fields. When this bit is set, the cbCFHeader, cbCFFolder, and cbCFData * fields. When this bit is set, the cbCFHeader, cbCFFolder, and cbCFData
* fields are present in this CFHEADER. * fields are present in this CFHEADER.
*/ */
static final int FLAG_RESERVE_PRESENT = 0x0004; int FLAG_RESERVE_PRESENT = 0x0004;
} }

View File

@ -15,16 +15,18 @@
*/ */
package dorkbox.util.cab.structure; package dorkbox.util.cab.structure;
import dorkbox.util.bytes.LittleEndian;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import dorkbox.util.bytes.LittleEndian;
public final class CabFolderEntry implements CabConstants { public final class CabFolderEntry implements CabConstants {
/** offset of the first CFDATA block in this folder, 4bytes */ /** offset of the first CFDATA block in this folder, 4bytes */
public long coffCabStart; public long coffCabStart;
/** number of CFDATA blocks in this folder, 2bytes */ /** number of CFDATA blocks in this folder, 2bytes */
public int cCFData; public int cCFData;
/** compression type indicator , 2bytes */ /** compression type indicator , 2bytes */
public int compressionMethod = 0; public int compressionMethod = 0;