From b003e02ef4baa024c15bb1cd4d81b802bc552dce Mon Sep 17 00:00:00 2001 From: Graham Date: Tue, 9 Feb 2021 18:24:11 +0000 Subject: [PATCH] De-duplicate key validation code in KeyBruteForcer Signed-off-by: Graham --- .../org/openrs2/archive/key/KeyBruteForcer.kt | 65 ++++++++++--------- 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/archive/src/main/kotlin/org/openrs2/archive/key/KeyBruteForcer.kt b/archive/src/main/kotlin/org/openrs2/archive/key/KeyBruteForcer.kt index 5305930c..4d615fad 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/key/KeyBruteForcer.kt +++ b/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 key = XteaKey(k0, k1, k2, k3) - val valid = Unpooled.wrappedBuffer(data).use { buf -> - 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() - } + if (validateKey(connection, data, key, keyId, containerId)) { break } } @@ -217,22 +202,8 @@ public class KeyBruteForcer @Inject constructor( val containerId = rows.getLong(1) val data = rows.getBytes(2) - val valid = Unpooled.wrappedBuffer(data).use { buf -> - 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() - } + if (validateKey(connection, data, key, keyId, containerId)) { + break } } } @@ -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 const val BATCH_SIZE = 1024 }