Esempio n. 1
0
def start():
    '''Starts the unpyc application.'''

    def copyright(option, opt_str, value, parser, *args, **kwargs):
        print __copyright

    def license(option, opt_str, value, parser, *args, **kwargs):
        print __license

    def version(option, opt_str, value, parser, *args, **kwargs):
        print __version

    def help(option, opt_str, value, parser, *args, **kwargs):
        print '--== Help ==--'
        parser.print_help()
        print

    parser = optparse.OptionParser(usage=__usage, add_help_option=False)

    decompileGroup = optparse.OptionGroup(parser, 'Decompilation')
    decompileGroup.add_option('-D', '--decompile',
              action='store_true', dest='decompile', default=False,
              help='decompile')
    decompileGroup.add_option('--debugDraw',
              action='store_true', dest='debugDraw', default=False,
              help='draw intermediate CFGs')

    disasmGroup = optparse.OptionGroup(parser, 'Disassembly')
    disasmGroup.add_option('-d', '--disassemble',
              action='store_true', dest='disassemble', default=False,
              help='disassemble')
    disasmGroup.add_option('-x', '--xref',
              action='store_true', dest='xref', default=False,
              help='show basic blocks and xrefs')
    disasmGroup.add_option('-v',
              action='count', dest='verbose', default=0,
              help='verbose, use twice for more verbosity')

    commonGroup = optparse.OptionGroup(parser, 'Common')
    commonGroup.add_option('-q', '--quiet',
              action='store_true', dest='quiet', default=False,
              help='don\'t print --== Disasm ==--, #--== Decompile ==--')

    guiGroup = optparse.OptionGroup(parser, 'Gui')
    guiGroup.add_option('-g', '--gui',
              action='store_true', dest='gui', default=False,
              help='gui (control flow graph)')

    infoGroup = optparse.OptionGroup(parser, 'Info')
    infoGroup.add_option('-c', '--copyright',
              action='callback', callback=copyright,
              help='copyright')
    infoGroup.add_option('-l', '--license',
              action='callback', callback=license,
              help='license')
    infoGroup.add_option('-V', '--version',
              action='callback', callback=version,
              help='version')
    infoGroup.add_option('-h', '--help',
              action='callback', callback=help,
              help='show this help message')

    parser.add_option_group(decompileGroup)
    parser.add_option_group(disasmGroup)
    parser.add_option_group(commonGroup)
    parser.add_option_group(guiGroup)
    parser.add_option_group(infoGroup)

    (options, args) = parser.parse_args()

    if options.disassemble or options.decompile or options.gui:
        if len(args) != 1:
            parser.error('incorrect number of arguments')
        else:
            filename = args[0]
            try:
                parser = parse.Parser(filename, verboseDisasm=options.verbose,
                                      xrefDisasm=options.xref)
                disassembler = disasm.Disassembler(parser.co)
                optimizingDisassembler = disasm.Disassembler(parser.co,
                                                           optimizeJumps=True)
                decompiler = decompile.Decompiler(optimizingDisassembler,
                                                  options.debugDraw)
            except (parse.ParseErrorException,
                    parse.IOErrorException,
                    parse.BadFirstObjectException), p:
                print p
                sys.exit(-1)
            except:
Esempio n. 2
0
 def __str__(self):
     od = disasm.Disassembler(self.value, optimizeJumps=True)
     # cut out 'return ' and '\n'
     x = decompile.Decompiler(od).decompile()[7:-1]
     return '(lambda ' + self.getParams() + ': ' + x + ')'
Esempio n. 3
0
def start():
    '''Starts the unpyc application.'''
    def copyright(option, opt_str, value, parser, *args, **kwargs):
        print __copyright

    def license(option, opt_str, value, parser, *args, **kwargs):
        print __license

    def version(option, opt_str, value, parser, *args, **kwargs):
        print __version

    def help(option, opt_str, value, parser, *args, **kwargs):
        print '--== Help ==--'
        parser.print_help()
        print

    parser = optparse.OptionParser(usage=__usage, add_help_option=False)

    decompileGroup = optparse.OptionGroup(parser, 'Decompilation')
    decompileGroup.add_option('-D',
                              '--decompile',
                              action='store_true',
                              dest='decompile',
                              default=False,
                              help='decompile')
    decompileGroup.add_option('--debugDraw',
                              action='store_true',
                              dest='debugDraw',
                              default=False,
                              help='draw intermediate CFGs')
    decompileGroup.add_option('--test',
              dest='selfTest', default='-1', metavar='STRICT',
              help='run diff of the original and generated ' \
                   'pyc files after decompilation '\
                   'if STRICT is 0 - ignore filename, firstlineno, lnotab; ' \
                   'if STRICT is 1 - ignore filename; ' \
                   'if STRICT is 2 - pyc files should be identical; ' \
                   '$? is 0 if diff was ok, 1 if not')

    disasmGroup = optparse.OptionGroup(parser, 'Disassembly')
    disasmGroup.add_option('-d',
                           '--disassemble',
                           action='store_true',
                           dest='disassemble',
                           default=False,
                           help='disassemble')
    disasmGroup.add_option('-x',
                           '--xref',
                           action='store_true',
                           dest='xref',
                           default=False,
                           help='show basic blocks and xrefs')
    disasmGroup.add_option('-v',
                           action='count',
                           dest='verbose',
                           default=0,
                           help='verbose, use twice for more verbosity')

    commonGroup = optparse.OptionGroup(parser, 'Common')
    commonGroup.add_option('--colist',
                           action='store_true',
                           dest='colist',
                           default=False,
                           help='list names of all code-objects')
    commonGroup.add_option(
        '--co',
        dest='coname',
        default=None,
        metavar='NAME',
        help='specify code-object to work with in a dotted manner')
    commonGroup.add_option(
        '-q',
        '--quiet',
        action='store_true',
        dest='quiet',
        default=False,
        help='don\'t print --== HEADER ==-- and diff output')

    diffGroup = optparse.OptionGroup(parser, 'Diff')
    diffGroup.add_option('--diff',
                         dest='diff',
                         default='-1',
                         metavar='STRICT',
                         help='compare two pyc files, STRICT is 0, 1, 2')

    guiGroup = optparse.OptionGroup(parser, 'Gui')
    guiGroup.add_option('-g',
                        '--gui',
                        action='store_true',
                        dest='gui',
                        default=False,
                        help='gui (control flow graph)')

    infoGroup = optparse.OptionGroup(parser, 'Info')
    infoGroup.add_option('-c',
                         '--copyright',
                         action='callback',
                         callback=copyright,
                         help='copyright')
    infoGroup.add_option('-l',
                         '--license',
                         action='callback',
                         callback=license,
                         help='license')
    infoGroup.add_option('-V',
                         '--version',
                         action='callback',
                         callback=version,
                         help='version')
    infoGroup.add_option('-h',
                         '--help',
                         action='callback',
                         callback=help,
                         help='show this help message')

    parser.add_option_group(decompileGroup)
    parser.add_option_group(disasmGroup)
    parser.add_option_group(commonGroup)
    parser.add_option_group(diffGroup)
    parser.add_option_group(guiGroup)
    parser.add_option_group(infoGroup)

    (options, args) = parser.parse_args()

    if options.disassemble or options.decompile or \
       options.gui or options.colist:
        if len(args) != 1:
            parser.error('incorrect number of arguments')
        else:
            filename = args[0]
            co = None
            full = True
            try:
                parser = parse.Parser(filename,
                                      verboseDisasm=options.verbose,
                                      xrefDisasm=options.xref)
                if options.coname is not None:
                    full = False
                    co = parser.findCoByAbsName(options.coname)
                    if co is None:
                        raise parse.CoNotFoundException(options.coname)
                else:
                    co = parser.co
                disassembler = disasm.Disassembler(co)
                optimizingDisassembler = disasm.Disassembler(
                    co, optimizeJumps=True)
                decompiler = decompile.Decompiler(optimizingDisassembler,
                                                  options.debugDraw)
            except (parse.ParseErrorException, parse.IOErrorException,
                    parse.BadFirstObjectException,
                    parse.CoNotFoundException), p:
                print p
                sys.exit(-1)
            except: