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

Go to the source code of this file.

Classes

struct  RZARFP
 

Typedefs

typedef struct RZARFP RzArFp
 

Functions

RZ_API RzArFpar_open_file (const char *arname, int perm, const char *filename)
 Open specific file withen a ar/lib file. More...
 
RZ_API RzListar_open_all (const char *arname, int perm)
 Open specific file withen a ar/lib file. More...
 
RZ_API void ar_close (RzArFp *f)
 
RZ_API int ar_read_at (RzArFp *f, ut64 off, void *buf, int count)
 
RZ_API int ar_write_at (RzArFp *f, ut64 off, void *buf, int count)
 

Typedef Documentation

◆ RzArFp

typedef struct RZARFP RzArFp

Function Documentation

◆ ar_close()

RZ_API void ar_close ( RzArFp f)

Definition at line 338 of file ar.c.

338  {
339  if (!f) {
340  return;
341  }
342  free(f->name);
343  if (!f->shared_buf) {
344  rz_buf_free(f->buf);
345  }
346  free(f);
347 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API void rz_buf_free(RzBuffer *b)
Free all internal data hold by the buffer and the buffer.
Definition: buf.c:1253
#define f(i)
Definition: sha256.c:46

References f, free(), and rz_buf_free().

Referenced by ar_open_all(), ar_open_file(), and rz_io_ar_close().

◆ ar_open_all()

RZ_API RzList* ar_open_all ( const char *  arname,
int  perm 
)

Open specific file withen a ar/lib file.

Parameters
arnamethe name of the .a file
Returns
a list of files or NULL

Open an ar/lib and returns all the object files inside it.

Definition at line 218 of file ar.c.

218  {
219  if (!arname) {
220  rz_sys_perror(__FUNCTION__);
221  return NULL;
222  }
223 
225  if (!files) {
226  rz_sys_perror(__FUNCTION__);
227  return NULL;
228  }
229 
230  RzBuffer *b = rz_buf_new_file(arname, perm, 0);
231  if (!b) {
233  rz_sys_perror(__FUNCTION__);
234  return NULL;
235  }
236 
237  ut64 arsize = rz_buf_size(b);
238 
239  if (!ar_check_magic(b)) {
241  rz_buf_free(b);
242  return NULL;
243  }
244 
245  filetable tbl = { NULL, 0, 0 };
246  int res = -1;
247  bool shared = false;
248 
249  do {
250  shared = !rz_list_empty(files);
251  RzArFp *arf = arfp_new(b, shared);
252  if (!arf) {
254  if (!shared) {
255  rz_buf_free(b);
256  }
257  return NULL;
258  }
259  if ((res = ar_parse_header(arf, &tbl, arsize)) <= 0) {
260  // on error or when it has reached the EOF
261  free(tbl.data);
262  ar_close(arf);
263  return files;
264  }
265  if (!rz_list_append(files, arf)) {
266  free(tbl.data);
267  ar_close(arf);
269  return NULL;
270  }
271  } while (res > 0);
272 
273  // this portion should never be reached
274  free(tbl.data);
275  return files;
276 }
bool ar_check_magic(RzBuffer *b)
Definition: ar.c:28
static int ar_parse_header(RzArFp *arf, filetable *tbl, ut64 arsize)
Definition: ar.c:76
static RzArFp * arfp_new(RzBuffer *b, bool shared_buf)
Definition: ar.c:15
RZ_API void ar_close(RzArFp *f)
Definition: ar.c:338
#define NULL
Definition: cris-opc.c:27
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 ut64 rz_buf_size(RZ_NONNULL RzBuffer *b)
Return the size of the buffer.
Definition: buf.c:1225
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
void(* RzListFree)(void *ptr)
Definition: rz_list.h:11
#define rz_sys_perror(x)
Definition: rz_types.h:336
#define b(i)
Definition: sha256.c:42
Definition: ar.c:9
char * data
Definition: ar.c:10
Definition: ar.h:7
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References ar_check_magic(), ar_close(), ar_parse_header(), arfp_new(), b, Filetable::data, files, free(), NULL, rz_buf_free(), rz_buf_new_file(), rz_buf_size(), rz_list_append(), rz_list_free(), rz_list_newf(), rz_sys_perror, and ut64().

Referenced by rz_io_ar_open_many().

◆ ar_open_file()

RZ_API RzArFp* ar_open_file ( const char *  arname,
int  perm,
const char *  filename 
)

Open specific file withen a ar/lib file.

Parameters
arnamethe name of the .a file
filenamethe name of file in the .a file that you wish to open
Returns
a handle of the internal filename or NULL

Open an ar/lib file by name.

Definition at line 286 of file ar.c.

286  {
287  if (!filename || !arname) {
288  rz_sys_perror(__FUNCTION__);
289  return NULL;
290  }
291 
292  RzBuffer *b = rz_buf_new_file(arname, perm, 0);
293  if (!b) {
294  rz_sys_perror(__FUNCTION__);
295  return NULL;
296  }
297 
298  ut64 arsize = rz_buf_size(b);
299 
300  if (!ar_check_magic(b)) {
301  rz_buf_free(b);
302  return NULL;
303  }
304 
305  RzArFp *arf = arfp_new(b, NULL);
306  if (!arf) {
307  rz_buf_free(b);
308  return NULL;
309  }
310 
311  filetable tbl = { NULL, 0, 0 };
312  int r;
313  while ((r = ar_parse_header(arf, &tbl, arsize)) > 0) {
314  if (filename) {
315  if (!strcmp(filename, arf->name)) {
316  // found the right file
317  break;
318  }
319  }
320 
321  // clean RzArFp for next loop
322  arf_clean_name(arf);
323  }
324 
325  free(tbl.data);
326 
327  if (r <= 0) {
328  if (r == 0 && filename) {
329  RZ_LOG_ERROR("ar: Cound not find file '%s' in archive '%s'\n", filename, arname);
330  }
331  ar_close(arf); // results in buf being free'd
332  return NULL;
333  }
334 
335  return arf;
336 }
static void arf_clean_name(RzArFp *arf)
Definition: ar.c:40
#define r
Definition: crypto_rc6.c:12
const char * filename
Definition: ioapi.h:137
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
char * name
Definition: ar.h:8

References ar_check_magic(), ar_close(), ar_parse_header(), arf_clean_name(), arfp_new(), b, Filetable::data, free(), RZARFP::name, NULL, r, rz_buf_free(), rz_buf_new_file(), rz_buf_size(), RZ_LOG_ERROR, rz_sys_perror, and ut64().

Referenced by rz_io_ar_open().

◆ ar_read_at()

RZ_API int ar_read_at ( RzArFp f,
ut64  off,
void *  buf,
int  count 
)

Definition at line 349 of file ar.c.

349  {
350  off += f->start;
351  if (off > f->end) {
352  return -1;
353  }
354  if (count + off > f->end) {
355  count = f->end - off;
356  }
357  return rz_buf_read_at(f->buf, off, buf, count);
358 }
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
voidpf void * buf
Definition: ioapi.h:138
int off
Definition: pal.c:13
RZ_API st64 rz_buf_read_at(RZ_NONNULL RzBuffer *b, ut64 addr, RZ_NONNULL RZ_OUT ut8 *buf, ut64 len)
Read len bytes of the buffer at the specified address.
Definition: buf.c:1136

References count, f, off, and rz_buf_read_at().

Referenced by rz_io_ar_read().

◆ ar_write_at()

RZ_API int ar_write_at ( RzArFp f,
ut64  off,
void *  buf,
int  count 
)

Definition at line 360 of file ar.c.

360  {
361  off += f->start;
362  if (off > f->end) {
363  return -1;
364  }
365  if (count + off > f->end) {
366  count = f->end - off;
367  }
368  return rz_buf_write_at(f->buf, off + f->start, buf, count);
369 }
RZ_API st64 rz_buf_write_at(RZ_NONNULL RzBuffer *b, ut64 addr, RZ_NONNULL const ut8 *buf, ut64 len)
Write len bytes of the buffer at the specified address.
Definition: buf.c:1197

References count, f, off, and rz_buf_write_at().

Referenced by rz_io_ar_write().