public void TestHugeDocument() { StringBuilder document = new StringBuilder(); // 5000 a's char[] largeWord; largeWord = Enumerable.Repeat('a', 5000).ToArray(); document.Append(largeWord); // a space document.Append(' '); // 2000 b's char[] largeWord2; largeWord2 = Enumerable.Repeat('b', 2000).ToArray(); document.Append(largeWord2); // Split on whitespace patterns, do not lowercase, no stopwords PatternAnalyzer a = new PatternAnalyzer(Version.LUCENE_CURRENT, PatternAnalyzer.WHITESPACE_PATTERN, false, null); Check(a, document.ToString(), new String[] { new String(largeWord), new String(largeWord2) }); }
private void CalcRating() { string text = null, output; isCalculating = true; try { #if DEBUG var sw = new Stopwatch(); sw.Start(); #endif Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate { text = PathText.Text; })); var map = new BeatmapInfo(text); var jack = new PatternAnalyzer(map.Notes, map.LNs, map.Data.Keys, map.Data.Bpms, map.Data.SpecialStyle); var specialStyle = map.Data.SpecialStyle || (map.Data.Keys == 8 && PatternAnalyzer.IsSpecialStyle(map.Notes, map.LNs)); var maxBpm = Math.Round(map.Data.Bpms.Select(cur => cur.Item1).Max(), 2); var minBpm = Math.Round(map.Data.Bpms.Select(cur => cur.Item1).Min(), 2); output = map.Data.Artist + " - " + map.Data.Title + " [" + map.Data.Diff + "]\nMade by " + map.Data.Creator + "\nBPM: " + (Math.Abs(maxBpm - minBpm) < 0.001 ? $"{maxBpm}" : $"{minBpm} - {maxBpm}\t") + "\tOD: " + map.Data.Od + "\tHP: " + map.Data.Hp + "\tKeys: " + (map.Data.Keys == 8 || specialStyle ? Convert.ToString(map.Data.Keys - 1) + "+1" : Convert.ToString(map.Data.Keys)) #if DEBUG + "\nJack Score: " + Math.Round(RatingCalculator.CalcJackScore(jack), 2) + " " + "\tVibro Ratio: " + Math.Round(jack.GetVibroRatio() * 100, 2) + "%" + "\tSpam Ratio: " + Math.Round(jack.GetSpamRatio() * 100, 2) + "%" + "\nDensity Score: " + Math.Round(map.JenksDensity, 2) + "\tSpeed Score: " + Math.Round(map.JenksSpeed, 2) #endif + "\nRating: " + Math.Round(RatingCalculator.CalcRating(map, jack), 2); #if DEBUG sw.Stop(); output += "\nElapsed Time: " + sw.ElapsedMilliseconds; #endif } catch (Exception ex) { output = ex.Message; } Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate { StateBlock.Text = output; })); isCalculating = false; }
public void Two_UpBars_Should_be_Contintuation() { PatternAnalyzer ptal = new PatternAnalyzer(); Bar bar1 = new Bar(5, 10, 0, 7, 0, DateTime.Now, DateTime.Now); ptal.AddBar(bar1); Bar bar2 = new Bar(6, 11, 1, 8, 0, DateTime.Now, DateTime.Now, bar1); ptal.AddBar(bar2); Assert.AreEqual(PatternState.Continuation, ptal.LastPattern.State); }
private void CalcRating() { string text = null, output; isCalculating = true; try { var sw = new Stopwatch(); sw.Start(); Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate { text = PathText.Text; })); var map = new BeatmapInfo(text); var jack = new PatternAnalyzer(map.Notes, map.LNs, map.Data.Keys, map.Data.SpecialStyle); var specialStyle = map.Data.SpecialStyle || (map.Data.Keys == 8 && (double)(jack.Notes[0].Count + jack.LNs[0].Count) / jack.Count < 0.06); output = map.Data.Artist + " - " + map.Data.Title + " [" + map.Data.Diff + "]\nMade by " + map.Data.Creator + "\nBPM: " + (Math.Abs(map.Data.MaxBpm - map.Data.MinBpm) < 0.001 ? Convert.ToString(map.Data.MaxBpm, CultureInfo.CurrentCulture) : map.Data.MinBpm + " - " + map.Data.MaxBpm + "\t") + "\tOD: " + map.Data.Od + "\tHP: " + map.Data.Hp + "\tKeys: " + (specialStyle ? Convert.ToString(map.Data.Keys - 1) + "+1" : Convert.ToString(map.Data.Keys)) + "\nMax Density: " + Math.Round(map.MaxDen, 2) + "\tAverage Density: " + Math.Round(map.AvgDen, 2) + "\tJack Ratio: " + Math.Round(jack.GetJackRatio() * 100, 2) + "%" #if DEBUG + "\nSpam Ratio: " + Math.Round(jack.GetSpamRatio() * 100, 2) + "%" + "\nCorrected Max Density: " + Math.Round(map.CorMaxDen, 2) + "\tCorrected Average Density: " + Math.Round(map.CorAvgDen, 2) #endif + "\nRating: " + Math.Round(RatingCalculator.CalcRating(map), 2); sw.Stop(); #if DEBUG output += "\nElapsed Time: " + sw.ElapsedMilliseconds; #endif } catch (Exception ex) { output = ex.Message; } Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate { StateBlock.Text = output; })); isCalculating = false; }
public void RoundtripsRegexFlags(PatternAnalyzer expected) { using MemoryStream stream = new MemoryStream(); using (Utf8JsonWriter writer = new Utf8JsonWriter(stream)) { ((IUtf8JsonSerializable)expected).Write(writer); } using JsonDocument doc = JsonDocument.Parse(stream.ToArray()); PatternAnalyzer actual = Analyzer.DeserializeAnalyzer(doc.RootElement) as PatternAnalyzer; CollectionAssert.AreEqual(expected.Flags, actual?.Flags); }
public void run(SignatureController patterns) { t = new Thread(() => { Result = PatternAnalyzer.AnalyzeByteArray(this.Buffer, patterns); this.Buffer = null; Complete = true; }); t.IsBackground = true; t.SetApartmentState(ApartmentState.MTA); t.Start(); }
public void Two_UpBars_One_Violating_DownBar_Should_be_DownPattern() { PatternAnalyzer ptal = new PatternAnalyzer(); Bar bar1 = new Bar(5, 10, 0, 7, 0, DateTime.Now, DateTime.Now); ptal.AddBar(bar1); Bar bar2 = new Bar(10, 15, 5, 12, 0, DateTime.Now, DateTime.Now, bar1); ptal.AddBar(bar2); Bar bar3 = new Bar(4, 9, -1, 6, 0, DateTime.Now, DateTime.Now, bar2); ptal.AddBar(bar3); Assert.AreEqual(PatternDirection.Down, ptal.LastPattern.Direction); }
public void Two_UpBars_With_One_NonViolating_DownBar_Should_be_PullBack() { PatternAnalyzer ptal = new PatternAnalyzer(); Bar bar1 = new Bar(5, 10, 0, 7, 0, DateTime.Now, DateTime.Now); ptal.AddBar(bar1); Bar bar2 = new Bar(10, 15, 5, 12, 0, DateTime.Now, DateTime.Now, bar1); ptal.AddBar(bar2); Bar bar3 = new Bar(9, 12, 2, 7, 0, DateTime.Now, DateTime.Now, bar2); ptal.AddBar(bar3); Assert.AreEqual(PatternState.PullBack, ptal.LastPattern.State); }
public static Signature ScanProcess(System.Diagnostics.Process process, ref SignatureController Signatures) { try { if (process.ProcessName == "runnable") { //System.IO.File.WriteAllBytes("dump.txt", buffer); Console.Write(""); } // Get the start address of the main module int regionStart = process.MainModule.EntryPointAddress.ToInt32(); int regionEnd = 0; // Could add multiple module support here ... // for now just get end address regionEnd = process.MainModule.ModuleMemorySize; // Create variables to read the block byte[] buffer = new byte[regionEnd]; IntPtr zero = IntPtr.Zero; // Try to read the section ReadProcessMemory(process.Handle, (IntPtr)regionStart, buffer, (UInt32)buffer.Length, out zero); //if (zero != IntPtr.Zero) { // Run a scan pass of the buffer var result = PatternAnalyzer.AnalyzeByteArray(buffer, Signatures); // Check for results if (result != null) { return(result); } } // nothing found return(null); } catch (Exception ex) { return(null); } //throw new NotImplementedException(); }
/* * Verify the analyzer analyzes to the expected contents. For PatternAnalyzer, * several methods are verified: * <ul> * <li>Analysis with a normal Reader * <li>Analysis with a FastStringReader * <li>Analysis with a String * </ul> */ private void Check(PatternAnalyzer analyzer, String document, String[] expected) { // ordinary analysis of a Reader AssertAnalyzesTo(analyzer, document, expected); // analysis with a "FastStringReader" TokenStream ts = analyzer.TokenStream("dummy", new PatternAnalyzer.FastStringReader(document)); AssertTokenStreamContents(ts, expected); // analysis of a String, uses PatternAnalyzer.tokenStream(String, String) TokenStream ts2 = analyzer.TokenStream("dummy", document); AssertTokenStreamContents(ts2, expected); }
public void TestNonWordPattern() { // Split on non-letter pattern, do not lowercase, no stopwords PatternAnalyzer a = new PatternAnalyzer(Version.LUCENE_CURRENT, PatternAnalyzer.NON_WORD_PATTERN, false, null); Check(a, "The quick brown Fox,the abcd1234 (56.78) dc.", new String[] { "The", "quick", "brown", "Fox", "the", "abcd", "dc" }); // split on non-letter pattern, lowercase, english stopwords PatternAnalyzer b = new PatternAnalyzer(Version.LUCENE_CURRENT, PatternAnalyzer.NON_WORD_PATTERN, true, StopAnalyzer.ENGLISH_STOP_WORDS_SET); Check(b, "The quick brown Fox,the abcd1234 (56.78) dc.", new String[] { "quick", "brown", "fox", "abcd", "dc" }); }
public void TestCustomPattern() { // Split on comma, do not lowercase, no stopwords PatternAnalyzer a = new PatternAnalyzer(Version.LUCENE_CURRENT, new Regex(",", RegexOptions.Compiled), false, null); Check(a, "Here,Are,some,Comma,separated,words,", new String[] { "Here", "Are", "some", "Comma", "separated", "words" }); // split on comma, lowercase, english stopwords PatternAnalyzer b = new PatternAnalyzer(Version.LUCENE_CURRENT, new Regex(",", RegexOptions.Compiled), true, StopAnalyzer.ENGLISH_STOP_WORDS_SET); Check(b, "Here,Are,some,Comma,separated,words,", new String[] { "here", "some", "comma", "separated", "words" }); }
public void TestWhitespacePattern() { // Split on whitespace patterns, do not lowercase, no stopwords PatternAnalyzer a = new PatternAnalyzer(Version.LUCENE_CURRENT, PatternAnalyzer.WHITESPACE_PATTERN, false, null); Check(a, "The quick brown Fox,the abcd1234 (56.78) dc.", new String[] { "The", "quick", "brown", "Fox,the", "abcd1234", "(56.78)", "dc." }); // Split on whitespace patterns, lowercase, english stopwords PatternAnalyzer b = new PatternAnalyzer(Version.LUCENE_CURRENT, PatternAnalyzer.WHITESPACE_PATTERN, true, StopAnalyzer.ENGLISH_STOP_WORDS_SET); Check(b, "The quick brown Fox,the abcd1234 (56.78) dc.", new String[] { "quick", "brown", "fox,the", "abcd1234", "(56.78)", "dc." }); }
/// <summary> /// Non referenced file scan /// </summary> /// <param name="filePath">File to scan.</param> /// <param name="Signatures">Signatures to scan with</param> /// <returns>Null or signature matching or found in file. </returns> //public static ScanResultArgs ScanFile(string filePath, SignatureController Signatures) //{ // return ScanFile(filePath, ref Signatures); //} /// <summary> /// Performs a scan on the provided file /// </summary> /// <param name="filePath">The file to scan.</param> /// <param name="Signatures">The signature controller</param> /// <param name="cached">The cache controller</param> /// <returns>Null or a signature</returns> public static ScanResultArgs ScanFile(string filePath, SignatureController Signatures) { //return ScanFileV2(filePath, Signatures); try { // Final args to return var args = new ScanResultArgs(); // *** NEW *** // Create a new filestream to read the file with FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); // Before performing any kind of scan, we need to make sure it's a executable file if (!isScannable(filePath)) { // If it's not, return null and theres no point of scanning it //return null; return(new ScanResultArgs() { Detection = null, Size = fs.Length }); } // Determine if the file is too large to scan based on settings if (fs.Length > Settings.MaxFileSize) { // If so, just return null... return(new ScanResultArgs() { Detection = null, Size = fs.Length }); } // *********** // Some local variables to store scan information in //byte[] hash = MD5.quickMD5(filePath); // If a cache database is supplied with the scan request //if (cached != null) //{ // // Try to obtain a md5 hash from the file (see exception below for possible issues) // //hash = MD5.FromFile(ref fs); // hash = MD5.quickMD5(filePath); // // Check the file cache to see if the file has already been scanned // var found = cached.GetById(hash); // // If a cached item was found in the db // if(found != null) // { // // Return the matching signature if any... // //return Signatures.GetById(found.SignatureId); // return new ScanResultArgs() { Detection = Signatures.GetById(found.SignatureId), Size = fs.Length, Cached = found }; // } //} // Either the file cache database is not being used or no entry was found // So we must perform a new scan on the file byte[] buffer = new byte[1024 * Settings.BufferSize]; // Scan in 32kb increments int read = -1; // count how many bytes have been read on each read here // Make sure our buffer isn't bigger than the file if (buffer.Length > fs.Length) { // If it is, resize the buffer accordinly buffer = new byte[fs.Length]; } // While there is data to read in the file stream while (read != 0) { // Attempt to read the buffered amount.. read = fs.Read(buffer, 0, buffer.Length); // If the buffered amount if greater than the amount read... // Lets shrink buffer to speed up the pattern search if (read < buffer.Length) { Array.Resize(ref buffer, read); } // Analyze the buffer with the pattern analyzer var result = PatternAnalyzer.AnalyzeByteArray(buffer, Signatures); // try version 2.... //var result = PatternAnalyzer.AnyalyzeByteArrayv2(buffer, Signatures); // If the result is not null... a detection was found if (result != null) { // Create args before closing args = new ScanResultArgs() { Detection = result, Size = fs.Length, }; // Detected upx packing... if (args.Detection.Definition == "PACKED") { // UPX ISNT WORKING YET //return new ScanResultArgs() { Detection = null, Size = 0 }; // unpack the file and store the unpacked path... string unpacked = API.UPXHelper.UnpackFile(filePath); // Perform another scan args = ScanFile(unpacked, Signatures); // this was an unpacked program... //if(args.Detection != null) //{ // //args.Detection.Definition = args.Detection.Definition + "/UPX"; //} // delete the unpacked file File.DeleteFile(unpacked); // Remove the unpacked file from white lsit Settings.WhiteList.Remove(unpacked); } // We already detected the threat so we do not need to read any more.. fs.Dispose(); // return the threat return(args); } } // We finished reading the file and no threat was detected // Time to clean and and may be log to cache // Create clean args args = new ScanResultArgs() { Detection = null, Size = fs.Length, }; // Close up the file stream fs.Close(); fs.Dispose(); // clear buffer buffer = null; // Return a clean scan return(args); } catch (Exception ex) // for debugging purposes { #if DEBUG Console.WriteLine(ex.Message); #endif // Throw an exception with info about what may have caused the error. throw new Exception(String.Format("[2] Unable to scan file at location {0}. File may be use or additional rights may be required.", filePath)); //return new ScanResultArgs() { Detection = null, Size = 0 }; } }