Skip to content
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
72 changes: 38 additions & 34 deletions cstruct/c_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,43 +78,47 @@ def parse_type(tokens: Tokens, __cls__: Type['AbstractCStruct'], byte_order: Opt
# signed/unsigned/struct
if c_type in ['signed', 'unsigned', 'struct', 'union'] and len(tokens) > 1:
c_type = c_type + " " + tokens.pop()
next_token = tokens.pop()
# short int, long int, or long long
if next_token in ['int', 'long']:
c_type = c_type + " " + next_token
next_token = tokens.pop()
# void *
if next_token.startswith("*"):
next_token = next_token[1:]
c_type = 'void *'
# parse length

vlen = 1
flexible_array = False
if "[" in next_token:
t = next_token.split("[")
if len(t) != 2:
raise ParserError("Error parsing: " + next_token)
next_token = t[0].strip()
vlen_part = t[1]
vlen_expr = []
while not vlen_part.endswith("]"):

if not c_type.endswith("{"):
next_token = tokens.pop()
# short int, long int, or long long
if next_token in ['int', 'long']:
c_type = c_type + " " + next_token
next_token = tokens.pop()
# void *
if next_token.startswith("*"):
next_token = next_token[1:]
c_type = 'void *'
# parse length
if "[" in next_token:
t = next_token.split("[")
if len(t) != 2:
raise ParserError("Error parsing: " + next_token)
next_token = t[0].strip()
vlen_part = t[1]
vlen_expr = []
while not vlen_part.endswith("]"):
vlen_expr.append(vlen_part.split("]")[0].strip())
vlen_part = tokens.pop()
t_vlen = vlen_part.split("]")[0].strip()
vlen_expr.append(vlen_part.split("]")[0].strip())
vlen_part = tokens.pop()
t_vlen = vlen_part.split("]")[0].strip()
vlen_expr.append(vlen_part.split("]")[0].strip())
t_vlen = " ".join(vlen_expr)
if not t_vlen:
flexible_array = True
vlen = 0
else:
try:
vlen = c_eval(t_vlen)
except (ValueError, TypeError):
vlen = int(t_vlen)
tokens.push(next_token)
# resolve typedefs
while c_type in TYPEDEFS:
c_type = TYPEDEFS[c_type]
t_vlen = " ".join(vlen_expr)
if not t_vlen:
flexible_array = True
vlen = 0
else:
try:
vlen = c_eval(t_vlen)
except (ValueError, TypeError):
vlen = int(t_vlen)
tokens.push(next_token)
# resolve typedefs
while c_type in TYPEDEFS:
c_type = TYPEDEFS[c_type]

# calculate fmt
if c_type.startswith('struct ') or c_type.startswith('union '): # struct/union
c_type, tail = c_type.split(' ', 1)
Expand Down
5 changes: 4 additions & 1 deletion tests/test_cstruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,12 @@ class MBR(cstruct.CStruct):
class Dummy(cstruct.CStruct):
__byte_order__ = cstruct.LITTLE_ENDIAN
__def__ = """
struct {
struct {
int i;
} s;
char c;
char vc[10];
int i;
int vi[10];
long long l;
long vl[10];
Expand Down Expand Up @@ -210,6 +212,7 @@ def test_inline():

def test_dummy():
dummy = Dummy()

dummy.c = b'A'
dummy.vc = b'ABCDEFGHIJ'
dummy.i = 123456
Expand Down