Search in sources :

Example 6 with CConnection

use of com.google.security.zynamics.binnavi.Database.CConnection in project binnavi by google.

the class PostgreSQLModuleFunctions method setImageBase.

/**
   * Changes the image base of a module.
   *
   * The module must be stored in the database connected to by the provider argument.
   *
   * @param provider The SQL provider that provides the connection.
   * @param module The module whose image base is changed.
   * @param address The new image base of the module.
   *
   * @throws CouldntSaveDataException Thrown if the image base of the module could not be changed.
   */
public static void setImageBase(final AbstractSQLProvider provider, final INaviModule module, final IAddress address) throws CouldntSaveDataException {
    checkArguments(provider, module);
    Preconditions.checkNotNull(address, "IE00495: Address argument can not be null");
    final CConnection connection = provider.getConnection();
    try {
        final String query = String.format("UPDATE %s SET image_base = %s " + " WHERE id = %d", CTableNames.MODULES_TABLE, address.toBigInteger().toString(), module.getConfiguration().getId());
        connection.executeUpdate(query, true);
    } catch (final SQLException e) {
        throw new CouldntSaveDataException(e);
    }
    PostgreSQLHelpers.updateModificationDate(connection, CTableNames.MODULES_TABLE, module.getConfiguration().getId());
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) SQLException(java.sql.SQLException) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException)

Example 7 with CConnection

use of com.google.security.zynamics.binnavi.Database.CConnection in project binnavi by google.

the class PostgreSQLProjectFunctions method createAddressSpace.

/**
   * Creates an address space in a project.
   *
   * The project must be stored in the database connected to by the provider argument.
   *
   * @param provider The SQL provider that provides the connection.
   * @param project The project where the address space is created.
   * @param name The name of the created address space.
   *
   * @return The created address space.
   *
   * @throws CouldntSaveDataException Thrown if the address space could not be created.
   */
public static CAddressSpace createAddressSpace(final AbstractSQLProvider provider, final INaviProject project, final String name) throws CouldntSaveDataException {
    checkArguments(provider, project);
    Preconditions.checkNotNull(name, "IE00521: Address space names can not be null");
    Preconditions.checkArgument(!("".equals(name)), "IE00522: Address space names can not be empty");
    final CConnection connection = provider.getConnection();
    final int projectId = project.getConfiguration().getId();
    NaviLogger.info("Creating a new address space with name %s in project %s (%d)", name, project.getConfiguration().getName(), projectId);
    try {
        // The new address space gets the current date as creation and
        // modification dates.
        final String query = "insert into " + CTableNames.ADDRESS_SPACES_TABLE + "(project_id, name, description, creation_date, modification_date) values(?, ?, '', NOW(), NOW()) returning id";
        final PreparedStatement statement = connection.getConnection().prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        try {
            statement.setInt(1, projectId);
            statement.setString(2, name);
            Integer addressSpaceId = null;
            final ResultSet resultSet = statement.executeQuery();
            try {
                while (resultSet.next()) {
                    if (resultSet.isFirst()) {
                        addressSpaceId = resultSet.getInt(1);
                        break;
                    }
                }
            } finally {
                resultSet.close();
            }
            Preconditions.checkNotNull(addressSpaceId, "IE02130: Error address space id may not be null after project creation");
            return PostgreSQLProjectFunctions.readAddressSpace(provider, addressSpaceId, project);
        } finally {
            statement.close();
        }
    } catch (final SQLException e) {
        throw new CouldntSaveDataException(e);
    }
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) SQLException(java.sql.SQLException) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 8 with CConnection

use of com.google.security.zynamics.binnavi.Database.CConnection in project binnavi by google.

the class PostgreSQLRawModuleFunctions method deleteRawModule.

/**
   * Deletes a raw module from the database. The raw module must be stored in the database connected
   * to by the provider argument.
   * 
   * @param provider The SQL provider that provides the connection.
   * @param module The raw module to delete.
   * @throws CouldntDeleteException Thrown if the raw module could not be deleted.
   */
public static void deleteRawModule(final AbstractSQLProvider provider, final INaviRawModule module) throws CouldntDeleteException {
    Preconditions.checkNotNull(provider, "IE00529: Provider argument can not be null");
    Preconditions.checkNotNull(module, "IE00530: Raw module can not be null");
    Preconditions.checkArgument(module.inSameDatabase(provider), "IE00531: Raw module is not part of this database");
    final CConnection connection = provider.getConnection();
    NaviLogger.info("Deleting raw module %s", module.getName());
    final int moduleId = module.getId();
    try {
        PostgreSQLHelpers.deleteTable(connection, String.format(CTableNames.RAW_ADDRESS_COMMENTS_TABLE, moduleId));
        PostgreSQLHelpers.deleteTable(connection, String.format(CTableNames.RAW_ADDRESS_REFERENCES_TABLE, moduleId));
        PostgreSQLHelpers.deleteTable(connection, String.format(CTableNames.RAW_BASIC_BLOCK_INSTRUCTIONS_TABLE, moduleId));
        PostgreSQLHelpers.deleteTable(connection, String.format(CTableNames.RAW_BASIC_BLOCKS_TABLE, moduleId));
        PostgreSQLHelpers.deleteTable(connection, String.format(CTableNames.RAW_CALLGRAPH_TABLE, moduleId));
        PostgreSQLHelpers.deleteTable(connection, String.format(CTableNames.RAW_CONTROL_FLOW_GRAPHS_TABLE, moduleId));
        PostgreSQLHelpers.deleteTable(connection, String.format(CTableNames.RAW_EXPRESSION_NODES_TABLE, moduleId));
        PostgreSQLHelpers.deleteTable(connection, String.format(CTableNames.RAW_EXPRESSION_SUBSTITUTIONS_TABLE, moduleId));
        PostgreSQLHelpers.deleteTable(connection, String.format(CTableNames.RAW_EXPRESSION_TREE_NODES_TABLE, moduleId));
        PostgreSQLHelpers.deleteTable(connection, String.format(CTableNames.RAW_EXPRESSION_TREES_TABLE, moduleId));
        PostgreSQLHelpers.deleteTable(connection, String.format(CTableNames.RAW_FUNCTIONS_TABLE, moduleId));
        PostgreSQLHelpers.deleteTable(connection, String.format(CTableNames.RAW_INSTRUCTIONS_TABLE, moduleId));
        PostgreSQLHelpers.deleteTable(connection, String.format(CTableNames.RAW_OPERANDS_TABLE, moduleId));
        PostgreSQLHelpers.deleteTable(connection, String.format(CTableNames.RAW_SECTIONS, moduleId));
        PostgreSQLHelpers.deleteTable(connection, String.format(CTableNames.RAW_TYPE_INSTACES, moduleId));
        PostgreSQLHelpers.deleteTable(connection, String.format(CTableNames.RAW_TYPES, moduleId));
        PostgreSQLHelpers.deleteTable(connection, String.format(CTableNames.RAW_BASE_TYPES, moduleId));
        PostgreSQLHelpers.deleteTable(connection, String.format(CTableNames.RAW_EXPRESSION_TYPES_TABLE, moduleId));
        PostgreSQLHelpers.deleteTable(connection, String.format(CTableNames.RAW_EXPRESSION_TYPE_INSTANCES, moduleId));
        PostgreSQLHelpers.deleteById(connection, CTableNames.RAW_MODULES_TABLE, moduleId);
    } catch (final SQLException e) {
        // Log this silently because failure at this point is not very
        // important
        CUtilityFunctions.logException(e);
    }
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) SQLException(java.sql.SQLException)

Example 9 with CConnection

use of com.google.security.zynamics.binnavi.Database.CConnection in project binnavi by google.

the class PostgreSQLTypeFunctions method appendTypeInstanceComment.

/**
   * This function appends a comment to the list of type instance comments associated to this type
   * instance.
   *
   * @param provider The provider to access the database.
   * @param moduleId The id of the module to witch the type instance is associated to.
   * @param instanceId The if of the type instance to which the comment is associated to.
   * @param commentText The comment text of the comment.
   * @param userId The id of the currently active user.
   *
   * @return The id of the comment generated by the database.
   *
   * @throws CouldntSaveDataException if the comment could not be stored in the database.
   */
public static Integer appendTypeInstanceComment(final SQLProvider provider, final int moduleId, final int instanceId, final String commentText, final Integer userId) throws CouldntSaveDataException {
    Preconditions.checkArgument(moduleId > 0, "Error: module id must be greater then zero");
    Preconditions.checkArgument(instanceId >= 0, "instance id must be greater or equal to zero");
    Preconditions.checkNotNull(commentText, "Error: comment text argument can not be null");
    Preconditions.checkNotNull(userId, "Error: user id argument can not be null");
    final CConnection connection = provider.getConnection();
    final String function = " { ? = call append_type_instance_comment(?, ?, ?, ?) } ";
    try {
        final CallableStatement appendCommentFunction = connection.getConnection().prepareCall(function);
        try {
            appendCommentFunction.registerOutParameter(1, Types.INTEGER);
            appendCommentFunction.setInt(2, moduleId);
            appendCommentFunction.setInt(3, instanceId);
            appendCommentFunction.setInt(4, userId);
            appendCommentFunction.setString(5, commentText);
            appendCommentFunction.execute();
            final int commentId = appendCommentFunction.getInt(1);
            if (appendCommentFunction.wasNull()) {
                throw new CouldntSaveDataException("Error: Got an comment id of null from the database");
            }
            return commentId;
        } finally {
            appendCommentFunction.close();
        }
    } catch (final SQLException exception) {
        throw new CouldntSaveDataException(exception);
    }
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) SQLException(java.sql.SQLException) CallableStatement(java.sql.CallableStatement) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException)

Example 10 with CConnection

use of com.google.security.zynamics.binnavi.Database.CConnection in project binnavi by google.

the class PostgreSQLFunctionFunctions method setDescription.

/**
   * Changes the description of a function.
   *
   * The function must be stored in the database connected to by the provider argument.
   *
   * @param provider The SQL provider that provides the connection.
   * @param function The function whose description is changed.
   * @param description The new description of the function.
   *
   * @throws CouldntSaveDataException Thrown if the new description could not be saved to the
   *         database.
   */
public static void setDescription(final AbstractSQLProvider provider, final INaviFunction function, final String description) throws CouldntSaveDataException {
    Preconditions.checkNotNull(provider, "IE00448: Provider argument can not be null");
    Preconditions.checkNotNull(function, "IE00449: Function argument can not be null");
    Preconditions.checkNotNull(description, "IE00450: Comment argument can not be null");
    Preconditions.checkArgument(function.inSameDatabase(provider), "IE00451: Function is not part of this database");
    final CConnection connection = provider.getConnection();
    final int module = function.getModule().getConfiguration().getId();
    final IAddress address = function.getAddress();
    final String query = "UPDATE " + CTableNames.FUNCTIONS_TABLE + " SET description = ? WHERE module_id = ? AND address = ?";
    try (PreparedStatement statement = connection.getConnection().prepareStatement(query)) {
        statement.setString(1, description);
        statement.setInt(2, module);
        statement.setObject(3, address.toBigInteger(), Types.BIGINT);
        statement.executeUpdate();
    } catch (final SQLException exception) {
        throw new CouldntSaveDataException(exception);
    }
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) SQLException(java.sql.SQLException) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) PreparedStatement(java.sql.PreparedStatement) IAddress(com.google.security.zynamics.zylib.disassembly.IAddress)

Aggregations

CConnection (com.google.security.zynamics.binnavi.Database.CConnection)70 SQLException (java.sql.SQLException)59 CouldntSaveDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException)31 PreparedStatement (java.sql.PreparedStatement)25 ResultSet (java.sql.ResultSet)25 CouldntLoadDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException)20 ArrayList (java.util.ArrayList)15 CouldntDeleteException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntDeleteException)7 CTag (com.google.security.zynamics.binnavi.Tagging.CTag)6 IAddress (com.google.security.zynamics.zylib.disassembly.IAddress)6 Timestamp (java.sql.Timestamp)6 HashMap (java.util.HashMap)6 Set (java.util.Set)6 DebuggerTemplate (com.google.security.zynamics.binnavi.debug.debugger.DebuggerTemplate)5 AbstractSQLProvider (com.google.security.zynamics.binnavi.Database.AbstractSQLProvider)4 INaviEdge (com.google.security.zynamics.binnavi.disassembly.INaviEdge)4 INaviViewNode (com.google.security.zynamics.binnavi.disassembly.INaviViewNode)4 ExpensiveBaseTest (com.google.security.zynamics.binnavi.disassembly.types.ExpensiveBaseTest)4 CallableStatement (java.sql.CallableStatement)4 Test (org.junit.Test)4