All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.dev.datlag.jsunpacker.Unbaser.kt Maven / Gradle / Ivy

There is a newer version: 1.0.2
Show newest version
package dev.datlag.jsunpacker

import kotlin.math.pow

internal data class Unbaser(
    private val base: Int
) {
    private val selector: Int = when {
        base > 62 -> 95
        base > 54 -> 62
        base > 52 -> 54
        else -> 52
    }

    fun unbase(value: String): Int {
        return if (base in 2..36) {
            value.toIntOrNull() ?: 0
        } else {
            val dict = ALPHABET[selector]?.toCharArray()?.mapIndexed { index, c ->
                c to index
            }?.toMap()
            var returnVal = 0
            val valArray = value.toCharArray().reversed()

            for (i in valArray.indices) {
                val cipher = valArray[i]
                returnVal += (base.toFloat().pow(i) * (dict?.get(cipher) ?: 0)).toInt()
            }
            returnVal
        }
    }

    companion object {
        private val ALPHABET = mapOf(
            52 to "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP",
            54 to "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQR",
            62 to "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
            95 to " !\"#\$%&\\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
        )
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy