Beispiel #1
0
def check_string(opt):
    """
    Return the calculated CRC sum of a string.
    """
    error = False
    if opt.undefined_crc_parameters:
        sys.stderr.write(
            "{0:s}: error: undefined parameters\n".format(progname))
        sys.exit(1)
    if opt.algorithm == 0:
        opt.algorithm = opt.algo_bit_by_bit | opt.algo_bit_by_bit_fast | opt.algo_table_driven

    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)

    crc = None
    if opt.algorithm & opt.algo_bit_by_bit:
        bbb_crc = alg.bit_by_bit(opt.check_string)
        if crc != None and bbb_crc != crc:
            error = True
        crc = bbb_crc
    if opt.algorithm & opt.algo_bit_by_bit_fast:
        bbf_crc = alg.bit_by_bit_fast(opt.check_string)
        if crc != None and bbf_crc != crc:
            error = True
        crc = bbf_crc
    if opt.algorithm & opt.algo_table_driven:
        # no point making the python implementation slower by using less than 8 bits as index.
        opt.tbl_idx_width = 8
        tbl_crc = alg.table_driven(opt.check_string)
        if crc != None and tbl_crc != crc:
            error = True
        crc = tbl_crc

    if error:
        sys.stderr.write(
            "{0:s}: error: different checksums!\n".format(progname))
        if opt.algorithm & opt.algo_bit_by_bit:
            sys.stderr.write(
                "       bit-by-bit:        {0:#x}\n".format(bbb_crc))
        if opt.algorithm & opt.algo_bit_by_bit_fast:
            sys.stderr.write(
                "       bit-by-bit-fast:   {0:#x}\n".format(bbf_crc))
        if opt.algorithm & opt.algo_table_driven:
            sys.stderr.write(
                "       table_driven:      {0:#x}\n".format(tbl_crc))
        sys.exit(1)
    return crc
Beispiel #2
0
    def __get_crc(self, model, check_str='123456789', expected_crc=None):
        """
        Get the CRC for a set of parameters from the Python reference implementation.
        """
        if self.verbose:
            out_str = 'Crc(width = {width:d}, poly = {poly:#x}, reflect_in = {reflect_in}, xor_in = {xor_in:#x}, reflect_out = {reflect_out}, xor_out = {xor_out:#x})'.format(
                **model)
            if expected_crc is not None:
                out_str += ' [check = {0:#x}]'.format(expected_crc)
            print(out_str)
        alg = Crc(width=model['width'],
                  poly=model['poly'],
                  reflect_in=model['reflect_in'],
                  xor_in=model['xor_in'],
                  reflect_out=model['reflect_out'],
                  xor_out=model['xor_out'])
        error = False
        crc = expected_crc

        if self.use_algo_bit_by_bit:
            bbb_crc = alg.bit_by_bit(check_str)
            if crc is None:
                crc = bbb_crc
            error = error or bbb_crc != crc
        if self.use_algo_bit_by_bit_fast:
            bbf_crc = alg.bit_by_bit_fast(check_str)
            if crc is None:
                crc = bbf_crc
            error = error or bbf_crc != crc
        if self.use_algo_table_driven:
            tbl_crc = alg.table_driven(check_str)
            if crc is None:
                crc = tbl_crc
            error = error or tbl_crc != crc

        if error:
            print('error: different checksums!')
            if expected_crc is not None:
                print('       check:             {0:#x}'.format(expected_crc))
            if self.use_algo_bit_by_bit:
                print('       bit-by-bit:        {0:#x}'.format(bbb_crc))
            if self.use_algo_bit_by_bit_fast:
                print('       bit-by-bit-fast:   {0:#x}'.format(bbf_crc))
            if self.use_algo_table_driven:
                print('       table_driven:      {0:#x}'.format(tbl_crc))
            return None
        return crc
Beispiel #3
0
def check_string(opt):
    """
    Return the calculated CRC sum of a string.
    """
    error = False
    if opt.undefined_crc_parameters:
        sys.stderr.write("{0:s}: error: undefined parameters\n".format(progname))
        sys.exit(1)
    if opt.algorithm == 0:
        opt.algorithm = opt.algo_bit_by_bit | opt.algo_bit_by_bit_fast | opt.algo_table_driven

    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)

    crc = None
    if opt.algorithm & opt.algo_bit_by_bit:
        bbb_crc = alg.bit_by_bit(opt.check_string)
        if crc != None and bbb_crc != crc:
            error = True
        crc = bbb_crc
    if opt.algorithm & opt.algo_bit_by_bit_fast:
        bbf_crc = alg.bit_by_bit_fast(opt.check_string)
        if crc != None and bbf_crc != crc:
            error = True
        crc = bbf_crc
    if opt.algorithm & opt.algo_table_driven:
        # no point making the python implementation slower by using less than 8 bits as index.
        opt.tbl_idx_width = 8
        tbl_crc = alg.table_driven(opt.check_string)
        if crc != None and tbl_crc != crc:
            error = True
        crc = tbl_crc

    if error:
        sys.stderr.write("{0:s}: error: different checksums!\n".format(progname))
        if opt.algorithm & opt.algo_bit_by_bit:
            sys.stderr.write("       bit-by-bit:        {0:#x}\n".format(bbb_crc))
        if opt.algorithm & opt.algo_bit_by_bit_fast:
            sys.stderr.write("       bit-by-bit-fast:   {0:#x}\n".format(bbf_crc))
        if opt.algorithm & opt.algo_table_driven:
            sys.stderr.write("       table_driven:      {0:#x}\n".format(tbl_crc))
        sys.exit(1)
    return crc
Beispiel #4
0
    def __get_crc(self, model, check_str = '123456789', expected_crc = None):
        """
        Get the CRC for a set of parameters from the Python reference implementation.
        """
        if self.verbose:
            out_str = 'Crc(width = {width:d}, poly = {poly:#x}, reflect_in = {reflect_in}, xor_in = {xor_in:#x}, reflect_out = {reflect_out}, xor_out = {xor_out:#x})'.format(**model)
            if expected_crc is not None:
                out_str += ' [check = {0:#x}]'.format(expected_crc)
            print(out_str)
        alg = Crc(width = model['width'], poly = model['poly'],
            reflect_in = model['reflect_in'], xor_in = model['xor_in'],
            reflect_out = model['reflect_out'], xor_out = model['xor_out'])
        error = False
        crc = expected_crc

        if self.use_algo_bit_by_bit:
            bbb_crc = alg.bit_by_bit(check_str)
            if crc is None:
                crc = bbb_crc
            error = error or bbb_crc != crc
        if self.use_algo_bit_by_bit_fast:
            bbf_crc = alg.bit_by_bit_fast(check_str)
            if crc is None:
                crc = bbf_crc
            error = error or bbf_crc != crc
        if self.use_algo_table_driven:
            tbl_crc = alg.table_driven(check_str)
            if crc is None:
                crc = tbl_crc
            error = error or tbl_crc != crc

        if error:
            print('error: different checksums!')
            if expected_crc is not None:
                print('       check:             {0:#x}'.format(expected_crc))
            if self.use_algo_bit_by_bit:
                print('       bit-by-bit:        {0:#x}'.format(bbb_crc))
            if self.use_algo_bit_by_bit_fast:
                print('       bit-by-bit-fast:   {0:#x}'.format(bbf_crc))
            if self.use_algo_table_driven:
                print('       table_driven:      {0:#x}'.format(tbl_crc))
            return None
        return crc