Skip to content

Commit

Permalink
Use RzBuffer for byte search.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rot127 committed Jan 2, 2025
1 parent d9c4159 commit f8cac67
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
12 changes: 7 additions & 5 deletions librz/search/bytes_search.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,17 @@ static inline bool bytes_pattern_compare_masked(RZ_BORROW RZ_NONNULL const ut8 *
return true;
}

static bool bytes_find(RzSearchFindOpt *fopts, void *user, ut64 address, ut8 *buffer, size_t size, RzThreadQueue *hits) {
static bool bytes_find(RzSearchFindOpt *fopts, void *user, ut64 address, const RzBuffer *buffer, RzThreadQueue *hits) {
if (!fopts) {
RZ_LOG_ERROR("bytes_find requires valid find options.\n");
return false;
}

RzPVector /*<BytesPattern *>*/ *patterns = (RzPVector *)user;
ut64 size = 0;
// Remove const classifier. Because the buffer API is not constified, unfortunately.
const ut8 *raw_buf = rz_buf_get_whole_hot_paths((RzBuffer *)buffer, &size);
void **it = NULL;

RzPVector /*<BytesPattern *>*/ *patterns = (RzPVector *)user;
rz_pvector_foreach (patterns, it) {
RzSearchBytesPattern *hp = (RzSearchBytesPattern *)*it;
for (size_t offset = 0; offset < size;) {
Expand All @@ -207,12 +209,12 @@ static bool bytes_find(RzSearchFindOpt *fopts, void *user, ut64 address, ut8 *bu
break;
}
if (!hp->mask) {
if (!bytes_pattern_compare_no_mask(buffer + offset, hp)) {
if (!bytes_pattern_compare_no_mask(raw_buf + offset, hp)) {
offset++;
continue;
}
} else {
if (!bytes_pattern_compare_masked(buffer + offset, hp)) {
if (!bytes_pattern_compare_masked(raw_buf + offset, hp)) {
offset++;
continue;
}
Expand Down
12 changes: 5 additions & 7 deletions librz/search/search.c
Original file line number Diff line number Diff line change
Expand Up @@ -590,28 +590,26 @@ static bool search_iterator_io_map_cb(void *element, void *user) {
ut64 at = window->addr;
ut64 size = window->size;

// read the buffer
ut8 *buffer = malloc(size);
rz_th_lock_enter(ctx->io_lock);
int read = rz_io_nread_at(ctx->io, at, buffer, size);
if (!buffer || read != size) {
RzBuffer *buffer = rz_io_nread_at_new_buf(ctx->io, at, size);
if (!buffer || rz_buf_size(buffer) != size) {
RZ_LOG_ERROR("search: failed to read at 0x%08" PFMT64x " (0x%08" PFMT64x " bytes)\n", at, size);
rz_th_lock_leave(ctx->io_lock);
goto failure;
}
rz_th_lock_leave(ctx->io_lock);

RzSearchFindBytesCallback find = col->find;
if (!find(ctx->opt->find_opts, col->user, at, buffer, size, ctx->hits)) {
if (!find(ctx->opt->find_opts, col->user, at, buffer, ctx->hits)) {
RZ_LOG_ERROR("search: failed search at 0x%08" PFMT64x "\n", at);
goto failure;
}

free(buffer);
rz_buf_free(buffer);
return rz_atomic_bool_get(ctx->loop);

failure:
free(buffer);
rz_buf_free(buffer);
rz_atomic_bool_set(ctx->loop, false);
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion librz/search/search_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ typedef bool (*RzSearchIsEmptyCallback)(void *user);
* \return True, if a match was found.
* \return False otherwise.
*/
typedef bool (*RzSearchFindBytesCallback)(RZ_NULLABLE RzSearchFindOpt *fopt, void *user, ut64 address, ut8 *buffer, size_t size, RZ_OUT RzThreadQueue *hits);
typedef bool (*RzSearchFindBytesCallback)(RZ_NULLABLE RzSearchFindOpt *fopt, void *user, ut64 address, const RzBuffer *buffer, RZ_OUT RzThreadQueue *hits);

/**
* \brief A callback to search a graph for a pattern.
Expand Down

0 comments on commit f8cac67

Please sign in to comment.