def _show_output(mappings, instance, validated, rtype): """ Return results in the rtype format. Results of non validated instance are not very interesting. """ if rtype == 'string': if not validated: str_fn = lambda x: str(x) else: outputter = text.RecursiveTextOutputter(mappings) str_fn = lambda x: outputter.parse(x) return "(%s\n, %s)" % (str_fn(instance), validated) # else {'json', 'pickled', 'python'} : # cast in pyObject parser = python.PythonOutputter(mappings) pyObj = parser.parse(instance) # last check to clean the structure from any ctypes Structure if python.findCtypesInPyObj(pyObj): raise HaystackError( 'Bug in framework, some Ctypes are still in the return results. Please Report test unit.') # finally if rtype == 'python': # pyobj return (pyObj, validated) elif rtype == 'json': # jsoned # cirular refs kills it check_circular=False, return json.dumps((pyObj, validated), default=python.json_encode_pyobj) elif rtype == 'pickled': # pickled return pickle.dumps((pyObj, validated)) raise ValueError('rtype should have a valid value')
def _output(mappings, outs, rtype): """ Return results in the rtype format""" if len(outs) == 0: log.info('Found no occurence.') return None if rtype == 'string': outputter = text.RecursiveTextOutputter(mappings) ret = '[' for ss, addr in outs: ret += "# --------------- 0x%lx \n%s" % (addr, outputter.parse(ss)) pass ret += ']' return ret parser = python.PythonOutputter(mappings) ret = [(parser.parse(ss), addr) for ss, addr in outs] # last check to clean the structure from any ctypes Structure if python.findCtypesInPyObj(ret): raise HaystackError( 'Bug in framework, some Ctypes are still in the return results. Please Report test unit.') # finally if rtype == 'python': # pyobj return ret elif rtype == 'json': # jsoned # cirular refs kills it check_circular=False, return json.dumps(ret, default=basicmodel.json_encode_pyobj) elif rtype == 'pickled': # pickled return pickle.dumps(ret) return None
def output_to_python(memory_handler, results): """ Transform ctypes results in a non-ctypes python object format :param memory_handler: IMemoryHandler :param results: results from the search_record :return: """ if not isinstance(results, list): raise TypeError('Feed me a list of results') # also generate POPOs my_model = memory_handler.get_model() pythoned_modules = my_model.get_pythoned_modules().keys() for module_name, module in my_model.get_imported_modules().items(): if module_name not in pythoned_modules: my_model.build_python_class_clones(module) # parse and generate instances parser = python.PythonOutputter(memory_handler) ret = [(parser.parse(ss), addr) for ss, addr in results] # last check to clean the structure from any ctypes Structure if python.findCtypesInPyObj(memory_handler, ret): raise HaystackError( 'Bug in framework, some Ctypes are still in the return results. Please Report test unit.') return ret