infer/matchers/
app.rs

1/// Returns whether a buffer is a wasm.
2///
3/// # Examples
4///
5/// ```rust
6/// use std::fs;
7/// assert!(infer::app::is_wasm(&fs::read("testdata/sample.wasm").unwrap()));
8/// ```
9pub fn is_wasm(buf: &[u8]) -> bool {
10    // WASM has starts with `\0asm`, followed by the version.
11    // http://webassembly.github.io/spec/core/binary/modules.html#binary-magic
12    buf.len() >= 8
13        && buf[0] == 0x00
14        && buf[1] == 0x61
15        && buf[2] == 0x73
16        && buf[3] == 0x6D
17        && buf[4] == 0x01
18        && buf[5] == 0x00
19        && buf[6] == 0x00
20        && buf[7] == 0x00
21}
22
23/// Returns whether a buffer is an EXE.
24///
25/// # Example
26///
27/// ```rust
28/// use std::fs;
29/// assert!(infer::app::is_exe(&fs::read("testdata/sample.exe").unwrap()));
30/// ```
31pub fn is_exe(buf: &[u8]) -> bool {
32    buf.len() > 1 && buf[0] == 0x4D && buf[1] == 0x5A
33}
34
35/// Returns whether a buffer is an ELF.
36pub fn is_elf(buf: &[u8]) -> bool {
37    buf.len() > 52 && buf[0] == 0x7F && buf[1] == 0x45 && buf[2] == 0x4C && buf[3] == 0x46
38}
39
40/// Returns whether a buffer is compiled Java bytecode.
41pub fn is_java(buf: &[u8]) -> bool {
42    buf.len() >= 8
43        && buf[0] == 0x43
44        && buf[1] == 0x41
45        && buf[2] == 0x76
46        && buf[3] == 0x45
47        && ((buf[4] == 0x42 && buf[5] == 0x01 && buf[6] == 0x42 && buf[7] == 0x45)
48            || (buf[4] == 0x44 && buf[5] == 0x30 && buf[6] == 0x30 && buf[7] == 0x44))
49}
50
51/// Returns whether a buffer is LLVM Bitcode.
52pub fn is_llvm(buf: &[u8]) -> bool {
53    buf.len() >= 2 && buf[0] == 0x42 && buf[1] == 0x43
54}