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

📄 pgpx509cert_util.c

📁 vc环境下的pgp源码
💻 C
📖 第 1 页 / 共 5 页
字号:

    PGPASN_TRACE_PRINT_FM(tag, 0x05, "NULL");

    (void)ctx; 

    if (erret == NULL) return 0;    /* can't report errors */
    if (nullstruct == NULL) {
        PGPASN_ERR(kPGPASNError_ErrUnpackNoStructure);
        return 0;
    }
    if (buf == NULL) {
        PGPASN_ERR(kPGPASNError_ErrUnpackNoBlockPtr);
        return 0;
    }
    if (buflen <= 0) return 0; /* no error -- no block */

    if (*buf != tag)
        return 0;    /* no error, no block */
    bytesused = 1;

    bytesused += PGPASN_GetLength(buf+bytesused, &datasize);
    if (datasize != 0) {
        PGPASN_ERR(kPGPASNError_ErrUnpackNullLth);
        return 0;
    }

    if (bytesused > buflen) {
        PGPASN_ERR(kPGPASNError_ErrUnpackOverrun); /* note this error */
        return 0;
    }

    nullstruct->len = 0;
    return bytesused;
} /* pgpasn_UnpkInPlaceNULL */

size_t pgpasn_UnpackNULL(
    PGPASN_CONTEXT *ctx,
    PGPASN_NULL **nullstruct,
    const unsigned char *buf,
    size_t buflen, 
    int        *erret)  /* error return */
{
    return(pgpasn_UnpackNULLInternal(ctx, nullstruct, buf, buflen, PGPASN_ID_NULL, erret));
} /* pgpasn_UnpackNULL */

size_t pgpasn_UnpackNULLInternal(
    PGPASN_CONTEXT *ctx,
    PGPASN_NULL **nullstruct,
    const unsigned char *buf,
    size_t buflen,
    unsigned char tag,
    int        *erret)  /* error return */
{
    size_t bytesused;
    PGPASN_NULL *local = NULL;

    if (erret == NULL)
        return 0;
    *erret = 0;

    if (ctx == NULL) {
        PGPASN_ERR(kPGPASNError_ErrBadContext);
        return 0;
    }

    if (nullstruct == NULL) {
        PGPASN_ERR(kPGPASNError_ErrUnpackNoStructure);
        return 0;
    }
    *nullstruct = NULL;

    if (buflen <= 0) return 0;

    if (*buf != tag) return 0;

    local = pgpasn_NewNULL(ctx);        /* carve a block for it */
    bytesused = pgpasn_UnpkInPlaceNULL(ctx, local, buf, buflen, tag, erret);
    if (*erret != 0) {
        if (local != NULL) pgpasn_FreeNULL(ctx, local);
        return 0;
    }

    *nullstruct = local;
    return bytesused;
} /* pgpasn_UnpackNULLInternal */

/************************************************************************
* PGPASN_BOOLEAN routines
*************************************************************************/

PGPASN_BOOLEAN *pgpasn_NewBOOLEAN(
    PGPASN_CONTEXT *ctx)
{
    PGPASN_BOOLEAN *f = NULL;

    if (ctx == NULL)
        return NULL;

    f = (PGPASN_BOOLEAN *)PGPASN_Alloc(ctx->memMgr, sizeof(PGPASN_BOOLEAN) );
    if (f != NULL)
        memset(f, 0, sizeof(PGPASN_BOOLEAN) );

    return ((PGPASN_BOOLEAN *) f);
} /* NewBOOLEAN */

void pgpasn_DropInPlaceBOOLEAN(
    PGPASN_CONTEXT *ctx,
    PGPASN_BOOLEAN *f)
{
    (void)ctx;
    (void)f;
    return ;
} /* pgpasn_DropInPlaceBOOLEAN */

void pgpasn_FreeBOOLEAN(
    PGPASN_CONTEXT *ctx,
    PGPASN_BOOLEAN *g )
{
    if (ctx == NULL)
        return;
    if (g != NULL)
        PGPASN_Free(ctx->memMgr, g);
} /* pgpasn_FreeBOOLEAN */

size_t pgpasn_SizeofBOOLEAN(
    PGPASN_CONTEXT *ctx,
    PGPASN_BOOLEAN *boolblock,
    int outerSizeFlag)
{
    (void)ctx; /* for future use */

    return pgpasn_SizeofBOOLEANInternal(boolblock, outerSizeFlag, PGPASN_FALSE);
}

size_t pgpasn_SizeofBOOLEANInternal(
    PGPASN_BOOLEAN *boolblock,
    int outerSizeFlag,
    int expTaggedSize)
{
    size_t length = 1;

    if (boolblock == NULL)
        return 0;

    if (outerSizeFlag == PGPASN_TRUE)
        length = 3;

    if (expTaggedSize == PGPASN_TRUE)
        length = PGPASN_Tagged(length, 0);

    return length;

} /* pgpasn_SizeofBOOLEANInternal */

size_t pgpasn_PackBOOLEAN(
    PGPASN_CONTEXT *ctx,
    unsigned char *buf,
    size_t buflen,
    PGPASN_BOOLEAN *boolblock,
    int *erret )
{
    return(pgpasn_PackBOOLEANInternal(ctx, buf, buflen, boolblock,  PGPASN_ID_BOOLEAN, erret));
} /* pgpasn_PackBOOLEAN */

size_t pgpasn_PackBOOLEANInternal(
    PGPASN_CONTEXT *ctx,
    unsigned char *buf,
    size_t buflen,
    PGPASN_BOOLEAN *boolblock,
    unsigned char tag,
    int *erret )
{
    unsigned char value = 0x00;
    size_t bytesused;

    (void)ctx; /* for future use */

    if (boolblock == NULL) return 0;

    if (boolblock->val != 0) value = 0xff;

    bytesused = PGPASN_PutByte(buf, tag);    /* this is a PGPASN_BOOLEAN */
    bytesused += PGPASN_PutByte(buf+bytesused, 1);    /* length = 1 byte */
    bytesused += PGPASN_PutByte(buf+bytesused, value); /* the value */

    if (bytesused > buflen) {
        PGPASN_ERR(kPGPASNError_ErrPackOverrun); /* note this error */
        return 0;
    }

    return bytesused;
} /* pgpasn_PackBOOLEANInternal */

size_t pgpasn_UnpkInPlaceBOOLEAN(
    PGPASN_CONTEXT *ctx,
    PGPASN_BOOLEAN *boolstruct,    /* output block */
    const unsigned char *buf,        /* loc of input pointer */
    size_t buflen,        /* max end of my region */
    unsigned char tag,
    int *erret)            /* error return location */
{
    size_t bytesused;
    size_t datasize;

    (void)ctx; 

    PGPASN_TRACE_PRINT_FM(tag, 0x01, "BOOLEAN");

    if (erret == NULL) return 0;    /* can't report errors */
    if (boolstruct == NULL) {
        PGPASN_ERR(kPGPASNError_ErrUnpackNoStructure);
        return 0;
    }
    if (buf == NULL) {
        PGPASN_ERR(kPGPASNError_ErrUnpackNoBlockPtr);
        return 0;
    }
    if (buflen <= 0) return 0; /* no error -- no block */

    if (*buf != tag) return 0;
    bytesused = 1;

    bytesused += PGPASN_GetLength(buf+bytesused, &datasize);
    if (datasize != 1) {
        PGPASN_ERR( kPGPASNError_ErrUnpackBooleanLth);
        return 0;
    }

    if (bytesused+datasize > buflen) {
        PGPASN_ERR(kPGPASNError_ErrUnpackOverrun); /* note this error */
        return 0;
    }

    PGPASN_TRACE_PRINT_DATA(buf+bytesused,1);

    bytesused += PGPASN_GetByte(buf+bytesused, (unsigned char *)&(boolstruct->val));
    if (boolstruct->val != 0) boolstruct->val = PGPASN_TRUE;

    return bytesused;
} /* pgpasn_UnpkInPlaceBOOLEAN */

size_t pgpasn_UnpackBOOLEAN(
    PGPASN_CONTEXT *ctx,
    PGPASN_BOOLEAN **boolstruct,
    const unsigned char *buf,
    size_t buflen,
    int        *erret)  /* error return */
{
    return(pgpasn_UnpackBOOLEANInternal(ctx, boolstruct, buf, buflen,
                               PGPASN_ID_BOOLEAN, erret));
}

size_t pgpasn_UnpackBOOLEANInternal(
    PGPASN_CONTEXT *ctx,
    PGPASN_BOOLEAN **boolstruct,
    const unsigned char *buf,
    size_t buflen,
    unsigned char tag,
    int        *erret)  /* error return */
{
    size_t bytesused;
    PGPASN_BOOLEAN *local = NULL;

    if (erret == NULL)
        return 0;
    *erret = 0;

    if (ctx == NULL) {
        PGPASN_ERR(kPGPASNError_ErrBadContext);
        return 0;
    }

    if (boolstruct == NULL) {
        PGPASN_ERR(kPGPASNError_ErrUnpackNoStructure);
        return 0;
    }
    *boolstruct = NULL;

    if (buflen <= 0) return 0;

    if (*buf != tag) return 0;

    local = pgpasn_NewBOOLEAN(ctx);    /* carve a block for it */
    bytesused = pgpasn_UnpkInPlaceBOOLEAN(ctx, local, buf, buflen, tag, erret);
    if (*erret != 0) {
        if (local != NULL) pgpasn_FreeBOOLEAN(ctx, local);
        return 0;
    }

    *boolstruct = local;
    return bytesused;
} /* pgpasn_UnpackBOOLEANInternal */

/************************************************************************
* Routines for PGPASN_BIT_STRING
*************************************************************************/

PGPASN_BIT_STRING *pgpasn_NewBIT_STRING(
    PGPASN_CONTEXT *ctx)
{
    PGPASN_BIT_STRING *f = NULL ;

    if (ctx == NULL)
        return NULL;

    f = (PGPASN_BIT_STRING *)PGPASN_Alloc(ctx->memMgr, sizeof(PGPASN_BIT_STRING) );
    if (f != NULL)
        memset( f, 0, sizeof(PGPASN_BIT_STRING) );

      return ((PGPASN_BIT_STRING *) f);
} /* pgpasn_NewBIT_STRING */

void pgpasn_DropInPlaceBIT_STRING(
    PGPASN_CONTEXT *ctx,
    PGPASN_BIT_STRING *f )
{
    if (ctx == NULL)
        return;

    if (f != NULL) {
        if ((f->val) != NULL) {
            PGPASN_Free(ctx->memMgr, f->val);
        }
        f->val = NULL ;
    }
    return ;
} /* pgpasn_DropInPlaceBIT_STRING */

void pgpasn_FreeBIT_STRING(
    PGPASN_CONTEXT *ctx,
    PGPASN_BIT_STRING *f )
{
    if (ctx == NULL)
        return;

    pgpasn_DropInPlaceBIT_STRING(ctx, f);
    if (f != NULL)
        PGPASN_Free(ctx->memMgr, f);
} /* pgpasn_FreeBIT_STRING */

size_t pgpasn_SizeofBIT_STRING(
    PGPASN_CONTEXT *ctx,
    PGPASN_BIT_STRING *bitblock,
    int outerSizeFlag)
{
    (void)ctx; /* for future use */

    return pgpasn_SizeofBIT_STRINGInternal(bitblock, outerSizeFlag, PGPASN_FALSE);
}

size_t pgpasn_SizeofBIT_STRINGInternal(
    PGPASN_BIT_STRING *bitblock,
    int outerSizeFlag,
    int expTaggedSize)
{
    size_t body_size;

    if (bitblock == NULL)
        return (0);

    body_size = 1 + bitblock->len;

    if (outerSizeFlag == PGPASN_TRUE)
        body_size = PGPASN_Tagged(body_size, 0);

    if (expTaggedSize == PGPASN_TRUE)
        body_size = PGPASN_Tagged(body_size, 0);

    return body_size;

} /* pgpasn_SizeofBIT_STRINGInternal */

size_t pgpasn_PackBIT_STRING(
    PGPASN_CONTEXT *ctx,
    unsigned char *buf,
    size_t buflen,
    PGPASN_BIT_STRING *bitblock,
    int *erret)
{
    return (PackVariableBlock(ctx, PGPASN_ID_BIT_STRING, PGPASN_TRUE, 
                 (unsigned char)bitblock->nuub,
                 buf, buflen, (PGPASN_VariableBlock *)bitblock, erret));
}

size_t pgpasn_PackBIT_STRINGInternal(
    PGPASN_CONTEXT *ctx,
    unsigned char *buf,
    size_t buflen,
    PGPASN_BIT_STRING *bitblock,
    unsigned char tag,
    int *erret)
{

    return (PackVariableBlock(ctx, tag, PGPASN_TRUE, 
                   (unsigned char)bitblock->nuub,
               buf, buflen, (PGPASN_VariableBlock *)bitblock, erret));

} /* pgpasn_PackBIT_STRINGInternal */


static size_t uipBitSegments(
    PGPASN_CONTEXT *ctx,
    int indefFlag,              /* is this an indefinite length block or not? */
    unsigned char exptag,    /* my expected block tag */
    PGPASN_BIT_STRING *bitblock,    /* output block */
    const unsigned char *buf,  /* loc of input pointer */
    size_t buflen,        /* max end of my region */
    int *erret)            /* error return location */
{
    size_t bytesused = 0;
    size_t datasize;

    /* validity of erret and ctx checked in UnpkInPlaceBIT_STRING */

    if (buf == NULL) {
        PGPASN_ERR(kPGPASNError_ErrUnpackNoBlockPtr);
        return 0;
    }

    /* validity of bitblock checked in UnpkInPlaceBIT_STRING */
    bitblock->val = NULL;
    bitblock->len = 0;
    bitblock->nuub = 0;

    /* while there are segments */
    while (1) {

        /* For indef length end-of-contents is 0x00 0x00 and we
           have to eat those two bytes, otherwise we see if we've
           used up all the available data yet */
        if ( indefFlag == 1 &&
             (*(buf+bytesused) == 0x00 && *(buf+bytesused+1) == 0x00) ) {
            bytesused += 2;
            break;
        }
        else if (indefFlag == 0 && bytesused == buflen)
            break;

        if (*buf != exptag) {
            PGPASN_ERR(kPGPASNError_ErrUnpackInvalidEncoding);
            return 0;
        }
        bytesused++; /* skip the tag byte */

        datasize = 0;
        bytesused += PGPASN_GetLength(buf+bytesused, &datasize);

        if ((int)datasize == -1) { /* no nested indef lengths for bit string types */
            PGPASN_ERR(kPGPASNError_ErrUnpackInvalidEncoding);
            return 0;
        }

        if (bytesused + datasize > buflen) {
            PGPASN_ERR(kPGPASNError_ErrUnpackOverrun); /* note this error */
            return 0;
        }

⌨️ 快捷键说明

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