AndrewSpittlemeister / bytechomp

A pure python declarative custom binary protocol parser & generator using dataclasses and type hinting. Like Pydantic for binary protocols.
MIT License
38 stars 7 forks source link

U8 fields bug #2

Closed psylity closed 9 months ago

psylity commented 10 months ago

I experience an ugly issue :(

following code

from dataclasses import dataclass
from bytechomp import Reader
from bytechomp.datatypes import U8, U32

@dataclass
class TestClass:
    F0: U32
    F1: U8
    F2: U32
    F3: U32

input_data = b"\x00\x00\x00\x00\xAA\xBB\xBB\xBB\xBB\xCC\xCC\xCC\xCC\x00\x00\x00"  # had to add \x00 * 3 to trigger reader.is_complete()

def hexdump(data: bytes):
    return "".join("%02x " % b for b in data)

reader = Reader[TestClass]().allocate()
reader.feed(input_data)
if reader.is_complete():
    response = reader.build()
    print("input data: ", hexdump(input_data))
    print("F0: %08X" % response.F0)
    print("F1: %08X" % response.F1)
    print("F2: %08X" % response.F2)
    print("F3: %08X" % response.F3)

returns:

input data:  00 00 00 00 aa bb bb bb bb cc cc cc cc 00 00 00 
F0: 00000000
F1: 000000AA
F2: CCCCCCBB
F3: 000000CC

expected behavior:

input data:  00 00 00 00 aa bb bb bb bb cc cc cc cc 00 00 00 
F0: 00000000
F1: 000000AA
F2: BBBBBBBB
F3: CCCCCCCC
bmarroquin commented 9 months ago

It looks like the issue is with adding the native @ prefix. Changing @ to = creates the expected behavior. The native size adds padding bytes. I'll submit a PR to add the Byte Order NATIVE_STANDARD and it will work as expected. I think this should be the default as it will be more reliable. more intuitive.

AndrewSpittlemeister commented 9 months ago

@bmarroquin, merged in your pull request, thanks for giving some life to this project lol. Hopefully in the future I'll be able to do work on projects like this.