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

📄 pgpmacfile.c

📁 著名的加密软件的应用于电子邮件中
💻 C
📖 第 1 页 / 共 3 页
字号:

    static int
macFileSeek(
    PgpFile *     file,
    long          offset,
    int           whence)
{
    MacFile *     mf = (MacFile *)file->priv;

    switch (whence)
    {
        case SEEK_SET:
            /* offset is correct, as is */
            break;
        case SEEK_CUR:
            offset += mf->filePos;
            break;
        case SEEK_END:
            offset += mf->totalSize;
            break;
    }

    if (offset < 0 || offset > mf->totalSize)
    {
        macSetError(file, PGPERR_FILE_OPFAIL);
        return PGPERR_FILE_OPFAIL;
    }

    mf->filePos = offset;
    return PGPERR_OK;
}

    static int
macFileEof(
    PgpFile const * file)
{
    MacFile *       mf = (MacFile *)file->priv;

    /*
     * XXX This will always return TRUE when writing files.
     *     Is that semantically correct?
     */
    return mf->filePos >= mf->totalSize;
}

    static long
macFileSizeAdvise(
    PgpFile const * file)
{
    MacFile *       mf = (MacFile *)file->priv;

    return mf->totalSize;
}

    static PgpFileError const *
macFileError(
    PgpFile const *     file)
{
    MacFile *           mf = (MacFile *)file->priv;

    if (mf->error != PGPERR_OK)
        return &mf->err;
    else
        return NULL;
}

    static void
macFileClearError(
    PgpFile *     file)
{
    MacFile *     mf = (MacFile *)file->priv;

    mf->error = PGPERR_OK;
}

    static int
macFileWrite2Read(
    PgpFile *     file)
{
    macSetError(file, PGPERR_FILE_BADOP);
    return PGPERR_FILE_BADOP;
}

    static PgpCfbContext *
macFileCfb(
    PgpFile const * file)
{
    (void)file; /* Quiet compiler warning */
    /* XXX: I'm not really sure what should be done here */
    return NULL;
}

#ifndef __powerc

static ushort asm CalcCRC16Contin(ushort start, void *data, long len)
{
    fralloc +
    move.l  D3, -(SP)
    move.w  #0x1021, D3
    clr.l   D0
    move.w  start, D0
    move.l  data, A0
    move.l  len, D2
    bra     Loop1
Loop0:
    move.b  (A0)+, D1
    lsl.w   #8, D1
    eor.w   D1, D0
    moveq   #7, D1
ShiftLoop0:
    lsl.w   #1, D0
    bcc     ShiftLoop1
    eor.w   D3, D0
ShiftLoop1:
    dbra    D1, ShiftLoop0
Loop1:
    dbra    D2, Loop0
    move.l  (SP)+, D3
    frfree
#if GENERATINGCFM || defined(__CFM68K__)
    rtd     #12
#else
    rts
#endif
}

static uchar asm CalcChecksum8Contin(uchar start, void *data, long len)
{
    FRALLOC +
    CLR.L   D0
    MOVE.B  start, D0
    MOVE.L  data, A0
    MOVE.L  len, D1
    BRA     Loop1
Loop0:
    ADD.B   (A0)+, D0
Loop1:
    DBRA    D1, Loop0
    FRFREE
#if GENERATINGCFM || defined(__CFM68K__)
    rtd     #12
#else
    rts
#endif
}

static ulong asm CalcCRC32(void *data, long len)
{
    fralloc +
    move.l  data, A0
    clr.l   D0
    move.l  len, D1
    lsr.l   #2, D1
    bra     Loop1
Loop0:
    rol.l   #1, D0
    add.l   (A0)+, D0
Loop1:
    dbra    D1, Loop0
    moveq   #3, D2
    move.l  len, D1
    and.l   D2, D1
    lsl.w   D2, D1
    clr.l   D2
    not.l   D2
    lsr.l   D1, D2
    not.l   D2
    and.l   (A0), D2
    rol.l   #1, D0
    add.l   D2, D0
    frfree
#if GENERATINGCFM || defined(__CFM68K__)
    rtd     #8
#else
    rts
#endif
}

#else

static ushort CalcCRC16Contin(ushort crc, void *data, long len)
{
    char *dp=(char *)data;
    short i;

    while(len--)
    {
        crc ^= (ushort)(*dp++) << 8;
        for (i = 0; i < 8; ++i)
        {
            if (crc & 0x8000)
                crc = (crc << 1) ^ 0x1021;
            else
                crc <<= 1;
        }
    }
    return(crc);
}

static uchar CalcChecksum8Contin(uchar start, void *data, long len)
{
    char *dp=(char *)data;

    while(len--)
        start+=*dp++;
    return start;
}

static ulong CalcCRC32(void *data, register long len)
{
    register ulong *dp=(ulong *)data;
    register ulong crc=0, x;
    register long len2;
    ulong highSet;

    len2=len;
    len>>=2;
    while(--len>=0)
    {
        highSet=(crc & 0x80000000);
        crc<<=1;
        if(highSet)
            crc|=1;
        crc+=*dp++;
    }
    x=3;
    len2 &=x;
    len2<<=x;
    x=0;
    x=~x;
    x>>=len2;
    x=~x;
    x &= *dp;
    highSet=(crc & 0x80000000);
    crc<<=1;
    if(highSet)
        crc|=1;
    crc+=x;
    return crc;
}

#endif

static uchar CalcChecksum8(void *data, long len)
{
    return CalcChecksum8Contin(0, data, len);
}

static ushort CalcCRC16(void *data, long len)
{
    return CalcCRC16Contin(0, data, len);
}

/*
 * This routine is called each time before writing to an actual fork. If
 * the kPGPCheckMacBin flag is set, it determines whether or not it's a
 * valid MacBinary header. If so, it creates the file and opens both the
 * data and resource forks. Otherwise it creates and opens the data fork,
 * and writes false header information to the data fork, using whatever
 * hints it can find to create the file with the correct type and creator.
 */
    static PGPError
PrepareToWrite(
    PgpFile *       file)
{
    MacFile *       mf = (MacFile *)file->priv;
    FSSpec          spec;
    HParamBlockRec  hio;
    PGPError        result = PGPERR_OK;
    OSErr           macResult;

    if (!(mf->flags & kPGPMacWrite))
        return PGPERR_OK;

    if (mf->flags & kPGPCheckMacBin)
    {
        if ((result = pgpFSSpecFromFileRef(mf->fileRef, &spec)) != PGPERR_OK)
            goto error;
        mf->flags &= ~(kPGPMacBinMode | kPGPCheckMacBin);
        if (mf->totalSize >= 126 && mf->macBinHeader.oldVersion == 0 &&
            mf->macBinHeader.minimumVersion <= 129 &&
            (mf->macBinHeader.info1.fdFlags & 0xFF) == 0 &&
            mf->macBinHeader.zero1 == 0 &&
            mf->macBinHeader.name[0] < 64)
        {
            ushort    crc;

            pgpAssert(sizeof(crc) == 2);
            pgpCopyMemory(&mf->macBinHeader.crc1, &crc, sizeof(crc));
            if (crc == CalcCRC16((uchar *)&mf->macBinHeader + 1, 124)
                || ((mf->flags & kPGPNoMacBinCRCOkay) && crc == 0))
                mf->flags |= kPGPMacBinMode;
        }
    }

    if (mf->dataRef != 0)
        return PGPERR_OK;

    if ((result = pgpFSSpecFromFileRef(mf->fileRef, &spec)) != PGPERR_OK)
        goto error;

    if (mf->flags & kPGPMacBinMode)
    {
        /* XXX Reconsider using a different file type for this */
        if ((result = pgpCreateFile(mf->fileRef, kPGPFileTypeDecryptedBin))
            != PGPERR_OK)
            goto error;

        hio.fileParam.ioNamePtr = spec.name;
        hio.fileParam.ioVRefNum = spec.vRefNum;
        hio.fileParam.ioDirID = spec.parID;
        hio.ioParam.ioPermssn = fsWrPerm;
        if ((macResult = PBHOpenDFSync(&hio)) != noErr)
            goto macError;
        mf->dataRef = hio.ioParam.ioRefNum;

        hio.fileParam.ioNamePtr = spec.name;
        hio.fileParam.ioVRefNum = spec.vRefNum;
        hio.fileParam.ioDirID = spec.parID;
        hio.ioParam.ioPermssn = fsWrPerm;
        if ((macResult = PBHOpenRFSync(&hio)) != noErr)
            goto macError;
        mf->resRef = hio.ioParam.ioRefNum;

        mf->dataOffset = 128;
        mf->resOffset = mf->dataOffset +
            (mf->macBinHeader.dLength + 127) & ~127L;
        mf->totalSize = mf->resOffset +
            (mf->macBinHeader.rLength + 127) & ~127L;
    }
    else
    {
        Boolean   created = FALSE;
        int       i;

        for (i = 0; smartBinRecs[i].ident != NULL; i++)
            if (pgpMemoryEqual((uchar *)&mf->macBinHeader + 1,
                               smartBinRecs[i].ident,
                               smartBinRecs[i].length))
            {
                if ((macResult = HCreate(spec.vRefNum, spec.parID,
                                         spec.name, smartBinRecs[i].creator,
                                         smartBinRecs[i].type)) != noErr)
                    goto macError;
                created = TRUE;
                break;
            }
        if (!created)
            if ((result = pgpCreateFile(mf->fileRef, mf->fileType))
                != PGPERR_OK)
                goto error;

        hio.fileParam.ioNamePtr = spec.name;
        hio.fileParam.ioVRefNum = spec.vRefNum;
        hio.fileParam.ioDirID = spec.parID;
        hio.ioParam.ioPermssn = fsWrPerm;
        if ((macResult = PBHOpenDFSync(&hio)) != noErr)
            goto macError;
        mf->dataRef = hio.ioParam.ioRefNum;

        mf->dataOffset = mf->resOffset = 0;

        {
            IOParam   io;

            io.ioRefNum = mf->dataRef;
            io.ioBuffer = (Ptr)&mf->macBinHeader + 1;
            io.ioPosMode = fsFromStart;
            io.ioPosOffset = 0;
            io.ioReqCount = mf->totalSize;
            pgpAssert(mf->totalSize <= 128);
            if ((macResult = PBWriteSync((ParmBlkPtr)&io)) != noErr)
                goto macError;
        }
    }

⌨️ 快捷键说明

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