embutils.utils.cobs module

COBS encoding/decoding implementation.

date

2021

author

Christian Wiche

contact

cwichel@gmail.com

license

The MIT License (MIT)

class embutils.utils.cobs.COBS[source]

Bases: object

Consistent Overhead Byte Stuffing (COBS) encoding/decoding utilities.

class Block(code, data, zero=False)[source]

Bases: object

COBS encoding block.

Method generated by attrs for class COBS.Block.

code: int

Block code

data: bytearray

Block data array

zero: bool

Flag. Defines if is required to add a zero at the end.

exception DecodeException[source]

Bases: Exception

COBS decoding exception.

static decode(data: bytearray) bytearray[source]

Decodes a byte array that was encoded using Consistent Overhead Byte Stuffing (COBS).

Parameters

data (bytearray) – Bytes to be decoded.

Returns

Decoded byte array.

Return type

bytearray

Raises

COBS.DecodeException – Encoded data is invalid.

static encode(data: bytearray = bytearray(b'')) bytearray[source]

Encode a byte array using Consistent Overhead Byte Stuffing (COBS).

Note

  • Encoding guarantees non-zero bytes on the output array (besides the terminator).

  • An empty string is encoded to [0x01, 0x00]

Parameters

data (bytearray) – Bytes to be encoded.

Returns

Encoded byte array.

Return type

bytearray