Пример #1
0
        private Stream PrivDecompress(Stream inFile)
        {
            if (!IsLz77Compressed(inFile))
            {
                return(inFile);
            }

            inFile.Seek(0L, SeekOrigin.Begin);
            uint num1 = 0;

            Headers.HeaderType headerType = Headers.DetectHeader(inFile);
            byte[]             buffer     = new byte[8];
            inFile.Seek((long)headerType, SeekOrigin.Begin);
            inFile.Read(buffer, 0, 8);
            if ((int)Shared.Swap(BitConverter.ToUInt32(buffer, 0)) != (int)lz77Magic)
            {
                inFile.Dispose();
                throw new Exception("Invaild Magic!");
            }
            if (buffer[4] != 16)
            {
                inFile.Dispose();
                throw new Exception("Unsupported Compression Type!");
            }
            uint num2 = BitConverter.ToUInt32(buffer, 4) >> 8;

            for (int index = 0; index < 4078; ++index)
            {
                textBuffer[index] = 223;
            }

            int          num3         = 4078;
            uint         num4         = 7;
            int          num5         = 7;
            MemoryStream memoryStream = new MemoryStream();

label_10:
            while (true)
            {
                num4 <<= 1;
                ++num5;
                if (num5 == 8)
                {
                    int num6;
                    if ((num6 = inFile.ReadByte()) != -1)
                    {
                        num4 = (uint)num6;
                        num5 = 0;
                    }
                    else
                    {
                        goto label_24;
                    }
                }
                if (((int)num4 & 128) == 0)
                {
                    int num6;
                    if ((num6 = inFile.ReadByte()) != inFile.Length - 1L)
                    {
                        if (num1 < num2)
                        {
                            memoryStream.WriteByte((byte)num6);
                        }

                        ushort[] textBuffer = this.textBuffer;
                        int      index      = num3;
                        int      num7       = index + 1;
                        int      num8       = (byte)num6;
                        textBuffer[index] = (ushort)num8;
                        num3 = num7 & 4095;
                        ++num1;
                    }
                    else
                    {
                        goto label_24;
                    }
                }
                else
                {
                    break;
                }
            }
            int num9;
            int num10;

            if ((num9 = inFile.ReadByte()) != -1 && (num10 = inFile.ReadByte()) != -1)
            {
                int num6 = num10 | num9 << 8 & 3840;
                int num7 = (num9 >> 4 & 15) + 2;
                for (int index1 = 0; index1 <= num7; ++index1)
                {
                    int num8 = this.textBuffer[num3 - num6 - 1 & 4095];
                    if (num1 < num2)
                    {
                        memoryStream.WriteByte((byte)num8);
                    }

                    ushort[] textBuffer = this.textBuffer;
                    int      index2     = num3;
                    int      num11      = index2 + 1;
                    int      num12      = (byte)num8;
                    textBuffer[index2] = (ushort)num12;
                    num3 = num11 & 4095;
                    ++num1;
                }
                goto label_10;
            }
label_24:
            return(memoryStream);
        }
Пример #2
0
 /// <summary>
 /// Checks whether a file is Lz77 compressed or not.
 /// </summary>
 /// <param name="file"></param>
 /// <returns></returns>
 public static bool IsLz77Compressed(byte[] file)
 {
     Headers.HeaderType headerType = Headers.DetectHeader(file);
     return((int)Shared.Swap(BitConverter.ToUInt32(file, (int)headerType)) == (int)lz77Magic);
 }
Пример #3
0
        private Stream decompress(Stream inFile)
        {
            if (!Lz77.IsLz77Compressed(inFile))
            {
                return(inFile);
            }
            inFile.Seek(0, SeekOrigin.Begin);

            int  i, j, k, r, c, z;
            uint flags, decompressedSize, currentSize = 0;

            Headers.HeaderType h = Headers.DetectHeader(inFile);

            byte[] temp = new byte[8];
            inFile.Seek((int)h, SeekOrigin.Begin);
            inFile.Read(temp, 0, 8);

            if (Shared.Swap(BitConverter.ToUInt32(temp, 0)) != lz77Magic)
            {
                inFile.Dispose(); throw new Exception("Invaild Magic!");
            }
            if (temp[4] != 0x10)
            {
                inFile.Dispose(); throw new Exception("Unsupported Compression Type!");
            }

            decompressedSize = (BitConverter.ToUInt32(temp, 4)) >> 8;

            for (i = 0; i < N - F; i++)
            {
                textBuffer[i] = 0xdf;
            }
            r = N - F; flags = 7; z = 7;

            MemoryStream outFile = new MemoryStream();

            while (true)
            {
                flags <<= 1;
                z++;

                if (z == 8)
                {
                    if ((c = inFile.ReadByte()) == -1)
                    {
                        break;
                    }

                    flags = (uint)c;
                    z     = 0;
                }

                if ((flags & 0x80) == 0)
                {
                    if ((c = inFile.ReadByte()) == inFile.Length - 1)
                    {
                        break;
                    }
                    if (currentSize < decompressedSize)
                    {
                        outFile.WriteByte((byte)c);
                    }

                    textBuffer[r++] = (byte)c;
                    r &= (N - 1);
                    currentSize++;
                }
                else
                {
                    if ((i = inFile.ReadByte()) == -1)
                    {
                        break;
                    }
                    if ((j = inFile.ReadByte()) == -1)
                    {
                        break;
                    }

                    j = j | ((i << 8) & 0xf00);
                    i = ((i >> 4) & 0x0f) + threshold;
                    for (k = 0; k <= i; k++)
                    {
                        c = textBuffer[(r - j - 1) & (N - 1)];
                        if (currentSize < decompressedSize)
                        {
                            outFile.WriteByte((byte)c);
                        }
                        textBuffer[r++] = (byte)c; r &= (N - 1); currentSize++;
                    }
                }
            }

            return(outFile);
        }