apkdmverity: use data_model crate for better handling of C structs
The data_model:DataInit trait provides methods for converting between
a C struct and an array of the same size, allowing us to drop the hand
crafted as_u8_slice methods.
Bug: N/A
Test: cargo test
Test: atest apkdmverity.test
Change-Id: Iaff910f0a638e91a428777b94dc6fb0b5fe53831
diff --git a/apkverity/src/dm/sys.rs b/apkverity/src/dm/sys.rs
index f623a2b..e709bf0 100644
--- a/apkverity/src/dm/sys.rs
+++ b/apkverity/src/dm/sys.rs
@@ -15,6 +15,7 @@
*/
use bitflags::bitflags;
+use data_model::DataInit;
// UAPI for device mapper can be found at include/uapi/linux/dm-ioctl.h
@@ -43,6 +44,7 @@
}
#[repr(C)]
+#[derive(Copy, Clone)]
pub struct DmIoctl {
pub version: [u32; 3],
pub data_size: u32,
@@ -58,6 +60,9 @@
pub data: [u8; 7],
}
+// SAFETY: C struct is safe to be initialized from raw data
+unsafe impl DataInit for DmIoctl {}
+
pub const DM_VERSION_MAJOR: u32 = 4;
pub const DM_VERSION_MINOR: u32 = 0;
pub const DM_VERSION_PATCHLEVEL: u32 = 0;
diff --git a/apkverity/src/dm/verity.rs b/apkverity/src/dm/verity.rs
index 950b26e..3a49ee2 100644
--- a/apkverity/src/dm/verity.rs
+++ b/apkverity/src/dm/verity.rs
@@ -19,6 +19,7 @@
// which is then given to `DeviceMapper` to create a mapper device.
use anyhow::{bail, Context, Result};
+use data_model::DataInit;
use std::io::Write;
use std::mem::size_of;
use std::path::Path;
@@ -55,7 +56,7 @@
pub struct DmVerityTarget(Box<[u8]>);
impl DmVerityTarget {
- pub fn as_u8_slice(&self) -> &[u8] {
+ pub fn as_slice(&self) -> &[u8] {
self.0.as_ref()
}
}
@@ -188,7 +189,7 @@
header.next = aligned_size as u32;
let mut buf = Vec::with_capacity(aligned_size);
- buf.write_all(header.as_u8_slice())?;
+ buf.write_all(header.as_slice())?;
buf.write_all(body.as_bytes())?;
buf.write_all(vec![0; padding].as_slice())?;
Ok(DmVerityTarget(buf.into_boxed_slice()))