Search in sources :

Example 26 with CTag

use of com.google.security.zynamics.binnavi.Tagging.CTag in project binnavi by google.

the class PostgreSQLViewTagLoader method loadViewTags.

/**
   * Loads view tags from the database.
   * 
   * @param connection Connection to a SQL database.
   * @param tableName Table name from where the tags are loaded.
   * @param containerColumn Column that identifies the tag container.
   * @param containerId ID of the tag container.
   * @param viewTagManager View tag manager that contains all tags of the database.
   * 
   * @return A pair of view IDs and the tags the views are tagged with.
   * 
   * @throws SQLException Thrown if the tags could not be loaded.
   */
public static Map<Integer, Set<CTag>> loadViewTags(final CConnection connection, final String tableName, final String containerColumn, final int containerId, final CTagManager viewTagManager) throws SQLException {
    final Map<Integer, Set<CTag>> setTag = new HashMap<Integer, Set<CTag>>();
    final String query = "SELECT " + CTableNames.TAGGED_VIEWS_TABLE + ".view_id, tag_id" + " FROM " + CTableNames.TAGGED_VIEWS_TABLE + " JOIN " + tableName + " ON " + tableName + ".view_id = " + CTableNames.TAGGED_VIEWS_TABLE + ".view_id" + " WHERE " + containerColumn + " = " + containerId + " ORDER BY view_id";
    final ResultSet resultSet = connection.executeQuery(query, true);
    try {
        int currentView = 0;
        Set<CTag> currentTags = new HashSet<CTag>();
        while (resultSet.next()) {
            final int view = resultSet.getInt("view_id");
            final int tagId = resultSet.getInt("tag_id");
            if (currentView == 0) {
                currentView = view;
            }
            if (currentView != view) {
                setTag.put(currentView, currentTags);
                currentTags = new HashSet<CTag>();
                currentView = view;
            }
            currentTags.add(CTagHelpers.findTag(viewTagManager.getRootTag(), tagId));
        }
        if (!currentTags.isEmpty()) {
            setTag.put(currentView, currentTags);
        }
    } finally {
        resultSet.close();
    }
    return setTag;
}
Also used : HashSet(java.util.HashSet) ResultSet(java.sql.ResultSet) Set(java.util.Set) HashMap(java.util.HashMap) CTag(com.google.security.zynamics.binnavi.Tagging.CTag) ResultSet(java.sql.ResultSet) HashSet(java.util.HashSet)

Example 27 with CTag

use of com.google.security.zynamics.binnavi.Tagging.CTag in project binnavi by google.

the class PostgreSQLViewsLoader method getNodeTags.

/**
   * Loads the node tags of the views of a project.
   * 
   * @param connection Provides the connection to the database.
   * @param project The project whose node-tagged views are determined.
   * @param nodeTagManager Provides the available node tags.
   * 
   * @return Maps view ID -> node tags used in the view.
   * 
   * @throws SQLException Thrown if the data could not be loaded.
   */
private static Map<Integer, Set<CTag>> getNodeTags(final CConnection connection, final INaviProject project, final ITagManager nodeTagManager) throws SQLException {
    final Map<Integer, Set<CTag>> tagMap = new HashMap<Integer, Set<CTag>>();
    final String query = " SELECT * FROM load_project_node_tags(?) ";
    final PreparedStatement statement = connection.getConnection().prepareStatement(query);
    statement.setInt(1, project.getConfiguration().getId());
    final ResultSet resultSet = statement.executeQuery();
    try {
        while (resultSet.next()) {
            final int viewId = resultSet.getInt("view_id");
            final int tagId = resultSet.getInt("tag_id");
            if (!tagMap.containsKey(viewId)) {
                tagMap.put(viewId, new HashSet<CTag>());
            }
            final CTag tag = CTagHelpers.findTag(nodeTagManager.getRootTag(), tagId);
            if (tag != null) {
                tagMap.get(viewId).add(tag);
            }
        }
    } finally {
        resultSet.close();
    }
    return tagMap;
}
Also used : HashSet(java.util.HashSet) ResultSet(java.sql.ResultSet) Set(java.util.Set) HashMap(java.util.HashMap) CTag(com.google.security.zynamics.binnavi.Tagging.CTag) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 28 with CTag

use of com.google.security.zynamics.binnavi.Tagging.CTag in project binnavi by google.

the class PostgreSQLViewsLoader method processQueryResults.

/**
   * Processes the results of a view loading query.
   * 
   * @param resultSet Contains the results of the SQL query.
   * @param module The module the views were loaded for.
   * @param tags Map that contains the tags the views are tagged with.
   * @param nodeTagManager Provides the node tags.
   * @param provider The connection to the database.
   * @param views The loaded views are stored in this list.
   * @param viewType View type of the loaded views.
   * @param graphType Graph type of the loaded views.
   * 
   * @return The loaded views.
   * 
   * @throws SQLException Thrown if the views could not be loaded.
   */
protected static final List<CView> processQueryResults(final ResultSet resultSet, final INaviModule module, final Map<Integer, Set<CTag>> tags, final ITagManager nodeTagManager, final SQLProvider provider, final List<CView> views, final ViewType viewType, final GraphType graphType) throws SQLException {
    final Map<Integer, Set<CTag>> nodeTagMap = getNodeTags(provider.getConnection(), module, nodeTagManager);
    try {
        while (resultSet.next()) {
            final int viewId = resultSet.getInt("view_id");
            final String name = PostgreSQLHelpers.readString(resultSet, "name");
            final String description = PostgreSQLHelpers.readString(resultSet, "description");
            final Timestamp creationDate = resultSet.getTimestamp("creation_date");
            final Timestamp modificationDate = resultSet.getTimestamp("modification_date");
            final boolean starState = resultSet.getBoolean("stared");
            final int nodeCount = resultSet.getInt("bbcount");
            final int edgeCount = resultSet.getInt("edgecount");
            final Set<CTag> viewTags = tags.containsKey(viewId) ? tags.get(viewId) : new HashSet<CTag>();
            final Set<CTag> nodeTags = nodeTagMap.containsKey(viewId) ? nodeTagMap.get(viewId) : new HashSet<CTag>();
            final CModuleViewGenerator generator = new CModuleViewGenerator(provider, module);
            views.add(generator.generate(viewId, name, description, viewType, graphType, creationDate, modificationDate, nodeCount, edgeCount, viewTags, nodeTags, starState));
        }
        return views;
    } finally {
        resultSet.close();
    }
}
Also used : CModuleViewGenerator(com.google.security.zynamics.binnavi.Database.CModuleViewGenerator) HashSet(java.util.HashSet) ResultSet(java.sql.ResultSet) Set(java.util.Set) CTag(com.google.security.zynamics.binnavi.Tagging.CTag) Timestamp(java.sql.Timestamp)

Example 29 with CTag

use of com.google.security.zynamics.binnavi.Tagging.CTag in project binnavi by google.

the class PostgreSQLViewsLoader method processQueryResults.

/**
   * Processes the results of a view loading query.
   * 
   * @param resultSet Contains the results of the SQL query.
   * @param project The project the views were loaded for.
   * @param tags Map that contains the tags the views are tagged with.
   * @param nodeTagManager Provides the node tags.
   * @param provider The connection to the database.
   * @param views The loaded views are stored in this list.
   * @param viewType View type of the loaded views.
   * @param graphType Graph type of the loaded views.
   * 
   * @return The loaded views.
   * 
   * @throws SQLException Thrown if the views could not be loaded.
   */
protected static final List<CView> processQueryResults(final ResultSet resultSet, final INaviProject project, final Map<Integer, Set<CTag>> tags, final ITagManager nodeTagManager, final SQLProvider provider, final List<CView> views, final ViewType viewType, final GraphType graphType) throws SQLException {
    final Map<Integer, Set<CTag>> nodeTagMap = getNodeTags(provider.getConnection(), project, nodeTagManager);
    try {
        while (resultSet.next()) {
            final int viewId = resultSet.getInt("view_id");
            final String name = PostgreSQLHelpers.readString(resultSet, "name");
            final String description = PostgreSQLHelpers.readString(resultSet, "description");
            final Timestamp creationDate = resultSet.getTimestamp("creation_date");
            final Timestamp modificationDate = resultSet.getTimestamp("modification_date");
            final boolean starState = resultSet.getBoolean("stared");
            final int nodeCount = resultSet.getInt("bbcount");
            final int edgeCount = resultSet.getInt("edgecount");
            final Set<CTag> viewTags = tags.containsKey(viewId) ? tags.get(viewId) : new HashSet<CTag>();
            final Set<CTag> nodeTags = nodeTagMap.containsKey(viewId) ? nodeTagMap.get(viewId) : new HashSet<CTag>();
            final CProjectViewGenerator generator = new CProjectViewGenerator(provider, project);
            views.add(generator.generate(viewId, name, description, viewType, graphType, creationDate, modificationDate, nodeCount, edgeCount, viewTags, nodeTags, starState));
        }
        return views;
    } finally {
        resultSet.close();
    }
}
Also used : HashSet(java.util.HashSet) ResultSet(java.sql.ResultSet) Set(java.util.Set) CTag(com.google.security.zynamics.binnavi.Tagging.CTag) CProjectViewGenerator(com.google.security.zynamics.binnavi.Database.CProjectViewGenerator) Timestamp(java.sql.Timestamp)

Example 30 with CTag

use of com.google.security.zynamics.binnavi.Tagging.CTag in project binnavi by google.

the class PostgreSQLNodeSaver method saveTags.

/**
   *
   * TODO (timkornau): this code here has serious issues and is in no way anything that we want to
   * keep.
   *
   * Saves the node tags to the database.
   *
   * @param connection The connection to the database.
   * @param nodes The nodes to save.
   * @param firstNode Database index of the first node.
   *
   * @throws SQLException Thrown if saving the tags failed.
   */
protected static void saveTags(final CConnection connection, final List<INaviViewNode> nodes, final int firstNode) throws SQLException {
    int counter = firstNode;
    final String deleteStatement = "DELETE FROM " + CTableNames.TAGGED_NODES_TABLE + " WHERE node_id IN (%s)";
    final String insertStatement = "INSERT INTO " + CTableNames.TAGGED_NODES_TABLE + " VALUES %s ";
    boolean isFirst = true;
    final StringBuilder range = new StringBuilder();
    for (int i = 0; i < nodes.size(); i++) {
        if (isFirst) {
            range.append(counter);
            isFirst = false;
            continue;
        }
        range.append(", ");
        range.append(counter);
        ++counter;
    }
    if (range.length() != 0) {
        connection.executeUpdate(String.format(deleteStatement, range.toString()), true);
    }
    counter = firstNode;
    final StringBuilder insert = new StringBuilder();
    isFirst = true;
    for (final INaviViewNode node : nodes) {
        final Iterator<CTag> it = node.getTagsIterator();
        while (it.hasNext()) {
            final CTag tag = it.next();
            insert.append(isFirst ? "" : ",");
            insert.append('(');
            insert.append(counter);
            insert.append(", ");
            insert.append(tag.getId());
            insert.append(')');
            isFirst = false;
        }
        ++counter;
    }
    if (insert.length() != 0) {
        connection.executeUpdate(String.format(insertStatement, insert.toString()), true);
    }
}
Also used : CTag(com.google.security.zynamics.binnavi.Tagging.CTag) INaviViewNode(com.google.security.zynamics.binnavi.disassembly.INaviViewNode)

Aggregations

CTag (com.google.security.zynamics.binnavi.Tagging.CTag)113 Test (org.junit.Test)55 MockSqlProvider (com.google.security.zynamics.binnavi.Database.MockClasses.MockSqlProvider)42 CTagManager (com.google.security.zynamics.binnavi.Tagging.CTagManager)35 Date (java.util.Date)28 CModule (com.google.security.zynamics.binnavi.disassembly.Modules.CModule)24 CAddress (com.google.security.zynamics.zylib.disassembly.CAddress)24 MockView (com.google.security.zynamics.binnavi.disassembly.MockView)21 MockDatabase (com.google.security.zynamics.binnavi.Database.MockClasses.MockDatabase)20 CView (com.google.security.zynamics.binnavi.disassembly.views.CView)18 ArrayList (java.util.ArrayList)17 Tree (com.google.security.zynamics.zylib.types.trees.Tree)15 INaviView (com.google.security.zynamics.binnavi.disassembly.views.INaviView)14 TreeNode (com.google.security.zynamics.zylib.types.trees.TreeNode)14 MockTagManager (com.google.security.zynamics.binnavi.Tagging.MockTagManager)13 CCodeNode (com.google.security.zynamics.binnavi.disassembly.CCodeNode)13 INaviFunction (com.google.security.zynamics.binnavi.disassembly.INaviFunction)13 INaviModule (com.google.security.zynamics.binnavi.disassembly.INaviModule)13 ExpensiveBaseTest (com.google.security.zynamics.binnavi.disassembly.types.ExpensiveBaseTest)13 INaviViewNode (com.google.security.zynamics.binnavi.disassembly.INaviViewNode)12