def test_function_predefined_table(self): for table_entry in _predefined_crc_definitions: # Check predefined function crc_func = mkPredefinedCrcFun(table_entry['name']) calc_value = crc_func("123456789") self.assertEqual(calc_value, table_entry['check'], "Wrong answer for CRC '%s'" % table_entry['name'])
def find_match(self, payload, checksum): for name in self.CRC_ALGORITHM_NAMES: crcfun = mkPredefinedCrcFun(name) computed_checksum = crcfun(payload) if computed_checksum == checksum: return name return None
def _calculate_CRC16(id_): """Calculate and return the CRC-16 for the identifier. Calculate the CRC-16 value for the given identifier. Return the CRC-16 integer value. ``id_`` should be a bytearray object, or a bytestring (Python 2 str). Some doctests: >>> self._calculate_CRC16(bytearray([0, 1, 2, 3, 0, 9, 0, 0, 255])) 41953 >>> self._calculate_CRC16(bytearray([0, 1, 2, 3, 0, 9, 0, 0, 255])) 58273 """ # Coerce to bytearray. If already a bytearray this will create a copy # so as to avoid side-effects of manipulation for CRC calculation id_ = bytearray(id_) # Reset CRC bytes in copy to 0 for calculation id_[6] = 0 id_[7] = 0 # Need to generate CRC func crc16fun = mkPredefinedCrcFun('crc-16') crc16 = crc16fun(str(id_)) # Return a 2 byte string representation of the resulting integer # in network byte order (big-endian) return crc16
def _update_data(self) -> None: """Update command data.""" # padding data self.data = SecBootBlckSize.align_block_fill_random(self.data) # update header self._header.count = len(self.data) crc32_function = mkPredefinedCrcFun("crc-32-mpeg") self._header.data = crc32_function(self.data, 0xFFFFFFFF)
def test_known_answers(self): for crcfun_name, v in self.known_answers: crcfun = mkPredefinedCrcFun(crcfun_name) self.assertEqual(crcfun('',0), 0, "Wrong answer for CRC '%s', input ''" % crcfun_name) for i, msg in enumerate(self.test_messages_for_known_answers): self.assertEqual(crcfun(msg), v[i], "Wrong answer for CRC %s, input '%s'" % (crcfun_name,msg)) self.assertEqual(crcfun(msg[4:], crcfun(msg[:4])), v[i], "Wrong answer for CRC %s, input '%s'" % (crcfun_name,msg)) self.assertEqual(crcfun(msg[-1:], crcfun(msg[:-1])), v[i], "Wrong answer for CRC %s, input '%s'" % (crcfun_name,msg))
def calc_crc(data: bytes) -> int: """Calculate CRC from the data. :param data: data to calculate CRC from :return: calculated CRC """ crc_function = mkPredefinedCrcFun("xmodem") return crc_function(data)
def __init__(self, api_key_file='mvshlf-api-key.json', api_url = 'https://api.moveshelf.com/graphql'): self._crc32c = mkPredefinedCrcFun('crc32c') self.api_url = api_url if path.isfile(api_key_file) == False: raise ValueError("No valid API key. Please check instructions on https://github.com/moveshelf/python-api-example") with open(api_key_file, 'r') as key_file: data = json.load(key_file) self._auth_token = BearerTokenAuth(data['secretKey'])
def __init__(self, port): if port: self._com = Serial(port, baudrate=115200, timeout=1.0) if self._com is None: raise Exception("environment sensor is not found") else: raise Exception("no such environment sensor COM port") self._crc16func = mkPredefinedCrcFun('crc-16') self._crc16obj = PredefinedCrc('crc-16')
def verify_crc(self): frame_header_bytes = bytearray(self.raw_header) # we want to check everything up to (but not including) the CRC, which is the last byte bytes_to_verify = frame_header_bytes[:-1] correct_crc = frame_header_bytes[-1] crcModFunc = mkPredefinedCrcFun('crc-8') actual_crc = crcModFunc(bytes_to_verify) if correct_crc != actual_crc: raise RuntimeError( 'Mismatched CRC! Expected {}, but computed value is {}'.format( hex(correct_crc), hex(actual_crc)))
def sign(self, image: bytes) -> bytes: """Do simple calculation of CRC and return updated image with it. :param image: Input raw image. :return: Image enriched by CRC in IVT table. """ # calculate CRC using MPEG2 specification over all of data (app and trustzone) # expect for 4 bytes at CRC_BLOCK_OFFSET crc32_function = mkPredefinedCrcFun("crc-32-mpeg") crc = crc32_function(image[:self.IVT_CRC_CERTIFICATE_OFFSET]) crc = crc32_function(image[self.IVT_CRC_CERTIFICATE_OFFSET + 4:], crc) # Recreate data with valid CRC value return self.update_crc_val_cert_offset(image, crc)
def get_hash(path): assert _usingExtension, "You must use the crcmod C extension" if isinstance(path, bytes): path = path.decode("ascii") hash_func = mkPredefinedCrcFun("crc-32") if os.path.islink(path): return SYMLINK + os.readlink(path) try: with open(path, "rb") as f: return hash_func(f.read()) except FileNotFoundError: return except IsADirectoryError: return
def parse_footer_crc(self): # read byte data of start of frame to start of footer frame_size = self.file.tell() - self.fileoff self.file.seek(self.fileoff) frame_data = self.file.read(frame_size) # CRC-16 # TODO (PT): check every >1 byte read and make sure we swap to big endian self.footer_crc = read_big_endian_uint16(self.file) crcModFunc = mkPredefinedCrcFun('crc-16-buypass') actual_crc = crcModFunc(frame_data) if self.footer_crc != actual_crc: raise RuntimeError( 'Frame footer CRC mismatch. Expected {}, got {}'.format( hex(actual_crc), hex(self.footer_crc)))
def test_known_answers(self): for crcfun_name, v in self.known_answers: crcfun = mkPredefinedCrcFun(crcfun_name) self.assertEqual( crcfun('', 0), 0, "Wrong answer for CRC '%s', input ''" % crcfun_name) for i, msg in enumerate(self.test_messages_for_known_answers): self.assertEqual( crcfun(msg), v[i], "Wrong answer for CRC %s, input '%s'" % (crcfun_name, msg)) self.assertEqual( crcfun(msg[4:], crcfun(msg[:4])), v[i], "Wrong answer for CRC %s, input '%s'" % (crcfun_name, msg)) self.assertEqual( crcfun(msg[-1:], crcfun(msg[:-1])), v[i], "Wrong answer for CRC %s, input '%s'" % (crcfun_name, msg))
def _calculate_crc16(id_): """Calculate and return the CRC-16 for the given identifier. Return the CRC-16 integer value. :param id_: The id being created :type id_: bytearray (bytestring in Python 2) :return: the CRC-16 integer value :rtype: int """ # Coerce to bytearray. If already a bytearray this will create a copy # so as to avoid side-effects of manipulation for CRC calculation id_ = bytearray(id_) # Reset CRC bytes in copy to 0 for calculation id_[6] = 0 id_[7] = 0 # Need to generate CRC func crc16fun = mkPredefinedCrcFun("crc-16") crc16 = crc16fun(id_) # Return a 2 byte string representation of the resulting integer # in network byte order (big-endian) return crc16
def parse(cls, data: bytes, offset: int = 0) -> "CmdLoad": """Parse command from bytes. :param data: Input data as bytes :param offset: The offset of input data :return: CMD Load object :raises SPSDKError: Raised when there is invalid CRC :raises SPSDKError: When there is incorrect header tag """ header = CmdHeader.parse(data, offset) if header.tag != EnumCmdTag.LOAD: raise SPSDKError("Incorrect header tag") offset += CmdHeader.SIZE header_count = SecBootBlckSize.align(header.count) cmd_data = data[offset:offset + header_count] crc32_function = mkPredefinedCrcFun("crc-32-mpeg") if header.data != crc32_function(cmd_data, 0xFFFFFFFF): raise SPSDKError("Invalid CRC in the command header") obj = CmdLoad(header.address, cmd_data) obj.header.data = header.data obj.header.flags = header.flags obj._update_data() return obj
print('error in {} at record {}'.format(fname, i)) print(e) #print(str(record)) print("FIle {} needs repairing".format(fname)) toBeRepaired.append(fname) return toBeRepaired ############################# # The code below here uses the crcmod package to implement an alternative method which is able to print out # if it finds a bad record and attempt to keep going. If the corruption in your file is just flipped bits this may be helpful. # If the corruption is added or deleted bytes this will probably crash and burn. import struct from crcmod.predefined import mkPredefinedCrcFun _crc_fn = mkPredefinedCrcFun('crc-32c') def calc_masked_crc(data): crc = _crc_fn(data) return (((crc >> 15) | (crc << 17)) + 0xa282ead8) & 0xFFFFFFFF def writeRecord(dstTFRecordPath, length, len_crc, data, data_crc): with open(dstTFRecordPath, 'ab') as f: f.write(length) f.write(len_crc) f.write(data) f.write(data_crc)
def fahrenheit_to_raw(value: int) -> int: raise NotImplemented def celcius_to_raw(value: int) -> int: res = value - 16 res = ensure_raw_range(res) return res def raw_to_celcius(value: int) -> int: return value + 16 modbus_crc = mkPredefinedCrcFun('modbus') class AirConditionerController: def __init__(self, host: str, port: int = 1998) -> None: self.host = host self.port = port self.data = AirConditionerData() self._reset_data() def _reset_data(self): self.data.d1 = 0 self.data.d2 = 0 self.data.d3 = 0 self.data.d4 = sbyte2byte(-124) self.data.d5 = 0
def test_crcmod_crc16(self): crcfun = mkPredefinedCrcFun("crc-16") self.assertEqual(crcfun('123456789'), 0xBB3D)
import numpy as np from PIL import Image from crcmod.predefined import mkPredefinedCrcFun crc16arc = mkPredefinedCrcFun('crc-16') def chunks(l, n): """Yield successive n-sized chunks from l.""" for i in range(0, len(l), n): yield l[i:i + n] def bytearr(frame): "array of all bytes of the frame" return bytearray([int(n, base=2) for n in chunks(frame.strip(), 8)]) def bitarr(frame, pad): "Array of *content* bits" data = frame.strip()[pad:-64] return [int(n, base=2) for n in data] def read_bitstream(fname): bitmap = [] hdr = [] ftr = [] is_hdr = True crcdat = bytearray() preamble = 3 frames = 0 with open(fname) as inp: for line in inp: