From eb0c51b3cf0b1aeb3a2fe747c57cbd5f624f5744 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 4 Jan 2020 15:31:53 +0100 Subject: [PATCH] =?utf8?q?=F0=9F=8E=A8=20Add=20helpers=20for=20Freenet?= =?utf8?q?=E2=80=99s=20base64=20implementation?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../net/pterodactylus/sone/freenet/FreenetURIs.kt | 4 ++-- .../net/pterodactylus/sone/text/SoneTextParser.kt | 9 ++++----- .../kotlin/net/pterodactylus/sone/utils/Freenet.kt | 23 ++++++++++++++++++++++ .../sone/core/FreenetInterfaceTest.kt | 4 ++-- 4 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 src/main/kotlin/net/pterodactylus/sone/utils/Freenet.kt diff --git a/src/main/kotlin/net/pterodactylus/sone/freenet/FreenetURIs.kt b/src/main/kotlin/net/pterodactylus/sone/freenet/FreenetURIs.kt index 3a6d43c..6d40b17 100644 --- a/src/main/kotlin/net/pterodactylus/sone/freenet/FreenetURIs.kt +++ b/src/main/kotlin/net/pterodactylus/sone/freenet/FreenetURIs.kt @@ -1,6 +1,6 @@ package net.pterodactylus.sone.freenet import freenet.keys.* -import freenet.support.Base64.* +import net.pterodactylus.sone.utils.* -val FreenetURI.routingKeyString: String get() = encode(routingKey) +val FreenetURI.routingKeyString: String get() = routingKey.asFreenetBase64 diff --git a/src/main/kotlin/net/pterodactylus/sone/text/SoneTextParser.kt b/src/main/kotlin/net/pterodactylus/sone/text/SoneTextParser.kt index 554c19b..b711ca8 100644 --- a/src/main/kotlin/net/pterodactylus/sone/text/SoneTextParser.kt +++ b/src/main/kotlin/net/pterodactylus/sone/text/SoneTextParser.kt @@ -7,6 +7,7 @@ import net.pterodactylus.sone.data.impl.* import net.pterodactylus.sone.database.* import net.pterodactylus.sone.text.LinkType.* import net.pterodactylus.sone.text.LinkType.USK +import net.pterodactylus.sone.utils.* import org.bitpedia.util.* import java.net.* import javax.inject.* @@ -71,7 +72,7 @@ class SoneTextParser @Inject constructor(private val soneProvider: SoneProvider? ?.takeIf { (it.size > 1) || ((it.size == 1) && (it.single() != "")) } ?.lastOrNull() ?: uri.docName - ?: "${uri.keyType}@${uri.routingKey.freenetBase64}" + ?: "${uri.keyType}@${uri.routingKey.asFreenetBase64}" }.let { FreenetLinkPart(linkWithoutBacklink.removeSuffix("/"), it, trusted = context?.routingKey?.contentEquals(FreenetURI(linkWithoutBacklink).routingKey) == true) } } catch (e: MalformedURLException) { PlainTextPart(linkWithoutBacklink) @@ -115,7 +116,7 @@ private fun List.mergeAdjacentPlainTextParts() = fold(emptyList()) { private fun List.removeEmptyPlainTextParts() = filterNot { it == PlainTextPart("") } -private val String.decodedId: String get() = Base64.encode(Base32.decode(this)) +private val String.decodedId: String get() = Base32.decode(this).asFreenetBase64 private val String.withoutProtocol get() = substring(indexOf("//") + 2) private val String.withoutUrlParameters get() = split('?').first() @@ -138,7 +139,7 @@ private val String.withoutMiddlePathComponents } private val String.withoutTrailingSlash get() = if (endsWith("/")) substring(0, length - 1) else this private val SoneTextParserContext.routingKey: ByteArray? get() = postingSone?.routingKey -private val Sone.routingKey: ByteArray get() = Base64.decode(id) +private val Sone.routingKey: ByteArray get() = id.fromFreenetBase64 private enum class LinkType(private val scheme: String, private val freenetLink: Boolean) { @@ -199,5 +200,3 @@ private fun isPunctuation(char: Char) = char in punctuationChars private val whitespace = Regex("[\\u000a\u0020\u00a0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u200c\u200d\u202f\u205f\u2060\u2800\u3000]") private data class NextLink(val position: Int, val linkType: LinkType, val link: String, val remainder: String) - -private val ByteArray.freenetBase64 get() = Base64.encode(this)!! diff --git a/src/main/kotlin/net/pterodactylus/sone/utils/Freenet.kt b/src/main/kotlin/net/pterodactylus/sone/utils/Freenet.kt new file mode 100644 index 0000000..a98117f --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/utils/Freenet.kt @@ -0,0 +1,23 @@ +/** + * Sone - Freenet.kt - Copyright © 2019 David ‘Bombe’ Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.utils + +import freenet.support.* + +val ByteArray.asFreenetBase64: String get() = Base64.encode(this) +val String.fromFreenetBase64: ByteArray get() = Base64.decode(this) diff --git a/src/test/kotlin/net/pterodactylus/sone/core/FreenetInterfaceTest.kt b/src/test/kotlin/net/pterodactylus/sone/core/FreenetInterfaceTest.kt index 6647434..e02cddb 100644 --- a/src/test/kotlin/net/pterodactylus/sone/core/FreenetInterfaceTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/core/FreenetInterfaceTest.kt @@ -10,7 +10,6 @@ import freenet.keys.* import freenet.keys.InsertableClientSSK.* import freenet.node.* import freenet.node.RequestStarter.* -import freenet.support.Base64 import freenet.support.api.* import freenet.support.io.* import net.pterodactylus.sone.core.FreenetInterface.* @@ -20,6 +19,7 @@ import net.pterodactylus.sone.data.impl.* import net.pterodactylus.sone.test.* import net.pterodactylus.sone.test.Matchers.* import net.pterodactylus.sone.test.TestUtil.* +import net.pterodactylus.sone.utils.* import org.hamcrest.MatcherAssert.* import org.hamcrest.Matchers.equalTo import org.hamcrest.Matchers.notNullValue @@ -82,7 +82,7 @@ class FreenetInterfaceTest { @Before fun setupSone() { val insertSsk = createRandom(randomSource, "test-0") - whenever(sone.id).thenReturn(Base64.encode(insertSsk.uri.routingKey)) + whenever(sone.id).thenReturn(insertSsk.uri.routingKey.asFreenetBase64) whenever(sone.requestUri).thenReturn(insertSsk.uri.uskForSSK()) } -- 2.7.4