diff --git a/CHANGELOG.md b/CHANGELOG.md index 80cac0f1..84c7d326 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Fixed + +- Crash on older Roku OS version due to `roByteArray.Slice` not being available + ## [0.43.1] - 2025-10-28 ### Added diff --git a/playlet-lib/src/components/Web/WebServer/Http/HttpRequest.bs b/playlet-lib/src/components/Web/WebServer/Http/HttpRequest.bs index 46073f73..fc25167d 100644 --- a/playlet-lib/src/components/Web/WebServer/Http/HttpRequest.bs +++ b/playlet-lib/src/components/Web/WebServer/Http/HttpRequest.bs @@ -1,6 +1,7 @@ import "HttpRequestState.bs" import "pkg:/components/Web/WebServer/Http/HttpConnection.bs" import "pkg:/components/Web/WebServer/Http/HttpUtils.bs" +import "pkg:/source/utils/ByteArrayUtils.bs" import "pkg:/source/utils/Logging.bs" import "pkg:/source/utils/MathUtils.bs" import "pkg:/source/utils/Types.bs" @@ -207,7 +208,7 @@ namespace Http return false end if - m.body_bytes = m.buffer.Slice(m.content_index) + m.body_bytes = ByteArrayUtils.Slice(m.buffer, m.content_index) m.ready_for_response = true return true end function diff --git a/playlet-lib/src/source/Protobuf/BinaryReader.bs b/playlet-lib/src/source/Protobuf/BinaryReader.bs index f889ce66..edd20f8e 100644 --- a/playlet-lib/src/source/Protobuf/BinaryReader.bs +++ b/playlet-lib/src/source/Protobuf/BinaryReader.bs @@ -1,3 +1,5 @@ +import "pkg:/source/utils/ByteArrayUtils.bs" + namespace Protobuf class BinaryReader @@ -79,7 +81,7 @@ namespace Protobuf if endPosition > m.buffer.Count() return invalid end if - value = m.buffer.Slice(m.position, endPosition) + value = ByteArrayUtils.Slice(m.buffer, m.position, endPosition) m.position += length return value end function diff --git a/playlet-lib/src/source/utils/ByteArrayUtils.bs b/playlet-lib/src/source/utils/ByteArrayUtils.bs new file mode 100644 index 00000000..f449a659 --- /dev/null +++ b/playlet-lib/src/source/utils/ByteArrayUtils.bs @@ -0,0 +1,65 @@ +import "StringUtils.bs" +namespace ByteArrayUtils + + function Slice(bytearray as object, start_pos = 0 as dynamic, end_pos = invalid as dynamic) as object + if FindMemberFunction(bytearray, "Slice") <> invalid + start_pos = ValidInt(start_pos) + if end_pos = invalid + return bytearray.Slice(start_pos) + end if + return bytearray.Slice(start_pos, end_pos) + end if + return SliceFallback(bytearray, start_pos, end_pos) + end function + + function SliceFallback(bytearray as object, start_pos = 0 as dynamic, end_pos = invalid as dynamic) as object + copy = CreateObject("roByteArray") + arrCount = bytearray.Count() + + ' default end_pos is array length + if end_pos = invalid + end_pos = arrCount + end if + + start_pos = ValidInt(start_pos) + ' handle negative start_pos + if start_pos < 0 + start_pos = arrCount + start_pos + end if + + ' handle negative end_pos + if end_pos < 0 + end_pos = arrCount + end_pos + end if + + ' clamp start_pos to bounds + if start_pos < 0 + start_pos = 0 + else if start_pos > arrCount + start_pos = arrCount + end if + + ' clamp end_pos to bounds + if end_pos < 0 + end_pos = 0 + else if end_pos > arrCount + end_pos = arrCount + end if + + count = end_pos - start_pos + if count <= 0 + return copy + end if + + tmp = `tmp:/${StringUtils.RandomId()}.bin` + if not bytearray.WriteFile(tmp, start_pos, count) + DeleteFile(tmp) + return copy + end if + + copy.ReadFile(tmp) + DeleteFile(tmp) + return copy + end function + +end namespace diff --git a/playlet-lib/src/source/utils/ByteArrayUtils.spec.bs b/playlet-lib/src/source/utils/ByteArrayUtils.spec.bs new file mode 100644 index 00000000..11b090c9 --- /dev/null +++ b/playlet-lib/src/source/utils/ByteArrayUtils.spec.bs @@ -0,0 +1,45 @@ +namespace Tests + + @suite + class ByteArrayUtilsTests extends Rooibos.BaseTestSuite + + protected override function beforeEach() + m.ba = CreateObject("roByteArray") + m.ba.FromHexString("00010203040506070809") + end function + + @describe("ByteArrayUtils.Slice and SliceFallback") + + @it("slices a byte array correctly") + @params("positive start and end", 2, 5, "020304") + @params("to the end if end is not provided", 7, invalid, "070809") + @params("the whole array if no params provided", invalid, invalid, "00010203040506070809") + @params("with negative start", -4, invalid, "06070809") + @params("with negative end", 0, -3, "00010203040506") + @params("with negative start and negative end", -5, -2, "050607") + @params("returns empty array for start greater than end", 5, 2, "") + @params("returns empty for start out of bounds", 20, invalid, "") + @params("clamps end to array length", 8, 20, "0809") + @params("handles empty slice", 3, 3, "") + function testSlice(description as string, start_pos as dynamic, end_pos as dynamic, expectedHex as string) + resultBa = ByteArrayUtils.Slice(m.ba, start_pos, end_pos) + m.assertEqual(resultBa.ToHexString(), expectedHex, "Slice failed for: " + description) + + resultBaFallback = ByteArrayUtils.SliceFallback(m.ba, start_pos, end_pos) + m.assertEqual(resultBaFallback.ToHexString(), expectedHex, "SliceFallback failed for: " + description) + end function + + @it("handles empty byte array") + function handles_empty_byte_array() + emptyBa = CreateObject("roByteArray") + + resultBa = ByteArrayUtils.Slice(emptyBa, 0, 0) + m.assertEqual(resultBa.ToHexString(), "") + + resultBaFallback = ByteArrayUtils.SliceFallback(emptyBa, 0, 0) + m.assertEqual(resultBaFallback.ToHexString(), "") + end function + + end class + +end namespace