use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException in project binnavi by google.
the class PostgresSQLDebuggerFunctions method setName.
/**
* Changes the name of an existing debugger template.
*
* The debugger must be stored in the database the provider argument is connected to.
*
* @param provider The connection to the database.
* @param debugger The debugger whose name value is changed.
* @param name The new name value of the debugger template.
*
* @throws CouldntSaveDataException Thrown if the name value could not be updated.
*/
public static void setName(final AbstractSQLProvider provider, final DebuggerTemplate debugger, final String name) throws CouldntSaveDataException {
Preconditions.checkNotNull(debugger, "IE00425: Debugger argument can not be null");
Preconditions.checkNotNull(name, "IE00426: Name argument can not be null");
Preconditions.checkArgument(debugger.inSameDatabase(provider), "IE00427: Debugger is not part of this database");
final String query = "UPDATE " + CTableNames.DEBUGGERS_TABLE + " SET name = ? WHERE id = ?";
try (PreparedStatement statement = provider.getConnection().getConnection().prepareStatement(query)) {
statement.setString(1, name);
statement.setInt(2, debugger.getId());
statement.executeUpdate();
} catch (final SQLException e) {
throw new CouldntSaveDataException(e);
}
}
use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException in project binnavi by google.
the class PostgresSQLDebuggerFunctions method setPort.
/**
* Changes the port of an existing debugger template.
*
* The debugger must be stored in the database the provider argument is connected to.
*
* @param provider The connection to the database.
* @param debugger The debugger whose port value is changed.
* @param port The new port value of the debugger template. This argument must be a valid port
* number.
*
* @throws CouldntSaveDataException Thrown if the port value could not be updated.
*/
public static void setPort(final AbstractSQLProvider provider, final DebuggerTemplate debugger, final int port) throws CouldntSaveDataException {
Preconditions.checkNotNull(debugger, "IE00428: Debugger argument can not be null");
Preconditions.checkArgument(NetHelpers.isValidPort(port), "IE00429: Invalid port argument");
Preconditions.checkArgument(debugger.inSameDatabase(provider), "IE00430: Debugger is not part of this database");
final String query = "UPDATE " + CTableNames.DEBUGGERS_TABLE + " SET port = ? WHERE id = ?";
try (PreparedStatement statement = provider.getConnection().getConnection().prepareStatement(query)) {
statement.setInt(1, port);
statement.setInt(2, debugger.getId());
statement.executeUpdate();
} catch (final SQLException e) {
throw new CouldntSaveDataException(e);
}
}
use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException 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++;
}
}
}
use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException 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());
}
use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException in project binnavi by google.
the class PostgreSQLNodeFunctions method appendGlobalCodeNodeComment.
/**
* Appends a comment as global code node comment to a code node.
*
* @param provider The SQL provider used to access the database.
* @param codeNode The code node to which the global 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 Thrown if the global code node comment could not be saved to
* the database.
*/
public static int appendGlobalCodeNodeComment(final SQLProvider provider, final INaviCodeNode codeNode, final String commentText, final Integer userId) throws CouldntSaveDataException {
Preconditions.checkNotNull(provider, "IE02445: provider argument can not be null");
Preconditions.checkNotNull(codeNode, "IE02446: codeNode argument can not be null");
Preconditions.checkNotNull(commentText, "IE02447: commentText argument can not be null");
Preconditions.checkNotNull(userId, "IE02448: userId argument can not be null");
final Connection connection = provider.getConnection().getConnection();
Integer moduleId = null;
final int nodeId = codeNode.getId();
final BigInteger nodeAddress = codeNode.getAddress().toBigInteger();
try {
moduleId = codeNode.getParentFunction().getModule().getConfiguration().getId();
} catch (final MaybeNullException exception) {
throw new CouldntSaveDataException("Error: Can not append global code node comments for nodes without a parent function");
}
final String function = "{ ? = call append_global_code_node_comment( ?, ?, ?, ?, ?) }";
try {
final CallableStatement appendCommentFunction = connection.prepareCall(function);
try {
appendCommentFunction.registerOutParameter(1, Types.INTEGER);
appendCommentFunction.setInt(2, moduleId);
appendCommentFunction.setInt(3, nodeId);
appendCommentFunction.setObject(4, nodeAddress, java.sql.Types.BIGINT);
appendCommentFunction.setInt(5, userId);
appendCommentFunction.setString(6, 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);
}
}
Aggregations