'Iterate over all occurrences of any keyword in a file.
Returns (keyword, offset) pairs.'

def filefind(self, f):

    opened = False
    if (not hasattr(f, 'read')):
        f = open(f, 'rb')
        opened = True
    try:
        state = self.start_state
        start_state = (state, ())
        next_state = self.transitions.get
        pos = 0
        while 1:
            data = f.read(FILE_BUFFER_SIZE)
            if (not data):
                break
            for char in data:
                pos += 1
                (state, matches) = next_state((state, char), start_state)
                if matches:
                    for match in matches:
                        (yield (match, (pos - len(match))))
    finally:
        if opened:
            f.close()
