Search in sources :

Example 46 with IComment

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

the class PostgreSQLInstructionFunctions method createInstructions.

/**
   * Saves an instruction to the database.
   *
   * @param provider The provider used to access the database.
   * @param instructions The instruction to save.
   *
   * @throws SQLException Thrown if the instruction could not be created.
   */
public static void createInstructions(final SQLProvider provider, final Iterable<INaviInstruction> instructions) throws SQLException {
    Preconditions.checkNotNull(provider, "IE01550: Provider argument can not be null");
    Preconditions.checkNotNull(instructions, "IE01554: Instruction argument can not be null");
    final String query = "INSERT INTO " + CTableNames.INSTRUCTIONS_TABLE + "(module_id, address, mnemonic, data, native, architecture, comment_id) " + "VALUES(?, ?, ?, ?, ?, ?, ?)";
    final PreparedStatement insertStatement = provider.getConnection().getConnection().prepareStatement(query);
    final ArrayList<INaviInstruction> instructionsWithUnsavedComments = new ArrayList<INaviInstruction>();
    final List<List<COperandTree>> operands = new ArrayList<List<COperandTree>>();
    for (final INaviInstruction instruction : instructions) {
        final String mnemonic = instruction.getMnemonic();
        final byte[] data = instruction.getData();
        operands.add(instruction.getOperands());
        final INaviModule module = instruction.getModule();
        final IAddress address = instruction.getAddress();
        final int moduleID = module.getConfiguration().getId();
        final List<IComment> comments = instruction.getGlobalComment();
        final Integer commentId = comments == null ? null : comments.size() == 0 ? null : Iterables.getLast(comments).getId();
        if ((comments != null) && (comments.size() != 0) && (commentId == null)) {
            instructionsWithUnsavedComments.add(instruction);
        }
        try {
            insertStatement.setInt(1, moduleID);
            insertStatement.setObject(2, address.toBigInteger(), Types.BIGINT);
            insertStatement.setString(3, mnemonic);
            insertStatement.setBytes(4, data);
            insertStatement.setBoolean(5, false);
            insertStatement.setObject(6, instruction.getArchitecture(), Types.OTHER);
            if (commentId == null) {
                insertStatement.setNull(7, Types.INTEGER);
            } else {
                insertStatement.setInt(7, commentId);
            }
            insertStatement.execute();
        } finally {
            insertStatement.close();
        }
    }
    // unsaved comments.
    for (final INaviInstruction instruction : instructionsWithUnsavedComments) {
        final ArrayList<IComment> instructionComments = new ArrayList<IComment>();
        for (final IComment comment : instruction.getGlobalComment()) {
            try {
                final Integer commentId = PostgreSQLInstructionFunctions.appendGlobalInstructionComment(provider, instruction, comment.getComment(), comment.getUser().getUserId());
                final IComment newComment = new CComment(commentId, comment.getUser(), comment.getParent(), comment.getComment());
                instructionComments.add(newComment);
            } catch (final CouldntSaveDataException exception) {
                CUtilityFunctions.logException(exception);
            }
        }
        instruction.initializeGlobalComment(instructionComments);
    }
    for (final List<COperandTree> operand : operands) {
        int position = 0;
        for (final COperandTree operandTree : operand) {
            createOperandTree(provider, operandTree, position);
            position++;
        }
    }
}
Also used : 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) IAddress(com.google.security.zynamics.zylib.disassembly.IAddress) BigInteger(java.math.BigInteger) CComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment) INaviModule(com.google.security.zynamics.binnavi.disassembly.INaviModule) COperandTree(com.google.security.zynamics.binnavi.disassembly.COperandTree) ArrayList(java.util.ArrayList) List(java.util.List) INaviInstruction(com.google.security.zynamics.binnavi.disassembly.INaviInstruction)

Example 47 with IComment

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

the class TypeInstanceContainerBackend method loadTypeInstance.

/**
   * Loads a single type instance from the database.
   *
   * @param typeInstanceId The {@link Integer} id of the {@link TypeInstance type instance} to load.
   * @return A {@link TypeInstance instance} if present in the database.
   *
   * @throws CouldntLoadDataException Thrown if the {@link TypeInstance type instance} could not be
   *         loaded from the database.
   */
public TypeInstance loadTypeInstance(final Integer typeInstanceId) throws CouldntLoadDataException {
    Preconditions.checkNotNull(typeInstanceId, "Error: typeInstanceId argument can not be null");
    final RawTypeInstance rawTypeInstance = provider.loadTypeInstance(module, typeInstanceId);
    final BaseType baseType = typeManager.getBaseType(rawTypeInstance.getTypeId());
    final Section section = sectionContainer.getSection(rawTypeInstance.getSectionId());
    final TypeInstance typeInstance = new TypeInstance(rawTypeInstance.getId(), rawTypeInstance.getName(), baseType, section, rawTypeInstance.getSectionOffset(), module);
    instancesById.put(typeInstance.getId(), typeInstance);
    if (rawTypeInstance.getCommentId() != null) {
        final CommentManager manager = CommentManager.get(provider);
        final List<IComment> comments = provider.loadCommentById(rawTypeInstance.getCommentId());
        manager.initializeTypeInstanceComment(typeInstance, comments);
    }
    return typeInstance;
}
Also used : IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) CommentManager(com.google.security.zynamics.binnavi.disassembly.CommentManager)

Example 48 with IComment

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

the class CommentManager method unloadComment.

/**
   * Unloads a comment and removes the items it references from the internal storage.
   *
   * @param strategy The {@link CommentingStrategy} which holds the function forwarders.
   * @param comments The List of {@link IComment} to be unloaded.
   */
private synchronized void unloadComment(final CommentingStrategy strategy, final List<IComment> comments) {
    Preconditions.checkNotNull(strategy, "Error: strategy argument can not be null");
    if (comments == null) {
        return;
    }
    strategy.removeComments(comments);
    for (final IComment comment : comments) {
        commentIdToComment.remove(comment.getId());
    }
}
Also used : IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment)

Example 49 with IComment

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

the class CommentManager method appendComment.

/**
   * This function provides the append comment functionality for any given Implementation of a
   * {@link CommentingStrategy}. It uses the methods of the interface to only have one algorithm
   * with different objects that can be commented.
   *
   * @param strategy The {@link CommentingStrategy} which holds the function forwarders.
   * @param commentText The commenting text to append.
   *
   * @return The generated comment.
   *
   * @throws CouldntSaveDataException if the comment could not be stored in the database.
   * @throws CouldntLoadDataException if the list of comments now associated with the commented
   *         object could not be loaded from the database.
   */
private synchronized List<IComment> appendComment(final CommentingStrategy strategy, final String commentText) throws CouldntSaveDataException, CouldntLoadDataException {
    final IUser user = CUserManager.get(provider).getCurrentActiveUser();
    final List<IComment> currentComments = new ArrayList<IComment>();
    if (strategy.isStored()) {
        currentComments.addAll(strategy.appendComment(commentText, user.getUserId()));
    } else {
        currentComments.addAll(strategy.getComments() == null ? new ArrayList<IComment>() : Lists.newArrayList(strategy.getComments()));
        final IComment parent = currentComments.isEmpty() ? null : Iterables.getLast(currentComments);
        final IComment newComment = new CComment(null, user, parent, commentText);
        currentComments.add(newComment);
    }
    strategy.saveComments(currentComments);
    for (final IComment comment : currentComments) {
        commentIdToComment.put(comment.getId(), comment);
    }
    for (final CommentListener listener : listeners) {
        try {
            strategy.sendAppendedCommentNotifcation(listener, Iterables.getLast(currentComments));
        } catch (final Exception exception) {
            CUtilityFunctions.logException(exception);
        }
    }
    return currentComments;
}
Also used : CComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment) IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) ArrayList(java.util.ArrayList) IUser(com.google.security.zynamics.binnavi.Gui.Users.Interfaces.IUser) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) CouldntDeleteException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntDeleteException)

Example 50 with IComment

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

the class GroupNodeTest method testSetCollapsed.

@Test
public void testSetCollapsed() {
    final Database database = new Database(new MockDatabase());
    final MockModule mockModule = new MockModule();
    final TagManager nodeTagManager = new TagManager(new MockTagManager(com.google.security.zynamics.binnavi.Tagging.TagType.NODE_TAG));
    final TagManager viewTagManager = new TagManager(new MockTagManager(com.google.security.zynamics.binnavi.Tagging.TagType.VIEW_TAG));
    final Module module = new Module(database, mockModule, nodeTagManager, viewTagManager);
    final MockView mockView = new MockView();
    final View view = new View(module, mockView, nodeTagManager, viewTagManager);
    final INaviGroupNode internalGroupNode = new CGroupNode(0, 0, 0, 0, 0, Color.RED, false, false, new HashSet<CTag>(), new ArrayList<IComment>(), false, new MockSqlProvider());
    final GroupNode node = new GroupNode(view, internalGroupNode, viewTagManager);
    final MockGroupNodeListener listener = new MockGroupNodeListener();
    node.addListener(listener);
    node.setCollapsed(true);
    assertEquals("changedState;", listener.events);
    assertTrue(node.isCollapsed());
    node.removeListener(listener);
}
Also used : IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) MockView(com.google.security.zynamics.binnavi.disassembly.MockView) CTag(com.google.security.zynamics.binnavi.Tagging.CTag) CGroupNode(com.google.security.zynamics.binnavi.disassembly.CGroupNode) INaviGroupNode(com.google.security.zynamics.binnavi.disassembly.INaviGroupNode) MockTagManager(com.google.security.zynamics.binnavi.Tagging.MockTagManager) INaviView(com.google.security.zynamics.binnavi.disassembly.views.INaviView) MockView(com.google.security.zynamics.binnavi.disassembly.MockView) MockTagManager(com.google.security.zynamics.binnavi.Tagging.MockTagManager) MockModule(com.google.security.zynamics.binnavi.disassembly.Modules.MockModule) MockSqlProvider(com.google.security.zynamics.binnavi.Database.MockClasses.MockSqlProvider) MockDatabase(com.google.security.zynamics.binnavi.Database.MockClasses.MockDatabase) MockDatabase(com.google.security.zynamics.binnavi.Database.MockClasses.MockDatabase) MockModule(com.google.security.zynamics.binnavi.disassembly.Modules.MockModule) CModule(com.google.security.zynamics.binnavi.disassembly.Modules.CModule) INaviGroupNode(com.google.security.zynamics.binnavi.disassembly.INaviGroupNode) CGroupNode(com.google.security.zynamics.binnavi.disassembly.CGroupNode) 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