Rizin
unix-like reverse engineering framework and cli tools
rz_file.h File Reference
#include <rz_list.h>

Go to the source code of this file.

Classes

struct  rz_mmap_t
 

Typedefs

typedef struct rz_mmap_t RzMmap
 

Functions

RZ_API bool rz_file_is_abspath (const char *file)
 
RZ_API bool rz_file_is_c (const char *file)
 
RZ_API bool rz_file_is_directory (const char *str)
 
RZ_API bool rz_file_is_regular (const char *str)
 
RZ_API bool rz_file_truncate (const char *filename, ut64 newsize)
 
RZ_API ut64 rz_file_size (const char *str)
 
RZ_API char * rz_file_root (const char *root, const char *path)
 
RZ_API RzMmaprz_file_mmap (const char *file, int perm, int mode, ut64 base)
 
RZ_API void * rz_file_mmap_resize (RzMmap *m, ut64 newsize)
 
RZ_API void rz_file_mmap_free (RzMmap *m)
 
RZ_API bool rz_file_chmod (const char *file, const char *mod, int recursive)
 
RZ_API char * rz_file_temp (const char *prefix)
 
RZ_API char * rz_file_path (const char *bin)
 
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. More...
 
RZ_API const char * rz_file_basename (const char *path)
 
RZ_API const char * rz_file_dos_basename (RZ_BORROW RZ_NONNULL const char *path)
 
RZ_API char * rz_file_dirname (const char *path)
 
RZ_API char * rz_file_abspath_rel (const char *cwd, const char *file)
 
RZ_API char * rz_file_abspath (const char *file)
 
RZ_API char * rz_file_relpath (const char *base, const char *path)
 
RZ_API char * rz_file_path_local_to_unix (const char *path)
 
RZ_API char * rz_file_path_unix_to_local (const char *path)
 
RZ_API char * rz_file_binsh (void)
 
RZ_API ut8rz_inflatew (RZ_NONNULL const ut8 *src, int srcLen, int *srcConsumed, int *dstLen, int wbits)
 
RZ_API ut8rz_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, and use MAX_WBITS as the window size logarithm. More...
 
RZ_API ut8rz_inflate_ignore_header (RZ_NONNULL const ut8 *src, int srcLen, int *srcConsumed, int *dstLen)
 inflate zlib compressed or gzipped. The input must be a raw stream with no header or trailer. More...
 
RZ_API ut8rz_deflatew (RZ_NONNULL const ut8 *src, int srcLen, int *srcConsumed, int *dstLen, int wbits)
 
RZ_API ut8rz_deflate (RZ_NONNULL const ut8 *src, int srcLen, int *srcConsumed, int *dstLen)
 deflate uncompressed data to zlib or gzipped, use MAX_WBITS as the window size logarithm. More...
 
RZ_API ut8rz_file_gzslurp (const char *str, int *outlen, int origonfail)
 
RZ_API char * rz_stdin_slurp (int *sz)
 
RZ_API RZ_OWN char * rz_file_slurp (const char *str, RZ_NULLABLE size_t *usz)
 
RZ_API char * rz_file_slurp_range (const char *str, ut64 off, int sz, int *osz)
 
RZ_API char * rz_file_slurp_random_line (const char *file)
 
RZ_API char * rz_file_slurp_random_line_count (const char *file, int *linecount)
 
RZ_API ut8rz_file_slurp_hexpairs (const char *str, int *usz)
 
RZ_API bool rz_file_dump (const char *file, const ut8 *buf, int len, bool append)
 
RZ_API bool rz_file_touch (const char *file)
 
RZ_API bool rz_file_hexdump (const char *file, const ut8 *buf, int len, int append)
 
RZ_API bool rz_file_rm (const char *file)
 
RZ_API bool rz_file_exists (const char *str)
 
RZ_API bool rz_file_fexists (const char *fmt,...) RZ_PRINTF_CHECK(1
 
RZ_API bool RZ_API char * rz_file_slurp_line (const char *file, int line, int context)
 
RZ_API char * rz_file_slurp_lines (const char *file, int line, int count)
 
RZ_API RZ_OWN char * rz_file_slurp_lines_from_bottom (const char *file, int line)
 
RZ_API int rz_file_mkstemp (RZ_NULLABLE const char *prefix, char **oname)
 
RZ_API char * rz_file_tmpdir (void)
 
RZ_API char * rz_file_readlink (const char *path)
 
RZ_API bool rz_file_copy (const char *src, const char *dst)
 
RZ_API RzListrz_file_globsearch (const char *globbed_path, int maxdepth)
 
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 More...
 
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 More...
 
RZ_API bool rz_file_is_deflated (RZ_NONNULL const char *src)
 check whether a file is a deflated (gzip) file More...
 

Typedef Documentation

◆ RzMmap

typedef struct rz_mmap_t RzMmap

Function Documentation

◆ rz_deflate()

RZ_API ut8* rz_deflate ( RZ_NONNULL const ut8 src,
int  srcLen,
int srcConsumed,
int dstLen 
)

deflate uncompressed data to zlib or gzipped, use MAX_WBITS as the window size logarithm.

See also
rz_deflatew()

Definition at line 340 of file compression.c.

340  {
342  rz_return_val_if_fail(srcLen > 0, NULL);
343  return rz_deflatew(src, srcLen, srcConsumed, dstLen, MAX_WBITS + 16);
344 }
lzma_index * src
Definition: index.h:567
RZ_API ut8 * rz_deflatew(RZ_NONNULL const ut8 *src, int srcLen, int *srcConsumed, int *dstLen, int wbits)
Definition: compression.c:323
#define NULL
Definition: cris-opc.c:27
#define MAX_WBITS
Definition: flirt.c:105
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108

References MAX_WBITS, NULL, rz_deflatew(), rz_return_val_if_fail, and src.

◆ rz_deflatew()

RZ_API ut8* rz_deflatew ( RZ_NONNULL const ut8 src,
int  srcLen,
int srcConsumed,
int dstLen,
int  wbits 
)

Definition at line 323 of file compression.c.

323  {
324  return NULL;
325 }

References NULL.

Referenced by rz_deflate().

◆ rz_file_abspath()

RZ_API char* rz_file_abspath ( const char *  file)

Definition at line 267 of file file.c.

267  {
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 }
RZ_API char * rz_file_abspath_rel(const char *cwd, const char *file)
Definition: file.c:219
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API char * rz_sys_getdir(void)
Get current working directory.
Definition: sys.c:521
Definition: gzappend.c:170

References test-lz4-speed::cwd, free(), NULL, rz_file_abspath_rel(), rz_return_val_if_fail, and rz_sys_getdir().

Referenced by file_save(), get_file_in_cur_dir(), getcommapath(), prj_dir_abs(), rz_cons_hud_path(), rz_core_cmd_file(), rz_core_file_open(), rz_core_file_print(), rz_core_file_reopen_debug(), rz_core_raw_file_print(), rz_core_rtr_http_run(), rz_core_run_script(), rz_debug_get_baddr(), rz_file_exists(), rz_file_path(), rz_lang_prompt(), rz_main_rizin(), and rz_test_chdir_fromtest().

◆ rz_file_abspath_rel()

RZ_API char* rz_file_abspath_rel ( const char *  cwd,
const char *  file 
)

Definition at line 219 of file file.c.

219  {
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 }
static char * rp[]
Definition: i8080dis.c:36
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")
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
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
#define RZ_STR_ISEMPTY(x)
Definition: rz_str.h:67
#define RZ_SYS_DIR
Definition: rz_types.h:218

References test-lz4-speed::cwd, free(), NULL, rp, rz_path_home_expand(), RZ_STR_ISEMPTY, rz_str_newf(), RZ_SYS_DIR, strdup(), and autogen_x86imm::tmp.

Referenced by prj_relative_restore(), rz_file_abspath(), and rz_test_main().

◆ rz_file_basename()

RZ_API const char* rz_file_basename ( const char *  path)

Definition at line 83 of file file.c.

83  {
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 }
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
RZ_API const char * rz_str_rchr(const char *base, const char *p, int ch)
Definition: str.c:829

References NULL, path, rz_return_val_if_fail, and rz_str_rchr().

Referenced by add_library(), addroflib(), autocmplt_cmd_arg_file(), cb_binprefix(), extract_binobj(), get_file_in_cur_dir(), MAIN_NAME(), prompt_add_file(), rz_cmd_debug_dmi(), rz_cmd_debug_dmS_handler(), rz_core_pdb_load_info(), rz_debug_dmp_init(), rz_lib_open(), and sigdb_signature_resolve_details().

◆ rz_file_binsh()

RZ_API char* rz_file_binsh ( void  )

Definition at line 393 of file file.c.

393  {
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 }
RZ_API char * rz_file_path(const char *bin)
Definition: file.c:354
#define RZ_STR_ISNOTEMPTY(x)
Definition: rz_str.h:68
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

References free(), rz_file_path(), RZ_STR_ISNOTEMPTY, rz_sys_getenv(), and strdup().

Referenced by rz_run_start(), and rz_sys_cmdbg().

◆ rz_file_chmod()

RZ_API bool rz_file_chmod ( const char *  file,
const char *  mod,
int  recursive 
)

Definition at line 19 of file chmod.c.

19  {
20 #if __UNIX__
21  oper = '=';
22  mode = 0;
23  if (!parsemode(mod)) {
24  return false;
25  }
26  return chmodr(file, recursive);
27 #else
28  return false;
29 #endif
30 }
int mod(int a, int b)
Definition: crypto_rot.c:8
const char int mode
Definition: ioapi.h:137

References mod().

Referenced by rz_main_rz_bin().

◆ rz_file_copy()

RZ_API bool rz_file_copy ( const char *  src,
const char *  dst 
)

Definition at line 1179 of file file.c.

1179  {
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 }
#define mask()
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 static offset struct stat static buf void long static basep lseek
Definition: sflib.h:113
char * dst
Definition: lz4.h:724
static RzSocket * s
Definition: rtr.c:28
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
RZ_API char * rz_str_replace(char *str, const char *key, const char *val, int g)
Definition: str.c:1110
RZ_API int rz_sys_cmdf(const char *fmt,...) RZ_PRINTF_CHECK(1
#define rz_sys_perror(x)
Definition: rz_types.h:336
#define O_WRONLY
Definition: sftypes.h:487
#define O_CREAT
Definition: sftypes.h:489
#define O_RDONLY
Definition: sftypes.h:486
int off_t
Definition: sftypes.h:41
#define O_TRUNC
Definition: sftypes.h:492
int ssize_t
Definition: sftypes.h:39
#define d(i)
Definition: sha256.c:44
#define SSIZE_MAX
Definition: win.h:28
#define SEEK_SET
Definition: zip.c:88
#define SEEK_END
Definition: zip.c:84

References close, d, dst, free(), lseek, mask, O_CREAT, O_RDONLY, O_TRUNC, O_WRONLY, RZ_LOG_ERROR, rz_str_replace(), rz_sys_cmdf(), rz_sys_perror, s, SEEK_END, SEEK_SET, src, SSIZE_MAX, and strdup().

Referenced by rz_cmd_shell_cp_handler().

◆ rz_file_deflate()

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

Parameters
srcsource file (string containing filename)
dstdestination file (string containing filename)
Returns
true, if successful; false otherwise

Definition at line 1329 of file file.c.

1329  {
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 }
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
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References dst, NULL, O_CREAT, O_RDONLY, O_WRONLY, rz_buf_free(), rz_buf_new_file(), rz_deflate_buf(), rz_return_val_if_fail, src, and ut64().

Referenced by rz_project_save_file().

◆ rz_file_dirname()

RZ_API char* rz_file_dirname ( const char *  path)

Definition at line 120 of file file.c.

120  {
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 }

References NULL, path, rz_return_val_if_fail, rz_str_rchr(), and strdup().

Referenced by autocmplt_cmd_arg_file(), download(), prj_dir_abs(), rz_core_bin_pdb_get_filename(), and sdb_disk_create().

◆ rz_file_dos_basename()

RZ_API const char* rz_file_dos_basename ( RZ_BORROW RZ_NONNULL const char *  path)

Definition at line 102 of file file.c.

102  {
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 }

References NULL, path, rz_return_val_if_fail, and rz_str_rchr().

Referenced by rz_bin_pdb_download(), rz_core_bin_pdb_get_filename(), rz_debug_dmp_maps(), rz_debug_dmp_modules(), and rz_debug_winkd_maps().

◆ rz_file_dump()

RZ_API bool rz_file_dump ( const char *  file,
const ut8 buf,
int  len,
bool  append 
)

Definition at line 838 of file file.c.

838  {
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 }
size_t len
Definition: 6502dis.c:15
#define append(x, y)
Definition: cmd_print.c:1740
voidpf void * buf
Definition: ioapi.h:138
string FILE
Definition: benchmark.py:21
#define eprintf(x, y...)
Definition: rlcc.c:7
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
static const z80_opcode fd[]
Definition: z80_tab.h:997

References append, eprintf, fd, benchmark::FILE, len, rz_return_val_if_fail, RZ_STR_ISEMPTY, rz_sys_fopen(), rz_sys_perror, and rz_sys_truncate().

Referenced by __dumpSections(), convert_dot_str_to_image(), dump_maps(), extract_binobj(), http_logf(), rz_buf_dump(), rz_core_rtr_http_run(), rz_egg_Cfile_parseCompiled(), rz_egg_Cfile_parser(), rz_file_touch(), rz_main_rizin(), rz_main_rz_bin(), rz_reopen_debug_rzrun_handler(), rz_run_start(), rz_test_main(), save_test_file_for_fix(), set_profile_string(), and visual_comma().

◆ rz_file_exists()

RZ_API bool rz_file_exists ( const char *  str)

Definition at line 192 of file file.c.

192  {
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 }
#define StructStat
Definition: file.c:29
RZ_API char * rz_file_abspath(const char *file)
Definition: file.c:267
static int file_stat(const char *file, StructStat *pStat)
Definition: file.c:32

References file_stat(), free(), rz_file_abspath(), rz_return_val_if_fail, RZ_STR_ISEMPTY, cmd_descs_generate::str, and StructStat.

Referenced by __func_name_from_ord(), bin_pe_parse_imports(), database_load(), download(), download_and_write(), find_include(), get_file_in_cur_dir(), get_main_arena_with_symbol(), GetHeapGlobalsOffset(), lang_lib_file_run(), load_sdb(), load_theme(), run_commands(), rz_analysis_function_cc_load_handler(), rz_bin_pdb_extract_in_folder(), rz_cmd_debug_dmi(), rz_cmd_info_pdb_load_handler(), rz_cmd_info_pdb_show_handler(), rz_cmd_kuery(), rz_core_analysis_cc_init(), rz_core_bin_apply_config(), rz_core_bin_pdb_get_filename(), rz_core_cmd_str_pipe(), rz_core_config_init(), rz_core_file_reopen_debug(), rz_core_io_file_open(), rz_core_rtr_http_run(), rz_core_visual_hud(), rz_egg_Cfile_parser(), rz_file_fexists(), rz_file_path(), rz_file_slurp(), rz_file_truncate(), rz_lib_path(), rz_main_rizin(), rz_platform_load_profile_sdb(), rz_platform_target_index_load_sdb(), rz_project_load_file_raw(), rz_resolve_jemalloc(), rz_run_start(), rz_sysreg_load_sdb(), rz_test_test_database_load_fuzz(), rz_type_db_load_callables_sdb(), rz_type_db_load_sdb(), rz_types_open_file(), rz_write_hex_from_file_handler(), and set_bin_relocs().

◆ rz_file_fexists()

RZ_API bool rz_file_fexists ( const char *  fmt,
  ... 
)

◆ rz_file_globsearch()

RZ_API RzList* rz_file_globsearch ( const char *  globbed_path,
int  maxdepth 
)

Definition at line 1263 of file file.c.

1263  {
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 }
static void recursive_search_glob(const char *path, const char *glob, RzList *list, int depth)
Definition: file.c:1232
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 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
RZ_API char RZ_API char * rz_str_newlen(const char *str, int len)
Definition: str.c:871
RZ_API char * rz_str_home(const char *str)
Definition: str.c:354
RZ_API const char * rz_str_last(const char *in, const char *ch)
Definition: str.c:3565

References files, free(), NULL, path, recursive_search_glob(), rz_list_append(), rz_list_free(), rz_list_newf(), rz_str_home(), rz_str_last(), rz_str_newf(), rz_str_newlen(), RZ_SYS_DIR, and strdup().

Referenced by rz_flirt_scan_handler(), and rz_sign_sigdb_load_database().

◆ rz_file_gzslurp()

RZ_API ut8* rz_file_gzslurp ( const char *  str,
int outlen,
int  origonfail 
)

Definition at line 529 of file file.c.

529  {
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 }
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
RZ_API RZ_OWN char * rz_file_slurp(const char *str, RZ_NULLABLE size_t *usz)
Definition: file.c:454
uint8_t ut8
Definition: lh5801.h:11
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
static int
Definition: sfsocketcall.h:114

References free(), in, int, NULL, out, rz_file_slurp(), rz_inflate(), rz_return_val_if_fail, and cmd_descs_generate::str.

◆ rz_file_hexdump()

RZ_API bool rz_file_hexdump ( const char *  file,
const ut8 buf,
int  len,
int  append 
)

Definition at line 794 of file file.c.

794  {
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 }
lzma_index ** i
Definition: index.h:629
#define IS_PRINTABLE(x)
Definition: rz_str_util.h:10
#define PFMT64x
Definition: rz_types.h:393
#define RZ_MIN(x, y)

References append, eprintf, fd, benchmark::FILE, i, IS_PRINTABLE, len, PFMT64x, RZ_MIN, rz_sys_fopen(), rz_sys_truncate(), and ut64().

◆ rz_file_inflate()

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

Parameters
srcsource file (string containing filename)
dstdestination file (string containing filename)
Returns
true, if successful; false otherwise

Definition at line 1361 of file file.c.

1361  {
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 }
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

References dst, NULL, O_CREAT, O_RDONLY, O_WRONLY, rz_buf_free(), rz_buf_new_file(), rz_inflate_buf(), rz_return_val_if_fail, src, and ut64().

Referenced by rz_project_load_file_raw().

◆ rz_file_is_abspath()

RZ_API bool rz_file_is_abspath ( const char *  file)

Definition at line 214 of file file.c.

214  {
216  return ((*file && file[1] == ':') || *file == '/');
217 }

References rz_return_val_if_fail, and RZ_STR_ISEMPTY.

Referenced by autocmplt_cmd_arg_file(), and rz_test_main().

◆ rz_file_is_c()

RZ_API bool rz_file_is_c ( const char *  file)

Definition at line 147 of file file.c.

147  {
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 }
static const char ext[]
Definition: apprentice.c:1981
RZ_API const char * rz_str_lchr(const char *str, char chr)
Definition: str.c:669

References ext, rz_return_val_if_fail, and rz_str_lchr().

Referenced by rz_core_run_script().

◆ rz_file_is_deflated()

RZ_API bool rz_file_is_deflated ( RZ_NONNULL const char *  src)

check whether a file is a deflated (gzip) file

Parameters
srcsource file (string containing filename)
Returns
true, if src is a deflated (gzip) file; false otherwise

Definition at line 1392 of file file.c.

1392  {
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 }
RZ_API char * rz_file_slurp_range(const char *str, ut64 off, int sz, int *osz)
Definition: file.c:595
#define header(is_bt, len_min, ret_op)
RZ_API size_t rz_str_nlen(const char *s, size_t n)
Definition: str.c:1949

References free(), header, NULL, rz_file_slurp_range(), rz_return_val_if_fail, rz_str_nlen(), and src.

Referenced by rz_project_load_file_raw().

◆ rz_file_is_directory()

◆ rz_file_is_regular()

RZ_API bool rz_file_is_regular ( const char *  str)

Definition at line 159 of file file.c.

159  {
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 }

References file_stat(), cmd_descs_generate::str, and StructStat.

Referenced by rz_core_parse_rizinrc(), rz_file_slurp(), rz_file_truncate(), rz_sys_fopen(), rz_syscmd_ls(), and try_load_file().

◆ rz_file_mkstemp()

RZ_API int rz_file_mkstemp ( RZ_NULLABLE const char *  prefix,
char **  oname 
)

Definition at line 1058 of file file.c.

1058  {
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 }
#define O_BINARY
Definition: cpipe.c:13
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz umask
Definition: sflib.h:65
RZ_API char * rz_file_tmpdir(void)
Definition: file.c:1132
unsigned char suffix[65536]
Definition: gun.c:164
unsigned short prefix[65536]
Definition: gun.c:163
void * malloc(size_t size)
Definition: malloc.c:123
const char * name
Definition: op.c:541
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 int rz_sys_open(const char *path, int perm, int mode)
Definition: sys.c:1740
int mode_t
Definition: sftypes.h:42
#define O_EXCL
Definition: sftypes.h:490
#define O_RDWR
Definition: sftypes.h:488
#define c(i)
Definition: sha256.c:43
#define h(i)
Definition: sha256.c:48
Definition: z80asm.h:102
DWORD LPWSTR
#define L
Definition: zip_err_str.c:7

References c, free(), h, L, LPWSTR, malloc(), mask, name, NULL, O_BINARY, O_EXCL, O_RDWR, path, prefix, rz_file_tmpdir(), rz_str_ncpy(), rz_str_newf(), rz_sys_open(), strdup(), suffix, and umask.

Referenced by assemble(), binutils_assemble(), config_path(), interact_commands(), rz_core_cmd_str_pipe(), rz_core_editor(), rz_core_sysenv_begin(), rz_project_load_file_raw(), and rz_project_save_file().

◆ rz_file_mmap()

RZ_API RzMmap* rz_file_mmap ( const char *  file,
int  perm,
int  mode,
ut64  base 
)

Definition at line 972 of file file.c.

972  {
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 }
static RzMmap * file_mmap(RzMmap *m)
Definition: file.c:959
RZ_API void rz_file_mmap_free(RzMmap *m)
Definition: file.c:995
#define RZ_NEW0(x)
Definition: rz_types.h:284

References file_mmap(), regress::m, NULL, rz_file_mmap_free(), RZ_NEW0, rz_sys_open(), and strdup().

Referenced by buf_mmap_init().

◆ rz_file_mmap_free()

RZ_API void rz_file_mmap_free ( RzMmap m)

Definition at line 995 of file file.c.

995  {
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 }
static const void static count static fd struct stat static buf struct pollfd unsigned static timeout void static offset munmap
Definition: sflib.h:43

References close, free(), regress::m, and munmap.

Referenced by buf_mmap_fini(), file_mmap(), rz_file_mmap(), and rz_file_mmap_resize().

◆ rz_file_mmap_resize()

RZ_API void* rz_file_mmap_resize ( RzMmap m,
ut64  newsize 
)

Definition at line 1017 of file file.c.

1017  {
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 }

References file_mmap(), regress::m, munmap, NULL, rz_file_mmap_free(), rz_sys_open(), and rz_sys_truncate().

Referenced by buf_mmap_resize().

◆ rz_file_path()

RZ_API char* rz_file_path ( const char *  bin)

Definition at line 354 of file file.c.

354  {
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 }
RZ_API bool rz_file_exists(const char *str)
Definition: file.c:192
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 RZ_JOIN_2_PATHS(p1, p2)
Definition: rz_types.h:224
#define RZ_SYS_ENVSEP
Definition: rz_types.h:219
Definition: malloc.c:26
static int file
Definition: z80asm.c:58

References file, free(), NULL, path, rz_file_abspath(), rz_file_exists(), RZ_JOIN_2_PATHS, rz_return_val_if_fail, rz_str_endswith(), rz_str_newf(), RZ_SYS_ENVSEP, rz_sys_getenv(), cmd_descs_generate::str, and strdup().

Referenced by dot_executable_path(), getViewerPath(), rz_cmd_shell_which_handler(), rz_core_run_script(), rz_egg_Cfile_getCompiler(), rz_file_binsh(), rz_main_rizin(), rz_run_start(), rz_test_chdir(), and rz_test_main().

◆ rz_file_path_join()

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.

Parameters
s1First path
s2Second path, can be NULL
Returns
Full path

Definition at line 1312 of file file.c.

1312  {
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 }
#define s1(x)
Definition: sha256.c:60

References NULL, rz_return_val_if_fail, rz_str_newf(), RZ_SYS_DIR, s1, s2, and strdup().

Referenced by __get_panels_config_dir_path(), autocmplt_cmd_arg_file(), cmd_print_format(), load_scripts_for(), load_sdb(), main_help(), rizin_fortune_file(), rz_core_analysis_cc_init(), rz_core_bin_apply_config(), rz_core_init(), rz_core_theme_load(), rz_core_visual_hud(), rz_path_home(), rz_path_prefix(), rz_platform_profiles_init(), rz_platform_target_index_init(), rz_run_config_env(), rz_sysreg_set_arch(), rz_type_db_init(), and rzfind_open_dir().

◆ rz_file_path_local_to_unix()

RZ_API char* rz_file_path_local_to_unix ( const char *  path)

Definition at line 332 of file file.c.

332  {
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 }
#define r
Definition: crypto_rc6.c:12

References NULL, path, r, rz_str_replace(), RZ_SYS_DIR, and strdup().

Referenced by prj_relative_make().

◆ rz_file_path_unix_to_local()

RZ_API char* rz_file_path_unix_to_local ( const char *  path)

Definition at line 343 of file file.c.

343  {
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 }

References NULL, path, r, rz_str_replace(), RZ_SYS_DIR, and strdup().

Referenced by prj_relative_restore().

◆ rz_file_readlink()

RZ_API char* rz_file_readlink ( const char *  path)

Definition at line 892 of file file.c.

892  {
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 }
static static sync static getppid static getegid const char static filename char static len readlink
Definition: sflib.h:65
static void repeat(struct parse *, sopno, int, int)
Definition: regcomp.c:1155

References NULL, path, readlink, repeat(), rz_return_val_if_fail, RZ_STR_ISEMPTY, and strdup().

◆ rz_file_relpath()

RZ_API char* rz_file_relpath ( const char *  base,
const char *  path 
)

Definition at line 278 of file file.c.

278  {
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 }
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))

References i, malloc(), memcpy(), NULL, path, r, and RZ_SYS_DIR.

Referenced by prj_relative_make().

◆ rz_file_rm()

RZ_API bool rz_file_rm ( const char *  file)

Definition at line 865 of file file.c.

865  {
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 }
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
RZ_API bool rz_file_is_directory(const char *str)
Definition: file.c:167
static static fork const void static count static fd const char static mode unlink
Definition: sflib.h:41

References free(), LPWSTR, rmdir, rz_file_is_directory(), RZ_STR_ISEMPTY, and unlink.

Referenced by __clear_layout_cb(), __rap_reg_profile(), download(), rz_cmd_debug(), rz_cmd_debug_dmi(), rz_cmd_shell_rm_handler(), rz_core_cmd_str_pipe(), rz_core_cmd_subst_i(), rz_core_editor(), rz_core_sysenv_end(), rz_egg_Cfile_parser(), rz_egg_load_file(), rz_project_load_file_raw(), rz_project_save_file(), and system_exec().

◆ rz_file_root()

RZ_API char* rz_file_root ( const char *  root,
const char *  path 
)

Definition at line 775 of file file.c.

775  {
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 }
int root
Definition: enough.c:226
RZ_API char * rz_str_append(char *ptr, const char *string)
Definition: str.c:1063

References free(), NULL, path, root, rz_return_val_if_fail, rz_str_append(), rz_str_replace(), RZ_SYS_DIR, s, and strdup().

Referenced by rz_core_rtr_http_run().

◆ rz_file_size()

RZ_API ut64 rz_file_size ( const char *  str)

Definition at line 205 of file file.c.

205  {
207  StructStat buf = { 0 };
208  if (file_stat(str, &buf) == -1) {
209  return 0;
210  }
211  return (ut64)buf.st_size;
212 }

References file_stat(), rz_return_val_if_fail, RZ_STR_ISEMPTY, cmd_descs_generate::str, StructStat, and ut64().

Referenced by rz_egg_Cfile_parser(), rz_type_parse_file(), rz_type_parse_file_stateless(), and showfile().

◆ rz_file_slurp()

RZ_API RZ_OWN char* rz_file_slurp ( const char *  str,
RZ_NULLABLE size_t usz 
)

Definition at line 454 of file file.c.

454  {
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 }
#define BS
Definition: file.c:25
RZ_API bool rz_file_is_regular(const char *str)
Definition: file.c:159
voidpf void uLong size
Definition: ioapi.h:138
void * realloc(void *ptr, size_t size)
Definition: malloc.c:144
#define RZ_FREE(x)
Definition: rz_types.h:369
#define UT16_MAX

References BS, eprintf, fd, benchmark::FILE, free(), malloc(), NULL, r, realloc(), rz_file_exists(), rz_file_is_regular(), RZ_FREE, rz_return_val_if_fail, rz_sys_fopen(), SEEK_END, SEEK_SET, cmd_descs_generate::str, and UT16_MAX.

Referenced by __open(), __rap_reg_profile(), DEFINE_HANDLE_TS_FCN_AND_SYMBOL(), getFileData(), getstr(), hash_parse_any(), interact_commands(), linux_info(), rcc_next(), read_test_file_for_fix(), rz_asm_pseudo_incbin(), rz_bin_fatmach0_new(), rz_bin_mz_new(), rz_bin_te_new(), rz_buf_new_slurp(), rz_cmd_debug_dml_handler(), rz_cmd_search(), rz_cons_hud_file(), rz_core_cmd_file(), rz_core_cmd_str_pipe(), rz_core_cmd_subst_i(), rz_core_editor(), rz_core_fortune_list(), rz_core_rtr_http_run(), rz_core_visual_cmd(), rz_debug_plugin_set_reg_profile(), rz_egg_Cfile_parseCompiled(), rz_egg_include(), rz_egg_load_file(), rz_file_gzslurp(), rz_file_slurp_line(), rz_file_slurp_lines(), rz_file_slurp_lines_from_bottom(), rz_file_slurp_random_line_count(), rz_lang_run_file(), rz_load_panels_layout(), rz_main_rizin(), rz_main_rz_agent(), rz_main_rz_asm(), rz_main_rz_find(), rz_main_rz_gg(), rz_reg_parse_gdb_profile(), rz_reg_set_profile(), rz_run_parsefile(), rz_str_version(), rz_syscmd_cat(), rz_syscmd_join(), rz_syscmd_sort(), rz_syscmd_uniq(), rz_test_load_asm_test_file(), rz_test_load_cmd_test_file(), rz_test_load_json_test_file(), rz_type_parse_file(), rz_type_parse_file_stateless(), rz_write_assembly_file_handler(), rz_write_from_file_handler(), rz_write_hex_from_file_handler(), rz_write_unified_patch_handler(), and visual_comma().

◆ rz_file_slurp_hexpairs()

RZ_API ut8* rz_file_slurp_hexpairs ( const char *  str,
int usz 
)

Definition at line 552 of file file.c.

552  {
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 }
static ut8 bytes[32]
Definition: asm_arc.c:23

References bytes, c, fd, benchmark::FILE, free(), malloc(), NULL, rz_return_val_if_fail, rz_sys_fopen(), SEEK_END, SEEK_SET, and cmd_descs_generate::str.

◆ rz_file_slurp_line()

RZ_API bool RZ_API char* rz_file_slurp_line ( const char *  file,
int  line,
int  context 
)

Definition at line 673 of file file.c.

673  {
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 }
static RzBinSourceLineInfo * lines(RzBinFile *bf)
Definition: bin_symbols.c:427
line
Definition: setup.py:34

References free(), i, setup::line, lines(), NULL, rz_file_slurp(), rz_return_val_if_fail, cmd_descs_generate::str, and strdup().

Referenced by core_analysis_graph_label(), rz_bin_addr2text(), and step_line().

◆ rz_file_slurp_lines()

RZ_API char* rz_file_slurp_lines ( const char *  file,
int  line,
int  count 
)

Definition at line 736 of file file.c.

736  {
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 }
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

References count, free(), i, setup::line, lines(), NULL, rz_file_slurp(), rz_return_val_if_fail, cmd_descs_generate::str, and strdup().

Referenced by rz_syscmd_head().

◆ rz_file_slurp_lines_from_bottom()

RZ_API RZ_OWN char* rz_file_slurp_lines_from_bottom ( const char *  file,
int  line 
)

Definition at line 708 of file file.c.

708  {
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 }

References free(), i, setup::line, lines(), NULL, rz_file_slurp(), rz_return_val_if_fail, cmd_descs_generate::str, and strdup().

Referenced by rz_syscmd_tail().

◆ rz_file_slurp_random_line()

RZ_API char* rz_file_slurp_random_line ( const char *  file)

Definition at line 624 of file file.c.

624  {
626  int i = 0;
628 }
RZ_API char * rz_file_slurp_random_line_count(const char *file, int *line)
Definition: file.c:630

References i, NULL, rz_file_slurp_random_line_count(), and rz_return_val_if_fail.

◆ rz_file_slurp_random_line_count()

RZ_API char* rz_file_slurp_random_line_count ( const char *  file,
int linecount 
)

Definition at line 630 of file file.c.

630  {
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 }
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
RZ_API int rz_num_rand(int max)
Definition: unum.c:47
RZ_API void rz_num_irand(void)
Definition: unum.c:43

References free(), i, setup::line, lines(), NULL, rz_file_slurp(), rz_num_irand(), rz_num_rand(), rz_return_val_if_fail, start, cmd_descs_generate::str, and strdup().

Referenced by rz_core_fortune_get_random(), and rz_file_slurp_random_line().

◆ rz_file_slurp_range()

RZ_API char* rz_file_slurp_range ( const char *  str,
ut64  off,
int  sz,
int osz 
)

Definition at line 595 of file file.c.

595  {
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 }
int off
Definition: pal.c:13

References fd, benchmark::FILE, int, malloc(), NULL, off, rz_sys_fopen(), SEEK_SET, and cmd_descs_generate::str.

Referenced by rz_file_is_deflated().

◆ rz_file_temp()

RZ_API char* rz_file_temp ( const char *  prefix)

Definition at line 1048 of file file.c.

1048  {
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 }
RZ_API ut64 rz_time_now(void)
Returns the current time in microseconds.
Definition: time.c:88

References free(), path, PFMT64x, prefix, rz_file_tmpdir(), rz_str_newf(), RZ_SYS_DIR, and rz_time_now().

Referenced by __rap_reg_profile(), rz_cmd_debug_dmi(), rz_core_cmd_subst_i(), rz_main_rizin(), rz_reopen_debug_rzrun_handler(), and set_profile_string().

◆ rz_file_tmpdir()

RZ_API char* rz_file_tmpdir ( void  )

Definition at line 1132 of file file.c.

1132  {
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 }
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
DWORD

References calloc(), DWORD, eprintf, free(), len, LPWSTR, NULL, path, rz_file_is_directory(), RZ_FREE, rz_sys_getenv(), and strdup().

Referenced by rz_core_config_init(), rz_file_mkstemp(), rz_file_temp(), rz_path_home(), rz_path_home_prefix(), and rz_str_home().

◆ rz_file_touch()

RZ_API bool rz_file_touch ( const char *  file)

Definition at line 833 of file file.c.

833  {
834  rz_return_val_if_fail(file, false);
835  return rz_file_dump(file, NULL, 0, true);
836 }
RZ_API bool rz_file_dump(const char *file, const ut8 *buf, int len, bool append)
Definition: file.c:838

References NULL, rz_file_dump(), and rz_return_val_if_fail.

◆ rz_file_truncate()

RZ_API bool rz_file_truncate ( const char *  filename,
ut64  newsize 
)

Definition at line 47 of file file.c.

47  {
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 }
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
const char * filename
Definition: ioapi.h:137
#define O_SYNC
Definition: sftypes.h:496

References close, eprintf, fd, ftruncate, O_RDWR, O_SYNC, r, rz_file_exists(), rz_file_is_directory(), rz_file_is_regular(), rz_return_val_if_fail, and rz_sys_open().

◆ rz_inflate()

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, and use MAX_WBITS as the window size logarithm.

See also
rz_inflatew()

Definition at line 18 of file compression.c.

18  {
20  rz_return_val_if_fail(srcLen > 0, NULL);
21  return rz_inflatew(src, srcLen, srcConsumed, dstLen, MAX_WBITS + 32);
22 }
RZ_API ut8 * rz_inflatew(RZ_NONNULL const ut8 *src, int srcLen, int *srcConsumed, int *dstLen, int wbits)
Definition: compression.c:319

References MAX_WBITS, NULL, rz_inflatew(), rz_return_val_if_fail, and src.

Referenced by __open(), rz_cmd_print(), rz_file_gzslurp(), and rz_sign_flirt_parse_compressed_pattern_from_buffer().

◆ rz_inflate_ignore_header()

RZ_API ut8* rz_inflate_ignore_header ( RZ_NONNULL const ut8 src,
int  srcLen,
int srcConsumed,
int dstLen 
)

inflate zlib compressed or gzipped. The input must be a raw stream with no header or trailer.

See also
rz_inflatew()

Definition at line 28 of file compression.c.

28  {
30  rz_return_val_if_fail(srcLen > 0, NULL);
31  return rz_inflatew(src, srcLen, srcConsumed, dstLen, -MAX_WBITS);
32 }

References MAX_WBITS, NULL, rz_inflatew(), rz_return_val_if_fail, and src.

Referenced by rz_sign_flirt_parse_compressed_pattern_from_buffer().

◆ rz_inflatew()

RZ_API ut8* rz_inflatew ( RZ_NONNULL const ut8 src,
int  srcLen,
int srcConsumed,
int dstLen,
int  wbits 
)

Definition at line 319 of file compression.c.

319  {
320  return NULL;
321 }

References NULL.

Referenced by rz_inflate(), and rz_inflate_ignore_header().

◆ rz_stdin_slurp()

RZ_API char* rz_stdin_slurp ( int sz)

Definition at line 408 of file file.c.

408  {
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 }
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 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
int read(izstream &zs, T *x, Items items)
Definition: zstream.h:115

References BS, close, dup, dup2, eprintf, free(), i, malloc(), NULL, read(), realloc(), and RZ_FREE.

Referenced by hash_context_create_desc_io_stdin(), hash_parse_any(), hash_parse_hexadecimal(), hash_parse_string(), rax(), rz_main_rizin(), rz_main_rz_asm(), and rzfind_open().