implement Base8 encoding, rename Binary and Hex encodings

This commit is contained in:
2025-11-18 11:21:46 +01:00
parent 52a1ca6553
commit 12344283f7
7 changed files with 38 additions and 67 deletions

View File

@@ -1,6 +1,6 @@
package com.infendro.encoding package com.infendro.encoding
object Hex : Encoding() { object Base16 : Encoding() {
private val CHARACTERS = ubyteArrayOf( private val CHARACTERS = ubyteArrayOf(
0x30U, 0x31U, 0x32U, 0x33U, 0x34U, 0x35U, 0x36U, 0x37U, 0x30U, 0x31U, 0x32U, 0x33U, 0x34U, 0x35U, 0x36U, 0x37U,
0x38U, 0x39U, 0x61U, 0x62U, 0x63U, 0x64U, 0x65U, 0x66U, 0x38U, 0x39U, 0x61U, 0x62U, 0x63U, 0x64U, 0x65U, 0x66U,
@@ -8,19 +8,9 @@ object Hex : Encoding() {
override fun encode( override fun encode(
bytes: UByteArray, bytes: UByteArray,
): UByteArray { ) = encode(bytes, CHARACTERS)
return encode(
bytes,
CHARACTERS,
)
}
override fun decode( override fun decode(
bytes: UByteArray, bytes: UByteArray,
): UByteArray { ) = decode(bytes, CHARACTERS)
return decode(
bytes,
CHARACTERS,
)
}
} }

View File

@@ -0,0 +1,15 @@
package com.infendro.encoding
object Base2 : Encoding() {
private val CHARACTERS = ubyteArrayOf(
0x30U, 0x31U,
)
override fun encode(
bytes: UByteArray,
) = encode(bytes, CHARACTERS)
override fun decode(
bytes: UByteArray,
) = decode(bytes, CHARACTERS)
}

View File

@@ -11,21 +11,9 @@ object Base32 : Encoding() {
override fun encode( override fun encode(
bytes: UByteArray, bytes: UByteArray,
): UByteArray { ) = encode(bytes, CHARACTERS, PADDING)
return encode(
bytes,
CHARACTERS,
PADDING,
)
}
override fun decode( override fun decode(
bytes: UByteArray, bytes: UByteArray,
): UByteArray { ) = decode(bytes, CHARACTERS, PADDING)
return decode(
bytes,
CHARACTERS,
PADDING,
)
}
} }

View File

@@ -15,21 +15,9 @@ object Base64 : Encoding() {
override fun encode( override fun encode(
bytes: UByteArray, bytes: UByteArray,
): UByteArray { ) = encode(bytes, CHARACTERS, PADDING)
return encode(
bytes,
CHARACTERS,
PADDING,
)
}
override fun decode( override fun decode(
bytes: UByteArray, bytes: UByteArray,
): UByteArray { ) = decode(bytes, CHARACTERS, PADDING)
return decode(
bytes,
CHARACTERS,
PADDING,
)
}
} }

View File

@@ -0,0 +1,15 @@
package com.infendro.encoding
object Base8 : Encoding() {
private val CHARACTERS = ubyteArrayOf(
0x30U, 0x31U, 0x32U, 0x33U, 0x34U, 0x35U, 0x36U, 0x37U,
)
override fun encode(
bytes: UByteArray,
) = encode(bytes, CHARACTERS)
override fun decode(
bytes: UByteArray,
) = decode(bytes, CHARACTERS)
}

View File

@@ -1,25 +0,0 @@
package com.infendro.encoding
object Binary : Encoding() {
private val CHARACTERS = ubyteArrayOf(
0x30U, 0x31U,
)
override fun encode(
bytes: UByteArray,
): UByteArray {
return encode(
bytes,
CHARACTERS,
)
}
override fun decode(
bytes: UByteArray,
): UByteArray {
return decode(
bytes,
CHARACTERS,
)
}
}

View File

@@ -61,7 +61,7 @@ sealed class Encoding {
padding: UByte? = null, padding: UByte? = null,
): UByteArray { ): UByteArray {
val length = log2(characters.size).toInt() val length = log2(characters.size).toInt()
val mask = 0xFFU val mask = 0xffU
val decoded = mutableListOf<UByte>() val decoded = mutableListOf<UByte>()