本文整理汇总了C++中DataChunk::Set方法的典型用法代码示例。如果您正苦于以下问题:C++ DataChunk::Set方法的具体用法?C++ DataChunk::Set怎么用?C++ DataChunk::Set使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DataChunk
的用法示例。
在下文中一共展示了DataChunk::Set方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: WriteCryptoDataTo
/*! \param Buffer Pointer to data to be written
* \param Offset The offset within the KLV value field of the first byte to write
* \param Size The number of bytes to write
* \return Size if all OK, else != Size. This may not equal the actual number of bytes written.
* The IV must have already been set.
* Only encrypted parts of the value may be written using this function (i.e. Offset >= PlaintextOffset)
*/
size_t KLVEObject::WriteCryptoDataTo(const UInt8 *Buffer, Position Offset, size_t Size)
{
// Self-deleting store to allow us to extend working buffer if required
DataChunk TempData;
// Are we going to need to write padding bytes?
bool AddPadding;
if(static_cast<Length>(Offset + Size) >= ValueLength) AddPadding = true; else AddPadding = false;
// Check if this is a "pointless" zero-byte write (rather than a request to write padding only)
if((!AddPadding) && (Size == 0)) return 0;
// Assume that the write will succeed and move the "next" pointer accordingly
CurrentWriteOffset += Size;
// Check if all the bytes will fit in the AwaitingEncryptionBuffer
if((!AddPadding) && (Size < static_cast<size_t>(EncryptionGranularity - AwaitingEncryption)))
{
// Add to the end of the waiting buffer
memcpy(&AwaitingEncryptionBuffer[AwaitingEncryption], Data.Data, Size);
// All done
return Size;
}
// Work out how many bytes we need to encrypt (estimate one - as many as offered)
Length BytesToEncrypt = Size;
// If there are any bytes waiting they need to be added to this write
if(AwaitingEncryption)
{
// Build the full data in the temporary buffer
TempData.ResizeBuffer(AwaitingEncryption + Size);
// Start with "waiting" data
TempData.Set(AwaitingEncryption, AwaitingEncryptionBuffer);
// Copy in the new data
TempData.Append(Size, Buffer);
// Replace the working buffer pointer with a pointer to the temporary buffer
Buffer = TempData.Data;
// Update the offset (move it back to the first waiting byte)
Offset -= AwaitingEncryption;
// Record the revised size
BytesToEncrypt = AwaitingEncryption + Size;
}
// Pad the data if required (i.e. if this is the last chunk of data)
if(AddPadding)
{
if(Offset + BytesToEncrypt > ValueLength)
{
error("Attempted to write beyond the end of the encrypted value in KLVEObject::WriteCryptoDataTo()\n");
return 0;
}
// Start by encrypting all but the last 16 bytes (including padding)
Length StartSize = EncryptedLength - (EncryptionGranularity + Offset);
// Sanity check the size of this chunk
if((sizeof(size_t) < 8) && (StartSize > 0xffffffff))
{
error("Tried to encrypt > 4GBytes, but this platform can only handle <= 4GByte chunks\n");
return 0;
}
// Don't write zero bytes
if(StartSize)
{
// Encrypt by making a copy
DataChunkPtr NewData = Encrypt->Encrypt(static_cast<size_t>(StartSize), Buffer);
if(!NewData) return 0;
// Write the encrypted data
Base_WriteDataTo(NewData->Data, DataOffset + Offset, static_cast<size_t>(StartSize));
// Update the current hash if we are calculating one
// TODO: Sort the possible overflow here
if(WriteHasher) WriteHasher->HashData(static_cast<size_t>(StartSize), NewData->Data);
}
// Buffer for last data to be encrypted
UInt8 TempBuffer[EncryptionGranularity];
// Copy in the remaining bytes from the end of the given buffer
// Add padding bytes in a 16-byte version of the scheme defined in RFC 2898
const UInt8 *pSrc = &Buffer[StartSize];
UInt8 *pDst = TempBuffer;
int i;
int EncData = (int)(BytesToEncrypt - StartSize);
//.........这里部分代码省略.........