-
Notifications
You must be signed in to change notification settings - Fork 0
/
Patch.d
140 lines (114 loc) · 3.58 KB
/
Patch.d
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
module Patch;
private {
import std.conv : to;
import std.exception : enforce;
import std.zlib : crc32, compress, uncompress;
import EFIHeaders : EFIGUID;
import Utils : toStruct, fromStruct;
}
enum PatchTokenType {
OpenRound,
CloseRound,
OpenCurly,
CloseCurly,
OpenSquare,
CloseSquare,
Equals,
Comma,
String,
Keyword
}
struct PatchToken {
PatchTokenType type;
string str;
this(PatchTokenType type, string str = "") {
this.type = type;
this.str = str;
}
}
const uint PatchCurrentVersion = 1;
struct PatchHeader {
uint patchVersion;
uint length;
uint crc32;
}
struct Patch {
PatchHeader header;
string name;
string file;
EFIGUID guid;
ubyte[] search;
ubyte[] replace;
ubyte[] fileReplace;
int occurs;
ubyte[] toBinary() {
ubyte[] data;
uint len = cast(uint)name.length;
data ~= fromStruct(&len, len.sizeof);
data ~= fromStruct(name.ptr, name.length);
len = cast(uint)file.length;
data ~= fromStruct(&len, len.sizeof);
data ~= fromStruct(file.ptr, file.length);
data ~= fromStruct(&guid, guid.sizeof);
len = cast(uint)search.length;
data ~= fromStruct(&len, len.sizeof);
data ~= search;
len = cast(uint)replace.length;
data ~= fromStruct(&len, len.sizeof);
data ~= replace;
len = cast(uint)fileReplace.length;
data ~= fromStruct(&len, len.sizeof);
data ~= fileReplace;
data ~= fromStruct(&occurs, occurs.sizeof);
data = cast(ubyte[])compress(data);
header.patchVersion = PatchCurrentVersion;
header.length = cast(uint)data.length;
header.crc32 = crc32(0, data);
return fromStruct(&header, header.sizeof) ~ data;
}
static Patch[] fromBinary(ubyte[] data) {
Patch patch;
uint len;
char[] name, file;
if(data.length == 0)
return [];
toStruct(data[0..$], &patch.header, header.sizeof);
enforce(patch.header.patchVersion == PatchCurrentVersion, "Unknown patch version, please update the patch and program");
enforce(header.sizeof + patch.header.length <= data.length, "Malformed patch");
enforce(crc32(0, data[header.sizeof..header.sizeof + patch.header.length]) == patch.header.crc32, "Corrupted patch");
ubyte[] patchdata = cast(ubyte[])uncompress(data[header.sizeof..header.sizeof + patch.header.length]);
size_t offset = 0;
toStruct(patchdata[offset..$], &len, len.sizeof);
offset += len.sizeof;
name.length = len;
toStruct(patchdata[offset..$], name.ptr, len);
offset += len;
patch.name = to!string(name);
toStruct(patchdata[offset..$], &len, len.sizeof);
offset += len.sizeof;
file.length = len;
toStruct(patchdata[offset..$], file.ptr, len);
offset += len;
patch.file = to!string(file);
toStruct(patchdata[offset..$], &patch.guid, guid.sizeof);
offset += guid.sizeof;
toStruct(patchdata[offset..$], &len, len.sizeof);
offset += len.sizeof;
patch.search.length = len;
toStruct(patchdata[offset..$], patch.search.ptr, len);
offset += len;
toStruct(patchdata[offset..$], &len, len.sizeof);
offset += len.sizeof;
patch.replace.length = len;
toStruct(patchdata[offset..$], patch.replace.ptr, len);
offset += len;
toStruct(patchdata[offset..$], &len, len.sizeof);
offset += len.sizeof;
patch.fileReplace.length = len;
toStruct(patchdata[offset..$], patch.fileReplace.ptr, len);
offset += len;
toStruct(patchdata[offset..$], &patch.occurs, occurs.sizeof);
offset += occurs.sizeof;
return patch ~ fromBinary(data[header.sizeof + patch.header.length..$]);
}
}