De-duplicate key validation code in KeyBruteForcer

Signed-off-by: Graham <gpe@openrs2.org>
bzip2
Graham 4 years ago
parent 5e6afb88f6
commit b003e02ef4
  1. 65
      archive/src/main/kotlin/org/openrs2/archive/key/KeyBruteForcer.kt

@ -112,22 +112,7 @@ public class KeyBruteForcer @Inject constructor(
val k3 = rows.getInt(5) val k3 = rows.getInt(5)
val key = XteaKey(k0, k1, k2, k3) val key = XteaKey(k0, k1, k2, k3)
val valid = Unpooled.wrappedBuffer(data).use { buf -> if (validateKey(connection, data, key, keyId, containerId)) {
Js5Compression.isKeyValid(buf, key)
}
if (valid) {
connection.prepareStatement(
"""
UPDATE containers
SET key_id = ?
WHERE id = ?
""".trimIndent()
).use { stmt ->
stmt.setLong(1, keyId)
stmt.setLong(2, containerId)
stmt.execute()
}
break break
} }
} }
@ -217,22 +202,8 @@ public class KeyBruteForcer @Inject constructor(
val containerId = rows.getLong(1) val containerId = rows.getLong(1)
val data = rows.getBytes(2) val data = rows.getBytes(2)
val valid = Unpooled.wrappedBuffer(data).use { buf -> if (validateKey(connection, data, key, keyId, containerId)) {
Js5Compression.isKeyValid(buf, key) break
}
if (valid) {
connection.prepareStatement(
"""
UPDATE containers
SET key_id = ?
WHERE id = ?
""".trimIndent()
).use { stmt ->
stmt.setLong(1, keyId)
stmt.setLong(2, containerId)
stmt.execute()
}
} }
} }
} }
@ -282,6 +253,36 @@ public class KeyBruteForcer @Inject constructor(
} }
} }
private fun validateKey(
connection: Connection,
data: ByteArray,
key: XteaKey,
keyId: Long,
containerId: Long
): Boolean {
val valid = Unpooled.wrappedBuffer(data).use { buf ->
Js5Compression.isKeyValid(buf, key)
}
if (!valid) {
return false
}
connection.prepareStatement(
"""
UPDATE containers
SET key_id = ?
WHERE id = ?
""".trimIndent()
).use { stmt ->
stmt.setLong(1, keyId)
stmt.setLong(2, containerId)
stmt.execute()
}
return true
}
private companion object { private companion object {
private const val BATCH_SIZE = 1024 private const val BATCH_SIZE = 1024
} }

Loading…
Cancel
Save