Re: Gamecube GCM Lossless Encoder
Posted: Tue Mar 22, 2016 5:24 am
this is more recent and cleaned C# code from my new utility
uint blockcount - the full gc image is 1459978240 bytes long, it is divided in 262144 [0x40000] byte blocks
so first block is 0x00, second is 0x01 etc... until the end.
byte[] ID - 4-byte array, first 4-bytes from image, gamecode
byte DiscNumber - 0x06 byte from image (Disc 1 == 0x00, Disc 2 == 0x01, etc)
uint blockcount - the full gc image is 1459978240 bytes long, it is divided in 262144 [0x40000] byte blocks
so first block is 0x00, second is 0x01 etc... until the end.
byte[] ID - 4-byte array, first 4-bytes from image, gamecode
byte DiscNumber - 0x06 byte from image (Disc 1 == 0x00, Disc 2 == 0x01, etc)
Code: Select all
private byte[] GetJunkBlock(uint blockcount, byte[] ID, byte DiscNumber)
{
uint[] buffer = new uint[0x824];
byte[] GarbageBlock = new byte[0x40000];
int i = 0, j = 0;
uint sample = 0;
blockcount = blockcount * 8 * 0x1ef29123;
while (i != 0x40000)
{
if ((i & 0x00007fff) == 0)
{
sample = (((((uint)ID[2] << 0x8) | ID[1]) << 0x10) | ((uint)(ID[3] + ID[2]) << 0x8)) | (uint)(ID[0] + ID[1]);
sample = ((sample ^ DiscNumber) * 0x260bcd5) ^ blockcount;
a10002710(sample, ref buffer);
j = 0x208;
blockcount += 0x1ef29123;
}
j++;
if (j == 0x209)
{
a100026e0(ref buffer);
j = 0;
}
GarbageBlock[i] = (byte)(buffer[j] >> 0x18);
GarbageBlock[i + 1] = (byte)(buffer[j] >> 0x12);
GarbageBlock[i + 2] = (byte)(buffer[j] >> 0x8);
GarbageBlock[i + 3] = (byte)buffer[j];
i += 4;
}
return GarbageBlock;
}
private void a10002710(uint sample, ref uint[] buffer)
{
uint temp = 0;
int i = 0;
while (i != 0x11)
{
for (int j = 0; j < 0x20; j++)
{
sample *= 0x5d588b65;
temp = (temp >> 1) | (sample++ & 0x80000000);
}
buffer[i] = temp;
i++;
}
buffer[0x10] ^= (buffer[0] >> 0x9) ^ (buffer[0x10] << 0x17);
i = 1;
while (i != 0x1f9)
{
buffer[i + 0x10] = ((buffer[i - 1] << 0x17) ^ (buffer[i] >> 0x9)) ^ buffer[i + 0xf];
i++;
}
for (i = 0; i < 3; i++)
{
a100026e0(ref buffer);
}
}
private void a100026e0(ref uint[] buffer)
{
int i = 0;
while (i != 0x20)
{
buffer[i] ^= buffer[i + 0x1e9];
i++;
}
while (i != 0x209)
{
buffer[i] ^= buffer[i - 0x20];
i++;
}
}