refactor encodings

This commit is contained in:
2025-11-10 18:59:53 +01:00
parent 08a79efa04
commit 71a47af43c
5 changed files with 55 additions and 55 deletions

View File

@@ -1,17 +1,17 @@
package com.infendro.encoding
object Base32 : Encoding() {
private val CHARACTERS = arrayOf<Byte>(
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50,
0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
0x59, 0x5a, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
private val CHARACTERS = ubyteArrayOf(
0x41U, 0x42U, 0x43U, 0x44U, 0x45U, 0x46U, 0x47U, 0x48U,
0x49U, 0x4aU, 0x4bU, 0x4cU, 0x4dU, 0x4eU, 0x4fU, 0x50U,
0x51U, 0x52U, 0x53U, 0x54U, 0x55U, 0x56U, 0x57U, 0x58U,
0x59U, 0x5aU, 0x32U, 0x33U, 0x34U, 0x35U, 0x36U, 0x37U,
)
private const val PADDING: Byte = 0x3d
private const val PADDING: UByte = 0x3dU
override fun encode(
bytes: ByteArray,
): ByteArray {
bytes: UByteArray,
): UByteArray {
return encode(
bytes,
CHARACTERS,
@@ -20,8 +20,8 @@ object Base32 : Encoding() {
}
override fun decode(
bytes: ByteArray,
): ByteArray {
bytes: UByteArray,
): UByteArray {
return decode(
bytes,
CHARACTERS,

View File

@@ -1,21 +1,21 @@
package com.infendro.encoding
object Base64 : Encoding() {
private val CHARACTERS = arrayOf<Byte>(
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50,
0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
0x59, 0x5a, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e,
0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
0x77, 0x78, 0x79, 0x7a, 0x30, 0x31, 0x32, 0x33,
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2b, 0x2f,
private val CHARACTERS = ubyteArrayOf(
0x41U, 0x42U, 0x43U, 0x44U, 0x45U, 0x46U, 0x47U, 0x48U,
0x49U, 0x4aU, 0x4bU, 0x4cU, 0x4dU, 0x4eU, 0x4fU, 0x50U,
0x51U, 0x52U, 0x53U, 0x54U, 0x55U, 0x56U, 0x57U, 0x58U,
0x59U, 0x5aU, 0x61U, 0x62U, 0x63U, 0x64U, 0x65U, 0x66U,
0x67U, 0x68U, 0x69U, 0x6aU, 0x6bU, 0x6cU, 0x6dU, 0x6eU,
0x6fU, 0x70U, 0x71U, 0x72U, 0x73U, 0x74U, 0x75U, 0x76U,
0x77U, 0x78U, 0x79U, 0x7aU, 0x30U, 0x31U, 0x32U, 0x33U,
0x34U, 0x35U, 0x36U, 0x37U, 0x38U, 0x39U, 0x2bU, 0x2fU,
)
private const val PADDING: Byte = 0x3d
private const val PADDING: UByte = 0x3dU
override fun encode(
bytes: ByteArray,
): ByteArray {
bytes: UByteArray,
): UByteArray {
return encode(
bytes,
CHARACTERS,
@@ -24,8 +24,8 @@ object Base64 : Encoding() {
}
override fun decode(
bytes: ByteArray,
): ByteArray {
bytes: UByteArray,
): UByteArray {
return decode(
bytes,
CHARACTERS,

View File

@@ -1,13 +1,13 @@
package com.infendro.encoding
object Binary : Encoding() {
private val CHARACTERS = arrayOf<Byte>(
0x30, 0x31,
private val CHARACTERS = ubyteArrayOf(
0x30U, 0x31U,
)
override fun encode(
bytes: ByteArray,
): ByteArray {
bytes: UByteArray,
): UByteArray {
return encode(
bytes,
CHARACTERS,
@@ -15,8 +15,8 @@ object Binary : Encoding() {
}
override fun decode(
bytes: ByteArray,
): ByteArray {
bytes: UByteArray,
): UByteArray {
return decode(
bytes,
CHARACTERS,

View File

@@ -6,28 +6,28 @@ import kotlin.math.pow
sealed class Encoding {
abstract fun encode(
bytes: ByteArray,
): ByteArray
bytes: UByteArray,
): UByteArray
abstract fun decode(
bytes: ByteArray,
): ByteArray
bytes: UByteArray,
): UByteArray
protected fun encode(
bytes: ByteArray,
characters: Array<Byte>,
padding: Byte? = null,
): ByteArray {
bytes: UByteArray,
characters: UByteArray,
padding: UByte? = null,
): UByteArray {
val length = log2(characters.size).toInt()
val mask = (2.0.pow(length) - 1).toUInt()
val encoded = mutableListOf<Byte>()
val encoded = mutableListOf<UByte>()
var buffer = 0U
var bits = 0
for (byte in bytes) {
buffer = (buffer shl 8) + byte.toUByte()
buffer = (buffer shl 8) + byte
bits += 8
while (bits >= length) {
@@ -52,18 +52,18 @@ sealed class Encoding {
}
}
return encoded.toByteArray()
return encoded.toUByteArray()
}
protected fun decode(
bytes: ByteArray,
characters: Array<Byte>,
padding: Byte? = null,
): ByteArray {
bytes: UByteArray,
characters: UByteArray,
padding: UByte? = null,
): UByteArray {
val length = log2(characters.size).toInt()
val mask = 0xFFU
val decoded = mutableListOf<Byte>()
val decoded = mutableListOf<UByte>()
var buffer = 0U
var bits = 0
@@ -79,7 +79,7 @@ sealed class Encoding {
if (bits >= 8) {
val offset = bits - 8
val character = (buffer shr offset).toByte()
val character = (buffer shr offset).toUByte()
buffer = buffer and (mask shl offset).inv()
bits -= 8
@@ -87,6 +87,6 @@ sealed class Encoding {
}
}
return decoded.toByteArray()
return decoded.toUByteArray()
}
}

View File

@@ -1,14 +1,14 @@
package com.infendro.encoding
object Hex : Encoding() {
private val CHARACTERS = arrayOf<Byte>(
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
private val CHARACTERS = ubyteArrayOf(
0x30U, 0x31U, 0x32U, 0x33U, 0x34U, 0x35U, 0x36U, 0x37U,
0x38U, 0x39U, 0x61U, 0x62U, 0x63U, 0x64U, 0x65U, 0x66U,
)
override fun encode(
bytes: ByteArray,
): ByteArray {
bytes: UByteArray,
): UByteArray {
return encode(
bytes,
CHARACTERS,
@@ -16,8 +16,8 @@ object Hex : Encoding() {
}
override fun decode(
bytes: ByteArray,
): ByteArray {
bytes: UByteArray,
): UByteArray {
return decode(
bytes,
CHARACTERS,