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

Implement GSM32 #9

Open
ThalusA opened this issue Jan 31, 2022 · 4 comments
Open

Implement GSM32 #9

ThalusA opened this issue Jan 31, 2022 · 4 comments

Comments

@ThalusA
Copy link

ThalusA commented Jan 31, 2022

Is it possible to implement CRC-32 calculation of GSM32 like in this document: link page 140?

@hex-in
Copy link
Owner

hex-in commented Mar 4, 2022

Is it possible to implement CRC-32 calculation of GSM32 like in this document: link page 140?

OK, No problem.

@hex-in
Copy link
Owner

hex-in commented Dec 31, 2023

Please provide me with some data and the CRC32 results for this data. I want to verify the calculation results.

Above, Thank you.

@hex-in
Copy link
Owner

hex-in commented Dec 31, 2023

image

polynomial = 0x04C11DB7L

@hex-in
Copy link
Owner

hex-in commented Jun 19, 2024

def crc32_gsm(data: bytes) -> int:
    # Generator polynomial
    POLY = 0x04C11DB7

    # Initial remainder
    crc = 0x00000000

    # Process each byte in the data
    for byte in data:
        crc ^= byte << 24  # Align byte to the high end of the current CRC value
        for _ in range(8):  # Process each bit in the byte
            if crc & 0x80000000:  # If the highest bit is set
                crc = (crc << 1) ^ POLY
            else:
                crc <<= 1
            crc &= 0xFFFFFFFF  # Ensure crc remains within 32-bit bounds

    return crc

def rearrange_data(data: bytes) -> bytes:
    """ Rearrange data into the format required by the CRC algorithm """
    words = []
    for i in range(0, len(data), 2):
        if i + 1 < len(data):
            mso = data[i]
            lso = data[i + 1]
        else:
            mso = data[i]
            lso = 0
        words.append(lso)
        words.append(mso)
    return bytes(words)

# Example usage
data = b'Test data for GSM32 CRC'
rearranged_data = rearrange_data(data)
checksum = crc32_gsm(rearranged_data)
print(f'GSM32 CRC checksum: {checksum:08X}')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants