From 938e7eae5798435b38366d6ae1d5bd6d3f6861ae Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Sun, 17 Oct 2021 17:11:34 +0800 Subject: [PATCH] Fix ImageDecoder of JPEG format; fix #1610 --- mirai-core-utils/src/commonMain/kotlin/Numbers.kt | 3 ++- mirai-core/src/commonMain/kotlin/message/ImageDecoder.kt | 8 +++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/mirai-core-utils/src/commonMain/kotlin/Numbers.kt b/mirai-core-utils/src/commonMain/kotlin/Numbers.kt index 7b894cbb5..3165105ac 100644 --- a/mirai-core-utils/src/commonMain/kotlin/Numbers.kt +++ b/mirai-core-utils/src/commonMain/kotlin/Numbers.kt @@ -13,4 +13,5 @@ package net.mamoe.mirai.utils public fun Int.toLongUnsigned(): Long = this.toLong().and(0xFFFF_FFFF) -public fun Short.toIntUnsigned(): Int = this.toUShort().toInt() \ No newline at end of file +public fun Short.toIntUnsigned(): Int = this.toUShort().toInt() +public fun Byte.toIntUnsigned(): Int = toInt() and 0xFF diff --git a/mirai-core/src/commonMain/kotlin/message/ImageDecoder.kt b/mirai-core/src/commonMain/kotlin/message/ImageDecoder.kt index 590863bac..c179dafb3 100644 --- a/mirai-core/src/commonMain/kotlin/message/ImageDecoder.kt +++ b/mirai-core/src/commonMain/kotlin/message/ImageDecoder.kt @@ -12,10 +12,7 @@ package net.mamoe.mirai.internal.message import kotlinx.io.core.* import kotlinx.io.streams.asInput import net.mamoe.mirai.message.data.ImageType -import net.mamoe.mirai.utils.ExternalResource -import net.mamoe.mirai.utils.readString -import net.mamoe.mirai.utils.toUHexString -import net.mamoe.mirai.utils.withUse +import net.mamoe.mirai.utils.* import java.io.IOException //SOF0-SOF3 SOF5-SOF7 SOF9-SOF11 SOF13-SOF15 Segment @@ -27,6 +24,7 @@ private val JPG_SOF_RANGE = listOf( 0xCD.toByte()..0xCF.toByte() ) +// https://docs.fileformat.com/image/jpeg/ private fun Input.getJPGImageInfo(): ImageInfo { require(readBytes(2).contentEquals(byteArrayOf(0xFF.toByte(), 0xD8.toByte()))) { "It's not a valid jpg file" @@ -51,7 +49,7 @@ private fun Input.getJPGImageInfo(): ImageInfo { //Other segment, skip discardExact( //Skip size=segment length - 2 (length data itself) - readShort().toInt() - 2 + readShort().toIntUnsigned() - 2 ) } }