Exemplo n.º 1
0
    def predictTest(self, cursor, userId, clf):
        cursor.execute(
            '''
SELECT TestRatings.movieId, ifnull(cast(MoviePopularity.popularity*10 as integer),5), MovieYearGenres.year, genreBits, {0} FROM TestRatings
join MovieYearGenres on TestRatings.movieId=MovieYearGenres.id
join MovieTags on TestRatings.movieId=MovieTags.id
left join MoviePopularity on TestRatings.movieId=MoviePopularity.movieId
where TestRatings.userId=?'''.format(','.join(
                ['tagBits' + str(i) for i in range(self.tagBitsCount)])),
            (userId, ))

        testingData = [
            list(row[0:3]) +
            list(bitstring.Bits(int=row[3], length=len(self.ALL_GENRES))) +
            flatNestList(
                [list(bitstring.Bits(int=b, length=32)) for b in row[4:]])
            for row in cursor.fetchall()
        ]

        testingData = np.array(testingData, dtype='int32')
        predictY = clf.predict(testingData[:, 1:])

        toDB = predictY[:, None]

        toDB = np.insert(toDB, 1, userId, axis=1)
        toDB = np.insert(toDB, 2, testingData[:, 0], axis=1)
        cursor.executemany(
            'update TestRatings set predict=? where userId=? and movieId=?',
            toDB.tolist())
Exemplo n.º 2
0
 def parse(self, bitstrm):
     """
     MSP430 instructions can have one or two extension words for 16 bit immediates
     We therefore extend the normal parsing so that we make sure we can
     get another word if we have to.
     """
     data = Instruction.parse(self, bitstrm)
     data['S'] = None
     data['D'] = None
     # We don't always have a source or destination.
     # Theoretically I could put these in the TypeXInstruction classes, but
     # I'm lazy. Note that we resolve these here, as opposed to later, due to
     # needing to fiddle with the bitstream.
     l.debug(data)
     if 's' in data:
         src_mode = int(data['A'], 2)
         if (src_mode == ArchMSP430.Mode.INDEXED_MODE and data['s'] != '0011') \
                 or (data['s'] == '0000' and src_mode == ArchMSP430.Mode.INDIRECT_AUTOINCREMENT_MODE):
             data['S'] = bitstring.Bits(uint=bitstrm.read('uintle:16'),
                                        length=16).bin
             self.bitwidth += 16
     if 'd' in data:
         dst_mode = int(data['a'], 2)
         if dst_mode == ArchMSP430.Mode.INDEXED_MODE:
             data['D'] = bitstring.Bits(uint=bitstrm.read('uintle:16'),
                                        length=16).bin
             self.bitwidth += 16
     return data
Exemplo n.º 3
0
    def trainClassifier(self, cursor, userId, clf):
        # The default value of MoviePopularity.popularity is 5 because
        # select cast(sum(rating) as real) / count(*) from Ratings
        # returns 0.503819588139731
        cursor.execute(
            '''
SELECT Ratings.rating, ifnull(cast(MoviePopularity.popularity*10 as integer),5), MovieYearGenres.year, genreBits, {0} FROM Ratings
join MovieYearGenres on Ratings.movieId=MovieYearGenres.id
join MovieTags on Ratings.movieId=MovieTags.id
left join MoviePopularity on Ratings.movieId=MoviePopularity.movieId
where Ratings.userId=?'''.format(','.join(
                ['tagBits' + str(i) for i in range(self.tagBitsCount)])),
            (userId, ))

        trainingData = [
            list(row[0:3]) +
            list(bitstring.Bits(int=row[3], length=len(self.ALL_GENRES))) +
            flatNestList(
                [list(bitstring.Bits(int=b, length=32)) for b in row[4:]])
            for row in cursor.fetchall()
        ]

        trainingData = np.array(trainingData, dtype='int32')
        if len(trainingData) == 0:
            raise Exception(
                'User {0} does not appear in training set.'.format(userId))
        y = trainingData[:, 0]
        X = trainingData[:, 1:]
        return clf.fit(X, y)
Exemplo n.º 4
0
    def spi_read(self, length):
        '''Read data from SPI device

        FPGA shall be configured with an appropriate bitstream that implements JTAG-to-SPI bridge using Xilinx BSCAN_SPARTAN6 primitive
        Xilinx USER1 instruction is used to activate this bridge
        Other TAPs in JTAG chain are put in bypass and their bypass registers are loaded with zeros

        Parameter:
        'length' : (int) Number of bytes to read

        Return:
        (list) Read bytes

        '''

        # logger.debug(f'SPI read, {length:d} bytes')

        # Reset TAP and advance TAP to Shift-IR state
        self.owner.jtag_advance(tms=bitstring.Bits('0b00110, 0b11111'))

        # Write USER1 instruction and advance TAP to Exit1-IR state
        self.owner.jtag_shift(data=self.ir_before +
                              self.instructions['user1'] + self.ir_after,
                              length=None,
                              tms=bitstring.Bits('0b1'))

        # Advance TAP to Shift-DR state
        self.owner.jtag_advance(tms=bitstring.Bits('0b0011'))

        # Prepare data
        data_w = bitstring.BitArray(length *
                                    8)  # Dummy zero bits for data read
        data_w.append(
            '0b1'
        )  # Fist bit to be shifted out shall be one. This is needed to activate SPI SS# in JTAG-to-SPI bridge as there might be zeros before it that get loaded into bypass registers of other TAPs
        data_w.prepend(
            '0b0'
        )  # Last bit to be shifted out (dummy bit) is needed in order to shift in the last bit from TDO as BSCAN_SPARTAN6 updates TDO on falling edge (see JTAG-to-SPI bridge FPGA design for details)
        data_w.prepend(self.dr_before)
        data_w.prepend(self.dr_after)

        # Shift data through JTAG-to-SPI bridge and advance TAP to Exit-DR state
        data_r = self.owner.jtag_shift(data=data_w,
                                       length=data_w.length,
                                       tms=bitstring.Bits('0b1'))

        # Advance TAP to Test-Logic-Reset state
        self.owner.jtag_advance(tms=bitstring.Bits('0b1111'))

        data_r = data_r[:length * 8]

        data_r.reverse()

        data_r = list(data_r.bytes)

        # logger.debug(f'OK, {len(data_r):d} bytes: [{", ".join([f"0x{item:02X}" for item in data_r])}]')

        return data_r
Exemplo n.º 5
0
    def test_compressed_message_bits__invalid_length__raises(self):
        # All '1' bits (27 bits)
        bits = bitstring.Bits(bin='0b111111111111111111111111111')
        with self.assertRaises(ValueError):
            protocol.PassthroughSmallMessage(bits)

        # All '1' bits (25 bits)
        bits = bitstring.Bits(bin='0b1111111111111111111111111')
        with self.assertRaises(ValueError):
            protocol.PassthroughSmallMessage(bits)
Exemplo n.º 6
0
 def setUp(self):
     self.helper = TestHelper()
     self.env = simpy.Environment()
     self.network = util.Network(self.env, self.helper.settings)
     self.peer_a_id = bs.Bits(uint=0, length=16)
     self.peer_b_id = bs.Bits(uint=1, length=16)
     self.peer_a = unittest.mock.Mock(spec=peer.Peer)
     self.peer_b = unittest.mock.Mock(spec=peer.Peer)
     self.peer_a_address = self.network.register(self.peer_a)
     self.peer_b_address = self.network.register(self.peer_b)
Exemplo n.º 7
0
def get_spn_bytes(message_data_bitstring, spn, pgn):
    spn_object = get_spn_object(spn)
    spn_length = spn_object["SPNLength"]
    spn_start = lookup_spn_startbit(spn_object, spn, pgn)

    if type(spn_length) is str and spn_length.startswith("Variable"):
        delimiter = spn_object.get("Delimiter")
        pgn_object = get_pgn_object(pgn)
        spn_list = pgn_object["SPNs"]
        if delimiter is None:
            if len(spn_list) == 1:
                spn_end = len(message_data_bitstring.bytes) * 8 - 1
                cut_data = message_data_bitstring[spn_start:spn_end + 1]
                return cut_data
            else:
                print(
                    "Warning: skipping SPN %d in non-delimited and multi-spn and variable-length PGN %d"
                    " (this is most-likely a problem in the JSONdb or source DA)"
                    % (spn, pgn),
                    file=sys.stderr)
                return bitstring.Bits(
                    bytes=b''
                )  # no way to handle multi-spn messages without a delimiter
        else:
            spn_ordinal = spn_list.index(spn)

            delimiter = delimiter.replace('0x', '')
            delimiter = bytes.fromhex(delimiter)
            spn_fields = message_data_bitstring.bytes.split(delimiter)

            if spn_start != -1:  # variable-len field with defined start; must be first variable-len field
                spn_end = len(spn_fields[0]) * 8 - 1
                cut_data = bitstring.Bits(
                    bytes=spn_fields[0])[spn_start:spn_end + 1]
                return cut_data
            else:  # variable-len field with unspecified start; requires field counting
                startbits_list = pgn_object["SPNStartBits"]
                num_fixedlen_spn_fields = sum(1 for s in startbits_list
                                              if s != -1)
                variable_spn_ordinal = spn_ordinal - num_fixedlen_spn_fields
                if num_fixedlen_spn_fields > 0:
                    variable_spn_fields = spn_fields[1:]
                else:
                    variable_spn_fields = spn_fields
                try:
                    cut_data = bitstring.Bits(
                        bytes=variable_spn_fields[variable_spn_ordinal])
                except IndexError:
                    cut_data = bitstring.Bits(bytes=b'')
                return cut_data
    else:
        spn_end = spn_start + spn_length - 1
        cut_data = message_data_bitstring[spn_start:spn_end + 1]
        return cut_data
Exemplo n.º 8
0
 def test_sends_queries(self):
     queried_id = bs.Bits(uint=10, length=16)
     excluded_id = bs.Bits(uint=111, length=16)
     self.network.send_query(self.peer_a_id, self.peer_a_address,
                             self.peer_b_address, queried_id,
                             set((excluded_id, )), 0)
     self.env.run()
     self.peer_b.recv_query.assert_called_with(self.peer_a_id,
                                               queried_id,
                                               0,
                                               excluded_peer_ids=set(
                                                   (excluded_id, )))
Exemplo n.º 9
0
 def transform(self, num):
     # id = (num[0])
     id = bitstring.Bits(uint=num[0], length=16)
     # x = ((num[1]) << 8) | (num[2])
     x = bitstring.Bits(uint=((num[2]) << 8) | (num[1]), length=16)
     # y = ((num[3]) << 8) | (num[4])
     y = bitstring.Bits(uint=((num[4]) << 8) | (num[3]), length=16)
     # z = ((num[5]) << 8) | (num[6])
     z = bitstring.Bits(uint=((num[6]) << 8) | (num[5]), length=16)
     # hsh =num[7]
     hsh = 0x0
     return id.unpack('int')[0], x.unpack('int')[0], y.unpack('int')[0], z.unpack('int')[0], hsh
Exemplo n.º 10
0
    def parse(package, offset):
        name, offset = parse_name(package, offset, '')
        r_type = bitstring.Bits(package)[offset:offset + 16].uint
        offset += 16
        r_class = bitstring.Bits(package)[offset:offset + 16].uint
        offset += 16
        ttl = bitstring.Bits(package)[offset:offset + 32].uint
        offset += 32
        rdlength = bitstring.Bits(package)[offset:offset + 16].uint
        offset += 16
        rdata, offset = parse_address(package, offset, r_type, rdlength)

        return DNSResponse(name, r_type, r_class, ttl, rdlength, rdata), offset
Exemplo n.º 11
0
 def id_with_prefix(self, prefix_str):
     while True:
         prefix = bs.Bits(bin=prefix_str)
         suffix_length = self.settings['id_length'] - len(prefix)
         suffix = bs.Bits(uint=random.randrange(2 ** suffix_length),
                          length=suffix_length)
         peer_id = prefix + suffix
         if peer_id not in self.all_peer_ids:
             self.all_peer_ids.add(peer_id)
             peer_prefix = peer_id[:self.settings['prefix_length']]
             self.all_prefixes.setdefault(peer_prefix, 0)
             self.all_prefixes[peer_prefix] += 1
             return peer_id
def codehuffmantree(huffmantreenode, nodecode):
    """ Traverse Huffman Tree to produce Prefix Codes"""
    #huffmantreenode.print()
    #print("Nodecode = ", nodecode)
    
    if (huffmantreenode.left == [] and huffmantreenode.right == []):
        huffmantreenode.code = nodecode     #no children - assign code
    else:
        leftcode = copy.copy(nodecode)      #append 0 to left
        leftcode.append(bitstring.Bits('0b0'))
        codehuffmantree(huffmantreenode.left,leftcode)
        rightcode = copy.copy(nodecode)     #append 1 to right
        rightcode.append(bitstring.Bits('0b1'))
        codehuffmantree(huffmantreenode.right,rightcode)
Exemplo n.º 13
0
def read_angle():
    with SMBus(1) as bus:
        # todo 1: read registers as blocks of data to make it faster
        # todo 2: use locks to avoid concurrency issues
        # todo 3: check is_ready register
        # todo 4: use more precise values (second component)
        # b1 = bus.read_byte_data(0x18, 0x27, 3)  # status register
        x = bus.read_byte_data(0x18, 0x29)
        y = bus.read_byte_data(0x18, 0x2B)

        x_val = float(bitstring.Bits(uint=x, length=8).unpack('int')[0])
        y_val = float(bitstring.Bits(uint=y, length=8).unpack('int')[0])
        angle = math.degrees(math.atan2(-y_val, x_val))
        # print("({}, {}) -> === {}".format(x_val, y_val, angle))
        return angle
Exemplo n.º 14
0
    def decode(self, arg):
        if type(arg) is bs.Bits:
            if arg.len != 64:
                raise ValueError("Ucodedecoder: Invalid bitstring length %u, only 64 bit are supported" % arg.len)
            bits = arg
        elif type(arg) is int or type(arg) is long:
            bits = bs.Bits(uint=arg, length=64)
        else:
            raise ValueError("Ucodedecoder: Unsupported argument type %s, supported are Bitstring.bits, int and long" % type(arg))
        if not self.plaintext:
            all1 = bs.Bits(uint=0xffffffffffffffff, length=64)
            bits = all1 ^ bits
        opc = Opcode(bits, self.arch)
        if self.arch == "k8":

            opbits = next(bits.cut(9, 1))
            mnem = self.getOp(opbits)
            opc.mnemonic = mnem.mnemonic
            opc.mnemcomment = mnem.comment
            opc.opbits = opbits

            dstbits = next(bits.cut(6, 12))
            reg = self.getReg(dstbits)
            opc.dst = reg
            opc.dstbits = dstbits

            dszbits = next(bits.cut(3, 31))
            dsz = self.getDsz(dszbits)
            opc.dstsize = dsz[0]
            opc.dstzx = dsz[1]
            opc.dszbits = dszbits
            opc.dstregsized = opc.dst.getReg(opc.dstsize)

            immbit = next(bits.cut(1, 40))
            isimm = immbit.all(False)
            opc.immbit = immbit
            opc.immsrc = isimm
            immbits = next(bits.cut(16, 48))
            opc.immbits = immbits
            srcbits = next(bits.cut(6, 49))
            opc.srcbits = srcbits
            if isimm:
                opc.imm = immbits.uint
                opc.srcreg = None
            else:
                opc.srcreg = self.getReg(srcbits)
                opc.imm = None
            return opc
Exemplo n.º 15
0
def num2vec(num):
    rep_sc = str('{:.11E}'.format(num))
    #     print(rep_sc)
    dec_part = int(rep_sc.split("E")[0].replace(".", ""))
    c = 1
    if dec_part < 0:
        c = -1
    dec_part = abs(dec_part)

    exp_part = int(rep_sc.split("E")[1])
    if exp_part < 0:
        exp_pos = 0
        exp_neg = exp_part
    else:
        exp_pos = exp_part
        exp_neg = 0

    exp_pos = abs(exp_pos)
    exp_neg = abs(exp_neg)

    rep_str = str("{:03}{:03}{:012}".format(exp_pos, exp_neg, dec_part))
    #     print(rep_str)

    #     print(dec_part)
    rep_int = int(rep_str) * c
    rep_bin = bitstring.Bits(int=rep_int, length=64).bin
    #     print(rep_bin)

    bin_tensor = torch.tensor(np.array([float(x) for x in rep_bin]))
    return bin_tensor
Exemplo n.º 16
0
 def __wmem2bin(self, mem, fileName, isBinaryString):
     import struct
     import bitstring
     lenExtMemWord = 64
     outFile = open(fileName, "wb")
     for memInd in range(len(mem)):
         if isBinaryString:
             if len(mem[memInd]) > lenExtMemWord:
                 raise "SIMD width needs to suit into 64 bit ExtMemWord for now"
             #pad to machine word
             if len(mem[memInd]) < lenExtMemWord:
                 mem[memInd] = (
                     '0' * (lenExtMemWord - len(mem[memInd]))) + mem[memInd]
             for b in range(lenExtMemWord / 8):
                 subByte = mem[memInd][lenExtMemWord -
                                       (b + 1) * 8:lenExtMemWord - b * 8]
                 outFile.write(struct.pack("B", int(subByte, 2)))
         else:
             weightBin = bitstring.Bits(
                 uint=mem[memInd],
                 length=lenExtMemWord).unpack("bin:" +
                                              str(lenExtMemWord))[0]
             #write byte by byte
             for b in range(lenExtMemWord / 8):
                 subByte = weightBin[lenExtMemWord -
                                     (b + 1) * 8:lenExtMemWord - b * 8]
                 outFile.write(struct.pack("B", int(subByte, 2)))
     outFile.close()
Exemplo n.º 17
0
    def _unpack_imu_channel(self, packet, scale=1):
        """Decode data packet of the accelerometer and gyro (imu) channels.

        Each packet is encoded with a 16bit timestamp followed by 9 samples
        with a 16 bit resolution.
        """
        bit_decoder = bitstring.Bits(bytes=packet)
        pattern = "uint:16,int:16,int:16,int:16,int:16, \
                   int:16,int:16,int:16,int:16,int:16"

        data = bit_decoder.unpack(pattern)

        packet_index = data[0]

        samples = [
            [
                scale * data[index],  # x
                scale * data[index + 1],  # y
                scale * data[index + 2]  # z
            ] for index in [1, 4, 7]
        ]

        # samples is a list with 3 samples
        # each sample is a list with [x, y, z]

        return packet_index, samples
Exemplo n.º 18
0
 def parse(package, offset):
     package_bits = bitstring.Bits(package)
     name, offset = parse_name(package, offset, '')
     r_type = package_bits[offset:offset + 16].uint
     offset += 16
     r_class = package_bits[offset:offset + 16].uint
     offset += 16
     ttl = package_bits[offset:offset + 32].uint
     offset += 32
     date_length = package_bits[offset:offset + 16].uint
     offset += 16
     prime_name_server, offset = parse_name(package, offset, '')
     resp_auth, offset = parse_name(package, offset, '')
     serial_number = package_bits[offset:offset + 32].uint
     offset += 32
     refresh_interval = package_bits[offset:offset + 32].uint
     offset += 32
     retry_interval = package_bits[offset:offset + 32].uint
     offset += 32
     expire_limit = package_bits[offset:offset + 32].uint
     offset += 32
     minimum_ttl = package_bits[offset:offset + 32].uint
     offset += 32
     return DNSAuthority(name, r_type, r_class, ttl, date_length,
                         prime_name_server, resp_auth, serial_number,
                         refresh_interval, retry_interval, expire_limit,
                         minimum_ttl), offset
Exemplo n.º 19
0
def _bitarray_to_message(barr):
    """Decodes a bitarray with length multiple of 5 to a byte message (removing the padded zeros if found)."""
    padding_len = len(barr) % 8
    if padding_len > 0:
        return bitstring.Bits(bin=barr.bin[:-padding_len]).bytes
    else:
        return barr.bytes
Exemplo n.º 20
0
 def testReadingLines(self):
     s = b"This is a test\nof reading lines\nof text\n"
     b = CBS(bytes=s)
     n = bitstring.Bits(bytes=b'\n')
     self.assertEqual(b.readto(n).bytes, b'This is a test\n')
     self.assertEqual(b.readto(n).bytes, b'of reading lines\n')
     self.assertEqual(b.readto(n).bytes, b'of text\n')
Exemplo n.º 21
0
Arquivo: loader.py Projeto: jm-/casint
    def _read_header(self, fp):
        header_bytes = fp.read(32)
        header_bits = bitstring.Bits(bytes=header_bytes)

        header_i_bits = ~header_bits
        header_i_bytes = header_i_bits.tobytes()

        (file_identifier, file_type_identifier, magic_sequence_1,
         control_byte_1, magic_sequence_2, total_file_size, control_byte_2,
         reserved_sequence_1,
         num_items) = struct.unpack('>8sB5sB1sIB9sH', header_i_bytes)

        if self.debug:
            print(f'file_identifier={file_identifier}')
            print(f'file_type_identifier={file_type_identifier}')
            print(f'total_file_size={total_file_size}')
            print(f'num_items={num_items}')

        # validate the control bytes
        lsb = total_file_size % 256
        assert (lsb + 0x41) % 256 == control_byte_1
        assert (lsb + 0xb8) % 256 == control_byte_2

        # validate magic sequences
        assert magic_sequence_1 == b'\x00\x10\x00\x10\x00'
        assert magic_sequence_2 == b'\x01'

        return num_items
Exemplo n.º 22
0
    def _decode_bitstring(self):
        """ Decode a 'bitstring' object from a 'png' or 'bmp' image

        Returns:
        bitstring -- binary representation of decoded file
        """
        def image_loop(end):
            """ Loop through image pixels to obtain 'bitstring' object

            Keyword arguments:
            end -- How many bits to decode

            Returns:
            str -- binary
            """
            data = ''
            current_length = 0
            for row in range(self.width):
                for col in range(self.height):
                    for cha in range(self.channels):
                        if current_length == end:
                            return data
                        data += str(self._get_lsb(self.pixels[row, col][cha]))
                        current_length += 1

        bstring_length = ((int(image_loop(32), 2)) + 32)
        bstring = image_loop(bstring_length)

        return bitstring.Bits('0b' + bstring[32:])
Exemplo n.º 23
0
def encode():
    st = bitstring.ConstBitStream(filename=sys.argv[2])
    f2 = open(sys.argv[3], 'w')
    first_code = True

    def write_b(b):
        nonlocal first_code
        nonlocal f2
        if first_code:
            f2.write(b)
            first_code = False
        else:
            f2.write(' ' + b)

    while True:
        try:
            a = st.read(18).uint
            b = num2word[a]
            write_b(b)
        except bitstring.ReadError:
            tail = st[st.pos:] + '0b1'
            if tail.len < 18:
                tail += bitstring.Bits(18 - tail.len)
            b = num2word[tail.uint]
            write_b(b)
            break
    f2.close()
Exemplo n.º 24
0
 def gen_bin_data(self, trace):
     """Generate the raw binary data for compression"""
     linearized = spintolinear.linearize_frames(trace, self.layout) + 1
     if self.packingType == "bit":
         return bitstring.Bits(linearized).tobytes()
     else:
         return linearized.tobytes()
Exemplo n.º 25
0
def _to_ecmdDataBuffer(buf, defwidth=64):
    if isinstance(buf, _int_types):
        # negative numbers in Python start with infinite one bits, so strip off infinity minus 64 ;)
        buf = buf & ((1 << defwidth) - 1)

        ecmd_buf = ecmd.ecmdDataBuffer(defwidth)
        if defwidth == 64:
            ecmd_buf.setDoubleWord(0, buf)
        elif defwidth == 32:
            ecmd_buf.setWord(0, buf)
        else:
            raise NotImplementedError()

        return ecmd_buf

    if isinstance(buf, bitstring.Bits):
        ecmd_buf = ecmd.ecmdDataBuffer(len(buf))
        if len(buf):
            buf_bytes = bytearray(buf.tobytes())
            ecmd_buf.memCopyIn(buf_bytes, len(buf_bytes))

        return ecmd_buf

    if isinstance(buf, _str_types):
        bits = bitstring.Bits(buf)
        return _to_ecmdDataBuffer(bits)

    return buf
Exemplo n.º 26
0
 def parse(self, bitstrm):
     numbits = len(self.bin_format)
     if self.arch.instruction_endness == 'Iend_LE':
         # This arch stores its instructions in memory endian-flipped compared to the ISA.
         # To enable natural lifter-writing, we let the user write them like in the manual, and correct for
         # endness here.
         instr_bits = bitstring.Bits(uint=bitstrm.peek("uintle:%d" %
                                                       numbits),
                                     length=numbits).bin
     else:
         instr_bits = bitstrm.peek("bin:%d" % numbits)
     data = {c: '' for c in self.bin_format if c in string.ascii_letters}
     for c, b in zip(self.bin_format, instr_bits):
         if c in '01':
             if b != c:
                 raise ParseError(
                     'Mismatch between format bit %c and instruction bit %c'
                     % (c, b))
         elif c in string.ascii_letters:
             data[c] += b
         else:
             raise ValueError('Invalid bin_format character %c' % c)
     # Hook here for extra matching functionality
     if hasattr(self, 'match_instruction'):
         # Should raise if it's not right
         self.match_instruction(data, bitstrm)
     # Use up the bits once we're sure it's right
     self.rawbits = bitstrm.read('hex:%d' % numbits)
     # Hook here for extra parsing functionality (e.g., trailers)
     if hasattr(self, '_extra_parsing'):
         data = self._extra_parsing(data, bitstrm)
     return data
Exemplo n.º 27
0
 def parse_from(data):
     bit_data = bitstring.Bits(data)
     # current_index = 0
     header = CacheParser.parse_header(bit_data)
     current_index = 96
     queries = []
     answers = []
     authority_answers = []
     additional_answers = []
     for i in range(header["Questions"]):
         query, index = CacheParser.parse_query(bit_data, current_index)
         queries.append(query)
         current_index = index
     for i in range(header["Answers"]):
         answer, index = CacheParser.parse_answer(bit_data, current_index)
         answers.append(answer)
         current_index = index
     for i in range(header["Authority"]):
         authority_answer, index = CacheParser.parse_answer(
             bit_data, current_index)
         authority_answers.append(authority_answer)
         current_index = index
     for i in range(header["Additional"]):
         additional_answer, index = CacheParser.parse_answer(
             bit_data, current_index)
         additional_answers.append(additional_answer)
         current_index = index
     return header, queries, answers, authority_answers, additional_answers
Exemplo n.º 28
0
def decode():
    f1 = open(sys.argv[2], 'r', encoding='latin1')
    f2 = open(sys.argv[3], 'wb')
    st = bitstring.BitArray()
    cw = ''
    while True:
        buf = f1.read(512)
        for c in buf:
            b = ord(c.upper())
            if (b >= ord('A')) and (b <= ord('Z')):
                cw += c.upper()
                if len(cw) == 5:
                    if cw not in word2num:
                        print('Wrong codeword: ' + cw)
                        exit(2)
                    if st.len >= 4096:
                        st[:4096].tofile(f2)
                        del st[:4096]
                    st.append(bitstring.Bits(uint=word2num[cw], length=18))
                    cw = ''
        if len(buf) != 512:
            break
    st[:st.rfind('0b1')[0]].tofile(f2)
    f1.close()
    f2.close()
Exemplo n.º 29
0
 def parse(self, bitstrm):
     beforebits = bitstrm.bitpos
     numbits = len(self.bin_format)
     if self.irsb_c.irsb.arch.memory_endness == 'Iend_LE':
         # Get it out little endian.  I hate this.
         instr_bits = bitstring.Bits(uint=bitstrm.peek("uintle:%d" %
                                                       numbits),
                                     length=numbits).bin
     else:
         instr_bits = bitstrm.peek("bin:%d" % numbits)
     data = {c: '' for c in self.bin_format if c in string.ascii_letters}
     for c, b in zip(self.bin_format, instr_bits):
         if c in '01':
             if b != c:
                 raise ParseError(
                     'Mismatch between format bit %c and instruction bit %c'
                     % (c, b))
         elif c in string.ascii_letters:
             data[c] += b
         else:
             raise ValueError('Invalid bin_format character %c' % c)
     # Hook here for extra matching functionality
     if hasattr(self, 'match_instruction'):
         # Should raise if it's not right
         self.match_instruction(data, bitstrm)
     # Use up the bits once we're sure it's right
     self.rawbits = bitstrm.read('hex:%d' % numbits)
     # Hook here for extra parsing functionality (e.g., trailers)
     if hasattr(self, '_extra_parsing'):
         data = self._extra_parsing(data, bitstrm)
     afterbits = bitstrm.bitpos
     self.bitwidth = afterbits - beforebits
     return data
Exemplo n.º 30
0
    def read(self):
        '''Read data from user read register

        FPGA shall be configured with an appropriate bitstream that implements 'jtag_register_read' module using Xilinx BSCAN_SPARTAN6 primitive
        Xilinx USER4 instruction is used

        Other TAPs in JTAG chain are put in bypass and their bypass registers are loaded with zeros

        Parameter:
        None

        Return:
        None

        '''

        # logger.debug('{description}, register read')

        # Reset TAP and advance TAP to Shift-IR state
        self.owner.jtag_advance(tms=bitstring.Bits('0b00110, 0b11111'))

        # Write USER4 instruction and advance TAP to Exit1-IR state
        self.owner.jtag_shift(data=self.ir_before +
                              self.instructions['user4'] + self.ir_after,
                              length=None,
                              tms=bitstring.Bits('0b1'))

        # Advance TAP to Shift-DR state
        self.owner.jtag_advance(tms=bitstring.Bits('0b0011'))

        # Shift data through and advance TAP to Exit-DR state
        data_r = self.owner.jtag_shift(data=None,
                                       length=self.dr_before.length +
                                       self.read_register.length +
                                       self.dr_after.length,
                                       tms=bitstring.Bits('0b1'))

        # Advance TAP to Test-Logic-Reset state
        self.owner.jtag_advance(tms=bitstring.Bits('0b1111'))

        self.read_register.value = data_r[self.dr_before.
                                          length:self.dr_before.length +
                                          self.read_register.length]

        # logger.debug('OK')

        return None