Rizin
unix-like reverse engineering framework and cli tools
bfile_string.c File Reference
#include <rz_bin.h>
#include <rz_util/rz_log.h>
#include <rz_util/rz_str_search.h>

Go to the source code of this file.

Classes

struct  search_interval_t
 
struct  shared_data_t
 
struct  search_thread_data_t
 

Macros

#define UTIL_STR_SCAN_OPT_BUFFER_SIZE   2048
 
#define RAW_FILE_ALIGNMENT   0x10000
 

Typedefs

typedef struct search_interval_t SearchInterval
 
typedef struct shared_data_t SharedData
 
typedef struct search_thread_data_t SearchThreadData
 

Functions

static st64 shared_data_read_at (SharedData *sd, ut64 addr, ut8 *buf, ut64 size)
 
static bool shared_ht_up_insert (SharedData *sd, const ut64 key, void *value)
 
static bool is_data_section (RzBinFile *a, RzBinSection *s)
 
static RzBinStringto_bin_string (RzDetectedString *src)
 
static RzListstring_scan_range (SearchThreadData *std, const ut64 paddr, const ut64 size)
 
static void * search_string_thread_runner (SearchThreadData *std)
 
static void bin_file_string_search_free (SearchThreadData *std)
 
static void interrupt_thread (RzThread *thread)
 
static void interrupt_pool (RzThreadPool *pool)
 
static bool create_string_search_thread (RzThreadPool *pool, size_t min_length, RzThreadQueue *intervals, SharedData *shared)
 
static int string_compare_sort (const RzBinString *a, const RzBinString *b)
 
static void string_scan_range_cfstring (RzBinFile *bf, HtUP *strings_db, RzList *results, const RzBinSection *section)
 
static void scan_cfstring_table (RzBinFile *bf, HtUP *strings_db, RzList *results, ut64 max_interval)
 
RZ_API RZ_OWN RzListrz_bin_file_strings (RZ_NONNULL RzBinFile *bf, size_t min_length, bool raw_strings)
 Generates a RzList struct containing RzBinString from a given RzBinFile. More...
 

Macro Definition Documentation

◆ RAW_FILE_ALIGNMENT

#define RAW_FILE_ALIGNMENT   0x10000

Definition at line 9 of file bfile_string.c.

◆ UTIL_STR_SCAN_OPT_BUFFER_SIZE

#define UTIL_STR_SCAN_OPT_BUFFER_SIZE   2048

Definition at line 8 of file bfile_string.c.

Typedef Documentation

◆ SearchInterval

◆ SearchThreadData

◆ SharedData

typedef struct shared_data_t SharedData

Function Documentation

◆ bin_file_string_search_free()

static void bin_file_string_search_free ( SearchThreadData std)
static

Definition at line 155 of file bfile_string.c.

155  {
156  if (!std) {
157  return;
158  }
159  rz_list_free(std->results);
161  free(std);
162 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
RzAtomicBool * loop
Definition: bfile_string.c:29
RZ_API void rz_atomic_bool_free(RZ_NULLABLE RzAtomicBool *tbool)
Frees a RzAtomicBool structure.
Definition: thread_types.c:39

References free(), search_thread_data_t::loop, search_thread_data_t::results, rz_atomic_bool_free(), and rz_list_free().

Referenced by create_string_search_thread(), and rz_bin_file_strings().

◆ create_string_search_thread()

static bool create_string_search_thread ( RzThreadPool pool,
size_t  min_length,
RzThreadQueue intervals,
SharedData shared 
)
static

Definition at line 181 of file bfile_string.c.

181  {
183  RzBinPlugin *plugin = rz_bin_file_cur_plugin(shared->bf);
184  bool check_ascii_freq = false;
185 
186  if (!min_length) {
187  min_length = plugin && plugin->minstrlen > 0 ? plugin->minstrlen : 4;
188  }
189 
190  if (shared->bf->rbin) {
192  check_ascii_freq = shared->bf->rbin->strseach_check_ascii_freq;
193  }
194 
196  if (!std) {
197  RZ_LOG_ERROR("bin_file_strings: cannot allocate SearchThreadData.\n");
198  return false;
199  }
200 
202  if (!std->results) {
204  return false;
205  }
206  std->shared = shared;
207  std->check_ascii_freq = check_ascii_freq;
208  std->encoding = encoding;
209  std->intervals = intervals;
210  std->min_length = min_length;
211  std->loop = rz_atomic_bool_new(true);
212 
214  if (!thread) {
215  RZ_LOG_ERROR("bin_file_strings: cannot allocate RzThread\n");
217  return false;
218  } else if (!rz_th_pool_add_thread(pool, thread)) {
219  RZ_LOG_ERROR("bin_file_strings: cannot add thread to pool\n");
220  interrupt_thread(thread);
222  rz_th_free(thread);
223  return false;
224  }
225  return true;
226 }
RZ_API RzBinPlugin * rz_bin_file_cur_plugin(RzBinFile *bf)
Definition: bfile.c:348
static void bin_file_string_search_free(SearchThreadData *std)
Definition: bfile_string.c:155
static void * search_string_thread_runner(SearchThreadData *std)
Definition: bfile_string.c:108
static void interrupt_thread(RzThread *thread)
Definition: bfile_string.c:164
RZ_API void rz_bin_string_free(void *_str)
Definition: bin.c:192
RZ_API RZ_OWN RzList * rz_list_newf(RzListFree f)
Returns a new initialized RzList pointer and sets the free method.
Definition: list.c:248
RZ_API void rz_th_free(RZ_NULLABLE RzThread *th)
Frees a RzThread structure.
Definition: thread.c:246
RZ_API RZ_OWN RzThread * rz_th_new(RZ_NONNULL RzThreadFunction function, RZ_NULLABLE void *user)
Creates and starts a new thread.
Definition: thread.c:198
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
RZ_API RzStrEnc rz_str_enc_string_as_type(RZ_NULLABLE const char *enc)
converts an encoding name to RzStrEnc
Definition: str.c:86
RzStrEnc
Definition: rz_str.h:19
@ RZ_STRING_ENC_GUESS
Definition: rz_str.h:33
void *(* RzThreadFunction)(void *user)
Definition: rz_th.h:28
#define RZ_NEW0(x)
Definition: rz_types.h:284
struct rz_bin_t * rbin
Definition: rz_bin.h:316
bool strseach_check_ascii_freq
Definition: rz_bin.h:361
char * strenc
Definition: rz_bin.h:357
Definition: thread.h:84
RzThreadQueue * intervals
Definition: bfile_string.c:23
SharedData * shared
Definition: bfile_string.c:28
RzBinFile * bf
Definition: bfile_string.c:18
RZ_API bool rz_th_pool_add_thread(RZ_NONNULL RzThreadPool *pool, RZ_NONNULL RzThread *thread)
Adds a thread to the thread pool.
Definition: thread_pool.c:138
RZ_API RZ_OWN RzAtomicBool * rz_atomic_bool_new(bool value)
Initialize a thread safe bool type container.
Definition: thread_types.c:24

References shared_data_t::bf, bin_file_string_search_free(), search_thread_data_t::check_ascii_freq, search_thread_data_t::encoding, cmd_descs_generate::encoding, interrupt_thread(), search_thread_data_t::intervals, search_thread_data_t::loop, search_thread_data_t::min_length, rz_bin_plugin_t::minstrlen, rz_bin_file_t::rbin, search_thread_data_t::results, rz_atomic_bool_new(), rz_bin_file_cur_plugin(), rz_bin_string_free(), rz_list_newf(), RZ_LOG_ERROR, RZ_NEW0, rz_str_enc_string_as_type(), RZ_STRING_ENC_GUESS, rz_th_free(), rz_th_new(), rz_th_pool_add_thread(), search_string_thread_runner(), search_thread_data_t::shared, rz_bin_t::strenc, and rz_bin_t::strseach_check_ascii_freq.

Referenced by rz_bin_file_strings().

◆ interrupt_pool()

static void interrupt_pool ( RzThreadPool pool)
static

Definition at line 173 of file bfile_string.c.

173  {
174  size_t pool_size = rz_th_pool_size(pool);
175  for (size_t i = 0; i < pool_size; ++i) {
176  RzThread *th = rz_th_pool_get_thread(pool, i);
177  interrupt_thread(th);
178  }
179 }
lzma_index ** i
Definition: index.h:629
RZ_API RZ_BORROW RzThread * rz_th_pool_get_thread(RZ_NONNULL RzThreadPool *pool, size_t index)
Returns the n-th thread in the thread pool.
Definition: thread_pool.c:158
RZ_API size_t rz_th_pool_size(RZ_NONNULL RzThreadPool *pool)
Returns the thread pool size.
Definition: thread_pool.c:189

References i, interrupt_thread(), rz_th_pool_get_thread(), and rz_th_pool_size().

Referenced by rz_bin_file_strings().

◆ interrupt_thread()

static void interrupt_thread ( RzThread thread)
static

Definition at line 164 of file bfile_string.c.

164  {
165  if (!thread) {
166  return;
167  }
169  rz_atomic_bool_set(std->loop, false);
170  rz_th_wait(thread);
171 }
RZ_API RZ_OWN void * rz_th_get_user(RZ_NONNULL RzThread *th)
Returns user pointer of thread.
Definition: thread.c:263
RZ_API bool rz_th_wait(RZ_NONNULL RzThread *th)
Awaits indefinetely for a thread to join.
Definition: thread.c:231
RZ_API void rz_atomic_bool_set(RZ_NONNULL RzAtomicBool *tbool, bool value)
Sets the value int the RzAtomicBool structure.
Definition: thread_types.c:68

References search_thread_data_t::loop, rz_atomic_bool_set(), rz_th_get_user(), and rz_th_wait().

Referenced by create_string_search_thread(), and interrupt_pool().

◆ is_data_section()

static bool is_data_section ( RzBinFile a,
RzBinSection s 
)
static

Definition at line 46 of file bfile_string.c.

46  {
47  if (s->has_strings || s->is_data) {
48  return true;
49  } else if (!s->name) {
50  return false;
51  }
52  // Rust binaries contains the _const section which is a data section.
53  return strstr(s->name, "_const") != NULL;
54 }
#define NULL
Definition: cris-opc.c:27
static RzSocket * s
Definition: rtr.c:28

References NULL, and s.

Referenced by rz_bin_file_strings().

◆ rz_bin_file_strings()

RZ_API RZ_OWN RzList* rz_bin_file_strings ( RZ_NONNULL RzBinFile bf,
size_t  min_length,
bool  raw_strings 
)

Generates a RzList struct containing RzBinString from a given RzBinFile.

Parameters
bfThe RzBinFile to use for searching for strings
min_lengthThe string minimum length (when len < 1, is set to 4)
raw_stringsWhen set to false, it will search for strings only in the data section
Returns
On success returns RzList pointer, otherwise NULL

Definition at line 325 of file bfile_string.c.

325  {
327 
328  HtUP *strings_db = NULL;
329  RzList *results = NULL;
330  RzThreadQueue *intervals = NULL;
331  RzThreadPool *pool = NULL;
333  ut64 max_interval = 0;
334  size_t pool_size = 1;
335 
336  if (min_length < 1) {
337  min_length = 4;
338  }
339 
340  if (bf->rbin) {
341  max_interval = bf->rbin->maxstrbuf;
342  }
343 
345  if (!pool) {
346  RZ_LOG_ERROR("bin_file_strings: cannot allocate thread pool.\n");
347  goto fail;
348  }
349  pool_size = rz_th_pool_size(pool);
350 
351  lock = rz_th_lock_new(false);
352  if (!lock) {
353  RZ_LOG_ERROR("bin_file_strings: cannot allocate thread lock.\n");
354  goto fail;
355  }
356 
358  if (!intervals) {
359  RZ_LOG_ERROR("bin_file_strings: cannot allocate intervals queue.\n");
360  goto fail;
361  }
362 
363  strings_db = ht_up_new0();
364  if (!strings_db) {
365  RZ_LOG_ERROR("bin_file_strings: cannot allocate string map.\n");
366  goto fail;
367  }
368 
369  if (raw_strings) {
370  // returns all the strings found on the RzBinFile
371  ut64 section_size = bf->size / pool_size;
372  if (section_size & (RAW_FILE_ALIGNMENT - 1)) {
373  section_size += RAW_FILE_ALIGNMENT;
374  section_size &= ~(RAW_FILE_ALIGNMENT - 1);
375  }
376  if (!section_size) {
377  section_size += RAW_FILE_ALIGNMENT;
378  }
379 
380  if (max_interval && section_size > max_interval) {
381  RZ_LOG_WARN("bin_file_strings: search interval size (0x%" PFMT64x
382  ") exeeds bin.maxstrbuf (0x%" PFMT64x "), skipping it.\n",
383  section_size, max_interval);
384  goto fail;
385  }
386 
387  for (ut64 from = 0; from < bf->size; from += section_size) {
389  if (!itv) {
390  RZ_LOG_ERROR("bin_file_strings: cannot allocate SearchInterval.\n");
391  goto fail;
392  }
393 
394  itv->paddr = from;
395  itv->psize = section_size;
396  if ((itv->paddr + itv->psize) > bf->size) {
397  itv->psize = bf->size - itv->paddr;
398  }
399 
400  if (!rz_th_queue_push(intervals, itv, true)) {
401  free(itv);
402  RZ_LOG_ERROR("bin_file_strings: cannot append SearchInterval to list.\n");
403  goto fail;
404  }
405  }
406  } else if (bf->o && !rz_list_empty(bf->o->sections)) {
407  // returns only the strings found on the RzBinFile but within the data section
408  RzListIter *iter = NULL;
410  RzBinObject *o = bf->o;
411  rz_list_foreach (o->sections, iter, section) {
412  if (section->paddr >= bf->size) {
413  continue;
414  } else if (max_interval && section->size > max_interval) {
415  RZ_LOG_WARN("bin_file_strings: search interval size (0x%" PFMT64x
416  ") exeeds bin.maxstrbuf (0x%" PFMT64x "), skipping it.\n",
417  section->size, max_interval);
418  continue;
419  }
420 
421  if (!is_data_section(bf, section)) {
422  continue;
423  }
424 
426  if (!itv) {
427  RZ_LOG_ERROR("bin_file_strings: cannot allocate SearchInterval.\n");
428  goto fail;
429  }
430 
431  itv->paddr = section->paddr;
432  itv->psize = section->size;
433  if ((itv->paddr + itv->psize) > bf->size) {
434  itv->psize = bf->size - itv->paddr;
435  }
436 
437  if (!rz_th_queue_push(intervals, itv, true)) {
438  free(itv);
439  RZ_LOG_ERROR("bin_file_strings: cannot append SearchInterval to list.\n");
440  goto fail;
441  }
442  }
443  }
444 
445  SharedData shared = {
446  .lock = lock,
447  .bf = bf,
448  .strings_db = strings_db,
449  };
450 
451  RZ_LOG_VERBOSE("bin_file_strings: using %u threads\n", (ut32)pool_size);
452  for (size_t i = 0; i < pool_size; ++i) {
453  if (!create_string_search_thread(pool, min_length, intervals, &shared)) {
454  interrupt_pool(pool);
455  goto fail;
456  }
457  }
458 
459  rz_th_pool_wait(pool);
460 
461  results = rz_list_newf(rz_bin_string_free);
462  if (!results) {
463  RZ_LOG_ERROR("bin_file_strings: cannot allocate results list.\n");
464  goto fail;
465  }
466 
467  for (ut32 i = 0; i < pool_size; ++i) {
468  RzThread *th = rz_th_pool_get_thread(pool, i);
469  if (!th) {
470  continue;
471  }
473  if (std) {
474  rz_list_join(results, std->results);
475  }
476  }
477 
478  if (!raw_strings) {
479  scan_cfstring_table(bf, strings_db, results, max_interval);
480  }
482 
483  {
484  RzListIter *it;
485  RzBinString *bstr;
486  ut32 ordinal = 0;
487  rz_list_foreach (results, it, bstr) {
488  bstr->ordinal = ordinal;
489  ordinal++;
490  }
491  }
492 
493 fail:
494  if (pool) {
495  for (ut32 i = 0; i < pool_size; ++i) {
496  RzThread *th = rz_th_pool_get_thread(pool, i);
497  if (!th) {
498  continue;
499  }
502  }
503  rz_th_pool_free(pool);
504  }
505  ht_up_free(strings_db);
507  rz_th_queue_free(intervals);
508  return results;
509 }
static void scan_cfstring_table(RzBinFile *bf, HtUP *strings_db, RzList *results, ut64 max_interval)
Definition: bfile_string.c:293
static void interrupt_pool(RzThreadPool *pool)
Definition: bfile_string.c:173
static bool is_data_section(RzBinFile *a, RzBinSection *s)
Definition: bfile_string.c:46
#define RAW_FILE_ALIGNMENT
Definition: bfile_string.c:9
static int string_compare_sort(const RzBinString *a, const RzBinString *b)
Definition: bfile_string.c:228
static bool create_string_search_thread(RzThreadPool *pool, size_t min_length, RzThreadQueue *intervals, SharedData *shared)
Definition: bfile_string.c:181
uint32_t ut32
RZ_API void rz_list_sort(RZ_NONNULL RzList *list, RZ_NONNULL RzListComparator cmp)
Sorts via merge sort or via insertion sort a list.
Definition: list.c:743
RZ_API bool rz_list_join(RZ_NONNULL RzList *list1, RZ_NONNULL RzList *list2)
Joins 2 list into one (list2 pointer needs to be freed by the user)
Definition: list.c:209
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
void(* RzListFree)(void *ptr)
Definition: rz_list.h:11
int(* RzListComparator)(const void *value, const void *list_data)
Definition: rz_list.h:33
#define RZ_LOG_VERBOSE(fmtstr,...)
Definition: rz_log.h:52
#define RZ_LOG_WARN(fmtstr,...)
Definition: rz_log.h:56
#define RZ_THREAD_POOL_ALL_CORES
Definition: rz_th.h:19
#define RZ_THREAD_QUEUE_UNLIMITED
Definition: rz_th.h:20
#define PFMT64x
Definition: rz_types.h:393
static struct sockaddr static addrlen static backlog const void static flags void struct sockaddr from
Definition: sfsocketcall.h:123
RzList * sections
Definition: rz_bin.h:267
ut32 ordinal
Definition: rz_bin.h:755
RzThreadPool is a structure which handles n-threads threads.
Definition: thread_pool.c:12
RzThreadQueue is a thread-safe queue that can be listened on from multiple threads.
Definition: thread_queue.c:17
uint32_t size
RzThreadLock * lock
Definition: bfile_string.c:17
#define fail(test)
Definition: tests.h:29
RZ_API void rz_th_lock_free(RZ_NULLABLE RzThreadLock *thl)
Frees a RzThreadLock structure.
Definition: thread_lock.c:89
RZ_API RZ_OWN RzThreadLock * rz_th_lock_new(bool recursive)
Allocates and initialize a RzThreadLock structure.
Definition: thread_lock.c:14
RZ_API void rz_th_pool_free(RZ_NULLABLE RzThreadPool *pool)
Kills (and frees) the threads and frees the RzThreadPool struct.
Definition: thread_pool.c:117
RZ_API bool rz_th_pool_wait(RZ_NONNULL RzThreadPool *pool)
Waits the end of all the threads in the thread pool.
Definition: thread_pool.c:170
RZ_API RZ_OWN RzThreadPool * rz_th_pool_new(size_t max_threads)
returns a new RzThreadPool structure with a pool of thread
Definition: thread_pool.c:96
RZ_API bool rz_th_queue_push(RZ_NONNULL RzThreadQueue *queue, RZ_NONNULL void *user, bool tail)
Pushes a new element into the queue.
Definition: thread_queue.c:75
RZ_API void rz_th_queue_free(RZ_NULLABLE RzThreadQueue *queue)
Frees a RzThreadQueue structure.
Definition: thread_queue.c:55
RZ_API RZ_OWN RzThreadQueue * rz_th_queue_new(size_t max_size, RZ_NULLABLE RzListFree qfree)
Allocates and initializes a new fifo queue.
Definition: thread_queue.c:32
static void lock(volatile int *lk)
Definition: malloc.c:61
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References bin_file_string_search_free(), create_string_search_thread(), fail, free(), from, i, interrupt_pool(), is_data_section(), shared_data_t::lock, lock(), NULL, rz_bin_string_t::ordinal, search_interval_t::paddr, PFMT64x, search_interval_t::psize, RAW_FILE_ALIGNMENT, search_thread_data_t::results, rz_bin_string_free(), rz_list_join(), rz_list_newf(), rz_list_sort(), RZ_LOG_ERROR, RZ_LOG_VERBOSE, RZ_LOG_WARN, RZ_NEW0, rz_return_val_if_fail, rz_th_get_user(), rz_th_lock_free(), rz_th_lock_new(), rz_th_pool_free(), rz_th_pool_get_thread(), rz_th_pool_new(), rz_th_pool_size(), rz_th_pool_wait(), rz_th_queue_free(), rz_th_queue_new(), rz_th_queue_push(), RZ_THREAD_POOL_ALL_CORES, RZ_THREAD_QUEUE_UNLIMITED, scan_cfstring_table(), rz_bin_object_t::sections, section::size, string_compare_sort(), and ut64().

Referenced by basefind_create_array_of_addresses(), rz_bin_object_reset_strings(), rz_bin_object_set_items(), rz_core_bin_whole_strings(), rz_main_rz_bin(), rzfind_open_file(), and strings().

◆ scan_cfstring_table()

static void scan_cfstring_table ( RzBinFile bf,
HtUP *  strings_db,
RzList results,
ut64  max_interval 
)
static

Definition at line 293 of file bfile_string.c.

293  {
294  RzListIter *iter = NULL;
296  RzBinObject *o = bf->o;
297  if (!o) {
298  return;
299  }
300  rz_list_foreach (o->sections, iter, section) {
301  if (!section->name || section->paddr >= bf->size) {
302  continue;
303  } else if (max_interval && section->size > max_interval) {
304  RZ_LOG_WARN("bin_file_strings: search interval size (0x%" PFMT64x
305  ") exeeds bin.maxstrbuf (0x%" PFMT64x "), skipping it.\n",
306  section->size, max_interval);
307  continue;
308  }
309 
310  if (strstr(section->name, "__cfstring")) {
311  string_scan_range_cfstring(bf, strings_db, results, section);
312  }
313  }
314 }
static void string_scan_range_cfstring(RzBinFile *bf, HtUP *strings_db, RzList *results, const RzBinSection *section)
Definition: bfile_string.c:241
RzBinObject * o
Definition: rz_bin.h:305

References NULL, rz_bin_file_t::o, PFMT64x, RZ_LOG_WARN, rz_bin_object_t::sections, section::size, rz_bin_file_t::size, and string_scan_range_cfstring().

Referenced by rz_bin_file_strings().

◆ search_string_thread_runner()

static void* search_string_thread_runner ( SearchThreadData std)
static

Definition at line 108 of file bfile_string.c.

108  {
109  SearchInterval *itv = NULL;
110  RzDetectedString *detected = NULL;
111  ut64 paddr = 0, psize = 0;
112  bool loop = true;
113  RzBinFile *bf = std->shared->bf; // this data is always RO
114 
115  do {
116  itv = rz_th_queue_pop(std->intervals, false);
117  if (!itv) {
118  break;
119  }
120  paddr = itv->paddr;
121  psize = itv->psize;
122  free(itv);
123  RZ_LOG_DEBUG("[%p] searching between [0x%08" PFMT64x " : 0x%08" PFMT64x "]\n", std, paddr, paddr + psize);
124 
125  RzList *list = string_scan_range(std, paddr, psize);
126  while (list && rz_atomic_bool_get(std->loop)) {
127  detected = rz_list_pop_head(list);
128  if (!detected) {
129  break;
130  }
131 
132  RzBinString *bstr = to_bin_string(detected);
133  if (!bstr || !rz_list_append(std->results, bstr)) {
134  rz_bin_string_free(bstr);
135  loop = false;
136  break;
137  } else if (!bf->o) {
138  continue;
139  }
140 
141  // find virt address.
142  bstr->paddr += bf->o->boffset;
143  bstr->vaddr = rz_bin_object_p2v(bf->o, bstr->paddr);
144 
145  shared_ht_up_insert(std->shared, bstr->vaddr, bstr);
146  }
147 
149  } while (loop && rz_atomic_bool_get(std->loop));
150 
151  RZ_LOG_DEBUG("[%p] died\n", std);
152  return NULL;
153 }
static RzBinString * to_bin_string(RzDetectedString *src)
Definition: bfile_string.c:56
static RzList * string_scan_range(SearchThreadData *std, const ut64 paddr, const ut64 size)
Definition: bfile_string.c:76
static bool shared_ht_up_insert(SharedData *sd, const ut64 key, void *value)
Definition: bfile_string.c:39
RZ_API ut64 rz_bin_object_p2v(RZ_NONNULL RzBinObject *obj, ut64 paddr)
Convert offset in the file to virtual address according to binary mappings.
Definition: bobj.c:970
static void list(RzEgg *egg)
Definition: rz-gg.c:52
RZ_API RZ_OWN void * rz_list_pop_head(RZ_NONNULL RzList *list)
Removes and returns the first element of the list.
Definition: list.c:401
RZ_API RZ_BORROW RzListIter * rz_list_append(RZ_NONNULL RzList *list, void *data)
Appends at the end of the list a new element.
Definition: list.c:288
#define RZ_LOG_DEBUG(fmtstr,...)
Definition: rz_log.h:49
XX curplugin == o->plugin.
Definition: rz_bin.h:298
ut64 boffset
Definition: rz_bin.h:262
uv_loop_t * loop
Definition: main.c:7
RZ_API RZ_OWN void * rz_th_queue_pop(RZ_NONNULL RzThreadQueue *queue, bool tail)
Removes an element from the queue, but does not awaits when empty.
Definition: thread_queue.c:102
RZ_API bool rz_atomic_bool_get(RZ_NONNULL RzAtomicBool *tbool)
Gets the current value hold by the RzAtomicBool structure.
Definition: thread_types.c:54

References shared_data_t::bf, rz_bin_object_t::boffset, free(), search_thread_data_t::intervals, list(), search_thread_data_t::loop, loop, NULL, rz_bin_file_t::o, search_interval_t::paddr, rz_bin_string_t::paddr, PFMT64x, search_interval_t::psize, search_thread_data_t::results, rz_atomic_bool_get(), rz_bin_object_p2v(), rz_bin_string_free(), rz_list_append(), rz_list_free(), rz_list_pop_head(), RZ_LOG_DEBUG, rz_th_queue_pop(), search_thread_data_t::shared, shared_ht_up_insert(), string_scan_range(), to_bin_string(), ut64(), and rz_bin_string_t::vaddr.

Referenced by create_string_search_thread().

◆ shared_data_read_at()

static st64 shared_data_read_at ( SharedData sd,
ut64  addr,
ut8 buf,
ut64  size 
)
static

Definition at line 32 of file bfile_string.c.

32  {
34  st64 ret = rz_buf_read_at(sd->bf->buf, addr, buf, size);
36  return ret;
37 }
voidpf void uLong size
Definition: ioapi.h:138
voidpf void * buf
Definition: ioapi.h:138
RZ_API st64 rz_buf_read_at(RZ_NONNULL RzBuffer *b, ut64 addr, RZ_NONNULL RZ_OUT ut8 *buf, ut64 len)
Read len bytes of the buffer at the specified address.
Definition: buf.c:1136
#define st64
Definition: rz_types_base.h:10
RzBuffer * buf
Definition: rz_bin.h:303
RZ_API void rz_th_lock_leave(RZ_NONNULL RzThreadLock *thl)
Releases a RzThreadLock structure.
Definition: thread_lock.c:75
RZ_API void rz_th_lock_enter(RZ_NONNULL RzThreadLock *thl)
Acquires a RzThreadLock structure.
Definition: thread_lock.c:45
static int addr
Definition: z80asm.c:58

References addr, shared_data_t::bf, rz_bin_file_t::buf, shared_data_t::lock, rz_buf_read_at(), rz_th_lock_enter(), rz_th_lock_leave(), and st64.

Referenced by string_scan_range().

◆ shared_ht_up_insert()

static bool shared_ht_up_insert ( SharedData sd,
const ut64  key,
void *  value 
)
static

Definition at line 39 of file bfile_string.c.

39  {
41  bool ret = ht_up_insert(sd->strings_db, key, value);
43  return ret;
44 }
static int value
Definition: cmd_api.c:93
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len key
Definition: sflib.h:118
HtUP * strings_db
Definition: bfile_string.c:19

References key, shared_data_t::lock, rz_th_lock_enter(), rz_th_lock_leave(), shared_data_t::strings_db, and value.

Referenced by search_string_thread_runner().

◆ string_compare_sort()

static int string_compare_sort ( const RzBinString a,
const RzBinString b 
)
static

Definition at line 228 of file bfile_string.c.

228  {
229  if (b->paddr > a->paddr) {
230  return -1;
231  } else if (b->paddr < a->paddr) {
232  return 1;
233  } else if (b->vaddr > a->vaddr) {
234  return -1;
235  } else if (b->vaddr < a->vaddr) {
236  return 1;
237  }
238  return 0;
239 }
#define b(i)
Definition: sha256.c:42
#define a(i)
Definition: sha256.c:41

References a, and b.

Referenced by rz_bin_file_strings().

◆ string_scan_range()

static RzList* string_scan_range ( SearchThreadData std,
const ut64  paddr,
const ut64  size 
)
static

Definition at line 76 of file bfile_string.c.

76  {
78  if (!found) {
79  return NULL;
80  }
81 
82  RzUtilStrScanOptions scan_opt = {
84  .max_uni_blocks = 4,
85  .min_str_length = std->min_length,
86  .prefer_big_endian = false,
87  .check_ascii_freq = std->check_ascii_freq,
88  };
89 
90  ut8 *buf = calloc(size, 1);
91  if (!buf) {
92  RZ_LOG_ERROR("bin_file_strings: cannot allocate string seac buffer.\n");
93  return NULL;
94  }
95 
96  shared_data_read_at(std->shared, paddr, buf, size);
97 
98  ut64 end = paddr + size;
99  int count = rz_scan_strings_raw(buf, found, &scan_opt, paddr, end, std->encoding);
100  free(buf);
101 
102  if (count <= 0) {
104  }
105  return found;
106 }
#define UTIL_STR_SCAN_OPT_BUFFER_SIZE
Definition: bfile_string.c:8
static st64 shared_data_read_at(SharedData *sd, ut64 addr, ut8 *buf, ut64 size)
Definition: bfile_string.c:32
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98
RZ_API const KEY_TYPE bool * found
Definition: ht_inc.h:130
uint8_t ut8
Definition: lh5801.h:11
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
RZ_API int rz_scan_strings_raw(RZ_NONNULL const ut8 *buf, RZ_NONNULL RzList *list, RZ_NONNULL const RzUtilStrScanOptions *opt, const ut64 from, const ut64 to, RzStrEnc type)
Look for strings in an RzBuffer.
Definition: str_search.c:362
#define RZ_FREE_CUSTOM(x, y)
Definition: rz_types.h:375
size_t buf_size
Maximum size of a detected string.
Definition: rz_str_search.h:28

References RzUtilStrScanOptions::buf_size, calloc(), search_thread_data_t::check_ascii_freq, count, search_thread_data_t::encoding, test_evm::end, found, free(), search_thread_data_t::min_length, NULL, RZ_FREE_CUSTOM, rz_list_free(), rz_list_newf(), RZ_LOG_ERROR, rz_scan_strings_raw(), search_thread_data_t::shared, shared_data_read_at(), ut64(), and UTIL_STR_SCAN_OPT_BUFFER_SIZE.

Referenced by search_string_thread_runner().

◆ string_scan_range_cfstring()

static void string_scan_range_cfstring ( RzBinFile bf,
HtUP *  strings_db,
RzList results,
const RzBinSection section 
)
static

Definition at line 241 of file bfile_string.c.

241  {
242  // load objc/swift strings from CFstring table section
243 
244  RzBinObject *o = bf->o;
245  const int bits = o->info ? o->info->bits : 32;
246  const int cfstr_size = (bits == 64) ? 32 : 16;
247  const int cfstr_offs = (bits == 64) ? 16 : 8;
248 
249  ut8 *sbuf = calloc(section->size, 1);
250  if (!sbuf) {
251  RZ_LOG_ERROR("bin_file_strings: cannot allocate RzBinString.\n");
252  return;
253  }
254 
255  rz_buf_read_at(bf->buf, section->paddr + cfstr_offs, sbuf, section->size);
256  for (ut64 i = 0; i < section->size; i += cfstr_size) {
257  ut8 *buf = sbuf;
258  ut8 *p = buf + i;
259  if ((i + ((bits == 64) ? 8 : 4)) >= section->size) {
260  break;
261  }
262 
263  ut64 cfstr_vaddr = section->vaddr + i;
264  ut64 cstr_vaddr = (bits == 64) ? rz_read_le64(p) : rz_read_le32(p);
265  if (!cstr_vaddr || cstr_vaddr == UT64_MAX) {
266  continue;
267  }
268 
269  RzBinString *s = ht_up_find(strings_db, cstr_vaddr, NULL);
270  if (!s) {
271  continue;
272  }
273 
275  if (!bs) {
276  RZ_LOG_ERROR("bin_file_strings: cannot allocate RzBinString\n");
277  break;
278  }
279 
280  bs->type = s->type;
281  bs->length = s->length;
282  bs->size = s->size;
283  bs->ordinal = s->ordinal;
284  bs->vaddr = cfstr_vaddr;
285  bs->paddr = rz_bin_object_v2p(o, bs->vaddr);
286  bs->string = rz_str_newf("cstr.%s", s->string);
287  rz_list_append(results, bs);
288  ht_up_insert(strings_db, bs->vaddr, bs);
289  }
290  free(sbuf);
291 }
int bits(struct state *s, int need)
Definition: blast.c:72
RZ_API ut64 rz_bin_object_v2p(RZ_NONNULL RzBinObject *obj, ut64 vaddr)
Convert virtual address to offset in the file according to binary mappings.
Definition: bobj.c:1015
void * p
Definition: libc.cpp:67
static ut32 rz_read_le32(const void *src)
Definition: rz_endian.h:239
static ut64 rz_read_le64(const void *src)
Definition: rz_endian.h:266
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
#define UT64_MAX
Definition: rz_types_base.h:86
RzBinInfo * info
Definition: rz_bin.h:287
char * string
Definition: rz_bin.h:752

References rz_bin_info_t::bits, bits(), rz_bin_file_t::buf, calloc(), free(), i, rz_bin_object_t::info, rz_bin_string_t::length, NULL, rz_bin_file_t::o, rz_bin_string_t::ordinal, p, rz_bin_string_t::paddr, rz_bin_object_v2p(), rz_buf_read_at(), rz_list_append(), RZ_LOG_ERROR, RZ_NEW0, rz_read_le32(), rz_read_le64(), rz_str_newf(), s, section::size, rz_bin_string_t::size, rz_bin_string_t::string, rz_bin_string_t::type, ut64(), UT64_MAX, and rz_bin_string_t::vaddr.

Referenced by scan_cfstring_table().

◆ to_bin_string()

static RzBinString* to_bin_string ( RzDetectedString src)
static

Definition at line 56 of file bfile_string.c.

56  {
58  if (!dst) {
60  RZ_LOG_ERROR("bin_file_strings: cannot allocate RzBinString.\n");
61  return NULL;
62  }
63 
64  dst->string = src->string;
65  dst->size = src->size;
66  dst->length = src->length;
67  dst->type = src->type;
68  dst->paddr = src->addr;
69  dst->vaddr = src->addr;
70 
71  // variables has been transfered to RzBinString
72  free(src);
73  return dst;
74 }
lzma_index * src
Definition: index.h:567
char * dst
Definition: lz4.h:724
RZ_API void rz_detected_string_free(RzDetectedString *str)
Definition: str_search.c:73

References dst, free(), NULL, rz_detected_string_free(), RZ_LOG_ERROR, RZ_NEW0, and src.

Referenced by search_string_thread_runner().