diff --git a/app/build.gradle b/app/build.gradle index 1f3e23b..a67021e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -12,8 +12,8 @@ android { minSdkVersion 23 //noinspection OldTargetApi targetSdkVersion 34 - versionCode 79 - versionName '2.5.6' + versionCode 80 + versionName '2.5.7' resourceConfigurations += ['zh', 'zh-rCN'] testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" diff --git a/app/src/main/java/top/fumiama/copymanga/strings/Base16384.kt b/app/src/main/java/top/fumiama/copymanga/strings/Base16384.kt index 9e7fec1..93dcbd8 100644 --- a/app/src/main/java/top/fumiama/copymanga/strings/Base16384.kt +++ b/app/src/main/java/top/fumiama/copymanga/strings/Base16384.kt @@ -11,35 +11,35 @@ object Base16384 { fun encode(input: ByteArray): String { val sb = StringBuilder() var buffer = 0 - var bitCount = 0 + var bLen = 0 for (b in input) { buffer = (buffer shl 8) or (b.toInt() and 0xFF) - bitCount += 8 - while (bitCount >= 14) { - bitCount -= 14 - val index = (buffer shr bitCount) and (BASE - 1) + bLen += 8 + while (bLen >= 14) { + bLen -= 14 + val index = (buffer shr bLen) and (BASE - 1) sb.append(Char(FIRST_CHAR + index)) - buffer = buffer and ((1 shl bitCount) - 1) + buffer = buffer and ((1 shl bLen) - 1) } } - if (bitCount in 1..6) { - buffer = (buffer shl (14 - bitCount)) + if (bLen > 0) { + buffer = (buffer shl (14 - bLen)) val index = buffer and (BASE - 1) sb.append(Char(FIRST_CHAR + index)) - val padIndex = PAD_START + bitCount + val padIndex = PAD_START + (input.size%7) sb.append(Char(padIndex)) } - Log.d("MyB14", "encode bitCount $bitCount, len ${input.size}, data: ${Base64.encode(input, Base64.DEFAULT).decodeToString()}") + Log.d("MyB14", "encode offset ${input.size%7}, len ${input.size}, data: ${Base64.encode(input, Base64.DEFAULT).decodeToString()}") return sb.toString() } fun decode(input: String): ByteArray { val bits = mutableListOf() - var tailBits = 0 + var offset = 0 for (c in input) { when (val code = c.code) { in FIRST_CHAR until FIRST_CHAR + BASE -> { @@ -49,17 +49,25 @@ object Base16384 { } } in (PAD_START + 1)..(PAD_START + 6) -> { - tailBits = code - PAD_START + offset = code - PAD_START break } else -> throw IllegalArgumentException("Invalid base16384 char: $c") } } - val totalBits = bits.size - (14 - tailBits) - val byteCount = totalBits / 8 - val out = ByteArray(byteCount) - for (i in 0 until byteCount) { + val dLen = input.toByteArray(Charsets.UTF_16BE).size + val outLen = when (offset) { + 0 -> dLen + 1 -> dLen - 4 + 2, 3 -> dLen - 6 + 4, 5 -> dLen - 8 + 6 -> dLen - 10 + else -> dLen + }/8*7 + offset + + val out = ByteArray(outLen) + for (i in 0 until outLen) { var byteVal = 0 for (j in 0 until 8) { if (bits[i * 8 + j]) byteVal = byteVal or (1 shl (7 - j)) @@ -67,7 +75,7 @@ object Base16384 { out[i] = byteVal.toByte() } - Log.d("MyB14", "decode totalBits $totalBits, tailBits $tailBits, len ${out.size}, data: ${Base64.encode(out, Base64.DEFAULT).decodeToString()}") + Log.d("MyB14", "decode offset $offset, len ${out.size}, data: ${Base64.encode(out, Base64.DEFAULT).decodeToString()}") return out } }