Пример #1
0
	def rotate_wrt_atom(self, atom_number, axis, angle):
		if atom_number < 1 and atom_number > len(self.rows):
			return #Do nothing

		#Get the coordinates of the given atom. Minus 1 because we
		#start atom counting at 1 instead of 0.
		center_coord = Struct()
		center_coord.x = self.rows[atom_number-1][1]
		center_coord.y = self.rows[atom_number-1][2]
		center_coord.z = self.rows[atom_number-1][3]
		return self.rotate(center_coord, axis, angle)
Пример #2
0
    def rotate_wrt_atom(self, atom_number, axis, angle):
        if atom_number < 1 and atom_number > len(self.rows):
            return  #Do nothing

        #Get the coordinates of the given atom. Minus 1 because we
        #start atom counting at 1 instead of 0.
        center_coord = Struct()
        center_coord.x = self.rows[atom_number - 1][1]
        center_coord.y = self.rows[atom_number - 1][2]
        center_coord.z = self.rows[atom_number - 1][3]
        return self.rotate(center_coord, axis, angle)
Пример #3
0
    def __init__(self):
        layout = [[
            sg.Button('Lista'),
            sg.Button('Fila'),
            sg.Button('Pilha'),
            sg.Button('Árvore'),
            sg.Button('Grafo')
        ], [sg.Button('Fechar')]]

        win = sg.Window('Projeto de ED', layout)
        event, value = win.read()
        random.shuffle(aux)
        global estrutura
        if (event == sg.WIN_CLOSED or event == 'Fechar'):
            win.close()
        elif (event == "Lista"):
            estrutura = ListaDinamica.CoviList()
            x = 0
            while (x < 100):
                dado = Struct.CovidLine(*data.loc[aux[x]])
                estrutura.inserir(dado)
                x += 1
            win.close()
            janela1()

        elif (event == "Fila"):
            estrutura = Fila.CoviList()
            x = 0
            while (x < 100):
                dado = Struct.CovidLine(*data.loc[aux[x]])
                estrutura.inserir(dado)
                x += 1
            win.close()
            janela1()

        elif (event == "Pilha"):
            estrutura = Pilha.CoviList()
            x = 0
            while (x < 100):
                dado = Struct.CovidLine(*data.loc[aux[x]])
                estrutura.inserir(dado)
                x += 1
            win.close()
            janela1()
        elif (event == "Árvore"):
            estrutura = Arvore.CoviList()
            x = 0
            while (x < 100):
                dado = Struct.CovidLine(*data.loc[aux[x]])
                estrutura.inserir(dado)
                x += 1
            win.close()
            janela1()
Пример #4
0
def getProductReview(url):
    L = []
    options = Options()
    options.add_argument('--headless')
    # options.add_argument('--disable-gpu')
    options.add_argument('user-agent=Mozilla/5.0')

    driver = webdriver.Chrome(options=options,
                              executable_path='./chromedriver')
    driver.get(url)
    driver.execute_script(
        "window.scrollBy(0, (document.body.scrollHeight*0.6));")

    comment = {}
    soup = BeautifulSoup(driver.page_source, 'html.parser')

    for review in soup.find_all('div', class_='BVRRContentReview'):
        author = review.find('span', class_='BVRRNickname').text
        date = review.find('span', class_='BVRRReviewDate').text
        title = review.find('span', class_='BVRRReviewTitle').text
        content = review.find('span', class_='BVRRReviewText').text
        score = review.find('span', class_='BVRRRatingNumber').text

        # print ("---")
        # print (author)
        # print (date)
        # print (title)
        # print (content)
        # print (score)
        comment = Struct.Comment(content, score, date, title)
        L.append(comment)

    driver.close()
    return L
Пример #5
0
def product_merge(L, result, source):
    for product in L:
        check = False
        for mp in result:
            if product.brand.upper() == mp.brand.upper() and name_check(
                    product.name, mp.name):
                # print (product.name);
                mp.price = min(mp.price, product.price)
                mp.original_price.append(product.price)
                mp.review_score = int(
                    (mp.review_score * len(mp.product_URL) +
                     product.review_score) / (len(mp.product_URL) + 1))
                mp.product_URL.append(product.product_URL)
                mp.figure_URL.append(product.figure_URL)
                mp.source.append(source)
                check = True
                break
        if (not check):
            mp = Struct.Merge_Product()
            mp.name = product.name
            mp.price = product.price
            mp.original_price.append(product.price)
            mp.product_URL.append(product.product_URL)
            mp.figure_URL.append(product.figure_URL)
            mp.review_score = int(product.review_score)
            mp.brand = product.brand
            mp.source.append(source)
            result.append(mp)
Пример #6
0
def merge(L1, L2, L3):
    result = []
    if (L1):
        for product in L1:
            mp = Struct.Merge_Product()
            mp.name = product.name
            mp.price = product.price
            mp.product_URL.append(product.product_URL)
            mp.figure_URL.append(product.figure_URL)
            mp.review_score = int(product.review_score)
            mp.brand = product.brand
            mp.source.append("Sephora")
            mp.original_price.append(product.price)
            result.append(mp)

    if (L2):
        if (len(result) == 0):
            for product in L2:
                mp = Struct.Merge_Product()
                mp.name = product.name
                mp.price = product.price
                mp.product_URL.append(product.product_URL)
                mp.figure_URL.append(product.figure_URL)
                mp.review_score = int(product.review_score)
                mp.brand = product.brand
                mp.source.append("Bloomindale")
                mp.original_price.append(product.price)
                result.append(mp)
        else:
            product_merge(L2, result, "Bloomindale")

    if (L3):
        if (len(result) == 0):
            for product in L3:
                mp = Struct.Merge_Product()
                mp.name = product.name
                mp.price = product.price
                mp.product_URL.append(product.product_URL)
                mp.figure_URL.append(product.figure_URL)
                mp.review_score = int(product.review_score)
                mp.brand = product.brand
                mp.source.append("UTLA")
                mp.original_price.append(product.price)
                result.append(mp)
        else:
            product_merge(L3, result, "UTLA")
    return result
Пример #7
0
def getProductReview(searchUrl):
    L = []
    # Init driver

    # headless
    chrome_options = Options()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-extensions')
    chrome_options.add_argument('--window-size=1920,1080')

    # certificate
    capabilities = DesiredCapabilities.CHROME.copy()
    capabilities['acceptSslCerts'] = True
    capabilities['acceptInsecureCerts'] = True

    driver = webdriver.Chrome(executable_path='./chromedriver',
                              chrome_options=chrome_options,
                              desired_capabilities=capabilities)
    # driver = webdriver.Chrome(executable_path='/Users/Leon/Desktop/capstone_test/chromedriver')
    # baseUrl = 'https://www.sephora.com';
    driver.get(searchUrl)
    time.sleep(0.5)  # Delays for 0.5 seconds.

    # Move ten times can scroll to the end (the height of the website is 4284px)
    for i in range(10):
        driver.execute_script("window.scrollBy(0,500)")
        time.sleep(0.2)
    html = driver.page_source
    soup = BeautifulSoup(html.encode('utf-8'), 'html.parser')

    reviewTitleClass = 'css-1fsuw0x'
    reviewContentClass = 'css-eq4i08'
    reviewDateClass = 'css-12z5fyi'

    reviewTitles = soup.findAll("div", {"class": reviewTitleClass})
    reviewContents = soup.findAll("div", {"class": reviewContentClass})
    reviewDates = soup.findAll("span", {"class": reviewDateClass})
    reviewRatings = getReviewRatings(soup)

    for i in range(len(reviewTitles)):
        title = reviewTitles[i].contents
        content = reviewContents[i].contents
        date = reviewDates[i].contents
        rating = reviewRatings[i]

        # print(content[0]);
        # print(rating);
        # print(date[0]);
        # print(title[0]);
        # print("--------")
        # print content
        comment = Struct.Comment(content[0], rating, date[0], title[0])
        L.append(comment)
    driver.close()
    return L
Пример #8
0
 def find_min_value(self):
     min = Struct()
     min.x = self.rows[0][1]  #Set to some initial value
     min.y = self.rows[0][2]
     min.z = self.rows[0][3]
     for row in self.rows:
         if row[1] < min.x:
             min.x = row[1]
         if row[2] < min.y:
             min.y = row[2]
         if row[3] < min.z:
             min.z = row[3]
     return min
Пример #9
0
 def find_max_value(self):
     max = Struct()
     max.x = self.rows[0][1]  #Set to some initial value
     max.y = self.rows[0][2]
     max.z = self.rows[0][3]
     for row in self.rows:
         if row[1] > max.x:
             max.x = row[1]
         if row[2] > max.y:
             max.y = row[2]
         if row[3] > max.z:
             max.z = row[3]
     return max
Пример #10
0
def getProductInfo(keyword):
    products = []
    url = 'https://shop.nordstrom.com/sr?keyword=%s' % keyword
    response = requests.get(url, headers={'User-Agent': "Mozilla/5.0"})
    print('url: ' + str(url))
    print('status code: ' + str(response.status_code))
    print('encoding: ' + str(response.encoding))
    soup = BeautifulSoup(response.content, 'html.parser')

    for product in soup.find_all('article', class_='productModule_Z1pWESH'):
        name = product.find(
            'span',
            class_='light_Z1MOMXL navigationLink_Z1yc2MG').span.text.strip(
                ' \t\n')
        product_url = 'https://shop.nordstrom.com' + product.find(
            'a', class_='linkWrapper_F71fC')['href']
        picture_url = product.find('img', class_='image_Z272g5Q')['src']
        prices = product.find_all(
            'span', attrs={'data-element': 'product-module-price-line-price'})
        normal_price = prices[0].text
        sales_price = -1
        if len(prices) > 1:
            sales_price = prices[1].text

        print('---')
        print(name)
        print(product_url)
        print(picture_url)
        print(normal_price)
        print(sales_price)
        p = Struct.Product("", name, normal_price, product_url, picture_url,
                           "0")
        if (sales_price != -1):
            p = Struct.Product("", name, sales_price, product_url, picture_url,
                               "0")

        products.append(p)

    return products
Пример #11
0
    def initialize(self):
        self.number_of_target_atoms = Struct()
        self.number_of_target_atoms.x = len(self.molecule_placement[0])
        self.number_of_target_atoms.y = len(self.molecule_placement)

        self.spacing_of_target_atoms = Struct()
        self.spacing_of_target_atoms.x = abs(self.upper_left_surface_atom.x- \
         self.lower_right_surface_atom.x)/(self.number_of_target_atoms.x-1) #-1 because counting atom connections
        self.spacing_of_target_atoms.y = abs(self.upper_left_surface_atom.y- \
         self.lower_right_surface_atom.y)/(self.number_of_target_atoms.y-1) #-1 because counting atom connections

        #Load our surface and molecule placing position
        self.surface = XYZ()
        self.surface.load(self.surface_xyz_file)

        self.place_molecule = XYZ()
        self.place_molecule.load(self.place_xyz_file)
        self.place_molecule.normalize_coordinates(
        )  #Make the molecule start from (0,0,0)
        #Make the molecule start from the upper left position
        self.place_molecule.translate(self.upper_left_surface_atom.x, self.upper_left_surface_atom.y,  \
         self.upper_left_surface_atom.z)
Пример #12
0
def create_register(option, type):
    if not (exists(option[1])):
        for elem in option[2:]:
            if (not [atomic for atomic in atomics if atomic.name == elem]):
                print("Ha introducido un tipo que no existe")
                return

        elements = []
        for elem in option[2:]:
            elements.append(search_element(elem, 'atomic'))
        if (type == "struct"):
            structs.append(Struct(option[1], elements))
        else:
            unions.append(Union(option[1], elements))
Пример #13
0
	def find_max_value(self):
		max = Struct()
		max.x = self.rows[0][1] #Set to some initial value
		max.y = self.rows[0][2]
		max.z = self.rows[0][3]
		for row in self.rows:
			if row[1] > max.x:
				max.x = row[1]
			if row[2] > max.y:
				max.y = row[2]
			if row[3] > max.z:
				max.z = row[3]
		return max
Пример #14
0
	def find_min_value(self):
		min = Struct()
		min.x = self.rows[0][1] #Set to some initial value
		min.y = self.rows[0][2]
		min.z = self.rows[0][3]
		for row in self.rows:
			if row[1] < min.x:
				min.x = row[1]
			if row[2] < min.y:
				min.y = row[2]
			if row[3] < min.z:
				min.z = row[3]
		return min
Пример #15
0
def main():
    #Arguments
    surface_xyz_file = '0etoh.xyz'
    place_xyz_file = 'placed_eto_orientation.xyz'  #The molecule we want to place on the surface
    output_xyz_file = 'placed.xyz'

    #Define the longer axis as the axis where more molecules can be placed
    #(see the molecule placement array). We also define the upper left atom as the one
    #closer to (0, 0):
    upper_left_surface_atom = Struct(x=1.69501, y=0.71964,
                                     z=31.50097)  #In Angstroms
    lower_right_surface_atom = Struct(x=14.68505, y=15.51403, z=31.51654)
    placement_z_distance_from_surface = 1.8141  #The distance of our 'to place' molecule from the surface.

    #Molecule placement: Use 1 to indicate placement of the molecule, 0 is vacant spot. If you want to
    # rotate the molecule, enter a list that takes the arguments:
    # [1, (int) atom to rotate wrt, (str) axis, (float) degrees]
    # ie. To rotate EtOH molecule about its O molecule along the z axis 180 degrees:
    #     [1, 5, 'z', 180]
    molecule_placement = [[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1],
                          [1, 1, 1], [1, 1, 1]]

    ethanol_place = Surface_place()
    ethanol_place.set_filenames(surface_xyz_file, place_xyz_file,
                                output_xyz_file)
    ethanol_place.define_surface_sites(upper_left_surface_atom,
                                       lower_right_surface_atom,
                                       placement_z_distance_from_surface)
    ethanol_place.define_molecule_placement(molecule_placement)

    ethanol_place.initialize()
    ethanol_place.place()

    #Now save the surface and any placed molecules
    ethanol_place.surface.export(output_xyz_file)
    print 'Surface placed file created: ' + output_xyz_file
Пример #16
0
    def __init__(self, opcao):
        dado = []

        layout = [
            [sg.Text('Por favor, informe aqui os dados:')],
            [sg.Text('Chave', size=(30, 1)),
             sg.InputText()],
            [sg.Text('Data de Observação', size=(30, 1)),
             sg.InputText()],
            [
                sg.Text('Estado da Província (Se for na China)', size=(30, 1)),
                sg.InputText()
            ], [sg.Text('Região do País', size=(30, 1)),
                sg.InputText()],
            [sg.Text('Última Atualização', size=(30, 1)),
             sg.InputText()],
            [sg.Text('Casos confirmados', size=(30, 1)),
             sg.InputText()],
            [sg.Text('Número de mortos', size=(30, 1)),
             sg.InputText()],
            [sg.Text('Números de recuperados', size=(30, 1)),
             sg.InputText()], [sg.Submit(), sg.Cancel()]
        ]

        window_ins = sg.Window('Dados', layout)
        event, values = window_ins.read()

        print(event)

        if (event == 'Cancel'):
            window_ins.close()
        else:
            for i in values:
                dado.append(values[i])

            dado[0] = int(dado[0])

            aux2 = Struct.CovidLine(*dado)

            if (opcao == 1):
                estrutura.inserir(aux2)
            elif (opcao == 2):
                estrutura2.inserir(aux2)
            elif (opcao == 3):
                estrutura3.inserir(aux2)

            window_ins.close()
Пример #17
0
def getProductInfo(keyword):
    L = []
    url = 'https://www.bloomingdales.com/shop/search?keyword=%s' % keyword
    response = requests.get(url, headers={'User-Agent': "Mozilla/5.0"})
    soup = BeautifulSoup(response.content, 'html.parser')
    # print ('url: ' + str(url))
    # print ('status code: ' + str(response.status_code))
    # print ('encoding: ' + str(response.encoding))

    for product in soup.find_all('div', class_='productThumbnail'):
        # print ("---")
        product_url = 'https://www.bloomingdales.com' + product.find(
            'a', class_='productDescLink')['href']
        # picture_url = product.find('img', class_ = 'thumbnailImage')['src']
        source = product.find('source', class_='thumbnailImage')
        if source.get('srcset') == None:
            picture_url = source['data-srcset']
        else:
            picture_url = source['srcset']

        if product.find('span', class_='regular') == None: continue

        price = product.find('span', class_='regular').text.strip(' \t\n')
        # score = product.find('div', class_ = 'b-pdp-rating').find('div')['style']

        span_lis = product.find('div',
                                class_='productDescription').find_all('span')
        span_num = len(span_lis)
        if span_num == 1:
            brand = span_lis[0].string
            name = span_lis[0].next_sibling.string.strip(' \t\n')
        else:
            brand = span_lis[0].text
            name = span_lis[1].text + ' ' + span_lis[2].text

        product = Struct.Product(brand, name, float(price[1:]), product_url,
                                 picture_url, 4.0)
        L.append(product)
        #print (product_url)
        #print (picture_url)
        #print (price)
        # print (score)
        #print (brand)
        #print (name)

    return L
Пример #18
0
def main():
    stop_api_update = False
    number_of_api_updates = 4
    alarm = False
    user = get_user("ID_VALUE")  # "sign in" as user
    pprint.pprint(user['events'][0])
    next_event = Struct.Struct(**user['events']
                               [0])  # convert dict returned to Struct object

    next_event.travel_time = TrafficAPI.get_travel_time(
        next_event,
        False)  # call api to determine the average travel time (not delay)
    api_call_time = calculate_api_call(
        next_event)  # get time at which the api needs to be called
    print(f"api_call_time: {api_call_time}")
    usual_travel_delay = next_event.travel_time + next_event.buffer

    alarm_time = update_time(
        next_event.time,
        usual_travel_delay)  # calculate the regular alarm time (no delay)
    print(f"Alarm time : {alarm_time}")

    while alarm == False:
        current_time = datetime.datetime.now()

        if (current_time >= api_call_time) and not stop_api_update:
            travel_time_delay = TrafficAPI.get_travel_time(
                next_event,
                True)  # gets the travel time with live traffic (seconds)
            departure_time_delay = travel_time_delay + next_event.buffer
            alarm_time = update_time(next_event.time, departure_time_delay +
                                     next_event.buffer)  # update alarm time
            print(f"Alarm time updated to: {alarm_time}")

            # update the api call time so it is not continuatlly called
            api_call_time = calculate_api_call(next_event, api_call_time,
                                               number_of_api_updates)
            print(f"API call time updated to: {api_call_time}")
            number_of_api_updates -= 1
            if (number_of_api_updates == 1):
                stop_api_update = True

        if current_time >= alarm_time:
            print("Sound the Alarm")
            alarm = True
Пример #19
0
    def load_struct(self):
        print("info: loading file:" + self.path)
        # check exist file
        if not os.path.exists(self.path):
            try:
                raise ValueError("Error: fine not exist")
            except ValueError as e:
                traceback.print_exc()
        # check file type
        root, ext = os.path.splitext(self.path)
        if not ext == '.yml' or ext == '.yaml':
            try:
                raise ValueError("Error: file is not yaml file")
            except ValueError as e:
                traceback.print_exc()
        # get info
        try:
            with open(self.path) as file:
                # PyYaml Setting
                yaml.add_constructor(
                    yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
                    lambda loader, node: OrderedDict(
                        loader.construct_pairs(node)))
                obj = yaml.load(file)

                print(obj)
                # detect struct_name key
                for key in obj.keys():
                    if key == 'msg_id':
                        self.msg_id = obj[key]
                    else:
                        self.struct_name = key
                # create instance
                self.struct = st.Struct(self.struct_name, self.msg_id)
                # add variables
                for key in obj[self.struct_name].keys():
                    variable_name = key
                    variable_type = obj[self.struct_name][key]
                    # add
                    self.struct.add_variable(variable_name, variable_type)
        except Exception as e:
            traceback.print_exc()
Пример #20
0
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        label = tk.Label(self, text="Lista")
        label.grid(row=0, column=1, padx=5, pady=5)

        estrutura = ListaDinamica.CoviList()
        data = pd.read_csv("./covid_19_data.csv",
                           header=1,
                           na_filter=True,
                           na_values='nan',
                           keep_default_na=False)
        aux = list(range(len(data)))
        random.shuffle(aux)
        x = 0

        while (x < 100):
            dado = Struct.CovidLine(*data.loc[aux[x]])
            estrutura.inserir(dado)
            x += 1

        button1 = tk.Button(self,
                            text="Voltar",
                            command=lambda: controller.show_frame(Main))
        button1.grid(row=3, column=1, padx=5, pady=5)

        button2 = tk.Button(
            self,
            text="Imprimir na tela",
            command=lambda: controller.show_frame(P1).estrutura.visualizar())
        button2.grid(row=1, column=1, padx=5, pady=5)

        button3 = tk.Button(self,
                            text="Exportar para arquivo de texto",
                            command=lambda: controller.show_frame())
        button3.grid(row=2, column=1, padx=5, pady=5)
Пример #21
0
class PEFile(BinaryAccessor):
    class SectionInfo:
        def __init__(self):
            self.virtual_size = None
            self.virtual_address = None
            self.size_of_raw_data = None
            self.pointer_to_raw_data = None
            self.characteristics = None

    def __init__(self, data):
        self.data = data
        self.valid = False
        self.callbacks = []
        self.symbols_by_name = {}
        self.symbols_by_addr = {}
        if not self.is_pe():
            return

        try:
            self.tree = Struct(self.data)
            self.mz = self.tree.struct("MZ header", "mz")
            self.mz.uint16("magic")
            self.mz.uint16("lastsize")
            self.mz.uint16("nblocks")
            self.mz.uint16("nreloc")
            self.mz.uint16("hdrsize")
            self.mz.uint16("minalloc")
            self.mz.uint16("maxalloc")
            self.mz.uint16("ss")
            self.mz.uint16("sp")
            self.mz.uint16("checksum")
            self.mz.uint16("ip")
            self.mz.uint16("cs")
            self.mz.uint16("relocpos")
            self.mz.uint16("noverlay")
            self.mz.bytes(8, "reserved1")
            self.mz.uint16("oem_id")
            self.mz.uint16("oem_info")
            self.mz.bytes(20, "reserved2")
            self.mz.uint32("pe_offset")

            self.header = self.tree.struct("PE header", "header")
            self.header.seek(self.mz.pe_offset)
            self.header.uint32("magic")
            self.header.uint16("machine")
            self.header.uint16("section_count")
            self.header.uint32("timestamp")
            self.header.uint32("coff_symbol_table")
            self.header.uint32("coff_symbol_count")
            self.header.uint16("optional_header_size")
            self.header.uint16("characteristics")

            self.header.struct("Optional header", "opt")
            self.header.opt.uint16("magic")
            self.header.opt.uint8("major_linker_version")
            self.header.opt.uint8("minor_linker_version")
            self.header.opt.uint32("size_of_code")
            self.header.opt.uint32("size_of_init_data")
            self.header.opt.uint32("size_of_uninit_data")
            self.header.opt.uint32("address_of_entry")
            self.header.opt.uint32("base_of_code")

            if self.header.opt.magic == 0x10b:  # 32-bit
                self.bits = 32
                self.header.opt.uint32("base_of_data")
                self.header.opt.uint32("image_base")
                self.header.opt.uint32("section_align")
                self.header.opt.uint32("file_align")
                self.header.opt.uint16("major_os_version")
                self.header.opt.uint16("minor_os_version")
                self.header.opt.uint16("major_image_version")
                self.header.opt.uint16("minor_image_version")
                self.header.opt.uint16("major_subsystem_version")
                self.header.opt.uint16("minor_subsystem_version")
                self.header.opt.uint32("win32_version")
                self.header.opt.uint32("size_of_image")
                self.header.opt.uint32("size_of_headers")
                self.header.opt.uint32("checksum")
                self.header.opt.uint16("subsystem")
                self.header.opt.uint16("dll_characteristics")
                self.header.opt.uint32("size_of_stack_reserve")
                self.header.opt.uint32("size_of_stack_commit")
                self.header.opt.uint32("size_of_heap_reserve")
                self.header.opt.uint32("size_of_heap_commit")
                self.header.opt.uint32("loader_flags")
                self.header.opt.uint32("data_dir_count")
            elif self.header.opt.magic == 0x20b:  # 64-bit
                self.bits = 64
                self.header.opt.uint64("image_base")
                self.header.opt.uint32("section_align")
                self.header.opt.uint32("file_align")
                self.header.opt.uint16("major_os_version")
                self.header.opt.uint16("minor_os_version")
                self.header.opt.uint16("major_image_version")
                self.header.opt.uint16("minor_image_version")
                self.header.opt.uint16("major_subsystem_version")
                self.header.opt.uint16("minor_subsystem_version")
                self.header.opt.uint32("win32_version")
                self.header.opt.uint32("size_of_image")
                self.header.opt.uint32("size_of_headers")
                self.header.opt.uint32("checksum")
                self.header.opt.uint16("subsystem")
                self.header.opt.uint16("dll_characteristics")
                self.header.opt.uint64("size_of_stack_reserve")
                self.header.opt.uint64("size_of_stack_commit")
                self.header.opt.uint64("size_of_heap_reserve")
                self.header.opt.uint64("size_of_heap_commit")
                self.header.opt.uint32("loader_flags")
                self.header.opt.uint32("data_dir_count")
            else:
                self.valid = False
                return

            self.image_base = self.header.opt.image_base

            self.data_dirs = self.header.array(self.header.opt.data_dir_count,
                                               "data_dirs")
            for i in xrange(0, self.header.opt.data_dir_count):
                self.data_dirs[i].uint32("virtual_address")
                self.data_dirs[i].uint32("size")

            self.sections = []
            header_section_obj = PEFile.SectionInfo()
            header_section_obj.virtual_size = self.header.opt.size_of_headers
            header_section_obj.virtual_address = 0
            header_section_obj.size_of_raw_data = self.header.opt.size_of_headers
            header_section_obj.pointer_to_raw_data = 0
            header_section_obj.characteristics = 0
            self.sections.append(header_section_obj)

            self.tree.array(self.header.section_count, "sections")
            for i in xrange(0, self.header.section_count):
                section = self.tree.sections[i]
                section.seek(self.mz.pe_offset +
                             self.header.optional_header_size + 24 + (i * 40))
                section.bytes(8, "name")
                section.uint32("virtual_size")
                section.uint32("virtual_address")
                section.uint32("size_of_raw_data")
                section.uint32("pointer_to_raw_data")
                section.uint32("pointer_to_relocs")
                section.uint32("pointer_to_line_numbers")
                section.uint16("reloc_count")
                section.uint16("line_number_count")
                section.uint32("characteristics")

                section_obj = PEFile.SectionInfo()
                section_obj.virtual_size = section.virtual_size
                section_obj.virtual_address = section.virtual_address & ~(
                    self.header.opt.section_align - 1)
                section_obj.size_of_raw_data = section.size_of_raw_data
                section_obj.pointer_to_raw_data = section.pointer_to_raw_data & ~(
                    self.header.opt.file_align - 1)
                section_obj.characteristics = section.characteristics
                self.sections.append(section_obj)

            self.symbols_by_name["_start"] = self.entry()
            self.symbols_by_addr[self.entry()] = "_start"

            if self.header.opt.data_dir_count >= 2:
                self.imports = self.tree.array(0, "imports")
                for i in xrange(0, self.data_dirs[1].size / 20):
                    if self.read(
                            self.image_base +
                            self.data_dirs[1].virtual_address + (i * 20),
                            4) == "\0\0\0\0":
                        break
                    if self.read(
                            self.image_base +
                            self.data_dirs[1].virtual_address + (i * 20) + 16,
                            4) == "\0\0\0\0":
                        break
                    self.imports.append()
                    dll = self.imports[i]
                    dll.seek(
                        self.virtual_address_to_file_offset(
                            self.image_base +
                            self.data_dirs[1].virtual_address) + (i * 20))
                    dll.uint32("lookup")
                    dll.uint32("timestamp")
                    dll.uint32("forward_chain")
                    dll.uint32("name")
                    dll.uint32("iat")

                for dll in self.imports:
                    name = self.read_string(self.image_base +
                                            dll.name).split('.')
                    if len(name) > 1:
                        name = '.'.join(name[0:-1])
                    else:
                        name = name[0]

                    entry_ofs = self.image_base + dll.lookup
                    iat_ofs = self.image_base + dll.iat
                    while True:
                        if self.bits == 32:
                            entry = self.read_uint32(entry_ofs)
                            is_ordinal = (entry & 0x80000000) != 0
                            entry &= 0x7fffffff
                        else:
                            entry = self.read_uint64(entry_ofs)
                            is_ordinal = (entry & 0x8000000000000000) != 0
                            entry &= 0x7fffffffffffffff

                        if (not is_ordinal) and (entry == 0):
                            break

                        if is_ordinal:
                            func = name + "!Ordinal%d" % (entry & 0xffff)
                        else:
                            func = name + "!" + self.read_string(
                                self.image_base + entry + 2)

                        self.symbols_by_name[func] = iat_ofs
                        self.symbols_by_addr[iat_ofs] = func

                        entry_ofs += self.bits / 8
                        iat_ofs += self.bits / 8

            if (self.header.opt.data_dir_count >=
                    1) and (self.data_dirs[0].size >= 40):
                self.exports = self.tree.struct("Export directory", "exports")
                self.exports.seek(
                    self.virtual_address_to_file_offset(
                        self.image_base + self.data_dirs[0].virtual_address))
                self.exports.uint32("characteristics")
                self.exports.uint32("timestamp")
                self.exports.uint16("major_version")
                self.exports.uint16("minor_version")
                self.exports.uint32("dll_name")
                self.exports.uint32("base")
                self.exports.uint32("function_count")
                self.exports.uint32("name_count")
                self.exports.uint32("address_of_functions")
                self.exports.uint32("address_of_names")
                self.exports.uint32("address_of_name_ordinals")

                self.exports.array(self.exports.function_count, "functions")
                for i in xrange(0, self.exports.function_count):
                    self.exports.functions[i].seek(
                        self.virtual_address_to_file_offset(
                            self.image_base +
                            self.exports.address_of_functions) + (i * 4))
                    self.exports.functions[i].uint32("address")

                self.exports.array(self.exports.name_count, "names")
                for i in xrange(0, self.exports.name_count):
                    self.exports.names[i].seek(
                        self.virtual_address_to_file_offset(
                            self.image_base + self.exports.address_of_names) +
                        (i * 4))
                    self.exports.names[i].uint32("address_of_name")

                self.exports.array(self.exports.name_count, "name_ordinals")
                for i in xrange(0, self.exports.name_count):
                    self.exports.name_ordinals[i].seek(
                        self.virtual_address_to_file_offset(
                            self.image_base +
                            self.exports.address_of_name_ordinals) + (i * 2))
                    self.exports.name_ordinals[i].uint16("ordinal")

                for i in xrange(0, self.exports.name_count):
                    function_index = self.exports.name_ordinals[
                        i].ordinal - self.exports.base
                    address = self.image_base + self.exports.functions[
                        function_index].address
                    name = self.read_string(
                        self.image_base +
                        self.exports.names[i].address_of_name)

                    self.symbols_by_addr[address] = name
                    self.symbols_by_name[name] = address

            self.tree.complete()
            self.valid = True
        except:
            self.valid = False

        if self.valid:
            self.data.add_callback(self)

    def read_string(self, addr):
        result = ""
        while True:
            ch = self.read(addr, 1)
            addr += 1
            if (len(ch) == 0) or (ch == '\0'):
                break
            result += ch
        return result

    def virtual_address_to_file_offset(self, addr):
        for i in self.sections:
            if ((addr >= (self.image_base + i.virtual_address)) and
                (addr <
                 (self.image_base + i.virtual_address + i.virtual_size))) and (
                     i.virtual_size != 0):
                cur = i
        if cur == None:
            return None
        ofs = addr - (self.image_base + cur.virtual_address)
        return cur.pointer_to_raw_data + ofs

    def read(self, ofs, len):
        result = ""
        while len > 0:
            cur = None
            for i in self.sections:
                if ((ofs >= (self.image_base + i.virtual_address)) and
                    (ofs <
                     (self.image_base + i.virtual_address + i.virtual_size))
                    ) and (i.virtual_size != 0):
                    cur = i
            if cur == None:
                break

            prog_ofs = ofs - (self.image_base + cur.virtual_address)
            mem_len = cur.virtual_size - prog_ofs
            file_len = cur.size_of_raw_data - prog_ofs
            if mem_len > len:
                mem_len = len
            if file_len > len:
                file_len = len

            if file_len <= 0:
                result += "\x00" * mem_len
                len -= mem_len
                ofs += mem_len
                continue

            result += self.data.read(cur.pointer_to_raw_data + prog_ofs,
                                     file_len)
            len -= file_len
            ofs += file_len

        return result

    def next_valid_addr(self, ofs):
        result = -1
        for i in self.sections:
            if ((self.image_base + i.virtual_address) >=
                    ofs) and (i.virtual_size != 0) and ((result == -1) or (
                        (self.image_base + i.virtual_address) < result)):
                result = self.image_base + i.virtual_address
        return result

    def get_modification(self, ofs, len):
        result = []
        while len > 0:
            cur = None
            for i in self.sections:
                if ((ofs >= (self.image_base + i.virtual_address)) and
                    (ofs <
                     (self.image_base + i.virtual_address + i.virtual_size))
                    ) and (i.virtual_size != 0):
                    cur = i
            if cur == None:
                break

            prog_ofs = ofs - (self.image_base + cur.virtual_address)
            mem_len = cur.virtual_size - prog_ofs
            file_len = cur.size_of_raw_data - prog_ofs
            if mem_len > len:
                mem_len = len
            if file_len > len:
                file_len = len

            if file_len <= 0:
                result += [DATA_ORIGINAL] * mem_len
                len -= mem_len
                ofs += mem_len
                continue

            result += self.data.get_modification(
                cur.pointer_to_raw_data + prog_ofs, file_len)
            len -= file_len
            ofs += file_len

        return result

    def write(self, ofs, data):
        result = 0
        while len(data) > 0:
            cur = None
            for i in self.sections:
                if ((ofs >= (self.image_base + i.virtual_address)) and
                    (ofs <
                     (self.image_base + i.virtual_address + i.virtual_size))
                    ) and (i.virtual_size != 0):
                    cur = i
            if cur == None:
                break

            prog_ofs = ofs - (self.image_base + cur.virtual_address)
            mem_len = cur.virtual_size - prog_ofs
            file_len = cur.size_of_raw_data - prog_ofs
            if mem_len > len:
                mem_len = len
            if file_len > len:
                file_len = len

            if file_len <= 0:
                break

            result += self.data.write(cur.pointer_to_raw_data + prog_ofs,
                                      data[0:file_len])
            data = data[file_len:]
            ofs += file_len

        return result

    def insert(self, ofs, data):
        return 0

    def remove(self, ofs, size):
        return 0

    def notify_data_write(self, data, ofs, contents):
        # Find sections that hold data backed by updated regions of the file
        for i in self.sections:
            if ((ofs + len(contents)) > i.pointer_to_raw_data) and (
                    ofs < (i.pointer_to_raw_data + i.size_of_raw_data)) and (
                        i.virtual_size != 0):
                # This section has been updated, compute which region has been changed
                from_start = ofs - i.pointer_to_raw_data
                data_ofs = 0
                length = len(contents)
                if from_start < 0:
                    length += from_start
                    data_ofs -= from_start
                    from_start = 0
                if (from_start + length) > i.size_of_raw_data:
                    length = i.size_of_raw_data - from_start

                # Notify callbacks
                if length > 0:
                    for cb in self.callbacks:
                        if hasattr(cb, "notify_data_write"):
                            cb.notify_data_write(
                                self, self.image_base + i.virtual_address +
                                from_start,
                                contents[data_ofs:(data_ofs + length)])

    def save(self, filename):
        self.data.save(filename)

    def start(self):
        return self.image_base

    def entry(self):
        return self.image_base + self.header.opt.address_of_entry

    def __len__(self):
        max = None
        for i in self.sections:
            if ((max == None) or
                ((self.image_base + i.virtual_address + i.virtual_size) >
                 max)) and (i.virtual_size != 0):
                max = self.image_base + i.virtual_address + i.virtual_size
        return max - self.start()

    def is_pe(self):
        if self.data.read(0, 2) != "MZ":
            return False
        ofs = self.data.read(0x3c, 4)
        if len(ofs) != 4:
            return False
        ofs = struct.unpack("<I", ofs)[0]
        if self.data.read(ofs, 4) != "PE\0\0":
            return False
        magic = self.data.read(ofs + 24, 2)
        if len(magic) != 2:
            return False
        magic = struct.unpack("<H", magic)[0]
        return (magic == 0x10b) or (magic == 0x20b)

    def architecture(self):
        if self.header.machine == 0x14c:
            return "x86"
        if self.header.machine == 0x8664:
            return "x86_64"
        if self.header.machine == 0x166:
            return "mips"
        if self.header.machine == 0x266:
            return "mips16"
        if self.header.machine == 0x366:
            return "mips"
        if self.header.machine == 0x466:
            return "mips16"
        if self.header.machine == 0x1f0:
            return "ppc"
        if self.header.machine == 0x1f1:
            return "ppc"
        if self.header.machine == 0x1c0:
            return "arm"
        if self.header.machine == 0x1c2:
            return "thumb"
        if self.header.machine == 0x1c4:
            return "thumb"
        if self.header.machine == 0xaa64:
            return "arm64"
        if self.header.machine == 0x200:
            return "ia64"
        return None

    def decorate_plt_name(self, name):
        return name + "@IAT"

    def create_symbol(self, addr, name):
        self.symbols_by_name[name] = addr
        self.symbols_by_addr[addr] = name

    def delete_symbol(self, addr, name):
        if name in self.symbols_by_name:
            del (self.symbols_by_name[name])
        if addr in self.symbols_by_addr:
            del (self.symbols_by_addr[addr])

    def add_callback(self, cb):
        self.callbacks.append(cb)

    def remove_callback(self, cb):
        self.callbacks.remove(cb)

    def is_modified(self):
        return self.data.is_modified()

    def find(self, regex, addr):
        while (addr < self.end()) and (addr != -1):
            data = self.read(addr, 0xfffffffff)
            match = regex.search(data)
            if match != None:
                return match.start() + addr

            addr += len(data)
            addr = self.next_valid_addr(addr)

        return -1

    def has_undo_actions(self):
        return self.data.has_undo_actions()

    def commit_undo(self, before_loc, after_loc):
        self.data.commit_undo(before_loc, after_loc)

    def undo(self):
        self.data.undo()

    def redo(self):
        self.data.redo()
Пример #22
0
    def __init__(self, data):
        self.data = data
        self.valid = False
        self.callbacks = []
        self.symbols_by_name = {}
        self.symbols_by_addr = {}
        if not self.is_pe():
            return

        try:
            self.tree = Struct(self.data)
            self.mz = self.tree.struct("MZ header", "mz")
            self.mz.uint16("magic")
            self.mz.uint16("lastsize")
            self.mz.uint16("nblocks")
            self.mz.uint16("nreloc")
            self.mz.uint16("hdrsize")
            self.mz.uint16("minalloc")
            self.mz.uint16("maxalloc")
            self.mz.uint16("ss")
            self.mz.uint16("sp")
            self.mz.uint16("checksum")
            self.mz.uint16("ip")
            self.mz.uint16("cs")
            self.mz.uint16("relocpos")
            self.mz.uint16("noverlay")
            self.mz.bytes(8, "reserved1")
            self.mz.uint16("oem_id")
            self.mz.uint16("oem_info")
            self.mz.bytes(20, "reserved2")
            self.mz.uint32("pe_offset")

            self.header = self.tree.struct("PE header", "header")
            self.header.seek(self.mz.pe_offset)
            self.header.uint32("magic")
            self.header.uint16("machine")
            self.header.uint16("section_count")
            self.header.uint32("timestamp")
            self.header.uint32("coff_symbol_table")
            self.header.uint32("coff_symbol_count")
            self.header.uint16("optional_header_size")
            self.header.uint16("characteristics")

            self.header.struct("Optional header", "opt")
            self.header.opt.uint16("magic")
            self.header.opt.uint8("major_linker_version")
            self.header.opt.uint8("minor_linker_version")
            self.header.opt.uint32("size_of_code")
            self.header.opt.uint32("size_of_init_data")
            self.header.opt.uint32("size_of_uninit_data")
            self.header.opt.uint32("address_of_entry")
            self.header.opt.uint32("base_of_code")

            if self.header.opt.magic == 0x10b:  # 32-bit
                self.bits = 32
                self.header.opt.uint32("base_of_data")
                self.header.opt.uint32("image_base")
                self.header.opt.uint32("section_align")
                self.header.opt.uint32("file_align")
                self.header.opt.uint16("major_os_version")
                self.header.opt.uint16("minor_os_version")
                self.header.opt.uint16("major_image_version")
                self.header.opt.uint16("minor_image_version")
                self.header.opt.uint16("major_subsystem_version")
                self.header.opt.uint16("minor_subsystem_version")
                self.header.opt.uint32("win32_version")
                self.header.opt.uint32("size_of_image")
                self.header.opt.uint32("size_of_headers")
                self.header.opt.uint32("checksum")
                self.header.opt.uint16("subsystem")
                self.header.opt.uint16("dll_characteristics")
                self.header.opt.uint32("size_of_stack_reserve")
                self.header.opt.uint32("size_of_stack_commit")
                self.header.opt.uint32("size_of_heap_reserve")
                self.header.opt.uint32("size_of_heap_commit")
                self.header.opt.uint32("loader_flags")
                self.header.opt.uint32("data_dir_count")
            elif self.header.opt.magic == 0x20b:  # 64-bit
                self.bits = 64
                self.header.opt.uint64("image_base")
                self.header.opt.uint32("section_align")
                self.header.opt.uint32("file_align")
                self.header.opt.uint16("major_os_version")
                self.header.opt.uint16("minor_os_version")
                self.header.opt.uint16("major_image_version")
                self.header.opt.uint16("minor_image_version")
                self.header.opt.uint16("major_subsystem_version")
                self.header.opt.uint16("minor_subsystem_version")
                self.header.opt.uint32("win32_version")
                self.header.opt.uint32("size_of_image")
                self.header.opt.uint32("size_of_headers")
                self.header.opt.uint32("checksum")
                self.header.opt.uint16("subsystem")
                self.header.opt.uint16("dll_characteristics")
                self.header.opt.uint64("size_of_stack_reserve")
                self.header.opt.uint64("size_of_stack_commit")
                self.header.opt.uint64("size_of_heap_reserve")
                self.header.opt.uint64("size_of_heap_commit")
                self.header.opt.uint32("loader_flags")
                self.header.opt.uint32("data_dir_count")
            else:
                self.valid = False
                return

            self.image_base = self.header.opt.image_base

            self.data_dirs = self.header.array(self.header.opt.data_dir_count,
                                               "data_dirs")
            for i in xrange(0, self.header.opt.data_dir_count):
                self.data_dirs[i].uint32("virtual_address")
                self.data_dirs[i].uint32("size")

            self.sections = []
            header_section_obj = PEFile.SectionInfo()
            header_section_obj.virtual_size = self.header.opt.size_of_headers
            header_section_obj.virtual_address = 0
            header_section_obj.size_of_raw_data = self.header.opt.size_of_headers
            header_section_obj.pointer_to_raw_data = 0
            header_section_obj.characteristics = 0
            self.sections.append(header_section_obj)

            self.tree.array(self.header.section_count, "sections")
            for i in xrange(0, self.header.section_count):
                section = self.tree.sections[i]
                section.seek(self.mz.pe_offset +
                             self.header.optional_header_size + 24 + (i * 40))
                section.bytes(8, "name")
                section.uint32("virtual_size")
                section.uint32("virtual_address")
                section.uint32("size_of_raw_data")
                section.uint32("pointer_to_raw_data")
                section.uint32("pointer_to_relocs")
                section.uint32("pointer_to_line_numbers")
                section.uint16("reloc_count")
                section.uint16("line_number_count")
                section.uint32("characteristics")

                section_obj = PEFile.SectionInfo()
                section_obj.virtual_size = section.virtual_size
                section_obj.virtual_address = section.virtual_address & ~(
                    self.header.opt.section_align - 1)
                section_obj.size_of_raw_data = section.size_of_raw_data
                section_obj.pointer_to_raw_data = section.pointer_to_raw_data & ~(
                    self.header.opt.file_align - 1)
                section_obj.characteristics = section.characteristics
                self.sections.append(section_obj)

            self.symbols_by_name["_start"] = self.entry()
            self.symbols_by_addr[self.entry()] = "_start"

            if self.header.opt.data_dir_count >= 2:
                self.imports = self.tree.array(0, "imports")
                for i in xrange(0, self.data_dirs[1].size / 20):
                    if self.read(
                            self.image_base +
                            self.data_dirs[1].virtual_address + (i * 20),
                            4) == "\0\0\0\0":
                        break
                    if self.read(
                            self.image_base +
                            self.data_dirs[1].virtual_address + (i * 20) + 16,
                            4) == "\0\0\0\0":
                        break
                    self.imports.append()
                    dll = self.imports[i]
                    dll.seek(
                        self.virtual_address_to_file_offset(
                            self.image_base +
                            self.data_dirs[1].virtual_address) + (i * 20))
                    dll.uint32("lookup")
                    dll.uint32("timestamp")
                    dll.uint32("forward_chain")
                    dll.uint32("name")
                    dll.uint32("iat")

                for dll in self.imports:
                    name = self.read_string(self.image_base +
                                            dll.name).split('.')
                    if len(name) > 1:
                        name = '.'.join(name[0:-1])
                    else:
                        name = name[0]

                    entry_ofs = self.image_base + dll.lookup
                    iat_ofs = self.image_base + dll.iat
                    while True:
                        if self.bits == 32:
                            entry = self.read_uint32(entry_ofs)
                            is_ordinal = (entry & 0x80000000) != 0
                            entry &= 0x7fffffff
                        else:
                            entry = self.read_uint64(entry_ofs)
                            is_ordinal = (entry & 0x8000000000000000) != 0
                            entry &= 0x7fffffffffffffff

                        if (not is_ordinal) and (entry == 0):
                            break

                        if is_ordinal:
                            func = name + "!Ordinal%d" % (entry & 0xffff)
                        else:
                            func = name + "!" + self.read_string(
                                self.image_base + entry + 2)

                        self.symbols_by_name[func] = iat_ofs
                        self.symbols_by_addr[iat_ofs] = func

                        entry_ofs += self.bits / 8
                        iat_ofs += self.bits / 8

            if (self.header.opt.data_dir_count >=
                    1) and (self.data_dirs[0].size >= 40):
                self.exports = self.tree.struct("Export directory", "exports")
                self.exports.seek(
                    self.virtual_address_to_file_offset(
                        self.image_base + self.data_dirs[0].virtual_address))
                self.exports.uint32("characteristics")
                self.exports.uint32("timestamp")
                self.exports.uint16("major_version")
                self.exports.uint16("minor_version")
                self.exports.uint32("dll_name")
                self.exports.uint32("base")
                self.exports.uint32("function_count")
                self.exports.uint32("name_count")
                self.exports.uint32("address_of_functions")
                self.exports.uint32("address_of_names")
                self.exports.uint32("address_of_name_ordinals")

                self.exports.array(self.exports.function_count, "functions")
                for i in xrange(0, self.exports.function_count):
                    self.exports.functions[i].seek(
                        self.virtual_address_to_file_offset(
                            self.image_base +
                            self.exports.address_of_functions) + (i * 4))
                    self.exports.functions[i].uint32("address")

                self.exports.array(self.exports.name_count, "names")
                for i in xrange(0, self.exports.name_count):
                    self.exports.names[i].seek(
                        self.virtual_address_to_file_offset(
                            self.image_base + self.exports.address_of_names) +
                        (i * 4))
                    self.exports.names[i].uint32("address_of_name")

                self.exports.array(self.exports.name_count, "name_ordinals")
                for i in xrange(0, self.exports.name_count):
                    self.exports.name_ordinals[i].seek(
                        self.virtual_address_to_file_offset(
                            self.image_base +
                            self.exports.address_of_name_ordinals) + (i * 2))
                    self.exports.name_ordinals[i].uint16("ordinal")

                for i in xrange(0, self.exports.name_count):
                    function_index = self.exports.name_ordinals[
                        i].ordinal - self.exports.base
                    address = self.image_base + self.exports.functions[
                        function_index].address
                    name = self.read_string(
                        self.image_base +
                        self.exports.names[i].address_of_name)

                    self.symbols_by_addr[address] = name
                    self.symbols_by_name[name] = address

            self.tree.complete()
            self.valid = True
        except:
            self.valid = False

        if self.valid:
            self.data.add_callback(self)
Пример #23
0
    def bands(self, args):
        """
		This method performs the band structure calculations.
		"""
        dest_dir = "bands"
        dummy_broaden = 1.0

        # spin polarized calculation?
        sp = args.sp

        # creating directory for bands
        if os.path.exists("bands"):
            print("bands directory already exists.")
        else:
            os.makedirs("bands")

        # copy Sig.out into /bands
        if self.checksig():
            shutil.copyfile("./ac/Sig.out", "./bands/Sig1.out")
        else:
            sys.exit()

        # interpolating
        self.interpol(args.emin, args.emax, args.rom, dummy_broaden, dest_dir,
                      sp)

        #############################Xingu's contribution###################################################################################

        #########################Read POSCAR ###################################################
        TB = Struct.TBstructure("POSCAR", p["atomnames"], p["orbs"])
        cor_at = p["cor_at"]
        cor_orb = p["cor_orb"]
        TB.Compute_cor_idx(cor_at, cor_orb)
        ############################# Generate sequence list ##################################
        sort_atm = sorted([atm for atms in cor_at for atm in atms])
        atm_sqn = self.Make_coor_list(sort_atm, cor_at)
        print("atm_sqn:%s" % atm_sqn)
        orb_sqn = []
        for i in atm_sqn:
            len_sf = 0
            for j in range(i):
                len_sf += max(
                    Make_coor_list(TB.TB_orbs[cor_at[j][0]], cor_orb[j])) + 1
            orb_idx = self.Make_coor_list(TB.TB_orbs[cor_at[i][0]], cor_orb[i])
            for idx in orb_idx:
                orb_sqn.append(idx + len_sf)
        print("orb_sqn:%s" % orb_sqn)
        ################################# Read sig.inp_real ####################################

        # non spin-polarized calculation
        if sp == False:

            fi = open("./bands/sig.inp_real", "r")
            [nom, ncor_orb] = [int(ele) for ele in fi.readline().split()[-2:]]
            temperature = float(fi.readline().split()[-1])
            sigmdc_tmp = [
                float(ele) for ele in fi.readline().split()[-1 * ncor_orb:]
            ]
            sigoo = fi.readline().split()
            vdc = fi.readline().split()
            sig_real = np.array([ele.split() for ele in fi.readlines()])
            fi.close()
            assert np.shape(sig_real)[0] == nom
            ###################### Write SigMoo ##################
            SigMoo = np.zeros((nom, len(orb_sqn) * 2 + 1), dtype=float)
            SigMoo[:, 0] = np.array(sig_real[:, 0])
            for i in range(len(orb_sqn)):
                SigMoo[:, i * 2 + 1] = np.array(sig_real[:,
                                                         orb_sqn[i] * 2 + 1])
                SigMoo[:, i * 2 + 2] = np.array(sig_real[:,
                                                         orb_sqn[i] * 2 + 2])
            np.savetxt("./bands/SigMoo_real.out", SigMoo, fmt="%.10f")
            ############################ Srite SigMdc.out ########################
            SigMdc = np.array([sigmdc_tmp[i] for i in orb_sqn])
            np.savetxt("./bands/SigMdc.out", SigMdc[None], fmt="%.12f")

        # spin polarized calculation
        if sp:

            siginpreal_files = ["sig.inp_real", "sig.inp_real_dn"]
            SigMooreal_files = ["SigMoo_real.out", "SigMoo_dn_real.out"]
            SigMdc_files = ["SigMdc.out", "SigMdc_dn.out"]

            for filecounter in range(len(siginpreal_files)):
                filestr = "./bands/" + siginpreal_files[filecounter]
                fi = open(filestr, "r")
                [nom,
                 ncor_orb] = [int(ele) for ele in fi.readline().split()[-2:]]
                temperature = float(fi.readline().split()[-1])
                sigmdc_tmp = [
                    float(ele) for ele in fi.readline().split()[-1 * ncor_orb:]
                ]
                sigoo = fi.readline().split()
                vdc = fi.readline().split()
                sig_real = np.array([ele.split() for ele in fi.readlines()])
                fi.close()
                assert np.shape(sig_real)[0] == nom
                ###################### Write SigMoo ##################
                SigMoo = np.zeros((nom, len(orb_sqn) * 2 + 1), dtype=float)
                SigMoo[:, 0] = np.array(sig_real[:, 0])
                for i in range(len(orb_sqn)):
                    SigMoo[:,
                           i * 2 + 1] = np.array(sig_real[:,
                                                          orb_sqn[i] * 2 + 1])
                    SigMoo[:,
                           i * 2 + 2] = np.array(sig_real[:,
                                                          orb_sqn[i] * 2 + 2])
                filestr = "./bands/" + SigMooreal_files[filecounter]
                np.savetxt(filestr, SigMoo, fmt="%.10f")
                ############################ Write SigMdc.out ########################
                SigMdc = np.array([sigmdc_tmp[i] for i in orb_sqn])
                print(sigmdc_tmp)
                print(SigMdc)
                filestr = "./bands/" + SigMdc_files[filecounter]
                np.savetxt(filestr, SigMdc[None], fmt="%.12f")

        # ################################################################################################################

        print("kpband=%s" % args.kpband)
        print("kplist=%s" % args.kplist)
        print("knames=%s" % args.knames)

        # generating k-path
        klist, dist_K, dist_SK = self.Create_kpath(args.kplist, args.kpband)
        fi = open("./bands/klist.dat", "w")
        for i in range(args.kpband):
            kcheck = 0
            for j, d in enumerate(dist_SK):
                if abs(dist_K[i] - d) < 1e-10:
                    fi.write("%.14f  %.14f  %.14f  %.14f  %s \n" % (
                        dist_K[i],
                        klist[i][0],
                        klist[i][1],
                        klist[i][2],
                        args.knames[j],
                    ))
                    kcheck = 1
                    break
            if kcheck == 0:
                fi.write("%.14f  %.14f  %.14f  %.14f \n" %
                         (dist_K[i], klist[i][0], klist[i][1], klist[i][2]))
        print("k-path generated.")
        fi.close()

        # copying files from DMFT directory to dos directory
        cmd = "cd bands && Copy_input.py ../ -post bands"
        out, err = subprocess.Popen(cmd, shell=True).communicate()
        if err:
            print("File copy failed!\n")
            print(err)
            sys.exit()
        else:
            print(out)

        # generating ksum.input
        self.genksum(args.rom, args.kpband)

        # running dmft_ksum_band

        if args.plotplain:
            print("\nCalculating plain band structure...")
            cmd = "cd bands && " + self.para_com + " " + "dmft_ksum_band"
            out, err = subprocess.Popen(cmd,
                                        shell=True,
                                        stdout=subprocess.PIPE,
                                        stderr=subprocess.PIPE).communicate()
            if err:
                print(err)
                print("Band structure calculation failed!\n")
                sys.exit()
            else:
                print("Band structure calculation complete.\n")
                self.plot_plain_bands(args)

        if args.plotpartial:
            print("\nCalculating projected band structure...")
            cmd = "cd bands && " + self.para_com + " " + "dmft_ksum_partial_band"
            out, err = subprocess.Popen(cmd,
                                        shell=True,
                                        stdout=subprocess.PIPE,
                                        stderr=subprocess.PIPE).communicate()
            if err:
                print(err)
                print("Band structure calculation failed!\n")
                sys.exit()
            else:
                print("Band structure calculation complete.\n")
                self.plot_partial_bands(args)

        if sp:
            print("\nCalculating spin-polarized band structure...")
            cmd = "cd bands && " + self.para_com + " " + "dmft_ksum_band"
            out, err = subprocess.Popen(cmd,
                                        shell=True,
                                        stdout=subprocess.PIPE,
                                        stderr=subprocess.PIPE).communicate()
            if err:
                print(err)
                print("Band structure calculation failed!\n")
                sys.exit()
            else:
                print("Band structure calculation complete.\n")
                self.plot_sp_bands(args)
Пример #24
0
	def __init__(self, data):
		self.data = data
		self.valid = False
		self.callbacks = []
		self.symbols_by_name = {}
		self.symbols_by_addr = {}
		if not self.is_pe():
			return

		try:
			self.tree = Struct(self.data)
			self.mz = self.tree.struct("MZ header", "mz")
			self.mz.uint16("magic")
			self.mz.uint16("lastsize")
			self.mz.uint16("nblocks")
			self.mz.uint16("nreloc")
			self.mz.uint16("hdrsize")
			self.mz.uint16("minalloc")
			self.mz.uint16("maxalloc")
			self.mz.uint16("ss")
			self.mz.uint16("sp")
			self.mz.uint16("checksum")
			self.mz.uint16("ip")
			self.mz.uint16("cs")
			self.mz.uint16("relocpos")
			self.mz.uint16("noverlay")
			self.mz.bytes(8, "reserved1")
			self.mz.uint16("oem_id")
			self.mz.uint16("oem_info")
			self.mz.bytes(20, "reserved2")
			self.mz.uint32("pe_offset")

			self.header = self.tree.struct("PE header", "header")
			self.header.seek(self.mz.pe_offset)
			self.header.uint32("magic")
			self.header.uint16("machine")
			self.header.uint16("section_count")
			self.header.uint32("timestamp")
			self.header.uint32("coff_symbol_table")
			self.header.uint32("coff_symbol_count")
			self.header.uint16("optional_header_size")
			self.header.uint16("characteristics")

			self.header.struct("Optional header", "opt")
			self.header.opt.uint16("magic")
			self.header.opt.uint8("major_linker_version")
			self.header.opt.uint8("minor_linker_version")
			self.header.opt.uint32("size_of_code")
			self.header.opt.uint32("size_of_init_data")
			self.header.opt.uint32("size_of_uninit_data")
			self.header.opt.uint32("address_of_entry")
			self.header.opt.uint32("base_of_code")

			if self.header.opt.magic == 0x10b: # 32-bit
				self.bits = 32
				self.header.opt.uint32("base_of_data")
				self.header.opt.uint32("image_base")
				self.header.opt.uint32("section_align")
				self.header.opt.uint32("file_align")
				self.header.opt.uint16("major_os_version")
				self.header.opt.uint16("minor_os_version")
				self.header.opt.uint16("major_image_version")
				self.header.opt.uint16("minor_image_version")
				self.header.opt.uint16("major_subsystem_version")
				self.header.opt.uint16("minor_subsystem_version")
				self.header.opt.uint32("win32_version")
				self.header.opt.uint32("size_of_image")
				self.header.opt.uint32("size_of_headers")
				self.header.opt.uint32("checksum")
				self.header.opt.uint16("subsystem")
				self.header.opt.uint16("dll_characteristics")
				self.header.opt.uint32("size_of_stack_reserve")
				self.header.opt.uint32("size_of_stack_commit")
				self.header.opt.uint32("size_of_heap_reserve")
				self.header.opt.uint32("size_of_heap_commit")
				self.header.opt.uint32("loader_flags")
				self.header.opt.uint32("data_dir_count")
			elif self.header.opt.magic == 0x20b: # 64-bit
				self.bits = 64
				self.header.opt.uint64("image_base")
				self.header.opt.uint32("section_align")
				self.header.opt.uint32("file_align")
				self.header.opt.uint16("major_os_version")
				self.header.opt.uint16("minor_os_version")
				self.header.opt.uint16("major_image_version")
				self.header.opt.uint16("minor_image_version")
				self.header.opt.uint16("major_subsystem_version")
				self.header.opt.uint16("minor_subsystem_version")
				self.header.opt.uint32("win32_version")
				self.header.opt.uint32("size_of_image")
				self.header.opt.uint32("size_of_headers")
				self.header.opt.uint32("checksum")
				self.header.opt.uint16("subsystem")
				self.header.opt.uint16("dll_characteristics")
				self.header.opt.uint64("size_of_stack_reserve")
				self.header.opt.uint64("size_of_stack_commit")
				self.header.opt.uint64("size_of_heap_reserve")
				self.header.opt.uint64("size_of_heap_commit")
				self.header.opt.uint32("loader_flags")
				self.header.opt.uint32("data_dir_count")
			else:
				self.valid = False
				return

			self.image_base = self.header.opt.image_base

			self.data_dirs = self.header.array(self.header.opt.data_dir_count, "data_dirs")
			for i in xrange(0, self.header.opt.data_dir_count):
				self.data_dirs[i].uint32("virtual_address")
				self.data_dirs[i].uint32("size")

			self.sections = []
			header_section_obj = PEFile.SectionInfo()
			header_section_obj.virtual_size = self.header.opt.size_of_headers
			header_section_obj.virtual_address = 0
			header_section_obj.size_of_raw_data = self.header.opt.size_of_headers
			header_section_obj.pointer_to_raw_data = 0
			header_section_obj.characteristics = 0
			self.sections.append(header_section_obj)

			self.tree.array(self.header.section_count, "sections")
			for i in xrange(0, self.header.section_count):
				section = self.tree.sections[i]
				section.seek(self.mz.pe_offset + self.header.optional_header_size + 24 + (i * 40))
				section.bytes(8, "name")
				section.uint32("virtual_size")
				section.uint32("virtual_address")
				section.uint32("size_of_raw_data")
				section.uint32("pointer_to_raw_data")
				section.uint32("pointer_to_relocs")
				section.uint32("pointer_to_line_numbers")
				section.uint16("reloc_count")
				section.uint16("line_number_count")
				section.uint32("characteristics")

				section_obj = PEFile.SectionInfo()
				section_obj.virtual_size = section.virtual_size
				section_obj.virtual_address = section.virtual_address & ~(self.header.opt.section_align - 1)
				section_obj.size_of_raw_data = section.size_of_raw_data
				section_obj.pointer_to_raw_data = section.pointer_to_raw_data & ~(self.header.opt.file_align - 1)
				section_obj.characteristics = section.characteristics
				self.sections.append(section_obj)

			self.symbols_by_name["_start"] = self.entry()
			self.symbols_by_addr[self.entry()] = "_start"

			if self.header.opt.data_dir_count >= 2:
				self.imports = self.tree.array(0, "imports")
				for i in xrange(0, self.data_dirs[1].size / 20):
					if self.read(self.image_base + self.data_dirs[1].virtual_address + (i * 20), 4) == "\0\0\0\0":
						break
					if self.read(self.image_base + self.data_dirs[1].virtual_address + (i * 20) + 16, 4) == "\0\0\0\0":
						break
					self.imports.append()
					dll = self.imports[i]
					dll.seek(self.virtual_address_to_file_offset(self.image_base + self.data_dirs[1].virtual_address) + (i * 20))
					dll.uint32("lookup")
					dll.uint32("timestamp")
					dll.uint32("forward_chain")
					dll.uint32("name")
					dll.uint32("iat")

				for dll in self.imports:
					name = self.read_string(self.image_base + dll.name).split('.')
					if len(name) > 1:
						name = '.'.join(name[0:-1])
					else:
						name = name[0]

					entry_ofs = self.image_base + dll.lookup
					iat_ofs = self.image_base + dll.iat
					while True:
						if self.bits == 32:
							entry = self.read_uint32(entry_ofs)
							is_ordinal = (entry & 0x80000000) != 0
							entry &= 0x7fffffff
						else:
							entry = self.read_uint64(entry_ofs)
							is_ordinal = (entry & 0x8000000000000000) != 0
							entry &= 0x7fffffffffffffff

						if (not is_ordinal) and (entry == 0):
							break

						if is_ordinal:
							func = name + "!Ordinal%d" % (entry & 0xffff)
						else:
							func = name + "!" + self.read_string(self.image_base + entry + 2)

						self.symbols_by_name[func] = iat_ofs
						self.symbols_by_addr[iat_ofs] = func

						entry_ofs += self.bits / 8
						iat_ofs += self.bits / 8

			if (self.header.opt.data_dir_count >= 1) and (self.data_dirs[0].size >= 40):
				self.exports = self.tree.struct("Export directory", "exports")
				self.exports.seek(self.virtual_address_to_file_offset(self.image_base + self.data_dirs[0].virtual_address))
				self.exports.uint32("characteristics")
				self.exports.uint32("timestamp")
				self.exports.uint16("major_version")
				self.exports.uint16("minor_version")
				self.exports.uint32("dll_name")
				self.exports.uint32("base")
				self.exports.uint32("function_count")
				self.exports.uint32("name_count")
				self.exports.uint32("address_of_functions")
				self.exports.uint32("address_of_names")
				self.exports.uint32("address_of_name_ordinals")

				self.exports.array(self.exports.function_count, "functions")
				for i in xrange(0, self.exports.function_count):
					self.exports.functions[i].seek(self.virtual_address_to_file_offset(self.image_base + self.exports.address_of_functions) + (i * 4))
					self.exports.functions[i].uint32("address")

				self.exports.array(self.exports.name_count, "names")
				for i in xrange(0, self.exports.name_count):
					self.exports.names[i].seek(self.virtual_address_to_file_offset(self.image_base + self.exports.address_of_names) + (i * 4))
					self.exports.names[i].uint32("address_of_name")

				self.exports.array(self.exports.name_count, "name_ordinals")
				for i in xrange(0, self.exports.name_count):
					self.exports.name_ordinals[i].seek(self.virtual_address_to_file_offset(self.image_base + self.exports.address_of_name_ordinals) + (i * 2))
					self.exports.name_ordinals[i].uint16("ordinal")

				for i in xrange(0, self.exports.name_count):
					function_index = self.exports.name_ordinals[i].ordinal - self.exports.base
					address = self.image_base + self.exports.functions[function_index].address
					name = self.read_string(self.image_base + self.exports.names[i].address_of_name)

					self.symbols_by_addr[address] = name
					self.symbols_by_name[name] = address

			self.tree.complete()
			self.valid = True
		except:
			self.valid = False

		if self.valid:
			self.data.add_callback(self)
Пример #25
0
    def __init__(self):
        layout = [[
            sg.Button('Adicionar novo dado'),
            sg.Button('Remover item')
        ], [sg.Button('Ver dados')],
                  [sg.Button('Exportar para arquivo de texto')],
                  [sg.Button('Voltar')]]

        win = sg.Window('Opções', layout)

        event, value = win.read()
        if (event == 'Ver dados'):
            win.close()
            janela1_tel()

        elif (event == 'Adicionar novo dado'):
            dado = []

            layout = [
                [sg.Text('Por favor, informe aqui os dados:')],
                [sg.Text('Chave', size=(30, 1)),
                 sg.InputText()],
                [sg.Text('Data de Observação', size=(30, 1)),
                 sg.InputText()],
                [
                    sg.Text('Estado da Província (Se for na China)',
                            size=(30, 1)),
                    sg.InputText()
                ], [sg.Text('Região do País', size=(30, 1)),
                    sg.InputText()],
                [sg.Text('Última Atualização', size=(30, 1)),
                 sg.InputText()],
                [sg.Text('Casos confirmados', size=(30, 1)),
                 sg.InputText()],
                [sg.Text('Número de mortos', size=(30, 1)),
                 sg.InputText()],
                [
                    sg.Text('Números de recuperados', size=(30, 1)),
                    sg.InputText()
                ], [sg.Submit(), sg.Cancel()]
            ]

            window_ins = sg.Window('Dados', layout)
            event, values = window_ins.read()
            window_ins.close()

            for i in values:
                dado.append(values[i])

            aux2 = Struct.CovidLine(*dado)

            for i in values:
                dado.append(i)

            estrutura.inserir(aux2)

            win.close()
            janela1()

        elif (event == 'Remover item'):
            item = estrutura.excluir()
            pop_done = sg.popup("Removido um item 1 item da estrutura")
            win.close()
            janela1_tel()

        elif (event == 'Exportar para arquivo de texto'):
            outf = open("saida.txt", "w")
            estrutura.escrever(outf)
            sg.popup('Arquivo criado com sucesso!')
            win.close()
            janela1()

        elif (event == 'Voltar'):
            win.close()
            Main()

        elif (event == sg.WIN_CLOSED):
            win.close()
Пример #26
0
    def __init__(self):
        layout = [[
            sg.Button('Lista'),
            sg.Button('Fila'),
            sg.Button('Pilha'),
            sg.Button('Árvore'),
            sg.Button('Grafo')
        ], [sg.Button('Fechar')]]

        win = sg.Window('Projeto de ED', layout)
        event, value = win.read()
        random.shuffle(aux)
        global estrutura
        global estrutura2
        global estrutura3

        if (event == sg.WIN_CLOSED or event == 'Fechar'):
            win.close()

        elif (event == "Lista"):
            estrutura = ListaDinamica.CoviList()
            x = 0
            while (x < 100):
                dado = Struct.CovidLine(*data.loc[aux[x]])
                estrutura.inserir(dado)
                x += 1
            win.close()
            janela_lista()

        elif (event == "Fila"):
            estrutura = Fila.CoviList()
            x = 0
            while (x < 100):
                dado = Struct.CovidLine(*data.loc[aux[x]])
                estrutura.inserir(dado)
                x += 1
            win.close()
            janela1()

        elif (event == "Pilha"):
            estrutura = Pilha.CoviList()
            x = 0
            while (x < 100):
                dado = Struct.CovidLine(*data.loc[aux[x]])
                estrutura.inserir(dado)
                x += 1
            win.close()
            janela1()

        elif (event == "Árvore"):
            texto = '''Escolha a lista dos dados que irão ser escolhidos para cada árvore:
0-Somente a chave
1-Data da observação
2-Província (China)
3-País
4-Última atualização
5-Casos Confirmados
6-Número de mortos
7-Número de recuperados
            '''
            for i in range(3):
                aux2 = sg.popup_get_text(
                    message=texto,
                    title="Digite uma chave da árvore {0}:".format(i + 1))

                while (aux2 == None or aux2.isnumeric() == False
                       or not (int(aux2) in range(8))):
                    aux2 = sg.popup_get_text(
                        message=texto,
                        title="Digite uma chave da árvore {0}:".format(i + 1))

                if (i == 0):
                    estrutura = Arvore.CoviList(int(aux2))
                    x = 0
                    while (x < 100):
                        dado = Struct.CovidLine(*data.loc[aux[x]])
                        estrutura.inserir(dado)
                        x += 1

                if (i == 1):
                    estrutura2 = Arvore.CoviList(int(aux2))
                    x = 0
                    while (x < 100):
                        dado = Struct.CovidLine(*data.loc[aux[x]])
                        estrutura2.inserir(dado)
                        x += 1

                if (i == 2):
                    estrutura3 = Arvore.CoviList(int(aux2))
                    x = 0
                    while (x < 100):
                        dado = Struct.CovidLine(*data.loc[aux[x]])
                        estrutura3.inserir(dado)
                        x += 1
            win.close()
            janela_arvore()

        elif (event == 'Grafo'):
            estrutura = Grafo.Grafo()

            for node in Struct.starwars["nodes"]:
                estrutura.add_vertice(node)

            for link in Struct.starwars["links"]:
                estrutura.add_aresta(link["source"], link["target"],
                                     10.0 / link["value"])
                estrutura.add_aresta(link["target"], link["source"],
                                     10.0 / link["value"])

            win.close()
            janela_grafo()
Пример #27
0
class PEFile(BinaryAccessor):
	class SectionInfo:
		def __init__(self):
			self.virtual_size = None
			self.virtual_address = None
			self.size_of_raw_data = None
			self.pointer_to_raw_data = None
			self.characteristics = None

	def __init__(self, data):
		self.data = data
		self.valid = False
		self.callbacks = []
		self.symbols_by_name = {}
		self.symbols_by_addr = {}
		if not self.is_pe():
			return

		try:
			self.tree = Struct(self.data)
			self.mz = self.tree.struct("MZ header", "mz")
			self.mz.uint16("magic")
			self.mz.uint16("lastsize")
			self.mz.uint16("nblocks")
			self.mz.uint16("nreloc")
			self.mz.uint16("hdrsize")
			self.mz.uint16("minalloc")
			self.mz.uint16("maxalloc")
			self.mz.uint16("ss")
			self.mz.uint16("sp")
			self.mz.uint16("checksum")
			self.mz.uint16("ip")
			self.mz.uint16("cs")
			self.mz.uint16("relocpos")
			self.mz.uint16("noverlay")
			self.mz.bytes(8, "reserved1")
			self.mz.uint16("oem_id")
			self.mz.uint16("oem_info")
			self.mz.bytes(20, "reserved2")
			self.mz.uint32("pe_offset")

			self.header = self.tree.struct("PE header", "header")
			self.header.seek(self.mz.pe_offset)
			self.header.uint32("magic")
			self.header.uint16("machine")
			self.header.uint16("section_count")
			self.header.uint32("timestamp")
			self.header.uint32("coff_symbol_table")
			self.header.uint32("coff_symbol_count")
			self.header.uint16("optional_header_size")
			self.header.uint16("characteristics")

			self.header.struct("Optional header", "opt")
			self.header.opt.uint16("magic")
			self.header.opt.uint8("major_linker_version")
			self.header.opt.uint8("minor_linker_version")
			self.header.opt.uint32("size_of_code")
			self.header.opt.uint32("size_of_init_data")
			self.header.opt.uint32("size_of_uninit_data")
			self.header.opt.uint32("address_of_entry")
			self.header.opt.uint32("base_of_code")

			if self.header.opt.magic == 0x10b: # 32-bit
				self.bits = 32
				self.header.opt.uint32("base_of_data")
				self.header.opt.uint32("image_base")
				self.header.opt.uint32("section_align")
				self.header.opt.uint32("file_align")
				self.header.opt.uint16("major_os_version")
				self.header.opt.uint16("minor_os_version")
				self.header.opt.uint16("major_image_version")
				self.header.opt.uint16("minor_image_version")
				self.header.opt.uint16("major_subsystem_version")
				self.header.opt.uint16("minor_subsystem_version")
				self.header.opt.uint32("win32_version")
				self.header.opt.uint32("size_of_image")
				self.header.opt.uint32("size_of_headers")
				self.header.opt.uint32("checksum")
				self.header.opt.uint16("subsystem")
				self.header.opt.uint16("dll_characteristics")
				self.header.opt.uint32("size_of_stack_reserve")
				self.header.opt.uint32("size_of_stack_commit")
				self.header.opt.uint32("size_of_heap_reserve")
				self.header.opt.uint32("size_of_heap_commit")
				self.header.opt.uint32("loader_flags")
				self.header.opt.uint32("data_dir_count")
			elif self.header.opt.magic == 0x20b: # 64-bit
				self.bits = 64
				self.header.opt.uint64("image_base")
				self.header.opt.uint32("section_align")
				self.header.opt.uint32("file_align")
				self.header.opt.uint16("major_os_version")
				self.header.opt.uint16("minor_os_version")
				self.header.opt.uint16("major_image_version")
				self.header.opt.uint16("minor_image_version")
				self.header.opt.uint16("major_subsystem_version")
				self.header.opt.uint16("minor_subsystem_version")
				self.header.opt.uint32("win32_version")
				self.header.opt.uint32("size_of_image")
				self.header.opt.uint32("size_of_headers")
				self.header.opt.uint32("checksum")
				self.header.opt.uint16("subsystem")
				self.header.opt.uint16("dll_characteristics")
				self.header.opt.uint64("size_of_stack_reserve")
				self.header.opt.uint64("size_of_stack_commit")
				self.header.opt.uint64("size_of_heap_reserve")
				self.header.opt.uint64("size_of_heap_commit")
				self.header.opt.uint32("loader_flags")
				self.header.opt.uint32("data_dir_count")
			else:
				self.valid = False
				return

			self.image_base = self.header.opt.image_base

			self.data_dirs = self.header.array(self.header.opt.data_dir_count, "data_dirs")
			for i in xrange(0, self.header.opt.data_dir_count):
				self.data_dirs[i].uint32("virtual_address")
				self.data_dirs[i].uint32("size")

			self.sections = []
			header_section_obj = PEFile.SectionInfo()
			header_section_obj.virtual_size = self.header.opt.size_of_headers
			header_section_obj.virtual_address = 0
			header_section_obj.size_of_raw_data = self.header.opt.size_of_headers
			header_section_obj.pointer_to_raw_data = 0
			header_section_obj.characteristics = 0
			self.sections.append(header_section_obj)

			self.tree.array(self.header.section_count, "sections")
			for i in xrange(0, self.header.section_count):
				section = self.tree.sections[i]
				section.seek(self.mz.pe_offset + self.header.optional_header_size + 24 + (i * 40))
				section.bytes(8, "name")
				section.uint32("virtual_size")
				section.uint32("virtual_address")
				section.uint32("size_of_raw_data")
				section.uint32("pointer_to_raw_data")
				section.uint32("pointer_to_relocs")
				section.uint32("pointer_to_line_numbers")
				section.uint16("reloc_count")
				section.uint16("line_number_count")
				section.uint32("characteristics")

				section_obj = PEFile.SectionInfo()
				section_obj.virtual_size = section.virtual_size
				section_obj.virtual_address = section.virtual_address & ~(self.header.opt.section_align - 1)
				section_obj.size_of_raw_data = section.size_of_raw_data
				section_obj.pointer_to_raw_data = section.pointer_to_raw_data & ~(self.header.opt.file_align - 1)
				section_obj.characteristics = section.characteristics
				self.sections.append(section_obj)

			self.symbols_by_name["_start"] = self.entry()
			self.symbols_by_addr[self.entry()] = "_start"

			if self.header.opt.data_dir_count >= 2:
				self.imports = self.tree.array(0, "imports")
				for i in xrange(0, self.data_dirs[1].size / 20):
					if self.read(self.image_base + self.data_dirs[1].virtual_address + (i * 20), 4) == "\0\0\0\0":
						break
					if self.read(self.image_base + self.data_dirs[1].virtual_address + (i * 20) + 16, 4) == "\0\0\0\0":
						break
					self.imports.append()
					dll = self.imports[i]
					dll.seek(self.virtual_address_to_file_offset(self.image_base + self.data_dirs[1].virtual_address) + (i * 20))
					dll.uint32("lookup")
					dll.uint32("timestamp")
					dll.uint32("forward_chain")
					dll.uint32("name")
					dll.uint32("iat")

				for dll in self.imports:
					name = self.read_string(self.image_base + dll.name).split('.')
					if len(name) > 1:
						name = '.'.join(name[0:-1])
					else:
						name = name[0]

					entry_ofs = self.image_base + dll.lookup
					iat_ofs = self.image_base + dll.iat
					while True:
						if self.bits == 32:
							entry = self.read_uint32(entry_ofs)
							is_ordinal = (entry & 0x80000000) != 0
							entry &= 0x7fffffff
						else:
							entry = self.read_uint64(entry_ofs)
							is_ordinal = (entry & 0x8000000000000000) != 0
							entry &= 0x7fffffffffffffff

						if (not is_ordinal) and (entry == 0):
							break

						if is_ordinal:
							func = name + "!Ordinal%d" % (entry & 0xffff)
						else:
							func = name + "!" + self.read_string(self.image_base + entry + 2)

						self.symbols_by_name[func] = iat_ofs
						self.symbols_by_addr[iat_ofs] = func

						entry_ofs += self.bits / 8
						iat_ofs += self.bits / 8

			if (self.header.opt.data_dir_count >= 1) and (self.data_dirs[0].size >= 40):
				self.exports = self.tree.struct("Export directory", "exports")
				self.exports.seek(self.virtual_address_to_file_offset(self.image_base + self.data_dirs[0].virtual_address))
				self.exports.uint32("characteristics")
				self.exports.uint32("timestamp")
				self.exports.uint16("major_version")
				self.exports.uint16("minor_version")
				self.exports.uint32("dll_name")
				self.exports.uint32("base")
				self.exports.uint32("function_count")
				self.exports.uint32("name_count")
				self.exports.uint32("address_of_functions")
				self.exports.uint32("address_of_names")
				self.exports.uint32("address_of_name_ordinals")

				self.exports.array(self.exports.function_count, "functions")
				for i in xrange(0, self.exports.function_count):
					self.exports.functions[i].seek(self.virtual_address_to_file_offset(self.image_base + self.exports.address_of_functions) + (i * 4))
					self.exports.functions[i].uint32("address")

				self.exports.array(self.exports.name_count, "names")
				for i in xrange(0, self.exports.name_count):
					self.exports.names[i].seek(self.virtual_address_to_file_offset(self.image_base + self.exports.address_of_names) + (i * 4))
					self.exports.names[i].uint32("address_of_name")

				self.exports.array(self.exports.name_count, "name_ordinals")
				for i in xrange(0, self.exports.name_count):
					self.exports.name_ordinals[i].seek(self.virtual_address_to_file_offset(self.image_base + self.exports.address_of_name_ordinals) + (i * 2))
					self.exports.name_ordinals[i].uint16("ordinal")

				for i in xrange(0, self.exports.name_count):
					function_index = self.exports.name_ordinals[i].ordinal - self.exports.base
					address = self.image_base + self.exports.functions[function_index].address
					name = self.read_string(self.image_base + self.exports.names[i].address_of_name)

					self.symbols_by_addr[address] = name
					self.symbols_by_name[name] = address

			self.tree.complete()
			self.valid = True
		except:
			self.valid = False

		if self.valid:
			self.data.add_callback(self)

	def read_string(self, addr):
		result = ""
		while True:
			ch = self.read(addr, 1)
			addr += 1
			if (len(ch) == 0) or (ch == '\0'):
				break
			result += ch
		return result

	def virtual_address_to_file_offset(self, addr):
		for i in self.sections:
			if ((addr >= (self.image_base + i.virtual_address)) and (addr < (self.image_base + i.virtual_address + i.virtual_size))) and (i.virtual_size != 0):
				cur = i
		if cur == None:
			return None
		ofs = addr - (self.image_base + cur.virtual_address)
		return cur.pointer_to_raw_data + ofs

	def read(self, ofs, len):
		result = ""
		while len > 0:
			cur = None
			for i in self.sections:
				if ((ofs >= (self.image_base + i.virtual_address)) and (ofs < (self.image_base + i.virtual_address + i.virtual_size))) and (i.virtual_size != 0):
					cur = i
			if cur == None:
				break

			prog_ofs = ofs - (self.image_base + cur.virtual_address)
			mem_len = cur.virtual_size - prog_ofs
			file_len = cur.size_of_raw_data - prog_ofs
			if mem_len > len:
				mem_len = len
			if file_len > len:
				file_len = len

			if file_len <= 0:
				result += "\x00" * mem_len
				len -= mem_len
				ofs += mem_len
				continue

			result += self.data.read(cur.pointer_to_raw_data + prog_ofs, file_len)
			len -= file_len
			ofs += file_len

		return result

	def next_valid_addr(self, ofs):
		result = -1
		for i in self.sections:
			if ((self.image_base + i.virtual_address) >= ofs) and (i.virtual_size != 0) and ((result == -1) or ((self.image_base + i.virtual_address) < result)):
				result = self.image_base + i.virtual_address
		return result

	def get_modification(self, ofs, len):
		result = []
		while len > 0:
			cur = None
			for i in self.sections:
				if ((ofs >= (self.image_base + i.virtual_address)) and (ofs < (self.image_base + i.virtual_address + i.virtual_size))) and (i.virtual_size != 0):
					cur = i
			if cur == None:
				break

			prog_ofs = ofs - (self.image_base + cur.virtual_address)
			mem_len = cur.virtual_size - prog_ofs
			file_len = cur.size_of_raw_data - prog_ofs
			if mem_len > len:
				mem_len = len
			if file_len > len:
				file_len = len

			if file_len <= 0:
				result += [DATA_ORIGINAL] * mem_len
				len -= mem_len
				ofs += mem_len
				continue

			result += self.data.get_modification(cur.pointer_to_raw_data + prog_ofs, file_len)
			len -= file_len
			ofs += file_len

		return result

	def write(self, ofs, data):
		result = 0
		while len(data) > 0:
			cur = None
			for i in self.sections:
				if ((ofs >= (self.image_base + i.virtual_address)) and (ofs < (self.image_base + i.virtual_address + i.virtual_size))) and (i.virtual_size != 0):
					cur = i
			if cur == None:
				break

			prog_ofs = ofs - (self.image_base + cur.virtual_address)
			mem_len = cur.virtual_size - prog_ofs
			file_len = cur.size_of_raw_data - prog_ofs
			if mem_len > len:
				mem_len = len
			if file_len > len:
				file_len = len

			if file_len <= 0:
				break

			result += self.data.write(cur.pointer_to_raw_data + prog_ofs, data[0:file_len])
			data = data[file_len:]
			ofs += file_len

		return result

	def insert(self, ofs, data):
		return 0

	def remove(self, ofs, size):
		return 0

	def notify_data_write(self, data, ofs, contents):
		# Find sections that hold data backed by updated regions of the file
		for i in self.sections:
			if ((ofs + len(contents)) > i.pointer_to_raw_data) and (ofs < (i.pointer_to_raw_data + i.size_of_raw_data)) and (i.virtual_size != 0):
				# This section has been updated, compute which region has been changed
				from_start = ofs - i.pointer_to_raw_data
				data_ofs = 0
				length = len(contents)
				if from_start < 0:
					length += from_start
					data_ofs -= from_start
					from_start = 0
				if (from_start + length) > i.size_of_raw_data:
					length = i.size_of_raw_data - from_start

				# Notify callbacks
				if length > 0:
					for cb in self.callbacks:
						if hasattr(cb, "notify_data_write"):
							cb.notify_data_write(self, self.image_base + i.virtual_address + from_start,
								contents[data_ofs:(data_ofs + length)])

	def save(self, filename):
		self.data.save(filename)

	def start(self):
		return self.image_base

	def entry(self):
		return self.image_base + self.header.opt.address_of_entry

	def __len__(self):
		max = None
		for i in self.sections:
			if ((max == None) or ((self.image_base + i.virtual_address + i.virtual_size) > max)) and (i.virtual_size != 0):
				max = self.image_base + i.virtual_address + i.virtual_size
		return max - self.start()

	def is_pe(self):
		if self.data.read(0, 2) != "MZ":
			return False
		ofs = self.data.read(0x3c, 4)
		if len(ofs) != 4:
			return False
		ofs = struct.unpack("<I", ofs)[0]
		if self.data.read(ofs, 4) != "PE\0\0":
			return False
		magic = self.data.read(ofs + 24, 2)
		if len(magic) != 2:
			return False
		magic = struct.unpack("<H", magic)[0]
		return (magic == 0x10b) or (magic == 0x20b)

	def architecture(self):
		if self.header.machine == 0x14c:
			return "x86"
		if self.header.machine == 0x8664:
			return "x86_64"
		if self.header.machine == 0x166:
			return "mips"
		if self.header.machine == 0x266:
			return "mips16"
		if self.header.machine == 0x366:
			return "mips"
		if self.header.machine == 0x466:
			return "mips16"
		if self.header.machine == 0x1f0:
			return "ppc"
		if self.header.machine == 0x1f1:
			return "ppc"
		if self.header.machine == 0x1c0:
			return "arm"
		if self.header.machine == 0x1c2:
			return "thumb"
		if self.header.machine == 0x1c4:
			return "thumb"
		if self.header.machine == 0xaa64:
			return "arm64"
		if self.header.machine == 0x200:
			return "ia64"
		return None

	def decorate_plt_name(self, name):
		return name + "@IAT"

	def create_symbol(self, addr, name):
		self.symbols_by_name[name] = addr
		self.symbols_by_addr[addr] = name

	def delete_symbol(self, addr, name):
		if name in self.symbols_by_name:
			del(self.symbols_by_name[name])
		if addr in self.symbols_by_addr:
			del(self.symbols_by_addr[addr])

	def add_callback(self, cb):
		self.callbacks.append(cb)

	def remove_callback(self, cb):
		self.callbacks.remove(cb)

	def is_modified(self):
		return self.data.is_modified()

	def find(self, regex, addr):
		while (addr < self.end()) and (addr != -1):
			data = self.read(addr, 0xfffffffff)
			match = regex.search(data)
			if match != None:
				return match.start() + addr

			addr += len(data)
			addr = self.next_valid_addr(addr)

		return -1

	def has_undo_actions(self):
		return self.data.has_undo_actions()

	def commit_undo(self, before_loc, after_loc):
		self.data.commit_undo(before_loc, after_loc)

	def undo(self):
		self.data.undo()

	def redo(self):
		self.data.redo()
Пример #28
0
    else:
        print "ksum.input is missing"
        exit()

    if nspin != 2:
        print "nspin must be 2 for HF calculations"
        exit()
    if ncor_orb != len(SigMdc):
        print "ncor_orb is not consistent with SigMdc"
        exit()
    if ncor_orb / norb != len(U):
        print "ncor_orb is not consistent with U"
        exit()

    execfile('INPUT.py')
    TB = Struct.TBstructure('POSCAR', p['atomnames'], p['orbs'])
    cor_at = p['cor_at']
    cor_orb = p['cor_orb']
    #TB.Compute_cor_idx(cor_at,cor_orb)

    if rank == 0:
        WAN = WAN90.WANNIER('wannier90')
        WAN.load_chk(p['path_bin'])
        WAN.load_eig()
        WAN.Compute_HamR0()
        ntet, kibz, tetkptr, tet, gptr, tet_idx = Get_Tetra(
            WAN.recip_latt, WAN.mp_grid, WAN.ikpt)
        ed = []
        for i, ats in enumerate(cor_at):
            ed.append([])
            for j, orbs in enumerate(cor_orb[i]):
Пример #29
0
def getProductInfo(keyword):
    # https://www.ulta.com/ulta/a/_/Ntt-temp/
    searchUrl = 'https://www.ulta.com/ulta/a/_/Ntt-' + keyword

    # headless
    chrome_options = Options()
    chrome_options.add_argument('--headless')
    # chrome_options.add_argument('--window-size=1920,1080');
    driver = webdriver.Chrome(executable_path='./chromedriver',
                              chrome_options=chrome_options)

    driver.get(searchUrl)
    time.sleep(0.5)  # Delays for 0.5 seconds.

    # Move five times can scroll to the end (the height of the website is 4284px)
    for i in range(5):
        driver.execute_script("window.scrollBy(0,1000)")
        time.sleep(0.3)
    html = driver.page_source

    # from bs4 import BeautifulSoup
    soup = BeautifulSoup(html.encode('utf-8'), 'html.parser')
    products = soup.findAll('div', {'class': 'productQvContainer'})

    # Brand
    productsBrands = []
    for product in products:
        temp = product.findAll('h4', {'class': 'prod-title'})
        productsBrand = temp[0].findAll('a', {})[0].contents[0].encode(
            'utf-8', 'ignore').strip()
        productsBrands.append(productsBrand)
    # print productsBrands[0];

    # Name
    productsNames = []
    for product in products:
        temp = product.findAll('p', {'class': 'prod-desc'})
        productsName = temp[0].findAll('a', {})[0].contents[0].encode(
            'utf-8', 'ignore').strip()
        productsNames.append(productsName)
    # print productsNames[0];

    # URL
    productsUrls = []
    for product in products:
        temp = product.findAll('p', {'class': 'prod-desc'})
        productsUrl = temp[0].findAll('a', {})[0].get('href')
        productsUrls.append(productsUrl)
    # print productsUrls[0];

    # Price
    productsPrices = []
    for product in products:
        temp = product.findAll('span', {'class': 'regPrice'})
        productsPrice = 0
        # Deal with promotion
        if len(temp) == 0:
            temp = product.findAll('span', {'class': 'pro-new-price'})
        productsPrice = temp[0].contents[0].encode('utf-8', 'ignore').strip()
        productsPrices.append(productsPrice)
    # print prices[0];

    # IMG_URL
    imageUrls = []
    for product in products:
        temp = product.findAll('a', {'class': 'product'})
        imageUrl = temp[0].findAll('img',
                                   {})[0].get('src').encode('utf-8',
                                                            'ignore').strip()
        imageUrls.append(imageUrl)
    # print imageUrls[0];

    # Rating
    productsRatings = []
    for product in products:
        temp = product.findAll('label', {'class': 'sr-only'})
        productsRating = temp[0].contents[0].encode('utf-8', 'ignore').strip()
        productsRating = productsRating.split()[0]
        productsRatings.append(productsRating)
    # print productsRatings[0];

    L = []
    baseUrl = 'https://www.ulta.com'
    for i in range(len(productsNames)):
        price = productsPrices[i]
        price = price.decode('utf-8')
        if '-' in price:
            price = price[0:price.find('-') - 1]
        price = price[price.find('$') + 1:]
        product = Struct.Product(productsBrands[i].decode('utf-8'),
                                 productsNames[i].decode('utf-8'),
                                 float(price), baseUrl + productsUrls[i],
                                 imageUrls[i].decode('utf-8'),
                                 float(productsRatings[i]))
        # print 'Brand:' + productsBrands[i];
        # print 'Product Name:' + productsNames[i];
        # print 'Product Url:' + productsUrls[i];
        # print 'Price:' + productsPrices[i];
        # print 'Image Url:' + imageUrls[i];
        # print 'Rating:' + productsRatings[i];
        # print '-----------'
        L.append(product)

    driver.close()
    return L
Пример #30
0
	def __init__(self, data):
		self.data = data
		self.valid = False
		self.callbacks = []
		self.symbols_by_name = {}
		self.symbols_by_addr = {}
		self.plt = {}
		if not self.is_macho():
			return

		try:
			self.tree = Struct(self.data)
			self.header = self.tree.struct("Mach-O header", "header")

			self.header.uint32_le("magic")
			if (self.header.magic == 0xfeedface) or (self.header.magic == 0xfeedfacf):
				self.header.uint32_le("cputype")
				self.header.uint32_le("cpusubtype")
				self.header.uint32_le("filetype")
				self.header.uint32_le("cmds")
				self.header.uint32_le("cmdsize")
				self.header.uint32_le("flags")
				if self.header.magic == 0xfeedfacf:
					self.header.uint32_le("reserved")
					self.bits = 64
				else:
					self.bits = 32
				self.big_endian = False
			elif (self.header.magic == 0xcefaedfe) or (self.header.magic == 0xcffaedfe):
				self.header.uint32_be("cputype")
				self.header.uint32_be("cpusubtype")
				self.header.uint32_be("filetype")
				self.header.uint32_be("cmds")
				self.header.uint32_be("cmdsize")
				self.header.uint32_be("flags")
				if self.header.magic == 0xcffaedfe:
					self.header.uint32_be("reserved")
					self.bits = 64
				else:
					self.bits = 32
				self.big_endian = True

			self.symbol_table = None
			self.dynamic_symbol_table = None

			# Parse loader commands
			self.commands = self.tree.array(self.header.cmds, "commands")
			self.segments = []
			self.sections = []
			offset = self.header.getSize()
			for i in xrange(0, self.header.cmds):
				cmd = self.commands[i]
				cmd.seek(offset)
				if self.big_endian:
					cmd.uint32_be("cmd")
					cmd.uint32_be("size")
				else:
					cmd.uint32_le("cmd")
					cmd.uint32_le("size")

				if cmd.cmd == 1: # SEGMENT
					cmd.bytes(16, "name")
					if self.big_endian:
						cmd.uint32_be("vmaddr")
						cmd.uint32_be("vmsize")
						cmd.uint32_be("fileoff")
						cmd.uint32_be("filesize")
						cmd.uint32_be("maxprot")
						cmd.uint32_be("initprot")
						cmd.uint32_be("nsects")
						cmd.uint32_be("flags")
					else:
						cmd.uint32_le("vmaddr")
						cmd.uint32_le("vmsize")
						cmd.uint32_le("fileoff")
						cmd.uint32_le("filesize")
						cmd.uint32_le("maxprot")
						cmd.uint32_le("initprot")
						cmd.uint32_le("nsects")
						cmd.uint32_le("flags")

					if cmd.initprot != 0: # Ignore __PAGE_ZERO or anything like it
						self.segments.append(cmd)

					cmd.array(cmd.nsects, "sections")
					for i in xrange(0, cmd.nsects):
						section = cmd.sections[i]
						section.bytes(16, "name")
						section.bytes(16, "segment")
						if self.big_endian:
							section.uint32_be("addr")
							section.uint32_be("size")
							section.uint32_be("offset")
							section.uint32_be("align")
							section.uint32_be("reloff")
							section.uint32_be("nreloc")
							section.uint32_be("flags")
							section.uint32_be("reserved1")
							section.uint32_be("reserved2")
						else:
							section.uint32_le("addr")
							section.uint32_le("size")
							section.uint32_le("offset")
							section.uint32_le("align")
							section.uint32_le("reloff")
							section.uint32_le("nreloc")
							section.uint32_le("flags")
							section.uint32_le("reserved1")
							section.uint32_le("reserved2")
						self.sections.append(section)

					for i in xrange(0, cmd.nsects):
						section = cmd.sections[i]
						section.array(section.nreloc, "relocs")
						for j in xrange(0, section.nreloc):
							reloc = section.relocs[j]
							reloc.seek(section.reloff + (j * 8))
							if self.big_endian:
								reloc.uint32_be("addr")
								reloc.uint32_be("value")
							else:
								reloc.uint32_le("addr")
								reloc.uint32_le("value")
				elif cmd.cmd == 25: # SEGMENT_64
					cmd.bytes(16, "name")
					if self.big_endian:
						cmd.uint64_be("vmaddr")
						cmd.uint64_be("vmsize")
						cmd.uint64_be("fileoff")
						cmd.uint64_be("filesize")
						cmd.uint32_be("maxprot")
						cmd.uint32_be("initprot")
						cmd.uint32_be("nsects")
						cmd.uint32_be("flags")
					else:
						cmd.uint64_le("vmaddr")
						cmd.uint64_le("vmsize")
						cmd.uint64_le("fileoff")
						cmd.uint64_le("filesize")
						cmd.uint32_le("maxprot")
						cmd.uint32_le("initprot")
						cmd.uint32_le("nsects")
						cmd.uint32_le("flags")

					if cmd.initprot != 0: # Ignore __PAGE_ZERO or anything like it
						self.segments.append(cmd)

					cmd.array(cmd.nsects, "sections")
					for i in xrange(0, cmd.nsects):
						section = cmd.sections[i]
						section.bytes(16, "name")
						section.bytes(16, "segment")
						if self.big_endian:
							section.uint64_be("addr")
							section.uint64_be("size")
							section.uint32_be("offset")
							section.uint32_be("align")
							section.uint32_be("reloff")
							section.uint32_be("nreloc")
							section.uint32_be("flags")
							section.uint32_be("reserved1")
							section.uint32_be("reserved2")
							section.uint32_be("reserved3")
						else:
							section.uint64_le("addr")
							section.uint64_le("size")
							section.uint32_le("offset")
							section.uint32_le("align")
							section.uint32_le("reloff")
							section.uint32_le("nreloc")
							section.uint32_le("flags")
							section.uint32_le("reserved1")
							section.uint32_le("reserved2")
							section.uint32_le("reserved3")
						self.sections.append(section)

					for i in xrange(0, cmd.nsects):
						section = cmd.sections[i]
						section.array(section.nreloc, "relocs")
						for j in xrange(0, section.nreloc):
							reloc = section.relocs[j]
							reloc.seek(section.reloff + (j * 8))
							if self.big_endian:
								reloc.uint32_be("addr")
								reloc.uint32_be("value")
							else:
								reloc.uint32_le("addr")
								reloc.uint32_le("value")
				elif cmd.cmd == 5: # UNIX_THREAD
					if self.header.cputype == 7: # x86
						cmd.uint32_le("flavor")
						cmd.uint32_le("count")
						for reg in ["eax", "ebx", "ecx", "edx", "edi", "esi", "ebp", "esp", "ss", "eflags",
							"eip", "cs", "ds", "es", "fs", "gs"]:
							cmd.uint32_le(reg)
						self.entry_addr = cmd.eip
					elif self.header.cputype == 0x01000007: # x86_64
						cmd.uint32_le("flavor")
						cmd.uint32_le("count")
						for reg in ["rax", "rbx", "rcx", "rdx", "rdi", "rsi", "rbp", "rsp", "r8", "r9",
								"r10", "r11", "r12", "r13", "r14", "r15", "rip", "rflags", "cs", "fs", "gs"]:
							cmd.uint64_le(reg)
						self.entry_addr = cmd.rip
					elif self.header.cputype == 18: # PPC32
						cmd.uint32_be("flavor")
						cmd.uint32_be("count")
						for reg in ["srr0", "srr1"] + ["r%d" % i for i in xrange(0, 32)] + ["cr", "xer",
							"lr", "ctr", "mq", "vrsave"]:
							cmd.uint32_be(reg)
						self.entry_addr = cmd.srr0
					elif self.header.cputype == 0x01000012: # PPC64
						cmd.uint32_be("flavor")
						cmd.uint32_be("count")
						for reg in ["srr0", "srr1"] + ["r%d" % i for i in xrange(0, 32)] + ["cr", "xer",
							"lr", "ctr", "mq", "vrsave"]:
							cmd.uint64_be(reg)
						self.entry_addr = cmd.srr0
					elif self.header.cputype == 12: # ARM
						cmd.uint32_le("flavor")
						cmd.uint32_le("count")
						for reg in ["r%d" % i for i in xrange(0, 13)] + ["sp", "lr", "pc", "cpsr"]:
							cmd.uint32_le(reg)
						self.entry_addr = cmd.pc
				elif cmd.cmd == 2: # SYMTAB
					if self.big_endian:
						cmd.uint32_be("symoff")
						cmd.uint32_be("nsyms")
						cmd.uint32_be("stroff")
						cmd.uint32_be("strsize")
					else:
						cmd.uint32_le("symoff")
						cmd.uint32_le("nsyms")
						cmd.uint32_le("stroff")
						cmd.uint32_le("strsize")

					self.symbol_table = self.tree.array(cmd.nsyms, "symtab")
					strings = self.data.read(cmd.stroff, cmd.strsize)

					sym_offset = cmd.symoff
					for j in xrange(0, cmd.nsyms):
						entry = self.symbol_table[j]
						entry.seek(sym_offset)

						if self.big_endian:
							entry.uint32_be("strx")
							entry.uint8("type")
							entry.uint8("sect")
							entry.uint16_be("desc")
							if self.bits == 32:
								entry.uint32_be("value")
							else:
								entry.uint64_be("value")
						else:
							entry.uint32_le("strx")
							entry.uint8("type")
							entry.uint8("sect")
							entry.uint16_le("desc")
							if self.bits == 32:
								entry.uint32_le("value")
							else:
								entry.uint64_le("value")

						str_end = strings.find("\x00", entry.strx)
						entry.name = strings[entry.strx:str_end]

						if self.bits == 32:
							sym_offset += 12
						else:
							sym_offset += 16
				elif cmd.cmd == 11: # DYSYMTAB
					if self.big_endian:
						cmd.uint32_be("ilocalsym")
						cmd.uint32_be("nlocalsym")
						cmd.uint32_be("iextdefsym")
						cmd.uint32_be("nextdefsym")
						cmd.uint32_be("iundefsym")
						cmd.uint32_be("nundefsym")
						cmd.uint32_be("tocoff")
						cmd.uint32_be("ntoc")
						cmd.uint32_be("modtaboff")
						cmd.uint32_be("nmodtab")
						cmd.uint32_be("extrefsymoff")
						cmd.uint32_be("nextrefsyms")
						cmd.uint32_be("indirectsymoff")
						cmd.uint32_be("nindirectsyms")
						cmd.uint32_be("extreloff")
						cmd.uint32_be("nextrel")
						cmd.uint32_be("locreloff")
						cmd.uint32_be("nlocrel")
					else:
						cmd.uint32_le("ilocalsym")
						cmd.uint32_le("nlocalsym")
						cmd.uint32_le("iextdefsym")
						cmd.uint32_le("nextdefsym")
						cmd.uint32_le("iundefsym")
						cmd.uint32_le("nundefsym")
						cmd.uint32_le("tocoff")
						cmd.uint32_le("ntoc")
						cmd.uint32_le("modtaboff")
						cmd.uint32_le("nmodtab")
						cmd.uint32_le("extrefsymoff")
						cmd.uint32_le("nextrefsyms")
						cmd.uint32_le("indirectsymoff")
						cmd.uint32_le("nindirectsyms")
						cmd.uint32_le("extreloff")
						cmd.uint32_le("nextrel")
						cmd.uint32_le("locreloff")
						cmd.uint32_le("nlocrel")
				elif (cmd.cmd & 0x7fffffff) == 0x22: # DYLD_INFO
					self.dynamic_symbol_table = cmd
					if self.big_endian:
						cmd.uint32_be("rebaseoff")
						cmd.uint32_be("rebasesize")
						cmd.uint32_be("bindoff")
						cmd.uint32_be("bindsize")
						cmd.uint32_be("weakbindoff")
						cmd.uint32_be("weakbindsize")
						cmd.uint32_be("lazybindoff")
						cmd.uint32_be("lazybindsize")
						cmd.uint32_be("exportoff")
						cmd.uint32_be("exportsize")
					else:
						cmd.uint32_le("rebaseoff")
						cmd.uint32_le("rebasesize")
						cmd.uint32_le("bindoff")
						cmd.uint32_le("bindsize")
						cmd.uint32_le("weakbindoff")
						cmd.uint32_le("weakbindsize")
						cmd.uint32_le("lazybindoff")
						cmd.uint32_le("lazybindsize")
						cmd.uint32_le("exportoff")
						cmd.uint32_le("exportsize")

				offset += cmd.size

			# Add symbols from symbol table
			if self.symbol_table:
				for i in xrange(0, len(self.symbol_table)):
					symbol = self.symbol_table[i]

					# Only use symbols that are within a section
					if ((symbol.type & 0xe) == 0xe) and (symbol.sect <= len(self.sections)):
						self.create_symbol(symbol.value, symbol.name)

			# If there is a DYLD_INFO section, parse it and add PLT entries
			if self.dynamic_symbol_table:
				self.parse_dynamic_tables([[self.dynamic_symbol_table.bindoff, self.dynamic_symbol_table.bindsize],
					[self.dynamic_symbol_table.lazybindoff, self.dynamic_symbol_table.lazybindsize]])

			self.tree.complete()
			self.valid = True
		except:
			self.valid = False
			raise

		if self.valid:
			self.data.add_callback(self)
Пример #31
0
class MachOFile(BinaryAccessor):
	def __init__(self, data):
		self.data = data
		self.valid = False
		self.callbacks = []
		self.symbols_by_name = {}
		self.symbols_by_addr = {}
		self.plt = {}
		if not self.is_macho():
			return

		try:
			self.tree = Struct(self.data)
			self.header = self.tree.struct("Mach-O header", "header")

			self.header.uint32_le("magic")
			if (self.header.magic == 0xfeedface) or (self.header.magic == 0xfeedfacf):
				self.header.uint32_le("cputype")
				self.header.uint32_le("cpusubtype")
				self.header.uint32_le("filetype")
				self.header.uint32_le("cmds")
				self.header.uint32_le("cmdsize")
				self.header.uint32_le("flags")
				if self.header.magic == 0xfeedfacf:
					self.header.uint32_le("reserved")
					self.bits = 64
				else:
					self.bits = 32
				self.big_endian = False
			elif (self.header.magic == 0xcefaedfe) or (self.header.magic == 0xcffaedfe):
				self.header.uint32_be("cputype")
				self.header.uint32_be("cpusubtype")
				self.header.uint32_be("filetype")
				self.header.uint32_be("cmds")
				self.header.uint32_be("cmdsize")
				self.header.uint32_be("flags")
				if self.header.magic == 0xcffaedfe:
					self.header.uint32_be("reserved")
					self.bits = 64
				else:
					self.bits = 32
				self.big_endian = True

			self.symbol_table = None
			self.dynamic_symbol_table = None

			# Parse loader commands
			self.commands = self.tree.array(self.header.cmds, "commands")
			self.segments = []
			self.sections = []
			offset = self.header.getSize()
			for i in xrange(0, self.header.cmds):
				cmd = self.commands[i]
				cmd.seek(offset)
				if self.big_endian:
					cmd.uint32_be("cmd")
					cmd.uint32_be("size")
				else:
					cmd.uint32_le("cmd")
					cmd.uint32_le("size")

				if cmd.cmd == 1: # SEGMENT
					cmd.bytes(16, "name")
					if self.big_endian:
						cmd.uint32_be("vmaddr")
						cmd.uint32_be("vmsize")
						cmd.uint32_be("fileoff")
						cmd.uint32_be("filesize")
						cmd.uint32_be("maxprot")
						cmd.uint32_be("initprot")
						cmd.uint32_be("nsects")
						cmd.uint32_be("flags")
					else:
						cmd.uint32_le("vmaddr")
						cmd.uint32_le("vmsize")
						cmd.uint32_le("fileoff")
						cmd.uint32_le("filesize")
						cmd.uint32_le("maxprot")
						cmd.uint32_le("initprot")
						cmd.uint32_le("nsects")
						cmd.uint32_le("flags")

					if cmd.initprot != 0: # Ignore __PAGE_ZERO or anything like it
						self.segments.append(cmd)

					cmd.array(cmd.nsects, "sections")
					for i in xrange(0, cmd.nsects):
						section = cmd.sections[i]
						section.bytes(16, "name")
						section.bytes(16, "segment")
						if self.big_endian:
							section.uint32_be("addr")
							section.uint32_be("size")
							section.uint32_be("offset")
							section.uint32_be("align")
							section.uint32_be("reloff")
							section.uint32_be("nreloc")
							section.uint32_be("flags")
							section.uint32_be("reserved1")
							section.uint32_be("reserved2")
						else:
							section.uint32_le("addr")
							section.uint32_le("size")
							section.uint32_le("offset")
							section.uint32_le("align")
							section.uint32_le("reloff")
							section.uint32_le("nreloc")
							section.uint32_le("flags")
							section.uint32_le("reserved1")
							section.uint32_le("reserved2")
						self.sections.append(section)

					for i in xrange(0, cmd.nsects):
						section = cmd.sections[i]
						section.array(section.nreloc, "relocs")
						for j in xrange(0, section.nreloc):
							reloc = section.relocs[j]
							reloc.seek(section.reloff + (j * 8))
							if self.big_endian:
								reloc.uint32_be("addr")
								reloc.uint32_be("value")
							else:
								reloc.uint32_le("addr")
								reloc.uint32_le("value")
				elif cmd.cmd == 25: # SEGMENT_64
					cmd.bytes(16, "name")
					if self.big_endian:
						cmd.uint64_be("vmaddr")
						cmd.uint64_be("vmsize")
						cmd.uint64_be("fileoff")
						cmd.uint64_be("filesize")
						cmd.uint32_be("maxprot")
						cmd.uint32_be("initprot")
						cmd.uint32_be("nsects")
						cmd.uint32_be("flags")
					else:
						cmd.uint64_le("vmaddr")
						cmd.uint64_le("vmsize")
						cmd.uint64_le("fileoff")
						cmd.uint64_le("filesize")
						cmd.uint32_le("maxprot")
						cmd.uint32_le("initprot")
						cmd.uint32_le("nsects")
						cmd.uint32_le("flags")

					if cmd.initprot != 0: # Ignore __PAGE_ZERO or anything like it
						self.segments.append(cmd)

					cmd.array(cmd.nsects, "sections")
					for i in xrange(0, cmd.nsects):
						section = cmd.sections[i]
						section.bytes(16, "name")
						section.bytes(16, "segment")
						if self.big_endian:
							section.uint64_be("addr")
							section.uint64_be("size")
							section.uint32_be("offset")
							section.uint32_be("align")
							section.uint32_be("reloff")
							section.uint32_be("nreloc")
							section.uint32_be("flags")
							section.uint32_be("reserved1")
							section.uint32_be("reserved2")
							section.uint32_be("reserved3")
						else:
							section.uint64_le("addr")
							section.uint64_le("size")
							section.uint32_le("offset")
							section.uint32_le("align")
							section.uint32_le("reloff")
							section.uint32_le("nreloc")
							section.uint32_le("flags")
							section.uint32_le("reserved1")
							section.uint32_le("reserved2")
							section.uint32_le("reserved3")
						self.sections.append(section)

					for i in xrange(0, cmd.nsects):
						section = cmd.sections[i]
						section.array(section.nreloc, "relocs")
						for j in xrange(0, section.nreloc):
							reloc = section.relocs[j]
							reloc.seek(section.reloff + (j * 8))
							if self.big_endian:
								reloc.uint32_be("addr")
								reloc.uint32_be("value")
							else:
								reloc.uint32_le("addr")
								reloc.uint32_le("value")
				elif cmd.cmd == 5: # UNIX_THREAD
					if self.header.cputype == 7: # x86
						cmd.uint32_le("flavor")
						cmd.uint32_le("count")
						for reg in ["eax", "ebx", "ecx", "edx", "edi", "esi", "ebp", "esp", "ss", "eflags",
							"eip", "cs", "ds", "es", "fs", "gs"]:
							cmd.uint32_le(reg)
						self.entry_addr = cmd.eip
					elif self.header.cputype == 0x01000007: # x86_64
						cmd.uint32_le("flavor")
						cmd.uint32_le("count")
						for reg in ["rax", "rbx", "rcx", "rdx", "rdi", "rsi", "rbp", "rsp", "r8", "r9",
								"r10", "r11", "r12", "r13", "r14", "r15", "rip", "rflags", "cs", "fs", "gs"]:
							cmd.uint64_le(reg)
						self.entry_addr = cmd.rip
					elif self.header.cputype == 18: # PPC32
						cmd.uint32_be("flavor")
						cmd.uint32_be("count")
						for reg in ["srr0", "srr1"] + ["r%d" % i for i in xrange(0, 32)] + ["cr", "xer",
							"lr", "ctr", "mq", "vrsave"]:
							cmd.uint32_be(reg)
						self.entry_addr = cmd.srr0
					elif self.header.cputype == 0x01000012: # PPC64
						cmd.uint32_be("flavor")
						cmd.uint32_be("count")
						for reg in ["srr0", "srr1"] + ["r%d" % i for i in xrange(0, 32)] + ["cr", "xer",
							"lr", "ctr", "mq", "vrsave"]:
							cmd.uint64_be(reg)
						self.entry_addr = cmd.srr0
					elif self.header.cputype == 12: # ARM
						cmd.uint32_le("flavor")
						cmd.uint32_le("count")
						for reg in ["r%d" % i for i in xrange(0, 13)] + ["sp", "lr", "pc", "cpsr"]:
							cmd.uint32_le(reg)
						self.entry_addr = cmd.pc
				elif cmd.cmd == 2: # SYMTAB
					if self.big_endian:
						cmd.uint32_be("symoff")
						cmd.uint32_be("nsyms")
						cmd.uint32_be("stroff")
						cmd.uint32_be("strsize")
					else:
						cmd.uint32_le("symoff")
						cmd.uint32_le("nsyms")
						cmd.uint32_le("stroff")
						cmd.uint32_le("strsize")

					self.symbol_table = self.tree.array(cmd.nsyms, "symtab")
					strings = self.data.read(cmd.stroff, cmd.strsize)

					sym_offset = cmd.symoff
					for j in xrange(0, cmd.nsyms):
						entry = self.symbol_table[j]
						entry.seek(sym_offset)

						if self.big_endian:
							entry.uint32_be("strx")
							entry.uint8("type")
							entry.uint8("sect")
							entry.uint16_be("desc")
							if self.bits == 32:
								entry.uint32_be("value")
							else:
								entry.uint64_be("value")
						else:
							entry.uint32_le("strx")
							entry.uint8("type")
							entry.uint8("sect")
							entry.uint16_le("desc")
							if self.bits == 32:
								entry.uint32_le("value")
							else:
								entry.uint64_le("value")

						str_end = strings.find("\x00", entry.strx)
						entry.name = strings[entry.strx:str_end]

						if self.bits == 32:
							sym_offset += 12
						else:
							sym_offset += 16
				elif cmd.cmd == 11: # DYSYMTAB
					if self.big_endian:
						cmd.uint32_be("ilocalsym")
						cmd.uint32_be("nlocalsym")
						cmd.uint32_be("iextdefsym")
						cmd.uint32_be("nextdefsym")
						cmd.uint32_be("iundefsym")
						cmd.uint32_be("nundefsym")
						cmd.uint32_be("tocoff")
						cmd.uint32_be("ntoc")
						cmd.uint32_be("modtaboff")
						cmd.uint32_be("nmodtab")
						cmd.uint32_be("extrefsymoff")
						cmd.uint32_be("nextrefsyms")
						cmd.uint32_be("indirectsymoff")
						cmd.uint32_be("nindirectsyms")
						cmd.uint32_be("extreloff")
						cmd.uint32_be("nextrel")
						cmd.uint32_be("locreloff")
						cmd.uint32_be("nlocrel")
					else:
						cmd.uint32_le("ilocalsym")
						cmd.uint32_le("nlocalsym")
						cmd.uint32_le("iextdefsym")
						cmd.uint32_le("nextdefsym")
						cmd.uint32_le("iundefsym")
						cmd.uint32_le("nundefsym")
						cmd.uint32_le("tocoff")
						cmd.uint32_le("ntoc")
						cmd.uint32_le("modtaboff")
						cmd.uint32_le("nmodtab")
						cmd.uint32_le("extrefsymoff")
						cmd.uint32_le("nextrefsyms")
						cmd.uint32_le("indirectsymoff")
						cmd.uint32_le("nindirectsyms")
						cmd.uint32_le("extreloff")
						cmd.uint32_le("nextrel")
						cmd.uint32_le("locreloff")
						cmd.uint32_le("nlocrel")
				elif (cmd.cmd & 0x7fffffff) == 0x22: # DYLD_INFO
					self.dynamic_symbol_table = cmd
					if self.big_endian:
						cmd.uint32_be("rebaseoff")
						cmd.uint32_be("rebasesize")
						cmd.uint32_be("bindoff")
						cmd.uint32_be("bindsize")
						cmd.uint32_be("weakbindoff")
						cmd.uint32_be("weakbindsize")
						cmd.uint32_be("lazybindoff")
						cmd.uint32_be("lazybindsize")
						cmd.uint32_be("exportoff")
						cmd.uint32_be("exportsize")
					else:
						cmd.uint32_le("rebaseoff")
						cmd.uint32_le("rebasesize")
						cmd.uint32_le("bindoff")
						cmd.uint32_le("bindsize")
						cmd.uint32_le("weakbindoff")
						cmd.uint32_le("weakbindsize")
						cmd.uint32_le("lazybindoff")
						cmd.uint32_le("lazybindsize")
						cmd.uint32_le("exportoff")
						cmd.uint32_le("exportsize")

				offset += cmd.size

			# Add symbols from symbol table
			if self.symbol_table:
				for i in xrange(0, len(self.symbol_table)):
					symbol = self.symbol_table[i]

					# Only use symbols that are within a section
					if ((symbol.type & 0xe) == 0xe) and (symbol.sect <= len(self.sections)):
						self.create_symbol(symbol.value, symbol.name)

			# If there is a DYLD_INFO section, parse it and add PLT entries
			if self.dynamic_symbol_table:
				self.parse_dynamic_tables([[self.dynamic_symbol_table.bindoff, self.dynamic_symbol_table.bindsize],
					[self.dynamic_symbol_table.lazybindoff, self.dynamic_symbol_table.lazybindsize]])

			self.tree.complete()
			self.valid = True
		except:
			self.valid = False
			raise

		if self.valid:
			self.data.add_callback(self)

	def read_leb128(self, data, ofs):
		value = 0
		shift = 0
		while ofs < len(data):
			cur = ord(data[ofs])
			ofs += 1
			value |= (cur & 0x7f) << shift
			shift += 7
			if (cur & 0x80) == 0:
				break
		return value, ofs

	def parse_dynamic_tables(self, tables):
		# Interpret DYLD_INFO instructions (not documented by Apple)
		# http://networkpx.blogspot.com/2009/09/about-lcdyldinfoonly-command.html
		ordinal = 0
		segment = 0
		offset = 0
		sym_type = 0
		name = ""

		for table in tables:
			offset = table[0]
			size = table[1]
			opcodes = self.data.read(offset, size)
			i = 0
			while i < len(opcodes):
				opcode = ord(opcodes[i])
				i += 1
				if (opcode >> 4) == 0:
					continue
				elif (opcode >> 4) == 1:
					ordinal = opcode & 0xf
				elif (opcode >> 4) == 2:
					ordinal, i = self.read_leb128(opcodes, i)
				elif (opcode >> 4) == 3:
					ordinal = -(opcode & 0xf)
				elif (opcode >> 4) == 4:
					name = ""
					while i < len(opcodes):
						ch = opcodes[i]
						i += 1
						if ch == '\x00':
							break
						name += ch
				elif (opcode >> 4) == 5:
					sym_type = opcode & 0xf
				elif (opcode >> 4) == 6:
					addend, i = self.read_leb128(opcodes, i)
				elif (opcode >> 4) == 7:
					segment = opcode & 0xf
					offset, i = self.read_leb128(opcodes, i)
				elif (opcode >> 4) == 8:
					rel, i = self.read_leb128(opcodes, i)
					offset += rel
				elif (opcode >> 4) >= 9:
					if (sym_type == 1) and (segment <= len(self.segments)):
						# Add pointer type entries to the PLT
						addr = self.segments[segment - 1].vmaddr + offset
						self.plt[addr] = name
						self.create_symbol(addr, self.decorate_plt_name(name))
					if self.bits == 32:
						offset += 4
					else:
						offset += 8
					if (opcode >> 4) == 10:
						rel, i = self.read_leb128(opcodes, i)
						offset += rel
					elif (opcode >> 4) == 11:
						offset += (opcode & 0xf) * 4
					elif (opcode >> 4) == 12:
						count, i = self.read_leb128(opcodes, i)
						skip, i = self.read_leb128(opcodes, i)

	def read(self, ofs, len):
		result = ""
		while len > 0:
			cur = None
			for i in self.segments:
				if ((ofs >= i.vmaddr) and (ofs < (i.vmaddr + i.vmsize))) and (i.vmsize != 0):
					cur = i
			if cur == None:
				break

			prog_ofs = ofs - cur.vmaddr
			mem_len = cur.vmsize - prog_ofs
			file_len = cur.filesize - prog_ofs
			if mem_len > len:
				mem_len = len
			if file_len > len:
				file_len = len

			if file_len <= 0:
				result += "\x00" * mem_len
				len -= mem_len
				ofs += mem_len
				continue

			result += self.data.read(cur.fileoff + prog_ofs, file_len)
			len -= file_len
			ofs += file_len

		return result

	def next_valid_addr(self, ofs):
		result = -1
		for i in self.segments:
			if (i.vmaddr >= ofs) and (i.vmsize != 0) and ((result == -1) or (i.vmaddr < result)):
				result = i.vmaddr
		return result

	def get_modification(self, ofs, len):
		result = []
		while len > 0:
			cur = None
			for i in self.segments:
				if ((ofs >= i.vmaddr) and (ofs < (i.vmaddr + i.vmsize))) and (i.vmsize != 0):
					cur = i
			if cur == None:
				break

			prog_ofs = ofs - cur.vmaddr
			mem_len = cur.vmsize - prog_ofs
			file_len = cur.filesize - prog_ofs
			if mem_len > len:
				mem_len = len
			if file_len > len:
				file_len = len

			if file_len <= 0:
				result += [DATA_ORIGINAL] * mem_len
				len -= mem_len
				ofs += mem_len
				continue

			result += self.data.get_modification(cur.fileoff + prog_ofs, file_len)
			len -= file_len
			ofs += file_len

		return result

	def write(self, ofs, data):
		result = 0
		while len(data) > 0:
			cur = None
			for i in self.segments:
				if ((ofs >= i.vmaddr) and (ofs < (i.vmaddr + i.vmsize))) and (i.vmsize != 0):
					cur = i
			if cur == None:
				break

			prog_ofs = ofs - cur.vmaddr
			mem_len = cur.vmsize - prog_ofs
			file_len = cur.filesize - prog_ofs
			if mem_len > len:
				mem_len = len
			if file_len > len:
				file_len = len

			if file_len <= 0:
				break

			result += self.data.write(cur.fileoff + prog_ofs, data[0:file_len])
			data = data[file_len:]
			ofs += file_len

		return result

	def insert(self, ofs, data):
		return 0

	def remove(self, ofs, size):
		return 0

	def notify_data_write(self, data, ofs, contents):
		# Find sections that hold data backed by updated regions of the file
		for i in self.segments:
			if ((ofs + len(contents)) > i.fileoff) and (ofs < (i.fileoff + i.filesize)) and (i.vmsize != 0):
				# This section has been updated, compute which region has been changed
				from_start = ofs - i.fileoff
				data_ofs = 0
				length = len(contents)
				if from_start < 0:
					length += from_start
					data_ofs -= from_start
					from_start = 0
				if (from_start + length) > i.filesize:
					length = i.filesize - from_start

				# Notify callbacks
				if length > 0:
					for cb in self.callbacks:
						if hasattr(cb, "notify_data_write"):
							cb.notify_data_write(self, i.vmaddr + from_start,
								contents[data_ofs:(data_ofs + length)])

	def save(self, filename):
		self.data.save(filename)

	def start(self):
		result = None
		for i in self.segments:
			if ((result == None) or (i.vmaddr < result)) and (i.vmsize != 0):
				result = i.vmaddr
		return result

	def entry(self):
		if not hasattr(self, "entry_addr"):
			return self.start()
		return self.entry_addr

	def __len__(self):
		max = None
		for i in self.segments:
			if ((max == None) or ((i.vmaddr + i.vmsize) > max)) and (i.vmsize != 0):
				max = i.vmaddr + i.vmsize
		return max - self.start()

	def is_macho(self):
		if self.data.read(0, 4) == "\xfe\xed\xfa\xce":
			return True
		if self.data.read(0, 4) == "\xfe\xed\xfa\xcf":
			return True
		if self.data.read(0, 4) == "\xce\xfa\xed\xfe":
			return True
		if self.data.read(0, 4) == "\xcf\xfa\xed\xfe":
			return True
		return False

	def architecture(self):
		if self.header.cputype == 7:
			return "x86"
		if self.header.cputype == 0x01000007:
			return "x86_64"
		if self.header.cputype == 12:
			return "arm"
		if self.header.cputype == 18:
			return "ppc"
		if self.header.cputype == 0x01000012:
			return "ppc"
		return None

	def decorate_plt_name(self, name):
		return name + "@PLT"

	def create_symbol(self, addr, name):
		self.symbols_by_name[name] = addr
		self.symbols_by_addr[addr] = name

	def delete_symbol(self, addr, name):
		if name in self.symbols_by_name:
			del(self.symbols_by_name[name])
		if addr in self.symbols_by_addr:
			del(self.symbols_by_addr[addr])

	def add_callback(self, cb):
		self.callbacks.append(cb)

	def remove_callback(self, cb):
		self.callbacks.remove(cb)

	def is_modified(self):
		return self.data.is_modified()

	def find(self, regex, addr):
		while (addr < self.end()) and (addr != -1):
			data = self.read(addr, 0xfffffffff)
			match = regex.search(data)
			if match != None:
				return match.start() + addr

			addr += len(data)
			addr = self.next_valid_addr(addr)

		return -1

	def has_undo_actions(self):
		return self.data.has_undo_actions()

	def commit_undo(self, before_loc, after_loc):
		self.data.commit_undo(before_loc, after_loc)

	def undo(self):
		self.data.undo()

	def redo(self):
		self.data.redo()
Пример #32
0
    def gen_win(self):
        """
        This method generates wannier90.win for the initial DFT run.
        """

        # generating wannier90.win
        TB = Struct.TBstructure("POSCAR", p["atomnames"], p["orbs"])
        TB.Compute_cor_idx(p["cor_at"], p["cor_orb"])
        # print((TB.TB_orbs))

        # Read number of bands from DFT input file
        try:
            if self.dft == "vasp":
                fi = open("INCAR", "r")
                data = fi.read()
                fi.close()
                self.DFT.NBANDS = int(
                    re.findall(r"\n\s*NBANDS\s*=\s*([\d\s]*)", data)[0])
                print("Number of bands read from INCAR = %d " %
                      self.DFT.NBANDS)

            elif self.dft == "siesta":
                fi = open(self.structurename + ".fdf", "r")
                data = fi.read()
                fi.close()
                self.DFT.NBANDS = int(
                    re.findall(r"\n\s*Siesta2Wannier90.NumberOfBands[\s0-9]*",
                               data)[0].split()[-1])
                print("Number of bands read from .fdf = %d " % self.DFT.NBANDS)

            elif self.dft == "qe":
                fi = open(self.structurename + ".scf.out", "r")
                data = fi.read()
                fi.close()
                self.DFT.NBANDS = int(
                    re.findall(
                        r"\n\s*number\s*of\s*Kohn-Sham\s*states=([\s\d]*)",
                        data)[0])
                print("Number of bands read from .scf.out = %d " %
                      self.DFT.NBANDS)

        except:
            self.DFT.NBANDS = 100
            print("WARNING: Number of bands not set in DFT input file!")

        # Setting num_bands in .win file.
        # If set to False num_bands is set to number of DFT bands.
        if list(p.keys()).count("num_bands_win"):
            if p["num_bands_win"]:
                self.wanbands = p["num_bands_win"]
                self.updatewanbands = False
            else:
                self.wanbands = self.DFT.NBANDS
        else:
            self.wanbands = self.DFT.NBANDS

        self.DFT.Create_win(
            TB,
            p["atomnames"],
            p["orbs"],
            p["L_rot"],
            self.wanbands,
            # Initially DFT.EFERMI is taken from DFT_mu.out but will
            # be updated later once the DFT calculation is complete.
            self.DFT.EFERMI + p["ewin"][0],
            self.DFT.EFERMI + p["ewin"][1],
            self.kmeshtol,
        )

        # If exclude_bands are to be included in the .win file.
        # Appending to current .win file.
        if list(p.keys()).count("exclude_bands"):
            if p["exclude_bands"]:
                f = open("wannier90.win", "a")
                f.write("\nexclude_bands :\t")
                f.write(", ".join(str(x) for x in p["exclude_bands"]))
                f.write("\n")
                f.close()

            else:
                pass
        else:
            pass

        # VASP populates the .win file when running but Siesta
        # does not so we need to create a complete .win file for
        # Siesta runs.

        if self.dft == "siesta":

            # Update wannier90.win file.
            f = open("wannier90.win", "a")
            f.write("\nbegin unit_cell_cart\n")
            np.savetxt(f, self.cell)
            f.write("end unit_cell_cart\n\n")

            # writing the atoms cart block
            f.write("begin atoms_frac\n")
            aT = (np.array([self.symbols])).T
            b = self.positions
            atoms_cart = np.concatenate((aT, b), axis=1)
            np.savetxt(f, atoms_cart, fmt="%s")
            f.write("end atoms_frac\n\n")

            # writing the mp_grid line
            fi = open(self.structurename + ".fdf")
            data = fi.read()
            fi.close()
            grid = re.findall(
                r"%block kgrid_Monkhorst_Pack([\s0-9.]*)%endblock kgrid_Monkhorst_Pack",
                data,
            )[0].split()
            f.write("mp_grid= %s %s %s \n" % (grid[0], grid[5], grid[10]))

            # kpoints
            f.write("\nbegin kpoints\n")
            cmd = "kmesh.pl " + grid[0] + " " + grid[5] + " " + grid[
                10] + " wannier"
            out, err = subprocess.Popen(cmd,
                                        shell=True,
                                        stdout=subprocess.PIPE,
                                        stderr=subprocess.PIPE).communicate()
            f.write(out.decode("utf-8"))
            if err:
                print(err.decode("utf-8"))
            f.write("end kpoints")
            f.close()
            print("wannier90.win generated.")

        # Similar to Siesta, Quantum Espresso requires the generation
        # of the .win file manually.
        # self.cell, self.symbols and self.positions is set from
        # read_poscar().

        elif self.dft == "qe" and self.aiida == False:

            # reading poscar that should be generated.
            self.read_poscar()

            # Update wannier90.win file then rename it
            f = open("wannier90.win", "a")
            f.write("\nbegin unit_cell_cart\n")
            np.savetxt(f, self.cell)
            f.write("end unit_cell_cart\n\n")

            # writing the atoms cart block
            f.write("begin atoms_frac\n")
            aT = (np.array([self.symbols])).T
            b = self.positions
            atoms_cart = np.concatenate((aT, b), axis=1)
            np.savetxt(f, atoms_cart, fmt="%s")
            f.write("end atoms_frac\n\n")

            # writing the mp_grid line
            fi = open(self.structurename + ".scf.in")
            data = fi.read()
            fi.close()
            self.grid = re.findall(
                r"K_POINTS\s*automatic\s*([\d\s]*)",
                data,
            )[0].split()[0:3]
            self.grid = [int(x) for x in self.grid]
            f.write("mp_grid= %s %s %s \n" %
                    (self.grid[0], self.grid[1], self.grid[2]))

            # kpoints
            f.write("\nbegin kpoints\n")
            cmd = ("kmesh.pl " + str(self.grid[0]) + " " + str(self.grid[1]) +
                   " " + str(self.grid[2]) + " wannier")
            out, err = subprocess.Popen(cmd,
                                        shell=True,
                                        stdout=subprocess.PIPE,
                                        stderr=subprocess.PIPE).communicate()
            f.write(out.decode("utf-8"))
            if err:
                print(err.decode("utf-8"))
            f.write("end kpoints")
            f.close()
            print("wannier90.win generated.")
Пример #33
0
    def __init__(self):
        layout = [[
            sg.Button('Adicionar novo dado'),
            sg.Button('Remover algum dado')
        ], [sg.Button('Ver dados')],
                  [sg.Button('Exportar para arquivo de texto')],
                  [sg.Button('Voltar')]]

        win = sg.Window('Opções', layout)

        event, value = win.read()
        print(event)
        if (event == 'Ver dados'):
            win.close()
            janela_lista_tel()

        elif (event == 'Adicionar novo dado'):
            pop = sg.popup_get_text(
                message='Digite a chave nova do dado que vai ser adicionado:')

            if (pop == None):
                win.close()
            else:
                at, prev = estrutura.buscar(int(pop))
                if at:
                    pop_error = sg.popup("A chave digitada já existe")
                else:
                    dado = []
                    dado.append(int(pop))

                    layout = [[sg.Text('Por favor, informe aqui os dados:')],
                              [
                                  sg.Text('Data de Observação', size=(30, 1)),
                                  sg.InputText()
                              ],
                              [
                                  sg.Text(
                                      'Estado da Província (Se for na China)',
                                      size=(30, 1)),
                                  sg.InputText()
                              ],
                              [
                                  sg.Text('Região do País', size=(30, 1)),
                                  sg.InputText()
                              ],
                              [
                                  sg.Text('Última Atualização', size=(30, 1)),
                                  sg.InputText()
                              ],
                              [
                                  sg.Text('Casos confirmados', size=(30, 1)),
                                  sg.InputText()
                              ],
                              [
                                  sg.Text('Número de mortos', size=(30, 1)),
                                  sg.InputText()
                              ],
                              [
                                  sg.Text('Números de recuperados',
                                          size=(30, 1)),
                                  sg.InputText()
                              ], [sg.Submit(), sg.Cancel()]]

                    window_ins = sg.Window('Dados', layout)
                    event, values = window_ins.read()
                    window_ins.close()

                    if (event == 'Cancel'):
                        win.close()
                        janela_lista()

                    for i in values:
                        dado.append(values[i])

                    aux2 = Struct.CovidLine(*dado)

                    for i in values:
                        dado.append(i)

                    estrutura.inserir(aux2)

            win.close()
            janela_lista()

        elif (event == 'Remover algum dado'):
            pop = sg.popup_get_text(message='Digite a chave do dado desejado:')

            if (pop == None):
                win.close()
            elif (estrutura.excluir(int(pop)) == False):
                pop_error = sg.popup("A chave digitada não existe")
            else:
                pop_done = sg.popup("Chave removida com sucesso")

            win.close()
            janela_lista()

        elif (event == 'Exportar para arquivo de texto'):
            outf = open("saida.txt", "w")
            estrutura.escrever(outf)
            sg.popup('Arquivo criado com sucesso!')
            win.close()
            janela_lista()

        elif (event == 'Voltar'):
            win.close()
            Main()

        elif (event == sg.WIN_CLOSED):
            win.close()
Пример #34
0
class ElfFile(BinaryAccessor):
	def __init__(self, data):
		self.data = data
		self.valid = False
		self.callbacks = []
		self.symbols_by_name = {}
		self.symbols_by_addr = {}
		if not self.is_elf():
			return

		try:
			self.tree = Struct(self.data)
			self.header = self.tree.struct("ELF header", "header")
			self.header.struct("ELF identification", "ident")

			self.header.ident.uint32("magic")
			self.header.ident.uint8("file_class")
			self.header.ident.uint8("encoding")
			self.header.ident.uint8("version")
			self.header.ident.uint8("abi")
			self.header.ident.uint8("abi_version")
			self.header.ident.bytes(7, "pad")

			self.header.uint16("type")
			self.header.uint16("arch")
			self.header.uint32("version")

			self.symbol_table_section = None
			self.dynamic_symbol_table_section = None

			if self.header.ident.file_class == 1: # 32-bit
				self.header.uint32("entry")
				self.header.uint32("program_header_offset")
				self.header.uint32("section_header_offset")
				self.header.uint32("flags")
				self.header.uint16("header_size")
				self.header.uint16("program_header_size")
				self.header.uint16("program_header_count")
				self.header.uint16("section_header_size")
				self.header.uint16("section_header_count")
				self.header.uint16("string_table")

				try:
					self.sections = self.tree.array(self.header.section_header_count, "sections")
					for i in range(0, self.header.section_header_count):
						section = self.sections[i]
						section.seek(self.header.section_header_offset + (i * 40))
						section.uint32("name")
						section.uint32("type")
						section.uint32("flags")
						section.uint32("addr")
						section.uint32("offset")
						section.uint32("size")
						section.uint32("link")
						section.uint32("info")
						section.uint32("align")
						section.uint32("entry_size")

						if section.type == 2:
							self.symbol_table_section = section
						elif section.type == 11:
							self.dynamic_symbol_table_section = section
				except:
					# Section headers are not required to load an ELF, skip errors
					self.sections = self.tree.array(0, "sections")
					pass

				self.program_headers = self.tree.array(self.header.program_header_count, "programHeaders")
				for i in range(0, self.header.program_header_count):
					header = self.program_headers[i]
					header.seek(self.header.program_header_offset + (i * 32))
					header.uint32("type")
					header.uint32("offset")
					header.uint32("virtual_addr")
					header.uint32("physical_addr")
					header.uint32("file_size")
					header.uint32("memory_size")
					header.uint32("flags")
					header.uint32("align")

				# Parse symbol tables
				self.symbols_by_name["_start"] = self.entry()
				self.symbols_by_addr[self.entry()] = "_start"

				try:
					if self.symbol_table_section:
						self.symbol_table = self.tree.array(self.symbol_table_section.size / 16, "Symbols", "symbols")
						self.parse_symbol_table_32(self.symbol_table, self.symbol_table_section, self.sections[self.symbol_table_section.link])

					if self.dynamic_symbol_table_section:
						self.dynamic_symbol_table = self.tree.array(self.dynamic_symbol_table_section.size / 16, "Symbols", "symbols")
						self.parse_symbol_table_32(self.dynamic_symbol_table, self.dynamic_symbol_table_section, self.sections[self.dynamic_symbol_table_section.link])
				except:
					# Skip errors in symbol table
					pass

				# Parse relocation tables
				self.plt = {}
				for section in self.sections:
					if section.type == 9:
						self.parse_reloc_32(section)
					elif section.type == 4:
						self.parse_reloca_32(section)
			elif self.header.ident.file_class == 2: # 64-bit
				self.header.uint64("entry")
				self.header.uint64("program_header_offset")
				self.header.uint64("section_header_offset")
				self.header.uint32("flags")
				self.header.uint16("header_size")
				self.header.uint16("program_header_size")
				self.header.uint16("program_header_count")
				self.header.uint16("section_header_size")
				self.header.uint16("section_header_count")
				self.header.uint16("string_table")

				try:
					self.sections = self.tree.array(self.header.section_header_count, "sections")
					for i in range(0, self.header.section_header_count):
						section = self.sections[i]
						section.seek(self.header.section_header_offset + (i * 64))
						section.uint32("name")
						section.uint32("type")
						section.uint64("flags")
						section.uint64("addr")
						section.uint64("offset")
						section.uint64("size")
						section.uint32("link")
						section.uint32("info")
						section.uint64("align")
						section.uint64("entry_size")

						if section.type == 2:
							self.symbol_table_section = section
						elif section.type == 11:
							self.dynamic_symbol_table_section = section
				except:
					# Section headers are not required to load an ELF, skip errors
					self.sections = self.tree.array(0, "sections")
					pass

				self.program_headers = self.tree.array(self.header.program_header_count, "program_headers")
				for i in range(0, self.header.program_header_count):
					header = self.program_headers[i]
					header.seek(self.header.program_header_offset + (i * 56))
					header.uint32("type")
					header.uint32("flags")
					header.uint64("offset")
					header.uint64("virtual_addr")
					header.uint64("physical_addr")
					header.uint64("file_size")
					header.uint64("memory_size")
					header.uint64("align")

				# Parse symbol tables
				self.symbols_by_name["_start"] = self.entry()
				self.symbols_by_addr[self.entry()] = "_start"

				try:
					if self.symbol_table_section:
						self.symbol_table = self.tree.array(self.symbol_table_section.size / 24, "Symbols", "symbols")
						self.parse_symbol_table_64(self.symbol_table, self.symbol_table_section, self.sections[self.symbol_table_section.link])

					if self.dynamic_symbol_table_section:
						self.dynamic_symbol_table = self.tree.array(self.dynamic_symbol_table_section.size / 24, "Symbols", "symbols")
						self.parse_symbol_table_64(self.dynamic_symbol_table, self.dynamic_symbol_table_section, self.sections[self.dynamic_symbol_table_section.link])
				except:
					# Skip errors in symbol table
					pass

				# Parse relocation tables
				self.plt = {}
				for section in self.sections:
					if section.type == 9:
						self.parse_reloc_64(section)
					elif section.type == 4:
						self.parse_reloca_64(section)

			self.tree.complete()
			self.valid = True
		except:
			self.valid = False

		if self.valid:
			self.data.add_callback(self)

	def read_string_table(self, strings, offset):
		end = strings.find("\x00", offset)
		return strings[offset:end]

	def parse_symbol_table_32(self, table, section, string_table):
		strings = self.data.read(string_table.offset, string_table.size)
		for i in range(0, section.size / 16):
			table[i].seek(section.offset + (i * 16))
			table[i].uint32("name_offset")
			table[i].uint32("value")
			table[i].uint32("size")
			table[i].uint8("info")
			table[i].uint8("other")
			table[i].uint16("section")
			table[i].name = self.read_string_table(strings, table[i].name_offset)

			if len(table[i].name) > 0:
				self.symbols_by_name[table[i].name] = table[i].value
				self.symbols_by_addr[table[i].value] = table[i].name

	def parse_symbol_table_64(self, table, section, string_table):
		strings = self.data.read(string_table.offset, string_table.size)
		for i in range(0, section.size / 24):
			table[i].seek(section.offset + (i * 24))
			table[i].uint32("name_offset")
			table[i].uint8("info")
			table[i].uint8("other")
			table[i].uint16("section")
			table[i].uint64("value")
			table[i].uint64("size")
			table[i].name = self.read_string_table(strings, table[i].name_offset)

			if len(table[i].name) > 0:
				self.symbols_by_name[table[i].name] = table[i].value
				self.symbols_by_addr[table[i].value] = table[i].name

	def parse_reloc_32(self, section):
		for i in range(0, section.size / 8):
			ofs = self.data.read_uint32(section.offset + (i * 8))
			info = self.data.read_uint32(section.offset + (i * 8) + 4)
			sym = info >> 8
			reloc_type = info & 0xff
			if reloc_type == 7: # R_386_JUMP_SLOT
				self.plt[ofs] = self.dynamic_symbol_table[sym].name
				self.symbols_by_name[self.decorate_plt_name(self.dynamic_symbol_table[sym].name)] = ofs
				self.symbols_by_addr[ofs] = self.decorate_plt_name(self.dynamic_symbol_table[sym].name)

	def parse_reloca_32(self, section):
		for i in range(0, section.size / 12):
			ofs = self.data.read_uint32(section.offset + (i * 12))
			info = self.data.read_uint32(section.offset + (i * 12) + 4)
			sym = info >> 8
			reloc_type = info & 0xff
			if reloc_type == 7: # R_386_JUMP_SLOT
				self.plt[ofs] = self.dynamic_symbol_table[sym].name
				self.symbols_by_name[self.decorate_plt_name(self.dynamic_symbol_table[sym].name)] = ofs
				self.symbols_by_addr[ofs] = self.decorate_plt_name(self.dynamic_symbol_table[sym].name)

	def parse_reloc_64(self, section):
		for i in range(0, section.size / 16):
			ofs = self.data.read_uint64(section.offset + (i * 16))
			info = self.data.read_uint64(section.offset + (i * 16) + 8)
			sym = info >> 32
			reloc_type = info & 0xff
			if reloc_type == 7: # R_X86_64_JUMP_SLOT
				self.plt[ofs] = self.dynamic_symbol_table[sym].name
				self.symbols_by_name[self.decorate_plt_name(self.dynamic_symbol_table[sym].name)] = ofs
				self.symbols_by_addr[ofs] = self.decorate_plt_name(self.dynamic_symbol_table[sym].name)

	def parse_reloca_64(self, section):
		for i in range(0, section.size / 24):
			ofs = self.data.read_uint64(section.offset + (i * 24))
			info = self.data.read_uint64(section.offset + (i * 24) + 8)
			sym = info >> 32
			reloc_type = info & 0xff
			if reloc_type == 7: # R_X86_64_JUMP_SLOT
				self.plt[ofs] = self.dynamic_symbol_table[sym].name
				self.symbols_by_name[self.decorate_plt_name(self.dynamic_symbol_table[sym].name)] = ofs
				self.symbols_by_addr[ofs] = self.decorate_plt_name(self.dynamic_symbol_table[sym].name)

	def read(self, ofs, len):
		result = ""
		while len > 0:
			cur = None
			for i in self.program_headers:
				if ((ofs >= i.virtual_addr) and (ofs < (i.virtual_addr + i.memory_size))) and (i.memory_size != 0):
					cur = i
			if cur == None:
				break

			prog_ofs = ofs - cur.virtual_addr
			mem_len = cur.memory_size - prog_ofs
			file_len = cur.file_size - prog_ofs
			if mem_len > len:
				mem_len = len
			if file_len > len:
				file_len = len

			if file_len <= 0:
				result += "\x00" * mem_len
				len -= mem_len
				ofs += mem_len
				continue

			result += self.data.read(cur.offset + prog_ofs, file_len)
			len -= file_len
			ofs += file_len

		return result

	def next_valid_addr(self, ofs):
		result = -1
		for i in self.program_headers:
			if (i.virtual_addr >= ofs) and (i.memory_size != 0) and ((result == -1) or (i.virtual_addr < result)):
				result = i.virtual_addr
		return result

	def get_modification(self, ofs, len):
		result = []
		while len > 0:
			cur = None
			for i in self.program_headers:
				if ((ofs >= i.virtual_addr) and (ofs < (i.virtual_addr + i.memory_size))) and (i.memory_size != 0):
					cur = i
			if cur == None:
				break

			prog_ofs = ofs - cur.virtual_addr
			mem_len = cur.memory_size - prog_ofs
			file_len = cur.file_size - prog_ofs
			if mem_len > len:
				mem_len = len
			if file_len > len:
				file_len = len

			if file_len <= 0:
				result += [DATA_ORIGINAL] * mem_len
				len -= mem_len
				ofs += mem_len
				continue

			result += self.data.get_modification(cur.offset + prog_ofs, file_len)
			len -= file_len
			ofs += file_len

		return result

	def write(self, ofs, data):
		result = 0
		while len(data) > 0:
			cur = None
			for i in self.program_headers:
				if ((ofs >= i.virtual_addr) and (ofs < (i.virtual_addr + i.memory_size))) and (i.memory_size != 0):
					cur = i
			if cur == None:
				break

			prog_ofs = ofs - cur.virtual_addr
			mem_len = cur.memory_size - prog_ofs
			file_len = cur.file_size - prog_ofs
			if mem_len > len:
				mem_len = len
			if file_len > len:
				file_len = len

			if file_len <= 0:
				break

			result += self.data.write(cur.offset + prog_ofs, data[0:file_len])
			data = data[file_len:]
			ofs += file_len

		return result

	def insert(self, ofs, data):
		return 0

	def remove(self, ofs, size):
		return 0

	def notify_data_write(self, data, ofs, contents):
		# Find sections that hold data backed by updated regions of the file
		for i in self.program_headers:
			if ((ofs + len(contents)) > i.offset) and (ofs < (i.offset + i.file_size)) and (i.memory_size != 0):
				# This section has been updated, compute which region has been changed
				from_start = ofs - i.offset
				data_ofs = 0
				length = len(contents)
				if from_start < 0:
					length += from_start
					data_ofs -= from_start
					from_start = 0
				if (from_start + length) > i.file_size:
					length = i.file_size - from_start

				# Notify callbacks
				if length > 0:
					for cb in self.callbacks:
						if hasattr(cb, "notify_data_write"):
							cb.notify_data_write(self, i.virtual_addr + from_start,
								contents[data_ofs:(data_ofs + length)])

	def save(self, filename):
		self.data.save(filename)

	def start(self):
		result = None
		for i in self.program_headers:
			if ((result == None) or (i.virtual_addr < result)) and (i.memory_size != 0):
				result = i.virtual_addr
		return result

	def entry(self):
		return self.header.entry

	def __len__(self):
		max = None
		for i in self.program_headers:
			if ((max == None) or ((i.virtual_addr + i.memory_size) > max)) and (i.memory_size != 0):
				max = i.virtual_addr + i.memory_size
		return max - self.start()

	def is_elf(self):
		return self.data.read(0, 4) == "\x7fELF"

	def architecture(self):
		if self.header.arch == 2:
			return "sparc"
		if self.header.arch == 3:
			return "x86"
		if self.header.arch == 4:
			return "m68000"
		if self.header.arch == 8:
			return "mips"
		if self.header.arch == 15:
			return "pa_risc"
		if self.header.arch == 18:
			return "sparc_32plus"
		if self.header.arch == 20:
			return "ppc"
		if self.header.arch == 40:
			return "arm"
		if self.header.arch == 41:
			return "alpha"
		if self.header.arch == 43:
			return "sparc_v9"
		if self.header.arch == 62:
			return "x86_64"
		return None

	def decorate_plt_name(self, name):
		return name + "@PLT"

	def create_symbol(self, addr, name):
		self.symbols_by_name[name] = addr
		self.symbols_by_addr[addr] = name

	def delete_symbol(self, addr, name):
		if name in self.symbols_by_name:
			del(self.symbols_by_name[name])
		if addr in self.symbols_by_addr:
			del(self.symbols_by_addr[addr])

	def add_callback(self, cb):
		self.callbacks.append(cb)

	def remove_callback(self, cb):
		self.callbacks.remove(cb)

	def is_modified(self):
		return self.data.is_modified()

	def find(self, regex, addr):
		while (addr < self.end()) and (addr != -1):
			data = self.read(addr, 0xfffffffff)
			match = regex.search(data)
			if match != None:
				return match.start() + addr

			addr += len(data)
			addr = self.next_valid_addr(addr)

		return -1

	def has_undo_actions(self):
		return self.data.has_undo_actions()

	def commit_undo(self, before_loc, after_loc):
		self.data.commit_undo(before_loc, after_loc)

	def undo(self):
		self.data.undo()

	def redo(self):
		self.data.redo()
Пример #35
0
                            TB.idx[at][TB.TB_orbs[at][0]],
                            TB.idx[at][TB.TB_orbs[at][-1]] + 1,
                        )
                    ]
                ),
                self.ommesh,
                "G_loc_" + at + ".out",
            )


if __name__ == "__main__":

    import Struct, VASP

    execfile("INPUT.py")  # Read input file
    TB = Struct.TBstructure("POSCAR", p["atomnames"], p["orbs"])
    cor_at = p["cor_at"]
    cor_orb = p["cor_orb"]
    TB.Compute_cor_idx(cor_at, cor_orb)
    print TB.TB_orbs
    DFT = VASP.VASP_class()
    DMFT = DMFT_class(p, pC, TB)
    DMFT.Update_Sigoo_and_Vdc(TB, "sig.inp")
    DMFT.Update_Nlatt(TB, p["nspin"])
    DMFT.Read_Sig(TB, p["nspin"])
    # print DMFT.Sig
    # DMFT.Compute_Sigoo_and_Vdc(Nd_qmc,p,TB)
    ed = array([loadtxt("Ed.out")])
    print ed
    DMFT.Compute_Energy(DFT, TB, ed)
    DMFT.Compute_Sigoo_and_Vdc(p, TB)
Пример #36
0
import csv
import Struct
import gzip

counts=dict()

TUMOR=sys.argv[1]
NORMAL=sys.argv[2]
MINCOV_NORMAL=25

print >>sys.stderr, "Reading normal ..."
cin=csv.DictReader(gzip.open(NORMAL),delimiter="\t")
CovStruct=Struct.Struct
CovStruct.setFields(cin.fieldnames)
for recD in cin:
	rec=Struct.Struct(recD)
	if len(rec.Ref)>1 or len(rec.Alt)>1:
		continue
	if rec.Ref=="N":
		continue
	if int(rec.BASEQ_depth)<MINCOV_NORMAL:
		continue
	key=(rec.Chrom, rec.Pos, rec.Ref, rec.Alt)
	counts[key]=rec
print >>sys.stderr, "done"
print >>sys.stderr, "Reading tumor ..."

HEADER="""
Chrom Pos Ref Alt
TUM.DP
TUM.Ap TUM.Cp TUM.Gp TUM.Tp
Пример #37
0
	def __init__(self, data):
		self.data = data
		self.valid = False
		self.callbacks = []
		self.symbols_by_name = {}
		self.symbols_by_addr = {}
		if not self.is_elf():
			return

		try:
			self.tree = Struct(self.data)
			self.header = self.tree.struct("ELF header", "header")
			self.header.struct("ELF identification", "ident")

			self.header.ident.uint32("magic")
			self.header.ident.uint8("file_class")
			self.header.ident.uint8("encoding")
			self.header.ident.uint8("version")
			self.header.ident.uint8("abi")
			self.header.ident.uint8("abi_version")
			self.header.ident.bytes(7, "pad")

			self.header.uint16("type")
			self.header.uint16("arch")
			self.header.uint32("version")

			self.symbol_table_section = None
			self.dynamic_symbol_table_section = None

			if self.header.ident.file_class == 1: # 32-bit
				self.header.uint32("entry")
				self.header.uint32("program_header_offset")
				self.header.uint32("section_header_offset")
				self.header.uint32("flags")
				self.header.uint16("header_size")
				self.header.uint16("program_header_size")
				self.header.uint16("program_header_count")
				self.header.uint16("section_header_size")
				self.header.uint16("section_header_count")
				self.header.uint16("string_table")

				try:
					self.sections = self.tree.array(self.header.section_header_count, "sections")
					for i in range(0, self.header.section_header_count):
						section = self.sections[i]
						section.seek(self.header.section_header_offset + (i * 40))
						section.uint32("name")
						section.uint32("type")
						section.uint32("flags")
						section.uint32("addr")
						section.uint32("offset")
						section.uint32("size")
						section.uint32("link")
						section.uint32("info")
						section.uint32("align")
						section.uint32("entry_size")

						if section.type == 2:
							self.symbol_table_section = section
						elif section.type == 11:
							self.dynamic_symbol_table_section = section
				except:
					# Section headers are not required to load an ELF, skip errors
					self.sections = self.tree.array(0, "sections")
					pass

				self.program_headers = self.tree.array(self.header.program_header_count, "programHeaders")
				for i in range(0, self.header.program_header_count):
					header = self.program_headers[i]
					header.seek(self.header.program_header_offset + (i * 32))
					header.uint32("type")
					header.uint32("offset")
					header.uint32("virtual_addr")
					header.uint32("physical_addr")
					header.uint32("file_size")
					header.uint32("memory_size")
					header.uint32("flags")
					header.uint32("align")

				# Parse symbol tables
				self.symbols_by_name["_start"] = self.entry()
				self.symbols_by_addr[self.entry()] = "_start"

				try:
					if self.symbol_table_section:
						self.symbol_table = self.tree.array(self.symbol_table_section.size / 16, "Symbols", "symbols")
						self.parse_symbol_table_32(self.symbol_table, self.symbol_table_section, self.sections[self.symbol_table_section.link])

					if self.dynamic_symbol_table_section:
						self.dynamic_symbol_table = self.tree.array(self.dynamic_symbol_table_section.size / 16, "Symbols", "symbols")
						self.parse_symbol_table_32(self.dynamic_symbol_table, self.dynamic_symbol_table_section, self.sections[self.dynamic_symbol_table_section.link])
				except:
					# Skip errors in symbol table
					pass

				# Parse relocation tables
				self.plt = {}
				for section in self.sections:
					if section.type == 9:
						self.parse_reloc_32(section)
					elif section.type == 4:
						self.parse_reloca_32(section)
			elif self.header.ident.file_class == 2: # 64-bit
				self.header.uint64("entry")
				self.header.uint64("program_header_offset")
				self.header.uint64("section_header_offset")
				self.header.uint32("flags")
				self.header.uint16("header_size")
				self.header.uint16("program_header_size")
				self.header.uint16("program_header_count")
				self.header.uint16("section_header_size")
				self.header.uint16("section_header_count")
				self.header.uint16("string_table")

				try:
					self.sections = self.tree.array(self.header.section_header_count, "sections")
					for i in range(0, self.header.section_header_count):
						section = self.sections[i]
						section.seek(self.header.section_header_offset + (i * 64))
						section.uint32("name")
						section.uint32("type")
						section.uint64("flags")
						section.uint64("addr")
						section.uint64("offset")
						section.uint64("size")
						section.uint32("link")
						section.uint32("info")
						section.uint64("align")
						section.uint64("entry_size")

						if section.type == 2:
							self.symbol_table_section = section
						elif section.type == 11:
							self.dynamic_symbol_table_section = section
				except:
					# Section headers are not required to load an ELF, skip errors
					self.sections = self.tree.array(0, "sections")
					pass

				self.program_headers = self.tree.array(self.header.program_header_count, "program_headers")
				for i in range(0, self.header.program_header_count):
					header = self.program_headers[i]
					header.seek(self.header.program_header_offset + (i * 56))
					header.uint32("type")
					header.uint32("flags")
					header.uint64("offset")
					header.uint64("virtual_addr")
					header.uint64("physical_addr")
					header.uint64("file_size")
					header.uint64("memory_size")
					header.uint64("align")

				# Parse symbol tables
				self.symbols_by_name["_start"] = self.entry()
				self.symbols_by_addr[self.entry()] = "_start"

				try:
					if self.symbol_table_section:
						self.symbol_table = self.tree.array(self.symbol_table_section.size / 24, "Symbols", "symbols")
						self.parse_symbol_table_64(self.symbol_table, self.symbol_table_section, self.sections[self.symbol_table_section.link])

					if self.dynamic_symbol_table_section:
						self.dynamic_symbol_table = self.tree.array(self.dynamic_symbol_table_section.size / 24, "Symbols", "symbols")
						self.parse_symbol_table_64(self.dynamic_symbol_table, self.dynamic_symbol_table_section, self.sections[self.dynamic_symbol_table_section.link])
				except:
					# Skip errors in symbol table
					pass

				# Parse relocation tables
				self.plt = {}
				for section in self.sections:
					if section.type == 9:
						self.parse_reloc_64(section)
					elif section.type == 4:
						self.parse_reloca_64(section)

			self.tree.complete()
			self.valid = True
		except:
			self.valid = False

		if self.valid:
			self.data.add_callback(self)