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
293fn compute_hash<V: Hash>(val: &V) -> u64 {
294 let mut hasher = RapidHasher::default();
295 val.hash(&mut hasher);
296 hasher.finish()
297}
298
299impl<D: Hashable + Parse> Parse for CustomKeyHashedView<D> {
300 type Error = anyhow::Error;
301
302 fn parse<'a>(cursor: PolicyCursor<'a>) -> Result<(Self, PolicyCursor<'a>), Self::Error> {
304 let (metadata, cursor) = le::U32::parse(cursor).map_err(Into::<anyhow::Error>::into)?;
306 let count = metadata.count();
307
308 let mut index = HashTable::with_capacity(count as usize);
310
311 let mut key_offset = cursor.offset();
312 let mut tail = cursor;
313 for _ in 0..count {
314 let (entry, next) = D::parse(tail).map_err(Into::<anyhow::Error>::into)?;
315 tail = next;
316
317 let key: &D::Key = entry.key();
318 index.insert_unique(compute_hash(&key), key_offset, |&key_offset| {
319 let policy_cursor = PolicyCursor::new_at(tail.data(), key_offset);
320 let (key, _) = D::Key::parse(policy_cursor)
321 .map_err(Into::<anyhow::Error>::into)
322 .expect("policy should be valid");
323 compute_hash::<D::Key>(&key)
324 });
325 key_offset = tail.offset();
326 }
327
328 Ok((Self { _phantom: PhantomData, index }, tail))
329 }
330}
331
332impl<D: Hashable + Parse> Validate for CustomKeyHashedView<D>
333where
334 SimpleArrayView<D::Value>: Validate<Error = anyhow::Error>,
335{
336 type Error = anyhow::Error;
337
338 fn validate(&self, context: &PolicyValidationContext) -> Result<(), Self::Error> {
339 for key_offset in self.index.iter() {
340 let cursor = PolicyCursor::new_at(&context.data, *key_offset);
341 let (entry, _) = D::parse(cursor).map_err(Into::<anyhow::Error>::into)?;
342
343 entry.values().validate(context)?;
344 }
345 Ok(())
346 }
347}
348
349struct HashedArrayViewEntryIter<'a, D: HasMetadata> {
352 policy_data: &'a PolicyData,
353 limit: PolicyOffset,
354 metadata: D::Metadata,
355 offset: Option<PolicyOffset>,
356}
357
358#[derive(Clone, Copy, Debug, KnownLayout, FromBytes, Immutable, PartialEq, Unaligned)]
365#[repr(C, packed)]
366pub(super) struct U24 {
367 low: u8,
368 middle: u8,
369 high: u8,
370}
371
372const_assert!(std::mem::size_of::<U24>() == 3);
374const_assert!(std::mem::align_of::<U24>() == 1);
375
376impl TryFrom<u32> for U24 {
377 type Error = ();
378
379 fn try_from(value: u32) -> Result<Self, Self::Error> {
380 if 0x1000000 <= value {
381 Err(())
382 } else {
383 Ok(Self {
384 low: (value & 0xff) as u8,
385 middle: ((value >> 8) & 0xff) as u8,
386 high: ((value >> 16) & 0xff) as u8,
387 })
388 }
389 }
390}
391
392impl From<U24> for u32 {
393 fn from(value: U24) -> u32 {
394 ((value.high as u32) << 16) + ((value.middle as u32) << 8) + (value.low as u32)
395 }
396}
397
398impl<'a, D: HasMetadata + Walk> Iterator for HashedArrayViewEntryIter<'a, D>
399where
400 D::Metadata: Eq,
401{
402 type Item = View<D>;
403
404 fn next(&mut self) -> Option<Self::Item> {
405 if let Some(offset) = self.offset
406 && offset < self.limit
407 {
408 let element = View::<D>::at(offset);
409 let metadata = element.read_metadata(&self.policy_data);
410 if metadata == self.metadata {
411 self.offset = Some(D::walk(&self.policy_data, offset));
412 Some(element)
413 } else {
414 self.offset = None;
415 None
416 }
417 } else {
418 None
419 }
420 }
421}
422
423#[derive(Debug, Clone)]
428pub(super) struct HashedArrayView<D: HasMetadata> {
429 phantom: PhantomData<D>,
430 index: HashTable<U24>,
431 limit: PolicyOffset,
435}
436
437impl<D: HasMetadata> HashedArrayView<D>
438where
439 D::Metadata: Hash,
440{
441 fn metadata_hash(metadata: &D::Metadata) -> u64 {
442 let mut hasher = RapidHasher::default();
443 metadata.hash(&mut hasher);
444 hasher.finish()
445 }
446}
447
448impl<D: Parse + HasMetadata + Walk> HashedArrayView<D>
449where
450 D::Metadata: Eq + PartialEq + Hash + Debug,
451{
452 pub fn find(&self, key: D::Metadata, policy_data: &PolicyData) -> Option<D> {
457 let key_hash = Self::metadata_hash(&key);
458 let offset = self.index.find(key_hash, |&offset| {
459 let element = View::<D>::at(u32::from(offset));
460 key == element.read_metadata(policy_data)
461 })?;
462 let element = View::<D>::at(u32::from(*offset));
463 Some(element.parse(policy_data))
464 }
465
466 pub(super) fn find_all(
469 &self,
470 key: D::Metadata,
471 policy_data: &PolicyData,
472 ) -> impl Iterator<Item = D> {
473 let key_hash = Self::metadata_hash(&key);
474 let offset = self.index.find(key_hash, |&offset| {
475 let element = View::<D>::at(u32::from(offset));
476 key == element.read_metadata(policy_data)
477 });
478 (HashedArrayViewEntryIter {
479 policy_data: policy_data,
480 limit: self.limit,
481 metadata: key,
482 offset: offset.map(|offset| u32::from(*offset)),
483 })
484 .map(|element| element.parse(policy_data))
485 }
486
487 pub(super) fn iter(&self, policy_data: &PolicyData) -> impl Iterator<Item = View<D>> {
489 self.index
490 .iter()
491 .map(|offset| {
492 let element = View::<D>::at(u32::from(*offset));
493 HashedArrayViewEntryIter {
494 policy_data: policy_data,
495 limit: self.limit,
496 metadata: element.read_metadata(policy_data),
497 offset: Some(u32::from(*offset)),
498 }
499 })
500 .flatten()
501 }
502}
503
504impl<D: Parse + HasMetadata + Walk> Parse for HashedArrayView<D>
505where
506 D::Metadata: Eq + Debug + PartialEq + Parse + Hash,
507{
508 type Error = anyhow::Error;
509
510 fn parse<'a>(cursor: PolicyCursor<'a>) -> Result<(Self, PolicyCursor<'a>), Self::Error> {
511 let (array_view, cursor) = SimpleArrayView::<D>::parse(cursor)?;
512
513 let mut index = HashTable::with_capacity(array_view.count as usize);
515
516 let limit = cursor.offset();
518
519 for view in array_view.data().iter(cursor.data()) {
522 let metadata = view.read_metadata(cursor.data());
523
524 index
525 .entry(
526 Self::metadata_hash(&metadata),
527 |&offset| {
528 let element = View::<D>::at(u32::from(offset));
529 metadata == element.read_metadata(cursor.data())
530 },
531 |&offset| {
532 let element = View::<D>::at(u32::from(offset));
533 Self::metadata_hash(&element.read_metadata(cursor.data()))
534 },
535 )
536 .or_insert(U24::try_from(view.start()).expect("Policy offsets ought fit in U24!"));
537 }
538
539 Ok((Self { phantom: PhantomData, index, limit }, cursor))
540 }
541}
542
543impl<D: Validate + Parse + HasMetadata + Walk> Validate for HashedArrayView<D>
544where
545 D::Metadata: Eq,
546{
547 type Error = anyhow::Error;
548
549 fn validate(&self, context: &PolicyValidationContext) -> Result<(), Self::Error> {
550 let policy_data = context.data.clone();
551 for element in self
552 .index
553 .iter()
554 .map(|offset| {
555 let element = View::<D>::at(u32::from(*offset));
556 HashedArrayViewEntryIter::<D> {
557 policy_data: &policy_data,
558 limit: self.limit,
559 metadata: element.read_metadata(&policy_data),
560 offset: Some(u32::from(*offset)),
561 }
562 })
563 .flatten()
564 {
565 element.validate(context)?;
566 }
567
568 Ok(())
569 }
570}
571
572#[cfg(test)]
573mod tests {
574 use super::U24;
575
576 #[test]
577 fn to_and_from_u24() {
578 for i in 0u32..0x10000 {
579 let u24_result = U24::try_from(i);
580 assert!(u24_result.is_ok());
581 let u24 = u24_result.unwrap();
582 assert_eq!(i >> 16, u24.high as u32);
583 assert_eq!((i >> 8) & 0xff, u24.middle as u32);
584 assert_eq!(i & 0xff, u24.low as u32);
585 let j = u32::from(u24);
586 assert_eq!(i, j);
587 }
588
589 assert!(U24::try_from(0x1000000).is_err());
590 }
591}