News: 11 March 2016 - Forum Rules
Current Moderators - DarkSol, KingMike, MathOnNapkins, Azkadellia, Danke

Author Topic: Cartographer and bit shifting  (Read 5887 times)

Klarth

  • Sr. Member
  • ****
  • Posts: 495
    • View Profile
Re: Cartographer and bit shifting
« Reply #20 on: May 11, 2016, 04:14:29 am »
I believe your file I/O is incorrect. If you compile in debug mode, your debugger should be able to give you more specific information (ie. specific line or a call stack). I don't really have experience outside of Visual Studio's debugger though.

Code: [Select]
outputFile.read(tocWordBuffer, tocPosition + 0x04);
tocWord = *reinterpret_cast<uint32_t*>(tocWordBuffer);
That code means that you read a total of tocPosition + 4 bytes. Which will certainly buffer overflow your 4 byte array. Since you are no longer reading the entire file into a byte array and processing the byte array, you can simplify the code to not use pointers, awkward casts, or a buffer variable.

Code: [Select]
uint32_t tocWord;
...
outputFile.read(&tocWord, 4); // Can also use sizeof(tocWord) or sizeof(uint32_t) instead of 4

theflyingzamboni

  • Jr. Member
  • **
  • Posts: 92
    • View Profile
Re: Cartographer and bit shifting
« Reply #21 on: May 11, 2016, 02:27:26 pm »
I believe your file I/O is incorrect. If you compile in debug mode, your debugger should be able to give you more specific information (ie. specific line or a call stack). I don't really have experience outside of Visual Studio's debugger though.

Code: [Select]
outputFile.read(tocWordBuffer, tocPosition + 0x04);
tocWord = *reinterpret_cast<uint32_t*>(tocWordBuffer);
That code means that you read a total of tocPosition + 4 bytes. Which will certainly buffer overflow your 4 byte array. Since you are no longer reading the entire file into a byte array and processing the byte array, you can simplify the code to not use pointers, awkward casts, or a buffer variable.

Code: [Select]
uint32_t tocWord;
...
outputFile.read(&tocWord, 4); // Can also use sizeof(tocWord) or sizeof(uint32_t) instead of 4

Oh wow, you're totally right. Not sure why I wrote it that way there, because I did it the correct way everywhere else. I apparently also told it to write tocPosition bytes for filesize instead of 4. It's writing garbage if I change the filesize, but at least now it's only 4 bytes of it.

I just tried doing &tocWord like you wrote, but it causes a compile-time error:

argument of type "uint32_t *" is incompatible with parameter of type "const char *"

EDIT: Nevermind, I got it. I just needed to add a reinterpret cast to it. Also, I wanted to write filesize, not the current word. I guess I was tired when I wrote that part. I think the program works now.

Onward to messing with TableLib and Atlas to try and make them work with TLoD for dumping/insertion. I feel like the dumper will actually be harder, but maybe I'm wrong. Atlas I should be able to do as you said before and make a new pointer type. I'm also thinking I'll need to dump text and text box dimensions sequentially, so in the dumper I'll need to merge their two pointer tables together, meaning I'll have to add something to Atlas to alternately write pointers to two separate tables.
« Last Edit: May 11, 2016, 03:32:18 pm by theflyingzamboni »
ROM wasn't hacked in a day.