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

📄 zdm2common.c

📁 IBE是一种非对称密码技术
💻 C
字号:
/* Copyright 2005-2006, Voltage Security, all rights reserved.
 */

#include "vibe.h"
#include "environment.h"
#include "base.h"
#include "libctx.h"
#include "zdm2common.h"
#include "datanode.h"
#include "template.h"
#include "errorctx.h"
#include "securemail.h"
#include "stringutil.h"
#include "vtassert.h"

const char* VoltZDM2RootAttributeName                 = "ZDMAttributes";
const char* VoltZDM2VersionAttributeName              = "Version";
const char* VoltZDM2CompatibleVersionAttributeName    = "CompatibleVersion";
const char* VoltZDM2SenderAttributeName               = "Sender";
const char* VoltZDM2RecipientsAttributeName           = "Recipients";
const char* VoltZDM2RecipientAttributeName            = "Recipient";
const char* VoltZDM2AddressAttributeName              = "Address";
const char* VoltZDM2FlagsAttributeName                = "Flags";
const char* VoltZDM2MessageIDAttributeName            = "MessageID";
const char* VoltZDM2ReplyToAttributeName              = "ReplyTo";
const char* VoltZDM2SubjectAttributeName              = "Subject";
const char* VoltZDM2TimestampAttributeName            = "Timestamp";
const char* VoltZDM2ContentTypeAttributeName          = "ContentType";
const char* VoltZDM2CharacterSetAttributeName         = "CharacterSet";
const char* VoltZDM2OriginalCharacterSetAttributeName = "OriginalCharacterSet";
const char* VoltZDM2MessageBlockStartTagPrefix        = "-----BEGIN VOLTAGE MESSAGE BLOCK V";
const char* VoltZDM2MessageBlockStartTagSuffix        = "-----";
const char* VoltZDM2MessageBlockEndTagPrefix          = "-----END VOLTAGE MESSAGE BLOCK V";
const char* VoltZDM2MessageBlockEndTagSuffix          = "-----";
const char* VoltZDM2SecureBlockStartTagPrefix         = "-----BEGIN VOLTAGE SECURE BLOCK V";
const char* VoltZDM2SecureBlockStartTagSuffix         = "-----";
const char* VoltZDM2SecureBlockEndTagPrefix           = "-----END VOLTAGE SECURE BLOCK V";
const char* VoltZDM2SecureBlockEndTagSuffix           = "-----";
const char* VoltZDM2InsecureAttributesFilePathName    = "InsecureAttributes";
const char* VoltZDM2TypeMessage                       = "VoltageZFRMsg";
const char* VoltZDM2TypeAttachment                    = "VoltageZFRDoc";
const char* VoltZDM2HeaderVariableName                = "Header";
const char* VoltZDM2MessageDataVariableName           = "MessageData";
const char* VoltZDM2CharSetVariableName               = "Charset";
const char* VoltZDM2MessageStartTagVariableName       = "MessageStartTag";
const char* VoltZDM2MessageEndTagVariableName         = "MessageEndTag";
const char* VoltZDM2DefaultCharacterSet               = "utf-8";
const char* VoltZDM2DefaultContentType                = "text/plain";
const char* VoltZDM2InputFieldTagPrefix1              = "      <input type=\"hidden\" name=\"ZFRdata";
const char* VoltZDM2InputFieldTagPrefix2              = "\" value=\"";
const char* VoltZDM2InputFieldTagSuffix               = "\">";
const char* VoltZDM2MetaVersionPrefix                 = "<meta name=\"VoltageZFRVersion\" content=\"";
const char* VoltZDM2MetaTypePrefix                    =  "<meta name=\"VoltageZFRType\" content=\"";
const char* VoltZDM2MetaSuffix                        = "\">";


int VoltZDM2FormatMessageTag(
  VtLibCtx libCtx,
  const unsigned char* prefix,
  const unsigned char* suffix,
  const unsigned char* newLine,
  unsigned int version,
  int encloseInHTMLComment,
  unsigned char** tag
)
{
  int status = 0;
  unsigned int prefixLength;
  unsigned int numLength;
  unsigned int suffixLength;
  unsigned int newLineLength;
  unsigned int tagLength;
  unsigned char* p;
  unsigned char num[100];
  unsigned char commentStart[] = "<!--";
  unsigned char commentEnd[] = "-->";
  unsigned int commentStartLength = sizeof(commentStart) - 1;
  unsigned int commentEndLength = sizeof(commentEnd) - 1;
  VOLT_DECLARE_FNCT_LINE(fnctLine)
  
  VT_ASSERT(libCtx != (VtLibCtx)0);
  VT_ASSERT(prefix != (unsigned char*)0);
  VT_ASSERT(suffix != (unsigned char*)0);
  VT_ASSERT(tag != (unsigned char**)0);
  
  do
  {
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VoltNumToDecimalString(version, num, sizeof(num), libCtx);
    if (status != 0)
      break;
      
    prefixLength = Z2Strlen(prefix);
    suffixLength = Z2Strlen(suffix);
    numLength = Z2Strlen(num);
    newLineLength = (newLine != (const unsigned char*)0) ?
      Z2Strlen(newLine) : 0;
    
    tagLength = prefixLength + numLength + suffixLength + newLineLength + 1;
    if (encloseInHTMLComment)
      tagLength += (commentStartLength + commentEndLength);
      
    *tag = (unsigned char*) Z3Malloc(tagLength);
    if (*tag == (unsigned char*)0)
    {
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VT_ERROR_MEMORY;
      break;
    }
    
    p = *tag;
    
    if (encloseInHTMLComment)
    {
      Z2Memcpy(p, commentStart, commentStartLength);
      p += commentStartLength;
    }
    Z2Memcpy(p, prefix, prefixLength);
    p += prefixLength;
    Z2Memcpy(p, num, numLength);
    p += numLength;
    Z2Memcpy(p, suffix, suffixLength);
    p += suffixLength;
    if (encloseInHTMLComment)
    {
      Z2Memcpy(p, commentEnd, commentEndLength);
      p += commentEndLength;
    }
    if (newLine != (unsigned char*)0)
    {
      Z2Memcpy(p, newLine, newLineLength);
      p += newLineLength;
    }
    *p = 0;
  }
  while (0);

  VOLT_LOG_ERROR_COMPARE(status, libCtx, status, 0,
    fnctLine, "VoltZDM2FormatMessageTag", (unsigned char*)0)

  return status;
}

int VoltZDM2FindMessageTag(
  VtLibCtx libCtx,
  const unsigned char* prefix,
  const unsigned char* suffix,
  const unsigned char* data,
  unsigned int dataLength,
  unsigned int startVersion,
  unsigned int endVersion,
  unsigned int* foundVersion,
  unsigned int* startOffset,
  unsigned int* endOffset
)
{
  const unsigned char* p = data;
  unsigned int prefixLength = Z2Strlen(prefix);
  unsigned int suffixLength = Z2Strlen(suffix);
  const unsigned char* end = data + dataLength - prefixLength;
  const unsigned char* tagStart;
  const unsigned char* versionStart;
  unsigned int version;
  
  VT_ASSERT(prefix != (unsigned char*)0);
  VT_ASSERT(suffix != (unsigned char*)0);
  VT_ASSERT(data != (unsigned char*)0);
  VT_ASSERT(dataLength >= 0);
  VT_ASSERT(startVersion > 0);
  VT_ASSERT(endVersion > 0);
  VT_ASSERT(endVersion >= startVersion);
  VT_ASSERT(foundVersion != (unsigned int*)0);
  VT_ASSERT(startOffset != (unsigned int*)0);
  VT_ASSERT(endOffset != (unsigned int*)0);
  
  for (;;)
  {
    /* Find the next occurrence of the tag prefix */
    tagStart = (unsigned char*)0;
    while (p < end)
    {
      if (VoltCaseInsensitiveCompareBuffers(p, prefixLength,
        prefix, prefixLength, libCtx) == 0)
      {
        tagStart = p;
        break;
      }
      p++;
    }

    /* If we didn't find the tag prefix, then we're done */
    if (tagStart == (unsigned char*)0)
      break;
    
    p += prefixLength;
    
    /* The version number (all numeric characters) is next, so advance
     * to the end of the version characters.
     */
    end = data + dataLength;
    versionStart = p;
    version = 0;
    while (p < end)
    {
      if ((*p < '0') || (*p > '9'))
        break;
      version = (version * 10) + *p - '0';
      p++;
    }
    
    /* Make sure the version we found is in the range specified by
     * the caller. If it's not, we keep going in case we find the
     * tag later in the data buffer.
     */
    if ((version < startVersion) || (version > endVersion))
      continue;
    
    /* Make sure there's still enough data left to match the suffix */
    if (p + suffixLength > end)
      break;
    
    /* Check for the suffix */
    if (VoltCaseInsensitiveCompareBuffers(p, suffixLength,
      suffix, suffixLength, libCtx) == 0)
    {
      *foundVersion = version;
      *startOffset = tagStart - data;
      *endOffset = p + suffixLength - data;
      return 1;
    }
  }

  return 0;
}

int VoltZDM2ExpandTemplate(
  VtLibCtx libCtx,
  unsigned char* text,
  unsigned int length,
  VtDataNodeObject templateVariables,
  VtStreamObject* outputStream
)
{
  int status = 0;
  VtStreamObject templateStream = (VtStreamObject)0;
  VtTemplateObject templateObj = (VtTemplateObject)0;
  VtStreamImplMemoryInfo memInfo;
  VOLT_DECLARE_FNCT_LINE(fnctLine)

  do
  {
    memInfo.data = text;
    memInfo.length = length;
    memInfo.flags = VT_STREAM_OPEN_ON_ACCESS;
    memInfo.openMode = VT_STREAM_OPEN_READ;
    
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtCreateStreamObject(libCtx, VtStreamImplMemory,
      (Pointer)&memInfo, &templateStream);
    if (status != 0)
      break;

    memInfo.data = (unsigned char*)0;
    memInfo.length = 0;
    memInfo.openMode = VT_STREAM_OPEN_READ_WRITE;
    
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtCreateStreamObject(libCtx, VtStreamImplMemory,
      (Pointer)&memInfo, outputStream);
    if (status != 0)
      break;

    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtCreateTemplateObject(libCtx, VtTemplateImplFreeMarker,
      (Pointer)0, &templateObj);
    if (status != 0)
      break;

    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtTemplateCompile(templateObj, templateStream);
    if (status != 0)
      break;
    
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtTemplateWrite(templateObj, templateVariables, *outputStream);
    if (status != 0)
      break;

    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtStreamSetPosition(*outputStream, 0, VT_STREAM_FROM_START);
    if (status != 0)
      break;
  }
  while (0);

  VtDestroyStreamObject(&templateStream);
  VtDestroyTemplateObject(&templateObj);
  
  if (status != 0)
  {
    VtDestroyStreamObject(outputStream);
    
    VOLT_LOG_ERROR(libCtx, status, VT_ERROR_TYPE_PRIMARY, fnctLine,
      "VoltZDM2ExpandTemplate", (unsigned char*)0)
  }
  
  return status;
}

int VoltZDM2FindMessageDataInTemplate(
  VtLibCtx libCtx,
  unsigned char* templateText,
  unsigned int templateLength,
  unsigned char** preMessageDataText,
  unsigned int* preMessageDataLength,
  unsigned char** postMessageDataText,
  unsigned int* postMessageDataLength
)
{
  int status = 0;
  unsigned int i;
  unsigned char* p;
  unsigned char* end;
  unsigned char messageDataName[256];
  unsigned int messageDataNameLength;
  int match;
  VOLT_DECLARE_FNCT_LINE(fnctLine)
  
  p = messageDataName;
  *p++ = '$';
  *p++ = '{';
  Z2Strcpy(p, VoltZDM2MessageDataVariableName);
  p += Z2Strlen(VoltZDM2MessageDataVariableName);
  *p++ = '}';
  messageDataNameLength = p - messageDataName;
  
  p = templateText;
  end = templateText + templateLength - messageDataNameLength;
  
  while (p <= end)
  {
    match = 1;
    for (i = 0; i < messageDataNameLength; i++)
    {
      if (Z2Tolower(p[i]) != Z2Tolower(messageDataName[i]))
      {
        match = 0;
        break;
      }
    }
    if (match)
      break;
    p++;
  }
  
  if (match)
  {
    *preMessageDataText = templateText;
    *preMessageDataLength = p - templateText;
    *postMessageDataText = p + messageDataNameLength;
    *postMessageDataLength = templateText + templateLength -
      *postMessageDataText;
  }
  else
  {
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VT_ERROR_NO_MESSAGE_DATA_VAR;
  }
  
  VOLT_LOG_ERROR_COMPARE(status, libCtx, status, VT_ERROR_TYPE_PRIMARY,
    fnctLine, "VoltZDM2FindMessageDataInTemplate", (unsigned char*)0)
    
  return status;
}

int VoltDetermineBase64InputSize(
  VtLibCtx libCtx,
  VtAlgorithmObject base64Encoder,
  unsigned int outputSize,
  unsigned int* inputSize
)
{
  int status = 0;
  unsigned int testInputSize;
  unsigned int testOutputSize;
  unsigned int low = 0;
  unsigned int high = outputSize;
  VoltEncodeDecodeSizeInfo encodeDecodeSizeInfo;
  VOLT_DECLARE_FNCT_LINE(fnctLine)

  while (high > low)
  {
    testInputSize = (low + high + 1) / 2;
    encodeDecodeSizeInfo.dataToProcess = (unsigned char *)0;
#if VT_64_BIT_LENGTH == 64
    encodeDecodeSizeInfo.dataToProcessLen = (VtUInt64)testInputSize;
#else
    encodeDecodeSizeInfo.dataToProcessLen = testInputSize;
#endif
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VoltBase64GetEncodeDecodeSize (
      base64Encoder, (VtRandomObject)0, VOLT_CALLER_ENCODE_FINAL,
      &encodeDecodeSizeInfo);
    if (status == 0)
      status = VT_ERROR_GENERAL;
    if (status == VT_ERROR_BUFFER_TOO_SMALL)
      status = 0;
    if (status != 0)
      break;
#if VT_64_BIT_LENGTH == 64
    testOutputSize = (unsigned int)(encodeDecodeSizeInfo.processedDataLen);
#else
    testOutputSize = encodeDecodeSizeInfo.processedDataLen;
#endif
    if (testOutputSize > outputSize)
      high = testInputSize - 1;
    else
      low = testInputSize;
  }

  *inputSize = low;

  VOLT_LOG_ERROR_COMPARE(status, libCtx, status, 0,
    fnctLine, "VoltDetermineBase64InputSize", (unsigned char*)0)

  return status;
}

⌨️ 快捷键说明

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