// 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() { uint[] bitLengthCount = new uint[17]; foreach (int codeLength in _codeLengthArray) { bitLengthCount[codeLength]++; } bitLengthCount[0] = 0; // clear count for length 0 uint[] nextCode = new uint[17]; uint tempCode = 0; for (int bits = 1; bits <= 16; bits++) { tempCode = (tempCode + bitLengthCount[bits - 1]) << 1; nextCode[bits] = tempCode; } uint[] code = new uint[MaxLiteralTreeElements]; for (int i = 0; i < _codeLengthArray.Length; i++) { int len = _codeLengthArray[i]; if (len > 0) { code[i] = FastEncoderStatics.BitReverse(nextCode[len], len); nextCode[len]++; } } return(code); }
static internal void WriteMatch(int matchLen, int matchPos, OutputBuffer output) { Debug.Assert(matchLen >= FastEncoderWindow.MinMatch && matchLen <= FastEncoderWindow.MaxMatch, "Illegal currentMatch length!"); Debug.WriteLineIf(CompressionTracingSwitch.Verbose, String.Format(CultureInfo.InvariantCulture, "Match: {0}:{1}", matchLen, matchPos), "Compression"); // Get the code information for a match code uint codeInfo = FastEncoderStatics.FastEncoderLiteralCodeInfo[(FastEncoderStatics.NumChars + 1 - FastEncoderWindow.MinMatch) + matchLen]; int 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); int extraBits = (int)(codeInfo >> 4) & 15; if (extraBits != 0) { output.WriteBits(extraBits, (uint)matchPos & FastEncoderStatics.BitMask[extraBits]); } }
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 uint codeInfo = FastEncoderStatics.FastEncoderLiteralCodeInfo[(FastEncoderStatics.NumChars + 1 - FastEncoderWindow.MinMatch) + matchLen]; int 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); int extraBits = (int)(codeInfo >> 4) & 15; if (extraBits != 0) { output.WriteBits(extraBits, (uint)matchPos & FastEncoderStatics.BitMask[extraBits]); } }
private uint[] CalculateHuffmanCode() { uint[] numArray = new uint[0x11]; byte[] codeLengthArray = this.codeLengthArray; for (int i = 0; i < codeLengthArray.Length; i++) { int index = codeLengthArray[i]; numArray[index]++; } numArray[0] = 0; uint[] numArray2 = new uint[0x11]; uint num2 = 0; for (int j = 1; j <= 0x10; j++) { numArray2[j] = (num2 + numArray[j - 1]) << 1; } uint[] numArray3 = new uint[0x120]; for (int k = 0; k < this.codeLengthArray.Length; k++) { int length = this.codeLengthArray[k]; if (length > 0) { numArray3[k] = FastEncoderStatics.BitReverse(numArray2[length], length); numArray2[length]++; } } return(numArray3); }
internal static void WriteMatch(int matchLen, int matchPos, OutputBuffer output) { uint num = FastEncoderStatics.FastEncoderLiteralCodeInfo[0xfe + matchLen]; int n = ((int)num) & 0x1f; if (n <= 0x10) { output.WriteBits(n, num >> 5); } else { output.WriteBits(0x10, (num >> 5) & 0xffff); output.WriteBits(n - 0x10, num >> 0x15); } num = FastEncoderStatics.FastEncoderDistanceCodeInfo[FastEncoderStatics.GetSlot(matchPos)]; output.WriteBits(((int)num) & 15, num >> 8); int num3 = ((int)(num >> 4)) & 15; if (num3 != 0) { output.WriteBits(num3, ((uint)matchPos) & FastEncoderStatics.BitMask[num3]); } }