I'm trying to reverse an algorithm I have that packs a bunch of unsigned shorts into memory. I attempted to reverse it, and I get correct numbers back 50% - 75% of the time, so I'm not sure what I'm doing wrong.
This is the algorithm to pack the numbers into memory:
BYTE packNumber(BYTE bStart, WORD value, BYTE * buffer, DWORD * counter)
{
value = (value<<(6-bStart));
*(buffer + *counter) |= (BYTE)(value>>8);
*(buffer + *counter+1) |= (BYTE)value;
bStart = (bStart+2)%8;
if (bStart)
*counter+= 1;
else
*counter+= 2;
return bStart;
}
This is called several times in a row, passing the returned bStart into the next call, until all the numbers have been packed into memory.
This is my attempt at reversing it:
BYTE unpackNumber(BYTE bStart, WORD *value, BYTE * buffer, DWORD * counter)
{
*value= 0;
*value|= *(buffer + *counter);
*value= *value<< 8;
*value|= *(buffer + *counter+1);
*wVal = (*value>>(6-bStart));
bStart = (bStart+2)%8;
if (bStart)
*counter+= 1;
else
*counter+= 2;
return bStart;
}
I know I'm doing something right, since I get back a bunch of correct data, though depending on the numbers I wrote into memory, anything from every forth to every second number read in is wrong.
Any ideas what I'm doing wrong here?