#------------------------------------------------------------
# Chris Lomont written IdaPRO Python tools
#------------------------------------------------------------
"""
LomontIdaProScripts.py - testing functions
"""
import idaapi
import idc
import types

def CountInstructions():
    """
    Count number of disassembled instructions in the current file
    """
    Message("\n\nRunning the instruction counter.\n")
    minEA = idaapi.cvar.inf.minEA
    maxEA = idaapi.cvar.inf.maxEA
    instructions = 0
    dataCount = 0
    dataBytes = 0
    unknownCount = 0
    codeBytes = 0
    Message("Searching range %d to %d.\n" % (minEA,maxEA))
    ea = minEA # minimum effective address
    while ea != BADADDR and ea <= maxEA:
        flags = GetFlags(ea) # get info about current EA
        eaNext = idaapi.next_head(ea, maxEA)
        if isCode(flags):
            instructions = instructions + 1
            if eaNext != BADADDR:
                codeBytes = codeBytes + eaNext - ea
            else :
                codeBytes = codeBytes + maxEA - ea                
        if isData(flags):
            dataCount = dataCount + 1
            if eaNext != BADADDR:
                dataBytes = dataBytes + eaNext - ea
            else:
                dataBytes = dataBytes + maxEA - ea                               
        if isUnknown(flags):
            unknownCount = unknownCount + 1
        ea = eaNext
    Message("%d bytes\n%d instructions in %d bytes\n%d data locations in %d bytes\n%d unknown bytes\n"
            % (maxEA-minEA,instructions, codeBytes,dataCount, dataBytes, unknownCount) )
    Message("%d total bytes, %d in data and code\n"%
            (maxEA-minEA,codeBytes+dataBytes))

# end of file
