Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Saviine Commands
- public const byte BYTE_NORMAL = 0xff;
- public const byte BYTE_SPECIAL = 0xfe;
- public const byte BYTE_OPEN = 0x00;
- public const byte BYTE_READ = 0x01;
- public const byte BYTE_CLOSE = 0x02;
- public const byte BYTE_OK = 0x03;
- public const byte BYTE_SETPOS = 0x04;
- public const byte BYTE_REQUEST = 0x08;
- public const byte BYTE_REQUEST_SLOW = 0x09;
- public const byte BYTE_HANDLE = 0x0A;
- public const byte BYTE_DUMP = 0x0B;
- public const byte BYTE_PING = 0x0C;
- public const byte BYTE_G_MODE = 0x0D;
- public const byte BYTE_MODE_D = 0x0E;
- public const byte BYTE_MODE_I = 0x0F;
- public const byte BYTE_CLOSE_DUMP = 0x10;
- public const byte BYTE_LOG_STR = 0xFB;
- public const byte BYTE_FILE = 0xC0;
- public const byte BYTE_FOLDER = 0xC1;
- public const byte BYTE_READ_DIR = 0xCC;
- public const byte BYTE_INJECTSTART = 0x40;
- public const byte BYTE_INJECTEND = 0x41;
- public const byte BYTE_DUMPSTART = 0x42;
- public const byte BYTE_DUMPEND = 0x43;
- public const byte BYTE_END = 0xfd;
- FileStream[] files = new FileStream[256];
- Dictionary<int, FileStream> files_request = new Dictionary<int, FileStream>();
- Dictionary<string, Dictionary<string, byte>> dir_files = new Dictionary<string, Dictionary<string, byte>>();
- public static int countDirectory(string targetDirectory)
- {
- int x = 0;
- // Process the list of files found in the directory.
- string[] fileEntries = Directory.GetFiles(targetDirectory);
- foreach (string fileName in fileEntries)
- x++;
- // Count subdirectories of this directory.
- string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
- foreach (string subdirectory in subdirectoryEntries)
- x++;
- return x;
- }
- void loop()
- {
- while (done == false)
- {
- byte cmd_byte = reader.ReadByte();
- switch (cmd_byte)
- {
- case BYTE_OPEN: //For Inject
- {
- Boolean failed = false;
- int len_path = reader.ReadInt32();
- int len_mode = reader.ReadInt32();
- string path = reader.ReadString(Encoding.ASCII, len_path - 1);
- if (reader.ReadByte() != 0)
- throw new InvalidDataException();
- string mode = reader.ReadString(Encoding.ASCII, len_mode - 1);
- if (reader.ReadByte() != 0)
- throw new InvalidDataException();
- path = path.Substring(path.LastIndexOf("/") + 1);
- path = "TPHD/Inject/" + path;
- if (path.Length == 0)
- failed = true;
- if (File.Exists(path) && !failed)
- {
- int handle = -1;
- for (int i = 1; i < files.Length; i++)
- {
- if (files[i] == null)
- {
- handle = i;
- break;
- }
- }
- if (handle == -1)
- {
- writer.Write(BYTE_SPECIAL);
- writer.Write(-19);
- writer.Write(0);
- break;
- }
- files[handle] = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
- writer.Write(BYTE_SPECIAL);
- writer.Write(0);
- writer.Write(handle);
- break;
- }
- //else on error:
- writer.Write(BYTE_NORMAL);
- break;
- }
- case BYTE_SETPOS:
- {
- int fd = reader.ReadInt32();
- int pos = reader.ReadInt32();
- if ((fd & 0x0fff00ff) == 0x0fff00ff)
- {
- int handle = (fd >> 8) & 0xff;
- if (files[handle] == null)
- {
- writer.Write(BYTE_SPECIAL);
- writer.Write(-38);
- break;
- }
- FileStream f = files[handle];
- f.Position = pos;
- writer.Write(BYTE_SPECIAL);
- writer.Write(0);
- }
- else
- {
- writer.Write(BYTE_NORMAL);
- }
- break;
- }
- case BYTE_INJECTEND:
- {
- //close all opened files
- for (int i = 1; i < files.Length; i++)
- {
- if (files[i] != null)
- {
- files[i].Close();
- files[i] = null;
- }
- }
- writer.Write(BYTE_OK);
- //INJECT IS FINISHED
- done = true;
- break;
- }
- case BYTE_DUMPEND:
- {
- //close all opened files
- for (int i = 1; i < files.Length; i++)
- {
- if (files[i] != null)
- {
- files[i].Close();
- files[i] = null;
- }
- }
- writer.Write(BYTE_OK);
- //DUMP IS FINISHED
- done = true;
- break;
- }
- case BYTE_READ_DIR: //For Inject
- {
- Boolean failed = false;
- int len_path = reader.ReadInt32();
- string path = reader.ReadString(Encoding.ASCII, len_path - 1);
- if (reader.ReadByte() != 0)
- throw new InvalidDataException();
- int x = 0;
- path = "TPHD/Inject";
- if (path.Length == 0)
- failed = true;
- if (Directory.Exists(path) && !failed)
- {
- x = countDirectory(path);
- if (x > 0)
- {
- Dictionary<string, byte> value;
- if (!dir_files.TryGetValue(path, out value))
- {
- value = new Dictionary<string, byte>();
- string[] fileEntries = Directory.GetFiles(path);
- foreach (string fn in fileEntries)
- {
- string fileName = Path.GetFileName(fn);
- value.Add(fileName, BYTE_FILE);
- }
- string[] subdirectoryEntries = Directory.GetDirectories(path);
- foreach (string sd in subdirectoryEntries)
- {
- string subdirectory = Path.GetFileName(sd);
- value.Add(subdirectory, BYTE_FOLDER);
- }
- dir_files.Add(path, value);
- }
- if (value.Count > 0)
- {
- writer.Write(BYTE_OK);
- foreach (var item in value)
- {
- //Write
- writer.Write(item.Value);
- writer.Write(item.Key.Length);
- writer.Write(item.Key, Encoding.ASCII, true);
- int length = 0;
- if (item.Value == BYTE_FILE)
- length = (int)new System.IO.FileInfo(path + "/" + item.Key).Length;
- writer.Write(length);
- value.Remove(item.Key);
- break;
- }
- writer.Write(BYTE_SPECIAL);
- break;
- }
- else
- {
- dir_files.Remove(path);
- }
- }
- }
- writer.Write(BYTE_END);
- break;
- }
- case BYTE_READ:
- {
- int size = reader.ReadInt32();
- int fd = reader.ReadInt32();
- FileStream f = files[fd];
- byte[] buffer = new byte[size];
- int sz = (int)f.Length;
- int rd = 0;
- writer.Write(BYTE_SPECIAL);
- rd = f.Read(buffer, 0, buffer.Length);
- writer.Write(rd);
- writer.Write(buffer, 0, rd);
- int offset = (int)f.Position;
- int progress = (int)(((float)offset / (float)sz) * 100);
- string strProgress = progress.ToString().PadLeft(3, ' ');
- string strSize = (sz / 1024).ToString();
- string strCurrent = (offset / 1024).ToString().PadLeft(strSize.Length, ' ');
- int ret = -5;
- if ((ret = reader.ReadByte()) != BYTE_OK)
- {
- //throw new InvalidDataException();
- }
- break;
- }
- case BYTE_HANDLE: //For Dump
- {
- // Read buffer params : fd, path length, path string
- int fd = reader.ReadInt32();
- int len_path = reader.ReadInt32();
- string path = reader.ReadString(Encoding.ASCII, len_path - 1);
- if (reader.ReadByte() != 0)
- throw new InvalidDataException();
- path = path.Substring(path.LastIndexOf("/") + 1);
- path = "TPHD/Dump/" + path;
- if (path.Length == 0)
- {
- writer.Write(BYTE_SPECIAL);
- break;
- }
- if (!Directory.Exists(path))
- {
- Directory.CreateDirectory(Path.GetDirectoryName(path));
- }
- // Add new file for incoming data
- files_request.Add(fd, new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write));
- // Send response
- writer.Write(BYTE_REQUEST_SLOW);
- // Send response
- writer.Write(BYTE_SPECIAL);
- break;
- }
- case BYTE_DUMP:
- {
- // Read buffer params : fd, size, file data
- int fd = reader.ReadInt32();
- int sz = reader.ReadInt32();
- byte[] buffer = new byte[sz];
- buffer = reader.ReadBytes(sz);
- // Look for file descriptor
- foreach (var item in files_request)
- {
- if (item.Key == fd)
- {
- FileStream dump_file = item.Value;
- if (dump_file == null)
- break;
- // Write to file
- dump_file.Write(buffer, 0, sz);
- break;
- }
- }
- // Send response
- writer.Write(BYTE_SPECIAL);
- break;
- }
- case BYTE_CLOSE:
- {
- //Log(log, "BYTE_CLOSE");
- int fd = reader.ReadInt32();
- if (files[fd] == null)
- {
- writer.Write(BYTE_SPECIAL);
- writer.Write(-38);
- break;
- }
- FileStream f = files[fd];
- writer.Write(BYTE_SPECIAL);
- writer.Write(0);
- f.Close();
- files[fd] = null;
- break;
- }
- case BYTE_CLOSE_DUMP:
- {
- int fd = reader.ReadInt32();
- if ((fd & 0x0fff00ff) != 0x0fff00ff)
- {
- // Check if it is a file to dump
- foreach (var item in files_request)
- {
- if (item.Key == fd)
- {
- FileStream dump_file = item.Value;
- if (dump_file == null)
- break;
- // Close file and remove from request list
- dump_file.Close();
- files_request.Remove(fd);
- break;
- }
- }
- // Send response
- writer.Write(BYTE_NORMAL);
- }
- break;
- }
- case BYTE_PING:
- {
- int val1 = reader.ReadInt32();
- int val2 = reader.ReadInt32();
- MessageBox.Show(name + " PING RECEIVED with values : " + val1.ToString() + " - " + val2.ToString());
- break;
- }
- default:
- throw new InvalidDataException();
- }
- }
- }
Add Comment
Please, Sign In to add comment