Ejemplo n.º 1
0
bool IAudio::Upload(const MemoryData& data)
{
	AudioFormat format = AudioDevice::Instance().GetAudioFormat(mChannelCount, mBitsPerSample);
	GenerateBuffer();
	AudioDevice::Instance().LoadBufferData(mBuffer, format, data.Data(), (uint)data.ByteSize(), mSampleRate);
	return true;
}
Ejemplo n.º 2
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;
	}
Ejemplo n.º 3
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;
}
Ejemplo n.º 4
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;
}
Ejemplo n.º 5
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());
}
Ejemplo n.º 6
0
void NetworkBuffer::Write(const MemoryData& val)
{
	EnsureWritableCount(val.Size());
	Memory::SafeCopy(WriteBegin(), WritableCount(), val.Data(), val.Size());
	HasWritten(val.Size());
}
Ejemplo n.º 7
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;
}