libscrc icon indicating copy to clipboard operation
libscrc copied to clipboard

Implement GSM32

Open ThalusA opened this issue 4 years ago • 4 comments

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

ThalusA avatar Jan 31 '22 20:01 ThalusA

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

OK, No problem.

hex-in avatar Mar 04 '22 13:03 hex-in

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 avatar Dec 31 '23 04:12 hex-in

image

polynomial = 0x04C11DB7L

hex-in avatar Dec 31 '23 04:12 hex-in

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}')

hex-in avatar Jun 19 '24 12:06 hex-in