Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix EPUB deobfuscation #475

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading