Skip to content

Commit

Permalink
Rewrite type system and compiler (v4) (#35)
Browse files Browse the repository at this point in the history
🇯🇵
  • Loading branch information
Schamper authored May 17, 2024
1 parent ca4b29a commit cd9572c
Show file tree
Hide file tree
Showing 60 changed files with 5,146 additions and 3,001 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ assert a.dumps() == d
The API to access enum members and their values is similar to that of the native Enum type in Python 3. Functionally, it's best comparable to the IntEnum type.

### Custom types
You can implement your own types by subclassing `BaseType` or `RawType`, and adding them to your cstruct instance with `addtype(name, type)`
You can implement your own types by subclassing `BaseType` or `RawType`, and adding them to your cstruct instance with `add_type(name, type)`

### Custom definition parsers
Don't like the C-like definition syntax? Write your own syntax parser!
Expand Down
69 changes: 36 additions & 33 deletions dissect/cstruct/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from dissect.cstruct.bitbuffer import BitBuffer
from dissect.cstruct.compiler import Compiler
from dissect.cstruct.cstruct import cstruct, ctypes, ctypes_type
from dissect.cstruct.exceptions import (
Error,
Expand All @@ -8,18 +7,25 @@
ResolveError,
)
from dissect.cstruct.expression import Expression
from dissect.cstruct.types.base import Array, BaseType, RawType
from dissect.cstruct.types.bytesinteger import BytesInteger
from dissect.cstruct.types.chartype import CharType
from dissect.cstruct.types.enum import Enum, EnumInstance
from dissect.cstruct.types.flag import Flag, FlagInstance
from dissect.cstruct.types.instance import Instance
from dissect.cstruct.types.leb128 import LEB128
from dissect.cstruct.types.packedtype import PackedType
from dissect.cstruct.types.pointer import Pointer, PointerInstance
from dissect.cstruct.types.structure import Field, Structure, Union
from dissect.cstruct.types.voidtype import VoidType
from dissect.cstruct.types.wchartype import WcharType
from dissect.cstruct.types import (
LEB128,
Array,
BaseType,
Char,
CharArray,
Enum,
Field,
Flag,
Int,
MetaType,
Packed,
Pointer,
Structure,
Union,
Void,
Wchar,
WcharArray,
)
from dissect.cstruct.utils import (
dumpstruct,
hexdump,
Expand All @@ -40,31 +46,28 @@
)

__all__ = [
"Compiler",
"Array",
"Union",
"Field",
"Instance",
"cstruct",
"ctypes",
"ctypes_type",
"LEB128",
"Structure",
"Expression",
"PackedType",
"Pointer",
"PointerInstance",
"VoidType",
"WcharType",
"RawType",
"Array",
"BaseType",
"CharType",
"Char",
"CharArray",
"Enum",
"EnumInstance",
"Expression",
"Field",
"Flag",
"FlagInstance",
"BytesInteger",
"Int",
"MetaType",
"Packed",
"Pointer",
"Structure",
"Union",
"Void",
"Wchar",
"WcharArray",
"BitBuffer",
"cstruct",
"ctypes",
"ctypes_type",
"dumpstruct",
"hexdump",
"pack",
Expand Down
8 changes: 4 additions & 4 deletions dissect/cstruct/bitbuffer.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from __future__ import annotations

from typing import TYPE_CHECKING, BinaryIO, Union
from typing import TYPE_CHECKING, BinaryIO

if TYPE_CHECKING:
from dissect.cstruct.types import RawType
from dissect.cstruct.types import BaseType


class BitBuffer:
Expand All @@ -17,7 +17,7 @@ def __init__(self, stream: BinaryIO, endian: str):
self._buffer = 0
self._remaining = 0

def read(self, field_type: RawType, bits: Union[int, bytes]) -> int:
def read(self, field_type: BaseType, bits: int) -> int:
if self._remaining == 0 or self._type != field_type:
self._type = field_type
self._remaining = field_type.size * 8
Expand All @@ -43,7 +43,7 @@ def read(self, field_type: RawType, bits: Union[int, bytes]) -> int:

return v

def write(self, field_type: RawType, data: int, bits: int) -> None:
def write(self, field_type: BaseType, data: int, bits: int) -> None:
if self._remaining == 0 or self._type != field_type:
if self._type:
self.flush()
Expand Down
Loading

0 comments on commit cd9572c

Please sign in to comment.