# Glitch City Laboratories Archives

Glitch City Laboratories closed on 1 September 2020 (announcement). This is an archived copy of a thread from Glitch City Laboratories Forums.

You can join Glitch City Research Institute to ask questions or discuss current developments.

You may also download the archive of this forum in .tar.gz, .sql.gz, or .sqlite.gz formats.

## Pokémon Discussion

### Crystal: Unused party sanitisation function - Page 1

Previous Page

- 1

Next Page

#### Crystal: Unused party sanitisation function

Posted by: Stackout
Date: 2017-10-03 11:37:50
I was looking in pokecrystal for something unrelated and this caught my eye.
As the filename would have you expect, it's in EN Crystal at [tt]$13A47 (4:7A47)[/tt]. (It's in JP Crystal at [tt]$13BB3 (4:7BB3)[/tt]). That's near the end of the bank, located after some Bug-Catching Contest-related stuff, and before a function that uses a lookup table to find square roots. It appears to not be in G/S (and given that it calls a function in a bank related to mobile code, that's not unexpected).
It is completely unreferenced and unused in at least JP and EN Crystal.
Here's what it does:

[li]If the number of Pokémon in the party is over 6, set it to 6[/li]
[li]The party species bytes are checked. If a byte is an invalid species ([tt]$00, $FC, $FE, $FF[/tt]), change that species byte and the first species byte of that Pokémon structure to Smeargle ([tt]$EB[/tt]).[/li]
[li]For all Pokémon in the party:

[li]If the second species byte in the Pokémon structure is invalid ([tt]$FD[/tt] (Egg) is also considered invalid here), change it to Smeargle.[/li]
[li]If the Pokémon's level is below 2, set it to 2; if it is over 100, set it to 100. Recalculate the Pokémon's stats, no matter whether or not the level was modified.[/li]
[li]If the Pokémon's nickname is unterminated after 6 bytes, terminate it by setting the 6th byte to [tt]$50[/tt]; if the nickname has any invalid characters (defined here as anything that is not [tt]$00, $05 - $13, $19 - $1C, $26 - $34, $3A - $3E, $40 - $48, $60 - $FF[/tt]; a function in a bank related to mobile functionality is called to do this), set it to the Pokémon species name (if the Pokémon is an Egg, then set it to [tt]""[/tt] ("Tamago"/Egg) followed by [tt]50 50 50[/tt] instead)[/li]
[li]If the Pokémon's Original Trainer name is unterminated after 6 bytes, terminate it by setting the 6th byte to [tt]$50[/tt]; if it has any invalid characters, set it to the player's name[/li]
[li]If the Pokémon has an invalid move in slot 1 ([tt]$00, $FC, $FD, $FE, $FF[/tt]), replace it with Pound. If there is an invalid move in slot 2, 3, or 4, delete all moves starting from that slot(!!!).[/li]

[/li]

Note that this code is only half localised in non-JP - it assumes nicknames and OT names are 6 bytes long, the list of characters considered valid assumes a Japanese game, and if an Egg's nickname contains invalid characters, it gets set to ""; however, when removing a Pokémon's nickname, or changing its OT name, it copies the correct number of bytes for the localisation.
Given that this calls a function in a mobile-related bank, this function may have been intended to have been used to force the legitimacy of party Pokémon when using mobile-related features. Of course, it may have just been debug code.

#### Re: Crystal: Unused party sanitisation function

Posted by: SatoMew
Date: 2017-10-03 19:52:03
Interesting find. Mayhaps it was debug code for the PCC's Trade Corner since the attendant does inform the player that either they only have 1 Pokémon in the party or that they are trying to send an Egg or abnormal Pokémon.

#### Re: Crystal: Unused party sanitisation function

Posted by: Stackout
Date: 2017-10-03 21:32:55
Pretty sure it is debug code now.
I searched for other places where the string validity check function is called.
It's called in three places. One of those seems to be unused, and all incorporate parts of the checks detailed in the OP.
The unused occurrence and first used occurrence are in Battle Tower code, sanitising Battle Tower Pokémon after they are read. (The unused occurrence seems to have just been dummied out, as it is directly after the used code ends.)
This code sanitises Pokémon nicknames, replaces any OT name with bad characters with "CHRIS" (only one terminator this time), makes sure all nicknames and the OT name are terminated, and sanitises invalid moves in the same way as detailed in the OP (except here, move [tt]$00[/tt] is never considered invalid).
The unused half of this function replaces invalid Pokémon species ([tt]$FD[/tt] being considered invalid) with Smeargle ([tt]$EB[/tt]). Instead of a simple greater-than-or-equal check, it checks for equality against each invalid species value in turn. Pokémon levels are also checked, but the maximum level instead of being hardcoded is taken from SRAM at [tt]5:B2FB[/tt].
The last occurrence is inside mobile-related code. Every single string (nickname, OT, mail, mail author) is checked for invalid bytes and termination (where the terminator is [tt]$4E[/tt] for some reason) within the correct length, by bankswitched calls. If one of these checks fails, the offending string is replaced with a default string by a bankswitched call.
Here's a list of default strings used:
OT name, mail author: "" Chris followed by two terminators
Pokémon nickname: "?????" unterminated(!!!)
Mail text: "にちは" "Konnichiwa" Hello followed by one terminator
The default strings are terminated by [tt]$50[/tt] as usual.
After checking the strings, an "item" at [tt]$C60E[/tt] is checked, if it's [tt]$FF[/tt] it gets replaced by [tt]$00[/tt]. Finally, the exact same level checks as in the OP are performed.

#### Re: Crystal: Unused party sanitisation function

Posted by: Háčky
Date: 2017-10-04 00:52:59

> The unused occurrence and first used occurrence are in Battle Tower code, sanitising Battle Tower Pokémon after they are read. (The unused occurrence seems to have just been dummied out, as it is directly after the used code ends.)
> This code sanitises Pokémon nicknames, replaces any OT name with bad characters with "CHRIS" (only one terminator this time), makes sure all nicknames and the OT name are terminated, and sanitises invalid moves in the same way as detailed in the OP (except here, move [tt]$00[/tt] is never considered invalid).
> The unused half of this function replaces invalid Pokémon species ([tt]$FD[/tt] being considered invalid) with Smeargle ([tt]$EB[/tt]). Instead of a simple greater-than-or-equal check, it checks for equality against each invalid species value in turn. Pokémon levels are also checked, but the maximum level instead of being hardcoded is taken from SRAM at [tt]5:B2FB[/tt].

Both of these checks are applied to Battle Tower opponents in the Japanese version. 5:B2FB is the chosen level of the Battle Tower challenge. Oddly, move sets are sanitized twice using slightly different criteria, the difference being that [tt]ValidateBTParty[/tt] checks if the first move slot is empty and fills it with Pound if it is, while [tt]CheckBTMonMovesForErrors[/tt], called from [tt]ReadBTTrainerParty[/tt], erases any moves following an invalid or empty second or third slot. The cumulative effect seems to be identical to the unused move check in bank 4.
It seems Game Freak relied solely on this client-side validation at first; the Pokémon Battle Historia reports that Pokémon with illegal move sets were found in level 100 Battle Rooms in February 2001, until some server-side checks were introduced in early March. But even in late 2002, Kakeru complained of encountering Blastoise which knew both Counter and Mirror Coat (it can learn Mirror Coat as an Egg move, and Counter using the Generation I TM, but a Pokémon with Mirror Coat cant be traded through the Time Capsule).

> The last occurrence is inside mobile-related code. Every single string (nickname, OT, mail, mail author) is checked for invalid bytes and termination (where the terminator is [tt]$4E[/tt] for some reason) within the correct length, by bankswitched calls. If one of these checks fails, the offending string is replaced with a default string by a bankswitched call.

I think this one is for Pokémon received from the Trade Corner.

#### Re: Crystal: Unused party sanitisation function

Posted by: SatoMew
Date: 2017-10-04 09:50:15

> This code sanitises Pokémon nicknames, replaces any OT name with bad characters with "CHRIS" (only one terminator this time), makes sure all nicknames and the OT name are terminated, and sanitises invalid moves in the same way as detailed in the OP (except here, move [tt]$00[/tt] is never considered invalid).

If it corresponds to "CHRIS" in English Crystal, then the localizers assumed the OT was male as the default name for both male and female players in Japanese Crystal is , which in English Crystal is rendered as "CHRIS" for boys and "KRIS" for girls. The game assigns these to the player when they provide a blank string on the name input screen of the new game tutorial.

> Here's a list of default strings used:
> OT name, mail author: "" Chris followed by two terminators
> Pokémon nickname: "?????" unterminated(!!!)
> Mail text: "にちは" "Konnichiwa" Hello followed by one terminator

Wait, does the game really spell out ? That's an awkward combination of hiragana and katakana. And oops for not terminating that :P

#### Re: Crystal: Unused party sanitisation function

Posted by: Háčky
Date: 2017-10-04 17:04:15

> Wait, does the game really spell out ? That's an awkward combination of hiragana and katakana. And oops for not terminating that :P

The Generation I and II games use the same character ($D8) for and (likewise with $CD for and へ); the disassemblies treat it as the hiragana by default.

#### Re: Crystal: Unused party sanitisation function

Posted by: SatoMew
Date: 2017-10-04 17:24:45

> The Generation I and II games use the same character ($D8) for and (likewise with $CD for and へ); the disassemblies treat it as the hiragana by default.

Oh, right. Thanks, Háčky! ^^

#### Re: Crystal: Unused party sanitisation function

Posted by: Torchickens
Date: 2019-09-29 19:20:49

> Pokémon nickname: "?????" unterminated(!!!)
> Mail text: "にちは" "Konnichiwa" Hello followed by one terminator
> The default strings are terminated by [tt]$50[/tt] as usual.
> After checking the strings, an "item" at [tt]$C60E[/tt] is checked, if it's [tt]$FF[/tt] it gets replaced by [tt]$00[/tt]. Finally, the exact same level checks as in the OP are performed.

> I think this one is for Pokémon received from the Trade Corner.

*Bump*. When I received a Pokémon through the JP version 05:A800>04 method with a bad nickname character, the nickname changed to "?????", however this string was terminated (bytes were E6 E6 E6 E6 E6 50) so I'm unsure if Wack0 made a mistake (or maybe this only applies to the unused EN code). I used an invalid OT character and the OT changed to as expected. I don't know how to test the others yet.
Edit: I just tried calling the Function11b6b4 in the disassembly link Wack0 posted on English Crystal. For bad OTs it gives the untranslated string, showing mojibake "H M". For bad nicknames I get "?????" unterminated as Wack0 said. However, I got E6 E6 E6 E6 E6 BA DE C6 C1 CA 50, so "?????にちは" (translated: "????? hello"), seemingly from the bad mail text handler. This sequence of bytes is rendered as "????? " and the part after ????? is terminated while ????? itself isn't.
Previous Page

- 1

Next Page