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

📄 base64impl.c

📁 IBE是一种非对称密码技术
💻 C
📖 第 1 页 / 共 2 页
字号:
  /* "Empty" the unprocessedData buffer.
   */
  encodeCtx->unprocessedDataLen = 0;

  /* Setting the block sizes to 1 means we'll do all the blocking
   * inside the implementation.
   */
  encodeCtx->codedBlockSize = 1;
  encodeCtx->plainBlockSize = 1;
  base64Ctx->converterLen = 0;

  return (0);
}

int Base64DecodeUpdate (
   VoltAlgorithmObject *obj,
   VtRandomObject random,
   unsigned char *dataToDecode,
   unsigned int dataToDecodeLen,
   unsigned char *decoding,
   unsigned int *decodingLen
   )
{
  int status;
  unsigned int index, eqCount;
  UInt32 currentChar;
  VoltEncodeClassCtx *encodeCtx = (VoltEncodeClassCtx *)(obj->classCtx);
  VoltBase64Ctx *base64Ctx = (VoltBase64Ctx *)(encodeCtx->localEncodeCtx);
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  status = 0;
  do
  {
    /* As long as we have bytes, decode blocks of 4.
     */
    index = base64Ctx->converterLen;
    eqCount = 1;
    if ( (index != 3) || (base64Ctx->converter[2] != EEEE) )
      eqCount = 0;

    *decodingLen = 0;
    while (dataToDecodeLen > 0)
    {
      currentChar = (UInt32)(base64DecodeChars[(*dataToDecode)]);
      dataToDecodeLen--;
      dataToDecode++;

      /* If the char is special, we may want to ignore it.
       */
      if (currentChar >= EEEE)
      {
        /* Ignore new lines.
         */
        if (currentChar == NNNN)
          continue;

        if (currentChar == EEEE)
        {
          /* It's an "=". We don't put "=" into the first or second position.
           */
          if ( (index == 0) || (index == 1) )
          {
            VOLT_SET_FNCT_LINE (fnctLine)
            status = VT_ERROR_INVALID_ENCODING;
            if (base64Ctx->info.errorCheck == VT_BASE64_ERROR_CHECK)
              break;
            status = 0;
            continue;
          }
          /* This is possibly padding, load it into the converter,
           * indicate we have an "=".
           */
          eqCount++;
        }
        else
        {
          /* It's invalid, error or ignore.
           */
          VOLT_SET_FNCT_LINE (fnctLine)
          status = VT_ERROR_INVALID_ENCODING;
          if (base64Ctx->info.errorCheck == VT_BASE64_ERROR_CHECK)
            break;
          status = 0;
          continue;
        }
      }

      /* Load this char into the converter and increment index.
       */
      base64Ctx->converter[index] = currentChar;
      index++;

      /* If we have 4 bytes, decode them.
       */
      while (index >= 4)
      {
        /* If there are any "=", we want to either delete them or convert
         * them to 0.
         */
        if (eqCount == 1)
        {
          /* Only 1 "=", if it's not the last, it's invalid, skip it.
           */
          if (base64Ctx->converter[2] == EEEE)
          {
            VOLT_SET_FNCT_LINE (fnctLine)
            status = VT_ERROR_INVALID_ENCODING;
            if (base64Ctx->info.errorCheck == VT_BASE64_ERROR_CHECK)
              break;
            status = 0;
            base64Ctx->converter[2] = base64Ctx->converter[3];
            index = 3;
            eqCount = 0;
            break;
          }
          base64Ctx->converter[3] = 0;
        }
        else if (eqCount == 2)
        {
          base64Ctx->converter[2] = 0;
          base64Ctx->converter[3] = 0;
        }

        /* Convert the 4 chars into 3.
         */
        base64Ctx->converter[0] <<= 18;
        base64Ctx->converter[1] <<= 12;
        base64Ctx->converter[2] <<= 6;
        base64Ctx->converter[3] |=
          (base64Ctx->converter[0] | base64Ctx->converter[1] |
          base64Ctx->converter[2]);

        *decoding = (unsigned char)(base64Ctx->converter[3] >> 16);
        decoding++;
        if (eqCount < 2)
        {
          *decoding = (unsigned char)(base64Ctx->converter[3] >> 8);
          decoding++;
          if (eqCount < 1)
          {
            *decoding = (unsigned char)base64Ctx->converter[3];
            decoding++;
          }
        }

        (*decodingLen) += (3 - eqCount);
        index = 0;
        eqCount = 0;
        base64Ctx->converterLen = 0;
      }
      if (status != 0)
        break;
    }

    if (status == 0)
      base64Ctx->converterLen = index;
  } while (0);

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

  return (status);
}

int Base64DecodeFinal (
   VoltAlgorithmObject *obj,
   VtRandomObject random,
   unsigned char *decoding,
   unsigned int *decodingLen
   )
{
  int status;
  unsigned int index, retCount, shiftCount;
  VoltEncodeClassCtx *encodeCtx = (VoltEncodeClassCtx *)(obj->classCtx);
  VoltBase64Ctx *base64Ctx = (VoltBase64Ctx *)(encodeCtx->localEncodeCtx);
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  *decodingLen = 0;

  do
  {
    /* If there are no values left in the converter, we're done.
     */
    status = 0;
    if (base64Ctx->converterLen == 0)
      break;

    /* If there are any values left in the converter, deal with them.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_ENCODING;
    if (base64Ctx->info.errorCheck == VT_BASE64_ERROR_CHECK)
      break;

    /* Pad with 0's.
     */
    for (index = base64Ctx->converterLen; index < 4; ++index)
      base64Ctx->converter[index] = 0;

    retCount = base64Ctx->converterLen;
    if (base64Ctx->converter[2] == EEEE)
    {
      base64Ctx->converter[2] = 0;
      retCount--;
    }

    /* Convert the 4 chars into 3.
     */
    base64Ctx->converter[0] <<= 18;
    base64Ctx->converter[1] <<= 12;
    base64Ctx->converter[2] <<= 6;
    base64Ctx->converter[3] |=
      (base64Ctx->converter[0] | base64Ctx->converter[1] |
      base64Ctx->converter[2]);

    shiftCount = 16;
    for (index = 0; index < retCount; ++index)
    {
      decoding[index] = (unsigned char)(base64Ctx->converter[3] >> shiftCount);
      (*decodingLen)++;
      shiftCount -= 8;
    }

    base64Ctx->converterLen = 0;
    status = 0;
  } while (0);

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

  return (status);
}

void Base64CtxDestroy (
   Pointer obj,
   Pointer ctx
   )
{
  VoltObject *voltObj = (VoltObject *)obj;
  VoltLibCtx *libCtx = (VoltLibCtx *)(voltObj->libraryCtx);
  VoltEncodeClassCtx *classCtx = (VoltEncodeClassCtx *)ctx;
  VoltBase64Ctx *base64Ctx;

  if ( (obj == (Pointer)0) || (ctx == (Pointer)0) )
    return;

  libCtx = (VoltLibCtx *)(voltObj->libraryCtx);
  base64Ctx = (VoltBase64Ctx *)(classCtx->localEncodeCtx);

  Z2Free (ctx);
}

static void DoEncodeUpdate (
   unsigned char *input,
   int blockSize,
   unsigned int newLineChar,
   unsigned char *output,
   unsigned int *outputLen
   )
{
  UInt32 value;
  unsigned int char0, char1, char2, char3;

  *outputLen = (unsigned int)((blockSize / 3) * 4);

  /* Keep grabbing blocks of 3 bytes until running out of data.
   */
  while (blockSize > 0)
  {
    value = (UInt32)(input[0]) << 16;
    value += ((UInt32)(input[1]) << 8);
    value += (UInt32)(input[2]);
    char0 = (unsigned int)((value & 0x00fc0000) >> 18);
    char1 = (unsigned int)((value & 0x0003f000) >> 12);
    char2 = (unsigned int)((value & 0x00000fc0) >> 6);
    char3 = (unsigned int) (value & 0x0000003f);
    *output = base64EncodeChars[char0];
    output++;
    *output = base64EncodeChars[char1];
    output++;
    *output = base64EncodeChars[char2];
    output++;
    *output = base64EncodeChars[char3];
    output++;
    blockSize -= 3;
    input += 3;
  }

  /* If there's no new line character, we're done.
   */
  if (newLineChar == VT_BASE64_NO_NEW_LINE)
    return;

  /* Place the new line.
   */
  if (newLineChar == VT_BASE64_NEW_LINE_LF)
  {
    *output = 0x0a;
    (*outputLen)++;
  }
  else
  {
    *output = 0x0d;
    *(output + 1) = 0x0a;
    (*outputLen) += 2;
  }
}

⌨️ 快捷键说明

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