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 3 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
12 changes: 6 additions & 6 deletions cve_bin_tool/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
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)
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"
10 changes: 5 additions & 5 deletions cve_bin_tool/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ class Strings:
# add tab to the printable character
PRINTABLE.add(9)

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

async def aio_parse(self):
async def aio_parse(self) -> str:
async with FileIO(self.filename, "rb") as f:
tmp = []
rhythmrx9 marked this conversation as resolved.
Show resolved Hide resolved
async for line in f:
Expand All @@ -33,5 +33,5 @@ async def aio_parse(self):
tmp = []
return self.output

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