예제 #1
0
def bch_check(inputhex):
    errors = [inputhex]
    preamble =''
    if len(inputhex)==36:
        strhex = inputhex[6:]
        preamble = inputhex[:6]
    elif len(inputhex)==30:
        strhex = inputhex
        preamble =''
    else:
        return False
    ## Error correction attempt for when BCH portion does not match recomputed
    try:
        _pdf1 = (Fcn.hextobin(strhex))[:61]
        _bch1 = (Fcn.hextobin(strhex))[61:82]
        bitflips1, newpdf1, newbch1 = bch1.pdf1_to_bch1(_pdf1, _bch1)
        _pdf2 = (Fcn.hextobin(strhex))[82:108]
        _bch2 = (Fcn.hextobin(strhex))[108:]
        bitflips2, newpdf2, newbch2 = bch2.pdf2_to_bch2(_pdf2, _bch2)
        if bitflips1 == -1 or bitflips2 == -1:
            errors.append('Too many bit errors to correct')
        elif bitflips1 > 0 or bitflips2 > 0:
            _newbin = newpdf1 + newbch1 + newpdf2 + newbch2
            fixhex= preamble+ Fcn.bin2hex(_newbin)
            errors.append(fixhex)
            errors.append(' {} bad pdf1 bit and {} bad pdf2 bit'.format(bitflips1, bitflips2))
            errors.append('Corrected Message: {} '.format(fixhex))
    except:
        return False


    return errors
예제 #2
0
def correct_bchsgb(testhex):

    pdf = decodefunctions.hextobin(testhex)[:204]
    bchsgb = (decodefunctions.hextobin(testhex))[204:]
    binary_data_pdf = newdata = pdf.zfill(204)
    correctedbch = bchsgb

    BCH_POLYNOMIAL = 285
    BCH_BITS = 6
    bitflips = 0

    bch = bchlibcaronoff.BCH(BCH_BITS, m=8, prim_poly=BCH_POLYNOMIAL)
    max_data_len = bch.n // 8 - (bch.ecc_bits + 7) // 8 + 1
    data = bytearray(bch1correct.bitstring_to_bytes(binary_data_pdf))
    rebuildpdf = ''

    for e in range(len(data)):
        segment = decodefunctions.dec2bin(data[e]).zfill(8)
        rebuildpdf = rebuildpdf + segment

    rebuildpdf = rebuildpdf.zfill(204)
    ecc = bch.encode(data)
    bchstring = ''

    for e in ecc:
        binchar = decodefunctions.dec2bin(e).zfill(8)
        bchstring = bchstring + binchar

    ecc_provided = bytearray(bch1correct.bitstring_to_bytes(bchsgb))

    packet = data + ecc_provided

    if ecc != ecc_provided:

        data, ecc = packet[:-bch.ecc_bytes], packet[-bch.ecc_bytes:]
        bch.data_len = max_data_len
        nerr = bch.decode(data, ecc)

        bitflips = nerr
        bch.correct(data, ecc)

        newdata = decodefunctions.dec2bin(data[0])
        for e in data[1:]:
            binchar = decodefunctions.dec2bin(e).zfill(8)
            newdata = newdata + binchar

        newdata = newdata.zfill(204)
        correctedbch = ''
        for e in ecc:
            binchar = decodefunctions.dec2bin(e).zfill(8)
            correctedbch = correctedbch + binchar
        newhex = decodefunctions.bin2hex(newdata + correctedbch)
    else:
        bitflips = 0
        newdata = pdf
        correctedbch = bchsgb
        newhex = decodefunctions.bin2hex(pdf + bchsgb)

    return (bitflips, newdata, correctedbch, newhex)
예제 #3
0
def make_full(hexinput):
    pdf1 = decodefunctions.hextobin(hexinput)
    b = pdf1[:204] + '0' * 48
    print(b, len(b))
    ecc = calcBCH(b, 0, 202, 250)
    newhex = decodefunctions.bin2hex(pdf1[:204] + ecc)
    print(newhex)
    print(ecc, decodefunctions.bin2hex(ecc))
    BCH_POLYNOMIAL = 285  # 285
    BCH_BITS = 6
    bitflips = 0
    bch = bchlib.BCH(BCH_POLYNOMIAL, BCH_BITS)

    max_data_len = bch.n // 8 - (bch.ecc_bits + 7) // 8

    data = bytearray(bch1correct.bitstring_to_bytes(pdf1[:204]))
    necc = bch.encode(data)
    bchstring = ''

    for e in necc:
        binchar = decodefunctions.dec2bin(e).zfill(8)
        bchstring = bchstring + binchar
    print(bchstring, decodefunctions.bin2hex(bchstring))

    newhex = decodefunctions.bin2hex(pdf1[:204] + bchstring)

    return newhex
예제 #4
0
def bch1_binarycalc(inputhex):
    try:
        bin = Fcn.hextobin(inputhex)
    except TypeError as err:
        return [inputhex + '  Is not a valid hex']

    result = ''

    if len(inputhex) == 36:
        strhex = inputhex[6:]
        bin = bin[24:]
        result = Fcn.calcbch(bin, "1001101101100111100011", 0, 61, 82)

    elif len(inputhex) == 30:
        strhex = inputhex
        result = Fcn.calcbch(bin, "1001101101100111100011", 0, 61, 82)

    elif len(inputhex) == 63:
        # SGB recalc bch1
        strhex = inputhex
        result = Fcn2.calcBCH(bin[2:], 0, 202, 250)

    else:
        result = 'Invalid Input Hex length of ' + str(
            len(inputhex)) + '.' + ' Valid length of FGB 30/36  or  63 for SGB'

    return result
예제 #5
0
def bch2_binarycalc(inputhex):
    try:
        bin = Fcn.hextobin(inputhex)
    except TypeError as err:
        return [inputhex + '  Is not a valid hex']

    result = ''

    if len(inputhex) == 36:
        strhex = inputhex[6:]
        bin = bin[24:]
        result = Fcn.calcbch(bin, '1010100111001', 82, 108, 120)

    elif len(inputhex) == 30:
        strhex = inputhex
        result = Fcn.calcbch(bin, '1010100111001', 82, 108, 120)

    elif len(inputhex) == 63:
        return ""

    else:
        result = 'Invalid Input Hex length of ' + str(
            len(inputhex)) + '.' + ' Valid lengths of FGB message are 30 or 36'

    return result
예제 #6
0
def random_error(hexvalid, pdf1err, pdf2err, sgberr):
    #pdf1/bc1 errors correctible 3
    #pdf2/bc2 errors correctible 2
    scramble = list(Fcn.hextobin(hexvalid))
    #print(scramble)
    b = Fcn.hextobin(hexvalid)

    if pdf1err or pdf2err:
        for ipos in random.sample(range(0, 82), pdf1err):
            scramble[ipos] = str(int(not int(scramble[ipos])))
        for j in random.sample(range(82, 120), pdf2err):
            scramble[j] = str(int(not int(scramble[j])))

    elif sgberr:
        for i in range(int(sgberr)):
            epos = random.randint(0, 251)
            scramble[epos] = str(int(not int(scramble[epos])))

    return Fcn.bin2hex(''.join(scramble))
예제 #7
0
def test_hex(testhex):

    pdf = decodefunctions.hextobin(testhex)[:204]
    print(pdf)
    print(len(pdf))
    bch = (decodefunctions.hextobin(testhex))[204:]

    print(bch, len(bch))

    bitflips, newpdf, newbch = pdf_to_bchsgb(pdf, bch)
    print('errors result: ', bitflips)
    if bitflips == -1:
        print('fail')
    elif bitflips == 0:
        print('match')
    elif bitflips > 0:
        print(20 * '-' + 'ORIGINAL MESSAGE' + 25 * '-')
        print(testhex)
        print(20 * '-' + 'CORRECTED MESSAGE' + 25 * '-')
        print(decodefunctions.bin2hex(newpdf + newbch))

        print(newpdf + newbch)
예제 #8
0
def bch_recalc(inputhex):
    results = [inputhex]
    preamble = ''
    if len(inputhex) == 36:
        strhex = inputhex[6:]
        preamble = inputhex[:6]
    elif len(inputhex) == 30:
        strhex = inputhex
        preamble = ''
    else:
        type = 'Hex length of ' + str(
            len(inputhex)
        ) + '.' + ' Length of First Generation Beacon Hex Code to test BCH must be 30 or 36'

        return [type]

    try:
        bin = Fcn.hextobin(strhex)
        _pdf1 = (bin)[:61]
        _bch1 = (bin)[61:82]
        _pdf2 = (bin)[82:108]
        _bch2 = (bin)[108:]

        bch1calc = Fcn.calcbch(bin, "1001101101100111100011", 0, 61, 82)
        bch2calc = Fcn.calcbch(bin, '1010100111001', 82, 108, 120)

        if _pdf1 + bch1calc + _pdf2 + bch2calc == bin:
            results.append(
                'bch1 and bch2 recomputed from provided pdf1 and pdf2 match')
        else:
            if bch1calc != _bch1:
                results.append(
                    'bch1 recomputed from provided pdf1 in input message {}'.
                    format(bch1calc))
            if bch2calc != _bch2:
                results.append(
                    'bch2 recomputed from provided pdf2 in input message {}'.
                    format(bch2calc))
            results.append(preamble +
                           Fcn.bin2hex(_pdf1 + bch1calc + _pdf2 + bch2calc))
        return results

    except TypeError as err:
        return [inputhex + '  Is not a valid hex']
예제 #9
0
    bch1= input("bch1: ")
    if not pdf1:
        pdf1='1101001111000100111010110010100000010100000010101010011010000'
        bch1='001000100000000101001'

    bitflips,newpdf,newbch = pdf1_to_bch1(pdf1,bch1)
    print(pdf1)
    print(newpdf,len(newpdf))
    print
    print(bch1)
    print(newbch,len(newbch))
    print(bitflips)
    if bitflips==-1:
        print('fail')

    pdf1=(decodefunctions.hextobin('D3C4EB28140AA681100A444154C224'))[:61]
    bch1=(decodefunctions.hextobin('D3C4EB28140AA681100A444154C224'))[61:82]
    #print('93CB0242508F3BC928BCB407180EC6',pdf1,len(pdf1))
    #print('bch1',bch1,len(bch1))
    pdf1=  (decodefunctions.hextobin('9D6940000045DF533687000452A622'))[:61]
    bch1 = (decodefunctions.hextobin('9D6940000045DF533687000452A622'))[61:82]
    bch1corrected = decodefunctions.calcbch(pdf1,"1001101101100111100011", 0, 61, 82)
    pdf2bch2=(decodefunctions.hextobin('9D6940000045DF533687000452A622'))[82:]
    print(pdf1+bch1,len(pdf1+bch1))
    print(bch1corrected)
    print(pdf1 + bch1 + pdf2bch2, len(pdf1 + bch1+pdf2bch2))
    print(decodefunctions.bin2hex((pdf1 + bch1corrected+pdf2bch2)))



예제 #10
0
    def processHex(self, strhex):
        self.bch1 = self.bch2 = self.tac = 'na'
        self.courseloc = ('na', 'na')
        self.location = ('na', 'na')
        self.fixedbits = ''
        self.hex = str(strhex)
        self.count = 1
        #print strhex
        self._loc = False
        self.tablebin = []
        if Fcn.hextobin(strhex):
            if len(strhex) == 15:
                #   15 Hex does not use bit 25 so an extra 0 needs to be padded
                self.type = '15 Hex ID'
                pad = '0' * 25

            elif len(strhex) == 22:
                self.type = 'Short Message'
                pad = '0' * 24
            elif len(strhex) == 30:
                self.type = 'Long Msg no Framesynch'
                pad = '0' * 24
            elif len(strhex) == 36:
                pad = ''
                self.type = 'Long Msg with Framesynch'
            else:
                self.type = 'Hex length of ' + str(
                    len(strhex)
                ) + '.' + '\nLength of First Generation Beacon Hex Code must be 15, 22 or 30'
                raise HexError('LengthError', self.type)
            self.hexcode = str(strhex)

        else:
            self.type = 'Not a valid Hex ID'
            raise HexError('FormatError', self.type)

        self.bin = '_' + pad + Fcn.hextobin(
            strhex) + (144 - len(pad + Fcn.hextobin(strhex))) * '0'

        if self.bin[17:25] == '11010000':
            self.testmsg = '1'
        else:
            self.testmsg = '0'

        self.bch = Bch(self.bin, self.type)

        self.id = ()

        if self.type != '15 Hex ID':
            formatflag = (self.bin[25], definitions.messagetype[self.bin[25]])
        else:
            formatflag = ('n/a', 'bit 25 not relevant in 15 Hex')

        protocolflag = self.bin[26]
        self.formatflag = formatflag
        self.countrydetail = Country(self.bin[27:37])  #self.country()

        #   protocol == '0' :Standard Location Protocol.
        #   type of location protocol is found at bits 37-40
        self._pflag = ['Location', 'User'][int(self.bin[26])]
        self.tablebin.append(
            ['25', self.bin[25], 'Message format', self.formatflag[1]])
        self.tablebin.append(
            ['26', self.bin[26], 'User or Location Protocol', self._pflag])
        self.tablebin.append(
            ['27-36', self.bin[27:37], 'Country', self.countrydetail.cname])

        if protocolflag == '0':
            self.locationProtocol()

        #   protocol == '1' 'User Protocol'
        #   type of user protocol located at bits 37-39
        elif protocolflag == '1':
            self.userProtocol()
예제 #11
0
        for e in ecc:
            binchar = decodefunctions.dec2bin(e).zfill(8)
            #         #print(e, binchar)
            correctedbch2 = correctedbch2 + binchar
        if (len(correctedbch2)) > 12:
            correctedbch2 = correctedbch2[:12]
    return (bitflips, newdata.zfill(26), correctedbch2)


if __name__ == "__main__":
    pdf2 = input("pdf2: ")
    bch2 = input("bch2: ")
    if not pdf2:

        pdf2 = '00010001000001010101001100'
        bch2 = '001000100100'

    bitflips, newpdf, newbch = pdf2_to_bch2(pdf2, bch2)
    print(pdf2)
    print(newpdf, len(newpdf))
    print(bch2)
    print(newbch)
    print(bitflips)
    if bitflips == -1:
        print('fail')

    pdf2 = (decodefunctions.hextobin('D3C4EB28140AA681100A444154C224'))[82:108]
    bch2 = (decodefunctions.hextobin('D3C4EB28140AA681100A444154C224'))[108:]
    print('D3C4EB28140AA681100A444154C224', pdf2, len(pdf2))
    print('bch2', bch2, len(bch2))
예제 #12
0
        mainmsg='0001110000111101111010001001111111000011111111100111100011111001110010001111101111000100010010000011101101111001001011110000101110110010001000100011001001110110100101000001010001110100010001011110110110'
        bchsgb='011001011000001000000101110011100111001101100010'
        print(mainmsg,bchsgb)
        calculatedBCH = Sgb.calcBCH(mainmsg, 0, 202, 250)
        print(calculatedBCH, len(calculatedBCH),bchsgb==calculatedBCH)
        print(Fcn.bin2hex('00'+mainmsg+calculatedBCH))
        print(len(mainmsg))
        h='070F7A27F0FF9E3E723EF1120EDE4BC2EC888C9DA5051D117B6658205CE7362'
    bitflips,newmsg,newbch = msgbits_to_bch(mainmsg,bchsgb)
    #print(mainmsg)
    #print(newmsg,len(newmsg))
    #print(bchsgb)
    #print(newbch,len(newbch))
    #print(bitflips)
    #if bitflips==-1:
    #    print('fail')

    testhex='00039A3D32618658622811F000000000001FFFF004030680258AB06AAA95EB9'
    m=(Fcn.hextobin(testhex))[2:204]
    b=(Fcn.hextobin(testhex))[204:]
    #print(testhex)
    #print(m,len(m))
    #print('bch',b,len(b))







예제 #13
0
    except:
        return False

    return errors


if __name__ == "__main__":
    strhex = input("Hex message: ")

    errors = []
    #errors = bch_recalc(strhex)

    if errors:
        print(errors)

    _pdf1 = (Fcn.hextobin(strhex))[:61]
    print(len(_pdf1))
    _bch1 = (Fcn.hextobin(strhex))[61:82]
    bitflips1, newpdf1, newbch1 = bch1.pdf1_to_bch1(_pdf1, _bch1)
    _pdf2 = (Fcn.hextobin(strhex))[82:108]
    _bch2 = (Fcn.hextobin(strhex))[108:]
    bitflips2, newpdf2, newbch2 = bch2.pdf2_to_bch2(_pdf2, _bch2)
    if bitflips1 == -1 or bitflips2 == -1:
        print('Too many bit errors to correct')
    elif bitflips1 > 0 or bitflips2 > 0:
        _newbin = newpdf1 + newbch1 + newpdf2 + newbch2
        print(' {} bad pdf1 bit and {} bad pdf2 bit'.format(
            bitflips1, bitflips2))
        print('Corrected Message: {} '.format(Fcn.bin2hex(_newbin)))
        print(_newbin, len(_newbin), len(newpdf1), len(newbch1), len(newpdf2),
              len(newbch2))
예제 #14
0
if __name__ == "__main__":
    pdf1 = input("pdf1: ")
    bch1 = input("bch1: ")
    if not pdf1:
        pdf1 = '1001001111001011000000100100001001010000100011110011101111001'
        bch1 = '001001010001011110010'

    bitflips, newpdf, newbch = pdf1_to_bch(pdf1, bch1)
    print(newpdf)
    print(newbch)
    print(bitflips)
    if bitflips == -1:
        print('fail')

    pdf1 = (decodefunctions.hextobin('93CB0242508F3BC928BCB407180EC6'))[:61]
    bch1 = (decodefunctions.hextobin('93CB0242508F3BC928BCB407180EC6'))[61:82]
    #print('93CB0242508F3BC928BCB407180EC6',pdf1,len(pdf1))
    #print('bch1',bch1,len(bch1))

# if not valid:
#     #errors detected so correct in place
#     print(bch1)
#     ecc_provided_t = bytearray(bitstring_to_bytes(bch1))
#     bchstr=''
#     for e in ecc_provided_t:
#         binchar = decodefunctions.dec2bin(e).zfill(8)
#         bchstr=bchstr+binchar
#         print(binchar)
#     print(bchstr,len(bchstr),decodefunctions.dec2bin(decodefunctions.bin2dec(bchstr)))
#     bch1_recalc=decodefunctions.dec2bin(decodefunctions.bin2dec(bchstr))