Rizin
unix-like reverse engineering framework and cli tools
rz_main.h File Reference
#include <rz_types.h>
#include <rz_getopt.h>

Go to the source code of this file.

Classes

struct  rz_main_t
 

Macros

#define MAIN_NAME   main
 
#define ARGV_TYPE   char
 
#define ARGV_TYPE_TO_UTF8(argc, argv)   (char **)argv
 
#define FREE_UTF8_ARGV(argc, utf8_argv)
 

Typedefs

typedef struct rz_main_t RzMain
 
typedef int(* RzMainCallback) (int argc, const char **argv)
 

Functions

 RZ_LIB_VERSION_HEADER (rz_main)
 
RZ_API RzMainrz_main_new (const char *name)
 
RZ_API void rz_main_free (RzMain *m)
 
RZ_API int rz_main_run (RzMain *m, int argc, const char **argv)
 
RZ_API int rz_main_version_print (const char *program)
 
RZ_API int rz_main_rz_ax (int argc, const char **argv)
 
RZ_API int rz_main_rz_run (int argc, const char **argv)
 
RZ_API int rz_main_rz_hash (int argc, const char **argv)
 
RZ_API int rz_main_rz_bin (int argc, const char **argv)
 
RZ_API int rz_main_rizin (int argc, const char **argv)
 
RZ_API int rz_main_rz_asm (int argc, const char **argv)
 
RZ_API int rz_main_rz_agent (int argc, const char **argv)
 
RZ_API int rz_main_rz_find (int argc, const char **argv)
 
RZ_API int rz_main_rz_diff (int argc, const char **argv)
 
RZ_API int rz_main_rz_gg (int argc, const char **argv)
 
RZ_API int rz_main_rz_sign (int argc, const char **argv)
 

Macro Definition Documentation

◆ ARGV_TYPE

#define ARGV_TYPE   char

Definition at line 29 of file rz_main.h.

◆ ARGV_TYPE_TO_UTF8

#define ARGV_TYPE_TO_UTF8 (   argc,
  argv 
)    (char **)argv

Definition at line 30 of file rz_main.h.

◆ FREE_UTF8_ARGV

#define FREE_UTF8_ARGV (   argc,
  utf8_argv 
)

Definition at line 31 of file rz_main.h.

◆ MAIN_NAME

#define MAIN_NAME   main

Definition at line 28 of file rz_main.h.

Typedef Documentation

◆ RzMain

typedef struct rz_main_t RzMain

◆ RzMainCallback

typedef int(* RzMainCallback) (int argc, const char **argv)

Definition at line 34 of file rz_main.h.

Function Documentation

◆ RZ_LIB_VERSION_HEADER()

RZ_LIB_VERSION_HEADER ( rz_main  )

◆ rz_main_free()

RZ_API void rz_main_free ( RzMain m)

Definition at line 40 of file main.c.

40  {
41  free(m);
42 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130

References free(), and regress::m.

Referenced by MAIN_NAME().

◆ rz_main_new()

RZ_API RzMain* rz_main_new ( const char *  name)

Definition at line 24 of file main.c.

24  {
25  int i = 0;
26  while (foo[i].name) {
27  if (!strcmp(name, foo[i].name)) {
28  RzMain *m = RZ_NEW0(RzMain);
29  if (m) {
30  m->name = strdup(foo[i].name);
31  m->main = foo[i].main;
32  }
33  return m;
34  }
35  i++;
36  }
37  return NULL;
38 }
lzma_index ** i
Definition: index.h:629
#define NULL
Definition: cris-opc.c:27
static RzMain foo[]
Definition: main.c:11
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")
#define RZ_NEW0(x)
Definition: rz_types.h:284
Definition: z80asm.h:102
int(* main)(int argc, const char **argv)
Definition: rz_main.h:18

References foo, i, regress::m, rz_main_t::main, NULL, RZ_NEW0, and strdup().

Referenced by MAIN_NAME().

◆ rz_main_rizin()

RZ_API int rz_main_rizin ( int  argc,
const char **  argv 
)

Definition at line 370 of file rizin.c.

370  {
371  RzCore *r;
372  bool forcequit = false;
373  bool haveRarunProfile = false;
374  RzListIter *iter;
375  int do_analysis = 0;
376  char *cmdn, *tmp;
377  RzCoreFile *fh = NULL;
378  RzIODesc *iod = NULL;
379  const char *prj = NULL;
380  int debug = 0;
381  int zflag = 0;
382  bool do_connect = false;
383  bool fullfile = false;
384  bool zerosep = false;
385  int help = 0;
386  enum { LOAD_BIN_ALL,
387  LOAD_BIN_NOTHING,
388  LOAD_BIN_STRUCTURES_ONLY } load_bin = LOAD_BIN_ALL;
389  bool run_rc = true;
390  int ret, c, perms = RZ_PERM_RX;
391  ut64 baddr = UT64_MAX;
392  ut64 seek = UT64_MAX;
393  bool do_list_io_plugins = false;
394  char *file = NULL;
395  char *pfile = NULL;
396  const char *asmarch = NULL;
397  const char *asmos = NULL;
398  const char *forcebin = NULL;
399  const char *asmbits = NULL;
400  char *customRarunProfile = NULL;
401  ut64 mapaddr = 0LL;
402  bool quiet = false;
403  bool quietLeak = false;
404  int is_gdb = false;
405  const char *s_seek = NULL;
406  bool compute_hashes = true;
407  RzList *cmds = rz_list_new();
408  RzList *evals = rz_list_new();
409  RzList *files = rz_list_new();
410  RzList *prefiles = rz_list_new();
411  RzCmdStateOutput state = { 0 };
412 
413 #define LISTS_FREE() \
414  { \
415  rz_list_free(cmds); \
416  rz_list_free(evals); \
417  rz_list_free(files); \
418  rz_list_free(prefiles); \
419  }
420 
421  bool noStderr = false;
422 
423 #ifdef __UNIX
424  sigset_t sigBlockMask;
425  sigemptyset(&sigBlockMask);
426  sigaddset(&sigBlockMask, SIGWINCH);
427  rz_signal_sigmask(SIG_BLOCK, &sigBlockMask, NULL);
428 #endif
429 
430  rz_sys_env_init();
431  // Create rz-run profile with startup environ
432  char **env = rz_sys_get_environ();
433  char *envprofile = rz_run_get_environ_profile(env);
434 
435  if (rz_sys_getenv_asbool("RZ_DEBUG")) {
436  char *sysdbg = rz_sys_getenv("RZ_DEBUG_TOOL");
437  char *fmt = (sysdbg && *sysdbg)
438  ? rz_str_newf("%s %%d", sysdbg)
439 #if __APPLE__
440  : rz_str_newf("lldb -p %%d");
441 #else
442  : rz_str_newf("gdb --pid %%d");
443 #endif
445  free(fmt);
446  free(sysdbg);
447  }
448 
449  r = rz_core_new();
450  if (!r) {
451  RZ_LOG_ERROR("Cannot initialize RzCore\n");
452  LISTS_FREE();
453  free(envprofile);
454  return 1;
455  }
456  r->rz_main_rizin = rz_main_rizin;
457  r->rz_main_rz_diff = rz_main_rz_diff;
458  r->rz_main_rz_find = rz_main_rz_find;
459  r->rz_main_rz_bin = rz_main_rz_bin;
460  r->rz_main_rz_gg = rz_main_rz_gg;
461  r->rz_main_rz_asm = rz_main_rz_asm;
462  r->rz_main_rz_ax = rz_main_rz_ax;
463 
464  r->io->envprofile = envprofile;
465 
466  rz_core_task_sync_begin(&r->tasks);
467  // HACK TO PERMIT '#!/usr/bin/rz - -i' hashbangs
468  if (argc > 2 && !strcmp(argv[1], "-") && !strcmp(argv[2], "-i")) {
469  argv[1] = argv[0];
470  argc--;
471  argv++;
472  }
473 
474  // -H option without argument
475  if (argc == 2 && !strcmp(argv[1], "-H")) {
477  LISTS_FREE();
478  return 0;
479  }
480 
482  bool load_l = true;
483  char *debugbackend = strdup("native");
484 
485  RzGetopt opt;
486  rz_getopt_init(&opt, argc, argv, "=02AMCwxfF:H:hm:e:nk:NdqQs:p:b:B:a:Lui:I:l:R:r:c:D:vVSTzuXt");
487  while (argc >= 2 && (c = rz_getopt_next(&opt)) != -1) {
488  switch (c) {
489  case '-':
490  RZ_LOG_ERROR("%c: invalid combinations of argument flags - %s\n", opt.opt, opt.argv[2]);
491  ret = 1;
492  goto beach;
493  break;
494  case '=':
495  RZ_FREE(r->cmdremote);
496  r->cmdremote = strdup("");
497  break;
498  case '2':
499  noStderr = true;
500  break;
501  case '0':
502  zerosep = true;
503  /* implicit -q */
504  rz_config_set(r->config, "scr.interactive", "false");
505  rz_config_set(r->config, "scr.prompt", "false");
506  rz_config_set_i(r->config, "scr.color", COLOR_MODE_DISABLED);
507  quiet = true;
508  break;
509  case 'u':
510  rz_config_set(r->config, "bin.filter", "false");
511  break;
512  case 'a':
513  asmarch = opt.arg;
514  break;
515  case 'z':
516  zflag++;
517  break;
518  case 'A':
519  do_analysis += do_analysis ? 1 : 2;
520  break;
521  case 'b':
522  asmbits = opt.arg;
523  break;
524  case 'B':
525  baddr = rz_num_math(r->num, opt.arg);
526  break;
527  case 'X':
528  rz_config_set(r->config, "bin.usextr", "false");
529  break;
530  case 'c':
531  rz_list_append(cmds, (void *)opt.arg);
532  break;
533  case 'C':
534  do_connect = true;
535  break;
536 #if DEBUGGER
537  case 'd': debug = 1; break;
538 #else
539  case 'd':
540  RZ_LOG_ERROR("Sorry. No debugger backend available.\n");
541  return 1;
542 #endif
543  case 'D': {
544  debug = 2;
545  free(debugbackend);
546  debugbackend = strdup(opt.arg);
547  RzCmdStateOutput state = { 0 };
549  if (!strcmp(opt.arg, "?")) {
553  rz_cons_flush();
554  LISTS_FREE();
555  return 0;
556  }
557  break;
558  }
559  case 'e':
560  if (!strcmp(opt.arg, "q")) {
561  rz_core_cmd0(r, "eq");
562  } else {
563  rz_config_eval(r->config, opt.arg);
564  rz_list_append(evals, (void *)opt.arg);
565  }
566  break;
567  case 'f':
568  fullfile = true;
569  break;
570  case 'F':
571  forcebin = opt.arg;
572  break;
573  case 'h':
574  help++;
575  break;
576  case 'H':
577  main_print_var(opt.arg);
578  LISTS_FREE();
579  return 0;
580  case 'i':
581  if (RZ_STR_ISEMPTY(opt.arg)) {
582  RZ_LOG_ERROR("Cannot open empty script path\n");
583  ret = 1;
584  goto beach;
585  }
586  rz_list_append(files, (void *)opt.arg);
587  break;
588  case 'I':
589  if (RZ_STR_ISEMPTY(opt.arg)) {
590  RZ_LOG_ERROR("Cannot open empty script path\n");
591  ret = 1;
592  goto beach;
593  }
594  rz_list_append(prefiles, (void *)opt.arg);
595  break;
596  case 'k':
597  asmos = opt.arg;
598  break;
599  case 'l':
600  rz_lib_open(r->lib, opt.arg);
601  break;
602  case 'L':
603  do_list_io_plugins = true;
604  break;
605  case 'm':
606  mapaddr = rz_num_math(r->num, opt.arg);
607  s_seek = opt.arg;
608  rz_config_set_i(r->config, "file.offset", mapaddr);
609  break;
610  case 'M':
611  rz_config_set(r->config, "bin.demangle", "false");
612  rz_config_set(r->config, "asm.demangle", "false");
613  break;
614  case 'n':
615  if (load_bin == LOAD_BIN_ALL) { // "-n"
616  load_bin = LOAD_BIN_NOTHING;
617  } else if (load_bin == LOAD_BIN_NOTHING) { // second n => "-nn"
618  load_bin = LOAD_BIN_STRUCTURES_ONLY;
619  }
620  rz_config_set(r->config, "file.info", "false");
621  break;
622  case 'N':
623  if (run_rc) {
624  run_rc = false;
625  } else {
626  load_l = false;
627  }
628  break;
629  case 'p':
630  prj = *opt.arg ? opt.arg : NULL;
631  break;
632  case 'Q':
633  quiet = true;
634  quietLeak = true;
635  break;
636  case 'q':
637  rz_config_set(r->config, "scr.interactive", "false");
638  rz_config_set(r->config, "scr.prompt", "false");
639  rz_config_set(r->config, "cfg.fortunes", "false");
640  if (quiet) {
641  forcequit = true;
642  }
643  quiet = true;
644  break;
645  case 'r':
646  if (RZ_STR_ISEMPTY(opt.arg)) {
647  RZ_LOG_ERROR("Cannot open empty rz-run profile path\n");
648  ret = 1;
649  goto beach;
650  }
651  haveRarunProfile = true;
652  rz_config_set(r->config, "dbg.profile", opt.arg);
653  break;
654  case 'R':
655  customRarunProfile = rz_str_appendf(customRarunProfile, "%s\n", opt.arg);
656  break;
657  case 's':
658  s_seek = opt.arg;
659  break;
660 #if USE_THREADS
661  case 't':
662 #if ALLOW_THREADED
663  threaded = true;
664 #else
665  eprintf("WARNING: -t is temporarily disabled!\n");
666 #endif
667  break;
668 #endif
669  case 'T':
670  compute_hashes = false;
671  break;
672  case 'v':
673  if (quiet) {
674  printf("%s\n", RZ_VERSION);
675  LISTS_FREE();
676  RZ_FREE(debugbackend);
677  free(customRarunProfile);
678  return 0;
679  } else {
681  LISTS_FREE();
682  RZ_FREE(debugbackend);
683  free(customRarunProfile);
684  return rz_main_version_print("rizin");
685  }
686  case 'V':
687  return rz_main_version_verify(1);
688  case 'w':
689  perms |= RZ_PERM_W;
690  break;
691  case 'x':
692  perms &= ~RZ_PERM_X;
693  rz_config_set(r->config, "io.exec", "false");
694  break;
695  default:
696  help++;
697  }
698  }
699  if (noStderr) {
700  if (-1 == close(2)) {
701  RZ_LOG_ERROR("Failed to close stderr");
702  LISTS_FREE();
703  RZ_FREE(debugbackend);
704  return 1;
705  }
706  const char nul[] = RZ_SYS_DEVNULL;
707  int new_stderr = open(nul, O_RDWR);
708  if (-1 == new_stderr) {
709  RZ_LOG_ERROR("Failed to open %s", nul);
710  LISTS_FREE();
711  RZ_FREE(debugbackend);
712  return 1;
713  }
714  if (2 != new_stderr) {
715  if (-1 == dup2(new_stderr, 2)) {
716  RZ_LOG_ERROR("Failed to dup2 stderr");
717  LISTS_FREE();
718  RZ_FREE(debugbackend);
719  return 1;
720  }
721  if (-1 == close(new_stderr)) {
722  RZ_LOG_ERROR("Failed to close %s", nul);
723  LISTS_FREE();
724  RZ_FREE(debugbackend);
725  return 1;
726  }
727  }
728  }
729  {
730  const char *dbg_profile = rz_config_get(r->config, "dbg.profile");
731  if (dbg_profile && *dbg_profile) {
732  char *msg = rz_file_slurp(dbg_profile, NULL);
733  if (msg) {
734  char *program = strstr(msg, "program=");
735  if (program) {
736  program += 8;
737  char *p = 0;
738  p = strstr(program, "\r\n");
739  if (!p) {
740  p = strchr(program, '\n');
741  }
742  if (p) {
743  *p = 0;
744  pfile = strdup(program);
745  }
746  }
747  free(msg);
748  } else {
749  eprintf("Cannot read dbg.profile '%s'\n", dbg_profile);
750  pfile = NULL; // strdup ("");
751  }
752  } else {
753  pfile = argv[opt.ind] ? strdup(argv[opt.ind]) : NULL;
754  }
755  }
756 
757  if (pfile && !*pfile) {
758  RZ_LOG_ERROR("Cannot open empty path\n");
759  ret = 1;
760  goto beach;
761  }
762 
763  if (do_list_io_plugins) {
764  if (rz_config_get_i(r->config, "cfg.plugins")) {
765  rz_core_loadlibs(r, RZ_CORE_LOADLIBS_ALL);
766  }
767  run_commands(r, NULL, prefiles, false, do_analysis);
768  run_commands(r, cmds, files, quiet, do_analysis);
769  if (quietLeak) {
770  exit(0);
771  }
776  rz_cons_flush();
777  LISTS_FREE();
778  free(pfile);
779  RZ_FREE(debugbackend);
780  return 0;
781  }
782 
783  if (help > 0) {
784  LISTS_FREE();
785  free(pfile);
786  RZ_FREE(debugbackend);
787  return main_help(help > 1 ? 2 : 0);
788  }
789  if (customRarunProfile) {
790  char *tfn = rz_file_temp(".rz-run");
791  if (!rz_file_dump(tfn, (const ut8 *)customRarunProfile, strlen(customRarunProfile), 0)) {
792  eprintf("Cannot create %s\n", tfn);
793  } else {
794  haveRarunProfile = true;
795  rz_config_set(r->config, "dbg.profile", tfn);
796  }
797  free(tfn);
798  }
799  if (debug == 1) {
800  if (opt.ind >= argc && !haveRarunProfile) {
801  RZ_LOG_ERROR("Missing argument for -d\n");
802  LISTS_FREE();
803  RZ_FREE(debugbackend);
804  return 1;
805  }
806  const char *src = haveRarunProfile ? pfile : argv[opt.ind];
807  if (src && *src) {
808  char *uri = strdup(src);
809  if (uri) {
810  char *p = strstr(uri, "://");
811  if (p) {
812  *p = 0;
813  // TODO: this must be specified by the io plugin, not hardcoded here
814  if (!strcmp(uri, "winedbg")) {
815  debugbackend = strdup("io");
816  } else {
817  debugbackend = uri;
818  uri = NULL;
819  }
820  debug = 2;
821  }
822  free(uri);
823  }
824  }
825  }
826 
827  tmp = NULL;
828  if (!load_l || (tmp = rz_sys_getenv("RZ_NOPLUGINS"))) {
829  rz_config_set_i(r->config, "cfg.plugins", 0);
830  free(tmp);
831  }
832  if (rz_config_get_i(r->config, "cfg.plugins")) {
833  rz_core_loadlibs(r, RZ_CORE_LOADLIBS_ALL);
834  }
835  run_commands(r, NULL, prefiles, false, do_analysis);
836  rz_list_free(prefiles);
837  prefiles = NULL;
838 
839  rz_bin_force_plugin(r->bin, forcebin);
840 
841  if (prj) {
842  rz_core_project_load_for_cli(r, prj, !pfile);
843  }
844 
845  if (do_connect) {
846  const char *uri = argv[opt.ind];
847  if (opt.ind >= argc) {
848  RZ_LOG_ERROR("Missing URI for -C\n");
849  LISTS_FREE();
850  RZ_FREE(debugbackend);
851  return 1;
852  }
853  if (strstr(uri, "://")) {
854  rz_core_cmdf(r, "R+ %s", uri);
855  } else {
856  argv[opt.ind] = rz_str_newf("http://%s/cmd/", argv[opt.ind]);
857  rz_core_cmdf(r, "R+ %s", argv[opt.ind]);
858  }
859  rz_core_cmd0(r, "R!=");
860  argv[opt.ind] = "-";
861  }
862 
863  switch (zflag) {
864  case 1:
865  rz_config_set(r->config, "bin.strings", "false");
866  break;
867  default:
868  break;
869  }
870  if (zflag > 3) {
871  eprintf("Sleeping now...\n");
872  rz_sys_sleep(zflag);
873  }
874 
875  if (run_rc) {
877  } else {
878  rz_config_set(r->config, "scr.utf8", "false");
879  }
880 
881  if (pfile && rz_file_is_directory(pfile)) {
882  if (debug) {
883  RZ_LOG_ERROR("Error: Cannot debug directories, yet.\n");
884  LISTS_FREE();
885  free(pfile);
886  RZ_FREE(debugbackend);
887  return 1;
888  }
889  if (rz_sys_chdir(argv[opt.ind])) {
890  RZ_LOG_ERROR("[d] Cannot open directory\n");
891  LISTS_FREE();
892  free(pfile);
893  RZ_FREE(debugbackend);
894  return 1;
895  }
896  } else if (argv[opt.ind] && !strcmp(argv[opt.ind], "-")) {
897  int sz;
898 #if __WINDOWS__
899  int result = _setmode(_fileno(stdin), _O_BINARY);
900  if (result == -1) {
901  RZ_LOG_ERROR("Cannot set stdin to binary mode");
902  return 1;
903  }
904 #endif
905  /* stdin/batch mode */
906  char *buf = rz_stdin_slurp(&sz);
907  eprintf("^D\n");
908 #if __WINDOWS__
909  const char *con_dev = "CON";
910 #else
911  const char *con_dev = "/dev/tty";
912 #endif
913  ut64 scr_color = rz_config_get_i(r->config, "scr.color");
914  const char *scr_interactive = rz_config_get(r->config, "scr.interactive");
915  while (rz_cons_free())
916  ;
917  rz_xfreopen(con_dev, "r", stdin);
918  rz_cons_new();
919  rz_config_set_i(r->config, "scr.color", scr_color);
920  rz_config_set(r->config, "scr.interactive", scr_interactive);
921  if (buf && sz > 0) {
922  char *path = rz_str_newf("malloc://%d", sz);
923  fh = rz_core_file_open(r, path, perms, mapaddr);
924  if (!fh) {
925  rz_cons_flush();
926  free(buf);
927  RZ_LOG_ERROR("[=] Cannot open '%s'\n", path);
928  LISTS_FREE();
929  free(path);
930  return 1;
931  }
932  rz_io_map_new(r->io, fh->fd, 7, 0LL, mapaddr,
933  rz_io_fd_size(r->io, fh->fd));
934  rz_io_write_at(r->io, mapaddr, (const ut8 *)buf, sz);
936  free(buf);
937  free(path);
938  // TODO: load rbin thing
939  } else {
940  RZ_LOG_ERROR("Cannot slurp from stdin\n");
941  free(buf);
942  LISTS_FREE();
943  return 1;
944  }
945  } else if (has_file_arg(argc, argv, &opt)) {
946  if (debug) {
947  if (asmbits) {
948  rz_config_set(r->config, "asm.bits", asmbits);
949  }
950  rz_config_set(r->config, "search.in", "dbg.map"); // implicit?
951  rz_config_set(r->config, "cfg.debug", "true");
952  perms = RZ_PERM_RWX;
953  if (opt.ind >= argc) {
954  RZ_LOG_ERROR("No program given to -d\n");
955  LISTS_FREE();
956  RZ_FREE(debugbackend);
957  return 1;
958  }
959  if (debug == 2) {
960  // autodetect backend with -D
961  if (strcmp(debugbackend, "dmp")) {
962  rz_config_set(r->config, "dbg.backend", debugbackend);
963  }
964  if (strcmp(debugbackend, "native")) {
965  if (!haveRarunProfile) {
966  pfile = strdup(argv[opt.ind++]);
967  }
968  // If plugin is winkd we should keep RWX permission to be able to write to the fd
969  if (strcmp(debugbackend, "winkd")) {
970  perms = RZ_PERM_RX; // XXX. should work with rw too
971  }
972  if (!strstr(pfile, "://")) {
973  opt.ind--; // take filename
974  }
975  fh = rz_core_file_open(r, pfile, perms, mapaddr);
976  iod = (r->io && fh) ? rz_io_desc_get(r->io, fh->fd) : NULL;
977  if (!strcmp(debugbackend, "gdb") || !strcmp(debugbackend, "dmp")) {
978  const char *filepath = rz_config_get(r->config, "dbg.exe.path");
979  ut64 addr = baddr;
980  if (addr == UINT64_MAX) {
981  addr = rz_config_get_i(r->config, "bin.baddr");
982  }
983  if (RZ_STR_ISNOTEMPTY(filepath) && rz_file_exists(filepath) && !rz_file_is_directory(filepath)) {
984  char *newpath = rz_file_abspath(filepath);
985  if (newpath) {
986  if (iod) {
987  free(iod->name);
988  iod->name = newpath;
989  }
990  if (addr == UINT64_MAX) {
991  addr = rz_debug_get_baddr(r->dbg, newpath);
992  }
994  }
995  } else if (is_valid_gdb_file(fh) || is_valid_dmp_file(fh)) {
996  filepath = iod->name;
997  if (RZ_STR_ISNOTEMPTY(filepath) && rz_file_exists(filepath) && !rz_file_is_directory(filepath)) {
998  if (addr == UINT64_MAX) {
999  addr = rz_debug_get_baddr(r->dbg, filepath);
1000  }
1001  rz_core_bin_load(r, filepath, addr);
1002  } else if ((filepath = get_file_in_cur_dir(filepath))) {
1003  // Present in local directory
1004  if (iod) {
1005  free(iod->name);
1006  iod->name = (char *)filepath;
1007  }
1008  if (addr == UINT64_MAX) {
1009  addr = rz_debug_get_baddr(r->dbg, filepath);
1010  }
1012  }
1013  }
1014  }
1015  }
1016  } else {
1017  const char *f = (haveRarunProfile && pfile) ? pfile : argv[opt.ind];
1018  is_gdb = (!memcmp(f, "gdb://", RZ_MIN(f ? strlen(f) : 0, 6)));
1019  if (!is_gdb) {
1020  pfile = strdup("dbg://");
1021  }
1022 #if __UNIX__
1023  /* implicit ./ to make unix behave like windows */
1024  if (f) {
1025  char *path, *escaped_path;
1026  if (strchr(f, '/')) {
1027  // f is a path
1028  path = strdup(f);
1029  } else {
1030  // f is a filename
1031  if (rz_file_exists(f)) {
1032  path = rz_str_prepend(strdup(f), "./");
1033  } else {
1034  path = rz_file_path(f);
1035  }
1036  }
1037  escaped_path = rz_str_arg_escape(path);
1038  pfile = rz_str_append(pfile, escaped_path);
1039  file = pfile; // probably leaks
1040  RZ_FREE(escaped_path);
1041  RZ_FREE(path);
1042  }
1043 #else
1044  if (f) {
1045  char *escaped_path = rz_str_arg_escape(f);
1046  pfile = rz_str_append(pfile, escaped_path);
1047  free(escaped_path);
1048  file = pfile; // rz_str_append (file, escaped_path);
1049  }
1050 #endif
1051  opt.ind++;
1052  while (opt.ind < argc) {
1053  char *escaped_arg = rz_str_arg_escape(argv[opt.ind]);
1054  file = rz_str_append(file, " ");
1055  file = rz_str_append(file, escaped_arg);
1056  free(escaped_arg);
1057  opt.ind++;
1058  }
1059  pfile = file;
1060  }
1061  }
1062  if (asmarch) {
1063  rz_config_set(r->config, "asm.arch", asmarch);
1064  }
1065  if (asmbits) {
1066  rz_config_set(r->config, "asm.bits", asmbits);
1067  }
1068  if (asmos) {
1069  rz_config_set(r->config, "asm.os", asmos);
1070  }
1071 
1072  if (!debug || debug == 2) {
1073  const char *dbg_profile = rz_config_get(r->config, "dbg.profile");
1074  if (opt.ind == argc && dbg_profile && *dbg_profile) {
1075  if (RZ_STR_ISEMPTY(pfile)) {
1076  RZ_LOG_ERROR("Missing file to open\n");
1077  ret = 1;
1078  RZ_FREE(debugbackend);
1079  goto beach;
1080  }
1081  fh = rz_core_file_open(r, pfile, perms, mapaddr);
1082  if (fh) {
1083  rz_core_bin_load(r, pfile, baddr);
1084  }
1085  }
1086  if (opt.ind < argc) {
1087  RZ_FREE(pfile);
1088  while (opt.ind < argc) {
1089  pfile = strdup(argv[opt.ind++]);
1090  fh = rz_core_file_open(r, pfile, perms, mapaddr);
1091  if (!fh && perms & RZ_PERM_W) {
1092  perms |= RZ_PERM_CREAT;
1093  fh = rz_core_file_open(r, pfile, perms, mapaddr);
1094  }
1095  if (perms & RZ_PERM_CREAT) {
1096  if (fh) {
1097  rz_config_set_i(r->config, "io.va", false);
1098  } else {
1099  eprintf("rz_io_create: Permission denied.\n");
1100  }
1101  }
1102  if (fh) {
1103  iod = r->io ? rz_io_desc_get(r->io, fh->fd) : NULL;
1104  if (iod && perms & RZ_PERM_X) {
1105  iod->perm |= RZ_PERM_X;
1106  }
1107  if (load_bin == LOAD_BIN_ALL) {
1108  const char *filepath = NULL;
1109  if (debug) {
1110  // XXX: incorrect for PIE binaries
1111  filepath = file ? strstr(file, "://") : NULL;
1112  filepath = filepath ? filepath + 3 : pfile;
1113  }
1114  if (r->file && iod && (iod->fd == r->file->fd) && iod->name) {
1115  filepath = iod->name;
1116  }
1117  /* Load rbin info from rz dbg:// or rz /bin/ls */
1118  /* the baddr should be set manually here */
1119  (void)rz_core_bin_load(r, filepath, baddr);
1120  // check if bin info is loaded and complain if -B was used
1121  RzBinFile *bi = rz_bin_cur(r->bin);
1122  bool haveBinInfo = bi && bi->o && bi->o->info && bi->o->info->type;
1123  if (!haveBinInfo && baddr != UT64_MAX) {
1124  eprintf("Warning: Don't use -B on unknown files. Consider using -m.\n");
1125  }
1126  } else {
1127  rz_io_map_new(r->io, iod->fd, perms, 0LL, mapaddr, rz_io_desc_size(iod));
1128  if (load_bin == LOAD_BIN_STRUCTURES_ONLY) {
1130  }
1131  }
1132  }
1133  }
1134  } else {
1135  if (fh) {
1136  iod = r->io ? rz_io_desc_get(r->io, fh->fd) : NULL;
1137  if (iod) {
1138  perms = iod->perm;
1139  rz_io_map_new(r->io, iod->fd, perms, 0LL, 0LL, rz_io_desc_size(iod));
1140  }
1141  }
1142  }
1143  if (mapaddr) {
1144  if (rz_config_get_i(r->config, "file.info")) {
1145  int fd = rz_io_fd_get_current(r->io);
1146  RzIODesc *desc = rz_io_desc_get(r->io, fd);
1147  if (desc) {
1148  RzBinOptions opt;
1149  opt.sz = 1024 * 1024 * 1;
1150  rz_core_bin_options_init(r, &opt, desc->fd, mapaddr, 0);
1151  RzBinFile *bf = rz_bin_open_io(r->bin, &opt);
1153  }
1154  }
1155  }
1156  } else {
1157  RzCoreFile *f = rz_core_file_open(r, pfile, perms, mapaddr);
1158  if (f) {
1159  fh = f;
1160  }
1161  if (fh) {
1162  rz_debug_use(r->dbg, is_gdb ? "gdb" : debugbackend);
1163  }
1164  /* load symbols when doing rz -d ls */
1165  // NOTE: the baddr is redefined to support PIE/ASLR
1166  baddr = rz_debug_get_baddr(r->dbg, pfile);
1167 
1168  if (baddr != UT64_MAX && baddr != 0 && r->dbg->verbose) {
1169  eprintf("bin.baddr 0x%08" PFMT64x "\n", baddr);
1170  }
1171  if (load_bin == LOAD_BIN_ALL) {
1172  if (baddr && baddr != UT64_MAX && r->dbg->verbose) {
1173  eprintf("Using 0x%" PFMT64x "\n", baddr);
1174  }
1175  if (rz_core_bin_load(r, pfile, baddr)) {
1176  RzBinObject *obj = rz_bin_cur_object(r->bin);
1177  if (obj && obj->info) {
1178  if (r->dbg->verbose) {
1179  eprintf("asm.bits %d\n", obj->info->bits);
1180  }
1181 #if __linux__ && __GNU_LIBRARY__ && __GLIBC__ && __GLIBC_MINOR__ && __x86_64__
1182  ut64 bitness = rz_config_get_i(r->config, "asm.bits");
1183  if (bitness == 32) {
1184  eprintf("glibc.fc_offset = 0x00148\n");
1185  rz_config_set_i(r->config, "dbg.glibc.fc_offset", 0x00148);
1186  }
1187 #endif
1188  }
1189  }
1190  }
1192  // Set Thumb Mode if necessary
1193  RzRegItem *thumb_reg = rz_reg_get(r->dbg->reg, "thumb", RZ_REG_TYPE_ANY);
1194  if (thumb_reg && rz_reg_get_value(r->dbg->reg, thumb_reg)) {
1195  rz_config_set_i(r->config, "asm.bits", 16);
1196  }
1197  rz_cons_reset();
1198  }
1199  if (!pfile) {
1200  pfile = file;
1201  }
1202  if (!fh && !prj) {
1203  if (pfile && *pfile) {
1204  rz_cons_flush();
1205  if (perms & RZ_PERM_W) {
1206  RZ_LOG_ERROR("[w] Cannot open '%s' for writing.\n", pfile);
1207  } else {
1208  RZ_LOG_ERROR("[r] Cannot open '%s'\n", pfile);
1209  }
1210  } else {
1211  RZ_LOG_ERROR("Missing file to open\n");
1212  }
1213  ret = 1;
1214  goto beach;
1215  }
1216  if (!r->file) { // no given file
1217  ret = 1;
1218  goto beach;
1219  }
1220  if (r->bin->cur && r->bin->cur->o && r->bin->cur->o->info && r->bin->cur->o->info->rclass && !strcmp("fs", r->bin->cur->o->info->rclass)) {
1221  const char *fstype = r->bin->cur->o->info->bclass;
1222  rz_core_cmdf(r, "m /root %s @ 0", fstype);
1223  }
1224  rz_core_cmd0(r, "R!"); // initalize io subsystem
1225  iod = r->io && fh ? rz_io_desc_get(r->io, fh->fd) : NULL;
1226  if (mapaddr) {
1227  rz_core_seek(r, mapaddr, true);
1228  }
1229  rz_list_foreach (evals, iter, cmdn) {
1230  rz_config_eval(r->config, cmdn);
1231  rz_cons_flush();
1232  }
1233  if (asmarch) {
1234  rz_config_set(r->config, "asm.arch", asmarch);
1235  }
1236  if (asmbits) {
1237  rz_config_set(r->config, "asm.bits", asmbits);
1238  }
1239  if (asmos) {
1240  rz_config_set(r->config, "asm.os", asmos);
1241  }
1242 
1243  debug = r->file && iod && (r->file->fd == iod->fd) && iod->plugin &&
1244  (iod->plugin->isdbg || (debug == 2 && !strcmp(iod->plugin->name, "dmp")));
1245  if (debug) {
1246  rz_core_setup_debugger(r, debugbackend, baddr == UT64_MAX);
1247  }
1248  RZ_FREE(debugbackend);
1249  RzBinObject *o = rz_bin_cur_object(r->bin);
1250  if (!debug && o && !o->regstate) {
1251  RzFlagItem *fi = rz_flag_get(r->flags, "entry0");
1252  if (fi) {
1253  rz_core_seek(r, fi->offset, true);
1254  } else {
1255  if (o) {
1257  RzListIter *iter;
1258  RzBinSection *s;
1259  rz_list_foreach (sections, iter, s) {
1260  if (s->perm & RZ_PERM_X) {
1261  ut64 addr = s->vaddr ? s->vaddr : s->paddr;
1262  rz_core_seek(r, addr, true);
1263  break;
1264  }
1265  }
1266  }
1267  }
1268  }
1269  if (o && o->info && compute_hashes) {
1270  // TODO: recall with limit=0 ?
1271  ut64 limit = rz_config_get_i(r->config, "bin.hashlimit");
1272  RzBinFile *bf = r->bin->cur;
1273  if (bf) {
1274  RzList *old_hashes = rz_bin_file_set_hashes(r->bin, rz_bin_file_compute_hashes(r->bin, bf, limit));
1275  rz_list_free(old_hashes);
1276  }
1277  }
1278  if (s_seek) {
1279  seek = rz_num_math(r->num, s_seek);
1280  if (seek != UT64_MAX) {
1281  rz_core_seek(r, seek, true);
1282  }
1283  }
1284 
1285  if (fullfile) {
1287  }
1288 
1289  rz_core_seek(r, r->offset, true); // read current block
1290 
1291  /* check if file.path has changed */
1292  if (iod && !strstr(iod->uri, "://")) {
1293  const char *npath;
1294  char *path = strdup(rz_config_get(r->config, "file.path"));
1295  iod = r->io ? rz_io_desc_get(r->io, fh->fd) : NULL;
1296  npath = rz_config_get(r->config, "file.path");
1297  if (!quiet && path && *path && npath && strcmp(path, npath)) {
1298  eprintf("WARNING: file.path change: %s => %s\n", path, npath);
1299  }
1300  free(path);
1301  }
1302 
1303  rz_list_foreach (evals, iter, cmdn) {
1304  rz_config_eval(r->config, cmdn);
1305  rz_cons_flush();
1306  }
1307 
1308  // no flagspace selected by default the beginning
1309  rz_flag_space_set(r->flags, NULL);
1310  /* load <file>.rz */
1311  {
1312  char *f = rz_str_newf("%s.rz", pfile);
1313  const char *uri_splitter = strstr(f, "://");
1314  const char *path = uri_splitter ? uri_splitter + 3 : f;
1315  if (rz_file_exists(path)) {
1316  // TODO: should 'q' unset the interactive bit?
1317  bool isInteractive = rz_cons_is_interactive();
1318  if (isInteractive && rz_cons_yesno('n', "Do you want to run the '%s' script? (y/N) ", path)) {
1320  }
1321  }
1322  free(f);
1323  }
1324  } else {
1326 
1327  rz_list_foreach (evals, iter, cmdn) {
1328  rz_config_eval(r->config, cmdn);
1329  rz_cons_flush();
1330  }
1331  if (asmarch) {
1332  rz_config_set(r->config, "asm.arch", asmarch);
1333  }
1334  if (asmbits) {
1335  rz_config_set(r->config, "asm.bits", asmbits);
1336  }
1337  if (asmos) {
1338  rz_config_set(r->config, "asm.os", asmos);
1339  }
1340  }
1341  {
1342  char *global_rc = rz_path_system_rc();
1343  if (rz_file_exists(global_rc)) {
1344  (void)rz_core_run_script(r, global_rc);
1345  }
1346  free(global_rc);
1347  }
1348 
1349  if (do_analysis > 0) {
1350  switch (do_analysis) {
1351  case 1: rz_core_cmd0(r, "aa"); break;
1352  case 2: rz_core_cmd0(r, "aaa"); break;
1353  case 3: rz_core_cmd0(r, "aaaa"); break;
1354  default: rz_core_cmd0(r, "aaaaa"); break;
1355  }
1356  rz_cons_flush();
1357  }
1358 #if UNCOLORIZE_NONTTY
1359 #if __UNIX__
1360  if (!rz_cons_isatty()) {
1361  rz_config_set_i(r->config, "scr.color", COLOR_MODE_DISABLED);
1362  }
1363 #endif
1364 #endif
1365  if (fullfile) {
1367  }
1368  if (perms & RZ_PERM_W) {
1369  RzPVector *maps = rz_io_maps(r->io);
1370  void **it;
1371  rz_pvector_foreach (maps, it) {
1372  RzIOMap *map = *it;
1373  map->perm |= RZ_PERM_W;
1374  }
1375  }
1376  ret = run_commands(r, cmds, files, quiet, do_analysis);
1377  rz_list_free(cmds);
1378  rz_list_free(evals);
1380  cmds = evals = files = NULL;
1381  if (forcequit) {
1382  ret = 1;
1383  }
1384  if (ret) {
1385  ret = 0;
1386  goto beach;
1387  }
1388  if (rz_config_get_i(r->config, "scr.prompt")) {
1389  if (run_rc && rz_config_get_i(r->config, "cfg.fortunes")) {
1391  rz_cons_flush();
1392  }
1393  }
1394  if (quiet) {
1395  rz_config_set(r->config, "scr.wheel", "false");
1396  rz_config_set(r->config, "scr.interactive", "false");
1397  rz_config_set(r->config, "scr.prompt", "false");
1398  }
1399  r->num->value = 0;
1400  if (zerosep) {
1401  rz_cons_zero();
1402  }
1403  if (seek != UT64_MAX) {
1404  rz_core_seek(r, seek, true);
1405  }
1406 
1407  // no flagspace selected by default the beginning
1408  rz_flag_space_set(r->flags, NULL);
1409  for (;;) {
1411  ret = r->num->value;
1412  debug = rz_config_get_i(r->config, "cfg.debug");
1413  if (ret != -1 && rz_cons_is_interactive()) {
1414  char *question;
1415  bool no_question_debug = ret & 1;
1416  bool no_question_save = (ret & 2) >> 1;
1417  bool y_kill_debug = (ret & 4) >> 2;
1418  bool y_save_project = (ret & 8) >> 3;
1419 
1420  if (rz_core_task_running_tasks_count(&r->tasks) > 0) {
1421  if (rz_cons_yesno('y', "There are running background tasks. Do you want to kill them? (Y/n)")) {
1422  rz_core_task_break_all(&r->tasks);
1423  rz_core_task_join(&r->tasks, r->tasks.main_task, -1);
1424  } else {
1425  continue;
1426  }
1427  }
1428 
1429  if (debug) {
1430  if (no_question_debug) {
1431  if (rz_config_get_i(r->config, "dbg.exitkills") && y_kill_debug) {
1432  rz_debug_kill(r->dbg, r->dbg->pid, r->dbg->tid, 9); // KILL
1433  }
1434  } else {
1435  if (rz_cons_yesno('y', "Do you want to quit? (Y/n)")) {
1436  if (rz_config_get_i(r->config, "dbg.exitkills") &&
1437  rz_debug_can_kill(r->dbg) &&
1438  rz_cons_yesno('y', "Do you want to kill the process? (Y/n)")) {
1439  rz_debug_kill(r->dbg, r->dbg->pid, r->dbg->tid, 9); // KILL
1440  }
1441  // Even if killed above, we must still detach, otherwise
1442  // there will be a zombie on macOS!
1443  rz_debug_detach(r->dbg, r->dbg->pid);
1444  } else {
1445  continue;
1446  }
1447  }
1448  }
1449 
1450  prj = rz_config_get(r->config, "prj.file");
1451  bool compress = rz_config_get_b(r->config, "prj.compress");
1453  if (no_question_save) {
1454  if (prj && *prj && y_save_project) {
1455  prj_err = rz_project_save_file(r, prj, compress);
1456  }
1457  } else {
1458  question = rz_str_newf("Do you want to save the '%s' project? (Y/n)", prj);
1459  if (prj && *prj && rz_cons_yesno('y', "%s", question)) {
1460  prj_err = rz_project_save_file(r, prj, compress);
1461  }
1462  free(question);
1463  }
1464  if (prj_err != RZ_PROJECT_ERR_SUCCESS) {
1465  RZ_LOG_ERROR("Failed to save project: %s\n", rz_project_err_message(prj_err));
1466  continue;
1467  }
1468 
1469  if (rz_config_get_i(r->config, "scr.confirmquit")) {
1470  if (!rz_cons_yesno('n', "Do you want to quit? (Y/n)")) {
1471  continue;
1472  }
1473  }
1474  } else {
1475  // rz_core_project_save (r, prj);
1476  if (debug && rz_config_get_i(r->config, "dbg.exitkills")) {
1477  rz_debug_kill(r->dbg, 0, false, 9); // KILL
1478  }
1479  }
1480  break;
1481  }
1482 
1483  if (mustSaveHistory(r->config)) {
1484  char *history = rz_path_home_history();
1485  rz_line_hist_save(history);
1486  free(history);
1487  }
1488 
1489  /* capture return value */
1490  ret = r->num->value;
1491 beach:
1492  if (quietLeak) {
1493  exit(ret);
1494  return ret;
1495  }
1496 
1497  rz_core_task_sync_end(&r->tasks);
1498 
1499  // not really needed, cause rz_core_fini will close the file
1500  // and this fh may be come stale during the command
1501  // execution.
1502  // rz_core_file_close (r, fh);
1503  rz_core_free(r);
1504  rz_cons_set_raw(0);
1505  rz_cons_free();
1506  LISTS_FREE();
1507  free(debugbackend);
1508  RZ_FREE(pfile);
1509  return ret;
1510 }
lzma_index * src
Definition: index.h:567
RZ_API RZ_OWN RzList * rz_bin_file_set_hashes(RzBin *bin, RZ_OWN RzList *new_hashes)
Set file_hashes on current RzBinInfo.
Definition: bfile.c:488
RZ_API RZ_OWN RzList * rz_bin_file_compute_hashes(RzBin *bin, RzBinFile *bf, ut64 limit)
Definition: bfile.c:411
RZ_API RzBinObject * rz_bin_cur_object(RzBin *bin)
Definition: bin.c:900
RZ_API RzBinFile * rz_bin_open_io(RzBin *bin, RzBinOptions *opt)
Definition: bin.c:283
RZ_API void rz_bin_force_plugin(RzBin *bin, const char *name)
Definition: bin.c:906
RZ_API RzBinFile * rz_bin_cur(RzBin *bin)
Definition: bin.c:895
RZ_DEPRECATE RZ_API RZ_BORROW RzList * rz_bin_get_sections(RZ_NONNULL RzBin *bin)
Definition: bin.c:597
static ut64 baddr(RzBinFile *bf)
Definition: bin_any.c:58
static RzList * maps(RzBinFile *bf)
Definition: bin_bf.c:116
RzList * sections(RzBinFile *bf)
Definition: bin_ne.c:110
const char * desc
Definition: bin_vsf.c:19
FILE * fh
Definition: cabinfo.c:52
RZ_API void rz_core_bin_options_init(RzCore *core, RZ_OUT RzBinOptions *opts, int fd, ut64 baseaddr, ut64 loadaddr)
Definition: cbin.c:87
RZ_API bool rz_core_bin_load_structs(RZ_NONNULL RzCore *core, RZ_NONNULL const char *file)
Definition: cbin.c:236
RZ_API bool rz_core_bin_apply_all_info(RzCore *r, RzBinFile *binfile)
Definition: cbin.c:311
RZ_API void rz_core_parse_rizinrc(RzCore *r)
Definition: cconfig.c:3704
RZ_API void rz_core_debug_map_update_flags(RzCore *core)
Definition: cdebug.c:462
RZ_API RzCmdStatus rz_core_debug_plugins_print(RzCore *core, RzCmdStateOutput *state)
Definition: cdebug.c:329
RZ_API RZ_BORROW RzCoreFile * rz_core_file_open(RZ_NONNULL RzCore *r, RZ_NONNULL const char *file, int flags, ut64 loadaddr)
Tries to open the file as is, otherwise tries as is a compilation of files.
Definition: cfile.c:1182
RZ_API bool rz_core_bin_load(RZ_NONNULL RzCore *r, RZ_NULLABLE const char *filenameuri, ut64 baddr)
Definition: cfile.c:942
RZ_API int rz_core_setup_debugger(RzCore *r, const char *debugbackend, bool attach)
Definition: cio.c:7
RZ_API int rz_core_block_read(RzCore *core)
Definition: cio.c:243
RZ_API RzCmdStatus rz_core_io_plugins_print(RzIO *io, RzCmdStateOutput *state)
Print the registered IO plugins according to state.
Definition: cio.c:481
RZ_API int rz_core_cmd0(RzCore *core, const char *cmd)
Definition: cmd.c:5428
RZ_API int rz_core_cmd_file(RzCore *core, const char *file)
Definition: cmd.c:5341
RZ_API bool rz_core_run_script(RzCore *core, RZ_NONNULL const char *file)
Definition: cmd.c:457
RZ_API int rz_core_cmdf(RzCore *core, const char *fmt,...)
Definition: cmd.c:5413
RZ_API bool rz_cmd_state_output_init(RZ_NONNULL RzCmdStateOutput *state, RzOutputMode mode)
Initialize a RzCmdStateOutput structure and its inner fields based on the provided mode.
Definition: cmd_api.c:2634
RZ_API void rz_cmd_state_output_fini(RZ_NONNULL RzCmdStateOutput *state)
Clear the inner fields of RzCmdStateOutput structure, but do not free it.
Definition: cmd_api.c:2603
RZ_API void rz_cmd_state_output_print(RZ_NONNULL RzCmdStateOutput *state)
Print the output accumulated in state to RzCons, if necessary.
Definition: cmd_api.c:2668
RZ_API bool rz_core_project_load_for_cli(RzCore *core, const char *file, bool load_bin_io)
Definition: cmd_project.c:6
int ZEXPORT compress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)
Definition: compress.c:68
RZ_API ut64 rz_config_get_i(RzConfig *cfg, RZ_NONNULL const char *name)
Definition: config.c:119
RZ_API bool rz_config_get_b(RzConfig *cfg, RZ_NONNULL const char *name)
Definition: config.c:142
RZ_API bool rz_config_eval(RZ_NONNULL RzConfig *cfg, RZ_NONNULL const char *str)
Sets the configuration variable and its value passed as argument.
Definition: config.c:575
RZ_API RzConfigNode * rz_config_set(RzConfig *cfg, RZ_NONNULL const char *name, const char *value)
Definition: config.c:267
RZ_API RzConfigNode * rz_config_set_i(RzConfig *cfg, RZ_NONNULL const char *name, const ut64 i)
Definition: config.c:419
RZ_API RZ_BORROW const char * rz_config_get(RzConfig *cfg, RZ_NONNULL const char *name)
Definition: config.c:75
RZ_API void rz_cons_zero(void)
Definition: cons.c:1749
RZ_API void rz_cons_set_raw(bool is_raw)
Definition: cons.c:1617
RZ_API RzCons * rz_cons_new(void)
Definition: cons.c:589
RZ_API bool rz_cons_is_interactive(void)
Definition: cons.c:365
RZ_API bool rz_cons_isatty(void)
Definition: cons.c:1341
RZ_API void rz_cons_flush(void)
Definition: cons.c:959
RZ_API void rz_cons_reset(void)
Definition: cons.c:804
RZ_API RzCons * rz_cons_free(void)
Definition: cons.c:658
#define r
Definition: crypto_rc6.c:12
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
static static fork const void static count static fd const char const char static newpath const char static path const char path
Definition: sflib.h:35
static static fork const void static count close
Definition: sflib.h:33
RZ_API int rz_line_hist_save(RZ_NONNULL const char *path)
Save the history of commands executed until now to file path.
Definition: dietline.c:497
size_t map(int syms, int left, int len)
Definition: enough.c:237
checking print the parsed form of the magic use in n conjunction with m to debug a new magic file n before installing it n output MIME type special files
Definition: file_opts.h:46
RZ_API RzFlagItem * rz_flag_get(RzFlag *f, const char *name)
Definition: flag.c:310
RZ_API void rz_core_fortune_print_random(RzCore *core)
Definition: fortune.c:60
RZ_API bool rz_cons_yesno(int def, const char *fmt,...)
Definition: input.c:666
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
void * p
Definition: libc.cpp:67
RZ_API void rz_core_prompt_loop(RzCore *r)
Definition: core.c:2688
RZ_API bool rz_core_block_size(RzCore *core, ut32 bsize)
Definition: core.c:2842
RZ_API void rz_core_free(RzCore *c)
Definition: core.c:2683
RZ_API RzCore * rz_core_new(void)
Definition: core.c:866
RZ_API bool rz_debug_can_kill(RzDebug *dbg)
check whether rz_debug_kill() will not definitely fail (for example because kill is unimplemented by ...
Definition: debug.c:1594
RZ_API int rz_debug_kill(RzDebug *dbg, int pid, int tid, int sig)
Definition: debug.c:1598
RZ_API int rz_debug_detach(RzDebug *dbg, int pid)
Definition: debug.c:583
RZ_API ut64 rz_debug_get_baddr(RzDebug *dbg, const char *file)
Definition: debug.c:1682
static bool mustSaveHistory(RzConfig *c)
Definition: rizin.c:320
static bool has_file_arg(int argc, const char **argv, RzGetopt *opt)
Definition: rizin.c:366
#define LISTS_FREE()
RZ_API int rz_main_rizin(int argc, const char **argv)
Definition: rizin.c:370
static bool is_valid_gdb_file(RzCoreFile *fh)
Definition: rizin.c:14
static void set_color_default(RzCore *r)
Definition: rizin.c:331
static int main_help(int line)
Definition: rizin.c:92
static int rz_main_version_verify(int show)
Definition: rizin.c:32
static bool is_valid_dmp_file(RzCoreFile *fh)
Definition: rizin.c:19
static char * get_file_in_cur_dir(const char *filepath)
Definition: rizin.c:24
static bool run_commands(RzCore *r, RzList *cmds, RzList *files, bool quiet, int do_analysis)
Definition: rizin.c:279
static int main_print_var(const char *var_name)
Definition: rizin.c:216
RZ_API RZ_OWN RzList * rz_list_new(void)
Returns a new initialized RzList pointer (free method is not initialized)
Definition: list.c:235
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
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
RZ_API int rz_core_loadlibs(RzCore *core, int where)
Definition: libs.c:119
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
static static fork const void static count static fd const char static mode const char static pathname const char static path const char static dev const char static group static getpid static getuid void void static data static pause const char static mode static sync const char const char static newpath const char static pathname unsigned long static filedes void static end_data_segment static handler static getegid char static len static pgid const char static path dup2
Definition: sflib.h:94
static uint32_t const uint8_t uint32_t uint32_t limit
Definition: memcmplen.h:45
RZ_API bool rz_debug_use(RzDebug *dbg, const char *str)
Definition: plugin.c:17
RZ_API RZ_NONNULL const char * rz_project_err_message(RzProjectErr err)
Definition: project.c:11
RZ_API RzProjectErr rz_project_save_file(RzCore *core, const char *file, bool compress)
Definition: project.c:42
RZ_API RzRegItem * rz_reg_get(RzReg *reg, const char *name, int type)
Definition: reg.c:344
#define eprintf(x, y...)
Definition: rlcc.c:7
static RzSocket * s
Definition: rtr.c:28
RZ_API ut64 rz_reg_get_value(RzReg *reg, RzRegItem *item)
Definition: rvalue.c:114
@ COLOR_MODE_DISABLED
Definition: rz_cons.h:442
RZ_API char * rz_file_temp(const char *prefix)
Definition: file.c:1048
RZ_API bool rz_file_is_directory(const char *str)
Definition: file.c:167
RZ_API bool rz_file_exists(const char *str)
Definition: file.c:192
RZ_API char * rz_stdin_slurp(int *sz)
Definition: file.c:408
RZ_API char * rz_file_abspath(const char *file)
Definition: file.c:267
RZ_API RZ_OWN char * rz_file_slurp(const char *str, RZ_NULLABLE size_t *usz)
Definition: file.c:454
RZ_API bool rz_file_dump(const char *file, const ut8 *buf, int len, bool append)
Definition: file.c:838
RZ_API char * rz_file_path(const char *bin)
Definition: file.c:354
RZ_API void rz_getopt_init(RzGetopt *go, int argc, const char **argv, const char *ostr)
Definition: getopt.c:17
RZ_API int rz_getopt_next(RzGetopt *opt)
Definition: getopt.c:29
RZ_API ut64 rz_io_fd_size(RzIO *io, int fd)
Definition: io_fd.c:42
RZ_API ut64 rz_io_desc_size(RzIODesc *desc)
Definition: io_desc.c:224
RZ_API RzIODesc * rz_io_desc_get(RzIO *io, int fd)
Definition: io_desc.c:73
RZ_API RZ_BORROW RzPVector * rz_io_maps(RzIO *io)
Returns the pointer to vector containing maps list.
Definition: io_map.c:435
RZ_API int rz_io_fd_get_current(RzIO *io)
Definition: io_fd.c:135
RZ_API bool rz_io_write_at(RzIO *io, ut64 addr, const ut8 *buf, int len)
Definition: io.c:358
RZ_API RzIOMap * rz_io_map_new(RzIO *io, int fd, int flags, ut64 delta, ut64 addr, ut64 size)
Definition: io_map.c:50
RZ_API int rz_lib_open(RzLib *lib, const char *file)
Definition: lib.c:281
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
RZ_API int rz_main_version_print(const char *program)
Definition: main.c:49
RZ_API int rz_main_rz_find(int argc, const char **argv)
Definition: rz-find.c:474
RZ_API int rz_main_rz_diff(int argc, const char **argv)
Definition: rz-diff.c:2254
RZ_API int rz_main_rz_ax(int argc, const char **argv)
Definition: rz-ax.c:721
RZ_API int rz_main_rz_asm(int argc, const char **argv)
RZ_API int rz_main_rz_gg(int argc, const char **argv)
Definition: rz-gg.c:116
RZ_API int rz_main_rz_bin(int argc, const char **argv)
Definition: rz-bin.c:681
RZ_API ut64 rz_num_math(RzNum *num, const char *str)
Definition: unum.c:456
RZ_API RZ_OWN char * rz_path_system_rc(void)
Return the system path of the global rizinrc file.
Definition: path.c:169
RZ_API RZ_OWN char * rz_path_home_history(void)
Return the path for the command history file.
Definition: path.c:212
@ RZ_PROJECT_ERR_SUCCESS
Definition: rz_project.h:20
enum rz_project_err RzProjectErr
@ RZ_REG_TYPE_ANY
Definition: rz_reg.h:35
RZ_API char * rz_run_get_environ_profile(char **env)
Definition: run.c:1329
#define RZ_STR_ISNOTEMPTY(x)
Definition: rz_str.h:68
RZ_API char * rz_str_appendf(char *ptr, const char *fmt,...) RZ_PRINTF_CHECK(2
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
RZ_API char * rz_str_append(char *ptr, const char *string)
Definition: str.c:1063
#define RZ_STR_ISEMPTY(x)
Definition: rz_str.h:67
RZ_API char * rz_str_prepend(char *ptr, const char *string)
Definition: str.c:1027
RZ_API char * rz_str_arg_escape(const char *arg)
Definition: str.c:2413
RZ_API char * rz_sys_getenv(const char *key)
Get the value of an environment variable named key or NULL if none exists.
Definition: sys.c:483
RZ_API void rz_sys_env_init(void)
Definition: sys.c:1108
RZ_API bool rz_sys_getenv_asbool(const char *key)
Return true if the environment variable has the value 1, false otherwise.
Definition: sys.c:511
#define RZ_SYS_DEVNULL
Definition: rz_sys.h:10
RZ_API bool rz_sys_chdir(RZ_NONNULL const char *s)
Change current directory to s, taking care of home expansion ~.
Definition: sys.c:532
RZ_API int rz_sys_crash_handler(const char *cmd)
Definition: sys.c:457
RZ_API int rz_sys_sleep(int secs)
Sleep for secs seconds.
Definition: sys.c:300
RZ_API char ** rz_sys_get_environ(void)
Definition: sys.c:1115
#define RZ_PERM_CREAT
Definition: rz_types.h:103
#define RZ_PERM_W
Definition: rz_types.h:94
#define rz_xfreopen(pathname, mode, stream)
Definition: rz_types.h:644
#define RZ_PERM_X
Definition: rz_types.h:95
#define RZ_PERM_RX
Definition: rz_types.h:97
@ RZ_OUTPUT_MODE_QUIET
Definition: rz_types.h:42
@ RZ_OUTPUT_MODE_STANDARD
Definition: rz_types.h:39
#define RZ_PERM_RWX
Definition: rz_types.h:98
#define RZ_FREE(x)
Definition: rz_types.h:369
#define PFMT64x
Definition: rz_types.h:393
#define RZ_MIN(x, y)
#define UT64_MAX
Definition: rz_types_base.h:86
#define rz_pvector_foreach(vec, it)
Definition: rz_vector.h:334
#define RZ_VERSION
Definition: rz_version.h:8
RZ_API bool rz_core_seek(RzCore *core, ut64 addr, bool rb)
Seek to addr.
Definition: seek.c:116
static struct sockaddr static addrlen static backlog const void msg
Definition: sfsocketcall.h:119
#define O_RDWR
Definition: sftypes.h:488
int sigset_t
Definition: sftypes.h:63
#define f(i)
Definition: sha256.c:46
#define c(i)
Definition: sha256.c:43
#define UINT64_MAX
Definition: gzappend.c:170
XX curplugin == o->plugin.
Definition: rz_bin.h:298
RzBinObject * o
Definition: rz_bin.h:305
char * type
Definition: rz_bin.h:211
RzBinInfo * info
Definition: rz_bin.h:287
char * regstate
Definition: rz_bin.h:286
Represent the output state of a command handler.
Definition: rz_cmd.h:91
ut64 offset
Definition: rz_flag.h:38
const char ** argv
Definition: rz_getopt.h:18
const char * arg
Definition: rz_getopt.h:15
char * name
Definition: rz_io.h:99
int fd
Definition: rz_io.h:96
char * uri
Definition: rz_io.h:98
struct rz_io_plugin_t * plugin
Definition: rz_io.h:103
int perm
Definition: rz_io.h:97
const char * name
Definition: rz_io.h:115
bool isdbg
Definition: rz_io.h:124
Definition: dis.h:43
static char ** env
Definition: sys.c:32
RZ_API void rz_core_task_join(RzCoreTaskScheduler *scheduler, RzCoreTask *current, int id)
Definition: task.c:96
RZ_API void rz_core_task_break_all(RzCoreTaskScheduler *scheduler)
Definition: task.c:495
RZ_API void rz_core_task_sync_end(RzCoreTaskScheduler *scheduler)
Definition: task.c:432
RZ_API int rz_core_task_running_tasks_count(RzCoreTaskScheduler *scheduler)
Definition: task.c:71
RZ_API void rz_core_task_sync_begin(RzCoreTaskScheduler *scheduler)
Definition: task.c:421
static int debug
Definition: visual.c:21
#define SIGWINCH
Definition: win.h:88
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static const z80_opcode fd[]
Definition: z80_tab.h:997
static int file
Definition: z80asm.c:58
static int addr
Definition: z80asm.c:58
static int seek(char *argv[])

References addr, rz_getopt_t::arg, rz_getopt_t::argv, argv, baddr(), rz_bin_info_t::bits, c, close, COLOR_MODE_DISABLED, compress(), debug, desc, dup2, env, eprintf, test-lz4-list::exit, f, fd, rz_io_desc_t::fd, fh, file, files, free(), get_file_in_cur_dir(), has_file_arg(), cmd_descs_generate::help, rz_getopt_t::ind, rz_bin_object_t::info, is_valid_dmp_file(), is_valid_gdb_file(), rz_io_plugin_t::isdbg, limit, LISTS_FREE, main_help(), main_print_var(), map(), maps(), msg, mustSaveHistory(), rz_io_desc_t::name, rz_io_plugin_t::name, NULL, rz_bin_file_t::o, O_RDWR, rz_flag_item_t::offset, rz_getopt_t::opt, p, path, rz_io_desc_t::perm, PFMT64x, rz_io_desc_t::plugin, printf(), r, rz_bin_object_t::regstate, run_commands(), rz_bin_cur(), rz_bin_cur_object(), rz_bin_file_compute_hashes(), rz_bin_file_set_hashes(), rz_bin_force_plugin(), rz_bin_get_sections(), rz_bin_open_io(), rz_cmd_state_output_fini(), rz_cmd_state_output_init(), rz_cmd_state_output_print(), rz_config_eval(), rz_config_get(), rz_config_get_b(), rz_config_get_i(), rz_config_set(), rz_config_set_i(), rz_cons_flush(), rz_cons_free(), rz_cons_is_interactive(), rz_cons_isatty(), rz_cons_new(), rz_cons_reset(), rz_cons_set_raw(), rz_cons_yesno(), rz_cons_zero(), rz_core_bin_apply_all_info(), rz_core_bin_load(), rz_core_bin_load_structs(), rz_core_bin_options_init(), rz_core_block_read(), rz_core_block_size(), rz_core_cmd0(), rz_core_cmd_file(), rz_core_cmdf(), rz_core_debug_map_update_flags(), rz_core_debug_plugins_print(), rz_core_file_open(), rz_core_fortune_print_random(), rz_core_free(), rz_core_io_plugins_print(), rz_core_loadlibs(), rz_core_new(), rz_core_parse_rizinrc(), rz_core_project_load_for_cli(), rz_core_prompt_loop(), rz_core_run_script(), rz_core_seek(), rz_core_setup_debugger(), rz_core_task_break_all(), rz_core_task_join(), rz_core_task_running_tasks_count(), rz_core_task_sync_begin(), rz_core_task_sync_end(), rz_debug_can_kill(), rz_debug_detach(), rz_debug_get_baddr(), rz_debug_kill(), rz_debug_use(), rz_file_abspath(), rz_file_dump(), rz_file_exists(), rz_file_is_directory(), rz_file_path(), rz_file_slurp(), rz_file_temp(), rz_flag_get(), RZ_FREE, rz_getopt_init(), rz_getopt_next(), rz_io_desc_get(), rz_io_desc_size(), rz_io_fd_get_current(), rz_io_fd_size(), rz_io_map_new(), rz_io_maps(), rz_io_write_at(), rz_lib_open(), rz_line_hist_save(), rz_list_append(), rz_list_free(), rz_list_new(), RZ_LOG_ERROR, rz_main_rizin(), rz_main_rz_asm(), rz_main_rz_ax(), rz_main_rz_bin(), rz_main_rz_diff(), rz_main_rz_find(), rz_main_rz_gg(), rz_main_version_print(), rz_main_version_verify(), RZ_MIN, rz_num_math(), RZ_OUTPUT_MODE_QUIET, RZ_OUTPUT_MODE_STANDARD, rz_path_home_history(), rz_path_system_rc(), RZ_PERM_CREAT, RZ_PERM_RWX, RZ_PERM_RX, RZ_PERM_W, RZ_PERM_X, rz_project_err_message(), RZ_PROJECT_ERR_SUCCESS, rz_project_save_file(), rz_pvector_foreach, rz_reg_get(), rz_reg_get_value(), RZ_REG_TYPE_ANY, rz_run_get_environ_profile(), rz_stdin_slurp(), rz_str_append(), rz_str_appendf(), rz_str_arg_escape(), RZ_STR_ISEMPTY, RZ_STR_ISNOTEMPTY, rz_str_newf(), rz_str_prepend(), rz_sys_chdir(), rz_sys_crash_handler(), RZ_SYS_DEVNULL, rz_sys_env_init(), rz_sys_get_environ(), rz_sys_getenv(), rz_sys_getenv_asbool(), rz_sys_sleep(), RZ_VERSION, rz_xfreopen, s, sections(), seek(), set_color_default(), SIGWINCH, src, strdup(), autogen_x86imm::tmp, rz_bin_info_t::type, UINT64_MAX, rz_io_desc_t::uri, ut64(), and UT64_MAX.

Referenced by MAIN_NAME(), and rz_main_rizin().

◆ rz_main_run()

RZ_API int rz_main_run ( RzMain m,
int  argc,
const char **  argv 
)

Definition at line 44 of file main.c.

44  {
45  rz_return_val_if_fail(m && m->main, -1);
46  return m->main(argc, argv);
47 }
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108

References argv, regress::m, and rz_return_val_if_fail.

Referenced by MAIN_NAME().

◆ rz_main_rz_agent()

RZ_API int rz_main_rz_agent ( int  argc,
const char **  argv 
)

Definition at line 61 of file rz-agent.c.

61  {
62  RzSocket *s;
65  int c;
66  int dodaemon = 0;
67  bool listenlocal = true;
68  const char *port = "8080";
69  const char *httpauthfile = NULL;
70  char *pfile = NULL;
71  memset(&so, 0, sizeof(so));
72 
73  RzGetopt opt;
74  rz_getopt_init(&opt, argc, argv, "adhup:t:sv");
75  while ((c = rz_getopt_next(&opt)) != -1) {
76  switch (c) {
77  case 'a':
78  listenlocal = false;
79  break;
80  case 'd':
81  dodaemon = 1;
82  break;
83  case 'h':
84  return usage(1);
85  case 'v':
86  return showversion();
87  case 'u':
88  so.httpauth = true;
89  break;
90  case 't':
91  httpauthfile = opt.arg;
92  break;
93  case 'p':
94  port = opt.arg;
95  break;
96  default:
97  return usage(0);
98  }
99  }
100  if (opt.ind != argc) {
101  return usage(0);
102  }
103 
104  so.accept_timeout = 0;
105  so.timeout = 3;
106 
107  if (so.httpauth) {
108  if (!httpauthfile) {
109  eprintf("No authentication user list set\n");
110  return usage(0);
111  }
112 
113  size_t sz;
114  pfile = rz_file_slurp(httpauthfile, &sz);
115  if (pfile) {
116  so.authtokens = rz_str_split_list(pfile, "\n", 0);
117  } else {
118  eprintf("Empty list of HTTP users\\n");
119  return usage(0);
120  }
121  }
122 #if USE_IOS_JETSAM
123  memorystatus_control(MEMORYSTATUS_CMD_SET_JETSAM_TASK_LIMIT, getpid(), 256, NULL, 0);
124 #endif
125  if (dodaemon) {
126 #if HAVE_FORK
127  int pid = rz_sys_fork();
128  if (pid > 0) {
129  printf("%d\n", pid);
130  return 0;
131  }
132 #endif
133  }
134  s = rz_socket_new(false);
135  s->local = listenlocal;
136  if (!rz_socket_listen(s, port, NULL)) {
137  eprintf("Cannot listen on %d\n", s->port);
138  rz_socket_free(s);
139  return 1;
140  }
141 
142  eprintf("http://localhost:%d/\n", s->port);
143 
144  (void)rz_cons_new();
145 
146  while (!rz_cons_singleton()->context->breaked) {
147  char *result_heap = NULL;
148  const char *result = page_index;
149 
150  rs = rz_socket_http_accept(s, &so);
151  if (!rs) {
152  continue;
153  }
154  if (!rs->auth) {
155  rz_socket_http_response(rs, 401, "", 0, NULL);
156  }
157  if (!strcmp(rs->method, "GET")) {
158  if (!strncmp(rs->path, "/proc/kill/", 11)) {
159  // TODO: show page here?
160  int pid = atoi(rs->path + 11);
161  if (pid > 0) {
162 #if __WINDOWS__
163  rz_sys_kill(pid, 0);
164 #else
166 #endif
167  }
168  } else if (!strncmp(rs->path, "/file/open/", 11)) {
169  int pid;
170  int session_port = 3000 + rz_num_rand(1024);
171  char *filename = rs->path + 11;
172  char *escaped_filename = rz_str_escape(filename);
173  size_t escaped_len = strlen(escaped_filename);
174  size_t cmd_len = escaped_len + 40;
175  char *cmd;
176 
177  if (!(cmd = malloc(cmd_len))) {
178  perror("malloc");
179  return 1;
180  }
181  snprintf(cmd, cmd_len, "rizin -q %s-e http.port=%d -cRh \"%s\"",
182  listenlocal ? "" : "-e http.bind=public ",
183  session_port, escaped_filename);
184 
185  // TODO: use rz_sys api to get pid when running in bg
186  pid = rz_sys_cmdbg(cmd);
187  free(cmd);
188  free(escaped_filename);
189  result = result_heap = malloc(1024 + escaped_len);
190  if (!result) {
191  perror("malloc");
192  free(pfile);
194  return 1;
195  }
196  sprintf(result_heap,
197  "<html><body>"
198  "<a href='/'>back</a><hr size=1/>"
199  " - <a target='_blank' href='http://localhost:%d/'>open</a><br />"
200  " - <a href='/proc/kill/%d'>kill</a><br />"
201  "</body></html>",
202  session_port, pid);
203  eprintf("\nchild pid %d\n\n", pid);
204  }
205  }
206  rz_socket_http_response(rs, 200, result, 0, NULL);
208  free(result_heap);
209  result_heap = NULL;
210  }
211  rz_cons_free();
212  free(pfile);
214  rz_socket_free(s);
215  return 0;
216 }
#define rs()
#define SIGKILL
RZ_API RzCons * rz_cons_singleton(void)
Definition: cons.c:300
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 cmd
Definition: sflib.h:79
const char * filename
Definition: ioapi.h:137
snprintf
Definition: kernel.h:364
sprintf
Definition: kernel.h:365
return memset(p, 0, total)
static const char * page_index
Definition: rz-agent.c:8
static int usage(int v)
Definition: rz-agent.c:46
static int showversion(void)
Definition: rz-agent.c:57
void * malloc(size_t size)
Definition: malloc.c:123
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc pid
Definition: sflib.h:64
RZ_API int rz_num_rand(int max)
Definition: unum.c:47
RZ_API RzSocketHTTPRequest * rz_socket_http_accept(RzSocket *s, RzSocketHTTPOptions *so)
RZ_API void rz_socket_http_response(RzSocketHTTPRequest *rs, int code, const char *out, int x, const char *headers)
RZ_API bool rz_socket_listen(RzSocket *s, const char *port, const char *certfile)
Definition: socket.c:474
RZ_API RzSocket * rz_socket_new(bool is_ssl)
Definition: socket.c:179
RZ_API int rz_socket_free(RzSocket *s)
Definition: socket.c:453
RZ_API void rz_socket_http_close(RzSocketHTTPRequest *rs)
RZ_API RZ_OWN char * rz_str_escape(RZ_NONNULL const char *buf)
Definition: str.c:1550
RZ_API RzList * rz_str_split_list(char *str, const char *c, int n)
Split the string str according to the substring c and returns a RzList with the result.
Definition: str.c:3429
RZ_API int rz_sys_fork(void)
Definition: sys.c:1679
RZ_API int rz_sys_kill(int pid, int sig)
Send signal sig to process with pid pid.
Definition: sys.c:1850
RZ_API int rz_sys_cmdbg(const char *cmd)
Definition: sys.c:644
RzConsContext * context
Definition: rz_cons.h:502

References rz_socket_http_options::accept_timeout, rz_getopt_t::arg, argv, rz_socket_http_options::authtokens, rz_cons_context_t::breaked, c, cmd, rz_cons_t::context, eprintf, free(), rz_socket_http_options::httpauth, rz_getopt_t::ind, rz_socket_t::local, malloc(), memset(), NULL, page_index, pid, rz_socket_t::port, printf(), rs, rz_cons_free(), rz_cons_new(), rz_cons_singleton(), rz_file_slurp(), rz_getopt_init(), rz_getopt_next(), rz_list_free(), rz_num_rand(), rz_socket_free(), rz_socket_http_accept(), rz_socket_http_close(), rz_socket_http_response(), rz_socket_listen(), rz_socket_new(), rz_str_escape(), rz_str_split_list(), rz_sys_cmdbg(), rz_sys_fork(), rz_sys_kill(), s, showversion(), SIGKILL, snprintf, sprintf, rz_socket_http_options::timeout, and usage().

Referenced by MAIN_NAME().

◆ rz_main_rz_asm()

RZ_API int rz_main_rz_asm ( int  argc,
const char **  argv 
)

Referenced by rz_main_rizin().

◆ rz_main_rz_ax()

RZ_API int rz_main_rz_ax ( int  argc,
const char **  argv 
)

Definition at line 721 of file rz-ax.c.

721  {
722  int i, fm = 0;
724  if (argc == 1) {
725  use_stdin(num, 0, &fm);
726  } else {
727  ut64 flags = 0;
728  for (i = 1; i < argc; i++) {
729  char *argv_i = strdup(argv[i]);
730  rz_str_unescape(argv_i);
731  rax(num, argv_i, 0, i == argc - 1, &flags, &fm);
732  free(argv_i);
733  }
734  }
735  rz_num_free(num);
736  num = NULL;
737  return 0;
738 }
static int rax(RzNum *num, char *str, int len, int last, ut64 *flags, int *fm)
Definition: rz-ax.c:238
static int use_stdin(RzNum *num, ut64 *flags, int *fm)
Definition: rz-ax.c:38
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc static sig const char static mode static oldfd struct tms static buf static getgid static geteuid const char static filename static arg static mask struct ustat static ubuf static getppid static setsid static egid sigset_t static set struct timeval struct timezone static tz fd_set fd_set fd_set struct timeval static timeout const char char static bufsiz const char static swapflags void static offset const char static length static mode static who const char struct statfs static buf unsigned unsigned num
Definition: sflib.h:126
RZ_API RzNum * rz_num_new(RzNumCallback cb, RzNumCallback2 cb2, void *ptr)
Definition: unum.c:75
RZ_API void rz_num_free(RzNum *num)
Definition: unum.c:87
RZ_API int rz_str_unescape(char *buf)
Definition: str.c:1300
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123

References argv, flags, free(), i, NULL, num, rax(), rz_num_free(), rz_num_new(), rz_str_unescape(), strdup(), use_stdin(), and ut64().

Referenced by MAIN_NAME(), and rz_main_rizin().

◆ rz_main_rz_bin()

RZ_API int rz_main_rz_bin ( int  argc,
const char **  argv 
)

Definition at line 681 of file rz-bin.c.

681  {
682  RzBin *bin = NULL;
683  const char *name = NULL;
684  const char *file = NULL;
685  const char *output = NULL;
686  int out_mode = RZ_MODE_PRINT;
687  ut64 laddr = UT64_MAX;
688  ut64 baddr = UT64_MAX;
689  const char *do_demangle = NULL;
690  const char *query = NULL;
691  int c, bits = 0, actions = 0;
692  char *create = NULL;
694  char *tmp, *ptr, *arch_name = NULL;
695  const char *arch = NULL;
696  const char *forcebin = NULL;
697  const char *chksum = NULL;
698  const char *op = NULL;
699  RzCoreFile *fh = NULL;
700  RzCoreBinFilter filter;
701  int xtr_idx = 0; // load all files if extraction is necessary.
702  bool rawstr = false;
703  int fd = -1;
704  RzCore core = { 0 };
705  ut64 at = UT64_MAX;
706  int result = 0;
707 
708  rz_core_init(&core);
709  bin = core.bin;
710  if (!(tmp = rz_sys_getenv("RZ_BIN_NOPLUGINS"))) {
711  char *homeplugindir = rz_path_home_prefix(RZ_PLUGINS);
712  // TODO: remove after 0.4.0 is released
713  char *oldhomeplugindir = rz_path_home_prefix(RZ_HOME_OLD_PLUGINS);
714  char *plugindir = rz_path_system(RZ_PLUGINS);
715  RzLib *l = rz_lib_new(NULL, NULL);
716  rz_lib_add_handler(l, RZ_LIB_TYPE_DEMANGLER, "demangler plugins",
717  &__lib_demangler_cb, &__lib_demangler_dt, bin->demangler);
718  rz_lib_add_handler(l, RZ_LIB_TYPE_BIN, "bin plugins",
720  rz_lib_add_handler(l, RZ_LIB_TYPE_BIN_XTR, "bin xtr plugins",
722  /* load plugins everywhere */
723  char *path = rz_sys_getenv(RZ_LIB_ENV);
724  if (!RZ_STR_ISEMPTY(path)) {
725  rz_lib_opendir(l, path, false);
726  }
727  rz_lib_opendir(l, homeplugindir, false);
728  rz_lib_opendir(l, oldhomeplugindir, false);
729  rz_lib_opendir(l, plugindir, false);
730  free(homeplugindir);
731  free(oldhomeplugindir);
732  free(plugindir);
733  free(path);
734  rz_lib_free(l);
735  }
736  free(tmp);
737 
738  if ((tmp = rz_sys_getenv("RZ_CONFIG"))) {
739  Sdb *config_sdb = sdb_new(NULL, tmp, 0);
740  if (config_sdb) {
741  rz_config_unserialize(core.config, config_sdb, NULL);
742  sdb_free(config_sdb);
743  } else {
744  eprintf("Cannot open file specified in RZ_CONFIG\n");
745  }
746  free(tmp);
747  }
748  if ((tmp = rz_sys_getenv("RZ_BIN_LANG"))) {
749  rz_config_set(core.config, "bin.lang", tmp);
750  free(tmp);
751  }
752  if ((tmp = rz_sys_getenv("RZ_BIN_DEMANGLE"))) {
753  rz_config_set(core.config, "bin.demangle", tmp);
754  free(tmp);
755  }
756  if ((tmp = rz_sys_getenv("RZ_BIN_MAXSTRBUF"))) {
757  rz_config_set(core.config, "bin.maxstrbuf", tmp);
758  free(tmp);
759  }
760  if ((tmp = rz_sys_getenv("RZ_BIN_STRFILTER"))) {
761  rz_config_set(core.config, "bin.str.filter", tmp);
762  free(tmp);
763  }
764  if ((tmp = rz_sys_getenv("RZ_BIN_STRPURGE"))) {
765  rz_config_set(core.config, "bin.str.purge", tmp);
766  free(tmp);
767  }
768  if ((tmp = rz_sys_getenv("RZ_BIN_DEBASE64"))) {
769  rz_config_set(core.config, "bin.debase64", tmp);
770  free(tmp);
771  }
772  if ((tmp = rz_sys_getenv("RZ_BIN_PDBSERVER"))) {
773  rz_config_set(core.config, "pdb.server", tmp);
774  free(tmp);
775  }
776 
777 #define is_active(x) (action & (x))
778 #define set_action(x) \
779  { \
780  actions++; \
781  action |= (x); \
782  }
783 #define unset_action(x) action &= ~x
784  RzGetopt opt;
785  rz_getopt_init(&opt, argc, argv, "DjgAf:F:a:B:G:b:cC:k:K:dD:Mm:n:N:@:isSVIHeEUlRwO:o:pPqQrTvLhuxYXzZ");
786  while ((c = rz_getopt_next(&opt)) != -1) {
787  switch (c) {
788  case 'g':
806  break;
807  case 'V': set_action(RZ_BIN_REQ_VERSIONINFO); break;
808  case 'T': set_action(RZ_BIN_REQ_SIGNATURE); break;
809  case 'w': set_action(RZ_BIN_REQ_TRYCATCH); break;
810  case 'q':
811  out_mode = (out_mode & RZ_MODE_SIMPLE ? RZ_MODE_SIMPLEST : RZ_MODE_SIMPLE);
812  break;
813  case 'j': out_mode = RZ_MODE_JSON; break;
814  case 'A': set_action(RZ_BIN_REQ_LISTARCHS); break;
815  case 'a': arch = opt.arg; break;
816  case 'C':
818  create = strdup(opt.arg);
819  break;
820  case 'u': bin->filter = 0; break;
821  case 'k': query = opt.arg; break;
822  case 'K': chksum = opt.arg; break;
823  case 'c':
827  } else {
829  }
830  break;
831  case 'f': arch_name = strdup(opt.arg); break;
832  case 'F': forcebin = opt.arg; break;
833  case 'b': bits = rz_num_math(NULL, opt.arg); break;
834  case 'm':
835  at = rz_num_math(NULL, opt.arg);
837  break;
838  case 'i':
840  break;
841  case 's':
843  break;
844  case 'S':
848  } else if (is_active(RZ_BIN_REQ_SECTIONS)) {
851  } else {
853  }
854  break;
855  case 'z':
857  rawstr = true;
858  } else {
860  }
861  break;
862  case 'Z': set_action(RZ_BIN_REQ_SIZE); break;
863  case 'I': set_action(RZ_BIN_REQ_INFO); break;
864  case 'H':
866  break;
867  case 'd': set_action(RZ_BIN_REQ_DWARF); break;
868  case 'P':
869  if (is_active(RZ_BIN_REQ_PDB)) {
872  } else {
874  }
875  break;
876  case 'D':
877  if (argv[opt.ind] && argv[opt.ind + 1] &&
878  (!argv[opt.ind + 1][0] || !strcmp(argv[opt.ind + 1], "all"))) {
879  rz_config_set(core.config, "bin.lang", argv[opt.ind]);
880  rz_config_set(core.config, "bin.demangle", "true");
881  opt.ind += 2;
882  } else {
883  do_demangle = argv[opt.ind];
884  }
885  break;
886  case 'e':
887  if (action & RZ_BIN_REQ_ENTRIES) {
890  } else {
892  }
893  break;
894  case 'E': set_action(RZ_BIN_REQ_EXPORTS); break;
895  case 'U': set_action(RZ_BIN_REQ_RESOURCES); break;
896  case 'Q': set_action(RZ_BIN_REQ_DLOPEN); break;
897  case 'M': set_action(RZ_BIN_REQ_MAIN); break;
898  case 'l': set_action(RZ_BIN_REQ_LIBS); break;
899  case 'R': set_action(RZ_BIN_REQ_RELOCS); break;
900  case 'Y': set_action(RZ_BIN_REQ_BASEFIND); break;
901  case 'x': set_action(RZ_BIN_REQ_EXTRACT); break;
902  case 'O':
903  op = opt.arg;
905  if (*op == 'c') {
906  rz_sys_setenv("RZ_BIN_CODESIGN_VERBOSE", "1");
907  }
908  if (isBinopHelp(op)) {
909  printf("Usage: iO [expression]:\n"
910  " d/s/1024 dump symbols\n"
911  " d/S/.text dump section\n"
912  " c show Codesign data\n"
913  " C show LDID entitlements\n");
914  rz_core_fini(&core);
915  return 0;
916  }
917  if (opt.ind == argc) {
918  eprintf("Missing filename\n");
919  rz_core_fini(&core);
920  return 1;
921  }
922  break;
923  case 'o': output = opt.arg; break;
924  case 'p': core.io->va = false; break;
925  case 'r': out_mode = RZ_MODE_RIZINCMD; break;
926  case 'v':
927  rz_core_fini(&core);
928  return rz_main_version_print("rz-bin");
929  case 'L':
931  break;
932  case 'G':
933  laddr = rz_num_math(NULL, opt.arg);
934  if (laddr == UT64_MAX) {
935  core.io->va = false;
936  }
937  break;
938  case 'B':
939  baddr = rz_num_math(NULL, opt.arg);
940  break;
941  case '@':
942  at = rz_num_math(NULL, opt.arg);
943  if (at == 0LL && *opt.arg != '0') {
944  at = UT64_MAX;
945  }
946  break;
947  case 'n':
948  name = opt.arg;
949  break;
950  case 'N':
951  tmp = strchr(opt.arg, ':');
952  rz_config_set(core.config, "bin.minstr", opt.arg);
953  if (tmp) {
954  rz_config_set(core.config, "bin.maxstr", tmp + 1);
955  }
956  break;
957  case 'h':
958  rz_core_fini(&core);
959  return rabin_show_help(1);
960  default:
962  break;
963  }
964  }
965 
967  const char *plugin_name = NULL;
968  if (opt.ind < argc) {
969  plugin_name = argv[opt.ind];
970  }
971  PJ *pj = pj_new();
972  if (!pj) {
973  rz_core_fini(&core);
974  return 1;
975  }
976  __listPlugins(bin, plugin_name, pj, out_mode);
977  if (out_mode == RZ_MODE_JSON) {
979  rz_cons_flush();
980  }
981  rz_core_fini(&core);
982  return 0;
983  }
984 
985  if (do_demangle) {
986  int ret_num = 1;
987  char *res = NULL;
988  const RzDemanglerPlugin *plugin = NULL;
989  if ((argc - opt.ind) < 2) {
990  rz_core_fini(&core);
991  return rabin_show_help(0);
992  }
993  file = argv[opt.ind + 1];
994  plugin = rz_demangler_plugin_get(bin->demangler, do_demangle);
995  if (!plugin) {
996  printf("Language '%s' is unsupported\nList of supported languages:\n", do_demangle);
998  rz_core_fini(&core);
999  return 1;
1000  }
1001  if (!strcmp(file, "-")) {
1002  for (;;) {
1003  file = stdin_gets(false);
1004  if (!file || !*file) {
1005  break;
1006  }
1007  res = rz_demangler_plugin_demangle(plugin, file);
1008  if (RZ_STR_ISNOTEMPTY(res)) {
1009  printf("%s\n", res);
1010  ret_num = 0;
1011  } else if (file && *file) {
1012  printf("%s\n", file);
1013  }
1014  RZ_FREE(res);
1015  RZ_FREE(file);
1016  }
1017  stdin_gets(true);
1018  } else {
1019  res = rz_demangler_plugin_demangle(plugin, file);
1020  if (RZ_STR_ISNOTEMPTY(res)) {
1021  printf("%s\n", res);
1022  ret_num = 0;
1023  } else {
1024  printf("%s\n", file);
1025  }
1026  }
1027  free(res);
1028  rz_core_fini(&core);
1029  return ret_num;
1030  }
1031  file = argv[opt.ind];
1032 
1033  if (file && !*file) {
1034  eprintf("Cannot open empty path\n");
1035  rz_core_fini(&core);
1036  return 1;
1037  }
1038 
1039  if (!query) {
1040  if (action & RZ_BIN_REQ_HELP || action == RZ_BIN_REQ_UNK || !file) {
1041  rz_core_fini(&core);
1042  return rabin_show_help(0);
1043  }
1044  }
1045  if (arch) {
1046  ptr = strchr(arch, '_');
1047  if (ptr) {
1048  *ptr = '\0';
1049  bits = rz_num_math(NULL, ptr + 1);
1050  }
1051  }
1052  if (action & RZ_BIN_REQ_CREATE) {
1053  // TODO: move in a function outside
1054  RzBuffer *b;
1055  int datalen, codelen;
1056  ut8 *data = NULL, *code = NULL;
1057  char *p2, *p = strchr(create, ':');
1058  if (!p) {
1059  eprintf("Invalid format for -C flag. Use 'format:codehexpair:datahexpair'\n");
1060  rz_core_fini(&core);
1061  return 1;
1062  }
1063  *p++ = 0;
1064  p2 = strchr(p, ':');
1065  if (p2) {
1066  // has data
1067  *p2++ = 0;
1068  data = malloc(strlen(p2) + 1);
1069  datalen = rz_hex_str2bin(p2, data);
1070  if (datalen < 0) {
1071  datalen = -datalen;
1072  }
1073  } else {
1074  data = NULL;
1075  datalen = 0;
1076  }
1077  code = malloc(strlen(p) + 1);
1078  if (!code) {
1079  rz_core_fini(&core);
1080  return 1;
1081  }
1082  codelen = rz_hex_str2bin(p, code);
1083  RzBinArchOptions opts;
1085  b = rz_bin_create(bin, create, code, codelen, data, datalen, &opts);
1086  if (b) {
1087  ut64 tmpsz;
1088  const ut8 *tmp = rz_buf_data(b, &tmpsz);
1089  if (rz_file_dump(file, tmp, tmpsz, 0)) {
1090  eprintf("Dumped %" PFMT64d " bytes in '%s'\n",
1091  tmpsz, file);
1092  (void)rz_file_chmod(file, "+x", 0);
1093  } else {
1094  eprintf("Error dumping into a.out\n");
1095  }
1096  rz_buf_free(b);
1097  } else {
1098  eprintf("Cannot create binary for this format '%s'.\n", create);
1099  }
1100  rz_core_fini(&core);
1101  return 0;
1102  }
1103  if (rawstr) {
1105  }
1106 
1107  if (!file) {
1108  eprintf("Missing file.\n");
1109  rz_core_fini(&core);
1110  return 1;
1111  }
1112 
1113  if (file && *file && action & RZ_BIN_REQ_DLOPEN) {
1114 #if __UNIX__
1115  int child = rz_sys_fork();
1116  if (child == -1) {
1117  rz_core_fini(&core);
1118  return 1;
1119  }
1120  if (child == 0) {
1121  return waitpid(child, NULL, 0);
1122  }
1123 #endif
1124  void *addr = rz_lib_dl_open(file);
1125  if (addr) {
1126  eprintf("%s is loaded at 0x%" PFMT64x "\n", file, (ut64)(size_t)(addr));
1128  rz_core_fini(&core);
1129  return 0;
1130  }
1131  eprintf("Cannot open the '%s' library\n", file);
1132  rz_core_fini(&core);
1133  return 0;
1134  }
1135 
1136  if (RZ_STR_ISNOTEMPTY(file)) {
1137  if ((fh = rz_core_file_open(&core, file, RZ_PERM_R, 0))) {
1138  fd = rz_io_fd_get_current(core.io);
1139  if (fd == -1) {
1140  eprintf("rz_core: Cannot open file '%s'\n", file);
1141  result = 1;
1142  goto err;
1143  }
1144  } else {
1145  eprintf("rz_core: Cannot open file '%s'\n", file);
1146  rz_core_fini(&core);
1147  return 1;
1148  }
1149  }
1150  bin->minstrlen = rz_config_get_i(core.config, "bin.minstr");
1151  bin->maxstrbuf = rz_config_get_i(core.config, "bin.maxstrbuf");
1152 
1153  rz_bin_force_plugin(bin, forcebin);
1155 
1156  RzBinOptions bo;
1157  rz_bin_options_init(&bo, fd, baddr, laddr, false);
1158  bo.obj_opts.elf_load_sections = rz_config_get_b(core.config, "elf.load.sections");
1159  bo.obj_opts.elf_checks_sections = rz_config_get_b(core.config, "elf.checks.sections");
1160  bo.obj_opts.elf_checks_segments = rz_config_get_b(core.config, "elf.checks.segments");
1161  bo.obj_opts.big_endian = rz_config_get_b(core.config, "cfg.bigendian");
1162  bo.xtr_idx = xtr_idx;
1163 
1164  RzBinFile *bf = rz_bin_open(bin, file, &bo);
1165  if (!bf) {
1166  eprintf("rz-bin: Cannot open file\n");
1167  result = 1;
1168  goto err;
1169  }
1170  /* required to automatically select a sub-bin when not specified */
1171  (void)rz_core_bin_update_arch_bits(&core);
1172 
1173  if (baddr != UT64_MAX) {
1175  }
1176  if (rawstr) {
1177  PJ *pj = NULL;
1178  if (out_mode == RZ_MODE_JSON) {
1179  pj = pj_new();
1180  if (!pj) {
1181  eprintf("rz-bin: Cannot allocate buffer for json array\n");
1182  result = 1;
1183  goto err;
1184  }
1185  pj_a(pj);
1186  }
1187  RzList *list = rz_bin_file_strings(bf, bin->minstrlen, true);
1188  RzListIter *it;
1189  RzBinString *string;
1190  rz_list_foreach (list, it, string) {
1191  print_string(bf, string, pj, out_mode);
1192  }
1193  rz_list_free(list);
1194  if (pj) {
1195  pj_end(pj);
1196  printf("%s", pj_string(pj));
1197  pj_free(pj);
1198  }
1199  }
1200  if (query) {
1201  if (out_mode) {
1203  rz_cons_flush();
1204  } else {
1205  if (!strcmp(query, "-")) {
1206  __sdb_prompt(bin->cur->sdb);
1207  } else {
1208  sdb_query(bin->cur->sdb, query);
1209  }
1210  }
1211  result = 0;
1212  goto err;
1213  }
1214 #define ismodejson (out_mode == RZ_MODE_JSON && actions > 0)
1215 #define run_action(n, x, y) \
1216  if (action & (x)) { \
1217  RzCmdStateOutput *st = add_header(&state, mode, n); \
1218  y(&core, st); \
1219  add_footer(&state, st); \
1220  }
1221 
1222  core.bin = bin;
1223  bin->cb_printf = rz_cons_printf;
1224  filter.offset = at;
1225  filter.name = name;
1226  RzList *chksum_list = NULL;
1227  if (RZ_STR_ISNOTEMPTY(chksum)) {
1228  chksum_list = rz_str_split_duplist_n(chksum, ",", 0, true);
1229  if (!chksum_list) {
1230  result = 1;
1231  goto err;
1232  }
1233  }
1234  rz_cons_new()->context->is_interactive = false;
1235 
1237  RzOutputMode mode = rad2outputmode(out_mode);
1239  result = 1;
1240  goto chksum_err;
1241  }
1242  start_state(&state);
1243 
1244  // List fatmach0 sub-binaries, etc
1245  if (action & RZ_BIN_REQ_LISTARCHS || ((arch || bits || arch_name) && !rz_bin_select(bin, arch, bits, arch_name))) {
1248  add_footer(&state, st);
1249  free(arch_name);
1250  }
1251  if (action & RZ_BIN_REQ_PDB_DWNLD) {
1252  SPDBOptions pdbopts;
1253  pdbopts.symbol_server = (char *)rz_config_get(core.config, "pdb.server");
1254  pdbopts.extract = rz_config_get_i(core.config, "pdb.extract");
1255 
1256  if ((tmp = rz_sys_getenv("RZ_BIN_SYMSTORE"))) {
1257  rz_config_set(core.config, "pdb.symstore", tmp);
1258  RZ_FREE(tmp);
1259  }
1260  pdbopts.symbol_store_path = (char *)rz_config_get(core.config, "pdb.symstore");
1261  result = rz_bin_pdb_download(core.bin, state.mode == RZ_OUTPUT_MODE_JSON ? state.d.pj : NULL, ismodejson, &pdbopts);
1262  }
1263 
1264  if ((tmp = rz_sys_getenv("RZ_BIN_PREFIX"))) {
1265  rz_config_set(core.config, "bin.prefix", tmp);
1266  free(tmp);
1267  }
1268 
1270  rz_core_bin_print(&core, bf, mask, &filter, &state, chksum_list);
1271 
1273  if (action & RZ_BIN_REQ_SRCLINE) {
1274  rabin_show_srcline(bin, at);
1275  }
1276  if (action & RZ_BIN_REQ_EXTRACT) {
1277  if (bf->xtr_data) {
1278  rabin_extract(bin, (!arch && !arch_name && !bits));
1279  } else {
1280  eprintf(
1281  "Cannot extract bins from '%s'. No supported "
1282  "plugins found!\n",
1283  bin->file);
1284  }
1285  }
1286  if (op && action & RZ_BIN_REQ_OPERATION) {
1287  rabin_do_operation(bin, op, out_mode, output, file);
1288  }
1289  end_state(&state);
1291 
1292  rz_cons_flush();
1293 
1294 chksum_err:
1295  rz_list_free(chksum_list);
1296 err:
1298  rz_core_fini(&core);
1299 
1300  return result;
1301 }
#define mask()
static bool err
Definition: armass.c:435
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.
Definition: bfile_string.c:325
RZ_API void rz_bin_load_filter(RzBin *bin, ut64 rules)
Definition: bin.c:930
RZ_API void rz_bin_arch_options_init(RzBinArchOptions *opt, const char *arch, int bits)
Definition: bin.c:84
RZ_API RzBuffer * rz_bin_create(RzBin *bin, const char *p, const ut8 *code, int codelen, const ut8 *data, int datalen, RzBinArchOptions *opt)
Definition: bin.c:862
RZ_API void rz_bin_options_init(RzBinOptions *opt, int fd, ut64 baseaddr, ut64 loadaddr, bool patch_relocs)
Definition: bin.c:75
RZ_API RzBinFile * rz_bin_open(RzBin *bin, const char *file, RzBinOptions *opt)
Definition: bin.c:200
RZ_API bool rz_bin_select(RzBin *bin, const char *arch, int bits, const char *name)
Definition: bin.c:813
RZ_API void rz_bin_set_baddr(RzBin *bin, ut64 baddr)
Definition: bin.c:549
static RzBuffer * create(RzBin *bin, const ut8 *code, int codelen, const ut8 *data, int datalen, RzBinArchOptions *opt)
Definition: bin_cgc.c:16
int bits(struct state *s, int need)
Definition: blast.c:72
RZ_API void rz_core_bin_export_info(RzCore *core, int mode)
Definition: cbin.c:108
RZ_API bool rz_core_bin_print(RzCore *core, RZ_NONNULL RzBinFile *bf, ut32 mask, RzCoreBinFilter *filter, RzCmdStateOutput *state, RzList *hashes)
Print (to RzCons or inside RzCmdStateOutput) the binary information specified in mask.
Definition: cbin.c:386
RZ_API int rz_core_bin_update_arch_bits(RzCore *r)
Definition: cbin.c:4503
RZ_API bool rz_core_bin_archs_print(RZ_NONNULL RzBin *bin, RZ_NONNULL RzCmdStateOutput *state)
Definition: cbin.c:5217
RZ_API void rz_core_file_close(RzCoreFile *fh)
Definition: cfile.c:1299
RZ_API bool rz_config_unserialize(RZ_NONNULL RzConfig *config, RZ_NONNULL Sdb *db, RZ_NULLABLE char **err)
Definition: config.c:564
RZ_API int rz_cons_printf(const char *format,...)
Definition: cons.c:1202
RZ_API void rz_cons_println(const char *str)
Definition: cons.c:233
cs_arch arch
Definition: cstool.c:13
uint32_t ut32
const char int mode
Definition: ioapi.h:137
RZ_API void rz_core_fini(RzCore *c)
Definition: core.c:2623
RZ_API bool rz_core_init(RzCore *core)
Definition: core.c:2381
RZ_API void rz_demangler_plugin_iterate(RZ_NONNULL RzDemangler *dem, RZ_NONNULL RzDemanglerIter iter, RZ_NULLABLE void *data)
Iterates over the plugin list.
Definition: demangler.c:127
RZ_API RZ_BORROW const RzDemanglerPlugin * rz_demangler_plugin_get(RZ_NONNULL RzDemangler *dem, RZ_NONNULL const char *language)
Returns a demangler plugin pointer based on the language that is found.
Definition: demangler.c:168
static RzOutputMode rad2outputmode(int rad)
Definition: rz-bin.c:63
static void print_string(RzBinFile *bf, RzBinString *string, PJ *pj, int mode)
Definition: rz-bin.c:638
#define run_action(n, x, y)
#define unset_action(x)
static RzCmdStateOutput * add_header(RzCmdStateOutput *main_state, RzOutputMode mode, const char *header)
Definition: rz-bin.c:45
static int __lib_bin_cb(RzLibPlugin *pl, void *user, void *data)
Definition: rz-bin.c:585
static int rabin_extract(RzBin *bin, int all)
Definition: rz-bin.c:353
static void end_state(RzCmdStateOutput *state)
Definition: rz-bin.c:19
static void classes_as_source_print(RzCore *core, RzCmdStateOutput *state)
Definition: rz-bin.c:56
static int __lib_bin_xtr_dt(RzLibPlugin *pl, void *p, void *u)
Definition: rz-bin.c:605
static ut32 actions2mask(ut64 action)
Definition: rz-bin.c:79
static int __lib_bin_dt(RzLibPlugin *pl, void *p, void *u)
Definition: rz-bin.c:592
static int __lib_demangler_cb(RzLibPlugin *pl, void *user, void *data)
Definition: rz-bin.c:576
static void __sdb_prompt(Sdb *sdb)
Definition: rz-bin.c:260
static bool add_footer(RzCmdStateOutput *main_state, RzCmdStateOutput *state)
Definition: rz-bin.c:29
static int rabin_show_help(int v)
Definition: rz-bin.c:153
#define is_active(x)
static bool print_demangler_info(const RzDemanglerPlugin *plugin, void *user)
Definition: rz-bin.c:632
static void start_state(RzCmdStateOutput *state)
Definition: rz-bin.c:13
static int rabin_show_srcline(RzBin *bin, ut64 at)
Definition: rz-bin.c:565
static int __lib_demangler_dt(RzLibPlugin *pl, void *p, void *u)
Definition: rz-bin.c:581
#define ismodejson
static char * stdin_gets(bool liberate)
Definition: rz-bin.c:235
static int __lib_bin_xtr_cb(RzLibPlugin *pl, void *user, void *data)
Definition: rz-bin.c:597
static int rabin_do_operation(RzBin *bin, const char *op, int rad, const char *output, const char *file)
Definition: rz-bin.c:471
static bool isBinopHelp(const char *op)
Definition: rz-bin.c:268
static void __listPlugins(RzBin *bin, const char *plugin_name, PJ *pj, int rad)
Definition: rz-bin.c:609
#define set_action(x)
static void list(RzEgg *egg)
Definition: rz-gg.c:52
RZ_API int sdb_query(Sdb *s, const char *cmd)
Definition: query.c:785
const char * name
Definition: op.c:541
static bool filter(RzParse *p, ut64 addr, RzFlag *f, RzAnalysisHint *hint, char *data, char *str, int len, bool big_endian)
Definition: filter.c:185
RZ_API int rz_bin_pdb_download(RZ_NONNULL RzBin *bin, RZ_NULLABLE PJ *pj, int isradjson, RZ_NONNULL SPDBOptions *options)
Download PDB file for currently opened RzBin file.
#define RZ_BIN_REQ_VERSIONINFO
Definition: rz_bin.h:68
#define RZ_BIN_REQ_SECTIONS_MAPPING
Definition: rz_bin.h:78
#define RZ_BIN_REQ_INITFINI
Definition: rz_bin.h:73
#define RZ_BIN_REQ_HELP
Definition: rz_bin.h:51
#define RZ_BIN_REQ_CLASSES_SOURCES
Definition: rz_bin.h:79
#define RZ_BIN_REQ_EXTRACT
Definition: rz_bin.h:57
#define RZ_BIN_REQ_RELOCS
Definition: rz_bin.h:58
#define RZ_BIN_REQ_SECTIONS
Definition: rz_bin.h:48
#define RZ_BIN_REQ_ENTRIES
Definition: rz_bin.h:45
#define RZ_BIN_REQ_PDB
Definition: rz_bin.h:64
#define RZ_BIN_REQ_LIBS
Definition: rz_bin.h:54
#define RZ_BIN_REQ_SYMBOLS
Definition: rz_bin.h:47
#define RZ_BIN_REQ_PDB_DWNLD
Definition: rz_bin.h:65
#define RZ_BIN_REQ_CLASSES
Definition: rz_bin.h:61
#define RZ_BIN_REQ_IMPORTS
Definition: rz_bin.h:46
#define RZ_BIN_REQ_BASEFIND
Definition: rz_bin.h:80
#define RZ_BIN_REQ_DWARF
Definition: rz_bin.h:62
#define RZ_BIN_REQ_RESOURCES
Definition: rz_bin.h:72
#define RZ_BIN_REQ_FIELDS
Definition: rz_bin.h:53
#define RZ_BIN_REQ_CREATE
Definition: rz_bin.h:60
#define RZ_BIN_REQ_LISTPLUGINS
Definition: rz_bin.h:71
#define RZ_BIN_REQ_DLOPEN
Definition: rz_bin.h:66
#define RZ_BIN_REQ_MAIN
Definition: rz_bin.h:56
#define RZ_BIN_REQ_STRINGS
Definition: rz_bin.h:52
#define RZ_BIN_REQ_LISTARCHS
Definition: rz_bin.h:59
#define RZ_BIN_REQ_SRCLINE
Definition: rz_bin.h:55
#define RZ_BIN_REQ_OPERATION
Definition: rz_bin.h:50
#define RZ_BIN_REQ_TRYCATCH
Definition: rz_bin.h:77
#define RZ_BIN_REQ_INFO
Definition: rz_bin.h:49
#define RZ_BIN_REQ_SIZE
Definition: rz_bin.h:63
#define RZ_BIN_REQ_SIGNATURE
Definition: rz_bin.h:76
#define RZ_BIN_REQ_EXPORTS
Definition: rz_bin.h:67
#define RZ_BIN_REQ_SEGMENTS
Definition: rz_bin.h:74
#define RZ_BIN_REQ_UNK
Definition: rz_bin.h:44
RZ_API void rz_buf_free(RzBuffer *b)
Free all internal data hold by the buffer and the buffer.
Definition: buf.c:1253
RZ_DEPRECATE RZ_API RZ_BORROW ut8 * rz_buf_data(RZ_NONNULL RzBuffer *b, RZ_NONNULL RZ_OUT ut64 *size)
Return a borrowed array of bytes representing the buffer data.
Definition: buf.c:1287
#define rz_demangler_plugin_demangle(x, y)
Definition: rz_demangler.h:29
bool(* RzDemanglerIter)(const RzDemanglerPlugin *plugin, void *data)
Definition: rz_demangler.h:27
RZ_API bool rz_file_chmod(const char *file, const char *mod, int recursive)
Definition: chmod.c:19
RZ_API int rz_hex_str2bin(const char *in, ut8 *out)
Convert an input string in into the binary form in out.
Definition: hex.c:444
RZ_API void rz_lib_free(RzLib *lib)
Definition: lib.c:189
#define RZ_LIB_ENV
Definition: rz_lib.h:23
RZ_API RzLib * rz_lib_new(const char *symname, const char *symnamefunc)
Definition: lib.c:176
RZ_API void * rz_lib_dl_open(const char *libname)
Definition: lib.c:54
@ RZ_LIB_TYPE_BIN
Definition: rz_lib.h:75
@ RZ_LIB_TYPE_BIN_XTR
Definition: rz_lib.h:76
@ RZ_LIB_TYPE_DEMANGLER
Definition: rz_lib.h:85
RZ_API bool rz_lib_opendir(RzLib *lib, const char *path, bool force)
Open all the libraries in the given directory, if it wasn't already opened.
Definition: lib.c:417
RZ_API bool rz_lib_add_handler(RzLib *lib, int type, const char *desc, RzLibCallback ct, RzLibCallback dt, void *user)
RZ_API int rz_lib_dl_close(void *handler)
Definition: lib.c:104
RZ_API RZ_OWN char * rz_path_home_prefix(RZ_NULLABLE const char *path)
Return path prefixed by the home prefix.
Definition: path.c:182
RZ_API RZ_OWN char * rz_path_system(RZ_NULLABLE const char *path)
Return the full system path of the given subpath path.
Definition: path.c:162
RZ_API PJ * pj_new(void)
Definition: pj.c:25
RZ_API PJ * pj_end(PJ *j)
Definition: pj.c:87
RZ_API const char * pj_string(PJ *pj)
Definition: pj.c:57
RZ_API void pj_free(PJ *j)
Definition: pj.c:34
RZ_API PJ * pj_a(PJ *j)
Definition: pj.c:81
RZ_API RzList * rz_str_split_duplist_n(const char *str, const char *c, int n, bool trim)
Split the string str according to the substring c and returns a RzList with the result.
Definition: str.c:3485
RZ_API int rz_sys_setenv(const char *key, const char *value)
Set an environment variable in the calling process.
Definition: sys.c:405
#define PFMT64d
Definition: rz_types.h:394
#define RZ_PERM_R
Definition: rz_types.h:93
#define RZ_MODE_SIMPLEST
Definition: rz_types.h:31
#define RZ_MODE_RIZINCMD
Definition: rz_types.h:26
#define RZ_MODE_PRINT
Definition: rz_types.h:25
#define RZ_MODE_JSON
Definition: rz_types.h:29
#define RZ_MODE_SIMPLE
Definition: rz_types.h:28
RzOutputMode
Enum to describe the way data are printed.
Definition: rz_types.h:38
@ RZ_OUTPUT_MODE_TABLE
Definition: rz_types.h:46
@ RZ_OUTPUT_MODE_JSON
Definition: rz_types.h:40
#define RZ_PLUGINS
Definition: rz_userconf.h:72
#define RZ_HOME_OLD_PLUGINS
Definition: rz_userconf.h:100
RZ_API Sdb * sdb_new(const char *path, const char *name, int lock)
Definition: sdb.c:47
RZ_API bool sdb_free(Sdb *s)
Definition: sdb.c:206
#define b(i)
Definition: sha256.c:42
const char * symbol_server
const char * symbol_store_path
Definition: malloc.c:26
Definition: inftree9.h:24
Definition: rz_pj.h:12
RzList * xtr_data
Definition: rz_bin.h:314
bool is_interactive
Definition: rz_cons.h:485
RzBin * bin
Definition: rz_core.h:298
RzIO * io
Definition: rz_core.h:313
RzConfig * config
Definition: rz_core.h:300
int va
Definition: rz_io.h:63
Definition: sdb.h:63
Definition: dis.c:32
diff_output_t output
Definition: zipcmp.c:237

References __lib_bin_cb(), __lib_bin_dt(), __lib_bin_xtr_cb(), __lib_bin_xtr_dt(), __lib_demangler_cb(), __lib_demangler_dt(), __listPlugins(), __sdb_prompt(), test-lz4-speed::action, actions2mask(), add_footer(), add_header(), addr, arch, rz_getopt_t::arg, argv, b, baddr(), rz_core_t::bin, bits(), c, classes_as_source_print(), rz_core_t::config, rz_cons_t::context, create(), end_state(), eprintf, err, SPDBOptions::extract, fd, fh, filter(), free(), rz_getopt_t::ind, rz_core_t::io, is_active, rz_cons_context_t::is_interactive, isBinopHelp(), ismodejson, list(), malloc(), mask, name, NULL, output, p, path, PFMT64d, PFMT64x, pj_a(), pj_end(), pj_free(), pj_new(), pj_string(), print_demangler_info(), print_string(), printf(), rabin_do_operation(), rabin_extract(), rabin_show_help(), rabin_show_srcline(), rad2outputmode(), run_action, rz_bin_arch_options_init(), rz_bin_create(), rz_bin_file_strings(), rz_bin_force_plugin(), rz_bin_load_filter(), rz_bin_open(), rz_bin_options_init(), rz_bin_pdb_download(), RZ_BIN_REQ_BASEFIND, RZ_BIN_REQ_CLASSES, RZ_BIN_REQ_CLASSES_SOURCES, RZ_BIN_REQ_CREATE, RZ_BIN_REQ_DLOPEN, RZ_BIN_REQ_DWARF, RZ_BIN_REQ_ENTRIES, RZ_BIN_REQ_EXPORTS, RZ_BIN_REQ_EXTRACT, RZ_BIN_REQ_FIELDS, RZ_BIN_REQ_HELP, RZ_BIN_REQ_IMPORTS, RZ_BIN_REQ_INFO, RZ_BIN_REQ_INITFINI, RZ_BIN_REQ_LIBS, RZ_BIN_REQ_LISTARCHS, RZ_BIN_REQ_LISTPLUGINS, RZ_BIN_REQ_MAIN, RZ_BIN_REQ_OPERATION, RZ_BIN_REQ_PDB, RZ_BIN_REQ_PDB_DWNLD, RZ_BIN_REQ_RELOCS, RZ_BIN_REQ_RESOURCES, RZ_BIN_REQ_SECTIONS, RZ_BIN_REQ_SECTIONS_MAPPING, RZ_BIN_REQ_SEGMENTS, RZ_BIN_REQ_SIGNATURE, RZ_BIN_REQ_SIZE, RZ_BIN_REQ_SRCLINE, RZ_BIN_REQ_STRINGS, RZ_BIN_REQ_SYMBOLS, RZ_BIN_REQ_TRYCATCH, RZ_BIN_REQ_UNK, RZ_BIN_REQ_VERSIONINFO, rz_bin_select(), rz_bin_set_baddr(), rz_buf_data(), rz_buf_free(), rz_cmd_state_output_fini(), rz_cmd_state_output_init(), rz_config_get(), rz_config_get_b(), rz_config_get_i(), rz_config_set(), rz_config_unserialize(), rz_cons_flush(), rz_cons_new(), rz_cons_printf(), rz_cons_println(), rz_core_bin_archs_print(), rz_core_bin_export_info(), rz_core_bin_print(), rz_core_bin_update_arch_bits(), rz_core_file_close(), rz_core_file_open(), rz_core_fini(), rz_core_init(), rz_demangler_plugin_demangle, rz_demangler_plugin_get(), rz_demangler_plugin_iterate(), rz_file_chmod(), rz_file_dump(), RZ_FREE, rz_getopt_init(), rz_getopt_next(), rz_hex_str2bin(), RZ_HOME_OLD_PLUGINS, rz_io_fd_get_current(), rz_lib_add_handler(), rz_lib_dl_close(), rz_lib_dl_open(), RZ_LIB_ENV, rz_lib_free(), rz_lib_new(), rz_lib_opendir(), RZ_LIB_TYPE_BIN, RZ_LIB_TYPE_BIN_XTR, RZ_LIB_TYPE_DEMANGLER, rz_list_free(), rz_main_version_print(), RZ_MODE_JSON, RZ_MODE_PRINT, RZ_MODE_RIZINCMD, RZ_MODE_SIMPLE, RZ_MODE_SIMPLEST, rz_num_math(), RZ_OUTPUT_MODE_JSON, RZ_OUTPUT_MODE_STANDARD, RZ_OUTPUT_MODE_TABLE, rz_path_home_prefix(), rz_path_system(), RZ_PERM_R, RZ_PLUGINS, RZ_STR_ISEMPTY, RZ_STR_ISNOTEMPTY, rz_str_split_duplist_n(), rz_sys_fork(), rz_sys_getenv(), rz_sys_setenv(), sdb_free(), sdb_new(), sdb_query(), set_action, start_state(), stdin_gets(), strdup(), SPDBOptions::symbol_server, SPDBOptions::symbol_store_path, autogen_x86imm::tmp, unset_action, ut64(), UT64_MAX, rz_io_t::va, and rz_bin_file_t::xtr_data.

Referenced by MAIN_NAME(), and rz_main_rizin().

◆ rz_main_rz_diff()

RZ_API int rz_main_rz_diff ( int  argc,
const char **  argv 
)

Definition at line 2254 of file rz-diff.c.

2254  {
2255  bool success = false;
2256  DiffContext ctx;
2257  rz_diff_parse_arguments(argc, argv, &ctx);
2258 
2259  switch (ctx.option) {
2260  case DIFF_OPT_DISTANCE:
2261  success = rz_diff_calculate_distance(&ctx);
2262  break;
2263  case DIFF_OPT_UNIFIED:
2264  success = rz_diff_unified_files(&ctx);
2265  break;
2266  case DIFF_OPT_GRAPH:
2267  success = rz_diff_graphs_files(&ctx);
2268  break;
2269  case DIFF_OPT_HEX_VISUAL:
2270  success = rz_diff_hex_visual(&ctx);
2271  break;
2272  case DIFF_OPT_VERSION:
2273  rz_main_version_print("rz-diff");
2274  success = true;
2275  break;
2276  case DIFF_OPT_USAGE:
2277  rz_diff_show_help(true);
2278  break;
2279  case DIFF_OPT_ERROR:
2280  break;
2281  case DIFF_OPT_HELP:
2282  success = true;
2283  // fallthrough
2284  default:
2285  rz_diff_show_help(false);
2286  break;
2287  }
2288 
2289  rz_list_free(ctx.evars);
2290  return success ? 0 : 1;
2291 }
static void rz_diff_show_help(bool usage_only)
Definition: rz-diff.c:195
static bool rz_diff_calculate_distance(DiffContext *ctx)
Definition: rz-diff.c:486
@ DIFF_OPT_HELP
Definition: rz-diff.c:55
@ DIFF_OPT_DISTANCE
Definition: rz-diff.c:58
@ DIFF_OPT_GRAPH
Definition: rz-diff.c:60
@ DIFF_OPT_UNIFIED
Definition: rz-diff.c:59
@ DIFF_OPT_ERROR
Definition: rz-diff.c:54
@ DIFF_OPT_VERSION
Definition: rz-diff.c:57
@ DIFF_OPT_HEX_VISUAL
Definition: rz-diff.c:61
@ DIFF_OPT_USAGE
Definition: rz-diff.c:56
static bool rz_diff_hex_visual(DiffContext *ctx)
Definition: rz-diff.c:2027
static bool rz_diff_graphs_files(DiffContext *ctx)
Definition: rz-diff.c:1413
static void rz_diff_parse_arguments(int argc, const char **argv, DiffContext *ctx)
Definition: rz-diff.c:254
static bool rz_diff_unified_files(DiffContext *ctx)
Definition: rz-diff.c:1297

References argv, DIFF_OPT_DISTANCE, DIFF_OPT_ERROR, DIFF_OPT_GRAPH, DIFF_OPT_HELP, DIFF_OPT_HEX_VISUAL, DIFF_OPT_UNIFIED, DIFF_OPT_USAGE, DIFF_OPT_VERSION, rz_diff_calculate_distance(), rz_diff_graphs_files(), rz_diff_hex_visual(), rz_diff_parse_arguments(), rz_diff_show_help(), rz_diff_unified_files(), rz_list_free(), and rz_main_version_print().

Referenced by MAIN_NAME(), and rz_main_rizin().

◆ rz_main_rz_find()

RZ_API int rz_main_rz_find ( int  argc,
const char **  argv 
)

Definition at line 474 of file rz-find.c.

474  {
475  RzfindOptions ro;
476  rzfind_options_init(&ro);
477 
478  int c;
479  const char *file = NULL;
480 
481  RzGetopt opt;
482  rz_getopt_init(&opt, argc, argv, "a:ie:b:jmM:s:w:S:I:x:Xzf:F:t:E:rqnhvZ");
483  while ((c = rz_getopt_next(&opt)) != -1) {
484  switch (c) {
485  case 'a':
486  ro.align = rz_num_math(NULL, opt.arg);
487  break;
488  case 'r':
489  ro.rad = true;
490  break;
491  case 'i':
492  ro.identify = true;
493  break;
494  case 'j':
495  ro.json = true;
496  break;
497  case 'n':
498  ro.nonstop = 1;
499  break;
500  case 'm':
501  ro.mode = RZ_SEARCH_MAGIC;
502  break;
503  case 'e':
504  ro.mode = RZ_SEARCH_REGEXP;
505  ro.hexstr = 0;
506  rz_list_append(ro.keywords, (void *)opt.arg);
507  break;
508  case 'E':
509  ro.mode = RZ_SEARCH_ESIL;
510  rz_list_append(ro.keywords, (void *)opt.arg);
511  break;
512  case 's':
513  ro.mode = RZ_SEARCH_KEYWORD;
514  ro.hexstr = false;
515  ro.widestr = false;
516  rz_list_append(ro.keywords, (void *)opt.arg);
517  break;
518  case 'w':
519  ro.mode = RZ_SEARCH_KEYWORD;
520  ro.hexstr = false;
521  ro.widestr = true;
522  rz_list_append(ro.keywords, (void *)opt.arg);
523  break;
524  case 'I':
525  ro.import = true;
526  rz_list_append(ro.keywords, (void *)opt.arg);
527  break;
528  case 'S':
529  ro.symbol = true;
530  rz_list_append(ro.keywords, (void *)opt.arg);
531  break;
532  case 'b':
533  ro.bsize = rz_num_math(NULL, opt.arg);
534  break;
535  case 'M':
536  // XXX should be from hexbin
537  ro.mask = opt.arg;
538  break;
539  case 'f':
540  ro.from = rz_num_math(NULL, opt.arg);
541  break;
542  case 'F': {
543  size_t data_size;
544  char *data = rz_file_slurp(opt.arg, &data_size);
545  if (!data) {
546  eprintf("Cannot slurp '%s'\n", opt.arg);
547  return 1;
548  }
549  char *hexdata = rz_hex_bin2strdup((ut8 *)data, data_size);
550  if (hexdata) {
551  ro.mode = RZ_SEARCH_KEYWORD;
552  ro.hexstr = true;
553  ro.widestr = false;
554  rz_list_append(ro.keywords, (void *)hexdata);
555  }
556  free(data);
557  } break;
558  case 't':
559  ro.to = rz_num_math(NULL, opt.arg);
560  break;
561  case 'x':
562  ro.mode = RZ_SEARCH_KEYWORD;
563  ro.hexstr = 1;
564  ro.widestr = 0;
565  rz_list_append(ro.keywords, (void *)opt.arg);
566  break;
567  case 'X':
568  ro.pr = rz_print_new();
569  break;
570  case 'q':
571  ro.quiet = true;
572  break;
573  case 'v':
574  return rz_main_version_print("rz-find");
575  case 'h':
576  return show_help(argv[0], 0);
577  case 'z':
578  ro.mode = RZ_SEARCH_STRING;
579  break;
580  case 'Z':
581  ro.showstr = true;
582  break;
583  default:
584  return show_help(argv[0], 1);
585  }
586  }
587  if (opt.ind == argc) {
588  return show_help(argv[0], 1);
589  }
590  /* Enable quiet mode if searching just a single file */
591  if (opt.ind + 1 == argc && RZ_STR_ISNOTEMPTY(argv[opt.ind]) && !rz_file_is_directory(argv[opt.ind])) {
592  ro.quiet = true;
593  }
594  if (ro.json) {
595  printf("[");
596  }
597  for (; opt.ind < argc; opt.ind++) {
598  file = argv[opt.ind];
599 
600  if (RZ_STR_ISEMPTY(file)) {
601  eprintf("Cannot open empty path\n");
603  return 1;
604  }
605  rzfind_open(&ro, file);
606  }
608  if (ro.json) {
609  printf("]\n");
610  }
611  return 0;
612 }
static int show_help(const char *argv0, int line)
Definition: rz-find.c:173
static int rzfind_open(RzfindOptions *ro, const char *file)
Definition: rz-find.c:456
static void rzfind_options_init(RzfindOptions *ro)
Definition: rz-find.c:48
RZ_API RzPrint * rz_print_new(void)
Definition: print.c:56
RZ_API char * rz_hex_bin2strdup(const ut8 *in, int len)
Definition: hex.c:415
@ RZ_SEARCH_REGEXP
Definition: rz_search.h:18
@ RZ_SEARCH_STRING
Definition: rz_search.h:20
@ RZ_SEARCH_ESIL
Definition: rz_search.h:16
@ RZ_SEARCH_MAGIC
Definition: rz_search.h:25
@ RZ_SEARCH_KEYWORD
Definition: rz_search.h:17
bool import
Definition: rz-find.c:22
bool showstr
Definition: rz-find.c:19
bool symbol
Definition: rz-find.c:23
bool hexstr
Definition: rz-find.c:25
bool rad
Definition: rz-find.c:20
const char * mask
Definition: rz-find.c:38
RzList * keywords
Definition: rz-find.c:37
ut64 bsize
Definition: rz-find.c:32
bool widestr
Definition: rz-find.c:26
bool nonstop
Definition: rz-find.c:27
RzPrint * pr
Definition: rz-find.c:36
bool quiet
Definition: rz-find.c:24
ut64 from
Definition: rz-find.c:33
bool identify
Definition: rz-find.c:21
bool json
Definition: rz-find.c:28

References RzfindOptions::align, rz_getopt_t::arg, argv, RzfindOptions::bsize, c, eprintf, free(), RzfindOptions::from, RzfindOptions::hexstr, RzfindOptions::identify, RzfindOptions::import, rz_getopt_t::ind, RzfindOptions::json, RzfindOptions::keywords, RzfindOptions::mask, RzfindOptions::mode, RzfindOptions::nonstop, NULL, RzfindOptions::pr, printf(), RzfindOptions::quiet, RzfindOptions::rad, rz_file_is_directory(), rz_file_slurp(), rz_getopt_init(), rz_getopt_next(), rz_hex_bin2strdup(), rz_list_append(), rz_list_free(), rz_main_version_print(), rz_num_math(), rz_print_new(), RZ_SEARCH_ESIL, RZ_SEARCH_KEYWORD, RZ_SEARCH_MAGIC, RZ_SEARCH_REGEXP, RZ_SEARCH_STRING, RZ_STR_ISEMPTY, RZ_STR_ISNOTEMPTY, rzfind_open(), rzfind_options_init(), show_help(), RzfindOptions::showstr, RzfindOptions::symbol, RzfindOptions::to, and RzfindOptions::widestr.

Referenced by MAIN_NAME(), and rz_main_rizin().

◆ rz_main_rz_gg()

RZ_API int rz_main_rz_gg ( int  argc,
const char **  argv 
)

Definition at line 116 of file rz-gg.c.

116  {
117  const char *file = NULL;
118  const char *padding = NULL;
119  const char *pattern = NULL;
120  const char *str = NULL;
121  char *bytes = NULL;
122  const char *contents = NULL;
123  const char *arch = RZ_SYS_ARCH;
124  const char *os = RZ_EGG_OS_NAME;
125  const char *format = "raw";
126  bool show_execute = false;
127  bool show_execute_rop = false;
128  int show_hex = 1;
129  int show_asm = 0;
130  int show_raw = 0;
131  int append = 0;
132  int show_str = 0;
133  ut64 get_offset = 0;
134  const char *shellcode = NULL;
135  const char *encoder = NULL;
136  char *sequence = NULL;
137  int bits = (RZ_SYS_BITS & RZ_SYS_BITS_64) ? 64 : 32;
138  int fmt = 0;
139  const char *ofile = NULL;
140  int ofileauto = 0;
141  RzBuffer *b;
142  int c, i, fd = -1;
143  RzEgg *egg = rz_egg_new();
144 
145  RzGetopt opt;
146  rz_getopt_init(&opt, argc, argv, "n:N:he:a:b:f:o:sxXrk:FOI:Li:c:p:P:B:C:vd:D:w:zq:S:");
147  while ((c = rz_getopt_next(&opt)) != -1) {
148  switch (c) {
149  case 'a':
150  arch = opt.arg;
151  if (!strcmp(arch, "trace")) {
152  show_asm = 1;
153  show_hex = 0;
154  }
155  break;
156  case 'e':
157  encoder = opt.arg;
158  break;
159  case 'b':
160  bits = atoi(opt.arg);
161  break;
162  case 'B':
163  bytes = rz_str_append(bytes, opt.arg);
164  break;
165  case 'C':
166  if (RZ_STR_ISEMPTY(opt.arg)) {
167  eprintf("Cannot open empty contents path\n");
168  free(sequence);
169  rz_egg_free(egg);
170  return 1;
171  }
172  contents = opt.arg;
173  break;
174  case 'w': {
175  char *arg = strdup(opt.arg);
176  char *p = strchr(arg, ':');
177  if (p) {
178  int len, off;
179  ut8 *b;
180  *p++ = 0;
181  off = rz_num_math(NULL, arg);
182  b = malloc(strlen(opt.arg) + 1);
183  len = rz_hex_str2bin(p, b);
184  if (len > 0) {
185  rz_egg_patch(egg, off, (const ut8 *)b, len);
186  } else {
187  eprintf("Invalid hexstr for -w\n");
188  }
189  free(b);
190  } else {
191  eprintf("Missing colon in -w\n");
192  }
193  free(arg);
194  } break;
195  case 'n':
196  case 'N': {
197  ut64 n = rz_num_math(NULL, opt.arg);
198  // TODO: support big endian too
199  // (this is always little because rz_egg_setup is further below)
200  rz_egg_patch_num(egg, -1, n, c == 'N' ? 64 : 32);
201  append = 1;
202  } break;
203  case 'd':
204  case 'D': {
205  char *p = strchr(opt.arg, ':');
206  if (p) {
207  *p = '\0';
208  ut64 n, off = rz_num_math(NULL, opt.arg);
209  *p = ':';
210  n = rz_num_math(NULL, p + 1);
211  // TODO: support big endian too
212  // (this is always little because rz_egg_setup is further below)
213  rz_egg_patch_num(egg, off, n, c == 'D' ? 64 : 32);
214  } else {
215  eprintf("Missing colon in -%c\n", c);
216  }
217  } break;
218  case 'S':
219  str = opt.arg;
220  break;
221  case 'o':
222  ofile = opt.arg;
223  break;
224  case 'O':
225  ofileauto = 1;
226  break;
227  case 'I':
228  if (RZ_STR_ISEMPTY(opt.arg)) {
229  eprintf("Cannot open empty include path\n");
230  free(sequence);
231  rz_egg_free(egg);
232  return 1;
233  }
234  rz_egg_lang_include_path(egg, opt.arg);
235  break;
236  case 'i':
237  shellcode = opt.arg;
238  break;
239  case 'p':
240  padding = opt.arg;
241  break;
242  case 'P':
243  pattern = opt.arg;
244  break;
245  case 'c': {
246  char *p = strchr(opt.arg, '=');
247  if (p) {
248  *p++ = 0;
249  rz_egg_option_set(egg, opt.arg, p);
250  } else {
251  rz_egg_option_set(egg, opt.arg, "true");
252  }
253  } break;
254  case 'F':
255 #if __APPLE__
256  format = "mach0";
257 #elif __WINDOWS__
258  format = "pe";
259 #else
260  format = "elf";
261 #endif
262  show_asm = 0;
263  break;
264  case 'f':
265  format = opt.arg;
266  show_asm = 0;
267  break;
268  case 's':
269  show_asm = 1;
270  show_hex = 0;
271  break;
272  case 'k':
273  os = opt.arg;
274  break;
275  case 'r':
276  show_raw = 1;
277  break;
278  case 'x':
279  // execute
280  show_execute = true;
281  break;
282  case 'X':
283  // execute rop chain
284  show_execute = 1;
285  show_execute_rop = 1;
286  break;
287  case 'L':
288  list(egg);
289  rz_egg_free(egg);
290  free(sequence);
291  return 0;
292  case 'h':
293  rz_egg_free(egg);
294  free(sequence);
295  return usage(1);
296  case 'v':
297  free(sequence);
298  rz_egg_free(egg);
299  return rz_main_version_print("rz-gg");
300  case 'z':
301  show_str = 1;
302  break;
303  case 'q':
304  get_offset = 1;
305  sequence = strdup(opt.arg);
306  break;
307  default:
308  free(sequence);
309  rz_egg_free(egg);
310  return 1;
311  }
312  }
313 
314  if (opt.ind == argc && !shellcode && !bytes && !contents && !encoder && !padding && !pattern && !append && !get_offset && !str) {
315  free(sequence);
316  rz_egg_free(egg);
317  return usage(0);
318  } else {
319  file = argv[opt.ind];
320  }
321 
322  if (bits == 64) {
323  if (!strcmp(format, "mach0")) {
324  format = "mach064";
325  } else if (!strcmp(format, "elf")) {
326  format = "elf64";
327  }
328  }
329 
330  // catch this first
331  if (get_offset) {
332  if (strncmp(sequence, "0x", 2)) {
333  eprintf("Need hex value with `0x' prefix e.g. 0x41414142\n");
334  free(sequence);
335  rz_egg_free(egg);
336  return 1;
337  }
338 
339  get_offset = rz_num_math(0, sequence);
340  printf("Little endian: %d\n", rz_debruijn_offset(0, NULL, get_offset, false));
341  printf("Big endian: %d\n", rz_debruijn_offset(0, NULL, get_offset, true));
342  free(sequence);
343  rz_egg_free(egg);
344  return 0;
345  }
346 
347  // initialize egg
348  rz_egg_setup(egg, arch, bits, 0, os);
349  if (file) {
350  if (RZ_STR_ISEMPTY(file)) {
351  eprintf("Cannot open empty path\n");
352  goto fail;
353  }
354  if (!strcmp(file, "-")) {
355  char buf[1024];
356  for (;;) {
357  if (!fgets(buf, sizeof(buf), stdin)) {
358  break;
359  }
360  if (feof(stdin)) {
361  break;
362  }
363  rz_egg_load(egg, buf, 0);
364  }
365  } else {
366  if (!rz_egg_load_file(egg, file)) {
367  eprintf("Cannot load file \"%s\"\n", file);
368  }
369  }
370  }
371 
372  // compile source code to assembly
373  if (!rz_egg_compile(egg)) {
374  if (!fmt) {
375  eprintf("rz_egg_compile: fail\n");
376  free(sequence);
377  rz_egg_free(egg);
378  return 1;
379  }
380  }
381 
382  // append the provided string
383  if (str) {
384  int l = strlen(str);
385  if (l > 0) {
386  rz_egg_raw(egg, (const ut8 *)str, l);
387  }
388  }
389 
390  // add raw file
391  if (contents) {
392  size_t l;
393  char *buf = rz_file_slurp(contents, &l);
394  if (buf && l > 0) {
395  rz_egg_raw(egg, (const ut8 *)buf, (int)l);
396  } else {
397  eprintf("Error loading '%s'\n", contents);
398  }
399  free(buf);
400  }
401 
402  // add shellcode
403  if (shellcode) {
404  if (!rz_egg_shellcode(egg, shellcode)) {
405  eprintf("Unknown shellcode '%s'\n", shellcode);
406  goto fail;
407  }
408  }
409 
410  // add raw bytes
411  if (bytes) {
412  ut8 *b = malloc(strlen(bytes) + 1);
413  int len = rz_hex_str2bin(bytes, b);
414  if (len > 0) {
415  if (!rz_egg_raw(egg, b, len)) {
416  eprintf("Unknown '%s'\n", shellcode);
417  free(b);
418  goto fail;
419  }
420  } else {
421  eprintf("Invalid hexpair string for -B\n");
422  }
423  free(b);
424  free(bytes);
425  bytes = NULL;
426  }
427 
428  /* set output (create output file if needed) */
429  if (ofileauto) {
430  if (file) {
431  char *o, *q, *p = strdup(file);
432  if ((o = strchr(p, '.'))) {
433  while ((q = strchr(o + 1, '.'))) {
434  o = q;
435  }
436  *o = 0;
437  fd = openfile(p, ISEXEC);
438  } else {
439  fd = openfile("a.out", ISEXEC);
440  }
441  free(p);
442  } else {
443  fd = openfile("a.out", ISEXEC);
444  }
445  if (fd == -1) {
446  eprintf("cannot open file '%s'\n", opt.arg);
447  goto fail;
448  }
449  close(fd);
450  }
451  if (ofile) {
452  fd = openfile(ofile, ISEXEC);
453  if (fd == -1) {
454  eprintf("cannot open file '%s'\n", ofile);
455  goto fail;
456  }
457  }
458 
459  // assemble to binary
460  if (!rz_egg_assemble(egg)) {
461  eprintf("rz_egg_assemble: invalid assembly\n");
462  goto fail;
463  }
464  if (encoder) {
465  if (!rz_egg_encode(egg, encoder)) {
466  eprintf("Invalid encoder '%s'\n", encoder);
467  goto fail;
468  }
469  }
470 
471  // add padding
472  if (padding) {
473  rz_egg_padding(egg, padding);
474  }
475 
476  // add pattern
477  if (pattern) {
478  rz_egg_pattern(egg, rz_num_math(NULL, pattern));
479  }
480 
481  // apply patches
482  if (!egg->bin) {
483  egg->bin = rz_buf_new_with_bytes(NULL, 0);
484  }
485  if (!rz_egg_get_bin(egg)) {
486  eprintf("rz_egg_get_bin: invalid egg :(\n");
487  goto fail;
488  }
489  rz_egg_finalize(egg);
490 
491  if (show_asm) {
492  printf("%s\n", rz_egg_get_assembly(egg));
493  }
494 
495  if (show_raw || show_hex || show_execute) {
496  if (show_execute) {
497  int r;
498  if (show_execute_rop) {
499  r = rz_egg_run_rop(egg);
500  } else {
501  r = rz_egg_run(egg);
502  }
503  rz_egg_free(egg);
504  return r;
505  }
506  b = rz_egg_get_bin(egg);
507  if (show_raw) {
508  ut64 blen;
509  const ut8 *tmp = rz_buf_data(b, &blen);
510  if (write(1, tmp, blen) != blen) {
511  eprintf("Failed to write buffer\n");
512  goto fail;
513  }
514  } else {
515  if (!format) {
516  eprintf("No format specified\n");
517  goto fail;
518  }
519  char *code = NULL;
520  ut64 tmpsz;
521  const ut8 *tmp = rz_buf_data(b, &tmpsz);
522  switch (*format) {
523  case 'c':
525  printf("%s\n", code);
526  free(code);
527  break;
528  case 'j': // json
530  printf("%s\n", code);
531  free(code);
532  break;
533  case 'r':
534  if (show_str) {
535  printf("\"");
536  for (i = 0; i < tmpsz; i++) {
537  printf("\\x%02x", tmp[i]);
538  }
539  printf("\"\n");
540  } else if (show_hex) {
541  rz_buf_seek(b, 0, RZ_BUF_SET);
542  for (i = 0; i < tmpsz; i++) {
543  printf("%02x", tmp[i]);
544  }
545  printf("\n");
546  } // else show_raw is_above()
547  break;
548  case 'p': // PE
549  if (strlen(format) >= 2 && format[1] == 'y') { // Python
551  printf("%s\n", code);
552  free(code);
553  }
554  break;
555  case 'e': // ELF
556  case 'm': // MACH0
557  create(format, arch, bits, tmp, tmpsz);
558  break;
559  default:
560  eprintf("unknown executable format (%s)\n", format);
561  goto fail;
562  }
563  }
564  }
565  if (fd != -1) {
566  close(fd);
567  }
568  free(sequence);
569  rz_egg_free(egg);
570  return 0;
571 fail:
572  if (fd != -1) {
573  close(fd);
574  }
575  free(sequence);
576  rz_egg_free(egg);
577  return 1;
578 }
size_t len
Definition: 6502dis.c:15
static ut8 bytes[32]
Definition: asm_arc.c:23
static ut64 get_offset(RzBinFile *bf, int type, int index)
Definition: bin_dex.c:206
static char * ofile
Definition: cmd_magic.c:9
#define append(x, y)
Definition: cmd_print.c:1740
static static fork write
Definition: sflib.h:33
RZ_API int rz_egg_shellcode(RzEgg *egg, const char *name)
Definition: egg.c:538
RZ_API int rz_egg_run_rop(RzEgg *egg)
Definition: egg.c:450
RZ_API void rz_egg_free(RzEgg *egg)
Definition: egg.c:111
RZ_API void rz_egg_finalize(RzEgg *egg)
Definition: egg.c:598
RZ_API RzEgg * rz_egg_new(void)
Definition: egg.c:44
RZ_API bool rz_egg_patch_num(RzEgg *egg, int off, ut64 num, ut32 bits)
Definition: egg.c:591
RZ_API bool rz_egg_load_file(RzEgg *egg, const char *file)
Definition: egg.c:214
RZ_API int rz_egg_padding(RzEgg *egg, const char *pad)
Definition: egg.c:472
RZ_API int rz_egg_run(RzEgg *egg)
Definition: egg.c:443
RZ_API int rz_egg_patch(RzEgg *egg, int off, const ut8 *buf, int len)
Definition: egg.c:576
RZ_API void rz_egg_pattern(RzEgg *egg, int size)
Definition: egg.c:625
RZ_API char * rz_egg_get_assembly(RzEgg *egg)
Definition: egg.c:434
RZ_API int rz_egg_encode(RzEgg *egg, const char *name)
Definition: egg.c:558
RZ_API int rz_egg_compile(RzEgg *egg)
Definition: egg.c:394
RZ_API RzBuffer * rz_egg_get_bin(RzEgg *egg)
Definition: egg.c:423
RZ_API void rz_egg_option_set(RzEgg *egg, const char *key, const char *val)
Definition: egg.c:530
RZ_API void rz_egg_load(RzEgg *egg, const char *code, int format)
Definition: egg.c:202
RZ_API bool rz_egg_assemble(RzEgg *egg)
Definition: egg.c:390
RZ_API bool rz_egg_setup(RzEgg *egg, const char *arch, int bits, int endian, const char *os)
Definition: egg.c:139
RZ_API int rz_egg_raw(RzEgg *egg, const ut8 *b, int len)
Definition: egg.c:281
RZ_API void rz_egg_lang_include_path(RzEgg *egg, const char *path)
Definition: egg_lang.c:149
static int openfile(const char *f, int x)
Definition: rz-gg.c:89
static int usage(int v)
Definition: rz-gg.c:10
#define ISEXEC
Definition: rz-gg.c:114
static int create(const char *format, const char *arch, int bits, const ut8 *code, int codelen)
Definition: rz-gg.c:69
int n
Definition: mipsasm.c:19
int off
Definition: pal.c:13
RZ_API st64 rz_buf_seek(RZ_NONNULL RzBuffer *b, st64 addr, int whence)
Modify the current cursor position in the buffer.
Definition: buf.c:1166
#define RZ_BUF_SET
Definition: rz_buf.h:14
RZ_API RZ_OWN RzBuffer * rz_buf_new_with_bytes(RZ_NULLABLE RZ_BORROW const ut8 *bytes, ut64 len)
Creates a new buffer with a bytes array.
Definition: buf.c:465
RZ_API int rz_debruijn_offset(int start, const char *charset, ut64 value, bool is_big_endian)
Finds the offset of a given value in a debrujn sequence.
Definition: debruijn.c:112
#define RZ_EGG_OS_NAME
Definition: rz_egg.h:148
RZ_API RZ_OWN char * rz_lang_byte_array(RZ_NONNULL const ut8 *buffer, size_t size, RzLangByteArrayType type)
@ RZ_LANG_BYTE_ARRAY_C_CPP_BYTES
@ RZ_LANG_BYTE_ARRAY_PYTHON
@ RZ_LANG_BYTE_ARRAY_JSON
@ RZ_SYS_BITS_64
Definition: rz_sys.h:21
#define RZ_SYS_BITS
Definition: rz_types.h:520
#define RZ_SYS_ARCH
Definition: rz_types.h:519
RzBuffer * bin
Definition: rz_egg.h:96
#define fail(test)
Definition: tests.h:29

References append, arch, rz_getopt_t::arg, argv, b, rz_egg_t::bin, bits(), bytes, c, close, create(), eprintf, fail, fd, free(), get_offset(), i, rz_getopt_t::ind, ISEXEC, len, list(), malloc(), n, NULL, off, ofile, openfile(), p, printf(), r, rz_buf_data(), rz_buf_new_with_bytes(), rz_buf_seek(), RZ_BUF_SET, rz_debruijn_offset(), rz_egg_assemble(), rz_egg_compile(), rz_egg_encode(), rz_egg_finalize(), rz_egg_free(), rz_egg_get_assembly(), rz_egg_get_bin(), rz_egg_lang_include_path(), rz_egg_load(), rz_egg_load_file(), rz_egg_new(), rz_egg_option_set(), RZ_EGG_OS_NAME, rz_egg_padding(), rz_egg_patch(), rz_egg_patch_num(), rz_egg_pattern(), rz_egg_raw(), rz_egg_run(), rz_egg_run_rop(), rz_egg_setup(), rz_egg_shellcode(), rz_file_slurp(), rz_getopt_init(), rz_getopt_next(), rz_hex_str2bin(), rz_lang_byte_array(), RZ_LANG_BYTE_ARRAY_C_CPP_BYTES, RZ_LANG_BYTE_ARRAY_JSON, RZ_LANG_BYTE_ARRAY_PYTHON, rz_main_version_print(), rz_num_math(), rz_str_append(), RZ_STR_ISEMPTY, RZ_SYS_ARCH, RZ_SYS_BITS, RZ_SYS_BITS_64, cmd_descs_generate::str, strdup(), autogen_x86imm::tmp, usage(), ut64(), and write.

Referenced by MAIN_NAME(), and rz_main_rizin().

◆ rz_main_rz_hash()

RZ_API int rz_main_rz_hash ( int  argc,
const char **  argv 
)

Definition at line 1205 of file rz-hash.c.

1205  {
1206  int result = 1;
1207  RzHashContext ctx = { 0 };
1208  ctx.rh = rz_hash_new();
1210 
1211  hash_parse_cmdline(argc, argv, &ctx);
1212 
1213  switch (ctx.operation) {
1214  case RZ_HASH_OP_LIST_ALGO:
1216  break;
1217  case RZ_HASH_OP_LUHN:
1219  goto rz_main_rz_hash_end;
1220  }
1221  break;
1222  case RZ_HASH_OP_HASH:
1224  goto rz_main_rz_hash_end;
1225  }
1226  break;
1227  case RZ_HASH_OP_DECRYPT:
1229  goto rz_main_rz_hash_end;
1230  }
1231  break;
1232  case RZ_HASH_OP_ENCRYPT:
1234  goto rz_main_rz_hash_end;
1235  }
1236  break;
1237  case RZ_HASH_OP_VERSION:
1238  rz_main_version_print("rz-hash");
1239  break;
1240  case RZ_HASH_OP_USAGE:
1241  rz_hash_show_help(true);
1242  goto rz_main_rz_hash_end;
1243  case RZ_HASH_OP_ERROR:
1244  goto rz_main_rz_hash_end;
1245  case RZ_HASH_OP_HELP:
1246  result = 0;
1247  /* fall-thru */
1248  default:
1249  rz_hash_show_help(false);
1250  goto rz_main_rz_hash_end;
1251  }
1252 
1253  result = 0;
1254 
1255 rz_main_rz_hash_end:
1257  return result;
1258 }
RZ_API RzHash * rz_hash_new(void)
Definition: hash.c:585
static void hash_context_fini(RzHashContext *ctx)
Definition: rz-hash.c:489
static bool hash_context_run(RzHashContext *ctx, RzHashRun run)
Definition: rz-hash.c:585
static bool calculate_encrypt(RzHashContext *ctx, RzIO *io, const char *filename)
Definition: rz-hash.c:1053
@ RZ_HASH_OP_HASH
Definition: rz-hash.c:36
@ RZ_HASH_OP_HELP
Definition: rz-hash.c:32
@ RZ_HASH_OP_USAGE
Definition: rz-hash.c:33
@ RZ_HASH_OP_VERSION
Definition: rz-hash.c:34
@ RZ_HASH_OP_DECRYPT
Definition: rz-hash.c:37
@ RZ_HASH_OP_LIST_ALGO
Definition: rz-hash.c:35
@ RZ_HASH_OP_LUHN
Definition: rz-hash.c:39
@ RZ_HASH_OP_ENCRYPT
Definition: rz-hash.c:38
@ RZ_HASH_OP_ERROR
Definition: rz-hash.c:31
static void hash_load_plugins(RzHashContext *ctx)
Definition: rz-hash.c:1179
static bool calculate_hash(RzHashContext *ctx, RzIO *io, const char *filename)
Definition: rz-hash.c:800
static void rz_hash_show_help(bool usage_only)
Definition: rz-hash.c:72
static void hash_parse_cmdline(int argc, const char **argv, RzHashContext *ctx)
Definition: rz-hash.c:318
static bool calculate_luhn(RzHashContext *ctx, RzIO *io, const char *filename)
Definition: rz-hash.c:1135
static bool calculate_decrypt(RzHashContext *ctx, RzIO *io, const char *filename)
Definition: rz-hash.c:977
static void rz_hash_show_algorithms(RzHashContext *ctx)
Definition: rz-hash.c:109

References argv, calculate_decrypt(), calculate_encrypt(), calculate_hash(), calculate_luhn(), hash_context_fini(), hash_context_run(), hash_load_plugins(), hash_parse_cmdline(), rz_hash_new(), RZ_HASH_OP_DECRYPT, RZ_HASH_OP_ENCRYPT, RZ_HASH_OP_ERROR, RZ_HASH_OP_HASH, RZ_HASH_OP_HELP, RZ_HASH_OP_LIST_ALGO, RZ_HASH_OP_LUHN, RZ_HASH_OP_USAGE, RZ_HASH_OP_VERSION, rz_hash_show_algorithms(), rz_hash_show_help(), and rz_main_version_print().

Referenced by MAIN_NAME().

◆ rz_main_rz_run()

RZ_API int rz_main_rz_run ( int  argc,
const char **  argv 
)

Definition at line 25 of file rz-run.c.

25  {
26  RzRunProfile *p;
27  int i, ret;
28  if (argc == 1 || !strcmp(argv[1], "-h")) {
29  printf("Usage: rz-run -v|-t|script.rr2 [directive ..]\n");
30  printf("%s", rz_run_help());
31  return 1;
32  }
33  if (!strcmp(argv[1], "-v")) {
34  return rz_main_version_print("rz-run");
35  }
36  const char *file = argv[1];
37  if (!strcmp(file, "-t")) {
38 #if __UNIX__
39  rz_run_tty();
40  return 0;
41 #else
42  eprintf("Not supported\n");
43  return 1;
44 #endif
45  }
46  if (*file && !strchr(file, '=')) {
47  p = rz_run_new(file);
48  } else {
49  bool noMoreDirectives = false;
50  int directiveIndex = 0;
51  p = rz_run_new(NULL);
52  for (i = *file ? 1 : 2; i < argc; i++) {
53  if (!strcmp(argv[i], "--")) {
54  noMoreDirectives = true;
55  continue;
56  }
57  if (noMoreDirectives) {
58  const char *word = argv[i];
59  char *line = directiveIndex
60  ? rz_str_newf("arg%d=%s", directiveIndex, word)
61  : rz_str_newf("program=%s", word);
63  directiveIndex++;
64  free(line);
65  } else {
67  }
68  }
69  }
70  if (!p) {
71  return 1;
72  }
73  ret = rz_run_config_env(p);
74  if (ret) {
75  printf("error while configuring the environment.\n");
76  rz_run_free(p);
77  return 1;
78  }
79  ret = rz_run_start(p);
80  rz_run_free(p);
81  return ret;
82 }
line
Definition: setup.py:34
RZ_API void rz_run_free(RzRunProfile *r)
Definition: run.c:122
RZ_API RzRunProfile * rz_run_new(const char *str)
Definition: run.c:86
RZ_API const char * rz_run_help(void)
Definition: run.c:630
RZ_API bool rz_run_parseline(RzRunProfile *p, const char *b)
Definition: run.c:479
RZ_API int rz_run_start(RzRunProfile *p)
Definition: run.c:1081
RZ_API int rz_run_config_env(RzRunProfile *p)
Definition: run.c:809

References argv, eprintf, free(), i, setup::line, NULL, p, printf(), rz_main_version_print(), rz_run_config_env(), rz_run_free(), rz_run_help(), rz_run_new(), rz_run_parseline(), rz_run_start(), and rz_str_newf().

Referenced by MAIN_NAME().

◆ rz_main_rz_sign()

RZ_API int rz_main_rz_sign ( int  argc,
const char **  argv 
)

Definition at line 40 of file rz-sign.c.

40  {
41  RzListIter *it;
42  const char *input_file = NULL;
43  const char *output_file = NULL;
44  char *config = NULL;
45  RzCore *core = NULL;
46  RzList *evars = NULL;
47  bool quiet = false;
48  RzGetopt opt;
49  int ret = 0;
50  ut32 n_nodes = 0;
51 
52  int c, option = RZ_SIGN_OPT_NONE;
53  size_t complexity = 0;
54 
55  evars = rz_list_newf(free);
56  if (!evars) {
57  RZ_LOG_ERROR("rz-sign: cannot allocate RzList\n");
58  return -1;
59  }
60 
61  rz_getopt_init(&opt, argc, argv, "aqhdc:o:e:v");
62  while ((c = rz_getopt_next(&opt)) != -1) {
63  switch (c) {
64  case 'a':
65  complexity++;
66  break;
67  case 'c':
68  if (option != RZ_SIGN_OPT_NONE) {
69  RZ_LOG_ERROR("rz-sign: cannot combine option -%c with previous options\n", c);
70  ret = -1;
71  goto rz_sign_end;
72  }
74  output_file = opt.arg;
75  break;
76  case 'o':
77  if (option != RZ_SIGN_OPT_NONE) {
78  RZ_LOG_ERROR("rz-sign: cannot combine option -%c with previous options\n", c);
79  ret = -1;
80  goto rz_sign_end;
81  }
83  output_file = opt.arg;
84  break;
85  case 'd':
86  if (option != RZ_SIGN_OPT_NONE) {
87  RZ_LOG_ERROR("rz-sign: cannot combine option -%c with previous options\n", c);
88  ret = -1;
89  goto rz_sign_end;
90  }
92  break;
93  case 'e':
94  if (!(config = rz_str_new(opt.arg)) || !rz_list_append(evars, config)) {
95  free(config);
96  RZ_LOG_ERROR("rz-sign: cannot add evaluable config variable '%s' to RzList\n", opt.arg);
97  ret = -1;
98  goto rz_sign_end;
99  }
100  break;
101  case 'q':
102  quiet = true;
103  break;
104  case 'v':
105  return rz_main_version_print("rz-sign");
106  case 'h':
108  goto rz_sign_end;
109  default:
110  RZ_LOG_ERROR("rz-sign: invalid option -%c\n", c);
112  ret = -1;
113  goto rz_sign_end;
114  }
115  }
116 
117  if (opt.ind >= argc) {
118  RZ_LOG_ERROR("rz-sign: input file was not provided\n");
120  ret = -1;
121  goto rz_sign_end;
122  }
123 
124  input_file = argv[opt.ind];
125 
126  if (option == RZ_SIGN_OPT_CREATE_FLIRT && complexity > 2) {
127  RZ_LOG_ERROR("rz-sign: Invalid analysis complexity (too many -a defined, max -aa)\n");
129  ret = -1;
130  goto rz_sign_end;
131  }
132 
133  core = rz_core_new();
134  if (!core) {
135  RZ_LOG_ERROR("rz-sign: Cannot allocate RzCore\n");
136  ret = -1;
137  goto rz_sign_end;
138  }
139  rz_config_set_b(core->config, "scr.interactive", false);
140  rz_config_set_b(core->config, "analysis.apply.signature", false);
141  rz_cons_reset();
143 
144  rz_core_loadlibs(core, RZ_CORE_LOADLIBS_ALL);
145 
146  if (!rz_core_file_open(core, input_file, 0, 0)) {
147  RZ_LOG_ERROR("rz-sign: Could not open file %s\n", input_file);
148  ret = -1;
149  goto rz_sign_end;
150  }
151 
152  (void)rz_core_bin_load(core, NULL, UT64_MAX);
153  (void)rz_core_bin_update_arch_bits(core);
154 
155  // quiet mode
156  if (quiet) {
157  rz_config_set_b(core->config, "scr.prompt", false);
158  rz_config_set_i(core->config, "scr.color", COLOR_MODE_DISABLED);
159  }
160 
161  // set all evars
162  rz_list_foreach (evars, it, config) {
163  if (!rz_config_eval(core->config, config)) {
164  RZ_LOG_ERROR("rz-sign: invalid option '%s'\n", config);
165  ret = -1;
166  goto rz_sign_end;
167  }
168  }
169 
170  switch (option) {
172  // convert a flirt file from .pat to .sig or viceversa
173  if (!rz_core_flirt_convert_file(core, input_file, output_file)) {
174  ret = -1;
175  } else if (!quiet) {
176  rz_cons_printf("rz-sign: %s was converted to %s.\n", input_file, output_file);
177  }
178  break;
180  // run analysis to find functions
181  perform_analysis(core, complexity);
182  // create flirt file
183  if (!rz_core_flirt_create_file(core, output_file, &n_nodes)) {
184  ret = -1;
185  } else if (!quiet) {
186  rz_cons_printf("rz-sign: written %u signatures to %s.\n", n_nodes, output_file);
187  }
188  break;
190  if (!rz_core_flirt_dump_file(input_file)) {
191  ret = -1;
192  }
193  break;
194  default:
195  RZ_LOG_ERROR("rz-sign: missing option, please set -c or -d or -o\n");
196  ret = -1;
197  break;
198  }
199  rz_cons_flush();
200 
201 rz_sign_end:
202  rz_list_free(evars);
203  rz_core_free(core);
204  return ret;
205 }
RZ_API RzConfigNode * rz_config_set_b(RzConfig *cfg, RZ_NONNULL const char *name, bool value)
Definition: config.c:201
RZ_API void rz_cons_set_interactive(bool x)
Definition: cons.c:1724
RZ_API bool rz_core_flirt_convert_file(RZ_NONNULL RzCore *core, RZ_NONNULL const char *input_file, RZ_NONNULL const char *output_file)
converts a FLIRT file to the other format.
Definition: csign.c:510
RZ_API bool rz_core_flirt_create_file(RZ_NONNULL RzCore *core, RZ_NONNULL const char *output_file, RZ_NULLABLE ut32 *written_nodes)
Generates a new FLIRT file from a given RzCore structure.
Definition: csign.c:431
RZ_API bool rz_core_flirt_dump_file(RZ_NONNULL const char *flirt_file)
Dumps the contents of a FLIRT file.
Definition: csign.c:372
@ RZ_SIGN_OPT_DUMP_FLIRT
Definition: rz-sign.c:11
@ RZ_SIGN_OPT_CONVERT_FLIRT
Definition: rz-sign.c:9
@ RZ_SIGN_OPT_CREATE_FLIRT
Definition: rz-sign.c:10
@ RZ_SIGN_OPT_NONE
Definition: rz-sign.c:8
static void rz_sign_show_help(void)
Definition: rz-sign.c:14
static void perform_analysis(RzCore *core, size_t complexity)
Definition: rz-sign.c:30
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 char * rz_str_new(const char *str)
Definition: str.c:865
Definition: getopt.h:84

References rz_getopt_t::arg, argv, c, COLOR_MODE_DISABLED, rz_core_t::config, free(), rz_getopt_t::ind, NULL, perform_analysis(), rz_config_eval(), rz_config_set_b(), rz_config_set_i(), rz_cons_flush(), rz_cons_printf(), rz_cons_reset(), rz_cons_set_interactive(), rz_core_bin_load(), rz_core_bin_update_arch_bits(), rz_core_file_open(), rz_core_flirt_convert_file(), rz_core_flirt_create_file(), rz_core_flirt_dump_file(), rz_core_free(), rz_core_loadlibs(), rz_core_new(), rz_getopt_init(), rz_getopt_next(), rz_list_append(), rz_list_free(), rz_list_newf(), RZ_LOG_ERROR, rz_main_version_print(), RZ_SIGN_OPT_CONVERT_FLIRT, RZ_SIGN_OPT_CREATE_FLIRT, RZ_SIGN_OPT_DUMP_FLIRT, RZ_SIGN_OPT_NONE, rz_sign_show_help(), rz_str_new(), and UT64_MAX.

Referenced by MAIN_NAME().

◆ rz_main_version_print()

RZ_API int rz_main_version_print ( const char *  program)

Definition at line 49 of file main.c.

49  {
50  char *s = rz_str_version(progname);
51  printf("%s\n", s);
52  free(s);
53  return 0;
54 }
static Sdb * s
Definition: main.c:15
RZ_API char * rz_str_version(const char *program)
Definition: str.c:4051
#define progname

References free(), printf(), progname, rz_str_version(), and s.

Referenced by rax(), rz_main_rizin(), rz_main_rz_asm(), rz_main_rz_bin(), rz_main_rz_diff(), rz_main_rz_find(), rz_main_rz_gg(), rz_main_rz_hash(), rz_main_rz_run(), rz_main_rz_sign(), and showversion().