Skip to main content

new_vdf_parser/
reader.rs

1trait HasByteConvert {
2  fn from_le_bytes(bytes: &[u8], offset: usize) -> Self;
3  fn from_be_bytes(bytes: &[u8], offset: usize) -> Self;
4}
5
6impl HasByteConvert for u8 {
7  fn from_le_bytes(bytes: &[u8], offset: usize) -> u8 {
8    return bytes[offset];
9  }
10  fn from_be_bytes(bytes: &[u8], offset: usize) -> u8 {
11    return bytes[offset];
12  }
13}
14
15impl HasByteConvert for u16 {
16  fn from_le_bytes(bytes: &[u8], offset: usize) -> u16 {
17    return u16::from_le_bytes(bytes[offset..offset+2].try_into().expect("incorrect length"));
18  }
19  fn from_be_bytes(bytes: &[u8], offset: usize) -> u16 {
20    return u16::from_be_bytes(bytes[offset..offset+2].try_into().expect("incorrect length"));
21  }
22}
23
24impl HasByteConvert for u32 {
25  fn from_le_bytes(bytes: &[u8], offset: usize) -> u32 {
26    return u32::from_le_bytes(bytes[offset..offset+4].try_into().expect("incorrect length"));
27  }
28  fn from_be_bytes(bytes: &[u8], offset: usize) -> u32 {
29    return u32::from_be_bytes(bytes[offset..offset+4].try_into().expect("incorrect length"));
30  }
31}
32
33impl HasByteConvert for u64 {
34  fn from_le_bytes(bytes: &[u8], offset: usize) -> u64 {
35    return u64::from_le_bytes(bytes[offset..offset+8].try_into().expect("incorrect length"));
36  }
37  fn from_be_bytes(bytes: &[u8], offset: usize) -> u64 {
38    return u64::from_be_bytes(bytes[offset..offset+8].try_into().expect("incorrect length"));
39  }
40}
41
42impl HasByteConvert for i8 {
43  fn from_le_bytes(bytes: &[u8], offset: usize) -> i8 {
44    return i8::from_le_bytes(bytes[offset..offset+1].try_into().expect("incorrect length"));
45  }
46  fn from_be_bytes(bytes: &[u8], offset: usize) -> i8 {
47    return i8::from_be_bytes(bytes[offset..offset+1].try_into().expect("incorrect length"));
48  }
49}
50
51impl HasByteConvert for i16 {
52  fn from_le_bytes(bytes: &[u8], offset: usize) -> i16 {
53    return i16::from_le_bytes(bytes[offset..offset+2].try_into().expect("incorrect length"));
54  }
55  fn from_be_bytes(bytes: &[u8], offset: usize) -> i16 {
56    return i16::from_be_bytes(bytes[offset..offset+2].try_into().expect("incorrect length"));
57  }
58}
59
60impl HasByteConvert for i32 {
61  fn from_le_bytes(bytes: &[u8], offset: usize) -> i32 {
62    return i32::from_le_bytes(bytes[offset..offset+4].try_into().expect("incorrect length"));
63  }
64  fn from_be_bytes(bytes: &[u8], offset: usize) -> i32 {
65    return i32::from_be_bytes(bytes[offset..offset+4].try_into().expect("incorrect length"));
66  }
67}
68
69impl HasByteConvert for i64 {
70  fn from_le_bytes(bytes: &[u8], offset: usize) -> i64 {
71    return i64::from_le_bytes(bytes[offset..offset+8].try_into().expect("incorrect length"));
72  }
73  fn from_be_bytes(bytes: &[u8], offset: usize) -> i64 {
74    return i64::from_be_bytes(bytes[offset..offset+8].try_into().expect("incorrect length"));
75  }
76}
77
78impl HasByteConvert for f32 {
79  fn from_le_bytes(bytes: &[u8], offset: usize) -> f32 {
80    return f32::from_le_bytes(bytes[offset..offset+4].try_into().expect("incorrect length"));
81  }
82  fn from_be_bytes(bytes: &[u8], offset: usize) -> f32 {
83    return f32::from_be_bytes(bytes[offset..offset+4].try_into().expect("incorrect length"));
84  }
85}
86
87impl HasByteConvert for f64 {
88  fn from_le_bytes(bytes: &[u8], offset: usize) -> f64 {
89    return f64::from_le_bytes(bytes[offset..offset+8].try_into().expect("incorrect length"));
90  }
91  fn from_be_bytes(bytes: &[u8], offset: usize) -> f64 {
92    return f64::from_be_bytes(bytes[offset..offset+8].try_into().expect("incorrect length"));
93  }
94}
95
96pub struct Reader<'a> {
97  data: &'a [u8],
98  offset: usize,
99  length: u64,
100}
101
102#[allow(dead_code)]
103impl Reader<'_> {
104  /// Gets the underlying data of the reader.
105  pub fn get_data(&self) -> &[u8] { return self.data; }
106  /// Gets the offset of the reader.
107  pub fn get_offset(&self) -> usize { return self.offset; }
108  /// Gets the length of the reader.
109  pub fn get_length(&self) -> u64 { return self.length; }
110
111  /// Creates a new Reader from the provided buffer.
112  pub fn new(buf: &[u8]) -> Reader<'_> { return Reader { data: buf, offset: 0, length: buf.len() as u64 }; }
113
114  /// Slices the Reader's buffer and returns a new Reader for the slice.
115  pub fn slice(&self, offset: usize, length: usize) -> Reader<'_> {
116    let sliced = &self.data[offset..(offset+length)];
117    return Reader { data: sliced, offset: 0, length: length as u64 };
118  }
119
120  /// Seek to a new offset, from 0 (start), 1 (current), or 2 (end) of the buffer.
121  pub fn seek(&mut self, offset: usize, position: u8) {
122    if position == 0 {
123      self.offset = offset;
124    } else if position == 1 {
125      self.offset += offset;
126    } else {
127      self.offset = (self.length as usize) - offset;
128    }
129  }
130  
131  /// Gets the remaining length of the buffer.
132  pub fn remaining(&mut self) -> u64 { return self.length - (self.offset as u64); }
133
134  /// Data reading interface.
135  fn read_i<T: HasByteConvert>(&mut self, endianness: bool) -> T {
136    if endianness {
137      return T::from_le_bytes(self.data, self.offset);
138    } else {
139      return T::from_be_bytes(self.data, self.offset);
140    }
141  }
142  
143  /// Reads the next char from the buffer.
144  pub fn read_char(&mut self, endianness: bool) -> char {
145    return self.read_uint8(endianness) as char;
146  }
147  
148  /// Reads the next 8 bit unsigned int from the buffer.
149  pub fn read_uint8(&mut self, endianness: bool) -> u8 {
150    let res = self.read_i::<u8>(endianness);
151    self.offset += 1;
152    return res;
153  }
154  /// Reads the next 16 bit unsigned int from the buffer.
155  pub fn read_uint16(&mut self, endianness: bool) -> u16 {
156    let res = self.read_i::<u16>(endianness);
157    self.offset += 2;
158    return res;
159  }
160  /// Reads the next 32 bit unsigned int from the buffer.
161  pub fn read_uint32(&mut self, endianness: bool) -> u32 {
162    let res = self.read_i::<u32>(endianness);
163    self.offset += 4;
164    return res;
165  }
166  /// Reads the next 64 bit unsigned int from the buffer.
167  pub fn read_uint64(&mut self, endianness: bool) -> u64 {
168    let res = self.read_i::<u64>(endianness);
169    self.offset += 8;
170    return res;
171  }
172  
173  /// Reads the next 8 bit signed int from the buffer.
174  pub fn read_int8(&mut self, endianness: bool) -> i8 {
175    let res = self.read_i::<i8>(endianness);
176    self.offset += 1;
177    return res;
178  }
179  /// Reads the next 16 bit signed int from the buffer.
180  pub fn read_int16(&mut self, endianness: bool) -> i16 {
181    let res = self.read_i::<i16>(endianness);
182    self.offset += 2;
183    return res;
184  }
185  /// Reads the next 32 bit signed int from the buffer.
186  pub fn read_int32(&mut self, endianness: bool) -> i32 {
187    let res = self.read_i::<i32>(endianness);
188    self.offset += 4;
189    return res;
190  }
191  /// Reads the next 64 bit signed int from the buffer.
192  pub fn read_int64(&mut self, endianness: bool) -> i64 {
193    let res = self.read_i::<i64>(endianness);
194    self.offset += 8;
195    return res;
196  }
197  
198  /// Reads the next 32 bit float from the buffer.
199  pub fn read_float32(&mut self, endianness: bool) -> f32 {
200    let res = self.read_i::<f32>(endianness);
201    self.offset += 4;
202    return res;
203  }
204  /// Reads the next 64 bit float from the buffer.
205  pub fn read_float64(&mut self, endianness: bool) -> f64 {
206    let res = self.read_i::<f64>(endianness);
207    self.offset += 8;
208    return res;
209  }
210
211  /// Reads the next string from the buffer, using the provided length or reading till next 00 byte.
212  pub fn read_string(&mut self, length: Option<u32>) -> String {
213    let mut len: usize = 0;
214
215    if length.is_some() {
216      len = length.unwrap() as usize;
217    } else {
218      loop {
219        if self.data[self.offset + len] == 0 {
220          break;
221        } else {
222          len += 1;
223        }
224      }
225    }
226
227    let u8_vec = self.data[self.offset..self.offset+len].to_vec();
228    
229    let utf8_res = String::from_utf8(u8_vec.clone());
230    self.offset += len + 1;
231
232    if utf8_res.is_ok() {
233      return utf8_res.unwrap();
234    } else {
235      let u16_vec: Vec<u16> = u8_vec.iter().map(| char_code | {
236        return char_code.to_owned() as u16;
237      }).collect();
238      let char_codes = &u16_vec[..];
239  
240      return String::from_utf16(char_codes).unwrap();
241    }
242  }
243}