|
1 | | -function y = is_executable_binary(filename) |
2 | 1 | % IS_EXECUTABLE_BINARY Check if a file is an executable binary by examining magic numbers |
3 | 2 | % |
4 | 3 | % NOTE: on Unix-like operating systems, often times what users run thinking |
5 | 4 | % it's a "program" is actually a shell script invoking a binary with |
6 | 5 | % options. |
7 | 6 | % fullfile(matlabroot, 'bin/matlab') |
8 | 7 | % is a shell script and thus is FALSE for this function. |
| 8 | +% |
| 9 | +% Inputs: |
| 10 | +% * filename: path to file |
| 11 | +% Outputs: |
| 12 | +% * y: true if the file is an executable binary, false otherwise |
| 13 | +% |
| 14 | +% This function checks the magic number of the file to determine if it is |
| 15 | +% an executable binary. It supports Windows (PE), macOS (Mach-O), and Linux |
| 16 | +% (ELF) formats. |
| 17 | + |
| 18 | +function y = is_executable_binary(filename) |
| 19 | +arguments |
| 20 | + filename {mustBeTextScalar} |
| 21 | +end |
9 | 22 |
|
10 | 23 | y = false; |
11 | 24 |
|
12 | 25 | fid = fopen(filename, 'rb'); |
13 | | -if fid < 0, return, end |
| 26 | +if fid < 0 |
| 27 | + return |
| 28 | +end |
14 | 29 |
|
15 | 30 | N = 4; |
16 | | -if ispc(), N = 2; end |
| 31 | +if ispc() |
| 32 | + N = 2; |
| 33 | +end |
17 | 34 |
|
18 | 35 | magic = fread(fid, N, 'uint8').'; |
19 | 36 | fclose(fid); |
20 | | -if numel(magic) < N, return, end |
| 37 | + |
| 38 | +if numel(magic) == N |
| 39 | + y = check_magic(magic); |
| 40 | +end |
| 41 | + |
| 42 | +end |
| 43 | + |
| 44 | + |
| 45 | +function y = check_magic(magic) |
21 | 46 |
|
22 | 47 | if ispc() |
23 | | - y = isequal(magic(1:2), [0x4d, 0x5a]); |
24 | | - % Check for PE magic number (MZ) |
| 48 | + y = isequal(magic(1:2), [0x4d, 0x5a]); |
| 49 | +% Check for PE magic number (MZ) |
25 | 50 | elseif ismac() |
26 | | - % Check for Mach-O magic number |
27 | | - feedface = [0xFE, 0xED, 0xFA, 0xCE]; |
28 | | - feedfacf = [0xFE, 0xED, 0xFA, 0xCF]; |
29 | | - cafebabe = [0xCA, 0xFE, 0xBA, 0xBE]; |
30 | | - cafebabf = [0xCA, 0xFE, 0xBA, 0xBF]; |
31 | | - for a = {feedface, fliplr(feedface), ... |
32 | | - feedfacf, fliplr(feedfacf), ... |
33 | | - cafebabe, fliplr(cafebabe), ... |
34 | | - cafebabf, fliplr(cafebabf)} |
35 | | - |
36 | | - y = isequal(magic, a{1}); |
37 | | - if y, return, end |
| 51 | + % Check for Mach-O magic number |
| 52 | + feedface = [0xFE, 0xED, 0xFA, 0xCE]; |
| 53 | + feedfacf = [0xFE, 0xED, 0xFA, 0xCF]; |
| 54 | + cafebabe = [0xCA, 0xFE, 0xBA, 0xBE]; |
| 55 | + cafebabf = [0xCA, 0xFE, 0xBA, 0xBF]; |
| 56 | + for a = {feedface, fliplr(feedface), ... |
| 57 | + feedfacf, fliplr(feedfacf), ... |
| 58 | + cafebabe, fliplr(cafebabe), ... |
| 59 | + cafebabf, fliplr(cafebabf)} |
| 60 | + |
| 61 | + y = isequal(magic, a{1}); |
| 62 | + if y |
| 63 | + return |
38 | 64 | end |
| 65 | + end |
39 | 66 | else |
40 | | - % Check for ELF magic number (0x7f followed by 'ELF') |
41 | | - y = isequal(magic, [0x7f, 0x45, 0x4c, 0x46]); |
| 67 | + % Check for ELF magic number (0x7f followed by 'ELF') |
| 68 | + y = isequal(magic, [0x7f, 0x45, 0x4c, 0x46]); |
42 | 69 | end |
43 | 70 |
|
44 | 71 | end |
0 commit comments