Exemple #1
0
def main():
    string1 = ''
    string2 = "c651ceb5fa05b4195f993513d8bb5381"
    crc = Crc(width = 16, poly = 0x8005,
              reflect_in = True, xor_in = 0x0000,
              reflect_out = True, xor_out = 0x0000)
    crc1 = 'a'
    crc2 = crc.table_driven(string2)
    print datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S') 
    while crc1 != crc2: 
        string1 = ''.join(random.SystemRandom().choice(string.ascii_letters + \
         string.digits) for _ in range(32))
        crc1 = crc.table_driven(string1)
 
    print datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S') 
    print string1
    print string2 
    print "CRC: " + str(crc1)
Exemple #2
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(
            sys.argv[0]))
        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(
            sys.argv[0]))
        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
def check_string(opt):
    """
    Return the calculated CRC sum of a string.
    """
    error = False
    if opt.UndefinedCrcParameters:
        sys.stderr.write("%s: error: undefined parameters\n" % sys.argv[0])
        sys.exit(1)
    
    #print(opt.Algorithm)

    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.ReflectIn, xor_in = opt.XorIn,
        reflect_out = opt.ReflectOut, xor_out = opt.XorOut,
        table_idx_width = opt.TableIdxWidth)
    
    opt.Algorithm = opt.Algo_Table_Driven

    crc = None
    if opt.Algorithm & opt.Algo_Bit_by_Bit:
      #print( "bit_by_bit")
        bbb_crc = alg.bit_by_bit(opt.CheckString)
        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.CheckString)
        #print( "bit_by_bit_fast")
        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.TableIdxWidth = 8
        tbl_crc = alg.table_driven(opt.CheckString)
        if crc != None and tbl_crc != crc:
            error = True
        crc = tbl_crc

    if error:
        sys.stderr.write("%s: error: different checksums!\n" % sys.argv[0])
        if opt.Algorithm & opt.Algo_Bit_by_Bit:
            sys.stderr.write("       bit-by-bit:        0x%x\n" % bbb_crc)
        if opt.Algorithm & opt.Algo_Bit_by_Bit_Fast:
            sys.stderr.write("       bit-by-bit-fast:   0x%x\n" % bbf_crc)
        if opt.Algorithm & opt.Algo_Table_Driven:
            sys.stderr.write("       table_driven:      0x%x\n" % tbl_crc)
        sys.exit(1)
    return crc
Exemple #4
0
def check_string(opt):
    """
    Return the calculated CRC sum of a string.
    """
    error = False
    if opt.UndefinedCrcParameters:
        sys.stderr.write("%s: error: undefined parameters\n" % sys.argv[0])
        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.ReflectIn,
              xor_in=opt.XorIn,
              reflect_out=opt.ReflectOut,
              xor_out=opt.XorOut,
              table_idx_width=opt.TableIdxWidth)

    crc = None
    if opt.Algorithm & opt.Algo_Bit_by_Bit:
        bbb_crc = alg.bit_by_bit(opt.CheckString)
        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.CheckString)
        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.TableIdxWidth = 8
        tbl_crc = alg.table_driven(opt.CheckString)
        if crc != None and tbl_crc != crc:
            error = True
        crc = tbl_crc

    if error:
        sys.stderr.write("%s: error: different checksums!\n" % sys.argv[0])
        if opt.Algorithm & opt.Algo_Bit_by_Bit:
            sys.stderr.write("       bit-by-bit:        0x%x\n" % bbb_crc)
        if opt.Algorithm & opt.Algo_Bit_by_Bit_Fast:
            sys.stderr.write("       bit-by-bit-fast:   0x%x\n" % bbf_crc)
        if opt.Algorithm & opt.Algo_Table_Driven:
            sys.stderr.write("       table_driven:      0x%x\n" % tbl_crc)
        sys.exit(1)
    return crc
Exemple #5
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
Exemple #6
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 = 0x%(poly)x, reflect_in = %(reflect_in)s, xor_in = 0x%(xor_in)x, reflect_out = %(reflect_out)s, xor_out = 0x%(xor_out)x)" % model
            if expected_crc is not None:
                out_str += " [check = 0x%x]" % 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:             0x%x" % expected_crc)
            if self.use_algo_bit_by_bit:
                print("       bit-by-bit:        0x%x" % bbb_crc)
            if self.use_algo_bit_by_bit_fast:
                print("       bit-by-bit-fast:   0x%x" % bbf_crc)
            if self.use_algo_table_driven:
                print("       table_driven:      0x%x" % tbl_crc)
            return None
        return crc
Exemple #7
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(sys.argv[0]))
        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(sys.argv[0]))
        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
Exemple #8
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 = 0x%(poly)x, reflect_in = %(reflect_in)s, xor_in = 0x%(xor_in)x, reflect_out = %(reflect_out)s, xor_out = 0x%(xor_out)x)" % model
            if expected_crc is not None:
                out_str += " [check = 0x%x]" % 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:             0x%x" % expected_crc)
            if self.use_algo_bit_by_bit:
                print("       bit-by-bit:        0x%x" % bbb_crc)
            if self.use_algo_bit_by_bit_fast:
                print("       bit-by-bit-fast:   0x%x" % bbf_crc)
            if self.use_algo_table_driven:
                print("       table_driven:      0x%x" % tbl_crc)
            return None
        return crc
Exemple #9
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
Exemple #10
0
class mfm_track:

    # -------------------------------------------------------------------
    def __init__(self, fname):
        print "Loading track image: %s..." % fname
        f = open(fname, "r")
        self.data = bytearray(f.read())
        f.close()

        self.samples = []
        self.gaps = []
        self.gap_hist = {}
        self.clock = []
        self.a1 = []

        self.a1_mark = [0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1]
        self.a1_buf = []
        self.a1_clk = []
        self.a1_pos = 0

        self.crc = Crc(width = 16, poly = 0x1021, reflect_in = False, xor_in = 0xffff, reflect_out = False, xor_out = 0x0000);
        #self.crc = Crc(width = 16, poly = 0x140a0445, reflect_in = False, xor_in = 0xffffffff, reflect_out = False, xor_out = 0x0000);

        print "Unpacking..."
        self.explode()

    # -------------------------------------------------------------------
    def explode(self):
        for byte in self.data:
            for bit in [7, 6, 5, 4, 3, 2, 1, 0]:
                v = (byte >> bit) & 1
                # [value, clock, a1, cell, validdbit, bit, validbyte, byte, crcok]
                self.samples.append([v, 0, 0, 0, 0, 0, 0, 0, 0])

    # -------------------------------------------------------------------
    def clock_regen(self, clock_period, early_clock_margin, clock_offset=0):
        counter = 0
        ov = 1
        next_clock = 0

        while counter < len(self.samples):
            v = self.samples[counter][0]
            self.samples[counter] = [v, 0, 0, 0, 0, 0, 0, 0, 0]

            # each rising edge restarts clock
            if (ov == 0) and (v == 1):
                # clock cleanup - remove previously inserted early clock ticks
                if early_clock_margin and not clock_offset:
                    prev_clock_tick = self.clock[len(self.clock)-1]
                    if counter - prev_clock_tick <= early_clock_margin:
                        self.clock.pop()
                        self.samples[prev_clock_tick][1] = 0

                if not clock_offset:
                    next_clock = counter + clock_period
                    self.clock.append(counter)
                    self.samples[counter][1] = 1
                else:
                    next_clock = counter + clock_offset

            # is it time for next clock tick?
            if counter >= next_clock:
                self.samples[counter][1] = 1
                self.clock.append(counter)
                next_clock = counter + clock_period

            ov = v
            counter += 1

    # -------------------------------------------------------------------
    def a1_match(self, clock):
        bit = self.samples[clock][0]
        self.a1_buf.append(bit)
        self.a1_clk.append(clock)

        if len(self.a1_buf) == 16:
            if self.a1_buf == self.a1_mark:
                clk = self.a1_clk[0]
                self.a1_buf = []
                self.a1_clk = []
                return clk
            else:
                self.a1_buf.pop(0)
                self.a1_clk.pop(0)

        return 0

    # -------------------------------------------------------------------
    def a1_search(self):
        clk = 0
        while clk < len(self.clock):
            a1beg = self.a1_match(self.clock[clk])
            if a1beg:
                self.a1.append(clk)
                self.samples[a1beg][2] = 1
                self.samples[self.clock[clk+1]][2] = 2
            clk += 1

    # -------------------------------------------------------------------
    def calc_gaps(self):
        ls = 0
        pos = opos = 0
        for (s,c,a1,cell,valbit,bit,valbyte,byte,crcok) in self.samples:
            if (s != ls) and (s == 1):
                diff = pos-opos
                self.gaps.append(diff)
                if (self.gap_hist.has_key(diff)):
                    self.gap_hist[diff] += 1
                else:
                    self.gap_hist[diff] = 1
                opos = pos
            pos += 1
            ls = s

    # -------------------------------------------------------------------
    def read_bytes(self, clkpos, b):
        bit = 7
        char = 0
        data = [ 0xa1 ]
        cellmark = 1
        crcok = False
        crc = 0
        while b > 0:
            # prepare 0, +1 and +2 clocks
            clk0 = self.clock[clkpos]
            clk1 = self.clock[clkpos+1]
            clk2 = self.clock[clkpos+2]

            # mark cell start
            self.samples[clk0][3] = cellmark
            cellmark *= -1

            # mark bit end
            self.samples[clk2][4] = 1
            self.samples[clk2][5] = self.samples[clk1][0]

            # shift bit value into the byte
            char |= self.samples[clk1][0] << bit
            bit -= 1

            if bit < 0:
                # append only data, not CRC
                if b > 2:
                    data.append(char)
                elif b == 2:
                    crc = self.crc.table_driven(''.join([chr(x) for x in data]))
                    print "%x" % crc
                    if char == (crc & 0xff00) >> 8:
                        self.samples[clk2][8] = 1
                        crcok = True
                    else:
                        self.samples[clk2][8] = 2
                        crcok = False
                elif b == 1:
                    if char == crc & 0xff:
                        self.samples[clk2][8] = 1
                        crcok &= True
                    else:
                        self.samples[clk2][8] = 2
                        crcok = False

                # mark and store byte
                self.samples[clk2][6] = 1
                self.samples[clk2][7] = char

                bit = 7
                b -= 1
                char = 0

            clkpos += 2
        self.samples[clk2][3] = 2
        return data, crcok

    # -------------------------------------------------------------------
    def analyze(self, clock, margin, offset):

        self.gaps = []
        self.gap_hist = {}
        self.clock = []
        self.a1 = []

        print "Regenerating clock..."
        self.clock_regen(clock, margin, offset)

        print "Analyzing signal gaps..."
        self.calc_gaps()

        print "Looking for sector header/data marks..."
        self.a1_search()
        print "%i A1 marks found." % len(self.a1)

        print "Analyzing sectors..."
        count = 0
        while count < len(self.a1):
            try:
                data, crcok = self.read_bytes(self.a1[count]+1, 6)
                print ''.join([chr(x) for x in data])
                print "---- CRC: %s --------------------------------------------------------" % str(crcok)
            except Exception, e:
                print str(e)
                pass
            count += 1
            try:
                data, crcok = self.read_bytes(self.a1[count]+1, 512 + 3)
                print ''.join([chr(x) for x in data])
                print "---- CRC: %s --------------------------------------------------------" % str(crcok)
            except Exception, e:
                print str(e)
                pass
            count += 1