Skip to content

Commit

Permalink
Fix EPUB deobfuscation (readium#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
mickael-menu committed Feb 26, 2024
1 parent 449b20d commit 82b1238
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ internal class EpubDeobfuscator(
@Suppress("Unused_parameter")
fun transform(url: Url, resource: Resource): Resource =
resource.flatMap {
val algorithm = resource.sourceUrl
?.let { encryptionData[it] }
?.algorithm
val algorithm = encryptionData[url]?.algorithm
if (algorithm != null && algorithm2length.containsKey(algorithm)) {
DeobfuscatingResource(resource, algorithm)
} else {
Expand Down Expand Up @@ -71,9 +69,10 @@ internal class EpubDeobfuscator(
)

private fun deobfuscate(bytes: ByteArray, obfuscationKey: ByteArray, obfuscationLength: Int) {
val toDeobfuscate = 0 until obfuscationLength
for (i in toDeobfuscate)
val toDeobfuscate = 0 until obfuscationLength.coerceAtMost(bytes.size)
for (i in toDeobfuscate) {
bytes[i] = bytes[i].xor(obfuscationKey[i % obfuscationKey.size])
}
}

private fun getHashKeyAdobe(pubId: String) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ class EpubDeobfuscatorTest {

private fun deobfuscate(url: Url, resource: Resource, algorithm: String?): Resource {
val encryptionData =
if (resource.sourceUrl != null && algorithm != null) {
mapOf(resource.sourceUrl as Url to Encryption(algorithm = algorithm))
if (algorithm != null) {
mapOf(url to Encryption(algorithm = algorithm))
} else {
emptyMap()
}
Expand Down

0 comments on commit 82b1238

Please sign in to comment.