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_8_random(self): data = NumberMask(random.randint(1, 0xFFFFFFFFFFFFFFFF), 64) crcSize = 8 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 = ModCRC() crcProc.setReversed(reverse) crcProc.setXorOutValue(xorOut) crcProc.setInitCRC(regInit, crcSize) crc = crcProc.calculate3(data, inputPoly) self.assertEqual(crc, crcLib)
def test_crcmod_8rohc(self): data = NumberMask(0xF00F, 16) inputPoly = NumberMask(0x107, 8) regInit = 0xFF xorOut = 0x0 crc_func = crcmod.predefined.mkCrcFun('crc-8-rohc') ## rev crcLib = crc_func(data.toASCII()) crcProc = ModCRC() crcProc.setReversed() crcProc.setRegisterInitValue(regInit) crcProc.setXorOutValue(xorOut) crc = crcProc.calculate3(data, inputPoly) # print "crcx: {:b} {:b}".format( crcLib, crc ) self.assertEqual(crc, crcLib)
def test_crcmod_8rev_init(self): data = NumberMask(0xFF0F, 16) inputPoly = NumberMask(0x181, 8) regInit = 0x0F xorOut = 0x0 crc_func = crcmod.mkCrcFun(inputPoly.masterData(), initCrc=regInit, rev=True, xorOut=xorOut) crcLib = crc_func(data.toASCII()) crcProc = ModCRC() crcProc.setReversed() crcProc.setRegisterInitValue(regInit) crcProc.setXorOutValue(xorOut) crc = crcProc.calculate3(data, inputPoly) # print "crcx: {:b} {:b}".format( crc, crcLib ) self.assertEqual(crc, crcLib)
def test_crcmod_8darc(self): data = NumberMask(0x01, 8) inputPoly = NumberMask(0x139, 8) regInit = 0x0 xorOut = 0x0 crc_func = crcmod.predefined.mkCrcFun('crc-8-darc') crcLib = crc_func(data.toASCII()) crcProc = ModCRC() crcProc.setReversed() crcProc.setRegisterInitValue(regInit) crcProc.setXorOutValue(xorOut) revData = data.reorderedBytes() crc = crcProc.calculate3(revData, inputPoly) # print "crcx: {:b} {:b}".format( crc, crcLib ) self.assertEqual(crc, crcLib)
def test_crcmod_8rev_symm(self): data = NumberMask(0xFF, 8) inputPoly = NumberMask(0x181, 8) ## leave symmetric regInit = 0x0 xorOut = 0x0 crc_func = crcmod.mkCrcFun(inputPoly.masterData(), initCrc=regInit, rev=True, xorOut=xorOut) crcLib = crc_func(data.toASCII()) crcProc = ModCRC() crcProc.setReversed() crcProc.setRegisterInitValue(regInit) crcProc.setXorOutValue(xorOut) revData = data.reorderedBytes() revPoly = inputPoly.reversed() crc = crcProc.calculate3(revData, revPoly) # print "crcx: {:b} {:b}".format( 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)
samples = int(args.samples) preSize = int(args.preamblesize) dataSize = int(args.datasize) poly = int(args.poly, 16) polySize = poly.bit_length() initCrc = 0x0 xor = int(args.xor, 16) rev = args.reversed print "## call command: {}".format(' '.join(sys.argv[:])) print "## pres:{} ds:{} poly:0x{:X} rev:{} initcrc:0x{:X} xor:0x{:X}".format( preSize, dataSize, poly, rev, initCrc, xor) crc_func = crcmod.mkCrcFun(poly, rev=rev, initCrc=initCrc, xorOut=xor) preambleFormat = "{}" dataFormat = "{:0" + str(dataSize / 4) + "X}" polyFormat = "{:0" + str(polySize / 4) + "X}" messageFormat = preambleFormat + dataFormat + " " + polyFormat for _ in xrange(0, samples): preamble = randomHexString(preSize) data = randomHexString(dataSize) messageNum = int(data, 16) messageMask = NumberMask(messageNum, dataSize * 4) polyCRC = crc_func(messageMask.toASCII()) print messageFormat.format(preamble, messageMask.dataNum, polyCRC) finally: pass