31
src/commonMain/kotlin/com/infendro/encoding/Base32.kt
Normal file
31
src/commonMain/kotlin/com/infendro/encoding/Base32.kt
Normal file
@@ -0,0 +1,31 @@
|
||||
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 const val PADDING: Byte = 0x3d
|
||||
|
||||
override fun encode(
|
||||
bytes: ByteArray,
|
||||
): ByteArray {
|
||||
return encode(
|
||||
bytes,
|
||||
CHARACTERS,
|
||||
PADDING,
|
||||
)
|
||||
}
|
||||
|
||||
override fun decode(
|
||||
bytes: ByteArray,
|
||||
): ByteArray {
|
||||
return decode(
|
||||
bytes,
|
||||
CHARACTERS,
|
||||
PADDING,
|
||||
)
|
||||
}
|
||||
}
|
||||
35
src/commonMain/kotlin/com/infendro/encoding/Base64.kt
Normal file
35
src/commonMain/kotlin/com/infendro/encoding/Base64.kt
Normal file
@@ -0,0 +1,35 @@
|
||||
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 const val PADDING: Byte = 0x3d
|
||||
|
||||
override fun encode(
|
||||
bytes: ByteArray,
|
||||
): ByteArray {
|
||||
return encode(
|
||||
bytes,
|
||||
CHARACTERS,
|
||||
PADDING,
|
||||
)
|
||||
}
|
||||
|
||||
override fun decode(
|
||||
bytes: ByteArray,
|
||||
): ByteArray {
|
||||
return decode(
|
||||
bytes,
|
||||
CHARACTERS,
|
||||
PADDING,
|
||||
)
|
||||
}
|
||||
}
|
||||
25
src/commonMain/kotlin/com/infendro/encoding/Binary.kt
Normal file
25
src/commonMain/kotlin/com/infendro/encoding/Binary.kt
Normal file
@@ -0,0 +1,25 @@
|
||||
package com.infendro.encoding
|
||||
|
||||
object Binary : Encoding() {
|
||||
private val CHARACTERS = arrayOf<Byte>(
|
||||
0x30, 0x31,
|
||||
)
|
||||
|
||||
override fun encode(
|
||||
bytes: ByteArray,
|
||||
): ByteArray {
|
||||
return encode(
|
||||
bytes,
|
||||
CHARACTERS,
|
||||
)
|
||||
}
|
||||
|
||||
override fun decode(
|
||||
bytes: ByteArray,
|
||||
): ByteArray {
|
||||
return decode(
|
||||
bytes,
|
||||
CHARACTERS,
|
||||
)
|
||||
}
|
||||
}
|
||||
92
src/commonMain/kotlin/com/infendro/encoding/Encoding.kt
Normal file
92
src/commonMain/kotlin/com/infendro/encoding/Encoding.kt
Normal file
@@ -0,0 +1,92 @@
|
||||
package com.infendro.encoding
|
||||
|
||||
import com.infendro.encoding.util.lcm
|
||||
import com.infendro.encoding.util.log2
|
||||
import kotlin.math.pow
|
||||
|
||||
sealed class Encoding {
|
||||
abstract fun encode(
|
||||
bytes: ByteArray,
|
||||
): ByteArray
|
||||
|
||||
abstract fun decode(
|
||||
bytes: ByteArray,
|
||||
): ByteArray
|
||||
|
||||
protected fun encode(
|
||||
bytes: ByteArray,
|
||||
characters: Array<Byte>,
|
||||
padding: Byte? = null,
|
||||
): ByteArray {
|
||||
val length = log2(characters.size).toInt()
|
||||
val mask = (2.0.pow(length) - 1).toUInt()
|
||||
|
||||
val encoded = mutableListOf<Byte>()
|
||||
|
||||
var buffer = 0U
|
||||
var bits = 0
|
||||
|
||||
for (byte in bytes) {
|
||||
buffer = (buffer shl 8) + byte.toUByte()
|
||||
bits += 8
|
||||
|
||||
while (bits >= length) {
|
||||
val offset = bits - length
|
||||
val index = (buffer shr offset).toInt()
|
||||
buffer = buffer and (mask shl offset).inv()
|
||||
bits -= length
|
||||
|
||||
encoded.add(characters[index])
|
||||
}
|
||||
}
|
||||
if (bits > 0) {
|
||||
buffer = buffer shl (length - bits)
|
||||
val index = buffer.toInt()
|
||||
encoded.add(characters[index])
|
||||
}
|
||||
|
||||
if (padding != null) {
|
||||
val block = lcm(length, 8) / length
|
||||
while (encoded.size % block != 0) {
|
||||
encoded.add(padding)
|
||||
}
|
||||
}
|
||||
|
||||
return encoded.toByteArray()
|
||||
}
|
||||
|
||||
protected fun decode(
|
||||
bytes: ByteArray,
|
||||
characters: Array<Byte>,
|
||||
padding: Byte? = null,
|
||||
): ByteArray {
|
||||
val length = log2(characters.size).toInt()
|
||||
val mask = 0xFFU
|
||||
|
||||
val decoded = mutableListOf<Byte>()
|
||||
|
||||
var buffer = 0U
|
||||
var bits = 0
|
||||
|
||||
for (byte in bytes) {
|
||||
if (byte == padding) break
|
||||
|
||||
val index = characters.indexOf(byte)
|
||||
if (index == -1) throw Exception()
|
||||
|
||||
buffer = (buffer shl length) + index.toUByte()
|
||||
bits += length
|
||||
|
||||
if (bits >= 8) {
|
||||
val offset = bits - 8
|
||||
val character = (buffer shr offset).toByte()
|
||||
buffer = buffer and (mask shl offset).inv()
|
||||
bits -= 8
|
||||
|
||||
decoded.add(character)
|
||||
}
|
||||
}
|
||||
|
||||
return decoded.toByteArray()
|
||||
}
|
||||
}
|
||||
26
src/commonMain/kotlin/com/infendro/encoding/Hex.kt
Normal file
26
src/commonMain/kotlin/com/infendro/encoding/Hex.kt
Normal file
@@ -0,0 +1,26 @@
|
||||
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,
|
||||
)
|
||||
|
||||
override fun encode(
|
||||
bytes: ByteArray,
|
||||
): ByteArray {
|
||||
return encode(
|
||||
bytes,
|
||||
CHARACTERS,
|
||||
)
|
||||
}
|
||||
|
||||
override fun decode(
|
||||
bytes: ByteArray,
|
||||
): ByteArray {
|
||||
return decode(
|
||||
bytes,
|
||||
CHARACTERS,
|
||||
)
|
||||
}
|
||||
}
|
||||
11
src/commonMain/kotlin/com/infendro/encoding/Main.kt
Normal file
11
src/commonMain/kotlin/com/infendro/encoding/Main.kt
Normal file
@@ -0,0 +1,11 @@
|
||||
package com.infendro.encoding
|
||||
|
||||
fun main() {
|
||||
val value = "ABC"
|
||||
val a0 = Binary.encode(value.encodeToByteArray())
|
||||
val a1 = Binary.decode(a0)
|
||||
|
||||
println(value)
|
||||
println(a0.decodeToString())
|
||||
println(a1.decodeToString())
|
||||
}
|
||||
31
src/commonMain/kotlin/com/infendro/encoding/util/Math.kt
Normal file
31
src/commonMain/kotlin/com/infendro/encoding/util/Math.kt
Normal file
@@ -0,0 +1,31 @@
|
||||
package com.infendro.encoding.util
|
||||
|
||||
import kotlin.math.log2
|
||||
|
||||
internal fun log2(
|
||||
value: Int,
|
||||
): Double {
|
||||
val x = value.toDouble()
|
||||
return log2(x)
|
||||
}
|
||||
|
||||
internal fun gcd(
|
||||
a: Int,
|
||||
b: Int,
|
||||
): Int {
|
||||
var x = a
|
||||
var y = b
|
||||
while (y != 0) {
|
||||
val temp = y
|
||||
y = x % y
|
||||
x = temp
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
||||
internal fun lcm(
|
||||
a: Int,
|
||||
b: Int,
|
||||
): Int {
|
||||
return (a * b) / gcd(a, b)
|
||||
}
|
||||
Reference in New Issue
Block a user