Rizin
unix-like reverse engineering framework and cli tools
file.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2007-2020 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include "rz_types.h"
5 #include "rz_util.h"
6 #include <stdio.h>
7 #include <time.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <rz_lib.h>
12 #if __UNIX__
13 #include <sys/time.h>
14 #include <sys/mman.h>
15 #include <limits.h>
16 #endif
17 #if HAVE_COPYFILE
18 #include <copyfile.h>
19 #endif
20 #if _MSC_VER
21 #include <rz_windows.h>
22 #include <process.h>
23 #endif
24 
25 #define BS 1024
26 #ifdef __WINDOWS__
27 #define StructStat struct _stat64
28 #else
29 #define StructStat struct stat
30 #endif
31 
32 static int file_stat(const char *file, StructStat *pStat) {
33  rz_return_val_if_fail(file && pStat, -1);
34 #if __WINDOWS__
35  wchar_t *wfile = rz_utf8_to_utf16(file);
36  if (!wfile) {
37  return -1;
38  }
39  int ret = _wstati64(wfile, pStat);
40  free(wfile);
41  return ret;
42 #else // __WINDOWS__
43  return stat(file, pStat);
44 #endif // __WINDOWS__
45 }
46 
47 RZ_API bool rz_file_truncate(const char *filename, ut64 newsize) {
49  int fd;
51  return false;
52  }
54  return false;
55  }
56 #if __WINDOWS__
57  fd = rz_sys_open(filename, O_RDWR, 0644);
58 #else
59  fd = rz_sys_open(filename, O_RDWR | O_SYNC, 0644);
60 #endif
61  if (fd == -1) {
62  return false;
63  }
64 #ifdef _MSC_VER
65  int r = _chsize(fd, newsize);
66 #else
67  int r = ftruncate(fd, newsize);
68 #endif
69  if (r != 0) {
70  eprintf("Could not resize %s file\n", filename);
71  close(fd);
72  return false;
73  }
74  close(fd);
75  return true;
76 }
77 
78 /*
79 Example:
80  str = rz_file_basename ("home/inisider/Downloads/user32.dll");
81  // str == user32.dll
82 */
83 RZ_API const char *rz_file_basename(const char *path) {
85  const char *ptr = rz_str_rchr(path, NULL, '/');
86  if (ptr) {
87  path = ptr + 1;
88  }
89 #if __WINDOWS__
90  if ((ptr = rz_str_rchr(path, NULL, '\\'))) {
91  path = ptr + 1;
92  }
93 #endif
94  return path;
95 }
96 
97 /* \brief Returns file name from a path accepting both `/` and `\` as directory separators
98  *
99  * \param path Path of file to get the file name
100  * \return const char * Pointer to the file name
101  */
104  const char *ptr = rz_str_rchr(path, NULL, '/');
105  if (ptr) {
106  path = ptr + 1;
107  }
108  if ((ptr = rz_str_rchr(path, NULL, '\\'))) {
109  path = ptr + 1;
110  }
111  return path;
112 }
113 
114 /*
115 Example:
116  str = rz_file_dirname ("home/inisider/Downloads/user32.dll");
117  // str == "home/inisider/Downloads"
118  free (str);
119 */
120 RZ_API char *rz_file_dirname(const char *path) {
122  char *newpath = strdup(path);
123  char *ptr = (char *)rz_str_rchr(newpath, NULL, '/');
124  if (ptr) {
125  if (ptr == newpath) {
126  ptr++;
127  }
128  *ptr = 0;
129  } else {
130  ptr = (char *)rz_str_rchr(newpath, NULL, '\\');
131  if (!ptr) {
132  ptr = newpath;
133  }
134  if (ptr && ptr == newpath && *ptr == '.') { // keep '.'
135  ptr++;
136  if (*ptr == '.') { // keep '..'
137  ptr++;
138  }
139  }
140  if (ptr) {
141  *ptr = 0;
142  }
143  }
144  return newpath;
145 }
146 
147 RZ_API bool rz_file_is_c(const char *file) {
148  rz_return_val_if_fail(file, false);
149  const char *ext = rz_str_lchr(file, '.'); // TODO: add api in rz_file_extension or rz_str_ext for this
150  if (ext) {
151  ext++;
152  if (!strcmp(ext, "cparse") || !strcmp(ext, "c") || !strcmp(ext, "h")) {
153  return true;
154  }
155  }
156  return false;
157 }
158 
159 RZ_API bool rz_file_is_regular(const char *str) {
160  StructStat buf = { 0 };
161  if (!str || !*str || file_stat(str, &buf) == -1) {
162  return false;
163  }
164  return ((S_IFREG & buf.st_mode) == S_IFREG);
165 }
166 
167 RZ_API bool rz_file_is_directory(const char *str) {
168  StructStat buf = { 0 };
170  if (file_stat(str, &buf) == -1) {
171  return false;
172  }
173 #ifdef S_IFBLK
174  if ((S_IFBLK & buf.st_mode) == S_IFBLK) {
175  return false;
176  }
177 #endif
178  return S_IFDIR == (S_IFDIR & buf.st_mode);
179 }
180 
181 RZ_API bool rz_file_fexists(const char *fmt, ...) {
182  int ret;
183  char string[BS];
184  va_list ap;
185  va_start(ap, fmt);
186  vsnprintf(string, sizeof(string), fmt, ap);
187  ret = rz_file_exists(string);
188  va_end(ap);
189  return ret;
190 }
191 
192 RZ_API bool rz_file_exists(const char *str) {
194  char *absfile = rz_file_abspath(str);
195  StructStat buf = { 0 };
196 
197  if (file_stat(absfile, &buf) == -1) {
198  free(absfile);
199  return false;
200  }
201  free(absfile);
202  return S_IFREG == (S_IFREG & buf.st_mode);
203 }
204 
205 RZ_API ut64 rz_file_size(const char *str) {
207  StructStat buf = { 0 };
208  if (file_stat(str, &buf) == -1) {
209  return 0;
210  }
211  return (ut64)buf.st_size;
212 }
213 
214 RZ_API bool rz_file_is_abspath(const char *file) {
216  return ((*file && file[1] == ':') || *file == '/');
217 }
218 
219 RZ_API char *rz_file_abspath_rel(const char *cwd, const char *file) {
220  char *ret = NULL;
221  if (RZ_STR_ISEMPTY(file) || !strcmp(file, ".") || !strcmp(file, "./")) {
222  return strdup(cwd);
223  }
224  if (strstr(file, "://")) {
225  return strdup(file);
226  }
227  ret = rz_path_home_expand(file);
228 #if __UNIX__
229  if (cwd && *ret != '/') {
230  char *tmp = rz_str_newf("%s" RZ_SYS_DIR "%s", cwd, ret);
231  if (!tmp) {
232  free(ret);
233  return NULL;
234  }
235  free(ret);
236  ret = tmp;
237  }
238 #elif __WINDOWS__
239  // Network path
240  if (!strncmp(ret, "\\\\", 2)) {
241  return strdup(ret);
242  }
243  if (!strchr(ret, ':')) {
244  char *tmp = rz_str_newf("%s" RZ_SYS_DIR "%s", cwd, ret);
245  if (!tmp) {
246  free(ret);
247  return NULL;
248  }
249  free(ret);
250  ret = tmp;
251  }
252 #endif
253 #if HAVE_REALPATH
254  char rp[PATH_MAX] = { 0 };
255  char *abspath = realpath(ret, rp); // second arg == NULL is only an extension
256  if (abspath) {
257  abspath = strdup(abspath);
258  if (abspath) {
259  free(ret);
260  ret = abspath;
261  }
262  }
263 #endif
264  return ret;
265 }
266 
267 RZ_API char *rz_file_abspath(const char *file) {
269  char *cwd = rz_sys_getdir();
270  if (cwd) {
271  char *ret = rz_file_abspath_rel(cwd, file);
272  free(cwd);
273  return ret;
274  }
275  return NULL;
276 }
277 
278 RZ_API char *rz_file_relpath(const char *base, const char *path) {
279  // skip longest common prefix
280  while (*base && *path) {
281  while (*base == *RZ_SYS_DIR) {
282  base++;
283  }
284  while (*path == *RZ_SYS_DIR) {
285  path++;
286  }
287  while (*base && *path && *base != *RZ_SYS_DIR && *path != *RZ_SYS_DIR) {
288  if (*base != *path) {
289  goto diverge;
290  }
291  base++;
292  path++;
293  }
294  }
295  while (*path == *RZ_SYS_DIR) {
296  path++;
297  }
298 
299  size_t ups;
300 diverge:
301  // count number of ".." needed which is just the number of remaining tokens in base
302  ups = 0;
303  while (*base) {
304  while (*base == *RZ_SYS_DIR) {
305  base++;
306  }
307  if (!*base) {
308  break;
309  }
310  ups++;
311  while (*base && *base != *RZ_SYS_DIR) {
312  base++;
313  }
314  }
315 
316  // put all the ".."s and append the rest of the path
317  size_t suff_len = strlen(path);
318  char *r = malloc(ups * 3 + suff_len + 1); // ups * strlen("../") + strlen(path)
319  if (!r) {
320  return NULL;
321  }
322  size_t i;
323  for (i = 0; i < ups; i++) {
324  r[i * 3] = '.';
325  r[i * 3 + 1] = '.';
326  r[i * 3 + 2] = *RZ_SYS_DIR;
327  }
328  memcpy(r + i * 3, path, suff_len + 1);
329  return r;
330 }
331 
333  char *r = strdup(path);
334  if (!r) {
335  return NULL;
336  }
337 #if __WINDOWS__
338  rz_str_replace(r, RZ_SYS_DIR, "/", true);
339 #endif
340  return r;
341 }
342 
344  char *r = strdup(path);
345  if (!r) {
346  return NULL;
347  }
348 #if __WINDOWS__
349  rz_str_replace(r, "/", RZ_SYS_DIR, true);
350 #endif
351  return r;
352 }
353 
354 RZ_API char *rz_file_path(const char *bin) {
356  char *file = NULL;
357  char *path = NULL;
358  char *str, *ptr;
359  const char *extension = "";
360  if (!strncmp(bin, "./", 2)) {
361  return rz_file_exists(bin)
363  : NULL;
364  }
365  char *path_env = (char *)rz_sys_getenv("PATH");
366 #if __WINDOWS__
367  if (!rz_str_endswith(bin, ".exe")) {
368  extension = ".exe";
369  }
370 #endif
371  if (path_env) {
372  str = path = strdup(path_env);
373  do {
374  ptr = strchr(str, RZ_SYS_ENVSEP[0]);
375  if (ptr) {
376  *ptr = '\0';
377  file = rz_str_newf(RZ_JOIN_2_PATHS("%s", "%s%s"), str, bin, extension);
378  if (rz_file_exists(file)) {
379  free(path);
380  free(path_env);
381  return file;
382  }
383  str = ptr + 1;
384  free(file);
385  }
386  } while (ptr);
387  }
388  free(path_env);
389  free(path);
390  return strdup(bin);
391 }
392 
393 RZ_API char *rz_file_binsh(void) {
394  char *bin_sh = rz_sys_getenv("SHELL");
395  if (RZ_STR_ISNOTEMPTY(bin_sh)) {
396  return bin_sh;
397  }
398  free(bin_sh);
399  bin_sh = rz_file_path("sh");
400  if (RZ_STR_ISNOTEMPTY(bin_sh)) {
401  return bin_sh;
402  }
403  free(bin_sh);
404  bin_sh = strdup("/bin/sh");
405  return bin_sh;
406 }
407 
408 RZ_API char *rz_stdin_slurp(int *sz) {
409 #if __UNIX__ || __WINDOWS__
410  int i, ret, newfd;
411  if ((newfd = dup(0)) < 0) {
412  return NULL;
413  }
414  char *buf = malloc(BS);
415  if (!buf) {
416  close(newfd);
417  return NULL;
418  }
419  for (i = 0; i >= 0; i += ret) {
420  char *new = realloc(buf, i + BS);
421  if (!new) {
422  eprintf("Cannot realloc to %d\n", i + BS);
423  free(buf);
424  close(newfd);
425  return NULL;
426  }
427  buf = new;
428  ret = read(0, buf + i, BS);
429  if (ret < 1) {
430  break;
431  }
432  }
433  if (i < 1) {
434  i = 0;
435  RZ_FREE(buf);
436  } else {
437  buf[i] = 0;
438  dup2(newfd, 0);
439  close(newfd);
440  }
441  if (sz) {
442  *sz = i;
443  }
444  if (!i) {
445  RZ_FREE(buf);
446  }
447  return buf;
448 #else
449 #warning TODO rz_stdin_slurp
450  return NULL;
451 #endif
452 }
453 
454 RZ_API RZ_OWN char *rz_file_slurp(const char *str, RZ_NULLABLE size_t *usz) {
456  if (usz) {
457  *usz = 0;
458  }
459  if (!rz_file_exists(str)) {
460  return NULL;
461  }
462  FILE *fd = rz_sys_fopen(str, "rb");
463  if (!fd) {
464  return NULL;
465  }
466  if (fseek(fd, 0, SEEK_END) == -1) {
467  // cannot determine the size of the file
468  }
469  long sz = ftell(fd);
470  if (sz < 0) {
471  fclose(fd);
472  return NULL;
473  }
474  if (!sz) {
475  if (rz_file_is_regular(str)) {
476  char *buf = NULL;
477  long size = 0;
478  (void)fseek(fd, 0, SEEK_SET);
479  do {
480  char *nbuf = realloc(buf, size + BS);
481  if (!nbuf) {
482  break;
483  }
484  buf = nbuf;
485  size_t r = fread(buf + size, 1, BS, fd);
486  if (ferror(fd)) {
487  RZ_FREE(buf);
488  goto regular_err;
489  }
490  size += r;
491  } while (!feof(fd));
492  char *nbuf = realloc(buf, size + 1);
493  if (!nbuf) {
494  fclose(fd);
495  free(buf);
496  return NULL;
497  }
498  buf = nbuf;
499  buf[size] = '\0';
500  if (usz) {
501  *usz = size;
502  }
503  regular_err:
504  fclose(fd);
505  return buf;
506  }
507  // try to read 64K
508  sz = UT16_MAX;
509  }
510  rewind(fd);
511  char *ret = (char *)malloc(sz + 1);
512  if (!ret) {
513  fclose(fd);
514  return NULL;
515  }
516  size_t rsz = fread(ret, 1, sz, fd);
517  if (rsz != sz) {
518  eprintf("Warning: rz_file_slurp: fread: truncated read\n");
519  sz = rsz;
520  }
521  fclose(fd);
522  ret[sz] = '\0';
523  if (usz) {
524  *usz = sz;
525  }
526  return ret;
527 }
528 
529 RZ_API ut8 *rz_file_gzslurp(const char *str, int *outlen, int origonfail) {
531  if (outlen) {
532  *outlen = 0;
533  }
534  size_t sz;
535  ut8 *in = (ut8 *)rz_file_slurp(str, &sz);
536  if (!in) {
537  return NULL;
538  }
539  ut8 *out = rz_inflate(in, (int)sz, NULL, outlen);
540  if (!out && origonfail) {
541  // if uncompression fails, return orig buffer ?
542  if (outlen) {
543  *outlen = (int)sz;
544  }
545  in[sz] = 0;
546  return in;
547  }
548  free(in);
549  return out;
550 }
551 
552 RZ_API ut8 *rz_file_slurp_hexpairs(const char *str, int *usz) {
554  if (usz) {
555  *usz = 0;
556  }
557  ut8 *ret;
558  long sz;
559  int c, bytes = 0;
560  FILE *fd = rz_sys_fopen(str, "rb");
561  if (!fd) {
562  return NULL;
563  }
564  (void)fseek(fd, 0, SEEK_END);
565  sz = ftell(fd);
566  (void)fseek(fd, 0, SEEK_SET);
567  ret = (ut8 *)malloc((sz >> 1) + 1);
568  if (!ret) {
569  fclose(fd);
570  return NULL;
571  }
572  for (;;) {
573  if (fscanf(fd, " #%*[^\n]") == 1) {
574  continue;
575  }
576  if (fscanf(fd, "%02x", &c) == 1) {
577  ret[bytes++] = c;
578  continue;
579  }
580  if (feof(fd)) {
581  break;
582  }
583  free(ret);
584  fclose(fd);
585  return NULL;
586  }
587  ret[bytes] = '\0';
588  fclose(fd);
589  if (usz) {
590  *usz = bytes;
591  }
592  return ret;
593 }
594 
595 RZ_API char *rz_file_slurp_range(const char *str, ut64 off, int sz, int *osz) {
596  char *ret;
597  size_t read_items;
598  FILE *fd = rz_sys_fopen(str, "rb");
599  if (!fd) {
600  return NULL;
601  }
602  // XXX handle out of bound reads (eof)
603  if (fseek(fd, off, SEEK_SET) < 0) {
604  fclose(fd);
605  return NULL;
606  }
607  ret = (char *)malloc(sz + 1);
608  if (ret) {
609  if (osz) {
610  *osz = (int)(size_t)fread(ret, 1, sz, fd);
611  } else {
612  read_items = fread(ret, 1, sz, fd);
613  if (!read_items) {
614  fclose(fd);
615  return ret;
616  }
617  }
618  ret[sz] = '\0';
619  }
620  fclose(fd);
621  return ret;
622 }
623 
626  int i = 0;
628 }
629 
632  /* Reservoir Sampling */
633  char *ptr = NULL, *str;
634  size_t i, lines, selection = -1;
635  int start = *line;
636  if ((str = rz_file_slurp(file, NULL))) {
637  rz_num_irand();
638  for (i = 0; str[i]; i++) {
639  if (str[i] == '\n') {
640  // here rand doesn't have any security implication
641  // https://www.securecoding.cert.org/confluence/display/c/MSC30-C.+Do+not+use+the+rand()+function+for+generating+pseudorandom+numbers
642  if (!(rz_num_rand((++(*line))))) {
643  selection = (*line - 1); /* The line we want. */
644  }
645  }
646  }
647  if ((selection < start) || (selection == -1)) {
648  free(str);
649  return NULL;
650  } else {
651  lines = selection - start;
652  }
653  if (lines > 0) {
654  for (i = 0; str[i] && lines; i++) {
655  if (str[i] == '\n') {
656  lines--;
657  }
658  }
659  ptr = str + i;
660  for (i = 0; ptr[i]; i++) {
661  if (ptr[i] == '\n') {
662  ptr[i] = '\0';
663  break;
664  }
665  }
666  ptr = strdup(ptr);
667  }
668  free(str);
669  }
670  return ptr;
671 }
672 
673 RZ_API char *rz_file_slurp_line(const char *file, int line, int context) {
675  int i, lines = 0;
676  size_t sz;
677  char *ptr = NULL, *str = rz_file_slurp(file, &sz);
678  // TODO: Implement context
679  if (str) {
680  for (i = 0; str[i]; i++) {
681  if (str[i] == '\n') {
682  lines++;
683  }
684  }
685  if (line > lines) {
686  free(str);
687  return NULL;
688  }
689  lines = line - 1;
690  for (i = 0; str[i] && lines; i++) {
691  if (str[i] == '\n') {
692  lines--;
693  }
694  }
695  ptr = str + i;
696  for (i = 0; ptr[i]; i++) {
697  if (ptr[i] == '\n') {
698  ptr[i] = '\0';
699  break;
700  }
701  }
702  ptr = strdup(ptr);
703  free(str);
704  }
705  return ptr;
706 }
707 
710  int i, lines = 0;
711  size_t sz;
712  char *ptr = NULL, *str = rz_file_slurp(file, &sz);
713  // TODO: Implement context
714  if (str) {
715  for (i = 0; str[i]; i++) {
716  if (str[i] == '\n') {
717  lines++;
718  }
719  }
720  if (line > lines) {
721  return str; // number of lines requested in more than present, return all
722  }
723  i--;
724  for (; str[i] && line; i--) {
725  if (str[i] == '\n') {
726  line--;
727  }
728  }
729  ptr = str + i;
730  ptr = strdup(ptr);
731  free(str);
732  }
733  return ptr;
734 }
735 
736 RZ_API char *rz_file_slurp_lines(const char *file, int line, int count) {
738  int i, lines = 0;
739  size_t sz;
740  char *ptr = NULL, *str = rz_file_slurp(file, &sz);
741  // TODO: Implement context
742  if (str) {
743  for (i = 0; str[i]; i++) {
744  if (str[i] == '\n') {
745  lines++;
746  }
747  }
748  if (line > lines) {
749  free(str);
750  return NULL;
751  }
752  lines = line - 1;
753  for (i = 0; str[i] && lines; i++) {
754  if (str[i] == '\n') {
755  lines--;
756  }
757  }
758  ptr = str + i;
759  for (i = 0; ptr[i]; i++) {
760  if (ptr[i] == '\n') {
761  if (count) {
762  count--;
763  } else {
764  ptr[i] = '\0';
765  break;
766  }
767  }
768  }
769  ptr = strdup(ptr);
770  free(str);
771  }
772  return ptr;
773 }
774 
775 RZ_API char *rz_file_root(const char *root, const char *path) {
777  char *ret, *s = rz_str_replace(strdup(path), "..", "", 1);
778  // XXX ugly hack
779  while (strstr(s, "..")) {
780  s = rz_str_replace(s, "..", "", 1);
781  }
782  while (strstr(s, "./")) {
783  s = rz_str_replace(s, "./", "", 1);
784  }
785  while (strstr(s, "//")) {
786  s = rz_str_replace(s, "//", "", 1);
787  }
789  ret = rz_str_append(ret, s);
790  free(s);
791  return ret;
792 }
793 
794 RZ_API bool rz_file_hexdump(const char *file, const ut8 *buf, int len, int append) {
795  FILE *fd;
796  int i, j;
797  if (!file || !*file || !buf || len < 0) {
798  eprintf("rz_file_hexdump file: %s buf: %p\n", file, buf);
799  return false;
800  }
801  if (append) {
802  fd = rz_sys_fopen(file, "ab");
803  } else {
804  rz_sys_truncate(file, 0);
805  fd = rz_sys_fopen(file, "wb");
806  }
807  if (!fd) {
808  eprintf("Cannot open '%s' for writing\n", file);
809  return false;
810  }
811  for (i = 0; i < len; i += 16) {
812  int l = RZ_MIN(16, len - i);
813  fprintf(fd, "0x%08" PFMT64x " ", (ut64)i);
814  for (j = 0; j + 2 <= l; j += 2) {
815  fprintf(fd, "%02x%02x ", buf[i + j], buf[i + j + 1]);
816  }
817  if (j < l) {
818  fprintf(fd, "%02x ", buf[i + j]);
819  j += 2;
820  }
821  if (j < 16) {
822  fprintf(fd, "%*s ", (16 - j) / 2 * 5, "");
823  }
824  for (j = 0; j < 16; j++) {
825  fprintf(fd, "%c", j < l && IS_PRINTABLE(buf[i + j]) ? buf[i + j] : '.');
826  }
827  fprintf(fd, "\n");
828  }
829  fclose(fd);
830  return true;
831 }
832 
833 RZ_API bool rz_file_touch(const char *file) {
834  rz_return_val_if_fail(file, false);
835  return rz_file_dump(file, NULL, 0, true);
836 }
837 
838 RZ_API bool rz_file_dump(const char *file, const ut8 *buf, int len, bool append) {
840  FILE *fd;
841  if (append) {
842  fd = rz_sys_fopen(file, "ab");
843  } else {
844  rz_sys_truncate(file, 0);
845  fd = rz_sys_fopen(file, "wb");
846  }
847  if (!fd) {
848  eprintf("Cannot open '%s' for writing\n", file);
849  return false;
850  }
851  if (buf) {
852  if (len < 0) {
853  len = strlen((const char *)buf);
854  }
855  if (len > 0 && fwrite(buf, len, 1, fd) != 1) {
856  rz_sys_perror("rz_file_dump: fwrite: error\n");
857  fclose(fd);
858  return false;
859  }
860  }
861  fclose(fd);
862  return true;
863 }
864 
865 RZ_API bool rz_file_rm(const char *file) {
866  if (RZ_STR_ISEMPTY(file)) {
867  return false;
868  }
869  if (rz_file_is_directory(file)) {
870 #if __WINDOWS__
871  LPWSTR wfile = rz_utf8_to_utf16(file);
872  bool ret = RemoveDirectoryW(wfile);
873 
874  free(wfile);
875  return !ret;
876 #else
877  return !rmdir(file);
878 #endif
879  } else {
880 #if __WINDOWS__
881  LPWSTR wfile = rz_utf8_to_utf16(file);
882  bool ret = DeleteFileW(wfile);
883 
884  free(wfile);
885  return !ret;
886 #else
887  return !unlink(file);
888 #endif
889  }
890 }
891 
892 RZ_API char *rz_file_readlink(const char *path) {
894 #if __UNIX__
895  int ret;
896  char pathbuf[4096] = { 0 };
897  strncpy(pathbuf, path, sizeof(pathbuf) - 1);
898 repeat:
899  ret = readlink(path, pathbuf, sizeof(pathbuf) - 1);
900  if (ret != -1) {
901  pathbuf[ret] = 0;
902  path = pathbuf;
903  goto repeat;
904  }
905  return strdup(pathbuf);
906 #endif
907  return NULL;
908 }
909 
910 #if __WINDOWS__
911 static RzMmap *file_mmap(RzMmap *m) {
912  bool is_write = (m->perm & O_WRONLY) || (m->perm & O_RDWR);
913  HANDLE fh = (HANDLE)_get_osfhandle(m->fd);
914  m->len = (DWORD)GetFileSize(fh, (LPDWORD)((char *)&m->len + sizeof(DWORD)));
915  if (m->len == INVALID_FILE_SIZE) {
916  rz_sys_perror("GetFileSize");
917  goto err;
918  }
919  if (m->len != 0) {
920  m->fm = CreateFileMappingW(fh,
921  NULL,
922  is_write ? PAGE_READWRITE : PAGE_READONLY,
923  0, 0, NULL);
924  if (!m->fm) {
925  rz_sys_perror("CreateFileMapping mmap");
926  goto err;
927  }
928  m->buf = MapViewOfFileEx(m->fm,
929  is_write ? (FILE_MAP_READ | FILE_MAP_WRITE) : FILE_MAP_READ,
930  0, 0, 0, (void *)m->base);
931  if (!m->buf) {
932  rz_sys_perror("MapViewOfFileEx");
933  goto err;
934  }
935  }
936  return m;
937 err:
939  return NULL;
940 }
941 #elif __UNIX__
942 static RzMmap *file_mmap(RzMmap *m) {
943  m->len = lseek(m->fd, (off_t)0, SEEK_END);
944  if (m->len) {
945  bool is_write = (m->perm & O_WRONLY) || (m->perm & O_RDWR);
946  m->buf = mmap((void *)(size_t)m->base,
947  m->len,
948  is_write ? PROT_READ | PROT_WRITE : PROT_READ,
949  MAP_SHARED, m->fd, 0);
950  if (m->buf == MAP_FAILED) {
951  rz_sys_perror("mmap");
953  return NULL;
954  }
955  }
956  return m;
957 }
958 #else
960  m->len = lseek(m->fd, (off_t)0, SEEK_END);
961  m->buf = malloc (m->len));
962  if (!m->buf) {
964  return NULL;
965  }
966  lseek(m->fd, (off_t)0, SEEK_SET);
967  rz_xread(m->fd, m->buf, m->len);
968  return m;
969 }
970 #endif
971 
972 RZ_API RzMmap *rz_file_mmap(const char *file, int perm, int mode, ut64 base) {
973  RzMmap *m = NULL;
974  m = RZ_NEW0(RzMmap);
975  if (!m) {
976  return NULL;
977  }
978  m->base = base;
979  m->perm = perm;
980  m->len = 0;
981  m->filename = strdup(file);
982  m->mode = mode;
983  if (!m->filename) {
985  return NULL;
986  }
987  m->fd = rz_sys_open(m->filename, m->perm, m->mode);
988  if (m->fd == -1) {
990  return NULL;
991  }
992  return file_mmap(m);
993 }
994 
996  if (!m) {
997  return;
998  }
999 #if __WINDOWS__
1000  if (m->buf) {
1001  UnmapViewOfFile(m->buf);
1002  }
1003  if (m->fm) {
1004  CloseHandle(m->fm);
1005  }
1006  if (m->fd != -1) {
1007  _close(m->fd);
1008  }
1009 #elif __UNIX__
1010  munmap(m->buf, m->len);
1011  close(m->fd);
1012 #endif
1013  free(m->filename);
1014  free(m);
1015 }
1016 
1018 #if __WINDOWS__
1019  if (m->buf) {
1020  UnmapViewOfFile(m->buf);
1021  }
1022  if (m->fm) {
1023  CloseHandle(m->fm);
1024  }
1025  if (m->fd != -1) {
1026  _close(m->fd);
1027  }
1028 #elif __UNIX__
1029  if (m->buf && munmap(m->buf, m->len) != 0) {
1030  return NULL;
1031  }
1032 #endif
1033  if (!rz_sys_truncate(m->filename, newsize)) {
1034  return NULL;
1035  }
1036  m->fd = rz_sys_open(m->filename, m->perm, m->mode);
1037  if (m->fd == -1) {
1039  return NULL;
1040  }
1041  // In case of mmap failure it frees the RzMmap and return NULL
1042  if (!file_mmap(m)) {
1043  return NULL;
1044  }
1045  return m->buf;
1046 }
1047 
1048 RZ_API char *rz_file_temp(const char *prefix) {
1049  if (!prefix) {
1050  prefix = "";
1051  }
1052  char *path = rz_file_tmpdir();
1053  char *res = rz_str_newf("%s" RZ_SYS_DIR "%s.%" PFMT64x, path, prefix, rz_time_now());
1054  free(path);
1055  return res;
1056 }
1057 
1058 RZ_API int rz_file_mkstemp(RZ_NULLABLE const char *prefix, char **oname) {
1059  int h = -1;
1060  char *path = rz_file_tmpdir();
1061  if (!prefix) {
1062  prefix = "rz";
1063  }
1064 #if __WINDOWS__
1065  LPWSTR wname = malloc(sizeof(WCHAR) * (MAX_PATH + 1));
1066  LPWSTR wpath = rz_utf8_to_utf16(path);
1067  LPWSTR wprefix = prefix ? rz_utf8_to_utf16(prefix) : _wcsdup(L"");
1068 
1069  if (!(wname && wpath && wprefix)) {
1070  goto err_r_file_mkstemp;
1071  }
1072 
1073  if (GetTempFileNameW(wpath, wprefix, 0, wname)) {
1074  char *name = rz_utf16_to_utf8(wname);
1075  h = rz_sys_open(name, O_RDWR | O_EXCL | O_BINARY, 0644);
1076  if (oname) {
1077  if (h != -1) {
1078  *oname = name;
1079  } else {
1080  *oname = NULL;
1081  free(name);
1082  }
1083  } else {
1084  free(name);
1085  }
1086  }
1087 err_r_file_mkstemp:
1088  free(wname);
1089  free(wpath);
1090  free(wprefix);
1091 #else
1092  char pfxx[1024];
1093  const char *suffix = strchr(prefix, '*');
1094 
1095  if (suffix) {
1096  suffix++;
1097  rz_str_ncpy(pfxx, prefix, (size_t)(suffix - prefix));
1098  prefix = pfxx;
1099  } else {
1100  suffix = "";
1101  }
1102 
1103  char *name = rz_str_newf("%s/rz.%s.XXXXXX%s", path, prefix, suffix);
1104  mode_t mask = umask(S_IWGRP | S_IWOTH);
1105  if (suffix && *suffix) {
1106 #if defined(__GLIBC__) && defined(__GLIBC_MINOR__) && 2 <= __GLIBC__ && 19 <= __GLIBC__MINOR__
1107  h = mkstemps(name, strlen(suffix));
1108 #else
1109  char *const xpos = strrchr(name, 'X');
1110  const char c = (char)(NULL != xpos ? *(xpos + 1) : 0);
1111  if (0 != c) {
1112  xpos[1] = 0;
1113  h = mkstemp(name);
1114  xpos[1] = c;
1115  } else {
1116  h = -1;
1117  }
1118 #endif
1119  } else {
1120  h = mkstemp(name);
1121  }
1122  umask(mask);
1123  if (oname) {
1124  *oname = (h != -1) ? strdup(name) : NULL;
1125  }
1126  free(name);
1127 #endif
1128  free(path);
1129  return h;
1130 }
1131 
1132 RZ_API char *rz_file_tmpdir(void) {
1133 #if __WINDOWS__
1134  char *path = NULL;
1135  DWORD len = 0;
1136 
1137  LPWSTR tmpdir = calloc(1, sizeof(WCHAR) * (MAX_PATH + 1));
1138  if (!tmpdir) {
1139  return NULL;
1140  }
1141  if ((len = GetTempPathW(MAX_PATH + 1, tmpdir)) == 0) {
1142  path = rz_sys_getenv("TEMP");
1143  if (!path) {
1144  path = strdup("C:\\WINDOWS\\Temp\\");
1145  }
1146  } else {
1147  tmpdir[len] = 0;
1148  // Windows XP sometimes returns short path name
1149  GetLongPathNameW(tmpdir, tmpdir, MAX_PATH + 1);
1150  path = rz_utf16_to_utf8(tmpdir);
1151  }
1152  free(tmpdir);
1153  // Windows 7, stat() function fail if tmpdir ends with '\\'
1154  if (path) {
1155  size_t path_len = strlen(path);
1156  if (path_len > 0 && path[path_len - 1] == '\\') {
1157  path[path_len - 1] = '\0';
1158  }
1159  }
1160 #else
1161  char *path = rz_sys_getenv("TMPDIR");
1162  if (path && !*path) {
1163  RZ_FREE(path);
1164  }
1165  if (!path) {
1166 #if __ANDROID__
1167  path = strdup("/data/data/org.rizin.rizininstaller/rizin/tmp");
1168 #else
1169  path = strdup("/tmp");
1170 #endif
1171  }
1172 #endif
1173  if (!rz_file_is_directory(path)) {
1174  eprintf("Cannot find temporary directory '%s'\n", path);
1175  }
1176  return path;
1177 }
1178 
1179 RZ_API bool rz_file_copy(const char *src, const char *dst) {
1180  /* TODO: implement in C */
1181  /* TODO: Use NO_CACHE for iOS dyldcache copying */
1182 #if HAVE_COPYFILE
1183  return copyfile(src, dst, 0, COPYFILE_DATA | COPYFILE_XATTR) != -1;
1184 #elif HAVE_COPY_FILE_RANGE
1185  int srcfd = open(src, O_RDONLY);
1186  if (srcfd == -1) {
1187  RZ_LOG_ERROR("rz_file_copy: Failed to open %s\n", src);
1188  return false;
1189  }
1190  int mask = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
1191  int dstfd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, mask);
1192  if (dstfd == -1) {
1193  RZ_LOG_ERROR("rz_file_copy: Failed to open %s\n", dst);
1194  close(srcfd);
1195  return false;
1196  }
1197  /* copy_file_path can handle large file up to SSIZE_MAX
1198  * with optimised performances.
1199  */
1200  off_t sz = lseek(srcfd, 0, SEEK_END);
1201  lseek(srcfd, 0, SEEK_SET);
1202  ssize_t ret = copy_file_range(srcfd, 0, dstfd, 0, SSIZE_MAX, 0);
1203  close(dstfd);
1204  close(srcfd);
1205  return ret == sz;
1206 #elif __WINDOWS__
1207  PWCHAR s = rz_utf8_to_utf16(src);
1208  PWCHAR d = rz_utf8_to_utf16(dst);
1209  if (!s || !d) {
1210  RZ_LOG_ERROR("rz_file_copy: Failed to allocate memory\n");
1211  free(s);
1212  free(d);
1213  return false;
1214  }
1215  bool ret = CopyFileW(s, d, 0);
1216  if (!ret) {
1217  rz_sys_perror("rz_file_copy");
1218  }
1219  free(s);
1220  free(d);
1221  return ret;
1222 #else
1223  char *src2 = rz_str_replace(strdup(src), "'", "\\'", 1);
1224  char *dst2 = rz_str_replace(strdup(dst), "'", "\\'", 1);
1225  int rc = rz_sys_cmdf("cp -f '%s' '%s'", src2, dst2);
1226  free(src2);
1227  free(dst2);
1228  return rc == 0;
1229 #endif
1230 }
1231 
1232 static void recursive_search_glob(const char *path, const char *glob, RzList *list, int depth) {
1233  if (depth < 1) {
1234  return;
1235  }
1236  char *file;
1237  RzListIter *iter;
1238  RzList *dir = rz_sys_dir(path);
1239  rz_list_foreach (dir, iter, file) {
1240  if (!strcmp(file, ".") || !strcmp(file, "..")) {
1241  continue;
1242  }
1243  char *filename = malloc(strlen(path) + strlen(file) + 2);
1244  if (!filename) {
1245  rz_list_free(dir);
1246  return;
1247  }
1248  strcpy(filename, path);
1249  strcat(filename, file);
1251  strcat(filename, RZ_SYS_DIR);
1252  recursive_search_glob(filename, glob, list, depth - 1);
1253  free(filename);
1254  } else if (rz_str_glob(file, glob)) {
1256  } else {
1257  free(filename);
1258  }
1259  }
1260  rz_list_free(dir);
1261 }
1262 
1263 RZ_API RzList *rz_file_globsearch(const char *_globbed_path, int maxdepth) {
1264  char *globbed_path = strdup(_globbed_path);
1266  char *glob = strchr(globbed_path, '*');
1267  if (!glob) {
1268  rz_list_append(files, strdup(globbed_path));
1269  } else {
1270  *glob = '\0';
1271  char *last_slash = (char *)rz_str_last(globbed_path, RZ_SYS_DIR);
1272  *glob = '*';
1273  char *path, *glob_ptr;
1274  if (last_slash) {
1275  glob_ptr = last_slash + 1;
1276  if (globbed_path[0] == '~') {
1277  char *rpath = rz_str_newlen(globbed_path + 2, last_slash - globbed_path - 1);
1278  path = rz_str_home(rpath ? rpath : "");
1279  free(rpath);
1280  } else {
1281  path = rz_str_newlen(globbed_path, last_slash - globbed_path + 1);
1282  }
1283  } else {
1284  glob_ptr = globbed_path;
1285  path = rz_str_newf(".%s", RZ_SYS_DIR);
1286  }
1287 
1288  if (!path) {
1290  free(globbed_path);
1291  return NULL;
1292  }
1293 
1294  if (*(glob + 1) == '*') { // "**"
1295  recursive_search_glob(path, glob_ptr, files, maxdepth);
1296  } else { // "*"
1297  recursive_search_glob(path, glob_ptr, files, 1);
1298  }
1299  free(path);
1300  }
1301  free(globbed_path);
1302  return files;
1303 }
1304 
1312 RZ_API RZ_OWN char *rz_file_path_join(RZ_NONNULL const char *s1, RZ_NULLABLE const char *s2) {
1314 
1315  if (!s2) {
1316  return strdup(s1);
1317  }
1318  bool ends_with_dir = s1[strlen(s1) - 1] == RZ_SYS_DIR[0];
1319  const char *sep = ends_with_dir ? "" : RZ_SYS_DIR;
1320  return rz_str_newf("%s%s%s", s1, sep, s2);
1321 }
1322 
1329 RZ_API bool rz_file_deflate(RZ_NONNULL const char *src, RZ_NONNULL const char *dst) {
1330  rz_return_val_if_fail(src && dst, false);
1331 
1332  bool ret = false;
1333 
1334  RzBuffer *src_buf = rz_buf_new_file(src, O_RDONLY, 0);
1335  RzBuffer *dst_buf = rz_buf_new_file(dst, O_WRONLY | O_CREAT, 0644);
1336 
1337  if (!(src_buf && dst_buf)) {
1338  goto return_goto;
1339  }
1340 
1341  ut64 block_size = 1 << 18; // 256 KB
1342 
1343  if (!rz_deflate_buf(src_buf, dst_buf, block_size, NULL)) {
1344  goto return_goto;
1345  }
1346 
1347  ret = true;
1348 
1349 return_goto:
1350  rz_buf_free(src_buf);
1351  rz_buf_free(dst_buf);
1352  return ret;
1353 }
1354 
1361 RZ_API bool rz_file_inflate(RZ_NONNULL const char *src, RZ_NONNULL const char *dst) {
1362  rz_return_val_if_fail(src && dst, false);
1363 
1364  bool ret = false;
1365 
1366  RzBuffer *src_buf = rz_buf_new_file(src, O_RDONLY, 0);
1367  RzBuffer *dst_buf = rz_buf_new_file(dst, O_WRONLY | O_CREAT, 0644);
1368 
1369  if (!(src_buf && dst_buf)) {
1370  goto return_goto;
1371  }
1372 
1373  ut64 block_size = 1 << 13; // 8 KB
1374 
1375  if (!rz_inflate_buf(src_buf, dst_buf, block_size, NULL)) {
1376  goto return_goto;
1377  }
1378 
1379  ret = true;
1380 
1381 return_goto:
1382  rz_buf_free(src_buf);
1383  rz_buf_free(dst_buf);
1384  return ret;
1385 }
1386 
1393  rz_return_val_if_fail(src, false);
1394 
1395  bool ret = false;
1396  unsigned char *header = (unsigned char *)rz_file_slurp_range(src, 0, 3, NULL);
1397 
1398  if (!header || rz_str_nlen((char *)header, 3) != 3) {
1399  goto return_goto;
1400  }
1401 
1402  ret = (header[0] == 0x1f && header[1] == 0x8b && header[2] == 0x08); // 1f 8b 08
1403 
1404 return_goto:
1405  free(header);
1406  return ret;
1407 }
size_t len
Definition: 6502dis.c:15
#define mask()
lzma_index ** i
Definition: index.h:629
lzma_index * src
Definition: index.h:567
static const char ext[]
Definition: apprentice.c:1981
static bool err
Definition: armass.c:435
static ut8 bytes[32]
Definition: asm_arc.c:23
static RzBinSourceLineInfo * lines(RzBinFile *bf)
Definition: bin_symbols.c:427
const lzma_allocator const uint8_t * in
Definition: block.h:527
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
FILE * fh
Definition: cabinfo.c:52
#define append(x, y)
Definition: cmd_print.c:1740
#define O_BINARY
Definition: cpipe.c:13
#define RZ_API
#define NULL
Definition: cris-opc.c:27
#define r
Definition: crypto_rc6.c:12
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 sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence ftruncate
Definition: sflib.h:113
static static fork const void static count close
Definition: sflib.h:33
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen rmdir
Definition: sflib.h:90
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void start
Definition: sflib.h:133
static static sync static getppid static getegid const char static filename char static len readlink
Definition: sflib.h:65
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz umask
Definition: sflib.h:65
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep lseek
Definition: sflib.h:113
int root
Definition: enough.c:226
RZ_API const char * rz_file_basename(const char *path)
Definition: file.c:83
RZ_API char * rz_file_tmpdir(void)
Definition: file.c:1132
RZ_API const char * rz_file_dos_basename(RZ_BORROW RZ_NONNULL const char *path)
Definition: file.c:102
RZ_API char * rz_file_temp(const char *prefix)
Definition: file.c:1048
RZ_API ut8 * rz_file_slurp_hexpairs(const char *str, int *usz)
Definition: file.c:552
RZ_API char * rz_file_binsh(void)
Definition: file.c:393
RZ_API bool rz_file_is_c(const char *file)
Definition: file.c:147
#define StructStat
Definition: file.c:29
RZ_API bool rz_file_is_directory(const char *str)
Definition: file.c:167
RZ_API char * rz_file_readlink(const char *path)
Definition: file.c:892
RZ_API bool rz_file_exists(const char *str)
Definition: file.c:192
RZ_API RZ_OWN char * rz_file_slurp_lines_from_bottom(const char *file, int line)
Definition: file.c:708
RZ_API char * rz_file_relpath(const char *base, const char *path)
Definition: file.c:278
RZ_API RzMmap * rz_file_mmap(const char *file, int perm, int mode, ut64 base)
Definition: file.c:972
RZ_API bool rz_file_touch(const char *file)
Definition: file.c:833
RZ_API bool rz_file_is_deflated(RZ_NONNULL const char *src)
check whether a file is a deflated (gzip) file
Definition: file.c:1392
RZ_API bool rz_file_is_abspath(const char *file)
Definition: file.c:214
#define BS
Definition: file.c:25
RZ_API bool rz_file_fexists(const char *fmt,...)
Definition: file.c:181
RZ_API bool rz_file_inflate(RZ_NONNULL const char *src, RZ_NONNULL const char *dst)
unzip the contents of src and store in dst
Definition: file.c:1361
static RzMmap * file_mmap(RzMmap *m)
Definition: file.c:959
RZ_API bool rz_file_is_regular(const char *str)
Definition: file.c:159
RZ_API char * rz_file_path_local_to_unix(const char *path)
Definition: file.c:332
RZ_API char * rz_stdin_slurp(int *sz)
Definition: file.c:408
RZ_API ut8 * rz_file_gzslurp(const char *str, int *outlen, int origonfail)
Definition: file.c:529
RZ_API bool rz_file_deflate(RZ_NONNULL const char *src, RZ_NONNULL const char *dst)
zip the contents of src and store in dst
Definition: file.c:1329
RZ_API char * rz_file_abspath(const char *file)
Definition: file.c:267
RZ_API bool rz_file_copy(const char *src, const char *dst)
Definition: file.c:1179
RZ_API int rz_file_mkstemp(RZ_NULLABLE const char *prefix, char **oname)
Definition: file.c:1058
RZ_API char * rz_file_dirname(const char *path)
Definition: file.c:120
RZ_API RZ_OWN char * rz_file_slurp(const char *str, RZ_NULLABLE size_t *usz)
Definition: file.c:454
RZ_API char * rz_file_slurp_random_line_count(const char *file, int *line)
Definition: file.c:630
RZ_API char * rz_file_root(const char *root, const char *path)
Definition: file.c:775
RZ_API bool rz_file_dump(const char *file, const ut8 *buf, int len, bool append)
Definition: file.c:838
RZ_API bool rz_file_rm(const char *file)
Definition: file.c:865
RZ_API char * rz_file_path(const char *bin)
Definition: file.c:354
RZ_API RZ_OWN char * rz_file_path_join(RZ_NONNULL const char *s1, RZ_NULLABLE const char *s2)
Concatenate two paths to create a new one with s1+s2 with the correct path separator.
Definition: file.c:1312
RZ_API ut64 rz_file_size(const char *str)
Definition: file.c:205
RZ_API char * rz_file_slurp_range(const char *str, ut64 off, int sz, int *osz)
Definition: file.c:595
RZ_API char * rz_file_slurp_line(const char *file, int line, int context)
Definition: file.c:673
RZ_API bool rz_file_hexdump(const char *file, const ut8 *buf, int len, int append)
Definition: file.c:794
RZ_API char * rz_file_abspath_rel(const char *cwd, const char *file)
Definition: file.c:219
RZ_API RzList * rz_file_globsearch(const char *_globbed_path, int maxdepth)
Definition: file.c:1263
RZ_API char * rz_file_path_unix_to_local(const char *path)
Definition: file.c:343
RZ_API void * rz_file_mmap_resize(RzMmap *m, ut64 newsize)
Definition: file.c:1017
RZ_API char * rz_file_slurp_random_line(const char *file)
Definition: file.c:624
RZ_API void rz_file_mmap_free(RzMmap *m)
Definition: file.c:995
static void recursive_search_glob(const char *path, const char *glob, RzList *list, int depth)
Definition: file.c:1232
static int file_stat(const char *file, StructStat *pStat)
Definition: file.c:32
RZ_API bool rz_file_truncate(const char *filename, ut64 newsize)
Definition: file.c:47
RZ_API char * rz_file_slurp_lines(const char *file, int line, int count)
Definition: file.c:736
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
unsigned char suffix[65536]
Definition: gun.c:164
unsigned short prefix[65536]
Definition: gun.c:163
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
static char * rp[]
Definition: i8080dis.c:36
voidpf void uLong size
Definition: ioapi.h:138
const char * filename
Definition: ioapi.h:137
const char int mode
Definition: ioapi.h:137
voidpf void * buf
Definition: ioapi.h:138
vsnprintf
Definition: kernel.h:366
uint8_t ut8
Definition: lh5801.h:11
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
static void list(RzEgg *egg)
Definition: rz-gg.c:52
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 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
void * realloc(void *ptr, size_t size)
Definition: malloc.c:144
void * malloc(size_t size)
Definition: malloc.c:123
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
static stat
Definition: sflib.h:131
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 dup
Definition: sflib.h:68
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 mmap
Definition: sflib.h:115
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")
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 static fork const void static count static fd const char static mode unlink
Definition: sflib.h:41
static const void static count static fd struct stat static buf struct pollfd unsigned static timeout void static offset munmap
Definition: sflib.h:43
char * dst
Definition: lz4.h:724
#define header(is_bt, len_min, ret_op)
string FILE
Definition: benchmark.py:21
line
Definition: setup.py:34
const char * name
Definition: op.c:541
int off
Definition: pal.c:13
static void repeat(struct parse *, sopno, int, int)
Definition: regcomp.c:1155
#define eprintf(x, y...)
Definition: rlcc.c:7
static RzSocket * s
Definition: rtr.c:28
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
RZ_API bool rz_inflate_buf(RZ_NONNULL RzBuffer *src, RZ_NONNULL RzBuffer *dst, ut64 block_size, ut8 *src_consumed)
inflate compressed data in RzBbuffer, use MAX_WBITS as the window size logarithm.
Definition: compression.c:360
RZ_API bool rz_deflate_buf(RZ_NONNULL RzBuffer *src, RZ_NONNULL RzBuffer *dst, ut64 block_size, ut8 *src_consumed)
deflate uncompressed data in RzBbuffer to zlib or gzipped, use MAX_WBITS as the window size logarithm...
Definition: compression.c:350
RZ_API void rz_buf_free(RzBuffer *b)
Free all internal data hold by the buffer and the buffer.
Definition: buf.c:1253
RZ_API RZ_OWN RzBuffer * rz_buf_new_file(const char *file, int perm, int mode)
Creates a new buffer from a file.
Definition: buf.c:317
RZ_API ut8 * rz_inflate(RZ_NONNULL const ut8 *src, int srcLen, int *srcConsumed, int *dstLen)
inflate zlib compressed or gzipped, automatically accepts either the zlib or gzip format,...
Definition: compression.c:18
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
RZ_API int rz_num_rand(int max)
Definition: unum.c:47
RZ_API void rz_num_irand(void)
Definition: unum.c:43
RZ_API RZ_OWN char * rz_path_home_expand(RZ_NULLABLE const char *path)
Return a new path with the ~ char expanded to the home directory.
Definition: path.c:268
#define RZ_STR_ISNOTEMPTY(x)
Definition: rz_str.h:68
RZ_API const char * rz_str_lchr(const char *str, char chr)
Definition: str.c:669
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
RZ_API char RZ_API char * rz_str_newlen(const char *str, int len)
Definition: str.c:871
RZ_API const char * rz_str_rchr(const char *base, const char *p, int ch)
Definition: str.c:829
RZ_API size_t rz_str_ncpy(char *dst, const char *src, size_t n)
Secure string copy with null terminator.
Definition: str.c:923
RZ_API char * rz_str_home(const char *str)
Definition: str.c:354
RZ_API bool rz_str_glob(const char *str, const char *glob)
Definition: str.c:2368
RZ_API const char * rz_str_last(const char *in, const char *ch)
Definition: str.c:3565
#define RZ_STR_ISEMPTY(x)
Definition: rz_str.h:67
RZ_API size_t rz_str_nlen(const char *s, size_t n)
Definition: str.c:1949
RZ_API char * rz_str_replace(char *str, const char *key, const char *val, int g)
Definition: str.c:1110
RZ_API bool rz_str_endswith(RZ_NONNULL const char *str, RZ_NONNULL const char *needle)
Checks if a string ends with a specifc sequence of characters (case sensitive)
Definition: str.c:3329
#define IS_PRINTABLE(x)
Definition: rz_str_util.h:10
RZ_API int rz_sys_cmdf(const char *fmt,...) RZ_PRINTF_CHECK(1
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 int rz_sys_open(const char *path, int perm, int mode)
Definition: sys.c:1740
RZ_API RzList * rz_sys_dir(const char *path)
Definition: sys.c:216
RZ_API int rz_sys_truncate(const char *file, int sz)
Definition: sys.c:1692
RZ_API FILE * rz_sys_fopen(const char *path, const char *mode)
Definition: sys.c:1815
RZ_API char * rz_sys_getdir(void)
Get current working directory.
Definition: sys.c:521
RZ_API ut64 rz_time_now(void)
Returns the current time in microseconds.
Definition: time.c:88
#define rz_xread(fd, buf, count)
Definition: rz_types.h:643
#define rz_sys_perror(x)
Definition: rz_types.h:336
#define RZ_SYS_DIR
Definition: rz_types.h:218
#define RZ_NULLABLE
Definition: rz_types.h:65
#define RZ_OWN
Definition: rz_types.h:62
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_JOIN_2_PATHS(p1, p2)
Definition: rz_types.h:224
#define RZ_NONNULL
Definition: rz_types.h:64
#define RZ_SYS_ENVSEP
Definition: rz_types.h:219
#define RZ_FREE(x)
Definition: rz_types.h:369
#define PFMT64x
Definition: rz_types.h:393
#define RZ_BORROW
Definition: rz_types.h:63
#define RZ_MIN(x, y)
#define UT16_MAX
static int
Definition: sfsocketcall.h:114
#define O_WRONLY
Definition: sftypes.h:487
#define PROT_READ
Definition: sftypes.h:95
#define O_CREAT
Definition: sftypes.h:489
int mode_t
Definition: sftypes.h:42
#define PROT_WRITE
Definition: sftypes.h:96
#define MAP_SHARED
Definition: sftypes.h:101
#define O_RDONLY
Definition: sftypes.h:486
#define O_EXCL
Definition: sftypes.h:490
#define O_SYNC
Definition: sftypes.h:496
int off_t
Definition: sftypes.h:41
#define O_RDWR
Definition: sftypes.h:488
#define O_TRUNC
Definition: sftypes.h:492
int ssize_t
Definition: sftypes.h:39
#define d(i)
Definition: sha256.c:44
#define c(i)
Definition: sha256.c:43
#define s1(x)
Definition: sha256.c:60
#define h(i)
Definition: sha256.c:48
Definition: malloc.c:26
Definition: gzappend.c:170
Definition: z80asm.h:102
#define SSIZE_MAX
Definition: win.h:28
DWORD LPWSTR
DWORD * HANDLE
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
DWORD
static const z80_opcode fd[]
Definition: z80_tab.h:997
static int file
Definition: z80asm.c:58
#define SEEK_SET
Definition: zip.c:88
#define SEEK_END
Definition: zip.c:84
#define L
Definition: zip_err_str.c:7
int read(izstream &zs, T *x, Items items)
Definition: zstream.h:115