Skip to content

Commit 1b3f4d9

Browse files
committed
refactor: rename createHuffmanDecodingTables
1 parent ff5ab42 commit 1b3f4d9

File tree

2 files changed

+40
-29
lines changed

2 files changed

+40
-29
lines changed

src/main/java/org/runejs/client/cache/bzip/BZip.java

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public static void decompress(BZipContext bzip2Context) {
113113
else
114114
curr--;
115115
} while (true);
116-
bzip2Context.aByteArrayArray822[t][i] = (byte) curr;
116+
bzip2Context.codeLengths[t][i] = (byte) curr;
117117
}
118118

119119
}
@@ -122,14 +122,14 @@ public static void decompress(BZipContext bzip2Context) {
122122
byte minLen = 32;
123123
int maxLen = 0;
124124
for (int i = 0; i < alphaSize; i++) {
125-
if (bzip2Context.aByteArrayArray822[t][i] > maxLen)
126-
maxLen = bzip2Context.aByteArrayArray822[t][i];
127-
if (bzip2Context.aByteArrayArray822[t][i] < minLen)
128-
minLen = bzip2Context.aByteArrayArray822[t][i];
125+
if (bzip2Context.codeLengths[t][i] > maxLen)
126+
maxLen = bzip2Context.codeLengths[t][i];
127+
if (bzip2Context.codeLengths[t][i] < minLen)
128+
minLen = bzip2Context.codeLengths[t][i];
129129
}
130130

131-
method294(bzip2Context.limit[t], bzip2Context.base[t], bzip2Context.perm[t],
132-
bzip2Context.aByteArrayArray822[t], minLen, maxLen, alphaSize);
131+
createHuffmanDecodingTables(bzip2Context.limit[t], bzip2Context.base[t], bzip2Context.perm[t],
132+
bzip2Context.codeLengths[t], minLen, maxLen, alphaSize);
133133
bzip2Context.minLens[t] = minLen;
134134
}
135135

@@ -445,31 +445,42 @@ public static byte getBit(BZipContext context) {
445445
return (byte) getBits(1, context);
446446
}
447447

448-
public static void method294(int[] arg0, int[] arg1, int[] arg2, byte[] arg3, int arg4, int arg5, int arg6) {
448+
private static void createHuffmanDecodingTables(int[] limit, int[] base, int[] perm, byte[] codeLengths, int minLen, int maxLen, int alphabetSize) {
449449
int i = 0;
450-
for(int i_84_ = arg4; i_84_ <= arg5; i_84_++) {
451-
for(int i_85_ = 0; i_85_ < arg6; i_85_++) {
452-
if(arg3[i_85_] == i_84_) {
453-
arg2[i] = i_85_;
450+
451+
// Populate the mapping from canonical code index to output symbol
452+
for(int len = minLen; len <= maxLen; len++) {
453+
for(int symIndex = 0; symIndex < alphabetSize; symIndex++) {
454+
if(codeLengths[symIndex] == len) {
455+
perm[i] = symIndex;
454456
i++;
455457
}
456458
}
457459
}
458-
for(int i_86_ = 0; i_86_ < 23; i_86_++)
459-
arg1[i_86_] = 0;
460-
for(int i_87_ = 0; i_87_ < arg6; i_87_++)
461-
arg1[arg3[i_87_] + 1]++;
462-
for(int i_88_ = 1; i_88_ < 23; i_88_++)
463-
arg1[i_88_] += arg1[i_88_ - 1];
464-
for(int i_89_ = 0; i_89_ < 23; i_89_++)
465-
arg0[i_89_] = 0;
466-
int i_90_ = 0;
467-
for(int i_91_ = arg4; i_91_ <= arg5; i_91_++) {
468-
i_90_ += arg1[i_91_ + 1] - arg1[i_91_];
469-
arg0[i_91_] = i_90_ - 1;
470-
i_90_ <<= 1;
460+
461+
// reset base arrays
462+
for(int index = 0; index < 23; index++)
463+
base[index] = 0;
464+
for(int index = 0; index < alphabetSize; index++)
465+
base[codeLengths[index] + 1]++;
466+
for(int index = 1; index < 23; index++)
467+
base[index] += base[index - 1];
468+
469+
// reset limit array
470+
for(int index = 0; index < 23; index++)
471+
limit[index] = 0;
472+
473+
// Calculate the first and last Huffman code for each code length (codes at a given
474+
// length are sequential in value)
475+
int code = 0;
476+
for(int len = minLen; len <= maxLen; len++) {
477+
code += base[len + 1] - base[len];
478+
limit[len] = code - 1;
479+
code <<= 1;
471480
}
472-
for(int i_92_ = arg4 + 1; i_92_ <= arg5; i_92_++)
473-
arg1[i_92_] = (arg0[i_92_ - 1] + 1 << 1) - arg1[i_92_];
481+
482+
// Update base array with appropriate values for decoding
483+
for(int len = minLen + 1; len <= maxLen; len++)
484+
base[len] = (limit[len - 1] + 1 << 1) - base[len];
474485
}
475486
}

src/main/java/org/runejs/client/cache/bzip/BZipContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class BZipContext {
1515
public int[] mtfbase = new int[16];
1616
public byte stateOutCh;
1717
public int tPos;
18-
public byte[][] aByteArrayArray822;
18+
public byte[][] codeLengths;
1919
public int anInt823;
2020
public boolean[] inUse16;
2121
public int anInt825;
@@ -41,7 +41,7 @@ public BZipContext() {
4141
anInt825 = 0;
4242
inUse16 = new boolean[16];
4343
cftab = new int[257];
44-
aByteArrayArray822 = new byte[6][258];
44+
codeLengths = new byte[6][258];
4545
nextIn = 0;
4646
minLens = new int[6];
4747
perm = new int[6][258];

0 commit comments

Comments
 (0)