Advertisement
Guest User

Untitled

a guest
Sep 4th, 2012
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.95 KB | None | 0 0
  1. [Serializable]
  2. public class CodeplugCommFile
  3. {
  4.     // Fields
  5.     private bool IsCrcBigEndian;
  6.  
  7.     // Methods
  8.     public CodeplugCommFile(bool isCrcBigEndian)
  9.     {
  10.         this.FileData = null;
  11.         this.IsCrcBigEndian = isCrcBigEndian;
  12.     }
  13.  
  14.     public PbaObject Deserialize(string ishFilePath)
  15.     {
  16.         FileStream stream = new FileStream(ishFilePath, FileMode.Open, FileAccess.Read);
  17.         byte[] buffer = new byte[stream.Length];
  18.         int length = (int) stream.Length;
  19.         int offset = 0;
  20.         while (length > 0)
  21.         {
  22.             int num3 = stream.Read(buffer, offset, length);
  23.             if (num3 == 0)
  24.             {
  25.                 break;
  26.             }
  27.             offset += num3;
  28.             length -= num3;
  29.         }
  30.         stream.Close();
  31.         if (!smethod_4(buffer))
  32.         {
  33.             throw new CommonException(CommonErrorCode.CommonFileInvalidCrc32);
  34.         }
  35.         CodeplugCommFileHeader header = smethod_5(buffer);
  36.         return this.method_2(header, buffer);
  37.     }
  38.  
  39.     public PbaObject Deserialize(byte[] fileData)
  40.     {
  41.         if (!smethod_4(fileData))
  42.         {
  43.             throw new CommonException(CommonErrorCode.CommonFileInvalidCrc32);
  44.         }
  45.         CodeplugCommFileHeader header = smethod_5(fileData);
  46.         return this.method_2(header, fileData);
  47.     }
  48.  
  49.     private byte[] method_0(CodeplugCommFileHeader codeplugCommFileHeader_0, PbaObject pbaObject_0)
  50.     {
  51.         switch (codeplugCommFileHeader_0.Level)
  52.         {
  53.             case InfoLevel.TypeLevelChangeDisc:
  54.                 return smethod_2(pbaObject_0);
  55.  
  56.             case InfoLevel.ItemLevelChangeDisc:
  57.                 return smethod_3(pbaObject_0);
  58.  
  59.             case InfoLevel.CodeplugData:
  60.                 return this.method_1(pbaObject_0);
  61.         }
  62.         return null;
  63.     }
  64.  
  65.     private byte[] method_1(PbaObject pbaObject_0)
  66.     {
  67.         IshItemCollection codeplugInIshItemCollection = pbaObject_0.GetCodeplugInIshItemCollection();
  68.         UtilityMethods.CheckIfParameterIsNull(codeplugInIshItemCollection, CommonErrorCode.ArgumentNull);
  69.         int num = 0;
  70.         foreach (KeyValuePair<IshHeader, IshItem> pair2 in codeplugInIshItemCollection)
  71.         {
  72.             if (pair2.Value.ItemSize > 0)
  73.             {
  74.                 num += 6 + pair2.Value.ItemSize;
  75.             }
  76.         }
  77.         byte[] buffer2 = new byte[num];
  78.         using (MemoryStream stream = new MemoryStream(buffer2))
  79.         {
  80.             byte[] array = null;
  81.             foreach (KeyValuePair<IshHeader, IshItem> pair in codeplugInIshItemCollection)
  82.             {
  83.                 if (pair.Value.ItemSize > 0)
  84.                 {
  85.                     IshHeader key = pair.Key;
  86.                     IshItem item = pair.Value;
  87.                     array = BitConverter.GetBytes(key.IshType);
  88.                     if (!BitConverter.IsLittleEndian)
  89.                     {
  90.                         Array.Reverse(array);
  91.                     }
  92.                     stream.Write(array, 0, 2);
  93.                     array = BitConverter.GetBytes(key.IshID);
  94.                     if (!BitConverter.IsLittleEndian)
  95.                     {
  96.                         Array.Reverse(array);
  97.                     }
  98.                     stream.Write(array, 0, 2);
  99.                     stream.WriteByte((byte) (item.ItemSize & 0xff));
  100.                     stream.WriteByte((byte) (item.ItemSize >> 8));
  101.                     if (!this.IsCrcBigEndian)
  102.                     {
  103.                         byte num2 = item.Data[item.Data.Length - 1];
  104.                         item.Data[item.Data.Length - 1] = item.Data[item.Data.Length - 2];
  105.                         item.Data[item.Data.Length - 2] = num2;
  106.                     }
  107.                     stream.Write(item.Data, 0, item.ItemSize);
  108.                 }
  109.             }
  110.         }
  111.         return buffer2;
  112.     }
  113.  
  114.     private PbaObject method_2(CodeplugCommFileHeader codeplugCommFileHeader_0, byte[] byte_0)
  115.     {
  116.         UtilityMethods.CheckIfParameterIsNull(byte_0, CommonErrorCode.ArgumentNull);
  117.         PbaObject obj2 = new PbaObject();
  118.         switch (codeplugCommFileHeader_0.Level)
  119.         {
  120.             case InfoLevel.TypeLevelChangeDisc:
  121.                 smethod_6(obj2, byte_0);
  122.                 return obj2;
  123.  
  124.             case InfoLevel.ItemLevelChangeDisc:
  125.                 smethod_7(obj2, byte_0);
  126.                 return obj2;
  127.  
  128.             case InfoLevel.CodeplugData:
  129.                 this.method_3(obj2, byte_0);
  130.                 return obj2;
  131.         }
  132.         return obj2;
  133.     }
  134.  
  135.     private void method_3(PbaObject pbaObject_0, byte[] byte_0)
  136.     {
  137.         IshItem item;
  138.         int length = byte_0.GetLength(0);
  139.         IshItemCollection ishItemCollection = new IshItemCollection();
  140.         for (int i = 3; i < (length - 4); i += item.ItemSize)
  141.         {
  142.             if ((((i + 2) + 2) + 2) >= (length - 4))
  143.             {
  144.                 throw new CommonException(CommonErrorCode.CommonFileInvalidCodeplugDataLength);
  145.             }
  146.             IshHeader headers = new IshHeader();
  147.             ushort num4 = byte_0[i];
  148.             i++;
  149.             ushort num5 = byte_0[i];
  150.             i++;
  151.             headers.IshType = (ushort) ((num5 << 8) | num4);
  152.             num4 = byte_0[i];
  153.             i++;
  154.             num5 = byte_0[i];
  155.             i++;
  156.             headers.IshID = (ushort) ((num5 << 8) | num4);
  157.             item = new IshItem();
  158.             num4 = byte_0[i];
  159.             i++;
  160.             num5 = byte_0[i];
  161.             i++;
  162.             item.ItemSize = (num5 << 8) | num4;
  163.             if ((i + item.ItemSize) > (length - 4))
  164.             {
  165.                 throw new CommonException(CommonErrorCode.CommonFileInvalidItemDataLength);
  166.             }
  167.             byte[] destinationArray = new byte[item.ItemSize];
  168.             Array.Copy(byte_0, i, destinationArray, 0, item.ItemSize);
  169.             item.Data = destinationArray;
  170.             if (!this.IsCrcBigEndian)
  171.             {
  172.                 byte num3 = item.Data[item.Data.Length - 1];
  173.                 item.Data[item.Data.Length - 1] = item.Data[item.Data.Length - 2];
  174.                 item.Data[item.Data.Length - 2] = num3;
  175.             }
  176.             ishItemCollection.Add(headers, item);
  177.         }
  178.         pbaObject_0.SetCodeplug(ishItemCollection);
  179.     }
  180.  
  181.     public byte[] Serialize(CodeplugCommFileHeader fileHeader, PbaObject pbaObject)
  182.     {
  183.         byte[] c = smethod_0(fileHeader);
  184.         byte[] buffer2 = this.method_0(fileHeader, pbaObject);
  185.         ArrayList list = new ArrayList();
  186.         list.AddRange(c);
  187.         list.AddRange(buffer2);
  188.         byte[] buffer3 = (byte[]) list.ToArray(typeof(byte));
  189.         byte[] buffer4 = smethod_1(buffer3);
  190.         ArrayList list2 = new ArrayList();
  191.         list2.AddRange(buffer3);
  192.         list2.AddRange(buffer4);
  193.         this.FileData = (byte[]) list2.ToArray(typeof(byte));
  194.         return this.FileData;
  195.     }
  196.  
  197.     public void Serialize(ComponentType compType, ComponentInstance compInst, CodeplugCommFileHeader fileHeader, PbaObject pbaObject)
  198.     {
  199.         byte[] c = smethod_0(fileHeader);
  200.         byte[] buffer2 = this.method_0(fileHeader, pbaObject);
  201.         ArrayList list = new ArrayList();
  202.         list.AddRange(c);
  203.         list.AddRange(buffer2);
  204.         byte[] buffer3 = (byte[]) list.ToArray(typeof(byte));
  205.         byte[] buffer4 = smethod_1(buffer3);
  206.         ArrayList list2 = new ArrayList();
  207.         list2.AddRange(buffer3);
  208.         list2.AddRange(buffer4);
  209.         this.FileData = (byte[]) list2.ToArray(typeof(byte));
  210.         FileStream stream = new FileStream(string.Format(CultureInfo.InvariantCulture, Path.GetTempPath() + "{0}_{1}.ish", new object[] { (byte) compType, (byte) compInst }), FileMode.Create, FileAccess.Write, FileShare.None);
  211.         stream.Write(this.FileData, 0, this.FileData.Length);
  212.         stream.Close();
  213.     }
  214.  
  215.     private static byte[] smethod_0(CodeplugCommFileHeader codeplugCommFileHeader_0)
  216.     {
  217.         return new byte[] { codeplugCommFileHeader_0.HeaderVer, codeplugCommFileHeader_0.Level, codeplugCommFileHeader_0.FullDelta };
  218.     }
  219.  
  220.     private static byte[] smethod_1(byte[] byte_0)
  221.     {
  222.         byte[] bytes = BitConverter.GetBytes(CheckSumCRC32.GetCRC32CheckSum(byte_0));
  223.         if (!BitConverter.IsLittleEndian)
  224.         {
  225.             Array.Reverse(bytes);
  226.         }
  227.         return bytes;
  228.     }
  229.  
  230.     private static byte[] smethod_2(PbaObject pbaObject_0)
  231.     {
  232.         UtilityMethods.CheckIfParameterIsNull(pbaObject_0.CodeplugData.TypeCounters, CommonErrorCode.ArgumentNull);
  233.         int num = pbaObject_0.CodeplugData.TypeCounters.Count * 6;
  234.         byte[] buffer = new byte[num];
  235.         using (MemoryStream stream = new MemoryStream(buffer))
  236.         {
  237.             byte[] array = null;
  238.             foreach (TypeLevelChangeDiscovery discovery in pbaObject_0.CodeplugData.TypeCounters)
  239.             {
  240.                 if (discovery != null)
  241.                 {
  242.                     array = BitConverter.GetBytes(discovery.IshType);
  243.                     if (!BitConverter.IsLittleEndian)
  244.                     {
  245.                         Array.Reverse(array);
  246.                     }
  247.                     stream.Write(array, 0, 2);
  248.                     array = BitConverter.GetBytes(discovery.Counter);
  249.                     if (!BitConverter.IsLittleEndian)
  250.                     {
  251.                         Array.Reverse(array);
  252.                     }
  253.                     stream.Write(array, 0, 4);
  254.                 }
  255.             }
  256.         }
  257.         return buffer;
  258.     }
  259.  
  260.     private static byte[] smethod_3(PbaObject pbaObject_0)
  261.     {
  262.         UtilityMethods.CheckIfParameterIsNull(pbaObject_0.CodeplugData.IDCounters, CommonErrorCode.ArgumentNull);
  263.         int num = pbaObject_0.CodeplugData.IDCounters.Count * 8;
  264.         byte[] buffer = new byte[num];
  265.         using (MemoryStream stream = new MemoryStream(buffer))
  266.         {
  267.             byte[] array = null;
  268.             foreach (ItemLevelChangeDiscovery discovery in pbaObject_0.CodeplugData.IDCounters)
  269.             {
  270.                 if (discovery != null)
  271.                 {
  272.                     array = BitConverter.GetBytes(discovery.IshType);
  273.                     if (!BitConverter.IsLittleEndian)
  274.                     {
  275.                         Array.Reverse(array);
  276.                     }
  277.                     stream.Write(array, 0, 2);
  278.                     array = BitConverter.GetBytes(discovery.IshID);
  279.                     if (!BitConverter.IsLittleEndian)
  280.                     {
  281.                         Array.Reverse(array);
  282.                     }
  283.                     stream.Write(array, 0, 2);
  284.                     array = BitConverter.GetBytes(discovery.Counter);
  285.                     if (!BitConverter.IsLittleEndian)
  286.                     {
  287.                         Array.Reverse(array);
  288.                     }
  289.                     stream.Write(array, 0, 4);
  290.                 }
  291.             }
  292.         }
  293.         return buffer;
  294.     }
  295.  
  296.     private static bool smethod_4(byte[] byte_0)
  297.     {
  298.         UtilityMethods.CheckIfParameterIsNull(byte_0, CommonErrorCode.ArgumentNull);
  299.         bool flag = true;
  300.         int length = byte_0.Length - 4;
  301.         if (0 >= length)
  302.         {
  303.             throw new CommonException(CommonErrorCode.CommonFileInvalidFileLength);
  304.         }
  305.         byte[] destinationArray = new byte[length];
  306.         Array.Copy(byte_0, 0, destinationArray, 0, length);
  307.         uint num2 = CheckSumCRC32.GetCRC32CheckSum(destinationArray);
  308.         uint num3 = byte_0[byte_0.Length - 4];
  309.         uint num4 = byte_0[(byte_0.Length - 4) + 1];
  310.         uint num5 = byte_0[(byte_0.Length - 4) + 2];
  311.         uint num6 = byte_0[(byte_0.Length - 4) + 3];
  312.         uint num7 = (((num6 << 0x18) | (num5 << 0x10)) | (num4 << 8)) | num3;
  313.         if (num2 != num7)
  314.         {
  315.             flag = false;
  316.         }
  317.         return flag;
  318.     }
  319.  
  320.     private static CodeplugCommFileHeader smethod_5(byte[] byte_0)
  321.     {
  322.         UtilityMethods.CheckIfParameterIsNull(byte_0, CommonErrorCode.ArgumentNull);
  323.         CodeplugCommFileHeader header = new CodeplugCommFileHeader();
  324.         int index = 0;
  325.         header.HeaderVer = byte_0[0];
  326.         if (0x10 != header.HeaderVer)
  327.         {
  328.             throw new CommonException(CommonErrorCode.CommonFileInvalidCommonFileVersion);
  329.         }
  330.         index++;
  331.         header.Level = (InfoLevel) byte_0[index];
  332.         index++;
  333.         header.FullDelta = (FullDeltaCodeplug) byte_0[index];
  334.         return header;
  335.     }
  336.  
  337.     private static void smethod_6(PbaObject pbaObject_0, byte[] byte_0)
  338.     {
  339.         int length = byte_0.GetLength(0);
  340.         if ((((length - 3) - 4) % 6) != 0)
  341.         {
  342.             throw new CommonException(CommonErrorCode.CommonFileInvalidTypeLevelDataLength);
  343.         }
  344.         List<TypeLevelChangeDiscovery> typeCounters = new List<TypeLevelChangeDiscovery>();
  345.         int index = 3;
  346.         while (index < (length - 4))
  347.         {
  348.             TypeLevelChangeDiscovery item = new TypeLevelChangeDiscovery();
  349.             ushort num3 = byte_0[index];
  350.             index++;
  351.             ushort num4 = byte_0[index];
  352.             item.IshType = (ushort) ((num4 << 8) | num3);
  353.             index++;
  354.             int num5 = byte_0[index];
  355.             index++;
  356.             int num6 = byte_0[index];
  357.             index++;
  358.             int num7 = byte_0[index];
  359.             index++;
  360.             int num8 = byte_0[index];
  361.             item.Counter = (uint) ((((num8 << 0x18) | (num7 << 0x10)) | (num6 << 8)) | num5);
  362.             index++;
  363.             typeCounters.Add(item);
  364.         }
  365.         pbaObject_0.CodeplugData.SetTypeCounters(typeCounters);
  366.     }
  367.  
  368.     private static void smethod_7(PbaObject pbaObject_0, byte[] byte_0)
  369.     {
  370.         int length = byte_0.GetLength(0);
  371.         if ((((length - 3) - 4) % 8) != 0)
  372.         {
  373.             throw new CommonException(CommonErrorCode.CommonFileInvalidItemLevelDataLength);
  374.         }
  375.         List<ItemLevelChangeDiscovery> idCounters = new List<ItemLevelChangeDiscovery>();
  376.         int index = 3;
  377.         while (index < (length - 4))
  378.         {
  379.             ItemLevelChangeDiscovery item = new ItemLevelChangeDiscovery();
  380.             ushort num3 = byte_0[index];
  381.             index++;
  382.             ushort num4 = byte_0[index];
  383.             item.IshType = (ushort) ((num4 << 8) | num3);
  384.             index++;
  385.             num3 = byte_0[index];
  386.             index++;
  387.             num4 = byte_0[index];
  388.             item.IshID = (ushort) ((num4 << 8) | num3);
  389.             index++;
  390.             int num5 = byte_0[index];
  391.             index++;
  392.             int num6 = byte_0[index];
  393.             index++;
  394.             int num7 = byte_0[index];
  395.             index++;
  396.             int num8 = byte_0[index];
  397.             item.Counter = (uint) ((((num8 << 0x18) | (num7 << 0x10)) | (num6 << 8)) | num5);
  398.             index++;
  399.             idCounters.Add(item);
  400.         }
  401.         pbaObject_0.CodeplugData.SetIDCounters(idCounters);
  402.     }
  403.  
  404.     // Properties
  405.     public byte[] FileData { get; set; }
  406. }
  407.  
  408.  
  409. Collapse Methods
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement