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>
|
|
|
|
|
|
|
|
namespace nall {
|
|
|
|
|
|
|
|
struct bpspatch {
|
2015-12-14 09:41:06 +00:00
|
|
|
inline auto modify(const uint8* data, uint size) -> bool;
|
|
|
|
inline auto source(const uint8* data, uint size) -> void;
|
|
|
|
inline auto target(uint8* data, uint size) -> void;
|
2011-08-18 13:58:27 +00:00
|
|
|
|
2015-12-14 09:41:06 +00:00
|
|
|
inline auto modify(const string& filename) -> bool;
|
|
|
|
inline auto source(const string& filename) -> bool;
|
|
|
|
inline auto target(const string& filename) -> bool;
|
2011-08-18 13:58:27 +00:00
|
|
|
|
2015-12-14 09:41:06 +00:00
|
|
|
inline auto metadata() const -> string;
|
|
|
|
inline auto size() const -> uint;
|
2011-08-18 13:58:27 +00:00
|
|
|
|
2015-12-14 09:41:06 +00:00
|
|
|
enum result : uint {
|
2011-08-18 13:58:27 +00:00
|
|
|
unknown,
|
|
|
|
success,
|
|
|
|
patch_too_small,
|
|
|
|
patch_invalid_header,
|
|
|
|
source_too_small,
|
|
|
|
target_too_small,
|
|
|
|
source_checksum_invalid,
|
|
|
|
target_checksum_invalid,
|
|
|
|
patch_checksum_invalid,
|
|
|
|
};
|
|
|
|
|
2015-12-14 09:41:06 +00:00
|
|
|
inline auto apply() -> result;
|
2011-08-18 13:58:27 +00:00
|
|
|
|
|
|
|
protected:
|
2015-12-14 09:41:06 +00:00
|
|
|
enum : uint { SourceRead, TargetRead, SourceCopy, TargetCopy };
|
2011-08-18 13:58:27 +00:00
|
|
|
|
|
|
|
filemap modifyFile;
|
2015-12-14 09:41:06 +00:00
|
|
|
const uint8* modifyData;
|
|
|
|
uint modifySize;
|
2011-08-18 13:58:27 +00:00
|
|
|
|
|
|
|
filemap sourceFile;
|
2015-12-14 09:41:06 +00:00
|
|
|
const uint8* sourceData;
|
|
|
|
uint sourceSize;
|
2011-08-18 13:58:27 +00:00
|
|
|
|
|
|
|
filemap targetFile;
|
2015-12-14 09:41:06 +00:00
|
|
|
uint8* targetData;
|
|
|
|
uint targetSize;
|
2011-08-18 13:58:27 +00:00
|
|
|
|
2015-12-14 09:41:06 +00:00
|
|
|
uint modifySourceSize;
|
|
|
|
uint modifyTargetSize;
|
|
|
|
uint modifyMarkupSize;
|
2011-08-20 14:40:44 +00:00
|
|
|
string metadataString;
|
2011-08-18 13:58:27 +00:00
|
|
|
};
|
|
|
|
|
2015-12-14 09:41:06 +00:00
|
|
|
auto bpspatch::modify(const uint8* data, uint size) -> bool {
|
2011-08-18 13:58:27 +00:00
|
|
|
if(size < 19) return false;
|
|
|
|
modifyData = data;
|
|
|
|
modifySize = size;
|
|
|
|
|
2015-12-14 09:41:06 +00:00
|
|
|
uint offset = 4;
|
|
|
|
auto decode = [&]() -> uint64 {
|
|
|
|
uint64 data = 0, shift = 1;
|
2011-08-18 13:58:27 +00:00
|
|
|
while(true) {
|
2015-12-14 09:41:06 +00:00
|
|
|
uint8 x = modifyData[offset++];
|
2011-08-18 13:58:27 +00:00
|
|
|
data += (x & 0x7f) * shift;
|
|
|
|
if(x & 0x80) break;
|
|
|
|
shift <<= 7;
|
|
|
|
data += shift;
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
|
|
|
modifySourceSize = decode();
|
|
|
|
modifyTargetSize = decode();
|
|
|
|
modifyMarkupSize = decode();
|
2011-08-20 14:40:44 +00:00
|
|
|
|
|
|
|
char buffer[modifyMarkupSize + 1];
|
2015-12-14 09:41:06 +00:00
|
|
|
for(uint n = 0; n < modifyMarkupSize; n++) buffer[n] = modifyData[offset++];
|
2011-08-20 14:40:44 +00:00
|
|
|
buffer[modifyMarkupSize] = 0;
|
|
|
|
metadataString = (const char*)buffer;
|
|
|
|
|
2011-08-18 13:58:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-14 09:41:06 +00:00
|
|
|
auto bpspatch::source(const uint8* data, uint size) -> void {
|
2011-08-18 13:58:27 +00:00
|
|
|
sourceData = data;
|
|
|
|
sourceSize = size;
|
|
|
|
}
|
|
|
|
|
2015-12-14 09:41:06 +00:00
|
|
|
auto bpspatch::target(uint8* data, uint size) -> void {
|
2011-08-18 13:58:27 +00:00
|
|
|
targetData = data;
|
|
|
|
targetSize = size;
|
|
|
|
}
|
|
|
|
|
2015-12-14 09:41:06 +00:00
|
|
|
auto bpspatch::modify(const string& filename) -> bool {
|
2011-08-18 13:58:27 +00:00
|
|
|
if(modifyFile.open(filename, filemap::mode::read) == false) return false;
|
|
|
|
return modify(modifyFile.data(), modifyFile.size());
|
|
|
|
}
|
|
|
|
|
2015-12-14 09:41:06 +00:00
|
|
|
auto bpspatch::source(const string& filename) -> bool {
|
2011-08-18 13:58:27 +00:00
|
|
|
if(sourceFile.open(filename, filemap::mode::read) == false) return false;
|
|
|
|
source(sourceFile.data(), sourceFile.size());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-14 09:41:06 +00:00
|
|
|
auto bpspatch::target(const string& filename) -> bool {
|
2011-08-18 13:58:27 +00:00
|
|
|
file fp;
|
|
|
|
if(fp.open(filename, file::mode::write) == false) return false;
|
|
|
|
fp.truncate(modifyTargetSize);
|
|
|
|
fp.close();
|
|
|
|
|
|
|
|
if(targetFile.open(filename, filemap::mode::readwrite) == false) return false;
|
|
|
|
target(targetFile.data(), targetFile.size());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-14 09:41:06 +00:00
|
|
|
auto bpspatch::metadata() const -> string {
|
2011-08-20 14:40:44 +00:00
|
|
|
return metadataString;
|
|
|
|
}
|
|
|
|
|
2015-12-14 09:41:06 +00:00
|
|
|
auto bpspatch::size() const -> uint {
|
2011-08-18 13:58:27 +00:00
|
|
|
return modifyTargetSize;
|
|
|
|
}
|
|
|
|
|
2015-12-14 09:41:06 +00:00
|
|
|
auto bpspatch::apply() -> result {
|
2011-08-18 13:58:27 +00:00
|
|
|
if(modifySize < 19) return result::patch_too_small;
|
|
|
|
|
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 modifyChecksum, targetChecksum;
|
2015-12-14 09:41:06 +00:00
|
|
|
uint modifyOffset = 0, sourceRelativeOffset = 0, targetRelativeOffset = 0, outputOffset = 0;
|
2011-08-18 13:58:27 +00:00
|
|
|
|
2015-12-14 09:41:06 +00:00
|
|
|
auto read = [&]() -> uint8 {
|
|
|
|
uint8 data = modifyData[modifyOffset++];
|
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
|
|
|
modifyChecksum.data(data);
|
2011-08-18 13:58:27 +00:00
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
2015-12-14 09:41:06 +00:00
|
|
|
auto decode = [&]() -> uint64 {
|
|
|
|
uint64 data = 0, shift = 1;
|
2011-08-18 13:58:27 +00:00
|
|
|
while(true) {
|
2015-12-14 09:41:06 +00:00
|
|
|
uint8 x = read();
|
2011-08-18 13:58:27 +00:00
|
|
|
data += (x & 0x7f) * shift;
|
|
|
|
if(x & 0x80) break;
|
|
|
|
shift <<= 7;
|
|
|
|
data += shift;
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
2015-12-14 09:41:06 +00:00
|
|
|
auto write = [&](uint8 data) {
|
2011-08-18 13:58:27 +00:00
|
|
|
targetData[outputOffset++] = data;
|
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
|
|
|
targetChecksum.data(data);
|
2011-08-18 13:58:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if(read() != 'B') return result::patch_invalid_header;
|
|
|
|
if(read() != 'P') return result::patch_invalid_header;
|
|
|
|
if(read() != 'S') return result::patch_invalid_header;
|
|
|
|
if(read() != '1') return result::patch_invalid_header;
|
|
|
|
|
|
|
|
modifySourceSize = decode();
|
|
|
|
modifyTargetSize = decode();
|
|
|
|
modifyMarkupSize = decode();
|
2015-12-14 09:41:06 +00:00
|
|
|
for(uint n = 0; n < modifyMarkupSize; n++) read();
|
2011-08-18 13:58:27 +00:00
|
|
|
|
|
|
|
if(modifySourceSize > sourceSize) return result::source_too_small;
|
|
|
|
if(modifyTargetSize > targetSize) return result::target_too_small;
|
|
|
|
|
|
|
|
while(modifyOffset < modifySize - 12) {
|
2015-12-14 09:41:06 +00:00
|
|
|
uint length = decode();
|
|
|
|
uint mode = length & 3;
|
2011-08-18 13:58:27 +00:00
|
|
|
length = (length >> 2) + 1;
|
|
|
|
|
|
|
|
switch(mode) {
|
|
|
|
case SourceRead:
|
|
|
|
while(length--) write(sourceData[outputOffset]);
|
|
|
|
break;
|
|
|
|
case TargetRead:
|
|
|
|
while(length--) write(read());
|
|
|
|
break;
|
|
|
|
case SourceCopy:
|
|
|
|
case TargetCopy:
|
2015-12-14 09:41:06 +00:00
|
|
|
int offset = decode();
|
2011-08-18 13:58:27 +00:00
|
|
|
bool negative = offset & 1;
|
|
|
|
offset >>= 1;
|
|
|
|
if(negative) offset = -offset;
|
|
|
|
|
|
|
|
if(mode == SourceCopy) {
|
|
|
|
sourceRelativeOffset += offset;
|
|
|
|
while(length--) write(sourceData[sourceRelativeOffset++]);
|
|
|
|
} else {
|
|
|
|
targetRelativeOffset += offset;
|
|
|
|
while(length--) write(targetData[targetRelativeOffset++]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-14 09:41:06 +00:00
|
|
|
uint32 modifySourceChecksum = 0, modifyTargetChecksum = 0, modifyModifyChecksum = 0;
|
|
|
|
for(uint n = 0; n < 32; n += 8) modifySourceChecksum |= read() << n;
|
|
|
|
for(uint n = 0; n < 32; n += 8) modifyTargetChecksum |= read() << n;
|
|
|
|
uint32 checksum = modifyChecksum.value();
|
|
|
|
for(uint n = 0; n < 32; n += 8) modifyModifyChecksum |= read() << n;
|
2011-08-18 13:58:27 +00:00
|
|
|
|
2015-12-14 09:41:06 +00:00
|
|
|
uint32 sourceChecksum = Hash::CRC32(sourceData, modifySourceSize).value();
|
2011-08-18 13:58:27 +00:00
|
|
|
|
|
|
|
if(sourceChecksum != modifySourceChecksum) return result::source_checksum_invalid;
|
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
|
|
|
if(targetChecksum.value() != modifyTargetChecksum) return result::target_checksum_invalid;
|
2011-08-18 13:58:27 +00:00
|
|
|
if(checksum != modifyModifyChecksum) return result::patch_checksum_invalid;
|
|
|
|
|
|
|
|
return result::success;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|