Search in sources :

Example 46 with CConnection

use of com.google.security.zynamics.binnavi.Database.CConnection in project binnavi by google.

the class PostgreSQLViewsLoader method loadViewFunctionMapping.

/**
   * Loads the view -> function mapping from the database.
   * 
   * @param provider The SQL provider that provides the connection.
   * @param flowgraphs List of all native Flow graph views of a module.
   * @param functions List of all functions of a module.
   * @param module The module from which to load the mapping.
   * 
   * @return A view -> function mapping and a function -> view mapping.
   * 
   * @throws CouldntLoadDataException Thrown if the mapping could not be loaded.
   */
public static final ImmutableBiMap<INaviView, INaviFunction> loadViewFunctionMapping(final AbstractSQLProvider provider, final List<IFlowgraphView> flowgraphs, final List<INaviFunction> functions, final CModule module) throws CouldntLoadDataException {
    checkArguments(provider, module, flowgraphs, functions);
    final HashMap<Integer, INaviView> viewmap = new HashMap<Integer, INaviView>();
    for (final IFlowgraphView view : flowgraphs) {
        viewmap.put(view.getConfiguration().getId(), view);
    }
    final HashMap<IAddress, INaviFunction> functionMap = new HashMap<IAddress, INaviFunction>();
    for (final INaviFunction function : functions) {
        functionMap.put(function.getAddress(), function);
    }
    final CConnection connection = provider.getConnection();
    final String query = "SELECT view_id, function FROM " + CTableNames.FUNCTION_VIEWS_TABLE + " WHERE module_id = " + module.getConfiguration().getId();
    final HashMap<INaviView, INaviFunction> viewFunctionMap = new HashMap<INaviView, INaviFunction>();
    try {
        final ResultSet resultSet = connection.executeQuery(query, true);
        try {
            while (resultSet.next()) {
                final INaviView view = viewmap.get(resultSet.getInt("view_id"));
                final INaviFunction function = functionMap.get(PostgreSQLHelpers.loadAddress(resultSet, "function"));
                if ((view != null) && (function != null)) {
                    viewFunctionMap.put(view, function);
                }
            }
        } finally {
            resultSet.close();
        }
        return new ImmutableBiMap.Builder<INaviView, INaviFunction>().putAll(viewFunctionMap).build();
    } catch (final SQLException e) {
        throw new CouldntLoadDataException(e);
    }
}
Also used : IFlowgraphView(com.google.security.zynamics.binnavi.disassembly.IFlowgraphView) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) IAddress(com.google.security.zynamics.zylib.disassembly.IAddress) CConnection(com.google.security.zynamics.binnavi.Database.CConnection) INaviView(com.google.security.zynamics.binnavi.disassembly.views.INaviView) ResultSet(java.sql.ResultSet) INaviFunction(com.google.security.zynamics.binnavi.disassembly.INaviFunction)

Example 47 with CConnection

use of com.google.security.zynamics.binnavi.Database.CConnection in project binnavi by google.

the class PostgreSQLModuleCallgraphsLoader method loadModuleCallgraphs.

/**
   * Loads the call graph views of a module. Depending on the view type argument, this function can
   * load the native call graph view or non-native call graph views.
   * 
   * The arguments module, viewTagManager, and nodeTagManager must belong to the database connected
   * to by the provider argument.
   * 
   * @param provider The connection to the database.
   * @param module The module whose call graph views are loaded.
   * @param viewTagManager The tag manager responsible for tagging the views.
   * @param nodeTagManager The tag manager responsible for tagging view nodes.
   * @param viewType The type of the views to load.
   * 
   * @return The loaded views.
   * 
   * @throws CouldntLoadDataException Thrown if the views could not be loaded.
   */
private static IFilledList<ICallgraphView> loadModuleCallgraphs(final AbstractSQLProvider provider, final CModule module, final CTagManager viewTagManager, final ITagManager nodeTagManager, final ViewType viewType) throws CouldntLoadDataException {
    PostgreSQLViewsLoader.checkArguments(provider, module, viewTagManager);
    final String query = "SELECT * FROM load_module_call_graphs(?, ?)";
    try {
        final CConnection connection = provider.getConnection();
        final PreparedStatement statement = connection.getConnection().prepareStatement(query);
        statement.setInt(1, module.getConfiguration().getId());
        statement.setObject(2, viewType == ViewType.Native ? "native" : "non-native", Types.OTHER);
        final ResultSet resultSet = statement.executeQuery();
        final Map<Integer, Set<CTag>> tags = PostgreSQLModuleViewsLoader.loadTags(connection, module, viewTagManager);
        return new FilledList<ICallgraphView>(processQueryResults(resultSet, module, tags, nodeTagManager, provider, new ArrayList<CView>(), viewType, GraphType.CALLGRAPH));
    } catch (final SQLException exception) {
        throw new CouldntLoadDataException(exception);
    }
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) Set(java.util.Set) ResultSet(java.sql.ResultSet) FilledList(com.google.security.zynamics.zylib.types.lists.FilledList) IFilledList(com.google.security.zynamics.zylib.types.lists.IFilledList) SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement)

Example 48 with CConnection

use of com.google.security.zynamics.binnavi.Database.CConnection in project binnavi by google.

the class PostgreSQLModuleFlowgraphsLoader method loadFlowGraphInformation.

public static ImmutableNaviViewConfiguration loadFlowGraphInformation(final SQLProvider provider, final INaviModule module, final Integer viewId) throws CouldntLoadDataException {
    Preconditions.checkNotNull(provider, "IE02275: provider argument can not be null");
    Preconditions.checkNotNull(module, "IE02394: module argument can not be null");
    Preconditions.checkNotNull(viewId, "IE02419: viewId argument can not be null");
    final CConnection connection = provider.getConnection();
    final String query = " SELECT * FROM load_module_flowgraph_information(?,?) ";
    try {
        final PreparedStatement statement = connection.getConnection().prepareStatement(query);
        statement.setInt(1, module.getConfiguration().getId());
        statement.setInt(2, viewId);
        final ResultSet resultSet = statement.executeQuery();
        while (resultSet.next()) {
            final int databaseViewId = resultSet.getInt("view_id");
            final String name = PostgreSQLHelpers.readString(resultSet, "name");
            final String description = PostgreSQLHelpers.readString(resultSet, "description");
            final ViewType viewType = resultSet.getString("type").equalsIgnoreCase("native") ? ViewType.Native : ViewType.NonNative;
            final Timestamp creationDate = resultSet.getTimestamp("creation_date");
            final Timestamp modificationDate = resultSet.getTimestamp("modification_date");
            final boolean isStared = resultSet.getBoolean("stared");
            final int nodeCount = resultSet.getInt("bbcount");
            final int edgeCount = resultSet.getInt("edgecount");
            final ImmutableNaviViewConfiguration viewConfiguration = new ImmutableNaviViewConfiguration(databaseViewId, name, description, viewType, creationDate, modificationDate, isStared, nodeCount, edgeCount);
            return viewConfiguration;
        }
        return null;
    } catch (final SQLException exception) {
        throw new CouldntLoadDataException(exception);
    }
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) ImmutableNaviViewConfiguration(com.google.security.zynamics.binnavi.disassembly.views.ImmutableNaviViewConfiguration) SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp) ViewType(com.google.security.zynamics.zylib.disassembly.ViewType)

Example 49 with CConnection

use of com.google.security.zynamics.binnavi.Database.CConnection in project binnavi by google.

the class PostgreSQLModuleMixedGraphsLoader method loadMixedgraphs.

/**
   * Loads the mixed-graph views of a module. These mixed graph views are necessarily non-native
   * because there are no native mixed graph views.
   * 
   * The module, the view tag manager, and the node tag manager must be stored in the database
   * connected to by the provider argument.
   * 
   * @param provider The SQL provider that provides the connection.
   * @param module The module from where the views are loaded.
   * @param viewTagManager View tag manager that contains all view tags of the database.
   * @param nodeTagManager The tag manager responsible for tagging view nodes.
   * 
   * @return A list of non-native mixed-graph views.
   * 
   * @throws CouldntLoadDataException Thrown if the views could not be loaded.
   */
public static IFilledList<INaviView> loadMixedgraphs(final AbstractSQLProvider provider, final CModule module, final CTagManager viewTagManager, final CTagManager nodeTagManager) throws CouldntLoadDataException {
    checkArguments(provider, module, viewTagManager);
    final String query = "SELECT * FROM load_module_mixed_graph(?)";
    try {
        final CConnection connection = provider.getConnection();
        final PreparedStatement statement = connection.getConnection().prepareStatement(query);
        statement.setInt(1, module.getConfiguration().getId());
        final ResultSet resultSet = statement.executeQuery();
        final Map<Integer, Set<CTag>> tags = loadTags(connection, module, viewTagManager);
        return new FilledList<INaviView>(processQueryResults(resultSet, module, tags, nodeTagManager, provider, new ArrayList<CView>(), ViewType.NonNative, GraphType.MIXED_GRAPH));
    } catch (final SQLException exception) {
        throw new CouldntLoadDataException(exception);
    }
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) Set(java.util.Set) ResultSet(java.sql.ResultSet) FilledList(com.google.security.zynamics.zylib.types.lists.FilledList) IFilledList(com.google.security.zynamics.zylib.types.lists.IFilledList) SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement)

Example 50 with CConnection

use of com.google.security.zynamics.binnavi.Database.CConnection in project binnavi by google.

the class PostgreSQLNodeLoader method loadNodes.

/**
   * Loads the view nodes of a view.
   * 
   * @param provider The connection to the database.
   * @param view The view whose nodes are loaded.
   * @param modules All modules that belong to the database.
   * @param nodeTagManager Tag manager responsible for tagging the nodes of the view.
   * 
   * @return The loaded nodes.
   * 
   * @throws SQLException Thrown of loading the nodes failed.
   * @throws CPartialLoadException Thrown if loading the nodes failed because a necessary module was
   *         not loaded.
   * @throws CouldntLoadDataException
   */
public static List<INaviViewNode> loadNodes(final AbstractSQLProvider provider, final INaviView view, final List<INaviModule> modules, final CTagManager nodeTagManager) throws SQLException, CPartialLoadException, CouldntLoadDataException {
    final List<INaviViewNode> nodes = new ArrayList<INaviViewNode>();
    PostgreSQLGroupNodeLoader.load(provider, view, nodes);
    PostgreSQLFunctionNodeLoader.load(provider, view, nodes);
    PostgreSQLCodeNodeLoader.load(provider, view, nodes, modules);
    PostgreSQLTextNodeLoader.load(provider, view, nodes);
    // It is very, very important to return the nodes in the order of their IDs
    // because when a graph is Saved As, the order of the loaded nodes is compared
    // to the order of the nodes before the graph was saved.
    //
    // Furthermore this must happen before group nodes are set up.
    // TODO: sp has said this sometime in the past without any reasoning why
    // therefore this has to be checked and understood otherwise this code is just
    // burning cycles.
    Collections.sort(nodes, new Comparator<INaviViewNode>() {

        @Override
        public int compare(final INaviViewNode lhs, final INaviViewNode rhs) {
            return lhs.getId() - rhs.getId();
        }
    });
    final CConnection connection = provider.getConnection();
    PostgreSQLGroupNodeLoader.setupGroupNodes(connection, view, nodes);
    loadNodeTags(connection, nodes, nodeTagManager);
    return nodes;
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) ArrayList(java.util.ArrayList) INaviViewNode(com.google.security.zynamics.binnavi.disassembly.INaviViewNode)

Aggregations

CConnection (com.google.security.zynamics.binnavi.Database.CConnection)70 SQLException (java.sql.SQLException)59 CouldntSaveDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException)31 PreparedStatement (java.sql.PreparedStatement)25 ResultSet (java.sql.ResultSet)25 CouldntLoadDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException)20 ArrayList (java.util.ArrayList)15 CouldntDeleteException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntDeleteException)7 CTag (com.google.security.zynamics.binnavi.Tagging.CTag)6 IAddress (com.google.security.zynamics.zylib.disassembly.IAddress)6 Timestamp (java.sql.Timestamp)6 HashMap (java.util.HashMap)6 Set (java.util.Set)6 DebuggerTemplate (com.google.security.zynamics.binnavi.debug.debugger.DebuggerTemplate)5 AbstractSQLProvider (com.google.security.zynamics.binnavi.Database.AbstractSQLProvider)4 INaviEdge (com.google.security.zynamics.binnavi.disassembly.INaviEdge)4 INaviViewNode (com.google.security.zynamics.binnavi.disassembly.INaviViewNode)4 ExpensiveBaseTest (com.google.security.zynamics.binnavi.disassembly.types.ExpensiveBaseTest)4 CallableStatement (java.sql.CallableStatement)4 Test (org.junit.Test)4