Advertisement
Guest User

Atacama

a guest
Apr 19th, 2008
1,105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.30 KB | None | 0 0
  1. /*  
  2. // LZ77Stream.cs by Atacama
  3. // Decompresses LZ77 Streams
  4.  
  5. Example usage:
  6.             string openFileName = "", saveFileName ="";
  7.             OpenFileDialog oFDlg = new OpenFileDialog();
  8.  
  9.             oFDlg.Title = "Open LZ77 Compressed File...";
  10.             oFDlg.Filter = "All files (*.*)|*.*";
  11.             oFDlg.FilterIndex = 1;
  12.             oFDlg.RestoreDirectory = true;
  13.             if (oFDlg.ShowDialog() == DialogResult.OK)
  14.             {
  15.                 openFileName = oFDlg.FileName;
  16.             }
  17.             else
  18.             {
  19.                 return;
  20.  
  21.             }
  22.  
  23.             SaveFileDialog sFDlg = new SaveFileDialog();
  24.  
  25.             sFDlg.Title = "File to Save Compressed Data to...";
  26.             sFDlg.Filter = "All files (*.*)|*.*";
  27.             sFDlg.FilterIndex = 1;
  28.             sFDlg.RestoreDirectory = true;
  29.             if (sFDlg.ShowDialog() == DialogResult.OK)
  30.             {
  31.                 saveFileName = sFDlg.FileName;
  32.             }
  33.             else
  34.             {
  35.                 return;
  36.             }
  37.             MemoryStream lzdata = new MemoryStream();
  38.             FileStream fIn = new FileStream(openFileName, FileMode.Open, FileAccess.Read);
  39.             FileStream fOut = new FileStream(saveFileName, FileMode.CreateNew, FileAccess.Write);
  40.             LZ77Stream lz = new LZ77Stream(lzdata, CompressionMode.Decompress);
  41.             byte[] Indata = new byte[fIn.Length];
  42.             fIn.Read(Indata, 0, (int) fIn.Length);
  43.             lz.Write(Indata, 0, (int) fIn.Length);
  44.             lzdata.WriteTo(fOut);
  45.             fOut.Close();
  46.             fIn.Close();
  47. */
  48. using System;
  49. using System.Collections.Generic;
  50. using System.Text;
  51. using System.IO;
  52. using System.IO.Compression;
  53. namespace bnrLoader
  54. {
  55.     // TODO LZ77Stream for inline decompression of data
  56.     class LZ77Stream : Stream
  57.     {
  58.         MemoryStream outMS;
  59.         CompressionMode cmode;
  60.         MemoryStream sIn;
  61.  
  62.         override public bool CanRead
  63.         {
  64.             get { return outMS.CanRead; }
  65.         }
  66.         override public bool CanSeek
  67.         {
  68.             get { return outMS.CanSeek; }
  69.         }
  70.         override public bool CanWrite
  71.         {
  72.             get { return outMS.CanWrite; }
  73.         }
  74.         override public long Length
  75.         {
  76.             get { return outMS.Length; }
  77.         }
  78.         override public long Position
  79.         {
  80.             get { return outMS.Position; }
  81.             set { outMS.Position = value; }
  82.         }
  83.         override public void Flush()
  84.         {
  85.             outMS.Flush();
  86.         }
  87.         override public long Seek(long offset, System.IO.SeekOrigin loc)
  88.         {
  89.  
  90.             return outMS.Seek(offset, loc);
  91.  
  92.         }
  93.  
  94.         override public void SetLength(long value)
  95.         {
  96.             outMS.SetLength(value);
  97.  
  98.         }
  99.         override public int Read(byte[] buffer, int offset, int length)
  100.         {
  101.             return outMS.Read(buffer, offset, length);
  102.         }
  103.         override public void Write(byte[] buffer, int offset, int count)
  104.         {
  105.             if (cmode == CompressionMode.Decompress)
  106.             {
  107.                 sIn = new MemoryStream(buffer, offset, count);
  108.                 Decompress();
  109.             }
  110.         }
  111.  
  112.  
  113.         public LZ77Stream(Stream stream, CompressionMode mode)
  114.         {
  115.             outMS = (MemoryStream) stream;
  116.             cmode = mode;
  117.  
  118.  
  119.         }
  120.         public void Decompress()
  121.         {
  122.  
  123.             int uncompSize, compSize;
  124.             byte Control;
  125.             short compOffset;
  126.             const int compSizeMin = 3;
  127.             Buffer mbuffer = new Buffer();
  128.             System.IO.BinaryReader bfio = new BinaryReader(this.sIn);
  129.             System.IO.BinaryWriter bfout = new BinaryWriter(this.outMS);
  130.  
  131.             uncompSize = bfio.ReadInt32();
  132.             uncompSize = uncompSize >> 8;
  133.  
  134.  
  135.             while (this.sIn.Position < this.sIn.Length)
  136.             {
  137.                 // Read a new control byte
  138.                 Control = bfio.ReadByte();
  139.                 for (int Index = 7; Index >= 0; Index--)
  140.                 {
  141.                     if (mbuffer.Position == uncompSize)
  142.                         break;
  143.                     if (((Control >> Index) & 1) > 0)
  144.                     {
  145.  
  146.                         compOffset = bfio.ReadInt16();
  147.  
  148.                         compSize = ((compOffset >> 4) & 0xf) + compSizeMin;
  149.                         short compLeft = (short)((compOffset & 0x0f) << 8);
  150.                         short compRight = (short)((compOffset & 0xff00) >> 8);
  151.                         compOffset = (short)(compLeft | compRight);
  152.  
  153.                         for (; compSize > 0; compSize--)
  154.                         {
  155.                             mbuffer.Current = mbuffer.Read((mbuffer.Position % mbuffer.Size) - compOffset - 1);
  156.                             mbuffer.Write(mbuffer.Current);
  157.                             bfout.Write(mbuffer.Current);
  158.                         }
  159.                     }
  160.                     else // Uncompressed data.
  161.                     {
  162.                         // Get the next uncompressed byte.
  163.                         mbuffer.Current = bfio.ReadByte();
  164.  
  165.                         // Push it into the buffer.
  166.                         mbuffer.Write(mbuffer.Current);
  167.  
  168.                         // Write the byte to the output.
  169.                         bfout.Write(mbuffer.Current);
  170.                     }
  171.                 }
  172.             }
  173.         }
  174.  
  175.  
  176.     }
  177.     class Buffer
  178.     {
  179.         public int Size = 0x80000;
  180.         public int Start = 0,
  181.                             End = 0;
  182.         public byte Current;
  183.         public int Position = 0;
  184.         private byte[] bBuffer;
  185.  
  186.         public Buffer()        {
  187.             bBuffer = new byte[Size];
  188.         }
  189.  
  190.         public void Write(byte Value)
  191.         {
  192.             bBuffer[this.End] = Value;
  193.  
  194.             if (this.Start == this.End + 1)
  195.                 this.Start++;
  196.  
  197.             this.End++;
  198.  
  199.             if (this.End >= this.Size)
  200.             {
  201.                 this.End = 0;
  202.                 this.Start = 1;
  203.             }
  204.  
  205.             if (this.Start >= this.Size)
  206.                 this.Start = 0;
  207.  
  208.             this.Position++;
  209.         }
  210.  
  211.         public byte Read(int Position)
  212.         {
  213.             return bBuffer[Position];
  214.         }
  215.     }
  216.  
  217.      
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement