-
Notifications
You must be signed in to change notification settings - Fork 153
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
Determine byte size of construct containing a Switch() using sizeof() with context fails on parent construct #1054
Comments
The problem from what I recall is that the kwarg passed to sizeof() is not accessible in the same way as context key obtained during parsing. In the switch, fiddle with |
Hello @arekbulski, |
Sorry, I dont know what is wrong with this parsing class, the Switch I mean. Can you use something like |
Hi @arekbulski, |
I used the latest version, 2.10.70. But Switch was not updated for years so that should not play a role. I think. |
I removed the I cannot make this work consistently. See below, please make sure the import construct
PACKET_STRUCT = construct.Struct(
"h" / construct.Struct(
"type" / construct.Enum(
construct.Int32ul,
p1=0x00000001,
p2=0x00000002,
),
),
"p" / construct.Switch(
construct.this.h.type, {
"p1": construct.Struct(
"v1" / construct.Int32ul,
),
"p2": construct.Struct(
"v1" / construct.Int32ul,
"v2" / construct.Int32ul,
),
},
),
)
print(PACKET_STRUCT.parse(bytearray((0x02, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x22, 0x22, 0x22, 0x22))))
print(f"p2 size [parent]: {PACKET_STRUCT.sizeof(h={'type': 'p2'})}") |
Yes I know... the change I made, it made sizeof() work but I think it breaks parse() at same time. 😞 WOrking with the Switch is a bitch. |
Hi @arekbulski, The import construct
PACKET_STRUCT = construct.Struct(
"l" / construct.Union(
"raw",
"raw" / construct.Int16ul,
"flags" / construct.Bitwise(
construct.Struct(
"f" / construct.Flag,
"p" / construct.Padding(15),
)
),
),
)
print(PACKET_STRUCT.sizeof()) Output:
While as far as I understand from documentation, if the 1st parameter of Is this correct? Should I open a new bug? Thanks, |
It's midnight here. I will investigate tomorrow and get back to you. Okay? |
Sure, it is also midnight here... sorry... nothing urgent.... I appreciate you replied so fast :) |
Hmm, now that I read it, the exception is different. It says explicitly that you cannot sizeof a Union. Its not a bug. |
but why? if it is a C like union, we can take the largest block, and construct added the 1st parameter to explicitly instruct how many bytes to advance in the stream, so in this explicit mode it is the size of the union in bytes or 0 if If you think that the 1st parameter is not a good candidate to determine the size in bytes of the union, please let a context to be passed so that the developer may select the union variant to calculate the size based on, so that this will behave similar to the Switch issue we discuss. End-state is to have |
I dont know why that is the case, I wrote this almost a decade ago. I will investigate it tomorrow in detail. |
Hi @arekbulski, |
None. I don't know how to fix this. |
Oh, that is sad... any alternative to switch? I thought of using union but union does not have sizeof as well and I do not understand how it parse provided a field. |
Yeah, the problem with Switch is actually several years old. You are not the first person who stumbled upon it. :| |
Maybe add Switchv2 that adds the required level? Currently it omits the key which causes the issue, in the following example, the import construct
PACKET_STRUCT = construct.Struct(
"h" / construct.Struct(
"type" / construct.Enum(
construct.Int32ul,
p1=0x00000001,
p2=0x00000002,
),
),
"p" / construct.Switch(
construct.this.h.type, {
"p1": construct.Struct(
"v1" / construct.Int32ul,
),
"p2": construct.Struct(
"v1" / construct.Int32ul,
"v2" / construct.Int32ul,
),
},
),
) |
Thanks! |
Hello,
I would like to calculate the size of struct with Switch statement, much similar to #692.
Each statement is working separately but the sizeof() of the entire construct blows up.
In my case I would assume that the following will calculate the size of
type==p2
packet size.The task is to determine the size without parse nor build with minimum requires context settings.
Maybe I am doing something wrong.
Version: construct-2.10.68
TEST
Calculate the size of
type==p2
packet:EXPECTED BEHAVIOR
ACTUAL BEHAVIOR
Thanks,
The text was updated successfully, but these errors were encountered: