From febe604ca177a04442c9e997ddd51dcc476b5415 Mon Sep 17 00:00:00 2001 From: olly Date: Mon, 26 Mar 2018 04:05:13 -0700 Subject: [PATCH] Trim zero padding from EBML string values Issue #4010 ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=190442962 --- RELEASENOTES.md | 2 ++ .../exoplayer2/extractor/mkv/DefaultEbmlReader.java | 12 +++++++++--- .../extractor/mkv/DefaultEbmlReaderTest.java | 8 ++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index a8b263db4d4..4fa71ec70f6 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -6,6 +6,8 @@ ([#3779](https://github.com/google/ExoPlayer/issues/3779)). * Fix ANR issue on Redmi 4X and Redmi Note 4 ([#4006](https://github.com/google/ExoPlayer/issues/4006)). +* Fix handling of zero padded strings when parsing Matroska streams + ([#4010](https://github.com/google/ExoPlayer/issues/4010)). * MediaSession extension: Omit fast forward and rewind actions when media is not seekable ([#4001](https://github.com/google/ExoPlayer/issues/4001)). diff --git a/library/core/src/main/java/com/google/android/exoplayer2/extractor/mkv/DefaultEbmlReader.java b/library/core/src/main/java/com/google/android/exoplayer2/extractor/mkv/DefaultEbmlReader.java index 91bc170205d..2c6130677f5 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/extractor/mkv/DefaultEbmlReader.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/extractor/mkv/DefaultEbmlReader.java @@ -202,10 +202,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. @@ -217,7 +218,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); } /** diff --git a/library/core/src/test/java/com/google/android/exoplayer2/extractor/mkv/DefaultEbmlReaderTest.java b/library/core/src/test/java/com/google/android/exoplayer2/extractor/mkv/DefaultEbmlReaderTest.java index 2fec5c7cabb..e44da0404b8 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/extractor/mkv/DefaultEbmlReaderTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/extractor/mkv/DefaultEbmlReaderTest.java @@ -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);