Skip to content

Commit

Permalink
Trim zero padding from EBML string values
Browse files Browse the repository at this point in the history
Issue #4010

-------------
Created by MOE: https:/google/moe
MOE_MIGRATED_REVID=190442962
  • Loading branch information
ojw28 committed Mar 27, 2018
1 parent 40947d5 commit dfe4bfb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
2 changes: 2 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
* Prevent multiple instances of SimpleCache in the same folder.
Previous instance must be released.
* DRM: Allow multiple listeners for `DefaultDrmSessionManager`.
* Fix handling of zero padded strings when parsing Matroska streams
([#4010](https:/google/ExoPlayer/issues/4010)).
* Fix ANR issue on Redmi 4X and Redmi Note 4
([#4006](https:/google/ExoPlayer/issues/4006)).
* Removed default renderer time offset of 60000000 from internal player. The
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,11 @@ private double readFloat(ExtractorInput input, int byteLength)
}

/**
* Reads and returns a string of length {@code byteLength} from the {@link ExtractorInput}.
* Reads a string of length {@code byteLength} from the {@link ExtractorInput}. Zero padding is
* removed, so the returned string may be shorter than {@code byteLength}.
*
* @param input The {@link ExtractorInput} from which to read.
* @param byteLength The length of the float being read.
* @param byteLength The length of the string being read, including zero padding.
* @return The read string value.
* @throws IOException If an error occurs reading from the input.
* @throws InterruptedException If the thread is interrupted.
Expand All @@ -224,7 +225,12 @@ private String readString(ExtractorInput input, int byteLength)
}
byte[] stringBytes = new byte[byteLength];
input.readFully(stringBytes, 0, byteLength);
return new String(stringBytes);
// Remove zero padding.
int trimmedLength = byteLength;
while (trimmedLength > 0 && stringBytes[trimmedLength - 1] == 0) {
trimmedLength--;
}
return new String(stringBytes, 0, trimmedLength);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ public void testStringElement() throws IOException, InterruptedException {
assertEvents(input, expected.events);
}

@Test
public void testStringElementWithZeroPadding() throws IOException, InterruptedException {
ExtractorInput input = createTestInput(0x42, 0x82, 0x86, 0x41, 0x62, 0x63, 0x00, 0x00, 0x00);
TestOutput expected = new TestOutput();
expected.stringElement(TestOutput.ID_DOC_TYPE, "Abc");
assertEvents(input, expected.events);
}

@Test
public void testStringElementEmpty() throws IOException, InterruptedException {
ExtractorInput input = createTestInput(0x42, 0x82, 0x80);
Expand Down

0 comments on commit dfe4bfb

Please sign in to comment.