Esempio n. 1
0
def read_program(filename):
    '''
    Identifies the program header (ELF/PE) and returns an ELF, PE or DataIO
    instance.

    Args:
        filename (str): the program to read.

    Returns:
        an instance of currently supported program format (ELF, PE)

    '''

    try:
        data = open(filename, 'rb')
    except (TypeError, IOError):
        data = bytes(filename)

    f = DataIO(data)

    try:
        # open file as a ELF object:
        p = elf.Elf(f)
        logger.info("ELF format detected")
        return p
    except elf.ElfError:
        f.seek(0)
        logger.debug('ElfError raised for %s' % f.name)

    try:
        # open file as a PE object:
        p = pe.PE(f)
        logger.info("PE format detected")
        return p
    except pe.PEError:
        f.seek(0)
        logger.debug('PEError raised for %s' % f.name)

    try:
        # open file as a HEX object:
        p = utils.HEX(f)
        logger.info("HEX format detected")
        return p
    except utils.FormatError:
        f.seek(0)
        logger.debug(' HEX FormatError raised for %s' % f.name)

    try:
        # open file as a SREC object:
        p = utils.SREC(f)
        logger.info("SREC format detected")
        return p
    except utils.FormatError:
        f.seek(0)
        logger.debug(' SREC FormatError raised for %s' % f.name)

    logger.warning('unknown format')
    return f
Esempio n. 2
0
def read_program(filename):
    """
    Identifies the program header and returns an ELF, PE, Mach-O or DataIO.

    Args:
        filename (str): the program to read.

    Returns:
        an instance of currently supported program format
        (ELF, PE, Mach-O, HEX, SREC)
    """

    try:
        data = open(filename, "rb")
    except (ValueError, TypeError, IOError):
        data = bytes(filename)

    f = DataIO(data)

    try:
        from amoco.system import elf

        # open file as a ELF object:
        p = elf.Elf(f)
        logger.info("ELF format detected")
        return p
    except elf.ElfError:
        f.seek(0)
        logger.debug("ElfError raised for %s" % f.name)

    try:
        from amoco.system import pe

        # open file as a PE object:
        p = pe.PE(f)
        logger.info("PE format detected")
        return p
    except pe.PEError:
        f.seek(0)
        logger.debug("PEError raised for %s" % f.name)

    try:
        from amoco.system import macho

        # open file as a Mach-O object:
        p = macho.MachO(f)
        logger.info("Mach-O format detected")
        return p
    except macho.MachOError:
        f.seek(0)
        logger.debug("MachOError raised for %s" % f.name)

    try:
        from amoco.system import utils

        # open file as a HEX object:
        p = utils.HEX(f)
        logger.info("HEX format detected")
        return p
    except utils.FormatError:
        f.seek(0)
        logger.debug(" HEX FormatError raised for %s" % f.name)

    try:
        # open file as a SREC object:
        p = utils.SREC(f)
        logger.info("SREC format detected")
        return p
    except utils.FormatError:
        f.seek(0)
        logger.debug(" SREC FormatError raised for %s" % f.name)

    logger.warning("unknown format")
    return f