Exemple #1
0
    def _dump_bb_ex(self, basic_block, print_ir=False):
        lines = []

        base_addr = basic_block.instrs[0].address

        formatter = HtmlFormatter()
        formatter.noclasses = True
        formatter.nowrap = True

        asm_mnemonic_max_width = 0

        for dinstr in basic_block:
            if len(dinstr.asm_instr.mnemonic) > asm_mnemonic_max_width:
                asm_mnemonic_max_width = len(dinstr.asm_instr.mnemonic)

        for instr in basic_block.instrs:
            asm_instr = self._dump_instr(instr.asm_instr,
                                         asm_mnemonic_max_width + 1,
                                         fill_char=" ")
            asm_instr = highlight(asm_instr, NasmLexer(), formatter)
            asm_instr = asm_instr.replace("span", "font")
            asm_instr = asm_instr.replace('style="color: ', 'color="')

            lines += [
                "<tr><td align='left'>    %08x [%02d] %s </td></tr>" %
                (instr.address, instr.asm_instr.size, asm_instr)
            ]

            if print_ir:
                for ir_instr in instr.ir_instrs:
                    lines += ["              " + str(ir_instr) + "\\l"]

        return "".join(lines)
Exemple #2
0
    def _dump_bb_ex(self, basic_block, print_ir=False):
        lines = []

        base_addr = basic_block.instrs[0].address

        formatter = HtmlFormatter()
        formatter.noclasses = True
        formatter.nowrap = True

        asm_mnemonic_max_width = 0

        for dinstr in basic_block:
            if len(dinstr.asm_instr.mnemonic) > asm_mnemonic_max_width:
                asm_mnemonic_max_width = len(dinstr.asm_instr.mnemonic)

        for instr in basic_block.instrs:
            asm_instr = self._dump_instr(instr.asm_instr, asm_mnemonic_max_width + 1, fill_char=" ")
            asm_instr = highlight(asm_instr, NasmLexer(), formatter)
            asm_instr = asm_instr.replace("span", "font")
            asm_instr = asm_instr.replace('style="color: ', 'color="')

            lines += ["<tr><td align='left'>    %08x [%02d] %s </td></tr>" % (instr.address, instr.asm_instr.size, asm_instr)]

            if print_ir:
                for ir_instr in instr.ir_instrs:
                    lines += ["              " + str(ir_instr) + "\\l"]

        return "".join(lines)
 def __conversion(
         self, code, var,
         styl):  # This is a Private Method ( __ before method name)
     lexer = get_lexer_by_name("python", stripall=True)
     formatter = HtmlFormatter(linenos=var, cssclass='source', style=styl)
     formatter.noclasses = True
     with open("demo/" + self.__outfil, "w") as f:
         f.write(
             "<i> ------- Formatted the code successfully! ------- </i><br>"
         )
         result = highlight(code, lexer, formatter, f)
     print(f"\nFormatted File is generated as- {self.__outfil}")
Exemple #4
0
def render_asm(instr, address):
    formatter = HtmlFormatter()
    formatter.noclasses = True
    formatter.nowrap = True

    #asm_str = instr.prefix + " " if instr.prefix else ""
    asm_str = instr

    asm_str = highlight(asm_str, NasmLexer(), formatter)
    asm_str = asm_str.replace("span", "font")
    asm_str = asm_str.replace('style="color: ', 'color="')
    asm_str = asm_str.replace('style="border: 1px solid ', 'color="')

    asm_tpl = "<tr><td align='left'>{address:08x} {assembly} </td></tr>"
    return asm_tpl.format(address=address, assembly=asm_str)
Exemple #5
0
    def format_html(self, bun, meat):
        html_headers = ('h1','h2','h2','h3','h4','h5','h6','h7','h8','h9')
        if bun in html_headers:
            #struct.add_header(meat)
            return "<{0}>{1}</{2}>\n".format(bun, meat, bun)
        elif 'ps' in bun:
            #child=struct.get_last(header).add_title(bun)
            ##ps=struct.get_last(header).append_ps(bun)
            ret = ""
            for line in meat.split('\n'):
                if '//#' in line:
                    l=line.split('//#')
                    #child.add_element(l[0], comment=l[1])
                    ##ps.append(l[0])
                    ret+='<p class="ps">{0}<span class="comment">{1}</span></p>\n'.format(l[0],l[1])
                else:
                    #child.add_element(line)
                    ##ps.append(l[0])
                    ret+='<p class="ps">{0}</p>\n'.format(line)
            return ret
        elif 'p' in bun:
            #struct.get_last(header).append_p(meat)
            if self.BR:
                meat=meat.split('\n')
                ret='<p>'
                for line in meat:
                    ret+=line+'<br />'
                ret+='</p>\n'
                return ret
            else:
                return '<p>{0}</p>'.format(meat)
        elif 'code' in bun:
            i = meat.find('>')
            lang = meat[:i]
            code = meat[i+2:-7] #-7 to remove endcode...
            #struct.get_last(header).append_code(lang, code)
            print "Get lexer by name"
            lang_=get_lexer_by_name(lang.lower())
            print lang_

            style_=HtmlFormatter(style='colorful').style
            format_=HtmlFormatter(style=style_)
            format_.noclasses = False
            format_.cssclass='code'
            format_.cssfile='code.css'
            return highlight(code, lang_, format_)
        else:
            print 'bun value was: ', bun
Exemple #6
0
    def _render_asm(self, instr, mnemonic_width, fill_char=""):
        formatter = HtmlFormatter()
        formatter.noclasses = True
        formatter.nowrap = True

        oprnds_str = ", ".join([str(oprnd) for oprnd in instr.operands])

        asm_str  = instr.prefix + " " if instr.prefix else ""
        asm_str += instr.mnemonic + fill_char * (mnemonic_width - len(instr.mnemonic))
        asm_str += " " + oprnds_str if oprnds_str else ""

        # TODO Highlight for ARM too.
        asm_str = highlight(asm_str, NasmLexer(), formatter)
        asm_str = asm_str.replace("span", "font")
        asm_str = asm_str.replace('style="color: ', 'color="')

        return self.asm_tpl.format(address=instr.address, size=instr.size, assembly=asm_str)
    def _render_asm(self, instr, mnemonic_width, options, fill_char=""):
        formatter = HtmlFormatter()
        formatter.noclasses = True
        formatter.nowrap = True

        oprnds_str = ", ".join([oprnd.to_string(**options) for oprnd in instr.operands])

        asm_str  = instr.prefix + " " if instr.prefix else ""
        asm_str += instr.mnemonic + fill_char * (mnemonic_width - len(instr.mnemonic))
        asm_str += " " + oprnds_str if oprnds_str else ""

        # TODO Highlight for ARM too.
        asm_str = highlight(asm_str, NasmLexer(), formatter)
        asm_str = asm_str.replace("span", "font")
        asm_str = asm_str.replace('style="color: ', 'color="')
        asm_str = asm_str.replace('style="border: 1px solid ', 'color="')

        return self.asm_tpl.format(address=instr.address, size=instr.size, assembly=asm_str)
def main_module():
    fname = input('Enter the file\'s name which is to be formatted :')

    try:
        rf = open(fname, 'r')
    except IOError:
        print(f"Couldn't read file {fname}")
        sys.exit()

    with rf:
        code = rf.read()

    L = input(
        '\nPress\n->1 to insert line Numbers\n->Any other key for not : ')
    if L == '1':
        var = True
    else:
        var = False

    style_list = [
        'autumn', 'borland', 'bw', 'colorful', 'default', 'emacs', 'fruity',
        'manni', 'monokai', 'murphy', 'native', 'pastie', 'rrt', 'tango',
        'trac', 'vim', 'friendly', 'perldoc'
    ]

    check = True

    while check:

        styl = input(f"\nSpecify a style from this list :\n{style_list}\n")

        if styl in style_list:
            lexer = get_lexer_by_name("python", stripall=True)
            formatter = HtmlFormatter(linenos=var,
                                      cssclass='source',
                                      style=styl)
            formatter.noclasses = True
            with open('demo/result.html', "w") as f:
                result = highlight(code, lexer, formatter, f)
            check = False
            print('\nFormatted File is generated as - result.html')
        else:
            print('Please select style from the list only')
Exemple #9
0
def view_problem(request):

    problem = Problem.objects.get(id = request.GET.get('pid'))
    if problem.user != request.user :
        problem_list = Share.objects.filter(share_user = request.user).values_list('problem', flat=True)
        if problem.id in problem_list :
            pass
        else :
            raise Http404()
    f = open(str(problem.docfile), 'r')
    code = f.read()
    f.close()
    formatter = HtmlFormatter()
    formatter.noclasses = True
    lexer = get_lexer_for_filename(str(problem.docfile))
    context = Context({
        'problem' : problem,
        'highlighted_code' : highlight(code, lexer, formatter)
    })
    return render(request, 'index_view_problem.html', context)
Exemple #10
0
 def __pygmentize(self, text):
     lexer_name = "%s" % (self.comboBox.currentText())
     lexer = get_lexer_by_name(lexer_name.lower(), stripall=True)
     formatter = HtmlFormatter()
     formatter.noclasses = True
     return highlight(text, lexer, formatter)
Exemple #11
0
    def OnCommand(self, n, cmd_id):
        if cmd_id == self.cmd_show_reasons:
            match = self.items[n]
            reasons = match[len(match) - 1]
            msg = "\n".join(reasons)
            info(msg)
        elif cmd_id == self.cmd_show_source:
            item = self.items[n]
            src_id = int(item[1])
            cur = self.importer.db.cursor()
            sql = "select source from src.functions where id = ?"
            cur.execute(sql, (src_id, ))
            row = cur.fetchone()
            if row is not None:
                fmt = HtmlFormatter()
                fmt.noclasses = True
                fmt.linenos = True
                func = row["source"]
                src = highlight(func, CppLexer(), fmt)
                title = "Source code of %s" % repr(item[2])
                cdiffer = CHtmlViewer()
                cdiffer.Show(src, title)
            cur.close()
        elif cmd_id == self.cmd_import_all:
            if askyn_c(
                    0,
                    "HIDECANCEL\nDo you really want to import all matched functions as well as struct, union, enum and typedef definitions?"
            ) == 1:
                import_items = []
                for item in self.items:
                    src_id, src_name, bin_ea = int(item[1]), item[2], int(
                        item[3], 16)
                    import_items.append([src_id, src_name, bin_ea])

                self.importer.import_items(import_items)
        elif cmd_id == self.cmd_import_selected:
            if len(self.selected_items) == 1 or askyn_c(
                    1,
                    "HIDECANCEL\nDo you really want to import the selected functions?"
            ) == 1:
                import_items = []
                for index in self.selected_items:
                    item = self.items[index]
                    src_id, src_name, bin_ea = int(item[1]), item[2], int(
                        item[3], 16)
                    import_items.append([src_id, src_name, bin_ea])

                import_definitions = askyn_c(
                    0,
                    "HIDECANCEL\nDo you also want to import all struct, union, enum and typedef definitions?"
                ) == 1
                self.importer.import_items(
                    import_items, import_definitions=import_definitions)
        elif cmd_id == self.cmd_diff_c:
            html_diff = CHtmlDiff()
            item = self.items[n]

            src_id = long(item[1])
            cur = self.differ.db.cursor()

            sql = "select source from src.functions where id = ?"
            cur.execute(sql, (src_id, ))
            row = cur.fetchone()
            cur.close()
            if not row:
                Warning("Cannot find the source function.")
                return False

            ea = long(item[3], 16)
            proto = self.differ.decompile_and_get(ea)
            if not proto:
                Warning("Cannot decompile function 0x%08x" % ea)
                return False

            buf1 = indent_source(row[0].decode("utf-8", "ignore"))
            buf2 = proto
            buf2 += u"\n".join(self.differ.pseudo[ea])
            new_buf = indent_source(buf2)
            src = html_diff.make_file(new_buf.split(u"\n"), buf1.split(u"\n"))

            title = "Diff pseudo-source %s - %s" % (item[2], item[4])
            cdiffer = CHtmlViewer()
            cdiffer.Show(src, title)
Exemple #12
0
def cprint(printable: Union[IRModule, PrimFunc],
           style: Optional[str] = None) -> None:
    """
    Print highlighted TVM script string with Pygments
    Parameters
    ----------
    printable : Union[IRModule, PrimFunc]
        The TVM script to be printed
    style : str, optional
        Printing style, auto-detected if None.
    Notes
    -----
    The style parameter follows the Pygments style names or Style objects. Three
    built-in styles are extended: "light", "dark" and "ansi". By default, "light"
    will be used for notebook environment and terminal style will be "ansi" for
    better style consistency. As an fallback when the optional Pygment library is
    not installed, plain text will be printed with a one-time warning to suggest
    installing the Pygment library. Other Pygment styles can be found in
    https://pygments.org/styles/
    """

    try:
        # pylint: disable=import-outside-toplevel
        import pygments
        from pygments import highlight
        from pygments.lexers.python import Python3Lexer
        from pygments.formatters import Terminal256Formatter, HtmlFormatter
        from pygments.style import Style
        from pygments.token import Keyword, Name, Comment, String, Number, Operator
        from packaging import version

        if version.parse(pygments.__version__) < version.parse("2.4.0"):
            raise ImportError("Required Pygments version >= 2.4.0 but got " +
                              pygments.__version__)
    except ImportError as err:
        with warnings.catch_warnings():
            warnings.simplefilter("once", UserWarning)
            install_cmd = sys.executable + ' -m pip install "Pygments>=2.4.0" --upgrade --user'
            warnings.warn(
                str(err) + "\n" +
                "To print highlighted TVM script, please install Pygments:\n" +
                install_cmd,
                category=UserWarning,
            )
        print(printable.script())
    else:

        class JupyterLight(Style):
            """A Jupyter-Notebook-like Pygments style configuration (aka. "light")"""

            background_color = ""
            styles = {
                Keyword: "bold #008000",
                Keyword.Type: "nobold #008000",
                Name.Function: "#0000FF",
                Name.Class: "bold #0000FF",
                Name.Decorator: "#AA22FF",
                String: "#BA2121",
                Number: "#008000",
                Operator: "bold #AA22FF",
                Operator.Word: "bold #008000",
                Comment: "italic #007979",
            }

        class VSCDark(Style):
            """A VSCode-Dark-like Pygments style configuration (aka. "dark")"""

            background_color = ""
            styles = {
                Keyword: "bold #c586c0",
                Keyword.Type: "#82aaff",
                Keyword.Namespace: "#4ec9b0",
                Name.Class: "bold #569cd6",
                Name.Function: "bold #dcdcaa",
                Name.Decorator: "italic #fe4ef3",
                String: "#ce9178",
                Number: "#b5cea8",
                Operator: "#bbbbbb",
                Operator.Word: "#569cd6",
                Comment: "italic #6a9956",
            }

        class AnsiTerminalDefault(Style):
            """The default style for terminal display with ANSI colors (aka. "ansi")"""

            background_color = ""
            styles = {
                Keyword: "bold ansigreen",
                Keyword.Type: "nobold ansigreen",
                Name.Class: "bold ansiblue",
                Name.Function: "bold ansiblue",
                Name.Decorator: "italic ansibrightmagenta",
                String: "ansiyellow",
                Number: "ansibrightgreen",
                Operator: "bold ansimagenta",
                Operator.Word: "bold ansigreen",
                Comment: "italic ansibrightblack",
            }

        is_in_notebook = "ipykernel" in sys.modules  # in notebook env (support html display).

        if style is None:
            # choose style automatically according to the environment:
            style = JupyterLight if is_in_notebook else AnsiTerminalDefault
        elif style == "light":
            style = JupyterLight
        elif style == "dark":
            style = VSCDark
        elif style == "ansi":
            style = AnsiTerminalDefault

        if is_in_notebook:  # print with HTML display
            from IPython.display import display, HTML  # pylint: disable=import-outside-toplevel

            formatter = HtmlFormatter(style=JupyterLight)
            formatter.noclasses = True  # inline styles
            html = highlight(printable.script(), Python3Lexer(), formatter)
            display(HTML(html))
        else:
            print(
                highlight(printable.script(), Python3Lexer(),
                          Terminal256Formatter(style=style)))