📄 zdm2common.c
字号:
/* Copyright 2005, 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* VoltZDM2CurrentVersion = "2";
const char* VoltZDM2CompatibleVersion = "2";
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";
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 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;
VOLT_DECLARE_FNCT_LINE(fnctLine)
while (high > low)
{
testInputSize = (low + high + 1) / 2;
VOLT_SET_FNCT_LINE(fnctLine)
status = VoltBase64GetEncodeDecodeSize(base64Encoder, (VtRandomObject)0,
VOLT_CALLER_ENCODE_FINAL, (unsigned char*)0, testInputSize, &testOutputSize);
if (status == 0)
status = VT_ERROR_GENERAL;
if (status == VT_ERROR_BUFFER_TOO_SMALL)
status = 0;
if (status != 0)
break;
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 + -