Skip to content

Commit 52308c2

Browse files
committed
BytesStripRight and BytesTerminate: can return original array
1 parent 3df4e31 commit 52308c2

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
@@ -460,22 +460,29 @@ public byte[] EnsureFixedContents(byte[] expected)
460460

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

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

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

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

0 commit comments

Comments
 (0)