📄 sfwrite.c
字号:
/* Copyright 2003-2006, Voltage Security, all rights reserved.
*/
#include "vibe.h"
#include "environment.h"
#include "base.h"
#include "libctx.h"
#include "securemail.h"
#include "algobj.h"
#include "encode.h"
#include "errorctx.h"
/* Create the AlgorithmObject that does not actually encode/decode, but
* copies.
*/
int VOLT_CALLING_CONV CreateEncodeDecodeCopyObject VOLT_PROTO_LIST ((
VtLibCtx libraryCtx,
VtAlgorithmImpl AlgorithmImpl,
Pointer associatedInfo,
VtAlgorithmObject *algObj
));
int VOLT_CALLING_CONV DestroyEncodeDecodeCopyObject VOLT_PROTO_LIST ((
VtAlgorithmObject *algObj
));
/* Implements VEncodeInit.
*/
int VOLT_CALLING_CONV CopyEncodeInit VOLT_PROTO_LIST ((
VoltAlgorithmObject *obj
));
/* Implements VGetEncodeOutputSize.
*/
int VOLT_CALLING_CONV CopyGetOutputSize VOLT_PROTO_LIST ((
VoltAlgorithmObject *obj,
unsigned int callFlag,
unsigned char *input,
unsigned int inputLen,
unsigned int *outputSize,
VtRandomObject random
));
/* Implements VEncodeUpdate.
*/
int VOLT_CALLING_CONV CopyEncodeUpdate VOLT_PROTO_LIST ((
VoltAlgorithmObject *obj,
VtRandomObject random,
unsigned char *dataToEncode,
unsigned int dataToEncodeLen,
unsigned char *encoding,
unsigned int *encodingLen
));
/* Implements VEncodeFinal.
*/
int VOLT_CALLING_CONV CopyEncodeFinal VOLT_PROTO_LIST ((
VoltAlgorithmObject *obj,
VtRandomObject random,
unsigned char *encoding,
unsigned int *encodingLen
));
int VoltSecureFileWriteInit (
VtSecureMailObject secureMailObj,
VtPolicyCtx policyCtx,
VtStorageCtx storageCtx,
VtTransportCtx transportCtx,
VtRandomObject random
)
{
int status;
unsigned int index, elementLen, newLineLen;
VoltSecureMailObject *obj = (VoltSecureMailObject *)secureMailObj;
VoltSecureMailWriteCtx *writeCtx = (VoltSecureMailWriteCtx *)(obj->localCtx);
VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
VtItem *beginMsg = &(writeCtx->itemArray[VOLT_WRITE_SM_ITEM_BEGIN_MSG]);
unsigned char *newLine =
writeCtx->itemArray[VOLT_WRITE_SM_ITEM_NEW_LINE].data;
VOLT_DECLARE_FNCT_LINE (fnctLine)
newLineLen = writeCtx->itemArray[VOLT_WRITE_SM_ITEM_NEW_LINE].len;
do
{
/* Now that we know what the new line will be, place it at the end
* of the BeginMsg.
*/
Z2Memcpy (beginMsg->data + beginMsg->len, newLine, newLineLen);
if (beginMsg->len != 0)
beginMsg->len += newLineLen;
/* Create the Base64 object. Actually, create a "dummy" base 64
* object, one that does no encoding or decoding.
*/
if (obj->base64 != (VtAlgorithmObject)0)
VtDestroyAlgorithmObject (&(obj->base64));
VOLT_SET_FNCT_LINE (fnctLine)
status = CreateEncodeDecodeCopyObject (
(VtLibCtx)libCtx, VtAlgorithmImplCopy, (Pointer)0, &(obj->base64));
if (status != 0)
break;
/* Init the subordinate objects.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VtPkcs7WriteInit (
obj->p7SignedData, policyCtx, storageCtx, transportCtx, random);
if (status != 0)
break;
VOLT_SET_FNCT_LINE (fnctLine)
status = VtPkcs7WriteInit (
obj->p7EnvelopedData, policyCtx, storageCtx, transportCtx, random);
if (status != 0)
break;
VOLT_SET_FNCT_LINE (fnctLine)
status = VtEncodeInit (obj->base64);
if (status != 0)
break;
/* How much header and footer data will we be writing out?
*/
for (index = VOLT_WRITE_SM_HEAD_INDEX_START;
index <= VOLT_WRITE_SM_HEAD_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)
elementLen += newLineLen;
writeCtx->prelimLen += elementLen;
}
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)
elementLen += newLineLen;
writeCtx->trailLen += elementLen;
}
obj->state = VOLT_SECURE_MAIL_STATE_WRITE_INIT;
} while (0);
VOLT_LOG_ERROR_INFO_COMPARE (
status, 0, secureMailObj, status, 0, 0,
(char *)0, "VoltSecureFileWriteInit", fnctLine, (char *)0)
return (status);
}
int VtAlgorithmImplCopy (
VtAlgorithmObject *object,
Pointer info,
unsigned int flag
)
{
int status;
unsigned int bufferSize;
VoltAlgorithmObject *obj = (VoltAlgorithmObject *)(*object);
VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
VoltEncodeClassCtx *encodeCtx;
unsigned char *buffer = (unsigned char *)0;
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
/* Check the flag, it should be VOLT_ALG_SET_TYPE_FLAG.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_TYPE;
if (flag != VOLT_ALG_SET_TYPE_FLAG)
break;
/* The associated info should be a NULL pointer.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_ASSOCIATED_INFO;
if (info != (Pointer)0)
break;
/* Check the class of the object. It should be 0 (not yet set)
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_TYPE;
if (obj->algClass != 0)
break;
/* Allocate enough space for an EncodeCtx.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_MEMORY;
bufferSize = sizeof (VoltEncodeClassCtx);
buffer = (unsigned char *)Z2Malloc (bufferSize, VOLT_MEMORY_SENSITIVE);
if (buffer == (unsigned char *)0)
break;
Z2Memset (buffer, 0, bufferSize);
/* Locate the contexts.
*/
encodeCtx = (VoltEncodeClassCtx *)buffer;
/* Fill in the fields.
* We'll set the blockSize's at Init.
*/
encodeCtx->GetEncodeOutputSize = CopyGetOutputSize;
encodeCtx->EncodeInit = CopyEncodeInit;
encodeCtx->EncodeUpdate = CopyEncodeUpdate;
encodeCtx->EncodeFinal = CopyEncodeFinal;
encodeCtx->DecodeInit = CopyEncodeInit;
encodeCtx->DecodeUpdate = CopyEncodeUpdate;
encodeCtx->DecodeFinal = CopyEncodeFinal;
obj->algClass = VOLT_CLASS_ENCODE;
obj->classCtx = (Pointer)encodeCtx;
obj->ClassCtxDestroy = VoltDefaultCtxDestroy;
status = 0;
} while (0);
/* If everything works, return 0.
*/
if (status == 0)
return (0);
/* If something went wrong free up what we allocated.
*/
if (buffer != (unsigned char *)0)
Z2Free (buffer);
VOLT_LOG_ERROR_INFO (
0, *object, status, 0, VT_ERROR_TYPE_PRIMARY,
(char *)0, "VtAlgorithmImplCopy", fnctLine, (char *)0)
return (status);
}
int CopyGetOutputSize (
VoltAlgorithmObject *obj,
unsigned int callFlag,
unsigned char *input,
unsigned int inputLen,
unsigned int *outputSize,
VtRandomObject random
)
{
*outputSize = inputLen;
return (0);
}
int CopyEncodeInit (
VoltAlgorithmObject *obj
)
{
VoltEncodeClassCtx *encodeCtx = (VoltEncodeClassCtx *)(obj->classCtx);
encodeCtx->plainBlockSize = 1;
encodeCtx->codedBlockSize = 1;
return (0);
}
int CopyEncodeUpdate (
VoltAlgorithmObject *obj,
VtRandomObject random,
unsigned char *dataToEncode,
unsigned int dataToEncodeLen,
unsigned char *encoding,
unsigned int *encodingLen
)
{
VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
Z2Memcpy (encoding, dataToEncode, dataToEncodeLen);
*encodingLen = dataToEncodeLen;
return (0);
}
int CopyEncodeFinal (
VoltAlgorithmObject *obj,
VtRandomObject random,
unsigned char *decoding,
unsigned int *decodingLen
)
{
*decodingLen = 0;
return (0);
}
int CreateEncodeDecodeCopyObject (
VtLibCtx libraryCtx,
VtAlgorithmImpl AlgorithmImpl,
Pointer associatedInfo,
VtAlgorithmObject *algObj
)
{
int status;
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
/* Create the generic object.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VoltCreateObject (
libraryCtx, (VoltObject **)algObj, sizeof (VoltAlgorithmObject),
VOLT_OBJECT_TYPE_ALGORITHM);
if (status != 0)
break;
/* Fill in further fields.
*/
((VoltAlgorithmObject *)(*algObj))->state = VOLT_STATE_CREATE_ALG_OBJ;
/* An algorithm impl to load?
*/
VOLT_SET_FNCT_LINE (fnctLine)
if (AlgorithmImpl != (VtAlgorithmImpl *)0)
status = AlgorithmImpl (algObj, associatedInfo, VOLT_ALG_SET_TYPE_FLAG);
} while (0);
VOLT_LOG_ERROR_INFO_COMPARE (
status, libraryCtx, 0, status, 0, 0,
(char *)0, "CreateEncodeDecodeCopyObject", fnctLine, (char *)0)
return (status);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -