From d7ca54174170d2a1d6418d2fd8c1b4cf99855699 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 | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/KaitaiStream.cs b/KaitaiStream.cs index 42b45a9..34fcc20 100644 --- a/KaitaiStream.cs +++ b/KaitaiStream.cs @@ -460,6 +460,7 @@ 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 @@ -469,6 +470,9 @@ public static byte[] BytesStripRight(byte[] src, byte padByte) while (newLen > 0 && src[newLen - 1] == padByte) newLen--; + if (newLen == src.Length) + return src; + byte[] dst = new byte[newLen]; Array.Copy(src, dst, newLen); return dst; @@ -476,6 +480,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 @@ -491,6 +496,9 @@ public static byte[] BytesTerminate(byte[] src, byte terminator, bool includeTer if (includeTerminator && newLen < maxLen) newLen++; + if (newLen == src.Length) + return src; + byte[] dst = new byte[newLen]; Array.Copy(src, dst, newLen); return dst;