Exemplo n.º 1
0
def decompileClass(path=[], targets=None, outpath=None, plugins=[]):
    if outpath is None:
        outpath = os.getcwd()

    e = Environment()
    for part in path:
        e.addToPath(part)

    makeGraph = makeCallback(plugins)
    start_time = time.time()
    # random.shuffle(targets)
    with e:  #keep jars open
        for i, target in enumerate(targets):
            print 'processing target {}, {} remaining'.format(
                target,
                len(targets) - i)
            c = e.getClass(target)
            source = javaclass.generateAST(c, makeGraph).print_()
            #The single class decompiler doesn't add package declaration currently so we add it here
            if '/' in target:
                package = 'package {};\n\n'.format(
                    target.replace('/', '.').rpartition('.')[0])
                source = package + source

            filename = script_util.writeFile(outpath, c.name, '.java', source)
            print 'Class written to', filename
            print time.time() - start_time, ' seconds elapsed'
            deleteUnusued(c)
Exemplo n.º 2
0
def decompileClass(path=[], targets=None, outpath=None, skipMissing=False):
    writeout = script_util.fileDirOut(outpath, '.java')

    e = Environment()
    for part in path:
        e.addToPath(part)

    start_time = time.time()
    # random.shuffle(targets)
    with e: #keep jars open
        for i,target in enumerate(targets):
            script_util.printVerbose('processing target {}, {} remaining'.format(target, len(targets)-i))

            try:
                c = e.getClass(target)
                source = javaclass.generateAST(c, makeGraph).print_()
            except ClassLoaderError as err:
                if skipMissing:
                    print 'failed to decompile {} due to missing or invalid class {}'.format(target, err.data)
                    continue
                else:
                    raise

            #The single class decompiler doesn't add package declaration currently so we add it here
            if '/' in target:
                package = 'package {};\n\n'.format(target.replace('/','.').rpartition('.')[0])
                source = package + source

            filename = writeout(c.name, source)
            script_util.printVerbose('Class written to ' + filename)
            script_util.printVerbose('{} seconds elapsed'.format(time.time() - start_time))
            deleteUnusued(c)
Exemplo n.º 3
0
def decompileClass(path=[],
                   targets=None,
                   outpath=None,
                   skip_errors=False,
                   add_throws=False,
                   magic_throw=False):
    out = script_util.makeWriter(outpath, '.java')

    e = Environment()
    for part in path:
        e.addToPath(part)

    start_time = time.time()
    # random.shuffle(targets)
    with e, out:
        printer = visitor.DefaultVisitor()
        for i, target in enumerate(targets):
            if isinstance(target, unicode):
                target = target.encode('utf8')
            print('processing target {}, {} remaining'.format(
                target,
                len(targets) - i))

            try:
                c = e.getClass(target)
                makeGraphCB = functools.partial(makeGraph, magic_throw)
                source = printer.visit(
                    javaclass.generateAST(c,
                                          makeGraphCB,
                                          skip_errors,
                                          add_throws=add_throws))
            except Exception as err:
                if not skip_errors:
                    raise
                if isinstance(err, ClassLoaderError):
                    print(
                        'Failed to decompile {} due to missing or invalid class {}'
                        .format(target, err.data))
                else:
                    import traceback
                    print(traceback.format_exc())
                continue

            # The single class decompiler doesn't add package declaration currently so we add it here
            if '/' in target:
                package = 'package {};\n\n'.format(
                    escapeString(target.replace('/', '.').rpartition('.')[0]))
                source = package + source

            filename = out.write(c.name.encode('utf8'), source)
            print('Class written to', filename)
            print(time.time() - start_time, ' seconds elapsed')
            deleteUnusued(c)
        print(len(e.classes) - len(targets), 'extra classes loaded')
Exemplo n.º 4
0
def decompileClass(path=[],
                   target=None,
                   outpath=None,
                   skip_errors=False,
                   add_throws=False,
                   magic_throw=False):
    out = open(outPath, "w")

    e = Environment()
    for part in path:
        e.addToPath(part)

    start_time = time.time()
    # random.shuffle(targets)
    with e, out:
        printer = visitor.DefaultVisitor()
        try:
            c = None
            with open(target, "rb") as targetF:
                stream = Reader(data=targetF.read())
                c = ClassFile(stream)
                c.env = e
                e.classes[c.name] = c

            makeGraphCB = functools.partial(makeGraph, magic_throw)
            source = printer.visit(
                javaclass.generateAST(c,
                                      makeGraphCB,
                                      skip_errors,
                                      add_throws=add_throws))
        except Exception as err:
            if not skip_errors:
                raise
            if isinstance(err, ClassLoaderError):
                print(
                    'Failed to decompile {} due to missing or invalid class {}'
                    .format(target, err.data))
            else:
                import traceback
                print(traceback.format_exc())
            return

        # The single class decompiler doesn't add package declaration currently so we add it here
        if '/' in target:
            package = 'package {};\n\n'.format(
                escapeString(target.replace('/', '.').rpartition('.')[0]))
            source = package + source

        out.write(source)
        print(time.time() - start_time, ' seconds elapsed')
        deleteUnusued(c)
        print(len(e.classes) - 1, 'extra classes loaded')