-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpatch.cs
268 lines (255 loc) · 6.91 KB
/
patch.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
257
258
259
260
261
262
263
264
265
266
267
268
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace vdb {
enum action {
CTRL_NEW = 0x10,
CTRL_DFF = 0x20,
CTRL_DFX = 0x30,
CTRL_MOV = 0x40,
CTRL_DEL = 0x50,
PATCH_COPY = 0x01,
PATCH_INSERT = 0x02,
};
abstract class CTRL {
public action act;
public static action readact(byte[] data, ref int off) {
action act = (action)data[off];
off += 1;
return act;
}
public static int read8(byte[] data, ref int off) {
int a = data[off];
off += 1;
return a;
}
public static int read16(byte[] data, ref int off) {
int a = data[off + 0];
int b = data[off + 1];
off += 2;
return b << 8 | a;
}
public static int read32(byte[] data, ref int off) {
int a = data[off++];
int b = data[off++];
int c = data[off++];
int d = data[off++];
return a | b << 8 | c << 16 | d << 24;
}
public byte[] readarr(byte[] data, int size, ref int off) {
byte[] x = new byte[size];
Buffer.BlockCopy(data, off, x, 0, size);
off += size;
return x;
}
public string readname(byte[] data, ref int off) {
int size = read16(data, ref off);
byte[] name = readarr(data, size, ref off);
return System.Text.Encoding.ASCII.GetString(name);
}
public byte[] readhash(byte[] data, ref int off) {
int size = (int)data[off]; off += 1;
return readarr(data, size, ref off);
}
public byte[] readdata(byte[] data, ref int off) {
int size = read32(data, ref off);
return readarr(data, size, ref off);
}
abstract public void read(byte[] data, ref int off);
};
class NEW : CTRL {
public byte[] hash;
public string name;
public byte[] data;
public override void read(byte[] x, ref int off) {
act = action.CTRL_NEW;
hash = readhash(x, ref off);
name = readname(x, ref off);
data = readdata(x, ref off);
}
};
class DFF : CTRL {
public byte[] hash;
public string name;
public byte[] patch;
public override void read(byte[] data, ref int off) {
act = action.CTRL_DFF;
hash = readhash(data, ref off);
name = readname(data, ref off);
patch = readdata(data, ref off);
}
};
class DFX : CTRL {
public byte[] hash;
public string name;
public string namea;
public byte[] patch;
public override void read(byte[] data, ref int off) {
act = action.CTRL_DFX;
hash = readhash(data, ref off);
name = readname(data, ref off);
namea = readname(data, ref off);
patch = readdata(data, ref off);
}
};
class DEL : CTRL {
public string name;
public override void read(byte[] data, ref int off) {
act = action.CTRL_DEL;
name = readname(data, ref off);
}
};
class MOV : CTRL {
public byte[] hash;
public string name;
public string namea;
public override void read(byte[] data, ref int off) {
act = action.CTRL_MOV;
hash = readhash(data, ref off);
name = readname(data, ref off);
namea = readname(data, ref off);
}
};
class patcher {
public static CTRL read(byte[] data, ref int off) {
action act = CTRL.readact(data, ref off);
CTRL o = null;
switch (act) {
case action.CTRL_NEW:
o = new NEW();
break;
case action.CTRL_DFF:
o = new DFF();
break;
case action.CTRL_DFX:
o = new DFX();
break;
case action.CTRL_MOV:
o = new MOV();
break;
case action.CTRL_DEL:
o = new DEL();
break;
}
o.read(data, ref off);
return o;
}
public byte[] patch_content(byte[] old, byte[] patch) {
int wr = 0, off = 0;
int end = patch.Length;
int fsize = CTRL.read32(patch, ref off);
byte[] buff = new byte[fsize];
while (off < end) {
int act = CTRL.read8(patch, ref off);
if (act == (int)action.PATCH_COPY) {
int pos, size;
pos = CTRL.read32(patch, ref off);
size = CTRL.read32(patch, ref off);
Buffer.BlockCopy(old, pos, buff, wr, size);
wr += size;
} else if (act == (int)action.PATCH_INSERT) {
int size;
size = CTRL.read32(patch, ref off);
Buffer.BlockCopy(patch, off, buff, wr, size);
off += size;
wr += size;
} else {
return null;
}
}
return buff;
}
Dictionary<string, byte[]> filecache = new Dictionary<string,byte[]>();
byte[] readfile(string name) {
byte[] o;
bool find = filecache.TryGetValue(name, out o);
if (find)
return o;
o = File.ReadAllBytes(name);
filecache[name] = o;
return o;
}
void writefile(string path, byte[] data) {
FileInfo file = new FileInfo(path);
file.Directory.Create();
File.WriteAllBytes(path, data);
}
public void patch(string patchpath, string dir) {
int offset = 0, end;
string tmp = Path.Combine(dir, "_temp");
List<CTRL> second = new List<CTRL>();
byte[] patch = File.ReadAllBytes(patchpath);
end = patch.Length;
//step1 dff/dfx
while (offset < end) {
string frompath, topath;
byte[] frombuf, tobuf;
CTRL o = read(patch, ref offset);
switch (o.act) {
case action.CTRL_DFF:
frompath = Path.Combine(dir, ((DFF)o).name);
topath = Path.Combine(tmp, ((DFF)o).name);
frombuf = readfile(frompath);
tobuf = patch_content(frombuf, ((DFF)o).patch);
writefile(topath, tobuf);
break;
case action.CTRL_DFX:
frompath = Path.Combine(dir, ((DFX)o).namea);
topath = Path.Combine(tmp, ((DFX)o).name);
frombuf = readfile(frompath);
tobuf = patch_content(frombuf, ((DFX)o).patch);
writefile(topath, tobuf);
break;
case action.CTRL_NEW:
case action.CTRL_MOV:
case action.CTRL_DEL:
second.Add(o);
break;
}
}
//step2 rename/delete/new
var iter = second.GetEnumerator();
while (iter.MoveNext()) {
byte[] data;
string temppath, frompath;
var ctrl = iter.Current;
switch (ctrl.act) {
case action.CTRL_NEW:
temppath = Path.Combine(tmp, ((NEW)ctrl).name);
writefile(temppath, ((NEW)ctrl).data);
break;
case action.CTRL_MOV:
frompath = Path.Combine(dir, ((MOV)ctrl).namea);
temppath = Path.Combine(tmp, ((MOV)ctrl).name);
data = readfile(frompath);
writefile(temppath, data);
break;
case action.CTRL_DEL:
frompath = Path.Combine(dir, ((DEL)ctrl).name);
File.Delete(frompath);
break;
}
}
//step3 merge
var prefix = tmp.Length + 1;
var files = Directory.GetFiles(tmp, "*", SearchOption.AllDirectories).ToList();
var fiter = files.GetEnumerator();
while (fiter.MoveNext()) {
string from = fiter.Current;
string to = from.Substring(prefix);
to = Path.Combine(dir, to);
var info = new FileInfo(to);
info.Directory.Create();
if (File.Exists(to))
File.Delete(to);
File.Move(from, to);
}
//step4 verify
//step5 clearnup
Directory.Delete(tmp);
}
}
}