Пример #1
0
        public int DecodeSymbol(BitReaderFwd reader)
        {
            int preview = reader.PreviewBits(MAX_SYMBOL_LENGTH);
            var decode  = DecodeTable[preview];

            reader.AdvanceBits(decode.Bits);
            return(decode.Symbol);
        }
Пример #2
0
        public byte[] Decompress(byte[] input)
        {
            Reader = new BitReaderFwd(input);
            var output = new List <byte>();
            int offset = 0;

            ReadHuffmanTable();
            while (true)
            {
                int symbol = Decoder.DecodeSymbol(Reader);
                if (symbol < 256)
                {
                    output.Add((byte)symbol);
                    offset++;
                }
                else if (symbol >= SYM_BEGINMATCH)
                {
                    int bits     = symbol - SYM_BEGINMATCH;
                    int distance = Reader.ReadBits(bits) | (1 << bits);
                    int length   = DecodeMatchLength();
                    while (length-- > 0)
                    {
                        output.Add(output[offset - distance]);
                        offset++;
                    }
                }
                else if (symbol == SYM_NEWTREE)
                {
                    ReadHuffmanTable();
                }
                else if (symbol == SYM_EOF)
                {
                    break;
                }
            }
            return(output.ToArray());
        }