📄 codersize.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 "base64.h"
#include "errorctx.h"
int VoltCopyGetEncodeDecodeSize (
VtAlgorithmObject coder,
VtRandomObject random,
unsigned int flagEncodeDecode,
VoltEncodeDecodeSizeInfo *encodeDecodeSizeInfo
)
{
VOLT_DECLARE_FNCT_LINE (fnctLine)
encodeDecodeSizeInfo->processedDataLen =
encodeDecodeSizeInfo->dataToProcessLen;
VOLT_SET_FNCT_LINE (fnctLine)
VOLT_LOG_ERROR_INFO (
0, coder, VT_ERROR_BUFFER_TOO_SMALL, 0, VT_ERROR_TYPE_PRIMARY,
(char *)0, "VoltCopyGetEncodeDecodeSize", fnctLine, (char *)0)
return (VT_ERROR_BUFFER_TOO_SMALL);
}
int VoltBase64GetEncodeDecodeSize (
VtAlgorithmObject coder,
VtRandomObject random,
unsigned int flagEncodeDecode,
VoltEncodeDecodeSizeInfo *encodeDecodeSizeInfo
)
{
int status;
#if VT_64_BIT_LENGTH == 64
VtUInt64 totalLen, blockCount, extra, padLen;
#else
unsigned int totalLen, blockCount, extra, padLen;
#endif
VoltAlgorithmObject *obj = (VoltAlgorithmObject *)coder;
VoltEncodeClassCtx *encodeCtx = (VoltEncodeClassCtx *)(obj->classCtx);
VoltBase64Ctx *base64Ctx = (VoltBase64Ctx *)(encodeCtx->localEncodeCtx);
VOLT_DECLARE_FNCT_LINE (fnctLine)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_BUFFER_TOO_SMALL;
switch (flagEncodeDecode)
{
default:
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_CALL_ORDER;
break;
case VOLT_CALLER_ENCODE_UPDATE:
/* For every binary block of input, there will be one base64
* block.
*/
#if VT_64_BIT_LENGTH == 64
totalLen =
encodeDecodeSizeInfo->dataToProcessLen +
(VtUInt64)(encodeCtx->unprocessedDataLen);
blockCount = totalLen / (VtUInt64)(encodeCtx->plainBlockSize);
encodeDecodeSizeInfo->processedDataLen =
blockCount * (VtUInt64)(encodeCtx->codedBlockSize);
#else
totalLen =
encodeDecodeSizeInfo->dataToProcessLen +
encodeCtx->unprocessedDataLen;
blockCount = totalLen / encodeCtx->plainBlockSize;
encodeDecodeSizeInfo->processedDataLen =
blockCount * encodeCtx->codedBlockSize;
#endif
break;
case VOLT_CALLER_ENCODE_FINAL:
/* For every binary block of input, there will be one base64
* block.
*/
#if VT_64_BIT_LENGTH == 64
totalLen =
encodeDecodeSizeInfo->dataToProcessLen +
(VtUInt64)(encodeCtx->unprocessedDataLen);
blockCount = totalLen / (VtUInt64)(encodeCtx->plainBlockSize);
encodeDecodeSizeInfo->processedDataLen =
blockCount * (VtUInt64)(encodeCtx->codedBlockSize);
#else
totalLen =
encodeDecodeSizeInfo->dataToProcessLen +
encodeCtx->unprocessedDataLen;
blockCount = totalLen / encodeCtx->plainBlockSize;
encodeDecodeSizeInfo->processedDataLen =
blockCount * encodeCtx->codedBlockSize;
#endif
/* For any extra left over, we'll "pad" to a multiple of three
* and the final output will be 4 bytes for every three.
* Then we'll add the required number of "=" characters.
* We'll also need the line feed at the end.
*/
#if VT_64_BIT_LENGTH == 64
extra = totalLen - (blockCount * (VtUInt64)(encodeCtx->plainBlockSize));
#else
extra = totalLen - (blockCount * encodeCtx->plainBlockSize);
#endif
if (extra == 0)
break;
padLen = extra % 3;
if (padLen != 0)
padLen = 3 - padLen;
extra += padLen;
extra = (extra / 3) * 4;
#if VT_64_BIT_LENGTH == 64
encodeDecodeSizeInfo->processedDataLen +=
(extra + (VtUInt64)(base64Ctx->newLineLen));
#else
encodeDecodeSizeInfo->processedDataLen +=
(extra + base64Ctx->newLineLen);
#endif
break;
case VOLT_CALLER_DECODE_UPDATE:
/* For every 4 bytes of input, there are 3 bytes of output.
* Actually, there are new line characters that should not be
* considered part of the total length, however, we're not going
* to search for them, so we'll use 3/4 * dataToProcessLen. This
* will produce a number that may be too large.
*/
#if VT_64_BIT_LENGTH == 64
totalLen =
encodeDecodeSizeInfo->dataToProcessLen +
(VtUInt64)(base64Ctx->converterLen);
#else
totalLen =
encodeDecodeSizeInfo->dataToProcessLen + base64Ctx->converterLen;
#endif
encodeDecodeSizeInfo->processedDataLen = (totalLen / 4) * 3;
break;
case VOLT_CALLER_DECODE_FINAL:
/* For every 4 bytes of input, there are 3 bytes of output.
* Actually, there are new line characters that should not be
* considered part of the total length, however, we're not going
* to search for them, so we'll use 3/4 * dataToProcessLen. This
* will produce a number that may be too large.
* For Final, the total length of valid characters must be a
* multiple of 4. If not, there will be an error (if checking).
* But we don't know yet how many of the characters are invalid,
* so we'll use the same formula.
* If there are any leftovers, we may want to deal with them, so
* find a "padded" length (the + 3).
*/
#if VT_64_BIT_LENGTH == 64
totalLen =
encodeDecodeSizeInfo->dataToProcessLen +
(VtUInt64)(base64Ctx->converterLen);
#else
totalLen =
encodeDecodeSizeInfo->dataToProcessLen + base64Ctx->converterLen;
#endif
encodeDecodeSizeInfo->processedDataLen = ((totalLen + 3) / 4) * 3;
}
VOLT_LOG_ERROR_INFO (
0, coder, status, 0, VT_ERROR_TYPE_PRIMARY,
(char *)0, "VoltBase64GetEncodeDecodeSize", fnctLine, (char *)0)
return (status);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -