mirror of
https://github.com/alfg/mp4-rust.git
synced 2024-06-02 13:39:54 +00:00
Allow empty, multi-byte, and arbitrary NUL terminated strings
This commit is contained in:
parent
fc0b48d8b8
commit
f049ddc94d
|
@ -264,22 +264,16 @@ impl<R: Read + Seek> ReadBox<&mut R> for UrlBox {
|
|||
|
||||
let (version, flags) = read_box_header_ext(reader)?;
|
||||
|
||||
let location = if size.saturating_sub(HEADER_SIZE + HEADER_EXT_SIZE) > 0 {
|
||||
let buf_size = size - HEADER_SIZE - HEADER_EXT_SIZE - 1;
|
||||
let mut buf = vec![0u8; buf_size as usize];
|
||||
reader.read_exact(&mut buf)?;
|
||||
match String::from_utf8(buf) {
|
||||
Ok(t) => {
|
||||
if t.len() != buf_size as usize {
|
||||
return Err(Error::InvalidData("string too small"));
|
||||
}
|
||||
t
|
||||
}
|
||||
_ => String::default(),
|
||||
}
|
||||
} else {
|
||||
String::default()
|
||||
};
|
||||
let buf_size = size
|
||||
.checked_sub(HEADER_SIZE + HEADER_EXT_SIZE)
|
||||
.ok_or(Error::InvalidData("url size too small"))?;
|
||||
|
||||
let mut buf = vec![0u8; buf_size as usize];
|
||||
reader.read_exact(&mut buf)?;
|
||||
if let Some(end) = buf.iter().position(|&b| b == b'\0') {
|
||||
buf.truncate(end);
|
||||
}
|
||||
let location = String::from_utf8(buf).unwrap_or_default();
|
||||
|
||||
skip_bytes_to(reader, start + size)?;
|
||||
|
||||
|
|
|
@ -53,20 +53,15 @@ impl<R: Read + Seek> ReadBox<&mut R> for HdlrBox {
|
|||
skip_bytes(reader, 12)?; // reserved
|
||||
|
||||
let buf_size = size
|
||||
.checked_sub(HEADER_SIZE + HEADER_EXT_SIZE + 20 + 1)
|
||||
.checked_sub(HEADER_SIZE + HEADER_EXT_SIZE + 20)
|
||||
.ok_or(Error::InvalidData("hdlr size too small"))?;
|
||||
|
||||
let mut buf = vec![0u8; buf_size as usize];
|
||||
reader.read_exact(&mut buf)?;
|
||||
|
||||
let handler_string = match String::from_utf8(buf) {
|
||||
Ok(t) => {
|
||||
if t.len() != buf_size as usize {
|
||||
return Err(Error::InvalidData("string too small"));
|
||||
}
|
||||
t
|
||||
}
|
||||
_ => String::from("null"),
|
||||
};
|
||||
if let Some(end) = buf.iter().position(|&b| b == b'\0') {
|
||||
buf.truncate(end);
|
||||
}
|
||||
let handler_string = String::from_utf8(buf).unwrap_or_default();
|
||||
|
||||
skip_bytes_to(reader, start + size)?;
|
||||
|
||||
|
@ -127,4 +122,52 @@ mod tests {
|
|||
let dst_box = HdlrBox::read_box(&mut reader, header.size).unwrap();
|
||||
assert_eq!(src_box, dst_box);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hdlr_empty() {
|
||||
let src_box = HdlrBox {
|
||||
version: 0,
|
||||
flags: 0,
|
||||
handler_type: str::parse::<FourCC>("vide").unwrap(),
|
||||
name: String::new(),
|
||||
};
|
||||
let mut buf = Vec::new();
|
||||
src_box.write_box(&mut buf).unwrap();
|
||||
assert_eq!(buf.len(), src_box.box_size() as usize);
|
||||
|
||||
let mut reader = Cursor::new(&buf);
|
||||
let header = BoxHeader::read(&mut reader).unwrap();
|
||||
assert_eq!(header.name, BoxType::HdlrBox);
|
||||
assert_eq!(src_box.box_size(), header.size);
|
||||
|
||||
let dst_box = HdlrBox::read_box(&mut reader, header.size).unwrap();
|
||||
assert_eq!(src_box, dst_box);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hdlr_extra() {
|
||||
let real_src_box = HdlrBox {
|
||||
version: 0,
|
||||
flags: 0,
|
||||
handler_type: str::parse::<FourCC>("vide").unwrap(),
|
||||
name: String::from("Good"),
|
||||
};
|
||||
let src_box = HdlrBox {
|
||||
version: 0,
|
||||
flags: 0,
|
||||
handler_type: str::parse::<FourCC>("vide").unwrap(),
|
||||
name: String::from_utf8(b"Good\0Bad".to_vec()).unwrap(),
|
||||
};
|
||||
let mut buf = Vec::new();
|
||||
src_box.write_box(&mut buf).unwrap();
|
||||
assert_eq!(buf.len(), src_box.box_size() as usize);
|
||||
|
||||
let mut reader = Cursor::new(&buf);
|
||||
let header = BoxHeader::read(&mut reader).unwrap();
|
||||
assert_eq!(header.name, BoxType::HdlrBox);
|
||||
assert_eq!(src_box.box_size(), header.size);
|
||||
|
||||
let dst_box = HdlrBox::read_box(&mut reader, header.size).unwrap();
|
||||
assert_eq!(real_src_box, dst_box);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue