|
| 1 | +package app.revanced.patches.shared.misc.hex |
| 2 | + |
| 3 | +import app.revanced.patcher.data.ResourceContext |
| 4 | +import app.revanced.patcher.patch.PatchException |
| 5 | +import app.revanced.patcher.patch.RawResourcePatch |
| 6 | +import kotlin.math.max |
| 7 | + |
| 8 | +abstract class BaseHexPatch : RawResourcePatch() { |
| 9 | + internal abstract val replacements: List<Replacement> |
| 10 | + |
| 11 | + override fun execute(context: ResourceContext) { |
| 12 | + replacements.groupBy { it.targetFilePath }.forEach { (targetFilePath, replacements) -> |
| 13 | + val targetFile = try { |
| 14 | + context[targetFilePath, true] |
| 15 | + } catch (e: Exception) { |
| 16 | + throw PatchException("Could not find target file: $targetFilePath") |
| 17 | + } |
| 18 | + |
| 19 | + // TODO: Use a file channel to read and write the file instead of reading the whole file into memory, |
| 20 | + // in order to reduce memory usage. |
| 21 | + val targetFileBytes = targetFile.readBytes() |
| 22 | + |
| 23 | + replacements.forEach { replacement -> |
| 24 | + replacement.replacePattern(targetFileBytes) |
| 25 | + } |
| 26 | + |
| 27 | + targetFile.writeBytes(targetFileBytes) |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Represents a pattern to search for and its replacement pattern. |
| 33 | + * |
| 34 | + * @property pattern The pattern to search for. |
| 35 | + * @property replacementPattern The pattern to replace the [pattern] with. |
| 36 | + * @property targetFilePath The path to the file to make the changes in relative to the APK root. |
| 37 | + */ |
| 38 | + class Replacement( |
| 39 | + private val pattern: String, |
| 40 | + replacementPattern: String, |
| 41 | + internal val targetFilePath: String, |
| 42 | + ) { |
| 43 | + private val patternBytes = pattern.toByteArrayPattern() |
| 44 | + private val replacementPattern = replacementPattern.toByteArrayPattern() |
| 45 | + |
| 46 | + init { |
| 47 | + if (this.patternBytes.size != this.replacementPattern.size) { |
| 48 | + throw PatchException("Pattern and replacement pattern must have the same length: $pattern") |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Replaces the [patternBytes] with the [replacementPattern] in the [targetFileBytes]. |
| 54 | + * |
| 55 | + * @param targetFileBytes The bytes of the file to make the changes in. |
| 56 | + */ |
| 57 | + fun replacePattern(targetFileBytes: ByteArray) { |
| 58 | + val startIndex = indexOfPatternIn(targetFileBytes) |
| 59 | + |
| 60 | + if (startIndex == -1) { |
| 61 | + throw PatchException("Pattern not found in target file: $pattern") |
| 62 | + } |
| 63 | + |
| 64 | + replacementPattern.copyInto(targetFileBytes, startIndex) |
| 65 | + } |
| 66 | + |
| 67 | + // TODO: Allow searching in a file channel instead of a byte array to reduce memory usage. |
| 68 | + /** |
| 69 | + * Returns the index of the first occurrence of [patternBytes] in the haystack |
| 70 | + * using the Boyer-Moore algorithm. |
| 71 | + * |
| 72 | + * @param haystack The array to search in. |
| 73 | + * |
| 74 | + * @return The index of the first occurrence of the [patternBytes] in the haystack or -1 |
| 75 | + * if the [patternBytes] is not found. |
| 76 | + */ |
| 77 | + private fun indexOfPatternIn(haystack: ByteArray): Int { |
| 78 | + val needle = patternBytes |
| 79 | + |
| 80 | + val haystackLength = haystack.size - 1 |
| 81 | + val needleLength = needle.size - 1 |
| 82 | + val right = IntArray(256) { -1 } |
| 83 | + |
| 84 | + for (i in 0 until needleLength) right[needle[i].toInt().and(0xFF)] = i |
| 85 | + |
| 86 | + var skip: Int |
| 87 | + for (i in 0..haystackLength - needleLength) { |
| 88 | + skip = 0 |
| 89 | + |
| 90 | + for (j in needleLength - 1 downTo 0) |
| 91 | + if (needle[j] != haystack[i + j]) { |
| 92 | + skip = max(1, j - right[haystack[i + j].toInt().and(0xFF)]) |
| 93 | + |
| 94 | + break |
| 95 | + } |
| 96 | + |
| 97 | + if (skip == 0) return i |
| 98 | + } |
| 99 | + return -1 |
| 100 | + } |
| 101 | + |
| 102 | + companion object { |
| 103 | + /** |
| 104 | + * Convert a string representing a pattern of hexadecimal bytes to a byte array. |
| 105 | + * |
| 106 | + * @return The byte array representing the pattern. |
| 107 | + * @throws PatchException If the pattern is invalid. |
| 108 | + */ |
| 109 | + private fun String.toByteArrayPattern() = try { |
| 110 | + split(" ").map { it.toInt(16).toByte() }.toByteArray() |
| 111 | + } catch (e: NumberFormatException) { |
| 112 | + throw PatchException( |
| 113 | + "Could not parse pattern: $this. A pattern is a sequence of case insensitive strings " + |
| 114 | + "representing hexadecimal bytes separated by spaces", |
| 115 | + e, |
| 116 | + ) |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments