Skip to content

Commit 13715a2

Browse files
authored
Merge pull request #177 from runejs/bz2-renaming
refactor: renaming for Bzip2
2 parents 22ad114 + 9878964 commit 13715a2

File tree

2 files changed

+123
-112
lines changed

2 files changed

+123
-112
lines changed

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

Lines changed: 121 additions & 110 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

@@ -299,7 +299,7 @@ else if (nextSym == 1)
299299
bzip2Context.tPos >>= 8;
300300
bzip2Context.nBlockUsed++;
301301
bzip2Context.nBlock = nblock;
302-
method291(bzip2Context);
302+
finalDecompressionStep(bzip2Context);
303303
flag19 = bzip2Context.nBlockUsed == bzip2Context.nBlock + 1 && bzip2Context.stateOutLen == 0;
304304
}
305305
}
@@ -335,141 +335,152 @@ public static void makeMaps(BZipContext context) {
335335
}
336336
}
337337

338-
public static void method291(BZipContext arg0) {
339-
byte i = arg0.stateOutCh;
340-
int i_0_ = arg0.stateOutLen;
341-
int i_1_ = arg0.nBlockUsed;
342-
int i_2_ = arg0.anInt811;
343-
int[] is = BZipContext.tt;
344-
int i_3_ = arg0.tPos;
345-
byte[] is_4_ = arg0.aByteArray837;
346-
int i_5_ = arg0.anInt825;
347-
int i_6_ = arg0.anInt823;
348-
int i_7_ = i_6_;
349-
int i_8_ = arg0.nBlock + 1;
350-
while_10_:
338+
public static void finalDecompressionStep(BZipContext bz) {
339+
byte currentByte = bz.stateOutCh;
340+
int remainingBytes = bz.stateOutLen;
341+
int blockUsed = bz.nBlockUsed;
342+
int lastByte = bz.anInt811;
343+
int[] tt = BZipContext.tt;
344+
int tPos = bz.tPos;
345+
byte[] output = bz.aByteArray837;
346+
int outputPos = bz.anInt825;
347+
int outputRemaining = bz.anInt823;
348+
int outputStart = outputRemaining;
349+
int blockEnd = bz.nBlock + 1;
350+
outerLoop:
351351
for(; ; ) {
352-
if(i_0_ > 0) {
352+
if(remainingBytes > 0) {
353353
for(; ; ) {
354-
if(i_6_ == 0)
355-
break while_10_;
356-
if(i_0_ == 1)
354+
if(outputRemaining == 0)
355+
break outerLoop;
356+
if(remainingBytes == 1)
357357
break;
358-
is_4_[i_5_] = i;
359-
i_0_--;
360-
i_5_++;
361-
i_6_--;
358+
output[outputPos] = currentByte;
359+
remainingBytes--;
360+
outputPos++;
361+
outputRemaining--;
362362
}
363-
if(i_6_ == 0) {
364-
i_0_ = 1;
363+
if(outputRemaining == 0) {
364+
remainingBytes = 1;
365365
break;
366366
}
367-
is_4_[i_5_] = i;
368-
i_5_++;
369-
i_6_--;
367+
output[outputPos] = currentByte;
368+
outputPos++;
369+
outputRemaining--;
370370
}
371371
boolean bool = true;
372372
while(bool) {
373373
bool = false;
374-
if(i_1_ == i_8_) {
375-
i_0_ = 0;
376-
break while_10_;
374+
if(blockUsed == blockEnd) {
375+
remainingBytes = 0;
376+
break outerLoop;
377377
}
378-
i = (byte) i_2_;
379-
i_3_ = is[i_3_];
380-
int i_9_ = (byte) (i_3_ & 0xff);
381-
i_3_ >>= 8;
382-
i_1_++;
383-
if(i_9_ != i_2_) {
384-
i_2_ = i_9_;
385-
if(i_6_ == 0) {
386-
i_0_ = 1;
387-
break while_10_;
378+
currentByte = (byte) lastByte;
379+
tPos = tt[tPos];
380+
int nextByte = (byte) (tPos & 0xff);
381+
tPos >>= 8;
382+
blockUsed++;
383+
if(nextByte != lastByte) {
384+
lastByte = nextByte;
385+
if(outputRemaining == 0) {
386+
remainingBytes = 1;
387+
break outerLoop;
388388
}
389-
is_4_[i_5_] = i;
390-
i_5_++;
391-
i_6_--;
389+
output[outputPos] = currentByte;
390+
outputPos++;
391+
outputRemaining--;
392392
bool = true;
393-
} else if(i_1_ == i_8_) {
394-
if(i_6_ == 0) {
395-
i_0_ = 1;
396-
break while_10_;
393+
} else if(blockUsed == blockEnd) {
394+
if(outputRemaining == 0) {
395+
remainingBytes = 1;
396+
break outerLoop;
397397
}
398-
is_4_[i_5_] = i;
399-
i_5_++;
400-
i_6_--;
398+
output[outputPos] = currentByte;
399+
outputPos++;
400+
outputRemaining--;
401401
bool = true;
402402
}
403403
}
404-
i_0_ = 2;
405-
i_3_ = is[i_3_];
406-
int i_10_ = (byte) (i_3_ & 0xff);
407-
i_3_ >>= 8;
408-
if(++i_1_ != i_8_) {
409-
if(i_10_ == i_2_) {
410-
i_0_ = 3;
411-
i_3_ = is[i_3_];
412-
i_10_ = (byte) (i_3_ & 0xff);
413-
i_3_ >>= 8;
414-
if(++i_1_ != i_8_) {
415-
if(i_10_ == i_2_) {
416-
i_3_ = is[i_3_];
417-
i_10_ = (byte) (i_3_ & 0xff);
418-
i_3_ >>= 8;
419-
i_1_++;
420-
i_0_ = (i_10_ & 0xff) + 4;
421-
i_3_ = is[i_3_];
422-
i_2_ = (byte) (i_3_ & 0xff);
423-
i_3_ >>= 8;
424-
i_1_++;
404+
remainingBytes = 2;
405+
tPos = tt[tPos];
406+
int nextByte = (byte) (tPos & 0xff);
407+
tPos >>= 8;
408+
if(++blockUsed != blockEnd) {
409+
if(nextByte == lastByte) {
410+
remainingBytes = 3;
411+
tPos = tt[tPos];
412+
nextByte = (byte) (tPos & 0xff);
413+
tPos >>= 8;
414+
if(++blockUsed != blockEnd) {
415+
if(nextByte == lastByte) {
416+
tPos = tt[tPos];
417+
nextByte = (byte) (tPos & 0xff);
418+
tPos >>= 8;
419+
blockUsed++;
420+
remainingBytes = (nextByte & 0xff) + 4;
421+
tPos = tt[tPos];
422+
lastByte = (byte) (tPos & 0xff);
423+
tPos >>= 8;
424+
blockUsed++;
425425
} else
426-
i_2_ = i_10_;
426+
lastByte = nextByte;
427427
}
428428
} else
429-
i_2_ = i_10_;
429+
lastByte = nextByte;
430430
}
431431
}
432-
arg0.anInt846 += i_7_ - i_6_;
433-
arg0.stateOutCh = i;
434-
arg0.stateOutLen = i_0_;
435-
arg0.nBlockUsed = i_1_;
436-
arg0.anInt811 = i_2_;
437-
BZipContext.tt = is;
438-
arg0.tPos = i_3_;
439-
arg0.aByteArray837 = is_4_;
440-
arg0.anInt825 = i_5_;
441-
arg0.anInt823 = i_6_;
432+
bz.anInt846 += outputStart - outputRemaining;
433+
bz.stateOutCh = currentByte;
434+
bz.stateOutLen = remainingBytes;
435+
bz.nBlockUsed = blockUsed;
436+
bz.anInt811 = lastByte;
437+
BZipContext.tt = tt;
438+
bz.tPos = tPos;
439+
bz.aByteArray837 = output;
440+
bz.anInt825 = outputPos;
441+
bz.anInt823 = outputRemaining;
442442
}
443443

444444
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)