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

📄 base64type.c

📁 IBE是一种非对称密码技术
💻 C
字号:
/* Copyright 2003-2006, Voltage Security, all rights reserved.
 */
#include "vibecrypto.h"
#include "environment.h"
#include "base.h"
#include "libctx.h"
#include "algobj.h"
#include "encode.h"
#include "base64.h"
#include "errorctx.h"

/* This routine does the work. It allocates and fills in the contexts.
 *
 * @param obj The algorithm object to set.
 * @return an int, 0 if the function completed successfully or a
 * non-zero error code.
 */
static int VOLT_CALLING_CONV SetObjectBase64 VOLT_PROTO_LIST ((
   VoltAlgorithmObject *obj,
   VtBase64Info *info
));

int VtAlgorithmImplBase64 (
   VtAlgorithmObject *object,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  VoltAlgorithmObject *obj = (VoltAlgorithmObject *)(*object);
  VtBase64Info *inputInfo = (VtBase64Info *)info;
  VtBase64Info newInfo;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Check the flag, it should be VOLT_ALG_SET_TYPE_FLAG.
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_TYPE;
    if (flag != VOLT_ALG_SET_TYPE_FLAG)
      break;

    /* The associated info should be a VtBase64Info struct or else
     * a NULL pointer.
     */
    if (info == (Pointer)0)
    {
      newInfo.base64BlockSize = VOLT_BASE64_DEFAULT_BLOCK_SIZE;
      newInfo.newLineCharacter = VT_BASE64_NEW_LINE_LF;
      newInfo.errorCheck = VT_BASE64_ERROR_CHECK;
    }
    else
    {
      /* The block size must be a multiple of 4 between 56 and 76.
       */
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_INVALID_ASSOCIATED_INFO;
      if ( (inputInfo->base64BlockSize < 56) ||
           (inputInfo->base64BlockSize > 76) )
        break;
      VOLT_SET_FNCT_LINE (fnctLine)
      if ((inputInfo->base64BlockSize & 3) != 0)
        break;

      /* The newLineCharacter must be one of the three supported.
       */
      VOLT_SET_FNCT_LINE (fnctLine)
      if ( (inputInfo->newLineCharacter != VT_BASE64_NEW_LINE_LF) &&
           (inputInfo->newLineCharacter != VT_BASE64_NEW_LINE_CR_LF) &&
           (inputInfo->newLineCharacter != VT_BASE64_NO_NEW_LINE) )
        break;

      /* The errorCheck field must be error or no error.
       */
      VOLT_SET_FNCT_LINE (fnctLine)
      if ( (inputInfo->errorCheck != VT_BASE64_ERROR_CHECK) &&
           (inputInfo->errorCheck != VT_BASE64_NO_ERROR_CHECK) )
        break;

      newInfo = (*inputInfo);
    }

    /* Check the class of the object. It should be 0 (not yet set)
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_SET;
    if (obj->algClass != 0)
      break;

    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = SetObjectBase64 (obj, &newInfo);

  } while (0);

  VOLT_LOG_ERROR_COMPARE (
    status, obj->voltObject.libraryCtx, status, errorType, fnctLine,
    "VtAlgorithmImplBase64", (char *)0)

  return (status);
}

static int SetObjectBase64 (
   VoltAlgorithmObject *obj,
   VtBase64Info *info
   )
{
  int status;
#if VOLT_ALIGNMENT != 1
  unsigned int pad;
#endif
  unsigned int bufferSize, offset;
  unsigned char *buffer = (unsigned char *)0;
  VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
  VoltEncodeClassCtx *encodeCtx;
  VoltBase64Ctx *base64Ctx;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Allocate enough space for an EncodeCtx and a Base64Ctx and bytes
     * for the unprocessedData buffer.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    bufferSize =
      sizeof (VoltEncodeClassCtx) + sizeof (VoltBase64Ctx) +
      ((info->base64BlockSize / 4) * 3) + 1;
#if VOLT_ALIGNMENT != 1
    /* If the alignment is 1, there's no need to pad. If not, compute
     * the pad length.
     */
    VOLT_COMPUTE_ALIGN_PAD (VOLT_ALIGNMENT, sizeof (VoltEncodeClassCtx), pad)
    bufferSize += pad;
#endif
    buffer = (unsigned char *)Z2Malloc (bufferSize, VOLT_MEMORY_SENSITIVE);
    if (buffer == (unsigned char *)0)
      break;
    Z2Memset (buffer, 0, bufferSize);

    /* Locate the contexts.
     */
    encodeCtx = (VoltEncodeClassCtx *)buffer;
    offset = sizeof (VoltEncodeClassCtx);
#if VOLT_ALIGNMENT != 1
    offset += pad;
#endif
    base64Ctx = (VoltBase64Ctx *)(buffer + offset);
    offset += sizeof (VoltBase64Ctx);
    encodeCtx->unprocessedData = buffer + offset;
    encodeCtx->localEncodeCtx = (Pointer)base64Ctx;

    /* Fill in the fields.
     * We'll set the blockSize's at Init.
     */
    encodeCtx->GetEncodeOutputSize = Base64GetOutputSize;
    encodeCtx->EncodeInit = Base64EncodeInit;
    encodeCtx->EncodeUpdate = Base64EncodeUpdate;
    encodeCtx->EncodeFinal = Base64EncodeFinal;
    encodeCtx->DecodeInit = Base64DecodeInit;
    encodeCtx->DecodeUpdate = Base64DecodeUpdate;
    encodeCtx->DecodeFinal = Base64DecodeFinal;
    /* No localCtxDestroy, the ClassCtxDestroy will take care of it.
     */

    base64Ctx->info.base64BlockSize = info->base64BlockSize;
    base64Ctx->info.newLineCharacter = info->newLineCharacter;
    base64Ctx->info.errorCheck = info->errorCheck;
    if (info->newLineCharacter == VT_BASE64_NO_NEW_LINE)
      base64Ctx->newLineLen = 0;
    else if (info->newLineCharacter == VT_BASE64_NEW_LINE_LF)
      base64Ctx->newLineLen = 1;
    else
      base64Ctx->newLineLen = 2;

    obj->algClass = VOLT_CLASS_ENCODE;
    obj->classCtx = (Pointer)encodeCtx;
    obj->ClassCtxDestroy = Base64CtxDestroy;

    status = 0;

  } while (0);

  /* If everything works, return 0.
   */
  if (status == 0)
    return (0);

  /* If something went wrong free up what we allocated.
   */
  if (buffer != (unsigned char *)0)
    Z2Free (buffer);

  VOLT_LOG_ERROR (
    obj->voltObject.libraryCtx, status, VT_ERROR_TYPE_PRIMARY, fnctLine,
    "SetObjectBase64", (char *)0)

  return (status);
}

⌨️ 快捷键说明

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