1use super::arrays::SimpleArrayView;
6use super::parser::{PolicyCursor, PolicyData, PolicyOffset};
7use super::{Counted, Parse, PolicyValidationContext, Validate};
8
9use hashbrown::hash_table::HashTable;
10use rapidhash::RapidHasher;
11use static_assertions::const_assert;
12use std::fmt::Debug;
13use std::hash::{Hash, Hasher};
14use std::marker::PhantomData;
15use zerocopy::{FromBytes, Immutable, KnownLayout, Unaligned, little_endian as le};
16
17pub trait HasMetadata {
23 type Metadata: FromBytes + Sized;
25}
26
27pub trait Walk {
31 fn walk(policy_data: &PolicyData, offset: PolicyOffset) -> PolicyOffset;
35}
36
37#[derive(Debug, Clone, Copy)]
42pub struct View<T> {
43 phantom: PhantomData<T>,
44
45 start: PolicyOffset,
47
48 end: PolicyOffset,
50}
51
52impl<T> View<T> {
53 pub fn new(start: PolicyOffset, end: PolicyOffset) -> Self {
55 Self { phantom: PhantomData, start, end }
56 }
57
58 fn start(&self) -> PolicyOffset {
60 self.start
61 }
62}
63
64impl<T: Sized> View<T> {
65 pub fn at(start: PolicyOffset) -> Self {
69 let end = start + std::mem::size_of::<T>() as u32;
70 Self::new(start, end)
71 }
72}
73
74impl<T: FromBytes + Sized> View<T> {
75 pub fn read(&self, policy_data: &PolicyData) -> T {
82 debug_assert_eq!(self.end - self.start, std::mem::size_of::<T>() as u32);
83 let start = self.start as usize;
84 let end = self.end as usize;
85 T::read_from_bytes(&policy_data[start..end]).unwrap()
86 }
87}
88
89impl<T: HasMetadata> View<T> {
90 pub fn metadata(&self) -> View<T::Metadata> {
94 View::<T::Metadata>::at(self.start)
95 }
96
97 pub fn read_metadata(&self, policy_data: &PolicyData) -> T::Metadata {
99 self.metadata().read(policy_data)
100 }
101}
102
103impl<T: Parse> View<T> {
104 pub fn parse(&self, policy_data: &PolicyData) -> T {
110 let cursor = PolicyCursor::new_at(policy_data, self.start);
111 let (object, _) =
112 T::parse(cursor).map_err(Into::<anyhow::Error>::into).expect("policy should be valid");
113 object
114 }
115}
116
117impl<T: Validate + Parse> Validate for View<T> {
118 type Error = anyhow::Error;
119
120 fn validate(&self, context: &PolicyValidationContext) -> Result<(), Self::Error> {
121 let object = self.parse(&context.data);
122 object.validate(context).map_err(Into::<anyhow::Error>::into)
123 }
124}
125
126#[derive(Debug, Clone, Copy)]
131pub struct ArrayDataView<D> {
132 phantom: PhantomData<D>,
133 start: PolicyOffset,
134 count: u32,
135}
136
137impl<D> ArrayDataView<D> {
138 pub fn new(start: PolicyOffset, count: u32) -> Self {
140 Self { phantom: PhantomData, start, count }
141 }
142
143 pub fn iter(self, policy_data: &PolicyData) -> ArrayDataViewIter<D> {
150 ArrayDataViewIter::new(policy_data.clone(), self.start, self.count)
151 }
152}
153
154pub struct ArrayDataViewIter<D> {
159 phantom: PhantomData<D>,
160 policy_data: PolicyData,
161 offset: PolicyOffset,
162 remaining: u32,
163}
164
165impl<T> ArrayDataViewIter<T> {
166 fn new(policy_data: PolicyData, offset: PolicyOffset, remaining: u32) -> Self {
168 Self { phantom: PhantomData, policy_data, offset, remaining }
169 }
170}
171
172impl<D: Walk> std::iter::Iterator for ArrayDataViewIter<D> {
173 type Item = View<D>;
174
175 fn next(&mut self) -> Option<Self::Item> {
176 if self.remaining > 0 {
177 let start = self.offset;
178 self.offset = D::walk(&self.policy_data, start);
179 self.remaining -= 1;
180 Some(View::new(start, self.offset))
181 } else {
182 None
183 }
184 }
185}
186
187#[derive(Debug, Clone, Copy)]
192pub(super) struct ArrayView<M, D> {
193 phantom: PhantomData<(M, D)>,
194 start: PolicyOffset,
195 count: u32,
196}
197
198impl<M, D> ArrayView<M, D> {
199 pub fn new(start: PolicyOffset, count: u32) -> Self {
201 Self { phantom: PhantomData, start, count }
202 }
203}
204
205impl<M: Sized, D> ArrayView<M, D> {
206 pub fn metadata(&self) -> View<M> {
208 View::<M>::at(self.start)
209 }
210
211 pub fn data(&self) -> ArrayDataView<D> {
213 ArrayDataView::new(self.metadata().end, self.count)
214 }
215}
216
217fn parse_array_data<'a, D: Parse>(
218 cursor: PolicyCursor<'a>,
219 count: u32,
220) -> Result<PolicyCursor<'a>, anyhow::Error> {
221 let mut tail = cursor;
222 for _ in 0..count {
223 let (_, next) = D::parse(tail).map_err(Into::<anyhow::Error>::into)?;
224 tail = next;
225 }
226 Ok(tail)
227}
228
229impl<M: Counted + Parse + Sized, D: Parse> Parse for ArrayView<M, D> {
230 type Error = anyhow::Error;
233
234 fn parse<'a>(cursor: PolicyCursor<'a>) -> Result<(Self, PolicyCursor<'a>), Self::Error> {
235 let start = cursor.offset();
236 let (metadata, cursor) = M::parse(cursor).map_err(Into::<anyhow::Error>::into)?;
237 let count = metadata.count();
238 let cursor = parse_array_data::<D>(cursor, count)?;
239 Ok((Self::new(start, count), cursor))
240 }
241}
242
243pub(super) trait Hashable {
247 type Key: Parse + Hash + Eq;
248 type Value: Parse + Walk;
249
250 fn key(&self) -> &Self::Key;
252
253 fn values(&self) -> &SimpleArrayView<Self::Value>;
255}
256
257#[derive(Debug, Clone)]
259pub(super) struct CustomKeyHashedView<D: Hashable> {
260 index: HashTable<PolicyOffset>,
262 _phantom: PhantomData<D>,
263}
264
265impl<D: Hashable + Parse> CustomKeyHashedView<D> {
266 pub(super) fn find_all(
269 &self,
270 query_key: D::Key,
271 policy_data: &PolicyData,
272 ) -> impl Iterator<Item = D::Value> {
273 let key_offset = self.index.find(compute_hash(&query_key), |&key_offset| {
274 let cursor = PolicyCursor::new_at(policy_data, key_offset);
275 let (key, _) = D::Key::parse(cursor)
276 .map_err(Into::<anyhow::Error>::into)
277 .expect("policy should be valid");
278
279 key == query_key
280 });
281
282 key_offset.into_iter().flat_map(move |&key_offset| {
283 let cursor = PolicyCursor::new_at(policy_data, key_offset);
284 let (entry, _) = D::parse(cursor)
285 .map_err(Into::<anyhow::Error>::into)
286 .expect("policy should be valid");
287
288 entry.values().data().iter(policy_data).map(move |v| v.parse(policy_data))
289 })
290 }
291
292 pub(super) fn iter<'a>(
293 &'a self,
294 policy_data: &'a PolicyData,
295 ) -> impl Iterator<Item = Result<D, anyhow::Error>> + 'a {
296 self.index.iter().map(move |&offset| {
297 let cursor = PolicyCursor::new_at(policy_data, offset);
298 let (entry, _) = D::parse(cursor).map_err(Into::<anyhow::Error>::into)?;
299 Ok(entry)
300 })
301 }
302}
303
304fn compute_hash<V: Hash>(val: &V) -> u64 {
305 let mut hasher = RapidHasher::default();
306 val.hash(&mut hasher);
307 hasher.finish()
308}
309
310impl<D: Hashable + Parse> Parse for CustomKeyHashedView<D> {
311 type Error = anyhow::Error;
312
313 fn parse<'a>(cursor: PolicyCursor<'a>) -> Result<(Self, PolicyCursor<'a>), Self::Error> {
315 let (metadata, cursor) = le::U32::parse(cursor).map_err(Into::<anyhow::Error>::into)?;
317 let count = metadata.count();
318
319 let mut index = HashTable::with_capacity(count as usize);
321
322 let mut key_offset = cursor.offset();
323 let mut tail = cursor;
324 for _ in 0..count {
325 let (entry, next) = D::parse(tail).map_err(Into::<anyhow::Error>::into)?;
326 tail = next;
327
328 let key: &D::Key = entry.key();
329 index.insert_unique(compute_hash(&key), key_offset, |&key_offset| {
330 let policy_cursor = PolicyCursor::new_at(tail.data(), key_offset);
331 let (key, _) = D::Key::parse(policy_cursor)
332 .map_err(Into::<anyhow::Error>::into)
333 .expect("policy should be valid");
334 compute_hash::<D::Key>(&key)
335 });
336 key_offset = tail.offset();
337 }
338
339 Ok((Self { _phantom: PhantomData, index }, tail))
340 }
341}
342
343impl<D: Hashable + Parse> Validate for CustomKeyHashedView<D>
344where
345 SimpleArrayView<D::Value>: Validate<Error = anyhow::Error>,
346{
347 type Error = anyhow::Error;
348
349 fn validate(&self, context: &PolicyValidationContext) -> Result<(), Self::Error> {
350 for key_offset in self.index.iter() {
351 let cursor = PolicyCursor::new_at(&context.data, *key_offset);
352 let (entry, _) = D::parse(cursor).map_err(Into::<anyhow::Error>::into)?;
353
354 entry.values().validate(context)?;
355 }
356 Ok(())
357 }
358}
359
360struct HashedArrayViewEntryIter<'a, D: HasMetadata> {
363 policy_data: &'a PolicyData,
364 limit: PolicyOffset,
365 metadata: D::Metadata,
366 offset: Option<PolicyOffset>,
367}
368
369#[derive(Clone, Copy, Debug, KnownLayout, FromBytes, Immutable, PartialEq, Unaligned)]
376#[repr(C, packed)]
377pub(super) struct U24 {
378 low: u8,
379 middle: u8,
380 high: u8,
381}
382
383const_assert!(std::mem::size_of::<U24>() == 3);
385const_assert!(std::mem::align_of::<U24>() == 1);
386
387impl TryFrom<u32> for U24 {
388 type Error = ();
389
390 fn try_from(value: u32) -> Result<Self, Self::Error> {
391 if 0x1000000 <= value {
392 Err(())
393 } else {
394 Ok(Self {
395 low: (value & 0xff) as u8,
396 middle: ((value >> 8) & 0xff) as u8,
397 high: ((value >> 16) & 0xff) as u8,
398 })
399 }
400 }
401}
402
403impl From<U24> for u32 {
404 fn from(value: U24) -> u32 {
405 ((value.high as u32) << 16) + ((value.middle as u32) << 8) + (value.low as u32)
406 }
407}
408
409impl<'a, D: HasMetadata + Walk> Iterator for HashedArrayViewEntryIter<'a, D>
410where
411 D::Metadata: Eq,
412{
413 type Item = View<D>;
414
415 fn next(&mut self) -> Option<Self::Item> {
416 if let Some(offset) = self.offset
417 && offset < self.limit
418 {
419 let element = View::<D>::at(offset);
420 let metadata = element.read_metadata(&self.policy_data);
421 if metadata == self.metadata {
422 self.offset = Some(D::walk(&self.policy_data, offset));
423 Some(element)
424 } else {
425 self.offset = None;
426 None
427 }
428 } else {
429 None
430 }
431 }
432}
433
434#[derive(Debug, Clone)]
439pub(super) struct HashedArrayView<D: HasMetadata> {
440 phantom: PhantomData<D>,
441 index: HashTable<U24>,
442 limit: PolicyOffset,
446}
447
448impl<D: HasMetadata> HashedArrayView<D>
449where
450 D::Metadata: Hash,
451{
452 fn metadata_hash(metadata: &D::Metadata) -> u64 {
453 let mut hasher = RapidHasher::default();
454 metadata.hash(&mut hasher);
455 hasher.finish()
456 }
457}
458
459impl<D: Parse + HasMetadata + Walk> HashedArrayView<D>
460where
461 D::Metadata: Eq + PartialEq + Hash + Debug,
462{
463 pub fn find(&self, key: D::Metadata, policy_data: &PolicyData) -> Option<D> {
468 let key_hash = Self::metadata_hash(&key);
469 let offset = self.index.find(key_hash, |&offset| {
470 let element = View::<D>::at(u32::from(offset));
471 key == element.read_metadata(policy_data)
472 })?;
473 let element = View::<D>::at(u32::from(*offset));
474 Some(element.parse(policy_data))
475 }
476
477 pub(super) fn find_all(
480 &self,
481 key: D::Metadata,
482 policy_data: &PolicyData,
483 ) -> impl Iterator<Item = D> {
484 let key_hash = Self::metadata_hash(&key);
485 let offset = self.index.find(key_hash, |&offset| {
486 let element = View::<D>::at(u32::from(offset));
487 key == element.read_metadata(policy_data)
488 });
489 (HashedArrayViewEntryIter {
490 policy_data: policy_data,
491 limit: self.limit,
492 metadata: key,
493 offset: offset.map(|offset| u32::from(*offset)),
494 })
495 .map(|element| element.parse(policy_data))
496 }
497
498 pub(super) fn iter(&self, policy_data: &PolicyData) -> impl Iterator<Item = View<D>> {
500 self.index
501 .iter()
502 .map(|offset| {
503 let element = View::<D>::at(u32::from(*offset));
504 HashedArrayViewEntryIter {
505 policy_data: policy_data,
506 limit: self.limit,
507 metadata: element.read_metadata(policy_data),
508 offset: Some(u32::from(*offset)),
509 }
510 })
511 .flatten()
512 }
513}
514
515impl<D: Parse + HasMetadata + Walk> Parse for HashedArrayView<D>
516where
517 D::Metadata: Eq + Debug + PartialEq + Parse + Hash,
518{
519 type Error = anyhow::Error;
520
521 fn parse<'a>(cursor: PolicyCursor<'a>) -> Result<(Self, PolicyCursor<'a>), Self::Error> {
522 let (array_view, cursor) = SimpleArrayView::<D>::parse(cursor)?;
523
524 let mut index = HashTable::with_capacity(array_view.count as usize);
526
527 let limit = cursor.offset();
529
530 for view in array_view.data().iter(cursor.data()) {
533 let metadata = view.read_metadata(cursor.data());
534
535 index
536 .entry(
537 Self::metadata_hash(&metadata),
538 |&offset| {
539 let element = View::<D>::at(u32::from(offset));
540 metadata == element.read_metadata(cursor.data())
541 },
542 |&offset| {
543 let element = View::<D>::at(u32::from(offset));
544 Self::metadata_hash(&element.read_metadata(cursor.data()))
545 },
546 )
547 .or_insert(U24::try_from(view.start()).expect("Policy offsets ought fit in U24!"));
548 }
549
550 Ok((Self { phantom: PhantomData, index, limit }, cursor))
551 }
552}
553
554impl<D: Validate + Parse + HasMetadata + Walk> Validate for HashedArrayView<D>
555where
556 D::Metadata: Eq,
557{
558 type Error = anyhow::Error;
559
560 fn validate(&self, context: &PolicyValidationContext) -> Result<(), Self::Error> {
561 let policy_data = context.data.clone();
562 for element in self
563 .index
564 .iter()
565 .map(|offset| {
566 let element = View::<D>::at(u32::from(*offset));
567 HashedArrayViewEntryIter::<D> {
568 policy_data: &policy_data,
569 limit: self.limit,
570 metadata: element.read_metadata(&policy_data),
571 offset: Some(u32::from(*offset)),
572 }
573 })
574 .flatten()
575 {
576 element.validate(context)?;
577 }
578
579 Ok(())
580 }
581}
582
583#[cfg(test)]
584mod tests {
585 use super::U24;
586
587 #[test]
588 fn to_and_from_u24() {
589 for i in 0u32..0x10000 {
590 let u24_result = U24::try_from(i);
591 assert!(u24_result.is_ok());
592 let u24 = u24_result.unwrap();
593 assert_eq!(i >> 16, u24.high as u32);
594 assert_eq!((i >> 8) & 0xff, u24.middle as u32);
595 assert_eq!(i & 0xff, u24.low as u32);
596 let j = u32::from(u24);
597 assert_eq!(i, j);
598 }
599
600 assert!(U24::try_from(0x1000000).is_err());
601 }
602}