logo

deblob

remove binary executables from a directory git clone https://hacktivis.me/git/deblob.git
commit: 074b50fa51aebb4378bc1e50fac94a4332bd6643
parent af06251e4d87bec3ce78ea1178ee71f0c87cc857
Author: Drew DeVault <sir@cmpwn.com>
Date:   Fri, 18 Feb 2022 10:21:57 +0100

Move magic numbers into global array

Diffstat:

Mmain.ha21+++++++++++++++------
1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/main.ha b/main.ha @@ -8,6 +8,7 @@ use getopt; use io; use os; use path; +use strings; type invalid = !void; type ok = !void; @@ -15,11 +16,14 @@ type ok = !void; let excludes: []str = []; let noop: bool = false; +const magic: [_]str = [ + "\x7FELF", + "!<arch>\n", + "\x55\xAA", +]; + fn is_blob(filename: str) (bool | invalid) = { static let buffer: [512]u8 = [0...]; - static const elf: []u8 = [0x7F, 'E', 'L', 'F']; - static const ar: []u8 = ['!', '<', 'a', 'r', 'c', 'h', '>', '\n']; // <arch>! - static const bios: []u8 = [0x55, 0xAA]; const file = match (os::open(filename)) { case let f: io::file => @@ -42,9 +46,14 @@ fn is_blob(filename: str) (bool | invalid) = { return invalid; }; - return bytes::hasprefix(buffer, elf) - || bytes::hasprefix(buffer, ar) - || bytes::hasprefix(buffer, bios); + for (let i = 0z; i < len(magic); i += 1) { + const magic = strings::toutf8(magic[i]); + if (bytes::hasprefix(buffer, magic)) { + return true; + }; + }; + + return false; }; @test fn is_blob() void = {