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

📄 smwrite.c

📁 IBE是一种非对称密码技术
💻 C
📖 第 1 页 / 共 4 页
字号:
      obj->p7EnvelopedData, VtPkcs7ParamDataLen64,
      (Pointer)&(writeCtx->signedDataLen));
    if (status != 0)
      break;
#else
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtSetPkcs7Param (
      obj->p7EnvelopedData, VtPkcs7ParamDataLen,
      (Pointer)&(writeCtx->signedDataLen));
    if (status != 0)
      break;
#endif

    /* How big is the enveloped data going to be?
     */
#if VT_64_BIT_LENGTH == 64
    /* Call VtGetTotalOutputLen64 to determine how big the SignedData
     * will be.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtGetTotalOutputLen64 (
      (VtLibCtx)libCtx, VtGetOutputLen64ImplPkcs7,
      (Pointer)(obj->p7EnvelopedData), &(writeCtx->envelopedDataLen));
    if (status != 0)
      break;
#else
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtPkcs7WriteFinal (
      obj->p7EnvelopedData, random,
      (unsigned char *)0, (writeCtx->signedDataLen),
      (unsigned char *)0, 0, &(writeCtx->envelopedDataLen));
    if (status == 0)
      status = VT_ERROR_GENERAL;
    if (status != VT_ERROR_BUFFER_TOO_SMALL)
      break;
#endif

    /* How big will the encoded data be?
     */
    encodeDecodeSizeInfo.dataToProcess = (unsigned char *)0;
    encodeDecodeSizeInfo.dataToProcessLen =
      writeCtx->envelopedDataLen + preP7->len;
    VOLT_SET_FNCT_LINE (fnctLine)
    status = obj->GetEncodeDecodeSize (
      obj->base64, (VtRandomObject)0, VOLT_CALLER_ENCODE_FINAL,
      &encodeDecodeSizeInfo);
    writeCtx->base64Len = encodeDecodeSizeInfo.processedDataLen;

    if (status == 0)
      status = VT_ERROR_GENERAL;
    if (status != VT_ERROR_BUFFER_TOO_SMALL)
      break;

    /* If this is ZDM, we may need to add padding.
     */
    if ((obj->formatType & VOLT_MESSAGE_FORMAT_ZDM) != 0)
    {
#if VT_64_BIT_LENGTH == 64
      numHi = (UInt32)(writeCtx->base64Len >> 32);
      numLo = (UInt32)(writeCtx->base64Len);
#else
      numHi = (UInt32)0;
      numLo = (UInt32)(writeCtx->base64Len);
#endif
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VoltAddZDMPad (obj, writeCtx, numLo, numHi);
      if (status != 0)
        break;
    }

#if VT_64_BIT_LENGTH == 64
    /* If 64-bit lengths, set the outputLen64 in case we need it.
     */
    obj->outputLen64 =
      writeCtx->base64Len + (VtUInt64)writeCtx->prelimLen +
      (VtUInt64)(writeCtx->trailLen);
#endif

    status = 0;
    obj->state = VOLT_SECURE_MAIL_STATE_WRITE_INIT_LEN;

  } while (0);

  if (asciiNum != (char *)0)
    Z2Free (asciiNum);

  VOLT_LOG_ERROR_INFO_COMPARE (
    status, 0, obj, status, 0, errorType,
    (char *)0, "SetTotalLengths", fnctLine, (char *)0)

  return (status);
}

int ConvertNumToAsciiAlloc (
   VoltLibCtx *libCtx,
   UInt32 theNumLo,
   UInt32 theNumHi,
   char **asciiNum,
   unsigned int *asciiNumLen
   )
{
  unsigned int places, index;
#if VT_64_BIT_LENGTH == 64
  VtUInt64 theNum, radix, value, current;
#else
  UInt32 theNum, radix, value, current;
#endif
  char *buffer = (unsigned char *)0;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  *asciiNum = (char *)0;

#if VT_64_BIT_LENGTH == 64
  theNum = (VtUInt64)theNumHi;
  theNum <<= 32;
  theNum += (VtUInt64)theNumLo;
#else
  theNum = theNumLo;
#endif

  /* First, count the size.
   */
  radix = 10;
  places = 1;
  while (theNum >= radix)
  {
    places++;
    radix *= 10;
  }

  /* Allocate the buffer to hold the result.
   */
  VOLT_SET_FNCT_LINE (fnctLine)
  buffer = (char *)Z2Malloc (places + 1, 0);
  if (buffer != (char *)0)
  {
    Z2Memset ((Pointer)buffer, 0x30, places);
    buffer[places] = 0;

    value = theNum;
    index = places - 1;
    do
    {
      current = value % 10;
      buffer[index] |= (char)current;
      value -= current;
      value = value / 10;
      index--;
    } while (value != 0);

    *asciiNum = buffer;
    *asciiNumLen = places;

    return (0);
  }

  VOLT_LOG_ERROR_INFO (
    libCtx, 0, VT_ERROR_MEMORY, 0, VT_ERROR_TYPE_PRIMARY,
    (char *)0, "ConvertNumToAsciiAlloc", fnctLine, (char *)0)

  return (VT_ERROR_MEMORY);
}

static int AppendToVtItem (
   VoltLibCtx *libCtx,
   unsigned char *dataToAppend,
   unsigned int dataToAppendLen,
   unsigned char *trailingCharacters,
   unsigned int trailingCharactersLen,
   VtItem *theItem
   )
{
  int status;
  unsigned int totalLen;
  unsigned char *newBuffer = (unsigned char *)0;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Allocate space for old an new.
     */
    totalLen = 0;
    if (theItem->data != (unsigned char *)0)
      totalLen = theItem->len;

    totalLen += dataToAppendLen + trailingCharactersLen;

    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    newBuffer = (unsigned char *)Z2Malloc (totalLen, VOLT_MEMORY_SENSITIVE);
    if (newBuffer == (unsigned char *)0)
      break;

    /* Copy the old into the new.
     */
    Z2Memcpy (newBuffer, theItem->data, theItem->len);

    /* Copy the new.
     */
    Z2Memcpy (newBuffer + theItem->len, dataToAppend, dataToAppendLen);

    /* Any trailing characters?
     */
    Z2Memcpy (
      newBuffer + theItem->len + dataToAppendLen, trailingCharacters,
      trailingCharactersLen);

    /* Free the old.
     */
    Z2Free (theItem->data);

    /* Set theItem with the new.
     */
    theItem->data = newBuffer;
    theItem->len = totalLen;

    status = 0;

  } while (0);

  /* If no errors, we're done.
   */
  if (status == 0)
    return (0);

  /* If error, free up what we allocated.
   */
  if (newBuffer != (unsigned char *)0)
    Z2Free (newBuffer);

  VOLT_LOG_ERROR_INFO (
    libCtx, 0, status, 0, VT_ERROR_TYPE_PRIMARY,
    (char *)0, "AppendToVtItem", fnctLine, (char *)0)

  return (status);
}

#define ZDM_REQUIRED_LEN    4096
#define ZDM_SEARCH_PATTERN  "\">"
#define ONE_LINE_LEN        64

int VoltAddZDMPad (
   VoltSecureMailObject *obj,
   VoltSecureMailWriteCtx *writeCtx,
   UInt32 numLo,
   UInt32 numHi
   )
{
  int status;
  unsigned int startLen, endLen, extraLen, totalLen, newLen, lineCount;
  unsigned int index, leftovers, offset;
  unsigned int messageLen;
  VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
  char *start, *end;
  unsigned char *buffer = (unsigned char *)0;
  unsigned char oneLine[ONE_LINE_LEN];
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* If messageLen is already > ZDM_REQUIRED_LEN, no need to go
     * further.
     */
    status = 0;
    if (numHi != (UInt32)0)
      break;
    if (numLo >= ZDM_REQUIRED_LEN)
      break;

    messageLen = (unsigned int)numLo;

    /* In writeCtx->itemArray[VOLT_WRITE_SM_ITEM_END_MSG] is the place
     * where we stop counting towards the ZDM_REQUIRED_LEN. Find that
     * place.
     * Don't use Strstr because this buffer is not necessarily
     * NULL-terminated.
     */
    start = (char *)(writeCtx->itemArray[VOLT_WRITE_SM_ITEM_END_MSG].data);
    end = start;
    offset = 0;
    /* Set newLen to a max. If we search this many bytes, we didn't
     * find the target and we won't, so stop looking.
     */
    newLen = writeCtx->itemArray[VOLT_WRITE_SM_ITEM_END_MSG].len - 1;
    do
    {
      if (end[0] == '"')
      {
        if (end[1] == '>')
          break;
      }
      offset++;
      end++;

      /* If we haven't looked through the entire buffer, keep looking.
       * If there's no more buffer to search, stop looking, the answer
       * is NULL, which will later trigger an error.
       */
      if (offset < newLen)
        continue;

      end = (char *)0;
      break;

    } while (1);

    /* If we didn't find the location, it wasn't there, something's
     * wrong.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_INPUT;
    if (end == (char *)0)
      break;

    startLen = (unsigned int)end - (unsigned int)start;
    endLen = writeCtx->itemArray[VOLT_WRITE_SM_ITEM_END_MSG].len - startLen;
    /* Any bytes up to the target are counted toward the
     * ZDM_REQUIRED_LEN.
     */
    totalLen = startLen;
    /* The BEGIN MESSAGE, BEGIN BLOCK, and END BLOCK are included in
     * the ZDM_REQUIRED_LEN, if they exist.
     */
    totalLen += writeCtx->itemArray[VOLT_WRITE_SM_ITEM_END_BLK].len;
    totalLen += writeCtx->itemArray[VOLT_WRITE_SM_ITEM_BEGIN_BLK].len;
    totalLen += writeCtx->itemArray[VOLT_WRITE_SM_ITEM_HEADER].len;
    /* Finally, add the message length.
     */
    totalLen += messageLen;

    /* If there are at least ZDM_REQUIRED_LEN bytes, no need to pad,
     * we're done.
     */
    status = 0;
    if (totalLen >= ZDM_REQUIRED_LEN)
      break;

    /* How many pad bytes do we need?
     */
    extraLen = ZDM_REQUIRED_LEN - totalLen;

    /* We're going to add a series of lines, each line consisting of a
     * series of spaces, followed by new line character(s). First,
     * build a line with the appropriate new line character. The total
     * length of the line will be ONE_LINE_LEN.
     */
    Z2Memset (oneLine, ' ', ONE_LINE_LEN);
    Z2Memcpy (
      oneLine + ONE_LINE_LEN -
      writeCtx->itemArray[VOLT_WRITE_SM_ITEM_NEW_LINE].len,
      writeCtx->itemArray[VOLT_WRITE_SM_ITEM_NEW_LINE].data,
      writeCtx->itemArray[VOLT_WRITE_SM_ITEM_NEW_LINE].len);

    /* How many full lines do we add? How many leftovers after we have
     * printed out a bunch of lines?
     */
    lineCount = extraLen / ONE_LINE_LEN;
    leftovers = extraLen - (lineCount * ONE_LINE_LEN);

    /* Allocate a buffer to hold the contents of the data in
     * itemArray[VOLT_WRITE_SM_ITEM_END_MSG] along with the new lines.
     * The new data will be placed into the middle of the buffer, which
     * is why we don't do a Realloc (although we could with a Memmove,
     * but choose not to for efficiency).
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    newLen = writeCtx->itemArray[VOLT_WRITE_SM_ITEM_END_MSG].len + extraLen;
    buffer = (unsigned char *)Z2Malloc (newLen + 1, 0);
    if (buffer == (unsigned char *)0)
      break;

    Z2Memcpy (
      buffer, writeCtx->itemArray[VOLT_WRITE_SM_ITEM_END_MSG].data, startLen);
    offset = startLen;
    for (index = 0; index < lineCount; ++index)
    {
      Z2Memcpy (buffer + offset, oneLine, ONE_LINE_LEN);
      offset += ONE_LINE_LEN;
    }
    /* Print out leftovers. Get rid of the new line characters in case
     * leftovers is ONE_LINE_LEN - 1 and newLineChar length is 2.
     */
    Z2Memset (
      oneLine + ONE_LINE_LEN -
      writeCtx->itemArray[VOLT_WRITE_SM_ITEM_NEW_LINE].len, ' ',
      writeCtx->itemArray[VOLT_WRITE_SM_ITEM_NEW_LINE].len);
    Z2Memcpy (buffer + offset, oneLine, leftovers);
    offset += leftovers;
    Z2Memcpy (
      buffer + offset,
      writeCtx->itemArray[VOLT_WRITE_SM_ITEM_END_MSG].data + startLen, endLen);
    buffer[newLen] = 0;

    /* Free the old buffer.
     */
    Z2Free (writeCtx->itemArray[VOLT_WRITE_SM_ITEM_END_MSG].data);
    /* Replace it with the new.
     */
    writeCtx->itemArray[VOLT_WRITE_SM_ITEM_END_MSG].data = buffer;
    writeCtx->itemArray[VOLT_WRITE_SM_ITEM_END_MSG].len = newLen;
    writeCtx->trailLen += extraLen;

    status = 0;

  } while (0);

  if (status == 0)
    return (0);

  /* If there was an error, free the buffer we would have returned, but
   * didn't.
   */
  if (buffer != (unsigned char *)0)
    Z2Free (buffer);

  VOLT_LOG_ERROR_INFO (
    0, obj, status, 0, VT_ERROR_TYPE_PRIMARY,
    (char *)0, "VoltAddZDMPad", fnctLine, (char *)0)

  return (status);
}

⌨️ 快捷键说明

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