Finding consecutive values in Vec<u8>

Trying to find the best Rust method to search for four consecutive values in a Vec array.

Using this as a start but not getting the results I expect. Value is populated by reading in binary data from various sources; files, registry, ... .

 value: &Vec<u8>
 if value.windows(4).any(|b| 
                            b[0] == 0x4D && 
                            b[1] == 0x5A &&
                            b[2] == 0x90 &&
                            b[3] == 0x00) { println!("found"}; }

Basically I'm looking for a Windows PE MZ header. I'm obviously not understanding something.

What about this:

fn main() {
	let value = vec!(1, 2, 3, 4, 0x4D, 0x5A, 0x90, 0x00, 5, 6);

	for i in value.windows(4) {
		if i == [0x4D, 0x5A, 0x90, 0x00] {
			println!("found!");
            break;
		}
	}
}
2 Likes

And this also works.

fn main() {
	let value = vec!(1, 2, 3, 4, 0x4D, 0x5A, 0x90, 0x00, 5, 6);

	if value.windows(4).any(|s| matches!(s, [0x4D, 0x5A, 0x90, 0x00])) {
		println!("found!");
	}
}
1 Like