Pokémon NDS Save File Checksum

From ProjectPokemon Wiki
Revision as of 21:26, 10 March 2009 by Sabresite (talk | contribs) (New page: Pokémon NDS uses a {{wplink|checksum|checksum}} at the footer each block in the save file to ensure consistent data. ==Diamong & Pearl usage== <code>...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Pokémon NDS uses a checksum at the footer each block in the save file to ensure consistent data.

Diamong & Pearl usage

filestream.Seek( 0xCF0E, SeekOrigin.Begin );
GetCheckSum( smallblock1 ); //small block 1
filestream.Seek( 0x1E2DE, SeekOrigin.Begin );
GetCheckSum( bigblock1 ); //big block 1
filestream.Seek( 0x4CF0E, SeekOrigin.Begin );
GetCheckSum( smallblock2 ); //small block 2
filestream.Seek( 0x5E2DE, SeekOrigin.Begin );
GetCheckSum( bigblock2 ); //big block 2

Platinum usage

filestream.Seek( 0xCF2A, SeekOrigin.Begin );
GetCheckSum( smallblock1 ); //small block 1
filestream.Seek( 0x1F10E, SeekOrigin.Begin );
GetCheckSum( bigblock1 ); //big block 1
filestream.Seek( 0x4CF2A, SeekOrigin.Begin );
GetCheckSum( smallblock2 ); //small block 2
filestream.Seek( 0x5F10E, SeekOrigin.Begin );
GetCheckSum( bigblock2 ); //big block 2

Determining the seeds

		public static int[] SeedTable;

		public static void GetSeeds()
		{
			SeedTable = new int[0x100];

			int index = 0;
			int result = 0;

			do
			{
				result = index << 8;
				int index2 = 0;
				do
				{
					if (((byte)(result>>8) & 0x80)!=0)
						result = (2 * result )^ 0x1021;
					else
						result *= 2;
				}
				while (++index < 8);

				seeds[index++] = (ushort)(result);
			} while (index <= 0xFF);
		}

Determining the checksum

		public ushort GetCheckSum( byte[] data )
		{
			int sum = 0xFFFF;

			for ( int i = 0; i < data.Length; i++ )
				sum = (sum << 8) ^ SeedTable[ (byte)(data[i] ^ (byte)(sum>>8)) ];

			return (ushort)sum;
		}