-
Notifications
You must be signed in to change notification settings - Fork 0
/
UPS.cs
256 lines (220 loc) · 8.62 KB
/
UPS.cs
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace Nintenlord.UPSpatcher
{
public class UPSfile
{
public bool ValidPatch { get; private set; }
uint originalFileCRC32;
uint newFileCRC32;
uint patchCRC32;
ulong oldFileSize;
ulong newFileSize;
ulong[] changedOffsets;
byte[][] XORbytes;
public UPSfile(string filePath)
{
List<ulong> changedOffsetsList = new List<ulong>();
List<byte[]> XORbytesList = new List<byte[]>();
ValidPatch = false;
if (!File.Exists(filePath))
return;
byte[] UPSfile;
try
{
var patchFile = File.Open(filePath, FileMode.Open);
if (patchFile.Length == 0) return;
BinaryReader br = new BinaryReader(patchFile);
Debug.Assert(patchFile.Length== br.BaseStream.Length);
UPSfile = br.ReadBytes((int)br.BaseStream.Length);
br.Close();
}
catch (Exception)
{
return;
}
unsafe
{
fixed (byte* UPS_ptr = &UPSfile[0])
{
//header
byte* currentPtr = UPS_ptr;
string header = new string((sbyte*)currentPtr,0,4,Encoding.ASCII);
if (header != "UPS1")
return;
currentPtr += 4;
oldFileSize = Decrypt(¤tPtr);
newFileSize = Decrypt(¤tPtr);
//body
ulong filePosition = 0;
while (currentPtr - UPS_ptr + 1 < UPSfile.Length - 12)
{
filePosition += Decrypt(¤tPtr);
changedOffsetsList.Add(filePosition);
List<byte> newXORdata = new List<byte>();
while (*currentPtr != 0)
{
newXORdata.Add(*(currentPtr++));
}
XORbytesList.Add(newXORdata.ToArray());
filePosition += (ulong)newXORdata.Count + 1;
currentPtr++;
}
//end
originalFileCRC32 = *((uint*)(currentPtr));
newFileCRC32 = *((uint*)(currentPtr + 4));
patchCRC32 = *((uint*)(currentPtr + 8));
}
}
changedOffsets = changedOffsetsList.ToArray();
XORbytes = XORbytesList.ToArray();
if (patchCRC32 != CRC32.crc32_calculate(ToBinary()))
return;
ValidPatch = true;
}
public UPSfile(byte[] originalFile, byte[] newFile)
{
List<ulong> changedOffsetsList = new List<ulong>();
List<byte[]> XORbytesList = new List<byte[]>();
ValidPatch = true;
oldFileSize = (ulong)originalFile.Length;
newFileSize = (ulong)newFile.Length;
ulong maxSize;
if (oldFileSize > newFileSize)
maxSize = oldFileSize;
else
maxSize = newFileSize;
for (ulong i = 0; i < maxSize; i++)
{
byte x = i < oldFileSize ? originalFile[i] : (byte)0x00;
byte y = i < newFileSize ? newFile[i] : (byte)0x00;
if (x != y)
{
changedOffsetsList.Add((ulong)i);
List<byte> newXORbytes = new List<byte>();
while (x != y && i < maxSize)
{
newXORbytes.Add((byte)(x ^ y));
i++;
x = i < oldFileSize ? originalFile[i] : (byte)0x00;
y = i < newFileSize ? newFile[i] : (byte)0x00;
}
XORbytesList.Add(newXORbytes.ToArray());
}
}
originalFileCRC32 = CRC32.crc32_calculate(originalFile);
newFileCRC32 = CRC32.crc32_calculate(newFile);
changedOffsets = changedOffsetsList.ToArray();
XORbytes = XORbytesList.ToArray();
patchCRC32 = CRC32.crc32_calculate(ToBinary());
}
static byte[] Encrypt(ulong offset)
{
List<byte> bytes = new List<byte>(8);
ulong x = offset & 0x7f;
offset >>= 7;
while (offset != 0)
{
bytes.Add((byte)x);
offset--;
x = offset & 0x7f;
offset >>= 7;
}
bytes.Add((byte)(0x80 | x));
return bytes.ToArray();
}
unsafe static ulong Decrypt(byte** pointer)
{
ulong value = 0;
int shift = 1;
byte x = *((*pointer)++);
value += (ulong)((x & 0x7F) * shift);
while ((x & 0x80) == 0)
{
shift <<= 7;
value += (ulong)shift;
x = *((*pointer)++);
value += (ulong)((x & 0x7F) * shift);
}
return value;
}
public bool ValidToApply(byte[] file)
{
uint fileCRC32 = CRC32.crc32_calculate(file);
bool fitsAsOld = oldFileSize == (ulong)file.Length && fileCRC32 == originalFileCRC32;
bool fitsAsNew = newFileSize == (ulong)file.Length && fileCRC32 == newFileCRC32;
return ValidPatch && (fitsAsOld || fitsAsNew);
}
public unsafe byte[] Apply(byte[] file)
{
ulong length = (ulong)file.LongLength;
if (length < newFileSize)
length = newFileSize;
byte[] result = new byte[length];
fixed (byte* resultPtr = &result[0])
{
Marshal.Copy(file, 0, new IntPtr(resultPtr), Math.Min(file.Length, result.Length));
//int index = file.Length;
//while (index < result.Length)
// resultPtr[index++] = 0;
for (int i = 0; i < changedOffsets.LongLength; i++)
for (ulong u = 0; u < (ulong)XORbytes[i].LongLength; u++)
resultPtr[changedOffsets[i] + u] ^= XORbytes[i][u];
}
return result;
}
public byte[] Apply(string path)
{
if (!ValidPatch || !File.Exists(path))
return null;
BinaryReader br = new BinaryReader(File.Open(path, FileMode.Open));
byte[] file = br.ReadBytes((int)br.BaseStream.Length);
br.Close();
return Apply(file);
}
private byte[] ToBinary()
{
List<byte> file = new List<byte>();
file.Add((byte)'U');
file.Add((byte)'P');
file.Add((byte)'S');
file.Add((byte)'1');
file.AddRange(Encrypt(oldFileSize));
file.AddRange(Encrypt(newFileSize));
for (int i = 0; i < changedOffsets.LongLength; i++)
{
ulong relativeOffset = changedOffsets[i];
if (i != 0)
relativeOffset -= changedOffsets[i - 1] + (ulong)XORbytes[i - 1].Length + 1;
file.AddRange(Encrypt(relativeOffset));
file.AddRange(XORbytes[i]);
file.Add(0);
}
file.AddRange(BitConverter.GetBytes(originalFileCRC32));
file.AddRange(BitConverter.GetBytes(newFileCRC32));
return file.ToArray();
}
public void writeToFile(string path)
{
BinaryWriter bw = new BinaryWriter(File.Open(path, FileMode.Create));
byte[] file = ToBinary();
bw.Write(file);
bw.Write(CRC32.crc32_calculate(file));
bw.Close();
}
public int[,] getData()
{
int[,] result = new int[changedOffsets.Length, 2];
for (int i = 0; i < changedOffsets.Length; i++)
{
result[i, 0] = (int)changedOffsets[i];
result[i, 1] = XORbytes[i].Length;
}
return result;
}
}
}