Ejemplo n.º 1
0
	def _generate_pawn(self, team, adv, empty, moves):
		pawns =  bitarray(self.pieces[team][PAWN])
		''' Loop for each pawn '''
		while(pawns.any()):

			pos_init = pawns.index(1)
			bb_init = bitarray(64)
			bb_init.setall(0)
			bb_init[pos_init] = 1

			# Movement to 1 forward
			if team == WHITE:
				bb_final = rightshift(bb_init, 8) & empty
			else:
				bb_final = leftshift(bb_init, 8) & empty

			self._check_move(team, PAWN, pos_init, bb_final, moves)

			# Normal capture
			if team == WHITE:
				bb_final_1 = rightshift(bb_init, 7) & adv & notHFile
				bb_final_2 = rightshift(bb_init, 9) & adv & notAFile
			else:
				bb_final_1 = leftshift(bb_init, 7) & adv & notAFile
				bb_final_2 = leftshift(bb_init, 9) & adv & notHFile

			self._check_move(team, PAWN, pos_init, bb_final_1, moves)
			self._check_move(team, PAWN, pos_init, bb_final_2, moves)

			pawns ^= bb_init
Ejemplo n.º 2
0
 def encrypt(self, message):
     """ Encrypts a message.
     Args:
         message (string): The message to encrypt.
     Returns:
         (string) The encrypted message. """
     k = self.key.tobytes()
     msg = bitarray(0)
     msg.frombytes(message.encode('latin-1'))
     s = list(range(256))
     j = 0
     for i in range(256):
         j = (j + s[i] + k[i % bits2bytes(self.keysize)]) % 256
         tmp = s[i]
         s[i] = s[j]
         s[j] = tmp
     m = msg.tobytes()
     c = bitarray(0)
     i = 0
     j = 0
     for it in range(len(m)):
         i = (i + 1) % 256
         j = (j + s[i]) % 256
         tmp = s[i]
         s[i] = s[j]
         s[j] = tmp
         xk = s[(s[i] + s[j]) % 256]
         bkey = bitarray(bin(xk).lstrip('0b'))
         while len(bkey) < 8:
             bkey.insert(0, False)
         c.extend(bkey ^ msg[8 * it:8 * (it + 1)])
     return c.tobytes().decode('latin-1')
Ejemplo n.º 3
0
Archivo: g15.py Proyecto: turski/mpdg15
	def _rdraw(self, offs=(0,0)):
		ofs_x, ofs_y = offs
		x0 = self.draw_area[2]
		y0 = self.draw_area[1]
		ofs_x += x0
		ofs_y += y0
		buf = empty_img()
		for i, line in enumerate(self.img):
			linebuf = line.copy()
			l = linebuf.length()
			if l == ofs_x:
				pass
			elif l < ofs_x:
				dl = ofs_x - l
				a = bitarray(dl)
				a.setall(False)
				linebuf = a + linebuf
			elif l > ofs_x:
				dl = l - ofs_x
				del linebuf[:dl]
			l = linebuf.length()
			if l == 160:
				pass
			elif l > 160:
				del linebuf[160:]
			elif l < 160:
				dl = 160 - l
				a = bitarray(dl)
				a.setall(False)
				linebuf += a
			y = ofs_y + i
			linebuf &= self.mask[y]
			buf[y] = linebuf
		self.buf = buf
		self.redraw = False
Ejemplo n.º 4
0
    def __init__(self, filename):
        self.disk = bloc_device(BLOCK_SIZE, filename)
        self.superBlock = minix_superbloc(self.disk)

        # bitmap inode
        self.inode_map = bitarray(endian='little')
        self.inode_map.frombytes(
            self.disk.read_bloc(2, self.superBlock.s_imap_blocks))

        # bitmap blocs
        self.zone_map = bitarray(endian='little')
        self.zone_map.frombytes(
            self.disk.read_bloc(2 + self.superBlock.s_imap_blocks,
                                self.superBlock.s_zmap_blocks))

        # inodes list
        inodes_from_img = self.disk.read_bloc(
            self.superBlock.s_imap_blocks + self.superBlock.s_zmap_blocks +
            MINIX_SUPER_BLOCK_NUM + 1, self.superBlock.s_firstdatazone)
        self.inodes_list = []
        self.inodes_list.append(minix_inode(None))
        for i in range(0, self.superBlock.s_ninodes):
            # take one inode by one and put it in inodes_list being a minix_inode object
            # i + 1 represents the number of inode
            self.inodes_list.append(
                minix_inode(
                    inodes_from_img[i * INODE_SIZE:(i + 1) * INODE_SIZE],
                    i + 1))
        return
Ejemplo n.º 5
0
Archivo: g15.py Proyecto: turski/mpdg15
	def _cdraw(self, offs=(0,0)):
		ofs_x, ofs_y = offs
		x1 = (self.draw_area[0] + self.draw_area[2])/2
		y1 = self.draw_area[1]
		x1 += ofs_x
		y1 += ofs_y
		buf = empty_img()
		for i, line in enumerate(self.img):
			linebuf = line.copy()
			l = linebuf.length()
			x0 = x1 - (l/2)
			if x0 == 0:
				pass
			elif x0 < 0:
				del linebuf[:abs(x0)]
			elif x0 > 0:
				a = bitarray(x0)
				a.setall(False)
				linebuf = a + linebuf
			l = linebuf.length()
			if l == 160:
				pass
			elif l > 160:
				del linebuf[160:]
			elif l < 160:
				dl = 160 - l
				a = bitarray(dl)
				a.setall(False)
				linebuf += a
			y = y1 + i
			linebuf &= self.mask[y]
			buf[y] = linebuf
		self.buf = buf
		self.redraw = False
Ejemplo n.º 6
0
def getbytes(data, start, end):

    array = bitarray()
    array.frombytes(data)

    length = array.length()
    bStart = length - start - 1
    bEnd = length - end

    # <-start     end->
    #  7 6 5 4 3 2 1 0
    # +---------------+
    # |0|0|1|0|1|1|1|1|
    # +---------------+

    peace = array[bStart:bEnd]

    pLen = peace.length()

    byte_len = 8
    rest = (byte_len - pLen) % byte_len

    zeroArr = bitarray(rest)
    zeroArr.setall(False)
    zeroArr += peace

    return zeroArr.tobytes()
Ejemplo n.º 7
0
def merge_data(first_data, second_data):

    f_array = bitarray()
    f_array.frombytes(first_data)

    s_array = bitarray()
    s_array.frombytes(second_data)

    return (f_array + s_array).tobytes()
Ejemplo n.º 8
0
def nytes_to_bytes(ins_list):
    ba_full = bitarray()
    for ins in ins_list:
        for nyte in ins:    
            ba = bitarray()
            ba.frombytes(struct.pack('>H', nyte))
            ba = ba[7:]
            ba_full += ba
            
    return ba_full.tobytes()
Ejemplo n.º 9
0
    def __init__(self):
        
        self.FD = 0x7E
        self.byteFD = (self.FD).to_bytes(1,byteorder='big',signed=False)

        self.insertingBitVal = True
        self.bitFrameDelim = bitarray()
        self.bitFrameDelim.frombytes(self.byteFD)
        
        self.bitPattern = bitarray()
        self.__initBitPattern(self.bitPattern)
Ejemplo n.º 10
0
    def __init__(self):

        self.FD = 0x7E
        self.byteFD = (self.FD).to_bytes(1, byteorder='big', signed=False)

        self.insertingBitVal = True
        self.bitFrameDelim = bitarray()
        self.bitFrameDelim.frombytes(self.byteFD)

        self.bitPattern = bitarray()
        self.__initBitPattern(self.bitPattern)
Ejemplo n.º 11
0
 def _initializeBFPattern(self):
     if not os.path.exists(self._bf_pattern_path):
         _bitarray = bitarray(self._mbits)
         _bitarray.setall(0)
         _bitarray.tofile(open(self._bf_pattern_path, "wb"))
         self._bflog.log("info", "Initializing bloomfilter pattern %s successful" % self._bf_pattern_path)
     else:
         self._bflog.log("info", "Bloomfilter pattern %s already existing" % self._bf_pattern_path)
         if self._mbits == 0:
             _bitarray = bitarray()
             _bitarray.fromfile(open(self._bf_pattern_path, "rb"))
             self._mbits = len(_bitarray)
Ejemplo n.º 12
0
Archivo: g15.py Proyecto: turski/mpdg15
	def __init__(self, daemon):
		self.daemon = daemon
		self.buf = None
		self.height = 43
		self.width = 160
		self.buf = [ bitarray(self.width * [False]) for x in range(self.height) ]
		self.clear()
Ejemplo n.º 13
0
 def __init__(self, keysize):
     self.keysize = keysize
     self.key = bitarray(keysize)
     self.p = 0
     self.q = 0
     self.n = 0
     self.x = 0
    def sendMessageFunction(self, status_message, output_message):
        error = ""
        message = ""
        a = bitarray('10000011')
        payload = json.dumps(status_message)
        if isinstance(payload, str):
            payload = payload.encode('utf-8')
        payload = payload.hex()
        self.calculatePayload((int)(len(payload) * 0.5), a)

        for el in payload:  # introducing security key
            bval = bin(int(el, 16))[2:].zfill(4)
            a.extend(bval)
        self.logger.info(output_message)
        client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            client_socket.connect((self.currentIP, int(self.port)))
            client_socket.sendall(a.tobytes())
            data, header = self.recv_timeout(client_socket, 30)
            client_socket.close()
        except socket.error as msg:
            error = str(msg)
            error = error + "Connection problems with server."
            return error, ""

        message = "Received:" + str(header[3:].decode(
            'utf-8', 'ignore').replace("\x00", ""))
        if (self.connection_is_accepted(header)):
            self.logger.info(message)
            self.logger.info('Function' + status_message["uri"] + ' executed.')
            error = ''
        else:
            error = data
        return error, str(message)
    def cancelSample(self):
        self.logger.info("Killing connections (if any).")
        # establish communication to microscope
        ####################################<<<<<<<<<<<<<<<<
        ## prepare package
        #  FIN 1
        #  RSV1,RSV2,RSV3 000
        #  Opcode 0x8C
        a = bitarray('10001000')
        #  Payload 9 bytes of 0
        a.extend(9 * 8 * '0')
        log = ''
        error = ""
        log = "Sending information to cancel sample:\n"
        self.logger.info('Message sent CANCEL SAMPLE.')
        client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            client_socket.connect((self.currentIP, int(self.port)))
            client_socket.sendall(a.tobytes())

            data, header = self.recv_timeout(client_socket)
        except socket.error as msg:
            error = msg
            error = str(
                error) + "\n In disconnnect :Connection problems with server."
            log = log + str(error)
            client_socket.close()
            return error, str(msg)
        client_socket.close()
        self.logger.info(
            "Received:" +
            str(header[3:].decode('utf-8', 'ignore').replace("\x00", "")) +
            "-.")
        return error, str(data)
 def refresh(self):
     self.logger.info("Refreshing settings.")
     error = ""
     message = ""
     confirm_start_message = {}
     confirm_start_message["uri"] = "refresh"
     a = bitarray('10000011')
     payload = json.dumps(confirm_start_message)
     payload = payload.encode().hex()
     self.calculatePayload((int)(len(payload) * 0.5), a)
     for el in payload:
         bval = bin(int(el, 16))[2:].zfill(4)
         a.extend(bval)
     error = ''
     self.logger.info('Command sent: REFRESH')
     client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     try:
         client_socket.connect((self.currentIP, int(self.port)))
         client_socket.sendall(a.tobytes())
         data, header = self.recv_timeout(client_socket)
     except socket.error as msg:
         error = msg
         error = str(
             error) + "\n In refresh : Connection problems with server."
         client_socket.close()
         return error, []
     client_socket.close()
     self.logger.info(
         "Received:" +
         str(header[3:].decode('utf-8', 'ignore').replace("\x00", "")) +
         "-.")
     return error, str(data)
Ejemplo n.º 17
0
def fcs(bits):
    """
    Append running bitwise FCS CRC checksum to end of generator
    """
    fcs = FCS()
    for bit in bits:
        yield bit
        fcs.update_bit(bit)


#    test = bitarray()
#    for byte in (digest & 0xff, digest >> 8):
#        print byte
#        for i in range(8):
#            b = (byte >> i) & 1 == 1
#            test.append(b)
#            yield b

# append fcs digest to bit stream

# n.b. wire format is little-bit-endianness in addition to little-endian
    digest = bitarray(endian="little")
    digest.frombytes(fcs.digest())
    for bit in digest:
        yield bit
 def __init__(self, l):
     self.data = bitarray(
         l
     )  #Poate primi o lungime si atunci se initializeaza cu bits random sau un array de 0 si 1 ce reprezinta bits sau un alt bitarray
     self.x = ((b - a) / (2**lungime - 1)) * ba2int(self.data, False) + a
     self.fit = functie(self.x)
     self.prob = 0
Ejemplo n.º 19
0
def set_data(o_data, n_data, start, end):

        old_data = bitarray()
        old_data.frombytes(o_data)
        new_data = bitarray()
        new_data.frombytes(n_data)

        length = len(new_data)
        bStart = length - start - 1
        bEnd = length - end

        first_peace = old_data[:bStart]
        second_peace = old_data[bEnd:]

        ret_data = first_peace + new_data[length - (bEnd - bStart):] + second_peace
        return ret_data.tobytes()
Ejemplo n.º 20
0
    def get_optimal_path(self, InSkills, OutSkills):

        OldGraph = self.Graph
        self.Graph.add_node(self.FakeBeginNode)
        FakeBeginEdges = self.gen_out_node_edges(self.FakeBeginNode, InSkills)
        self.Graph.add_weighted_edges_from(FakeBeginEdges)

        self.FakeEndNode =  self.DB.CourseCID
        
        self.Graph.add_nodes_from([self.FakeEndNode])

        PathesList = []

        FakeEndEdges = self.gen_inp_node_edges(self.FakeEndNode, OutSkills)
        self.Graph.add_weighted_edges_from(FakeEndEdges)

        #print InSkills, OutSkills

        answer = topological_sort(self.Graph)
        answer = answer[answer.index(0) + 1:answer.index(self.FakeEndNode)]    

        #print answer, self.check_path(answer, InSkills, OutSkills)

        barr = bitarray(2**len(answer))
        barr.setall(False)

        opt_path = self.recurse_search(answer, answer, InSkills, OutSkills, barr)

        self.Graph.remove_node(self.FakeBeginNode)
        self.Graph.remove_node(self.FakeEndNode)

        #print '^ ', self.debug_i, self.debug_m, ' ^'

        return index2path(opt_path[1], answer)
Ejemplo n.º 21
0
Archivo: g15.py Proyecto: turski/mpdg15
	def _ldraw(self, offs=(0,0)):
		ofs_x, ofs_y = offs
		x0 = self.draw_area[0]
		y0 = self.draw_area[1]
		ofs_x += x0
		ofs_y += y0
		buf = empty_img()
		for i, line in enumerate(self.img):
			linebuf = line.copy()
			if ofs_x == 0:
				pass
			elif ofs_x < 0:
				del linebuf[:abs(ofs_x)]
			else:
				linebuf[:0] = bitarray(ofs_x * [False])
			l = linebuf.length()
			if l == 160:
				pass
			elif l < 160:
				linebuf.extend((160-l)*[False])
			else:
				del(linebuf[160:])
			y = ofs_y + i
			linebuf &= self.mask[y]
			buf[y] = linebuf
		self.buf = buf
		self.redraw = False
Ejemplo n.º 22
0
    def __init__(self,
                 x,
                 y,
                 angle,
                 img_name,
                 collision_map_name,
                 obj_id=None,
                 visible=True,
                 respawn_timer=200):
        img_name = os.path.join('img', img_name)
        collision_map_name = os.path.join('img', collision_map_name)

        img = resourcemanager.get_image(img_name)
        cm = resourcemanager.get_image(collision_map_name)
        PhysicalObject.__init__(self,
                                x,
                                y,
                                cm,
                                img,
                                mass=1,
                                visible=visible,
                                obj_id=obj_id)

        self.respawn_timer = respawn_timer

        self.moves = bitarray([False] * 5)
        self.lefti = 0
        self.righti = 1
        self.jumpi = 2
        self.jetsi = 3
        self.firei = 4

        self.img = img
        self.angle = angle
        self.leftv = Vector2D(x=-0.1, y=0)
        self.rightv = Vector2D(x=0.1, y=0)
        self.jumpv = Vector2D(x=0, y=-5)
        self.jetv = Vector2D(x=0, y=-0.2)
        self.xlimit = 5
        self.ylimit = 10
        self.ground = False
        self.friction = 0.3
        self.kills = 0
        self.deaths = 0
        self.weaponnum = 0

        self.hp = 100
        self.weapons = [None] * 3
        self.weapons[0] = AK47(self)
        self.weapons[1] = TestWeapon(self)
        self.weapons[2] = Bazooka(self)
        self.weapon = self.weapons[2]
        self.weaponnum = 2
        self.attach(self.weapon, self.rect.centerx, self.rect.centery)

        for i in self.weapons:
            i.visible = False

        self.weapon.visible = True
        self.respawn = False
Ejemplo n.º 23
0
def decompress(input_fileName, output_fileName):
    start = time.time()
    data = readFile(input_fileName).to01()
    wBitSize = int(data[0:8], 2)
    lBitSize = int(data[8:16], 2)
    data = data[16:]

    tupleSize = wBitSize + lBitSize + 1  #is the size of each tuple in bits
    result = ""  #will be a big string to be put into the bitarray at the end
    currentPointer = 0  #index of where we are in result
    relativePointer = 0  #index of pointer that goes back

    while (len(data) >= tupleSize):
        distanceBack = int(data[0:wBitSize], 2)
        charCopy = int(data[wBitSize:wBitSize + lBitSize], 2)
        tupleChar = data[wBitSize + lBitSize]
        data = data[tupleSize:]

        if (distanceBack == 0 and charCopy == 0):
            result = result + tupleChar
            currentPointer = currentPointer + 1
        else:
            relativePointer = currentPointer - distanceBack
            copiedChar = result[relativePointer:relativePointer + charCopy]
            result = result + copiedChar + tupleChar
            currentPointer = currentPointer + charCopy + 1

    resultBitArray = bitarray(result)
    end = time.time()
    with open(output_fileName, "wb+") as output_file:
        resultBitArray.tofile(output_file)
Ejemplo n.º 24
0
def _get_all(promoter, genome, excite_offset, genesize):
    promsize = len(promoter)
    plist = genome.search(bitarray(promoter))
    plist = filter(
        lambda index: int(excite_offset) <= index <
        (genome.length() - (int(genesize) + promsize)), plist)
    return plist
Ejemplo n.º 25
0
 def set_key(self, new_key):
     """ Sets the key to the argument value
     Args:
         new_key (string): the new key to set. """
     self.key = bitarray()
     self.key.frombytes(new_key.encode("latin-1"))
     self.keylength = self.key.length()
Ejemplo n.º 26
0
def junk(code, *args, **kwargs):
    tsize = int(args[0])
    instream = bitarray('0' * tsize)
    insertpos = random.randint(1, len(code) - tsize)
    c = code[:insertpos]
    c += instream
    c += code[insertpos:]
    return c
Ejemplo n.º 27
0
	def init_g15(self):
		host = 'localhost'
		port = 15550
		g15 = G15()
		g15.set_timeout(0.2)
		g15.connect(host, port)
		self.keys = bitarray('0000000000')
		self.g15 = g15
Ejemplo n.º 28
0
def junk(code, *args, **kwargs):
    tsize = int(args[0])
    instream = bitarray('0'*tsize)
    insertpos = random.randint(1, len(code)-tsize)
    c = code[:insertpos]
    c += instream
    c += code[insertpos:]
    return c
Ejemplo n.º 29
0
def coddingFile(file):
    dfrequencia = countT(file)
    l = createNode(dfrequencia)
    dic = huffmann(l)
    k = bitarray()
    for i in file:
        k += dic[i]
    return k
Ejemplo n.º 30
0
def coddingFile(file):
    dfrequencia = countT(file)
    l = createNode(dfrequencia)
    dic = huffmann(l)
    k = bitarray()
    for i in file:
        k += dic[i]
    return k
Ejemplo n.º 31
0
    def getFieldVal(self, fName, vInd):
        """get field value as string
        fName is field as (as is in Metedata)
        vInd is field value index (zero bases)

        raises IndexError in case vInd is larger then field value number
        raises KeyError in case fName is not a valid field name
        """

        # get field object
        for f in self.fields:
            if f["FieldName"] == fName:
                fld = f
                break
        else:
            raise KeyError

        if f["NoOfSymbols"] == "0":  # this effectively means None...
            return self.NoneValueStr

        # check value index range
        if vInd >= int(f["NoOfSymbols"]):
            raise IndexError

        # read data sequentially
        os.lseek(
            self.fp, self.stPos + int(f["Offset"]), os.SEEK_SET
        )  # seek to symbol table beginning
        for i in range(vInd + 1):
            # read field type byte
            b = ord(os.read(self.fp, 1))

            # read value
            if b in [4, 5, 6]:  # string containing types
                if b in [5, 6]:  # skip numeric value
                    skipSize = 4 if b == 5 else 8  # byte size to skip
                    smth = os.lseek(self.fp, skipSize, os.SEEK_CUR)

                val = b""
                while True:  # have to read bytewise - very ineffective
                    bt = os.read(self.fp, 1)
                    if bt == b"\0":
                        break
                    val += bt
                val = val.decode("utf-8")  # string is always UTF-8

            elif b in [1, 2]:  # numeric types
                a = bitarray(os.read(self.fp, b * 4))
                if b == 1:
                    val = a.unpack("intle:32")[0]
                else:
                    val = a.unpack("floatle:64")[0]

            else:
                val = b""
                print("UNHANDLED YET TYPE: ", b)  # never happened to me...

        return val
Ejemplo n.º 32
0
 def setMoves(self, data):
     data = pickle.loads(data)
     hack = bitarray()
     hack.frombytes(data[0])
     self.moves = hack
     self.weaponnum = data[1]
     self.setWeapon(self.weaponnum)
     self.weapon.fire = self.moves[self.firei]
     self.weapon.setAngle(data[2])
Ejemplo n.º 33
0
	def __init__(self, team, piece_type, pos_init, pos_final, capture=False, capture_type=None):
		self.team = team
		self.piece_type = piece_type
		self.pos_init = pos_init
		self.pos_final = pos_final

		self.pos_init_bb = bitarray(64)
		self.pos_init_bb.setall(0)
		self.pos_init_bb[pos_init[0]*8 + pos_init[1]] = 1

		self.pos_final_bb = bitarray(64)
		self.pos_final_bb.setall(0)
		self.pos_final_bb[pos_final[0]*8 + pos_final[1]] = 1

		self.from_to = self.pos_init_bb ^ self.pos_final_bb

		self.capture = capture
		self.capture_type = capture_type
Ejemplo n.º 34
0
def mk_sketch(x, W, cfg):
    """ Given centered input ndarray, create sketch. """
    if cfg.quantize:
        bools = [np.dot(x, row) >= 0.0 for row in W]
        return bitarray(bools)
    else:
        x = x.reshape(-1, 1)
        res = np.dot(W, x).flatten()
        return normalize_array(res)
Ejemplo n.º 35
0
def mk_sketch(x, W, cfg):
    """ Given centered input ndarray, create sketch. """
    if cfg.quantize:
        bools = [np.dot(x, row) >= 0.0 for row in W]
        return bitarray(bools)
    else:
        x = x.reshape(-1,1)
        res = np.dot(W, x).flatten()
        return normalize_array(res)
Ejemplo n.º 36
0
    def Substitude(self, msg):
        """
            Substitudes all bits in input message
        """

        bits = bitarray()
        if type(msg) == str:
            bits.frombytes(msg)
        elif type(msg) == bitarray:
            bits = msg
        else:
            raise ValueError("Not valid type of input data")

        res = bitarray(bits.length() * [0])
        size = len(self.__bare)
        for i in range(0, bits.length()):
            res[i] = bits[(i / size) * size + self.__bare[i % size]]
        return res
Ejemplo n.º 37
0
    def Reduce(self, block, size):
        """
        Shrinks or extends block to specified size with permutation
        """

        bits = bitarray()
        if type(block) == str:
            bits.frombytes(block)
        elif type(block) == bitarray:
            bits = block
        else:
            raise ValueError("Not valid type of input data")

        res = bitarray(size * [0])

        for i in range(0, size):
            res[i] = bits[self.__bare[i]]
        return res
Ejemplo n.º 38
0
Archivo: g15.py Proyecto: turski/mpdg15
	def get_keys(self):
		try:
			buf = self.sock.recv(1024)
			a = bitarray(endian='little')
			a.fromstring(buf)
			self.keys = a[18:28]
		except socket.timeout:
			return None
		return self.keys
Ejemplo n.º 39
0
 def decode(self, constBytes):
     bitArray = bitarray()
     bitArray.frombytes(constBytes)
     #cut waste residue
     cutPos = theLowNearestMultipleOf(self.newWordSize, len(constBytes) * 8)
     bitArray = bitArray[:cutPos]
     self.__evaluateControlBits(bitArray, self.__wordRecalculate)
     #delete control bits
     self.__delControlBits(bitArray)
     return bitArray.tobytes()
def manchester_encoding(s):
    temp_bitarray = bitarray()
    for i in range(len(s)):
        if (s[i] == '0'):
            temp_bitarray.append(False)
            temp_bitarray.append(True)
        if (s[i] == '1'):
            temp_bitarray.append(True)
            temp_bitarray.append(False)
    return temp_bitarray
Ejemplo n.º 41
0
    def __init__(self, max_items, false_positive_rate):
        """
        Create a Bloom filter
        :param max_items: the maximum items to be used with this filter.
        :param false_positive_rate: expected is-in-the-set false positive rate.
        """

        self.num_of_bit = math.log(max_items)
        self.bit_array = bitarray(self.num_of_bit)
        self.hash_func = []
Ejemplo n.º 42
0
 def __init__(self, n_edges, n_nodes, graph):
     self.edges = bitarray(n_edges)
     if self.edges.count() != 0:
         self.edges ^= self.edges
     assert self.edges.count() == 0, 'initialization nonzero'
     self.nodes = bitarray(n_nodes)
     if self.nodes.count() != 0:
         self.nodes ^= self.nodes
     assert self.nodes.count() == 0, 'initialization nonzero'
     self.graph = graph
     self.root = -1
     self.roots = set() #This is used to allow multiple root structure
     self.ext_set = set()
     self.ext_list = []
     self.ext_mapping = {}
     self.start = -1
     self.end = -1
     self.unaligned = []
     self.ext_label = {} #This attribute is used for the refinement of the external nodes
Ejemplo n.º 43
0
 def insertToBloomFilterPattern(self, item):
     try:
         _bitarray = bitarray()
         _bitarray.fromfile(open(self._bf_pattern_path, "rb"))
         for i in xrange(5):
             _bitarray[self._getMurMurHash(item, seedm=i)] = 1
         _bitarray.tofile(open(self._bf_pattern_path, "wb"))
         self._bflog.log("info", "Insertion of item %s to bloomfilter pattern successful" % item)
     except Exception, ex:
         self._bflog.log("error", "Problem on inserting item to bloomfilter pattern : %s" % ex)
Ejemplo n.º 44
0
 def __init__(self, n_edges, n_nodes, graph):
     self.edges = bitarray(n_edges)
     if self.edges.count() != 0:
         self.edges ^= self.edges
     assert self.edges.count() == 0, 'initialization nonzero'
     self.nodes = bitarray(n_nodes)
     if self.nodes.count() != 0:
         self.nodes ^= self.nodes
     assert self.nodes.count() == 0, 'initialization nonzero'
     self.graph = graph
     self.root = -1
     self.roots = set() #This is used to allow multiple root structure
     self.ext_set = set()
     self.ext_list = []
     self.ext_mapping = {}
     self.start = -1
     self.end = -1
     self.unaligned = []
     self.ext_label = {} #This attribute is used for the refinement of the external nodes
Ejemplo n.º 45
0
 def decode(self,constBytes):
     bitArray = bitarray()
     bitArray.frombytes(constBytes)
     #cut waste residue 
     cutPos = theLowNearestMultipleOf(self.newWordSize,len(constBytes)*8)
     bitArray = bitArray[:cutPos]
     self.__evaluateControlBits(bitArray,self.__wordRecalculate)
     #delete control bits
     self.__delControlBits(bitArray)
     return bitArray.tobytes()
Ejemplo n.º 46
0
def compHuffman(texto):
    l = []
    for i in freq:
        l.append(N(i, freq[i]))
    l.sort()
    arvore = huffmann(l)
    array = bitarray()
    for i in texto:
        array += arvore[i]
    return array.tobytes()
Ejemplo n.º 47
0
def getSHA256HashArray(hashId, dataString):
    message = hashlib.sha256()

    message.update(str(hashId) + dataString)
    messageInBytes = message.digest()

    messageInBitArray = bitarray(endian='little')
    messageInBitArray.frombytes(messageInBytes)

    return messageInBitArray
Ejemplo n.º 48
0
    def __init__(self, bits):
        self.version = ba2int(bits[0:3])
        self.typeid = ba2int(bits[3:6])
        self.packets = []
        self.length = 6

        if  self.typeid == 4: # parse literal value
            temp = bitarray()
            chunk = bitarray('1')
            i = 1
            while chunk[0] == 1:
                i += 5
                self.length += 5
                chunk = bits[i:i+5]
                temp += chunk[1:]
            
            self.litval = ba2int(temp)
        else: #parse operator
            self.length_type_id = bits[6]
            self.length += 1
            # Just doing the same non-dry logic for each scenario, but I think that's okay for now
            if not self.length_type_id:
                self.subpackets_total_len = ba2int(bits[7:22])
                self.length += 15
                j = 22
                children_length = 0
                while children_length < self.subpackets_total_len:
                    np = Packet(bits[j:])
                    self.length += np.length
                    children_length += np.length
                    j += np.length
                    self.packets.append(np)
            else:
                self.subpackets_num = ba2int(bits[7:18])
                self.length += 11
                j = 18
                
                while len(self.packets) < self.subpackets_num:
                    np = Packet(bits[j:])
                    self.length += np.length
                    j += np.length
                    self.packets.append(np)
def manchester_decoding(temp):
    temp_bitarray = bitarray()
    i = 0
    while i <= (len(temp) - 1):
        if temp[i] == 1 and temp[i + 1] == 0:
            temp_bitarray.append(True)
        if temp[i] == 0 and temp[i + 1] == 1:
            temp_bitarray.append(False)
        i = i + 2

    return temp_bitarray
Ejemplo n.º 50
0
    def baseValues(nbItemsToFilter, falsePosRate):
        nFilterBytes = math.ceil(((-1)/math.log(2)*2nbItemsToFiltermath.log(p))/8) #in bytes
        if nFilterBytes > 36000:
            nFilterBytes = 36000 #Correction the maximum size of the filter
        filter = bitarray(nFilterBytes) #Filter creation
        filter = initFilt(filter, nFilterBytes)

        nHashFuncs = math.ceil((nFilterBytes8)/nbItemsToFilter*math.log(2))
        if nHashFuncs > 50:
            nHashFuncs = 50 #Correction the maximum number of Hash Functions
        nTweak = random.randint(0, 4294967295)
Ejemplo n.º 51
0
def compress(input_fileName, output_fileName, ws,
             ls):  #ws is window size, ls is lookahead buffer size
    start = time.time()
    ls = ols(ls)  #making sure its not 0 or 1
    wBitSize = math.ceil(math.log(
        (ws + 1), 2))  # amount of bits needed for window size (d)
    lBitSize = math.ceil(
        math.log((ls + 1), 2)
    )  # amount of bits needed for look-ahead buffer (l), the character will always be 8 bits
    data = readFile(input_fileName).to01()
    size = len(data)

    result = bitarray(
    )  #will be a big string to be put into the bitarray at the end
    result = result + (("{:08b}").format(wBitSize)) + (
        ("{:08b}").format(lBitSize)
    )  #metadata to pass wBitSize and lBitSize to the decoder
    currentIndex = 0  #location of current pointer

    while currentIndex < size:
        distanceBack, charCopy = match(data, currentIndex, ws, ls)
        currentIndex = currentIndex + charCopy
        if (currentIndex >= size):
            tupleChar = data[
                -1]  #make sure the last tuple has the last character as its additional bit
            charCopy = charCopy - 1

        else:
            tupleChar = data[currentIndex]
            currentIndex = currentIndex + 1
        result = result + bitarray(
            ("{:0" + str(wBitSize) + "b}").format(distanceBack)) + (
                ("{0:0" + str(lBitSize) + "b}").format(charCopy)) + tupleChar

    resultBitArray = result
    resultBitArray.fill(
    )  #makes it so everything that number is a multiple of 8

    end = time.time()
    with open(output_fileName, "wb") as output_file:
        resultBitArray.tofile(output_file)
Ejemplo n.º 52
0
	def generate(self, team):
		adv = bitarray(64)
		adv.setall(0)
		adv |= self.pieces[-team][PAWN]
		adv |= self.pieces[-team][QUEEN]
		adv |= self.pieces[-team][KNIGHT]

		empty = bitarray(64)
		empty.setall(0)
		empty |= adv
		empty |= self.pieces[team][PAWN]
		empty |= self.pieces[team][QUEEN]
		empty |= self.pieces[team][KNIGHT]
		empty = ~empty

		moves = []
		self._generate_pawn(team, adv, empty, moves)
		self._generate_knight(team, adv, empty, moves)
		self._generate_queen(team, adv, empty, moves)

		return moves
Ejemplo n.º 53
0
def get_pix(image):
    pixel = image.load()
    width, height = image.size
    bitmap = bitarray()
    for h in range(height):
        for w in range(width):
            # if int(sum(pixel[w, h])) > (255 * 3 / 2):
            if pixel[w, h] > 0:
                bitmap.append(False)
            else:
                bitmap.append(True)
    return bitmap
Ejemplo n.º 54
0
 def __init__(self, idx, global_to_local, local_to_global):
     self.idx = idx
     self.global_to_local = global_to_local
     self.local_to_global = local_to_global
     self.bips = {}
     self.tips = len(local_to_global)
     # keep track which bits are deleted and which are not
     self.valid_bits = bitarray('1' * self.tips)
     self.first = 0
     self.tmp_first = 0
     # saves the valid bits for the first check iteration
     self.tmp_delete = self.valid_bits.copy()
Ejemplo n.º 55
0
 def lookupFromBloomFilterPattern(self, item):
     lookupflag = True
     try:
         _bitarray = bitarray()
         _bitarray.fromfile(open(self._bf_pattern_path, "rb"))
         for i in xrange(5):
             if _bitarray[self._getMurMurHash(item, seedm=i)] != 1:
                 lookupflag = False
                 break
         self._bflog.log("info", "Item lookup %s from bloomfilter pattern was successful [%s]" % (item, lookupflag))
     except Exception, ex:
         lookupflag = False
         self._bflog.log("error", "Problem on looking up the item from the bloomfilter pattern : %s" % ex)
Ejemplo n.º 56
0
def fcs_validate(bits):
    buffer = bitarray()
    fcs = FCS()

    for bit in bits:
        buffer.append(bit)
        if len(buffer) > 16:
            bit = buffer.pop(0)
            fcs.update(bit)
            yield bit

    if buffer.tobytes() != fcs.digest():
        raise Exception("FCS checksum invalid.")
Ejemplo n.º 57
0
 def coding(self):
     l = {}
     r = {}
     if self.x:
         return {self.x: bitarray()}
     if self.l: 
         l = self.l.coding()
     if self.r:
         r = self.r.coding()
     ret = {}
     ret.update(addprefixos(l, '0'))
     ret.update(addprefixos(r, '1'))
     return ret
Ejemplo n.º 58
0
    def encode(self,constBytes):

        bitArray = bitarray()
        bitArray.frombytes(constBytes)
        posList = bitArray.search(self.bitFrameDelim)

        counter = 0
        for pos in posList:
            bitArray.insert(pos+len(self.bitFrameDelim)-1+counter,self.insertingBitVal)
            counter += 1
        #add zero bits to the last incomplete
        bitArray.fill()
        
        return (bitArray.tobytes(), len(posList))