Skip to content

Commit

Permalink
Add support for line numbers in Context
Browse files Browse the repository at this point in the history
  • Loading branch information
mahaloz committed Sep 17, 2024
1 parent 2996a2c commit 6dabff5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion libbs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "1.22.1"
__version__ = "1.23.0"


import logging
Expand Down
9 changes: 7 additions & 2 deletions libbs/artifacts/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@ def __str__(self):
return f"<Comment: @{hex(self.addr)} len={cmt_len}>"

@staticmethod
def linewrap_comment(comment: str, width=80):
wrapped_text = textwrap.fill(comment, width=width)
def linewrap_comment(comment: str, width=100) -> str:
# Split the comment into lines based on existing newlines
lines = comment.split('\n')
# Wrap each line individually and preserve newlines
wrapped_lines = [textwrap.fill(line, width=width) for line in lines]
# Join the wrapped lines with newline characters
wrapped_text = '\n'.join(wrapped_lines)
return wrapped_text

def nonconflict_merge(self, obj2: "Comment", **kwargs) -> "Comment":
Expand Down
5 changes: 5 additions & 0 deletions libbs/artifacts/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Context(Artifact):
__slots__ = Artifact.__slots__ + (
"addr",
"func_addr",
"line_number",
"screen_name",
"variable"
)
Expand All @@ -15,12 +16,14 @@ def __init__(
self,
addr: Optional[int] = None,
func_addr: Optional[int] = None,
line_number: Optional[int] = None,
screen_name: Optional[str] = None,
variable: Optional[str] = None,
**kwargs
):
self.addr = addr
self.func_addr = func_addr
self.line_number = line_number
self.screen_name = screen_name
self.variable = variable
super().__init__(**kwargs)
Expand All @@ -32,5 +35,7 @@ def __str__(self):
post_text = f"@{hex(self.func_addr)}" + post_text
if self.addr is not None:
post_text = hex(self.addr) + post_text
if self.line_number is not None:
post_text += f" line={self.line_number}"

return f"<Context {post_text}>"
2 changes: 2 additions & 0 deletions libbs/decompilers/ida/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -1343,6 +1343,7 @@ def view_to_bs_context(view, get_var=True) -> typing.Optional[Context]:
lvar = vu.item.get_lvar()
if lvar:
ctx.variable = lvar.name
ctx.line_number = vu.cpos.lnnum if vu.cpos else None

return ctx

Expand Down Expand Up @@ -1388,6 +1389,7 @@ def activate(self, ctx):
if bs_ctx is None:
return

bs_ctx = self.deci.art_lifter.lift(bs_ctx)
dec_view = ida_hexrays.get_widget_vdui(ctx.widget)
self.action_function(bs_ctx, deci=self.deci)

Expand Down

0 comments on commit 6dabff5

Please sign in to comment.