private void _AddTextToFileBox(LevenshteinMatches matches) { List <Run> runs = new List <Run>(); foreach (LevenshteinMatch match in matches.foundMatches) { string[] parts = new string[] { match.contextL, match.value, match.contextR }; int i = 0; foreach (string s in parts) { i = (i + 1) % 2; if (i == 0) { runs.Add(new Run(s) { Foreground = new SolidColorBrush(Color.FromRgb( (byte)(match.length - match.distance).Map(0, match.length, 100, 255), (byte)(match.length - match.distance).Map(0, match.length, 100, 255), 0)) }); } else { runs.Add(new Run(s)); } } runs.Add(new Run("「" + match.distance + "」\n")); foreach (var item in runs) { FilesTextBlock.Inlines.Add(item); } } FilesTextBlock.Inlines.Add(new Run("\n")); }
public LevenshteinMatches ExtractPhrase(string sourceText) { string phrase = MainWindow.SearchPhrase; LevenshteinMatches matches = sourceText.Levenshtein(phrase, mode: window.scanerMode); return(matches); }
public static LevenshteinMatches LevenshteinMultiMatrixParallel(this string str, string pattern, int maxDistance = -1, bool onlyBestResults = false, bool caseSensitive = false) { if (maxDistance < 0) { maxDistance = pattern.Length / 2; } //check if system will run out of memory, if so split string/ .NET 2 GB object size limit var a = pattern.Length * pattern.Length * str.Length; if (a > 15000000) { int mid = str.Length / 2; string leftPart = str.Substring(0, mid + pattern.Length - 1); string rightPart = str.Substring(mid); return(new LevenshteinMatches(leftPart.LevenshteinMultiMatrixParallel(pattern, maxDistance, onlyBestResults, caseSensitive), rightPart.LevenshteinMultiMatrixParallel(pattern, maxDistance, onlyBestResults, caseSensitive))); } int exprLen = pattern.Length; int strLen = str.Length; int numOfMatrixes = strLen - exprLen + 1; int[] results = new int[strLen]; int[][,] dimension = new int[strLen][, ]; for (int i = 0; i < numOfMatrixes; i++) { dimension[i] = new int[exprLen + 1, exprLen + 1]; } Parallel.For(0, numOfMatrixes, i => { results[i] = RectangleLevenshteinCPU(dimension[i], str.Substring(i, exprLen), pattern, caseSensitive); }); LevenshteinMatches matches = new LevenshteinMatches(); object bestDistance = int.MaxValue; Parallel.For(0, numOfMatrixes, i => { if (results[i] <= maxDistance) { lock (matches) { //lock (bestDistance) bestDistance = (int)bestDistance < results[i] ? bestDistance : results[i]; matches.addMatch(str, i, exprLen, results[i]); } } }); if (onlyBestResults) { matches.removeMatches((int)bestDistance); } return(matches); }
public LevenshteinMatches LevenshteinDualRowCPUTest( string str, string expression, int maxDistance, bool onlyBestResults, bool caseSensitive ) { LevenshteinMatches result = StringExtension.LevenshteinDualRowCPU (str, expression, maxDistance, onlyBestResults, caseSensitive); return(result); // TODO: add assertions to method StringExtensionTest.LevenshteinDualRowCPUTest(String, String, Int32, Boolean, Boolean) }
public void AddTextToFileBox(LevenshteinMatches matches) { if (matches.hasMatches) { if (!FilesTextBlock.Dispatcher.CheckAccess()) { FilesTextBlock.Dispatcher.Invoke(() => { _AddTextToFileBox(matches); }); } else { _AddTextToFileBox(matches); } } }
public void AddTextToNetworkBox(LevenshteinMatches matches) { if (matches.hasMatches) { if (!NetworkTextBlock.Dispatcher.CheckAccess()) { NetworkTextBlock.Dispatcher.Invoke(() => { _AddTextToNetworkBox(matches); }); } else { _AddTextToNetworkBox(matches); } } }
public void SearchFiles(List <string> files, DriveInfo drive) { foreach (string file in files) { //Console.Out.WriteLine(File.ReadAllText(file)); try { Console.WriteLine("Skanowanie " + file); LevenshteinMatches matches = SearchPhraseInFile(file); Console.WriteLine("Przeskanowano " + file); //foreach(string str in File.ReadLines(file)) if (matches.hasMatches) { window.AddTextToFileBox(file); window.AddTextToFileBox("\n"); window.AddTextToFileBox(matches); } } catch (UnauthorizedAccessException) { window.AddTextToFileBox("Cannot access:" + file); } catch (IOException) { //device detached } if (Settings.Default.ScanADS && drive.DriveFormat.Equals("NTFS")) { //search for ads //string fileName = Path.GetFileName(file); FileInfo fileInfo = new FileInfo(file); foreach (AlternateDataStreamInfo stream in fileInfo.ListAlternateDataStreams()) { string streamName = stream.Name; AlternateDataStreamInfo s = fileInfo.GetAlternateDataStream(stream.Name, FileMode.Open); LevenshteinMatches matches = null; using (StreamReader reader = s.OpenText()) { matches = ExtractPhrase(reader.ReadToEnd()); } if (matches.hasMatches) { window.AddTextToFileBox(file + ":" + streamName); window.AddTextToFileBox("\n"); window.AddTextToFileBox(matches); } } } } }
public void AddTextToClipBoardBox(LevenshteinMatches matches) { if (matches.hasMatches) { if (!ClipboardTextBlock.Dispatcher.CheckAccess()) { try { ClipboardTextBlock.Dispatcher.Invoke(() => { _AddTextToClipBoardBox(matches); }); } catch { } } else { _AddTextToClipBoardBox(matches); } } }
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { if (msg == 0x0308) { //wyciąganie danych z obrazów i dźwięków IDataObject iData = new DataObject(); try { iData = Clipboard.GetDataObject(); } catch (ExternalException externEx) { Console.Out.WriteLine("InteropServices.ExternalException: {0}", externEx.Message); return(IntPtr.Zero);; } catch (Exception) { return(IntPtr.Zero);; } if (iData.GetDataPresent(DataFormats.Text) || iData.GetDataPresent(DataFormats.Rtf) || iData.GetDataPresent(DataFormats.UnicodeText)) { try { string _data = (string)iData.GetData(DataFormats.Text); Console.Out.WriteLine((string)iData.GetData(DataFormats.Text)); string phrase = SearchPhrase; //way to change levenshtein methode LevenshteinMatches results = _data.Levenshtein(phrase, mode: keyloggerMode); if (results != null && results.hasMatches) { AddTextToClipBoardBox(results); } } catch (System.Runtime.InteropServices.COMException) { } } else { Console.Out.WriteLine("(cannot display this format)"); } } return(IntPtr.Zero); }
public static LevenshteinMatches LevenshteinMultiMatrixSingleThread(this string str, string pattern, int maxDistance = -1, bool onlyBestResults = false, bool caseSensitive = false) { if (maxDistance < 0) { maxDistance = pattern.Length / 2; } int exprLen = pattern.Length; long strLen = str.Length - exprLen + 1; int[] results = new int[strLen]; int[,] dimension = new int[exprLen + 1, exprLen + 1]; for (int i = 0; i < strLen; i++) { results[i] = SqueareLevenshteinCPU(dimension, str.Substring(i, exprLen), pattern, caseSensitive); } LevenshteinMatches matches = new LevenshteinMatches(); int bestDistance = int.MaxValue; for (int i = 0; i < strLen; i++) { if (results[i] <= maxDistance) { bestDistance = bestDistance < results[i] ? bestDistance : results[i]; matches.addMatch(str, i, exprLen, results[i]); } } if (onlyBestResults) { matches.removeMatches(bestDistance); } return(matches); }
private void BackgroundThread() { while (!BackgroundThreadStop) { bool shouldSleep = true; lock (QueueLock) { if (PacketQueue.Count != 0) { shouldSleep = false; } } if (shouldSleep) { Thread.Sleep(250); } else // should process the queue { List <RawCapture> queue; lock (QueueLock) { // swap queues, giving the capture callback a new one queue = PacketQueue; PacketQueue = new List <RawCapture>(); } Console.WriteLine("BackgroundThread: Queue.Count is {0}", queue.Count); foreach (var packet in queue) { var _packet = PacketDotNet.Packet.ParsePacket(packet.LinkLayerType, packet.Data); if (_packet is PacketDotNet.EthernetPacket) { var ip = (PacketDotNet.IpPacket)_packet.Extract(typeof(PacketDotNet.IpPacket)); if (ip != null) { bool filterOut = true; InterfaceClass tmpInterface = null; foreach (InterfaceClass iFace in UsedInterfaces) { if (iFace.Addres.Equals(ip.DestinationAddress.ToString())) { filterOut = false; tmpInterface = iFace; break; } } if (filterOut == false) { filterOut = true; var tcp = (PacketDotNet.TcpPacket)_packet.Extract(typeof(PacketDotNet.TcpPacket)); if (tcp != null) { if (tmpInterface.isPortValid(tcp.DestinationPort)) { string phrase = MainWindow.SearchPhrase; LevenshteinMatches matches = Encoding.UTF8.GetString(tcp.PayloadData).Levenshtein(phrase, mode: window.snifferMode); if (matches.hasMatches) { window.AddTextToNetworkBox(tmpInterface.Addres + ":" + tcp.DestinationPort + "\n"); window.AddTextToNetworkBox(matches); } } } var udp = (PacketDotNet.UdpPacket)_packet.Extract(typeof(PacketDotNet.UdpPacket)); if (udp != null) { if (tmpInterface.isPortValid(udp.DestinationPort)) { string phrase = MainWindow.SearchPhrase; LevenshteinMatches matches = Encoding.UTF8.GetString(udp.PayloadData).Levenshtein(phrase, mode: window.snifferMode); if (matches.hasMatches) { window.AddTextToNetworkBox(tmpInterface.Addres + ":" + udp.DestinationPort + "\n"); window.AddTextToNetworkBox(matches); } } } } } } } } } }
private static void KeyCapturedHandle(string s) { if (s.Substring(0, 1).Equals("[") && s.Substring(s.Length - 1, 1).Equals("]")) { } else { if (s.Substring(0, 1).Equals("<") && s.Substring(s.Length - 1, 1).Equals(">")) //special characters { s = s.Substring(1, s.Length - 2); if (s.Equals("Backspace")) { timerResetString.Stop(); if (keyBuffer.Length > 0) { keyBuffer = keyBuffer.Remove(keyBuffer.Length - 1); cursorPosition--; } } else if (s.Equals("Left")) { timerResetString.Stop(); if (cursorPosition > 0) { cursorPosition--; } } else if (s.Equals("Right")) { timerResetString.Stop(); if (cursorPosition < keyBuffer.Length) { cursorPosition++; } } } else //normal characters { timerResetString.Stop(); keyBuffer += s; cursorPosition++; if (s.Equals("\n")) //user returned(ended) string { cursorPosition = 0; keyBuffer = ""; } } if (keyBuffer.Length > 50) { keyBuffer = keyBuffer.Remove(0, 1); } Console.Out.WriteLine(keyBuffer); string phrase = MainWindow.SearchPhrase; LevenshteinMatches result = keyBuffer.Levenshtein(phrase, mode: window.keyloggerMode); if (result != null && result.hasMatches) { window.AddTextToClipBoardBox(result); } timerResetString.Start(); } }