Skip to content

Commit 82b1238

Browse files
authored
Fix EPUB deobfuscation (readium#475)
1 parent 449b20d commit 82b1238

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

readium/streamer/src/main/java/org/readium/r2/streamer/parser/epub/EpubDeobfuscator.kt

+4-5
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ internal class EpubDeobfuscator(
2727
@Suppress("Unused_parameter")
2828
fun transform(url: Url, resource: Resource): Resource =
2929
resource.flatMap {
30-
val algorithm = resource.sourceUrl
31-
?.let { encryptionData[it] }
32-
?.algorithm
30+
val algorithm = encryptionData[url]?.algorithm
3331
if (algorithm != null && algorithm2length.containsKey(algorithm)) {
3432
DeobfuscatingResource(resource, algorithm)
3533
} else {
@@ -71,9 +69,10 @@ internal class EpubDeobfuscator(
7169
)
7270

7371
private fun deobfuscate(bytes: ByteArray, obfuscationKey: ByteArray, obfuscationLength: Int) {
74-
val toDeobfuscate = 0 until obfuscationLength
75-
for (i in toDeobfuscate)
72+
val toDeobfuscate = 0 until obfuscationLength.coerceAtMost(bytes.size)
73+
for (i in toDeobfuscate) {
7674
bytes[i] = bytes[i].xor(obfuscationKey[i % obfuscationKey.size])
75+
}
7776
}
7877

7978
private fun getHashKeyAdobe(pubId: String) =

readium/streamer/src/test/java/org/readium/r2/streamer/parser/epub/EpubDeobfuscatorTest.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ class EpubDeobfuscatorTest {
4545

4646
private fun deobfuscate(url: Url, resource: Resource, algorithm: String?): Resource {
4747
val encryptionData =
48-
if (resource.sourceUrl != null && algorithm != null) {
49-
mapOf(resource.sourceUrl as Url to Encryption(algorithm = algorithm))
48+
if (algorithm != null) {
49+
mapOf(url to Encryption(algorithm = algorithm))
5050
} else {
5151
emptyMap()
5252
}

0 commit comments

Comments
 (0)