Search in sources :

Example 96 with INaviModule

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

the class PostgreSQLCommentNotificationParser method processTypeInstanceCommentNotification.

/**
   * Parses the {@link PGNotification notifications} from the PostgreSQL database back end for
   * {@link TypeInstance type instance} comments by using a regular expression.
   *
   * @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 processTypeInstanceCommentNotification(final PGNotification notification, final SQLProvider provider) {
    final Matcher matcher = TYPE_INSTANCE_PATTERN.matcher(notification.getParameter());
    if (!matcher.find()) {
        return null;
    }
    final Integer moduleId = Integer.parseInt(matcher.group(3));
    final Integer typeInstanceId = Integer.parseInt(matcher.group(4));
    final Integer commentId = matcher.group(5).equals("null") ? null : Integer.parseInt(matcher.group(5));
    final INaviModule module = provider.findModule(moduleId);
    if (module == null || !module.isLoaded()) {
        return null;
    }
    final TypeInstance instance = module.getContent().getTypeInstanceContainer().getTypeInstanceById(typeInstanceId);
    if (instance == null) {
        return null;
    }
    final CommentOperation operation = commentId == null ? CommentOperation.DELETE : CommentOperation.APPEND;
    return new TypeInstanceCommentNotificationContainer(instance, operation, commentId);
}
Also used : BigInteger(java.math.BigInteger) CommentOperation(com.google.security.zynamics.binnavi.disassembly.CommentManager.CommentOperation) TypeInstanceCommentNotificationContainer(com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.TypeInstanceCommentNotificationContainer) INaviModule(com.google.security.zynamics.binnavi.disassembly.INaviModule) Matcher(java.util.regex.Matcher) TypeInstance(com.google.security.zynamics.binnavi.disassembly.types.TypeInstance)

Example 97 with INaviModule

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

the class PostgreSQLCommentNotificationParser method processInstructionGlobalCommentNotification.

/**
   * Parses the notifications from the database back end for global instruction comments by using a
   * regular expression. If the regular expression matched the supplied {@link PGNotification}
   * notification, it is determined if the {@link INaviInstruction} instruction in the notification
   * is currently loaded, and if a {@link CommentNotificationContainer} with the gathered data is
   * returned.
   *
   * @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 processInstructionGlobalCommentNotification(final PGNotification notification, final SQLProvider provider) {
    final Matcher matcher = INSTRUCTION_GLOBAL_PATTERN.matcher(notification.getParameter());
    if (!matcher.find()) {
        return null;
    }
    final Integer moduleId = Integer.parseInt(matcher.group(3));
    final IAddress address = new CAddress(new BigInteger(matcher.group(4)));
    final Integer commentId = matcher.group(7) == null ? null : Integer.parseInt(matcher.group(7));
    final INaviModule module = provider.findModule(moduleId);
    if ((module == null) || !module.isLoaded()) {
        return null;
    }
    final INaviInstruction instruction = InstructionCache.get(provider).getInstructionByAddress(address, moduleId);
    if (instruction == null) {
        return null;
    }
    final CommentOperation operation = commentId == null ? CommentOperation.DELETE : CommentOperation.APPEND;
    return new InstructionCommentNotificationContainer(instruction, null, operation, CommentScope.GLOBAL, commentId);
}
Also used : BigInteger(java.math.BigInteger) CommentOperation(com.google.security.zynamics.binnavi.disassembly.CommentManager.CommentOperation) INaviModule(com.google.security.zynamics.binnavi.disassembly.INaviModule) Matcher(java.util.regex.Matcher) BigInteger(java.math.BigInteger) InstructionCommentNotificationContainer(com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.InstructionCommentNotificationContainer) IAddress(com.google.security.zynamics.zylib.disassembly.IAddress) CAddress(com.google.security.zynamics.zylib.disassembly.CAddress) INaviInstruction(com.google.security.zynamics.binnavi.disassembly.INaviInstruction)

Example 98 with INaviModule

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

the class PostgreSQLCommentNotificationParser method processNodeGlobalCommentNotification.

/**
   * Parses the notifications from the database back end for global code node comments by using a
   * regular expression. If the regular expression matches the supplied {@link PGNotification}
   * notification, it is determined if the code node in the notification is currently loaded, and if
   * a {@link CommentNotificationContainer} with the data from the notification is returned.
   *
   * @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> processNodeGlobalCommentNotification(final PGNotification notification, final SQLProvider provider) {
    final Matcher matcher = NODE_GLOBAL_PATTERN.matcher(notification.getParameter());
    if (!matcher.find()) {
        return new ArrayList<>();
    }
    final String databaseOperation = matcher.group(2);
    final int moduleId = Integer.parseInt(matcher.group(3));
    final IAddress nodeAddress = new CAddress(new BigInteger(matcher.group(4)));
    final Integer commentId = matcher.group(6) == null ? null : Integer.parseInt(matcher.group(6));
    final INaviModule notificationModule = provider.findModule(moduleId);
    if ((notificationModule == null) || !notificationModule.isLoaded()) {
        return new ArrayList<>();
    }
    final ImmutableCollection<INaviViewNode> nodes = NodeCache.get(provider).getNodeByAddress(nodeAddress, moduleId);
    if (nodes == null) {
        return new ArrayList<>();
    }
    final CommentOperation operation = databaseOperation.equalsIgnoreCase("DELETE") ? CommentOperation.DELETE : CommentOperation.APPEND;
    Collection<CommentNotification> notifications = new ArrayList<>();
    for (INaviViewNode node : nodes) {
        notifications.add(new CodeNodeCommentNotificationContainer((INaviCodeNode) node, 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) 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) INaviCodeNode(com.google.security.zynamics.binnavi.disassembly.INaviCodeNode) INaviModule(com.google.security.zynamics.binnavi.disassembly.INaviModule) BigInteger(java.math.BigInteger) INaviViewNode(com.google.security.zynamics.binnavi.disassembly.INaviViewNode) CodeNodeCommentNotificationContainer(com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.CodeNodeCommentNotificationContainer)

Example 99 with INaviModule

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

the class PostgreSQLCommentNotificationParser method processNodeLocalInstructionCommentNotification.

/**
   * Parses the {@link PGNotification} notifications from the database back end for local
   * instruction comments by using a regular expression. If the regular expression matches the
   * supplied {@link PGNotification} notification, it is determined if the instruction in the
   * notification is currently loaded, and if a {@link CommentNotificationContainer} with the data
   * from the notification is returned.
   *
   * @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 processNodeLocalInstructionCommentNotification(final PGNotification notification, final SQLProvider provider) {
    final Matcher instructionMatcher = INSTRUCTION_LOCAL_PATTERN.matcher(notification.getParameter());
    final boolean instructionMatchFound = instructionMatcher.find();
    if (!instructionMatchFound) {
        return null;
    }
    final Integer moduleId = Integer.parseInt(instructionMatcher.group(3));
    final Integer nodeId = Integer.parseInt(instructionMatcher.group(4));
    final BigInteger notificationInstructionAddress = new BigInteger(instructionMatcher.group(6));
    final Integer commentId = instructionMatcher.group(7).equals("null") ? null : Integer.parseInt(instructionMatcher.group(7));
    final INaviModule module = provider.findModule(moduleId);
    if ((module == null) || !module.isLoaded()) {
        return null;
    }
    final IAddress address = new CAddress(notificationInstructionAddress);
    final INaviInstruction instruction = InstructionCache.get(provider).getInstructionByAddress(address, module.getConfiguration().getId());
    if (instruction == null) {
        return null;
    }
    final INaviCodeNode codeNode = (INaviCodeNode) NodeCache.get(provider).getNodeById(nodeId);
    if (codeNode == null) {
        return null;
    }
    final CommentOperation operation = commentId == null ? CommentOperation.DELETE : CommentOperation.APPEND;
    return new InstructionCommentNotificationContainer(instruction, codeNode, operation, CommentScope.LOCAL, commentId);
}
Also used : BigInteger(java.math.BigInteger) CommentOperation(com.google.security.zynamics.binnavi.disassembly.CommentManager.CommentOperation) INaviCodeNode(com.google.security.zynamics.binnavi.disassembly.INaviCodeNode) INaviModule(com.google.security.zynamics.binnavi.disassembly.INaviModule) Matcher(java.util.regex.Matcher) BigInteger(java.math.BigInteger) InstructionCommentNotificationContainer(com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.containers.InstructionCommentNotificationContainer) IAddress(com.google.security.zynamics.zylib.disassembly.IAddress) CAddress(com.google.security.zynamics.zylib.disassembly.CAddress) INaviInstruction(com.google.security.zynamics.binnavi.disassembly.INaviInstruction)

Example 100 with INaviModule

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

the class PostgreSQLFunctionNodeLoader method load.

/**
   * Loads the function nodes of a view.
   * 
   * @param provider The connection to the database.
   * @param view The view whose function nodes are loaded.
   * @param nodes The loaded nodes are stored here.
   * 
   * @throws CPartialLoadException Thrown if loading the nodes failed because a necessary module was
   *         not loaded.
   * @throws CouldntLoadDataException
   */
public static void load(final AbstractSQLProvider provider, final INaviView view, final List<INaviViewNode> nodes) throws CPartialLoadException, CouldntLoadDataException {
    Preconditions.checkNotNull(provider, "IE02510: provider argument can not be null");
    Preconditions.checkNotNull(view, "IE02511: view argument can not be null");
    Preconditions.checkNotNull(nodes, "IE02512: nodes argument can not be null");
    // TODO (timkornau): query needs to go into the database.
    final String query = "SELECT nodes.view_id, nodes.id, functions.module_id, " + " function, fnodes.comment_id as local_comment, x, y, width, height, " + " color, selected, visible FROM " + CTableNames.NODES_TABLE + " AS nodes JOIN " + CTableNames.FUNCTION_NODES_TABLE + " AS fnodes " + " ON nodes.id = fnodes.node_id JOIN " + CTableNames.FUNCTIONS_TABLE + " AS functions ON functions.address = fnodes.function " + " AND functions.module_id = fnodes.module_id  WHERE view_id = ?";
    final Map<Integer, INaviFunctionNode> commentIdToFunctionNode = new HashMap<Integer, INaviFunctionNode>();
    try {
        final PreparedStatement statement = provider.getConnection().getConnection().prepareStatement(query);
        statement.setInt(1, view.getConfiguration().getId());
        final ResultSet resultSet = statement.executeQuery();
        try {
            while (resultSet.next()) {
                final int moduleId = resultSet.getInt("module_id");
                final INaviModule module = provider.findModule(moduleId);
                if (!module.isLoaded()) {
                    try {
                        module.load();
                    } catch (final CouldntLoadDataException e) {
                        throw new CPartialLoadException("E00064: The view could not be loaded because not all modules that form the view are loaded", module);
                    } catch (final LoadCancelledException e) {
                        throw new CPartialLoadException("E00065: The view could not be loaded because not all modules that form the view are loaded", module);
                    }
                }
                final IAddress address = PostgreSQLHelpers.loadAddress(resultSet, "function");
                final INaviFunction function = module.getContent().getFunctionContainer().getFunction(address);
                final int nodeId = resultSet.getInt("id");
                Integer commentId = resultSet.getInt("local_comment");
                if (resultSet.wasNull()) {
                    commentId = null;
                }
                final double posX = resultSet.getDouble("x");
                final double posY = resultSet.getDouble("y");
                final double width = resultSet.getDouble("width");
                final double height = resultSet.getDouble("height");
                final Color color = new Color(resultSet.getInt("color"));
                final boolean selected = resultSet.getBoolean("selected");
                final boolean visible = resultSet.getBoolean("visible");
                final INaviFunctionNode functionNode = new CFunctionNode(nodeId, function, posX, posY, width, height, color, selected, visible, null, new HashSet<CTag>(), provider);
                nodes.add(functionNode);
                if (commentId != null) {
                    commentIdToFunctionNode.put(commentId, functionNode);
                }
            }
        } finally {
            resultSet.close();
        }
        if (!commentIdToFunctionNode.isEmpty()) {
            final HashMap<Integer, ArrayList<IComment>> commentIdsToComments = PostgreSQLCommentFunctions.loadMultipleCommentsById(provider, commentIdToFunctionNode.keySet());
            for (final Entry<Integer, ArrayList<IComment>> commentIdToComment : commentIdsToComments.entrySet()) {
                commentIdToFunctionNode.get(commentIdToComment.getKey()).initializeLocalFunctionComment(commentIdToComment.getValue());
            }
        }
    } catch (final SQLException exception) {
        throw new CouldntLoadDataException(exception);
    }
}
Also used : CFunctionNode(com.google.security.zynamics.binnavi.disassembly.CFunctionNode) CPartialLoadException(com.google.security.zynamics.binnavi.Database.Exceptions.CPartialLoadException) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) Color(java.awt.Color) CTag(com.google.security.zynamics.binnavi.Tagging.CTag) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) IAddress(com.google.security.zynamics.zylib.disassembly.IAddress) INaviModule(com.google.security.zynamics.binnavi.disassembly.INaviModule) ResultSet(java.sql.ResultSet) INaviFunctionNode(com.google.security.zynamics.binnavi.disassembly.INaviFunctionNode) LoadCancelledException(com.google.security.zynamics.binnavi.Database.Exceptions.LoadCancelledException) INaviFunction(com.google.security.zynamics.binnavi.disassembly.INaviFunction)

Aggregations

INaviModule (com.google.security.zynamics.binnavi.disassembly.INaviModule)180 Test (org.junit.Test)105 ExpensiveBaseTest (com.google.security.zynamics.binnavi.disassembly.types.ExpensiveBaseTest)69 INaviFunction (com.google.security.zynamics.binnavi.disassembly.INaviFunction)39 CAddress (com.google.security.zynamics.zylib.disassembly.CAddress)39 INaviView (com.google.security.zynamics.binnavi.disassembly.views.INaviView)29 UnrelocatedAddress (com.google.security.zynamics.binnavi.disassembly.UnrelocatedAddress)28 IAddress (com.google.security.zynamics.zylib.disassembly.IAddress)28 MockModule (com.google.security.zynamics.binnavi.disassembly.Modules.MockModule)24 BreakpointAddress (com.google.security.zynamics.binnavi.debug.models.breakpoints.BreakpointAddress)22 ArrayList (java.util.ArrayList)19 INaviInstruction (com.google.security.zynamics.binnavi.disassembly.INaviInstruction)18 CView (com.google.security.zynamics.binnavi.disassembly.views.CView)13 BigInteger (java.math.BigInteger)13 CTag (com.google.security.zynamics.binnavi.Tagging.CTag)12 CouldntSaveDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException)11 CTagManager (com.google.security.zynamics.binnavi.Tagging.CTagManager)11 CouldntLoadDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException)9 CAddressSpace (com.google.security.zynamics.binnavi.disassembly.AddressSpaces.CAddressSpace)9 COperandTree (com.google.security.zynamics.binnavi.disassembly.COperandTree)9