1#-----------------------------------------------------------------------------
2# Demonstrate the use of the code generator
3from crcmod import Crc
4
5g8 = 0x185
6g16 = 0x11021
7g24 = 0x15D6DCB
8g32 = 0x104C11DB7
9
10def polyFromBits(bits):
11    p = 0
12    for n in bits:
13        p = p | (1 << n)
14    return p
15
16# The following is from Standard ECMA-182 "Data Interchange on 12,7 mm 48-Track
17# Magnetic Tape Cartridges -DLT1 Format-", December 1992.
18
19g64 = polyFromBits([64, 62, 57, 55, 54, 53, 52, 47, 46, 45, 40, 39, 38, 37,
20            35, 33, 32, 31, 29, 27, 24, 23, 22, 21, 19, 17, 13, 12, 10, 9, 7,
21            4, 1, 0])
22
23print('Generating examples.c')
24out = open('examples.c', 'w')
25out.write('''// Define the required data types
26typedef unsigned char      UINT8;
27typedef unsigned short     UINT16;
28typedef unsigned int       UINT32;
29typedef unsigned long long UINT64;
30''')
31Crc(g8, rev=False).generateCode('crc8',out)
32Crc(g8, rev=True).generateCode('crc8r',out)
33Crc(g16, rev=False).generateCode('crc16',out)
34Crc(g16, rev=True).generateCode('crc16r',out)
35Crc(g24, rev=False).generateCode('crc24',out)
36Crc(g24, rev=True).generateCode('crc24r',out)
37Crc(g32, rev=False).generateCode('crc32',out)
38Crc(g32, rev=True).generateCode('crc32r',out)
39Crc(g64, rev=False).generateCode('crc64',out)
40Crc(g64, rev=True).generateCode('crc64r',out)
41
42# Check out the XOR-out feature.
43Crc(g16, initCrc=0, rev=True, xorOut=~0).generateCode('crc16x',out)
44Crc(g24, initCrc=0, rev=True, xorOut=~0).generateCode('crc24x',out)
45Crc(g32, initCrc=0, rev=True, xorOut=~0).generateCode('crc32x',out)
46Crc(g64, initCrc=0, rev=True, xorOut=~0).generateCode('crc64x',out)
47
48out.close()
49print('Done')
50