Fix ImageDecoder of JPEG format; fix #1610

This commit is contained in:
Karlatemp 2021-10-17 17:11:34 +08:00
parent ee78b5dbe5
commit 938e7eae57
No known key found for this signature in database
GPG Key ID: 21FBDDF664FF06F8
2 changed files with 5 additions and 6 deletions

View File

@ -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()
public fun Short.toIntUnsigned(): Int = this.toUShort().toInt()
public fun Byte.toIntUnsigned(): Int = toInt() and 0xFF

View File

@ -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
)
}
}