1use std::convert::AsRef;
2use std::fmt;
3use std::ops;
4use std::ops::Deref;
5use std::ops::DerefMut;
6
7use crate::*;
8
9pub type ByteVector = Vec<u8>;
10
11#[derive(Debug, Clone)]
13pub struct ByteArray<const N: usize>([u8; N]);
14
15impl<const N: usize> ByteArray<N> {
16 pub fn new() -> ByteArray<N> {
17 ByteArray::<N>([0; N])
18 }
19}
20
21impl<const N: usize> Default for ByteArray<N> {
22 fn default() -> Self {
23 ByteArray::<N>([0x00; N])
24 }
25}
26
27impl<const N: usize> AsRef<[u8]> for ByteArray<N> {
28 fn as_ref(&self) -> &[u8] {
29 &self.0
30 }
31}
32
33impl<const N: usize> Deref for ByteArray<N> {
34 type Target = [u8];
35 fn deref(&self) -> &[u8] {
36 &self.0
37 }
38}
39
40impl<const N: usize> DerefMut for ByteArray<N> {
41 fn deref_mut(&mut self) -> &mut [u8] {
42 &mut self.0
43 }
44}
45
46impl<const N: usize> PartialEq for ByteArray<N> {
47 fn eq(&self, other: &Self) -> bool {
48 return self.0 == other.0;
49 }
50}
51
52impl<const N: usize> From<&[u8; N]> for ByteArray<N> {
53 fn from(value: &[u8; N]) -> Self {
54 ByteArray::<N>(*value)
55 }
56}
57
58impl<const N: usize> From<[u8; N]> for ByteArray<N> {
59 fn from(value: [u8; N]) -> Self {
60 ByteArray::<N>(value)
61 }
62}
63
64impl<const N: usize> fmt::Display for ByteArray<N> {
65 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66 write!(f, "{:?}", self.0)
67 }
68}
69
70impl<const N: usize> ops::BitXor for ByteArray<N> {
71 type Output = Self;
72
73 fn bitxor(self, rhs: Self) -> Self::Output {
74 let mut res = self;
75 for i in 0..N {
76 res.0[i] = res.0[i] ^ rhs.0[i];
77 }
78 res
79 }
80}
81
82impl<const N: usize> ops::Index<usize> for ByteArray<N> {
83 type Output = u8;
84
85 fn index(&self, index: usize) -> &Self::Output {
86 &self.0.as_slice()[index]
87 }
88}
89
90impl<const N: usize> ops::IndexMut<usize> for ByteArray<N> {
91 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
92 &mut self.0.as_mut_slice()[index]
93 }
94}
95
96pub struct MemoryTaker<'a> {
98 mem: &'a [u8],
99 idx: usize,
100 length: usize,
101}
102
103impl<'a> MemoryTaker<'a> {
104 pub fn new(src: &'a [u8]) -> MemoryTaker {
105 MemoryTaker {
106 mem: src,
107 idx: 0,
108 length: src.len(),
109 }
110 }
111
112 pub fn take(&mut self, dst: &mut [u8]) -> Result<&mut Self> {
113 let need_length = dst.len();
114 if self.idx + need_length > self.length {
115 return Err(MEMORY_TAKER_HAS_NO_ENOUGH_CONTENT.clone());
116 }
117 memcpy(dst, &self.mem[self.idx..self.idx + need_length])?;
118 self.idx += need_length;
119 Ok(self)
120 }
121
122 pub fn take_all(&mut self) -> Result<ByteVector> {
123 if self.idx >= self.length {
124 return Err(MEMORY_TAKER_HAS_NO_ENOUGH_CONTENT.clone());
125 }
126 let res = self.mem[self.idx..].to_vec();
127 self.idx = self.length;
128 Ok(res)
129 }
130}
131
132pub fn memcpy(dst: &mut [u8], src: &[u8]) -> Result<()> {
134 if dst.len() != src.len() {
135 return Err(SRC_AND_DST_MEMORY_HAS_DIFFERENT_LENGTH.clone());
136 }
137 dst.copy_from_slice(src);
138 Ok(())
139}
140
141pub fn xor(src1: &[u8], src2: &[u8]) -> Result<ByteVector> {
143 if src1.len() != src2.len() {
144 return Err(TWO_MEMORY_HAS_DIFFERENT_LENGTH_WHEN_XOR_OPERATION.clone());
145 }
146 let target_len = src1.len();
147 let mut res = vec![0x00; target_len];
148 for i in 0..target_len {
149 res[i] = src1[i] ^ src2[i];
150 }
151 Ok(res)
152}
153
154#[cfg(test)]
155mod tests {
156 use super::*;
157
158 #[test]
159 fn test_xor() {
160 let a = ByteArray::<4>::from(&[0x12, 0x43, 0x56, 0x87]);
161 let b = ByteArray::<4>::from(&[0x75, 0x4A, 0xB1, 0xC9]);
162 let res = a ^ b;
163 assert_eq!(res.as_ref(), &[0x67, 0x09, 0xE7, 0x4E]);
164 println!("Res: {}", res);
165 }
166
167 #[test]
168 fn test_memory_taker() {
169 let v = b"123456789".to_vec();
170 let mut mt = MemoryTaker::new(&v);
171 let mut v1: Vec<u8> = vec![0u8; 2];
172 let mut v2 = vec![0u8; 3];
173 let v3 = mt
174 .take(&mut v1)
175 .unwrap()
176 .take(&mut v2)
177 .unwrap()
178 .take_all()
179 .unwrap();
180 assert_eq!(v1, b"12".to_vec());
181 assert_eq!(v2, b"345".to_vec());
182 assert_eq!(v3, b"6789".to_vec());
183 let mut v1: Vec<u8> = vec![0u8; 2];
184 assert!(mt.take(&mut v1).is_err());
185 }
186}