Beispiel #1
0
        // Calculate the huffman code for each character based on the code length for each character.
        // This algorithm is described in standard RFC 1951
        private uint[] CalculateHuffmanCode()
        {
            var bitLengthCount = new uint[17];

            foreach (int codeLength in codeLengthArray)
            {
                bitLengthCount[codeLength]++;
            }
            bitLengthCount[0] = 0;             // clear count for length 0

            var  nextCode = new uint[17];
            uint tempCode = 0;

            for (var bits = 1; bits <= 16; bits++)
            {
                tempCode       = (tempCode + bitLengthCount[bits - 1]) << 1;
                nextCode[bits] = tempCode;
            }

            var code = new uint[MaxLiteralTreeElements];

            for (var i = 0; i < codeLengthArray.Length; i++)
            {
                int len = codeLengthArray[i];

                if (len > 0)
                {
                    code[i] = FastEncoderStatics.BitReverse(nextCode[len], len);
                    nextCode[len]++;
                }
            }
            return(code);
        }
Beispiel #2
0
        internal static void WriteMatch(int matchLen, int matchPos, OutputBuffer output)
        {
            Debug.Assert(matchLen >= FastEncoderWindow.MinMatch && matchLen <= FastEncoderWindow.MaxMatch,
                         "Illegal currentMatch length!");

            // Get the code information for a match code
            var codeInfo =
                FastEncoderStatics.FastEncoderLiteralCodeInfo[
                    FastEncoderStatics.NumChars + 1 - FastEncoderWindow.MinMatch + matchLen];
            var codeLen = (int)codeInfo & 31;

            Debug.Assert(codeLen != 0, "Invalid Match Length!");
            if (codeLen <= 16)
            {
                output.WriteBits(codeLen, codeInfo >> 5);
            }
            else
            {
                output.WriteBits(16, (codeInfo >> 5) & 65535);
                output.WriteBits(codeLen - 16, codeInfo >> (5 + 16));
            }

            // Get the code information for a distance code
            codeInfo = FastEncoderStatics.FastEncoderDistanceCodeInfo[FastEncoderStatics.GetSlot(matchPos)];
            output.WriteBits((int)(codeInfo & 15), codeInfo >> 8);
            var extraBits = (int)(codeInfo >> 4) & 15;

            if (extraBits != 0)
            {
                output.WriteBits(extraBits, (uint)matchPos & FastEncoderStatics.BitMask[extraBits]);
            }
        }
Beispiel #3
0
        private UInt32[] CalculateHuffmanCode()
        {
            UInt32[] array  = new UInt32[17];
            Byte[]   array2 = this.codeLengthArray;
            for (Int32 i = 0; i < (Int32)array2.Length; i++)
            {
                Int32 num = (Int32)array2[i];
                array[num] += 1u;
            }
            array[0] = 0u;
            UInt32[] array3 = new UInt32[17];
            UInt32   num2   = 0u;

            for (Int32 j = 1; j <= 16; j++)
            {
                num2      = num2 + array[j - 1] << 1;
                array3[j] = num2;
            }
            UInt32[] array4 = new UInt32[288];
            for (Int32 k = 0; k < (Int32)this.codeLengthArray.Length; k++)
            {
                Int32 num3 = (Int32)this.codeLengthArray[k];
                if (num3 > 0)
                {
                    array4[k]     = FastEncoderStatics.BitReverse(array3[num3], num3);
                    array3[num3] += 1u;
                }
            }
            return(array4);
        }
Beispiel #4
0
        private uint[] CalculateHuffmanCode()
        {
            uint[] array  = new uint[17];
            byte[] array2 = this.codeLengthArray;
            for (int i = 0; i < array2.Length; i++)
            {
                int num = (int)array2[i];
                array[num] += 1u;
            }
            array[0] = 0u;
            uint[] array3 = new uint[17];
            uint   num2   = 0u;

            for (int j = 1; j <= 16; j++)
            {
                num2      = num2 + array[j - 1] << 1;
                array3[j] = num2;
            }
            uint[] array4 = new uint[288];
            for (int k = 0; k < this.codeLengthArray.Length; k++)
            {
                int num3 = (int)this.codeLengthArray[k];
                if (num3 > 0)
                {
                    array4[k]     = FastEncoderStatics.BitReverse(array3[num3], num3);
                    array3[num3] += 1u;
                }
            }
            return(array4);
        }
Beispiel #5
0
        internal static void WriteMatch(int matchLen, int matchPos, OutputBuffer output)
        {
            uint num  = FastEncoderStatics.FastEncoderLiteralCodeInfo[254 + matchLen];
            int  num2 = (int)(num & 31u);

            if (num2 <= 16)
            {
                output.WriteBits(num2, num >> 5);
            }
            else
            {
                output.WriteBits(16, num >> 5 & 65535u);
                output.WriteBits(num2 - 16, num >> 21);
            }
            num = FastEncoderStatics.FastEncoderDistanceCodeInfo[FastEncoderStatics.GetSlot(matchPos)];
            output.WriteBits((int)(num & 15u), num >> 8);
            int num3 = (int)(num >> 4 & 15u);

            if (num3 != 0)
            {
                output.WriteBits(num3, (uint)(matchPos & (int)FastEncoderStatics.BitMask[num3]));
            }
        }