Search in sources :

Example 11 with CTag

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

the class PostgreSQLGroupNodeLoader method load.

/**
   * Loads the group nodes of a view.
   * 
   * @param provider The connection to the database.
   * @param view The view whose group nodes are loaded.
   * @param nodes The loaded nodes are stored here.
   * 
   * @throws SQLException Thrown of loading the nodes failed.
   * @throws CouldntLoadDataException
   */
public static void load(final AbstractSQLProvider provider, final INaviView view, final List<INaviViewNode> nodes) throws SQLException, CouldntLoadDataException {
    Preconditions.checkNotNull(provider, "IE02513: provider argument can not be null");
    Preconditions.checkNotNull(view, "IE02514: view argument can not be null");
    Preconditions.checkNotNull(nodes, "IE02515: nodes argument can not be null");
    final Map<Integer, INaviGroupNode> commentIdToGroupNode = new HashMap<Integer, INaviGroupNode>();
    final String query = "SELECT id, comment_id , collapsed, x, y, width, height, color, selected, visible " + " FROM " + CTableNames.NODES_TABLE + " JOIN " + CTableNames.GROUP_NODES_TABLE + " ON id = node_id WHERE view_id = " + view.getConfiguration().getId();
    try (ResultSet resultSet = provider.getConnection().executeQuery(query, true)) {
        while (resultSet.next()) {
            final int nodeId = resultSet.getInt("id");
            Integer commentId = resultSet.getInt("comment_id");
            if (resultSet.wasNull()) {
                commentId = null;
            }
            final boolean collapsed = resultSet.getBoolean("collapsed");
            final double posX = resultSet.getDouble("x");
            final double posY = resultSet.getDouble("y");
            final double width = resultSet.getDouble("width");
            final double height = resultSet.getDouble("height");
            final Color color = new Color(resultSet.getInt("color"));
            final boolean selected = resultSet.getBoolean("selected");
            final boolean visible = resultSet.getBoolean("visible");
            final INaviGroupNode groupNode = new CGroupNode(nodeId, posX, posY, width, height, color, selected, visible, new HashSet<CTag>(), null, collapsed, provider);
            if (commentId != null) {
                commentIdToGroupNode.put(commentId, groupNode);
            }
            nodes.add(groupNode);
        }
        if (!commentIdToGroupNode.isEmpty()) {
            final HashMap<Integer, ArrayList<IComment>> commentIdsToComments = PostgreSQLCommentFunctions.loadMultipleCommentsById(provider, commentIdToGroupNode.keySet());
            for (final Entry<Integer, ArrayList<IComment>> commentIdToComment : commentIdsToComments.entrySet()) {
                commentIdToGroupNode.get(commentIdToComment.getKey()).initializeComment(commentIdToComment.getValue());
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) Color(java.awt.Color) CTag(com.google.security.zynamics.binnavi.Tagging.CTag) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) INaviGroupNode(com.google.security.zynamics.binnavi.disassembly.INaviGroupNode) CGroupNode(com.google.security.zynamics.binnavi.disassembly.CGroupNode)

Example 12 with CTag

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

the class PostgreSQLNodeLoader method loadNodeTags.

/**
   * Loads the node tags of a list of nodes.
   * 
   * @param connection The connection to the database.
   * @param nodes The nodes whose tags are loaded.
   * @param nodeTagManager Manages all node tags of the database.
   * 
   * @throws SQLException Thrown if loading the node tags failed.
   */
private static void loadNodeTags(final CConnection connection, final List<INaviViewNode> nodes, final CTagManager nodeTagManager) throws SQLException {
    final HashMap<Integer, INaviViewNode> idNodeMap = new HashMap<Integer, INaviViewNode>();
    final HashMap<Integer, CTag> idTagMap = new HashMap<Integer, CTag>();
    final StringBuffer range = new StringBuffer();
    boolean isFirst = true;
    for (final INaviViewNode node : nodes) {
        range.append(isFirst ? "" : ",");
        range.append(node.getId());
        isFirst = false;
        idNodeMap.put(node.getId(), node);
    }
    if (isFirst) {
        return;
    }
    final String query = String.format("SELECT node_id, tag_id FROM %s WHERE node_id IN (%s)", CTableNames.TAGGED_NODES_TABLE, range.toString());
    final ResultSet resultSet = connection.executeQuery(query, true);
    try {
        final Set<Integer> tagIds = new HashSet<Integer>();
        while (resultSet.next()) {
            tagIds.add(resultSet.getInt("tag_id"));
        }
        final Collection<CTag> tags = CTagHelpers.findTags(nodeTagManager.getRootTag(), tagIds);
        for (final CTag tag : tags) {
            idTagMap.put(tag.getId(), tag);
        }
        resultSet.beforeFirst();
        while (resultSet.next()) {
            final INaviViewNode node = idNodeMap.get(resultSet.getInt("node_id"));
            final CTag tag = idTagMap.get(resultSet.getInt("tag_id"));
            ((CNaviViewNode) node).tagNodeSilent(tag);
        }
    } finally {
        resultSet.close();
    }
}
Also used : HashMap(java.util.HashMap) CTag(com.google.security.zynamics.binnavi.Tagging.CTag) CNaviViewNode(com.google.security.zynamics.binnavi.disassembly.CNaviViewNode) ResultSet(java.sql.ResultSet) INaviViewNode(com.google.security.zynamics.binnavi.disassembly.INaviViewNode) HashSet(java.util.HashSet)

Example 13 with CTag

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

the class PostgreSQLTagFunctions method createTag.

/**
   * Creates a new tag in the database.
   * 
   * @param provider The connection to the database.
   * @param parent The parent tag of the tag.
   * @param name The name of the new tag.
   * @param description The description of the new tag.
   * @param type The type of the new tag.
   * 
   * @return The new tag.
   * 
   * @throws CouldntSaveDataException Thrown if creating the tag failed.
   */
public static CTag createTag(final AbstractSQLProvider provider, final CTag parent, final String name, final String description, final TagType type) throws CouldntSaveDataException {
    checkArguments(provider, parent, type);
    Preconditions.checkNotNull(name, "IE00556: Name argument can not be null");
    Preconditions.checkNotNull(description, "IE00557: Description argument can not be null");
    final CConnection connection = provider.getConnection();
    final String query = "insert into " + CTableNames.TAGS_TABLE + "(parent_id, name, description, type) values(?, ?, ?, ?::tag_type) returning id";
    try (PreparedStatement statement = connection.getConnection().prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)) {
        if (parent.getId() == 0) {
            statement.setNull(1, Types.INTEGER);
        } else {
            statement.setInt(1, parent.getId());
        }
        statement.setString(2, name);
        statement.setString(3, description);
        statement.setString(4, tagToString(type));
        Integer id = null;
        try (ResultSet resultSet = statement.executeQuery()) {
            while (resultSet.next()) {
                if (resultSet.isFirst()) {
                    id = resultSet.getInt(1);
                }
            }
        }
        if (id != null) {
            return new CTag(id, name, description, type, provider);
        } else {
            throw new IllegalStateException("IE02141: Error id can not be null");
        }
    } catch (final SQLException e) {
        throw new CouldntSaveDataException(e);
    }
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) SQLException(java.sql.SQLException) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) ResultSet(java.sql.ResultSet) CTag(com.google.security.zynamics.binnavi.Tagging.CTag) PreparedStatement(java.sql.PreparedStatement)

Example 14 with CTag

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

the class PostgreSQLTagFunctions method deleteTag.

/**
   * Deletes a tag from the database.
   * 
   * @param provider The connection to the database.
   * @param tag The tag to delete.
   * 
   * @throws CouldntDeleteException Thrown if the tag could not be deleted.
   */
public static void deleteTag(final AbstractSQLProvider provider, final ITreeNode<CTag> tag) throws CouldntDeleteException {
    checkArguments(provider, tag);
    Preconditions.checkNotNull(tag.getParent(), "IE00558: Can not delete the root tag");
    final CConnection connection = provider.getConnection();
    try {
        final ITreeNode<CTag> parent = tag.getParent();
        final String parentId = parent.getObject().getId() == 0 ? "null" : String.valueOf(parent.getObject().getId());
        final String query_1 = String.format("UPDATE %s SET parent_id = %s WHERE parent_id = ?", CTableNames.TAGS_TABLE, parentId);
        try (PreparedStatement statement_1 = connection.getConnection().prepareStatement(query_1)) {
            statement_1.setInt(1, tag.getObject().getId());
            statement_1.executeUpdate();
        }
        final String query_2 = String.format("DELETE FROM %s WHERE id = ?", CTableNames.TAGS_TABLE);
        try (PreparedStatement statement_2 = connection.getConnection().prepareStatement(query_2)) {
            statement_2.setInt(1, tag.getObject().getId());
            statement_2.executeUpdate();
        }
    } catch (final SQLException e) {
        throw new CouldntDeleteException(e);
    }
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) CouldntDeleteException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntDeleteException) SQLException(java.sql.SQLException) CTag(com.google.security.zynamics.binnavi.Tagging.CTag) PreparedStatement(java.sql.PreparedStatement)

Example 15 with CTag

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

the class PostgreSQLTagFunctions method moveTag.

/**
   * Moves a tag.
   * 
   * @param provider Connection to the database.
   * @param newParentNode The new parent node of the tag.
   * @param movedNode The tag to move.
   * 
   * @throws CouldntSaveDataException Thrown if the tag could not be moved.
   */
public static void moveTag(final AbstractSQLProvider provider, final ITreeNode<CTag> newParentNode, final ITreeNode<CTag> movedNode, final TagType type) throws CouldntSaveDataException {
    Preconditions.checkNotNull(provider, "IE02083: Provider argument can not be null");
    Preconditions.checkNotNull(newParentNode, "IE02190: Parent argument can not be null");
    Preconditions.checkNotNull(movedNode, "IE02191: Child argument can not be null");
    final List<Integer> childIds = new ArrayList<>();
    for (final ITreeNode<CTag> childChild : movedNode.getChildren()) {
        childIds.add(childChild.getObject().getId());
    }
    try {
        final String childParentId = movedNode.getParent().getObject().getId() == 0 ? "null" : String.valueOf(movedNode.getParent().getObject().getId());
        if (!childIds.isEmpty()) {
            // Connect the parent of the child tag with the children of the child tag
            provider.getConnection().executeUpdate("update " + CTableNames.TAGS_TABLE + " set parent_id = " + childParentId + " where id in (" + Commafier.commafy(childIds) + ") and type = '" + tagToString(type) + "'", true);
        }
        // Connect the parent tag with the child tag
        provider.getConnection().executeUpdate("update " + CTableNames.TAGS_TABLE + " set parent_id = " + newParentNode.getObject().getId() + " where id = " + movedNode.getObject().getId() + " and type = '" + tagToString(type) + "'", true);
    } catch (final SQLException e) {
        throw new CouldntSaveDataException(e);
    }
}
Also used : SQLException(java.sql.SQLException) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) ArrayList(java.util.ArrayList) CTag(com.google.security.zynamics.binnavi.Tagging.CTag)

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