Skip to content
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

feat(): add some configurations #178

Merged
merged 8 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/docs/api/crc16.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

::: crc.Crc16
options:
members: ["CCITT", "GSM", "PROFIBUS"]
members: ["GSM", "PROFIBUS", "IBM","MAXIM","USB","X25","DNP"]
2 changes: 1 addition & 1 deletion docs/docs/api/crc8.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

::: crc.Crc8
options:
members: ["CCITT", "AUTOSAR", "BLUETOOTH", "SAEJ1850", "MAXIM_DOW"]
members: ["CCITT", "AUTOSAR", "BLUETOOTH", "SAEJ1850", "MAXIM_DOW","ITU","ROHC"]
13 changes: 13 additions & 0 deletions docs/docs/changelog/unreleased.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
# Unreleased

## ✨ Features

* Add support for the following Crc8 configurations:
- ITU
- ROHC

* Add support for the following Crc16 configurations:
- IBM
- MAXIM
- USB
- X25 (X-25)
- DNP

## 🔩 Internal
* Re-lock dependencies
1 change: 1 addition & 0 deletions docs/docs/contributors.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Thank you to all contributors for your help in improving this project. 🚀
* Gert van Dijk
* Riccardo Malavolti
* Ramya-Ab
* Allen-saltedfish
* Dependabot


63 changes: 63 additions & 0 deletions src/crc/_crc.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,24 @@ class Crc8(enum.Enum):
reverse_output=True,
)

ITU = Configuration(
width=8,
polynomial=0x07,
init_value=0x00,
final_xor_value=0x55,
reverse_input=False,
reverse_output=False,
)

ROHC = Configuration(
width=8,
polynomial=0x07,
init_value=0xFF,
final_xor_value=0x00,
reverse_input=True,
reverse_output=True,
)


@enum.unique
class Crc16(enum.Enum):
Expand Down Expand Up @@ -535,6 +553,51 @@ class Crc16(enum.Enum):
reverse_output=True,
)

IBM = Configuration(
width=16,
polynomial=0x8005,
init_value=0x0000,
final_xor_value=0x0000,
reverse_input=True,
reverse_output=True,
)

MAXIM = Configuration(
width=16,
polynomial=0x8005,
init_value=0x0000,
final_xor_value=0xFFFF,
reverse_input=True,
reverse_output=True,
)

USB = Configuration(
width=16,
polynomial=0x8005,
init_value=0xFFFF,
final_xor_value=0xFFFF,
reverse_input=True,
reverse_output=True,
)

X25 = Configuration(
width=16,
polynomial=0x1021,
init_value=0xFFFF,
final_xor_value=0xFFFF,
reverse_input=True,
reverse_output=True,
)

DNP = Configuration(
width=16,
polynomial=0x3D65,
init_value=0x0000,
final_xor_value=0xFFFF,
reverse_input=True,
reverse_output=True,
)


@enum.unique
class Crc32(enum.Enum):
Expand Down
112 changes: 112 additions & 0 deletions test/unit/test_crc.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,38 @@ def test_crc8_saej1850_zero(self):
crc_register.update(test.data.encode("utf-8"))
self.assertEqual(test.checksum, crc_register.digest())

def test_crc8_itu(self):
config = Crc8.ITU
for register_type in self._register_types:
crc_register = register_type(config)
test_suit = [
Fixture(data="", checksum=0x55),
Fixture(data=string.digits[1:], checksum=0xA1),
Fixture(data=string.digits[1:][::-1], checksum=0xC4),
Fixture(data=string.digits, checksum=0x10),
Fixture(data=string.digits[::-1], checksum=0x3B),
]
for test in test_suit:
crc_register.init()
crc_register.update(test.data.encode("utf-8"))
self.assertEqual(test.checksum, crc_register.digest())

def test_crc8_rohc(self):
config = Crc8.ROHC
for register_type in self._register_types:
crc_register = register_type(config)
test_suit = [
Fixture(data="", checksum=0xFF),
Fixture(data=string.digits[1:], checksum=0xD0),
Fixture(data=string.digits[1:][::-1], checksum=0x68),
Fixture(data=string.digits, checksum=0xE3),
Fixture(data=string.digits[::-1], checksum=0x62),
]
for test in test_suit:
crc_register.init()
crc_register.update(test.data.encode("utf-8"))
self.assertEqual(test.checksum, crc_register.digest())

def test_crc16_ccitt(self):
config = Crc16.XMODEM
for register_type in self._register_types:
Expand Down Expand Up @@ -300,6 +332,86 @@ def test_cr16_kermit(self):
crc_register.update(test.data.encode("utf-8"))
self.assertEqual(test.checksum, crc_register.digest())

def test_cr16_ibm(self):
config = Crc16.IBM
for register_type in self._register_types:
crc_register = register_type(config)
test_suit = [
Fixture(data="", checksum=0x0000),
Fixture(data=string.digits[1:], checksum=0xBB3D),
Fixture(data=string.digits[1:][::-1], checksum=0x39D9),
Fixture(data=string.digits, checksum=0x443D),
Fixture(data=string.digits[::-1], checksum=0x8EF8),
]
for test in test_suit:
crc_register.init()
crc_register.update(test.data.encode("utf-8"))
self.assertEqual(test.checksum, crc_register.digest())

def test_cr16_maxim(self):
config = Crc16.MAXIM
for register_type in self._register_types:
crc_register = register_type(config)
test_suit = [
Fixture(data="", checksum=0xFFFF),
Fixture(data=string.digits[1:], checksum=0x44C2),
Fixture(data=string.digits[1:][::-1], checksum=0xC626),
Fixture(data=string.digits, checksum=0xBBC2),
Fixture(data=string.digits[::-1], checksum=0x7107),
]
for test in test_suit:
crc_register.init()
crc_register.update(test.data.encode("utf-8"))
self.assertEqual(test.checksum, crc_register.digest())

def test_cr16_usb(self):
config = Crc16.USB
for register_type in self._register_types:
crc_register = register_type(config)
test_suit = [
Fixture(data="", checksum=0x0000),
Fixture(data=string.digits[1:], checksum=0xB4C8),
Fixture(data=string.digits[1:][::-1], checksum=0x362C),
Fixture(data=string.digits, checksum=0xBCB2),
Fixture(data=string.digits[::-1], checksum=0x7677),
]
for test in test_suit:
crc_register.init()
crc_register.update(test.data.encode("utf-8"))
self.assertEqual(test.checksum, crc_register.digest())

def test_cr16_x25(self):
config = Crc16.X25
for register_type in self._register_types:
crc_register = register_type(config)
test_suit = [
Fixture(data="", checksum=0x0000),
Fixture(data=string.digits[1:], checksum=0x906E),
Fixture(data=string.digits[1:][::-1], checksum=0x8578),
Fixture(data=string.digits, checksum=0x3C16),
Fixture(data=string.digits[::-1], checksum=0x3EB1),
]
for test in test_suit:
crc_register.init()
crc_register.update(test.data.encode("utf-8"))
self.assertEqual(test.checksum, crc_register.digest())

def test_cr16_dnp(self):
config = Crc16.DNP
for register_type in self._register_types:
crc_register = register_type(config)
test_suit = [
Fixture(data="", checksum=0xFFFF),
Fixture(data=string.digits[1:], checksum=0xEA82),
Fixture(data=string.digits[1:][::-1], checksum=0x51A6),
Fixture(data=string.digits, checksum=0x6772),
Fixture(data=string.digits[::-1], checksum=0x4C77),
]
for test in test_suit:
crc_register.init()
crc_register.update(test.data.encode("utf-8"))
self.assertEqual(test.checksum, crc_register.digest())

def test_crc16_with_reflected_input(self):
config = Configuration(16, 0x1021, 0, 0, True, False)
for register_type in self._register_types:
Expand Down
Loading