def check_file(opt): """ Calculate the CRC of a file. This algorithm uses the table_driven CRC algorithm. """ if opt.undefined_crc_parameters: sys.stderr.write("{0:s}: error: undefined parameters\n".format(sys.argv[0])) sys.exit(1) alg = Crc( width=opt.width, poly=opt.poly, reflect_in=opt.reflect_in, xor_in=opt.xor_in, reflect_out=opt.reflect_out, xor_out=opt.xor_out, table_idx_width=opt.tbl_idx_width) if not opt.reflect_in: register = opt.xor_in else: register = alg.reflect(opt.xor_in, opt.width) try: with open(opt.check_file, 'rb') as f: check_bytes = bytearray(f.read(1024)) while check_bytes != b"": register = crc_file_update(alg, register, check_bytes) check_bytes = bytearray(f.read(1024)) except IOError: sys.stderr.write( "{0:s}: error: can't open file {1:s}\n".format(sys.argv[0], opt.check_file)) sys.exit(1) if opt.reflect_out: register = alg.reflect(register, opt.width) register = register ^ opt.xor_out return register
def check_file(opt): """ Calculate the CRC of a file. This algorithm uses the table_driven CRC algorithm. """ if opt.UndefinedCrcParameters: sys.stderr.write("%s: error: undefined parameters\n" % sys.argv[0]) sys.exit(1) alg = Crc(width = opt.Width, poly = opt.Poly, reflect_in = opt.ReflectIn, xor_in = opt.XorIn, reflect_out = opt.ReflectOut, xor_out = opt.XorOut, table_idx_width = opt.TableIdxWidth) try: in_file = open(opt.CheckFile, 'rb') except IOError: sys.stderr.write("%s: error: can't open file %s\n" % (sys.argv[0], opt.CheckFile)) sys.exit(1) if not opt.ReflectIn: register = opt.XorIn else: register = alg.reflect(opt.XorIn, opt.Width) # Read bytes from the file. check_bytes = in_file.read(1024) while check_bytes: register = crc_file_update(alg, register, check_bytes) check_bytes = in_file.read(1024) in_file.close() if opt.ReflectOut: register = alg.reflect(register, opt.Width) register = register ^ opt.XorOut return register
def check_file(opt): """ Calculate the CRC of a file. This algorithm uses the table_driven CRC algorithm. """ if opt.UndefinedCrcParameters: sys.stderr.write("%s: error: undefined parameters\n" % sys.argv[0]) sys.exit(1) alg = Crc(width = opt.Width, poly = opt.Poly, reflect_in = opt.ReflectIn, xor_in = opt.XorIn, reflect_out = opt.ReflectOut, xor_out = opt.XorOut, table_idx_width = opt.TableIdxWidth) try: in_file = open(opt.CheckFile, 'rb') except IOError: sys.stderr.write("%s: error: can't open file %s\n" % (sys.argv[0], opt.CheckFile)) sys.exit(1) if not opt.ReflectIn: register = opt.XorIn else: register = alg.reflect(opt.XorIn, opt.Width) # Read bytes from the file. check_byte_str = in_file.read() while check_byte_str: register = crc_file_update(alg, register, check_byte_str) check_byte_str = in_file.read() in_file.close() if opt.ReflectOut: register = alg.reflect(register, opt.Width) register = register ^ opt.XorOut return register
def check_file(opt): """ Calculate the CRC of a file. This algorithm uses the table_driven CRC algorithm. """ if opt.undefined_crc_parameters: sys.stderr.write("{0:s}: error: undefined parameters\n".format( sys.argv[0])) sys.exit(1) alg = Crc(width=opt.width, poly=opt.poly, reflect_in=opt.reflect_in, xor_in=opt.xor_in, reflect_out=opt.reflect_out, xor_out=opt.xor_out, table_idx_width=opt.tbl_idx_width) try: in_file = open(opt.check_file, 'rb') except IOError: sys.stderr.write("{0:s}: error: can't open file {1:s}\n".format( sys.argv[0], opt.check_file)) sys.exit(1) if not opt.reflect_in: register = opt.xor_in else: register = alg.reflect(opt.xor_in, opt.width) # Read bytes from the file. check_bytes = in_file.read(1024) while check_bytes: register = crc_file_update(alg, register, check_bytes) check_bytes = in_file.read(1024) in_file.close() if opt.reflect_out: register = alg.reflect(register, opt.width) register = register ^ opt.xor_out return register
def __get_init_value(self): """ Return the init value of a C implementation, according to the selected algorithm and to the given options. If no default option is given for a given parameter, value in the cfg_t structure must be used. """ if self.opt.Algorithm == self.opt.Algo_Bit_by_Bit: if self.opt.XorIn == None or self.opt.Width == None or self.opt.Poly == None: return None crc = Crc( width=self.opt.Width, poly=self.opt.Poly, reflect_in=self.opt.ReflectIn, xor_in=self.opt.XorIn, reflect_out=self.opt.ReflectOut, xor_out=self.opt.XorOut, table_idx_width=self.opt.TableIdxWidth, ) init = crc.NonDirectInit elif self.opt.Algorithm == self.opt.Algo_Bit_by_Bit_Fast: if self.opt.XorIn == None: return None init = self.opt.XorIn elif self.opt.Algorithm == self.opt.Algo_Table_Driven: if self.opt.ReflectIn == None or self.opt.XorIn == None or self.opt.Width == None: return None if self.opt.Poly == None: poly = 0 else: poly = self.opt.Poly crc = Crc( width=self.opt.Width, poly=poly, reflect_in=self.opt.ReflectIn, xor_in=self.opt.XorIn, reflect_out=self.opt.ReflectOut, xor_out=self.opt.XorOut, table_idx_width=self.opt.TableIdxWidth, ) if self.opt.ReflectIn: init = crc.reflect(crc.DirectInit, self.opt.Width) else: init = crc.DirectInit else: init = 0 return self.__pretty_hex(init, self.opt.Width)
def _get_init_value(opt): """ Return the init value of a C implementation, according to the selected algorithm and to the given options. If no default option is given for a given parameter, value in the cfg_t structure must be used. """ if opt.algorithm == opt.algo_bit_by_bit: if opt.xor_in is None or opt.width is None or opt.poly is None: return None crc = Crc(width=opt.width, poly=opt.poly, reflect_in=opt.reflect_in, xor_in=opt.xor_in, reflect_out=opt.reflect_out, xor_out=opt.xor_out, table_idx_width=opt.tbl_idx_width) init = crc.nondirect_init elif opt.algorithm == opt.algo_bit_by_bit_fast: if opt.xor_in is None: return None init = opt.xor_in elif opt.algorithm == opt.algo_table_driven: if opt.reflect_in is None or opt.xor_in is None or opt.width is None: return None if opt.poly is None: poly = 0 else: poly = opt.poly crc = Crc(width=opt.width, poly=poly, reflect_in=opt.reflect_in, xor_in=opt.xor_in, reflect_out=opt.reflect_out, xor_out=opt.xor_out, table_idx_width=opt.tbl_idx_width) if opt.reflect_in: init = crc.reflect(crc.direct_init, opt.width) else: init = crc.direct_init else: init = 0 return _pretty_hex(init, opt.width)
def __get_init_value(self): """ Return the init value of a C implementation, according to the selected algorithm and to the given options. If no default option is given for a given parameter, value in the cfg_t structure must be used. """ if self.opt.Algorithm == self.opt.Algo_Bit_by_Bit: if self.opt.XorIn == None or self.opt.Width == None or self.opt.Poly == None: return None crc = Crc(width=self.opt.Width, poly=self.opt.Poly, reflect_in=self.opt.ReflectIn, xor_in=self.opt.XorIn, reflect_out=self.opt.ReflectOut, xor_out=self.opt.XorOut, table_idx_width=self.opt.TableIdxWidth) init = crc.NonDirectInit elif self.opt.Algorithm == self.opt.Algo_Bit_by_Bit_Fast: if self.opt.XorIn == None: return None init = self.opt.XorIn elif self.opt.Algorithm == self.opt.Algo_Table_Driven: if self.opt.ReflectIn == None or self.opt.XorIn == None or self.opt.Width == None: return None if self.opt.Poly == None: poly = 0 else: poly = self.opt.Poly crc = Crc(width=self.opt.Width, poly=poly, reflect_in=self.opt.ReflectIn, xor_in=self.opt.XorIn, reflect_out=self.opt.ReflectOut, xor_out=self.opt.XorOut, table_idx_width=self.opt.TableIdxWidth) if self.opt.ReflectIn: init = crc.reflect(crc.DirectInit, self.opt.Width) else: init = crc.DirectInit else: init = 0 return self.__pretty_hex(init, self.opt.Width)