Versión funcional
This commit is contained in:
@@ -7,10 +7,16 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
ifstream inputFile;
|
|
||||||
string filePath = "";
|
string filePath = "";
|
||||||
vector<unsigned char> buffer;
|
vector<unsigned char> buffer;
|
||||||
|
|
||||||
|
int desp = 0x2000;
|
||||||
|
int numBytes = 0x2000;
|
||||||
|
int firstByte = desp + 0x200;
|
||||||
|
int lastByte = desp + numBytes - 1;
|
||||||
|
int checksum1 = desp + 0x270;
|
||||||
|
int checksum2 = desp + 0x271;
|
||||||
|
|
||||||
void checkParameters(int argc, char *argv[])
|
void checkParameters(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
if (argc != 2)
|
if (argc != 2)
|
||||||
@@ -24,16 +30,34 @@ void checkParameters(int argc, char *argv[])
|
|||||||
|
|
||||||
void loadFile(string s)
|
void loadFile(string s)
|
||||||
{
|
{
|
||||||
inputFile.open(s, ios::binary);
|
ifstream inputFile(s, ios::binary);
|
||||||
if (!inputFile.good())
|
if (inputFile.good())
|
||||||
|
buffer.assign(istreambuf_iterator<char>(inputFile), {});
|
||||||
|
else
|
||||||
{
|
{
|
||||||
cout << "No se puede abrir el archivo: " << s << endl;
|
cout << "No se puede abrir el archivo: " << s << endl;
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
inputFile.close();
|
||||||
|
}
|
||||||
|
|
||||||
// Copia el stream al buffer
|
void saveFile(string s)
|
||||||
vector<unsigned char> temp(std::istreambuf_iterator<char>(inputFile), {});
|
{
|
||||||
buffer = temp;
|
ofstream outputFile(s, ios::binary);
|
||||||
|
if (outputFile.good())
|
||||||
|
{
|
||||||
|
for (auto i : buffer)
|
||||||
|
{
|
||||||
|
outputFile << i;
|
||||||
|
}
|
||||||
|
cout << "Fichero guardado: " << s << endl;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cout << "No se puede abrir el archivo: " << s << endl;
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
outputFile.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
void printBuffer()
|
void printBuffer()
|
||||||
@@ -48,11 +72,31 @@ void printBuffer()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int calcChecksum()
|
||||||
|
{
|
||||||
|
int sum = 0;
|
||||||
|
for (int i = firstByte; i <= lastByte; i++)
|
||||||
|
{
|
||||||
|
if (i != checksum1 and i != checksum2)
|
||||||
|
sum += static_cast<unsigned>(buffer.at(i));
|
||||||
|
}
|
||||||
|
sum += 3328; // Corrige el checksum
|
||||||
|
buffer.at(checksum1) = sum % 256;
|
||||||
|
buffer.at(checksum2) = (sum >> 8) % 256;
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
checkParameters(argc, argv);
|
checkParameters(argc, argv);
|
||||||
loadFile(filePath);
|
loadFile(filePath);
|
||||||
printBuffer();
|
int sum = calcChecksum();
|
||||||
cout << "Fin del programa" << endl;
|
saveFile(filePath);
|
||||||
|
|
||||||
|
cout << hex;
|
||||||
|
cout << "0x" << checksum1 << ": 0x" << static_cast<unsigned>(buffer.at(checksum1)) << endl;
|
||||||
|
cout << "0x" << checksum2 << ": 0x" << static_cast<unsigned>(buffer.at(checksum2)) << endl;
|
||||||
|
cout << "checksum (0x" << firstByte << " .. 0x" << lastByte << "): 0x" << sum << endl;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user