Search in sources :

Example 91 with IComment

use of com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment in project binnavi by google.

the class InstructionCommentsDataModel method updateDataRepresentation.

private void updateDataRepresentation() {
    internalData.clear();
    for (INaviInstruction instruction : node.getInstructions()) {
        internalData.add(new Pair<INaviInstruction, ArrayList<IComment>>(instruction, new ArrayList<IComment>()));
    }
    int currentIndex = 0;
    for (Pair<INaviInstruction, IComment> dataPair : commentAccessor.getAllComments()) {
        // Is this the right instruction?
        while (dataPair.first() != internalData.get(currentIndex).first()) {
            currentIndex++;
        }
        internalData.get(currentIndex).second().add(dataPair.second());
    }
}
Also used : IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) ArrayList(java.util.ArrayList) INaviInstruction(com.google.security.zynamics.binnavi.disassembly.INaviInstruction)

Example 92 with IComment

use of com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment in project binnavi by google.

the class InstructionCommentsDataModel method getCommentAndIndexAtCoordinate.

/*
   * The string that IComment contains is a multi-line string; it contains newline characters that
   * make the UI display multiple "lines" of comments for a given row (code address).
   *
   * In order to make it easy for the caller to identify which substring of a IComment is relevant
   * given a particular cursor position, this function returns the beginning and the end index
   * within the larger IComment of the comment indicated by coordinate.
   */
private Pair<IComment, Pair<Integer, Integer>> getCommentAndIndexAtCoordinate(CodeDisplayCoordinate coordinate) {
    // Get the list of comments for the instruction.
    ArrayList<IComment> comments = internalData.get(coordinate.getRow()).second();
    int currentLineIndex = 0;
    // coordinate.
    for (IComment comment : comments) {
        int numberOfCommentLines = Math.max(comment.getNumberOfCommentLines(), 1);
        // Does the current comment (at currentLineIndex) span over the line field of the coordinate?
        if ((currentLineIndex + numberOfCommentLines - 1) >= coordinate.getLine()) {
            // Yes. Is it an empty comment? Then return now.
            if (comment.getNumberOfCommentLines() == 0) {
                return new Pair<IComment, Pair<Integer, Integer>>(comment, new Pair<Integer, Integer>(0, 0));
            }
            // The comment is non-empty and spans the line in the coordinate. Now identify the beginning
            // and end indices of the right line within the comment (remember that comments are strings
            // with newlines - so a given line that is displayed is just a beginning and end index into
            // a longer string).
            String commentString = comment.getComment();
            int beginIndex = 0;
            int endIndex = 0;
            int targetSubstringIndex = coordinate.getLine() - currentLineIndex;
            // The beginIndex is the index of the first character after the Nth newline (where N is
            // targetSubstringIndex), the endIndex is the last character before either end-of-string or
            // the N+1th newline.
            int newlineCounter = 0;
            while (newlineCounter != targetSubstringIndex) {
                if (commentString.charAt(beginIndex++) == '\n') {
                    newlineCounter++;
                }
            }
            endIndex = commentString.indexOf('\n', beginIndex);
            endIndex = (endIndex == -1) ? commentString.length() : endIndex;
            return new Pair<IComment, Pair<Integer, Integer>>(comment, new Pair<Integer, Integer>(beginIndex, endIndex));
        }
        currentLineIndex += numberOfCommentLines;
    }
    // This can never happen.
    return null;
}
Also used : IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) Pair(com.google.security.zynamics.zylib.general.Pair)

Example 93 with IComment

use of com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment in project binnavi by google.

the class CCommentUtilities method getLocalInstructionComments.

/**
   * TODO (timkornau): either comment the function or find a better way on how the commenting for
   * instructions can access all comments.
   */
public static List<Pair<INaviInstruction, IComment>> getLocalInstructionComments(final CCodeNode codeNode) {
    Preconditions.checkNotNull(codeNode, "IE02633: codeNode argument can not be null");
    final List<Pair<INaviInstruction, IComment>> values = new ArrayList<Pair<INaviInstruction, IComment>>();
    final CCodeNodeComments currentComments = codeNode.getComments();
    for (final INaviInstruction instruction : codeNode.getInstructions()) {
        final List<IComment> comments = currentComments.getLocalInstructionComment(instruction);
        if ((comments == null) || comments.isEmpty()) {
            values.add(new Pair<INaviInstruction, IComment>(instruction, null));
            continue;
        } else {
            for (final IComment comment : comments) {
                values.add(new Pair<INaviInstruction, IComment>(instruction, comment));
            }
        }
    }
    return values;
}
Also used : IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) CCodeNodeComments(com.google.security.zynamics.binnavi.disassembly.CCodeNodeComments) ArrayList(java.util.ArrayList) INaviInstruction(com.google.security.zynamics.binnavi.disassembly.INaviInstruction) Pair(com.google.security.zynamics.zylib.general.Pair)

Example 94 with IComment

use of com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment in project binnavi by google.

the class PostgreSQLFunctionCommentTests method editFunctionComment5.

@Test
public void editFunctionComment5() throws CouldntLoadDataException, CouldntSaveDataException {
    final List<IComment> comments = function.getGlobalComment() == null ? new ArrayList<IComment>() : function.getGlobalComment();
    final IComment lastComment = comments.size() == 0 ? null : Iterables.getLast(comments);
    final IUser user = new UniqueTestUserGenerator(getProvider()).nextActiveUser();
    final String commentText = " FUNCTION COMMENT TEST BEFORE EDIT ";
    final Integer commentId = getProvider().appendFunctionComment(function, commentText, user.getUserId());
    final IComment newComment = new CComment(commentId, user, lastComment, commentText);
    final ArrayList<IComment> newComments = getProvider().loadCommentById(commentId);
    assertNotNull(newComments);
    assertEquals(comments.size() + 1, newComments.size());
    assertEquals(newComment, newComments.get(newComments.size() - 1));
    final String commentAfterEdit = " FUNCTION COMMENT TEST AFTER EDIT ";
    getProvider().editFunctionComment(function, commentId, user.getUserId(), commentAfterEdit);
    final ArrayList<IComment> commentsAfterEdit = getProvider().loadCommentById(commentId);
    assertEquals(commentAfterEdit, Iterables.getLast(commentsAfterEdit).getComment());
    assertEquals(commentsAfterEdit.size(), newComments.size());
}
Also used : CComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment) IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) IUser(com.google.security.zynamics.binnavi.Gui.Users.Interfaces.IUser) UniqueTestUserGenerator(com.google.security.zynamics.binnavi.Database.PostgreSQL.UniqueTestUserGenerator) ExpensiveBaseTest(com.google.security.zynamics.binnavi.disassembly.types.ExpensiveBaseTest) Test(org.junit.Test)

Example 95 with IComment

use of com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment in project binnavi by google.

the class PostgreSQLFunctionCommentTests method deleteFunctionComment7.

/**
   * This test checks if the delete of a comment in a series of comments works if the comment is the
   * first comment.
   *
   * <pre>
   * Comment 1:      
   * Comment 2:  ->  Comment 2:
   * Comment 3:      Comment 3:
   * </pre>
   *
   */
@Test
public void deleteFunctionComment7() throws CouldntLoadDataException, CouldntSaveDataException, CouldntDeleteException {
    final IUser user = new UniqueTestUserGenerator(getProvider()).nextActiveUser();
    final List<IComment> storedComments = function.getGlobalComment() == null ? new ArrayList<IComment>() : function.getGlobalComment();
    final IComment lastComment = storedComments.isEmpty() ? null : Iterables.getLast(storedComments);
    final String comment1String = " Comment 1: ";
    final int comment1Id = getProvider().appendFunctionComment(function, comment1String, user.getUserId());
    final IComment comment1 = new CComment(comment1Id, user, lastComment, comment1String);
    final String comment2String = " Comment 2: ";
    final int comment2Id = getProvider().appendFunctionComment(function, comment2String, user.getUserId());
    final IComment comment2 = new CComment(comment2Id, user, comment1, comment2String);
    final String comment3String = " Comment 3: ";
    final int comment3Id = getProvider().appendFunctionComment(function, comment3String, user.getUserId());
    final IComment comment3 = new CComment(comment3Id, user, comment2, comment3String);
    final ArrayList<IComment> commentsBeforeDelete = getProvider().loadCommentById(comment3Id);
    assertNotNull(commentsBeforeDelete);
    assertEquals(storedComments.size() + 3, commentsBeforeDelete.size());
    assertTrue(commentsBeforeDelete.contains(comment1));
    assertTrue(commentsBeforeDelete.contains(comment2));
    assertTrue(commentsBeforeDelete.contains(comment3));
    assertEquals(comment3, Iterables.getLast(commentsBeforeDelete));
    assertEquals(comment2, commentsBeforeDelete.get(commentsBeforeDelete.size() - 2));
    assertEquals(comment1, commentsBeforeDelete.get(commentsBeforeDelete.size() - 3));
    getProvider().deleteFunctionComment(function, comment1Id, user.getUserId());
    final ArrayList<IComment> commentsAfterDelete1 = getProvider().loadCommentById(comment1Id);
    assertNotNull(commentsAfterDelete1);
    assertTrue(commentsAfterDelete1.isEmpty());
    final ArrayList<IComment> commentsAfterDelete2 = getProvider().loadCommentById(comment3Id);
    final IComment comment2AfterDelete = new CComment(comment2Id, user, lastComment, comment2String);
    final IComment comment3AfterDelete = new CComment(comment3Id, user, comment2AfterDelete, comment3String);
    assertNotNull(commentsAfterDelete2);
    assertEquals(storedComments.size() + 2, commentsAfterDelete2.size());
    assertTrue(commentsAfterDelete2.contains(comment3AfterDelete));
    assertTrue(commentsAfterDelete2.contains(comment2AfterDelete));
    assertEquals(comment3AfterDelete, Iterables.getLast(commentsAfterDelete2));
    assertEquals(comment2AfterDelete, commentsAfterDelete2.get(commentsAfterDelete2.size() - 2));
}
Also used : CComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment) IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) IUser(com.google.security.zynamics.binnavi.Gui.Users.Interfaces.IUser) UniqueTestUserGenerator(com.google.security.zynamics.binnavi.Database.PostgreSQL.UniqueTestUserGenerator) ExpensiveBaseTest(com.google.security.zynamics.binnavi.disassembly.types.ExpensiveBaseTest) Test(org.junit.Test)

Aggregations

IComment (com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment)167 Test (org.junit.Test)129 CComment (com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment)114 IUser (com.google.security.zynamics.binnavi.Gui.Users.Interfaces.IUser)97 UniqueTestUserGenerator (com.google.security.zynamics.binnavi.Database.PostgreSQL.UniqueTestUserGenerator)89 ExpensiveBaseTest (com.google.security.zynamics.binnavi.disassembly.types.ExpensiveBaseTest)89 ArrayList (java.util.ArrayList)20 INaviCodeNode (com.google.security.zynamics.binnavi.disassembly.INaviCodeNode)18 INaviGroupNode (com.google.security.zynamics.binnavi.disassembly.INaviGroupNode)16 INaviTextNode (com.google.security.zynamics.binnavi.disassembly.INaviTextNode)13 CouldntSaveDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException)12 INaviView (com.google.security.zynamics.binnavi.disassembly.views.INaviView)11 CouldntLoadDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException)10 MockSqlProvider (com.google.security.zynamics.binnavi.Database.MockClasses.MockSqlProvider)10 INaviInstruction (com.google.security.zynamics.binnavi.disassembly.INaviInstruction)9 INaviViewNode (com.google.security.zynamics.binnavi.disassembly.INaviViewNode)9 PreparedStatement (java.sql.PreparedStatement)8 CTag (com.google.security.zynamics.binnavi.Tagging.CTag)7 INaviEdge (com.google.security.zynamics.binnavi.disassembly.INaviEdge)7 INaviFunction (com.google.security.zynamics.binnavi.disassembly.INaviFunction)7