Skip to content

Commit

Permalink
Small changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Schamper committed May 6, 2024
1 parent 4937662 commit e854073
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 18 deletions.
1 change: 0 additions & 1 deletion dissect/cstruct/cstruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,6 @@ def _make_array(self, type_: MetaType, num_entries: Optional[int | Expression])
"type": type_,
"num_entries": num_entries,
"null_terminated": null_terminated,
"dynamic": dynamic,
}

return self._make_type(name, bases, size, alignment=type_.alignment, attrs=attrs)
Expand Down
17 changes: 8 additions & 9 deletions dissect/cstruct/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
from typing import TYPE_CHECKING, Optional

from dissect.cstruct import compiler
from dissect.cstruct.exceptions import ParserError
from dissect.cstruct.exceptions import (
ExpressionParserError,
ExpressionTokenizerError,
ParserError,
)
from dissect.cstruct.expression import Expression
from dissect.cstruct.types import ArrayMetaType, Field, MetaType

Expand Down Expand Up @@ -89,7 +93,7 @@ def _constant(self, tokens: TokenConsumer) -> None:
if isinstance(value, str):
try:
value = Expression(self.cstruct, value).evaluate()
except Exception:
except (ExpressionParserError, ExpressionTokenizerError):
pass

self.cstruct.consts[match["name"]] = value
Expand Down Expand Up @@ -131,9 +135,7 @@ def _enum(self, tokens: TokenConsumer) -> None:
if not d["type"]:
d["type"] = "uint32"

factory = self.cstruct._make_enum
if enumtype == "flag":
factory = self.cstruct._make_flag
factory = self.cstruct._make_flag if enumtype == "flag" else self.cstruct._make_enum

enum = factory(d["name"] or "", self.cstruct.resolve(d["type"]), values)
if not enum.__name__:
Expand Down Expand Up @@ -165,10 +167,7 @@ def _typedef(self, tokens: TokenConsumer) -> None:
def _struct(self, tokens: TokenConsumer, register: bool = False) -> None:
stype = tokens.consume()

if stype.value.startswith("union"):
factory = self.cstruct._make_union
else:
factory = self.cstruct._make_struct
factory = self.cstruct._make_union if stype.value.startswith("union") else self.cstruct._make_struct

st = None
names = []
Expand Down
2 changes: 1 addition & 1 deletion dissect/cstruct/types/char.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def _write(cls, stream: BinaryIO, data: bytes) -> int:
if isinstance(data, list) and data and isinstance(data[0], int):
data = bytes(data)

if isinstance(data, str):
elif isinstance(data, str):
data = data.encode("latin-1")

if cls.null_terminated:
Expand Down
4 changes: 4 additions & 0 deletions dissect/cstruct/types/flag.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@ class Flag(BaseType, IntFlag, metaclass=EnumMetaType):
def __repr__(self) -> str:
result = super().__repr__()
if not self.__class__.__name__:
# Deal with anonymous enums by stripping off the first bit
# I.e. <.RED: 1> -> <RED: 1>
result = f"<{result[2:]}"
return result

def __str__(self) -> str:
result = super().__str__()
if not self.__class__.__name__:
# Deal with anonymous enums by stripping off the first bit
# I.e. <.RED: 1> -> <RED: 1>
result = f"<{result[1:]}"
return result

Expand Down
14 changes: 7 additions & 7 deletions dissect/cstruct/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pprint
import string
import sys
from typing import Iterator
from typing import Iterator, Optional

from dissect.cstruct.types.structure import Structure

Expand Down Expand Up @@ -38,7 +38,7 @@
Palette = list[tuple[str, str]]


def _hexdump(data: bytes, palette: Palette = None, offset: int = 0, prefix: str = "") -> Iterator[str]:
def _hexdump(data: bytes, palette: Optional[Palette] = None, offset: int = 0, prefix: str = "") -> Iterator[str]:
"""Hexdump some data.
Args:
Expand Down Expand Up @@ -99,8 +99,8 @@ def _hexdump(data: bytes, palette: Palette = None, offset: int = 0, prefix: str


def hexdump(
data: bytes, palette=None, offset: int = 0, prefix: str = "", output: str = "print"
) -> Iterator[str] | str | None:
data: bytes, palette: Optional[Palette] = None, offset: int = 0, prefix: str = "", output: str = "print"
) -> Optional[Iterator[str] | str]:
"""Hexdump some data.
Args:
Expand All @@ -127,7 +127,7 @@ def _dumpstruct(
offset: int,
color: bool,
output: str,
) -> str | None:
) -> Optional[str]:
palette = []
colors = [
(COLOR_RED, COLOR_BG_RED),
Expand Down Expand Up @@ -178,11 +178,11 @@ def _dumpstruct(

def dumpstruct(
obj: Structure | type[Structure],
data: bytes = None,
data: Optional[bytes] = None,
offset: int = 0,
color: bool = True,
output: str = "print",
) -> str | None:
) -> Optional[str]:
"""Dump a structure or parsed structure instance.
Prints a colorized hexdump and parsed structure output.
Expand Down

0 comments on commit e854073

Please sign in to comment.