Commit 098dc655 authored by Kevin Cianfarini's avatar Kevin Cianfarini

Use BigInteger for BencodedInt

The bencoding specification defines BencodedInt as any base 10 ASCII text
representation of an integer. This can be arbitrarily large, and should not
be limited to the storage capacities of `Int` or `Long`.

Therefore, we include a multiplatform big number library.

Closes #1
parent 913cf92f
Pipeline #161288535 passed with stages
in 6 minutes and 14 seconds
......@@ -7,6 +7,9 @@ version = "unspecified"
repositories {
jcenter()
maven {
url = uri("https://oss.sonatype.org/content/repositories/snapshots") // TODO remove with Kotlin 1.4 release of BigNum
}
}
kotlin {
......@@ -17,12 +20,14 @@ kotlin {
all {
languageSettings.useExperimentalAnnotation("kotlin.ExperimentalStdlibApi")
languageSettings.useExperimentalAnnotation("kotlin.ExperimentalUnsignedTypes")
}
val commonMain by getting {
dependencies {
implementation(kotlin("stdlib-common"))
implementation(project(":corbit-binary"))
implementation("com.ionspin.kotlin:bignum:0.1.6-1.3.72-SNAPSHOT")
}
}
val commonTest by getting {
......
package com.corbit.bencoding
import com.corbit.binary.BinaryString
import com.ionspin.kotlin.bignum.integer.BigInteger
sealed class BencodedData {
fun encode(): BinaryString = Encoder(this).encode()
}
data class BencodedInt(val value: Long) : BencodedData()
data class BencodedInt(val value: BigInteger) : BencodedData()
data class BencodedString(val value: BinaryString) : BencodedData()
......
......@@ -2,6 +2,8 @@ package com.corbit.bencoding
import com.corbit.binary.BinaryString
import com.corbit.binary.slice
import com.ionspin.kotlin.bignum.integer.BigInteger
import com.ionspin.kotlin.bignum.integer.toBigInteger
internal class Decoder(private val source: BinaryString) {
......@@ -34,8 +36,8 @@ internal class Decoder(private val source: BinaryString) {
}
private fun decodeInteger(): BencodedInt {
val value: Long = readToNextToken(BencoderToken.END).run {
slice(1, size - 2).utf8.toLong()
val value: BigInteger = readToNextToken(BencoderToken.END).run {
slice(1, size - 2).utf8.toBigInteger()
}
return BencodedInt(value)
......
package com.corbit.bencoding
import com.corbit.binary.asBinaryString
import com.ionspin.kotlin.bignum.integer.toBigInteger
import kotlin.test.Test
import kotlin.test.assertEquals
......@@ -14,7 +15,7 @@ class DecoderTest {
val result = data.decode()
// assert
assertEquals(123, (result as BencodedInt).value)
assertEquals(123.toBigInteger(), (result as BencodedInt).value)
}
@Test fun `simple bencoded string can be decoded`() {
......@@ -79,9 +80,9 @@ class DecoderTest {
// assert
val expected = listOf(
BencodedInt(123),
BencodedInt(123.toBigInteger()),
BencodedString("spam".asBinaryString()),
BencodedInt(12),
BencodedInt(12.toBigInteger()),
BencodedString("eggs".asBinaryString())
)
assertEquals(
......@@ -100,8 +101,8 @@ class DecoderTest {
// assert
assertEquals(
BencodedList(listOf(
BencodedList(listOf(BencodedInt(123))),
BencodedList(listOf(BencodedInt(123)))
BencodedList(listOf(BencodedInt(123.toBigInteger()))),
BencodedList(listOf(BencodedInt(123.toBigInteger())))
)),
result
)
......
......@@ -2,6 +2,7 @@ package com.corbit.bencoding
import com.corbit.binary.BinaryString
import com.corbit.binary.asBinaryString
import com.ionspin.kotlin.bignum.integer.toBigInteger
import kotlin.test.Test
import kotlin.test.assertEquals
......@@ -9,7 +10,7 @@ class EncoderTest {
@Test fun `bencoded int can properly be encoded to binary data`() {
// arrange
val data = BencodedInt(123)
val data = BencodedInt(123.toBigInteger())
// act
val result = data.encode()
......@@ -66,7 +67,7 @@ class EncoderTest {
@Test fun `trivial bencoded list is encoded to proper binary data`() {
// arrange
val data = BencodedList(listOf(
BencodedInt(123),
BencodedInt(123.toBigInteger()),
BencodedString("eggs".asBinaryString())
))
......@@ -84,7 +85,7 @@ class EncoderTest {
// arrange
val data = BencodedList(listOf(
BencodedList(listOf(
BencodedInt(123),
BencodedInt(123.toBigInteger()),
BencodedString("eggs".asBinaryString())
))
))
......@@ -116,7 +117,7 @@ class EncoderTest {
@Test fun `trivial bencoded dictionary is encoded to proper binary data`() {
// arrange
val data = BencodedDict(mapOf(
BencodedString("foo".asBinaryString()) to BencodedInt(123),
BencodedString("foo".asBinaryString()) to BencodedInt(123.toBigInteger()),
BencodedString("bar".asBinaryString()) to BencodedString("baz".asBinaryString())
))
......@@ -134,7 +135,7 @@ class EncoderTest {
// arrange
val data = BencodedDict(mapOf(
BencodedString("map".asBinaryString()) to BencodedDict(mapOf(
BencodedString("foo".asBinaryString()) to BencodedInt(123),
BencodedString("foo".asBinaryString()) to BencodedInt(123.toBigInteger()),
BencodedString("bar".asBinaryString()) to BencodedString("baz".asBinaryString())
))
))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment