Debian Codegraph @

nall/beat/multi.hpp

raw
1 #ifndef NALL_BEAT_MULTI_HPP
2 #define NALL_BEAT_MULTI_HPP
3
4 #include <nall/beat/patch.hpp>
5 #include <nall/beat/linear.hpp>
6 #include <nall/beat/delta.hpp>
7
8 namespace nall {
9
10 struct bpsmulti {
11 enum : unsigned {
12 CreatePath = 0,
13 CreateFile = 1,
14 ModifyFile = 2,
15 MirrorFile = 3,
16 };
17
18 enum : unsigned {
19 OriginSource = 0,
20 OriginTarget = 1,
21 };
22
23 bool create(const string& patchName, const string& sourcePath, const string& targetPath, bool delta = false, const string& metadata = "") {
24 if(fp.open()) fp.close();
25 fp.open(patchName, file::mode::write);
26 checksum = ~0;
27
28 writeString("BPM1"); //signature
29 writeNumber(metadata.length());
30 writeString(metadata);
31
32 lstring sourceList, targetList;
33 ls(sourceList, sourcePath, sourcePath);
34 ls(targetList, targetPath, targetPath);
35
36 for(auto& targetName : targetList) {
37 if(targetName.endsWith("/")) {
38 targetName.rtrim<1>("/");
39 writeNumber(CreatePath | ((targetName.length() - 1) << 2));
40 writeString(targetName);
41 } else if(auto position = sourceList.find(targetName)) { //if sourceName == targetName
42 file sp, dp;
43 sp.open({sourcePath, targetName}, file::mode::read);
44 dp.open({targetPath, targetName}, file::mode::read);
45
46 bool identical = sp.size() == dp.size();
47 uint32_t cksum = ~0;
48
49 for(unsigned n = 0; n < sp.size(); n++) {
50 uint8_t byte = sp.read();
51 if(identical && byte != dp.read()) identical = false;
52 cksum = crc32_adjust(cksum, byte);
53 }
54
55 if(identical) {
56 writeNumber(MirrorFile | ((targetName.length() - 1) << 2));
57 writeString(targetName);
58 writeNumber(OriginSource);
59 writeChecksum(~cksum);
60 } else {
61 writeNumber(ModifyFile | ((targetName.length() - 1) << 2));
62 writeString(targetName);
63 writeNumber(OriginSource);
64
65 if(delta == false) {
66 bpslinear patch;
67 patch.source({sourcePath, targetName});
68 patch.target({targetPath, targetName});
69 patch.create({temppath(), "temp.bps"});
70 } else {
71 bpsdelta patch;
72 patch.source({sourcePath, targetName});
73 patch.target({targetPath, targetName});
74 patch.create({temppath(), "temp.bps"});
75 }
76
77 auto buffer = file::read({temppath(), "temp.bps"});
78 writeNumber(buffer.size());
79 for(auto &byte : buffer) write(byte);
80 }
81 } else {
82 writeNumber(CreateFile | ((targetName.length() - 1) << 2));
83 writeString(targetName);
84 auto buffer = file::read({targetPath, targetName});
85 writeNumber(buffer.size());
86 for(auto &byte : buffer) write(byte);
87 writeChecksum(crc32_calculate(buffer.data(), buffer.size()));
88 }
89 }
90
91 //checksum
92 writeChecksum(~checksum);
93 fp.close();
94 return true;
95 }
96
97 bool apply(const string& patchName, const string& sourcePath, const string& targetPath) {
98 directory::remove(targetPath); //start with a clean directory
99 directory::create(targetPath);
100
101 if(fp.open()) fp.close();
102 fp.open(patchName, file::mode::read);
103 checksum = ~0;
104
105 if(readString(4) != "BPM1") return false;
106 auto metadataLength = readNumber();
107 while(metadataLength--) read();
108
109 while(fp.offset() < fp.size() - 4) {
110 auto encoding = readNumber();
111 unsigned action = encoding & 3;
112 unsigned targetLength = (encoding >> 2) + 1;
113 string targetName = readString(targetLength);
114
115 if(action == CreatePath) {
116 directory::create({targetPath, targetName, "/"});
117 } else if(action == CreateFile) {
118 file fp;
119 fp.open({targetPath, targetName}, file::mode::write);
120 auto fileSize = readNumber();
121 while(fileSize--) fp.write(read());
122 uint32_t cksum = readChecksum();
123 } else if(action == ModifyFile) {
124 auto encoding = readNumber();
125 string originPath = encoding & 1 ? targetPath : sourcePath;
126 string sourceName = (encoding >> 1) == 0 ? targetName : readString(encoding >> 1);
127 auto patchSize = readNumber();
128 vector<uint8_t> buffer;
129 buffer.resize(patchSize);
130 for(unsigned n = 0; n < patchSize; n++) buffer[n] = read();
131 bpspatch patch;
132 patch.modify(buffer.data(), buffer.size());
133 patch.source({originPath, sourceName});
134 patch.target({targetPath, targetName});
135 if(patch.apply() != bpspatch::result::success) return false;
136 } else if(action == MirrorFile) {
137 auto encoding = readNumber();
138 string originPath = encoding & 1 ? targetPath : sourcePath;
139 string sourceName = (encoding >> 1) == 0 ? targetName : readString(encoding >> 1);
140 file::copy({originPath, sourceName}, {targetPath, targetName});
141 uint32_t cksum = readChecksum();
142 }
143 }
144
145 uint32_t cksum = ~checksum;
146 if(read() != (uint8_t)(cksum >> 0)) return false;
147 if(read() != (uint8_t)(cksum >> 8)) return false;
148 if(read() != (uint8_t)(cksum >> 16)) return false;
149 if(read() != (uint8_t)(cksum >> 24)) return false;
150
151 fp.close();
152 return true;
153 }
154
155 protected:
156 file fp;
157 uint32_t checksum;
158
159 //create() functions
160 void ls(lstring& list, const string& path, const string& basepath) {
161 lstring paths = directory::folders(path);
162 for(auto& pathname : paths) {
163 list.append(string{path, pathname}.ltrim<1>(basepath));
164 ls(list, {path, pathname}, basepath);
165 }
166
167 lstring files = directory::files(path);
168 for(auto& filename : files) {
169 list.append(string{path, filename}.ltrim<1>(basepath));
170 }
171 }
172
173 void write(uint8_t data) {
174 fp.write(data);
175 checksum = crc32_adjust(checksum, data);
176 }
177
178 void writeNumber(uint64_t data) {
179 while(true) {
180 uint64_t x = data & 0x7f;
181 data >>= 7;
182 if(data == 0) {
183 write(0x80 | x);
184 break;
185 }
186 write(x);
187 data--;
188 }
189 }
190
191 void writeString(const string& text) {
192 unsigned length = text.length();
193 for(unsigned n = 0; n < length; n++) write(text[n]);
194 }
195
196 void writeChecksum(uint32_t cksum) {
197 write(cksum >> 0);
198 write(cksum >> 8);
199 write(cksum >> 16);
200 write(cksum >> 24);
201 }
202
203 //apply() functions
204 uint8_t read() {
205 uint8_t data = fp.read();
206 checksum = crc32_adjust(checksum, data);
207 return data;
208 }
209
210 uint64_t readNumber() {
211 uint64_t data = 0, shift = 1;
212 while(true) {
213 uint8_t x = read();
214 data += (x & 0x7f) * shift;
215 if(x & 0x80) break;
216 shift <<= 7;
217 data += shift;
218 }
219 return data;
220 }
221
222 string readString(unsigned length) {
223 string text;
224 text.reserve(length + 1);
225 for(unsigned n = 0; n < length; n++) text[n] = read();
226 text[length] = 0;
227 return text;
228 }
229
230 uint32_t readChecksum() {
231 uint32_t checksum = 0;
232 checksum |= read() << 0;
233 checksum |= read() << 8;
234 checksum |= read() << 16;
235 checksum |= read() << 24;
236 return checksum;
237 }
238 };
239
240 }
241
242 #endif