nsemu/NintendoObject.cpp

73 lines
2.1 KiB
C++
Raw Normal View History

2017-11-14 07:23:42 -05:00
/* nsemu - LGPL - Copyright 2017 rkx1209<rkx1209dev@gmail.com> */
#include "Nsemu.hpp"
#include <lz4.h>
2018-02-15 08:41:15 -05:00
NintendoObject::NintendoObject(string path) {
fp.open (path.c_str (), ios::in | ios::binary);
fp.seekg (0, ios_base::end);
2017-12-31 04:12:32 -05:00
length = (uint32_t) fp.tellg ();
fp.seekg (0);
}
2018-02-15 08:41:15 -05:00
NintendoObject::~NintendoObject() {
2017-12-31 04:12:32 -05:00
fp.close ();
2017-11-14 07:23:42 -05:00
}
char *decompress(ifstream &fp, uint32_t offset, uint32_t csize, uint32_t usize) {
2017-12-31 04:12:32 -05:00
fp.seekg (offset);
char *buf = new char[csize];
char *obuf = new char[usize];
2017-12-31 04:12:32 -05:00
fp.read (buf, csize);
assert (LZ4_decompress_safe (buf, obuf, csize, usize) == usize);
delete[] buf;
return obuf;
}
2018-03-05 08:20:48 -05:00
int Nso::load(Nsemu *nsemu) {
2017-11-14 07:23:42 -05:00
NsoHeader hdr;
2017-12-31 04:12:32 -05:00
fp.read ((char *) &hdr, sizeof(NsoHeader));
2018-02-15 08:38:40 -05:00
if (hdr.magic != byte_swap32_str ("NSO0")) {
2017-11-14 07:23:42 -05:00
return 0;
2017-12-31 04:12:32 -05:00
}
2017-11-14 07:23:42 -05:00
uint32_t size = hdr.dataLoc + hdr.dataSize + hdr.bssSize;
2017-12-31 04:12:32 -05:00
if (size & 0xfff) {
2017-11-14 07:23:42 -05:00
size = (size & ~0xfff) + 0x1000;
2017-12-31 04:12:32 -05:00
}
2018-03-17 13:17:00 -04:00
uint64_t base = 0;
2017-12-31 04:12:32 -05:00
char *text = decompress (fp, hdr.textOff, hdr.rdataOff - hdr.textOff, hdr.textSize);
2018-03-17 13:17:00 -04:00
if (!Memory::CopytoEmu (nsemu, (void *) text, base + hdr.textLoc, hdr.textSize)) {
2017-12-31 04:12:32 -05:00
delete[] text;
ns_abort ("Failed to copy to .text\n");
}
/* --- For test --- */
2018-04-19 09:26:12 -04:00
uint8_t *txt_dump = new uint8_t[hdr.textSize];
Memory::CopyfromEmu (nsemu, (void *) txt_dump, base + hdr.textLoc, hdr.textSize);
bindump ((uint8_t*)text, 105);
bindump (txt_dump, 105);
/* ---------------- */
delete[] text;
2018-03-17 13:17:00 -04:00
ns_print(".text[0x%x] size = 0x%x\n", hdr.textOff, hdr.textSize);
ns_print(".rdata[0x%x] size = 0x%x\n", hdr.rdataOff, hdr.rdataSize);
ns_print(".data[0x%x] size = 0x%x\n", hdr.dataOff, hdr.dataSize);
2017-12-31 04:12:32 -05:00
char *rdata = decompress (fp, hdr.rdataOff, hdr.dataOff - hdr.rdataOff, hdr.rdataSize);
2018-03-17 13:17:00 -04:00
if (!Memory::CopytoEmu (nsemu, (void *) rdata, base + hdr.rdataLoc, hdr.rdataSize)) {
2017-12-31 04:12:32 -05:00
delete[] rdata;
ns_abort ("Failed to copy to .rdata\n");
}
2017-11-14 07:23:42 -05:00
delete[] rdata;
2017-12-31 04:12:32 -05:00
char *data = decompress (fp, hdr.dataOff, length - hdr.dataOff, hdr.dataSize);
2018-03-17 13:17:00 -04:00
if (!Memory::CopytoEmu (nsemu, (void *) data, base + hdr.dataLoc, hdr.dataSize)) {
2017-12-31 04:12:32 -05:00
delete[] data;
ns_abort ("Failed to copy to .data\n");
}
2017-11-14 07:23:42 -05:00
delete[] data;
return size;
}
2018-03-05 08:20:48 -05:00
int Nro::load(Nsemu *nsemu) {
2017-12-31 04:12:32 -05:00
return 0;
}