| 1 | mod child_entry; |
| 2 | mod entry; |
| 3 | mod header; |
| 4 | mod node; |
| 5 | mod value_entry; |
| 6 | |
| 7 | use crate::{Error, Result}; |
| 8 | pub use child_entry::*; |
| 9 | pub use entry::*; |
| 10 | pub use header::*; |
| 11 | pub use node::*; |
| 12 | pub use value_entry::*; |
| 13 | |
| 14 | /// Hardware database signature. |
| 15 | pub const HWDB_SIG: [u8; 8] = [b'K', b'S', b'L', b'P', b'H', b'H', b'R', b'H']; |
| 16 | /// Hardware database signature (string representation). |
| 17 | pub const HWDB_SIG_STR: &str = "KSLPHHRH"; |
| 18 | |
| 19 | /// Parses a string from the HWDB buffer. |
| 20 | pub fn trie_string(hwdb_buf: &[u8], offset: usize) -> Result<&str> { |
| 21 | let buf_len = hwdb_buf.len(); |
| 22 | if (0..buf_len).contains(&offset) { |
| 23 | let str_end = hwdb_buf[offset..] |
| 24 | .iter() |
| 25 | .position(|c| c == &b'\0' || c == &b'\n') |
| 26 | .map(|end| offset + end) |
| 27 | .unwrap_or(buf_len); |
| 28 | |
| 29 | std::str::from_utf8(&hwdb_buf[offset..str_end]) |
| 30 | .map_err(|_| Error::UdevHwdb("failed to parse utf-8 trie_string".to_string())) |
| 31 | } else { |
| 32 | Err(Error::UdevHwdb("invalid trie_string offset".to_string())) |
| 33 | } |
| 34 | } |