Esempio n. 1
0
        /// <summary>
        /// Decodes LZ compressed bytes
        /// </summary>
        /// <param name="codes">Compressed bytes</param>
        /// <param name="decoderDic">Dictionary to use in decoding</param>
        /// <returns>Decompressed data</returns>
        private static byte[] _asLZDecoded(byte[] codes, ILZ78CodingTable <byte[]> decoderDic)
        {
            for (int i = 0; i < 256; i++)
            {
                var searchKey = i.AsBytes();
                var codeWord  = new[] { (byte)i };
                decoderDic.Insert(searchKey, codeWord);
            }

            //decompress compressed codes as int 32 byte array
            var decompressedCodes = codes.AsDecompressed();

            using (var decodedOutPut = new ByteMemoryStream(decompressedCodes.Length * 2)) {
                var O  = decompressedCodes.AsInt(0).AsBytes();
                var S  = new byte[0];
                var C2 = new byte[0];

                var item = decoderDic.Search(O);

                decodedOutPut.AddBytes(item.CodeWord);

                for (int i = 4; i < decompressedCodes.Length; i += 4)
                {
                    var N = decompressedCodes.AsInt(i).AsBytes();

                    var itemN = decoderDic.Search(N);
                    var itemO = decoderDic.Search(O);

                    if (itemN == null)
                    {
                        S = itemO.CodeWord.Concatenate(C2);
                    }
                    else
                    {
                        S = itemN.CodeWord;
                    }

                    decodedOutPut.AddBytes(S);

                    C2 = new byte[] { S[0] };

                    var C3 = itemO.CodeWord.Concatenate(C2);

                    decoderDic.Insert(((decoderDic.Count)).AsBytes(), C3);

                    O = N;
                }

                return(decodedOutPut.ReadBytes());
            }
        }