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

Fix 32bit pointer support #46

Merged
merged 4 commits into from
Aug 29, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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