about summary refs log tree commit diff stats
path: root/example/ida/utils.py
diff options
context:
space:
mode:
authorCamille Mougey <camille.mougey@cea.fr>2015-02-09 21:18:42 +0100
committerCamille Mougey <camille.mougey@cea.fr>2015-02-09 21:18:42 +0100
commit11abe47d5b60d1442e1da6d55b9ac53f6f8f632b (patch)
treed0e8d1b21664be6f327c33ae896e06ad1644a399 /example/ida/utils.py
parentd876fd8be63a3b319d206b59d57574fc3c5da8ff (diff)
downloadmiasm-11abe47d5b60d1442e1da6d55b9ac53f6f8f632b.tar.gz
miasm-11abe47d5b60d1442e1da6d55b9ac53f6f8f632b.zip
Example/IDA: Move `translatorForm` to `utils`
Diffstat (limited to 'example/ida/utils.py')
-rw-r--r--example/ida/utils.py56
1 files changed, 56 insertions, 0 deletions
diff --git a/example/ida/utils.py b/example/ida/utils.py
index 6b42cd83..8b9dcf6a 100644
--- a/example/ida/utils.py
+++ b/example/ida/utils.py
@@ -2,6 +2,7 @@ import idaapi
 from idc import *
 
 from miasm2.analysis.machine import Machine
+from miasm2.ir.translators import Translator
 import miasm2.expression.expression as m2_expr
 
 
@@ -111,3 +112,58 @@ def expr2colorstr(regs_ids, expr):
         s = str(expr)
 
     return s
+
+
+class translatorForm(idaapi.Form):
+    """Translator Form.
+
+    Offer a ComboBox with available languages (ie. IR translators) and the
+    corresponding translation."""
+
+    flags = (idaapi.Form.MultiLineTextControl.TXTF_FIXEDFONT | \
+                 idaapi.Form.MultiLineTextControl.TXTF_READONLY)
+
+    def __init__(self, expr):
+        "@expr: Expr instance"
+
+        # Init
+        self.languages = list(Translator.available_languages())
+        self.expr = expr
+
+        # Initial translation
+        text = Translator.to_language(self.languages[0]).from_expr(self.expr)
+
+        # Create the Form
+        idaapi.Form.__init__(self, r"""STARTITEM 0
+Python Expression
+{FormChangeCb}
+<Language:{cbLanguage}>
+<Translation:{result}>
+""", {
+            'result': idaapi.Form.MultiLineTextControl(text=text,
+                                                       flags=translatorForm.flags),
+            'cbLanguage': idaapi.Form.DropdownListControl(
+                    items=self.languages,
+                    readonly=True,
+                    selval=0),
+            'FormChangeCb': idaapi.Form.FormChangeCb(self.OnFormChange),
+        })
+
+    def OnFormChange(self, fid):
+        if fid == self.cbLanguage.id:
+            # Display the Field (may be hide)
+            self.ShowField(self.result, True)
+
+            # Translate the expression
+            dest_lang = self.languages[self.GetControlValue(self.cbLanguage)]
+            try:
+                text = Translator.to_language(dest_lang).from_expr(self.expr)
+            except Exception, error:
+                self.ShowField(self.result, False)
+                return -1
+
+            # Update the form
+            self.SetControlValue(self.result,
+                                 idaapi.textctrl_info_t(text=str(text),
+                                                        flags=translatorForm.flags))
+        return 1