/// <summary> /// Extracts a byte pattern and mask from a PEiD style string signature. /// </summary> /// <param name="signature">PEiD style string signature.</param> /// <returns>Byte pattern and mask as tuple.</returns> /// <exception cref="ArgumentOutOfRangeException"></exception> public static unsafe (byte[] aob, string mask) GetPatternAndMaskFromSignature(string signature) { // remove whitespace and split every 2 chars so we can support sigs that dont even have whitespace in the first place or are not formatted with whitespace after every byte/wildcard // 0F ?? AE ?? CC |standard // 0F??AE ?? CC |works now also signature = StringR.RemoveWhitespace(signature); if (signature.Length % 2 != 0) { throw new ArgumentOutOfRangeException(nameof(signature), "signature length(excluding whitespace) must be divisible by 2, make sure to prepend bytes with 0 if necessary and make wildcards full ?? instead of single ?"); } // split string in pairs of 2 string[] split = StringR.SplitN(signature, 2); byte[] bytes = new byte[split.Length]; char * mask = stackalloc char[split.Length]; for (int i = 0; i < split.Length; i++) { // check if a wildcard is in string pair because of halfbyte masking 0xFF | 0x?F | 0xF? | 0x?? if (split[i][0] == '?' || split[i][1] == '?') { bytes[i] = 0; mask[i] = '?'; } // consider everything that is not a wildcard a byte else { bytes[i] = (byte)int.Parse(split[i], NumberStyles.HexNumber); mask[i] = 'x'; } } return(bytes, new string(mask)); }
public void StringToByteSize(string input, long output) { Assert.AreEqual(output, StringR.StringToByteSize(input)); Assert.IsTrue(StringR.StringToByteSize(input, out long result)); Assert.AreEqual(output, result); }
public bool IsValid(string?input, [NotNullWhen(false)] out AobError?aobError) { if (string.IsNullOrWhiteSpace(input)) { aobError = new AobError(input, "Input is null or empty"); return(false); } string trimmed = StringR.RemoveWhitespace(input); IEnumerable <char> illegalChars = trimmed.ToLowerInvariant().Except(AllowedChars); if (illegalChars.Any()) { aobError = new AobError(input, $"Illegal chars {RFReborn.Extensions.IEnumerableExtensions.ToObjectsString(illegalChars)}"); return(false); } if (trimmed.Length % 2 != 0) { aobError = new AobError(input, "Bytes need to be full bytes, length of input has to be divisible by two"); return(false); } aobError = null; return(true); }
/// <summary> /// Returns the standardized format of a signature, delimited with a space after every byte, with bytes being 2 chars, eg: "0F ?? AE ?? CC" /// </summary> /// <param name="signature">signature to format</param> /// <returns>formatted signature</returns> /// <exception cref="ArgumentOutOfRangeException"></exception> public static string Standardize(string signature) { signature = StringR.RemoveWhitespace(signature); if (signature.Length % 2 != 0) { throw new ArgumentOutOfRangeException(nameof(signature), "signature length(excluding whitespace) must be divisible by 2, make sure to prepend bytes with 0 if necessary and make wildcards full ?? instead of single ?"); } return(StringR.InSplit(signature, 2, " ").ToUpperInvariant()); }
/// <summary> /// Check if the path is ignored by the given pattern /// </summary> /// <param name="path">path to check</param> /// <param name="pattern">pattern to use</param> public static bool IsIgnored(string path, string pattern) { if (StringR.WildcardMatch(path, pattern)) { return(true); } DirectoryInfo di = new(path); return(StringR.WildcardMatch(di.Name, pattern)); }
public void ReverseStringCheck() { //Arrange var somestring = "HELLO"; //Act var actual = StringR.Reverse(somestring); var expected = "OLLEH"; //Assert Assert.Equal(expected, actual); }
public void ReverseStringCheck2() { //Arrange var somestring = "1234"; //Act var actual = StringR.Reverse(somestring); var expected = "4321"; //Assert Assert.Equal(expected, actual); }
/// <summary> /// Finds files by walking the root and wildcard matching the path and alt path with a given mask /// </summary> /// <param name="root">Root path to start walking</param> /// <param name="pattern">Wildcard mask to match filepath</param> public static IEnumerable <string> FindFilesByMatch(string root, string pattern) { foreach (string file in Walk(root, FileSystemEnumeration.FilesOnly)) { string[] tests = { Path.GetFileName(file), file, GetAltPath(file) }; foreach (string test in tests) { if (StringR.WildcardMatch(test, pattern)) { yield return(file); break; } } } }
public void NullInput() { Assert.ThrowsException <ArgumentNullException>(() => StringR.StringToByteSize(null)); Assert.IsFalse(StringR.StringToByteSize(null, out long _)); }
public void StringToByteSizeNumberOverflow(string input) { Assert.ThrowsException <OverflowException>(() => StringR.StringToByteSize(input)); Assert.IsFalse(StringR.StringToByteSize(input, out long _)); }
public void StringToByteSizeNotFormattableAsNumber(string input) { Assert.ThrowsException <FormatException>(() => StringR.StringToByteSize(input)); Assert.IsFalse(StringR.StringToByteSize(input, out long _)); }
/// <summary> /// Checks if a given pattern matches a string /// </summary> /// <param name="input">input to check</param> /// <param name="pattern">pattern to match</param> /// <returns>returns <see langword="true" /> if pattern matches; <see langword="false" /> otherwise</returns> public static bool WildcardMatch(this string input, string pattern) => StringR.WildcardMatch(input, pattern);
public string Prettify(string input) { input = StringR.RemoveWhitespace(input); return(StringR.InSplit(input, 2, " ").ToUpperInvariant()); }