@@ -171,3 +171,137 @@ def get_assembly_function(self, identifier: str) -> Optional[str]:
171171 except Exception as e :
172172 bn .log_error (f"Error getting assembly for function: { e } " )
173173 return None
174+
175+ def define_types (self , c_code : str ) -> Dict [str , str ]:
176+ """Define types from C code string
177+
178+ Args:
179+ c_code: C code string containing type definitions
180+
181+ Returns:
182+ Dictionary mapping type names to their string representations
183+
184+ Raises:
185+ RuntimeError: If no binary is loaded
186+ ValueError: If parsing the types fails
187+ """
188+ if not self .binary_ops .current_view :
189+ raise RuntimeError ("No binary loaded" )
190+
191+ try :
192+ # Parse the C code string to get type objects
193+ parse_result = self .binary_ops .current_view .parse_types_from_string (c_code )
194+
195+ # Define each type in the binary view
196+ defined_types = {}
197+ for name , type_obj in parse_result .types .items ():
198+ self .binary_ops .current_view .define_user_type (name , type_obj )
199+ defined_types [str (name )] = str (type_obj )
200+
201+ return defined_types
202+ except Exception as e :
203+ raise ValueError (f"Failed to define types: { str (e )} " )
204+
205+ def rename_variable (self , function_name : str , old_name : str , new_name : str ) -> Dict [str , str ]:
206+ """Rename a variable inside a function
207+
208+ Args:
209+ function_name: Name of the function containing the variable
210+ old_name: Current name of the variable
211+ new_name: New name for the variable
212+
213+ Returns:
214+ Dictionary with status message
215+
216+ Raises:
217+ RuntimeError: If no binary is loaded
218+ ValueError: If the function is not found or variable cannot be renamed
219+ """
220+ if not self .binary_ops .current_view :
221+ raise RuntimeError ("No binary loaded" )
222+
223+ # Find the function by name
224+ function = self .binary_ops .get_function_by_name_or_address (function_name )
225+ if not function :
226+ raise ValueError (f"Function '{ function_name } ' not found" )
227+
228+ # Try to rename the variable
229+ try :
230+ # Get the variable by name and rename it
231+ variable = function .get_variable_by_name (old_name )
232+ if not variable :
233+ raise ValueError (f"Variable '{ old_name } ' not found in function '{ function_name } '" )
234+
235+ variable .name = new_name
236+ return {"status" : f"Successfully renamed variable '{ old_name } ' to '{ new_name } ' in function '{ function_name } '" }
237+ except Exception as e :
238+ raise ValueError (f"Failed to rename variable: { str (e )} " )
239+
240+
241+ def retype_variable (self , function_name : str , name : str , type_str : str ) -> Dict [str , str ]:
242+ """Retype a variable inside a function
243+
244+ Args:
245+ function_name: Name of the function containing the variable
246+ name: Current name of the variable
247+ type: C type for the variable
248+
249+ Returns:
250+ Dictionary with status message
251+
252+ Raises:
253+ RuntimeError: If no binary is loaded
254+ ValueError: If the function is not found or variable cannot be retyped
255+ """
256+ if not self .binary_ops .current_view :
257+ raise RuntimeError ("No binary loaded" )
258+
259+ # Find the function by name
260+ function = self .binary_ops .get_function_by_name_or_address (function_name )
261+ if not function :
262+ raise ValueError (f"Function '{ function_name } ' not found" )
263+
264+ # Try to rename the variable
265+ try :
266+ # Get the variable by name and rename it
267+ variable = function .get_variable_by_name (name )
268+ if not variable :
269+ raise ValueError (f"Variable '{ name } ' not found in function '{ function_name } '" )
270+
271+ variable .type = type_str
272+ return {"status" : f"Successfully retyped variable '{ name } ' to '{ type_str } ' in function '{ function_name } '" }
273+ except Exception as e :
274+ raise ValueError (f"Failed to rename variable: { str (e )} " )
275+
276+
277+ def edit_function_signature (self , function_name : str , signature : str ) -> Dict [str , str ]:
278+ """Rename a variable inside a function
279+
280+ Args:
281+ function_name: Name of the function to edit the signature of
282+ signature: new signature to apply
283+
284+ Returns:
285+ Dictionary with status message
286+
287+ Raises:
288+ RuntimeError: If no binary is loaded
289+ ValueError: If the function is not found or variable cannot be renamed
290+ """
291+ if not self .binary_ops .current_view :
292+ raise RuntimeError ("No binary loaded" )
293+
294+ # Find the function by name
295+ function = self .binary_ops .get_function_by_name_or_address (function_name )
296+ if not function :
297+ raise ValueError (f"Function '{ function_name } ' not found" )
298+
299+ function .type = self .binary_ops .current_view .parse_type_string (signature )[0 ]
300+
301+ function .reanalyze (bn .FunctionUpdateType .UserFunctionUpdate )
302+
303+ try :
304+ return {"status" : f"Successfully" }
305+ except Exception as e :
306+ raise ValueError (f"Failed to rename variable: { str (e )} " )
307+
0 commit comments