📄 zdmsmwrite.c
字号:
case VOLT_SECURE_MAIL_STATE_WRITE_INIT:
case VOLT_ZDM_FROM_SM_STATE_WRITE_HEADER:
case VOLT_ZDM_FROM_SM_STATE_WRITE_HEAD_2:
case VOLT_ZDM_FROM_SM_STATE_WRITE_BODY:
/* If in this state, there's still something more than the footer
* to write out.
*/
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = VoltZDMFromSMWriteUpdate (
secureMailObj, random, inputData, inputDataLen,
(unsigned char *)0, 0, &outputLenUpdate);
if ( (status != 0) && (status != VT_ERROR_BUFFER_TOO_SMALL) )
break;
encodeDecodeSizeInfo.dataToProcess = (unsigned char *)0;
#if VT_64_BIT_LENGTH == 64
encodeDecodeSizeInfo.dataToProcessLen = (VtUInt64)0;
#else
encodeDecodeSizeInfo.dataToProcessLen = 0;
#endif
VOLT_SET_FNCT_LINE (fnctLine)
status = obj->GetEncodeDecodeSize (
obj->base64, (VtRandomObject)0, VOLT_CALLER_ENCODE_FINAL,
&encodeDecodeSizeInfo);
if (status != VT_ERROR_BUFFER_TOO_SMALL)
break;
#if VT_64_BIT_LENGTH == 64
b64Len = (unsigned int)(encodeDecodeSizeInfo.processedDataLen);
#else
b64Len = encodeDecodeSizeInfo.processedDataLen;
#endif
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_BUFFER_TOO_SMALL;
*messageLen = outputLenUpdate + b64Len + writeCtx->trailLen;
if ( (bufferSize < *messageLen) || (inputData == (unsigned char *)0) )
break;
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = VoltZDMFromSMWriteUpdate (
secureMailObj, random, inputData, inputDataLen,
message, bufferSize, &offset);
if (status != 0)
break;
/* If this is ZDM From SecureMail, the Update call should have
* set the state to FOOTER.
*/
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_INPUT;
if ( (obj->formatType == VOLT_MESSAGE_FORMAT_ZDM_MAIL) &&
(obj->state != VOLT_ZDM_FROM_SM_STATE_WRITE_FOOTER) )
break;
goto VoltZdmFromSmStateWriteFooter;
case VOLT_ZDM_FROM_SM_STATE_WRITE_FOOTER:
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_BUFFER_TOO_SMALL;
*messageLen = writeCtx->trailLen;
if (bufferSize < *messageLen)
break;
VoltZdmFromSmStateWriteFooter:
/* Write out any trailing info.
*/
newLine = writeCtx->itemArray[VOLT_WRITE_SM_ITEM_NEW_LINE].data;
newLineLen = writeCtx->itemArray[VOLT_WRITE_SM_ITEM_NEW_LINE].len;
for (index = VOLT_WRITE_SM_FOOT_INDEX_START;
index <= VOLT_WRITE_SM_FOOT_INDEX_END; ++index)
{
/* Add in the length of the actual data to write out.
* If there is data to write out, add a newLine.
*/
elementLen = writeCtx->itemArray[index].len;
if (elementLen == 0)
continue;
Z2Memcpy (
message + offset, writeCtx->itemArray[index].data, elementLen);
offset += elementLen;
Z2Memcpy (message + offset, newLine, newLineLen);
offset += newLineLen;
}
obj->state = VOLT_ZDM_FROM_SM_STATE_WRITE_COMPLETE;
status = 0;
}
VOLT_LOG_ERROR_INFO_COMPARE (
status, 0, secureMailObj, status, 0, errorType,
(char *)0, "VoltZDMFromSMWriteFinal", fnctLine, (char *)0)
return (status);
}
int VoltOldZDMFromSFWriteFinal (
VtZDMObject zdmObj,
VtRandomObject random,
unsigned char *inputData,
unsigned int inputDataLen,
unsigned char *message,
unsigned int bufferSize,
unsigned int *messageLen
)
{
int status;
VoltZDMObject *zObj = (VoltZDMObject *)zdmObj;
VOLT_DECLARE_FNCT_LINE (fnctLine)
VOLT_SET_FNCT_LINE (fnctLine)
status = VoltZDMFromSFWriteFinal (
(VtSecureMailObject)(zObj->localCtx), random, inputData, inputDataLen,
message, bufferSize, messageLen);
VOLT_LOG_ERROR_INFO_COMPARE (
status, 0, zdmObj, status, 0, 0,
(char *)0, "VoltOldZDMFromSFWriteFinal", fnctLine, (char *)0)
return (status);
}
int VoltZDMFromSFWriteFinal (
VtSecureMailObject secureMailObj,
VtRandomObject random,
unsigned char *inputData,
unsigned int inputDataLen,
unsigned char *message,
unsigned int bufferSize,
unsigned int *messageLen
)
{
int status;
unsigned int index, b64Len, newLineLen, elementLen, offset;
VoltSecureMailObject *obj = (VoltSecureMailObject *)(secureMailObj);
VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
VoltSecureMailWriteCtx *writeCtx = (VoltSecureMailWriteCtx *)(obj->localCtx);
unsigned char *newLine;
VoltEncodeDecodeSizeInfo encodeDecodeSizeInfo;
VOLT_DECLARE_ERROR_TYPE (errorType)
VOLT_DECLARE_FNCT_LINE (fnctLine)
if (obj->state == VOLT_ZDM_FROM_SM_STATE_WRITE_COMPLETE)
return (0);
offset = 0;
switch (obj->state)
{
default:
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_CALL_ORDER;
break;
case VOLT_SECURE_MAIL_STATE_WRITE_INIT:
case VOLT_ZDM_FROM_SM_STATE_WRITE_HEADER:
case VOLT_ZDM_FROM_SM_STATE_WRITE_HEAD_2:
case VOLT_ZDM_FROM_SM_STATE_WRITE_BODY:
/* How big will the base64 data be?
*/
encodeDecodeSizeInfo.dataToProcess = (unsigned char *)0;
#if VT_64_BIT_LENGTH == 64
encodeDecodeSizeInfo.dataToProcessLen = (VtUInt64)inputDataLen;
#else
encodeDecodeSizeInfo.dataToProcessLen = inputDataLen;
#endif
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = obj->GetEncodeDecodeSize (
obj->base64, (VtRandomObject)0, VOLT_CALLER_ENCODE_FINAL,
&encodeDecodeSizeInfo);
#if VT_64_BIT_LENGTH == 64
b64Len = (unsigned int)(encodeDecodeSizeInfo.processedDataLen);
#else
b64Len = encodeDecodeSizeInfo.processedDataLen;
#endif
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_BUFFER_TOO_SMALL;
*messageLen = b64Len + writeCtx->prelimLen + writeCtx->trailLen;
if (bufferSize < *messageLen)
break;
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = VoltZDMFromSMWriteUpdate (
secureMailObj, random, inputData, inputDataLen,
message, bufferSize, &offset);
if (status != 0)
break;
/* Finish the Base64.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VtEncodeFinal (
obj->base64, (VtRandomObject)0, (unsigned char *)0, 0,
message + offset, bufferSize - offset, &b64Len);
if (status != 0)
break;
offset += b64Len;
/* Write out any trailing info.
*/
newLine = writeCtx->itemArray[VOLT_WRITE_SM_ITEM_NEW_LINE].data;
newLineLen = writeCtx->itemArray[VOLT_WRITE_SM_ITEM_NEW_LINE].len;
for (index = VOLT_WRITE_SM_FOOT_INDEX_START;
index <= VOLT_WRITE_SM_FOOT_INDEX_END; ++index)
{
/* Add in the length of the actual data to write out.
* If there is data to write out, add a newLine.
*/
elementLen = writeCtx->itemArray[index].len;
if (elementLen == 0)
continue;
Z2Memcpy (
message + offset, writeCtx->itemArray[index].data, elementLen);
offset += elementLen;
Z2Memcpy (message + offset, newLine, newLineLen);
offset += newLineLen;
}
obj->state = VOLT_ZDM_FROM_SM_STATE_WRITE_COMPLETE;
status = 0;
}
VOLT_LOG_ERROR_INFO_COMPARE (
status, 0, secureMailObj, status, 0, errorType,
(char *)0, "VoltZDMFromSFWriteFinal", fnctLine, (char *)0)
return (status);
}
static int ComputeSMStartAddress (
VoltLibCtx *libCtx,
VoltSecureMailObject *obj,
VoltSecureMailWriteCtx *writeCtx,
unsigned char *inputData,
unsigned int inputDataLen,
unsigned char **dataStart
)
{
int status;
unsigned int bytesRead, completeFlag, currentMessageLen;
unsigned int newLineLen;
unsigned char *currentMessage;
unsigned char *beginBlk = VOLT_SECURE_MAIL_BEGIN_BLOCK;
VoltZDMFromSecureMailCtx *zdmCtx =
(VoltZDMFromSecureMailCtx *)(obj->localCtx);
VOLT_DECLARE_ERROR_TYPE (errorType)
VOLT_DECLARE_FNCT_LINE (fnctLine)
/* If we're converting a SecureFile to ZDM, the beginning is
* inputData.
*/
if (obj->formatType == VOLT_MESSAGE_FORMAT_ZDM_ATTACHMENT)
{
*dataStart = inputData;
return (0);
}
*dataStart = (unsigned char *)0;
currentMessage = inputData;
currentMessageLen = inputDataLen;
status = 0;
switch (obj->state)
{
default:
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_CALL_ORDER;
break;
case VOLT_ZDM_FROM_SM_STATE_WRITE_HEAD_2:
/* The previous call to ComputeStartAddress found the end of the
* SM header, but there was no input data. That is, the last byte
* of inputData was the last byte of the SM header.
* If prelimLen is 0, the ZDM header material was written out,
* we're now on to the BODY. If prelimLen is not 0, the previous
* call returned BUFFER_TOO_SMALL, so we're operating on the same
* data a second time, so start over.
*/
if (writeCtx->prelimLen == 0)
{
obj->state = VOLT_ZDM_FROM_SM_STATE_WRITE_BODY;
goto VoltZdmFromSmStateWriteBody;
}
case VOLT_SECURE_MAIL_STATE_WRITE_INIT:
/* Set the state to WRITE_HEADER, becasue that's what we need to
* do first.
*/
obj->state = VOLT_ZDM_FROM_SM_STATE_WRITE_HEADER;
VoltZdmFromSmStateWriteHeader:
zdmCtx->unprocessedData.len = 0;
if (currentMessageLen == 0)
break;
case VOLT_ZDM_FROM_SM_STATE_WRITE_HEADER:
/* Keep reading lines until we reach BEGIN_BLOCK.
*/
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = VoltSMGetNextBlock (
obj, &(zdmCtx->unprocessedData), currentMessage, currentMessageLen,
1, &bytesRead, &completeFlag, &newLineLen);
if (status != 0)
break;
/* If we didn't complete a block, copy nothing.
*/
if (completeFlag == 0)
break;
currentMessage += bytesRead;
currentMessageLen -= bytesRead;
/* Is this block the one we're looking for? If not, read another
* block.
*/
if (zdmCtx->unprocessedData.len - newLineLen !=
VOLT_SECURE_MAIL_BEGIN_BLOCK_LEN)
goto VoltZdmFromSmStateWriteHeader;
if (Z2Memcmp (
zdmCtx->unprocessedData.data, beginBlk,
VOLT_SECURE_MAIL_BEGIN_BLOCK_LEN) != 0)
goto VoltZdmFromSmStateWriteHeader;
/* This is the block, is there more to read? On the odd chance
* that the last byte of input was the last byte of header, set
* the state to
*/
obj->state = VOLT_ZDM_FROM_SM_STATE_WRITE_HEAD_2;
if (currentMessageLen == 0)
break;
zdmCtx->unprocessedData.len = 0;
obj->state = VOLT_ZDM_FROM_SM_STATE_WRITE_HEADER;
*dataStart = currentMessage;
break;
VoltZdmFromSmStateWriteBody:
case VOLT_ZDM_FROM_SM_STATE_WRITE_BODY:
/* We're reading the body, so the inputData is the beginning of
* the data to copy. Unless the beginning of the inputData is the
* first dash of the first -----END footer.
*/
*dataStart = inputData;
if (inputData[0] != '-')
break;
*dataStart = (unsigned char *)0;
obj->state = VOLT_ZDM_FROM_SM_STATE_WRITE_FOOTER;
}
VOLT_LOG_ERROR_INFO_COMPARE (
status, libCtx, obj, status, 0, errorType,
(char *)0, "ComputeSMStartAddress", fnctLine, (char *)0)
return (status);
}
static int ComputeSMFootAddress (
VoltLibCtx *libCtx,
VoltSecureMailObject *obj,
unsigned char *msgData,
unsigned int msgDataLen,
unsigned char **footStart
)
{
char theDash = '-';
/* If this is ZDM From SecureMail, find the dash
*/
if (obj->formatType == VOLT_MESSAGE_FORMAT_ZDM_MAIL)
{
*footStart = (unsigned char *)Strchr ((char *)msgData, theDash, libCtx);
return (0);
}
/* If this is ZDM from SecureMail, there is never a footer.
*/
*footStart = (unsigned char *)0;
return (0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -