1use super::super::alloc::SliceWrapper;
2use super::super::alloc::SliceWrapperMut;
3use super::interface::Freezable;
4use core::cmp::min;
5#[derive(Copy, Clone, Default, Debug)]
6pub struct InputReference<'a> {
7 pub data: &'a [u8],
8 pub orig_offset: usize, }
10impl<'a> SliceWrapper<u8> for InputReference<'a> {
11 fn slice(&self) -> &[u8] {
12 self.data
13 }
14}
15
16impl<'a> Freezable for InputReference<'a> {
17 fn freeze(&self) -> super::interface::SliceOffset {
18 debug_assert!(self.data.len() <= 0xffff_ffff);
19 super::interface::SliceOffset(self.orig_offset, self.data.len() as u32)
20 }
21}
22
23#[derive(Default)]
24pub struct InputReferenceMut<'a> {
25 pub data: &'a mut [u8],
26 pub orig_offset: usize, }
28
29impl<'a> SliceWrapper<u8> for InputReferenceMut<'a> {
30 fn slice(&self) -> &[u8] {
31 self.data
32 }
33}
34impl<'a> SliceWrapperMut<u8> for InputReferenceMut<'a> {
35 fn slice_mut(&mut self) -> &mut [u8] {
36 self.data
37 }
38}
39
40impl<'a> From<InputReferenceMut<'a>> for InputReference<'a> {
41 fn from(val: InputReferenceMut<'a>) -> InputReference<'a> {
42 InputReference {
43 data: val.data,
44 orig_offset: val.orig_offset,
45 }
46 }
47}
48
49impl<'a> From<&'a InputReferenceMut<'a>> for InputReference<'a> {
50 fn from(val: &'a InputReferenceMut<'a>) -> InputReference<'a> {
51 InputReference {
52 data: val.data,
53 orig_offset: val.orig_offset,
54 }
55 }
56}
57
58#[derive(Clone, Debug, Copy)]
59pub struct InputPair<'a>(pub InputReference<'a>, pub InputReference<'a>);
60
61impl<'a> PartialEq for InputPair<'a> {
62 fn eq(&self, other: &InputPair<'_>) -> bool {
63 if self.0.len() + self.1.len() != other.0.len() + other.1.len() {
64 return false;
65 }
66 for (a_iter, b_iter) in self
67 .0
68 .data
69 .iter()
70 .chain(self.1.data.iter())
71 .zip(other.0.data.iter().chain(other.1.data.iter()))
72 {
73 if *a_iter != *b_iter {
74 return false;
75 }
76 }
77 true
78 }
79}
80impl<'a> core::ops::Index<usize> for InputPair<'a> {
81 type Output = u8;
82 fn index(&self, index: usize) -> &u8 {
83 if index >= self.0.len() {
84 &self.1.data[index - self.0.len()]
85 } else {
86 &self.0.data[index]
87 }
88 }
89}
90impl<'a> core::fmt::LowerHex for InputPair<'a> {
91 fn fmt(&self, fmtr: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
92 for item in self.0.data {
93 fmtr.write_fmt(format_args!("{:02x}", item))?
94 }
95 for item in self.1.data {
96 fmtr.write_fmt(format_args!("{:02x}", item))?
97 }
98 Ok(())
99 }
100}
101
102impl<'a> InputPair<'a> {
103 pub fn split_at(&self, loc: usize) -> (InputPair<'a>, InputPair<'a>) {
104 if loc >= self.0.len() {
105 let offset_from_self_1 = loc - self.0.len();
106 let (first, second) = self.1.data.split_at(min(offset_from_self_1, self.1.len()));
107 return (
108 InputPair::<'a>(
109 self.0,
110 InputReference::<'a> {
111 data: first,
112 orig_offset: self.1.orig_offset,
113 },
114 ),
115 InputPair::<'a>(
116 InputReference::<'a>::default(),
117 InputReference::<'a> {
118 data: second,
119 orig_offset: offset_from_self_1 + self.1.orig_offset,
120 },
121 ),
122 );
123 }
124 let (first, second) = self.0.data.split_at(min(loc, self.0.len()));
125 (
126 InputPair::<'a>(
127 InputReference::<'a> {
128 data: first,
129 orig_offset: self.0.orig_offset,
130 },
131 InputReference::<'a>::default(),
132 ),
133 InputPair::<'a>(
134 InputReference::<'a> {
135 data: second,
136 orig_offset: self.0.orig_offset + loc,
137 },
138 self.1,
139 ),
140 )
141 }
142 pub fn len(&self) -> usize {
143 self.0.len() + self.1.len()
144 }
145}