1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use core;
use super::super::alloc::SliceWrapper;
use super::super::alloc::SliceWrapperMut;
use super::interface::Freezable;
#[derive(Copy,Clone,Default,Debug)]
pub struct InputReference<'a> {
    pub data: &'a [u8],
    pub orig_offset: usize, // offset into the original slice of data
}
impl<'a> SliceWrapper<u8> for InputReference<'a> {
    fn slice(&self) -> & [u8] {
        self.data
    }
}

impl<'a> Freezable for InputReference<'a> {
    fn freeze(&self) -> super::interface::SliceOffset {
        debug_assert!(self.data.len() <= 0xffffffff);
        super::interface::SliceOffset(self.orig_offset, self.data.len() as u32)
    }
}

pub struct InputReferenceMut<'a> {
    pub data: &'a mut [u8],
    pub orig_offset: usize, // offset into the original slice of data
}

impl<'a> SliceWrapper<u8> for InputReferenceMut<'a> {
    fn slice(&self) -> & [u8] {
        self.data
    }
}
impl<'a> SliceWrapperMut<u8> for InputReferenceMut<'a> {
    fn slice_mut(&mut self) -> &mut [u8] {
        self.data
    }
}
impl <'a> Default for InputReferenceMut<'a> {
    fn default() -> Self {
        InputReferenceMut {
            data:&mut[],
            orig_offset:0,
        }
    }
}
impl <'a> From<InputReferenceMut<'a>> for InputReference<'a> {
   fn from(val: InputReferenceMut<'a>) -> InputReference<'a> {
       InputReference{
           data: val.data,
           orig_offset: val.orig_offset,
       }
   }
}

impl <'a> From<&'a InputReferenceMut<'a>> for InputReference<'a> {
   fn from(val: &'a InputReferenceMut<'a>) -> InputReference<'a> {
       InputReference{
           data: val.data,
           orig_offset: val.orig_offset,
       }
   }
}

#[derive(Clone, Debug,Copy)]
pub struct InputPair<'a>(pub InputReference<'a>, pub InputReference<'a>);

impl<'a> core::cmp::PartialEq for InputPair<'a> {
    fn eq<'b>(&self, other: &InputPair<'b>) -> bool {
        if self.0.len() + self.1.len() != other.0.len() + other.1.len() {
            return false;
        }
        for (a_iter, b_iter) in self.0.data.iter().chain(self.1.data.iter()).zip(other.0.data.iter().chain(other.1.data.iter())) {
            if *a_iter != *b_iter {
                return false;
            }
        }
        return true;
    }
}
impl<'a> core::ops::Index<usize> for InputPair<'a> {
  type Output = u8;
  fn index(&self, index:usize) -> &u8 {
    if index >= self.0.len() {
      &self.1.data[index - self.0.len()]
    } else {
      &self.0.data[index]
    }
  }
}
impl<'a> core::fmt::LowerHex for InputPair<'a> {
    fn fmt(&self, fmtr: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
        for item in self.0.data {
            if let Err(e) = fmtr.write_fmt(format_args!("{:02x}", item)) {
                return Err(e)
            }
        }
        for item in self.1.data {
            if let Err(e) = fmtr.write_fmt(format_args!("{:02x}", item)) {
                return Err(e);
            }
        }
        Ok(())
    }
}

impl<'a> InputPair<'a> {
    pub fn split_at(&self, loc : usize) -> (InputPair<'a>, InputPair<'a>) {
        if loc >= self.0.len() {
            let offset_from_self_1 = loc - self.0.len();
            let (first, second) = self.1.data.split_at(core::cmp::min(offset_from_self_1,
                                                                 self.1.len()));
            return (InputPair::<'a>(self.0,
                                    InputReference::<'a>{data:first,
                                                   orig_offset:self.1.orig_offset}),
                    InputPair::<'a>(InputReference::<'a>::default(),
                                    InputReference::<'a>{data:second,
                                                   orig_offset:offset_from_self_1 + self.1.orig_offset,
                                    }),
                    );
        }
        let (first, second) = self.0.data.split_at(core::cmp::min(loc,
                                                                  self.0.len()));
        (InputPair::<'a>(InputReference::<'a>{data:first,
                                        orig_offset:self.0.orig_offset},
                         InputReference::<'a>::default()),
         InputPair::<'a>(InputReference::<'a>{data:second,
                                        orig_offset:self.0.orig_offset + loc},
                         self.1))
    }
    pub fn len(&self) -> usize {
        self.0.len() + self.1.len()
    }
}