Search in sources :

Example 11 with INaviFunction

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

the class PostgreSQLViewsLoader method checkArguments.

/**
   * Validates arguments.
   * 
   * @param provider Provider argument to validate.
   * @param module Module argument to validate.
   * @param flowgraphs Flowgraphs argument to validate.
   * @param functions Functions argument to validate.
   */
private static void checkArguments(final AbstractSQLProvider provider, final CModule module, final List<IFlowgraphView> flowgraphs, final List<INaviFunction> functions) {
    Preconditions.checkNotNull(provider, "IE00630: Provider argument can not be null");
    Preconditions.checkNotNull(module, "IE00631: Module argument can not be null");
    Preconditions.checkArgument(module.inSameDatabase(provider), "IE00632: Module is not part of this database");
    Preconditions.checkNotNull(flowgraphs, "IE00633: Flowgraphs argument can not be null");
    for (final IFlowgraphView view : flowgraphs) {
        Preconditions.checkNotNull(view, "IE00634: View list contains a null-element");
        Preconditions.checkArgument(view.inSameDatabase(provider), "IE00635: View is not part of this database");
    }
    Preconditions.checkNotNull(functions, "IE00636: Functions argument can not be null");
    for (final INaviFunction function : functions) {
        Preconditions.checkNotNull(function, "IE00637: Function list contains a null-element");
        Preconditions.checkArgument(function.inSameDatabase(provider), "IE00638: Function is not part of this database");
    }
}
Also used : IFlowgraphView(com.google.security.zynamics.binnavi.disassembly.IFlowgraphView) INaviFunction(com.google.security.zynamics.binnavi.disassembly.INaviFunction)

Example 12 with INaviFunction

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

the class PostgreSQLCommentNotificationParser method processFunctionCommentNotification.

/**
   * Parses the notifications from the database back end for function comments by using a regular
   * expression. If the regular expression matches the supplied {@link PGNotification} notification,
   * 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 processFunctionCommentNotification(final PGNotification notification, final SQLProvider provider) {
    final Matcher matcher = FUNCTION_PATTERN.matcher(notification.getParameter());
    if (!matcher.find()) {
        return null;
    }
    final Integer notificationModuleId = Integer.parseInt(matcher.group(3));
    final IAddress notificationFunctionAddress = new CAddress(new BigInteger(matcher.group(4)));
    final Integer commentId = matcher.group(5).equals("null") ? null : Integer.parseInt(matcher.group(5));
    final INaviModule module = provider.findModule(notificationModuleId);
    if ((module == null) || !module.isLoaded()) {
        return null;
    }
    final INaviFunction function = module.getContent().getFunctionContainer().getFunction(notificationFunctionAddress);
    if (function == null) {
        return null;
    }
    final CommentOperation operation = commentId == null ? CommentOperation.DELETE : CommentOperation.APPEND;
    return new FunctionCommentNotificationContainer(function, operation, commentId);
}
Also used : BigInteger(java.math.BigInteger) CommentOperation(com.google.security.zynamics.binnavi.disassembly.CommentManager.CommentOperation) FunctionCommentNotificationContainer(com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.FunctionCommentNotificationContainer) INaviModule(com.google.security.zynamics.binnavi.disassembly.INaviModule) Matcher(java.util.regex.Matcher) BigInteger(java.math.BigInteger) INaviFunction(com.google.security.zynamics.binnavi.disassembly.INaviFunction) IAddress(com.google.security.zynamics.zylib.disassembly.IAddress) CAddress(com.google.security.zynamics.zylib.disassembly.CAddress)

Example 13 with INaviFunction

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

the class PostgreSQLNodeSaver method saveFunctionNodes.

/**
   * Saves the function nodes to the database.
   *
   * @param provider The connection to the database.
   * @param nodes The nodes to save.
   * @param firstNode The database index of the first node.
   * @param functionNodeIndices Index into the nodes list that identifies the function nodes.
   *
   * @throws SQLException Thrown if saving the function nodes failed.
   */
protected static void saveFunctionNodes(final SQLProvider provider, final List<INaviViewNode> nodes, final int firstNode, final List<Integer> functionNodeIndices) throws SQLException {
    if (functionNodeIndices.isEmpty()) {
        return;
    }
    final String query = "INSERT INTO " + CTableNames.FUNCTION_NODES_TABLE + "(module_id, node_id, function, comment_id) VALUES (?, ?, ?, ?)";
    final ArrayList<INaviFunctionNode> functionNodesWithUnsavedComments = new ArrayList<INaviFunctionNode>();
    final PreparedStatement preparedStatement = provider.getConnection().getConnection().prepareStatement(query);
    try {
        for (final int index : functionNodeIndices) {
            final CFunctionNode node = (CFunctionNode) nodes.get(index);
            final INaviFunction function = node.getFunction();
            final List<IComment> comments = node.getLocalFunctionComment();
            final Integer commentId = comments == null ? null : comments.size() == 0 ? null : Iterables.getLast(comments).getId();
            if ((comments != null) && (comments.size() != 0) && (commentId == null)) {
                functionNodesWithUnsavedComments.add(node);
            }
            preparedStatement.setInt(1, function.getModule().getConfiguration().getId());
            preparedStatement.setInt(2, firstNode + index);
            preparedStatement.setObject(3, function.getAddress().toBigInteger(), Types.BIGINT);
            if (commentId == null) {
                preparedStatement.setNull(4, Types.INTEGER);
            } else {
                preparedStatement.setInt(4, commentId);
            }
            preparedStatement.addBatch();
        }
        preparedStatement.executeBatch();
    } finally {
        preparedStatement.close();
    }
    for (final INaviFunctionNode functionNode : functionNodesWithUnsavedComments) {
        final ArrayList<IComment> functionNodeComments = new ArrayList<IComment>();
        for (final IComment comment : functionNode.getLocalFunctionComment()) {
            try {
                final Integer commentId = provider.appendFunctionNodeComment(functionNode, comment.getComment(), comment.getUser().getUserId());
                final IComment newComment = new CComment(commentId, comment.getUser(), comment.getParent(), comment.getComment());
                functionNodeComments.add(newComment);
            } catch (final CouldntSaveDataException exception) {
                CUtilityFunctions.logException(exception);
            }
        }
        functionNode.initializeLocalFunctionComment(functionNodeComments);
    }
}
Also used : CFunctionNode(com.google.security.zynamics.binnavi.disassembly.CFunctionNode) IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) CComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment) INaviFunctionNode(com.google.security.zynamics.binnavi.disassembly.INaviFunctionNode) INaviFunction(com.google.security.zynamics.binnavi.disassembly.INaviFunction)

Example 14 with INaviFunction

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

the class PostgreSQLNodeSaver method saveCodeNodes.

/**
   * Saves the code nodes to the database.
   *
   * @param provider The connection to the database.
   * @param nodes The nodes to save.
   * @param firstNode The database index of the first node.
   * @param codeNodeIndices Index into the nodes list that identifies the code nodes.
   *
   * @throws SQLException Thrown if saving the code node instructions failed.
   */
protected static void saveCodeNodes(final SQLProvider provider, final List<INaviViewNode> nodes, final int firstNode, final List<Integer> codeNodeIndices) throws SQLException {
    if (!codeNodeIndices.isEmpty()) {
        final List<Pair<INaviCodeNode, INaviInstruction>> instructionsWithUnsavedLocalComments = PostgreSQLNodeSaver.saveCodeNodeInstructions(provider, nodes, firstNode, codeNodeIndices);
        final String query = "INSERT INTO " + CTableNames.CODE_NODES_TABLE + "(module_id, node_id, parent_function, comment_id) VALUES (?, ?, ?, ?)";
        final ArrayList<INaviCodeNode> codeNodesWithUnsavedComments = new ArrayList<INaviCodeNode>();
        final PreparedStatement preparedStatement = provider.getConnection().getConnection().prepareStatement(query);
        try {
            for (final int index : codeNodeIndices) {
                final INaviCodeNode codeNode = (INaviCodeNode) nodes.get(index);
                codeNode.setId(firstNode + index);
                INaviFunction function = null;
                try {
                    function = codeNode.getParentFunction();
                } catch (final MaybeNullException e) {
                }
                final int moduleId = Iterables.getLast(codeNode.getInstructions()).getModule().getConfiguration().getId();
                final List<IComment> comment = codeNode.getComments().getLocalCodeNodeComment();
                final Integer commentId = comment == null ? null : comment.size() == 0 ? null : Iterables.getLast(comment).getId();
                if ((comment != null) && (comment.size() != 0) && (commentId == null)) {
                    codeNodesWithUnsavedComments.add(codeNode);
                }
                preparedStatement.setInt(1, moduleId);
                preparedStatement.setInt(2, firstNode + index);
                if (function == null) {
                    preparedStatement.setNull(3, Types.BIGINT);
                } else {
                    preparedStatement.setObject(3, function.getAddress().toBigInteger(), Types.BIGINT);
                }
                if (commentId == null) {
                    preparedStatement.setNull(4, Types.INTEGER);
                } else {
                    preparedStatement.setInt(4, commentId);
                }
                preparedStatement.addBatch();
            }
            preparedStatement.executeBatch();
        } finally {
            preparedStatement.close();
        }
        // implementation.
        for (final INaviCodeNode codeNode : codeNodesWithUnsavedComments) {
            final ArrayList<IComment> codeNodecomments = new ArrayList<IComment>();
            for (final IComment comment : codeNode.getComments().getLocalCodeNodeComment()) {
                try {
                    final Integer commentId = PostgreSQLNodeFunctions.appendLocalCodeNodeComment(provider, codeNode, comment.getComment(), comment.getUser().getUserId());
                    final IComment newComment = new CComment(commentId, comment.getUser(), comment.getParent(), comment.getComment());
                    codeNodecomments.add(newComment);
                } catch (final CouldntSaveDataException exception) {
                    CUtilityFunctions.logException(exception);
                }
            }
            codeNode.getComments().initializeLocalCodeNodeComment(codeNodecomments);
        }
        // implementation.
        for (final Pair<INaviCodeNode, INaviInstruction> pair : instructionsWithUnsavedLocalComments) {
            final ArrayList<IComment> localInstructionComments = new ArrayList<IComment>();
            for (final IComment comment : pair.first().getComments().getLocalInstructionComment(pair.second())) {
                try {
                    final int commentId = PostgreSQLInstructionFunctions.appendLocalInstructionComment(provider, pair.first(), pair.second(), comment.getComment(), comment.getUser().getUserId());
                    final IComment newComment = new CComment(commentId, comment.getUser(), comment.getParent(), comment.getComment());
                    localInstructionComments.add(newComment);
                } catch (final CouldntSaveDataException exception) {
                    CUtilityFunctions.logException(exception);
                }
            }
            pair.first().getComments().initializeLocalInstructionComment(pair.second(), localInstructionComments);
        }
    }
}
Also used : IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) MaybeNullException(com.google.security.zynamics.binnavi.Exceptions.MaybeNullException) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) CComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment) INaviCodeNode(com.google.security.zynamics.binnavi.disassembly.INaviCodeNode) INaviFunction(com.google.security.zynamics.binnavi.disassembly.INaviFunction) Pair(com.google.security.zynamics.zylib.general.Pair) INaviInstruction(com.google.security.zynamics.binnavi.disassembly.INaviInstruction)

Example 15 with INaviFunction

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

the class CCodeNodeMenu method addOpenOriginalFunctionMenu.

private void addOpenOriginalFunctionMenu(final CGraphModel model, final NaviNode node) {
    final INaviCodeNode rawNode = (INaviCodeNode) node.getRawNode();
    try {
        final INaviFunction nodeFunction = rawNode.getParentFunction();
        final INaviFunction viewFunction = model.getViewContainer().getFunction(model.getGraph().getRawView());
        if (nodeFunction != viewFunction) {
            add(CActionProxy.proxy(new COpenOriginalFunction(model.getParent(), model.getViewContainer(), nodeFunction)));
        }
    } catch (final MaybeNullException e) {
    // If there is no original function then we can not open it.
    }
}
Also used : COpenOriginalFunction(com.google.security.zynamics.binnavi.ZyGraph.Menus.Actions.COpenOriginalFunction) INaviCodeNode(com.google.security.zynamics.binnavi.disassembly.INaviCodeNode) MaybeNullException(com.google.security.zynamics.binnavi.Exceptions.MaybeNullException) INaviFunction(com.google.security.zynamics.binnavi.disassembly.INaviFunction)

Aggregations

INaviFunction (com.google.security.zynamics.binnavi.disassembly.INaviFunction)94 INaviModule (com.google.security.zynamics.binnavi.disassembly.INaviModule)39 Test (org.junit.Test)38 INaviView (com.google.security.zynamics.binnavi.disassembly.views.INaviView)26 INaviInstruction (com.google.security.zynamics.binnavi.disassembly.INaviInstruction)20 ExpensiveBaseTest (com.google.security.zynamics.binnavi.disassembly.types.ExpensiveBaseTest)16 IAddress (com.google.security.zynamics.zylib.disassembly.IAddress)16 INaviCodeNode (com.google.security.zynamics.binnavi.disassembly.INaviCodeNode)14 CAddress (com.google.security.zynamics.zylib.disassembly.CAddress)14 CTag (com.google.security.zynamics.binnavi.Tagging.CTag)12 INaviViewNode (com.google.security.zynamics.binnavi.disassembly.INaviViewNode)12 ArrayList (java.util.ArrayList)12 MaybeNullException (com.google.security.zynamics.binnavi.Exceptions.MaybeNullException)11 MockView (com.google.security.zynamics.binnavi.disassembly.MockView)10 CCodeNode (com.google.security.zynamics.binnavi.disassembly.CCodeNode)9 FilledList (com.google.security.zynamics.zylib.types.lists.FilledList)9 HashMap (java.util.HashMap)9 CFunctionNode (com.google.security.zynamics.binnavi.disassembly.CFunctionNode)8 COperandTree (com.google.security.zynamics.binnavi.disassembly.COperandTree)8 INaviEdge (com.google.security.zynamics.binnavi.disassembly.INaviEdge)8