|
@@ -3,7 +3,7 @@
|
|
|
use crate::TagType;
|
|
|
use core::mem;
|
|
|
use core::slice;
|
|
|
-use core::str::Utf8Error;
|
|
|
+use core::str;
|
|
|
|
|
|
/// This tag contains the command line string.
|
|
|
///
|
|
@@ -31,9 +31,48 @@ impl CommandLineTag {
|
|
|
/// assert_eq!("/bootarg", command_line);
|
|
|
/// }
|
|
|
/// ```
|
|
|
- pub fn command_line(&self) -> Result<&str, Utf8Error> {
|
|
|
+ pub fn command_line(&self) -> Result<&str, str::Utf8Error> {
|
|
|
+ // strlen without null byte
|
|
|
let strlen = self.size as usize - mem::size_of::<CommandLineTag>();
|
|
|
let bytes = unsafe { slice::from_raw_parts((&self.string) as *const u8, strlen) };
|
|
|
- core::str::from_utf8(bytes)
|
|
|
+ str::from_utf8(bytes)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+#[cfg(test)]
|
|
|
+mod tests {
|
|
|
+ use crate::TagType;
|
|
|
+
|
|
|
+ const MSG: &str = "hello";
|
|
|
+
|
|
|
+ /// Returns the tag structure in bytes in native endian format.
|
|
|
+ fn get_bytes() -> std::vec::Vec<u8> {
|
|
|
+ // size is: 4 bytes for tag + 4 bytes for size + length of null-terminated string
|
|
|
+ let size = (4 + 4 + MSG.as_bytes().len() + 1) as u32;
|
|
|
+ [
|
|
|
+ &((TagType::Cmdline as u32).to_ne_bytes()),
|
|
|
+ &size.to_ne_bytes(),
|
|
|
+ MSG.as_bytes(),
|
|
|
+ // Null Byte
|
|
|
+ &[0],
|
|
|
+ ]
|
|
|
+ .iter()
|
|
|
+ .flat_map(|bytes| bytes.iter())
|
|
|
+ .copied()
|
|
|
+ .collect()
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Tests to parse a string with a terminating null byte from the tag (as the spec defines).
|
|
|
+ #[test]
|
|
|
+ fn test_parse_str() {
|
|
|
+ let tag = get_bytes();
|
|
|
+ let tag = unsafe {
|
|
|
+ tag.as_ptr()
|
|
|
+ .cast::<super::CommandLineTag>()
|
|
|
+ .as_ref()
|
|
|
+ .unwrap()
|
|
|
+ };
|
|
|
+ assert_eq!({ tag.typ }, TagType::Cmdline);
|
|
|
+ assert_eq!(tag.command_line().expect("must be valid UTF-8"), MSG);
|
|
|
}
|
|
|
}
|