1*004e86ffSlogin #![allow(dead_code)] 2*004e86ffSlogin 3*004e86ffSlogin use core::mem::size_of; 4*004e86ffSlogin 5*004e86ffSlogin use alloc::vec::Vec; 6*004e86ffSlogin 7*004e86ffSlogin use crate::{ 8*004e86ffSlogin include::bindings::bindings::{E2BIG, EINVAL, EOVERFLOW}, 9*004e86ffSlogin io::SeekFrom, 10*004e86ffSlogin }; 11*004e86ffSlogin 12*004e86ffSlogin /// @brief 本模块用于为数组提供游标的功能,以简化其操作。 13*004e86ffSlogin #[derive(Debug)] 14*004e86ffSlogin pub struct VecCursor { 15*004e86ffSlogin /// 游标管理的数据 16*004e86ffSlogin data: Vec<u8>, 17*004e86ffSlogin /// 游标的位置 18*004e86ffSlogin pos: usize, 19*004e86ffSlogin } 20*004e86ffSlogin 21*004e86ffSlogin impl VecCursor { 22*004e86ffSlogin /// @brief 新建一个游标 23*004e86ffSlogin pub fn new(data: Vec<u8>) -> Self { 24*004e86ffSlogin return Self { data: data, pos: 0 }; 25*004e86ffSlogin } 26*004e86ffSlogin 27*004e86ffSlogin /// @brief 创建一个全0的cursor 28*004e86ffSlogin pub fn zerod(length: usize) -> Self { 29*004e86ffSlogin let mut result = VecCursor { 30*004e86ffSlogin data: Vec::new(), 31*004e86ffSlogin pos: 0, 32*004e86ffSlogin }; 33*004e86ffSlogin result.data.resize(length, 0); 34*004e86ffSlogin return result; 35*004e86ffSlogin } 36*004e86ffSlogin 37*004e86ffSlogin /// @brief 获取游标管理的数据的可变引用 38*004e86ffSlogin pub fn get_mut(&mut self) -> &mut Vec<u8> { 39*004e86ffSlogin return &mut self.data; 40*004e86ffSlogin } 41*004e86ffSlogin 42*004e86ffSlogin /// @brief 获取游标管理的数据的不可变引用 43*004e86ffSlogin pub fn get_ref(&self) -> &Vec<u8> { 44*004e86ffSlogin return &self.data; 45*004e86ffSlogin } 46*004e86ffSlogin 47*004e86ffSlogin /// @brief 读取一个u8的数据(小端对齐) 48*004e86ffSlogin pub fn read_u8(&mut self) -> Result<u8, i32> { 49*004e86ffSlogin if self.pos >= self.data.len() { 50*004e86ffSlogin return Err(-(E2BIG as i32)); 51*004e86ffSlogin } 52*004e86ffSlogin self.pos += 1; 53*004e86ffSlogin return Ok(self.data[self.pos - 1]); 54*004e86ffSlogin } 55*004e86ffSlogin 56*004e86ffSlogin /// @brief 读取一个u16的数据(小端对齐) 57*004e86ffSlogin pub fn read_u16(&mut self) -> Result<u16, i32> { 58*004e86ffSlogin if self.pos + 2 > self.data.len() { 59*004e86ffSlogin return Err(-(E2BIG as i32)); 60*004e86ffSlogin } 61*004e86ffSlogin let mut res = 0u16; 62*004e86ffSlogin res |= (self.data[self.pos] as u16) & 0xff; 63*004e86ffSlogin self.pos += 1; 64*004e86ffSlogin res |= ((self.data[self.pos] as u16) & 0xff) << 8; 65*004e86ffSlogin self.pos += 1; 66*004e86ffSlogin 67*004e86ffSlogin return Ok(res); 68*004e86ffSlogin } 69*004e86ffSlogin 70*004e86ffSlogin /// @brief 读取一个u32的数据(小端对齐) 71*004e86ffSlogin pub fn read_u32(&mut self) -> Result<u32, i32> { 72*004e86ffSlogin if self.pos + 4 > self.data.len() { 73*004e86ffSlogin return Err(-(E2BIG as i32)); 74*004e86ffSlogin } 75*004e86ffSlogin let mut res = 0u32; 76*004e86ffSlogin for i in 0..4 { 77*004e86ffSlogin res |= ((self.data[self.pos] as u32) & 0xff) << (8 * i); 78*004e86ffSlogin self.pos += 1; 79*004e86ffSlogin } 80*004e86ffSlogin 81*004e86ffSlogin return Ok(res); 82*004e86ffSlogin } 83*004e86ffSlogin 84*004e86ffSlogin /// @brief 读取一个u64的数据(小端对齐) 85*004e86ffSlogin pub fn read_u64(&mut self) -> Result<u64, i32> { 86*004e86ffSlogin if self.pos + 8 > self.data.len() { 87*004e86ffSlogin return Err(-(E2BIG as i32)); 88*004e86ffSlogin } 89*004e86ffSlogin let mut res = 0u64; 90*004e86ffSlogin for i in 0..8 { 91*004e86ffSlogin res |= ((self.data[self.pos] as u64) & 0xff) << (8 * i); 92*004e86ffSlogin self.pos += 1; 93*004e86ffSlogin } 94*004e86ffSlogin 95*004e86ffSlogin return Ok(res); 96*004e86ffSlogin } 97*004e86ffSlogin 98*004e86ffSlogin /// @brief 精确读取与buf同样大小的数据。 99*004e86ffSlogin /// 100*004e86ffSlogin /// @param buf 要读取到的目标缓冲区 101*004e86ffSlogin /// 102*004e86ffSlogin /// @return Ok(()) 成功读取 103*004e86ffSlogin /// @retunr Err(-E2BIG) 没有这么多数据,读取失败 104*004e86ffSlogin pub fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), i32> { 105*004e86ffSlogin if self.pos + buf.len() > self.data.len() { 106*004e86ffSlogin return Err(-(E2BIG as i32)); 107*004e86ffSlogin } 108*004e86ffSlogin buf.copy_from_slice(&self.data[self.pos..self.pos + buf.len()]); 109*004e86ffSlogin self.pos += buf.len(); 110*004e86ffSlogin return Ok(()); 111*004e86ffSlogin } 112*004e86ffSlogin 113*004e86ffSlogin /// @brief 小端对齐,读取数据到u16数组. 114*004e86ffSlogin /// 115*004e86ffSlogin /// @param buf 目标u16数组 116*004e86ffSlogin pub fn read_u16_into(&mut self, buf: &mut [u16]) -> Result<(), i32> { 117*004e86ffSlogin if self.pos + buf.len() * size_of::<u16>() > self.data.len() * size_of::<u16>() { 118*004e86ffSlogin return Err(-(E2BIG as i32)); 119*004e86ffSlogin } 120*004e86ffSlogin 121*004e86ffSlogin for i in 0..buf.len() { 122*004e86ffSlogin buf[i] = self.read_u16()?; 123*004e86ffSlogin } 124*004e86ffSlogin 125*004e86ffSlogin return Ok(()); 126*004e86ffSlogin } 127*004e86ffSlogin 128*004e86ffSlogin /// @brief 调整游标的位置 129*004e86ffSlogin /// 130*004e86ffSlogin /// @param 调整的相对值 131*004e86ffSlogin /// 132*004e86ffSlogin /// @return Ok(新的游标位置) 调整成功,返回新的游标位置 133*004e86ffSlogin /// @return Err(-EOVERFLOW) 调整失败,游标超出正确的范围。(失败时游标位置不变) 134*004e86ffSlogin pub fn seek(&mut self, origin: SeekFrom) -> Result<usize, i32> { 135*004e86ffSlogin let pos: i64; 136*004e86ffSlogin match origin { 137*004e86ffSlogin SeekFrom::SeekSet(offset) => { 138*004e86ffSlogin pos = offset; 139*004e86ffSlogin } 140*004e86ffSlogin SeekFrom::SeekCurrent(offset) => { 141*004e86ffSlogin pos = self.pos as i64 + offset; 142*004e86ffSlogin } 143*004e86ffSlogin SeekFrom::SeekEnd(offset) => { 144*004e86ffSlogin // 请注意,此处的offset应小于等于0,否则肯定是不合法的 145*004e86ffSlogin pos = self.data.len() as i64 + offset; 146*004e86ffSlogin } 147*004e86ffSlogin SeekFrom::Invalid => { 148*004e86ffSlogin return Err(-(EINVAL as i32)); 149*004e86ffSlogin } 150*004e86ffSlogin } 151*004e86ffSlogin 152*004e86ffSlogin if pos < 0 || pos > self.data.len() as i64 { 153*004e86ffSlogin return Err(-(EOVERFLOW as i32)); 154*004e86ffSlogin } 155*004e86ffSlogin self.pos = pos as usize; 156*004e86ffSlogin return Ok(self.pos); 157*004e86ffSlogin } 158*004e86ffSlogin 159*004e86ffSlogin /// @brief 写入一个u8的数据(小端对齐) 160*004e86ffSlogin pub fn write_u8(&mut self, value: u8) -> Result<u8, i32> { 161*004e86ffSlogin if self.pos >= self.data.len() { 162*004e86ffSlogin return Err(-(E2BIG as i32)); 163*004e86ffSlogin } 164*004e86ffSlogin 165*004e86ffSlogin self.data[self.pos] = value; 166*004e86ffSlogin self.pos += 1; 167*004e86ffSlogin 168*004e86ffSlogin return Ok(value); 169*004e86ffSlogin } 170*004e86ffSlogin 171*004e86ffSlogin /// @brief 写入一个u16的数据(小端对齐) 172*004e86ffSlogin pub fn write_u16(&mut self, value: u16) -> Result<u16, i32> { 173*004e86ffSlogin if self.pos + 2 > self.data.len() { 174*004e86ffSlogin return Err(-(E2BIG as i32)); 175*004e86ffSlogin } 176*004e86ffSlogin 177*004e86ffSlogin self.data[self.pos] = (value & 0xff) as u8; 178*004e86ffSlogin self.pos += 1; 179*004e86ffSlogin self.data[self.pos] = ((value >> 8) & 0xff) as u8; 180*004e86ffSlogin self.pos += 1; 181*004e86ffSlogin 182*004e86ffSlogin return Ok(value); 183*004e86ffSlogin } 184*004e86ffSlogin 185*004e86ffSlogin /// @brief 写入一个u32的数据(小端对齐) 186*004e86ffSlogin pub fn write_u32(&mut self, value: u32) -> Result<u32, i32> { 187*004e86ffSlogin if self.pos + 4 > self.data.len() { 188*004e86ffSlogin return Err(-(E2BIG as i32)); 189*004e86ffSlogin } 190*004e86ffSlogin 191*004e86ffSlogin for i in 0..4 { 192*004e86ffSlogin self.data[self.pos] = ((value >> (i * 8)) & 0xff) as u8; 193*004e86ffSlogin self.pos += 1; 194*004e86ffSlogin } 195*004e86ffSlogin 196*004e86ffSlogin return Ok(value); 197*004e86ffSlogin } 198*004e86ffSlogin 199*004e86ffSlogin /// @brief 写入一个u64的数据(小端对齐) 200*004e86ffSlogin pub fn write_u64(&mut self, value: u64) -> Result<u64, i32> { 201*004e86ffSlogin if self.pos + 8 > self.data.len() { 202*004e86ffSlogin return Err(-(E2BIG as i32)); 203*004e86ffSlogin } 204*004e86ffSlogin 205*004e86ffSlogin for i in 0..8 { 206*004e86ffSlogin self.data[self.pos] = ((value >> (i * 8)) & 0xff) as u8; 207*004e86ffSlogin self.pos += 1; 208*004e86ffSlogin } 209*004e86ffSlogin 210*004e86ffSlogin return Ok(value); 211*004e86ffSlogin } 212*004e86ffSlogin 213*004e86ffSlogin /// @brief 精确写入与buf同样大小的数据。 214*004e86ffSlogin /// 215*004e86ffSlogin /// @param buf 要写入到的目标缓冲区 216*004e86ffSlogin /// 217*004e86ffSlogin /// @return Ok(()) 成功写入 218*004e86ffSlogin /// @retunr Err(-E2BIG) 没有这么多数据,写入失败 219*004e86ffSlogin pub fn write_exact(&mut self, buf: &[u8]) -> Result<(), i32> { 220*004e86ffSlogin if self.pos + buf.len() > self.data.len() { 221*004e86ffSlogin return Err(-(E2BIG as i32)); 222*004e86ffSlogin } 223*004e86ffSlogin 224*004e86ffSlogin self.data[self.pos..self.pos + buf.len()].copy_from_slice(&buf[..]); 225*004e86ffSlogin self.pos += buf.len(); 226*004e86ffSlogin 227*004e86ffSlogin return Ok(()); 228*004e86ffSlogin } 229*004e86ffSlogin 230*004e86ffSlogin /// @brief 获取当前的数据切片 231*004e86ffSlogin pub fn as_slice(&self) -> &[u8] { 232*004e86ffSlogin return &self.data[..]; 233*004e86ffSlogin } 234*004e86ffSlogin 235*004e86ffSlogin /// @brief 获取可变数据切片 236*004e86ffSlogin pub fn as_mut_slice(&mut self) -> &mut [u8] { 237*004e86ffSlogin return &mut self.data[..]; 238*004e86ffSlogin } 239*004e86ffSlogin 240*004e86ffSlogin /// @brief 获取当前游标的位置 241*004e86ffSlogin #[inline] 242*004e86ffSlogin pub fn pos(&self) -> usize { 243*004e86ffSlogin return self.pos; 244*004e86ffSlogin } 245*004e86ffSlogin 246*004e86ffSlogin /// @brief 获取缓冲区数据的大小 247*004e86ffSlogin #[inline] 248*004e86ffSlogin pub fn len(&self) -> usize { 249*004e86ffSlogin return self.data.len(); 250*004e86ffSlogin } 251*004e86ffSlogin } 252