From 7e2f45c31d6b790942afdd330e07136151903d6d Mon Sep 17 00:00:00 2001 From: Arkadiusz Bulski Date: Sat, 7 Apr 2018 01:31:00 +0200 Subject: [PATCH] BytesStripRight and BytesTerminate: can return original array --- KaitaiStream.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/KaitaiStream.cs b/KaitaiStream.cs index e4f8f56..e89ef1a 100644 --- a/KaitaiStream.cs +++ b/KaitaiStream.cs @@ -461,15 +461,21 @@ public byte[] EnsureFixedContents(byte[] expected) /// /// Perform right-to-left strip on a byte array. + /// WARNING: Can return original byte array. /// /// The data, as byte array /// The padding byte, as integer public static byte[] BytesStripRight(byte[] src, byte padByte) { int newLen = src.Length; + int maxLen = src.Length; + while (newLen > 0 && src[newLen - 1] == padByte) newLen--; + if (newLen == maxLen) + return src; + byte[] dst = new byte[newLen]; Array.Copy(src, dst, newLen); return dst; @@ -477,6 +483,7 @@ public static byte[] BytesStripRight(byte[] src, byte padByte) /// /// Perform left-to-right search of specified terminating byte, and cutoff remaining bytes. + /// WARNING: Can return original byte array. /// /// The data, as byte array /// The terminating byte, as integer @@ -492,6 +499,9 @@ public static byte[] BytesTerminate(byte[] src, byte terminator, bool includeTer if (includeTerminator && newLen < maxLen) newLen++; + if (newLen == maxLen) + return src; + byte[] dst = new byte[newLen]; Array.Copy(src, dst, newLen); return dst;