| 1 | use std::mem; |
| 2 | |
| 3 | use crate::{Error, Result}; |
| 4 | |
| 5 | use super::{HWDB_SIG, HWDB_SIG_STR}; |
| 6 | |
| 7 | /// On-disk trie objects |
| 8 | #[repr(C, packed(8))] |
| 9 | #[derive(Clone, Copy, Debug, Default, PartialEq)] |
| 10 | pub struct TrieHeader { |
| 11 | signature: [u8; 8], |
| 12 | tool_version: u64, |
| 13 | file_size: u64, |
| 14 | header_size: u64, |
| 15 | node_size: u64, |
| 16 | child_entry_size: u64, |
| 17 | value_entry_size: u64, |
| 18 | nodes_root_off: u64, |
| 19 | nodes_len: u64, |
| 20 | strings_len: u64, |
| 21 | } |
| 22 | |
| 23 | impl TrieHeader { |
| 24 | /// Creates a new [TrieHeader]. |
| 25 | pub const fn new() -> Self { |
| 26 | Self { |
| 27 | signature: HWDB_SIG, |
| 28 | tool_version: 0, |
| 29 | file_size: 0, |
| 30 | header_size: 0, |
| 31 | node_size: 0, |
| 32 | child_entry_size: 0, |
| 33 | value_entry_size: 0, |
| 34 | nodes_root_off: 0, |
| 35 | nodes_len: 0, |
| 36 | strings_len: 0, |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /// Gets the [TrieHeader] object signature. |
| 41 | pub fn signature(&self) -> &str { |
| 42 | std::str::from_utf8(self.signature.as_ref()).unwrap_or("") |
| 43 | } |
| 44 | |
| 45 | /// Version of the tool which created the file. |
| 46 | pub const fn tool_version(&self) -> u64 { |
| 47 | self.tool_version |
| 48 | } |
| 49 | |
| 50 | /// Sets the version of the tool which created the file. |
| 51 | pub fn set_tool_version(&mut self, val: u64) { |
| 52 | self.tool_version = val; |
| 53 | } |
| 54 | |
| 55 | /// Builder function that sets the version of the tool which created the file. |
| 56 | pub fn with_tool_version(mut self, val: u64) -> Self { |
| 57 | self.set_tool_version(val); |
| 58 | self |
| 59 | } |
| 60 | |
| 61 | /// Gets the file size of the database file. |
| 62 | pub const fn file_size(&self) -> u64 { |
| 63 | self.file_size |
| 64 | } |
| 65 | |
| 66 | /// Sets the file size of the database file. |
| 67 | pub fn set_file_size(&mut self, val: u64) { |
| 68 | self.file_size = val; |
| 69 | } |
| 70 | |
| 71 | /// Builder function that sets the file size of the database file. |
| 72 | pub fn with_file_size(mut self, val: u64) -> Self { |
| 73 | self.set_file_size(val); |
| 74 | self |
| 75 | } |
| 76 | |
| 77 | /// Gets the header size of the [TrieHeader]. |
| 78 | pub const fn header_size(&self) -> u64 { |
| 79 | self.header_size |
| 80 | } |
| 81 | |
| 82 | /// Sets the header size of the [TrieHeader]. |
| 83 | pub fn set_header_size(&mut self, val: u64) { |
| 84 | self.header_size = val; |
| 85 | } |
| 86 | |
| 87 | /// Builder function that sets the header size of the [TrieHeader]. |
| 88 | pub fn with_header_size(mut self, val: u64) -> Self { |
| 89 | self.set_header_size(val); |
| 90 | self |
| 91 | } |
| 92 | |
| 93 | /// Gets the node size of the database file. |
| 94 | pub const fn node_size(&self) -> u64 { |
| 95 | self.node_size |
| 96 | } |
| 97 | |
| 98 | /// Sets the node size of the database file. |
| 99 | pub fn set_node_size(&mut self, val: u64) { |
| 100 | self.node_size = val; |
| 101 | } |
| 102 | |
| 103 | /// Builder function that sets the node size of the database file. |
| 104 | pub fn with_node_size(mut self, val: u64) -> Self { |
| 105 | self.set_node_size(val); |
| 106 | self |
| 107 | } |
| 108 | |
| 109 | /// Gets the child entry size of the database file. |
| 110 | pub const fn child_entry_size(&self) -> u64 { |
| 111 | self.child_entry_size |
| 112 | } |
| 113 | |
| 114 | /// Sets the child entry size of the database file. |
| 115 | pub fn set_child_entry_size(&mut self, val: u64) { |
| 116 | self.child_entry_size = val; |
| 117 | } |
| 118 | |
| 119 | /// Builder function that sets child entry size of the database file. |
| 120 | pub fn with_child_entry_size(mut self, val: u64) -> Self { |
| 121 | self.set_child_entry_size(val); |
| 122 | self |
| 123 | } |
| 124 | |
| 125 | /// Gets the value entry size of the database file. |
| 126 | pub const fn value_entry_size(&self) -> u64 { |
| 127 | self.value_entry_size |
| 128 | } |
| 129 | |
| 130 | /// Sets the value entry size of the database file. |
| 131 | pub fn set_value_entry_size(&mut self, val: u64) { |
| 132 | self.value_entry_size = val; |
| 133 | } |
| 134 | |
| 135 | /// Builder function that sets value entry size of the database file. |
| 136 | pub fn with_value_entry_size(mut self, val: u64) -> Self { |
| 137 | self.set_value_entry_size(val); |
| 138 | self |
| 139 | } |
| 140 | |
| 141 | /// Gets the offset of the root trie node. |
| 142 | pub const fn nodes_root_off(&self) -> u64 { |
| 143 | self.nodes_root_off |
| 144 | } |
| 145 | |
| 146 | /// Sets the offset of the root trie node. |
| 147 | pub fn set_nodes_root_off(&mut self, val: u64) { |
| 148 | self.nodes_root_off = val; |
| 149 | } |
| 150 | |
| 151 | /// Builder function that sets the offset of the root trie node. |
| 152 | pub fn with_nodes_root_off(mut self, val: u64) -> Self { |
| 153 | self.set_nodes_root_off(val); |
| 154 | self |
| 155 | } |
| 156 | |
| 157 | /// Gets the size of the nodes section. |
| 158 | pub const fn nodes_len(&self) -> u64 { |
| 159 | self.nodes_len |
| 160 | } |
| 161 | |
| 162 | /// Sets the size of the nodes section. |
| 163 | pub fn set_nodes_len(&mut self, val: u64) { |
| 164 | self.nodes_len = val; |
| 165 | } |
| 166 | |
| 167 | /// Builder function that sets the size of the nodes section. |
| 168 | pub fn with_nodes_len(mut self, val: u64) -> Self { |
| 169 | self.set_nodes_len(val); |
| 170 | self |
| 171 | } |
| 172 | |
| 173 | /// Gets the size of the strings section. |
| 174 | pub const fn strings_len(&self) -> u64 { |
| 175 | self.strings_len |
| 176 | } |
| 177 | |
| 178 | /// Sets the size of the strings section. |
| 179 | pub fn set_strings_len(&mut self, val: u64) { |
| 180 | self.strings_len = val; |
| 181 | } |
| 182 | |
| 183 | /// Builder function that sets the size of the strings section. |
| 184 | pub fn with_strings_len(mut self, val: u64) -> Self { |
| 185 | self.set_strings_len(val); |
| 186 | self |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | impl TryFrom<&[u8]> for TrieHeader { |
| 191 | type Error = Error; |
| 192 | |
| 193 | fn try_from(val: &[u8]) -> Result<Self> { |
| 194 | if val.len() < mem::size_of::<Self>() { |
| 195 | Err(Error::InvalidLen(val.len())) |
| 196 | } else { |
| 197 | let mut idx = 0usize; |
| 198 | |
| 199 | let signature: [u8; 8] = val[idx..idx + 8].try_into()?; |
| 200 | |
| 201 | if signature != HWDB_SIG { |
| 202 | let sig_str = std::str::from_utf8(signature.as_ref()).unwrap_or(""); |
| 203 | Err(Error::UdevHwdb(format!( |
| 204 | "invalid HWDB signature, have: {sig_str}, expected: {HWDB_SIG_STR}" |
| 205 | ))) |
| 206 | } else { |
| 207 | idx += signature.len(); |
| 208 | |
| 209 | let tool_version = u64::from_le_bytes(val[idx..idx + 8].try_into()?); |
| 210 | idx += mem::size_of::<u64>(); |
| 211 | |
| 212 | let file_size = u64::from_le_bytes(val[idx..idx + 8].try_into()?); |
| 213 | idx += mem::size_of::<u64>(); |
| 214 | |
| 215 | let header_size = u64::from_le_bytes(val[idx..idx + 8].try_into()?); |
| 216 | idx += mem::size_of::<u64>(); |
| 217 | |
| 218 | let node_size = u64::from_le_bytes(val[idx..idx + 8].try_into()?); |
| 219 | idx += mem::size_of::<u64>(); |
| 220 | |
| 221 | let child_entry_size = u64::from_le_bytes(val[idx..idx + 8].try_into()?); |
| 222 | idx += mem::size_of::<u64>(); |
| 223 | |
| 224 | let value_entry_size = u64::from_le_bytes(val[idx..idx + 8].try_into()?); |
| 225 | idx += mem::size_of::<u64>(); |
| 226 | |
| 227 | let nodes_root_off = u64::from_le_bytes(val[idx..idx + 8].try_into()?); |
| 228 | idx += mem::size_of::<u64>(); |
| 229 | |
| 230 | let nodes_len = u64::from_le_bytes(val[idx..idx + 8].try_into()?); |
| 231 | idx += mem::size_of::<u64>(); |
| 232 | |
| 233 | let strings_len = u64::from_le_bytes(val[idx..idx + 8].try_into()?); |
| 234 | |
| 235 | Ok(Self { |
| 236 | signature, |
| 237 | tool_version, |
| 238 | file_size, |
| 239 | header_size, |
| 240 | node_size, |
| 241 | child_entry_size, |
| 242 | value_entry_size, |
| 243 | nodes_root_off, |
| 244 | nodes_len, |
| 245 | strings_len, |
| 246 | }) |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | } |