def process_file(path, fn, args, state):
    """
    The main function for reading a file and decrypting it.
    """
    full_fn = os.path.join(path, fn)

    if not re.match("^[cbdefghijklnrtuv]+$", fn):
        if args.debug:
            sys.stderr.write("warning: Ignoring non-modhex file '%s'\n" % (full_fn))
        return True

    if (args.start_id is not None) or (args.stop_id is not None):
        this = int(pyhsm.yubikey.modhex_decode(fn), 16)
        if (args.start_id is not None) and this < args.start_id:
            if args.debug:
                sys.stderr.write("warning: Skipping public id %s (%i) < %i\n" % (fn, this, args.start_id))
            return True
        if (args.stop_id is not None) and this > args.stop_id:
            if args.debug:
                sys.stderr.write("warning: Skipping public id %s (%i) > %i\n" % (fn, this, args.stop_id))
            return True

    if args.debug:
        sys.stderr.write("Loading AEAD : %s\n" % full_fn)

    aead = pyhsm.aead_cmd.YHSM_GeneratedAEAD(None, None, '')
    aead.load(full_fn)

    if not aead.nonce:
        # AEAD file version 0, need to fill in nonce etc.
        if args.key_handle is None:
            sys.stderr.write("error: AEAD in file %s does not include key_handle, and none provided.\n" % (full_fn))
            state.log_failed(full_fn)
            return False
        aead.key_handle = args.key_handle
        aead.nonce = pyhsm.yubikey.modhex_decode(fn).decode('hex')

    if args.debug:
        sys.stderr.write("%s\n" % aead)
        sys.stderr.write("AEAD len %i : %s\n" % (len(aead.data), aead.data.encode('hex')))
    pt = pyhsm.soft_hsm.aesCCM(args.aes_key, aead.key_handle, aead.nonce, aead.data, decrypt = True)

    if args.print_filename:
        print("%s " % (full_fn)),

    if args.format == 'raw':
        print(pt.encode('hex'))
    elif args.format == 'aead':
        # encrypt secrets with new key
        ct = pyhsm.soft_hsm.aesCCM(args.aes_key_out, args.key_handle_out, aead.nonce, pt, decrypt = False)
        aead_out = pyhsm.aead_cmd.YHSM_GeneratedAEAD(aead.nonce, args.key_handle_out, ct)
        filename = aead_filename(args.output_dir, args.key_handle_out_orig, fn)
        aead_out.save(filename)
        if args.print_filename:
            print ""
    elif args.format == 'yubikey-csv':
        key = pt[:pyhsm.defines.KEY_SIZE]
        uid = pt[pyhsm.defines.KEY_SIZE:]
        access_code = '00' * 6
        timestamp = ''
        global yknum
        yknum += 1
        print("%i,%s,%s,%s,%s,%s,,,,," % (yknum,
                                          fn,
                                          uid.encode('hex'),
                                          key.encode('hex'),
                                          access_code,
                                          timestamp,
                                          ))

    state.log_success(full_fn)

    return True
The main function for reading a file and decrypting it.