Code:
unsigned DecodeArray(BitStream* input, unsigned sizeInBits, unsigned maxCharsToWrite, unsigned char* output)
{
struct HuffmanEncodingTreeNode
{
unsigned char value;
unsigned weight;
HuffmanEncodingTreeNode* left;
HuffmanEncodingTreeNode* right;
HuffmanEncodingTreeNode* parent;
};
HuffmanEncodingTreeNode* currentNode;
HuffmanEncodingTreeNode* root;
unsigned outputWriteIndex;
outputWriteIndex = 0;
currentNode = root;
// For each bit, go left if it is a 0 and right if it is a 1. When we reach a leaf, that gives us the desired value and we restart from the root
for (unsigned counter = 0; counter < sizeInBits; counter++)
{
if (input->ReadBit() == false) // left!
currentNode = currentNode->left;
else
currentNode = currentNode->right;
if (currentNode->left == 0 && currentNode->right == 0) // Leaf
{
if (outputWriteIndex < maxCharsToWrite)
output[outputWriteIndex] = currentNode->value;
outputWriteIndex++;
currentNode = root;
}
}
return outputWriteIndex;
}
bool DecodeString(char* output, int maxCharsToWrite, BitStream* input)
{
unsigned short stringBitLength;
int bytesInStream;
output[0] = 0;
if (input->ReadCompressed(stringBitLength) == false)
return false;
if (input->GetNumberOfUnreadBits() < stringBitLength)
return false;
bytesInStream = DecodeArray(input, stringBitLength, maxCharsToWrite, (unsigned char*)output);
if (bytesInStream < maxCharsToWrite)
output[bytesInStream] = 0;
else
output[maxCharsToWrite - 1] = 0;
return true;
}
RPC
Code:
case RPC_ShowDialog:
{
struct SAMPDialog
{
int iIsActive;
BYTE bDialogStyle;
WORD wDialogID;
BYTE bTitleLength;
char szTitle[257];
BYTE bButton1Len;
char szButton1[257];
BYTE bButton2Len;
char szButton2[257];
char szInfo[1024];
};
SAMPDialog house;
PCHAR Data = reinterpret_cast<PCHAR>(rpcParams->input);
int iBitLength = rpcParams->numberOfBitsOfData;
BitStream bsData((unsigned char*)Data, (iBitLength / 8) + 1, false);
bsData.Read(house.wDialogID);
bsData.Read(house.bDialogStyle);
bsData.Read(house.bTitleLength);
bsData.Read(house.szTitle, house.bTitleLength);
house.szTitle[house.bTitleLength] = '\0';
bsData.Read(house.bButton1Len);
bsData.Read(house.szButton1, house.bButton1Len);
house.szButton1[house.bButton1Len] = '\0';
bsData.Read(house.bButton2Len);
bsData.Read(house.szButton2, house.bButton2Len);
house.szButton2[house.bButton2Len] = '\0';
DecodeString(house.szInfo, 256, &bsData);
}