Compare commits

..

4 Commits

Author SHA1 Message Date
841bdfa21e Merge pull request '1.3.0 release' (#7) from develop into main
All checks were successful
/ publish (push) Successful in 4m1s
Reviewed-on: Infendro/encoding-kt#7
2026-01-29 16:46:03 +00: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
14 changed files with 177 additions and 157 deletions

View File

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

View File

@@ -1,5 +1,10 @@
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 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
private val alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".encodeToByteArray()
private val standard = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".encodeToByteArray()
private val url = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_".encodeToByteArray()
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
interface Encoding {
val alphabet: ByteArray
val base: Int
get() = alphabet.size
fun encode(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
class NumericEncoding internal constructor(
override val alphabet: ByteArray,
private val alphabet: ByteArray,
) : Encoding {
private val base: Int = alphabet.size
override fun encode(bytes: ByteArray): ByteArray {
if (bytes.isEmpty()) return byteArrayOf()
@@ -27,10 +29,8 @@ class NumericEncoding internal constructor(
var value = BigInt.zero
for (i in 0..<bytes.size) {
val byte = bytes[i]
val index = alphabet.indexOf(byte)
if (index == -1) throw IllegalByteException(byte)
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
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 StreamEncoding internal constructor(
override val alphabet: ByteArray,
private val alphabet: ByteArray,
private val padding: Byte? = null,
) : 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 {
val bits = log2(base).toInt()
val size = when (padding) {
null -> (bytes.size * 8).divCeil(bits)
else -> (bytes.size * 8).divCeil(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 bufferBits = 0
for (i in bytes.indices) {
buffer = (buffer shl 8) + bytes[i]
for (byte in bytes) {
buffer = (buffer shl 8) or (byte.toInt() and 0xFF)
bufferBits += 8
while (bufferBits >= bits) {
val offset = bufferBits - bits
val index = (buffer ushr offset)
buffer = buffer and ((base - 1) shl offset).inv()
val index = (buffer shr (bufferBits - bits)) and mask
bufferBits -= bits
result[r++] = alphabet[index]
encoded[i++] = alphabet[index]
}
}
if (bufferBits > 0) {
val index = buffer shl (bits - bufferBits)
result[r] = alphabet[index]
val index = (buffer shl (bits - bufferBits)) and mask
encoded[i++] = alphabet[index]
}
return result
while (i < encoded.size) {
encoded[i++] = padding!!
}
return encoded
}
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 bufferBits = 0
for (i in bytes.indices) {
val byte = bytes[i]
val value = alphabet.indexOf(byte)
if (value == -1) throw IllegalByteException(byte)
buffer = (buffer shl bits) + value.toByte()
for (b in 0..<end) {
val byte = bytes[b]
val index = alphabet.indexOf(byte)
if (index == -1) throw IllegalByteException(byte)
buffer = (buffer shl bits) or index
bufferBits += bits
if (bufferBits >= 8) {
val offset = bufferBits - 8
val character = (buffer ushr offset).toByte()
buffer = buffer and (0xFF shl offset).inv()
val byte = (buffer shr (bufferBits - 8)) and 0xFF
bufferBits -= 8
result[r++] = character
decoded[i++] = byte.toByte()
}
}
return result
return decoded
}
}

View File

@@ -49,40 +49,24 @@ internal class BigInt private constructor(
}
fun toByteArray(): ByteArray {
val msw = words.last()
val mswBytes = when {
(msw ushr 24) != 0 -> 4
(msw ushr 16) != 0 -> 3
(msw ushr 8) != 0 -> 2
else -> 1
val bytes = ByteArray((words.size * 4) - words.last().countLeadingZeroBytes())
for (i in bytes.indices) {
val (wi, bi) = (bytes.lastIndex - i).divRem(4)
bytes[i] = (words[wi] ushr (bi * 8)).toByte()
}
val size = ((words.size - 1) * 4) + mswBytes
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
return bytes
}
companion object {
val zero: BigInt
get() = BigInt(intArrayOf(0))
fun from(value: ByteArray): BigInt {
val size = value.size.divCeil(4)
val words = IntArray(size)
for (i in value.indices) {
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)
fun from(bytes: ByteArray): BigInt {
val words = IntArray(bytes.size.divCeil(4))
for (i in bytes.indices) {
val (wi, bi) = (bytes.lastIndex - i).divRem(4)
words[wi] = words[wi] or ((bytes[i].toInt() and 0xFF) shl (bi * 8))
}
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,15 +2,15 @@ package com.infendro.encoding.util
import kotlin.math.log2
internal fun log2(value: Int): Double =
log2(value.toDouble())
internal fun Int.divCeil(n: Int): Int =
(this + n - 1) / n
internal fun Int.align(n: Int): Int =
divCeil(n) * n
internal fun log2(value: Int): Double =
log2(value.toDouble())
internal fun gcd(a: Int, b: Int): Int {
var x = a
var y = b

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
fun encode() {
val actual = encoding.encode(original).also { println(it.decodeToString()) }
val actual = encoding.encode(original)
assertContentEquals(encoded, actual)
}