Skip to content

Commit

Permalink
Improve whitelisted word recognition
Browse files Browse the repository at this point in the history
  • Loading branch information
Pseurae committed Oct 29, 2024
1 parent b81b139 commit 95754f1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
8 changes: 5 additions & 3 deletions tools/decap/decap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,16 @@ int main(int argc, char *argv[])
f.ProcessStringPointerArray(0x1e79b0, 2, 8); // Boy/Girl
f.ProcessStringPointerArray(0x1e79c0, 5, 8); // Male preset names
f.ProcessStringPointerArray(0x1e79e8, 5, 8); // Female preset names
f.ProcessStringPointerArray(0x376d04, 6); // Stat labels
f.ProcessStringPointerArray(0x3c5564 + 20, 349, 44); // Item descriptions
f.ProcessStringPointerArray(0x3c1004, 25); // Nature names
f.ProcessStringPointerArray(0x3c1068, 8); // Summary Screen headers
f.ProcessStringPointerArray(0x39f494, 10, 8); // Party menu options
f.ProcessStringPointerArray(0x376d24, 10, 8); // Start menu options
f.ProcessStringPointerArray(0x1fa110, 78); // Ability descriptions
f.ProcessStringPointerArray(0x3e73c4 + 4, 88, 8); // Map names

f.SetWhitelist(&WhitelistEntries);
f.ProcessStringPointerArray(0x376d04, 6); // Stat labels
f.ProcessStringPointerArray(0x3c5564 + 20, 349, 44); // Item descriptions
f.ProcessStringPointerArray(0x1fa110, 78); // Ability descriptions
f.ProcessStringPointerArray(0x3c09d8, 354); // Move descriptions

f.Save(argv[2]);
Expand Down
26 changes: 11 additions & 15 deletions tools/decap/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,22 @@ void File::Save(const std::string &filename)
fclose(fp);
}

struct Word
{
unsigned char text[10];
unsigned int len;
};

static const Word sWhitelistEntries[] = {
const std::vector<Word> WhitelistEntries = {
{ {0xC2, 0xC7}, 2 }, // HM
{ {0xCE, 0xC7}, 2 }, // TM
{ {0xC2, 0xCA}, 2 }, // HP
{ {0xCA, 0xCA}, 2 }, // PP
};

bool CheckIfWhitelisted(unsigned char *buffer, unsigned int idx)
unsigned int CheckIfWhitelisted(const std::vector<Word>& whitelist, unsigned char *buffer, unsigned int idx)
{
for (int i = 0; i < (int)(sizeof(sWhitelistEntries) / sizeof(*sWhitelistEntries)); ++i)
for (const auto &word : WhitelistEntries)
{
if (memcmp(&buffer[idx], sWhitelistEntries[i].text, sWhitelistEntries[i].len) == 0)
{
return true;
}
if (memcmp(&buffer[idx], word.text, word.len) == 0)
return word.len;
}

return false;
return 0;
}

// 0x0 is space
Expand All @@ -81,8 +73,12 @@ void File::ProcessString(int location, int max_size)
int idx = location + j;
if (idx > m_Size) FATAL_ERROR("Index %d is greater than file size (%ld)\n", idx, m_Size);

if (CheckIfWhitelisted(m_Buffer, idx))
unsigned int whitelistLength;
if (m_Whitelist != nullptr && (whitelistLength = CheckIfWhitelisted(*m_Whitelist, m_Buffer, idx)) > 0)
{
j += whitelistLength - 1;
continue;
}

unsigned char c = m_Buffer[idx];

Expand Down
11 changes: 11 additions & 0 deletions tools/decap/file.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
#pragma once

#include <string>
#include <vector>

struct Word
{
unsigned char text[10];
unsigned int len;
};

extern const std::vector<Word> WhitelistEntries;

class File
{
Expand All @@ -14,7 +23,9 @@ class File
void ProcessStringArray(int location, int size, int count);
void ProcessStringArray(int location, int size, int count, int stride);
void ProcessStringPointerArray(int location, int count, int stride = 4);
inline void SetWhitelist(const std::vector<Word> *whitelist) { m_Whitelist = whitelist; }
private:
unsigned char *m_Buffer;
long m_Pos, m_Size;
const std::vector<Word> *m_Whitelist;
};

0 comments on commit 95754f1

Please sign in to comment.