# Path: sibyl/heuristics/func.py
# class FuncHeuristic(Heuristic):
#     """Provide heuristic for function start address detection"""
# 
#     # Enabled passes
#     heuristics = [
#         named_symbols,
#         pattern_matching,
#         recursive_call,
#         ida_funcs,
#         ghidra_funcs,
#     ]
# 
#     def __init__(self, cont, machine, filename):
#         """
#         @cont: miasm2's Container instance
#         @machine: miasm2's Machine instance
#         @filename: target's filename
#         """
#         super(FuncHeuristic, self).__init__()
#         self.cont = cont
#         self.machine = machine
#         self.filename = filename
# 
#     def do_votes(self):
#         """Call recursive_call at the end"""
#         do_recursive = False
#         if recursive_call in self.heuristics:
#             do_recursive = True
#             self.heuristics.remove(recursive_call)
# 
#         super(FuncHeuristic, self).do_votes()
#         addresses = self._votes
# 
#         if do_recursive:
#             new_addresses = recursive_call(self,
#                                            [addr
#                                             for addr, vote in addresses.iteritems()
#                                             if vote > 0])
#             for addr, vote in new_addresses.iteritems():
#                 addresses[addr] = addresses.get(addr, 0) + vote
#         self._votes = addresses
# 
#     def guess(self):
#         for address, value in self.votes.iteritems():
#             # Heuristic may vote negatively
#             if value > 0:
#                 yield address


import os
import re
import subprocess
from argparse import ArgumentParser
from utils.log import log_error, log_success, log_info
from elfesteem.elf_init import ELF
from sibyl.heuristics.func import FuncHeuristic
    with open(c_file) as fdesc:
        data = fdesc.read()
    funcs = []
    for match in match_C.finditer(data):
        funcs.append(match.groups()[0])
    funcs = list(name for name in set(funcs) if name not in whitelist_funcs)

    # Find corresponding binary offset
    to_check = []
    with open(filename) as fdesc:
        elf = ELF(fdesc.read())

    symbols = {}
    for name, symb in elf.getsectionbyname(".symtab").symbols.iteritems():
        offset = symb.value
        if name.startswith("__"):
            name = name[2:]
        symbols.setdefault(name, set()).add(offset)
        if name in funcs:
            if name.startswith(custom_tag):
                ## Custom tags can be used to write equivalent functions like
                ## 'my_strlen' for a custom strlen
                name = name[len(custom_tag):]
            to_check.append((offset, name))
    return to_check, symbols


def get_funcs_heuristics(c_file, filename):
    """Get function from Sibyl heuristics"""
    # Force the activation of all heuristics

# Path: sibyl/heuristics/func.py
# class FuncHeuristic(Heuristic):
#     """Provide heuristic for function start address detection"""
# 
#     # Enabled passes
#     heuristics = [
#         named_symbols,
#         pattern_matching,
#         recursive_call,
#         ida_funcs,
#         ghidra_funcs,
#     ]
# 
#     def __init__(self, cont, machine, filename):
#         """
#         @cont: miasm2's Container instance
#         @machine: miasm2's Machine instance
#         @filename: target's filename
#         """
#         super(FuncHeuristic, self).__init__()
#         self.cont = cont
#         self.machine = machine
#         self.filename = filename
# 
#     def do_votes(self):
#         """Call recursive_call at the end"""
#         do_recursive = False
#         if recursive_call in self.heuristics:
#             do_recursive = True
#             self.heuristics.remove(recursive_call)
# 
#         super(FuncHeuristic, self).do_votes()
#         addresses = self._votes
# 
#         if do_recursive:
#             new_addresses = recursive_call(self,
#                                            [addr
#                                             for addr, vote in addresses.iteritems()
#                                             if vote > 0])
#             for addr, vote in new_addresses.iteritems():
#                 addresses[addr] = addresses.get(addr, 0) + vote
#         self._votes = addresses
# 
#     def guess(self):
#         for address, value in self.votes.iteritems():
#             # Heuristic may vote negatively
#             if value > 0:
#                 yield address


# Path: test/find/run_ctests.py
import os
import re
import subprocess
from argparse import ArgumentParser
from utils.log import log_error, log_success, log_info
from elfesteem.elf_init import ELF
from sibyl.heuristics.func import FuncHeuristic

    with open(c_file) as fdesc:
        data = fdesc.read()
    funcs = []
    for match in match_C.finditer(data):
        funcs.append(match.groups()[0])
    funcs = list(name for name in set(funcs) if name not in whitelist_funcs)

    # Find corresponding binary offset
    to_check = []
    with open(filename) as fdesc:
        elf = ELF(fdesc.read())

    symbols = {}
    for name, symb in elf.getsectionbyname(".symtab").symbols.iteritems():
        offset = symb.value
        if name.startswith("__"):
            name = name[2:]
        symbols.setdefault(name, set()).add(offset)
        if name in funcs:
            if name.startswith(custom_tag):
                ## Custom tags can be used to write equivalent functions like
                ## 'my_strlen' for a custom strlen
                name = name[len(custom_tag):]
            to_check.append((offset, name))
    return to_check, symbols


def get_funcs_heuristics(c_file, filename):
    """Get function from Sibyl heuristics"""
    # Force the activation of all heuristics

    fh = FuncHeuristic(None, None, "")