Skip to content

Commit 572d440

Browse files
committed
EBML: Implement string parsing
Signed-off-by: Serial <69764315+Serial-ATA@users.noreply.github.com>
1 parent 25ee3b4 commit 572d440

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/ebml/element_reader.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::ebml::vint::VInt;
22
use crate::error::Result;
3-
use crate::macros::decode_err;
3+
use crate::macros::{decode_err, try_vec};
44

55
use std::io::Read;
66

@@ -342,8 +342,21 @@ where
342342
})
343343
}
344344

345-
pub(crate) fn read_string(&mut self) -> Result<String> {
346-
todo!()
345+
pub(crate) fn read_string(&mut self, element_length: u64) -> Result<String> {
346+
// https://www.rfc-editor.org/rfc/rfc8794.html#section-7.4
347+
// A String Element MUST declare a length in octets from zero to VINTMAX
348+
let mut content = try_vec![0; element_length as usize];
349+
self.reader.read_exact(&mut content)?;
350+
351+
// https://www.rfc-editor.org/rfc/rfc8794.html#section-13
352+
// Null Octets, which are octets with all bits set to zero,
353+
// MAY follow the value of a String Element or UTF-8 Element to serve as a terminator.
354+
if let Some(i) = content.iter().rposition(|x| *x != 0) {
355+
let new_len = i + 1;
356+
content.truncate(new_len);
357+
}
358+
359+
String::from_utf8(content).map_err(Into::into)
347360
}
348361

349362
pub(crate) fn read_utf8(&mut self) -> Result<String> {

src/ebml/read.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ where
119119
ElementIdent::EBMLReadVersion => {
120120
properties.header.read_version = element_reader.read_unsigned_int(size)?
121121
},
122-
ElementIdent::DocType => properties.header.doc_type = element_reader.read_string()?,
122+
ElementIdent::DocType => {
123+
properties.header.doc_type = element_reader.read_string(size)?
124+
},
123125
ElementIdent::DocTypeVersion => {
124126
properties.header.doc_type_version = element_reader.read_unsigned_int(size)?
125127
},

0 commit comments

Comments
 (0)