-
Notifications
You must be signed in to change notification settings - Fork 15
/
rdp_dump.cpp
201 lines (167 loc) · 4.39 KB
/
rdp_dump.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* Copyright (c) 2020 Themaister
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "rdp_dump.hpp"
#include <string.h>
#include <algorithm>
namespace RDP
{
enum class Command : uint32_t
{
Invalid = 0,
UpdateDram = 1,
RDPCommand = 2,
SetVIRegister = 3,
EndFrame = 4,
SignalComplete = 5,
EndOfFile = 6,
UpdateDramFlush = 7,
UpdateHiddenDram = 8,
UpdateHiddenDramFlush = 9
};
bool DumpPlayer::load_dump(const char *path)
{
file.reset(fopen(path, "rb"));
if (!file)
return false;
char header[8];
if (fread(header, 1, 8, file.get()) != 8)
return false;
if (memcmp(header, "RDPDUMP2", 8) != 0)
return false;
uint32_t rdram_size, hidden_dram_size;
if (!read_word(rdram_size) || !read_word(hidden_dram_size))
return false;
if (rdram_size != 4 * 1024 * 1024 && rdram_size != 8 * 1024 * 1024)
return false;
if (hidden_dram_size != 4 * 1024 * 1024)
return false;
rdram_cache.resize(rdram_size);
rdram_hidden_cache.resize(hidden_dram_size);
return true;
}
bool DumpPlayer::rewind()
{
if (fseek(file.get(), 16, SEEK_SET) == 0)
{
std::fill(rdram_cache.begin(), rdram_cache.end(), 0);
std::fill(rdram_hidden_cache.begin(), rdram_hidden_cache.end(), 0);
return true;
}
else
return false;
}
void DumpPlayer::set_command_interface(CommandListenerInterface *iface_)
{
iface = iface_;
}
bool DumpPlayer::iterate()
{
uint32_t command_u32;
if (!read_word(command_u32))
return false;
auto command = static_cast<Command>(command_u32);
switch (command)
{
case Command::EndOfFile:
iface->eof();
return false;
case Command::SetVIRegister:
{
uint32_t index, value;
if (!read_word(index) || !read_word(value))
return false;
iface->set_vi_register(VIRegister(index), value);
break;
}
case Command::RDPCommand:
{
uint32_t cmd_id;
if (!read_word(cmd_id))
return false;
uint32_t word_count;
if (!read_word(word_count))
return false;
command_buffer.resize(word_count);
if (word_count)
{
if (fread(command_buffer.data(), sizeof(uint32_t), word_count, file.get()) != word_count)
return false;
}
iface->command(static_cast<Op>(cmd_id), word_count, command_buffer.data());
break;
}
case Command::EndFrame:
iface->end_frame();
break;
case Command::SignalComplete:
iface->signal_complete();
break;
case Command::UpdateDram:
{
uint32_t offset, size;
if (!read_word(offset) || !read_word(size))
return false;
if (offset + size > rdram_cache.size())
return false;
if (fread(rdram_cache.data() + offset, 1, size, file.get()) != size)
return false;
break;
}
case Command::UpdateHiddenDram:
{
uint32_t offset, size;
if (!read_word(offset) || !read_word(size))
return false;
if (offset + size > rdram_hidden_cache.size())
return false;
if (fread(rdram_hidden_cache.data() + offset, 1, size, file.get()) != size)
return false;
break;
}
case Command::UpdateDramFlush:
{
iface->update_rdram(rdram_cache.data(), rdram_cache.size(), 0);
break;
}
case Command::UpdateHiddenDramFlush:
{
iface->update_hidden_rdram(rdram_hidden_cache.data(), rdram_hidden_cache.size(), 0);
break;
}
default:
return false;
}
return true;
}
bool DumpPlayer::read_word(uint32_t &value)
{
return fread(&value, sizeof(value), 1, file.get()) == 1;
}
size_t DumpPlayer::get_rdram_size() const
{
return rdram_cache.size();
}
size_t DumpPlayer::get_hidden_rdram_size() const
{
return rdram_hidden_cache.size();
}
}