I am attempting to reverse engineer an LZ1/LZ77 decompression algorithm. The length of an area of the decode buffer/window to be output is encoded in the file as a variable length integer. I've read as much as I can about variable length integer encoding and the method being used in this case does not appear to be like any others I have seen. Perhaps to avoid patent issues or maybe just to obfuscate. The included code might not be quite complete but it is working on at least several files at this point.
I cannot see how, if at all, the formulas being used below could be reduced into something simpler. Most of the variable length integer encoding algorithms use some sort of loop but for this one, I have not been able to do that because the formula just doesn't seem to be consistent when evaluating each nibble.
Suggestions are greatly appreciated.
private static int getLength(BitReader bitStream)
{
const int minSize = 2;
int length = 0;
byte nibble3, nibble2, nibble1;
nibble3 = bitStream.ReadNibble();
if (nibble3 >= 0xc)
{
nibble2 = bitStream.ReadNibble();
nibble1 = bitStream.ReadNibble();
if (nibble3 == 0xF & nibble2 == 0xF & nibble1 == 0xF) return -1;
if ((nibble3 & 2) != 0)
{
length = (((((nibble3 & 7) + 3) << 6) + 8)) +
((nibble2 & 7) << 3) + nibble1 + minSize;
}
else if ((nibble3 & 1) != 0)
{
length = (((nibble3 & 7) << 6) + 8) +
((((nibble2 & 7)) + 1) << 3) + nibble1 + minSize;
}
else
{
length = ((((nibble3 & 7) << 4) + 8)) +
((nibble2 & 7) << 4) + nibble1 + minSize;
}
}
else if ((nibble3 & 8) != 0)
{
nibble1 = bitStream.ReadNibble();
length = ((((nibble3 & 7) << 1) + 1) << 3) + nibble1 + minSize;
}
else
{
length = nibble3 + minSize;
}
return length;
}