Search in sources :

Example 16 with INaviEdge

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

the class PostgreSQLViewCreator method createView.

/**
   * Inserts a new view in the database by copying an existing view.
   * 
   * @param provider The connection to the database.
   * @param containerId The ID of the container where the view is created.
   * @param view The view to be copied.
   * @param name The name of the new view.
   * @param description The description of the new view.
   * @param containerTable Name of the view container table.
   * @param viewContainerTable Name of the view container views table.
   * @param generator Generates the view.
   * @return The created view.
   * @throws CouldntSaveDataException Thrown if the view could not be created.
   */
private static CView createView(final AbstractSQLProvider provider, final int containerId, final INaviView view, final String name, final String description, final String containerTable, final String viewContainerTable, final ViewGenerator generator) throws CouldntSaveDataException {
    final CConnection connection = provider.getConnection();
    try {
        PostgreSQLHelpers.beginTransaction(connection);
        final int viewId = insertView(connection, name, description);
        // Mark the view as a module view
        connection.executeUpdate("INSERT INTO " + viewContainerTable + " VALUES(" + containerId + ", " + viewId + ")", true);
        final List<INaviViewNode> nodes = view.getGraph().getNodes();
        final List<INaviEdge> edges = view.getGraph().getEdges();
        // Store all nodes
        PostgreSQLNodeSaver.writeNodes(provider, viewId, nodes);
        // Store all edges
        PostgreSQLEdgeSaver.writeEdges(provider, edges);
        PostgreSQLHelpers.endTransaction(connection);
        final String query = "SELECT creation_date, modification_date FROM " + CTableNames.VIEWS_TABLE + " WHERE id = " + viewId;
        final ResultSet resultSet = connection.executeQuery(query, true);
        try {
            while (resultSet.next()) {
                final Timestamp creationDate = resultSet.getTimestamp("creation_date");
                final Timestamp modificationDate = resultSet.getTimestamp("modification_date");
                PostgreSQLHelpers.updateModificationDate(connection, containerTable, containerId);
                return generator.generate(viewId, name, description, ViewType.NonNative, view.getGraphType(), creationDate, modificationDate, view.getNodeCount(), view.getEdgeCount(), new HashSet<CTag>(), new HashSet<CTag>(), false);
            }
            throw new CouldntSaveDataException("Error: Couldnt't load the created view");
        } finally {
            resultSet.close();
        }
    } catch (final SQLException exception) {
        CUtilityFunctions.logException(exception);
        try {
            PostgreSQLHelpers.rollback(connection);
        } catch (final SQLException e) {
            CUtilityFunctions.logException(e);
        }
        throw new CouldntSaveDataException(exception);
    }
}
Also used : SQLException(java.sql.SQLException) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) CTag(com.google.security.zynamics.binnavi.Tagging.CTag) INaviEdge(com.google.security.zynamics.binnavi.disassembly.INaviEdge) Timestamp(java.sql.Timestamp) CConnection(com.google.security.zynamics.binnavi.Database.CConnection) ResultSet(java.sql.ResultSet) INaviViewNode(com.google.security.zynamics.binnavi.disassembly.INaviViewNode)

Example 17 with INaviEdge

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

the class CViewPruner method convertEdges.

/**
   * Converts edges of the original view to the pruned view.
   *
   * @param view The original view.
   * @param prunedView The pruned view.
   * @param nodeMap A mapping between nodes of the original view and nodes of the pruned view.
   */
private static void convertEdges(final INaviView view, final INaviView prunedView, final Map<INaviViewNode, INaviViewNode> nodeMap) {
    final Set<Pair<INaviViewNode, INaviViewNode>> createdEdges = new HashSet<Pair<INaviViewNode, INaviViewNode>>();
    for (final INaviEdge edge : view.getGraph().getEdges()) {
        final Set<EdgeResult> sources = getSources(edge, nodeMap, new HashSet<INaviEdge>());
        final Set<EdgeResult> targets = getTargets(edge, nodeMap, new HashSet<INaviEdge>());
        for (final EdgeResult source : sources) {
            for (final EdgeResult target : targets) {
                final Pair<INaviViewNode, INaviViewNode> edgePair = new Pair<INaviViewNode, INaviViewNode>(source.m_node, target.m_node);
                if (createdEdges.contains(edgePair)) {
                    continue;
                }
                prunedView.getContent().createEdge(source.m_node, target.m_node, source.m_type);
                createdEdges.add(edgePair);
            }
        }
    }
}
Also used : INaviViewNode(com.google.security.zynamics.binnavi.disassembly.INaviViewNode) INaviEdge(com.google.security.zynamics.binnavi.disassembly.INaviEdge) HashSet(java.util.HashSet) Pair(com.google.security.zynamics.zylib.general.Pair)

Example 18 with INaviEdge

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

the class CViewPruner method getTargets.

/**
   * Collects edge information about incoming edges.
   *
   * @param edge The edge whose information is collected.
   * @param nodeMap Maps between nodes of the old view and nodes of the new view.
   * @param visited Already visited edges.
   *
   * @return The collected edge information.
   */
private static Set<EdgeResult> getTargets(final INaviEdge edge, final Map<INaviViewNode, INaviViewNode> nodeMap, final Set<INaviEdge> visited) {
    final INaviViewNode target = edge.getTarget();
    visited.add(edge);
    final Set<EdgeResult> targets = new HashSet<EdgeResult>();
    if (nodeMap.containsKey(target)) {
        targets.add(new EdgeResult(nodeMap.get(target), edge.getType()));
    } else {
        for (final INaviEdge outgoingEdge : target.getOutgoingEdges()) {
            if (!visited.contains(outgoingEdge)) {
                targets.addAll(getTargets(outgoingEdge, nodeMap, visited));
            }
        }
    }
    return targets;
}
Also used : INaviViewNode(com.google.security.zynamics.binnavi.disassembly.INaviViewNode) INaviEdge(com.google.security.zynamics.binnavi.disassembly.INaviEdge) HashSet(java.util.HashSet)

Example 19 with INaviEdge

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

the class CGlobalEdgeCommentSynchronizer method updateOpenViews.

/**
   * Pushes a new global edge comment to all open views.
   *
   * @param edge The edge that has the new comment.
   * @param comments The new comment of the code node.
   *
   * @throws CouldntSaveDataException Thrown if updating the edge comments failed.
   */
public static void updateOpenViews(final INaviEdge edge, final ArrayList<IComment> comments) throws CouldntSaveDataException {
    try {
        final Pair<INaviModule, INaviModule> modules = getModules(edge);
        if (modules.first() != modules.second()) {
            // TODO: Handle this
            return;
        }
        if ((modules.first() != null) && (modules.second() != null) && modules.first().isLoaded()) {
            final List<INaviEdge> edgelist = new ArrayList<INaviEdge>();
            final Quad<Integer, IAddress, Integer, IAddress> refEdgeData = getEdgeData(edge);
            for (final INaviView view : modules.first().getContent().getViewContainer().getViews()) {
                edgelist.addAll(collectEdges(view, refEdgeData));
            }
            for (final INaviEdge updateEdge : edgelist) {
                updateEdge.initializeGlobalComment(comments);
            }
        }
    } catch (final MaybeNullException exception) {
    // Trying to update global comments of code nodes without global comments.
    }
}
Also used : INaviView(com.google.security.zynamics.binnavi.disassembly.views.INaviView) INaviModule(com.google.security.zynamics.binnavi.disassembly.INaviModule) MaybeNullException(com.google.security.zynamics.binnavi.Exceptions.MaybeNullException) ArrayList(java.util.ArrayList) INaviEdge(com.google.security.zynamics.binnavi.disassembly.INaviEdge) IAddress(com.google.security.zynamics.zylib.disassembly.IAddress)

Example 20 with INaviEdge

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

the class PostgreSQLCommentNotificationParser method processEdgeGlobalCommentNotification.

/**
   * Parses the notifications from the database back end for global edge comments by using a regular
   * expression. If the regular expression matches the supplied notification it tries to figure out
   * if the edge which was commented is loaded in BinNavi at this point in time. If the edge is
   * loaded determine the operation which was performed by the database and then return a
   * {@link CommentNotificationContainer} with the gathered results.
   *
   * @param notification The {@link PGNotification} from the PostgreSQL database server.
   * @param provider The {@link SQLProvider} which is used to communicate with the database.
   */
static Collection<CommentNotification> processEdgeGlobalCommentNotification(final PGNotification notification, final SQLProvider provider) {
    final Matcher matcher = EDGE_GLOBAL_PATTERN.matcher(notification.getParameter());
    if (!matcher.find()) {
        return new ArrayList<>();
    }
    final String databaseOperation = matcher.group(2);
    final Integer notificationSourceModuleId = Integer.parseInt(matcher.group(3));
    final Integer notificationDestinationModuleId = Integer.parseInt(matcher.group(4));
    final IAddress notificationEdgeSourceAddress = new CAddress(new BigInteger(matcher.group(5)));
    final IAddress notificationEdgeDestinationAddress = new CAddress(new BigInteger(matcher.group(6)));
    final Integer commentId = matcher.group(8) == null ? null : Integer.parseInt(matcher.group(8));
    final INaviModule notificationSourceModule = provider.findModule(notificationSourceModuleId);
    if ((notificationSourceModule == null) || !notificationSourceModule.isLoaded()) {
        return new ArrayList<>();
    }
    final INaviModule notificationDestinationModule = provider.findModule(notificationDestinationModuleId);
    if ((notificationDestinationModule == null) || !notificationDestinationModule.isLoaded()) {
        return new ArrayList<>();
    }
    final CommentOperation operation = databaseOperation.equalsIgnoreCase("DELETE") ? CommentOperation.DELETE : CommentOperation.APPEND;
    Collection<CommentNotification> notifications = new ArrayList<>();
    final ImmutableCollection<INaviEdge> edges = EdgeCache.get(provider).getEdgeBySourceAndTarget(notificationEdgeSourceAddress, notificationSourceModuleId, notificationEdgeDestinationAddress, notificationDestinationModuleId);
    for (INaviEdge edge : edges) {
        notifications.add(new EdgeCommentNotificationContainer(edge, operation, CommentScope.GLOBAL, commentId));
    }
    return notifications;
}
Also used : CommentNotification(com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.interfaces.CommentNotification) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) INaviEdge(com.google.security.zynamics.binnavi.disassembly.INaviEdge) IAddress(com.google.security.zynamics.zylib.disassembly.IAddress) CAddress(com.google.security.zynamics.zylib.disassembly.CAddress) BigInteger(java.math.BigInteger) CommentOperation(com.google.security.zynamics.binnavi.disassembly.CommentManager.CommentOperation) EdgeCommentNotificationContainer(com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.EdgeCommentNotificationContainer) INaviModule(com.google.security.zynamics.binnavi.disassembly.INaviModule) BigInteger(java.math.BigInteger)

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