forked from SourMesen/Mesen
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathIpsPatcher.cpp
More file actions
188 lines (164 loc) · 4.87 KB
/
IpsPatcher.cpp
File metadata and controls
188 lines (164 loc) · 4.87 KB
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
#include "stdafx.h"
#include <assert.h>
#include <cstring>
#include <sstream>
#include "IpsPatcher.h"
class IpsRecord
{
public:
uint32_t Address = 0;
uint16_t Length = 0;
vector<uint8_t> Replacement;
//For RLE records (when length == 0)
uint16_t RepeatCount = 0;
uint8_t Value = 0;
bool ReadRecord(std::istream &ipsFile)
{
uint8_t buffer[3];
ipsFile.read((char*)buffer, 3);
if(memcmp(buffer, "EOF", 3) == 0) {
//EOF reached
return false;
} else {
Address = buffer[2] | (buffer[1] << 8) | (buffer[0] << 16);
ipsFile.read((char*)buffer, 2);
Length = buffer[1] | (buffer[0] << 8);
if(Length == 0) {
//RLE record
ipsFile.read((char*)buffer, 3);
RepeatCount = buffer[1] | (buffer[0] << 8);
Value = buffer[2];
} else {
Replacement.resize(Length);
ipsFile.read((char*)Replacement.data(), Length);
}
return true;
}
}
void WriteRecord(vector<uint8_t> &output)
{
output.push_back((Address >> 16) & 0xFF);
output.push_back((Address >> 8) & 0xFF);
output.push_back(Address & 0xFF);
output.push_back((Length >> 8) & 0xFF);
output.push_back(Length & 0xFF);
if(Length == 0) {
output.push_back((RepeatCount >> 8) & 0xFF);
output.push_back(RepeatCount & 0xFF);
output.push_back(Value);
} else {
output.insert(output.end(), Replacement.data(), Replacement.data() + Replacement.size());
}
}
};
bool IpsPatcher::PatchBuffer(string ipsFilepath, vector<uint8_t> &input, vector<uint8_t> &output)
{
ifstream ipsFile(ipsFilepath, std::ios::in | std::ios::binary);
if(ipsFile) {
return PatchBuffer(ipsFile, input, output);
}
return false;
}
bool IpsPatcher::PatchBuffer(vector<uint8_t> &ipsData, vector<uint8_t> &input, vector<uint8_t> &output)
{
std::stringstream ss;
ss.write((char*)ipsData.data(), ipsData.size());
return PatchBuffer(ss, input, output);
}
bool IpsPatcher::PatchBuffer(std::istream &ipsFile, vector<uint8_t> &input, vector<uint8_t> &output)
{
char header[5];
ipsFile.read((char*)&header, 5);
if(memcmp((char*)&header, "PATCH", 5) != 0) {
//Invalid ips file
return false;
}
vector<IpsRecord> records;
int32_t truncateOffset = -1;
size_t maxOutputSize = input.size();
while(!ipsFile.eof()) {
IpsRecord record;
if(record.ReadRecord(ipsFile)) {
if(record.Address + record.Length + record.RepeatCount > maxOutputSize) {
maxOutputSize = record.Address + record.Length + record.RepeatCount;
}
records.push_back(record);
} else {
//EOF, try to read truncate offset record if it exists
uint8_t buffer[3];
ipsFile.read((char*)buffer, 3);
if(!ipsFile.eof()) {
truncateOffset = buffer[2] | (buffer[1] << 8) | (buffer[0] << 16);
}
break;
}
}
output.resize(maxOutputSize);
std::copy(input.begin(), input.end(), output.begin());
for(IpsRecord record : records) {
if(record.Length == 0) {
std::fill(&output[record.Address], &output[record.Address]+record.RepeatCount, record.Value);
} else {
std::copy(record.Replacement.begin(), record.Replacement.end(), output.begin()+record.Address);
}
}
if(truncateOffset != -1 && (int32_t)output.size() > truncateOffset) {
output.resize(truncateOffset);
}
return true;
}
vector<uint8_t> IpsPatcher::CreatePatch(vector<uint8_t> originalData, vector<uint8_t> newData)
{
assert(originalData.size() == newData.size());
vector<uint8_t> patchFile;
uint8_t header[5] = { 'P', 'A', 'T', 'C', 'H' };
patchFile.insert(patchFile.end(), header, header + sizeof(header));
size_t i = 0, len = originalData.size();
while(i < len) {
while(i < len && originalData[i] == newData[i]) {
i++;
}
if(i < len) {
IpsRecord patchRecord;
uint8_t rleByte = newData[i];
uint8_t rleCount = 0;
bool createRleRecord = false;
patchRecord.Address = (uint32_t)i;
patchRecord.Length = 0;
while(i < len && patchRecord.Length < 65535 && originalData[i] != newData[i]) {
if(newData[i] == rleByte) {
rleCount++;
} else if(createRleRecord) {
break;
} else {
rleByte = newData[i];
rleCount = 1;
}
patchRecord.Length++;
i++;
if((patchRecord.Length == rleCount && rleCount > 3) || rleCount > 13) {
//Making a RLE entry would probably save space, so write the current entry and create a RLE entry after it
if(patchRecord.Length == rleCount) {
//Same character since the start of this entry, make the RLE entry now
createRleRecord = true;
} else {
patchRecord.Length -= rleCount;
i -= rleCount;
break;
}
}
}
if(createRleRecord) {
patchRecord.Length = 0;
patchRecord.RepeatCount = rleCount;
patchRecord.Value = rleByte;
} else {
patchRecord.Replacement = vector<uint8_t>(&newData[patchRecord.Address], &newData[patchRecord.Address + patchRecord.Length]);
}
patchRecord.WriteRecord(patchFile);
}
}
uint8_t endOfFile[3] = { 'E', 'O', 'F' };
patchFile.insert(patchFile.end(), endOfFile, endOfFile + sizeof(endOfFile));
return patchFile;
}