radareorg / radare2-bindings

Bindings of the r2 api for Valabind and friends
GNU Lesser General Public License v3.0
130 stars 92 forks source link

Rework C Example file #222

Closed afjoseph closed 4 months ago

afjoseph commented 5 years ago

The following example file uses old APIs r_bin_load() and does not take r_io file descriptors into account: https://github.com/radare/radare2-bindings/blob/master/c/test-bin.c.

I propose the following example file:

#include <r_bin.h>
#include <r_core.h>

bool
get_bin_info_from_r2(const char* restrict a_filename, Binary const* restrict a_bin)
{
    RCore rcore = { 0 };
    RBin* rbin = 0;
    RBinInfo* rinfo = 0;
    RBinOptions ropt = { 0 };
    int fd = 0;
    bool ret = false;

    r_core_init(&rcore);
    rbin = rcore.bin;

    fd = r_io_fd_open(rbin->iob.io, a_filename, R_PERM_R, 0);
    if (fd == -1) {
        printf("r_core: Cannot open file '%s'\n", a_filename);
        goto cleanup;
        ret = false;
    }

    r_bin_options_init(&ropt, fd, 0, UT64_MAX, false);
    if (!r_bin_open_io(rbin, &ropt)) {
        fprintf(stderr, "Failed to get binary rinfo\n");
        goto cleanup;
        ret = false;
    }

    rinfo = r_bin_get_info(rbin);
    if (!rinfo) {
        fprintf(stderr, "Failed to get binary rinfo\n");
        goto cleanup;
        ret = false;
    }

    printf("arch: %s\n", rinfo->arch);
    printf("bits: %d\n", rinfo->bits);

    ret = true;

cleanup:
    if (fd != -1)
        r_io_fd_close(rbin->iob.io, fd);
    if (!rinfo)
        r_bin_info_free(rinfo);

    r_core_fini(&rcore);

    return ret;
}

If I get a :+1:, I'll proceed with the PR :)

radare commented 5 years ago

Make a pr and i’ll review it there

On 2 Sep 2019, at 08:55, Abdullah Joseph notifications@github.com wrote:

The following example file uses old APIs r_bin_load() and does not take r_io file descriptors into account: https://github.com/radare/radare2-bindings/blob/master/c/test-bin.c.

I propose the following example file:

include

include

bool get_bin_info_from_r2(const char restrict a_filename, Binary const restrict a_bin) { RCore rcore = { 0 }; RBin rbin = 0; RBinInfo rinfo = 0; RBinOptions ropt = { 0 }; int fd = 0; bool ret = false;

r_core_init(&rcore);
rbin = rcore.bin;

fd = r_io_fd_open(rbin->iob.io, a_filename, R_PERM_R, 0);
if (fd == -1) {
    printf("r_core: Cannot open file '%s'\n", a_filename);
    goto cleanup;
    ret = false;
}

r_bin_options_init(&ropt, fd, 0, UT64_MAX, false);
if (!r_bin_open_io(rbin, &ropt)) {
    fprintf(stderr, "Failed to get binary rinfo\n");
    goto cleanup;
    ret = false;
}

rinfo = r_bin_get_info(rbin);
if (!rinfo) {
    fprintf(stderr, "Failed to get binary rinfo\n");
    goto cleanup;
    ret = false;
}

printf("arch: %s\n", rinfo->arch);
printf("bits: %d\n", rinfo->bits);

ret = true;

cleanup: if (fd != -1) r_io_fd_close(rbin->iob.io, fd); if (!rinfo) r_bin_info_free(rinfo);

r_core_fini(&rcore);

return ret;

} If I get a 👍, I'll proceed with the PR :)

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

trufae commented 4 months ago

Check the r2skel repo for examples. it will be good to have more examples in this repository, and the api has been changing over time, so r2skel is the right place to keep track of the evolution for those usecases.

closing here