implement Base8 encoding, rename Binary and Hex encodings
This commit is contained in:
@@ -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,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
15
src/commonMain/kotlin/com/infendro/encoding/Base2.kt
Normal file
15
src/commonMain/kotlin/com/infendro/encoding/Base2.kt
Normal 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)
|
||||||
|
}
|
||||||
@@ -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,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
15
src/commonMain/kotlin/com/infendro/encoding/Base8.kt
Normal file
15
src/commonMain/kotlin/com/infendro/encoding/Base8.kt
Normal 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)
|
||||||
|
}
|
||||||
@@ -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,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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>()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user