예제 #1
0
        private int EncodeBytes(byte[] input, uint inputSize, byte[] output, int outputInitializedOffset, HuffmanTree tree)
        {
            int resultSize = outputInitializedOffset, code;

            for (int i = 0; i < inputSize; i++)
            {
                if (tree.Exists(input[i]))
                {
                    code = tree.GetCode(input[i], out int bitsCount);
                    for (int j = 0; j < bitsCount; j++)
                    {
                        SetBit(output, resultSize + j, GetBit(code, j));
                    }
                    resultSize += bitsCount;
                }
                else
                {
                    code = tree.GetCode(tree.newNode, out int bitsCount);
                    for (int j = 0; j < bitsCount; j++)
                    {
                        SetBit(output, resultSize + j, GetBit(code, j));
                    }
                    resultSize += bitsCount;
                    code        = input[i];
                    bitsCount   = 8;
                    for (int j = 0; j < bitsCount; j++)
                    {
                        SetBit(output, resultSize + j, GetBit(code, j));
                    }
                    resultSize += bitsCount;
                }
                tree.Add(input[i]);
            }
            return(resultSize);
        }
예제 #2
0
        private uint DecodeBytes(byte[] input, uint inputBitSize, uint pointer, out uint unDecodedBitSize, byte[] output, HuffmanTree tree, bool ignore32)
        {
            uint counter = 0;
            byte character;
            int  reserved = ignore32 ? 0 : 32;

            while (pointer < inputBitSize - reserved)
            {
                character = DecodeByte(input, pointer, tree, out bool isNew, out uint bitLength);
                pointer  += bitLength;
                if (isNew)
                {
                    character = GetByte(input, pointer);
                    pointer  += 8;
                }
                output[counter] = character;
                tree.Add(character);
                counter++;
            }
            unDecodedBitSize = inputBitSize - pointer;
            return(counter);
        }