blob: 28ba75a02cc58440a80743940d192ef9853dd3ff (
plain)
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
|
using System;
using System.IO;
using System.Text;
namespace com.clusterrr.hakchi_gui
{
public static class IpsPatcher
{
public static void Patch(byte[] patch, ref byte[] data)
{
if (Encoding.ASCII.GetString(patch, 0, 5) != "PATCH") throw new Exception("Invalid IPS file");
int pos = 5;
while (pos < data.Length)
{
UInt32 address = (UInt32)(patch[pos + 2] | patch[pos + 1] * 0x100 | patch[pos] * 0x10000);
if (pos + 3 >= patch.Length) break;
UInt16 length = (UInt16)(patch[pos + 4] | patch[pos + 3] * 0x100);
if (pos + 5 >= patch.Length) break;
pos += 5;
if (length > 0)
{
while (length > 0)
{
if (address >= data.Length)
Array.Resize(ref data, data.Length * 2);
data[address] = patch[pos];
address++;
pos++;
length--;
}
}
else
{
length = (UInt16)(patch[pos + 1] | patch[pos] * 0x100);
var b = patch[pos + 2];
while (length > 0)
{
if (address >= data.Length)
Array.Resize(ref data, data.Length * 2);
data[address] = b;
address++;
length--;
}
pos += 3;
}
}
}
public static void Patch(string patchFile, ref byte[] data)
{
Patch(File.ReadAllBytes(patchFile), ref data);
}
public static void Patch(string patchFile, string inFile, string outFile)
{
var patch = File.ReadAllBytes(patchFile);
var data = File.ReadAllBytes(inFile);
Patch(patchFile, ref data);
File.WriteAllBytes(outFile, data);
}
}
}
|