def test_crcmod_c8d16_rev(self): data = NumberMask(0xE3F2, 16) inputPoly = NumberMask(0x1F0, 8) regInit = 0x0 xorOut = 0x0 reverse = True crc_func = crcmod.mkCrcFun(inputPoly.masterData(), rev=reverse, initCrc=regInit, xorOut=xorOut) crcLib = crc_func(data.toASCII()) # print "crc: {:X} {:X}".format( crc, crc2 ) crcProc = DivisionCRC() crcProc.setReversed(reverse) crcProc.setXorOutValue(xorOut) crcProc.setRegisterInitValue(regInit) data.reorderBytes() inputPoly.reverse() crc = crcProc.calculate3(data, inputPoly) # print "values: {} poly:{:X} init:{:X} xorOut:{:08b} rev:{} crc:{:08b} crcmod:{:08b} crcxor:{:08b}".format( data, inputPoly, regInit, xorOut, reverse, crc, crcLib, crc^crcLib ) self.assertEqual(crc, crcLib)
def test_crcmod_c8d64_random(self): data = NumberMask(random.randint(1, 0xFFFFFFFFFFFFFFFF), 64) crcSize = 8 crcMax = 2**8 - 1 inputPoly = NumberMask(0x100 | random.randint(1, crcMax), crcSize) ## 'regInit' and 'xorOut' are not incompatible regInit = 0x0 xorOut = 0x0 reverse = bool(random.randint(0, 1)) crc_func = crcmod.mkCrcFun(inputPoly.masterData(), rev=reverse, initCrc=regInit, xorOut=xorOut) crcLib = crc_func(data.toASCII()) # print "crc: {:X} {:X}".format( crc, crc2 ) crcProc = DivisionCRC() crcProc.setReversed(reverse) crcProc.setXorOutValue(xorOut) crcProc.setRegisterInitValue(regInit) # crcProc.setInitCRC( regInit ) if reverse: data.reorderBytes() inputPoly.reverse() crc = crcProc.calculate3(data, inputPoly) # print "values: {} poly:{:X} init:{:X} xorOut:{:08b} rev:{} crc:{:08b} crcmod:{:08b} crcxor:{:08b}".format( data, inputPoly, regInit, xorOut, reverse, crc, crcLib, crc^crcLib ) self.assertEqual(crc, crcLib)
def test_crcmod_c16d64_random(self): data = NumberMask(random.randint(1, 0xFFFFFFFFFFFFFFFF), 64) crcSize = 16 crcMax = 2**crcSize - 1 inputPoly = NumberMask((0x1 << crcSize) | random.randint(1, crcMax), crcSize) # regInit = random.randint(0, crcMax) # xorOut = random.randint(0, crcMax) regInit = 0x0 xorOut = 0x0 reverse = bool(random.randint(0, 1)) crc_func = crcmod.mkCrcFun(inputPoly.masterData(), rev=reverse, initCrc=regInit, xorOut=xorOut) crcLib = crc_func(data.toASCII()) # print "crc: {:X} {:X}".format( crc, crc2 ) crcProc = HwCRC() crcProc.setReversed(reverse) crcProc.setXorOutValue(xorOut) if reverse: data.reorderBytes() inputPoly.reverse() crcInit = reverse_number(regInit ^ xorOut, crcSize) crcProc.setRegisterInitValue(crcInit) else: crcInit = regInit ^ xorOut crcProc.setRegisterInitValue(crcInit) crc = crcProc.calculate3(data, inputPoly) # print "values: {} poly:{:X} init:{:X} xorOut:{:08b} rev:{} crc:{:08b} crcmod:{:08b} crcxor:{:08b}".format( data, inputPoly, regInit, xorOut, reverse, crc, crcLib, crc^crcLib ) self.assertEqual(crc, crcLib)