A while back, I reverse-engineered a checksum algorithm from an MMO used to check the validity of an item that's linked to chat (similar to WoW). The idea is that if the checksum is invalid then the game client would ignore the link when clicked. Otherwise clicking on the item link in in-game chat would display the stat and attrib for that item.
ushort16 CreateChecksum(const string &itemlink)
{
stringstream parseitemlink(itemlink);
uint32 hexform[ITEMLINKGROUPCOUNT] = {0};
uint32 hexsum = 0;
//Parse itemLink string into hexform array
for (int i = 0; i < ITEMLINKGROUPCOUNT; ++i)
parseitemlink >> hex >> hexform[i];
//sum all the itemlink group together
for (int i = 0; i < ITEMLINKGROUPCOUNT; ++i)
hexsum += hexform[i];
for (int i = 0; i < ITEMLINKGROUPCOUNT; ++i)
{
uint32 ebx = hexform[i], edi = ebx * i;
//if loop iteration is odd store MSB 2-bytes.
//Otherwise, store working hexgroup as is untouched
if (i & 0x01)
ebx = hexform[i] >> 16; // aka same as dividing hexform[i] by 65,536
// (hexform[i] / 65,536) + (hexform[i] * i) + hexsum -- for odd groups
// hexform[i] + (hexform[i] * i) + hexsum -- for even groups
ebx += edi + hexsum;
hexsum = ebx ^ hexform[i];
}
for (int i = 0; i < ITEMLINKGROUPCOUNT; ++i)
{
// the more familiar high-level form would be
// hexform[i]^2 + hexform[i] * hexsum
uint32 ecx = (hexform[i] + 1) * hexsum,
eax = ecx * hexform[i];
eax >>= 16;
eax += ecx;
hexsum = eax ^ hexform[i];
}
//return the lower 2-bytes of hexsum
//as the final checksum
return hexsum & 0xFFFF;
}//CreateChecksum
The format of the itemlink
is comprised of a group of hexadecimal separated with a space in string format. It's passed into main()
as an argument when the program is run.
Here's what an itemlink
's hex string might look like:
const string EXAMPLELINK = "36c6a 0 3f000a54 d0f1 0 0 0 0 0 0 20d0";
Are there any code smells or readability issues in this code segment? Can any part(s) of it be improved?