Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LZSS
- {
- public static byte[] Decompress(byte[] data)
- {
- bool debug = false;
- byte[] decompBuffer = new byte[0x0FFFFFFF];
- int decompPointer = 4096;
- //List<byte> decompBuffer = new List<byte>();
- //decompBuffer.AddRange(new byte[4096]);
- int c = 0;
- int len = BitConverter.ToInt32(data, c);
- Console.WriteLine("Compressed Binary Length: " + len.ToString("X8"));
- c += 4;
- //c += 4;
- while ( c < len)
- {
- byte flag = data[c];
- if (debug)
- {
- Console.Write((c - 4096).ToString("X8"));
- Console.Write(": ");
- Console.Write(data[c].ToString("X2") + " (flag byte 0b");
- for (int i = 7; i >= 0; i--)
- {
- Console.Write((((flag & (1 << i))) != 0) ? 1 : 0);
- }
- Console.WriteLine(")");
- }
- c++;
- for (int i = 0; i < 8; i++)
- {
- if ((flag & (1 << i)) == 0)
- {
- int offset = (data[c + 0] + ((data[c + 1] & 0xF0) << 4));
- offset = (offset >> 11) == 0 ? offset : -1 ^ 0xFFF | offset;
- int length = ((data[c + 1] & 0x0F)) + 3;
- offset += 18;
- if (debug)
- {
- Console.Write(" ");
- Console.Write(data[c].ToString("X2") + " " + data[c + 1].ToString("X2") + "\t" + length + " bytes from 0x" + offset.ToString("X3"));
- }
- //offset = 4096 - offset;
- int start;
- start = ((int)((decompPointer - 0) & 0xFFFFF000) + offset);
- if (debug)
- {
- Console.Write("\t 0x");
- Console.Write(decompPointer.ToString("X8") + ": ");
- }
- for (int j = start; j < start + length; j++)
- {
- decompBuffer[decompPointer] = decompBuffer[j];
- decompPointer++;
- if (debug)
- {
- Console.Write(decompBuffer[j].ToString("X2"));
- Console.Write(" ");
- }
- }
- if (debug)
- {
- Console.WriteLine(" (i.e. copied from 0x" + start.ToString("X8") + ")");
- }
- //decompBuffer.AddRange(decompBuffer.Skip((decompBuffer.Count()) - (4096 - offset)).Take(length).ToArray());
- c += 2;
- }
- else
- {
- if (debug)
- {
- Console.Write(" ");
- Console.Write(data[c].ToString("X2") + " \tliteral byte");
- Console.Write(" \t 0x");
- Console.Write(decompPointer.ToString("X8") + ": ");
- Console.Write(data[c].ToString("X2"));
- Console.WriteLine(" ");
- }
- decompBuffer[decompPointer] = data[c];
- decompPointer++;
- c++;
- }
- }
- if (debug)
- {
- Console.WriteLine((c).ToString("X8") + "/" + len.ToString("X8"));
- }
- }
- return decompBuffer.Take(decompPointer).Skip(4096).ToArray();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement