Skip to content

Commit 7e2f45c

Browse files
committed
BytesStripRight and BytesTerminate: can return original array
1 parent 90c9228 commit 7e2f45c

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

KaitaiStream.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,22 +461,29 @@ public byte[] EnsureFixedContents(byte[] expected)
461461

462462
/// <summary>
463463
/// Perform right-to-left strip on a byte array.
464+
/// WARNING: Can return original byte array.
464465
/// </summary>
465466
/// <param name="src">The data, as byte array</param>
466467
/// <param name="padByte">The padding byte, as integer</param>
467468
public static byte[] BytesStripRight(byte[] src, byte padByte)
468469
{
469470
int newLen = src.Length;
471+
int maxLen = src.Length;
472+
470473
while (newLen > 0 && src[newLen - 1] == padByte)
471474
newLen--;
472475

476+
if (newLen == maxLen)
477+
return src;
478+
473479
byte[] dst = new byte[newLen];
474480
Array.Copy(src, dst, newLen);
475481
return dst;
476482
}
477483

478484
/// <summary>
479485
/// Perform left-to-right search of specified terminating byte, and cutoff remaining bytes.
486+
/// WARNING: Can return original byte array.
480487
/// </summary>
481488
/// <param name="src">The data, as byte array</param>
482489
/// <param name="terminator">The terminating byte, as integer</param>
@@ -492,6 +499,9 @@ public static byte[] BytesTerminate(byte[] src, byte terminator, bool includeTer
492499
if (includeTerminator && newLen < maxLen)
493500
newLen++;
494501

502+
if (newLen == maxLen)
503+
return src;
504+
495505
byte[] dst = new byte[newLen];
496506
Array.Copy(src, dst, newLen);
497507
return dst;

0 commit comments

Comments
 (0)