title: Implementing a LZ77 Compression algorithm into C#
body: <p>Well I am currently trying to implement a compression algorithm in my project, it has to be lz77 as a matter of effect... I am already able to decompress data but I can't imagine where to start in terms of compressing. I thought it would be best to pass by a byte array with data, but that's about it...
  My problem is that all descriptions of the algorithm are quite cryptic for me to understand. I would appreciate a clear description of how the algorithm works and what I have to watch for.
  Also: Am I obliged to use unsafe methods and pointers when coding in C#? It would be better to avoid that... I suppose.</p>
  
  <p>Here is what I got so far thanks to the information you gave me:</p>
  
  <pre><code>private const int searchWindow = 4095;
      private const byte lookaheadWindow = 15;
      public static byte[] lzCompressData(byte[] input)
      {
          int position = 0;
          List&lt;byte&gt; tempInput = input.ToList();
          List&lt;byte&gt; output = new List&lt;byte&gt;();
          MemoryStream init = new MemoryStream();
          BinaryWriter inbw = new BinaryWriter(init);
          inbw.Write(((input.Length &lt;&lt; 8) &amp; 0xFFFFFF00) | 0x10);
          output.AddRange(init.ToArray());
          while (position &lt; input.Length)
          {
              byte decoder = 0;
              List&lt;byte&gt; tempOutput = new List&lt;byte&gt;();
              for (int i = 0; i &lt; 8; ++i)
              {
                  List&lt;byte&gt; eligible;
                  if(position &lt; 255)
                  {
                      eligible = tempInput.GetRange(0, position);
                  }
                  else
                  {
                      eligible = tempInput.GetRange(position - searchWindow, searchWindow);
                  }
                  if (!(position &gt; input.Length - 8))
                  {
                      MemoryStream ms = new MemoryStream(eligible.ToArray());
                      List&lt;byte&gt; currentSequence = new List&lt;byte&gt;();
                      currentSequence.Add(input[position]);
                      int offset = 0;
                      int length = 0;
                      long tempoffset = StreamHelper.FindPosition(ms, currentSequence.ToArray());
                      while ((tempoffset != -1) &amp;&amp; (length &lt; lookaheadWindow) &amp;&amp; position &lt; input.Length - 8)
                      {
                          offset = (int)tempoffset;
                          length = currentSequence.Count;
                          position++;
                          currentSequence.Add(input[position]);
  
                      }
                      if (length &gt;= 3)
                      {
                          decoder = (byte)(decoder | (byte)(1 &lt;&lt; i));
                          byte b1 = (byte)((length &lt;&lt; 4) | (offset &gt;&gt; 8));
                          byte b2 = (byte)(offset &amp; 0xFF);
                          tempOutput.Add(b1);
                          tempOutput.Add(b2);
                      }
                      else
                      {
                          tempOutput.Add(input[position]);
                          position++;
                      }
                  }
                  else
                  {
                      if (position &lt; input.Length)
                      {
                          tempOutput.Add(input[position]);
                          position++;
                      }
                      else
                      {
                          tempOutput.Add(0xFF);
                      }
                  }
              }
              output.Add(decoder);
              output.AddRange(tempOutput.ToArray());
          }
          return output.ToArray();
      }
  </code></pre>