From c5f9286bbca08bc3305c308be926f3d67f87aaf0 Mon Sep 17 00:00:00 2001 From: N3rdL0rd <168280212+N3rdL0rd@users.noreply.github.com> Date: Sat, 28 Dec 2024 15:31:33 -0600 Subject: [PATCH] feat: More commands for string manipulation --- crashlink/__main__.py | 59 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/crashlink/__main__.py b/crashlink/__main__.py index 476786a..6059a63 100644 --- a/crashlink/__main__.py +++ b/crashlink/__main__.py @@ -231,7 +231,7 @@ def save(self, args: List[str]) -> None: if len(args) == 0: print("Usage: save ") return - print("Serialising...") + print("Serialising... (don't panic if it looks stuck!)") ser = self.code.serialise() print("Saving...") with open(args[0], "wb") as f: @@ -259,6 +259,63 @@ def pseudo(self, args: List[str]) -> None: return print("Function not found.") + def savestrings(self, args: List[str]) -> None: + """Save all strings in the bytecode to a given path. `savestrings `""" + if len(args) == 0: + print("Usage: savestrings ") + return + with open(args[0], "wb") as f: + for string in self.code.strings.value: + f.write(string.encode("utf-8", errors="surrogateescape") + b"\n") + print("Strings saved.") + + def ss(self, args: List[str]) -> None: + """ + Search for a string in the bytecode by substring. `ss ` + """ + if len(args) == 0: + print("Usage: ss ") + return + query = " ".join(args) + for i, string in enumerate(self.code.strings.value): + if query.lower() in string.lower(): + print(f"String {i}: {string}") + + def string(self, args: List[str]) -> None: + """ + Print a string by index. `string ` + """ + if len(args) == 0: + print("Usage: string ") + return + try: + index = int(args[0]) + except ValueError: + print("Invalid index.") + return + try: + print(self.code.strings.value[index]) + except IndexError: + print("String not found.") + + def setstring(self, args: List[str]) -> None: + """ + Set a string by index. `setstring ` + """ + if len(args) < 2: + print("Usage: setstring ") + return + try: + index = int(args[0]) + except ValueError: + print("Invalid index.") + return + try: + self.code.strings.value[index] = " ".join(args[1:]) + except IndexError: + print("String not found.") + print("String set.") + def _get_commands(self) -> Dict[str, Callable[[List[str]], None]]: """Get all command methods using reflection""" return {