Skip to content

Instantly share code, notes, and snippets.

@Vulcanacht
Created November 28, 2015 02:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vulcanacht/61bf95d88f93e5d2609f to your computer and use it in GitHub Desktop.
Save Vulcanacht/61bf95d88f93e5d2609f to your computer and use it in GitHub Desktop.
protected byte[] unpackMetadata(byte[] data) {
Buffer buffer = new Buffer(data);
int compression = buffer.g1();
int inSize = buffer.g4();
switch (compression) {
case 0: { //No compression
final byte[] output = new byte[inSize];
buffer.gdata(output, 0, inSize);
return output;
}
case 1: { //BZIP2 Compression
int outSize = buffer.g4();
byte[] output = new byte[outSize];
BZ2InputStream.decompress(output, outSize, data, 9);
return output;
}
case 2: { //GZIP Compression
int outSize = buffer.g4();
byte[] output = new byte[outSize];
new GZipDeflater().deflate(buffer, output);
return output;
}
default:
throw new RuntimeException("Unknown Compression Type: " + compression);
}
}
protected void decodeMetadata(byte[] packed) {
final Buffer buffer = new Buffer(unpackMetadata(packed));
final int protocol = buffer.g1();
if (protocol < 5 || protocol > 7)
throw new RuntimeException("Incorrect JS5 protocol number: ");
if (protocol >= 6) {
int indexVersion = buffer.g4();
}
final int identifierFlag = buffer.g1();
if (protocol >= 7) {
this.entryCount = buffer.gsmarti();
} else {
this.entryCount = buffer.g2();
}
int offset = 0;
int largestGroupID = -1;
this.entry_group_id = new int[this.entryCount];
if (protocol >= 7) {
for (int e = 0; e < (this.entryCount); e++) {
this.entry_group_id[e] = offset += buffer.gsmarti();
if (this.entry_group_id[e] > largestGroupID) {
largestGroupID = this.entry_group_id[e];
}
}
} else {
for (int i_5_ = 0; i_5_ < (this.entryCount); i_5_++) {
this.entry_group_id[i_5_] = offset += buffer.g2();
if (this.entry_group_id[i_5_] > largestGroupID) {
largestGroupID = this.entry_group_id[i_5_];
}
}
}
this.file_crc_values = new int[largestGroupID + 1];
this.file_version_values = new int[largestGroupID + 1];
this.file_counts = new int[largestGroupID + 1];
this.file_cache_indices = new int[largestGroupID + 1][];
this.group_data_buffers = new Object[largestGroupID + 1];
this.file_data_buffers = new Object[largestGroupID + 1][];
if (identifierFlag != 0) {
this.group_name_hashes = new int[largestGroupID + 1];
for (int i_6_ = 0; i_6_ < (this.entryCount); i_6_++) {
this.group_name_hashes[(this.entry_group_id[i_6_])] = buffer.g4();
}
this.nameHashTable = new NameHashTable(this.group_name_hashes);
}
for (int crc = 0; crc < (this.entryCount); crc++) {
this.file_crc_values[(this.entry_group_id[crc])] = buffer.g4();
}
for (int version = 0; version < (this.entryCount); version++) {
this.file_version_values[(this.entry_group_id[version])] = buffer.g4();
}
for (int child = 0; child < (this.entryCount); child++) {
this.file_counts[(this.entry_group_id[child])] = buffer.g2();
}
if (protocol >= 7) {
for (int entry = 0; entry < (this.entryCount); entry++) {
final int index = this.entry_group_id[entry];
final int children = this.file_counts[index];
offset = 0;
int length = -1;
this.file_cache_indices[index] = new int[children];
for (int child = 0; child < children; child++) {
final int size = (this.file_cache_indices[index][child] = offset += buffer.gsmarti());
if (size > length) {
length = size;
}
}
this.file_data_buffers[index] = new Object[length + 1];
}
} else {
for (int i_16_ = 0; i_16_ < (this.entryCount); i_16_++) {
final int index = this.entry_group_id[i_16_];
final int children = this.file_counts[index];
offset = 0;
int i_19_ = -1;
this.file_cache_indices[index] = new int[children];
for (int i_20_ = 0; i_20_ < children; i_20_++) {
final int i_21_ = (this.file_cache_indices[index][i_20_] = offset += buffer.g2());
if (i_21_ > i_19_) {
i_19_ = i_21_;
}
}
this.file_data_buffers[index] = new Object[1 + i_19_];
}
}
if (identifierFlag != 0) {
this.group_child_names_raw = new int[largestGroupID + 1][];
this.group_file_names = new NameHashTable[largestGroupID + 1];
for (int entry = 0; entry < (this.entryCount); entry++) {
final int groupId = this.entry_group_id[entry];
final int entryCount = this.file_counts[groupId];
this.group_child_names_raw[groupId] = (new int[this.file_data_buffers[groupId].length]);
for (int fileEntry = 0; fileEntry < entryCount; fileEntry++) {
this.group_child_names_raw[groupId][this.file_cache_indices[groupId][fileEntry]] = buffer.g4();
}
this.group_file_names[groupId] = new NameHashTable(this.group_child_names_raw[groupId]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment