Advertisement
Guest User

LZSS

a guest
Sep 25th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.05 KB | None | 0 0
  1. public class LZSS
  2.     {
  3.         public static byte[] Decompress(byte[] data)
  4.         {
  5.             bool debug = false;
  6.             byte[] decompBuffer = new byte[0x0FFFFFFF];
  7.             int decompPointer = 4096;
  8.            
  9.             //List<byte> decompBuffer = new List<byte>();
  10.             //decompBuffer.AddRange(new byte[4096]);
  11.  
  12.             int c = 0;
  13.             int len = BitConverter.ToInt32(data, c);
  14.             Console.WriteLine("Compressed Binary Length: " + len.ToString("X8"));
  15.             c += 4;
  16.             //c += 4;
  17.  
  18.             while ( c < len)
  19.             {
  20.                 byte flag = data[c];
  21.  
  22.                 if (debug)
  23.                 {
  24.                     Console.Write((c - 4096).ToString("X8"));
  25.                     Console.Write(": ");
  26.                     Console.Write(data[c].ToString("X2") + " (flag byte 0b");
  27.                     for (int i = 7; i >= 0; i--)
  28.                     {
  29.                         Console.Write((((flag & (1 << i))) != 0) ? 1 : 0);
  30.                     }
  31.                     Console.WriteLine(")");
  32.                 }
  33.                 c++;
  34.  
  35.                 for (int i = 0; i < 8; i++)
  36.                 {
  37.                     if ((flag & (1 << i)) == 0)
  38.                     {
  39.                         int offset = (data[c + 0] + ((data[c + 1] & 0xF0) << 4));
  40.                         offset = (offset >> 11) == 0 ? offset : -1 ^ 0xFFF | offset;
  41.                         int length = ((data[c + 1] & 0x0F)) + 3;
  42.  
  43.                         offset += 18;
  44.  
  45.                         if (debug)
  46.                         {
  47.                            
  48.                             Console.Write("             ");
  49.                             Console.Write(data[c].ToString("X2") + " " + data[c + 1].ToString("X2") + "\t" + length + " bytes from 0x" + offset.ToString("X3"));
  50.                         }
  51.                         //offset = 4096 - offset;
  52.  
  53.                         int start;
  54.  
  55.                         start = ((int)((decompPointer - 0) & 0xFFFFF000) + offset);
  56.  
  57.                         if (debug)
  58.                         {
  59.                             Console.Write("\t 0x");
  60.                             Console.Write(decompPointer.ToString("X8") + ": ");
  61.                         }
  62.  
  63.                         for (int j = start; j < start + length; j++)
  64.                         {
  65.                             decompBuffer[decompPointer] = decompBuffer[j];
  66.                             decompPointer++;
  67.                             if (debug)
  68.                             {
  69.                                 Console.Write(decompBuffer[j].ToString("X2"));
  70.                                 Console.Write(" ");
  71.                             }
  72.                         }
  73.                         if (debug)
  74.                         {
  75.                             Console.WriteLine("   (i.e. copied from 0x" + start.ToString("X8") + ")");
  76.                         }
  77.                         //decompBuffer.AddRange(decompBuffer.Skip((decompBuffer.Count()) - (4096 - offset)).Take(length).ToArray());
  78.                         c += 2;
  79.                     }
  80.                     else
  81.                     {
  82.                         if (debug)
  83.                         {
  84.                             Console.Write("             ");
  85.                             Console.Write(data[c].ToString("X2") + "   \tliteral byte");
  86.                             Console.Write("     \t 0x");
  87.                             Console.Write(decompPointer.ToString("X8") + ": ");
  88.                             Console.Write(data[c].ToString("X2"));
  89.                             Console.WriteLine(" ");
  90.                         }
  91.  
  92.                         decompBuffer[decompPointer] = data[c];
  93.                         decompPointer++;
  94.                         c++;
  95.                     }
  96.                 }
  97.                 if (debug)
  98.                 {
  99.                     Console.WriteLine((c).ToString("X8") + "/" + len.ToString("X8"));
  100.                 }
  101.             }
  102.             return decompBuffer.Take(decompPointer).Skip(4096).ToArray();
  103.         }
  104.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement