Skip to content

Commit

Permalink
Merge branch 'main' into feature/expression_parser_precedence_negation
Browse files Browse the repository at this point in the history
  • Loading branch information
Schamper committed Aug 31, 2023
2 parents 1c3dc82 + c254806 commit 2fff05a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
6 changes: 3 additions & 3 deletions dissect/cstruct/cstruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import ctypes as _ctypes
import sys
from typing import Any, BinaryIO
from typing import Any, BinaryIO, Optional

from dissect.cstruct.exceptions import ResolveError
from dissect.cstruct.parser import CStyleParser, TokenParser
Expand Down Expand Up @@ -30,7 +30,7 @@ class cstruct:
DEF_CSTYLE = 1
DEF_LEGACY = 2

def __init__(self, endian: str = "<", pointer: str = None):
def __init__(self, endian: str = "<", pointer: Optional[str] = None):
self.endian = endian

self.consts = {}
Expand Down Expand Up @@ -146,7 +146,7 @@ def __init__(self, endian: str = "<", pointer: str = None):
}
# fmt: on

pointer = pointer or "uint64" if sys.maxsize > 2**32 else "uint32"
pointer = pointer or ("uint64" if sys.maxsize > 2**32 else "uint32")
self.pointer = self.resolve(pointer)
self._anonymous_count = 0

Expand Down
4 changes: 3 additions & 1 deletion tests/test_align.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,11 @@ def test_align_pointer():
uint16 d;
};
"""
c = cstruct.cstruct()
c = cstruct.cstruct(pointer="uint64")
c.load(d, align=True)

assert c.pointer is c.uint64

fields = c.test.fields
assert c.test.align
assert c.test.alignment == 8
Expand Down
1 change: 0 additions & 1 deletion tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def test_preserve_comment_newlines():
#define multi_anchor
"""
data = TokenParser._remove_comments(cdef)
print(repr(data))

mock_token = Mock()
mock_token.match.string = data
Expand Down
18 changes: 18 additions & 0 deletions tests/test_pointer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from unittest.mock import patch

import pytest
from dissect import cstruct

Expand All @@ -18,6 +20,7 @@ def test_pointer_basic(compiled):
cs.load(cdef, compiled=compiled)

assert verify_compiled(cs.ptrtest, compiled)
assert cs.pointer is cs.uint16

buf = b"\x04\x00\x08\x00\x01\x02\x03\x04\x05\x06\x07\x08"
obj = cs.ptrtest(buf)
Expand Down Expand Up @@ -63,6 +66,7 @@ def test_pointer_struct(compiled):

assert verify_compiled(cs.test, compiled)
assert verify_compiled(cs.ptrtest, compiled)
assert cs.pointer is cs.uint16

buf = b"\x02\x00testt\x00e\x00s\x00t\x00\x01\x02\x03\x04\x05\x06\x07lalala\x00t\x00e\x00s\x00t\x00\x00\x00"
obj = cs.ptrtest(buf)
Expand Down Expand Up @@ -95,6 +99,7 @@ def test_array_of_pointers(compiled):
cs.load(cdef, compiled=compiled)

assert verify_compiled(cs.mainargs, compiled)
assert cs.pointer is cs.uint16

buf = b"\x02\x09\x00\x16\x00\x00\x00\x00\x00argument one\x00argument two\x00"
obj = cs.mainargs(buf)
Expand Down Expand Up @@ -142,3 +147,16 @@ def test_pointer_arithmetic():

inst |= 8
assert inst._addr == 12


def test_pointer_sys_size():
with patch("sys.maxsize", 2**64):
c = cstruct.cstruct()
assert c.pointer is c.uint64

with patch("sys.maxsize", 2**32):
c = cstruct.cstruct()
assert c.pointer is c.uint32

c = cstruct.cstruct(pointer="uint16")
assert c.pointer is c.uint16

0 comments on commit 2fff05a

Please sign in to comment.