Search in sources :

Example 11 with MaybeNullException

use of com.google.security.zynamics.binnavi.Exceptions.MaybeNullException in project binnavi by google.

the class PostgreSQLNodeFunctions method appendLocalCodeNodeComment.

/**
 * Appends a comment as local code node comment to a code node.
 *
 * @param provider The provider used to access the database.
 * @param codeNode The code node where the comment will be appended.
 * @param commentText The text of the comment which will be appended.
 * @param userId the user id of the currently active user.
 *
 * @throws CouldntSaveDataException
 */
public static int appendLocalCodeNodeComment(final SQLProvider provider, final INaviCodeNode codeNode, final String commentText, final Integer userId) throws CouldntSaveDataException {
    Preconditions.checkNotNull(provider, "IE02453: provider argument can not be null");
    Preconditions.checkNotNull(codeNode, "IE02454: codeNode argument can not be null");
    Preconditions.checkNotNull(commentText, "IE02455: commentText argument can not be null");
    Preconditions.checkNotNull(userId, "IE02456: userId argument can not be null");
    final Connection connection = provider.getConnection().getConnection();
    final String function = "{ ? = call append_local_code_node_comment( ?, ?, ?, ?) }";
    Integer moduleId = null;
    final int nodeId = codeNode.getId();
    try {
        moduleId = codeNode.getParentFunction().getModule().getConfiguration().getId();
    } catch (final MaybeNullException exception) {
        throw new CouldntSaveDataException(exception);
    }
    try {
        final CallableStatement appendCommentFunction = connection.prepareCall(function);
        try {
            appendCommentFunction.registerOutParameter(1, Types.INTEGER);
            appendCommentFunction.setInt(2, moduleId);
            appendCommentFunction.setInt(3, nodeId);
            appendCommentFunction.setInt(4, userId);
            appendCommentFunction.setString(5, commentText);
            appendCommentFunction.execute();
            final int commentId = appendCommentFunction.getInt(1);
            if (appendCommentFunction.wasNull()) {
                throw new CouldntSaveDataException("E00037: ");
            }
            return commentId;
        } finally {
            appendCommentFunction.close();
        }
    } catch (final SQLException exception) {
        throw new CouldntSaveDataException(exception);
    }
}
Also used : BigInteger(java.math.BigInteger) MaybeNullException(com.google.security.zynamics.binnavi.Exceptions.MaybeNullException) SQLException(java.sql.SQLException) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) CConnection(com.google.security.zynamics.binnavi.Database.CConnection)

Example 12 with MaybeNullException

use of com.google.security.zynamics.binnavi.Exceptions.MaybeNullException in project binnavi by google.

the class PostgreSQLNodeFunctions method deleteGlobalCodeNodeComment.

/**
 * Deletes a global node comment from the list of global node comments associated with this code
 * node.
 *
 * @param provider The provider used to access the database.
 * @param codeNode The code node where the comment will be deleted.
 * @param commentId The comment id of the comment to be deleted.
 * @param userId The user id of the currently active user.
 *
 * @throws CouldntDeleteException Thrown if the comment could not be deleted from the database.
 */
public static void deleteGlobalCodeNodeComment(final SQLProvider provider, final INaviCodeNode codeNode, final Integer commentId, final Integer userId) throws CouldntDeleteException {
    Preconditions.checkNotNull(provider, "IE02465: provider argument can not be null");
    Preconditions.checkNotNull(codeNode, "IE02466: codeNode argument can not be null");
    Preconditions.checkNotNull(commentId, "IE02467: comment argument can not be null");
    Preconditions.checkNotNull(userId, "IE02468: userId argument can not be null");
    final String function = " { ? = call delete_global_code_node_comment(?, ?, ?, ?, ?) } ";
    try {
        final CallableStatement deleteCommentStatement = provider.getConnection().getConnection().prepareCall(function);
        try {
            deleteCommentStatement.registerOutParameter(1, Types.INTEGER);
            deleteCommentStatement.setInt(2, codeNode.getParentFunction().getModule().getConfiguration().getId());
            deleteCommentStatement.setInt(3, codeNode.getId());
            deleteCommentStatement.setObject(4, codeNode.getAddress().toBigInteger(), Types.BIGINT);
            deleteCommentStatement.setInt(5, commentId);
            deleteCommentStatement.setInt(6, userId);
            deleteCommentStatement.execute();
            deleteCommentStatement.getInt(1);
            if (deleteCommentStatement.wasNull()) {
                throw new IllegalArgumentException("Error: the comment id returned by the database was null");
            }
        } catch (final MaybeNullException exception) {
            throw new CouldntDeleteException(exception);
        } finally {
            deleteCommentStatement.close();
        }
    } catch (final SQLException exception) {
        throw new CouldntDeleteException(exception);
    }
}
Also used : CouldntDeleteException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntDeleteException) MaybeNullException(com.google.security.zynamics.binnavi.Exceptions.MaybeNullException) SQLException(java.sql.SQLException) CallableStatement(java.sql.CallableStatement)

Example 13 with MaybeNullException

use of com.google.security.zynamics.binnavi.Exceptions.MaybeNullException in project binnavi by google.

the class PostgreSQLAddressSpaceLoader method loadImageBases.

/**
 * Loads the image bases of the given modules within the given address space.
 *
 * The address space ID and the modules in the module list must all reference items that are
 * stored in the database connected to by connection argument.
 *
 * @param connection Connection to the database.
 * @param addressSpaceId ID of the address space.
 * @param list Modules whose image bases are loaded.
 *
 * @return A mapping of modules -> image bases for all modules in the address space.
 *
 * @throws CouldntLoadDataException Thrown if the image bases could not be loaded.
 */
private static Map<INaviModule, IAddress> loadImageBases(final CConnection connection, final int addressSpaceId, final List<INaviModule> list) throws CouldntLoadDataException {
    final HashMap<INaviModule, IAddress> imageBases = new HashMap<INaviModule, IAddress>();
    final String query = "SELECT module_id, image_base FROM " + CTableNames.SPACE_MODULES_TABLE + " WHERE address_space_id = " + addressSpaceId;
    try {
        final ResultSet resultSet = connection.executeQuery(query, true);
        try {
            while (resultSet.next()) {
                try {
                    final INaviModule module = findModule(list, resultSet.getInt("module_id"));
                    imageBases.put(module, PostgreSQLHelpers.loadAddress(resultSet, "image_base"));
                } catch (final MaybeNullException exception) {
                    // I can not think of a scenario where this can happen.
                    CUtilityFunctions.logException(exception);
                }
            }
        } finally {
            resultSet.close();
        }
        return imageBases;
    } catch (final SQLException exception) {
        throw new CouldntLoadDataException(exception);
    }
}
Also used : INaviModule(com.google.security.zynamics.binnavi.disassembly.INaviModule) MaybeNullException(com.google.security.zynamics.binnavi.Exceptions.MaybeNullException) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) ResultSet(java.sql.ResultSet) IAddress(com.google.security.zynamics.zylib.disassembly.IAddress)

Example 14 with MaybeNullException

use of com.google.security.zynamics.binnavi.Exceptions.MaybeNullException in project binnavi by google.

the class CResolveAllFunctionDialog method resolveAllFunctions.

/**
 * Takes the information from the GUI and forwards functions.
 */
private int resolveAllFunctions() {
    int counter = 0;
    for (final INaviModule currentModule : m_targetModules) {
        for (final INaviFunction currentFunction : currentModule.getContent().getFunctionContainer().getFunctions()) {
            final String originalName = currentFunction.getOriginalModulename();
            if (!originalName.equalsIgnoreCase(currentModule.getConfiguration().getName()) && !originalName.equalsIgnoreCase("")) {
                for (final INaviModule targetModule : m_sourceModules) {
                    final String targetModuleName = targetModule.getConfiguration().getName();
                    if (targetModuleName.toUpperCase().contains(originalName.toUpperCase()) && CFunctionHelpers.isForwardableFunction(currentFunction) && (currentFunction.getForwardedFunctionModuleId() == 0)) {
                        String currentFunctionName = currentFunction.getName();
                        if (currentFunctionName.startsWith("__imp_")) {
                            currentFunctionName = currentFunctionName.substring("__imp_".length());
                        }
                        try {
                            final INaviFunction targetFunction = targetModule.getContent().getFunctionContainer().getFunction(currentFunctionName);
                            currentFunction.setForwardedFunction(targetFunction);
                            ++counter;
                        } catch (final MaybeNullException exception) {
                        } catch (final CouldntSaveDataException exception) {
                            CUtilityFunctions.logException(exception);
                        }
                    }
                }
            }
        }
    }
    return counter;
}
Also used : INaviModule(com.google.security.zynamics.binnavi.disassembly.INaviModule) MaybeNullException(com.google.security.zynamics.binnavi.Exceptions.MaybeNullException) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) INaviFunction(com.google.security.zynamics.binnavi.disassembly.INaviFunction)

Example 15 with MaybeNullException

use of com.google.security.zynamics.binnavi.Exceptions.MaybeNullException in project binnavi by google.

the class CResolveFunctionDialog method resolveFunctions.

/**
 * Takes the information from the GUI and forwards functions.
 */
private void resolveFunctions() {
    final Object selectedParentString = m_parentBox.getSelectedItem();
    if (selectedParentString == null) {
        return;
    }
    final Object selectedModule = m_targetModuleBox.getSelectedItem();
    if (selectedModule == null) {
        return;
    }
    final INaviModule targetModule = ((CModuleWrapper) selectedModule).getObject();
    if (!targetModule.isLoaded()) {
        if (CMessageBox.showYesNoQuestion(this, "The target module must be loaded before functions can be forwarded.\n\n" + "Do you want to load the target module now?") == JOptionPane.NO_OPTION) {
            return;
        }
        CModuleLoader.loadModule(this, targetModule);
    }
    int counter = 0;
    final String parentString = selectedParentString.toString();
    for (final INaviFunction sourceFunction : m_module.getContent().getFunctionContainer().getFunctions()) {
        if (sourceFunction.getOriginalModulename().equalsIgnoreCase(parentString) && CFunctionHelpers.isForwardableFunction(sourceFunction)) {
            String sourceFunctionName = sourceFunction.getName();
            if (sourceFunctionName.startsWith("__imp_")) {
                sourceFunctionName = sourceFunctionName.substring("__imp_".length());
            }
            try {
                final INaviFunction targetFunction = targetModule.getContent().getFunctionContainer().getFunction(sourceFunctionName);
                sourceFunction.setForwardedFunction(targetFunction);
                ++counter;
            } catch (final MaybeNullException exception) {
            // There is no function in the target modules with the name of the source function.
            } catch (final CouldntSaveDataException exception) {
                CUtilityFunctions.logException(exception);
                final String message = "E00023: " + "Could not save function forwarding";
                final String description = CUtilityFunctions.createDescription(String.format("Could not forward the function '%s' from module '%s' to module '%s'", sourceFunction.getName(), m_module.getConfiguration().getName(), targetModule.getConfiguration().getName()), new String[] { "The database connection was dropped while saving." }, new String[] { "The changes in function forwarding were not saved. Try saving function " + "forwarding again. If necessary, close the connection to the database and " + "reconnect." });
                NaviErrorDialog.show(this, message, description, exception);
            }
        }
    }
    if (counter == 0) {
        CMessageBox.showInformation(this, "No functions suitable for forwarding were found.");
    } else {
        CMessageBox.showInformation(this, String.format("%d functions were forwarded from module '%s' to module '%s'", counter, m_module.getConfiguration().getName(), targetModule.getConfiguration().getName()));
    }
}
Also used : INaviModule(com.google.security.zynamics.binnavi.disassembly.INaviModule) MaybeNullException(com.google.security.zynamics.binnavi.Exceptions.MaybeNullException) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) INaviFunction(com.google.security.zynamics.binnavi.disassembly.INaviFunction)

Aggregations

MaybeNullException (com.google.security.zynamics.binnavi.Exceptions.MaybeNullException)42 INaviCodeNode (com.google.security.zynamics.binnavi.disassembly.INaviCodeNode)11 INaviFunction (com.google.security.zynamics.binnavi.disassembly.INaviFunction)11 TargetProcessThread (com.google.security.zynamics.binnavi.debug.models.processmanager.TargetProcessThread)10 CouldntSaveDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException)8 INaviInstruction (com.google.security.zynamics.binnavi.disassembly.INaviInstruction)7 SQLException (java.sql.SQLException)7 ProcessManager (com.google.security.zynamics.binnavi.debug.models.processmanager.ProcessManager)6 CallableStatement (java.sql.CallableStatement)6 ArrayList (java.util.ArrayList)5 CTag (com.google.security.zynamics.binnavi.Tagging.CTag)4 INaviFunctionNode (com.google.security.zynamics.binnavi.disassembly.INaviFunctionNode)4 INaviModule (com.google.security.zynamics.binnavi.disassembly.INaviModule)4 IAddress (com.google.security.zynamics.zylib.disassembly.IAddress)4 CouldntDeleteException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntDeleteException)3 CCodeNode (com.google.security.zynamics.binnavi.disassembly.CCodeNode)3 INaviViewNode (com.google.security.zynamics.binnavi.disassembly.INaviViewNode)3 CAddress (com.google.security.zynamics.zylib.disassembly.CAddress)3 CConnection (com.google.security.zynamics.binnavi.Database.CConnection)2 CouldntLoadDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException)2