use of com.google.security.zynamics.binnavi.disassembly.INaviCodeNode in project binnavi by google.
the class PostgreSQLNodeSaver method saveCodeNodes.
/**
* Saves the code nodes to the database.
*
* @param provider The connection to the database.
* @param nodes The nodes to save.
* @param firstNode The database index of the first node.
* @param codeNodeIndices Index into the nodes list that identifies the code nodes.
*
* @throws SQLException Thrown if saving the code node instructions failed.
*/
protected static void saveCodeNodes(final SQLProvider provider, final List<INaviViewNode> nodes, final int firstNode, final List<Integer> codeNodeIndices) throws SQLException {
if (!codeNodeIndices.isEmpty()) {
final List<Pair<INaviCodeNode, INaviInstruction>> instructionsWithUnsavedLocalComments = PostgreSQLNodeSaver.saveCodeNodeInstructions(provider, nodes, firstNode, codeNodeIndices);
final String query = "INSERT INTO " + CTableNames.CODE_NODES_TABLE + "(module_id, node_id, parent_function, comment_id) VALUES (?, ?, ?, ?)";
final ArrayList<INaviCodeNode> codeNodesWithUnsavedComments = new ArrayList<INaviCodeNode>();
final PreparedStatement preparedStatement = provider.getConnection().getConnection().prepareStatement(query);
try {
for (final int index : codeNodeIndices) {
final INaviCodeNode codeNode = (INaviCodeNode) nodes.get(index);
codeNode.setId(firstNode + index);
INaviFunction function = null;
try {
function = codeNode.getParentFunction();
} catch (final MaybeNullException e) {
}
final int moduleId = Iterables.getLast(codeNode.getInstructions()).getModule().getConfiguration().getId();
final List<IComment> comment = codeNode.getComments().getLocalCodeNodeComment();
final Integer commentId = comment == null ? null : comment.size() == 0 ? null : Iterables.getLast(comment).getId();
if ((comment != null) && (comment.size() != 0) && (commentId == null)) {
codeNodesWithUnsavedComments.add(codeNode);
}
preparedStatement.setInt(1, moduleId);
preparedStatement.setInt(2, firstNode + index);
if (function == null) {
preparedStatement.setNull(3, Types.BIGINT);
} else {
preparedStatement.setObject(3, function.getAddress().toBigInteger(), Types.BIGINT);
}
if (commentId == null) {
preparedStatement.setNull(4, Types.INTEGER);
} else {
preparedStatement.setInt(4, commentId);
}
preparedStatement.addBatch();
}
preparedStatement.executeBatch();
} finally {
preparedStatement.close();
}
// implementation.
for (final INaviCodeNode codeNode : codeNodesWithUnsavedComments) {
final ArrayList<IComment> codeNodecomments = new ArrayList<IComment>();
for (final IComment comment : codeNode.getComments().getLocalCodeNodeComment()) {
try {
final Integer commentId = PostgreSQLNodeFunctions.appendLocalCodeNodeComment(provider, codeNode, comment.getComment(), comment.getUser().getUserId());
final IComment newComment = new CComment(commentId, comment.getUser(), comment.getParent(), comment.getComment());
codeNodecomments.add(newComment);
} catch (final CouldntSaveDataException exception) {
CUtilityFunctions.logException(exception);
}
}
codeNode.getComments().initializeLocalCodeNodeComment(codeNodecomments);
}
// implementation.
for (final Pair<INaviCodeNode, INaviInstruction> pair : instructionsWithUnsavedLocalComments) {
final ArrayList<IComment> localInstructionComments = new ArrayList<IComment>();
for (final IComment comment : pair.first().getComments().getLocalInstructionComment(pair.second())) {
try {
final int commentId = PostgreSQLInstructionFunctions.appendLocalInstructionComment(provider, pair.first(), pair.second(), comment.getComment(), comment.getUser().getUserId());
final IComment newComment = new CComment(commentId, comment.getUser(), comment.getParent(), comment.getComment());
localInstructionComments.add(newComment);
} catch (final CouldntSaveDataException exception) {
CUtilityFunctions.logException(exception);
}
}
pair.first().getComments().initializeLocalInstructionComment(pair.second(), localInstructionComments);
}
}
}
use of com.google.security.zynamics.binnavi.disassembly.INaviCodeNode in project binnavi by google.
the class PostgreSQLNodeSaver method saveCodeNodeInstructions.
/**
* Saves the mapping between code nodes and their instructions to the database.
*
* @param provider The provider used to access the database.
* @param nodes The nodes to save.
* @param firstNode The database index of the first node.
* @param codeNodeIndices Index into the nodes list that identifies the code nodes.
*
* @throws SQLException Thrown if saving the code node instructions failed.
*/
protected static ArrayList<Pair<INaviCodeNode, INaviInstruction>> saveCodeNodeInstructions(final SQLProvider provider, final List<INaviViewNode> nodes, final int firstNode, final List<Integer> codeNodeIndices) throws SQLException {
if (!nodes.isEmpty()) {
final Set<INaviInstruction> unsavedInstructions = new HashSet<INaviInstruction>();
for (final int index : codeNodeIndices) {
final CCodeNode node = (CCodeNode) nodes.get(index);
final Iterable<INaviInstruction> instructions = node.getInstructions();
for (final INaviInstruction instruction : instructions) {
if (!(instruction.isStored())) {
unsavedInstructions.add(instruction);
}
}
}
PostgreSQLInstructionFunctions.createInstructions(provider, unsavedInstructions);
final String query = "INSERT INTO " + CTableNames.CODENODE_INSTRUCTIONS_TABLE + " (module_id, node_id, position, address, comment_id) VALUES (?, ?, ?, ?, ?)";
final PreparedStatement preparedStatement = provider.getConnection().getConnection().prepareStatement(query);
final ArrayList<Pair<INaviCodeNode, INaviInstruction>> instructionsWithUnsavedLocalComments = new ArrayList<Pair<INaviCodeNode, INaviInstruction>>();
try {
for (final Integer index : codeNodeIndices) {
final INaviCodeNode codeNode = (INaviCodeNode) nodes.get(index);
int position = 0;
for (final INaviInstruction instruction : codeNode.getInstructions()) {
final List<IComment> comments = codeNode.getComments().getLocalInstructionComment(instruction);
final Integer commentId = comments == null ? null : comments.size() == 0 ? null : Iterables.getLast(comments).getId();
if ((comments != null) && (comments.size() != 0) && (commentId == null)) {
instructionsWithUnsavedLocalComments.add(new Pair<INaviCodeNode, INaviInstruction>(codeNode, instruction));
}
final int moduleId = instruction.getModule().getConfiguration().getId();
preparedStatement.setInt(1, moduleId);
preparedStatement.setInt(2, firstNode + index);
preparedStatement.setInt(3, position);
preparedStatement.setObject(4, instruction.getAddress().toBigInteger(), Types.BIGINT);
if (commentId == null) {
preparedStatement.setNull(5, Types.INTEGER);
} else {
preparedStatement.setInt(5, commentId);
}
position++;
preparedStatement.addBatch();
}
}
preparedStatement.executeBatch();
} finally {
preparedStatement.close();
}
return instructionsWithUnsavedLocalComments;
}
return null;
}
use of com.google.security.zynamics.binnavi.disassembly.INaviCodeNode in project binnavi by google.
the class PostgreSQLCommentNotificationParser method processNodeLocalNodeCommentNotification.
/**
* Parses the {@link PGNotification} notifications from the PostgreSQL database back end for local
* 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
* gathered 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 processNodeLocalNodeCommentNotification(final PGNotification notification, final SQLProvider provider) {
final Matcher matcher = NODE_LOCAL_PATTERN.matcher(notification.getParameter());
if (!matcher.find()) {
return null;
}
final Integer moduleId = Integer.parseInt(matcher.group(3));
final Integer nodeId = Integer.parseInt(matcher.group(4));
final Integer commentId = matcher.group(6).equals("null") ? null : Integer.parseInt(matcher.group(6));
final INaviModule module = provider.findModule(moduleId);
if (!module.isLoaded()) {
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 CodeNodeCommentNotificationContainer(codeNode, operation, CommentScope.LOCAL, commentId);
}
use of com.google.security.zynamics.binnavi.disassembly.INaviCodeNode in project binnavi by google.
the class PostgreSQLNotificationParserTest method testGlobalCodeNodeCommentParsingAppendFirstComment.
@Test
public void testGlobalCodeNodeCommentParsingAppendFirstComment() {
new MockModule(provider, Lists.newArrayList(mockView), Lists.newArrayList(mockFunction));
MockPGNotification notification = new MockPGNotification("comment_changes", "bn_global_node_comments INSERT 1 4608 3333");
final Collection<CommentNotification> result = PostgreSQLCommentNotificationParser.processNodeGlobalCommentNotification(notification, provider);
assertEquals(1, result.size());
final CodeNodeCommentNotificationContainer container = (CodeNodeCommentNotificationContainer) Iterables.getFirst(result, null);
final INaviCodeNode node = container.getNode();
assertEquals(1111, node.getId());
assertEquals(new CAddress(4608), node.getAddress());
assertEquals(CommentOperation.APPEND, container.getOperation());
assertEquals(new Integer(3333), container.getCommentId());
assertEquals(CommentScope.GLOBAL, container.getScope());
}
use of com.google.security.zynamics.binnavi.disassembly.INaviCodeNode in project binnavi by google.
the class PostgreSQLNotificationParserTest method testLocalCodeNodeCommentParsingDelete.
@Test
public void testLocalCodeNodeCommentParsingDelete() {
new MockModule(provider, Lists.newArrayList(mockView), Lists.newArrayList(mockFunction));
MockPGNotification notification = new MockPGNotification("comment_changes", "bn_code_nodes UPDATE 1 1111 4608 null");
final CommentNotification result = PostgreSQLCommentNotificationParser.processNodeLocalNodeCommentNotification(notification, provider);
assertNotNull(result);
final CodeNodeCommentNotificationContainer container = (CodeNodeCommentNotificationContainer) result;
final INaviCodeNode node = container.getNode();
assertEquals(1111, node.getId());
assertEquals(new CAddress(4608), node.getAddress());
assertEquals(CommentOperation.DELETE, container.getOperation());
assertNull(container.getCommentId());
assertEquals(CommentScope.LOCAL, container.getScope());
}
Aggregations