2016-01-07 08:14:33 +00:00
|
|
|
#pragma once
|
2011-08-18 13:58:27 +00:00
|
|
|
|
|
|
|
#include <nall/file.hpp>
|
|
|
|
#include <nall/filemap.hpp>
|
|
|
|
#include <nall/stdint.hpp>
|
|
|
|
#include <nall/string.hpp>
|
2016-05-16 09:51:12 +00:00
|
|
|
#include <nall/hash/crc32.hpp>
|
2011-08-18 13:58:27 +00:00
|
|
|
|
|
|
|
namespace nall {
|
|
|
|
|
|
|
|
struct bpsdelta {
|
Update to v097r14 release.
byuu says:
This is a few days old, but oh well.
This WIP changes nall,hiro,ruby,icarus back to (u)int(8,16,32,64)_t.
I'm slowly pushing for (u)int(8,16,32,64) to use my custom
Integer<Size>/Natural<Size> classes instead. But it's going to be one
hell of a struggle to get that into higan.
2016-02-16 09:11:58 +00:00
|
|
|
inline auto source(const uint8_t* data, uint size) -> void;
|
|
|
|
inline auto target(const uint8_t* data, uint size) -> void;
|
2011-08-20 14:40:44 +00:00
|
|
|
|
2015-12-14 09:41:06 +00:00
|
|
|
inline auto source(const string& filename) -> bool;
|
|
|
|
inline auto target(const string& filename) -> bool;
|
|
|
|
inline auto create(const string& filename, const string& metadata = "") -> bool;
|
2011-08-18 13:58:27 +00:00
|
|
|
|
|
|
|
protected:
|
2015-12-14 09:41:06 +00:00
|
|
|
enum : uint { SourceRead, TargetRead, SourceCopy, TargetCopy };
|
|
|
|
enum : uint { Granularity = 1 };
|
2011-08-18 13:58:27 +00:00
|
|
|
|
|
|
|
struct Node {
|
2013-05-02 11:25:45 +00:00
|
|
|
Node() = default;
|
|
|
|
~Node() { if(next) delete next; }
|
2015-12-14 09:41:06 +00:00
|
|
|
uint offset = 0;
|
|
|
|
Node* next = nullptr;
|
2011-08-18 13:58:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
filemap sourceFile;
|
Update to v097r14 release.
byuu says:
This is a few days old, but oh well.
This WIP changes nall,hiro,ruby,icarus back to (u)int(8,16,32,64)_t.
I'm slowly pushing for (u)int(8,16,32,64) to use my custom
Integer<Size>/Natural<Size> classes instead. But it's going to be one
hell of a struggle to get that into higan.
2016-02-16 09:11:58 +00:00
|
|
|
const uint8_t* sourceData;
|
2015-12-14 09:41:06 +00:00
|
|
|
uint sourceSize;
|
2011-08-18 13:58:27 +00:00
|
|
|
|
|
|
|
filemap targetFile;
|
Update to v097r14 release.
byuu says:
This is a few days old, but oh well.
This WIP changes nall,hiro,ruby,icarus back to (u)int(8,16,32,64)_t.
I'm slowly pushing for (u)int(8,16,32,64) to use my custom
Integer<Size>/Natural<Size> classes instead. But it's going to be one
hell of a struggle to get that into higan.
2016-02-16 09:11:58 +00:00
|
|
|
const uint8_t* targetData;
|
2015-12-14 09:41:06 +00:00
|
|
|
uint targetSize;
|
2011-08-18 13:58:27 +00:00
|
|
|
};
|
|
|
|
|
Update to v097r14 release.
byuu says:
This is a few days old, but oh well.
This WIP changes nall,hiro,ruby,icarus back to (u)int(8,16,32,64)_t.
I'm slowly pushing for (u)int(8,16,32,64) to use my custom
Integer<Size>/Natural<Size> classes instead. But it's going to be one
hell of a struggle to get that into higan.
2016-02-16 09:11:58 +00:00
|
|
|
auto bpsdelta::source(const uint8_t* data, uint size) -> void {
|
2011-08-20 14:40:44 +00:00
|
|
|
sourceData = data;
|
|
|
|
sourceSize = size;
|
|
|
|
}
|
|
|
|
|
Update to v097r14 release.
byuu says:
This is a few days old, but oh well.
This WIP changes nall,hiro,ruby,icarus back to (u)int(8,16,32,64)_t.
I'm slowly pushing for (u)int(8,16,32,64) to use my custom
Integer<Size>/Natural<Size> classes instead. But it's going to be one
hell of a struggle to get that into higan.
2016-02-16 09:11:58 +00:00
|
|
|
auto bpsdelta::target(const uint8_t* data, uint size) -> void {
|
2011-08-20 14:40:44 +00:00
|
|
|
targetData = data;
|
|
|
|
targetSize = size;
|
|
|
|
}
|
|
|
|
|
2015-12-14 09:41:06 +00:00
|
|
|
auto bpsdelta::source(const string& filename) -> bool {
|
2011-08-18 13:58:27 +00:00
|
|
|
if(sourceFile.open(filename, filemap::mode::read) == false) return false;
|
2011-08-20 14:40:44 +00:00
|
|
|
source(sourceFile.data(), sourceFile.size());
|
2011-08-18 13:58:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-14 09:41:06 +00:00
|
|
|
auto bpsdelta::target(const string& filename) -> bool {
|
2011-08-18 13:58:27 +00:00
|
|
|
if(targetFile.open(filename, filemap::mode::read) == false) return false;
|
2011-08-20 14:40:44 +00:00
|
|
|
target(targetFile.data(), targetFile.size());
|
2011-08-18 13:58:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-14 09:41:06 +00:00
|
|
|
auto bpsdelta::create(const string& filename, const string& metadata) -> bool {
|
2011-08-18 13:58:27 +00:00
|
|
|
file modifyFile;
|
|
|
|
if(modifyFile.open(filename, file::mode::write) == false) return false;
|
|
|
|
|
Update to v094r09 release.
byuu says:
This will easily be the biggest diff in the history of higan. And not in
a good way.
* target-higan and target-loki have been blown away completely
* nall and ruby massively updated
* phoenix replaced with hiro (pretty near a total rewrite)
* target-higan restarted using hiro (just a window for now)
* all emulation cores updated to compile again
* installation changed to not require root privileges (installs locally)
For the foreseeable future (maybe even permanently?), the new higan UI
will only build under Linux/BSD with GTK+ 2.20+. Probably the most
likely route for Windows/OS X will be to try and figure out how to build
hiro/GTK on those platforms, as awful as that would be. The other
alternative would be to produce new UIs for those platforms ... which
would actually be a good opportunity to make something much more user
friendly.
Being that I just started on this a few hours ago, that means that for
at least a few weeks, don't expect to be able to actually play any
games. Right now, you can pretty much just compile the binary and that's
it. It's quite possible that some nall changes didn't produce
compilation errors, but will produce runtime errors. So until the UI can
actually load games, we won't know if anything is broken. But we should
mostly be okay. It was mostly just trim<1> -> trim changes, moving to
Hash::SHA256 (much cleaner), and patching some reckless memory copy
functions enough to compile.
Progress isn't going to be like it was before: I'm now dividing my time
much thinner between studying and other hobbies.
My aim this time is not to produce a binary for everyone to play games
on. Rather, it's to keep the emulator alive. I want to be able to apply
critical patches again. And I would also like the base of the emulator
to live on, for use in other emulator frontends that utilize higan.
2015-02-26 10:10:46 +00:00
|
|
|
Hash::CRC32 sourceChecksum, modifyChecksum;
|
2015-12-14 09:41:06 +00:00
|
|
|
uint sourceRelativeOffset = 0, targetRelativeOffset = 0, outputOffset = 0;
|
2011-08-18 13:58:27 +00:00
|
|
|
|
Update to v097r14 release.
byuu says:
This is a few days old, but oh well.
This WIP changes nall,hiro,ruby,icarus back to (u)int(8,16,32,64)_t.
I'm slowly pushing for (u)int(8,16,32,64) to use my custom
Integer<Size>/Natural<Size> classes instead. But it's going to be one
hell of a struggle to get that into higan.
2016-02-16 09:11:58 +00:00
|
|
|
auto write = [&](uint8_t data) {
|
2011-08-18 13:58:27 +00:00
|
|
|
modifyFile.write(data);
|
2016-10-27 21:16:58 +00:00
|
|
|
modifyChecksum.input(data);
|
2011-08-18 13:58:27 +00:00
|
|
|
};
|
|
|
|
|
Update to v097r14 release.
byuu says:
This is a few days old, but oh well.
This WIP changes nall,hiro,ruby,icarus back to (u)int(8,16,32,64)_t.
I'm slowly pushing for (u)int(8,16,32,64) to use my custom
Integer<Size>/Natural<Size> classes instead. But it's going to be one
hell of a struggle to get that into higan.
2016-02-16 09:11:58 +00:00
|
|
|
auto encode = [&](uint64_t data) {
|
2011-08-18 13:58:27 +00:00
|
|
|
while(true) {
|
Update to v097r14 release.
byuu says:
This is a few days old, but oh well.
This WIP changes nall,hiro,ruby,icarus back to (u)int(8,16,32,64)_t.
I'm slowly pushing for (u)int(8,16,32,64) to use my custom
Integer<Size>/Natural<Size> classes instead. But it's going to be one
hell of a struggle to get that into higan.
2016-02-16 09:11:58 +00:00
|
|
|
uint64_t x = data & 0x7f;
|
2011-08-18 13:58:27 +00:00
|
|
|
data >>= 7;
|
|
|
|
if(data == 0) {
|
|
|
|
write(0x80 | x);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
write(x);
|
|
|
|
data--;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
write('B');
|
|
|
|
write('P');
|
|
|
|
write('S');
|
|
|
|
write('1');
|
|
|
|
|
|
|
|
encode(sourceSize);
|
|
|
|
encode(targetSize);
|
|
|
|
|
2015-12-14 09:41:06 +00:00
|
|
|
uint markupSize = metadata.length();
|
2011-08-18 13:58:27 +00:00
|
|
|
encode(markupSize);
|
2015-12-14 09:41:06 +00:00
|
|
|
for(uint n = 0; n < markupSize; n++) write(metadata[n]);
|
2011-08-18 13:58:27 +00:00
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
Node* sourceTree[65536];
|
|
|
|
Node* targetTree[65536];
|
2015-12-14 09:41:06 +00:00
|
|
|
for(uint n = 0; n < 65536; n++) sourceTree[n] = nullptr, targetTree[n] = nullptr;
|
2011-08-18 13:58:27 +00:00
|
|
|
|
|
|
|
//source tree creation
|
2015-12-14 09:41:06 +00:00
|
|
|
for(uint offset = 0; offset < sourceSize; offset++) {
|
Update to v097r14 release.
byuu says:
This is a few days old, but oh well.
This WIP changes nall,hiro,ruby,icarus back to (u)int(8,16,32,64)_t.
I'm slowly pushing for (u)int(8,16,32,64) to use my custom
Integer<Size>/Natural<Size> classes instead. But it's going to be one
hell of a struggle to get that into higan.
2016-02-16 09:11:58 +00:00
|
|
|
uint16_t symbol = sourceData[offset + 0];
|
2016-10-27 21:16:58 +00:00
|
|
|
sourceChecksum.input(symbol);
|
2011-08-18 13:58:27 +00:00
|
|
|
if(offset < sourceSize - 1) symbol |= sourceData[offset + 1] << 8;
|
|
|
|
Node *node = new Node;
|
|
|
|
node->offset = offset;
|
|
|
|
node->next = sourceTree[symbol];
|
|
|
|
sourceTree[symbol] = node;
|
|
|
|
}
|
|
|
|
|
2015-12-14 09:41:06 +00:00
|
|
|
uint targetReadLength = 0;
|
2011-08-18 13:58:27 +00:00
|
|
|
|
|
|
|
auto targetReadFlush = [&]() {
|
|
|
|
if(targetReadLength) {
|
|
|
|
encode(TargetRead | ((targetReadLength - 1) << 2));
|
2015-12-14 09:41:06 +00:00
|
|
|
uint offset = outputOffset - targetReadLength;
|
2011-08-18 13:58:27 +00:00
|
|
|
while(targetReadLength) write(targetData[offset++]), targetReadLength--;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
while(outputOffset < targetSize) {
|
2015-12-14 09:41:06 +00:00
|
|
|
uint maxLength = 0, maxOffset = 0, mode = TargetRead;
|
2011-08-18 13:58:27 +00:00
|
|
|
|
Update to v097r14 release.
byuu says:
This is a few days old, but oh well.
This WIP changes nall,hiro,ruby,icarus back to (u)int(8,16,32,64)_t.
I'm slowly pushing for (u)int(8,16,32,64) to use my custom
Integer<Size>/Natural<Size> classes instead. But it's going to be one
hell of a struggle to get that into higan.
2016-02-16 09:11:58 +00:00
|
|
|
uint16_t symbol = targetData[outputOffset + 0];
|
2011-08-18 13:58:27 +00:00
|
|
|
if(outputOffset < targetSize - 1) symbol |= targetData[outputOffset + 1] << 8;
|
|
|
|
|
2011-08-20 14:40:44 +00:00
|
|
|
{ //source read
|
2015-12-14 09:41:06 +00:00
|
|
|
uint length = 0, offset = outputOffset;
|
2011-08-20 14:40:44 +00:00
|
|
|
while(offset < sourceSize && offset < targetSize && sourceData[offset] == targetData[offset]) {
|
|
|
|
length++;
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
if(length > maxLength) maxLength = length, mode = SourceRead;
|
|
|
|
}
|
|
|
|
|
2011-08-18 13:58:27 +00:00
|
|
|
{ //source copy
|
2013-05-02 11:25:45 +00:00
|
|
|
Node* node = sourceTree[symbol];
|
2011-08-18 13:58:27 +00:00
|
|
|
while(node) {
|
2015-12-14 09:41:06 +00:00
|
|
|
uint length = 0, x = node->offset, y = outputOffset;
|
2011-08-18 13:58:27 +00:00
|
|
|
while(x < sourceSize && y < targetSize && sourceData[x++] == targetData[y++]) length++;
|
|
|
|
if(length > maxLength) maxLength = length, maxOffset = node->offset, mode = SourceCopy;
|
|
|
|
node = node->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{ //target copy
|
2013-05-02 11:25:45 +00:00
|
|
|
Node* node = targetTree[symbol];
|
2011-08-18 13:58:27 +00:00
|
|
|
while(node) {
|
2015-12-14 09:41:06 +00:00
|
|
|
uint length = 0, x = node->offset, y = outputOffset;
|
2011-08-18 13:58:27 +00:00
|
|
|
while(y < targetSize && targetData[x++] == targetData[y++]) length++;
|
|
|
|
if(length > maxLength) maxLength = length, maxOffset = node->offset, mode = TargetCopy;
|
|
|
|
node = node->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
//target tree append
|
|
|
|
node = new Node;
|
|
|
|
node->offset = outputOffset;
|
|
|
|
node->next = targetTree[symbol];
|
|
|
|
targetTree[symbol] = node;
|
|
|
|
}
|
|
|
|
|
|
|
|
{ //target read
|
|
|
|
if(maxLength < 4) {
|
2015-12-14 09:41:06 +00:00
|
|
|
maxLength = min((uint)Granularity, targetSize - outputOffset);
|
2011-08-18 13:58:27 +00:00
|
|
|
mode = TargetRead;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(mode != TargetRead) targetReadFlush();
|
|
|
|
|
|
|
|
switch(mode) {
|
|
|
|
case SourceRead:
|
|
|
|
encode(SourceRead | ((maxLength - 1) << 2));
|
|
|
|
break;
|
|
|
|
case TargetRead:
|
|
|
|
//delay write to group sequential TargetRead commands into one
|
|
|
|
targetReadLength += maxLength;
|
|
|
|
break;
|
|
|
|
case SourceCopy:
|
|
|
|
case TargetCopy:
|
|
|
|
encode(mode | ((maxLength - 1) << 2));
|
2015-12-14 09:41:06 +00:00
|
|
|
int relativeOffset;
|
2011-08-18 13:58:27 +00:00
|
|
|
if(mode == SourceCopy) {
|
|
|
|
relativeOffset = maxOffset - sourceRelativeOffset;
|
|
|
|
sourceRelativeOffset = maxOffset + maxLength;
|
|
|
|
} else {
|
|
|
|
relativeOffset = maxOffset - targetRelativeOffset;
|
|
|
|
targetRelativeOffset = maxOffset + maxLength;
|
|
|
|
}
|
|
|
|
encode((relativeOffset < 0) | (abs(relativeOffset) << 1));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
outputOffset += maxLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
targetReadFlush();
|
|
|
|
|
2016-10-27 21:16:58 +00:00
|
|
|
for(uint n = 0; n < 32; n += 8) write(sourceChecksum.digest().hex() >> n);
|
|
|
|
uint32_t targetChecksum = Hash::CRC32(targetData, targetSize).digest().hex();
|
2015-12-14 09:41:06 +00:00
|
|
|
for(uint n = 0; n < 32; n += 8) write(targetChecksum >> n);
|
2016-10-27 21:16:58 +00:00
|
|
|
uint32_t outputChecksum = modifyChecksum.digest().hex();
|
2015-12-14 09:41:06 +00:00
|
|
|
for(uint n = 0; n < 32; n += 8) write(outputChecksum >> n);
|
2011-08-18 13:58:27 +00:00
|
|
|
|
|
|
|
modifyFile.close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|