diff --git a/mirai-core-timpc/src/commonMain/kotlin/net.mamoe.mirai.timpc/network/packet/login/PasswordSubmission.kt b/mirai-core-timpc/src/commonMain/kotlin/net.mamoe.mirai.timpc/network/packet/login/PasswordSubmission.kt index c6ea1822d..e635ae10d 100644 --- a/mirai-core-timpc/src/commonMain/kotlin/net.mamoe.mirai.timpc/network/packet/login/PasswordSubmission.kt +++ b/mirai-core-timpc/src/commonMain/kotlin/net.mamoe.mirai.timpc/network/packet/login/PasswordSubmission.kt @@ -27,7 +27,7 @@ internal inline class PrivateKey(override val value: ByteArray) : DecrypterByteA internal inline class SubmitPasswordResponseDecrypter(private val privateKey: PrivateKey) : Decrypter { override fun decrypt(input: ByteReadPacket, offset: Int, length: Int): ByteReadPacket { - var decrypted = ShareKey.decrypt(input) + var decrypted = ShareKey.decrypt(input, offset, length) (decrypted.remaining).let { if (it.toInt() % 8 == 0 && it >= 16) { decrypted = try { @@ -263,7 +263,7 @@ internal object SubmitPasswordPacket : PacketFactory { } } +fun Decrypter.decrypt(input: ByteReadPacket): ByteReadPacket = this.decrypt(input, 0, input.remaining.toInt()) + /** * 解密器 */ interface Decrypter { - fun decrypt(input: ByteReadPacket, offset: Int = 0, length: Int = (input.remaining - offset).toInt()): ByteReadPacket + // do not write with default args. NoSuchMethodError when inline classes override this function + fun decrypt(input: ByteReadPacket, offset: Int, length: Int): ByteReadPacket /** * 连接后将会先用 this 解密, 再用 [another] 解密 */ diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/cryptor/TEA.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/cryptor/TEA.kt index c08ef64c0..b921fcfbf 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/cryptor/TEA.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/cryptor/TEA.kt @@ -94,21 +94,19 @@ fun IoBuffer.decryptBy(key: ByteArray, offset: Int = 0, length: Int = readRemain // region ByteReadPacket extension -fun ByteReadPacket.decryptBy(key: ByteArray, offset: Int = 0, length: Int = key.size - offset): ByteReadPacket = decryptAsByteArray(key) { data -> ByteReadPacket(data, offset, length) } +fun ByteReadPacket.decryptBy(key: ByteArray, offset: Int = 0, length: Int = key.size - offset): ByteReadPacket = decryptAsByteArray(key, offset, length) { data -> ByteReadPacket(data) } -fun ByteReadPacket.decryptBy(key: IoBuffer, offset: Int = 0, length: Int = key.readRemaining - offset): ByteReadPacket = decryptAsByteArray(key) { data -> ByteReadPacket(data, offset, length) } +fun ByteReadPacket.decryptBy(key: IoBuffer, offset: Int = 0, length: Int = key.readRemaining - offset): ByteReadPacket = decryptAsByteArray(key, offset, length) { data -> ByteReadPacket(data) } -inline fun ByteReadPacket.decryptAsByteArray(key: ByteArray, consumer: (ByteArray) -> R): R = +inline fun ByteReadPacket.decryptAsByteArray(key: ByteArray, offset: Int = 0, length: Int = key.size - offset, consumer: (ByteArray) -> R): R = ByteArrayPool.useInstance { - val length = remaining.toInt() - readFully(it, 0, length) + readFully(it, offset, length) consumer(it.decryptBy(key, length)) }.also { close() } -inline fun ByteReadPacket.decryptAsByteArray(key: IoBuffer, consumer: (ByteArray) -> R): R = +inline fun ByteReadPacket.decryptAsByteArray(key: IoBuffer, offset: Int = 0, length: Int = key.readRemaining - offset, consumer: (ByteArray) -> R): R = ByteArrayPool.useInstance { - val length = remaining.toInt() - readFully(it, 0, length) + readFully(it, offset, length) consumer(it.decryptBy(key, length)) }.also { close() } diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/io/OutputUtils.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/io/OutputUtils.kt index 091b9e543..2704b7a3e 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/io/OutputUtils.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/io/OutputUtils.kt @@ -47,7 +47,7 @@ fun BytePacketBuilder.writeShortLVByteArray(byteArray: ByteArray): Int { inline fun BytePacketBuilder.writeIntLVPacket(tag: UByte? = null, lengthOffset: ((Long) -> Long) = {it}, builder: BytePacketBuilder.() -> Unit): Int = BytePacketBuilder().apply(builder).build().use { if (tag != null) writeUByte(tag) - val length = (lengthOffset.invoke(it.remaining) ?: it.remaining).coerceAtMostOrFail(0xFFFFL) + val length = lengthOffset.invoke(it.remaining).coerceAtMostOrFail(0xFFFFL) writeInt(length.toInt()) writePacket(it) return length.toInt() @@ -56,7 +56,7 @@ inline fun BytePacketBuilder.writeIntLVPacket(tag: UByte? = null, lengthOffset: inline fun BytePacketBuilder.writeShortLVPacket(tag: UByte? = null, lengthOffset: ((Long) -> Long) = {it}, builder: BytePacketBuilder.() -> Unit): Int = BytePacketBuilder().apply(builder).build().use { if (tag != null) writeUByte(tag) - val length = (lengthOffset.invoke(it.remaining) ?: it.remaining).coerceAtMostOrFail(0xFFFFL) + val length = lengthOffset.invoke(it.remaining).coerceAtMostOrFail(0xFFFFL) writeUShort(length.toUShort()) writePacket(it) return length.toInt() @@ -65,7 +65,7 @@ inline fun BytePacketBuilder.writeShortLVPacket(tag: UByte? = null, lengthOffset inline fun BytePacketBuilder.writeUVarIntLVPacket(tag: UByte? = null, lengthOffset: ((Long) -> Long) = {it}, builder: BytePacketBuilder.() -> Unit) = BytePacketBuilder().apply(builder).build().use { if (tag != null) writeUByte(tag) - writeUVarInt((lengthOffset.invoke(it.remaining) ?: it.remaining).coerceAtMostOrFail(0xFFFFL)) + writeUVarInt(lengthOffset.invoke(it.remaining).coerceAtMostOrFail(0xFFFFL)) writePacket(it) }