Skip to content

Commit

Permalink
further preparations for Kotlin transpilation
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 603638823
  • Loading branch information
eustas authored and copybara-github committed Feb 2, 2024
1 parent 200f379 commit c1362a7
Show file tree
Hide file tree
Showing 11 changed files with 391 additions and 335 deletions.
52 changes: 27 additions & 25 deletions java/org/brotli/dec/BitReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ static int peekBits(State s) {
* otherwise BitReader will become broken.
*/
static int readFewBits(State s, int n) {
final int val = peekBits(s) & ((1 << n) - 1);
final int v = peekBits(s) & ((1 << n) - 1);
s.bitOffset += n;
return val;
return v;
}

static int readBits(State s, int n) {
Expand Down Expand Up @@ -212,55 +212,57 @@ static int halfAvailable(State s) {
}

static void copyRawBytes(State s, byte[] data, int offset, int length) {
int pos = offset;
int len = length;
if ((s.bitOffset & 7) != 0) {
throw new BrotliRuntimeException("Unaligned copyBytes");
}

// Drain accumulator.
while ((s.bitOffset != BITNESS) && (length != 0)) {
data[offset++] = (byte) peekBits(s);
while ((s.bitOffset != BITNESS) && (len != 0)) {
data[pos++] = (byte) peekBits(s);
s.bitOffset += 8;
length--;
len--;
}
if (length == 0) {
if (len == 0) {
return;
}

// Get data from shadow buffer with "sizeof(int)" granularity.
final int copyNibbles = Math.min(halfAvailable(s), length >> LOG_HALF_SIZE);
final int copyNibbles = Math.min(halfAvailable(s), len >> LOG_HALF_SIZE);
if (copyNibbles > 0) {
final int readOffset = s.halfOffset << LOG_HALF_SIZE;
final int delta = copyNibbles << LOG_HALF_SIZE;
System.arraycopy(s.byteBuffer, readOffset, data, offset, delta);
offset += delta;
length -= delta;
System.arraycopy(s.byteBuffer, readOffset, data, pos, delta);
pos += delta;
len -= delta;
s.halfOffset += copyNibbles;
}
if (length == 0) {
if (len == 0) {
return;
}

// Read tail bytes.
if (halfAvailable(s) > 0) {
// length = 1..3
fillBitWindow(s);
while (length != 0) {
data[offset++] = (byte) peekBits(s);
while (len != 0) {
data[pos++] = (byte) peekBits(s);
s.bitOffset += 8;
length--;
len--;
}
checkHealth(s, 0);
return;
}

// Now it is possible to copy bytes directly.
while (length > 0) {
final int len = Utils.readInput(s, data, offset, length);
if (len == -1) {
while (len > 0) {
final int chunkLen = Utils.readInput(s, data, pos, len);
if (chunkLen == -1) {
throw new BrotliRuntimeException("Unexpected end of input");
}
offset += len;
length -= len;
pos += chunkLen;
len -= chunkLen;
}
}

Expand All @@ -273,16 +275,16 @@ static void bytesToNibbles(State s, int byteLen) {
if (BITNESS == 64) {
final int[] intBuffer = s.intBuffer;
for (int i = 0; i < halfLen; ++i) {
intBuffer[i] = ((byteBuffer[i * 4] & 0xFF))
| ((byteBuffer[(i * 4) + 1] & 0xFF) << 8)
| ((byteBuffer[(i * 4) + 2] & 0xFF) << 16)
| ((byteBuffer[(i * 4) + 3] & 0xFF) << 24);
intBuffer[i] = ((int) byteBuffer[i * 4] & 0xFF)
| (((int) byteBuffer[(i * 4) + 1] & 0xFF) << 8)
| (((int) byteBuffer[(i * 4) + 2] & 0xFF) << 16)
| (((int) byteBuffer[(i * 4) + 3] & 0xFF) << 24);
}
} else {
final short[] shortBuffer = s.shortBuffer;
for (int i = 0; i < halfLen; ++i) {
shortBuffer[i] = (short) ((byteBuffer[i * 2] & 0xFF)
| ((byteBuffer[(i * 2) + 1] & 0xFF) << 8));
shortBuffer[i] = (short) (((int) byteBuffer[i * 2] & 0xFF)
| (((int) byteBuffer[(i * 2) + 1] & 0xFF) << 8));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions java/org/brotli/dec/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private static void unpackLookupTable(int[] lookup, String map, String rle) {
}
// UTF8
for (int i = 0; i < 128; ++i) {
lookup[1024 + i] = 4 * (map.charAt(i) - 32);
lookup[1024 + i] = 4 * ((int) map.charAt(i) - 32);
}
for (int i = 0; i < 64; ++i) {
lookup[1152 + i] = i & 1;
Expand All @@ -35,7 +35,7 @@ private static void unpackLookupTable(int[] lookup, String map, String rle) {
int offset = 1280;
for (int k = 0; k < 19; ++k) {
final int value = k & 3;
final int rep = rle.charAt(k) - 32;
final int rep = (int) rle.charAt(k) - 32;
for (int i = 0; i < rep; ++i) {
lookup[offset++] = value;
}
Expand Down
89 changes: 47 additions & 42 deletions java/org/brotli/dec/Decode.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,16 @@ final class Decode {
private static int log2floor(int i) {
int result = -1;
int step = 16;
int v = i;
while (step > 0) {
if ((i >>> step) != 0) {
int next = v >>> step;
if (next != 0) {
result += step;
i = i >>> step;
v = next;
}
step = step >> 1;
}
return result + i;
return result + v;
}

private static int calculateDistanceAlphabetSize(int npostfix, int ndirect, int maxndistbits) {
Expand All @@ -164,14 +166,12 @@ private static int calculateDistanceAlphabetLimit(int maxDistance, int npostfix,
}

private static void unpackCommandLookupTable(short[] cmdLookup) {
final short[] insertLengthOffsets = new short[24];
final short[] copyLengthOffsets = new short[24];
final int[] insertLengthOffsets = new int[24];
final int[] copyLengthOffsets = new int[24];
copyLengthOffsets[0] = 2;
for (int i = 0; i < 23; ++i) {
insertLengthOffsets[i + 1] =
(short) (insertLengthOffsets[i] + (1 << INSERT_LENGTH_N_BITS[i]));
copyLengthOffsets[i + 1] =
(short) (copyLengthOffsets[i] + (1 << COPY_LENGTH_N_BITS[i]));
insertLengthOffsets[i + 1] = insertLengthOffsets[i] + (1 << (int) INSERT_LENGTH_N_BITS[i]);
copyLengthOffsets[i + 1] = copyLengthOffsets[i] + (1 << (int) COPY_LENGTH_N_BITS[i]);
}

for (int cmdCode = 0; cmdCode < NUM_COMMAND_CODES; ++cmdCode) {
Expand All @@ -184,14 +184,15 @@ private static void unpackCommandLookupTable(short[] cmdLookup) {
}
final int insertCode = (((0x29850 >>> (rangeIdx * 2)) & 0x3) << 3) | ((cmdCode >>> 3) & 7);
final int copyCode = (((0x26244 >>> (rangeIdx * 2)) & 0x3) << 3) | (cmdCode & 7);
final short copyLengthOffset = copyLengthOffsets[copyCode];
final int copyLengthOffset = copyLengthOffsets[copyCode];
final int distanceContext =
distanceContextOffset + (copyLengthOffset > 4 ? 3 : copyLengthOffset - 2);
distanceContextOffset + (copyLengthOffset > 4 ? 3 : (copyLengthOffset - 2));
final int index = cmdCode * 4;
cmdLookup[index + 0] =
(short) (INSERT_LENGTH_N_BITS[insertCode] | (COPY_LENGTH_N_BITS[copyCode] << 8));
cmdLookup[index + 1] = insertLengthOffsets[insertCode];
cmdLookup[index + 2] = copyLengthOffsets[copyCode];
(short)
((int) INSERT_LENGTH_N_BITS[insertCode] | ((int) COPY_LENGTH_N_BITS[copyCode] << 8));
cmdLookup[index + 1] = (short) insertLengthOffsets[insertCode];
cmdLookup[index + 2] = (short) copyLengthOffsets[copyCode];
cmdLookup[index + 3] = (short) distanceContext;
}
}
Expand Down Expand Up @@ -357,7 +358,7 @@ private static void decodeMetaBlockLength(State s) {
if (bits == 0 && i + 1 == sizeBytes && sizeBytes > 1) {
throw new BrotliRuntimeException("Exuberant nibble");
}
s.metaBlockLength |= bits << (i * 8);
s.metaBlockLength += bits << (i * 8);
}
} else {
for (int i = 0; i < sizeNibbles; ++i) {
Expand All @@ -366,7 +367,7 @@ private static void decodeMetaBlockLength(State s) {
if (bits == 0 && i + 1 == sizeNibbles && sizeNibbles > 4) {
throw new BrotliRuntimeException("Exuberant nibble");
}
s.metaBlockLength |= bits << (i * 4);
s.metaBlockLength += bits << (i * 4);
}
}
s.metaBlockLength++;
Expand All @@ -380,8 +381,8 @@ private static void decodeMetaBlockLength(State s) {
*/
private static int readSymbol(int[] tableGroup, int tableIdx, State s) {
int offset = tableGroup[tableIdx];
final int val = BitReader.peekBits(s);
offset += val & HUFFMAN_TABLE_MASK;
final int v = BitReader.peekBits(s);
offset += v & HUFFMAN_TABLE_MASK;
final int bits = tableGroup[offset] >> 16;
final int sym = tableGroup[offset] & 0xFFFF;
if (bits <= HUFFMAN_TABLE_BITS) {
Expand All @@ -390,7 +391,7 @@ private static int readSymbol(int[] tableGroup, int tableIdx, State s) {
}
offset += sym;
final int mask = (1 << bits) - 1;
offset += (val & mask) >>> HUFFMAN_TABLE_BITS;
offset += (v & mask) >>> HUFFMAN_TABLE_BITS;
s.bitOffset += ((tableGroup[offset] >> 16) + HUFFMAN_TABLE_BITS);
return tableGroup[offset] & 0xFFFF;
}
Expand All @@ -404,10 +405,11 @@ private static int readBlockLength(int[] tableGroup, int tableIdx, State s) {
}

private static void moveToFront(int[] v, int index) {
final int value = v[index];
while (index > 0) {
v[index] = v[index - 1];
index--;
int i = index;
final int value = v[i];
while (i > 0) {
v[i] = v[i - 1];
i--;
}
v[0] = value;
}
Expand All @@ -418,7 +420,7 @@ private static void inverseMoveToFrontTransform(byte[] v, int vLen) {
mtf[i] = i;
}
for (int i = 0; i < vLen; ++i) {
final int index = v[i] & 0xFF;
final int index = (int) v[i] & 0xFF;
v[i] = (byte) mtf[index];
if (index != 0) {
moveToFront(mtf, index);
Expand Down Expand Up @@ -463,7 +465,7 @@ private static void readHuffmanCodeLengths(
final int oldRepeat = repeat;
if (repeat > 0) {
repeat -= 2;
repeat <<= extraBits;
repeat = repeat << extraBits;
}
BitReader.fillBitWindow(s);
repeat += BitReader.readFewBits(s, extraBits) + 3;
Expand Down Expand Up @@ -689,8 +691,8 @@ private static void decodeLiteralBlockSwitch(State s) {
s.literalBlockLength = decodeBlockTypeAndLength(s, 0, s.numLiteralBlockTypes);
final int literalBlockType = s.rings[5];
s.contextMapSlice = literalBlockType << LITERAL_CONTEXT_BITS;
s.literalTreeIdx = s.contextMap[s.contextMapSlice] & 0xFF;
final int contextMode = s.contextModes[literalBlockType];
s.literalTreeIdx = (int) s.contextMap[s.contextMapSlice] & 0xFF;
final int contextMode = (int) s.contextModes[literalBlockType];
s.contextLookupOffset1 = contextMode << 9;
s.contextLookupOffset2 = s.contextLookupOffset1 + 256;
}
Expand All @@ -711,7 +713,7 @@ private static void maybeReallocateRingBuffer(State s) {
/* TODO(eustas): Handle 2GB+ cases more gracefully. */
final int minimalNewSize = s.expectedTotalSize;
while ((newSize >> 1) > minimalNewSize) {
newSize >>= 1;
newSize = newSize >> 1;
}
if ((s.inputEnd == 0) && newSize < 16384 && s.maxRingBufferSize >= 16384) {
newSize = 16384;
Expand All @@ -722,8 +724,9 @@ private static void maybeReallocateRingBuffer(State s) {
}
final int ringBufferSizeWithSlack = newSize + MAX_TRANSFORMED_WORD_LENGTH;
final byte[] newBuffer = new byte[ringBufferSizeWithSlack];
if (s.ringBuffer.length != 0) {
System.arraycopy(s.ringBuffer, 0, newBuffer, 0, s.ringBufferSize);
final byte[] oldBuffer = s.ringBuffer;
if (oldBuffer.length != 0) {
System.arraycopy(oldBuffer, 0, newBuffer, 0, s.ringBufferSize);
}
s.ringBuffer = newBuffer;
s.ringBufferSize = newSize;
Expand Down Expand Up @@ -850,7 +853,7 @@ private static void readMetablockHuffmanCodesAndContextMaps(State s) {
final int numLiteralTrees = decodeContextMap(contextMapLength, s.contextMap, s);
s.trivialLiteralContext = 1;
for (int j = 0; j < contextMapLength; ++j) {
if (s.contextMap[j] != j >> LITERAL_CONTEXT_BITS) {
if ((int) s.contextMap[j] != j >> LITERAL_CONTEXT_BITS) {
s.trivialLiteralContext = 0;
break;
}
Expand Down Expand Up @@ -1021,7 +1024,7 @@ private static void initializeCompoundDictionaryCopy(State s, int address, int l
if (s.cdBlockBits == -1) {
initializeCompoundDictionary(s);
}
int index = s.cdBlockMap[address >>> s.cdBlockBits];
int index = (int) s.cdBlockMap[address >>> s.cdBlockBits];
while (address >= s.cdChunkOffsets[index + 1]) {
index++;
}
Expand Down Expand Up @@ -1123,10 +1126,10 @@ static void decompress(State s) {
s.commandBlockLength--;
BitReader.fillBitWindow(s);
final int cmdCode = readSymbol(s.commandTreeGroup, s.commandTreeIdx, s) << 2;
final short insertAndCopyExtraBits = CMD_LOOKUP[cmdCode];
final int insertLengthOffset = CMD_LOOKUP[cmdCode + 1];
final int copyLengthOffset = CMD_LOOKUP[cmdCode + 2];
s.distanceCode = CMD_LOOKUP[cmdCode + 3];
final int insertAndCopyExtraBits = (int) CMD_LOOKUP[cmdCode];
final int insertLengthOffset = (int) CMD_LOOKUP[cmdCode + 1];
final int copyLengthOffset = (int) CMD_LOOKUP[cmdCode + 2];
s.distanceCode = (int) CMD_LOOKUP[cmdCode + 3];
BitReader.fillBitWindow(s);
{
final int insertLengthExtraBits = insertAndCopyExtraBits & 0xFF;
Expand Down Expand Up @@ -1161,16 +1164,17 @@ static void decompress(State s) {
}
}
} else {
int prevByte1 = ringBuffer[(s.pos - 1) & ringBufferMask] & 0xFF;
int prevByte2 = ringBuffer[(s.pos - 2) & ringBufferMask] & 0xFF;
int prevByte1 = (int) ringBuffer[(s.pos - 1) & ringBufferMask] & 0xFF;
int prevByte2 = (int) ringBuffer[(s.pos - 2) & ringBufferMask] & 0xFF;
while (s.j < s.insertLength) {
BitReader.readMoreInput(s);
if (s.literalBlockLength == 0) {
decodeLiteralBlockSwitch(s);
}
final int literalContext = Context.LOOKUP[s.contextLookupOffset1 + prevByte1]
| Context.LOOKUP[s.contextLookupOffset2 + prevByte2];
final int literalTreeIdx = s.contextMap[s.contextMapSlice + literalContext] & 0xFF;
final int literalTreeIdx =
(int) s.contextMap[s.contextMapSlice + literalContext] & 0xFF;
s.literalBlockLength--;
prevByte2 = prevByte1;
BitReader.fillBitWindow(s);
Expand Down Expand Up @@ -1204,7 +1208,8 @@ static void decompress(State s) {
}
s.distanceBlockLength--;
BitReader.fillBitWindow(s);
final int distTreeIdx = s.distContextMap[s.distContextMapSlice + distanceCode] & 0xFF;
final int distTreeIdx =
(int) s.distContextMap[s.distContextMapSlice + distanceCode] & 0xFF;
distanceCode = readSymbol(s.distanceTreeGroup, distTreeIdx, s);
if (distanceCode < NUM_DISTANCE_SHORT_CODES) {
final int index =
Expand All @@ -1214,7 +1219,7 @@ static void decompress(State s) {
throw new BrotliRuntimeException("Negative distance"); // COV_NF_LINE
}
} else {
final int extraBits = s.distExtraBits[distanceCode];
final int extraBits = (int) s.distExtraBits[distanceCode];
int bits;
if (s.bitOffset + extraBits <= BitReader.BITNESS) {
bits = BitReader.readFewBits(s, extraBits);
Expand Down Expand Up @@ -1337,7 +1342,7 @@ static void decompress(State s) {
if (s.pos > s.ringBufferSize) {
Utils.copyBytesWithin(ringBuffer, 0, s.ringBufferSize, s.pos);
}
s.pos &= ringBufferMask;
s.pos = s.pos & ringBufferMask;
s.ringBufferBytesWritten = 0;
}
s.runningState = s.nextRunningState;
Expand Down
10 changes: 5 additions & 5 deletions java/org/brotli/dec/DictionaryData.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,20 @@ private static void unpackDictionaryData(ByteBuffer dictionary, String data0, St
int offset = 0;
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;
final int skip = (int) skipFlip.charAt(2 * i) - 36;
final int flip = (int) skipFlip.charAt(2 * i + 1) - 36;
for (int j = 0; j < skip; ++j) {
dict[offset] ^= 3;
dict[offset] = (byte) ((int) dict[offset] ^ 3);
offset++;
}
for (int j = 0; j < flip; ++j) {
dict[offset] ^= 236;
dict[offset] = (byte) ((int) dict[offset] ^ 236);
offset++;
}
}

for (int i = 0; i < sizeBitsData.length(); ++i) {
sizeBits[i] = sizeBitsData.charAt(i) - 65;
sizeBits[i] = (int) sizeBitsData.charAt(i) - 65;
}

dictionary.put(dict);
Expand Down
Loading

0 comments on commit c1362a7

Please sign in to comment.