From 2ffc7b6c574254cc4fd584726aeb9ebe5b828a33 Mon Sep 17 00:00:00 2001 From: Marco Grassi Date: Fri, 18 Nov 2022 21:38:21 +0800 Subject: [PATCH 1/2] upgrade dependencies and fix for new deku/bitvec version --- Cargo.toml | 6 +++--- axmldecoder-printer/Cargo.toml | 2 +- src/stringpool.rs | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b842686..bb35afc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,6 @@ exclude = ["examples/**", "axmldecoder-printer/**"] [dependencies] byteorder = "1.4.3" -deku = "~0.13" -indexmap = "1.7.0" -thiserror = "1.0.30" +deku = "~0.15" +indexmap = "1.9.2" +thiserror = "1.0.37" diff --git a/axmldecoder-printer/Cargo.toml b/axmldecoder-printer/Cargo.toml index 735b9f9..0b7160c 100644 --- a/axmldecoder-printer/Cargo.toml +++ b/axmldecoder-printer/Cargo.toml @@ -7,5 +7,5 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -anyhow = "1.0.40" +anyhow = "1.0.66" axmldecoder = { path = "../" } diff --git a/src/stringpool.rs b/src/stringpool.rs index 8284aa1..ed19c3e 100644 --- a/src/stringpool.rs +++ b/src/stringpool.rs @@ -26,7 +26,7 @@ pub(crate) struct StringPool { pub(crate) strings: Vec>, } -type DekuRest = BitSlice; +type DekuRest = BitSlice; impl StringPool { fn read_strings<'a>( header: &StringPoolHeader, From 0b0259e01ad28a13fc85531d8b1ee9ac2f7501ed Mon Sep 17 00:00:00 2001 From: Marco Grassi Date: Fri, 18 Nov 2022 21:46:07 +0800 Subject: [PATCH 2/2] code style and fix clippy warnings --- src/lib.rs | 7 +++++-- src/stringpool.rs | 5 +++-- src/xml.rs | 31 ++++++++++++++++--------------- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2e12d6b..4687016 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -45,8 +45,11 @@ pub enum ParseError { Utf16StringParseError(std::string::FromUtf16Error), } -///Parses an Android binary XML and returns a [XmlDocument] object. +///Parses an Android binary XML and returns a [`XmlDocument`] object. /// +/// # Errors +/// +/// Will return `ParseError` if `input` cannot be parsed ///```rust ///use axmldecoder::parse; ///# use axmldecoder::ParseError; @@ -76,7 +79,7 @@ mod tests { let mut f = File::open(entry.path()).unwrap(); let mut buf = Vec::new(); f.read_to_end(&mut buf).unwrap(); - parse(&buf).expect(&format!("{} failed to parse", entry.path().display())); + parse(&buf).unwrap_or_else(|_| panic!("{} failed to parse", entry.path().display())); } } } diff --git a/src/stringpool.rs b/src/stringpool.rs index ed19c3e..44f4c22 100644 --- a/src/stringpool.rs +++ b/src/stringpool.rs @@ -22,7 +22,7 @@ pub(crate) struct StringPoolHeader { #[derive(Debug, DekuRead)] pub(crate) struct StringPool { pub(crate) header: StringPoolHeader, - #[deku(reader = "StringPool::read_strings(&*header, deku::rest)")] + #[deku(reader = "StringPool::read_strings(header, deku::rest)")] pub(crate) strings: Vec>, } @@ -32,11 +32,12 @@ impl StringPool { header: &StringPoolHeader, mut rest: &'a DekuRest, ) -> Result<(&'a DekuRest, Vec>), DekuError> { + const STRINGPOOL_HEADER_SIZE: usize = std::mem::size_of::(); + assert_eq!(header.style_count, 0); let flag_is_utf8 = (header.flags & (1 << 8)) != 0; - const STRINGPOOL_HEADER_SIZE: usize = std::mem::size_of::(); let s = usize::try_from(header.chunk_header.size).unwrap() - STRINGPOOL_HEADER_SIZE; let mut string_pool_data = vec![0; s]; diff --git a/src/xml.rs b/src/xml.rs index 121e8a0..35eedb6 100644 --- a/src/xml.rs +++ b/src/xml.rs @@ -65,7 +65,7 @@ impl XmlDocument { element_tracker .last_mut() .unwrap() - .insert_children(Node::Cdata(cdata)) + .insert_children(Node::Cdata(cdata)); } }; } @@ -74,6 +74,7 @@ impl XmlDocument { } ///Returns the root [Element] of the XML document. + #[must_use] pub fn get_root(&self) -> &Option { &self.root } @@ -130,7 +131,14 @@ impl XmlDocument { let value = attr.typed_value.get_value(string_pool); let mut final_name = String::new(); - if !name.is_empty() { + if name.is_empty() { + let resource_id = resource_map + .get(usize::try_from(attr.name).unwrap()) + .ok_or(ParseError::ResourceIdNotFound(attr.name))?; + let resource_str = get_resource_string(*resource_id) + .ok_or(ParseError::UnknownResourceString(*resource_id))?; + final_name.push_str(&resource_str); + } else { if let Some(n) = ns { // There are samples where the namespace value is the // raw string instead of a URI found in a namespace chunk. @@ -143,13 +151,6 @@ impl XmlDocument { }; } final_name.push_str(&name); - } else { - let resource_id = resource_map - .get(usize::try_from(attr.name).unwrap()) - .ok_or(ParseError::ResourceIdNotFound(attr.name))?; - let resource_str = get_resource_string(*resource_id) - .ok_or(ParseError::UnknownResourceString(*resource_id))?; - final_name.push_str(&resource_str); } attributes.insert(final_name, value.to_string()); @@ -180,16 +181,19 @@ pub struct Element { impl Element { ///Returns a map of attributes associated with the element. + #[must_use] pub fn get_attributes(&self) -> &IndexMap { &self.attributes } ///Returns the element tag. + #[must_use] pub fn get_tag(&self) -> &str { &self.tag } ///Returns a list of child nodes. + #[must_use] pub fn get_children(&self) -> &Vec { &self.children } @@ -206,6 +210,7 @@ pub struct Cdata { } impl Cdata { + #[must_use] pub fn get_data(&self) -> &str { &self.data } @@ -1546,11 +1551,7 @@ fn get_resource_string(resource_id: u32) -> Option { "colorSecondary", ]; - let i = resource_id - 0x1010000; + let i = resource_id - 0x0101_0000; - Some( - RESOURCE_STRINGS - .get(usize::try_from(i).unwrap())? - .to_string(), - ) + Some((*RESOURCE_STRINGS.get(usize::try_from(i).unwrap())?).to_string()) }