From fd30061263cd748539ee252c22c9d1f00d2ab793 Mon Sep 17 00:00:00 2001 From: Ibrahim Akgul Date: Thu, 17 Nov 2022 18:46:20 +0300 Subject: [PATCH] Update Utils.cs This pull request is related to the issue https://github.com/EricZimmerman/ExtensionBlocks/issues/7 --- ExtensionBlocks/Utils.cs | 60 ++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/ExtensionBlocks/Utils.cs b/ExtensionBlocks/Utils.cs index a4490dd..8e5a077 100644 --- a/ExtensionBlocks/Utils.cs +++ b/ExtensionBlocks/Utils.cs @@ -4100,52 +4100,58 @@ private static string[] MultistringToStringArray(ref char[] arg) public static List GetStringsFromMultistring(byte[] rawBytes) { - var outstrings = new List(); + var outstrings = new List(); //drop last 2 bytes rawBytes = rawBytes.Take(rawBytes.Length - 2).ToArray(); var bytestring = BitConverter.ToString(rawBytes); - if (bytestring.EndsWith("-00-00-00") == false) + if (bytestring.StartsWith("00-00-00-") == false) { - bytestring = bytestring + "-00"; - } + if (bytestring.EndsWith("-00-00-00") == false) + { + bytestring = bytestring + "-00"; + } - var index = 0; + var index = 0; - try - { - var regexObj = new Regex("00-00-", RegexOptions.IgnoreCase); - var matchResults = regexObj.Match(bytestring); - while (matchResults.Success) + try { - // matched text: matchResults.Value - // match start: matchResults.Index + var regexObj = new Regex("00-00-", RegexOptions.IgnoreCase); + var matchResults = regexObj.Match(bytestring); + while (matchResults.Success) + { + // matched text: matchResults.Value + // match start: matchResults.Index - var instancePosition = (matchResults.Index + 3)/3; + var instancePosition = (matchResults.Index + 3) / 3; - if (instancePosition + 2 < rawBytes.Length) - { - outstrings.Add( - ReplaceNulls(Encoding.Unicode.GetString(rawBytes, index, instancePosition - index))); + if (instancePosition + 2 < rawBytes.Length) + { + outstrings.Add( + ReplaceNulls(Encoding.Unicode.GetString(rawBytes, index, instancePosition - index))); - index += instancePosition + 2; - } + index += instancePosition + 2; + } - // match length: matchResults.Length - matchResults = matchResults.NextMatch(); + // match length: matchResults.Length + matchResults = matchResults.NextMatch(); + } + if (index + 4 <= rawBytes.Length) + { + outstrings.Add(ReplaceNulls(Encoding.Unicode.GetString(rawBytes, index, rawBytes.Length - index))); + } } - if (index + 4 <= rawBytes.Length) + catch (ArgumentException) { - outstrings.Add(ReplaceNulls(Encoding.Unicode.GetString(rawBytes, index, rawBytes.Length - index))); + throw; } } - catch (ArgumentException) + else { - throw; + outstrings.Add(string.Empty); } - return outstrings; } @@ -4246,4 +4252,4 @@ public static string GetFolderNameFromGuid(string guid) return thedate; } } -} \ No newline at end of file +}