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=False, polynomial=213, reflect_input=False, reflect_output=False, initial_value=0, xor_output=0, automatic_calculation=True)

8-bit crc little endian serializer.

Parameters
  • strict=False – If set to True crc is checked when decoding data. An exception is raised if crcs do not match.

  • polynomial=0xd5 – the crc polynomial

  • reflect_input=False – reflect input data

  • reflect_output=False – reflect output data

  • initial_value=0x00 – start with initial_value

  • xor_output=0x00 – xor output data with xor_output

  • automatic_calculation=True – if set to True crc is automatically calculated when encoding data

byte_size()

Return the length of the byte type in bytes.

Returns

the length of the byte type in bytes.

Return type

int

decode(data, full_data=b'', context_data=b'')

Decode a binary string into a byte type and return the number of bytes that were decoded.

Parameters
  • data (bytes) – the data buffer that is decoded.

  • full_data (bytes) – the binary data string until the part to be decoded. The user normally does not need to supply this.

  • context_data (bytes) – the binary data of the current context. The user normally does not need to supply this.

Returns

the number of bytes that were decoded.

Return type

int

encode(full_data=b'', context_data=b'')

Return the encoded byte type as a binary string.

Parameters
  • full_data (bytes) – the binary data string until the part to be encoded. The user normally does not need to supply this.

  • context_data (bytes) – the binary data of the current context. The user normally does not need to supply this.

get()

Return the value.

Returns

the value.

introspect(stop_at=None)

Return the introspection representation of the object as a string.

root()

return root element

set(value)

Set the value.

Parameters

value – the new value

set_automatic_calculation(enabled=True)

Enable or disable automatic calculation when encode is called.

Parameters

enabled=True (bool) – the new enabled state.

set_fuzzing_values(values)

Set fuzzing values.

Parameters

values (iterable) – the values used for fuzzing.

size()

Return the length of the byte type in bytes.

Returns

the length of the byte type in bytes.

Return type

int

up()

Return the parent element.

Usage:

>>> from oser import ByteStruct
>>> from oser import ULInt16
>>> from oser import CRCL8
>>> from oser import 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
>>> bytesDecoded = instance.decode(binary)
>>> print(bytesDecoded)
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)

>>>
>>> bytesDecoded = instance.decode("\x01\x00\x02\x00\x53")
>>> print(bytesDecoded)
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)

>>>
>>> bytesDecoded = instance.decode("\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=False, polynomial=213, reflect_input=False, reflect_output=False, initial_value=0, xor_output=0, automatic_calculation=True)

8-bit crc big endian serializer.

Parameters
  • strict=False – If set to True crc is checked when decoding data. An exception is raised if crcs do not match.

  • polynomial=0xd5 – the crc polynomial

  • reflect_input=False – reflect input data

  • reflect_output=False – reflect output data

  • initial_value=0x00 – start with initial_value

  • xor_output=0x00 – xor output data with xor_output

  • automatic_calculation=True – if set to True crc is automatically calculated when encoding data

byte_size()

Return the length of the byte type in bytes.

Returns

the length of the byte type in bytes.

Return type

int

decode(data, full_data=b'', context_data=b'')

Decode a binary string into a byte type and return the number of bytes that were decoded.

Parameters
  • data (bytes) – the data buffer that is decoded.

  • full_data (bytes) – the binary data string until the part to be decoded. The user normally does not need to supply this.

  • context_data (bytes) – the binary data of the current context. The user normally does not need to supply this.

encode(full_data=b'', context_data=b'')

Return the encoded binary string.

Parameters
  • full_data (bytes) – the binary data string until the part to be encoded. The user normally does not need to supply this.

  • context_data (bytes) – the binary data of the current context. The user normally does not need to supply this.

Returns

the encoded binary string.

Return type

bytes

get()

Return the value.

Returns

the value.

introspect(stop_at=None)

Return the introspection representation of the object as a string.

root()

return root element

set(value)

Set the value.

Parameters

value – the new value

set_automatic_calculation(enabled=True)

Enable or disable automatic calculation when encode is called.

Parameters

enabled=True (bool) – the new enabled state.

set_fuzzing_values(values)

Set fuzzing values.

Parameters

values (iterable) – the values used for fuzzing.

size()

Return the length of the byte type in bytes.

Returns

the length of the byte type in bytes.

Return type

int

up()

Return the parent element.

Usage:

>>> from oser import ByteStruct
>>> from oser import UBInt16
>>> from oser import CRCB8
>>> from oser import 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
>>> bytesDecoded = instance.decode(binary)
>>> print(bytesDecoded)
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)

>>>
>>> bytesDecoded = instance.decode("\x00\x01\x00\x02\xFC")
>>> print(bytesDecoded)
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)

>>>
>>> bytesDecoded = instance.decode("\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=False, polynomial=41003, reflect_input=False, reflect_output=False, initial_value=0, xor_output=0, automatic_calculation=True)

16-bit crc little endian serializer.

Parameters
  • strict=False – If set to True crc is checked when decoding data. An exception is raised if crcs do not match.

  • polynomial=0xa02b – the crc polynomial

  • reflect_input=False – reflect input data

  • reflect_output=False – reflect output data

  • initial_value=0x0000 – start with initial_value

  • xor_output=0x0000 – xor output data with xor_output

  • automatic_calculation=True – if set to True crc is automatically calculated when encoding data

byte_size()

Return the length of the byte type in bytes.

Returns

the length of the byte type in bytes.

Return type

int

decode(data, full_data=b'', context_data=b'')

Decode a binary string into a byte type and return the number of bytes that were decoded.

Parameters
  • data (bytes) – the data buffer that is decoded.

  • full_data (bytes) – the binary data string until the part to be decoded. The user normally does not need to supply this.

  • context_data (bytes) – the binary data of the current context. The user normally does not need to supply this.

Returns

the number of bytes that were decoded.

Return type

int

encode(full_data=b'', context_data=b'')

Return the encoded binary string.

Parameters
  • full_data (bytes) – the binary data string until the part to be encoded. The user normally does not need to supply this.

  • context_data (bytes) – the binary data of the current context. The user normally does not need to supply this.

Returns

the encoded binary string.

Return type

bytes

get()

Return the value.

Returns

the value.

introspect(stop_at=None)

Return the introspection representation of the object as a string.

root()

return root element

set(value)

Set the value.

Parameters

value – the new value

set_automatic_calculation(enabled=True)

Enable or disable automatic calculation when encode is called.

Parameters

enabled=True (bool) – the new enabled state.

set_fuzzing_values(values)

Set fuzzing values.

Parameters

values (iterable) – the values used for fuzzing.

size()

Return the length of the byte type in bytes.

Returns

the length of the byte type in bytes.

Return type

int

up()

Return the parent element.

Usage:

>>> from oser import ByteStruct
>>> from oser import ULInt16
>>> from oser import CRCL16
>>> from oser import 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
>>> bytesDecoded = instance.decode(binary)
>>> print(bytesDecoded)
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

>>>
>>> bytesDecoded = instance.decode("\x01\x00\x02\x00\xFC\xC3")
>>> print(bytesDecoded)
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

>>>
>>> bytesDecoded = instance.decode("\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=False, polynomial=41003, reflect_input=False, reflect_output=False, initial_value=0, xor_output=0, automatic_calculation=True)

16-bit crc big endian serializer.

Parameters
  • strict=False – If set to True crc is checked when decoding data. An exception is raised if crcs do not match.

  • polynomial=0xa02b – the crc polynomial

  • reflect_input=False – reflect input data

  • reflect_output=False – reflect output data

  • initial_value=0x0000 – start with initial_value

  • xor_output=0x0000 – xor output data with xor_output

  • automatic_calculation=True – if set to True crc is automatically calculated when encoding data

byte_size()

Return the length of the byte type in bytes.

Returns

the length of the byte type in bytes.

Return type

int

decode(data, full_data=b'', context_data=b'')

Decode a binary string into a byte type and return the number of bytes that were decoded.

Parameters
  • data (bytes) – the data buffer that is decoded.

  • full_data (bytes) – the binary data string until the part to be decoded. The user normally does not need to supply this.

  • context_data (bytes) – the binary data of the current context. The user normally does not need to supply this.

Returns

the number of bytes that were decoded.

Return type

int

encode(full_data=b'', context_data=b'')

Return the encoded binary string.

Parameters
  • full_data (bytes) – the binary data string until the part to be encoded. The user normally does not need to supply this.

  • context_data (bytes) – the binary data of the current context. The user normally does not need to supply this.

Returns

the encoded binary string.

Return type

bytes

get()

Return the value.

Returns

the value.

introspect(stop_at=None)

Return the introspection representation of the object as a string.

root()

return root element

set(value)

Set the value.

Parameters

value – the new value

set_automatic_calculation(enabled=True)

Enable or disable automatic calculation when encode is called.

Parameters

enabled=True (bool) – the new enabled state.

set_fuzzing_values(values)

Set fuzzing values.

Parameters

values (iterable) – the values used for fuzzing.

size()

Return the length of the byte type in bytes.

Returns

the length of the byte type in bytes.

Return type

int

up()

Return the parent element.

Usage:

>>> from oser import ByteStruct
>>> from oser import UBInt16
>>> from oser import CRCB16
>>> from oser import 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
>>> bytesDecoded = instance.decode(binary)
>>> print(bytesDecoded)
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

>>>
>>> bytesDecoded = instance.decode("\x00\x01\x00\x02\xF8\x51")
>>> print(bytesDecoded)
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

>>>
>>> bytesDecoded = instance.decode("\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=False, polynomial=517762881, reflect_input=False, reflect_output=False, initial_value=0, xor_output=0, automatic_calculation=True)

32-bit crc little endian serializer.

Parameters
  • strict=False – If set to True crc is checked when decoding data. An exception is raised if crcs do not match.

  • polynomial=0x1edc6f41 – the crc polynomial

  • reflect_input=False – reflect input data

  • reflect_output=False – reflect output data

  • initial_value=0x00000000 – start with initial_value

  • xor_output=0x00000000 – xor output data with xor_output

  • automatic_calculation=True – if set to True crc is automatically calculated when encoding data

byte_size()

Return the length of the byte type in bytes.

Returns

the length of the byte type in bytes.

Return type

int

decode(data, full_data=b'', context_data=b'')

Decode a binary string into a byte type and return the number of bytes that were decoded.

Parameters
  • data (bytes) – the data buffer that is decoded.

  • full_data (bytes) – the binary data string until the part to be decoded. The user normally does not need to supply this.

  • context_data (bytes) – the binary data of the current context. The user normally does not need to supply this.

Returns

the number of bytes that were decoded.

Return type

int

encode(full_data=b'', context_data=b'')

Return the encoded binary string.

Parameters
  • full_data (bytes) – the binary data string until the part to be encoded. The user normally does not need to supply this.

  • context_data (bytes) – the binary data of the current context. The user normally does not need to supply this.

Returns

the encoded binary string.

Return type

bytes

get()

Return the value.

Returns

the value.

introspect(stop_at=None)

Return the introspection representation of the object as a string.

root()

return root element

set(value)

Set the value.

Parameters

value – the new value

set_automatic_calculation(enabled=True)

Enable or disable automatic calculation when encode is called.

Parameters

enabled=True (bool) – the new enabled state.

set_fuzzing_values(values)

Set fuzzing values.

Parameters

values (iterable) – the values used for fuzzing.

size()

Return the length of the byte type in bytes.

Returns

the length of the byte type in bytes.

Return type

int

up()

Return the parent element.

Usage:

>>> from oser import ByteStruct
>>> from oser import ULInt16
>>> from oser import CRCL32
>>> from oser import 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
>>> bytesDecoded = instance.decode(binary)
>>> print(bytesDecoded)
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

>>>
>>> bytesDecoded = instance.decode("\x01\x00\x02\x00\xB5\xED\xF0\xDC")
>>> print(bytesDecoded)
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

>>>
>>> bytesDecoded = instance.decode("\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=False, polynomial=517762881, reflect_input=False, reflect_output=False, initial_value=0, xor_output=0, automatic_calculation=True)

32-bit crc big endian serializer.

Parameters
  • strict=False – If set to True crc is checked when decoding data. An exception is raised if crcs do not match.

  • polynomial=0x1edc6f41 – the crc polynomial

  • reflect_input=False – reflect input data

  • reflect_output=False – reflect output data

  • initial_value=0x00000000 – start with initial_value

  • xor_output=0x00000000 – xor output data with xor_output

  • automatic_calculation=True – if set to True crc is automatically calculated when encoding data

byte_size()

Return the length of the byte type in bytes.

Returns

the length of the byte type in bytes.

Return type

int

decode(data, full_data=b'', context_data=b'')

Decode a binary string into a byte type and return the number of bytes that were decoded.

Parameters
  • data (bytes) – the data buffer that is decoded.

  • full_data (bytes) – the binary data string until the part to be decoded. The user normally does not need to supply this.

  • context_data (bytes) – the binary data of the current context. The user normally does not need to supply this.

Returns

the number of bytes that were decoded.

Return type

int

encode(full_data=b'', context_data=b'')

Return the encoded binary string.

Parameters
  • full_data (bytes) – the binary data string until the part to be encoded. The user normally does not need to supply this.

  • context_data (bytes) – the binary data of the current context. The user normally does not need to supply this.

Returns

the encoded binary string.

Return type

bytes

get()

Return the value.

Returns

the value.

introspect(stop_at=None)

Return the introspection representation of the object as a string.

root()

return root element

set(value)

Set the value.

Parameters

value – the new value

set_automatic_calculation(enabled=True)

Enable or disable automatic calculation when encode is called.

Parameters

enabled=True (bool) – the new enabled state.

set_fuzzing_values(values)

Set fuzzing values.

Parameters

values (iterable) – the values used for fuzzing.

size()

Return the length of the byte type in bytes.

Returns

the length of the byte type in bytes.

Return type

int

up()

Return the parent element.

Usage:

>>> from oser import ByteStruct
>>> from oser import UBInt16
>>> from oser import CRCB32
>>> from oser import 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
>>> bytesDecoded = instance.decode(binary)
>>> print(bytesDecoded)
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

>>>
>>> bytesDecoded = instance.decode("\x00\x01\x00\x02\xFF\x56\x3A\x53")
>>> print(bytesDecoded)
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

>>>
>>> bytesDecoded = instance.decode("\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=False, polynomial=4823603603198064275, reflect_input=False, reflect_output=False, initial_value=0, xor_output=0, automatic_calculation=True)

64-bit crc little endian serializer.

Parameters
  • strict=False – If set to True crc is checked when decoding data. An exception is raised if crcs do not match.

  • polynomial=0x42f0e1eba9ea3693 – the crc polynomial

  • reflect_input=False – reflect input data

  • reflect_output=False – reflect output data

  • initial_value=0x0000000000000000 – start with initial_value

  • xor_output=0x0000000000000000 – xor output data with xor_output

  • automatic_calculation=True – if set to True crc is automatically calculated when encoding data

byte_size()

Return the length of the byte type in bytes.

Returns

the length of the byte type in bytes.

Return type

int

decode(data, full_data=b'', context_data=b'')

Decode a binary string into a byte type and return the number of bytes that were decoded.

Parameters
  • data (bytes) – the data buffer that is decoded.

  • full_data (bytes) – the binary data string until the part to be decoded. The user normally does not need to supply this.

  • context_data (bytes) – the binary data of the current context. The user normally does not need to supply this.

Returns

the number of bytes that were decoded.

Return type

int

encode(full_data=b'', context_data=b'')

Return the encoded binary string.

Parameters
  • full_data (bytes) – the binary data string until the part to be encoded. The user normally does not need to supply this.

  • context_data (bytes) – the binary data of the current context. The user normally does not need to supply this.

Returns

the encoded binary string.

Return type

bytes

get()

Return the value.

Returns

the value.

introspect(stop_at=None)

Return the introspection representation of the object as a string.

root()

return root element

set(value)

Set the value.

Parameters

value – the new value

set_automatic_calculation(enabled=True)

Enable or disable automatic calculation when encode is called.

Parameters

enabled=True (bool) – the new enabled state.

set_fuzzing_values(values)

Set fuzzing values.

Parameters

values (iterable) – the values used for fuzzing.

size()

Return the length of the byte type in bytes.

Returns

the length of the byte type in bytes.

Return type

int

up()

Return the parent element.

Usage:

>>> from oser import ByteStruct
>>> from oser import ULInt16
>>> from oser import CRCL64
>>> from oser import 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
>>> bytesDecoded = instance.decode(binary)
>>> print(bytesDecoded)
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

>>>
>>> bytesDecoded = instance.decode("\x01\x00\x02\x00\xEA\x1F\x12\x02\xC4\xF9\x66\xF9")
>>> print(bytesDecoded)
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

>>>
>>> bytesDecoded = instance.decode("\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=False, polynomial=4823603603198064275, reflect_input=False, reflect_output=False, initial_value=0, xor_output=0, automatic_calculation=True)

64-bit crc big endian serializer.

Parameters
  • strict=False – If set to True crc is checked when decoding data. An exception is raised if crcs do not match.

  • polynomial=0x42f0e1eba9ea3693 – the crc polynomial

  • reflect_input=False – reflect input data

  • reflect_output=False – reflect output data

  • initial_value=0x0000000000000000 – start with initial_value

  • xor_output=0x0000000000000000 – xor output data with xor_output

  • automatic_calculation=True – if set to True crc is automatically calculated when encoding data

byte_size()

Return the length of the byte type in bytes.

Returns

the length of the byte type in bytes.

Return type

int

decode(data, full_data=b'', context_data=b'')

Decode a binary string into a byte type and return the number of bytes that were decoded.

Parameters
  • data (bytes) – the data buffer that is decoded.

  • full_data (bytes) – the binary data string until the part to be decoded. The user normally does not need to supply this.

  • context_data (bytes) – the binary data of the current context. The user normally does not need to supply this.

Returns

the number of bytes that were decoded.

Return type

int

encode(full_data=b'', context_data=b'')

Return the encoded binary string.

Parameters
  • full_data (bytes) – the binary data string until the part to be encoded. The user normally does not need to supply this.

  • context_data (bytes) – the binary data of the current context. The user normally does not need to supply this.

Returns

the encoded binary string.

Return type

bytes

get()

Return the value.

Returns

the value.

introspect(stop_at=None)

Return the introspection representation of the object as a string.

root()

return root element

set(value)

Set the value.

Parameters

value – the new value

set_automatic_calculation(enabled=True)

Enable or disable automatic calculation when encode is called.

Parameters

enabled=True (bool) – the new enabled state.

set_fuzzing_values(values)

Set fuzzing values.

Parameters

values (iterable) – the values used for fuzzing.

size()

Return the length of the byte type in bytes.

Returns

the length of the byte type in bytes.

Return type

int

up()

Return the parent element.

Usage:

>>> from oser import ByteStruct
>>> from oser import UBInt16
>>> from oser import CRCB64
>>> from oser import 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
>>> bytesDecoded = instance.decode(binary)
>>> print(bytesDecoded)
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

>>>
>>> bytesDecoded = instance.decode("\x00\x01\x00\x02\xA6\x0F\x34\x48\x69\x03\x75\xE1")
>>> print(bytesDecoded)
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

>>>
>>> bytesDecoded = instance.decode("\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