-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInMemoryInStream.cpp
67 lines (58 loc) · 1.6 KB
/
InMemoryInStream.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "InMemoryInStream.h"
#include <span>
#include <fmt/format.h>
InMemoryInStream::InMemoryInStream(std::span<const std::byte> Data): Data(Data)
{}
InMemoryInStream::~InMemoryInStream() = default;
Z7_COM7F_IMF(InMemoryInStream::Read(void *data, UInt32 size, UInt32 *processedSize))
{
const auto TruncatedNextPosition = std::min(Position + static_cast<std::size_t>(size), Data.size());
const auto TruncatedSize = TruncatedNextPosition - Position;
std::memcpy(data, Data.data() + Position, TruncatedSize);
fmt::print("InMemoryInStream::Read {} of {} bytes at {}.\n", TruncatedSize, size, Position);
Position = TruncatedNextPosition;
if (processedSize)
*processedSize = static_cast<UInt32>(TruncatedSize);
return S_OK;
}
Z7_COM7F_IMF(InMemoryInStream::Seek(Int64 offset, UInt32 seekOrigin, UInt64* newPosition))
{
if (seekOrigin == STREAM_SEEK_SET)
{
if (offset < 0)
{
Position = 0;
return ERROR_NEGATIVE_SEEK;
}
Position = std::min(Data.size(), (size_t)offset);
if (newPosition)
*newPosition = Position;
return S_OK;
}
if (seekOrigin == STREAM_SEEK_CUR)
{
if (-offset > Position)
{
Position = 0;
return ERROR_NEGATIVE_SEEK;
}
Position = std::min(Data.size(), Position + offset);
if (newPosition)
*newPosition = Position;
return S_OK;
}
if (seekOrigin == STREAM_SEEK_END)
{
if (-offset > Data.size())
{
Position = 0;
return ERROR_NEGATIVE_SEEK;
}
std::size_t Last = Data.empty() ? Data.size() - 1 : 0;
Position = std::min(Data.size(), Last + offset);
if (newPosition)
*newPosition = Position;
return S_OK;
}
return STG_E_INVALIDFUNCTION;
}