⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pgpfileref.c

📁 著名的加密软件的应用于电子邮件中
💻 C
📖 第 1 页 / 共 3 页
字号:
    pgpMemFree(newPath);
    return result;
}

    FILE *
pgpStdIOOpenTempFile(
    PGPFileRef **       tempFileRef,
    PGPError *          errorCode)
{
    char                fileName[L_tmpnam];
    PGPError            result = PGPERR_OK;
    FILE *              stdIOFile = NULL;

    pgpa(pgpaAddrValid(tempFileRef, PGPFileRef *));

    *tempFileRef = NULL;

#if defined(_PATH_TMP) && HAVE_MKSTEMP  /* [ */

    {
        int     fd = -1;

        strncpy(fileName, _PATH_TMP "ptmpXXXXXX", sizeof(fileName));
        fileName[sizeof(fileName) - 1] = '\0';
        if ((fd = mkstemp(fileName)) == -1 ||
            (stdIOFile = fdopen(fd, "w+b")) == NULL)
        {
            if (fd != -1)
                close(fd);
            goto fileError;
        }
        if ((*tempFileRef = pgpNewFileRefFromFullPath(fileName)) == NULL)
        {
            result = PGPERR_NOMEM;
            goto error;
        }
    }

#else  /* ] defined(_PATH_TMP) && HAVE_MKSTEMP [ */

    if (tmpnam(fileName) != fileName)
        goto fileError;

    if ((*tempFileRef = pgpNewFileRefFromFullPath(fileName)) == NULL)
    {
        result = PGPERR_NOMEM;
        goto error;
    }

    /* XXX Perhaps check for an error and retry if necessary */
    stdIOFile = pgpFileRefStdIOOpen(*tempFileRef, kPGPFileOpenStdUpdateFlags,
                                    kPGPFileTypeDecryptedBin, errorCode);

#endif  /* ] defined(_PATH_TMP) && HAVE_MKSTEMP */

    if (stdIOFile == NULL)
    {
    fileError:
        result = PGPERR_NO_FILE;
        goto error;
    }

    return stdIOFile;

error:
    if (*tempFileRef != NULL)
    {
        pgpFreeFileRef(*tempFileRef);
        *tempFileRef = NULL;
    }
    if (errorCode != NULL)
    {
        pgpAssertAddrValid(errorCode, PGPError);
        *errorCode = result;
    }
    return NULL;
}

#endif  /* ] MACINTOSH */

    PgpFile *
pgpFileRefOpen(
    PGPFileRef const *  fileRef,
    PGPFileOpenFlags    flags,
    PGPFileType         fileType,
    PGPError *          errorCode)
{
    FILE *              stdIOFile;

#if MACINTOSH  /* [ */
        switch (flags & (kPGPFileOpenReadWritePerm |
                         kPGPFileOpenAppend | kPGPFileOpenTruncate |
                         kPGPFileOpenCreate | kPGPFileOpenTextMode))
        {
            case kPGPFileOpenReadPerm:
                return pgpFileRefMacReadOpen(fileRef, flags, errorCode);
            case kPGPFileOpenStdWriteFlags:
                return pgpFileRefMacWriteOpen(fileRef, fileType, flags,
                                              errorCode);
            default:
                break;
        }
        pgpAssertMsg((flags & (kPGPFileOpenMaybeLocalEncode |
                               kPGPFileOpenForceLocalEncode)) == 0,
               "This combination of flags not currently supported");
#endif  /* ] MACINTOSH */

    stdIOFile = pgpFileRefStdIOOpen(fileRef, flags, fileType, errorCode);
    if (stdIOFile == NULL)
        return NULL;

    switch (flags & kPGPFileOpenReadWritePerm)
    {
        case kPGPFileOpenReadPerm:
            return pgpFileReadOpen(stdIOFile, NULL, NULL);
        case kPGPFileOpenWritePerm:
            return pgpFileWriteOpen(stdIOFile, NULL);
        default:
            pgpAssertMsg(0, "No support for simultaneous read/write yet");
            break;
    }
    fclose(stdIOFile);
    return NULL;
}

    PgpFileRead *
pgpFileRefReadCreate(
    PGPFileRef const *  fileRef,
    PGPFileOpenFlags    flags,
    PGPError *          errorCode)
{
    PgpFile *           pgpFile;
    PgpFileRead *       pgpFileRead;

    if ((pgpFile = pgpFileRefOpen(fileRef, flags,
                                  kPGPFileTypeNone, errorCode)) == NULL)
    return NULL;

    if ((pgpFileRead = pgpPGPFileReadCreate(pgpFile, TRUE)) == NULL)
    {
        pgpFileClose(pgpFile);
        return NULL;
    }
    return pgpFileRead;
}

/* XXX Compatibility routines follow, intended to be phased out */

/*
 * pgpFileRefStdIOOpen is currently the heart of the implementation
 * for opening files, but this may change later.
 */

    FILE *
pgpFileRefStdIOOpen(
    PGPFileRef const *  fileRef,
    PGPFileOpenFlags    flags,
    PGPFileType         fileType,
    PGPError *          errorCode)
{
    char        mode[5];
    FILE *      stdIOFile = NULL;
    PGPError    result = PGPERR_OK;

#if DEBUG
    pgpa(pgpaPGPFileRefValid(fileRef));
    if (flags & kPGPFileOpenCreate)
        pgpa(pgpaPGPFileTypeValid(fileType));
#endif

    switch (flags & ~(kPGPFileOpenTextMode |
                      kPGPFileOpenMaybeLocalEncode |
                      kPGPFileOpenForceLocalEncode |
                      kPGPFileOpenLocalEncodeHashOnly |
                      kPGPFileOpenNoMacBinCRCOkay))
    {
        case kPGPFileOpenReadPerm:
            strcpy(mode, "r");
            break;
        case kPGPFileOpenStdWriteFlags:
            strcpy(mode, "w");
            break;
        case kPGPFileOpenStdAppendFlags:
            strcpy(mode, "a");
            break;
        case kPGPFileOpenReadWritePerm:
            strcpy(mode, "r+");
            break;
        case kPGPFileOpenStdUpdateFlags:
            strcpy(mode, "w+");
            break;
        case kPGPFileOpenStdAppendFlags | kPGPFileOpenReadPerm:
            strcpy(mode, "a+");
            break;
        default:
            pgpAssertMsg(0, "Unsupported open mode");
            result = PGPERR_BADPARAM;
            goto error;
    }
    if (!(flags & kPGPFileOpenTextMode))
        strcat(mode, "b");

#if WIN32
    strcat (mode, "c");     /* Commit-to-disk mode, flushes writes out */
#endif

#if MACINTOSH  /* [ */
    {
        FSSpec   spec;
        short    refNum;
        SInt8    permission;
        OSErr    macResult;

        if (pgpFSSpecFromFileRef(fileRef, &spec) == PGPERR_OK)
        {
            if (flags & kPGPFileOpenCreate)
            {
                macResult = HCreate(spec.vRefNum, spec.parID, spec.name,
                                    sMacFileTypeTable[fileType].creator,
                                    sMacFileTypeTable[fileType].type);
                if (macResult != noErr && macResult != dupFNErr)
                {
                    result = pgpErrorFromMacError(macResult, PGPERR_NO_FILE);
                    goto error;
                }
            }
            switch (flags & kPGPFileOpenReadWritePerm)
            {
                case kPGPFileOpenReadPerm:
                    permission = fsRdPerm;
                    break;
                case kPGPFileOpenWritePerm:
                    permission = fsWrPerm;
                    break;
                case kPGPFileOpenReadWritePerm:
                    permission = fsRdWrPerm;
                    break;
                default:
                    pgpAssertMsg(0, "Unsupported open mode");
                    result = PGPERR_BADPARAM;
                    goto error;
            }
            if ((macResult = FSpOpenDF(&spec, permission, &refNum)) != noErr)
            {
                result = pgpErrorFromMacError(macResult, PGPERR_NO_FILE);
                goto error;
            }
            stdIOFile = fdopen(refNum, mode);
            if (stdIOFile == NULL)
                FSClose(refNum);
        }
    }
#else  /* ] MACINTOSH [ */
    {
        char *    fullPath = NULL;
#if UNIX
        mode_t    oldMask;
#endif

        if (pgpFullPathFromFileRef(fileRef, &fullPath) == PGPERR_OK)
        {
#if UNIX
            oldMask = umask(sUnixFileTypeTable[fileType].mask);
#endif
            stdIOFile = fopen(fullPath, mode);
            if (stdIOFile == NULL)
            {
                if (errno == EACCES
#ifdef EROFS
                    || errno == EROFS
#endif
                   )
                {
                    result = PGPERR_FILE_PERMISSIONS;
                }
                else
                {
                    result = PGPERR_NO_FILE;
                }
            }
#if UNIX
            umask(oldMask);
#endif
            pgpMemFree(fullPath);
            if (stdIOFile == NULL)
                goto error;
        }
    }
#endif  /* ] MACINTOSH */

    if (stdIOFile == NULL)
    {
        result = PGPERR_NO_FILE;
        goto error;
    }
    return stdIOFile;

error:
    if (errorCode != NULL)
    {
        pgpAssertAddrValid(errorCode, PGPError);
        *errorCode = result;
    }
    return NULL;
}


/*
 * Due to the way memory is handled on WIN32 systems with static linking,
 * fclose must be called from the same library which called fopen.
 * This function servers that purpose. It should be called by external
 * libraries and apps to close FILE * handles returned by this module.
 */
    PGPError
pgpStdIOClose(
    FILE *      stdIOFile)
{
    if (fclose (stdIOFile)) {
        return PGPERR_FILE_OPFAIL;
    }
    return PGPERR_OK;
}


    PgpFile *
pgpFileRefReadOpen(
    PGPFileRef const *  fileRef,
    PgpUICb const *     ui,
    void *              ui_arg,
    PGPError *          errorCode)
{
    pgpAssert(ui == NULL && ui_arg == NULL);
    (void)ui;    /* Avoid warnings */
    (void)ui_arg;

    return pgpFileRefOpen(fileRef, kPGPFileOpenReadPerm, kPGPFileTypeNone,
                          errorCode);
}

    PgpFile *
pgpFileRefWriteOpen(
    PGPFileRef const *  fileRef,
    PGPFileType         fileType,
    PgpCfbContext *     cfb,
    PGPError *          errorCode)
{
    pgpAssert(cfb == NULL);
    (void)cfb;    /* Avoid warning */

    return pgpFileRefOpen(fileRef, kPGPFileOpenStdWriteFlags, fileType,
                          errorCode);
}

    PgpFile *
pgpFileRefProcWriteOpen(
    PGPFileRef const *  fileRef,
    PGPFileType         fileType,
    int                 (*doClose)  (FILE *file, void *arg),
    void *              arg,
    PGPError *          errorCode)
{
    FILE *      stdIOFile;
    PgpFile *   pgpFile;

    if ((stdIOFile = pgpFileRefStdIOOpen(fileRef, kPGPFileOpenStdWriteFlags,
                                         fileType, errorCode)) == NULL)
        return NULL;

    if ((pgpFile = pgpFileProcWriteOpen(stdIOFile, doClose, arg)) == NULL)
    {
        fclose(stdIOFile);
        if (errorCode != NULL)
        {
            pgpAssertAddrValid(errorCode, PGPError);
            *errorCode = PGPERR_NO_FILE;
        }
        return NULL;
    }

    return pgpFile;
}

/*
 * Local Variables:
 * tab-width: 4
 * End:
 * vi: ts=4 sw=4
 * vim: si
 */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -