Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
kotlin("jvm") version "2.3.0" apply false
kotlin("jvm") version "2.3.21" apply false
id("com.android.kotlin.multiplatform.library") version "9.1.1" apply false
id("com.diffplug.spotless") version "8.2.0"
id("com.github.ben-manes.versions") version "0.53.0"
Expand Down
4 changes: 2 additions & 2 deletions kex/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ kotlin {

sourceSets {
commonMain.dependencies {
implementation("io.ktor:ktor-io:3.4.0")
implementation("io.ktor:ktor-io:3.5.2")
}
commonTest.dependencies {
implementation(kotlin("test"))
Expand All @@ -44,7 +44,7 @@ publishing {
groupId = "de.peilicke.sascha"
version = "1.1.4"

artifact(javadocJar.get())
artifact(javadocJar)
pom {
name.set("Kex")
description.set("Hex string encoder/decoder for Kotlin/Multiplatform. Supports Android, iOS, JavaScript and plain JVM environments.")
Expand Down
26 changes: 14 additions & 12 deletions kex/src/commonMain/kotlin/saschpe/kex/Hex.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/
package saschpe.kex

import io.ktor.utils.io.core.*
import kotlinx.io.Buffer
import kotlinx.io.Sink
import kotlinx.io.readByteArray
import kotlin.experimental.or
import kotlin.jvm.JvmStatic
import kotlin.math.min
Expand Down Expand Up @@ -69,23 +71,23 @@ object Hex {
* @return A ByteArray containing the hex-encoded data.
*/
@JvmStatic
fun encode(data: ByteArray): ByteArray = buildPacket { encodeInternal(data, this) }.readBytes()
fun encode(data: ByteArray): ByteArray = Buffer().apply { encodeInternal(data, this) }.readByteArray()

/**
* Decode the hex-encoded input data. It is assumed the input data is valid.
*
* @return A ByteArray representing the decoded data.
*/
@JvmStatic
fun decode(data: ByteArray): ByteArray = buildPacket { decodeInternal(data, this) }.readBytes()
fun decode(data: ByteArray): ByteArray = Buffer().apply { decodeInternal(data, this) }.readByteArray()

/**
* Decode the hex-encoded String data - whitespaces will be ignored.
*
* @return A ByteArray representing the decoded data.
*/
@JvmStatic
fun decode(data: String): ByteArray = buildPacket { decodeInternal(data, this) }.readBytes()
fun decode(data: String): ByteArray = Buffer().apply { decodeInternal(data, this) }.readByteArray()

private fun encodeInternal(inBuf: ByteArray, inOff: Int, inLen: Int, outBuf: ByteArray, outOff: Int): Int {
var inPos = inOff
Expand All @@ -99,21 +101,21 @@ object Hex {
return outPos - outOff
}

private fun encodeInternal(buf: ByteArray, output: Output): Int {
private fun encodeInternal(buf: ByteArray, output: Sink): Int {
var offM = 0
var lenM = buf.size
val tmp = ByteArray(72)
while (lenM > 0) {
val inLen = min(36, lenM)
val outLen = encodeInternal(buf, offM, inLen, tmp, 0)
output.writeFully(tmp, 0, outLen)
output.write(tmp, 0, outLen)
offM += inLen
lenM -= inLen
}
return lenM * 2
}

private fun decodeInternal(data: ByteArray, output: Output): Int {
private fun decodeInternal(data: ByteArray, output: Sink): Int {
val off = 0
val length = data.size
var b1: Byte
Expand Down Expand Up @@ -143,18 +145,18 @@ object Hex {
}
buf[bufOff++] = (b1.toInt() shl 4 or b2.toInt()).toByte()
if (bufOff == buf.size) {
output.writeFully(buf, 0, buf.size)
output.write(buf, 0, buf.size)
bufOff = 0
}
outLen++
}
if (bufOff > 0) {
output.writeFully(buf, 0, bufOff)
output.write(buf, 0, bufOff)
}
return outLen
}

private fun decodeInternal(data: String, output: Output): Int {
private fun decodeInternal(data: String, output: Sink): Int {
var b1: Byte
var b2: Byte
var length = 0
Expand Down Expand Up @@ -182,13 +184,13 @@ object Hex {
}
buf[bufOff++] = (b1.toInt() shl 4 or b2.toInt()).toByte()
if (bufOff == buf.size) {
output.writeFully(buf, 0, buf.size)
output.write(buf, 0, buf.size)
bufOff = 0
}
length++
}
if (bufOff > 0) {
output.writeFully(buf, 0, bufOff)
output.write(buf, 0, bufOff)
}
return length
}
Expand Down
14 changes: 10 additions & 4 deletions kotlin-js-store/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ is-fullwidth-code-point@^3.0.0:
resolved "https://registry.yarnpkg.com/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz#f116f8064fe90b3f7844a38997c0b75051269f1d"
integrity sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==

is-path-inside@^3.0.3:
version "3.0.3"
resolved "https://registry.yarnpkg.com/is-path-inside/-/is-path-inside-3.0.3.tgz#d231362e53a07ff2b0e0ea7fed049161ffd16283"
integrity sha512-Fd4gABb+ycGAmKou8eMftCupSir5lRxqf4aD/vd0cD2qc4HL07OjCeuHMr8Ro4CoMaeCKDB0/ECBOVWjTwUvPQ==

is-plain-obj@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/is-plain-obj/-/is-plain-obj-2.1.0.tgz#45e42e37fccf1f40da8e5f76ee21515840c09287"
Expand Down Expand Up @@ -288,10 +293,10 @@ minimatch@^9.0.4, minimatch@^9.0.5:
resolved "https://registry.yarnpkg.com/minipass/-/minipass-7.1.3.tgz#79389b4eb1bb2d003a9bba87d492f2bd37bdc65b"
integrity sha512-tEBHqDnIoM/1rXME1zgka9g6Q2lcoCkxHLuc7ODJ5BxbP5d4c2Z5cGgtXAku59200Cx7diuHTOYfSBD8n6mm8A==

mocha@11.7.2:
version "11.7.2"
resolved "https://registry.yarnpkg.com/mocha/-/mocha-11.7.2.tgz#3c0079fe5cc2f8ea86d99124debcc42bb1ab22b5"
integrity sha512-lkqVJPmqqG/w5jmmFtiRvtA2jkDyNVUcefFJKb2uyX4dekk8Okgqop3cgbFiaIvj8uCRJVTP5x9dfxGyXm2jvQ==
mocha@11.7.5:
version "11.7.5"
resolved "https://registry.yarnpkg.com/mocha/-/mocha-11.7.5.tgz#58f5bbfa5e0211ce7e5ee6128107cefc2515a627"
integrity sha512-mTT6RgopEYABzXWFx+GcJ+ZQ32kp4fMf0xvpZIIfSq9Z8lC/++MtcCnQ9t5FP2veYEP95FIYSvW+U9fV4xrlig==
dependencies:
browser-stdout "^1.3.1"
chokidar "^4.0.1"
Expand All @@ -301,6 +306,7 @@ mocha@11.7.2:
find-up "^5.0.0"
glob "^10.4.5"
he "^1.2.0"
is-path-inside "^3.0.3"
js-yaml "^4.1.0"
log-symbols "^4.1.0"
minimatch "^9.0.5"
Expand Down
Loading