| 1 | use std::mem; |
| 2 | |
| 3 | use crate::{hwdb, Error, Result}; |
| 4 | |
| 5 | use super::{TrieChildEntry, TrieNode, TrieValueEntry}; |
| 6 | |
| 7 | /// Represents the full Trie entry in the HWDB. |
| 8 | #[repr(C)] |
| 9 | #[derive(Clone, Debug, PartialEq)] |
| 10 | pub struct TrieEntry { |
| 11 | node: TrieNode, |
| 12 | children: Vec<TrieChildEntry>, |
| 13 | values: Vec<TrieValueEntry>, |
| 14 | } |
| 15 | |
| 16 | impl TrieEntry { |
| 17 | /// Creates a new [TrieEntry]. |
| 18 | pub const fn new() -> Self { |
| 19 | Self { |
| 20 | node: TrieNode::new(), |
| 21 | children: Vec::new(), |
| 22 | values: Vec::new(), |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | /// Gets a reference to the [TrieNode]. |
| 27 | pub const fn node(&self) -> &TrieNode { |
| 28 | &self.node |
| 29 | } |
| 30 | |
| 31 | /// Gets a reference to the list of [TrieChildEntry]. |
| 32 | pub fn children(&self) -> &[TrieChildEntry] { |
| 33 | self.children.as_ref() |
| 34 | } |
| 35 | |
| 36 | /// Gets a reference to the list of [TrieValueEntry]. |
| 37 | pub fn values(&self) -> &[TrieValueEntry] { |
| 38 | self.values.as_ref() |
| 39 | } |
| 40 | |
| 41 | /// Gets the total length of the [TrieEntry]. |
| 42 | pub fn len(&self) -> usize { |
| 43 | let children_len = self.children.len().saturating_mul(hwdb::child_entry_size()); |
| 44 | let values_len = self.values.len().saturating_mul(hwdb::value_entry_size()); |
| 45 | |
| 46 | mem::size_of::<TrieNode>() |
| 47 | .saturating_add(children_len) |
| 48 | .saturating_add(values_len) |
| 49 | } |
| 50 | |
| 51 | /// Gets whether the [TrieValueEntry] has no children and values. |
| 52 | pub fn is_empty(&self) -> bool { |
| 53 | self.children.is_empty() && self.values.is_empty() |
| 54 | } |
| 55 | |
| 56 | /// Looks up a child node in the HWDB buffer. |
| 57 | /// |
| 58 | /// Parameters: |
| 59 | /// |
| 60 | /// - `hwdb_buf`: in-memory buffer of the entire HWDB. |
| 61 | /// - `c`: Child index to search the list of [TrieChildEntry]. |
| 62 | /// |
| 63 | /// Returns [Some(TrieNode)](TrieNode) on success, [`None`] otherwise. |
| 64 | pub fn lookup_child(&self, hwdb_buf: &[u8], c: u8) -> Option<Self> { |
| 65 | let search = TrieChildEntry::new().with_c(c); |
| 66 | let buf_len = hwdb_buf.len(); |
| 67 | |
| 68 | // assuming children are sorted (done in initialisation), perform a binary search instead like C hwdb |
| 69 | let child = self |
| 70 | .children |
| 71 | .binary_search_by(|child| child.cmp(&search)) |
| 72 | .ok() |
| 73 | .and_then(|idx| self.children.get(idx))?; |
| 74 | |
| 75 | let child_off = child.child_off() as usize; |
| 76 | |
| 77 | // if the child offset is in range, attempt to construct a `TrieNode` at that offset |
| 78 | if (0..buf_len).contains(&child_off) { |
| 79 | Self::try_from(&hwdb_buf[child_off..]).ok() |
| 80 | } else { |
| 81 | None |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | impl Default for TrieEntry { |
| 87 | fn default() -> Self { |
| 88 | Self::new() |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | impl TryFrom<&[u8]> for TrieEntry { |
| 93 | type Error = Error; |
| 94 | |
| 95 | fn try_from(val: &[u8]) -> Result<Self> { |
| 96 | let node = TrieNode::try_from(val)?; |
| 97 | |
| 98 | let mut idx = hwdb::node_size(); |
| 99 | |
| 100 | let val_end = val.len(); |
| 101 | let child_len = hwdb::child_entry_size(); |
| 102 | let child_count = node.children_count() as usize; |
| 103 | let child_end = idx.saturating_add(child_count.saturating_mul(child_len)); |
| 104 | |
| 105 | let mut children: Vec<TrieChildEntry> = Vec::with_capacity(child_count); |
| 106 | |
| 107 | if (idx..val_end).contains(&child_end) && child_count > 0 { |
| 108 | for c in val[idx..].chunks_exact(child_len).take(child_count) { |
| 109 | children.push(c.try_into()?); |
| 110 | idx = idx.saturating_add(child_len); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | children.sort(); |
| 115 | |
| 116 | let value_len = hwdb::value_entry_size(); |
| 117 | let value_count = node.values_count() as usize; |
| 118 | let value_end = idx.saturating_add(value_count.saturating_mul(value_len)); |
| 119 | |
| 120 | let mut values: Vec<TrieValueEntry> = Vec::with_capacity(value_count); |
| 121 | |
| 122 | if (idx..val_end).contains(&value_end) && value_count > 0 { |
| 123 | for c in val[idx..].chunks_exact(value_len).take(value_count) { |
| 124 | values.push(c.try_into()?); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | Ok(Self { |
| 129 | node, |
| 130 | children, |
| 131 | values, |
| 132 | }) |
| 133 | } |
| 134 | } |