diff --git a/src/commonMain/kotlin/nl/astraeus/tba/ByteArrayHandler.kt b/src/commonMain/kotlin/nl/astraeus/tba/ByteArrayHandler.kt index 23d1707..ae817c7 100644 --- a/src/commonMain/kotlin/nl/astraeus/tba/ByteArrayHandler.kt +++ b/src/commonMain/kotlin/nl/astraeus/tba/ByteArrayHandler.kt @@ -66,7 +66,7 @@ open class ByteArrayHandler( fun getClob(index: Int): String = buffer.getClob(index) - fun getBlob(index: Int): SlicedByteArray = buffer.getBlob(index) + fun getBlob(index: Int, length: Int): SlicedByteArray = buffer.getBlob(index, length) fun slice(range: IntRange): ByteArrayHandler { return ByteArrayHandler( diff --git a/src/commonMain/kotlin/nl/astraeus/tba/ByteArrayProperties.kt b/src/commonMain/kotlin/nl/astraeus/tba/ByteArrayProperties.kt index c645b50..f9d4561 100644 --- a/src/commonMain/kotlin/nl/astraeus/tba/ByteArrayProperties.kt +++ b/src/commonMain/kotlin/nl/astraeus/tba/ByteArrayProperties.kt @@ -163,7 +163,8 @@ class BlobProperty( name: String ) : ByteArrayPropertyWithLength(name) { override fun getValue(thisRef: TypedByteArray, property: KProperty<*>): SlicedByteArray { - return thisRef.data.getBlob(getIndex(thisRef)) + val result = thisRef.data.getBlob(getIndex(thisRef), getMaxLength(thisRef)) + return result } override fun setValue(thisRef: TypedByteArray, property: KProperty<*>, value: SlicedByteArray) { diff --git a/src/commonMain/kotlin/nl/astraeus/tba/SlicedByteArray.kt b/src/commonMain/kotlin/nl/astraeus/tba/SlicedByteArray.kt index 41f7f1e..70c47db 100644 --- a/src/commonMain/kotlin/nl/astraeus/tba/SlicedByteArray.kt +++ b/src/commonMain/kotlin/nl/astraeus/tba/SlicedByteArray.kt @@ -24,7 +24,7 @@ class SlicedByteArray( } fun getShort(index: Int): Short { - check(offset + index + 1 < offset + length) { "Index out of bounds" } + check(offset + index + 1 < offset + length) { "Index out of bounds, ${offset + index + 1} > ${offset + length} ($offset, $index, $length)" } return ((data[offset + index].toInt() shl 8) or (data[offset + index + 1].toInt() and 0xff)).toShort() } @@ -82,14 +82,14 @@ class SlicedByteArray( fun getClob(index: Int): String { val length = getInt(index) - check(offset + index + length < offset + this.length) { "Index out of bounds" } + check(offset + index + length <= offset + this.length) { "Index out of bounds" } val str = data.copyOfRange(offset + index + 4, offset + index + 4 + length).decodeToString() return str } - fun getBlob(index: Int): SlicedByteArray { - val length = getInt(index) - check(offset + index + length < offset + this.length) { "Index out of bounds" } + fun getBlob(index: Int, length: Int): SlicedByteArray { + check(index + length <= this.length) { "Index out of bounds ($index, $length, ${this.length}" } + val str = SlicedByteArray(data, offset + index + 4, length) return str } @@ -143,7 +143,7 @@ class SlicedByteArray( throw IllegalArgumentException("Blob is too long") } this.setInt(offset + index, bytes.size) - bytes.copyInto(data, offset + index + 4, maxLength - 4) + bytes.copyInto(data, offset + index + 4, maxLength) } companion object { @@ -152,7 +152,7 @@ class SlicedByteArray( offset: Int = 0, length: Int = data.size ): SlicedByteArray { - return SlicedByteArray(data, offset,length) + return SlicedByteArray(data, offset, length) } } }