コード例 #1
0
ファイル: ZipReader.cpp プロジェクト: xuxiaowei007/Medusa
MemoryData ZipReader::ReadAllData(StringRef fileName)const
{
	MemoryData result;

	if (mZipFile==nullptr||fileName.IsEmpty())
	{
		return result;
	}


	const ZipFileInfo* zipEntryInfo=mFileDict.TryGetByOtherKey(fileName,fileName.HashCode());
	if (zipEntryInfo==nullptr)
	{
		return result;
	}

	int err = unzGoToFilePos(mZipFile, (unz_file_pos*)&zipEntryInfo->Pos);
	if (err!=UNZ_OK)
	{
		return result;
	}

	err = unzOpenCurrentFile(mZipFile);
	if (err!=UNZ_OK)
	{
		return result;
	}

	result=MemoryData::Alloc(zipEntryInfo->UncompressedSize);
	int readSize = unzReadCurrentFile(mZipFile, result.MutableData(), (uint)zipEntryInfo->UncompressedSize);
	Log::Assert(readSize==(int)zipEntryInfo->UncompressedSize,"Invalid zip file size.");	//readSize could be 0 because we may have zero file such as "StringTable-enus.bin"
	unzCloseCurrentFile(mZipFile);

	return result;
}
コード例 #2
0
ファイル: BufferStream.cpp プロジェクト: fjz13/Medusa
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;
		}
	}
コード例 #3
0
ファイル: IAudio.cpp プロジェクト: fjz13/Medusa
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;
}
コード例 #4
0
ファイル: IStream.cpp プロジェクト: fjz13/Medusa
size_t IStream::ReadToStream(size_t size, IStream& dest, size_t bufferSize/*=1024*/)const
{
	RETURN_ZERO_IF_FALSE(CanRead() && dest.CanWrite());

	if (dest.IsPtrAvailable())//could directly write
	{
		dest.ReserveLeftSize(size);
		byte* buffer = dest.MutablePtr();
		MemoryData destBuffer = MemoryData::FromStatic(buffer, size);
		return ReadDataTo(destBuffer, DataReadingMode::AlwaysCopy);
	}
	else
	{
		//should use temp  buffer
		size_t count = 0;
		size_t realBufferSize = Math::Min(LeftLength(), bufferSize, size);

		MemoryData tempBuffer = MemoryData::Alloc(realBufferSize);
		do
		{
			size_t readSize = Math::Min(size, realBufferSize);
			tempBuffer.ForceSetSize(readSize);

			readSize = ReadDataTo(tempBuffer, DataReadingMode::AlwaysCopy);
			BREAK_IF_ZERO(readSize);
			tempBuffer.ForceSetSize(readSize);
			count += dest.WriteData(tempBuffer);
			tempBuffer.ForceSetSize(realBufferSize);
			size -= readSize;
		} while (size > 0);
		return count;
	}

}
コード例 #5
0
ファイル: Random.cpp プロジェクト: xuxiaowei007/Medusa
void Random::NextBytes(MemoryData& outData)
{
	byte* buffer = outData.MutableData();
	size_t size = outData.Size();
	FOR_EACH_SIZE(i, size)
	{
		buffer[i] = NextByte();
	}
}
コード例 #6
0
ファイル: Aes256Encoder.cpp プロジェクト: xuxiaowei007/Medusa
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();
}
コード例 #7
0
u1 MemoryDataArray::getfield(u4 ref, u2 cp_index, u4 *data) {
	MemoryData *instance = (MemoryData *)ref;
	int index = instance->classref->get_field_index(cp_index);
	u1 type = *instance->classref->get_field_type(index);

	if( isStatic( instance->classref->get_field_flags(index) ) ) {
		printf("Error field is static: mem_data_array.getfield\n");
		exit(0);
	}
	instance->get_data(index, data, type);
	return type;
}
コード例 #8
0
ファイル: Aes256Encoder.cpp プロジェクト: xuxiaowei007/Medusa
Aes256Encoder::Aes256Encoder(const MemoryData& key, Random* random /*= nullptr*/)
	: mRandom(random),
	mKey(key.Clone())
{
    uint maxSize=KeyMaxSize;
	Log::AssertFormat(mKey.Size() <= maxSize, "Too bigger key size:{} > Max support:{}", mKey.Size(), maxSize);
}
コード例 #9
0
ファイル: LZMADecoder.cpp プロジェクト: fjz13/Medusa
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;
}
コード例 #10
0
ファイル: BehaviorConfig.cpp プロジェクト: fjz13/Medusa
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;
}
コード例 #11
0
ファイル: Aes256Encoder.cpp プロジェクト: xuxiaowei007/Medusa
void Aes256Encoder::Encrypt(MemoryData& rkey, const MemoryData& key,const MemoryData& salt, unsigned char* buffer)
{
	unsigned char i, rcon;

	copy_key(rkey, key, salt);
	add_round_key(rkey.MutableData(), buffer, 0);
	for (i = 1, rcon = 1; i < RoundCount; ++i)
	{
		sub_bytes(buffer);
		shift_rows(buffer);
		mix_columns(buffer);
		if (!(i & 1))
			expand_enc_key(rkey.MutableData(), &rcon);
		add_round_key(rkey.MutableData(), buffer, i);
	}
	sub_bytes(buffer);
	shift_rows(buffer);
	expand_enc_key(rkey.MutableData(), &rcon);
	add_round_key(rkey.MutableData(), buffer, i);
}
コード例 #12
0
ファイル: System_win.cpp プロジェクト: fjz13/Medusa
static MemoryData GetFontDataHelper(const HFONT fontHandle)
{
	//only work with ttf,but not ttc
	MemoryData result;
	HDC hdc = ::CreateCompatibleDC(NULL);
	if (hdc != NULL)
	{
		::SelectObject(hdc, fontHandle);
		const DWORD size = ::GetFontData(hdc, 0, 0, NULL, 0);
		if (size > 0)
		{
			result = MemoryData::Alloc(size);
			if (::GetFontData(hdc, 0, 0, result.MutableData(), size) != size)
			{
				result = MemoryData::Empty;
			}
		}
		::DeleteDC(hdc);
	}
	return result;
}
コード例 #13
0
ファイル: IStream.cpp プロジェクト: fjz13/Medusa
size_t IStream::CopyTo(IStream& dest, size_t bufferSize/*=1024*/)const
{
	RETURN_ZERO_IF_FALSE(CanRead() && dest.CanWrite());

	size_t realBufferSize = Math::Min(LeftLength(), bufferSize);
	size_t count = 0;

	MemoryData buffer = MemoryData::Alloc(realBufferSize);
	do
	{
		size_t readSize = ReadDataTo(buffer, DataReadingMode::AlwaysCopy);
		BREAK_IF_ZERO(readSize);
		buffer.ForceSetSize(readSize);
		count += dest.WriteData(buffer);
		buffer.ForceSetSize(realBufferSize);
	} while (true);



	return count;
}
コード例 #14
0
ファイル: BlockWriteStream.cpp プロジェクト: fjz13/Medusa
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;
	}
コード例 #15
0
ファイル: BlockReadStream.cpp プロジェクト: fjz13/Medusa
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;
			}
		}
コード例 #16
0
ファイル: File.cpp プロジェクト: fjz13/Medusa
bool File::Copy(StringRef srcFilePath, StringRef destFilePath)
{
	FileStream srcFile;
	FileStream destFile;
	byte buffer[1024];
	MemoryData data = MemoryData::FromStatic(buffer);
	if (srcFile.OpenReadBinary(srcFilePath) && destFile.OpenNewWriteBinary(destFilePath))
	{
		while (true)
		{
			size_t readCount = srcFile.ReadDataTo(data);
			if (readCount == 0)
			{
				return true;
			}
			data.ForceSetSize(readCount);
			destFile.WriteData(data);
		}

	}

	return false;
}
コード例 #17
0
ファイル: IStream.cpp プロジェクト: fjz13/Medusa
MemoryData IStream::ReadData(size_t size, DataReadingMode mode/*=DataReadingMode::AlwaysCopy*/)const
{
	if (LeftLength() >= size)
	{
		if (Ptr() == nullptr)	//not support direct move
		{
			mode = DataReadingMode::AlwaysCopy;
		}

		switch (mode)
		{
		case DataReadingMode::AlwaysCopy:
		{
			MemoryData data = MemoryData::Alloc(size);
			size_t readSize = ReadDataTo(data, mode);
			data.ForceSetSize(readSize);

			return data;
		}
		break;
		case DataReadingMode::DirectMove:
		{
			MemoryData data;
			data.ForceSetSize(size);
			size_t readSize = ReadDataTo(data, mode);
			data.ForceSetSize(readSize);

			return data;
		}
		break;
		default:
			break;
		}

	}
	return MemoryData::Empty;
}
コード例 #18
0
void MemoryDataArray::putfield(u4 ref, u2 cp_index, u4 *data, u1 in_type) {
	MemoryData *instance = (MemoryData *)ref;
	int index = instance->classref->get_field_index(cp_index);
	u1 type = *instance->classref->get_field_type(index);
	
	if(in_type != type) {
		printf("Error data type %c != %c: mem_data_array.putfield\n", type, in_type);
		exit(0);
	}
	if( isStatic( instance->classref->get_field_flags(index) ) ) {
		printf("Error field is static: mem_data_array.putfield\n");
		exit(0);
	}
	instance->put_data(index, data, type);

//#define TEST_PUTFIELD
#ifdef TEST_PUTFIELD
	printf("TEST_PUTFIELD\n")
	printf("addr %p, value %08X\n",instance, ref);
	printf("field index %d\n",index);
	printf("field type %c\n",type);
#endif

}
コード例 #19
0
ファイル: NetworkBuffer.cpp プロジェクト: fjz13/Medusa
NetworkBuffer::NetworkBuffer(const MemoryData& data)
	:mStream(data)
{
	mWriteIndex = data.Size();
}
コード例 #20
0
ファイル: Aes256Encoder.cpp プロジェクト: xuxiaowei007/Medusa
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());
}
コード例 #21
0
ファイル: ZipReader.cpp プロジェクト: xuxiaowei007/Medusa
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;
}
コード例 #22
0
ファイル: NetworkBuffer.cpp プロジェクト: fjz13/Medusa
void NetworkBuffer::Write(const MemoryData& val)
{
	EnsureWritableCount(val.Size());
	Memory::SafeCopy(WriteBegin(), WritableCount(), val.Data(), val.Size());
	HasWritten(val.Size());
}