Advertisement
Guest User

Untitled

a guest
Sep 4th, 2012
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.35 KB | None | 0 0
  1. public class CompressFile
  2. {
  3.     // Fields
  4.     private static bool bool_0 = false;
  5.     private static CoderPropID[] coderPropID_0 = new CoderPropID[] { CoderPropID.DictionarySize, CoderPropID.PosStateBits, CoderPropID.LitContextBits, CoderPropID.LitPosBits, CoderPropID.Algorithm, CoderPropID.NumFastBytes, CoderPropID.MatchFinder, CoderPropID.EndMarker };
  6.     private static int int_0 = 0x10000;
  7.     private static object[] object_0 = new object[] { int_0, 2, 3, 0, 1, 0x20, "bt4", bool_0 };
  8.     private uint uint_0;
  9.  
  10.     // Methods
  11.     public static byte[] Compress(byte[] inputBytes)
  12.     {
  13.         Encoder encoder = new Encoder();
  14.         Stream inStream = new MemoryStream(inputBytes);
  15.         Stream outStream = new MemoryStream();
  16.         encoder.SetCoderProperties(coderPropID_0, object_0);
  17.         encoder.WriteCoderProperties(outStream);
  18.         for (int i = 0; i < 8; i++)
  19.         {
  20.             outStream.WriteByte((byte) (inStream.Length >> (8 * i)));
  21.         }
  22.         encoder.Code(inStream, outStream, inStream.Length, 0L, null);
  23.         outStream.Position = 13L;
  24.         byte[] buffer = new byte[outStream.Length - 13L];
  25.         outStream.Read(buffer, 0, buffer.Length);
  26.         byte[] destinationArray = new byte[10];
  27.         destinationArray[0] = 0x10;
  28.         destinationArray[1] = 0x11;
  29.         byte[] bytes = BitConverter.GetBytes(buffer.Length);
  30.         Array.Copy(bytes, 0, destinationArray, 2, bytes.Length);
  31.         byte[] sourceArray = BitConverter.GetBytes(inputBytes.Length);
  32.         Array.Copy(sourceArray, 0, destinationArray, 6, sourceArray.Length);
  33.         List<byte> list = new List<byte>();
  34.         list.AddRange(destinationArray);
  35.         list.AddRange(buffer);
  36.         byte[] collection = BitConverter.GetBytes((int) CheckSumCRC32.GetCRC32CheckSum(list.ToArray()));
  37.         list.AddRange(collection);
  38.         return list.ToArray();
  39.     }
  40.  
  41.     public static List<CompressFrangment> ConvertToCompressFileArray(byte[] inputBytes)
  42.     {
  43.         CompressFrangment frangment;
  44.         List<CompressFrangment> list = new List<CompressFrangment>();
  45.         for (long i = 0L; i < (inputBytes.Length - 4); i = (i + 10L) + frangment.compressSize)
  46.         {
  47.             frangment = new CompressFrangment {
  48.                 headVersion = inputBytes[(int) ((IntPtr) i)],
  49.                 algorithmAndLevel = inputBytes[(int) ((IntPtr) (i + 1L))],
  50.                 compressSize = BitConverter.ToInt32(new byte[] { inputBytes[(int) ((IntPtr) (i + 2L))], inputBytes[(int) ((IntPtr) (i + 3L))], inputBytes[(int) ((IntPtr) (i + 4L))], inputBytes[(int) ((IntPtr) (i + 5L))] }, 0),
  51.                 uncompressSize = BitConverter.ToInt32(new byte[] { inputBytes[(int) ((IntPtr) (i + 6L))], inputBytes[(int) ((IntPtr) (i + 7L))], inputBytes[(int) ((IntPtr) (i + 8L))], inputBytes[(int) ((IntPtr) (i + 9L))] }, 0),
  52.                 compressData = new byte[frangment.compressSize]
  53.             };
  54.             Array.Copy(inputBytes, i + 10L, frangment.compressData, 0L, (long) frangment.compressSize);
  55.             list.Add(frangment);
  56.         }
  57.         return list;
  58.     }
  59.  
  60.     public static byte[] Decompress(List<CompressFrangment> cmpList)
  61.     {
  62.         List<byte> list = new List<byte>();
  63.         foreach (CompressFrangment frangment in cmpList)
  64.         {
  65.             byte[] compressData = frangment.compressData;
  66.             byte num = (byte) (frangment.algorithmAndLevel / 0x10);
  67.             byte num2 = (byte) (frangment.algorithmAndLevel & 15);
  68.             byte[] buffer = new byte[frangment.uncompressSize];
  69.             if (num == 2)
  70.             {
  71.                 uint num3 = 0;
  72.                 FastLZWrapper.DataDecompress(frangment.compressData, buffer, ref num3);
  73.                 if (num3 != frangment.uncompressSize)
  74.                 {
  75.                     throw new CommonException(CommonErrorCode.Communication_Compression_FASTLZ_Decompress_Failed);
  76.                 }
  77.             }
  78.             else if (num == 1)
  79.             {
  80.                 Decoder decoder = new Decoder();
  81.                 byte[] buffer3 = new byte[5];
  82.                 buffer3[0] = 0x5d;
  83.                 buffer3[3] = 1;
  84.                 byte[] properties = buffer3;
  85.                 switch (num2)
  86.                 {
  87.                     case 0:
  88.                     {
  89.                         byte[] buffer7 = new byte[5];
  90.                         buffer7[0] = 0x5d;
  91.                         buffer7[2] = 0x40;
  92.                         properties = buffer7;
  93.                         break;
  94.                     }
  95.                     case 2:
  96.                     {
  97.                         byte[] buffer6 = new byte[5];
  98.                         buffer6[0] = 0x5d;
  99.                         buffer6[3] = 4;
  100.                         properties = buffer6;
  101.                         break;
  102.                     }
  103.                 }
  104.                 decoder.SetDecoderProperties(properties);
  105.                 List<byte> list2 = new List<byte>();
  106.                 list2.AddRange(properties);
  107.                 list2.AddRange(BitConverter.GetBytes(frangment.uncompressSize));
  108.                 list2.AddRange(new byte[4]);
  109.                 list2.AddRange(frangment.compressData);
  110.                 MemoryStream stream = new MemoryStream(list2.ToArray());
  111.                 MemoryStream outStream = new MemoryStream(frangment.uncompressSize);
  112.                 decoder.Train(stream);
  113.                 stream.Position = 13L;
  114.                 decoder.Code(stream, outStream, (long) frangment.compressData.Length, (long) frangment.uncompressSize, null);
  115.                 buffer = outStream.ToArray();
  116.             }
  117.             compressData = buffer;
  118.             list.AddRange(compressData);
  119.         }
  120.         return list.ToArray();
  121.     }
  122.  
  123.     // Properties
  124.     public uint UInt32_0
  125.     {
  126.         get
  127.         {
  128.             return this.uint_0;
  129.         }
  130.         set
  131.         {
  132.             this.uint_0 = value;
  133.         }
  134.     }
  135.  
  136.     // Nested Types
  137.     internal static class Class3
  138.     {
  139.         // Fields
  140.         public const byte byte_0 = 0x10;
  141.         public const int int_0 = 10;
  142.         public const int int_1 = 2;
  143.         public const int int_2 = 6;
  144.     }
  145.  
  146.     public enum CompressAlogorithem : byte
  147.     {
  148.         FastLZ = 2,
  149.         LZMA = 1,
  150.         NoCompression = 0
  151.     }
  152.  
  153.     public enum CompressLevel : byte
  154.     {
  155.         Level0 = 0,
  156.         Level1 = 1,
  157.         Level2 = 2
  158.     }
  159. }
  160.  
  161.  
  162. Collapse Methods
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement