Skip to content

Commit d497d16

Browse files
committed
Properly check the size that's going to be written
The extra 0x200 bytes from the banner, icon, and size need to be included in the check. Also renamed some variables and moved a couple things.
1 parent e8eb6e0 commit d497d16

File tree

1 file changed

+12
-22
lines changed

1 file changed

+12
-22
lines changed

ttyd-tools/rel/source/memcard.cpp

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
#include <gc/card.h>
66
#include <ttyd/string.h>
7-
#include <ttyd/__mem.h>
87

98
#include <cstdio>
109

@@ -173,8 +172,11 @@ int32_t writeSettings(char *description, char *fileName,
173172
return ReturnCode;
174173
}
175174

176-
uint32_t SettingsStructSize;
177-
uint32_t SettingsStructSizeAdjusted;
175+
// Get the size thats going to be written
176+
uint32_t FileSize = sizeof(struct SettingsStruct) + 0x200;
177+
178+
// Adjust the file size to be in multiples of 0x2000, rounding up
179+
uint32_t FileSizeAdjusted = (FileSize + 0x2000 - 1) & ~(0x2000 - 1);
178180

179181
// Open the settings file if it exists
180182
ReturnCode = gc::card::CARDOpen(CARD_SLOTA, fileName, fileInfo);
@@ -197,22 +199,16 @@ int32_t writeSettings(char *description, char *fileName,
197199
}
198200

199201
// Get the size of the file
200-
uint32_t CurrentFileSize = *reinterpret_cast<uint32_t *>(&tempFileData[0x40]);
202+
uint32_t StoredFileSize = *reinterpret_cast<uint32_t *>(&tempFileData[0x40]);
201203

202204
// Delete the data that holds the size, as it's not needed anymore
203205
delete[] (tempFileData);
204206

205207
// Adjust the file size to be in multiples of 0x2000, rounding up
206-
uint32_t CurrentFileSizeAdjusted = (CurrentFileSize + 0x2000 - 1) & ~(0x2000 - 1);
207-
208-
// Get the size thats going to be written
209-
SettingsStructSize = sizeof(struct SettingsStruct);
210-
211-
// Adjust the file size to be in multiples of 0x2000, rounding up
212-
SettingsStructSizeAdjusted = (SettingsStructSize + 0x2000 - 1) & ~(0x2000 - 1);
208+
uint32_t StoredFileSizeAdjusted = (StoredFileSize + 0x2000 - 1) & ~(0x2000 - 1);
213209

214210
// Make sure the size being written does not exceed the current size
215-
if (SettingsStructSizeAdjusted > CurrentFileSizeAdjusted)
211+
if (FileSizeAdjusted > StoredFileSizeAdjusted)
216212
{
217213
// The new size exceeds the current size, so a new file must be made created
218214
// Close the file
@@ -250,12 +246,6 @@ int32_t writeSettings(char *description, char *fileName,
250246
{
251247
return ReturnCode;
252248
}
253-
254-
// Get the size thats going to be written
255-
SettingsStructSize = sizeof(struct SettingsStruct);
256-
257-
// Adjust the file size to be in multiples of 0x2000, rounding up
258-
SettingsStructSizeAdjusted = (SettingsStructSize + 0x2000 - 1) & ~(0x2000 - 1);
259249
break;
260250
}
261251
default:
@@ -265,13 +255,13 @@ int32_t writeSettings(char *description, char *fileName,
265255
}
266256

267257
// Set up the memory to be written to the file
268-
char *MiscData = new char[SettingsStructSizeAdjusted];
269-
clearMemory(MiscData, SettingsStructSizeAdjusted);
258+
char *MiscData = new char[FileSizeAdjusted];
259+
clearMemory(MiscData, FileSizeAdjusted);
270260

271261
// Copy the name, description, and file size into the memory
272262
ttyd::string::strcpy(MiscData, "Paper Mario");
273263
ttyd::string::strcpy(&MiscData[0x20], description);
274-
*reinterpret_cast<uint32_t *>(&MiscData[0x40]) = SettingsStructSize + 0x200;
264+
*reinterpret_cast<uint32_t *>(&MiscData[0x40]) = FileSize;
275265

276266
// Set up the struct to hold the variables to store
277267
SettingsStruct *Settings = reinterpret_cast<SettingsStruct *>(&MiscData[0x200]);
@@ -296,7 +286,7 @@ int32_t writeSettings(char *description, char *fileName,
296286
Settings->DisplaysButtonCombos[ONSCREEN_TIMER + 1] = OnScreenTimer.ButtonCombo[1];
297287

298288
// Write the data to the file
299-
ReturnCode = writeToCard(fileInfo, MiscData, SettingsStructSizeAdjusted, 0x2000, nullptr);
289+
ReturnCode = writeToCard(fileInfo, MiscData, FileSizeAdjusted, 0x2000, nullptr);
300290

301291
delete[] (MiscData);
302292
gc::card::CARDClose(fileInfo);

0 commit comments

Comments
 (0)