Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static bool compress(string filename)
- {
- FileStream File1 = File.Open(filename, FileMode.Open);
- FileStream FileC = File.Open(GetFullPathWithoutExtension(filename) + "_compressed.bin", FileMode.Create);
- bool copy = false; //Copy Bytes CMD
- bool fill = false; //Fill Bytes CMD
- bool fillw = false; //Fill Words CMD
- bool filli = false; //Fill Incrementing Byte CMD
- bool copy_plus = false; //Copy Bytes from Destination Base + 16-bit
- bool copy_minus = false; //Copy Bytes from Dest Position - minus 8-bit
- bool invert = false; //For invert
- int offset = 0;
- byte currentbyte = 0;
- byte currentbyte2 = 0;
- byte currentbyte3 = 0;
- byte currentbyte4 = 0;
- UInt16 currentword = 0;
- UInt16 currentword2 = 0;
- UInt16 currentword3 = 0;
- //vars for fill byte/incrementing
- byte inc_check_b = 0;
- byte fillcheck_b = 0;
- //vers for fill word
- UInt16 fillcheck_w = 0;
- int length = 1;
- currentbyte = (byte)File1.ReadByte();
- while (File1.Position < File1.Length)
- {
- currentbyte2 = (byte)File1.ReadByte();
- if((File1.Position & 1) == 0) currentword = (UInt16)(currentbyte ^ (currentbyte2 << 8));
- //Check for possible cmd
- fillcheck_b = currentbyte;
- if (fillcheck_b == currentbyte2)
- {
- fill = true;
- offset = (int)File1.Position - 2;
- length++;
- }
- else if (fillcheck_b + 1 == currentbyte2)
- {
- filli = true;
- offset = (int)File1.Position - 2;
- length++;
- }
- else if (((File1.Position & 1) == 0) && File1.Position > 2 && fillcheck_w == currentword)
- {
- fillw = true;
- offset = (int)File1.Position - 4;
- length += 3;
- }
- else if (File1.Position > 2)
- {
- copy = true;
- offset = (int)File1.Position - 2;
- }
- //Make compression
- if (fill)
- {
- Console.WriteLine("fill:" + offset.ToString("X") + ":" + fillcheck_b.ToString("X") + "-" + currentbyte2.ToString("X"));
- while (fill)
- {
- currentbyte3 = (byte)File1.ReadByte();
- if (currentbyte3 == fillcheck_b) length++;
- else
- {
- int num = (length / 0x300) + 1;
- while (num > 0)
- {
- int currentlength = 0;
- if (num > 1) currentlength = 0x300;
- else currentlength = length;
- if (currentlength <= 0x20)
- {
- FileC.WriteByte((byte)(0x20 ^ (currentlength - 1)));
- FileC.WriteByte(fillcheck_b);
- length -= currentlength;
- num--;
- }
- else if (currentlength <= 0x300)
- {
- FileC.WriteByte((byte)(0xE4 ^ ((currentlength - 1) >> 8)));
- FileC.WriteByte((byte)((currentlength - 1) & 0xFF));
- FileC.WriteByte(fillcheck_b);
- length -= currentlength;
- num--;
- }
- }
- break;
- }
- }
- }
- else if (filli)
- {
- Console.WriteLine("filli:" + offset.ToString("X") + ":" + fillcheck_b.ToString("X") + "-" + currentbyte2.ToString("X"));
- while (filli)
- {
- currentbyte3 = (byte)File1.ReadByte();
- if ((currentbyte3 + (length - 1)) == fillcheck_b) length++;
- else
- {
- int num = (length / 0x300) + 1;
- while (num > 0)
- {
- int currentlength = 0;
- if (num > 1) currentlength = 0x300;
- else currentlength = length;
- if (currentlength <= 0x20)
- {
- FileC.WriteByte((byte)(0x60 ^ (currentlength - 1)));
- FileC.WriteByte(fillcheck_b);
- length -= currentlength;
- num--;
- }
- else if (currentlength <= 0x300)
- {
- FileC.WriteByte((byte)(0xEC ^ ((currentlength - 1) >> 8)));
- FileC.WriteByte((byte)((currentlength - 1) & 0xFF));
- FileC.WriteByte(fillcheck_b);
- length -= currentlength;
- num--;
- }
- }
- break;
- }
- }
- }
- else if (fillw)
- {
- Console.WriteLine("fillw:" + offset.ToString("X") + ":" + fillcheck_w.ToString("X") + "-" + currentword.ToString("X"));
- while (fillw)
- {
- currentword2 = (UInt16)File1.ReadByte();
- currentbyte3 = (byte)File1.ReadByte();
- currentword2 ^= (UInt16)(currentbyte3 << 8);
- if ((currentword2) == fillcheck_w) length += 2;
- else
- {
- int num = (length / 0x300) + 1;
- while (num > 0)
- {
- int currentlength = 0;
- if (num > 1) currentlength = 0x300;
- else currentlength = length;
- if (currentlength <= 0x20)
- {
- FileC.WriteByte((byte)(0x60 ^ (currentlength - 1)));
- FileC.WriteByte((byte)(fillcheck_w & 0xFF));
- FileC.WriteByte((byte)(fillcheck_w >> 8));
- length -= currentlength;
- num--;
- }
- else if (currentlength <= 0x300)
- {
- FileC.WriteByte((byte)(0xEC ^ ((currentlength - 1) >> 8)));
- FileC.WriteByte((byte)((currentlength - 1) & 0xFF));
- FileC.WriteByte((byte)(fillcheck_w & 0xFF));
- FileC.WriteByte((byte)(fillcheck_w >> 8));
- length -= currentlength;
- num--;
- }
- }
- break;
- }
- }
- }
- else if (copy)
- {
- Console.WriteLine("copy:" + offset.ToString("X") + ":" + fillcheck_b.ToString("X") + "-" + currentbyte2.ToString("X"));
- bool checkcopy = (File1.Position & 1) == 1;
- currentbyte3 = (byte)File1.ReadByte();
- while (copy)
- {
- if (!checkcopy)
- {
- currentword3 = currentword2;
- currentbyte3 = currentbyte4;
- currentbyte4 = (byte)File1.ReadByte();
- currentword2 = (UInt16)(currentbyte3 << 8);
- currentword2 ^= (UInt16)(currentbyte4);
- checkcopy = true;
- }
- else
- {
- currentbyte3 = currentbyte4;
- currentbyte4 = (byte)File1.ReadByte();
- checkcopy = false;
- }
- //if (currentbyte4 != currentbyte3) Console.WriteLine("WTF1");
- //else if (currentword3 != currentword2) Console.WriteLine("WTF3");
- //else if ((currentbyte3 + 1) != currentbyte4) Console.WriteLine("WTF2");
- if (currentbyte4 != currentbyte3) length++;
- //else if ((currentbyte3 + 1) != currentbyte4) length++;
- else if (currentword3 != currentword2) length++;
- else
- {
- if (currentbyte4 == currentbyte3) length--;
- //else if ((currentbyte4 + (length - 1)) == currentbyte3) length--;
- else if (currentword3 == currentword2) length -= 1;
- File1.Seek(offset, SeekOrigin.Begin);
- int num = (length / 0x300) + 1;
- while (num > 0)
- {
- int currentlength = 0;
- if (num > 1) currentlength = 0x300;
- else currentlength = length;
- if (currentlength <= 0x20)
- {
- FileC.WriteByte((byte)(0x00 ^ (currentlength - 1)));
- while (currentlength > 0)
- {
- FileC.WriteByte((byte)File1.ReadByte());
- length--;
- currentlength--;
- }
- num--;
- }
- else if (currentlength <= 0x300)
- {
- FileC.WriteByte((byte)(0xE0 ^ ((currentlength - 1) >> 8)));
- FileC.WriteByte((byte)((currentlength - 1) & 0xFF));
- while (currentlength > 0)
- {
- FileC.WriteByte((byte)File1.ReadByte());
- length--;
- currentlength--;
- }
- num--;
- }
- }
- break;
- }
- }
- }
- length = 1;
- if (!copy) currentbyte = currentbyte3;
- else currentbyte = (byte)(currentword2 & 0xFF);
- fillcheck_w = currentword;
- fill = false;
- filli = false;
- fillw = false;
- copy = false;
- copy_minus = false;
- copy_plus = false;
- }
- FileC.WriteByte(0xFF);
- File1.Close();
- FileC.Close();
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement