From 763c77283c1f9cf22475f4b6cd38073a9e96c832 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Tue, 24 Nov 2020 11:03:45 +0100 Subject: Discard terminating null-bytes at end of strings. --- src/main.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0c51ea0..52a8d3f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,13 +31,13 @@ use std::{ /// Parser for length prefixed strings. /// -/// Cubase uses a string format where the length of the string -/// is given as a 32 bit big endian word before the actual string -/// data. The string is also zero terminated, with the zero term -/// char included in the length. +/// Cubase uses a string format where the length of the string is given as a 32 bit big endian word +/// before the actual string data. The string may also be zero terminated, with the zero term char +/// included in the length. In that case, we trim off the ending zero bytes. fn cmstring(data: &[u8]) -> IResult<&[u8], &str> { let (r, s) = length_data(be_u32)(data)?; - Ok((r, std::str::from_utf8(s).unwrap())) + let end = s.iter().rposition(|b| b != &0u8).unwrap() + 1; + Ok((r, std::str::from_utf8(&s[0..end]).unwrap())) } #[derive(Debug, PartialEq)] @@ -188,7 +188,7 @@ fn m_midi_part<'a>(data: &'a [u8]) -> IResult<&'a [u8], MMidiPart<'a>> { fn node_value<'a>(class: &str, data: &'a [u8]) -> IResult<&'a [u8], NodeValue<'a>> { match class { - "PAppVersion\0" => { + "PAppVersion" => { let (r, appver) = p_appversion(&data)?; Ok((r, NodeValue::Version(appver))) }, -- cgit v1.2.3