Search in sources :

Example 21 with INaviEdge

use of com.google.security.zynamics.binnavi.disassembly.INaviEdge in project binnavi by google.

the class PostgreSQLCommentNotificationParser method processEdgeLocalCommentNotification.

/**
   * Parses the notifications from the database back end for local edge comments by using a regular
   * expression. If the regular expression matches the supplied {@link PGNotification} notification,
   * it is determined if the edge in question is loaded, and if a
   * {@link CommentNotificationContainer} is build with the data from the notification.
   *
   * @param notification The {@link PGNotification} from the PostgreSQL database server.
   * @param provider The {@link SQLProvider} which is used to communicate with the database.
   */
static CommentNotification processEdgeLocalCommentNotification(final PGNotification notification, final SQLProvider provider) {
    final Matcher matcher = EDGE_LOCAL_PATTERN.matcher(notification.getParameter());
    if (!matcher.find()) {
        return null;
    }
    final Integer edgeId = Integer.parseInt(matcher.group(3));
    final Integer commentId = matcher.group(4).equals("null") ? null : Integer.parseInt(matcher.group(4));
    final INaviEdge edge = EdgeCache.get(provider).getEdgeById(edgeId);
    if (edge == null) {
        return null;
    }
    final CommentOperation operation = commentId == null ? CommentOperation.DELETE : CommentOperation.APPEND;
    return new EdgeCommentNotificationContainer(edge, operation, CommentScope.LOCAL, commentId);
}
Also used : BigInteger(java.math.BigInteger) CommentOperation(com.google.security.zynamics.binnavi.disassembly.CommentManager.CommentOperation) EdgeCommentNotificationContainer(com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.EdgeCommentNotificationContainer) Matcher(java.util.regex.Matcher) INaviEdge(com.google.security.zynamics.binnavi.disassembly.INaviEdge)

Example 22 with INaviEdge

use of com.google.security.zynamics.binnavi.disassembly.INaviEdge in project binnavi by google.

the class PostgreSQLEdgeLoader method loadEdges.

/**
   * Loads the edges of a view.
   *
   * @param provider The connection to the database.
   * @param view The view whose edges are loaded.
   * @param nodeLookup Maps between node IDs and their corresponding node objects.
   * @param edgeToGlobalCommentMap Maps between edge IDs and their associated comments.
   *
   * @return The loaded edges.
   *
   * @throws CouldntLoadDataException
   */
private static List<INaviEdge> loadEdges(final AbstractSQLProvider provider, final INaviView view, final Map<Integer, INaviViewNode> nodeLookup, final Map<Integer, ArrayList<IComment>> edgeToGlobalCommentMap) throws CouldntLoadDataException {
    final String query = "SELECT * FROM load_view_edges(" + view.getConfiguration().getId() + ")";
    List<CBend> currentPaths = new ArrayList<CBend>();
    final Map<Integer, INaviEdge> commentIdToEdge = new HashMap<Integer, INaviEdge>();
    final Map<Integer, INaviEdge> edgeIdToEdge = new HashMap<Integer, INaviEdge>();
    try {
        final CConnection connection = provider.getConnection();
        final PreparedStatement statement = connection.getConnection().prepareStatement(query);
        final ResultSet resultSet = statement.executeQuery();
        try {
            while (resultSet.next()) {
                final int edgeId = resultSet.getInt("id");
                if (edgeIdToEdge.containsKey(edgeId)) {
                    final INaviEdge edge = edgeIdToEdge.get(edgeId);
                    final double pathX = resultSet.getDouble("x");
                    final double pathY = resultSet.getDouble("y");
                    if (!resultSet.wasNull()) {
                        edge.addBend(pathX, pathY);
                    }
                    continue;
                }
                final int sourceNode = resultSet.getInt("source_node_id");
                final int targetNode = resultSet.getInt("target_node_id");
                Integer localCommentId = resultSet.getInt("comment_id");
                if (resultSet.wasNull()) {
                    localCommentId = null;
                }
                final double x1 = resultSet.getDouble("x1");
                final double y1 = resultSet.getDouble("y1");
                final double x2 = resultSet.getDouble("x2");
                final double y2 = resultSet.getDouble("y2");
                final EdgeType type = EdgeType.valueOf(resultSet.getString("type").toUpperCase());
                final Color color = new Color(resultSet.getInt("color"));
                final boolean visible = resultSet.getBoolean("visible");
                final boolean selected = resultSet.getBoolean("selected");
                final INaviViewNode source = nodeLookup.get(sourceNode);
                final INaviViewNode target = nodeLookup.get(targetNode);
                final double pathX = resultSet.getDouble("x");
                final double pathY = resultSet.getDouble("y");
                if (!resultSet.wasNull()) {
                    currentPaths.add(new CBend(pathX, pathY));
                }
                final CNaviViewEdge edge = new CNaviViewEdge(edgeId, source, target, type, x1, y1, x2, y2, color, selected, visible, null, currentPaths, provider);
                if (localCommentId != null) {
                    commentIdToEdge.put(localCommentId, edge);
                }
                final ArrayList<IComment> globalComments = edgeToGlobalCommentMap.containsKey(edgeId) ? edgeToGlobalCommentMap.get(edgeId) : null;
                if ((globalComments != null) && (globalComments.size() != 0)) {
                    initializeGlobalComment(edge, globalComments, provider);
                }
                source.addOutgoingEdge(edge);
                target.addIncomingEdge(edge);
                edgeIdToEdge.put(edge.getId(), edge);
                currentPaths = new ArrayList<CBend>();
            }
            if (!commentIdToEdge.isEmpty()) {
                final HashMap<Integer, ArrayList<IComment>> commentIdToComments = PostgreSQLCommentFunctions.loadMultipleCommentsById(provider, commentIdToEdge.keySet());
                for (final Entry<Integer, ArrayList<IComment>> commentIdToComment : commentIdToComments.entrySet()) {
                    commentIdToEdge.get(commentIdToComment.getKey()).initializeLocalComment(commentIdToComment.getValue());
                }
            }
        } finally {
            resultSet.close();
        }
    } catch (final SQLException exception) {
        throw new CouldntLoadDataException("Error: Loading of view edges failed");
    }
    return Lists.newArrayList(edgeIdToEdge.values());
}
Also used : IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) ArrayList(java.util.ArrayList) INaviEdge(com.google.security.zynamics.binnavi.disassembly.INaviEdge) CBend(com.google.security.zynamics.zylib.gui.zygraph.edges.CBend) ResultSet(java.sql.ResultSet) INaviViewNode(com.google.security.zynamics.binnavi.disassembly.INaviViewNode) Color(java.awt.Color) PreparedStatement(java.sql.PreparedStatement) EdgeType(com.google.security.zynamics.zylib.gui.zygraph.edges.EdgeType) CConnection(com.google.security.zynamics.binnavi.Database.CConnection) CNaviViewEdge(com.google.security.zynamics.binnavi.disassembly.CNaviViewEdge)

Example 23 with INaviEdge

use of com.google.security.zynamics.binnavi.disassembly.INaviEdge in project binnavi by google.

the class PostgreSQLViewSaver method save.

/**
   * Saves a view to the database.
   * 
   * @param provider The SQL provider that provides the connection.
   * @param view The view to save to the database.
   * 
   * @throws CouldntSaveDataException Thrown if the view could not be saved to the database.
   */
public static void save(final AbstractSQLProvider provider, final CView view) throws CouldntSaveDataException {
    PostgreSQLViewSaver.checkArguments(provider, view);
    final CConnection connection = provider.getConnection();
    try {
        PostgreSQLHelpers.beginTransaction(connection);
        final int viewId = view.getConfiguration().getId();
        final List<INaviViewNode> nodes = view.getGraph().getNodes();
        final List<INaviEdge> edges = view.getGraph().getEdges();
        PostgreSQLViewSaver.deleteNodes(connection, viewId);
        // Store all nodes
        PostgreSQLNodeSaver.writeNodes(provider, viewId, nodes);
        // Store all edges
        PostgreSQLEdgeSaver.writeEdges(provider, edges);
        PostgreSQLHelpers.endTransaction(connection);
    } catch (final SQLException exception) {
        try {
            PostgreSQLHelpers.rollback(connection);
        } catch (final SQLException e) {
            CUtilityFunctions.logException(e);
        }
        throw new CouldntSaveDataException(exception);
    }
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) SQLException(java.sql.SQLException) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) INaviViewNode(com.google.security.zynamics.binnavi.disassembly.INaviViewNode) INaviEdge(com.google.security.zynamics.binnavi.disassembly.INaviEdge)

Example 24 with INaviEdge

use of com.google.security.zynamics.binnavi.disassembly.INaviEdge in project binnavi by google.

the class EdgeCache method addEdges.

public void addEdges(final List<INaviEdge> edges) {
    final ImmutableMap<Integer, INaviEdge> edgesMap = Maps.uniqueIndex(edges, new Function<INaviEdge, Integer>() {

        @Override
        public Integer apply(final INaviEdge edge) {
            return edge.getId();
        }
    });
    edgesByIdCache.putAll(edgesMap);
    for (final INaviEdge edge : edges) {
        if (edge.getSource() instanceof IAddressNode && edge.getTarget() instanceof IAddressNode) {
            final IAddress sourceAddress = ((IAddressNode) edge.getSource()).getAddress();
            final IAddress targetAddress = ((IAddressNode) edge.getTarget()).getAddress();
            Integer sourceModuleId = null;
            Integer targetModuleId = null;
            if (edge.getSource() instanceof INaviCodeNode) {
                sourceModuleId = getModuleId((INaviCodeNode) edge.getSource());
            } else if (edge.getSource() instanceof INaviFunctionNode) {
                sourceModuleId = getModuleId((INaviFunctionNode) edge.getSource());
            }
            if (edge.getTarget() instanceof INaviCodeNode) {
                targetModuleId = getModuleId((INaviCodeNode) edge.getTarget());
            } else if (edge.getTarget() instanceof INaviFunctionNode) {
                targetModuleId = getModuleId((INaviFunctionNode) edge.getTarget());
            }
            if (targetModuleId != null && sourceModuleId != null) {
                UpdateAddressModuleIdCache(sourceAddress, sourceModuleId, targetAddress, targetModuleId, edge);
            }
        }
    }
}
Also used : INaviCodeNode(com.google.security.zynamics.binnavi.disassembly.INaviCodeNode) INaviFunctionNode(com.google.security.zynamics.binnavi.disassembly.INaviFunctionNode) IAddressNode(com.google.security.zynamics.binnavi.disassembly.IAddressNode) INaviEdge(com.google.security.zynamics.binnavi.disassembly.INaviEdge) IAddress(com.google.security.zynamics.zylib.disassembly.IAddress)

Example 25 with INaviEdge

use of com.google.security.zynamics.binnavi.disassembly.INaviEdge in project binnavi by google.

the class PostgreSQLEdgeSaver method fillEdgesTable.

/**
   * Writes the data from the edge objects to the edges table.
   * 
   * @param connection Connection to a PostgreSQL database.
   * @param edges The edges to write.
   * 
   * @throws SQLException Thrown if storing the edges failed.
   */
private static void fillEdgesTable(final CConnection connection, final List<INaviEdge> edges) throws SQLException {
    final String query = "INSERT INTO " + CTableNames.EDGES_TABLE + "(source_node_id, target_node_id, x1, y1, x2, y2, type, " + "color, visible, selected, comment_id) VALUES " + "( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ";
    final PreparedStatement preparedStatement = connection.getConnection().prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS);
    try {
        for (final INaviEdge edge : edges) {
            preparedStatement.setInt(1, edge.getSource().getId());
            preparedStatement.setInt(2, edge.getTarget().getId());
            preparedStatement.setDouble(3, edge.getX1());
            preparedStatement.setDouble(4, edge.getY1());
            preparedStatement.setDouble(5, edge.getX2());
            preparedStatement.setDouble(6, edge.getY2());
            preparedStatement.setObject(7, edge.getType().toString().toLowerCase(), Types.OTHER);
            preparedStatement.setInt(8, edge.getColor().getRGB());
            preparedStatement.setBoolean(9, edge.isVisible());
            preparedStatement.setBoolean(10, edge.isSelected());
            if (edge.getLocalComment() == null) {
                preparedStatement.setNull(11, Types.INTEGER);
            } else {
                preparedStatement.setInt(11, Iterables.getLast(edge.getLocalComment()).getId());
            }
            preparedStatement.addBatch();
        }
        preparedStatement.executeBatch();
        final ResultSet resultSet = preparedStatement.getGeneratedKeys();
        for (final INaviEdge edge : edges) {
            if (resultSet.next()) {
                edge.setId(resultSet.getInt(1));
            } else {
                throw new IllegalStateException("Error: The number of keys generated does not match the number of edges");
            }
        }
    } catch (final SQLException exception) {
        CUtilityFunctions.logException(exception);
        CUtilityFunctions.logException(exception.getNextException());
    } finally {
        preparedStatement.close();
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) INaviEdge(com.google.security.zynamics.binnavi.disassembly.INaviEdge)

Aggregations

INaviEdge (com.google.security.zynamics.binnavi.disassembly.INaviEdge)47 INaviViewNode (com.google.security.zynamics.binnavi.disassembly.INaviViewNode)21 Test (org.junit.Test)17 CTag (com.google.security.zynamics.binnavi.Tagging.CTag)9 INaviCodeNode (com.google.security.zynamics.binnavi.disassembly.INaviCodeNode)9 INaviFunction (com.google.security.zynamics.binnavi.disassembly.INaviFunction)8 EdgeCommentNotificationContainer (com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.EdgeCommentNotificationContainer)7 IComment (com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment)7 CCodeNode (com.google.security.zynamics.binnavi.disassembly.CCodeNode)7 CNaviViewEdge (com.google.security.zynamics.binnavi.disassembly.CNaviViewEdge)7 MockView (com.google.security.zynamics.binnavi.disassembly.MockView)7 CBend (com.google.security.zynamics.zylib.gui.zygraph.edges.CBend)7 CommentNotification (com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.interfaces.CommentNotification)6 MockInstruction (com.google.security.zynamics.binnavi.disassembly.MockInstruction)6 SQLException (java.sql.SQLException)6 ArrayList (java.util.ArrayList)6 INaviInstruction (com.google.security.zynamics.binnavi.disassembly.INaviInstruction)5 MockFunction (com.google.security.zynamics.binnavi.disassembly.MockFunction)5 MockModule (com.google.security.zynamics.binnavi.disassembly.Modules.MockModule)5 FilledList (com.google.security.zynamics.zylib.types.lists.FilledList)5