1use crate::Endian;
2use std::io::{Error, ErrorKind, SeekFrom};
3
4pub trait BinaryReader {
6 fn set_endian(&mut self, endian: Endian);
7 fn endian(&self) -> Endian;
8
9 fn read_byte(&mut self) -> Result<u8, Error>;
10 fn read_u8(&mut self) -> Result<u8, Error>;
11
12 #[deprecated(since = "0.0.10", note = "Use new function `read_exact()` instead")]
13 fn read_bytes(&mut self, array: &mut [u8]) -> Result<(), Error> {
14 self.read_exact(array)
15 }
16
17 fn read_exact(&mut self, array: &mut [u8]) -> Result<(), Error>;
18
19 fn read_bytes_as_vec(&mut self, len: usize) -> Result<Vec<u8>, Error>;
21
22 fn read_bytes_no_move(&mut self, len: usize) -> Result<Vec<u8>, Error>;
28
29 fn read_u16(&mut self) -> Result<u16, Error>;
30 fn read_u32(&mut self) -> Result<u32, Error>;
31 fn read_u64(&mut self) -> Result<u64, Error>;
32 fn read_u128(&mut self) -> Result<u128, Error>;
33 fn read_i8(&mut self) -> Result<i8, Error>;
34 fn read_i16(&mut self) -> Result<i16, Error>;
35 fn read_i32(&mut self) -> Result<i32, Error>;
36 fn read_i64(&mut self) -> Result<i64, Error>;
37 fn read_i128(&mut self) -> Result<i128, Error>;
38
39 fn read_f32(&mut self) -> Result<f32, Error>;
40 fn read_f64(&mut self) -> Result<f64, Error>;
41
42 fn read_u16_be(&mut self) -> Result<u16, Error>;
43 fn read_u32_be(&mut self) -> Result<u32, Error>;
44 fn read_u64_be(&mut self) -> Result<u64, Error>;
45 fn read_u128_be(&mut self) -> Result<u128, Error>;
46 fn read_i16_be(&mut self) -> Result<i16, Error>;
47 fn read_i32_be(&mut self) -> Result<i32, Error>;
48 fn read_i64_be(&mut self) -> Result<i64, Error>;
49 fn read_i128_be(&mut self) -> Result<i128, Error>;
50
51 fn read_f32_be(&mut self) -> Result<f32, Error>;
52 fn read_f64_be(&mut self) -> Result<f64, Error>;
53
54 fn read_u16_le(&mut self) -> Result<u16, Error>;
55 fn read_u32_le(&mut self) -> Result<u32, Error>;
56 fn read_u64_le(&mut self) -> Result<u64, Error>;
57 fn read_u128_le(&mut self) -> Result<u128, Error>;
58 fn read_i16_le(&mut self) -> Result<i16, Error>;
59 fn read_i32_le(&mut self) -> Result<i32, Error>;
60 fn read_i64_le(&mut self) -> Result<i64, Error>;
61 fn read_i128_le(&mut self) -> Result<i128, Error>;
62
63 fn read_f32_le(&mut self) -> Result<f32, Error>;
64 fn read_f64_le(&mut self) -> Result<f64, Error>;
65
66 fn read_ascii_string(&mut self, size: usize) -> Result<String, Error> {
81 let mut array: Vec<u8> = vec![0; size];
82 self.read_exact(&mut array)?;
83
84 let buf = &array;
85 let mut s = Vec::new();
86 for b in buf {
87 if *b == 0 {
88 break;
89 }
90 s.push(*b as u16);
91 }
92 let res = String::from_utf16(&s);
93 match res {
94 Ok(strings) => Ok(strings),
95 _ => {
96 let err = "This string can not read";
97 Err(Error::new(ErrorKind::Other, err))
98 }
99 }
100 }
101
102 fn read_utf16_string(&mut self, size: usize) -> Result<String, Error> {
106 let size = size / 2;
107 let mut array: Vec<u16> = vec![0; size];
108 for i in 0..size {
109 array[i] = self.read_u16()?;
110 }
111 let res = String::from_utf16(&array);
112 match res {
113 Ok(strings) => Ok(strings),
114 _ => {
115 let err = "This string can not read";
116 Err(Error::new(ErrorKind::Other, err))
117 }
118 }
119 }
120
121 fn read_utf16be_string(&mut self, size: usize) -> Result<String, Error> {
122 let endian = self.endian();
123 self.set_endian(Endian::BigEndian);
124 let result = self.read_utf16_string(size);
125 self.set_endian(endian);
126 result
127 }
128
129 fn read_utf16le_string(&mut self, size: usize) -> Result<String, Error> {
130 let endian = self.endian();
131 self.set_endian(Endian::LittleEndian);
132 let result = self.read_utf16_string(size);
133 self.set_endian(endian);
134 result
135 }
136
137 fn read_utf8_string(&mut self, size: usize) -> Result<String, Error> {
138 let mut array: Vec<u8> = vec![0; size];
139 for i in 0..size {
140 array[i] = self.read_u8()?;
141 }
142 let res = String::from_utf8(array);
143 match res {
144 Ok(strings) => Ok(strings),
145 _ => {
146 let err = "This string can not read";
147 Err(Error::new(ErrorKind::Other, err))
148 }
149 }
150 }
151
152 #[cfg(feature = "codec")]
153 fn read_local_string(&mut self, size: usize, code: CodeType) -> Result<String, Error>;
154
155 fn skip_ptr(&mut self, size: usize) -> Result<usize, Error>;
157
158 fn offset(&mut self) -> Result<u64, Error>;
159 fn seek(&mut self, seek: SeekFrom) -> Result<u64, Error>;
160}