Skip to content

Commit

Permalink
fix: Invalid mode fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jandk committed Jun 16, 2024
1 parent d24af66 commit d080b59
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/main/java/be/twofold/tinybcdec/BC6Decoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ final class BC6Decoder extends BlockDecoder {
@SuppressWarnings("PointlessArithmeticExpression")
public void decodeBlock(byte[] src, int srcPos, byte[] dst, int dstPos, int bytesPerLine) {
Bits bits = Bits.from(src, srcPos);
Mode mode = MODES.get(mode(bits));
int modeIndex = mode(bits);
if (modeIndex >= MODES.size()) {
return;
}
Mode mode = MODES.get(modeIndex);

int[] colors = new int[12];
for (short op : mode.ops) {
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/be/twofold/tinybcdec/BC7Decoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ final class BC7Decoder extends BlockDecoder {
@Override
public void decodeBlock(byte[] src, int srcPos, byte[] dst, int dstPos, int bytesPerLine) {
Bits bits = Bits.from(src, srcPos);
Mode mode = MODES.get(mode(bits));
int modeIndex = mode(bits);
if (modeIndex >= MODES.size()) {
return;
}
Mode mode = MODES.get(modeIndex);
int partition = bits.get(mode.pb);
int rotation = bits.get(mode.rb);
boolean selection = bits.get(mode.isb) != 0;
Expand Down Expand Up @@ -235,12 +239,10 @@ private int unpack(int i, int n) {

private int mode(Bits bits) {
int mode = 0;
while (true) {
if (bits.get1() != 0) {
return mode;
}
while (mode < 8 && bits.get1() == 0) {
mode++;
}
return mode;
}

private static final class Mode {
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/be/twofold/tinybcdec/BC6DecoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,15 @@ void testBC6H_SF16() throws IOException {
assertThat(actual).isEqualTo(expected);
}

@Test
void testBC6InvalidBlock() {
byte[] src = new byte[16];
byte[] invalidModes = {0b10011, 0b10111, 0b11011, 0b11111};
for (byte invalidMode : invalidModes) {
src[0] = invalidMode;
BlockDecoder decoder = BlockDecoder.create(BlockFormat.BC6Unsigned, PixelOrder.RGB);
var actual = decoder.decode(4, 4, src, 0);
assertThat(actual).isEqualTo(new byte[16 * 3 * 2]);
}
}
}
7 changes: 7 additions & 0 deletions src/test/java/be/twofold/tinybcdec/BC7DecoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,11 @@ void testBC7() throws IOException {
assertThat(actual).isEqualTo(expected);
}

@Test
void testBC7InvalidBlock() {
byte[] src = new byte[16];
byte[] actual = decoder.decode(4, 4, src, 0);
assertThat(actual).isEqualTo(new byte[16 * 4]);
}

}

0 comments on commit d080b59

Please sign in to comment.