Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System;
- using System.Runtime.InteropServices;
- namespace lzss
- {
- public static class Lzss
- {
- private const int a = 12;
- private const int b = 4;
- private const int c = 0x1000;
- private const int d = 0x10;
- private const int e = 1;
- private const int f = 0x11;
- private const int g = 0x1000;
- private const int h = 0;
- private const int i = 0;
- private static int MOD_WINDOW(int A_0)
- {
- return (A_0 & 0xfff);
- }
- private static void a(ref Lzss.ctxt ctxt)
- {
- ctxt.DataBuffer[0] = 0;
- ctxt.FlagBitMask = 1;
- ctxt.f = ctxt.e;
- ctxt.e = 1;
- }
- private static unsafe int InputBit(byte* A_0, ref Lzss.ctxt A_1)
- {
- A_1.inc_input_string = 0;
- if (A_1.FlagBitMask == 0x100)
- {
- InitInputBuffer(A_0, ref A_1);
- A_1.inc_input_string = 1;
- }
- A_1.FlagBitMask = A_1.FlagBitMask << 1;
- return (A_1.DataBuffer[0] & (A_1.FlagBitMask >> 1));
- }
- private static void a(int A_0, ref Lzss.ctxt A_1)
- {
- if (A_1.b[A_0].a != 0)
- {
- if (A_1.b[A_0].c == 0)
- {
- b(A_0, A_1.b[A_0].b, ref A_1);
- }
- else if (A_1.b[A_0].b == 0)
- {
- b(A_0, A_1.b[A_0].c, ref A_1);
- }
- else
- {
- int num = b(A_0, ref A_1);
- a(num, ref A_1);
- a(A_0, num, ref A_1);
- }
- }
- }
- private static int a(int A_0, ref int A_1, ref Lzss.ctxt A_2)
- {
- int num = 0;
- int c = 0;
- int num3 = 0;
- int num4 = 0;
- int b = 0;
- if (A_0 == 0)
- {
- return 0;
- }
- c = A_2.b[0x1000].c;
- num4 = 0;
- while (true)
- {
- for (num = 0; num < 0x11; num++)
- {
- num3 = A_2.window[MOD_WINDOW((int)(A_0 + num))] - A_2.window[MOD_WINDOW((int)(c + num))];
- if (num3 != 0)
- {
- break;
- }
- }
- if (num >= num4)
- {
- num4 = num;
- A_1 = c;
- if (num4 >= 0x11)
- {
- a(c, A_0, ref A_2);
- return num4;
- }
- }
- if (num3 >= 0)
- {
- b = A_2.b[c].c;
- }
- else
- {
- b = A_2.b[c].b;
- }
- if (b == 0)
- {
- b = A_0;
- A_2.b[A_0].a = c;
- A_2.b[A_0].c = 0;
- A_2.b[A_0].b = 0;
- return num4;
- }
- c = b;
- }
- }
- private static void a(int A_0, int A_1, ref Lzss.ctxt A_2)
- {
- int a = A_2.b[A_0].a;
- if (A_2.b[a].b == A_0)
- {
- A_2.b[a].b = A_1;
- }
- else
- {
- A_2.b[a].c = A_1;
- }
- A_2.b[A_1] = A_2.b[A_0];
- A_2.b[A_2.b[A_1].b].a = A_1;
- A_2.b[A_2.b[A_1].c].a = A_1;
- A_2.b[A_0].a = 0;
- }
- private static unsafe int a(int A_0, byte* A_1, ref Lzss.ctxt A_2)
- {
- A_2.DataBuffer[A_2.e++] = (byte)A_0;
- A_2.DataBuffer[0] = (byte)(A_2.DataBuffer[0] | ((byte)A_2.FlagBitMask));
- A_2.FlagBitMask = A_2.FlagBitMask << 1;
- A_2.j = 0;
- if (A_2.FlagBitMask == 0x100)
- {
- A_2.j = 1;
- return c(A_1, ref A_2);
- }
- return 1;
- }
- private static unsafe int uncompress(byte* input_string, byte* output_string, int uncompressedSize)
- {
- Lzss.ctxt ctxt = new Lzss.ctxt();
- ctxt.init();
- byte* numPtr = input_string;
- InitInputBuffer(input_string, ref ctxt);
- input_string++;
- int index = 1;
- while (uncompressedSize != 0)
- {
- byte c;
- if (Lzss.InputBit(input_string, ref ctxt) != 0)
- {
- if (ctxt.inc_input_string == 1)
- {
- input_string++;
- }
- c = input_string[0];
- input_string++;
- output_string[0] = c;
- output_string++;
- uncompressedSize--;
- ctxt.window[index] = c;
- index = Lzss.MOD_WINDOW((int)(index + 1));
- }
- else
- {
- if (ctxt.inc_input_string == 1)
- {
- input_string++;
- }
- int match_length = input_string[0];
- input_string++;
- int match_position = input_string[0];
- input_string++;
- match_position |= (match_length & 15) << 8;
- match_length = match_length >> 4;
- match_length++;
- if (match_length < uncompressedSize)
- {
- uncompressedSize -= match_length + 1;
- }
- else
- {
- uncompressedSize = 0;
- match_length = uncompressedSize - 1;
- }
- for (int i = 0; i <= match_length; i++)
- {
- c = ctxt.window[Lzss.MOD_WINDOW((int)(match_position + i))];
- output_string[0] = c;
- output_string++;
- ctxt.window[index] = c;
- index = Lzss.MOD_WINDOW((int)(index + 1));
- }
- }
- }
- ulong num6 = (ulong)((long)((input_string - numPtr) / 1));
- return (int)num6;
- }
- private static unsafe int a(int A_0, int A_1, byte* A_2, ref Lzss.ctxt A_3)
- {
- A_3.DataBuffer[A_3.e] = (byte)(A_1 << 4);
- A_3.DataBuffer[A_3.e++] = (byte)(A_3.DataBuffer[A_3.e++] | ((byte)(A_0 >> 8)));
- A_3.DataBuffer[A_3.e++] = (byte)(A_0 & 0xff);
- A_3.FlagBitMask = A_3.FlagBitMask << 1;
- A_3.j = 0;
- if (A_3.FlagBitMask == 0x100)
- {
- A_3.j = 1;
- return c(A_2, ref A_3);
- }
- return 1;
- }
- private static int b(int A_0, ref Lzss.ctxt A_1)
- {
- int b = A_1.b[A_0].b;
- while (A_1.b[b].c != 0)
- {
- b = A_1.b[b].c;
- }
- return b;
- }
- private static unsafe void InitInputBuffer(byte* A_0, ref Lzss.ctxt A_1)
- {
- A_1.FlagBitMask = 1;
- A_1.DataBuffer[0] = A_0[0];
- }
- private static void b(int A_0, int A_1, ref Lzss.ctxt A_2)
- {
- A_2.b[A_1].a = A_2.b[A_0].a;
- if (A_2.b[A_2.b[A_0].a].c == A_0)
- {
- A_2.b[A_2.b[A_0].a].c = A_1;
- }
- else
- {
- A_2.b[A_2.b[A_0].a].b = A_1;
- }
- A_2.b[A_0].a = 0;
- }
- private static unsafe int b(byte* A_0, byte* A_1, int A_2)
- {
- byte num2;
- Lzss.ctxt a = new Lzss.ctxt();
- a.init();
- a.h = 0;
- a.g = A_2;
- Lzss.a(ref a);
- int num4 = 1;
- int num = 0;
- while (num < 0x11)
- {
- num2 = A_0[0];
- A_0++;
- A_2--;
- if (A_2 < 0)
- {
- break;
- }
- a.window[num4 + num] = num2;
- num++;
- }
- int num3 = num;
- c(num4, ref a);
- int num6 = 0;
- int num7 = 0;
- while (num3 > 0)
- {
- int num5;
- if (num6 > num3)
- {
- num6 = num3;
- }
- if (num6 <= 1)
- {
- num5 = 1;
- if (Lzss.a(a.window[num4], A_1, ref a) == 0)
- {
- return 0;
- }
- if (a.j != 0)
- {
- A_1 += (byte*)a.f;
- }
- }
- else
- {
- if (Lzss.a(num7, num6 - 2, A_1, ref a) == 0)
- {
- return 0;
- }
- if (a.j != 0)
- {
- A_1 += (byte*)a.f;
- }
- num5 = num6;
- }
- for (num = 0; num < num5; num++)
- {
- Lzss.a(Lzss.MOD_WINDOW((int)(num4 + 0x11)), ref a);
- num2 = A_0[0];
- A_2--;
- if (A_2 < 0)
- {
- num3--;
- }
- else
- {
- A_0++;
- a.window[Lzss.MOD_WINDOW((int)(num4 + 0x11))] = num2;
- }
- num4 = Lzss.MOD_WINDOW((int)(num4 + 1));
- if (num3 != 0)
- {
- num6 = Lzss.a(num4, ref num7, ref a);
- }
- }
- }
- if (a.j == 0)
- {
- c(A_1, ref a);
- }
- return a.h;
- }
- private static void c(int A_0, ref Lzss.ctxt A_1)
- {
- for (int i = 0; i < 0x1001; i++)
- {
- A_1.b[i].a = 0;
- A_1.b[i].c = 0;
- A_1.b[i].b = 0;
- }
- A_1.b[0x1000].c = A_0;
- A_1.b[A_0].a = 0x1000;
- A_1.b[A_0].c = 0;
- A_1.b[A_0].b = 0;
- }
- private static unsafe int c(byte* A_0, ref Lzss.ctxt A_1)
- {
- if (A_1.e != 1)
- {
- for (int i = 0; i < A_1.e; i++)
- {
- A_0[i] = A_1.DataBuffer[i];
- }
- A_1.h += (int)A_1.e;
- a(ref A_1);
- }
- return 1;
- }
- public static unsafe byte[] CompressContent(byte[] uncompressedContent)
- {
- byte[] destinationArray = new byte[uncompressedContent.Length + 100];
- Array.Copy(uncompressedContent, destinationArray, uncompressedContent.Length);
- int length = 0;
- fixed (byte* numRef = uncompressedContent)
- {
- fixed (byte* numRef2 = destinationArray)
- {
- length = b(numRef, numRef2, uncompressedContent.Length);
- }
- }
- byte[] buffer2 = new byte[length];
- Array.Copy(destinationArray, buffer2, length);
- return buffer2;
- }
- public static unsafe byte[] UncompressContent(byte[] compressedContent, int uncompressedSize)
- {
- byte[] destinationArray = new byte[compressedContent.Length + 100];
- Array.Copy(compressedContent, destinationArray, compressedContent.Length);
- byte[] sourceArray = new byte[uncompressedSize + 0x400];
- fixed (byte* numRef = sourceArray)
- {
- fixed (byte* numRef2 = destinationArray)
- {
- int num = uncompress(numRef2, numRef, uncompressedSize);
- }
- }
- byte[] buffer3 = new byte[uncompressedSize];
- Array.Copy(sourceArray, buffer3, uncompressedSize);
- return buffer3;
- }
- [StructLayout(LayoutKind.Sequential)]
- private struct ctxt
- {
- public byte[] window;
- public Lzss.b[] b;
- public byte[] DataBuffer;
- public int FlagBitMask;
- public uint e;
- public uint f;
- public int g;
- public int h;
- public int inc_input_string;
- public int j;
- public void init()
- {
- this.window = new byte[0x1000];
- this.b = new Lzss.b[0x1001];
- this.DataBuffer = new byte[0x11];
- }
- }
- [StructLayout(LayoutKind.Sequential)]
- private struct b
- {
- public int a;
- public int b;
- public int c;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement