logo

deblob

remove binary executables from a directory git clone https://hacktivis.me/git/deblob.git
commit: 1b5b4e32509df0c7c55fe198b04f571f82390842
parent 615d91e277233e4c1492874ed9d4078304906ca3
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Fri, 30 Sep 2022 06:54:54 +0200

Add support for All Portable Executable (aka .exe) files

Diffstat:

Mdeblob.11-
Mmain.ha17+++++++++++++++--
Atest/fixtures/monodx.dll0
Atest/fixtures/monodx.dll.license2++
4 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/deblob.1 b/deblob.1 @@ -54,7 +54,6 @@ Portable Executable files, typically .Sq *.exe and .Sq *.dll . -Supported variants: ECMA-335 aka .Net .It Lua bytecode .It diff --git a/main.ha b/main.ha @@ -1,6 +1,7 @@ // Copyright © 2019-2022 deblob Authors <https://hacktivis.me/projects/deblob> // SPDX-License-Identifier: BSD-3-Clause use bytes; +use endian; use errors; use fmt; use fnmatch; @@ -30,6 +31,8 @@ const magic: [_][]u8 = [ [0x61, 0x0D, '\r', '\n'], // (3425i litte-endian) Python 3.9 [0x6F, 0x0D, '\r', '\n'], // (3439i litte-endian) Python 3.10 ]; +const dos_magic: []u8 = ['M', 'Z']; +const pe_magic: []u8 = ['P', 'E', 0x00, 0x00]; fn is_blob(filename: str) (bool | fs::error | io::error) = { static let buffer: [512]u8 = [0...]; @@ -49,6 +52,15 @@ fn is_blob(filename: str) (bool | fs::error | io::error) = { }; }; + // Special check to detect *all* Microsoft Portable Executable files + if (bytes::hasprefix(buffer, dos_magic)) { + const pe_offset = endian::legetu32(buffer[60..64]); + + if ((pe_offset < 512+4) && bytes::hasprefix(buffer[pe_offset..pe_offset+4], pe_magic)) { + return true; + }; + }; + return false; }; @@ -65,6 +77,7 @@ fn is_blob(filename: str) (bool | fs::error | io::error) = { ("test/fixtures/hello.o", true), ("test/fixtures/hello.a", true), ("test/fixtures/hello.exe", true), + ("test/fixtures/monodx.dll", true), ("test/fixtures/hello.wasm", true), ("test/fixtures/Elixir.Hex.API.Auth.beam", true), //("test/fixtures/option.rom", true), @@ -170,7 +183,7 @@ fn check_dir(dirname: str) (void | errors::invalid) = { case let e: fs::error => fmt::fatalf("os::readdir({}): {}", dirname, fs::strerror(e)); }; - assert(len(files_before) == 22); + assert(len(files_before) == 24); const ret = check_dir(dirname); assert(ret is void); @@ -181,7 +194,7 @@ fn check_dir(dirname: str) (void | errors::invalid) = { case let e: fs::error => fmt::fatalf("os::readdir({}): {}", dirname, fs::strerror(e)); }; - assert(len(files_after) == 13); + assert(len(files_after) == 14); }; export fn main() void = { diff --git a/test/fixtures/monodx.dll b/test/fixtures/monodx.dll Binary files differ. diff --git a/test/fixtures/monodx.dll.license b/test/fixtures/monodx.dll.license @@ -0,0 +1,2 @@ +SPDX-FileCopyrightText: 2013 Alistair Leslie-Hughes +SPDX-License-Identifier: MIT