(exe: string, cb?: (str: string, fatal: boolean) => void)
| 732 | // Returns true if the ELF header match the elf magic number, false in all other cases |
| 733 | // |
| 734 | export function validateELFHeader(exe: string, cb?: (str: string, fatal: boolean) => void): boolean { |
| 735 | try { |
| 736 | if (!fs.existsSync(exe)) { |
| 737 | if (cb) { |
| 738 | cb(`File not found "executable": "${exe}"`, true); |
| 739 | } |
| 740 | return false; |
| 741 | } |
| 742 | const buffer = Buffer.alloc(16); |
| 743 | const fd = fs.openSync(exe, 'r'); |
| 744 | const n = fs.readSync(fd, buffer, 0, 16, 0); |
| 745 | fs.closeSync(fd); |
| 746 | if (n !== 16) { |
| 747 | if (cb) { |
| 748 | cb(`Could not read 16 bytes from "executable": "${exe}"`, true); |
| 749 | } |
| 750 | return false; |
| 751 | } |
| 752 | // First four chars are 0x7f, 'E', 'L', 'F' |
| 753 | if ((buffer[0] !== 0x7f) || (buffer[1] !== 0x45) || (buffer[2] !== 0x4c) || (buffer[3] !== 0x46)) { |
| 754 | if (cb) { |
| 755 | cb(`Not a valid ELF file "executable": "${exe}". Many debug functions can fail or not work properly`, false); |
| 756 | } |
| 757 | return false; |
| 758 | } |
| 759 | return true; |
| 760 | } |
| 761 | catch (e) { |
| 762 | if (cb) { |
| 763 | cb(`Could not read file "executable": "${exe}" ${e ? e.toString() : ''}`, true); |
| 764 | } |
| 765 | return false; |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | // |
| 770 | // You have two choices. |
no test coverage detected