Пример #1
0
        private static void addEntry(int rootNumber, string pattern, string decode)
        {
            if (roots[rootNumber - 1] == null)
            {
                roots[rootNumber - 1] = new HuffmanEntry();
            }

            HuffmanEntry currentEntry = roots[rootNumber - 1];

            for (int index = 0; index < pattern.Length; index++)
            {
                char patternChar = pattern[index];

                switch (patternChar)
                {
                case '0':
                    if (currentEntry.P0 == null)
                    {
                        currentEntry.P0 = new HuffmanEntry();
                        currentEntry    = currentEntry.P0;
                        if (index == pattern.Length - 1)
                        {
                            currentEntry.Value = decode;
                        }
                    }
                    else
                    {
                        currentEntry = currentEntry.P0;
                        if (currentEntry.Value != null && index == pattern.Length - 1)
                        {
                            Logger.Instance.Write("Dictionary entry already set");
                        }
                    }
                    break;

                case '1':
                    if (currentEntry.P1 == null)
                    {
                        currentEntry.P1 = new HuffmanEntry();
                        currentEntry    = currentEntry.P1;
                        if (index == pattern.Length - 1)
                        {
                            currentEntry.Value = decode;
                        }
                    }
                    else
                    {
                        currentEntry = currentEntry.P1;
                        if (currentEntry.Value != null && index == pattern.Length - 1)
                        {
                            Logger.Instance.Write("Dictionary entry already set");
                        }
                    }
                    break;

                default:
                    break;
                }
            }
        }
        private static void addEntry(int rootNumber, string pattern, string decode)
        {
            if (roots[rootNumber - 1] == null)
                roots[rootNumber - 1] = new HuffmanEntry();

            HuffmanEntry currentEntry = roots[rootNumber - 1];

            for (int index = 0; index < pattern.Length; index++)
            {
                char patternChar = pattern[index];

                switch (patternChar)
                {
                    case '0':
                        if (currentEntry.P0 == null)
                        {
                            currentEntry.P0 = new HuffmanEntry();
                            currentEntry = currentEntry.P0;
                            if (index == pattern.Length - 1)
                                currentEntry.Value = decode;
                        }
                        else
                        {
                            currentEntry = currentEntry.P0;
                            if (currentEntry.Value != null && index == pattern.Length - 1)
                                Logger.Instance.Write("Dictionary entry already set");
                        }
                        break;
                    case '1':
                        if (currentEntry.P1 == null)
                        {
                            currentEntry.P1 = new HuffmanEntry();
                            currentEntry = currentEntry.P1;
                            if (index == pattern.Length - 1)
                                currentEntry.Value = decode;
                        }
                        else
                        {
                            currentEntry = currentEntry.P1;
                            if (currentEntry.Value != null && index == pattern.Length - 1)
                                Logger.Instance.Write("Dictionary entry already set");
                        }
                        break;
                    default:
                        break;
                }
            }
        }
        private static string decodeData(byte[] byteData, HuffmanEntry[] roots, int startIndex)
        {
            StringBuilder outputString = new StringBuilder();

            HuffmanEntry currentEntry = roots[0];
            byte         mask         = 0x80;
            bool         finished     = false;

            StringBuilder bitString = new StringBuilder();

            for (int index = startIndex; index < byteData.Length && !finished; index++)
            {
                byte dataByte = byteData[index];

                while (mask > 0 && !finished)
                {
                    if (currentEntry.HoldsValue)
                    {
                        switch ((int)currentEntry.Value[0])
                        {
                        case stop:
                            finished = true;
                            break;

                        case escape:
                            byte encodedValue;
                            do
                            {
                                encodedValue = 0x00;

                                for (int bitCount = 0; bitCount < 8; bitCount++)
                                {
                                    encodedValue = (byte)(encodedValue << 1);

                                    if ((dataByte & mask) != 0)
                                    {
                                        encodedValue |= 0x01;
                                    }

                                    mask = (byte)(mask >> 1);
                                    if (mask == 0)
                                    {
                                        index++;
                                        dataByte = byteData[index];
                                        mask     = 0x80;
                                    }
                                }
                            }while ((encodedValue & 0x80) != 0);

                            finished = ((int)encodedValue < 0x20);

                            if (!finished)
                            {
                                outputString.Append((char)encodedValue);
                                currentEntry = roots[encodedValue];
                                bitString    = new StringBuilder();
                            }

                            break;

                        default:
                            outputString.Append(currentEntry.Value);
                            currentEntry = roots[(int)currentEntry.Value[0]];
                            bitString    = new StringBuilder();
                            break;
                        }
                    }

                    if (!finished)
                    {
                        if ((dataByte & mask) == 0)
                        {
                            bitString.Append("0");

                            if (currentEntry.P0 != null)
                            {
                                currentEntry = currentEntry.P0;
                            }
                            else
                            {
                                Logger.Instance.Write(" ** DECOMPRESSION FAILED **");
                                Logger.Instance.Write("Original data: " + Utils.ConvertToHex(byteData));
                                Logger.Instance.Write("Decoded data: " + outputString.ToString());
                                Logger.Instance.Write("Bit string: " + bitString.ToString());
                                return(outputString.ToString() + " ** DECOMPRESSION FAILED **");
                            }
                        }
                        else
                        {
                            bitString.Append("1");

                            if (currentEntry.P1 != null)
                            {
                                currentEntry = currentEntry.P1;
                            }
                            else
                            {
                                Logger.Instance.Write(" ** DECOMPRESSION FAILED **");
                                Logger.Instance.Write("Original data: " + Utils.ConvertToHex(byteData));
                                Logger.Instance.Write("Decoded data: " + outputString.ToString());
                                Logger.Instance.Write("Bit string: " + bitString.ToString());
                                return(outputString.ToString() + " ** DECOMPRESSION FAILED **");
                            }
                        }

                        mask = (byte)(mask >> 1);
                    }
                }

                mask = 0x80;
            }

            return(outputString.ToString());
        }
        private static void loadFile(HuffmanEntry[] roots, string filename)
        {
            FileStream   fileStream   = new FileStream(filename, FileMode.Open, FileAccess.Read);
            StreamReader streamReader = new StreamReader(fileStream);

            while (!streamReader.EndOfStream)
            {
                string line = streamReader.ReadLine();

                if (line != string.Empty && !line.StartsWith("####"))
                {
                    string[] parts = line.Split(new char[] { ':' });
                    if (parts.Length == 4)
                    {
                        int rootOffSet = (int)(resolveChar(parts[0]));

                        if (roots[rootOffSet] == null)
                        {
                            roots[rootOffSet] = new HuffmanEntry();
                        }

                        HuffmanEntry currentEntry = roots[rootOffSet];
                        string       pattern      = parts[1];

                        for (int index = 0; index < parts[1].Length; index++)
                        {
                            char patternChar = pattern[index];

                            switch (patternChar)
                            {
                            case '0':
                                if (currentEntry.P0 == null)
                                {
                                    currentEntry.P0 = new HuffmanEntry();
                                    currentEntry    = currentEntry.P0;
                                    if (index == pattern.Length - 1)
                                    {
                                        currentEntry.Value = resolveChar(parts[2]).ToString();
                                    }
                                }
                                else
                                {
                                    currentEntry = currentEntry.P0;
                                    if (currentEntry.HoldsValue && index == pattern.Length - 1)
                                    {
                                        Logger.Instance.Write("Dictionary entry already set");
                                    }
                                }
                                break;

                            case '1':
                                if (currentEntry.P1 == null)
                                {
                                    currentEntry.P1 = new HuffmanEntry();
                                    currentEntry    = currentEntry.P1;
                                    if (index == pattern.Length - 1)
                                    {
                                        currentEntry.Value = resolveChar(parts[2]).ToString();
                                    }
                                }
                                else
                                {
                                    currentEntry = currentEntry.P1;
                                    if (currentEntry.HoldsValue && index == pattern.Length - 1)
                                    {
                                        Logger.Instance.Write("Dictionary entry already set: " + line);
                                    }
                                }
                                break;

                            default:
                                break;
                            }
                        }
                    }
                }
            }

            streamReader.Close();
            fileStream.Close();
        }
Пример #5
0
        /// <summary>
        /// Decompress a compressed Huffman string.
        /// </summary>
        /// <param name="rootNumber">The root table to use.</param>
        /// <param name="byteData">The compressed byte data.</param>
        /// <returns>The decompressed string.</returns>
        public static string DecodeData(int rootNumber, byte[] byteData)
        {
            StringBuilder outputString = new StringBuilder();

            HuffmanEntry currentEntry = roots[rootNumber - 1];

            byte mask;

            if (offsetStart)
            {
                mask = 0x20;
            }
            else
            {
                mask = 0x80;
            }

            StringBuilder bitString = new StringBuilder();

            for (int index = 0; index < byteData.Length; index++)
            {
                byte dataByte = byteData[index];

                while (mask > 0)
                {
                    if (currentEntry.Value != null)
                    {
                        if (currentEntry.Value != "??")
                        {
                            outputString.Append(currentEntry.Value);
                        }
                        currentEntry = roots[rootNumber - 1];
                        bitString    = new StringBuilder();
                    }

                    if ((dataByte & mask) == 0)
                    {
                        bitString.Append("0");

                        if (currentEntry.P0 != null)
                        {
                            currentEntry = currentEntry.P0;
                        }
                        else
                        {
                            Logger.Instance.Write(" ** DECOMPRESSION FAILED **");
                            Logger.Instance.Write("Original data: " + Utils.ConvertToHex(byteData));
                            Logger.Instance.Write("Decoded data: " + outputString.ToString());
                            Logger.Instance.Write("Bit string: " + bitString.ToString());
                            return(outputString.ToString() + " ** DECOMPRESSION FAILED **");
                        }
                    }
                    else
                    {
                        bitString.Append("1");

                        if (currentEntry.P1 != null)
                        {
                            currentEntry = currentEntry.P1;
                        }
                        else
                        {
                            Logger.Instance.Write(" ** DECOMPRESSION FAILED **");
                            Logger.Instance.Write("Original data: " + Utils.ConvertToHex(byteData));
                            Logger.Instance.Write("Decoded data: " + outputString.ToString());
                            Logger.Instance.Write("Bit string: " + bitString.ToString());
                            return(outputString.ToString() + " ** DECOMPRESSION FAILED **");
                        }
                    }

                    mask = (byte)(mask >> 1);
                }

                mask = 0x80;
            }

            if (currentEntry.Value != null && currentEntry.Value != "??")
            {
                outputString.Append(currentEntry.Value);
            }

            return(outputString.ToString());
        }
        private static void loadFile(HuffmanEntry[] roots, string filename)
        {
            FileStream fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
            StreamReader streamReader = new StreamReader(fileStream);

            while (!streamReader.EndOfStream)
            {
                string line = streamReader.ReadLine();

                if (line != string.Empty && !line.StartsWith("####"))
                {
                    string[] parts = line.Split(new char[] { ':' });
                    if (parts.Length == 4)
                    {
                        int rootOffSet = (int)(resolveChar(parts[0]));

                        if (roots[rootOffSet] == null)
                            roots[rootOffSet] = new HuffmanEntry();

                        HuffmanEntry currentEntry = roots[rootOffSet];
                        string pattern = parts[1];

                        for (int index = 0; index < parts[1].Length; index++)
                        {
                            char patternChar = pattern[index];

                            switch (patternChar)
                            {
                                case '0':
                                    if (currentEntry.P0 == null)
                                    {
                                        currentEntry.P0 = new HuffmanEntry();
                                        currentEntry = currentEntry.P0;
                                        if (index == pattern.Length - 1)
                                            currentEntry.Value = resolveChar(parts[2]).ToString();
                                    }
                                    else
                                    {
                                        currentEntry = currentEntry.P0;
                                        if (currentEntry.HoldsValue && index == pattern.Length - 1)
                                            Logger.Instance.Write("Dictionary entry already set");
                                    }
                                    break;
                                case '1':
                                    if (currentEntry.P1 == null)
                                    {
                                        currentEntry.P1 = new HuffmanEntry();
                                        currentEntry = currentEntry.P1;
                                        if (index == pattern.Length - 1)
                                            currentEntry.Value = resolveChar(parts[2]).ToString();
                                    }
                                    else
                                    {
                                        currentEntry = currentEntry.P1;
                                        if (currentEntry.HoldsValue && index == pattern.Length - 1)
                                            Logger.Instance.Write("Dictionary entry already set: " + line);
                                    }
                                    break;
                                default:
                                    break;
                            }
                        }
                    }
                }
            }

            streamReader.Close();
            fileStream.Close();
        }
        private static string decodeData(byte[] byteData, HuffmanEntry[] roots, int startIndex)
        {
            StringBuilder outputString = new StringBuilder();

            HuffmanEntry currentEntry = roots[0];
            byte mask = 0x80;
            bool finished = false;

            StringBuilder bitString = new StringBuilder();

            for (int index = startIndex; index < byteData.Length && !finished; index++)
            {
                byte dataByte = byteData[index];

                while (mask > 0 && !finished)
                {
                    if (currentEntry.HoldsValue)
                    {
                        switch ((int)currentEntry.Value[0])
                        {
                            case stop:
                                finished = true;
                                break;
                            case escape:
                                byte encodedValue;
                                do
                                {
                                    encodedValue = 0x00;

                                    for (int bitCount = 0; bitCount < 8; bitCount++)
                                    {
                                        encodedValue = (byte)(encodedValue << 1);

                                        if ((dataByte & mask) != 0)
                                            encodedValue |= 0x01;

                                        mask = (byte)(mask >> 1);
                                        if (mask == 0)
                                        {
                                            index++;
                                            dataByte = byteData[index];
                                            mask = 0x80;
                                        }
                                    }
                                }
                                while ((encodedValue & 0x80) != 0);

                                finished = ((int)encodedValue < 0x20);

                                if (!finished)
                                {
                                    outputString.Append((char)encodedValue);
                                    currentEntry = roots[encodedValue];
                                    bitString = new StringBuilder();
                                }

                                break;
                            default:
                                outputString.Append(currentEntry.Value);
                                currentEntry = roots[(int)currentEntry.Value[0]];
                                bitString = new StringBuilder();
                                break;
                        }
                    }

                    if (!finished)
                    {
                        if ((dataByte & mask) == 0)
                        {
                            bitString.Append("0");

                            if (currentEntry.P0 != null)
                                currentEntry = currentEntry.P0;
                            else
                            {
                                Logger.Instance.Write(" ** DECOMPRESSION FAILED **");
                                Logger.Instance.Write("Original data: " + Utils.ConvertToHex(byteData));
                                Logger.Instance.Write("Decoded data: " + outputString.ToString());
                                Logger.Instance.Write("Bit string: " + bitString.ToString());
                                return (outputString.ToString() + " ** DECOMPRESSION FAILED **");
                            }
                        }
                        else
                        {
                            bitString.Append("1");

                            if (currentEntry.P1 != null)
                                currentEntry = currentEntry.P1;
                            else
                            {
                                Logger.Instance.Write(" ** DECOMPRESSION FAILED **");
                                Logger.Instance.Write("Original data: " + Utils.ConvertToHex(byteData));
                                Logger.Instance.Write("Decoded data: " + outputString.ToString());
                                Logger.Instance.Write("Bit string: " + bitString.ToString());
                                return (outputString.ToString() + " ** DECOMPRESSION FAILED **");
                            }
                        }

                        mask = (byte)(mask >> 1);
                    }
                }

                mask = 0x80;
            }

            return (outputString.ToString());
        }