示例#1
0
size_t BufferStream::ReadDataTo(MemoryData& outData, DataReadingMode mode/*=DataReadingMode::AlwaysCopy*/)const
{
	RETURN_ZERO_IF_FALSE(CanRead());
	FlushOnReadWrite(StreamDataOperation::Read);

	size_t outPos = 0;
	size_t outSize = outData.Size();

	//read left buffer data
	size_t bufferLeftLength = mBufferLength - mBuffer.Position();
	if (bufferLeftLength != 0)
	{
		size_t readSize = Math::Min(bufferLeftLength, outSize);
		MemoryData tempData = MemoryData::FromStatic(outData.MutableData() + outPos, readSize);
		readSize = mBuffer.ReadDataTo(tempData, DataReadingMode::AlwaysCopy);
		outPos += readSize;
		outSize -= readSize;
	}

	//directly read to out data block per block
	size_t blockSize = mBuffer.Length();
	size_t blockCount = outSize / blockSize;
	FOR_EACH_SIZE(i, blockCount)
	{
		MemoryData tempData = MemoryData::FromStatic(outData.MutableData() + outPos, blockSize);
		size_t readSize = mSourceStream->ReadDataTo(tempData);
		outPos += readSize;
		outSize -= readSize;
		if (readSize != blockSize)	//last block
		{
			return outPos;
		}
	}
示例#2
0
void Random::NextBytes(MemoryData& outData)
{
	byte* buffer = outData.MutableData();
	size_t size = outData.Size();
	FOR_EACH_SIZE(i, size)
	{
		buffer[i] = NextByte();
	}
}
示例#3
0
size_t Aes256Encoder::OnCode(const MemoryData& input, MemoryData& output) const
{
	MemoryStream inputStream(input);
	MemoryStream outputStream(output);

	size_t inputLength = input.Size();

	MemoryData salt = MemoryData::Alloc(KeyMaxSize - mKey.Size());
	salt.ClearZero();

	// Generate salt
	if (mRandom!=nullptr)
	{
		mRandom->NextBytes(salt);
	}

	MemoryData rkey = MemoryData::Alloc(KeyMaxSize);

	// Calculate padding
	size_t padding = 0;
	if (inputLength % BlockSize != 0)
		padding = (BlockSize - (inputLength % BlockSize));

	// Add salt
	outputStream.WriteData(salt);

	// Add 1 bytes for padding size
	outputStream.WriteChar((char)(padding & 0xFF));

	// Reset buffer
	byte buffer[3 * BlockSize];
	size_t bufferPos = 0;

	MemoryData bufferData = MemoryData::FromStatic(buffer, BlockSize);
	while (!inputStream.IsEnd())
	{
		bufferPos += inputStream.ReadDataTo(bufferData);

		if (bufferPos == BlockSize)
		{
			Encrypt(rkey, mKey, salt, buffer);
			outputStream.WriteData(bufferData);
			bufferPos = 0;
		}
	}

	if (bufferPos > 0)
	{
		//padding with 0
		Memory::SetZero(buffer + bufferPos, BlockSize - bufferPos);

		Encrypt(rkey,mKey, salt, buffer);
		outputStream.WriteData(bufferData);
	}

	return outputStream.Position();
}
示例#4
0
size_t LZMADecoder::OnCode(const MemoryData& input, MemoryData& output) const
{
	RETURN_ZERO_IF_EMPTY(input);
	const byte* inBuffer = input.Data();
	size_t inSize = input.Size();

	ELzmaStatus outStatus;

	ISzAlloc myAlloc;
	myAlloc.Alloc = LZMAAlloc;
	myAlloc.Free = LZMAFree;

	CLzmaDec p;
	SRes res;
	
	LzmaDec_Construct(&p);

	res = LzmaDec_AllocateProbs(&p, inBuffer, LZMA_PROPS_SIZE, &myAlloc);

	uint64 fileSize = 0;
	for (int i = 0; i < 8; i++)
		fileSize |= ((uint64)inBuffer[LZMA_PROPS_SIZE + i]) << (8 * i);

	if (output.Size()<(size_t)fileSize)
	{
		Log::AssertFailedFormat("output size:{} < expected size{}", output.Size(), fileSize);
		LzmaDec_FreeProbs(&p, &myAlloc);
		return 0;
	}

	LzmaDec_Init(&p);

	p.dic = output.MutableData();
	p.dicBufSize = (size_t)fileSize;

	size_t outSize = inSize - 13;
	res = LzmaDec_DecodeToDic(&p, (size_t)fileSize, inBuffer + 13, &outSize, LZMA_FINISH_ANY, &outStatus);

	if (res == SZ_OK && outStatus == LZMA_STATUS_NEEDS_MORE_INPUT)
		res = SZ_ERROR_INPUT_EOF;

	LzmaDec_FreeProbs(&p, &myAlloc);
	return (size_t)fileSize;
}
示例#5
0
bool BehaviorConfig::LoadFromData(const FileIdRef& fileId, const MemoryData& data, uint format /*= 0*/)
{
	Unload();
	RETURN_FALSE_IF(data.IsNull());

	pugi::xml_document doc;
	pugi::xml_parse_result result = doc.load_buffer(data.Data(), data.Size());
	if (!result)
	{
		Log::AssertFailedFormat("Cannot parse xml:{} because {}", fileId, result.description());
		return false;
	}
	for (const auto& child : doc.first_child().children())
	{
		StringRef typeName = child.name();
		StringRef id = child.attribute("Id").value();
		if (id.IsEmpty())
		{
			id = typeName;
		}

#ifdef MEDUSA_SAFE_CHECK
		if (ContainsId(id))
		{
			Log::AssertFailedFormat("Duplicate id:{} in {}", id.c_str(), typeName.c_str());
		}
#endif

		IBehavior* behavior = BehaviorFactory::Instance().SmartCreate(typeName);
		behavior->LoadFromXmlNode(child);
		behavior->Initialize();
		if (id.EndWith("Behavior"))
		{
			Add(id, behavior);
		}
		else
		{
			Add(id + "Behavior", behavior);
		}
	}


	return true;
}
示例#6
0
size_t BlockWriteStream::WriteData(const MemoryData& data, DataReadingMode mode /*= DataReadingMode::AlwaysCopy*/)
{
	RETURN_ZERO_IF_FALSE(CanWrite());

	size_t dataPos = 0;
	size_t dataSize = data.Size();

	if (mBuffer.Position() > 0)
	{
		size_t bufferLeftLength = mBuffer.LeftLength();
		size_t writeSize = Math::Min(bufferLeftLength, dataSize);
		MemoryData tempData = MemoryData::FromStatic(data.Data(), writeSize);
		writeSize = mBuffer.WriteData(tempData, mode);
		dataPos += writeSize;
		dataSize -= writeSize;

		if (mBuffer.IsEnd())
		{
			WriteCurrentBlock();
			++mBlockIndex;
		}
		else
		{
			//all data write to buffer
			return dataPos;
		}
	}

	//directly write data block per block
	size_t blockSize = mBuffer.Length();
	size_t blockCount = dataSize / blockSize;
	FOR_EACH_SIZE(i, blockCount)
	{
		MemoryData tempData = MemoryData::FromStatic(data.Data() + dataPos, blockSize);
		size_t writeSize = WriteBlock(mBlockIndex, tempData);
		++mBlockIndex;

		dataPos += writeSize;
		dataSize -= writeSize;
	}
示例#7
0
size_t BlockReadStream::ReadDataTo(MemoryData& outData, DataReadingMode mode/*=DataReadingMode::AlwaysCopy*/)const
{
	RETURN_ZERO_IF_FALSE(CanRead());

	size_t outPos = 0;
	size_t outSize = outData.Size();

	//read left buffer data
	size_t bufferLeftLength = mBufferLength - mBuffer.Position();
	if (bufferLeftLength != 0)
	{
		size_t readSize = Math::Min(bufferLeftLength, outSize);
		MemoryData tempData = MemoryData::FromStatic(outData.MutableData(), readSize);
		readSize = mBuffer.ReadDataTo(tempData, DataReadingMode::AlwaysCopy);
		outPos += readSize;
		outSize -= readSize;
	}

	if (outSize > 0)
	{
		mBuffer.Rewind();
		mBufferLength = 0;

		//directly read to out data block per block
		size_t blockSize = mBuffer.Length();
		size_t blockCount = outSize / blockSize;
		FOR_EACH_SIZE(i, blockCount)
		{
			MemoryData tempData = MemoryData::FromStatic(outData.MutableData() + outPos, blockSize);
			MemoryStream tempStream(tempData);
			++mBlockIndex;
			size_t readSize = LoadBlockTo(mBlockIndex, tempStream);
			outPos += readSize;
			outSize -= readSize;
			if (readSize != blockSize)	//reach file end
			{
				return outPos;
			}
		}
示例#8
0
void Aes256Encoder::copy_key(MemoryData& rkey, const MemoryData& key, const MemoryData& salt)
{
	Memory::SafeCopy(rkey.MutableData(), key.Size(), key.Data(), key.Size());
	Memory::SafeCopy(rkey.MutableData() + key.Size(), salt.Size(), salt.Data(), salt.Size());
}
示例#9
0
void NetworkBuffer::Write(const MemoryData& val)
{
	EnsureWritableCount(val.Size());
	Memory::SafeCopy(WriteBegin(), WritableCount(), val.Data(), val.Size());
	HasWritten(val.Size());
}
示例#10
0
NetworkBuffer::NetworkBuffer(const MemoryData& data)
	:mStream(data)
{
	mWriteIndex = data.Size();
}
示例#11
0
MemoryData ZipReader::DecompressGZIP(const MemoryData& data, size_t expectedSize)
{
	MemoryData result = MemoryData::Alloc(expectedSize);
	int ret;
	z_stream strm;

	strm.zalloc = Z_NULL;
	strm.zfree = Z_NULL;
	strm.opaque = Z_NULL;
	strm.next_in = (byte*)data.Data();
	strm.avail_in = (uint)data.Size();
	strm.next_out = result.MutableData();
	strm.avail_out = (uint)result.Size();

	ret = inflateInit2(&strm, 15 + 32);

	if (ret != Z_OK)
	{
		return MemoryData::Empty;
	}

	do
	{
		ret = inflate(&strm, Z_SYNC_FLUSH);

		switch (ret)
		{
			case Z_NEED_DICT:
			case Z_STREAM_ERROR:
				ret = Z_DATA_ERROR;
			case Z_DATA_ERROR:
			case Z_MEM_ERROR:
				inflateEnd(&strm);
				return MemoryData::Empty;
		}

		if (ret != Z_STREAM_END)
		{
			
			byte* newData = (byte *)realloc(result.MutableData(), result.Size() * 2);
			result.ForceSetDataAndSize(newData, result.Size() * 2);

			if (!result.IsValid())
			{
				inflateEnd(&strm);
				return MemoryData::Empty;
			}

			strm.next_out = (Bytef *)(result.Data() + result.Size());
			strm.avail_out = (uint)result.Size();
		}
	} while (ret != Z_STREAM_END);

	if (strm.avail_in != 0)
	{
		return MemoryData::Empty;
	}

	inflateEnd(&strm);

	return result;
}