Search in sources :

Example 16 with CConnection

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

the class PostgresSQLDebuggerFunctions method createDebuggerTemplate.

/**
   * Creates a new debugger template in the database.
   * 
   * @param provider SQL provider of the new debugger template.
   * @param name Name of the new debugger template. This argument must be non-empty.
   * @param host Host of the new debugger template. This argument must be non-empty.
   * @param port Port of the new debugger template. This argument must be a valid port number.
   * 
   * @return The new debugger template.
   * 
   * @throws CouldntSaveDataException Thrown if the new debugger template could not be written to
   *         the database.
   */
public static DebuggerTemplate createDebuggerTemplate(final AbstractSQLProvider provider, final String name, final String host, final int port) throws CouldntSaveDataException {
    Preconditions.checkNotNull(name, "IE00417: Debugger names can not be null");
    Preconditions.checkArgument(!name.isEmpty(), "IE00418: Debugger names can not be empty");
    Preconditions.checkNotNull(host, "IE00419: Debugger host can not be null");
    Preconditions.checkArgument(!host.isEmpty(), "IE00418: Debugger host can not be empty");
    Preconditions.checkArgument((port > 0) && (port <= 65535), "IE00421: Debugger port is out of bounds");
    NaviLogger.info("Creating new debugger %s (%s:%d)", name, host, port);
    final CConnection connection = provider.getConnection();
    final String query = "INSERT INTO " + CTableNames.DEBUGGERS_TABLE + "(name, host, port) VALUES(?, ?, ?) RETURNING id";
    try (PreparedStatement statement = connection.getConnection().prepareStatement(query)) {
        statement.setString(1, name);
        statement.setString(2, host);
        statement.setInt(3, port);
        int id = -1;
        try (ResultSet resultSet = statement.executeQuery()) {
            while (resultSet.next()) {
                id = resultSet.getInt("id");
            }
        }
        return new DebuggerTemplate(id, name, host, port, provider);
    } catch (final SQLException e) {
        throw new CouldntSaveDataException(e);
    }
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) DebuggerTemplate(com.google.security.zynamics.binnavi.debug.debugger.DebuggerTemplate) SQLException(java.sql.SQLException) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 17 with CConnection

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

the class PostgreSQLDataFunctions method saveData.

/**
   * Saves the data of a module to the database.
   * 
   * @param provider Provides the connection to the database.
   * @param module The module whose data is stored in the database.
   * @param data The data of the module to store in the database.
   * 
   * @throws CouldntSaveDataException Thrown if the module data could not be stored.
   */
public static void saveData(final AbstractSQLProvider provider, final INaviModule module, final byte[] data) throws CouldntSaveDataException {
    Preconditions.checkNotNull(provider, "IE01267: Provider argument can not be null");
    Preconditions.checkNotNull(module, "IE01268: Module argument can not be null");
    Preconditions.checkNotNull(data, "IE01269: Data argument can not be null");
    final CConnection connection = provider.getConnection();
    try {
        connection.executeUpdate("DELETE FROM " + CTableNames.DATA_PARTS_TABLE + " WHERE module_id = " + module.getConfiguration().getId(), true);
    } catch (final SQLException exception) {
        throw new CouldntSaveDataException(exception);
    }
    final String preparedStatement = "INSERT INTO " + CTableNames.DATA_PARTS_TABLE + "(module_id, part_id, data) VALUES(?, ?, ?)";
    try (PreparedStatement statement = provider.getConnection().getConnection().prepareStatement(preparedStatement)) {
        statement.setInt(1, module.getConfiguration().getId());
        statement.setInt(2, 0);
        statement.setBinaryStream(3, new ByteArrayInputStream(data, 0, data.length), data.length);
        statement.execute();
    } catch (final SQLException exception) {
        throw new CouldntSaveDataException(exception);
    }
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) PreparedStatement(java.sql.PreparedStatement)

Example 18 with CConnection

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

the class PostgreSQLDatabaseFunctions method loadProjects.

/**
   * Loads the projects of a database.
   * 
   * @param provider The SQL provider that provides the connection.
   * @param debuggerManager Debugger manager object that belongs to the given database.
   * 
   * @return A list of projects that contains the projects stored in the database.
   * 
   * @throws CouldntLoadDataException Thrown if the projects could not be loaded from the database.
   */
public static List<INaviProject> loadProjects(final AbstractSQLProvider provider, final DebuggerTemplateManager debuggerManager) throws CouldntLoadDataException {
    PostgreSQLDatabaseFunctions.checkArguments(provider, debuggerManager);
    final CConnection connection = provider.getConnection();
    final List<INaviProject> projects = new ArrayList<>();
    if (!PostgreSQLHelpers.hasTable(connection, CTableNames.PROJECTS_TABLE)) {
        return projects;
    }
    String query = "SELECT id, name, description, creation_date, modification_date, " + " (SELECT count(*) FROM " + CTableNames.ADDRESS_SPACES_TABLE + " WHERE project_id = " + CTableNames.PROJECTS_TABLE + ".id) " + " AS addressspace_count FROM " + CTableNames.PROJECTS_TABLE;
    try (ResultSet resultSet = connection.executeQuery(query, true)) {
        while (resultSet.next()) {
            final int projectId = resultSet.getInt("id");
            final String name = PostgreSQLHelpers.readString(resultSet, "name");
            final String description = PostgreSQLHelpers.readString(resultSet, "description");
            final int addressSpaceCount = resultSet.getInt("addressspace_count");
            final Timestamp creationDate = resultSet.getTimestamp("creation_date");
            final Timestamp modificationDate = resultSet.getTimestamp("modification_date");
            final List<DebuggerTemplate> debuggers = PostgreSQLDatabaseFunctions.getAssignedDebuggers(connection, projectId, debuggerManager);
            projects.add(new CProject(projectId, name, description == null ? "" : description, creationDate, modificationDate, addressSpaceCount, debuggers, provider));
        }
    } catch (final SQLException e) {
        throw new CouldntLoadDataException(e);
    }
    return new ArrayList<INaviProject>(projects);
}
Also used : CProject(com.google.security.zynamics.binnavi.disassembly.CProject) DebuggerTemplate(com.google.security.zynamics.binnavi.debug.debugger.DebuggerTemplate) SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) ArrayList(java.util.ArrayList) Timestamp(java.sql.Timestamp) CConnection(com.google.security.zynamics.binnavi.Database.CConnection) INaviProject(com.google.security.zynamics.binnavi.disassembly.INaviProject) ResultSet(java.sql.ResultSet)

Example 19 with CConnection

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

the class PostgreSQLDatabaseFunctions method loadRawModules.

/**
   * Loads the raw modules of a database.
   * 
   * @param provider The SQL provider that provides the connection.
   * 
   * @return A list of raw modules that contains the raw modules stored in the database.
   * 
   * @throws CouldntLoadDataException Thrown if the raw modules could not be loaded from the
   *         database.
   */
public static final List<INaviRawModule> loadRawModules(final AbstractSQLProvider provider) throws CouldntLoadDataException {
    Preconditions.checkNotNull(provider, "IE00416: Provider argument can not be null");
    final CConnection connection = provider.getConnection();
    final List<INaviRawModule> modules = new ArrayList<INaviRawModule>();
    if (!PostgreSQLHelpers.hasTable(connection, CTableNames.RAW_MODULES_TABLE)) {
        return modules;
    }
    final String query = "SELECT id, name FROM " + CTableNames.RAW_MODULES_TABLE + " ORDER BY id";
    try (ResultSet resultSet = connection.executeQuery(query, true)) {
        while (resultSet.next()) {
            final int rawModuleId = resultSet.getInt("id");
            final String name = PostgreSQLHelpers.readString(resultSet, "name");
            final boolean isComplete = PostgreSQLDatabaseFunctions.checkRawModulesTables(provider.getConnection(), PostgreSQLHelpers.getDatabaseName(provider.getConnection()), rawModuleId);
            final int functionCount = isComplete ? PostgreSQLDatabaseFunctions.getRawModuleFunctionCount(connection, rawModuleId) : 0;
            final CRawModule module = new CRawModule(rawModuleId, name, functionCount, isComplete, provider);
            modules.add(module);
        }
    } catch (final SQLException e) {
        throw new CouldntLoadDataException(e);
    }
    return modules;
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) INaviRawModule(com.google.security.zynamics.binnavi.disassembly.INaviRawModule) SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) CRawModule(com.google.security.zynamics.binnavi.disassembly.CRawModule)

Example 20 with CConnection

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

the class PostgreSQLDatabaseFunctions method loadModules.

/**
   * Loads the modules of a database.
   * 
   * @param provider The SQL provider that provides the connection.
   * @param rawModules Previously loaded raw module objects.
   * @param debuggerManager Debugger manager object that belongs to the given database.
   * 
   * @return A list of modules that contains the modules stored in the database.
   * 
   * @throws CouldntLoadDataException Thrown if the modules could not be loaded from the database.
   */
public static List<INaviModule> loadModules(final AbstractSQLProvider provider, final List<INaviRawModule> rawModules, final DebuggerTemplateManager debuggerManager) throws CouldntLoadDataException {
    Preconditions.checkNotNull(rawModules, "IE02043: rawModules argument can not be null");
    PostgreSQLDatabaseFunctions.checkArguments(provider, debuggerManager);
    final List<CModule> modules = new ArrayList<>();
    final CConnection connection = provider.getConnection();
    if (!PostgreSQLHelpers.hasTable(connection, CTableNames.MODULES_TABLE)) {
        return new ArrayList<INaviModule>(modules);
    }
    final String query = "SELECT id, raw_module_id, " + CTableNames.MODULES_TABLE + ".name, " + " md5, sha1, description, import_time, modification_date, file_base, image_base, stared, " + " initialization_state, debugger_id, " + " (SELECT count(*) FROM " + CTableNames.FUNCTIONS_TABLE + " " + " WHERE id = " + CTableNames.FUNCTIONS_TABLE + ".module_id) " + " AS function_count, " + " (SELECT count(*) FROM " + CTableNames.MODULE_VIEWS_TABLE + " JOIN " + CTableNames.VIEWS_TABLE + " ON view_id = id " + " WHERE type = 'non-native' and module_id = " + CTableNames.MODULES_TABLE + ".id) " + " AS view_count FROM " + CTableNames.MODULES_TABLE + " " + " WHERE raw_module_id IS NOT NULL ORDER BY id";
    try (ResultSet resultSet = connection.executeQuery(query, true)) {
        while (resultSet.next()) {
            final int moduleId = resultSet.getInt("id");
            final String name = PostgreSQLHelpers.readString(resultSet, "name");
            final String md5 = PostgreSQLHelpers.readString(resultSet, "md5");
            final String sha1 = PostgreSQLHelpers.readString(resultSet, "sha1");
            final String comment = PostgreSQLHelpers.readString(resultSet, "description");
            final Timestamp timestamp = resultSet.getTimestamp("import_time");
            final Timestamp modificationDate = resultSet.getTimestamp("modification_date");
            int functionCount = resultSet.getInt("function_count");
            final int viewCount = resultSet.getInt("view_count");
            final IAddress imageBase = PostgreSQLHelpers.loadAddress(resultSet, "image_base");
            final IAddress fileBase = PostgreSQLHelpers.loadAddress(resultSet, "file_base");
            final int debuggerId = resultSet.getInt("debugger_id");
            final boolean isStared = resultSet.getBoolean("stared");
            final int initializationState = resultSet.getInt("initialization_state");
            final DebuggerTemplate description = debuggerManager.findDebugger(debuggerId);
            final int rawModuleId = resultSet.getInt("raw_module_id");
            final INaviRawModule rawModule = PostgreSQLDatabaseFunctions.findRawModule(rawModuleId, rawModules);
            if ((functionCount == 0) && (rawModule != null)) {
                functionCount = rawModule.getFunctionCount();
            }
            modules.add(new CModule(moduleId, name, comment, timestamp, modificationDate, md5, sha1, functionCount, viewCount, fileBase, imageBase, description, rawModule, initializationState, isStared, provider));
        }
    } catch (final SQLException e) {
        throw new CouldntLoadDataException(e);
    }
    return new ArrayList<INaviModule>(modules);
}
Also used : DebuggerTemplate(com.google.security.zynamics.binnavi.debug.debugger.DebuggerTemplate) SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) ArrayList(java.util.ArrayList) Timestamp(java.sql.Timestamp) IAddress(com.google.security.zynamics.zylib.disassembly.IAddress) CConnection(com.google.security.zynamics.binnavi.Database.CConnection) INaviRawModule(com.google.security.zynamics.binnavi.disassembly.INaviRawModule) ResultSet(java.sql.ResultSet) CModule(com.google.security.zynamics.binnavi.disassembly.Modules.CModule)

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