Skip to content

Commit d7b5349

Browse files
committed
OrcLib: CacheStream: use reference for underlying stream
1 parent 667deac commit d7b5349

File tree

6 files changed

+33
-30
lines changed

6 files changed

+33
-30
lines changed

src/OrcLib/Authenticode.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,8 @@ HRESULT Authenticode::Verify(LPCWSTR szFileName, const std::shared_ptr<ByteStrea
665665
HRESULT hr = E_FAIL;
666666
std::error_code ec;
667667

668-
PeParser pe(std::make_shared<CacheStream>(pStream), ec);
668+
CacheStream cache(*pStream, 1048576);
669+
PeParser pe(cache, ec);
669670
if (ec)
670671
{
671672
Log::Debug(L"Failed to parse pe file (filename: {}) [{}]", szFileName, ec);

src/OrcLib/CacheStream.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ using namespace std::string_view_literals;
1414

1515
namespace Orc {
1616

17-
CacheStream::CacheStream(std::shared_ptr<ByteStream> stream, size_t cacheSize)
17+
CacheStream::CacheStream(ByteStream& stream, size_t cacheSize)
1818
: ByteStream()
19-
, m_stream(std::move(stream))
19+
, m_stream(stream)
2020
, m_streamOffset(0)
2121
, m_offset(0)
2222
, m_cache()
@@ -27,14 +27,14 @@ CacheStream::CacheStream(std::shared_ptr<ByteStream> stream, size_t cacheSize)
2727
m_cache.resize(cacheSize);
2828
}
2929

30-
STDMETHODIMP Orc::CacheStream::Clone(std::shared_ptr<ByteStream>& clone)
30+
STDMETHODIMP Orc::CacheStream::Clone(ByteStream& clone)
3131
{
3232
return E_NOTIMPL;
3333
}
3434

3535
HRESULT CacheStream::Close()
3636
{
37-
HRESULT hr = m_stream->Close();
37+
HRESULT hr = m_stream.Close();
3838
if (FAILED(hr))
3939
{
4040
Log::Debug("Failed to close underlying cached stream [{}]", SystemError(hr));
@@ -59,7 +59,7 @@ HRESULT CacheStream::Duplicate(const CacheStream& other)
5959

6060
HRESULT CacheStream::Open()
6161
{
62-
HRESULT hr = m_stream->IsOpen();
62+
HRESULT hr = m_stream.IsOpen();
6363
if (hr != S_OK)
6464
{
6565
Log::Debug("Failed to open CacheStream: underlying stream is closed [{}]", SystemError(hr));
@@ -107,7 +107,7 @@ HRESULT CacheStream::Read_(
107107
else
108108
{
109109
ULONGLONG streamRead = 0;
110-
hr = m_stream->Read(m_cache.data(), m_cache.size(), &streamRead);
110+
hr = m_stream.Read(m_cache.data(), m_cache.size(), &streamRead);
111111
if (FAILED(hr))
112112
{
113113
return hr;
@@ -149,7 +149,7 @@ CacheStream::SetFilePointer(__in LONGLONG DistanceToMove, __in DWORD dwMoveMetho
149149
pCurrPointer = &newOffset;
150150
}
151151

152-
HRESULT hr = m_stream->SetFilePointer(DistanceToMove, dwMoveMethod, pCurrPointer);
152+
HRESULT hr = m_stream.SetFilePointer(DistanceToMove, dwMoveMethod, pCurrPointer);
153153
if (FAILED(hr))
154154
{
155155
return hr;
@@ -162,7 +162,7 @@ CacheStream::SetFilePointer(__in LONGLONG DistanceToMove, __in DWORD dwMoveMetho
162162

163163
ULONG64 CacheStream::GetSize()
164164
{
165-
return m_stream->GetSize();
165+
return m_stream.GetSize();
166166
}
167167

168168
HRESULT CacheStream::SetSize(ULONG64 ullNewSize)

src/OrcLib/CacheStream.h

+8-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
#include "OrcLib.h"
1212

13-
#include "MemoryStream.h"
13+
#include "ByteStream.h"
14+
#include "Utils/MetaPtr.h"
1415

1516
#pragma managed(push, off)
1617

@@ -21,17 +22,17 @@ class CBinaryBuffer;
2122
class CacheStream : public ByteStream
2223
{
2324
public:
24-
CacheStream(std::shared_ptr<ByteStream> stream, size_t cacheSize = 1048576);
25+
CacheStream(ByteStream& stream, size_t cacheSize);
2526
~CacheStream();
2627
HRESULT Open();
2728

2829
void Accept(ByteStreamVisitor& visitor) override { return visitor.Visit(*this); };
2930

30-
STDMETHOD(IsOpen)() { return m_stream->IsOpen(); };
31+
STDMETHOD(IsOpen)() { return m_stream.IsOpen(); };
3132

32-
STDMETHOD(CanRead)() { return m_stream->CanRead(); };
33+
STDMETHOD(CanRead)() { return m_stream.CanRead(); };
3334
STDMETHOD(CanWrite)() { return S_FALSE; };
34-
STDMETHOD(CanSeek)() { return m_stream->CanSeek(); };
35+
STDMETHOD(CanSeek)() { return m_stream.CanSeek(); };
3536

3637
//
3738
// CByteStream implementation
@@ -52,13 +53,13 @@ class CacheStream : public ByteStream
5253
STDMETHOD_(ULONG64, GetSize)();
5354
STDMETHOD(SetSize)(ULONG64 ullSize);
5455

55-
STDMETHOD(Clone)(std::shared_ptr<ByteStream>& clone);
56+
STDMETHOD(Clone)(ByteStream& clone);
5657
STDMETHOD(Close)();
5758

5859
HRESULT Duplicate(const CacheStream& other);
5960

6061
private:
61-
std::shared_ptr<ByteStream> m_stream;
62+
ByteStream& m_stream;
6263
uint64_t m_streamOffset;
6364
uint64_t m_offset;
6465
std::vector<uint8_t> m_cache;

src/OrcLib/FileFormat/PeParser.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -193,23 +193,23 @@ void ParseImageSections(
193193

194194
namespace Orc {
195195

196-
PeParser::PeParser(std::shared_ptr<ByteStream> stream, std::error_code& ec)
197-
: m_stream(std::move(stream))
196+
PeParser::PeParser(ByteStream& stream, std::error_code& ec)
197+
: m_stream(stream)
198198
{
199-
::ParseImageDosHeader(*m_stream, m_imageDosHeader, ec);
199+
::ParseImageDosHeader(m_stream, m_imageDosHeader, ec);
200200
if (ec)
201201
{
202202
return;
203203
}
204204

205205
::ParseImageNtHeader(
206-
*m_stream, m_imageDosHeader, m_imageNtHeader, m_imageOptionalHeaders32, m_imageOptionalHeaders64, ec);
206+
m_stream, m_imageDosHeader, m_imageNtHeader, m_imageOptionalHeaders32, m_imageOptionalHeaders64, ec);
207207
if (ec)
208208
{
209209
return;
210210
}
211211

212-
::ParseImageSections(*m_stream, m_imageNtHeader, m_imageSectionsHeaders, ec);
212+
::ParseImageSections(m_stream, m_imageNtHeader, m_imageSectionsHeaders, ec);
213213
if (ec)
214214
{
215215
return;
@@ -313,7 +313,7 @@ void PeParser::ReadDirectory(uint8_t index, std::vector<uint8_t>& buffer, std::e
313313
}
314314

315315
buffer.resize(directory.Size);
316-
ReadChunkAt(*m_stream, directory.VirtualAddress, buffer, ec);
316+
ReadChunkAt(m_stream, directory.VirtualAddress, buffer, ec);
317317
if (ec)
318318
{
319319
Log::Debug("Failed to read directory (index: {}) [{}]", index, ec);
@@ -403,7 +403,7 @@ void PeParser::GetHashedChunks(PeChunks& chunks, std::error_code& ec) const
403403
}
404404

405405
chunks[3].offset = GetSizeOfOptionalHeaders();
406-
chunks[3].length = m_stream->GetSize() - chunks[3].offset - secdir.Size;
406+
chunks[3].length = m_stream.GetSize() - chunks[3].offset - secdir.Size;
407407
}
408408

409409
void PeParser::Hash(CryptoHashStreamAlgorithm algorithms, const PeChunks& chunks, PeHash& output, std::error_code& ec)
@@ -418,7 +418,7 @@ void PeParser::Hash(CryptoHashStreamAlgorithm algorithms, const PeChunks& chunks
418418
return;
419419
}
420420

421-
const auto written = ::CopyChunks(chunks, *m_stream, *hashstream, ec);
421+
const auto written = ::CopyChunks(chunks, m_stream, *hashstream, ec);
422422
if (ec)
423423
{
424424
Log::Debug("Failed to hash chunks [{}]", ec);
@@ -427,7 +427,7 @@ void PeParser::Hash(CryptoHashStreamAlgorithm algorithms, const PeChunks& chunks
427427

428428
// MS does zero padding for PEs that are not 8 modulo (often with catalogs)
429429
const uint8_t padding[8] = {0, 0, 0, 0, 0, 0, 0, 0};
430-
const auto alignment = m_stream->GetSize() % sizeof(padding);
430+
const auto alignment = m_stream.GetSize() % sizeof(padding);
431431
if (alignment != 0)
432432
{
433433
const auto paddingLength = sizeof(padding) - alignment;

src/OrcLib/FileFormat/PeParser.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class PeParser
4444
using PeChunks = std::array<PeChunk, 4>;
4545
using SectionHeaders = fmt::basic_memory_buffer<IMAGE_SECTION_HEADER, 16>;
4646

47-
PeParser(std::shared_ptr<ByteStream> stream, std::error_code& ec);
47+
PeParser(ByteStream& stream, std::error_code& ec);
4848

4949
bool HasDebugDirectory() const;
5050
void ReadDebugDirectory(std::vector<uint8_t>& buffer, std::error_code& ec) const;
@@ -66,7 +66,7 @@ class PeParser
6666
void Hash(CryptoHashStreamAlgorithm algorithms, const PeChunks& chunks, PeHash& output, std::error_code& ec) const;
6767

6868
private:
69-
mutable std::shared_ptr<ByteStream> m_stream;
69+
ByteStream& m_stream;
7070
IMAGE_DOS_HEADER m_imageDosHeader;
7171
ImageNtHeader m_imageNtHeader;
7272
std::optional<IMAGE_OPTIONAL_HEADER32> m_imageOptionalHeaders32;

src/OrcLib/PEInfo.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ HRESULT PEInfo::OpenVersionInformation()
307307
return E_POINTER;
308308
}
309309

310-
auto stream = std::make_shared<CacheStream>(directStream, 4096);
310+
auto stream = std::make_shared<CacheStream>(*directStream, 4096);
311311

312312
ULONGLONG ullBytesRead;
313313
size_t rsrc_rsrc_offset = 0;
@@ -648,7 +648,7 @@ HRESULT PEInfo::OpenAllHash(Intentions localIntentions)
648648
// Pe Hash
649649
{
650650
std::error_code ec;
651-
PeParser pe(memstream, ec);
651+
PeParser pe(*memstream, ec);
652652
if (ec)
653653
{
654654
Log::Debug(L"Failed to parse pe hash '{}' [{}]", m_FileInfo.m_szFullName, ec);
@@ -723,7 +723,8 @@ HRESULT PEInfo::OpenPeHash(Intentions localIntentions)
723723
return E_POINTER;
724724

725725
std::error_code ec;
726-
PeParser pe(std::make_shared<CacheStream>(std::move(stream)), ec);
726+
CacheStream cache(*stream, 1048576);
727+
PeParser pe(cache, ec);
727728
if (ec)
728729
{
729730
Log::Error(L"Failed to parse PE '{}' [{}]", m_FileInfo.m_szFullName, ec);

0 commit comments

Comments
 (0)