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

📄 derhelp.c

📁 IBE是一种非对称密码技术
💻 C
📖 第 1 页 / 共 2 页
字号:
     */
    if ( (explicitTag != 0) && (derElement->explicitTag == 0) )
    {
      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VoltGetNextDerElement (
        libCtx, encoding, encodingLen, 0, explicitTag, 0, derElement,
        bytesRead);
      if (status != 0)
        break;

      /* If the call couldn't find the entire EXPLICIT, we're done for
       * now.
       */
      if (derElement->complete == 0)
        break;

      encoding += *bytesRead;
      encodingLen -= *bytesRead;

      /* The info is currently stored with the regular tag fields, move
       * it to the explicit fields, then clear the regular fields.
       */
      derElement->explicitTag = derElement->tag;
      derElement->explicitLen = derElement->valueLen;
      derElement->tag = 0;
      derElement->elementLen = 0;
      derElement->valueLen = 0;
      derElement->totalLen = 0;
      derElement->complete = 0;

      if (encodingLen == 0)
        break;
    }

    /* If we don't have the tag and len yet, get them.
     */
    if (derElement->tag == 0)
    {
      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = CollectTagAndLenRealloc (
        libCtx, encoding, encodingLen, &(derElement->element),
        &(derElement->elementSize), &(derElement->elementLen),
        &octetsRead, &complete);
      if (status != 0)
        break;

      *bytesRead += octetsRead;

      /* If it didn't finish, return.
       */
      if (complete == 0)
        break;

      VOLT_SET_FNCT_LINE (fnctLine)
      status = VoltDecodeTagAndLen (
        libCtx, derElement->element, derElement->elementLen,
        &(derElement->tag), &lengthLen, &lenLo, &lenHi, VOLT_LEN_LIMIT);
      if (status != 0)
        break;

      /* Expected tag?
       */
      VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_INVALID_ENCODING;
      if (derElement->tag != expectedTag)
        break;

#if VT_64_BIT_LENGTH == 64
      derElement->valueLen = ((VtUInt64)lenHi) << 32;
      derElement->valueLen += (VtUInt64)lenLo;
      derElement->totalLen = (VtUInt64)(lengthLen + 1);
#else
      derElement->valueLen = (unsigned int)lenLo;
      derElement->totalLen = lengthLen + 1;
#endif

      /* If the value is not requested, we're done.
       */
      status = 0;
      derElement->complete = 1;
      if (valueFlag == 0)
        break;

      encoding += octetsRead;
      encodingLen -= octetsRead;

      /* The caller wants the value, the totalLen needs to reflect that.
       */
      derElement->totalLen += derElement->valueLen;
      derElement->complete = 0;

#if VT_64_BIT_LENGTH == 64
      /* If this is 64-bit lengths, we still limit the number of bytes
       * we'll store internally.
       */
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_INVALID_INPUT_LENGTH;
      if (derElement->totalLen > (VtUInt64)0xffffffff)
        break;
      status = 0;
#endif

      if (encodingLen == 0)
        break;
    }

    /* How many bytes do we need to copy?
     */
    octetsRead =
      (unsigned int)(derElement->totalLen - derElement->elementLen);
    if (encodingLen >= octetsRead)
      derElement->complete = 1;
    else
      octetsRead = encodingLen;

    *bytesRead += octetsRead;
    status = StoreElementData (libCtx, derElement, encoding, octetsRead);

  } while (0);

  VOLT_LOG_ERROR_INFO_COMPARE (
    status, libCtx, 0, status, 0, errorType,
    (char *)0, "VoltGetNextDerElement", fnctLine, (char *)0)

  return (status);
}

void VoltResetDerElement (
   VoltDerElement *derElement
   )
{
  derElement->elementLen = 0;
  derElement->valueLen = 0;
  derElement->totalLen = 0;
  derElement->explicitLen = 0;
  derElement->explicitTag = 0;
  derElement->tag = 0;
  derElement->complete = 0;
}

static int StoreElementData (
   VoltLibCtx *libCtx,
   VoltDerElement *derElement,
   unsigned char *dataToStore,
   unsigned int dataToStoreLen
   )
{
  int status;
  unsigned int totalLen;
  unsigned char *buffer = (unsigned char *)0;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* We need a buffer big enough to handle the data in the buffer
     * along with the new data.
     */
    totalLen = derElement->elementLen + dataToStoreLen;
    if (derElement->elementSize < totalLen)
    {
      status = VT_ERROR_MEMORY;
      VOLT_SET_FNCT_LINE (fnctLine)
      buffer = (unsigned char *)Z2Malloc (totalLen, VOLT_MEMORY_SENSITIVE);
      if (buffer == (unsigned char *)0)
        break;

      /* Copy the old into the new and destroy the old.
       */
      if (derElement->element != (unsigned char *)0)
      {
        Z2Memcpy (buffer, derElement->element, derElement->elementLen);
        Z2Free (derElement->element);
      }
      derElement->element = buffer;
      derElement->elementSize = totalLen;
    }

    /* Append new to end of old.
     */
    Z2Memcpy (
      derElement->element + derElement->elementLen,
      dataToStore, dataToStoreLen);
    derElement->elementLen += dataToStoreLen;

    status = 0;

  } while (0);

  VOLT_LOG_ERROR_COMPARE (
    status, (VtLibCtx)libCtx, status, VT_ERROR_TYPE_PRIMARY,
    fnctLine, "StoreElementData", (char *)0)

  return (status);
}

static int CollectTagAndLenRealloc (
   VoltLibCtx *libCtx,
   unsigned char *encoding,
   unsigned int encodingLen,
   unsigned char **buffer,
   unsigned int *bufferSize,
   unsigned int *bufferLen,
   unsigned int *bytesRead,
   unsigned int *complete
   )
{
  int status;
  unsigned int currentSize, newSize, currentLen, newLen;
  unsigned int index, offset, count, done;
  unsigned char *buf;
  unsigned char theOctets[10];
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    currentSize = *bufferSize;
    currentLen = *bufferLen;
    buf = *buffer;
    *bytesRead = 0;

    done = 0;
    newSize = currentSize;

    /* If there are any bytes in the caller-supplied buffer, copy them
     * into the local buffer.
     */
    for (index = 0; index < currentLen; ++index)
      theOctets[index] = buf[index];

    newLen = 0;
    status = 0;
    switch (currentLen)
    {
      case 0:
        /* Nothing has been collected so far.
         */
        theOctets[0] = encoding[0];
        encodingLen--;
        encoding++;
        newLen = 1;
        *bytesRead = 1;
        if (encodingLen == 0)
          break;

      case 1:
        /* We have the tag, but not the first length octet.
         */
        count = (unsigned int)encoding[0];
        theOctets[1] = encoding[0];
        encodingLen--;
        encoding++;
        newLen++;
        *bytesRead += 1;

        /* If the first length octet is <= 0x80, it is the only length
         * octet.
         */
        done = 1;
        if (count <= 0x80)
          break;
        done = 0;

        /* If the first length octet is > 0x80, it tells us how many
         * octets make up the length.
         */
        VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
        VOLT_SET_FNCT_LINE (fnctLine)
        status = VT_ERROR_INVALID_ENCODING;
        if ((count & 0xf0) != 0x80)
          break;
        if ((count & 0x0f) > 8)
          break;

        status = 0;
        if (encodingLen == 0)
          break;

      default:
        /* We have the tag and first length octet. How many bytes make
         * up the length?
         */
        count = (unsigned int)(theOctets[1]);
        count &= 0xf;

        /* Make sure that the current buffer does not already have the
         * expected number of bytes.
         */
        VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
        VOLT_SET_FNCT_LINE (fnctLine)
        status = VT_ERROR_INVALID_ENCODING;
        if (currentLen >= (count + 2))
          break;
        status = 0;

        /* If we already have some of the length octets, determine how
         * many we do need.
         */
        offset = 2;
        if (currentLen > 2)
        {
          count -= (currentLen - 2);
          offset += (currentLen - 2);
        }

        /* Do we have enough bytes in the encoding?
         */
        if (encodingLen >= count)
          done = 1;
        else
          count = encodingLen;

        newLen += count;

        /* Copy as many bytes as we can.
         */
        for (index = 0; index < count; ++index)
          theOctets[index + offset] = encoding[index];

        *bytesRead += count;
    }
    if (status != 0)
      break;

    /* Copy from the local buffer into the caller-supplied buffer.
     */
    if (currentSize < (currentLen + newLen))
    {
      VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_MEMORY;
      *buffer = (unsigned char *)Z2Realloc (*buffer, currentLen + newLen);
      if (*buffer == (unsigned char *)0)
        break;
      buf = *buffer;
      status = 0;

      newSize = currentLen + newLen;
    }

    for (index = 0; index < currentLen + newLen; ++index)
      buf[index] = theOctets[index];

  } while (0);

  *bufferSize = newSize;
  *bufferLen = currentLen + newLen;
  *complete = done;

  VOLT_LOG_ERROR_INFO_COMPARE (
    status, libCtx, 0, status, 0, errorType,
    (char *)0, "CollectTagAndLenRealloc", fnctLine, (char *)0)

  return (status);
}

⌨️ 快捷键说明

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