Skip to content

Commit

Permalink
prepare java decoder for transpilation to Kotlin
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 601023149
  • Loading branch information
eustas authored and copybara-github committed Jan 24, 2024
1 parent d5e697b commit 200f379
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 37 deletions.
40 changes: 23 additions & 17 deletions java/org/brotli/dec/Decode.java
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ private static void decodeMetaBlockLength(State s) {
if (sizeBytes == 0) {
return;
}
for (int i = 0; i < sizeBytes; i++) {
for (int i = 0; i < sizeBytes; ++i) {
BitReader.fillBitWindow(s);
final int bits = BitReader.readFewBits(s, 8);
if (bits == 0 && i + 1 == sizeBytes && sizeBytes > 1) {
Expand All @@ -360,7 +360,7 @@ private static void decodeMetaBlockLength(State s) {
s.metaBlockLength |= bits << (i * 8);
}
} else {
for (int i = 0; i < sizeNibbles; i++) {
for (int i = 0; i < sizeNibbles; ++i) {
BitReader.fillBitWindow(s);
final int bits = BitReader.readFewBits(s, 4);
if (bits == 0 && i + 1 == sizeNibbles && sizeNibbles > 4) {
Expand Down Expand Up @@ -405,18 +405,19 @@ private static int readBlockLength(int[] tableGroup, int tableIdx, State s) {

private static void moveToFront(int[] v, int index) {
final int value = v[index];
for (; index > 0; index--) {
while (index > 0) {
v[index] = v[index - 1];
index--;
}
v[0] = value;
}

private static void inverseMoveToFrontTransform(byte[] v, int vLen) {
final int[] mtf = new int[256];
for (int i = 0; i < 256; i++) {
for (int i = 0; i < 256; ++i) {
mtf[i] = i;
}
for (int i = 0; i < vLen; i++) {
for (int i = 0; i < vLen; ++i) {
final int index = v[i] & 0xFF;
v[i] = (byte) mtf[index];
if (index != 0) {
Expand Down Expand Up @@ -470,7 +471,7 @@ private static void readHuffmanCodeLengths(
if (symbol + repeatDelta > numSymbols) {
throw new BrotliRuntimeException("symbol + repeatDelta > numSymbols"); // COV_NF_LINE
}
for (int i = 0; i < repeatDelta; i++) {
for (int i = 0; i < repeatDelta; ++i) {
codeLengths[symbol++] = repeatCodeLen;
}
if (repeatCodeLen != 0) {
Expand Down Expand Up @@ -507,7 +508,7 @@ private static int readSimpleHuffmanCode(int alphabetSizeMax, int alphabetSizeLi
final int maxBits = 1 + log2floor(alphabetSizeMax - 1);

final int numSymbols = BitReader.readFewBits(s, 2) + 1;
for (int i = 0; i < numSymbols; i++) {
for (int i = 0; i < numSymbols; ++i) {
BitReader.fillBitWindow(s);
final int symbol = BitReader.readFewBits(s, maxBits);
if (symbol >= alphabetSizeLimit) {
Expand Down Expand Up @@ -569,7 +570,7 @@ private static int readComplexHuffmanCode(int alphabetSizeLimit, int skip,
final int[] codeLengthCodeLengths = new int[CODE_LENGTH_CODES];
int space = 32;
int numCodes = 0;
for (int i = skip; i < CODE_LENGTH_CODES && space > 0; i++) {
for (int i = skip; i < CODE_LENGTH_CODES; ++i) {
final int codeLenIdx = CODE_LENGTH_CODE_ORDER[i];
BitReader.fillBitWindow(s);
final int p = BitReader.peekBits(s) & 15;
Expand All @@ -580,6 +581,7 @@ private static int readComplexHuffmanCode(int alphabetSizeLimit, int skip,
if (v != 0) {
space -= (32 >> v);
numCodes++;
if (space <= 0) break;
}
}
if (space != 0 && numCodes != 1) {
Expand Down Expand Up @@ -630,7 +632,8 @@ private static int decodeContextMap(int contextMapSize, byte[] contextMap, State
final int[] table = new int[tableSize + 1];
final int tableIdx = table.length - 1;
readHuffmanCode(alphabetSize, alphabetSize, table, tableIdx, s);
for (int i = 0; i < contextMapSize; ) {
int i = 0;
while (i < contextMapSize) {
BitReader.readMoreInput(s);
BitReader.fillBitWindow(s);
final int code = readSymbol(table, tableIdx, s);
Expand Down Expand Up @@ -829,22 +832,24 @@ private static void readMetablockHuffmanCodesAndContextMaps(State s) {
s.numDirectDistanceCodes = BitReader.readFewBits(s, 4) << s.distancePostfixBits;
// TODO(eustas): Reuse?
s.contextModes = new byte[s.numLiteralBlockTypes];
for (int i = 0; i < s.numLiteralBlockTypes;) {
int i = 0;
while (i < s.numLiteralBlockTypes) {
/* Ensure that less than 256 bits read between readMoreInput. */
final int limit = Math.min(i + 96, s.numLiteralBlockTypes);
for (; i < limit; ++i) {
while (i < limit) {
BitReader.fillBitWindow(s);
s.contextModes[i] = (byte) BitReader.readFewBits(s, 2);
i++;
}
BitReader.readMoreInput(s);
}

// TODO(eustas): Reuse?
s.contextMap = new byte[s.numLiteralBlockTypes << LITERAL_CONTEXT_BITS];
final int numLiteralTrees = decodeContextMap(s.numLiteralBlockTypes << LITERAL_CONTEXT_BITS,
s.contextMap, s);
final int contextMapLength = s.numLiteralBlockTypes << LITERAL_CONTEXT_BITS;
s.contextMap = new byte[contextMapLength];
final int numLiteralTrees = decodeContextMap(contextMapLength, s.contextMap, s);
s.trivialLiteralContext = 1;
for (int j = 0; j < s.numLiteralBlockTypes << LITERAL_CONTEXT_BITS; j++) {
for (int j = 0; j < contextMapLength; ++j) {
if (s.contextMap[j] != j >> LITERAL_CONTEXT_BITS) {
s.trivialLiteralContext = 0;
break;
Expand Down Expand Up @@ -1253,7 +1258,8 @@ static void decompress(State s) {
final int dstEnd = dst + copyLength;
if ((srcEnd < ringBufferMask) && (dstEnd < ringBufferMask)) {
if (copyLength < 12 || (srcEnd > dst && dstEnd > src)) {
for (int k = 0; k < copyLength; k += 4) {
final int numQuads = (copyLength + 3) >> 2;
for (int k = 0; k < numQuads; ++k) {
ringBuffer[dst++] = ringBuffer[src++];
ringBuffer[dst++] = ringBuffer[src++];
ringBuffer[dst++] = ringBuffer[src++];
Expand All @@ -1266,7 +1272,7 @@ static void decompress(State s) {
s.metaBlockLength -= copyLength;
s.pos += copyLength;
} else {
for (; s.j < s.copyLength;) {
while (s.j < s.copyLength) {
ringBuffer[s.pos] =
ringBuffer[(s.pos - s.distance) & ringBufferMask];
s.metaBlockLength--;
Expand Down
8 changes: 4 additions & 4 deletions java/org/brotli/dec/DictionaryData.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ private static void unpackDictionaryData(ByteBuffer dictionary, String data0, St

// Toggle high bit using run-length delta encoded "skipFlip".
int offset = 0;
final int n = skipFlip.length();
for (int i = 0; i < n; i += 2) {
final int skip = skipFlip.charAt(i) - 36;
final int flip = skipFlip.charAt(i + 1) - 36;
final int n = skipFlip.length() >> 1;
for (int i = 0; i < n; ++i) {
final int skip = skipFlip.charAt(2 * i) - 36;
final int flip = skipFlip.charAt(2 * i + 1) - 36;
for (int j = 0; j < skip; ++j) {
dict[offset] ^= 3;
offset++;
Expand Down
36 changes: 20 additions & 16 deletions java/org/brotli/dec/Huffman.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,28 +64,26 @@ private static int nextTableBitSize(int[] count, int len, int rootBits) {
static int buildHuffmanTable(int[] tableGroup, int tableIdx, int rootBits, int[] codeLengths,
int codeLengthsSize) {
final int tableOffset = tableGroup[tableIdx];
int key; // Reversed prefix code.
final int[] sorted = new int[codeLengthsSize]; // Symbols sorted by code length.
// TODO(eustas): fill with zeroes?
final int[] count = new int[MAX_LENGTH + 1]; // Number of codes of each length.
final int[] offset = new int[MAX_LENGTH + 1]; // Offsets in sorted table for each length.
int symbol;

// Build histogram of code lengths.
for (symbol = 0; symbol < codeLengthsSize; symbol++) {
count[codeLengths[symbol]]++;
for (int sym = 0; sym < codeLengthsSize; ++sym) {
count[codeLengths[sym]]++;
}

// Generate offsets into sorted symbol table by code length.
offset[1] = 0;
for (int len = 1; len < MAX_LENGTH; len++) {
for (int len = 1; len < MAX_LENGTH; ++len) {
offset[len + 1] = offset[len] + count[len];
}

// Sort symbols by length, by symbol order within each length.
for (symbol = 0; symbol < codeLengthsSize; symbol++) {
if (codeLengths[symbol] != 0) {
sorted[offset[codeLengths[symbol]]++] = symbol;
for (int sym = 0; sym < codeLengthsSize; ++sym) {
if (codeLengths[sym] != 0) {
sorted[offset[codeLengths[sym]]++] = sym;
}
}

Expand All @@ -95,29 +93,34 @@ static int buildHuffmanTable(int[] tableGroup, int tableIdx, int rootBits, int[]

// Special case code with only one value.
if (offset[MAX_LENGTH] == 1) {
for (key = 0; key < totalSize; key++) {
tableGroup[tableOffset + key] = sorted[0];
for (int k = 0; k < totalSize; ++k) {
tableGroup[tableOffset + k] = sorted[0];
}
return totalSize;
}

// Fill in root table.
key = 0;
symbol = 0;
for (int len = 1, step = 2; len <= rootBits; len++, step <<= 1) {
for (; count[len] > 0; count[len]--) {
int key = 0; // Reversed prefix code.
int symbol = 0;
int step = 1;
for (int len = 1; len <= rootBits; ++len) {
step <<= 1;
while (count[len] > 0) {
replicateValue(tableGroup, tableOffset + key, step, tableSize,
len << 16 | sorted[symbol++]);
key = getNextKey(key, len);
count[len]--;
}
}

// Fill in 2nd level tables and add pointers to root table.
final int mask = totalSize - 1;
int low = -1;
int currentOffset = tableOffset;
for (int len = rootBits + 1, step = 2; len <= MAX_LENGTH; len++, step <<= 1) {
for (; count[len] > 0; count[len]--) {
step = 1;
for (int len = rootBits + 1; len <= MAX_LENGTH; ++len) {
step <<= 1;
while (count[len] > 0) {
if ((key & mask) != low) {
currentOffset += tableSize;
tableBits = nextTableBitSize(count, len, rootBits);
Expand All @@ -130,6 +133,7 @@ static int buildHuffmanTable(int[] tableGroup, int tableIdx, int rootBits, int[]
replicateValue(tableGroup, currentOffset + (key >> rootBits), step, tableSize,
(len - rootBits) << 16 | sorted[symbol++]);
key = getNextKey(key, len);
count[len]--;
}
}
return totalSize;
Expand Down

1 comment on commit 200f379

@StefanOltmann
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great to see progress here 👍

Please sign in to comment.