forked from Luca1991/NDSFactory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathndsfactory.cpp
166 lines (137 loc) · 4.75 KB
/
ndsfactory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <string>
#include <iostream>
#include <fstream>
#include <cmath>
#include "ndsfactory.h"
#include "fatstruct.h"
#include "crctable.h"
NDSFactory::NDSFactory()
{
}
bool NDSFactory::loadRomHeader(const std::string& romPath, std::vector<char>& romHeader)
{
std::streampos headerSize = sizeof(NDSHeader);
std::ifstream romFile (romPath, std::ios::in|std::ios::binary|std::ios::ate);
if (romFile.is_open())
{
romHeader.resize(static_cast<unsigned long>(headerSize));
romFile.seekg (0, std::ios::beg);
romFile.read (romHeader.data(), headerSize);
romFile.close();
return true;
}
return false;
}
bool NDSFactory::dumpDataFromFile(const std::string& romPath, const std::string& savePath, uint32_t startAddr, uint32_t size)
{
std::ifstream romFile (romPath, std::ios::in|std::ios::binary|std::ios::ate);
std::ofstream savedFile (savePath, std::ios::out|std::ios::binary|std::ios::ate);
if (romFile.is_open() && savedFile.is_open())
{
std::vector<char> dumpBuffer(size);
romFile.seekg (startAddr, std::ios::beg);
romFile.read (dumpBuffer.data(), size);
romFile.close();
savedFile.write(dumpBuffer.data(), size);
savedFile.close();
return true;
}
return false;
}
bool NDSFactory::readBytesFromFile(std::vector<char>& byteBuffer, const std::string& romPath, uint32_t startAddr, uint32_t size)
{
std::ifstream romFile (romPath, std::ios::in|std::ios::binary|std::ios::ate);
if (romFile.is_open())
{
romFile.seekg (startAddr, std::ios::beg);
romFile.read (byteBuffer.data(), size);
romFile.close();
return true;
}
return false;
}
bool NDSFactory::writeSectionToFile(const std::string& sectionPath, const std::string& savePath, uint32_t startAddr, uint32_t size)
{
std::ifstream sectionFile (sectionPath, std::ios::in|std::ios::binary|std::ios::ate);
std::ofstream savedFile (savePath, std::ios::out|std::ios::binary|std::ios::app);
if (sectionFile.is_open() && savedFile.is_open())
{
std::vector<char> dumpBuffer(size);
sectionFile.seekg (0, std::ios::beg);
sectionFile.read (dumpBuffer.data(), size);
sectionFile.close();
savedFile.seekp(startAddr, std::ios::beg);
savedFile.write(dumpBuffer.data(), size);
savedFile.close();
return true;
}
return false;
}
bool NDSFactory::writeBytesToFile(std::vector<char>& byteBuffer, const std::string& savePath, uint32_t startAddr, uint32_t size)
{
std::ofstream savedFile (savePath, std::ios::out|std::ios::binary|std::ios::app);
if (savedFile.is_open())
{
savedFile.seekp(startAddr, std::ios::beg);
savedFile.write(byteBuffer.data(), size);
savedFile.close();
return true;
}
return false;
}
bool NDSFactory::writePaddingToFile(char paddingChar, const std::string& filePath, uint32_t startAddr, const uint32_t size)
{
std::vector<char> paddingBytes(size, paddingChar);
return writeBytesToFile(paddingBytes, filePath, startAddr, size);
}
int NDSFactory::getCardSizeInBytes(int cardType)
{
return static_cast<int>(pow(2, 17 + cardType));
}
bool NDSFactory::checkArm9FooterPresence(const std::string& sectionPath, uint32_t size)
{
std::ifstream sectionFile (sectionPath, std::ios::in|std::ios::binary|std::ios::ate);
if (sectionFile.is_open())
{
long sectionRealSize = sectionFile.tellg();
sectionFile.close();
if (sectionRealSize >= size + Arm9FooterSize)
{
return true;
}
}
return false;
}
bool NDSFactory::patchFat(const std::string& fatSectionPath, uint32_t shiftSize, const std::string& savePath)
{
std::vector<char> fatBytes;
std::ifstream sectionFile (fatSectionPath, std::ios::in|std::ios::binary|std::ios::ate);
if (!sectionFile.is_open())
return false;
long sectionSize = sectionFile.tellg();
fatBytes.resize(static_cast<unsigned long>(sectionSize));
sectionFile.seekg (0, std::ios::beg);
sectionFile.read (fatBytes.data(), sectionSize);
sectionFile.close();
FatRange* pfatrange = reinterpret_cast<FatRange*>(fatBytes.data());
for(size_t i = 0; i < fatBytes.size(); i += sizeof(FatRange), pfatrange++) {
pfatrange->startAddr += shiftSize;
pfatrange->endAddr += shiftSize;
}
return writeBytesToFile(fatBytes, savePath, 0, static_cast<uint32_t>(sectionSize));
}
uint16_t NDSFactory::calcHeaderCrc16(const std::vector<char>& romHeader)
{
uint8_t loc;
uint16_t crc = 0xFFFF;
size_t size = 0x15E;
size_t index = 0;
while (size--)
{
loc = static_cast<unsigned char>(romHeader[index] ^ crc);
index++;
crc >>= 8;
crc ^= lCRCTable[loc];
}
return crc;
}