Checksum Types
CRC — Cyclic Redundancy Check
A CRC calculates a crc over the data in its hierarchical level, e.g. the oser.ByteStruct
it
is located in.
The user can control automatic calculation and checks by setting crc._automatic_calculation to False
at runtime.
CRCL8
8-bit crc little endian.
- class oser.CRCL8(strict: bool = False, polynomial: int = 213, reflect_input: bool = False, reflect_output: bool = False, initial_value: int = 0, xor_output: int = 0, automatic_calculation: bool = True, format: str = 'hex')
8-bit crc little endian serializer.
- Parameters:
strict – bool = False: If set to
True
crc is checked when decoding data. An exception is raised if crcs do not match.polynomial – int = 0xd5: the crc polynomial
reflect_input – bool = False: reflect input data
reflect_output – bool = False: reflect output data
initial_value – int = 0x00: start with initial_value
xor_output – int = 0x00: xor output data with xor_output
automatic_calculation – bool = True: if set to
True
crc is automatically calculated when encoding dataformat – str = “hex” (string): Use “hex” to format values to hexadecimal. Use “bin” to format values to binary. Use
None
for decimal format.
- decode(data: bytes, full_data: bytes = b'', context_data: bytes = b'') int
Decode a binary string into a byte type and return the number of bytes that were decoded.
- Parameters:
- Returns:
the number of bytes that were decoded.
- Return type:
- encode(full_data: bytes = b'', context_data: bytes = b'') bytes
Return the encoded byte type as a binary string.
- get_byte_size() int
Return the length of the byte type in bytes.
- Returns:
the length of the byte type in bytes.
- Return type:
- get_size() int
Return the length of the byte type in bytes.
- Returns:
the length of the byte type in bytes.
- Return type:
- introspect(stop_at: ByteStruct | BitStruct | ByteType | BitType | None = None) str
Return the introspection representation of the object as a string.
- Parameters:
stop_at=None (object) – stop introspection at
stop_at
.
- root() ByteStruct | BitStruct
return root element
- set_automatic_calculation(enabled: bool = True) None
Enable or disable automatic calculation when
encode
is called.- Parameters:
enabled=True (bool) – the new enabled state.
- set_fuzzing_values(values: Generator[Any, None, None] | List[Any] | None) None
Set fuzzing values.
- Parameters:
values (iterable) – the values used for fuzzing.
- up() ByteStruct | BitStruct
Return the parent element.
Usage:
>>> from oser import ByteStruct, UBInt16, CRCL8, to_hex
>>> class Data(ByteStruct):
... def __init__(self):
... super(Data, self).__init__()
...
... self.a = ULInt16(1)
... self.b = ULInt16(2)
...
... self.crc = CRCL8(strict=True)
...
>>> instance = Data()
>>> print(instance)
Data():
a: 1 (ULInt16)
b: 2 (ULInt16)
crc: 0 (CRCL8)
>>> print(instance.introspect())
- - Data():
0 \x01 a: 1 (ULInt16)
1 \x00
2 \x02 b: 2 (ULInt16)
3 \x00
4 \x00 crc: 0 (CRCL8)
>>> binary = instance.encode()
>>> print(to_hex(binary))
0| 1| 2| 3| 4
\x01\x00\x02\x00\x53
>>> bytes_decoded = instance.decode(binary)
>>> print(bytes_decoded)
5
>>> print(instance)
Data():
a: 1 (ULInt16)
b: 2 (ULInt16)
crc: 83 (CRCL8)
>>> print(instance.introspect())
- - Data():
0 \x01 a: 1 (ULInt16)
1 \x00
2 \x02 b: 2 (ULInt16)
3 \x00
4 \x53 crc: 83 (CRCL8)
>>> bytes_decoded = instance.decode(b"\x01\x00\x02\x00\x53")
>>> print(bytes_decoded)
5
>>> print(instance)
Data():
a: 1 (ULInt16)
b: 2 (ULInt16)
crc: 83 (CRCL8)
>>> print(instance.introspect())
- - Data():
0 \x01 a: 1 (ULInt16)
1 \x00
2 \x02 b: 2 (ULInt16)
3 \x00
4 \x53 crc: 83 (CRCL8)
>>> bytes_decoded = instance.decode(b"\x01\x00\x02\x00\x00") # corrupted crc
oser.ChecksumMismatchException: Data could not be decoded!
Parsing object is:
- - Data():
0 \x01 a: 1 (ULInt16)
1 \x00
2 \x02 b: 2 (ULInt16)
3 \x00
4 \x00 crc: 0 (CRCL8)
^^^^ Checksum mismatch: expected \x53 but found \x00
CRCB8
8-bit crc big endian.
- class oser.CRCB8(strict: bool = False, polynomial: int = 213, reflect_input: bool = False, reflect_output: bool = False, initial_value: int = 0, xor_output: int = 0, automatic_calculation: bool = True, format: str = 'hex')
8-bit crc big endian serializer.
- Parameters:
strict – bool = False: If set to
True
crc is checked when decoding data. An exception is raised if crcs do not match.polynomial – int = 0xd5: the crc polynomial
reflect_input – bool = False: reflect input data
reflect_output – bool = False: reflect output data
initial_value – int = 0x00: start with initial_value
xor_output – int = 0x00: xor output data with xor_output
automatic_calculation – bool = True: if set to
True
crc is automatically calculated when encoding dataformat – str = “hex” (string): Use “hex” to format values to hexadecimal. Use “bin” to format values to binary. Use
None
for decimal format.
- decode(data: bytes, full_data: bytes = b'', context_data: bytes = b'') int
Decode a binary string into a byte type and return the number of bytes that were decoded.
- get_byte_size() int
Return the length of the byte type in bytes.
- Returns:
the length of the byte type in bytes.
- Return type:
- get_size() int
Return the length of the byte type in bytes.
- Returns:
the length of the byte type in bytes.
- Return type:
- introspect(stop_at: ByteStruct | BitStruct | ByteType | BitType | None = None) str
Return the introspection representation of the object as a string.
- Parameters:
stop_at=None (object) – stop introspection at
stop_at
.
- root() ByteStruct | BitStruct
return root element
- set_automatic_calculation(enabled: bool = True) None
Enable or disable automatic calculation when
encode
is called.- Parameters:
enabled=True (bool) – the new enabled state.
- set_fuzzing_values(values: Generator[Any, None, None] | List[Any] | None) None
Set fuzzing values.
- Parameters:
values (iterable) – the values used for fuzzing.
- up() ByteStruct | BitStruct
Return the parent element.
Usage:
>>> from oser import ByteStruct, UBInt16, CRCB8, to_hex
>>> class Data(ByteStruct):
... def __init__(self):
... super(Data, self).__init__()
...
... self.a = UBInt16(1)
... self.b = UBInt16(2)
...
... self.crc = CRCB8(strict=True)
...
>>> instance = Data()
>>> print(instance)
Data():
a: 1 (UBInt16)
b: 2 (UBInt16)
crc: 0 (CRCB8)
>>> print(instance.introspect())
- - Data():
0 \x00 a: 1 (UBInt16)
1 \x01
2 \x00 b: 2 (UBInt16)
3 \x02
4 \x00 crc: 0 (CRCB8)
>>> binary = instance.encode()
>>> print(to_hex(binary))
0| 1| 2| 3| 4
\x00\x01\x00\x02\xFC
>>> bytes_decoded = instance.decode(binary)
>>> print(bytes_decoded)
5
>>> print(instance)
Data():
a: 1 (UBInt16)
b: 2 (UBInt16)
crc: 252 (CRCB8)
>>> print(instance.introspect())
- - Data():
0 \x00 a: 1 (UBInt16)
1 \x01
2 \x00 b: 2 (UBInt16)
3 \x02
4 \xfc crc: 252 (CRCB8)
>>> bytes_decoded = instance.decode(b"\x00\x01\x00\x02\xFC")
>>> print(bytes_decoded)
5
>>> print(instance)
Data():
a: 1 (UBInt16)
b: 2 (UBInt16)
crc: 252 (CRCB8)
>>> print(instance.introspect())
- - Data():
0 \x00 a: 1 (UBInt16)
1 \x01
2 \x00 b: 2 (UBInt16)
3 \x02
4 \xfc crc: 252 (CRCB8)
>>> bytes_decoded = instance.decode(b"\x00\x01\x00\x02\x00") # corrupted crc
oser.ChecksumMismatchException: Data could not be decoded!
Parsing object is:
- - Data():
0 \x00 a: 1 (UBInt16)
1 \x01
2 \x00 b: 2 (UBInt16)
3 \x02
4 \x00 crc: 0 (CRCB8)
^^^^ Checksum mismatch: expected \xfc but found \x00
CRCL16
16-bit crc little endian.
- class oser.CRCL16(strict: bool = False, polynomial: int = 41003, reflect_input: bool = False, reflect_output: bool = False, initial_value: int = 0, xor_output: int = 0, automatic_calculation: bool = True, format: str = 'hex')
16-bit crc little endian serializer.
- Parameters:
strict – bool = False: If set to
True
crc is checked when decoding data. An exception is raised if crcs do not match.polynomial – int = 0xa02b: the crc polynomial
reflect_input – bool = False: reflect input data
reflect_output – bool = False: reflect output data
initial_value – int = 0x0000: start with initial_value
xor_output – int = 0x0000: xor output data with xor_output
automatic_calculation – bool = True: if set to
True
crc is automatically calculated when encoding dataformat – str = “hex” (string): Use “hex” to format values to hexadecimal. Use “bin” to format values to binary. Use
None
for decimal format.
- decode(data: bytes, full_data: bytes = b'', context_data: bytes = b'') int
Decode a binary string into a byte type and return the number of bytes that were decoded.
- Parameters:
- Returns:
the number of bytes that were decoded.
- Return type:
- get_byte_size() int
Return the length of the byte type in bytes.
- Returns:
the length of the byte type in bytes.
- Return type:
- get_size() int
Return the length of the byte type in bytes.
- Returns:
the length of the byte type in bytes.
- Return type:
- introspect(stop_at: ByteStruct | BitStruct | ByteType | BitType | None = None) str
Return the introspection representation of the object as a string.
- Parameters:
stop_at=None (object) – stop introspection at
stop_at
.
- root() ByteStruct | BitStruct
return root element
- set_automatic_calculation(enabled: bool = True) None
Enable or disable automatic calculation when
encode
is called.- Parameters:
enabled=True (bool) – the new enabled state.
- set_fuzzing_values(values: Generator[Any, None, None] | List[Any] | None) None
Set fuzzing values.
- Parameters:
values (iterable) – the values used for fuzzing.
- up() ByteStruct | BitStruct
Return the parent element.
Usage:
>>> from oser import ByteStruct, UBInt16, CRCL16, to_hex
>>> class Data(ByteStruct):
... def __init__(self):
... super(Data, self).__init__()
...
... self.a = ULInt16(1)
... self.b = ULInt16(2)
...
... self.crc = CRCL16(strict=True)
...
>>> instance = Data()
>>> print(instance)
Data():
a: 1 (ULInt16)
b: 2 (ULInt16)
crc: 0 (CRCL16)
>>> print(instance.introspect())
- - Data():
0 \x01 a: 1 (ULInt16)
1 \x00
2 \x02 b: 2 (ULInt16)
3 \x00
4 \x00 crc: 0 (CRCL16)
5 \x00
>>> binary = instance.encode()
>>> print(to_hex(binary))
0| 1| 2| 3| 4| 5
\x01\x00\x02\x00\xFC\xC3
>>> bytes_decoded = instance.decode(binary)
>>> print(bytes_decoded)
6
>>> print(instance)
Data():
a: 1 (ULInt16)
b: 2 (ULInt16)
crc: 50172 (CRCL16)
>>> print(instance.introspect())
- - Data():
0 \x01 a: 1 (ULInt16)
1 \x00
2 \x02 b: 2 (ULInt16)
3 \x00
4 \xfc crc: 50172 (CRCL16)
5 \xc3
>>> bytes_decoded = instance.decode(b"\x01\x00\x02\x00\xFC\xC3")
>>> print(bytes_decoded)
6
>>> print(instance)
Data():
a: 1 (ULInt16)
b: 2 (ULInt16)
crc: 50172 (CRCL16)
>>> print(instance.introspect())
- - Data():
0 \x01 a: 1 (ULInt16)
1 \x00
2 \x02 b: 2 (ULInt16)
3 \x00
4 \xfc crc: 50172 (CRCL16)
5 \xc3
>>> bytes_decoded = instance.decode(b"\x01\x00\x02\x00\x00\x00") # corrupted crc
oser.ChecksumMismatchException: Data could not be decoded!
Parsing object is:
- - Data():
0 \x01 a: 1 (ULInt16)
1 \x00
2 \x02 b: 2 (ULInt16)
3 \x00
4 \x00 crc: 0 (CRCL16)
5 \x00
^^^^ Checksum mismatch: expected \xc3fc but found \x0000
CRCB16
16-bit crc big endian.
- class oser.CRCB16(strict: bool = False, polynomial: int = 41003, reflect_input: bool = False, reflect_output: bool = False, initial_value: int = 0, xor_output: int = 0, automatic_calculation: bool = True, format: str = 'hex')
16-bit crc big endian serializer.
- Parameters:
strict – bool = False: If set to
True
crc is checked when decoding data. An exception is raised if crcs do not match.polynomial – int = 0xa02b: the crc polynomial
reflect_input – bool = False: reflect input data
reflect_output – bool = False: reflect output data
initial_value – int = 0x0000: start with initial_value
xor_output – int = 0x0000: xor output data with xor_output
automatic_calculation – bool = True: if set to
True
crc is automatically calculated when encoding dataformat – str = “hex” (string): Use “hex” to format values to hexadecimal. Use “bin” to format values to binary. Use
None
for decimal format.
- decode(data: bytes, full_data: bytes = b'', context_data: bytes = b'') int
Decode a binary string into a byte type and return the number of bytes that were decoded.
- Parameters:
- Returns:
the number of bytes that were decoded.
- Return type:
- get_byte_size() int
Return the length of the byte type in bytes.
- Returns:
the length of the byte type in bytes.
- Return type:
- get_size() int
Return the length of the byte type in bytes.
- Returns:
the length of the byte type in bytes.
- Return type:
- introspect(stop_at: ByteStruct | BitStruct | ByteType | BitType | None = None) str
Return the introspection representation of the object as a string.
- Parameters:
stop_at=None (object) – stop introspection at
stop_at
.
- root() ByteStruct | BitStruct
return root element
- set_automatic_calculation(enabled: bool = True) None
Enable or disable automatic calculation when
encode
is called.- Parameters:
enabled=True (bool) – the new enabled state.
- set_fuzzing_values(values: Generator[Any, None, None] | List[Any] | None) None
Set fuzzing values.
- Parameters:
values (iterable) – the values used for fuzzing.
- up() ByteStruct | BitStruct
Return the parent element.
Usage:
>>> from oser import ByteStruct, UBInt16, CRCB16, to_hex
>>> class Data(ByteStruct):
... def __init__(self):
... super(Data, self).__init__()
...
... self.a = UBInt16(1)
... self.b = UBInt16(2)
...
... self.crc = CRCB16(strict=True)
...
>>> instance = Data()
>>> print(instance)
Data():
a: 1 (UBInt16)
b: 2 (UBInt16)
crc: 0 (CRCB16)
>>> print(instance.introspect())
- - Data():
0 \x00 a: 1 (UBInt16)
1 \x01
2 \x00 b: 2 (UBInt16)
3 \x02
4 \x00 crc: 0 (CRCB16)
5 \x00
>>> binary = instance.encode()
>>> print(to_hex(binary))
0| 1| 2| 3| 4| 5
\x00\x01\x00\x02\xF8\x51
>>> bytes_decoded = instance.decode(binary)
>>> print(bytes_decoded)
6
>>> print(instance)
Data():
a: 1 (UBInt16)
b: 2 (UBInt16)
crc: 63569 (CRCB16)
>>> print(instance.introspect())
- - Data():
0 \x00 a: 1 (UBInt16)
1 \x01
2 \x00 b: 2 (UBInt16)
3 \x02
4 \xf8 crc: 63569 (CRCB16)
5 \x51
>>> bytes_decoded = instance.decode(b"\x00\x01\x00\x02\xF8\x51")
>>> print(bytes_decoded)
6
>>> print(instance)
Data():
a: 1 (UBInt16)
b: 2 (UBInt16)
crc: 63569 (CRCB16)
>>> print(instance.introspect())
- - Data():
0 \x00 a: 1 (UBInt16)
1 \x01
2 \x00 b: 2 (UBInt16)
3 \x02
4 \xf8 crc: 63569 (CRCB16)
5 \x51
>>> bytes_decoded = instance.decode(b"\x00\x01\x00\x02\x00\x00") # corrupted crc
oser.ChecksumMismatchException: Data could not be decoded!
Parsing object is:
- - Data():
0 \x00 a: 1 (UBInt16)
1 \x01
2 \x00 b: 2 (UBInt16)
3 \x02
4 \x00 crc: 0 (CRCB16)
5 \x00
^^^^ Checksum mismatch: expected \xf851 but found \x0000
CRCL32
32-bit crc little endian.
- class oser.CRCL32(strict: bool = False, polynomial: int = 517762881, reflect_input: bool = False, reflect_output: bool = False, initial_value: int = 0, xor_output: int = 0, automatic_calculation: bool = True, format: str = 'hex')
32-bit crc little endian serializer.
- Parameters:
strict – bool = False: If set to
True
crc is checked when decoding data. An exception is raised if crcs do not match.polynomial – int = 0x1edc6f41: the crc polynomial
reflect_input – bool = False: reflect input data
reflect_output – bool = False: reflect output data
initial_value – int = 0x00000000: start with initial_value
xor_output – int = 0x00000000: xor output data with xor_output
automatic_calculation – bool = True: if set to
True
crc is automatically calculated when encoding dataformat – str = “hex” (string): Use “hex” to format values to hexadecimal. Use “bin” to format values to binary. Use
None
for decimal format.
- decode(data: bytes, full_data: bytes = b'', context_data: bytes = b'') int
Decode a binary string into a byte type and return the number of bytes that were decoded.
- Parameters:
- Returns:
the number of bytes that were decoded.
- Return type:
- get_byte_size() int
Return the length of the byte type in bytes.
- Returns:
the length of the byte type in bytes.
- Return type:
- get_size() int
Return the length of the byte type in bytes.
- Returns:
the length of the byte type in bytes.
- Return type:
- introspect(stop_at: ByteStruct | BitStruct | ByteType | BitType | None = None) str
Return the introspection representation of the object as a string.
- Parameters:
stop_at=None (object) – stop introspection at
stop_at
.
- root() ByteStruct | BitStruct
return root element
- set_automatic_calculation(enabled: bool = True) None
Enable or disable automatic calculation when
encode
is called.- Parameters:
enabled=True (bool) – the new enabled state.
- set_fuzzing_values(values: Generator[Any, None, None] | List[Any] | None) None
Set fuzzing values.
- Parameters:
values (iterable) – the values used for fuzzing.
- up() ByteStruct | BitStruct
Return the parent element.
Usage:
>>> from oser import ByteStruct, UBInt16, CRCL32, to_hex
>>> class Data(ByteStruct):
... def __init__(self):
... super(Data, self).__init__()
...
... self.a = ULInt16(1)
... self.b = ULInt16(2)
...
... self.crc = CRCL32(strict=True)
...
>>> instance = Data()
>>> print(instance)
Data():
a: 1 (ULInt16)
b: 2 (ULInt16)
crc: 0 (CRCL32)
>>> print(instance.introspect())
- - Data():
0 \x01 a: 1 (ULInt16)
1 \x00
2 \x02 b: 2 (ULInt16)
3 \x00
4 \x00 crc: 0 (CRCL32)
5 \x00
6 \x00
7 \x00
>>> binary = instance.encode()
>>> print(to_hex(binary))
0| 1| 2| 3| 4| 5| 6| 7
\x01\x00\x02\x00\xB5\xED\xF0\xDC
>>> bytes_decoded = instance.decode(binary)
>>> print(bytes_decoded)
8
>>> print(instance)
Data():
a: 1 (ULInt16)
b: 2 (ULInt16)
crc: 3706777013 (CRCL32)
>>> print(instance.introspect())
- - Data():
0 \x01 a: 1 (ULInt16)
1 \x00
2 \x02 b: 2 (ULInt16)
3 \x00
4 \xb5 crc: 3706777013 (CRCL32)
5 \xed
6 \xf0
7 \xdc
>>> bytes_decoded = instance.decode(b"\x01\x00\x02\x00\xB5\xED\xF0\xDC")
>>> print(bytes_decoded)
8
>>> print(instance)
Data():
a: 1 (ULInt16)
b: 2 (ULInt16)
crc: 3706777013 (CRCL32)
>>> print(instance.introspect())
- - Data():
0 \x01 a: 1 (ULInt16)
1 \x00
2 \x02 b: 2 (ULInt16)
3 \x00
4 \xb5 crc: 3706777013 (CRCL32)
5 \xed
6 \xf0
7 \xdc
>>> bytes_decoded = instance.decode(b"\x01\x00\x02\x00\x00\x00\x00\x00") # corrupted crc
oser.ChecksumMismatchException: Data could not be decoded!
Parsing object is:
- - Data():
0 \x01 a: 1 (ULInt16)
1 \x00
2 \x02 b: 2 (ULInt16)
3 \x00
4 \x00 crc: 0 (CRCL32)
5 \x00
6 \x00
7 \x00
^^^^ Checksum mismatch: expected \xdcf0edb5 but found \x00000000
CRCB32
32-bit crc big endian.
- class oser.CRCB32(strict: bool = False, polynomial: int = 517762881, reflect_input: bool = False, reflect_output: bool = False, initial_value: int = 0, xor_output: int = 0, automatic_calculation: bool = True, format: str = 'hex')
32-bit crc big endian serializer.
- Parameters:
strict – bool = False: If set to
True
crc is checked when decoding data. An exception is raised if crcs do not match.polynomial – int = 0x1edc6f41: the crc polynomial
reflect_input – bool = False: reflect input data
reflect_output – bool = False: reflect output data
initial_value – int = 0x00000000: start with initial_value
xor_output – int = 0x00000000: xor output data with xor_output
automatic_calculation – bool = True: if set to
True
crc is automatically calculated when encoding dataformat – str = “hex” (string): Use “hex” to format values to hexadecimal. Use “bin” to format values to binary. Use
None
for decimal format.
- decode(data: bytes, full_data: bytes = b'', context_data: bytes = b'') int
Decode a binary string into a byte type and return the number of bytes that were decoded.
- Parameters:
- Returns:
the number of bytes that were decoded.
- Return type:
- get_byte_size() int
Return the length of the byte type in bytes.
- Returns:
the length of the byte type in bytes.
- Return type:
- get_size() int
Return the length of the byte type in bytes.
- Returns:
the length of the byte type in bytes.
- Return type:
- introspect(stop_at: ByteStruct | BitStruct | ByteType | BitType | None = None) str
Return the introspection representation of the object as a string.
- Parameters:
stop_at=None (object) – stop introspection at
stop_at
.
- root() ByteStruct | BitStruct
return root element
- set_automatic_calculation(enabled: bool = True) None
Enable or disable automatic calculation when
encode
is called.- Parameters:
enabled=True (bool) – the new enabled state.
- set_fuzzing_values(values: Generator[Any, None, None] | List[Any] | None) None
Set fuzzing values.
- Parameters:
values (iterable) – the values used for fuzzing.
- up() ByteStruct | BitStruct
Return the parent element.
Usage:
>>> from oser import ByteStruct, UBInt16, CRCB32, to_hex
>>> class Data(ByteStruct):
... def __init__(self):
... super(Data, self).__init__()
...
... self.a = UBInt16(1)
... self.b = UBInt16(2)
...
... self.crc = CRCB32(strict=True)
...
>>> instance = Data()
>>> print(instance)
Data():
a: 1 (UBInt16)
b: 2 (UBInt16)
crc: 0 (CRCB32)
>>> print(instance.introspect())
- - Data():
0 \x00 a: 1 (UBInt16)
1 \x01
2 \x00 b: 2 (UBInt16)
3 \x02
4 \x00 crc: 0 (CRCB32)
5 \x00
6 \x00
7 \x00
>>> binary = instance.encode()
>>> print(to_hex(binary))
0| 1| 2| 3| 4| 5| 6| 7
\x00\x01\x00\x02\xFF\x56\x3A\x53
>>> bytes_decoded = instance.decode(binary)
>>> print(bytes_decoded)
8
>>> print(instance)
Data():
a: 1 (UBInt16)
b: 2 (UBInt16)
crc: 4283841107 (CRCB32)
>>> print(instance.introspect())
- - Data():
0 \x00 a: 1 (UBInt16)
1 \x01
2 \x00 b: 2 (UBInt16)
3 \x02
4 \xff crc: 4283841107 (CRCB32)
5 \x56
6 \x3a
7 \x53
>>> bytes_decoded = instance.decode(b"\x00\x01\x00\x02\xFF\x56\x3A\x53")
>>> print(bytes_decoded)
8
>>> print(instance)
Data():
a: 1 (UBInt16)
b: 2 (UBInt16)
crc: 4283841107 (CRCB32)
>>> print(instance.introspect())
- - Data():
0 \x00 a: 1 (UBInt16)
1 \x01
2 \x00 b: 2 (UBInt16)
3 \x02
4 \xff crc: 4283841107 (CRCB32)
5 \x56
6 \x3a
7 \x53
>>> bytes_decoded = instance.decode(b"\x00\x01\x00\x02\x00\x00\x00\x00") # corrupted crc
oser.ChecksumMismatchException: Data could not be decoded!
Parsing object is:
- - Data():
0 \x00 a: 1 (UBInt16)
1 \x01
2 \x00 b: 2 (UBInt16)
3 \x02
4 \x00 crc: 0 (CRCB32)
5 \x00
6 \x00
7 \x00
^^^^ Checksum mismatch: expected \xff563a53 but found \x00000000
CRCL64
64-bit crc little endian.
- class oser.CRCL64(strict: bool = False, polynomial: int = 4823603603198064275, reflect_input: bool = False, reflect_output: bool = False, initial_value: int = 0, xor_output: int = 0, automatic_calculation: bool = True, format: str = 'hex')
64-bit crc little endian serializer.
- Parameters:
strict – bool = False: If set to
True
crc is checked when decoding data. An exception is raised if crcs do not match.polynomial – int = 0x42f0e1eba9ea3693: the crc polynomial
reflect_input – bool = False: reflect input data
reflect_output – bool = False: reflect output data
initial_value – int = 0x0000000000000000: start with initial_value
xor_output – int = 0x0000000000000000: xor output data with xor_output
automatic_calculation – bool = True: if set to
True
crc is automatically calculated when encoding dataformat – str = “hex” (string): Use “hex” to format values to hexadecimal. Use “bin” to format values to binary. Use
None
for decimal format.
- decode(data: bytes, full_data: bytes = b'', context_data: bytes = b'') int
Decode a binary string into a byte type and return the number of bytes that were decoded.
- Parameters:
- Returns:
the number of bytes that were decoded.
- Return type:
- get_byte_size() int
Return the length of the byte type in bytes.
- Returns:
the length of the byte type in bytes.
- Return type:
- get_size() int
Return the length of the byte type in bytes.
- Returns:
the length of the byte type in bytes.
- Return type:
- introspect(stop_at: ByteStruct | BitStruct | ByteType | BitType | None = None) str
Return the introspection representation of the object as a string.
- Parameters:
stop_at=None (object) – stop introspection at
stop_at
.
- root() ByteStruct | BitStruct
return root element
- set_automatic_calculation(enabled: bool = True) None
Enable or disable automatic calculation when
encode
is called.- Parameters:
enabled=True (bool) – the new enabled state.
- set_fuzzing_values(values: Generator[Any, None, None] | List[Any] | None) None
Set fuzzing values.
- Parameters:
values (iterable) – the values used for fuzzing.
- up() ByteStruct | BitStruct
Return the parent element.
Usage:
>>> from oser import ByteStruct, UBInt16, CRCL64, to_hex
>>> class Data(ByteStruct):
... def __init__(self):
... super(Data, self).__init__()
...
... self.a = ULInt16(1)
... self.b = ULInt16(2)
...
... self.crc = CRCL64(strict=True)
...
>>> instance = Data()
>>> print(instance)
Data():
a: 1 (ULInt16)
b: 2 (ULInt16)
crc: 0 (CRCL64)
>>> print(instance.introspect())
- - Data():
0 \x01 a: 1 (ULInt16)
1 \x00
2 \x02 b: 2 (ULInt16)
3 \x00
4 \x00 crc: 0 (CRCL64)
5 \x00
6 \x00
7 \x00
8 \x00
9 \x00
10 \x00
11 \x00
>>> binary = instance.encode()
>>> print(to_hex(binary))
0| 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| 11
\x01\x00\x02\x00\xEA\x1F\x12\x02\xC4\xF9\x66\xF9
>>> bytes_decoded = instance.decode(binary)
>>> print(bytes_decoded)
12
>>> print(instance)
Data():
a: 1 (ULInt16)
b: 2 (ULInt16)
crc: 17971325983312191466 (CRCL64)
>>> print(instance.introspect())
- - Data():
0 \x01 a: 1 (ULInt16)
1 \x00
2 \x02 b: 2 (ULInt16)
3 \x00
4 \xea crc: 17971325983312191466 (CRCL64)
5 \x1f
6 \x12
7 \x02
8 \xc4
9 \xf9
10 \x66
11 \xf9
>>> bytes_decoded = instance.decode(b"\x01\x00\x02\x00\xEA\x1F\x12\x02\xC4\xF9\x66\xF9")
>>> print(bytes_decoded)
12
>>> print(instance)
Data():
a: 1 (ULInt16)
b: 2 (ULInt16)
crc: 17971325983312191466 (CRCL64)
>>> print(instance.introspect())
- - Data():
0 \x01 a: 1 (ULInt16)
1 \x00
2 \x02 b: 2 (ULInt16)
3 \x00
4 \xea crc: 17971325983312191466 (CRCL64)
5 \x1f
6 \x12
7 \x02
8 \xc4
9 \xf9
10 \x66
11 \xf9
>>> bytes_decoded = instance.decode(b"\x01\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00") # corrupted crc
oser.ChecksumMismatchException: Data could not be decoded!
Parsing object is:
- - Data():
0 \x01 a: 1 (ULInt16)
1 \x00
2 \x02 b: 2 (ULInt16)
3 \x00
4 \x00 crc: 0 (CRCL64)
5 \x00
6 \x00
7 \x00
8 \x00
9 \x00
10 \x00
11 \x00
^^^^ Checksum mismatch: expected \xf966f9c402121fea but found \x0000000000000000
CRCB64
64-bit crc big endian.
- class oser.CRCB64(strict: bool = False, polynomial: int = 4823603603198064275, reflect_input: bool = False, reflect_output: bool = False, initial_value: int = 0, xor_output: int = 0, automatic_calculation: bool = True, format: str = 'hex')
64-bit crc big endian serializer.
- Parameters:
strict – bool = False: If set to
True
crc is checked when decoding data. An exception is raised if crcs do not match.polynomial – int = 0x42f0e1eba9ea3693: the crc polynomial
reflect_input – bool = False: reflect input data
reflect_output – bool = False: reflect output data
initial_value – int = 0x0000000000000000: start with initial_value
xor_output – int = 0x0000000000000000: xor output data with xor_output
automatic_calculation – bool = True: if set to
True
crc is automatically calculated when encoding dataformat – str = “hex” (string): Use “hex” to format values to hexadecimal. Use “bin” to format values to binary. Use
None
for decimal format.
- decode(data: bytes, full_data: bytes = b'', context_data: bytes = b'') int
Decode a binary string into a byte type and return the number of bytes that were decoded.
- Parameters:
- Returns:
the number of bytes that were decoded.
- Return type:
- get_byte_size() int
Return the length of the byte type in bytes.
- Returns:
the length of the byte type in bytes.
- Return type:
- get_size() int
Return the length of the byte type in bytes.
- Returns:
the length of the byte type in bytes.
- Return type:
- introspect(stop_at: ByteStruct | BitStruct | ByteType | BitType | None = None) str
Return the introspection representation of the object as a string.
- Parameters:
stop_at=None (object) – stop introspection at
stop_at
.
- root() ByteStruct | BitStruct
return root element
- set_automatic_calculation(enabled: bool = True) None
Enable or disable automatic calculation when
encode
is called.- Parameters:
enabled=True (bool) – the new enabled state.
- set_fuzzing_values(values: Generator[Any, None, None] | List[Any] | None) None
Set fuzzing values.
- Parameters:
values (iterable) – the values used for fuzzing.
- up() ByteStruct | BitStruct
Return the parent element.
Usage:
>>> from oser import ByteStruct, UBInt16, CRCB64, to_hex
>>> class Data(ByteStruct):
... def __init__(self):
... super(Data, self).__init__()
...
... self.a = UBInt16(1)
... self.b = UBInt16(2)
...
... self.crc = CRCB64(strict=True)
...
>>> instance = Data()
>>> print(instance)
Data():
a: 1 (UBInt16)
b: 2 (UBInt16)
crc: 0 (CRCB64)
>>> print(instance.introspect())
- - Data():
0 \x00 a: 1 (UBInt16)
1 \x01
2 \x00 b: 2 (UBInt16)
3 \x02
4 \x00 crc: 0 (CRCB64)
5 \x00
6 \x00
7 \x00
8 \x00
9 \x00
10 \x00
11 \x00
>>> binary = instance.encode()
>>> print(to_hex(binary))
0| 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| 11
\x00\x01\x00\x02\xA6\x0F\x34\x48\x69\x03\x75\xE1
>>> bytes_decoded = instance.decode(binary)
>>> print(bytes_decoded)
12
>>> print(instance)
Data():
a: 1 (UBInt16)
b: 2 (UBInt16)
crc: 11965840220550821345 (CRCB64)
>>> print(instance.introspect())
- - Data():
0 \x00 a: 1 (UBInt16)
1 \x01
2 \x00 b: 2 (UBInt16)
3 \x02
4 \xa6 crc: 11965840220550821345 (CRCB64)
5 \x0f
6 \x34
7 \x48
8 \x69
9 \x03
10 \x75
11 \xe1
>>> bytes_decoded = instance.decode(b"\x00\x01\x00\x02\xA6\x0F\x34\x48\x69\x03\x75\xE1")
>>> print(bytes_decoded)
12
>>> print(instance)
Data():
a: 1 (UBInt16)
b: 2 (UBInt16)
crc: 11965840220550821345 (CRCB64)
>>> print(instance.introspect())
- - Data():
0 \x00 a: 1 (UBInt16)
1 \x01
2 \x00 b: 2 (UBInt16)
3 \x02
4 \xa6 crc: 11965840220550821345 (CRCB64)
5 \x0f
6 \x34
7 \x48
8 \x69
9 \x03
10 \x75
11 \xe1
>>> bytes_decoded = instance.decode(b"\x00\x01\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00") # corrupted crc
oser.ChecksumMismatchException: Data could not be decoded!
Parsing object is:
- - Data():
0 \x00 a: 1 (UBInt16)
1 \x01
2 \x00 b: 2 (UBInt16)
3 \x02
4 \x00 crc: 0 (CRCB64)
5 \x00
6 \x00
7 \x00
8 \x00
9 \x00
10 \x00
11 \x00
^^^^ Checksum mismatch: expected \xa60f3448690375e1 but found \x0000000000000000