Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add type hints in strings.py and file.py #1565

Merged
merged 6 commits into from
Mar 10, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 7 additions & 7 deletions cve_bin_tool/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
from cve_bin_tool.async_utils import FileIO, run_coroutine


async def aio_is_binary(filename):
async def aio_is_binary(filename: str) -> bool:
"""Read the magic bytes from a file and determine if it is an executable
binary."""
signature = await read_signature(filename)
signature: bytes = await read_signature(filename)
for name, method in inspect.getmembers(
sys.modules[__name__], predicate=inspect.isfunction
):
Expand All @@ -24,26 +24,26 @@ async def aio_is_binary(filename):
return False


def is_binary(filename):
def is_binary(filename: str) -> bool:
return run_coroutine(aio_is_binary(filename))


async def read_signature(filename, length=4):
async def read_signature(filename: str, length: int = 4) -> bytes:
"""Read the signature, first length bytes, from filename."""
async with FileIO(filename, "rb") as file_handle:
return await file_handle.read(length)


def check_elf(_filename, signature):
def check_elf(_filename: str, signature: bytes) -> bool:
"""Check for an ELF signature."""
return signature == b"\x7f\x45\x4c\x46"


def check_pe(_filename, signature):
def check_pe(_filename: str, signature: bytes) -> bool:
"""Check for windows/dos PE signature, aka 0x5a4d."""
return signature[:4] == b"\x4d\x5a"


def check_fake_test(_filename, signature):
def check_fake_test(_filename: str, signature: bytes) -> bool:
"""check for fake tests under windows."""
return signature == b"MZ\x90\x00"
14 changes: 8 additions & 6 deletions cve_bin_tool/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,24 @@
and MacOS.
"""

from typing import ClassVar, List, Set

from cve_bin_tool.async_utils import FileIO, run_coroutine


class Strings:
# printable characters
PRINTABLE = set(range(32, 128))
PRINTABLE: ClassVar[Set[int]] = set(range(32, 128))
# add tab to the printable character
PRINTABLE.add(9)

def __init__(self, filename=""):
def __init__(self, filename: str = "") -> None:
self.filename = filename
self.output = ""
self.output: str = ""

async def aio_parse(self):
async def aio_parse(self) -> str:
async with FileIO(self.filename, "rb") as f:
tmp = []
tmp: List[str] = []
async for line in f:
for char in line:
# remove all unprintable characters
Expand All @@ -33,5 +35,5 @@ async def aio_parse(self):
tmp = []
return self.output

def parse(self):
def parse(self) -> str:
return run_coroutine(self.aio_parse())