Advertisement
LuisMk

PacketCompression

Jan 17th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace PangyaAPI
  10. {
  11.     public static class PacketCompression
  12.     {
  13.         public static byte[] GeneratePacketId(byte key)
  14.         {
  15.             string keyCriptografia = (Convert.ToInt32(key) + 1).ToString();
  16.  
  17.             ProcessStartInfo pInfo = new ProcessStartInfo();
  18.             pInfo.CreateNoWindow = false;
  19.             pInfo.UseShellExecute = false;
  20.             pInfo.FileName = @"C:\PangyaCompress\test\Project2.exe";
  21.             pInfo.Arguments = $"0100E271D24D000000110000 {keyCriptografia}";
  22.             pInfo.WindowStyle = ProcessWindowStyle.Hidden;
  23.             pInfo.RedirectStandardOutput = true;
  24.             pInfo.RedirectStandardError = true;
  25.  
  26.             string output = String.Empty;
  27.  
  28.             using (Process p = Process.Start(pInfo))
  29.             {
  30.                 p.WaitForExit();
  31.  
  32.                 var exitCode = p.ExitCode;
  33.                 output = p.StandardOutput.ReadToEnd();
  34.                 var error = p.StandardError.ReadToEnd();
  35.  
  36.                 if (output.Length > 0)
  37.                 {
  38.                     // you have some output
  39.                 }
  40.  
  41.                 if (error.Length > 0)
  42.                 {
  43.                     // you have some error details
  44.                 }
  45.  
  46.             }
  47.  
  48.             return StringToByteArray(output.Remove(8, output.Length - 8));
  49.         }
  50.  
  51.  
  52.         public enum TipoOperacao
  53.         {
  54.             Compress = 1,
  55.             Decompress = 2
  56.         }
  57.  
  58.         //Decompress
  59.         public static byte[] Compress(byte[] packet, TipoOperacao operacao)
  60.         {
  61.             var packetStr = ConvertToString(packet);
  62.  
  63.             ProcessStartInfo pInfo = new ProcessStartInfo();
  64.             pInfo.CreateNoWindow = false;
  65.             pInfo.UseShellExecute = false;
  66.             pInfo.FileName = @"C:\PangyaCompress\compression\Project2.exe";
  67.             pInfo.Arguments = $"{(int)operacao} {packetStr}";
  68.             pInfo.WindowStyle = ProcessWindowStyle.Hidden;
  69.             pInfo.RedirectStandardOutput = true;
  70.             pInfo.RedirectStandardError = true;
  71.  
  72.             string output = String.Empty;
  73.  
  74.             using (Process p = Process.Start(pInfo))
  75.             {
  76.                 p.WaitForExit();
  77.  
  78.                 var exitCode = p.ExitCode;
  79.                 output = p.StandardOutput.ReadToEnd();
  80.                 var error = p.StandardError.ReadToEnd();
  81.  
  82.                 if (output.Length > 0)
  83.                 {
  84.                     // you have some output
  85.                 }
  86.  
  87.                 if (error.Length > 0)
  88.                 {
  89.                     // you have some error details
  90.                 }
  91.  
  92.             }
  93.  
  94.             if (operacao == TipoOperacao.Compress)
  95.             {
  96.                 //Remove os 4 primeiros bytes pois isto é tratado depois
  97.                 output = output.Remove(0, 8);
  98.  
  99.                 //Remove 11 00 00
  100.                 output = output.Remove(output.Length - 6, 6);
  101.             }
  102.  
  103.             //if (returnInHex)
  104.             //    return ConvertToCsharpByteHex(output);
  105.  
  106.             return StringToByteArray(output);
  107.         }
  108.  
  109.         //public static byte[] CompressToByte(string packet, byte key)
  110.         //{
  111.         //    return StringToByteArray(Compress(packet, key));
  112.         //}
  113.  
  114.         public static byte[] StringToByteArray(String hex)
  115.         {
  116.             int NumberChars = hex.Length;
  117.             byte[] bytes = new byte[NumberChars / 2];
  118.             for (int i = 0; i < NumberChars; i += 2)
  119.                 bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
  120.             return bytes;
  121.         }
  122.  
  123.         private static string ConvertToString(byte[] message)
  124.         {
  125.             return BitConverter.ToString(message).Replace("-", "");
  126.         }
  127.  
  128.         private static string ConvertToCsharpByteHex(string input)
  129.         {
  130.             StringBuilder sb = new StringBuilder();
  131.  
  132.             for (int i = 0; i < input.Length; i += 2)
  133.             {
  134.                 sb.Append("0x"+input.Substring(i, 2)+", ");
  135.             }
  136.  
  137.             return sb.ToString();
  138.         }
  139.  
  140.         public static string GetCsharpStringFromFile(string path)
  141.         {
  142.             string filePath = path;
  143.             return ConvertToCsharpByteHex(ConvertToString(System.IO.File.ReadAllBytes(filePath)));
  144.         }
  145.  
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement