jsxbin_parser/
binary_reader.rs1use crate::error::{ParserError, ParserResult};
2use byteorder::{LittleEndian, ReadBytesExt};
3use std::io::Cursor;
4
5pub struct BinaryReader {
7 cursor: Cursor<Vec<u8>>,
8}
9
10impl BinaryReader {
11 pub fn new(data: Vec<u8>) -> Self {
13 BinaryReader {
14 cursor: Cursor::new(data),
15 }
16 }
17
18 pub fn position(&self) -> usize {
20 self.cursor.position() as usize
21 }
22
23 pub fn seek(&mut self, pos: usize) -> ParserResult<()> {
25 use std::io::Seek;
26 self.cursor
27 .seek(std::io::SeekFrom::Start(pos as u64))
28 .map(|_| ())
29 .map_err(|e| ParserError::IoError(e.to_string()))
30 }
31
32 pub fn read_u8(&mut self) -> ParserResult<u8> {
34 self.cursor
35 .read_u8()
36 .map_err(|_| ParserError::UnexpectedEof(self.position()))
37 }
38
39 pub fn peek_u8(&self) -> ParserResult<u8> {
41 let pos = self.cursor.position() as usize;
42 self.cursor
43 .get_ref()
44 .get(pos)
45 .copied()
46 .ok_or(ParserError::UnexpectedEof(pos))
47 }
48
49 pub fn read_u16_le(&mut self) -> ParserResult<u16> {
51 self.cursor
52 .read_u16::<LittleEndian>()
53 .map_err(|_| ParserError::UnexpectedEof(self.position()))
54 }
55
56 pub fn read_u32_le(&mut self) -> ParserResult<u32> {
58 self.cursor
59 .read_u32::<LittleEndian>()
60 .map_err(|_| ParserError::UnexpectedEof(self.position()))
61 }
62
63 pub fn read_u64_le(&mut self) -> ParserResult<u64> {
65 self.cursor
66 .read_u64::<LittleEndian>()
67 .map_err(|_| ParserError::UnexpectedEof(self.position()))
68 }
69
70 pub fn read_f32_le(&mut self) -> ParserResult<f32> {
72 self.cursor
73 .read_f32::<LittleEndian>()
74 .map_err(|_| ParserError::UnexpectedEof(self.position()))
75 }
76
77 pub fn read_f64_le(&mut self) -> ParserResult<f64> {
79 self.cursor
80 .read_f64::<LittleEndian>()
81 .map_err(|_| ParserError::UnexpectedEof(self.position()))
82 }
83
84 pub fn read_string(&mut self, len: usize) -> ParserResult<String> {
86 let mut buf = vec![0u8; len];
87 use std::io::Read;
88 self.cursor
89 .read_exact(&mut buf)
90 .map_err(|_| ParserError::UnexpectedEof(self.position()))?;
91
92 String::from_utf8(buf).map_err(|_| {
93 ParserError::ParseError {
94 offset: self.position(),
95 message: "Invalid UTF-8 sequence".to_string(),
96 }
97 })
98 }
99
100 pub fn read_cstring(&mut self) -> ParserResult<String> {
102 let mut result = String::new();
103 loop {
104 let byte = self.read_u8()?;
105 if byte == 0 {
106 break;
107 }
108 result.push(byte as char);
109 }
110 Ok(result)
111 }
112
113 pub fn read_bytes(&mut self, count: usize) -> ParserResult<Vec<u8>> {
115 let mut buf = vec![0u8; count];
116 use std::io::Read;
117 self.cursor
118 .read_exact(&mut buf)
119 .map_err(|_| ParserError::UnexpectedEof(self.position()))?;
120 Ok(buf)
121 }
122
123 pub fn is_eof(&self) -> bool {
125 self.cursor.position() as usize >= self.cursor.get_ref().len()
126 }
127
128 pub fn len(&self) -> usize {
130 self.cursor.get_ref().len()
131 }
132
133 pub fn is_empty(&self) -> bool {
135 self.len() == 0
136 }
137
138 pub fn remaining(&self) -> usize {
140 self.len() - self.position()
141 }
142}
143
144#[cfg(test)]
145mod tests {
146 use super::*;
147
148 #[test]
149 fn test_read_u8() {
150 let mut reader = BinaryReader::new(vec![42, 43, 44]);
151 assert_eq!(reader.read_u8().unwrap(), 42);
152 assert_eq!(reader.position(), 1);
153 }
154
155 #[test]
156 fn test_read_u32_le() {
157 let bytes = vec![0x01, 0x02, 0x03, 0x04];
158 let mut reader = BinaryReader::new(bytes);
159 assert_eq!(reader.read_u32_le().unwrap(), 0x04030201);
160 }
161
162 #[test]
163 fn test_peek_u8() {
164 let mut reader = BinaryReader::new(vec![42]);
165 assert_eq!(reader.peek_u8().unwrap(), 42);
166 assert_eq!(reader.position(), 0); }
168
169 #[test]
170 fn test_eof() {
171 let mut reader = BinaryReader::new(vec![42]);
172 reader.read_u8().unwrap();
173 assert!(reader.is_eof());
174 }
175}