Compare commits

...

6 Commits

Author SHA1 Message Date
12ba3bf530 Update README.md 2026-04-16 16:12:39 +00:00
b2d8048713 Upgrade plugin 2026-02-15 14:10:59 +01:00
c366c79d4c refactor utility function name 2026-02-03 23:03:59 +01:00
e08cbb0fa9 fix README
All checks were successful
/ test (pull_request) Successful in 45s
2026-01-21 22:09:31 +01:00
61d6a8e191 implement Base45 2026-01-21 22:04:51 +01:00
6283c2f44d refactor encodings 2026-01-21 22:03:03 +01:00
15 changed files with 183 additions and 163 deletions

View File

@@ -1,15 +1,19 @@
# encoding-kt # encoding.kt
`encoding-kt` is a Kotlin Multiplatform library that provides common `ByteArray` encodings. `encoding.kt` is a Kotlin Multiplatform library that provides common `ByteArray` encodings.
Supported encodings: Supported encodings:
- Base2 (binary) - Base2 (binary)
- Base8 (octal) - Base8 (octal)
- Base16 (hexadecimal) - Base10 (decimal)
- Base16 (hexadecimal, uppercase/lowercase)
- Base32 - Base32
- Base36 - Base36
- Base45
- Base56
- Base58
- Base62 - Base62
- Base64 - Base64 (standard/URL)
## Installation ## Installation
@@ -35,6 +39,6 @@ fun main() {
// encode the string with the desired encoding // encode the string with the desired encoding
val encoded = Base16.encode(test).decodeToString() val encoded = Base16.encode(test).decodeToString()
println(encoded) // 48656c6c6f20576f726c6421 println(encoded) // 48656C6C6F20576F726C6421
} }
``` ```

View File

@@ -1,5 +1,5 @@
[versions] [versions]
infendro = "1.0.0" infendro = "1.1.0"
kotlin = "2.3.0" kotlin = "2.3.0"
[plugins] [plugins]

View File

@@ -1,5 +1,10 @@
package com.infendro.encoding package com.infendro.encoding
private val alphabet = "0123456789ABCDEF".encodeToByteArray() private val uppercase = "0123456789ABCDEF".encodeToByteArray()
private val lowercase = "0123456789abcdef".encodeToByteArray()
object Base16 : Encoding by StreamEncoding(alphabet) object Base16 : Encoding by StreamEncoding(uppercase) {
val UPPERCASE = this
object LOWERCASE : Encoding by StreamEncoding(lowercase)
}

View File

@@ -3,4 +3,4 @@ package com.infendro.encoding
private val alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567".encodeToByteArray() private val alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567".encodeToByteArray()
private const val padding = '='.code.toByte() private const val padding = '='.code.toByte()
object Base32 : Encoding by PaddedStreamEncoding(alphabet, padding) object Base32 : Encoding by StreamEncoding(alphabet, padding)

View File

@@ -0,0 +1,51 @@
package com.infendro.encoding
import com.infendro.encoding.exception.IllegalByteException
object Base45 : Encoding {
private val alphabet: ByteArray = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:".encodeToByteArray()
private val base: Int = alphabet.size
override fun encode(bytes: ByteArray): ByteArray {
val result = mutableListOf<Byte>()
for (i in bytes.indices step 2) {
val remaining = minOf(bytes.size - i, 2)
var value = 0
for (j in 0..<remaining) {
val byte = bytes[i + j]
value = (value shl 8) or (byte.toInt() and 0xFF)
}
repeat(remaining + 1) {
result += alphabet[value % base]
value /= base
}
}
return result.toByteArray()
}
override fun decode(bytes: ByteArray): ByteArray {
val result = mutableListOf<Byte>()
for (i in bytes.indices step 3) {
val remaining = minOf(bytes.size - i, 3)
var value = 0
for (j in (0..<remaining).reversed()) {
val byte = bytes[i + j]
val index = alphabet.indexOf(byte)
if (index == -1) throw IllegalByteException(byte)
value = value * base + index
}
for (j in (0..<(remaining - 1)).reversed()) {
result += ((value shr (j * 8)) and 0xFF).toByte()
}
}
return result.toByteArray()
}
}

View File

@@ -1,6 +1,11 @@
package com.infendro.encoding package com.infendro.encoding
private val alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".encodeToByteArray() private val standard = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".encodeToByteArray()
private val url = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_".encodeToByteArray()
private const val padding = '='.code.toByte() private const val padding = '='.code.toByte()
object Base64 : Encoding by PaddedStreamEncoding(alphabet, padding) object Base64 : Encoding by StreamEncoding(standard, padding) {
val STANDARD = this
object URL : Encoding by StreamEncoding(url)
}

View File

@@ -1,10 +1,6 @@
package com.infendro.encoding package com.infendro.encoding
interface Encoding { interface Encoding {
val alphabet: ByteArray
val base: Int
get() = alphabet.size
fun encode(bytes: ByteArray): ByteArray fun encode(bytes: ByteArray): ByteArray
fun decode(bytes: ByteArray): ByteArray fun decode(bytes: ByteArray): ByteArray
} }

View File

@@ -4,8 +4,10 @@ import com.infendro.encoding.exception.IllegalByteException
import com.infendro.encoding.util.BigInt import com.infendro.encoding.util.BigInt
class NumericEncoding internal constructor( class NumericEncoding internal constructor(
override val alphabet: ByteArray, private val alphabet: ByteArray,
) : Encoding { ) : Encoding {
private val base: Int = alphabet.size
override fun encode(bytes: ByteArray): ByteArray { override fun encode(bytes: ByteArray): ByteArray {
if (bytes.isEmpty()) return byteArrayOf() if (bytes.isEmpty()) return byteArrayOf()
@@ -27,10 +29,8 @@ class NumericEncoding internal constructor(
var value = BigInt.zero var value = BigInt.zero
for (i in 0..<bytes.size) { for (i in 0..<bytes.size) {
val byte = bytes[i] val byte = bytes[i]
val index = alphabet.indexOf(byte) val index = alphabet.indexOf(byte)
if (index == -1) throw IllegalByteException(byte) if (index == -1) throw IllegalByteException(byte)
value = value.timesAdd(base, index) value = value.timesAdd(base, index)
} }

View File

@@ -1,78 +0,0 @@
package com.infendro.encoding
import com.infendro.encoding.exception.IllegalByteException
import com.infendro.encoding.util.align
import com.infendro.encoding.util.divCeil
import com.infendro.encoding.util.lcm
import com.infendro.encoding.util.log2
class PaddedStreamEncoding internal constructor(
override val alphabet: ByteArray,
private val padding: Byte,
) : Encoding {
override fun encode(bytes: ByteArray): ByteArray {
val bits = log2(base).toInt()
val block = lcm(bits, 8) / bits
val size = (bytes.size * 8).divCeil(bits).align(block)
val result = ByteArray(size)
var r = 0
var buffer = 0
var bufferBits = 0
for (i in bytes.indices) {
buffer = (buffer shl 8) + bytes[i]
bufferBits += 8
while (bufferBits >= bits) {
val offset = bufferBits - bits
val index = (buffer ushr offset)
buffer = buffer and ((base - 1) shl offset).inv()
bufferBits -= bits
result[r++] = alphabet[index]
}
}
if (bufferBits > 0) {
val index = buffer shl (bits - bufferBits)
result[r++] = alphabet[index]
}
while (r < result.size) {
result[r++] = padding
}
return result
}
override fun decode(bytes: ByteArray): ByteArray {
val bits = log2(base).toInt()
val end = bytes.indexOf(padding).takeIf { it != -1 } ?: bytes.size
val size = end * bits / 8
val result = ByteArray(size)
var r = 0
var buffer = 0
var bufferBits = 0
for (i in 0..<end) {
val byte = bytes[i]
val value = alphabet.indexOf(byte)
if (value == -1) throw IllegalByteException(byte)
buffer = (buffer shl bits) + value.toByte()
bufferBits += bits
if (bufferBits >= 8) {
val offset = bufferBits - 8
val character = (buffer ushr offset).toByte()
buffer = buffer and (0xFF shl offset).inv()
bufferBits -= 8
result[r++] = character
}
}
return result
}
}

View File

@@ -1,70 +1,80 @@
package com.infendro.encoding package com.infendro.encoding
import com.infendro.encoding.exception.IllegalByteException import com.infendro.encoding.exception.IllegalByteException
import com.infendro.encoding.util.divCeil import com.infendro.encoding.util.align
import com.infendro.encoding.util.ceilDiv
import com.infendro.encoding.util.lcm
import com.infendro.encoding.util.log2 import com.infendro.encoding.util.log2
class StreamEncoding internal constructor( class StreamEncoding internal constructor(
override val alphabet: ByteArray, private val alphabet: ByteArray,
private val padding: Byte? = null,
) : Encoding { ) : Encoding {
private val base: Int = alphabet.size
private val bits: Int = log2(base).toInt()
private val block: Int = lcm(bits, 8) / bits
private val mask: Int = (0x01 shl bits) - 1
override fun encode(bytes: ByteArray): ByteArray { override fun encode(bytes: ByteArray): ByteArray {
val bits = log2(base).toInt() val size = when (padding) {
null -> (bytes.size * 8).ceilDiv(bits)
else -> (bytes.size * 8).ceilDiv(bits).align(block)
}
val encoded = ByteArray(size)
var i = 0
val size = (bytes.size * 8).divCeil(bits)
val result = ByteArray(size)
var r = 0
var buffer = 0 var buffer = 0
var bufferBits = 0 var bufferBits = 0
for (i in bytes.indices) { for (byte in bytes) {
buffer = (buffer shl 8) + bytes[i] buffer = (buffer shl 8) or (byte.toInt() and 0xFF)
bufferBits += 8 bufferBits += 8
while (bufferBits >= bits) { while (bufferBits >= bits) {
val offset = bufferBits - bits val index = (buffer shr (bufferBits - bits)) and mask
val index = (buffer ushr offset)
buffer = buffer and ((base - 1) shl offset).inv()
bufferBits -= bits bufferBits -= bits
encoded[i++] = alphabet[index]
result[r++] = alphabet[index]
} }
} }
if (bufferBits > 0) { if (bufferBits > 0) {
val index = buffer shl (bits - bufferBits) val index = (buffer shl (bits - bufferBits)) and mask
result[r] = alphabet[index] encoded[i++] = alphabet[index]
} }
return result while (i < encoded.size) {
encoded[i++] = padding!!
}
return encoded
} }
override fun decode(bytes: ByteArray): ByteArray { override fun decode(bytes: ByteArray): ByteArray {
val bits = log2(base).toInt() val end = when (padding) {
null -> bytes.size
else -> when (val index = bytes.indexOf(padding)) {
-1 -> bytes.size
else -> index
}
}
val size = end * bits / 8
val decoded = ByteArray(size)
var i = 0
val size = bytes.size * bits / 8
val result = ByteArray(size)
var r = 0
var buffer = 0 var buffer = 0
var bufferBits = 0 var bufferBits = 0
for (i in bytes.indices) { for (b in 0..<end) {
val byte = bytes[i] val byte = bytes[b]
val index = alphabet.indexOf(byte)
val value = alphabet.indexOf(byte) if (index == -1) throw IllegalByteException(byte)
if (value == -1) throw IllegalByteException(byte) buffer = (buffer shl bits) or index
buffer = (buffer shl bits) + value.toByte()
bufferBits += bits bufferBits += bits
if (bufferBits >= 8) { if (bufferBits >= 8) {
val offset = bufferBits - 8 val byte = (buffer shr (bufferBits - 8)) and 0xFF
val character = (buffer ushr offset).toByte()
buffer = buffer and (0xFF shl offset).inv()
bufferBits -= 8 bufferBits -= 8
decoded[i++] = byte.toByte()
result[r++] = character
} }
} }
return result return decoded
} }
} }

View File

@@ -49,40 +49,24 @@ internal class BigInt private constructor(
} }
fun toByteArray(): ByteArray { fun toByteArray(): ByteArray {
val msw = words.last() val bytes = ByteArray((words.size * 4) - words.last().countLeadingZeroBytes())
val mswBytes = when { for (i in bytes.indices) {
(msw ushr 24) != 0 -> 4 val (wi, bi) = (bytes.lastIndex - i).divRem(4)
(msw ushr 16) != 0 -> 3 bytes[i] = (words[wi] ushr (bi * 8)).toByte()
(msw ushr 8) != 0 -> 2
else -> 1
} }
val size = ((words.size - 1) * 4) + mswBytes return bytes
val result = ByteArray(size)
for (i in 0..<size) {
val index = size - 1 - i
val shift = (index % 4) * 8
result[i] = (words[index / 4] ushr shift).toByte()
}
return result
} }
companion object { companion object {
val zero: BigInt val zero: BigInt
get() = BigInt(intArrayOf(0)) get() = BigInt(intArrayOf(0))
fun from(value: ByteArray): BigInt { fun from(bytes: ByteArray): BigInt {
val size = value.size.divCeil(4) val words = IntArray(bytes.size.ceilDiv(4))
val words = IntArray(size) for (i in bytes.indices) {
val (wi, bi) = (bytes.lastIndex - i).divRem(4)
for (i in value.indices) { words[wi] = words[wi] or ((bytes[i].toInt() and 0xFF) shl (bi * 8))
val byte = value[i].toInt() and 0xFF
val index = value.lastIndex - i
val shift = (index % 4) * 8
words[index / 4] = words[index / 4] or (byte shl shift)
} }
return BigInt(words) return BigInt(words)
} }
} }

View File

@@ -0,0 +1,7 @@
package com.infendro.encoding.util
fun Int.divRem(divisor: Int): Pair<Int, Int> =
Pair(this / divisor, this % divisor)
fun Int.countLeadingZeroBytes(): Int =
countLeadingZeroBits() / 8

View File

@@ -2,14 +2,14 @@ package com.infendro.encoding.util
import kotlin.math.log2 import kotlin.math.log2
internal fun log2(value: Int): Double = internal fun Int.ceilDiv(n: Int): Int =
log2(value.toDouble())
internal fun Int.divCeil(n: Int): Int =
(this + n - 1) / n (this + n - 1) / n
internal fun Int.align(n: Int): Int = internal fun Int.align(n: Int): Int =
divCeil(n) * n ceilDiv(n) * n
internal fun log2(value: Int): Double =
log2(value.toDouble())
internal fun gcd(a: Int, b: Int): Int { internal fun gcd(a: Int, b: Int): Int {
var x = a var x = a

View File

@@ -0,0 +1,36 @@
package com.infendro.encoding
import kotlin.test.Test
import kotlin.test.assertContentEquals
class `Base45 Test` {
val encoding: Encoding
get() = Base45
val original = "Hello World!".encodeToByteArray()
val encoded = "%69 VD82EI2B.KESTC".encodeToByteArray()
@Test
fun `encode empty`() {
val actual = encoding.encode(byteArrayOf())
assertContentEquals(byteArrayOf(), actual)
}
@Test
fun `decode empty`() {
val actual = encoding.decode(byteArrayOf())
assertContentEquals(byteArrayOf(), actual)
}
@Test
fun encode() {
val actual = encoding.encode(original)
assertContentEquals(encoded, actual)
}
@Test
fun decode() {
val actual = encoding.decode(encoded)
assertContentEquals(original, actual)
}
}

View File

@@ -24,7 +24,7 @@ class `Base56 Test` {
@Test @Test
fun encode() { fun encode() {
val actual = encoding.encode(original).also { println(it.decodeToString()) } val actual = encoding.encode(original)
assertContentEquals(encoded, actual) assertContentEquals(encoded, actual)
} }