1+ use alloc:: vec;
12use alloc:: vec:: Vec ;
23
34use crate :: { Atom , BinaryPattern , JumpType , MatchTarget , ReadWidth } ;
@@ -17,18 +18,12 @@ pub struct BinaryMatcher<'a> {
1718
1819impl < ' a > BinaryMatcher < ' a > {
1920 pub fn new ( pattern : & ' a dyn BinaryPattern , target : & ' a dyn MatchTarget ) -> Self {
20- let mut save_stack = Vec :: new ( ) ;
21- save_stack. resize ( 8 , 0 ) ;
22-
23- let mut cursor_stack = Vec :: new ( ) ;
24- cursor_stack. resize ( 8 , 0 ) ;
25-
2621 Self {
2722 pattern,
2823 target,
2924
30- save_stack,
31- cursor_stack,
25+ save_stack : vec ! [ 0 ; 8 ] ,
26+ cursor_stack : vec ! [ 0 ; 8 ] ,
3227
3328 match_offset : 0 ,
3429 }
@@ -42,11 +37,7 @@ impl<'a> BinaryMatcher<'a> {
4237 match atoms[ atom_cursor] {
4338 Atom :: ByteSequence { seq_start, seq_end } => {
4439 let expected_bytes = & self . pattern . byte_sequence ( ) [ seq_start..seq_end] ;
45- let Some ( actual_bytes) =
46- self . target . subrange ( data_cursor, expected_bytes. len ( ) )
47- else {
48- return None ;
49- } ;
40+ let actual_bytes = self . target . subrange ( data_cursor, expected_bytes. len ( ) ) ?;
5041
5142 if expected_bytes != actual_bytes {
5243 return None ;
@@ -104,16 +95,11 @@ impl<'a> BinaryMatcher<'a> {
10495 self . save_stack . truncate ( save_stack_size) ;
10596 self . cursor_stack . truncate ( cursor_stack_size) ;
10697
107- if let Some ( data_cursor ) = self . match_atoms (
98+ self . match_atoms (
10899 data_cursor,
109100 & atoms[ atom_cursor + 1 + left_len
110101 ..atom_cursor + 1 + left_len + right_len] ,
111- ) {
112- data_cursor
113- } else {
114- /* non of the branches matched */
115- return None ;
116- }
102+ ) ?
117103 } ;
118104
119105 atom_cursor += 1 + left_len + right_len;
@@ -122,30 +108,18 @@ impl<'a> BinaryMatcher<'a> {
122108 Atom :: Jump ( mode) => {
123109 data_cursor = match mode {
124110 JumpType :: RelByte => {
125- let Some ( value) = self . target . subrange ( data_cursor, 1 ) else {
126- return None ;
127- } ;
128-
111+ let value = self . target . subrange ( data_cursor, 1 ) ?;
129112 ( data_cursor + 1 ) . wrapping_add_signed ( value[ 0 ] as i8 as isize )
130113 }
131114 JumpType :: RelDWord => {
132- let Some ( value) = self . target . subrange ( data_cursor, 4 ) else {
133- return None ;
134- } ;
135-
115+ let value = self . target . subrange ( data_cursor, 4 ) ?;
136116 let value = i32:: from_le_bytes ( value. try_into ( ) . unwrap ( ) ) ;
137117 ( data_cursor + 4 ) . wrapping_add_signed ( value as isize )
138118 }
139119 JumpType :: AbsQWord => {
140- let Some ( value) = self . target . subrange ( data_cursor, 8 ) else {
141- return None ;
142- } ;
120+ let value = self . target . subrange ( data_cursor, 8 ) ?;
143121 let value = u64:: from_le_bytes ( value. try_into ( ) . unwrap ( ) ) ;
144- let Some ( value) = self . target . translate_absolute_address ( value) else {
145- return None ;
146- } ;
147-
148- value
122+ self . target . translate_absolute_address ( value) ?
149123 }
150124 } ;
151125 atom_cursor += 1 ;
@@ -154,24 +128,15 @@ impl<'a> BinaryMatcher<'a> {
154128 Atom :: Read ( width) => {
155129 let ( value, width) = match width {
156130 ReadWidth :: Byte => {
157- let Some ( value) = self . target . subrange ( data_cursor, 1 ) else {
158- return None ;
159- } ;
160-
131+ let value = self . target . subrange ( data_cursor, 1 ) ?;
161132 ( value[ 0 ] as u32 , 1 )
162133 }
163134 ReadWidth :: Word => {
164- let Some ( value) = self . target . subrange ( data_cursor, 2 ) else {
165- return None ;
166- } ;
167-
135+ let value = self . target . subrange ( data_cursor, 2 ) ?;
168136 ( u16:: from_le_bytes ( value. try_into ( ) . unwrap ( ) ) as u32 , 2 )
169137 }
170138 ReadWidth :: DWord => {
171- let Some ( value) = self . target . subrange ( data_cursor, 4 ) else {
172- return None ;
173- } ;
174-
139+ let value = self . target . subrange ( data_cursor, 4 ) ?;
175140 ( u32:: from_le_bytes ( value. try_into ( ) . unwrap ( ) ) , 4 )
176141 }
177142 } ;
0 commit comments