mirror of
https://github.com/jakcron/nstool.git
synced 2024-12-22 18:55:29 +00:00
[fnd] overloaded fnd::io::readFile to allow specifying an offset and size.
This commit is contained in:
parent
b5c6cf6cb4
commit
7167477874
|
@ -7,6 +7,7 @@ namespace fnd
|
||||||
namespace io
|
namespace io
|
||||||
{
|
{
|
||||||
void readFile(const std::string& path, MemoryBlob& blob);
|
void readFile(const std::string& path, MemoryBlob& blob);
|
||||||
|
void readFile(const std::string& path, size_t offset, size_t len, MemoryBlob& blob);
|
||||||
void writeFile(const std::string& path, const MemoryBlob& blob);
|
void writeFile(const std::string& path, const MemoryBlob& blob);
|
||||||
void writeFile(const std::string& path, const byte_t* data, size_t len);
|
void writeFile(const std::string& path, const byte_t* data, size_t len);
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,6 +41,48 @@ void io::readFile(const std::string& path, MemoryBlob & blob)
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void fnd::io::readFile(const std::string& path, size_t offset, size_t len, MemoryBlob& blob)
|
||||||
|
{
|
||||||
|
FILE* fp;
|
||||||
|
size_t filesz, filepos;
|
||||||
|
|
||||||
|
if ((fp = fopen(path.c_str(), "rb")) == NULL)
|
||||||
|
{
|
||||||
|
throw Exception(kModuleName, "Failed to open \"" + path + "\": does not exist");
|
||||||
|
}
|
||||||
|
|
||||||
|
fseek(fp, 0, SEEK_END);
|
||||||
|
filesz = ftell(fp);
|
||||||
|
rewind(fp);
|
||||||
|
fseek(fp, offset, SEEK_SET);
|
||||||
|
|
||||||
|
if (filesz < len || filesz < offset || filesz < (offset + len))
|
||||||
|
{
|
||||||
|
throw Exception(kModuleName, "Failed to open \"" + path + "\": file to small");
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
blob.alloc(len);
|
||||||
|
} catch (const fnd::Exception& e)
|
||||||
|
{
|
||||||
|
fclose(fp);
|
||||||
|
throw fnd::Exception(kModuleName, "Failed to allocate memory for file: " + std::string(e.what()));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (filepos = 0; len > kBlockSize; len -= kBlockSize, filepos += kBlockSize)
|
||||||
|
{
|
||||||
|
fread(blob.getBytes() + filepos, 1, kBlockSize, fp);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (len)
|
||||||
|
{
|
||||||
|
fread(blob.getBytes() + filepos, 1, len, fp);
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(fp);
|
||||||
|
}
|
||||||
|
|
||||||
void io::writeFile(const std::string& path, const MemoryBlob & blob)
|
void io::writeFile(const std::string& path, const MemoryBlob & blob)
|
||||||
{
|
{
|
||||||
writeFile(path, blob.getBytes(), blob.getSize());
|
writeFile(path, blob.getBytes(), blob.getSize());
|
||||||
|
|
|
@ -250,7 +250,7 @@ void decryptXciHeader(const byte_t* src, byte_t* dst)
|
||||||
const byte_t* src_iv = ((const sXciHeader*)src)->encryption_iv;
|
const byte_t* src_iv = ((const sXciHeader*)src)->encryption_iv;
|
||||||
byte_t iv[crypto::aes::kAesBlockSize];
|
byte_t iv[crypto::aes::kAesBlockSize];
|
||||||
|
|
||||||
for (int i = 0; i < crypto::aes::kAesBlockSize; i++)
|
for (size_t i = 0; i < crypto::aes::kAesBlockSize; i++)
|
||||||
{
|
{
|
||||||
iv[i] = src_iv[15 - i];
|
iv[i] = src_iv[15 - i];
|
||||||
}
|
}
|
||||||
|
@ -270,21 +270,13 @@ int main(int argc, char** argv)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
FILE* fp = fopen(argv[1], "rb");
|
|
||||||
if (fp == nullptr)
|
|
||||||
{
|
|
||||||
printf("Failed to open file.\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
fnd::MemoryBlob xciFile;
|
fnd::MemoryBlob xciFile;
|
||||||
xciFile.alloc(0x200);
|
fnd::io::readFile(argv[1], 0x100, 0x100, xciFile);
|
||||||
fread(xciFile.getBytes(), 0x200, 1, fp);
|
|
||||||
fclose(fp);
|
|
||||||
|
|
||||||
sXciHeader hdr;
|
sXciHeader* hdr = (sXciHeader*)xciFile.getBytes();
|
||||||
decryptXciHeader(xciFile.getBytes() + 0x100, (byte_t*)&hdr);
|
decryptXciHeader(xciFile.getBytes(), xciFile.getBytes());
|
||||||
printXciHeader(hdr, true);
|
printXciHeader(*hdr, true);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
Reference in a new issue