forked from nextgal/pfs_upk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MemoryStream.cpp
135 lines (132 loc) · 2.54 KB
/
MemoryStream.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include<vector>
#include<memory>
#include"MemoryStream.h"
using namespace std;
BYTE* MemoryStream::GetBase()
{
return _base.get();
}
const uint MemoryStream::GetPosition() const
{
return Position;
}
const size_t MemoryStream::GetSize() const
{
return Length;
}
const BYTE* MemoryStream::GetBase() const
{
return _base.get();
}
BYTE* MemoryStream::GetPtr()
{
return _base.get() + Position;
}
const BYTE* MemoryStream::GetPtr() const
{
return _base.get() + Position;
}
void MemoryStream::Close()
{
_base.reset();
}
void MemoryStream::Dispose()
{
Close();
}
long MemoryStream::Seek(unsigned int _offset, int _Mode)
{
switch (_Mode)
{
case MEM_BEG:
{
Position = _offset;
break;
}
case MEM_CUR:
{
Position += _offset;
break;
}
case MEM_END:
{
Position = Length - _offset;
break;
}
default:
{
break;
}
}
return Position;
}
long MemoryStream::Read(vector<BYTE>& _buffer, unsigned int _size)
{
Read(_buffer.data(), _size);
return _size;
}
long MemoryStream::Read(void* _buffer, unsigned int _size)
{
memcpy(_buffer, _base.get() + Position, _size);
Position += _size;
return _size;
}
template<typename _Ty>
void MemoryStream::Write(const vector<_Ty>& _source)
{
Write(_source.data(), _source.size());
}
template<typename _Ty>
void MemoryStream::Write(size_t _Pos, const _Ty* _Src, size_t _Size)
{
if (Position + _Size * sizeof(_Ty) / sizeof(BYTE) > Length) return;
memcpy(_base.get() + _Pos, _Src, _Size * sizeof(_Ty) / sizeof(BYTE));
Position += _Size;
}
template<typename _Ty>
void MemoryStream::Write(const _Ty* _Src, size_t _Size)
{
Write(Position, (BYTE*)_Src, _Size);
}
void MemoryStream::Write(const MemoryStream& _Src)
{
Write(Position, _Src.GetBase(), _Src.Length);
}
void MemoryStream::Write(const MemoryStream* _Src)
{
Write(*(_Src));
}
void MemoryStream::Write(const char* _Src, size_t _Size)
{
Write(Position, (BYTE*)_Src, _Size);
}
void MemoryStream::Write(const unsigned int* _Src, size_t _Size)
{
Write(Position, (BYTE*)_Src, _Size);
}
void MemoryStream::Write(const int _Val, size_t _Size)
{
const BYTE* _Ptr = (BYTE*)&_Val;
for (size_t i = 0; i < _Size; i++)
{
Write(_Ptr, sizeof(BYTE));
}
}
MemoryStream::MemoryStream(BYTE* _Ptr, unsigned int _size)
{
Position = 0;
Length = _size;
_base = std::unique_ptr<BYTE>(_Ptr);
}
MemoryStream::MemoryStream(const vector<BYTE>& _source)
{
std::unique_ptr<BYTE> _real(new BYTE[_source.size()]);
Length = _source.size();
Position = 0;
memcpy(_real.get(), _source.data(), _source.size() * sizeof(BYTE));
_base = std::move(_real);
}
MemoryStream::~MemoryStream()
{
this->Dispose();
}