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; } }
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; }
void Random::NextBytes(MemoryData& outData) { byte* buffer = outData.MutableData(); size_t size = outData.Size(); FOR_EACH_SIZE(i, size) { buffer[i] = NextByte(); } }
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); }
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; } }
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; }
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; }
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()); }
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; }