dragonbane

Saviine

Dec 22nd, 2018 (edited)
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.94 KB | None | 0 0
  1.    //Saviine Commands
  2.         public const byte BYTE_NORMAL = 0xff;
  3.         public const byte BYTE_SPECIAL = 0xfe;
  4.         public const byte BYTE_OPEN = 0x00;
  5.         public const byte BYTE_READ = 0x01;
  6.         public const byte BYTE_CLOSE = 0x02;
  7.         public const byte BYTE_OK = 0x03;
  8.         public const byte BYTE_SETPOS = 0x04;
  9.         public const byte BYTE_REQUEST = 0x08;
  10.         public const byte BYTE_REQUEST_SLOW = 0x09;
  11.         public const byte BYTE_HANDLE = 0x0A;
  12.         public const byte BYTE_DUMP = 0x0B;
  13.         public const byte BYTE_PING = 0x0C;
  14.         public const byte BYTE_G_MODE = 0x0D;
  15.         public const byte BYTE_MODE_D = 0x0E;
  16.         public const byte BYTE_MODE_I = 0x0F;
  17.         public const byte BYTE_CLOSE_DUMP = 0x10;
  18.         public const byte BYTE_LOG_STR = 0xFB;
  19.         public const byte BYTE_FILE = 0xC0;
  20.         public const byte BYTE_FOLDER = 0xC1;
  21.         public const byte BYTE_READ_DIR = 0xCC;
  22.         public const byte BYTE_INJECTSTART = 0x40;
  23.         public const byte BYTE_INJECTEND = 0x41;
  24.         public const byte BYTE_DUMPSTART = 0x42;
  25.         public const byte BYTE_DUMPEND = 0x43;
  26.         public const byte BYTE_END = 0xfd;
  27.  
  28.             FileStream[] files = new FileStream[256];
  29.             Dictionary<int, FileStream> files_request = new Dictionary<int, FileStream>();
  30.             Dictionary<string, Dictionary<string, byte>> dir_files = new Dictionary<string, Dictionary<string, byte>>();
  31.  
  32.  
  33.         public static int countDirectory(string targetDirectory)
  34.         {
  35.             int x = 0;
  36.             // Process the list of files found in the directory.
  37.             string[] fileEntries = Directory.GetFiles(targetDirectory);
  38.             foreach (string fileName in fileEntries)
  39.                 x++;
  40.  
  41.             // Count subdirectories of this directory.
  42.             string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
  43.             foreach (string subdirectory in subdirectoryEntries)
  44.                 x++;
  45.  
  46.             return x;
  47.         }
  48.     void loop()
  49.     {
  50.            while (done == false)
  51.             {
  52.                 byte cmd_byte = reader.ReadByte();
  53.                 switch (cmd_byte)
  54.                 {
  55.                     case BYTE_OPEN: //For Inject
  56.                         {
  57.                             Boolean failed = false;
  58.  
  59.                             int len_path = reader.ReadInt32();
  60.                             int len_mode = reader.ReadInt32();
  61.                             string path = reader.ReadString(Encoding.ASCII, len_path - 1);
  62.  
  63.                             if (reader.ReadByte() != 0)
  64.                                 throw new InvalidDataException();
  65.  
  66.                             string mode = reader.ReadString(Encoding.ASCII, len_mode - 1);
  67.  
  68.                             if (reader.ReadByte() != 0)
  69.                                 throw new InvalidDataException();
  70.  
  71.                             path = path.Substring(path.LastIndexOf("/") + 1);
  72.                             path = "TPHD/Inject/" + path;
  73.  
  74.                             if (path.Length == 0)
  75.                                 failed = true;
  76.  
  77.                             if (File.Exists(path) && !failed)
  78.                             {
  79.                                 int handle = -1;
  80.                                 for (int i = 1; i < files.Length; i++)
  81.                                 {
  82.                                     if (files[i] == null)
  83.                                     {
  84.                                         handle = i;
  85.                                         break;
  86.                                     }
  87.                                 }
  88.                                 if (handle == -1)
  89.                                 {
  90.                                     writer.Write(BYTE_SPECIAL);
  91.                                     writer.Write(-19);
  92.                                     writer.Write(0);
  93.                                     break;
  94.                                 }
  95.  
  96.                                 files[handle] = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
  97.  
  98.                                 writer.Write(BYTE_SPECIAL);
  99.                                 writer.Write(0);
  100.                                 writer.Write(handle);
  101.                                 break;
  102.                             }
  103.                             //else on error:
  104.                             writer.Write(BYTE_NORMAL);
  105.  
  106.                             break;
  107.                         }
  108.                     case BYTE_SETPOS:
  109.                         {
  110.                             int fd = reader.ReadInt32();
  111.                             int pos = reader.ReadInt32();
  112.                             if ((fd & 0x0fff00ff) == 0x0fff00ff)
  113.                             {
  114.                                 int handle = (fd >> 8) & 0xff;
  115.                                 if (files[handle] == null)
  116.                                 {
  117.                                     writer.Write(BYTE_SPECIAL);
  118.                                     writer.Write(-38);
  119.                                     break;
  120.                                 }
  121.                                 FileStream f = files[handle];
  122.                                 f.Position = pos;
  123.                                 writer.Write(BYTE_SPECIAL);
  124.                                 writer.Write(0);
  125.                             }
  126.                             else
  127.                             {
  128.                                 writer.Write(BYTE_NORMAL);
  129.                             }
  130.                             break;
  131.                         }
  132.  
  133.                     case BYTE_INJECTEND:
  134.                         {
  135.                             //close all opened files
  136.                             for (int i = 1; i < files.Length; i++)
  137.                             {
  138.                                 if (files[i] != null)
  139.                                 {
  140.                                     files[i].Close();
  141.                                     files[i] = null;
  142.                                 }
  143.                             }
  144.                             writer.Write(BYTE_OK);
  145.  
  146.                             //INJECT IS FINISHED      
  147.                             done = true;
  148.  
  149.                             break;
  150.                         }
  151.                     case BYTE_DUMPEND:
  152.                         {
  153.                             //close all opened files
  154.                             for (int i = 1; i < files.Length; i++)
  155.                             {
  156.                                 if (files[i] != null)
  157.                                 {
  158.                                     files[i].Close();
  159.                                     files[i] = null;
  160.                                 }
  161.                             }
  162.                             writer.Write(BYTE_OK);
  163.  
  164.                             //DUMP IS FINISHED
  165.                             done = true;
  166.  
  167.                             break;
  168.                         }
  169.                     case BYTE_READ_DIR: //For Inject
  170.                         {
  171.                             Boolean failed = false;
  172.  
  173.                             int len_path = reader.ReadInt32();
  174.                             string path = reader.ReadString(Encoding.ASCII, len_path - 1);
  175.  
  176.                             if (reader.ReadByte() != 0)
  177.                                 throw new InvalidDataException();
  178.  
  179.                             int x = 0;
  180.  
  181.                             path = "TPHD/Inject";
  182.  
  183.                             if (path.Length == 0)
  184.                                 failed = true;
  185.  
  186.                             if (Directory.Exists(path) && !failed)
  187.                             {
  188.                                 x = countDirectory(path);
  189.                                 if (x > 0)
  190.                                 {
  191.                                     Dictionary<string, byte> value;
  192.                                     if (!dir_files.TryGetValue(path, out value))
  193.                                     {
  194.                                         value = new Dictionary<string, byte>();
  195.                                         string[] fileEntries = Directory.GetFiles(path);
  196.  
  197.                                         foreach (string fn in fileEntries)
  198.                                         {
  199.                                             string fileName = Path.GetFileName(fn);
  200.                                             value.Add(fileName, BYTE_FILE);
  201.                                         }
  202.                                         string[] subdirectoryEntries = Directory.GetDirectories(path);
  203.                                         foreach (string sd in subdirectoryEntries)
  204.                                         {
  205.                                             string subdirectory = Path.GetFileName(sd);
  206.                                             value.Add(subdirectory, BYTE_FOLDER);
  207.                                         }
  208.                                         dir_files.Add(path, value);
  209.                                     }
  210.  
  211.                                     if (value.Count > 0)
  212.                                     {
  213.                                         writer.Write(BYTE_OK);
  214.                                         foreach (var item in value)
  215.                                         {
  216.                                             //Write
  217.                                             writer.Write(item.Value);
  218.                                             writer.Write(item.Key.Length);
  219.                                             writer.Write(item.Key, Encoding.ASCII, true);
  220.  
  221.                                             int length = 0;
  222.                                             if (item.Value == BYTE_FILE)
  223.                                                 length = (int)new System.IO.FileInfo(path + "/" + item.Key).Length;
  224.  
  225.                                             writer.Write(length);
  226.  
  227.                                             value.Remove(item.Key);
  228.  
  229.                                             break;
  230.                                         }
  231.                                         writer.Write(BYTE_SPECIAL);
  232.  
  233.                                         break;
  234.                                     }
  235.                                     else
  236.                                     {
  237.  
  238.                                         dir_files.Remove(path);
  239.  
  240.                                     }
  241.                                 }
  242.                             }
  243.                             writer.Write(BYTE_END);
  244.  
  245.                             break;
  246.                         }
  247.                     case BYTE_READ:
  248.                         {
  249.                             int size = reader.ReadInt32();
  250.                             int fd = reader.ReadInt32();
  251.  
  252.                             FileStream f = files[fd];
  253.  
  254.                             byte[] buffer = new byte[size];
  255.                             int sz = (int)f.Length;
  256.                             int rd = 0;
  257.  
  258.                             writer.Write(BYTE_SPECIAL);
  259.  
  260.                             rd = f.Read(buffer, 0, buffer.Length);
  261.  
  262.                             writer.Write(rd);
  263.                             writer.Write(buffer, 0, rd);
  264.  
  265.                             int offset = (int)f.Position;
  266.                             int progress = (int)(((float)offset / (float)sz) * 100);
  267.  
  268.                             string strProgress = progress.ToString().PadLeft(3, ' ');
  269.                             string strSize = (sz / 1024).ToString();
  270.                             string strCurrent = (offset / 1024).ToString().PadLeft(strSize.Length, ' ');
  271.  
  272.                             int ret = -5;
  273.                             if ((ret = reader.ReadByte()) != BYTE_OK)
  274.                             {
  275.                                 //throw new InvalidDataException();
  276.                             }
  277.  
  278.                             break;
  279.                         }
  280.                     case BYTE_HANDLE: //For Dump
  281.                         {
  282.                             // Read buffer params : fd, path length, path string
  283.                             int fd = reader.ReadInt32();
  284.                             int len_path = reader.ReadInt32();
  285.                             string path = reader.ReadString(Encoding.ASCII, len_path - 1);
  286.  
  287.                             if (reader.ReadByte() != 0)
  288.                                 throw new InvalidDataException();
  289.  
  290.                             path = path.Substring(path.LastIndexOf("/") + 1);
  291.  
  292.                             path = "TPHD/Dump/" + path;
  293.  
  294.                             if (path.Length == 0)
  295.                             {
  296.                                 writer.Write(BYTE_SPECIAL);
  297.                                 break;
  298.                             }
  299.  
  300.                             if (!Directory.Exists(path))
  301.                             {
  302.                                 Directory.CreateDirectory(Path.GetDirectoryName(path));
  303.                             }
  304.  
  305.                             // Add new file for incoming data
  306.                             files_request.Add(fd, new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write));
  307.  
  308.                             // Send response
  309.                             writer.Write(BYTE_REQUEST_SLOW);
  310.  
  311.                             // Send response
  312.                             writer.Write(BYTE_SPECIAL);
  313.                             break;
  314.                         }
  315.                     case BYTE_DUMP:
  316.                         {
  317.                             // Read buffer params : fd, size, file data
  318.                             int fd = reader.ReadInt32();
  319.                             int sz = reader.ReadInt32();
  320.                             byte[] buffer = new byte[sz];
  321.                             buffer = reader.ReadBytes(sz);
  322.  
  323.                             // Look for file descriptor
  324.                             foreach (var item in files_request)
  325.                             {
  326.                                 if (item.Key == fd)
  327.                                 {
  328.                                     FileStream dump_file = item.Value;
  329.                                     if (dump_file == null)
  330.                                         break;
  331.  
  332.                                     // Write to file
  333.                                     dump_file.Write(buffer, 0, sz);
  334.  
  335.                                     break;
  336.                                 }
  337.                             }
  338.                             // Send response
  339.                             writer.Write(BYTE_SPECIAL);
  340.                             break;
  341.                         }
  342.                     case BYTE_CLOSE:
  343.                         {
  344.                             //Log(log, "BYTE_CLOSE");
  345.                             int fd = reader.ReadInt32();
  346.  
  347.                             if (files[fd] == null)
  348.                             {
  349.                                 writer.Write(BYTE_SPECIAL);
  350.                                 writer.Write(-38);
  351.                                 break;
  352.                             }
  353.                             FileStream f = files[fd];
  354.  
  355.                             writer.Write(BYTE_SPECIAL);
  356.                             writer.Write(0);
  357.                             f.Close();
  358.                             files[fd] = null;
  359.  
  360.                             break;
  361.                         }
  362.                     case BYTE_CLOSE_DUMP:
  363.                         {
  364.                             int fd = reader.ReadInt32();
  365.                             if ((fd & 0x0fff00ff) != 0x0fff00ff)
  366.                             {
  367.                                 // Check if it is a file to dump
  368.                                 foreach (var item in files_request)
  369.                                 {
  370.                                     if (item.Key == fd)
  371.                                     {
  372.                                         FileStream dump_file = item.Value;
  373.                                         if (dump_file == null)
  374.                                             break;
  375.  
  376.                                         // Close file and remove from request list
  377.                                         dump_file.Close();
  378.                                         files_request.Remove(fd);
  379.                                         break;
  380.                                     }
  381.                                 }
  382.  
  383.                                 // Send response
  384.                                 writer.Write(BYTE_NORMAL);
  385.                             }
  386.                             break;
  387.                         }
  388.                     case BYTE_PING:
  389.                         {
  390.                             int val1 = reader.ReadInt32();
  391.                             int val2 = reader.ReadInt32();
  392.  
  393.                             MessageBox.Show(name + " PING RECEIVED with values : " + val1.ToString() + " - " + val2.ToString());
  394.                             break;
  395.                         }
  396.                     default:
  397.                         throw new InvalidDataException();
  398.                 }
  399.             }
  400. }
Add Comment
Please, Sign In to add comment