コード例 #1
0
ファイル: container.py プロジェクト: LucaBongiorni/kitty
 def render(self, ctx=None):
     '''
     :param ctx: rendering context in which the method was called
     :rtype: `Bits`
     :return: rendered value of the container
     '''
     self._initialize()
     if ctx is None:
         ctx = RenderContext()
     ctx.push(self)
     self.set_offset(self._offset, ctx)
     if self.is_default():
         self._current_rendered = self._default_rendered
         ctx.pop()
         return self._default_rendered
     rendered = BitArray()
     offset = 0
     for field in self._fields:
         frendered = field.render(ctx)
         if not isinstance(frendered, Bits):
             raise KittyException('the field %s:%s was rendered to type %s, you should probably wrap it with appropriate encoder' % (
                 field.get_name(), type(field), type(frendered)))
         rendered.append(frendered)
         offset += len(frendered)
     self.set_current_value(rendered)
     ctx.pop()
     return self._current_rendered
コード例 #2
0
    def address_decode(self, address: str) -> str:
        """Given a string containing an XRB/NANO/BAN address, confirm validity and provide resulting hex address"""
        if (address[:4] == 'xrb_' or address[:5] == 'nano_'
                and not self.banano_mode) or (address[:4] == 'bcb_'
                                              and self.banano_mode):
            account_map = "13456789abcdefghijkmnopqrstuwxyz"  # each index = binary value, account_lookup[0] == '1'
            account_lookup = {}
            for i in range(
                    0, 32
            ):  # populate lookup index with prebuilt bitarrays ready to append
                account_lookup[account_map[i]] = BitArray(uint=i, length=5)
            data = address.split('_')[1]
            acrop_key = data[:
                             -8]  # we want everything after 'xrb_' or 'nano_' but before the 8-char checksum
            acrop_check = data[-8:]  # extract checksum

            # convert base-32 (5-bit) values to byte string by appending each 5-bit value to the bitstring,
            # essentially bitshifting << 5 and then adding the 5-bit value.
            number_l = BitArray()
            for x in range(0, len(acrop_key)):
                number_l.append(account_lookup[acrop_key[x]])

            number_l = number_l[
                4:]  # reduce from 260 to 256 bit (upper 4 bits are never used as account is a uint256)
            check_l = BitArray()

            for x in range(0, len(acrop_check)):
                check_l.append(account_lookup[acrop_check[x]])
            check_l.byteswap()  # reverse byte order to match hashing format
            result = number_l.hex.upper()
            return result

        return False
コード例 #3
0
def main():
    """
    1. Get file.
    2. Convert from base64 to hex string representation.
    3. Convert hex string representation to BitArray.
    4. Feed into Repeating_XOR_Breaker
    """
    cipher_source = "https://cryptopals.com/static/challenge-data/6.txt"
    cipher_reader = Data_reader(cipher_source)

    data = cipher_reader.get_data()
    data = ''.join(data)
    converted = Base64_To_Hex(data).convert()
    data_to_break = BitArray(hex=converted)
    breaker = Repeating_XOR_Breaker(data_to_break)

    candidate_keys = breaker.solve()

    print "Candidate Keys:", candidate_keys

    for key_set in candidate_keys:
        whole_key = BitArray(hex='0x00')
        for key in key_set:
            whole_key.append(BitArray(int=key, length=8)) 

        print "whole_key:", whole_key
        decrypter = ExtendedKeyDecrypter()
        decrypter.set_decrypt_key(whole_key.hex)
        print decrypter.decrypt()
コード例 #4
0
        def twentyFourBitValues(color):

            outgoingData = BitArray()
            for colorChannel in color:
                outgoingData.append(BitArray(uint=colorChannel, length=8))

            return outgoingData
コード例 #5
0
ファイル: Generator.py プロジェクト: OSSDC/Octavo
 def branch_to_binary(self, bdo, branch, configuration):
     # Processed here instead of in Resolver since it depends on binary values.
     if branch.prediction == configuration.branch_label_not_taken:
         predict = bdo.predict_not_taken
         predict_enable = bdo.predict_enabled
     elif branch.prediction == configuration.branch_label_taken:
         predict = bdo.predict_taken
         predict_enable = bdo.predict_enabled
     elif branch.prediction == configuration.branch_label_none:
         predict = bdo.predict_not_taken
         predict_enable = bdo.predict_disabled
     else:
         print(
             "Invalid branch prediction setting {0} on branch {1}.".format(
                 branch.prediction, branch))
         self.ask_for_debugger()
     # ECL FIXME We don't have a way to disable branch origin yet in the assembly source. See DESIGN_NOTES TODO.
     origin_enable = bdo.origin_enabled
     origin = BitArray(uint=branch.origin, length=bdo.origin_width)
     destination = BitArray(uint=branch.destination,
                            length=bdo.destination_width)
     condition = self.condition_to_binary(bdo, branch)
     branch_bits = BitArray()
     # Field order must match hardware
     for entry in [
             origin, origin_enable, destination, predict, predict_enable,
             condition
     ]:
         branch_bits.append(entry)
     return branch_bits
コード例 #6
0
def lzwv_encode(in_bytes):
    max_entries = 2**MAX_CODE_LEN
    in_array = bytearray(in_bytes)
    dictionary = {bytes([i]): i for i in range(256)}
    code_len = 9
    out_array = BitArray()
    while len(in_array) > 1:
        # Find string s that's not in the dictionary
        s = b''
        while (s in dictionary or s == b'') and len(in_array) > 0:
            s = s + bytes([in_array.pop(0)])
        in_array.insert(0, s[-1])

        # Emit code for the s[:-1], which is in the dictionary
        out_array.append(Bits(uint=dictionary[s[:-1]], length=code_len))

        # If there's room in the dictionary, add s
        c = len(dictionary)
        if c < max_entries:
            dictionary[s] = c
            # If just added max value for code len, increment it.
            if c == 2**code_len - 1:
                code_len = min(MAX_CODE_LEN, code_len + 1)
    # Emit code for final value
    out_array.append(
        Bits(uint=dictionary[bytes([in_array.pop(0)])], length=code_len))
    return out_array.tobytes()
コード例 #7
0
def toCharset(charset, string, toUpper=False):
    """Convert unicode text to various charsets using a lookup table

	Args:
		charset (string): lookup table
		string (string): unicode string to convert
		toUpper (bool, optional): Make chars uppercase before converting
		(intended for 6 bit charsets or charsets that do not support uppercase).
		Defaults to False.

	Returns:
		bytes: sequence of bytes (split into bits of length
	"""
    asciibits = BitArray()
    cut = int(8 - log2(len(charset)))
    if toUpper:
        chars = chars.upper()
    for char in string:
        bits = charset.find(char)
        if bits < 0:
            if charset == GOST:
                bits = 25  # use '*' for GOST
            else:
                bits = charset.find("?")
        asciibits.append(BitArray(bytes([bits]))[cut:])
    return asciibits.tobytes()
コード例 #8
0
def main(top_block_cls=top_block, options=None):

    tb = top_block_cls()
    tb.start()
    tb.wait()

    print("Looking for flag")

    stream = BitArray()
    with open(os.getenv("DIR", "/data") + os.sep + "output.txt", "rb") as f:
        byte = f.read(1)
        while byte:
            if byte == b'\x01':
                stream.append('0b1')
            else:
                stream.append('0b0')
            byte = f.read(1)

    # look for the start of the flag in the stream of bits
    pos = stream.find('0x666c6167')

    if len(pos) > 0:
        stream <<= pos[0]

        bitlen = len(stream)

        print(stream[0:bitlen / 8 * 8].bytes)
    else:
        print("Did not find the flag")
コード例 #9
0
ファイル: huffman.py プロジェクト: tytarsi/compression
    def encode(self, outfile):
        # calculate frequencies
        for char in self.text:
            self.frequencies[char] += 1

        # construct huffman tree
        for k, v in self.frequencies.items():
            self.pq.put(Leaf(k, v))
        for _ in range(self.pq.qsize() - 1):
            min1 = self.pq.get()
            min2 = self.pq.get()
            new_node = Branch(min1, min2)
            self.pq.put(new_node)

        tree = self.pq.get()

        # put encodings into a dict for each char, and flip for decodings
        self._get_encodings(tree)
        self.decodings = {str(v.bin): k for k, v in self.encodings.items()}

        # generate output bit array
        bits_out = BitArray(bin="")
        for char in self.text:
            bits_out.append(self.encodings[char])

        with open(outfile, 'wb') as f:
            bits_out.tofile(f)

        return bits_out
コード例 #10
0
ファイル: RTCM_decode.py プロジェクト: AlessioMorale/pyUblox
    def get_word(self, allow_recalc=False):
        word = BitArray(0)
        for i in range(5):
            b = self.buf.popleft()

            if b >> 6 != 1:
                pass
                #print("6-of-8 decode wrong")
                #raise RTCMBitError()

            b = BitArray(uint=(b&0x3f), length=6)
            b.reverse()
            word.append(b)

        if self.p30:
            word ^= BitArray(uint=0x3fffffc0, length=30)

        print(hex(word.uint))

        if allow_recalc and self.calculate_parity(word) != word.uint & 0x3f:
            self.p29 = 1

        if self.calculate_parity(word) != word.uint & 0x3f:
            raise RTCMParityError()

        self.p30 = word.uint & 1
        self.p29 = (word.uint & 2) >> 1

        return word
コード例 #11
0
ファイル: Generator.py プロジェクト: lcyls/Octavo
 def pointer_to_binary(self, pointer, configuration, thread=None):
     """Convert a Pointer object to the binary init load value for a Programmed Offset entry."""
     # Add the Default Offset for each thread, so we point to the correct per-thread instance of the pointed-to variable
     # None thread means a shared variable holds the init data, or other unique location identical to all threads.
     if thread is not None:
         default_offset = configuration.default_offset.offsets[thread]
     else:
         default_offset = 0
     # Addresses wrap around when the offset is added,
     # so express negative values as the modular sum value
     # Read and write pointers have different address ranges and offset bit widths
     if "D" in pointer.memory:
         # write pointer
         offset = (pointer.base + pointer.offset - pointer.address +
                   default_offset) % configuration.memory_depth_words_write
         offset = BitArray(uint=offset,
                           length=configuration.po_write_offset_bit_width)
     else:
         # read pointer
         offset = (pointer.base + pointer.offset - pointer.address +
                   default_offset) % configuration.memory_depth_words
         offset = BitArray(uint=offset,
                           length=configuration.po_read_offset_bit_width)
     # The increment is a signed magnitude number (absolute value and sign bit)
     increment = BitArray(uint=abs(pointer.incr),
                          length=configuration.po_increment_bits)
     increment_sign = self.to_sign_bit(pointer.incr)
     increment_sign = BitArray(uint=increment_sign,
                               length=configuration.po_increment_sign_bits)
     # Now pack them into the Programmed Offset entry binary configurations
     pointer_bits = BitArray()
     for entry in [increment_sign, increment, offset]:
         pointer_bits.append(entry)
     return pointer_bits
コード例 #12
0
ファイル: packet.py プロジェクト: dcrowder2/tftp
	def check_checksum(checksum, packet):
		all_words = packet[:128]
		# removing the checksum from the packet for the calculation of the checksum, since it wasn't in the original
		# calculation for the checksum
		all_words.append(Bits('0b00000000000000000000000000000000'))
		# and then all the data
		all_words.append(packet[160:])
		end_of_word = 16
		summation = (~all_words[:end_of_word]).uint

		for i in range((len(all_words) // 16) - 1):
			start_of_word = end_of_word
			end_of_word += 16
			summation += (~all_words[start_of_word:end_of_word]).uint
		# If there is more bits that are a full word we need to add them as well
		if len(all_words[end_of_word:]) > 0:
			final_word = BitArray(16 - len(all_words[end_of_word:]))
			final_word.append(all_words[end_of_word:])
			summation += (~final_word).uint

		bin_sum = Bits(bin(summation))

		while len(bin_sum) > 16:
			over_hang = bin_sum[:(len(bin_sum) - 16)].uint
			left_over = bin_sum[(len(bin_sum) - 16):].uint
			bin_sum = Bits(bin(over_hang + left_over))

		if len(bin_sum) < 16:
			temp_sum = BitArray(16 - len(bin_sum))
			temp_sum.append(bin_sum)
			bin_sum = temp_sum
		calculated_checksum = ~bin_sum

		return calculated_checksum == checksum
コード例 #13
0
    def get_word(self, allow_recalc=False):
        word = BitArray(0)
        for i in range(5):
            b = self.buf.popleft()

            if b >> 6 != 1:
                pass
                #print("6-of-8 decode wrong")
                #raise RTCMBitError()

            b = BitArray(uint=(b & 0x3f), length=6)
            b.reverse()
            word.append(b)

        if self.p30:
            word ^= BitArray(uint=0x3fffffc0, length=30)

        print(hex(word.uint))

        if allow_recalc and self.calculate_parity(word) != word.uint & 0x3f:
            self.p29 = 1

        if self.calculate_parity(word) != word.uint & 0x3f:
            raise RTCMParityError()

        self.p30 = word.uint & 1
        self.p29 = (word.uint & 2) >> 1

        return word
コード例 #14
0
 def render(self, ctx=None):
     '''
     :param ctx: rendering context in which the method was called
     :rtype: `Bits`
     :return: rendered value of the container
     '''
     self._initialize()
     render_count = 1
     if ctx is None:
         ctx = RenderContext()
         if self._need_second_pass:
             render_count = 2
     ctx.push(self)
     if self.is_default():
         self._current_rendered = self._default_rendered
     else:
         if self.offset is None:
             self.offset = 0
         for i in range(render_count):
             offset = self.offset
             rendered = BitArray()
             for field in self._fields:
                 field.set_offset(offset)
                 frendered = field.render(ctx)
                 if not isinstance(frendered, Bits):
                     raise KittyException('the field %s:%s was rendered to type %s, you should probably wrap it with appropriate encoder' % (
                         field.get_name(), type(field), type(frendered)))
                 rendered.append(frendered)
                 offset += len(frendered)
             self.set_current_value(rendered)
     ctx.pop()
     return self._current_rendered
コード例 #15
0
class Encryptor():
    def __init__(self, key):
        self.key = BitArray(hex=key.encode('hex'))
        self.data_to_encrypt = None
        self.result_cipher = BitArray(bin='0')

    def _l_padding(self, data_stream):
        stream = BitArray(hex=data_stream.encode('hex'))
        stream_len = stream.len
        while stream_len % self.key.len:
            stream = BitArray(bin='0') + stream
            stream_len = stream.len

        return stream

    def _pad_to_hex_size(self):
        stream = self.result_cipher
        stream_len = stream.len
        while stream_len % 8:
            stream = BitArray(bin='0') + stream
            stream_len = stream.len

        self.result_cipher = stream

    def encrypt(self, data):
        self.data_to_encrypt = self._l_padding(data)
        skip_by = self.key.len
        for pos in xrange(0, self.data_to_encrypt.len, skip_by):
            result = self.data_to_encrypt[pos:skip_by] ^ self.key
            self.result_cipher.append(result)
            skip_by += self.key.len

        self._pad_to_hex_size()

        return self.result_cipher.hex
コード例 #16
0
ファイル: fields.py プロジェクト: ESP32DE/esptool
 def get_coding_scheme_warnings(self, silent=False):
     """ Check if the coding scheme has detected any errors. """
     old_addr_reg = 0
     reg_value = 0
     ret_fail = False
     for block in self.blocks:
         if block.id == 0:
             words = [
                 self.read_reg(self.REGS.EFUSE_RD_REPEAT_ERR0_REG +
                               offs * 4) for offs in range(5)
             ]
             data = BitArray()
             for word in reversed(words):
                 data.append("uint:32=%d" % word)
             # pos=32 because EFUSE_WR_DIS goes first it is 32bit long and not under error control
             block.err_bitarray.overwrite(data, pos=32)
             block.num_errors = block.err_bitarray.count(True)
             block.fail = block.num_errors != 0
         else:
             addr_reg, err_num_mask, err_num_offs, fail_bit = self.REGS.BLOCK_ERRORS[
                 block.id]
             if err_num_mask is None or err_num_offs is None or fail_bit is None:
                 continue
             if addr_reg != old_addr_reg:
                 old_addr_reg = addr_reg
                 reg_value = self.read_reg(addr_reg)
             block.fail = reg_value & (1 << fail_bit) != 0
             block.num_errors = (reg_value >> err_num_offs) & err_num_mask
         ret_fail |= block.fail
         if not silent and (block.fail or block.num_errors):
             print("Error(s) in BLOCK%d [ERRORS:%d FAIL:%d]" %
                   (block.id, block.num_errors, block.fail))
     if (self.debug or ret_fail) and not silent:
         self.print_status_regs()
     return ret_fail
コード例 #17
0
def mimeencode(s, debug=False):
	if type(s) is not bytearray and type(s) is not bytes:
		s = bytearray(s, 'utf-8')
	s = BitArray(s)
	s = list(s)
	pad = len(s)
	while len(s)%24 != 0:
		s.append(False)
	s_6bits = []
	padded = []#list of chars resulting entirely from padding
	j = 0
	for i in range(0, len(s), 6):
		s_6bits.append(s[i:i+6])
		if i >= pad - 1:
			padded.append(j)
		j += 1
	encoded = ""
	for i in range(len(s_6bits)):
		if i not in padded:
			a = ""
			for j in range(len(s_6bits[i])):
				a += str(int(s_6bits[i][j]))
			a = int(a, 2)
			encoded += mimetable[a]
		else:
			encoded += "="
	return bytes(encoded, 'utf-8')
コード例 #18
0
def print_message(msg: can.Message):
    global txt
    frameId = msg.data[0]
    frameCount = Bits(int=int(frameId), length=8).hex[0]
    frameNumber = Bits(int=int(frameId), length=8).hex[1]

    frame = BitArray()
    for x in range(6):
        x = x + 2
        frame.append(Bits(uint=int(msg.data[x]), length=8))

    txt = txt + frame.bin

    if frameNumber == frameCount:
        instrumentPanelWindow.clear()
        text = TextDecoder.decode(txt)
        fragments = text.split("\n")
        # print(fragments)

        row = 1
        for fragment in fragments:
            instrumentPanelWindow.addstr(row, 1, ' ' + fragment)
            # print(fragment)
            # print(row)
            row += 1
        instrumentPanelWindow.border()
        instrumentPanelWindow.refresh()
        # print(TextDecoder.decode(txt))
        txt = ''
コード例 #19
0
    def get_bytes(self):

        bitstream = BitArray()
        current_address = self.imagebase
        labels_to_addresses_map = {}

        for ast_node in self.ast:
            ast_node.set_node_bitfields(self.compute_node_bitfields(ast_node))
            ast_node.set_node_address(current_address)
            for lbl in ast_node.labels:
                labels_to_addresses_map[lbl] = ast_node.address
            # TODO: What happens with addressing in non-standard word sizes?
            bit_length = self.bitfields_to_bitarray(
                ast_node.node_bitfields).length
            byte_length = int(bit_length / DEFAULT_BYTE_BITSIZE)
            if bit_length % DEFAULT_BYTE_BITSIZE != 0:
                byte_length += 1
            current_address += byte_length

        for ast_node in self.ast:
            self.update_label_placeholders(ast_node, labels_to_addresses_map)

        for ast_node in self.ast:
            ast_node.set_node_bitfields(self.compute_node_bitfields(ast_node))
            node_bitarray = self.bitfields_to_bitarray(ast_node.node_bitfields)
            bitstream.append(node_bitarray)

        return bitstream.tobytes()
コード例 #20
0
def bytes_from_mnemonic(mnemonic_str: str) -> bytes:
    mnemonic: List[str] = mnemonic_str.split(" ")
    if len(mnemonic) not in [12, 15, 18, 21, 24]:
        raise ValueError("Invalid mnemonic length")

    word_list = {
        word: i
        for i, word in enumerate(bip39_word_list().splitlines())
    }
    bit_array = BitArray()
    for i in range(0, len(mnemonic)):
        word = mnemonic[i]
        if word not in word_list:
            raise ValueError(
                f"'{word}' is not in the mnemonic dictionary; may be misspelled"
            )
        value = word_list[word]
        bit_array.append(BitArray(uint=value, length=11))

    CS: int = len(mnemonic) // 3
    ENT: int = len(mnemonic) * 11 - CS
    assert len(bit_array) == len(mnemonic) * 11
    assert ENT % 32 == 0

    entropy_bytes = bit_array[:ENT].bytes
    checksum_bytes = bit_array[ENT:]
    checksum = BitArray(std_hash(entropy_bytes))[:CS]

    assert len(checksum_bytes) == CS

    if checksum != checksum_bytes:
        raise ValueError("Invalid order of mnemonic words")

    return entropy_bytes
コード例 #21
0
ファイル: Generator.py プロジェクト: laforest/Octavo
 def pointer_to_binary (self, pointer, configuration, thread = None):
     """Convert a Pointer object to the binary init load value for a Programmed Offset entry."""
     # Add the Default Offset for each thread, so we point to the correct per-thread instance of the pointed-to variable
     # None thread means a shared variable holds the init data, or other unique location identical to all threads.
     if thread is not None:
         default_offset = configuration.default_offset.offsets[thread]
     else:
         default_offset = 0
     # Addresses wrap around when the offset is added, 
     # so express negative values as the modular sum value
     # Read and write pointers have different address ranges and offset bit widths
     if "D" in pointer.memory:
         # write pointer
         offset = (pointer.base + pointer.offset - pointer.address + default_offset) % configuration.memory_depth_words_write
         offset = BitArray(uint=offset, length=configuration.po_write_offset_bit_width) 
     else:
         # read pointer
         offset = (pointer.base + pointer.offset - pointer.address + default_offset) % configuration.memory_depth_words
         offset = BitArray(uint=offset,  length=configuration.po_read_offset_bit_width) 
     # The increment is a signed magnitude number (absolute value and sign bit)
     increment      = BitArray(uint=abs(pointer.incr), length=configuration.po_increment_bits)
     increment_sign = self.to_sign_bit(pointer.incr)
     increment_sign = BitArray(uint=increment_sign, length=configuration.po_increment_sign_bits)
     # Now pack them into the Programmed Offset entry binary configurations
     pointer_bits = BitArray()
     for entry in [increment_sign, increment, offset]:
         pointer_bits.append(entry)
     return pointer_bits
コード例 #22
0
ファイル: player.py プロジェクト: mrevilme/Dota2Discord
 def team(self):
     c = BitArray()
     c.append("uint:8=%s" % self.player_slot)
     if c[0] == 0:
         return "radiant"
     else:
         return "dire"
コード例 #23
0
    def compress(self, charstring):
        out = BitArray()
        floor = 0
        ceiling = 1

        self.lm.refresh()

        for char in charstring:
            probs = self.lm.get_probs()
            i = charsetd[char]
            newfloor = sum(probs[:i])
            newceiling = newfloor + probs[i]

            d = ceiling - floor
            newfloor = floor + (d * newfloor)
            newceiling = floor + (d * newceiling)

            while newfloor >= .5 or newceiling < .5:
                if newceiling < .5:
                    out.append("0b0")
                    newfloor = newfloor * 2
                    newceiling = newceiling * 2
                if newfloor >= .5:
                    out.append("0b1")
                    newfloor = (newfloor * 2) - 1
                    newceiling = (newceiling * 2) - 1

            floor, ceiling = newfloor, newceiling
            self.lm.put_char(char)

        return out
コード例 #24
0
    def _write_read_bits(self, out):
        """Output bits on TDI while reading TDO bits in"""

        # a bitstring.BitStream() has first bit in left-most array position (ie. msb first)
        length = out.len
        byte = BitArray(out)    # copy out so can modify it
        byte.append(8-length)   # pad 0's to be 8 bits long

        # Check number of bits.
        if not (0 < length <= 8):
            raise JtagError('Wrong number of bits: {}'.format(length))

        #@@@#print('out: ', out, 'length: ', length, 'byte: ', byte, 'byte as uint: ', byte.uint)
        
        # length of 0 for 1 bit, length of 7 for 8 bits, etc.
        cmd = array('B', (Ftdi.RW_BITS_PVE_NVE_MSB, length-1, byte.uint))

        #@@@#print('cmd: ', cmd)
        
        self._stack_cmd(cmd)
        self.sync()
        
        data = self._ftdi.read_data_bytes(1, 4)
        if (len(data) != 1):
            raise JtagError('Not all data read! Expected {} bytes but only read {} bytes'.format(1,len(data)))

        tdo = BitArray(data)

        # Only pass back the same number of bits as clocked
        # out. Although MSB, a MSB bit read left shifts the bits
        # starting at bit 0. So it is right shifted MSB from reads by
        # left shifted MSB on bit writes. (Go Fugure?)
        tdo = tdo[8-length:]
        return tdo
コード例 #25
0
def toBits(bytearraydata):  ## Received byte array to bit array.

    bitArray = BitArray()
    for databyte in bytearraydata:
        bitByte = BitArray('uint:8={value}'.format(value=databyte))
        bitArray.append(bitByte)
    return bitArray
コード例 #26
0
ファイル: client.py プロジェクト: yan730/avacity-2.0
 def handle(self, connection, address):
     self.address = address
     self.connection = connection
     logging.debug(f"Connection from {address[0]}")
     buffer = ConstBitStream()
     while True:
         try:
             data = connection.recv(1024)
         except OSError:
             break
         if not data:
             break
         data = buffer + ConstBitStream(data)
         buffer = ConstBitStream()
         if data.hex == "3c706f6c6963792d66696c652d726571756573742f3e00":
             send_data = BitArray(const.XML)
             send_data.append("0x00")
             connection.send(send_data.bytes)
             continue
         while len(data) - data.pos > 32:
             length = data.read(32).int
             if (len(data) - data.pos) / 8 < length:
                 data.pos = 0
                 break
             final_data = protocol.processFrame(data.read(length * 8),
                                                True)
             if final_data:
                 try:
                     self.server.process_data(final_data, self)
                 except Exception:
                     logging.exception("Ошибка при обработке данных")
         if len(data) - data.pos > 0:
             buffer = data.read(len(data) - data.pos)
     self._close_connection()
コード例 #27
0
        def twenty_four_bit_values(color):

            outgoing_data = BitArray()
            for color_channel in color:
                outgoing_data.append(BitArray(uint=color_channel, length=8))

            return outgoing_data
コード例 #28
0
    def build_cmd(self, command, arg, uid, data_type):
        cmd = command.encode(encoding="utf-8")
        if len(command) < 10:
            cmd += bytes(10 - len(command))
        # add data padding
        if arg is None:
            cmd += int(0).to_bytes(8, byteorder='big')
        elif data_type is self.CmdDataType.NUMBER or data_type is self.CmdDataType.RECORD:
            cmd += int(arg).to_bytes(8, byteorder='big')
        elif data_type is self.CmdDataType.SIGNED:
            cmd += int(arg).to_bytes(8, byteorder='big', signed=True)
        elif data_type is self.CmdDataType.STRING:
            cmd += self.str2bit(arg)
        elif data_type is self.CmdDataType.STORED:
            ba = BitArray()
            ba += [0] * 38
            ba.append(
                BitArray(int(arg['resolution']).to_bytes(1,
                                                         byteorder='big'))[2:])
            ba.append(
                BitArray(int(arg['offset']).to_bytes(3, byteorder='big'))[4:])
            cmd += ba.tobytes()
        else:
            self.logger.debug("Unhandled data type: %s" % (str(data_type)))
            raise self.UnknownDataType("Unhandled data type: %s" %
                                       str(data_type))

        # add index
        cmd += uid.to_bytes(2, byteorder='big')

        return cmd
コード例 #29
0
ファイル: validations.py プロジェクト: NominaN/Banano
    def validate_checksum_xrb(cls, address: str) -> bool:
        """Given an xrb/nano/ban address validate the checksum. Return true if valid, false otherwise"""
        if len(address) == 64 and address[:4] == 'ban_':
            # Populate 32-char account index
            account_map = "13456789abcdefghijkmnopqrstuwxyz"
            account_lookup = {}
            for i in range(0, 32):
                account_lookup[account_map[i]] = BitArray(uint=i, length=5)

            # Extract key from address (everything after prefix)
            acrop_key = address[4:-8]
            # Extract checksum from address
            acrop_check = address[-8:]

            # Convert base-32 (5-bit) values to byte string by appending each 5-bit value to the bitstring, essentially bitshifting << 5 and then adding the 5-bit value.
            number_l = BitArray()
            for x in range(0, len(acrop_key)):
                number_l.append(account_lookup[acrop_key[x]])
            number_l = number_l[
                4:]  # reduce from 260 to 256 bit (upper 4 bits are never used as account is a uint256)

            check_l = BitArray()
            for x in range(0, len(acrop_check)):
                if acrop_check[x] not in account_lookup:
                    return False
                check_l.append(account_lookup[acrop_check[x]])
            check_l.byteswap()  # reverse byte order to match hashing format

            # verify checksum
            h = blake2b(digest_size=5)
            h.update(number_l.bytes)
            return h.hexdigest() == check_l.hex

        return False
コード例 #30
0
ファイル: 3Des.py プロジェクト: andrew-uwb/TripleDES
    def roundKeyGen(self, initialKey, operation):

        cKey = BitArray()
        dKey = BitArray()
        self.roundKeys = list()

        for roundCount in range(0, 16):

            #Split 56-bit key into C and D
            cKey = initialKey[0:28]
            dKey = initialKey[28:56]

            #Rotate to the left by the amount appropriate for each round
            cKey.rol(self.roundShift[roundCount])
            dKey.rol(self.roundShift[roundCount])

            #Put them back together for the 56-bit key
            cKey.append(dKey)
            initialKey = cKey
            #The 48-bit permutation is calculated and stored in the list of round keys
            #The 56-bit key is kept and saved to calculate the next round key
            self.roundKeys.append(self.permuteBits(initialKey, self.ROUND_P))

        #If we're doing a decryption we need to run the round keys in reverse order.
        if (operation == 'DECRYPT'):

            roundKeyReversal = list()
            i = 0

            for i in range(0, 16):
                roundKeyReversal.append(self.roundKeys[15 - i])

            self.roundKeys = roundKeyReversal
コード例 #31
0
ファイル: cvc_parsing.py プロジェクト: junxzm1990/simulator
def p_expression_concat(p):
    "expression : expression CONCAT expression"
    s1=BitArray(p[1])
    s2=BitArray(p[3])
    # print str(s2)
    s1.append('0b' + s2.bin)
    p[0] = '0b' + s1.bin
コード例 #32
0
def main(bwfilename, fastqfilename, k):
    """
    Prend un fichier sérialisé et un jeu de données de référence
    """
    global wt
    global converter
    process = psutil.Process(os.getpid())
    print(process.memory_info().rss)

    with open(bwfilename, 'rb') as bw_file:
        start_time = time.time()
        parseBWC(bw_file)
        print("ProcessBWT--- %s seconds ---" % (time.time() - start_time))
    print(process.memory_info().rss)
    with open("".join(bwfilename.split('.')[0].split('_')[0:2])+"_f.fastq", 'w') as kmer_file:
        for kmerset in ur.transfer_kmer(fastqfilename, k):
            k_pres = BitArray()
            for kmer in kmerset:
                k_pres.append([foundKmer(kmer)])
                print("Process1KMER--- %s seconds ---" % (time.time() - start_time))
            header = "@"
            header += ur.commun_read_ref(k_pres, k)
            header +=  ur.coverage_read_ref(k_pres, k)
            kmer_file.write(header)
            print("Process1READ--- %s seconds ---" % (time.time() - start_time))
            print(process.memory_info().rss)
コード例 #33
0
ファイル: base_fields.py プロジェクト: FranckCCC/gullivigne
 def read(self):
     words = self.get_words()
     data = BitArray()
     for word in reversed(words):
         data.append("uint:32=%d" % word)
     self.bitarray.overwrite(data, pos=0)
     self.print_block(self.bitarray, "read_regs")
コード例 #34
0
ファイル: pyrai.py プロジェクト: icarusglider/PyRai
def xrb_account(address):
    # Given a string containing an XRB address, confirm validity and provide resulting hex address
    if len(address) == 64 and (address[:4] == 'xrb_'):
        account_map = "13456789abcdefghijkmnopqrstuwxyz"  # each index = binary value, account_lookup[0] == '1'
        account_lookup = {}
        for i in range(
                0, 32
        ):  # populate lookup index with prebuilt bitarrays ready to append
            account_lookup[account_map[i]] = BitArray(uint=i, length=5)

        acrop_key = address[
            4:
            -8]  # we want everything after 'xrb_' but before the 8-char checksum
        acrop_check = address[-8:]  # extract checksum

        # convert base-32 (5-bit) values to byte string by appending each 5-bit value to the bitstring, essentially bitshifting << 5 and then adding the 5-bit value.
        number_l = BitArray()
        for x in range(0, len(acrop_key)):
            number_l.append(account_lookup[acrop_key[x]])
        number_l = number_l[
            4:]  # reduce from 260 to 256 bit (upper 4 bits are never used as account is a uint256)

        check_l = BitArray()
        for x in range(0, len(acrop_check)):
            check_l.append(account_lookup[acrop_check[x]])
        check_l.byteswap()  # reverse byte order to match hashing format

        result = number_l.hex.upper()

        # verify checksum
        h = blake2b(digest_size=5)
        h.update(number_l.bytes)
        if (h.hexdigest() == check_l.hex): return result
        return False
    return False
コード例 #35
0
ファイル: iso.py プロジェクト: Gigahawk/ps2isopatcher
 def set_entry(self, name, lba, size):
     i = 0
     print(f"Searching for {name}")
     while True:
         total_len = self.tbl_data[i * 8:(i + 1) * 8].int
         if total_len == 0:
             break
         offset = i * 8
         entry = self.tbl_data[offset:(i + total_len) * 8]
         name_len = self._get_name_length(entry)
         n = self._get_name(entry, name_len)
         print(n)
         if n == name:
             iso_offset = offset + self.dt_addr
             lba_offset = iso_offset + 2 * 8
             size_offset = iso_offset + 10 * 8
             lba_le = Bits(uintle=lba, length=4 * 8)
             lba_be = Bits(uintbe=lba, length=4 * 8)
             lba_bits = BitArray()
             lba_bits.append(lba_le)
             lba_bits.append(lba_be)
             size_le = Bits(uintle=size, length=4 * 8)
             size_be = Bits(uintbe=size, length=4 * 8)
             size_bits = BitArray()
             size_bits.append(size_le)
             size_bits.append(size_be)
             self.data.overwrite(lba_bits, lba_offset)
             self.data.overwrite(size_bits, size_offset)
             import pdb
             pdb.set_trace()
             self.set_tbl_data()
             break
         i += total_len
コード例 #36
0
ファイル: Assembler.py プロジェクト: laforest/Octavo
 def branch(self, origin, origin_enable, destination, predict_taken, predict_enable, condition_name):
     condition_bits      = self.conditions[condition_name]
     origin_bits         = BitArray(uint=origin, length=Branch.origin_width)
     destination_bits    = BitArray(uint=destination, length=Branch.destination_width)
     config = BitArray()
     for entry in [origin_bits, origin_enable, destination_bits, predict_taken, predict_enable, condition_bits]:
         config.append(entry)
     return config
コード例 #37
0
    def decrypt(self):
        result = BitArray(hex='0x00')
        skip_by = self.key_len
        for i in xrange(0, self.cipher_stream.len, skip_by):
            xor_res = self.cipher_stream[i:skip_by] ^ self.XOR_to_use
            result.append(xor_res)
            skip_by += self.key_len

        return result
コード例 #38
0
 def testCreationFromOct(self):
     s = BitArray(oct="7")
     self.assertEqual(s.oct, "7")
     self.assertEqual(s.bin, "111")
     s.append("0o1")
     self.assertEqual(s.bin, "111001")
     s.oct = "12345670"
     self.assertEqual(s.length, 24)
     self.assertEqual(s.bin, "001010011100101110111000")
     s = BitArray("0o123")
     self.assertEqual(s.oct, "123")
コード例 #39
0
ファイル: test_bitarray.py プロジェクト: flynnsark/bitstring
 def testCreationFromOct(self):
     s = BitArray(oct='7')
     self.assertEqual(s.oct, '7')
     self.assertEqual(s.bin, '111')
     s.append('0o1')
     self.assertEqual(s.bin, '111001')
     s.oct = '12345670'
     self.assertEqual(s.length, 24)
     self.assertEqual(s.bin, '001010011100101110111000')
     s = BitArray('0o123')
     self.assertEqual(s.oct, '123')
コード例 #40
0
def generateConfirmationKey(identitySecret,time,tag=""):
    identitysecret = b64decode(identitySecret)
    secret = BitArray(bytes=identitysecret,length=len(identitysecret)*8)
    if tag != "":
        tagBuff = BitArray(bytes=tag,length=len(tag)*8)
    buff = BitArray(4*8)
    time = int(time)
    buff.append(BitArray(int=time,length=32))
    if tag != "":
        buff.append(tagBuff)
    conf_hmac = hmac.new(secret.tobytes(),buff.tobytes(),hashlib.sha1)
    return b64encode(conf_hmac.digest())
コード例 #41
0
ファイル: PIRQueryObject.py プロジェクト: eliLemb/pir_project
 def createZVector(self,aListOfGfuncionXorCwList):
     tempZ_Vector = BitArray()
     tempResultPerSection = BitArray(int = 0,length = (int)(math.sqrt(self.DB_LENGTH)))
     
     aListOfGfuncionXorCwList.reverse()
     while aListOfGfuncionXorCwList:
         tempResultPerSection = BitArray(int = 0,length = (int)(math.sqrt(self.DB_LENGTH)))
         for _ in range(0,self.seedsPerSection):
             tempResultPerSection = tempResultPerSection ^ aListOfGfuncionXorCwList.pop()
         tempZ_Vector.append(tempResultPerSection)
         
     return tempZ_Vector   
コード例 #42
0
def bitPackDPCMModel(order,predictor,nCodeWords,codebook,sigLen,encodedx):
    a=BitArray(uint=order, length=8)
    b=BitArray().join(BitArray(float=x, length=32) for x in predictor.params)
    a.append(b)
    a.append(BitArray(uint=int(nCodeWords), length=8))
    b=BitArray().join(BitArray(float=x, length=32) for x in codebook)
    a.append(b)
    a.append(BitArray(uint=int(sigLen), length=32))
    nBits=int(np.ceil(np.log2(nCodeWords)));
    b=BitArray().join(BitArray(uint=x, length=nBits) for x in encodedx)
    a.append(b)
    return(a)
コード例 #43
0
ファイル: Generator.py プロジェクト: laforest/Octavo
 def condition_to_binary (self, bdo, branch):
     condition = branch.condition
     dyadic    = bdo.dyadic
     condition_bits = BitArray()
     for entry in [condition.a, condition.b, condition.ab_operator]:
         field_bits = getattr(dyadic, entry, None)
         field_bits = getattr(bdo, entry, field_bits)
         if field_bits is None:
             print("Unknown branch field value: {0}".format(entry))
             self.ask_for_debugger()
         condition_bits.append(field_bits)
     return condition_bits
コード例 #44
0
ファイル: Fields.py プロジェクト: InfectedPacket/Vmfcat
 def get_bit_array(self):
     b = BitArray()
     dtgfields = self.fields.values()
     #self.fields.sort()
     dtgfields.sort()
     for f in dtgfields:
         if (f.name == "ext"):
             if (self.has_extension):
                 fbits = f.get_bit_array()
                 b.append(fbits)
         else:
             fbits = f.get_bit_array()
             b.append(fbits)
     return b
コード例 #45
0
ファイル: versaload.py プロジェクト: going-digital/versaload
class Bmc:
    """
    Bmc Biphase Mark Coding

    Generate biphase mark coding streams
    """
    def __init__(self):
        self.__lastState = False
        self.__data = BitArray()

    def add(self,datastream):
        """
        Add datastream
        """
        for b in datastream:
            if self.__lastState:
                if b:
                    self.__data.append('0b00')
                    self.__lastState = False
                else:
                    self.__data.append('0b01')
            else:
                if b:
                    self.__data.append('0b11')
                    self.__lastState = True
                else:
                    self.__data.append('0b10')

    def get(self):
        """
        Return modulated datastream
        """
        return self.__data
コード例 #46
0
 def encode_header(self, res):
     res.append(pack('uint:8', self.d['layout_version']))
     res.append(pack('uint:8', self.d['eeprom_size']))
     res.append(pack('uint:8', 0)) # Used EEPROM size
     uid = BitArray()
     uid.append(pack('uint:8', self.d['bus_protocol_version']))
     uid.append(pack('uintbe:16', self.d['model']))
     uid.append(pack('uint:8', self.d['hardware_revision']))
     uid.append(pack('uintbe:24', self.d['serial']))
     # Calculate CRC over unique id
     uid.append(pack('uint:8', unique_id_crc(uid.bytes)))
     res.append(uid)
     res.append(pack('uint:8', self.d['firmware_version']))
     res.append_string(self.d['name'])
コード例 #47
0
ファイル: Opcode_Manager.py プロジェクト: laforest/Octavo
 def to_binary (self, operators):
     """Converts the fields of the control bits of an instruction opcode, 
        looked-up from symbolic names, to binary encoding. 
        Field values are strings naming the same field in the dyadic/triadic
        operations objects."""
     control_bits = BitArray()
     for entry in [self.split, self.shift, self.dyadic3, self.addsub, self.dual, self.dyadic2, self.dyadic1, self.select]:
         field_bits = getattr(operators.dyadic, entry, None)
         field_bits = getattr(operators.triadic, entry, field_bits)
         if field_bits is None:
             print("Unknown opcode field value: {0}".format(entry))
             self.ask_for_debugger()
         control_bits.append(field_bits)
     return control_bits
コード例 #48
0
ファイル: container.py プロジェクト: Fors3cDream/kitty
 def render(self):
     """
     :return: rendered value of the container
     """
     rendered = BitArray()
     for field in self._fields:
         frendered = field.render()
         if not isinstance(frendered, Bits):
             raise KittyException(
                 "the field %s:%s was rendered to type %s, you should probably wrap it with appropriate encoder"
                 % (field.get_name(), type(field), type(frendered))
             )
         rendered.append(frendered)
     self.set_current_value(rendered)
     return self._current_rendered
コード例 #49
0
ファイル: dcc_packet_factory.py プロジェクト: hsanjuan/dccpi
    def function_group_one_packet(address, fl, fl1, fl2, fl3, fl4):
        address_bin = BitArray('uint:8=%d' % address)

        functions = [fl, fl4, fl3, fl2, fl1]
        instruction_bin = BitArray('0b100')
        for f in functions:
            if f:
                instruction_bin.append('0b1')
            else:
                instruction_bin.append('0b0')

        error = address_bin ^ instruction_bin
        data = [instruction_bin, error]

        return DCCGeneralPacket(address_bin, data)
コード例 #50
0
ファイル: Assembler.py プロジェクト: laforest/Octavo
 def gen_po(self, po_entry, target_address, increment):
     po_address      = self.memmap_obj.indirect[po_entry]
     offset          = target_address - po_address
     if increment >= 0:
         sign = 0
     else:
         sign = 1
     sign        = BitArray(uint=sign,      length=self.po_increment_sign_bits)
     increment   = BitArray(uint=increment, length=self.po_increment_bits)
     offset      = BitArray(uint=offset,    length=self.offset_width)
     po          = BitArray()
     for field in [sign, increment, offset]:
         po.append(field)
     # A programmed offset is only correct in a given PO entry
     return (po_entry, po)
コード例 #51
0
    def get_distance(self):
        """
        Assuming that the two bit strings are the same length:
        1. XOR the two bit strings. This will calculate which positions are different.
        2. Count the number of set bits from the result of step 1.
        """
        self._pre_process()

        bit_str_len = self.bit_str1.len
        resulting_diff_stream = BitArray(bin='0')

        for i in range(0, bit_str_len):
            result = self.bit_str1[i] ^ self.bit_str2[i]
            resulting_diff_stream.append(BitArray(bool=result))

        return resulting_diff_stream.count(1)
コード例 #52
0
ファイル: h3_hash.py プロジェクト: 4sp1r3/appreal
 def hash(self,key):
     """This function computes hash values for the given key according to the H3 function family. The result is returned as an integer value on 32 bits. However if the hash function generates lower number of bits, higher positions are set to 0"""
     value = BitArray()
     key_val = BitArray(bytes=key, length = self.input_size)
     for i in range(0,self.bit_size):
         bit = [key_val[j] and self.seed[i][j] for j in range(0,self.input_size)]
         bit_v = bit[0]
         for j in range(1,self.input_size):
             bit_v = bit_v ^ bit[j]
         if bit_v:
             value.append('0b1')
         else:
             value.append('0b0') 
         #print(key_val.bin,self.seed[i].bin,bit,bit_v)
     #print(value.uint,value.bin)
     return value.uint
コード例 #53
0
    def __add__(self,other):
        
        while (len(self) != len(other)):
            if (len(self) < len(other)):
                self = StandardAddress(bin='000'+self.bin)
            else:
                other = StandardAddress(bin='000'+other.bin)

        digits1 = self.digits()
        digits2 = other.digits()
        digits1.reverse()
        digits2.reverse()

        addressOut = []
        carries = []

        for digit1,digit2 in zip(digits1,digits2):

            digits = [digit1,digit2] + carries

            carries = []
            rem = Bits(bin='000')
            for digit in digits:

                remtemp = remainderDigits(rem,digit)
                car = carryDigits([rem,digit,remtemp])
                rem = remtemp
                carries.append(car)

            addressOut.append(rem)
        
        addressOut.reverse()
        bits = BitArray('')

        #If there are still non-zero carries
        if not all(c.bin == '000' or c.bin == '111' for c in carries):
            rem = Bits(bin='000')
            for digit in carries:
                remtemp = remainderDigits(rem,digit)
                car = carryDigits([rem,digit,remtemp])
                rem = remtemp
            if rem.bin != '000' and rem.bin != '111': bits.append(rem)

        for digit in addressOut:
            bits.append(digit)

        return StandardAddress(bin=bits.bin)
コード例 #54
0
ファイル: population.py プロジェクト: 5agado/intro-ai
 def newGeneration(self):
     #elitarism
     newGen = self.getBestIndividuals(self.elites_num)
     
     while len(newGen) < self.size:
         g_len = [None] * self.chromoSize #lengths of a chromo genes
         
         #trasform to single bit array
         c1 = self.rouletteSelection()
         offspring1 = BitArray()
         for i, gene in enumerate(c1.genes):
             g_len[i] = len(gene)
             offspring1.append(gene)
             
         c2 = self.rouletteSelection()
         offspring2 = BitArray()
         for gene in c2.genes:
             offspring2.append(gene)
             
         self.crossover(offspring1, offspring2)
         self.mutate(offspring1)
         self.mutate(offspring2)
         
         #return to array of bit arrays            
         newC1 = self.Chromo(self.chromoSize)
         newC1genes = []
         lim = 0
         for i in range(self.chromoSize):
             newC1genes.append(offspring1[(lim):(lim + g_len[i])])
             lim += g_len[i]
         newC1.genes = newC1genes
         
         newC2 = self.Chromo(self.chromoSize)
         newC2genes = []
         lim = 0
         for i in range(self.chromoSize):
             newC2genes.append(offspring2[(lim):(lim + g_len[i])])
             lim += g_len[i]
         newC2.genes = newC2genes
             
         
         newGen.append(newC1)
         newGen.append(newC2)
         
         
     self.chromos = newGen
     self.generation_num += 1
コード例 #55
0
def addDigits(digit1,digit2):

    bits = BitArray('')
    wrapCarry = False

    for i in range(3):
        xorBit = digit1.bin[i] != digit2.bin[i]
        remainder = xorBit != wrapCarry

        if wrapCarry:
            wrapCarry = xorBit or (digit1.bin[i] == '1' and digit2.bin[i] == '1')
        else:
            wrapCarry = digit1.bin[i] == '1' and digit2.bin[i] == '1'

        bits.append(Bits(bool=remainder))

    return [wrapCarry,bits]
コード例 #56
0
ファイル: lifx.py プロジェクト: DeanMakovsky/light-mood-sound
def constructBody(hsb, ttime, kelvin):
	"constructs message body bitstring for packet"
	mssg = BitArray()
	mssg.append('uintle:8=0')
	mssg.append('uintle:16='+str(hsb[0]))
	mssg.append('uintle:16='+str(hsb[1]))
	mssg.append('uintle:16='+str(hsb[2]))
	mssg.append('uintle:16='+str(kelvin))
	mssg.append('uintle:32='+str(ttime))
	return mssg
コード例 #57
0
ファイル: __init__.py プロジェクト: elfixit/mini-power-pc
 def compile_str(self, mnemonicstr):
     opcode_all = BitArray(100*8)
     for line in mnemonicstr.splitlines():
         realline = line.split(';')
         elements = realline[0].split()
         if not len(elements) == 0:
             cls = self._mnemonics[elements.pop(0)]
             opcode = cls.compile(*elements)
             if self.debug:
                 print "compiled line: {} to: {}".format(line, opcode.bin)
             if not len(opcode) == 16:
                     raise Exception("Invalid opcode compiling.. {}")
             opcode_all.append(opcode)
         else:
             opcode_all.append(Bits(16))
     opcode_final = BitArray(600*8)
     opcode_final[0:len(opcode_all)] = opcode_all
     return opcode_final
コード例 #58
0
def initial_position():
    # The first length*height bits represent the white player's pieces
    # The second length*height bits represent the black player's pieces
    # The last two  groups of 8 bits represent the turn bit and the number of passes in a row
    initial_pos = BitArray('0b0') * (2 * area + 16)
    board_set(initial_pos, length / 2 - 1, height / 2 - 1, WHITE)
    board_set(initial_pos, length / 2 - 1, height / 2, BLACK)
    board_set(initial_pos, length / 2, height / 2 - 1, BLACK)
    board_set(initial_pos, length / 2, height / 2, WHITE)

    incr_turn(initial_pos)
    incr_turn(initial_pos)

    # We need our board have have a length that's a multiple of eight, since we
    # convert it to a tuple of bytes
    while len(initial_pos) % 8 != 0:
        initial_pos.append('0b0')

    return initial_pos
コード例 #59
0
ファイル: main.py プロジェクト: AOQNRMGYXLMV/pcuva-problems
def solve(par):
    M, Q, construct, queries = par
    bits = BitArray()
    for m in construct:
        for i in range(m[0]):
            bits.append('0b' + m[1])

    nodes = set()
    for q in queries:  # (i-1, i]
        nodes.add(q[1] - 1)
        nodes.add(q[2])
    nodes = list(nodes)
    nodes.sort()

    count = [0] * len(nodes)
    for i in range(1, len(nodes)):
        count[i] = bits[nodes[i - 1] + 1:nodes[i] + 1].count(1)
    size = [0] * len(nodes)
    for i in range(1, len(nodes)):
        size[i] = nodes[i] - nodes[i - 1]

    tree = SegmentTreeSum(len(nodes))
    tree.buildTree(count, 1, 0, len(nodes) - 1)

    results = []
    for q in queries:
        i1 = nodes.index(q[1] - 1)
        i2 = nodes.index(q[2])
        if q[0] == 'F':
            for i in range(i1 + 1, i2 + 1):
                count[i] = size[i]
                tree.update(i, size[i])
        if q[0] == 'E':
            for i in range(i1 + 1, i2 + 1):
                count[i] = 0
                tree.update(i, 0)
        if q[0] == 'I':
            for i in range(i1 + 1, i2 + 1):
                count[i] = size[i] - count[i]
                tree.update(i, count[i])
        if q[0] == 'S':
            results.append(tree.query(i1 + 1, i2))
    return '\n'.join(str(e) for e in results) + '\n'