Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .dielib_commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
09df9ccafe48a0531987ad1e605402ed79d4c3f6
4 changes: 3 additions & 1 deletion cmake/FindDieLibrary.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,12 @@ list(INSERT CMAKE_MODULE_PATH 0

find_package(Qt6 REQUIRED COMPONENTS Core Qml Concurrent)

file(STRINGS "${ROOT_DIR}/.dielib_commit" DIE_LIBRARY_GIT_TAG)
message(STATUS "Using tag ${DIE_LIBRARY_GIT_TAG} for DieLibrary")
FetchContent_Declare(
DieLibrary
GIT_REPOSITORY "https://github.com/horsicq/die_library"
GIT_TAG 09df9ccafe48a0531987ad1e605402ed79d4c3f6
GIT_TAG "${DIE_LIBRARY_GIT_TAG}"
)

set(DIE_BUILD_AS_STATIC ON CACHE INTERNAL "")
Expand Down
1 change: 1 addition & 0 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ target_compile_definitions(_die
PRIVATE
DIE_VERSION="${DIE_VERSION}"
DIELIB_VERSION="${DIELIB_VERSION}"
DIELIB_TAG="${DIE_LIBRARY_GIT_TAG}"
)

target_link_libraries(_die PRIVATE $<TARGET_FILE:die> $<TARGET_PROPERTY:die,LINK_LIBRARIES>)
Expand Down
23 changes: 13 additions & 10 deletions python/die/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pathlib
import warnings

from typing import Generator, Optional, Union
from typing import Generator

from ._die import __version__ # ty:ignore[unresolved-import]
from ._die import DieFlags as _DieFlags # ty:ignore[unresolved-import]
Expand All @@ -13,9 +13,9 @@
ScanMemoryExA as _ScanMemoryExA,
LoadDatabaseA as _LoadDatabaseA,
)
from ._die import die_version, dielib_version # ty:ignore[unresolved-import]
from ._die import die_version, dielib_version, dielib_tag # ty:ignore[unresolved-import]

__all__ = ["die_version", "dielib_version"]
__all__ = ["die_version", "dielib_version", "dielib_tag"]
version_major, version_minor, version_patch = map(int, __version__.split("."))


Expand Down Expand Up @@ -108,7 +108,7 @@ def iterdir(self):


# Initialize database path with smart handling
database_path = _DatabasePath(__path__[0]) / "db"
database_path: pathlib.Path = _DatabasePath(__path__[0]) / "db"
"""Path to the DIE signature database

This path automatically points to the correct database location,
Expand Down Expand Up @@ -138,8 +138,8 @@ class ScanFlags(enum.IntFlag):


def scan_file(
filepath: Union[pathlib.Path, str], flags: ScanFlags, database: Optional[str] = None
) -> Optional[str]:
filepath: pathlib.Path | str, flags: ScanFlags, database: str | None = None
) -> str | None:
"""
Scan the given file against the signature database, if specified

Expand Down Expand Up @@ -192,8 +192,8 @@ def __enum_db(root: pathlib.Path) -> Generator[pathlib.Path, None, None]:


def scan_memory(
memory: Union[bytes, bytearray], flags: ScanFlags, database: Optional[str] = None
) -> Optional[str]:
memory: bytes | bytearray, flags: ScanFlags, database: str | None = None
) -> str | None:
"""
Scan the given sequence of bytes against the signature database, if specified

Expand All @@ -220,11 +220,14 @@ def scan_memory(
return res.strip()


def load_database(database: str) -> int:
def load_database(database: str | pathlib.Path) -> int:
"""
Load a database
"""
if not isinstance(database, str):
if isinstance(database, pathlib.Path):
database = str(database)

elif not isinstance(database, str):
raise TypeError

return _LoadDatabaseA(database)
4 changes: 4 additions & 0 deletions python/inc/die.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
#define DIE_VERSION ""
#endif // DIE_VERSION

#ifndef DIELIB_TAG
#define DIELIB_TAG ""
#endif // DIELIB_TAG

#ifdef __cplusplus
namespace DIE
{
Expand Down
1 change: 1 addition & 0 deletions python/src/die.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ NB_MODULE(_die, m)
m.attr("__version__") = "0.5.0";
m.attr("die_version") = DIE_VERSION;
m.attr("dielib_version") = DIELIB_VERSION;
m.attr("dielib_tag") = DIELIB_TAG;

m.def("ScanFileA", DIE::ScanFileA, "filename"_a, "flags"_a, "database"_a, "Scan a file against known signatures");

Expand Down
21 changes: 14 additions & 7 deletions python/tests/test_die.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def test_constants():
assert die.die_version
assert isinstance(die.dielib_version, str)
assert die.dielib_version
assert isinstance(die.dielib_tag, str)
assert die.dielib_tag

# validate die database
assert isinstance(die.database_path, die._DatabasePath)
Expand Down Expand Up @@ -117,9 +119,11 @@ def test_scan_export_format_xml(target_binary: pathlib.Path) -> None:
assert xml.Result
if platform.system() == "Windows":
assert hasattr(xml.Result, "PE64")
assert xml.Result.PE64
assert xml.Result.PE64["filetype"] == "PE64"
elif platform.system() == "Linux":
assert hasattr(xml.Result, "ELF64")
assert xml.Result.ELF64
Comment thread
calladoum-elastic marked this conversation as resolved.
Comment thread
calladoum-elastic marked this conversation as resolved.
assert xml.Result.ELF64["filetype"] == "ELF64"


Expand Down Expand Up @@ -170,16 +174,18 @@ def test_database_path_backward_compatibility():
# Test 2: database_path should resolve to a valid location with PE/ directory
db_path = pathlib.Path(path_new)
assert db_path.exists(), f"Database path does not exist: {db_path}"
assert (db_path / 'PE').exists(), f"PE directory not found at {db_path}"
assert (db_path / "PE").exists(), f"PE directory not found at {db_path}"

# Test 3: Old usage with /'db' should work through smart path resolution
# The smart path should detect the version and handle accordingly
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
path_old = str(die.database_path / 'db')
path_old = str(die.database_path / "db")

# The path should exist in both old and new versions
assert pathlib.Path(path_old).exists(), f"Old usage path doesn't exist: {path_old}"
assert pathlib.Path(path_old).exists(), (
f"Old usage path doesn't exist: {path_old}"
)

if len(w) > 0:
# New fixed version: got deprecation warning
Expand All @@ -200,13 +206,14 @@ def test_database_path_resolves_correctly():
db_path = pathlib.Path(str(die.database_path))

# Check for PE directory (main signature database)
assert (db_path / 'PE').exists(), f"PE directory not found at {db_path}"
assert (db_path / "PE").exists(), f"PE directory not found at {db_path}"

# Check for other expected directories
expected_dirs = ['PE', 'ELF', 'MACH']
expected_dirs = ["PE", "ELF", "MACH"]
for dir_name in expected_dirs:
assert (db_path / dir_name).exists(), \
assert (db_path / dir_name).exists(), (
f"Expected directory {dir_name} not found at {db_path}"
)


def test_scan_with_explicit_database_path(target_binary: pathlib.Path):
Expand All @@ -230,7 +237,7 @@ def test_scan_with_explicit_database_path(target_binary: pathlib.Path):
res = die.scan_file(
target_binary,
die.ScanFlags.DEEP_SCAN,
database=str(die.database_path / 'db'),
database=str(die.database_path / "db"),
)
assert res
assert isinstance(res, str)
Loading