diff --git a/core/src/main/kotlin/ch/dissem/bitmessage/entity/BitmessageAddress.kt b/core/src/main/kotlin/ch/dissem/bitmessage/entity/BitmessageAddress.kt index c0e978b..87ee0cd 100644 --- a/core/src/main/kotlin/ch/dissem/bitmessage/entity/BitmessageAddress.kt +++ b/core/src/main/kotlin/ch/dissem/bitmessage/entity/BitmessageAddress.kt @@ -116,15 +116,15 @@ class BitmessageAddress : Serializable { constructor(address: String) { this.address = address val bytes = Base58.decode(address.substring(3)) - val `in` = ByteArrayInputStream(bytes) + val input = ByteArrayInputStream(bytes) val counter = AccessCounter() - this.version = varInt(`in`, counter) - this.stream = varInt(`in`, counter) - this.ripe = Bytes.expand(bytes(`in`, bytes.size - counter.length() - 4), 20) + this.version = varInt(input, counter) + this.stream = varInt(input, counter) + this.ripe = Bytes.expand(bytes(input, bytes.size - counter.length() - 4), 20) // test checksum var checksum = cryptography().doubleSha512(bytes, bytes.size - 4) - val expectedChecksum = bytes(`in`, 4) + val expectedChecksum = bytes(input, 4) for (i in 0..3) { if (expectedChecksum[i] != checksum[i]) throw IllegalArgumentException("Checksum of address failed") diff --git a/core/src/main/kotlin/ch/dissem/bitmessage/entity/CustomMessage.kt b/core/src/main/kotlin/ch/dissem/bitmessage/entity/CustomMessage.kt index 1186a18..0e925d9 100644 --- a/core/src/main/kotlin/ch/dissem/bitmessage/entity/CustomMessage.kt +++ b/core/src/main/kotlin/ch/dissem/bitmessage/entity/CustomMessage.kt @@ -75,9 +75,9 @@ open class CustomMessage(val customCommand: String, private val data: ByteArray? val COMMAND_ERROR = "ERROR" @JvmStatic - fun read(`in`: InputStream, length: Int): CustomMessage { + fun read(input: InputStream, length: Int): CustomMessage { val counter = AccessCounter() - return CustomMessage(varString(`in`, counter), bytes(`in`, length - counter.length())) + return CustomMessage(varString(input, counter), bytes(input, length - counter.length())) } @JvmStatic diff --git a/core/src/main/kotlin/ch/dissem/bitmessage/entity/Plaintext.kt b/core/src/main/kotlin/ch/dissem/bitmessage/entity/Plaintext.kt index 9797990..236d581 100644 --- a/core/src/main/kotlin/ch/dissem/bitmessage/entity/Plaintext.kt +++ b/core/src/main/kotlin/ch/dissem/bitmessage/entity/Plaintext.kt @@ -751,28 +751,28 @@ class Plaintext private constructor( companion object { @JvmStatic - fun read(type: Type, `in`: InputStream): Plaintext { - return readWithoutSignature(type, `in`) - .signature(Decode.varBytes(`in`)) + fun read(type: Type, input: InputStream): Plaintext { + return readWithoutSignature(type, input) + .signature(Decode.varBytes(input)) .received(UnixTime.now) .build() } @JvmStatic - fun readWithoutSignature(type: Type, `in`: InputStream): Plaintext.Builder { - val version = Decode.varInt(`in`) + fun readWithoutSignature(type: Type, input: InputStream): Plaintext.Builder { + val version = Decode.varInt(input) return Builder(type) .addressVersion(version) - .stream(Decode.varInt(`in`)) - .behaviorBitfield(Decode.int32(`in`)) - .publicSigningKey(Decode.bytes(`in`, 64)) - .publicEncryptionKey(Decode.bytes(`in`, 64)) - .nonceTrialsPerByte(if (version >= 3) Decode.varInt(`in`) else 0) - .extraBytes(if (version >= 3) Decode.varInt(`in`) else 0) - .destinationRipe(if (type == MSG) Decode.bytes(`in`, 20) else null) - .encoding(Decode.varInt(`in`)) - .message(Decode.varBytes(`in`)) - .ackMessage(if (type == MSG) Decode.varBytes(`in`) else null) + .stream(Decode.varInt(input)) + .behaviorBitfield(Decode.int32(input)) + .publicSigningKey(Decode.bytes(input, 64)) + .publicEncryptionKey(Decode.bytes(input, 64)) + .nonceTrialsPerByte(if (version >= 3) Decode.varInt(input) else 0) + .extraBytes(if (version >= 3) Decode.varInt(input) else 0) + .destinationRipe(if (type == MSG) Decode.bytes(input, 20) else null) + .encoding(Decode.varInt(input)) + .message(Decode.varBytes(input)) + .ackMessage(if (type == MSG) Decode.varBytes(input) else null) } } } diff --git a/core/src/main/kotlin/ch/dissem/bitmessage/entity/payload/GenericPayload.kt b/core/src/main/kotlin/ch/dissem/bitmessage/entity/payload/GenericPayload.kt index 1ccd680..41e071f 100644 --- a/core/src/main/kotlin/ch/dissem/bitmessage/entity/payload/GenericPayload.kt +++ b/core/src/main/kotlin/ch/dissem/bitmessage/entity/payload/GenericPayload.kt @@ -65,7 +65,7 @@ class GenericPayload(version: Long, override val stream: Long, val data: ByteArr companion object { @JvmStatic - fun read(version: Long, stream: Long, `is`: InputStream, length: Int) = - GenericPayload(version, stream, Decode.bytes(`is`, length)) + fun read(version: Long, stream: Long, input: InputStream, length: Int) = + GenericPayload(version, stream, Decode.bytes(input, length)) } } diff --git a/core/src/main/kotlin/ch/dissem/bitmessage/entity/payload/GetPubkey.kt b/core/src/main/kotlin/ch/dissem/bitmessage/entity/payload/GetPubkey.kt index 893c36f..252abd9 100644 --- a/core/src/main/kotlin/ch/dissem/bitmessage/entity/payload/GetPubkey.kt +++ b/core/src/main/kotlin/ch/dissem/bitmessage/entity/payload/GetPubkey.kt @@ -68,8 +68,8 @@ class GetPubkey : ObjectPayload { companion object { @JvmStatic - fun read(`is`: InputStream, stream: Long, length: Int, version: Long): GetPubkey { - return GetPubkey(version, stream, Decode.bytes(`is`, length)) + fun read(input: InputStream, stream: Long, length: Int, version: Long): GetPubkey { + return GetPubkey(version, stream, Decode.bytes(input, length)) } } } diff --git a/core/src/main/kotlin/ch/dissem/bitmessage/entity/payload/Msg.kt b/core/src/main/kotlin/ch/dissem/bitmessage/entity/payload/Msg.kt index f8a4314..a26c94b 100644 --- a/core/src/main/kotlin/ch/dissem/bitmessage/entity/payload/Msg.kt +++ b/core/src/main/kotlin/ch/dissem/bitmessage/entity/payload/Msg.kt @@ -16,8 +16,11 @@ package ch.dissem.bitmessage.entity.payload -import ch.dissem.bitmessage.entity.* +import ch.dissem.bitmessage.entity.Encrypted +import ch.dissem.bitmessage.entity.Plaintext import ch.dissem.bitmessage.entity.Plaintext.Type.MSG +import ch.dissem.bitmessage.entity.PlaintextHolder +import ch.dissem.bitmessage.entity.SignedStreamableWriter import ch.dissem.bitmessage.exception.DecryptionFailedException import java.io.InputStream import java.io.OutputStream @@ -103,8 +106,6 @@ class Msg : ObjectPayload, Encrypted, PlaintextHolder { val ACK_LENGTH = 32 @JvmStatic - fun read(`in`: InputStream, stream: Long, length: Int): Msg { - return Msg(stream, CryptoBox.read(`in`, length)) - } + fun read(input: InputStream, stream: Long, length: Int) = Msg(stream, CryptoBox.read(input, length)) } } diff --git a/core/src/main/kotlin/ch/dissem/bitmessage/entity/payload/V2Pubkey.kt b/core/src/main/kotlin/ch/dissem/bitmessage/entity/payload/V2Pubkey.kt index be50b78..1aa3374 100644 --- a/core/src/main/kotlin/ch/dissem/bitmessage/entity/payload/V2Pubkey.kt +++ b/core/src/main/kotlin/ch/dissem/bitmessage/entity/payload/V2Pubkey.kt @@ -107,13 +107,13 @@ open class V2Pubkey constructor( } companion object { - @JvmStatic fun read(`in`: InputStream, stream: Long): V2Pubkey { + @JvmStatic fun read(input: InputStream, stream: Long): V2Pubkey { return V2Pubkey( version = 2, stream = stream, - behaviorBitfield = Decode.uint32(`in`).toInt(), - signingKey = Decode.bytes(`in`, 64), - encryptionKey = Decode.bytes(`in`, 64) + behaviorBitfield = Decode.uint32(input).toInt(), + signingKey = Decode.bytes(input, 64), + encryptionKey = Decode.bytes(input, 64) ) } } diff --git a/core/src/main/kotlin/ch/dissem/bitmessage/entity/payload/V3Pubkey.kt b/core/src/main/kotlin/ch/dissem/bitmessage/entity/payload/V3Pubkey.kt index 3149fa7..92e669c 100644 --- a/core/src/main/kotlin/ch/dissem/bitmessage/entity/payload/V3Pubkey.kt +++ b/core/src/main/kotlin/ch/dissem/bitmessage/entity/payload/V3Pubkey.kt @@ -17,8 +17,6 @@ package ch.dissem.bitmessage.entity.payload import ch.dissem.bitmessage.entity.EncryptedStreamableWriter -import ch.dissem.bitmessage.entity.SignedStreamableWriter -import ch.dissem.bitmessage.entity.StreamableWriter import ch.dissem.bitmessage.utils.Decode import ch.dissem.bitmessage.utils.Encode import java.io.InputStream @@ -144,16 +142,16 @@ class V3Pubkey protected constructor( } companion object { - @JvmStatic fun read(`is`: InputStream, stream: Long): V3Pubkey { + @JvmStatic fun read(input: InputStream, stream: Long): V3Pubkey { return V3Pubkey( version = 3, stream = stream, - behaviorBitfield = Decode.int32(`is`), - signingKey = Decode.bytes(`is`, 64), - encryptionKey = Decode.bytes(`is`, 64), - nonceTrialsPerByte = Decode.varInt(`is`), - extraBytes = Decode.varInt(`is`), - signature = Decode.varBytes(`is`) + behaviorBitfield = Decode.int32(input), + signingKey = Decode.bytes(input, 64), + encryptionKey = Decode.bytes(input, 64), + nonceTrialsPerByte = Decode.varInt(input), + extraBytes = Decode.varInt(input), + signature = Decode.varBytes(input) ) } } diff --git a/core/src/main/kotlin/ch/dissem/bitmessage/entity/payload/V4Broadcast.kt b/core/src/main/kotlin/ch/dissem/bitmessage/entity/payload/V4Broadcast.kt index 6d5b7cf..9a209b8 100644 --- a/core/src/main/kotlin/ch/dissem/bitmessage/entity/payload/V4Broadcast.kt +++ b/core/src/main/kotlin/ch/dissem/bitmessage/entity/payload/V4Broadcast.kt @@ -59,8 +59,7 @@ open class V4Broadcast : Broadcast { companion object { @JvmStatic - fun read(`in`: InputStream, stream: Long, length: Int): V4Broadcast { - return V4Broadcast(4, stream, CryptoBox.read(`in`, length), null) - } + fun read(input: InputStream, stream: Long, length: Int) = + V4Broadcast(4, stream, CryptoBox.read(input, length), null) } } diff --git a/core/src/main/kotlin/ch/dissem/bitmessage/entity/payload/V4Pubkey.kt b/core/src/main/kotlin/ch/dissem/bitmessage/entity/payload/V4Pubkey.kt index a3e744a..cad0518 100644 --- a/core/src/main/kotlin/ch/dissem/bitmessage/entity/payload/V4Pubkey.kt +++ b/core/src/main/kotlin/ch/dissem/bitmessage/entity/payload/V4Pubkey.kt @@ -137,13 +137,12 @@ class V4Pubkey : Pubkey, Encrypted { companion object { @JvmStatic - fun read(`in`: InputStream, stream: Long, length: Int, encrypted: Boolean): V4Pubkey { - if (encrypted) - return V4Pubkey(stream, - Decode.bytes(`in`, 32), - CryptoBox.read(`in`, length - 32)) - else - return V4Pubkey(V3Pubkey.read(`in`, stream)) + fun read(input: InputStream, stream: Long, length: Int, encrypted: Boolean) = if (encrypted) { + V4Pubkey(stream, + Decode.bytes(input, 32), + CryptoBox.read(input, length - 32)) + } else { + V4Pubkey(V3Pubkey.read(input, stream)) } } } diff --git a/core/src/main/kotlin/ch/dissem/bitmessage/entity/payload/V5Broadcast.kt b/core/src/main/kotlin/ch/dissem/bitmessage/entity/payload/V5Broadcast.kt index 4ca18d5..6545632 100644 --- a/core/src/main/kotlin/ch/dissem/bitmessage/entity/payload/V5Broadcast.kt +++ b/core/src/main/kotlin/ch/dissem/bitmessage/entity/payload/V5Broadcast.kt @@ -61,8 +61,7 @@ class V5Broadcast : V4Broadcast { companion object { @JvmStatic - fun read(`is`: InputStream, stream: Long, length: Int): V5Broadcast { - return V5Broadcast(stream, Decode.bytes(`is`, 32), CryptoBox.read(`is`, length - 32)) - } + fun read(input: InputStream, stream: Long, length: Int) = + V5Broadcast(stream, Decode.bytes(input, 32), CryptoBox.read(input, length - 32)) } } diff --git a/core/src/main/kotlin/ch/dissem/bitmessage/entity/valueobject/PrivateKey.kt b/core/src/main/kotlin/ch/dissem/bitmessage/entity/valueobject/PrivateKey.kt index 5b88e02..305703d 100644 --- a/core/src/main/kotlin/ch/dissem/bitmessage/entity/valueobject/PrivateKey.kt +++ b/core/src/main/kotlin/ch/dissem/bitmessage/entity/valueobject/PrivateKey.kt @@ -191,13 +191,13 @@ data class PrivateKey( } @JvmStatic - fun read(`is`: InputStream): PrivateKey { - val version = Decode.varInt(`is`).toInt() - val stream = Decode.varInt(`is`) - val len = Decode.varInt(`is`).toInt() - val pubkey = Factory.readPubkey(version.toLong(), stream, `is`, len, false) ?: throw ApplicationException("Unknown pubkey version encountered") - val signingKey = Decode.varBytes(`is`) - val encryptionKey = Decode.varBytes(`is`) + fun read(input: InputStream): PrivateKey { + val version = Decode.varInt(input).toInt() + val stream = Decode.varInt(input) + val len = Decode.varInt(input).toInt() + val pubkey = Factory.readPubkey(version.toLong(), stream, input, len, false) ?: throw ApplicationException("Unknown pubkey version encountered") + val signingKey = Decode.varBytes(input) + val encryptionKey = Decode.varBytes(input) return PrivateKey(signingKey, encryptionKey, pubkey) } } diff --git a/core/src/main/kotlin/ch/dissem/bitmessage/factory/Factory.kt b/core/src/main/kotlin/ch/dissem/bitmessage/factory/Factory.kt index 9780c4b..8042df7 100644 --- a/core/src/main/kotlin/ch/dissem/bitmessage/factory/Factory.kt +++ b/core/src/main/kotlin/ch/dissem/bitmessage/factory/Factory.kt @@ -156,11 +156,11 @@ object Factory { return GetPubkey.read(stream, streamNumber, length, version) } - @JvmStatic fun readPubkey(version: Long, stream: Long, `is`: InputStream, length: Int, encrypted: Boolean): Pubkey? { + @JvmStatic fun readPubkey(version: Long, stream: Long, input: InputStream, length: Int, encrypted: Boolean): Pubkey? { when (version.toInt()) { - 2 -> return V2Pubkey.read(`is`, stream) - 3 -> return V3Pubkey.read(`is`, stream) - 4 -> return V4Pubkey.read(`is`, stream, length, encrypted) + 2 -> return V2Pubkey.read(input, stream) + 3 -> return V3Pubkey.read(input, stream) + 4 -> return V4Pubkey.read(input, stream, length, encrypted) } LOG.debug("Unexpected pubkey version $version, handling as generic payload object") return null diff --git a/core/src/main/kotlin/ch/dissem/bitmessage/factory/V3MessageFactory.kt b/core/src/main/kotlin/ch/dissem/bitmessage/factory/V3MessageFactory.kt index 57fc49f..3afa82a 100644 --- a/core/src/main/kotlin/ch/dissem/bitmessage/factory/V3MessageFactory.kt +++ b/core/src/main/kotlin/ch/dissem/bitmessage/factory/V3MessageFactory.kt @@ -39,59 +39,52 @@ object V3MessageFactory { private val LOG = LoggerFactory.getLogger(V3MessageFactory::class.java) @JvmStatic - fun read(`in`: InputStream): NetworkMessage? { - findMagic(`in`) - val command = getCommand(`in`) - val length = Decode.uint32(`in`).toInt() + fun read(input: InputStream): NetworkMessage? { + findMagic(input) + val command = getCommand(input) + val length = Decode.uint32(input).toInt() if (length > 1600003) { throw NodeException("Payload of $length bytes received, no more than 1600003 was expected.") } - val checksum = Decode.bytes(`in`, 4) + val checksum = Decode.bytes(input, 4) - val payloadBytes = Decode.bytes(`in`, length) + val payloadBytes = Decode.bytes(input, length) if (testChecksum(checksum, payloadBytes)) { val payload = getPayload(command, ByteArrayInputStream(payloadBytes), length) - if (payload != null) - return NetworkMessage(payload) - else - return null + return payload?.let { NetworkMessage(payload) } } else { throw IOException("Checksum failed for message '$command'") } } @JvmStatic - fun getPayload(command: String, stream: InputStream, length: Int): MessagePayload? { - when (command) { - "version" -> return parseVersion(stream) - "verack" -> return VerAck() - "addr" -> return parseAddr(stream) - "inv" -> return parseInv(stream) - "getdata" -> return parseGetData(stream) - "object" -> return readObject(stream, length) - "custom" -> return readCustom(stream, length) - else -> { - LOG.debug("Unknown command: " + command) - return null - } + fun getPayload(command: String, stream: InputStream, length: Int): MessagePayload? = when (command) { + "version" -> parseVersion(stream) + "verack" -> VerAck() + "addr" -> parseAddr(stream) + "inv" -> parseInv(stream) + "getdata" -> parseGetData(stream) + "object" -> readObject(stream, length) + "custom" -> readCustom(stream, length) + else -> { + LOG.debug("Unknown command: " + command) + null } } - private fun readCustom(`in`: InputStream, length: Int): MessagePayload { - return CustomMessage.read(`in`, length) - } + private fun readCustom(input: InputStream, length: Int): MessagePayload = CustomMessage.read(input, length) @JvmStatic - fun readObject(`in`: InputStream, length: Int): ObjectMessage { + fun readObject(input: InputStream, length: Int): ObjectMessage { val counter = AccessCounter() - val nonce = Decode.bytes(`in`, 8, counter) - val expiresTime = Decode.int64(`in`, counter) - val objectType = Decode.uint32(`in`, counter) - val version = Decode.varInt(`in`, counter) - val stream = Decode.varInt(`in`, counter) + val nonce = Decode.bytes(input, 8, counter) + val expiresTime = Decode.int64(input, counter) + val objectType = Decode.uint32(input, counter) + val version = Decode.varInt(input, counter) + val stream = Decode.varInt(input, counter) - val data = Decode.bytes(`in`, length - counter.length()) + val data = Decode.bytes(input, length - counter.length()) var payload: ObjectPayload try { val dataStream = ByteArrayInputStream(data) @@ -116,7 +109,7 @@ object V3MessageFactory { private fun parseGetData(stream: InputStream): GetData { val count = Decode.varInt(stream) val inventoryVectors = LinkedList() - for (i in 0..count - 1) { + for (i in 0 until count) { inventoryVectors.add(parseInventoryVector(stream)) } return GetData(inventoryVectors) @@ -125,7 +118,7 @@ object V3MessageFactory { private fun parseInv(stream: InputStream): Inv { val count = Decode.varInt(stream) val inventoryVectors = LinkedList() - for (i in 0..count - 1) { + for (i in 0 until count) { inventoryVectors.add(parseInventoryVector(stream)) } return Inv(inventoryVectors) @@ -134,7 +127,7 @@ object V3MessageFactory { private fun parseAddr(stream: InputStream): Addr { val count = Decode.varInt(stream) val networkAddresses = LinkedList() - for (i in 0..count - 1) { + for (i in 0 until count) { networkAddresses.add(parseAddress(stream, false)) } return Addr(networkAddresses) @@ -160,9 +153,7 @@ object V3MessageFactory { .streams(*streamNumbers).build() } - private fun parseInventoryVector(stream: InputStream): InventoryVector { - return InventoryVector(Decode.bytes(stream, 32)) - } + private fun parseInventoryVector(stream: InputStream) = InventoryVector(Decode.bytes(stream, 32)) private fun parseAddress(stream: InputStream, light: Boolean): NetworkAddress { val time: Long @@ -188,12 +179,7 @@ object V3MessageFactory { private fun testChecksum(checksum: ByteArray, payload: ByteArray): Boolean { val payloadChecksum = cryptography().sha512(payload) - for (i in checksum.indices) { - if (checksum[i] != payloadChecksum[i]) { - return false - } - } - return true + return checksum.indices.none { checksum[it] != payloadChecksum[it] } } private fun getCommand(stream: InputStream): String { @@ -210,10 +196,10 @@ object V3MessageFactory { return String(bytes, 0, end, Charsets.US_ASCII) } - private fun findMagic(`in`: InputStream) { + private fun findMagic(input: InputStream) { var pos = 0 for (i in 0..1619999) { - val b = `in`.read().toByte() + val b = input.read().toByte() if (b == NetworkMessage.MAGIC_BYTES[pos]) { if (pos + 1 == NetworkMessage.MAGIC_BYTES.size) { return diff --git a/core/src/main/kotlin/ch/dissem/bitmessage/ports/NodeRegistryHelper.kt b/core/src/main/kotlin/ch/dissem/bitmessage/ports/NodeRegistryHelper.kt index 921218d..80b897b 100644 --- a/core/src/main/kotlin/ch/dissem/bitmessage/ports/NodeRegistryHelper.kt +++ b/core/src/main/kotlin/ch/dissem/bitmessage/ports/NodeRegistryHelper.kt @@ -31,8 +31,8 @@ object NodeRegistryHelper { @JvmStatic fun loadStableNodes(): Map> { - javaClass.classLoader.getResourceAsStream("nodes.txt").use { `in` -> - val scanner = Scanner(`in`) + javaClass.classLoader.getResourceAsStream("nodes.txt").use { input -> + val scanner = Scanner(input) var stream: Long = 0 val result = HashMap>() var streamSet: MutableSet? = null diff --git a/core/src/main/kotlin/ch/dissem/bitmessage/utils/Decode.kt b/core/src/main/kotlin/ch/dissem/bitmessage/utils/Decode.kt index 3eb192e..c19854f 100644 --- a/core/src/main/kotlin/ch/dissem/bitmessage/utils/Decode.kt +++ b/core/src/main/kotlin/ch/dissem/bitmessage/utils/Decode.kt @@ -25,21 +25,21 @@ import java.nio.ByteBuffer * https://bitmessage.org/wiki/Protocol_specification#Common_structures */ object Decode { - @JvmStatic fun shortVarBytes(`in`: InputStream, counter: AccessCounter): ByteArray { - val length = uint16(`in`, counter) - return bytes(`in`, length, counter) + @JvmStatic fun shortVarBytes(input: InputStream, counter: AccessCounter): ByteArray { + val length = uint16(input, counter) + return bytes(input, length, counter) } - @JvmStatic @JvmOverloads fun varBytes(`in`: InputStream, counter: AccessCounter? = null): ByteArray { - val length = varInt(`in`, counter).toInt() - return bytes(`in`, length, counter) + @JvmStatic @JvmOverloads fun varBytes(input: InputStream, counter: AccessCounter? = null): ByteArray { + val length = varInt(input, counter).toInt() + return bytes(input, length, counter) } - @JvmStatic @JvmOverloads fun bytes(`in`: InputStream, count: Int, counter: AccessCounter? = null): ByteArray { + @JvmStatic @JvmOverloads fun bytes(input: InputStream, count: Int, counter: AccessCounter? = null): ByteArray { val result = ByteArray(count) var off = 0 while (off < count) { - val read = `in`.read(result, off, count - off) + val read = input.read(result, off, count - off) if (read < 0) { throw IOException("Unexpected end of stream, wanted to read $count bytes but only got $off") } @@ -49,60 +49,58 @@ object Decode { return result } - @JvmStatic fun varIntList(`in`: InputStream): LongArray { - val length = varInt(`in`).toInt() + @JvmStatic fun varIntList(input: InputStream): LongArray { + val length = varInt(input).toInt() val result = LongArray(length) - for (i in 0..length - 1) { - result[i] = varInt(`in`) + for (i in 0 until length) { + result[i] = varInt(input) } return result } - @JvmStatic @JvmOverloads fun varInt(`in`: InputStream, counter: AccessCounter? = null): Long { - val first = `in`.read() + @JvmStatic @JvmOverloads fun varInt(input: InputStream, counter: AccessCounter? = null): Long { + val first = input.read() AccessCounter.inc(counter) when (first) { - 0xfd -> return uint16(`in`, counter).toLong() - 0xfe -> return uint32(`in`, counter) - 0xff -> return int64(`in`, counter) + 0xfd -> return uint16(input, counter).toLong() + 0xfe -> return uint32(input, counter) + 0xff -> return int64(input, counter) else -> return first.toLong() } } - @JvmStatic fun uint8(`in`: InputStream): Int { - return `in`.read() - } + @JvmStatic fun uint8(input: InputStream): Int = input.read() - @JvmStatic @JvmOverloads fun uint16(`in`: InputStream, counter: AccessCounter? = null): Int { + @JvmStatic @JvmOverloads fun uint16(input: InputStream, counter: AccessCounter? = null): Int { AccessCounter.inc(counter, 2) - return `in`.read() shl 8 or `in`.read() + return input.read() shl 8 or input.read() } - @JvmStatic @JvmOverloads fun uint32(`in`: InputStream, counter: AccessCounter? = null): Long { + @JvmStatic @JvmOverloads fun uint32(input: InputStream, counter: AccessCounter? = null): Long { AccessCounter.inc(counter, 4) - return (`in`.read() shl 24 or (`in`.read() shl 16) or (`in`.read() shl 8) or `in`.read()).toLong() + return (input.read() shl 24 or (input.read() shl 16) or (input.read() shl 8) or input.read()).toLong() } - @JvmStatic fun uint32(`in`: ByteBuffer): Long { - return (u(`in`.get()) shl 24 or (u(`in`.get()) shl 16) or (u(`in`.get()) shl 8) or u(`in`.get())).toLong() + @JvmStatic fun uint32(input: ByteBuffer): Long { + return (u(input.get()) shl 24 or (u(input.get()) shl 16) or (u(input.get()) shl 8) or u(input.get())).toLong() } - @JvmStatic @JvmOverloads fun int32(`in`: InputStream, counter: AccessCounter? = null): Int { + @JvmStatic @JvmOverloads fun int32(input: InputStream, counter: AccessCounter? = null): Int { AccessCounter.inc(counter, 4) - return ByteBuffer.wrap(bytes(`in`, 4)).int + return ByteBuffer.wrap(bytes(input, 4)).int } - @JvmStatic @JvmOverloads fun int64(`in`: InputStream, counter: AccessCounter? = null): Long { + @JvmStatic @JvmOverloads fun int64(input: InputStream, counter: AccessCounter? = null): Long { AccessCounter.inc(counter, 8) - return ByteBuffer.wrap(bytes(`in`, 8)).long + return ByteBuffer.wrap(bytes(input, 8)).long } - @JvmStatic @JvmOverloads fun varString(`in`: InputStream, counter: AccessCounter? = null): String { - val length = varInt(`in`, counter).toInt() + @JvmStatic @JvmOverloads fun varString(input: InputStream, counter: AccessCounter? = null): String { + val length = varInt(input, counter).toInt() // technically, it says the length in characters, but I think this one might be correct // otherwise it will get complicated, as we'll need to read UTF-8 char by char... - return String(bytes(`in`, length, counter)) + return String(bytes(input, length, counter)) } /** diff --git a/core/src/test/kotlin/ch/dissem/bitmessage/entity/ExtendedEncodingTest.kt b/core/src/test/kotlin/ch/dissem/bitmessage/entity/ExtendedEncodingTest.kt index bc4e8d6..bde06b0 100644 --- a/core/src/test/kotlin/ch/dissem/bitmessage/entity/ExtendedEncodingTest.kt +++ b/core/src/test/kotlin/ch/dissem/bitmessage/entity/ExtendedEncodingTest.kt @@ -44,20 +44,20 @@ class ExtendedEncodingTest { @Test fun `ensure simple message is encoded`() { - val `in` = Message.Builder() + val input = Message.Builder() .subject("Test sübject") .body("test bödy") .build() - assertThat(`in`.zip(), notNullValue()) + assertThat(input.zip(), notNullValue()) - val out = ExtendedEncodingFactory.unzip(`in`.zip()) - assertThat(out, `is`(`in`)) + val out = ExtendedEncodingFactory.unzip(input.zip()) + assertThat(out, `is`(input)) } @Test fun `ensure complete message is encoded and decoded`() { - val `in` = Message.Builder() + val input = Message.Builder() .addParent(TestUtils.randomInventoryVector()) .addParent(TestUtils.randomInventoryVector()) .subject("Test sübject") @@ -72,22 +72,22 @@ class ExtendedEncodingTest { ) .build() - assertThat(`in`.zip(), notNullValue()) + assertThat(input.zip(), notNullValue()) - val out = ExtendedEncodingFactory.unzip(`in`.zip()) - assertThat(out, `is`(`in`)) + val out = ExtendedEncodingFactory.unzip(input.zip()) + assertThat(out, `is`(input)) } @Test fun `ensure vote is encoded and decoded`() { - val `in` = Vote.Builder() + val input = Vote.Builder() .msgId(TestUtils.randomInventoryVector()) .vote("+1") .build() - assertThat(`in`.zip(), notNullValue()) + assertThat(input.zip(), notNullValue()) - val out = ExtendedEncodingFactory.unzip(`in`.zip()) - assertThat(out, `is`(`in`)) + val out = ExtendedEncodingFactory.unzip(input.zip()) + assertThat(out, `is`(input)) } } diff --git a/core/src/test/kotlin/ch/dissem/bitmessage/entity/SerializationTest.kt b/core/src/test/kotlin/ch/dissem/bitmessage/entity/SerializationTest.kt index 10845e7..b928cdc 100644 --- a/core/src/test/kotlin/ch/dissem/bitmessage/entity/SerializationTest.kt +++ b/core/src/test/kotlin/ch/dissem/bitmessage/entity/SerializationTest.kt @@ -87,8 +87,8 @@ class SerializationTest : TestBase() { .build() val out = ByteArrayOutputStream() expected.writer().write(out) - val `in` = ByteArrayInputStream(out.toByteArray()) - val actual = Plaintext.read(MSG, `in`) + val input = ByteArrayInputStream(out.toByteArray()) + val actual = Plaintext.read(MSG, input) // Received is automatically set on deserialization, so we'll need to set it to null val received = Plaintext::class.java.getDeclaredField("received") @@ -112,8 +112,8 @@ class SerializationTest : TestBase() { .build() val out = ByteArrayOutputStream() expected.writer().write(out) - val `in` = ByteArrayInputStream(out.toByteArray()) - val actual = Plaintext.read(MSG, `in`) + val input = ByteArrayInputStream(out.toByteArray()) + val actual = Plaintext.read(MSG, input) // Received is automatically set on deserialization, so we'll need to set it to null val received = Plaintext::class.java.getDeclaredField("received") @@ -137,8 +137,8 @@ class SerializationTest : TestBase() { val out = ByteArrayOutputStream() expected.writer().write(out) - val `in` = ByteArrayInputStream(out.toByteArray()) - val actual = Plaintext.read(MSG, `in`) + val input = ByteArrayInputStream(out.toByteArray()) + val actual = Plaintext.read(MSG, input) // Received is automatically set on deserialization, so we'll need to set it to null val received = Plaintext::class.java.getDeclaredField("received") @@ -169,8 +169,8 @@ class SerializationTest : TestBase() { private fun doTest(resourceName: String, version: Int, expectedPayloadType: Class<*>) { val data = TestUtils.getBytes(resourceName) - val `in` = ByteArrayInputStream(data) - val objectMessage = Factory.getObjectMessage(version, `in`, data.size) + val input = ByteArrayInputStream(data) + val objectMessage = Factory.getObjectMessage(version, input, data.size) val out = ByteArrayOutputStream() assertNotNull(objectMessage) objectMessage!!.writer().write(out) @@ -190,8 +190,8 @@ class SerializationTest : TestBase() { val oos = ObjectOutputStream(out) oos.writeObject(plaintext) - val `in` = ByteArrayInputStream(out.toByteArray()) - val ois = ObjectInputStream(`in`) + val input = ByteArrayInputStream(out.toByteArray()) + val ois = ObjectInputStream(input) assertEquals(plaintext, ois.readObject()) } } diff --git a/core/src/test/kotlin/ch/dissem/bitmessage/utils/DecodeTest.kt b/core/src/test/kotlin/ch/dissem/bitmessage/utils/DecodeTest.kt index f1cf244..d80f5f8 100644 --- a/core/src/test/kotlin/ch/dissem/bitmessage/utils/DecodeTest.kt +++ b/core/src/test/kotlin/ch/dissem/bitmessage/utils/DecodeTest.kt @@ -34,8 +34,8 @@ class DecodeTest { } private fun testCodec(number: Long) { - val `is` = ByteArrayOutputStream() - Encode.varInt(number, `is`) - assertEquals(number, Decode.varInt(ByteArrayInputStream(`is`.toByteArray()))) + val out = ByteArrayOutputStream() + Encode.varInt(number, out) + assertEquals(number, Decode.varInt(ByteArrayInputStream(out.toByteArray()))) } } diff --git a/core/src/test/kotlin/ch/dissem/bitmessage/utils/TestUtils.kt b/core/src/test/kotlin/ch/dissem/bitmessage/utils/TestUtils.kt index 0d92726..ec0ebfb 100644 --- a/core/src/test/kotlin/ch/dissem/bitmessage/utils/TestUtils.kt +++ b/core/src/test/kotlin/ch/dissem/bitmessage/utils/TestUtils.kt @@ -49,18 +49,18 @@ object TestUtils { @JvmStatic fun loadObjectMessage(version: Int, resourceName: String): ObjectMessage { val data = getBytes(resourceName) - val `in` = ByteArrayInputStream(data) - return Factory.getObjectMessage(version, `in`, data.size) ?: throw NoSuchElementException("error loading object message") + val input = ByteArrayInputStream(data) + return Factory.getObjectMessage(version, input, data.size) ?: throw NoSuchElementException("error loading object message") } @JvmStatic fun getBytes(resourceName: String): ByteArray { - val `in` = javaClass.classLoader.getResourceAsStream(resourceName) + val input = javaClass.classLoader.getResourceAsStream(resourceName) val out = ByteArrayOutputStream() val buffer = ByteArray(1024) - var len = `in`.read(buffer) + var len = input.read(buffer) while (len != -1) { out.write(buffer, 0, len) - len = `in`.read(buffer) + len = input.read(buffer) } return out.toByteArray() } @@ -71,9 +71,8 @@ object TestUtils { return InventoryVector(bytes) } - @JvmStatic fun getResource(resourceName: String): InputStream { - return javaClass.classLoader.getResourceAsStream(resourceName) - } + @JvmStatic fun getResource(resourceName: String): InputStream = + javaClass.classLoader.getResourceAsStream(resourceName) @JvmStatic fun loadIdentity(address: String): BitmessageAddress { val privateKey = PrivateKey.read(TestUtils.getResource(address + ".privkey")) diff --git a/exports/src/main/kotlin/ch/dissem/bitmessage/exports/ContactExport.kt b/exports/src/main/kotlin/ch/dissem/bitmessage/exports/ContactExport.kt index 5796ea3..4bcacbc 100644 --- a/exports/src/main/kotlin/ch/dissem/bitmessage/exports/ContactExport.kt +++ b/exports/src/main/kotlin/ch/dissem/bitmessage/exports/ContactExport.kt @@ -68,7 +68,7 @@ object ContactExport { Factory.readPubkey( version = version, stream = stream, - `is` = ByteArrayInputStream(it), + input = ByteArrayInputStream(it), length = it.size, encrypted = false ) diff --git a/extensions/src/main/kotlin/ch/dissem/bitmessage/extensions/CryptoCustomMessage.kt b/extensions/src/main/kotlin/ch/dissem/bitmessage/extensions/CryptoCustomMessage.kt index 4d67cb1..45024f4 100644 --- a/extensions/src/main/kotlin/ch/dissem/bitmessage/extensions/CryptoCustomMessage.kt +++ b/extensions/src/main/kotlin/ch/dissem/bitmessage/extensions/CryptoCustomMessage.kt @@ -81,16 +81,16 @@ class CryptoCustomMessage : CustomMessage { @Throws(DecryptionFailedException::class) fun decrypt(privateKey: ByteArray): T { - val `in` = SignatureCheckingInputStream(container?.decrypt(privateKey) ?: throw IllegalStateException("no encrypted data available")) + val input = SignatureCheckingInputStream(container?.decrypt(privateKey) ?: throw IllegalStateException("no encrypted data available")) if (dataReader == null) throw IllegalStateException("no data reader available") - val addressVersion = varInt(`in`) - val stream = varInt(`in`) - val behaviorBitfield = int32(`in`) - val publicSigningKey = bytes(`in`, 64) - val publicEncryptionKey = bytes(`in`, 64) - val nonceTrialsPerByte = if (addressVersion >= 3) varInt(`in`) else 0 - val extraBytes = if (addressVersion >= 3) varInt(`in`) else 0 + val addressVersion = varInt(input) + val stream = varInt(input) + val behaviorBitfield = int32(input) + val publicSigningKey = bytes(input, 64) + val publicEncryptionKey = bytes(input, 64) + val nonceTrialsPerByte = if (addressVersion >= 3) varInt(input) else 0 + val extraBytes = if (addressVersion >= 3) varInt(input) else 0 val sender = BitmessageAddress(Factory.createPubkey( addressVersion, @@ -103,9 +103,9 @@ class CryptoCustomMessage : CustomMessage { )) this.sender = sender - data = dataReader.read(sender, `in`) + data = dataReader.read(sender, input) - `in`.checkSignature(sender.pubkey!!) + input.checkSignature(sender.pubkey!!) return data!! } @@ -124,7 +124,7 @@ class CryptoCustomMessage : CustomMessage { } interface Reader { - fun read(sender: BitmessageAddress, `in`: InputStream): T + fun read(sender: BitmessageAddress, input: InputStream): T } private inner class SignatureCheckingInputStream internal constructor(private val wrapped: InputStream) : InputStream() { diff --git a/extensions/src/main/kotlin/ch/dissem/bitmessage/extensions/pow/ProofOfWorkRequest.kt b/extensions/src/main/kotlin/ch/dissem/bitmessage/extensions/pow/ProofOfWorkRequest.kt index bb368f7..9780c69 100644 --- a/extensions/src/main/kotlin/ch/dissem/bitmessage/extensions/pow/ProofOfWorkRequest.kt +++ b/extensions/src/main/kotlin/ch/dissem/bitmessage/extensions/pow/ProofOfWorkRequest.kt @@ -56,8 +56,8 @@ data class ProofOfWorkRequest @JvmOverloads constructor(val sender: BitmessageAd class Reader(private val identity: BitmessageAddress) : CryptoCustomMessage.Reader { - override fun read(sender: BitmessageAddress, `in`: InputStream): ProofOfWorkRequest { - return ProofOfWorkRequest.read(identity, `in`) + override fun read(sender: BitmessageAddress, input: InputStream): ProofOfWorkRequest { + return ProofOfWorkRequest.read(identity, input) } } @@ -87,12 +87,12 @@ data class ProofOfWorkRequest @JvmOverloads constructor(val sender: BitmessageAd companion object { @JvmStatic - fun read(client: BitmessageAddress, `in`: InputStream): ProofOfWorkRequest { + fun read(client: BitmessageAddress, input: InputStream): ProofOfWorkRequest { return ProofOfWorkRequest( client, - bytes(`in`, 64), - Request.valueOf(varString(`in`)), - varBytes(`in`) + bytes(input, 64), + Request.valueOf(varString(input)), + varBytes(input) ) } } diff --git a/extensions/src/test/kotlin/ch/dissem/bitmessage/extensions/CryptoCustomMessageTest.kt b/extensions/src/test/kotlin/ch/dissem/bitmessage/extensions/CryptoCustomMessageTest.kt index 90dfa54..d24ed0e 100644 --- a/extensions/src/test/kotlin/ch/dissem/bitmessage/extensions/CryptoCustomMessageTest.kt +++ b/extensions/src/test/kotlin/ch/dissem/bitmessage/extensions/CryptoCustomMessageTest.kt @@ -44,14 +44,13 @@ class CryptoCustomMessageTest : TestBase() { val out = ByteArrayOutputStream() messageBefore.writer().write(out) - val `in` = ByteArrayInputStream(out.toByteArray()) + val input = ByteArrayInputStream(out.toByteArray()) - val customMessage = CustomMessage.read(`in`, out.size()) + val customMessage = CustomMessage.read(input, out.size()) val messageAfter = CryptoCustomMessage.read(customMessage, object : CryptoCustomMessage.Reader { - override fun read(sender: BitmessageAddress, `in`: InputStream): GenericPayload { - return GenericPayload.read(0, 1, `in`, 100) - } + override fun read(sender: BitmessageAddress, input: InputStream) = + GenericPayload.read(0, 1, input, 100) }) val payloadAfter = messageAfter.decrypt(sendingIdentity.publicDecryptionKey) @@ -72,9 +71,9 @@ class CryptoCustomMessageTest : TestBase() { val out = ByteArrayOutputStream() messageBefore.writer().write(out) - val `in` = ByteArrayInputStream(out.toByteArray()) + val input = ByteArrayInputStream(out.toByteArray()) - val customMessage = CustomMessage.read(`in`, out.size()) + val customMessage = CustomMessage.read(input, out.size()) val messageAfter = CryptoCustomMessage.read(customMessage, ProofOfWorkRequest.Reader(sendingIdentity)) val requestAfter = messageAfter.decrypt(sendingIdentity.publicDecryptionKey) diff --git a/wif/src/main/kotlin/ch/dissem/bitmessage/wif/WifImporter.kt b/wif/src/main/kotlin/ch/dissem/bitmessage/wif/WifImporter.kt index 5b7e958..83931ac 100644 --- a/wif/src/main/kotlin/ch/dissem/bitmessage/wif/WifImporter.kt +++ b/wif/src/main/kotlin/ch/dissem/bitmessage/wif/WifImporter.kt @@ -51,7 +51,7 @@ import java.util.* */ class WifImporter constructor( private val ctx: BitmessageContext, - `in`: InputStream, + input: InputStream, vararg features: Pubkey.Feature ) { private val identities = LinkedList() @@ -62,7 +62,7 @@ class WifImporter constructor( init { val ini = Ini() - ini.load(`in`) + ini.load(input) for ((key, section) in ini) { if (!key.startsWith("BM-"))