Fix how ExifInterface creates VP8X chunks for WebP files

The current code reads alpha into an int, doesn't shift it back to the
least-significant-bit, and then shifts that value 4 more bits before
writing to the VP8X 'flags' byte. This results in setting the MSB of the
flags byte, which is a reserved bit that must always be zero, resulting
in an invalid file.

VP8X part of WebP spec:
https://developers.google.com/speed/webp/docs/riff_container#extended_file_format

Test: ExifInterfaceTest (after removing the suppression for this bug)

Bug: 253622642
Change-Id: I42469a479c94442eb395124c4eac174bd9ab6e09
This commit is contained in:
Rakesh Kumar
2022-11-08 13:45:40 +05:30
parent e3e6fa1ecc
commit 0d30ebcac2

View File

@@ -3825,7 +3825,7 @@ public class ExifInterface {
int widthAndHeight = 0;
int width = 0;
int height = 0;
int alpha = 0;
boolean alpha = false;
// Save VP8 frame data for later
byte[] vp8Frame = new byte[3];
@@ -3860,7 +3860,7 @@ public class ExifInterface {
width = ((widthAndHeight << 18) >> 18) + 1;
height = ((widthAndHeight << 4) >> 18) + 1;
// Retrieve alpha bit
alpha = widthAndHeight & (1 << 3);
alpha = (widthAndHeight & (1 << 28)) != 0;
bytesToRead -= (1 /* VP8L signature */ + 4);
}
@@ -3868,10 +3868,12 @@ public class ExifInterface {
nonHeaderOutputStream.write(WEBP_CHUNK_TYPE_VP8X);
nonHeaderOutputStream.writeInt(WEBP_CHUNK_TYPE_VP8X_DEFAULT_LENGTH);
byte[] data = new byte[WEBP_CHUNK_TYPE_VP8X_DEFAULT_LENGTH];
// ALPHA flag
if (alpha) {
data[0] = (byte) (data[0] | (1 << 4));
}
// EXIF flag
data[0] = (byte) (data[0] | (1 << 3));
// ALPHA flag
data[0] = (byte) (data[0] | (alpha << 4));
// VP8X stores Width - 1 and Height - 1 values
width -= 1;
height -= 1;