Advertisement
LuigiBlood

compression fail

Jan 19th, 2013
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.60 KB | None | 0 0
  1. public static bool compress(string filename)
  2.         {
  3.             FileStream File1 = File.Open(filename, FileMode.Open);
  4.             FileStream FileC = File.Open(GetFullPathWithoutExtension(filename) + "_compressed.bin", FileMode.Create);
  5.  
  6.             bool copy = false; //Copy Bytes CMD
  7.             bool fill = false; //Fill Bytes CMD
  8.             bool fillw = false; //Fill Words CMD
  9.             bool filli = false; //Fill Incrementing Byte CMD
  10.             bool copy_plus = false; //Copy Bytes from Destination Base + 16-bit
  11.             bool copy_minus = false; //Copy Bytes from Dest Position - minus 8-bit
  12.             bool invert = false; //For invert
  13.  
  14.             int offset = 0;
  15.  
  16.             byte currentbyte = 0;
  17.             byte currentbyte2 = 0;
  18.             byte currentbyte3 = 0;
  19.             byte currentbyte4 = 0;
  20.             UInt16 currentword = 0;
  21.             UInt16 currentword2 = 0;
  22.             UInt16 currentword3 = 0;
  23.             //vars for fill byte/incrementing
  24.             byte inc_check_b = 0;
  25.             byte fillcheck_b = 0;
  26.             //vers for fill word
  27.             UInt16 fillcheck_w = 0;
  28.  
  29.             int length = 1;
  30.  
  31.             currentbyte = (byte)File1.ReadByte();
  32.  
  33.             while (File1.Position < File1.Length)
  34.             {
  35.                 currentbyte2 = (byte)File1.ReadByte();
  36.                 if((File1.Position & 1) == 0) currentword = (UInt16)(currentbyte ^ (currentbyte2 << 8));
  37.                 //Check for possible cmd
  38.                 fillcheck_b = currentbyte;
  39.                 if (fillcheck_b == currentbyte2)
  40.                 {
  41.                     fill = true;
  42.                     offset = (int)File1.Position - 2;
  43.                     length++;
  44.  
  45.                 }
  46.                 else if (fillcheck_b + 1 == currentbyte2)
  47.                 {
  48.                     filli = true;
  49.                     offset = (int)File1.Position - 2;
  50.                     length++;
  51.                 }
  52.                 else if (((File1.Position & 1) == 0) && File1.Position > 2 && fillcheck_w == currentword)
  53.                 {
  54.                     fillw = true;
  55.                     offset = (int)File1.Position - 4;
  56.                     length += 3;
  57.                 }
  58.                 else if (File1.Position > 2)
  59.                 {
  60.                     copy = true;
  61.                     offset = (int)File1.Position - 2;
  62.                 }
  63.  
  64.                 //Make compression
  65.                 if (fill)
  66.                 {
  67.                     Console.WriteLine("fill:" + offset.ToString("X") + ":" + fillcheck_b.ToString("X") + "-" + currentbyte2.ToString("X"));
  68.                     while (fill)
  69.                     {
  70.                         currentbyte3 = (byte)File1.ReadByte();
  71.                         if (currentbyte3 == fillcheck_b) length++;
  72.                         else
  73.                         {
  74.                                 int num = (length / 0x300) + 1;
  75.                                 while (num > 0)
  76.                                 {
  77.                                     int currentlength = 0;
  78.                                     if (num > 1) currentlength = 0x300;
  79.                                     else currentlength = length;
  80.  
  81.                                     if (currentlength <= 0x20)
  82.                                     {
  83.                                         FileC.WriteByte((byte)(0x20 ^ (currentlength - 1)));
  84.                                         FileC.WriteByte(fillcheck_b);
  85.                                         length -= currentlength;
  86.                                         num--;
  87.                                     }
  88.                                     else if (currentlength <= 0x300)
  89.                                     {
  90.                                         FileC.WriteByte((byte)(0xE4 ^ ((currentlength - 1) >> 8)));
  91.                                         FileC.WriteByte((byte)((currentlength - 1) & 0xFF));
  92.                                         FileC.WriteByte(fillcheck_b);
  93.                                         length -= currentlength;
  94.                                         num--;
  95.                                     }
  96.                                 }
  97.                                 break;
  98.                         }
  99.                     }
  100.  
  101.                 }
  102.                 else if (filli)
  103.                 {
  104.                     Console.WriteLine("filli:" + offset.ToString("X") + ":" + fillcheck_b.ToString("X") + "-" + currentbyte2.ToString("X"));
  105.                     while (filli)
  106.                     {
  107.                         currentbyte3 = (byte)File1.ReadByte();
  108.                         if ((currentbyte3 + (length - 1)) == fillcheck_b) length++;
  109.                         else
  110.                         {
  111.                                 int num = (length / 0x300) + 1;
  112.                                 while (num > 0)
  113.                                 {
  114.                                     int currentlength = 0;
  115.                                     if (num > 1) currentlength = 0x300;
  116.                                     else currentlength = length;
  117.  
  118.                                     if (currentlength <= 0x20)
  119.                                     {
  120.                                         FileC.WriteByte((byte)(0x60 ^ (currentlength - 1)));
  121.                                         FileC.WriteByte(fillcheck_b);
  122.                                         length -= currentlength;
  123.                                         num--;
  124.                                     }
  125.                                     else if (currentlength <= 0x300)
  126.                                     {
  127.                                         FileC.WriteByte((byte)(0xEC ^ ((currentlength - 1) >> 8)));
  128.                                         FileC.WriteByte((byte)((currentlength - 1) & 0xFF));
  129.                                         FileC.WriteByte(fillcheck_b);
  130.                                         length -= currentlength;
  131.                                         num--;
  132.                                     }
  133.                                 }
  134.                                 break;
  135.                         }
  136.                     }
  137.  
  138.                 }
  139.                 else if (fillw)
  140.                 {
  141.                     Console.WriteLine("fillw:" + offset.ToString("X") + ":" + fillcheck_w.ToString("X") + "-" + currentword.ToString("X"));
  142.                     while (fillw)
  143.                     {
  144.                         currentword2 = (UInt16)File1.ReadByte();
  145.                         currentbyte3 = (byte)File1.ReadByte();
  146.                         currentword2 ^= (UInt16)(currentbyte3 << 8);
  147.  
  148.                         if ((currentword2) == fillcheck_w) length += 2;
  149.                         else
  150.                         {
  151.                                 int num = (length / 0x300) + 1;
  152.                                 while (num > 0)
  153.                                 {
  154.                                     int currentlength = 0;
  155.                                     if (num > 1) currentlength = 0x300;
  156.                                     else currentlength = length;
  157.  
  158.                                     if (currentlength <= 0x20)
  159.                                     {
  160.                                         FileC.WriteByte((byte)(0x60 ^ (currentlength - 1)));
  161.                                         FileC.WriteByte((byte)(fillcheck_w & 0xFF));
  162.                                         FileC.WriteByte((byte)(fillcheck_w >> 8));
  163.                                         length -= currentlength;
  164.                                         num--;
  165.                                     }
  166.                                     else if (currentlength <= 0x300)
  167.                                     {
  168.                                         FileC.WriteByte((byte)(0xEC ^ ((currentlength - 1) >> 8)));
  169.                                         FileC.WriteByte((byte)((currentlength - 1) & 0xFF));
  170.                                         FileC.WriteByte((byte)(fillcheck_w & 0xFF));
  171.                                         FileC.WriteByte((byte)(fillcheck_w >> 8));
  172.                                         length -= currentlength;
  173.                                         num--;
  174.                                     }
  175.                                 }
  176.                                 break;
  177.                         }
  178.                     }
  179.  
  180.                 }
  181.                 else if (copy)
  182.                 {
  183.                     Console.WriteLine("copy:" + offset.ToString("X") + ":" + fillcheck_b.ToString("X") + "-" + currentbyte2.ToString("X"));
  184.                     bool checkcopy = (File1.Position & 1) == 1;
  185.                     currentbyte3 = (byte)File1.ReadByte();
  186.                     while (copy)
  187.                     {
  188.                         if (!checkcopy)
  189.                         {
  190.                             currentword3 = currentword2;
  191.                             currentbyte3 = currentbyte4;
  192.                             currentbyte4 = (byte)File1.ReadByte();
  193.                             currentword2 = (UInt16)(currentbyte3 << 8);
  194.                             currentword2 ^= (UInt16)(currentbyte4);
  195.                             checkcopy = true;
  196.                         }
  197.                         else
  198.                         {
  199.                             currentbyte3 = currentbyte4;
  200.                             currentbyte4 = (byte)File1.ReadByte();
  201.                             checkcopy = false;
  202.                         }
  203.  
  204.                         //if (currentbyte4 != currentbyte3) Console.WriteLine("WTF1");
  205.                         //else if (currentword3 != currentword2) Console.WriteLine("WTF3");
  206.                         //else if ((currentbyte3 + 1) != currentbyte4) Console.WriteLine("WTF2");
  207.  
  208.  
  209.                         if (currentbyte4 != currentbyte3) length++;
  210.                         //else if ((currentbyte3 + 1) != currentbyte4) length++;
  211.                         else if (currentword3 != currentword2) length++;
  212.                         else
  213.                         {
  214.                             if (currentbyte4 == currentbyte3) length--;
  215.                             //else if ((currentbyte4 + (length - 1)) == currentbyte3) length--;
  216.                             else if (currentword3 == currentword2) length -= 1;
  217.                             File1.Seek(offset, SeekOrigin.Begin);
  218.                                 int num = (length / 0x300) + 1;
  219.                                 while (num > 0)
  220.                                 {
  221.                                     int currentlength = 0;
  222.                                     if (num > 1) currentlength = 0x300;
  223.                                     else currentlength = length;
  224.                                    
  225.                                     if (currentlength <= 0x20)
  226.                                     {
  227.                                         FileC.WriteByte((byte)(0x00 ^ (currentlength - 1)));
  228.                                         while (currentlength > 0)
  229.                                         {
  230.                                             FileC.WriteByte((byte)File1.ReadByte());
  231.                                             length--;
  232.                                             currentlength--;
  233.                                         }
  234.                                         num--;
  235.                                     }
  236.                                     else if (currentlength <= 0x300)
  237.                                     {
  238.                                         FileC.WriteByte((byte)(0xE0 ^ ((currentlength - 1) >> 8)));
  239.                                         FileC.WriteByte((byte)((currentlength - 1) & 0xFF));
  240.                                         while (currentlength > 0)
  241.                                         {
  242.                                             FileC.WriteByte((byte)File1.ReadByte());
  243.                                             length--;
  244.                                             currentlength--;
  245.                                         }
  246.                                         num--;
  247.                                     }
  248.                                 }
  249.                                 break;
  250.                         }
  251.                     }
  252.  
  253.                 }
  254.  
  255.                 length = 1;
  256.                 if (!copy) currentbyte = currentbyte3;
  257.                 else currentbyte = (byte)(currentword2 & 0xFF);
  258.                 fillcheck_w = currentword;
  259.  
  260.                 fill = false;
  261.                 filli = false;
  262.                 fillw = false;
  263.                 copy = false;
  264.                 copy_minus = false;
  265.                 copy_plus = false;
  266.             }
  267.  
  268.             FileC.WriteByte(0xFF);
  269.             File1.Close();
  270.             FileC.Close();
  271.             return true;
  272.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement