diff --git a/libr/bin/p/bin_art.c b/libr/bin/p/bin_art.c index c0c739283c..26b3eac30d 100644 --- a/libr/bin/p/bin_art.c +++ b/libr/bin/p/bin_art.c @@ -119,11 +119,11 @@ static RBinInfo* info(RBinFile *arch) { return ret; } -static int check_bytes(const ut8 *buf, ut64 length) { +static bool check_bytes(const ut8 *buf, ut64 length) { return (buf && length>3 && !strncmp ((const char *)buf, "art\n", 4)); } -static int check(RBinFile *arch) { +static bool check(RBinFile *arch) { const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; ut64 sz = arch ? r_buf_size (arch->buf): 0; return check_bytes (bytes, sz); diff --git a/libr/bin/p/bin_bf.c b/libr/bin/p/bin_bf.c index 53b524a4a9..7f06d8fd26 100644 --- a/libr/bin/p/bin_bf.c +++ b/libr/bin/p/bin_bf.c @@ -5,9 +5,6 @@ #include #include -static int check(RBinFile *arch); -static int check_bytes(const ut8 *buf, ut64 length); - static Sdb* get_sdb (RBinObject *o) { if (!o) return NULL; //struct r_bin_[NAME]_obj_t *bin = (struct r_bin_r_bin_[NAME]_obj_t *) o->bin_obj; @@ -71,14 +68,7 @@ static RBinInfo* info(RBinFile *arch) { return ret; } -static int check(RBinFile *arch) { - const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; - ut64 sz = arch ? r_buf_size (arch->buf): 0; - return check_bytes (bytes, sz); - -} - -static int check_bytes(const ut8 *buf, ut64 length) { +static bool check_bytes(const ut8 *buf, ut64 length) { int i, is_bf = 0; if (buf && length > 0) { int max = R_MIN (16, length); @@ -106,6 +96,13 @@ static int check_bytes(const ut8 *buf, ut64 length) { return is_bf; } +static bool check(RBinFile *arch) { + const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; + ut64 sz = arch ? r_buf_size (arch->buf): 0; + return check_bytes (bytes, sz); + +} + static RList* entries(RBinFile *arch) { RList *ret; RBinAddr *ptr = NULL; diff --git a/libr/bin/p/bin_bflt.c b/libr/bin/p/bin_bflt.c index 833bbba865..7bf0b87c69 100644 --- a/libr/bin/p/bin_bflt.c +++ b/libr/bin/p/bin_bflt.c @@ -295,11 +295,11 @@ static RBinInfo *info(RBinFile *arch) { return info; } -static int check_bytes(const ut8 *buf, ut64 length) { +static bool check_bytes(const ut8 *buf, ut64 length) { return length > 4 && !memcmp (buf, "bFLT", 4); } -static int check(RBinFile *arch) { +static bool check(RBinFile *arch) { const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; ut64 sz = arch ? r_buf_size (arch->buf): 0; if (!bytes || !sz) { diff --git a/libr/bin/p/bin_bios.c b/libr/bin/p/bin_bios.c index 8615047f4f..77f7089fa9 100644 --- a/libr/bin/p/bin_bios.c +++ b/libr/bin/p/bin_bios.c @@ -5,9 +5,6 @@ #include #include -static int check(RBinFile *arch); -static int check_bytes(const ut8 *buf, ut64 length); - static Sdb* get_sdb (RBinObject *o) { if (!o) return NULL; //struct r_bin_[NAME]_obj_t *bin = (struct r_bin_r_bin_[NAME]_obj_t *) o->bin_obj; @@ -62,13 +59,7 @@ static RBinInfo* info(RBinFile *arch) { return ret; } -static int check(RBinFile *arch) { - const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; - ut64 sz = arch ? r_buf_size (arch->buf): 0; - return check_bytes (bytes, sz); -} - -static int check_bytes(const ut8 *buf, ut64 length) { +static bool check_bytes(const ut8 *buf, ut64 length) { if (buf && length > 0xffff && buf[0] != 0xcf) { const ut32 ep = length - 0x10000 + 0xfff0; /* F000:FFF0 address */ /* hacky check to avoid detecting multidex bins as bios */ @@ -84,16 +75,24 @@ static int check_bytes(const ut8 *buf, ut64 length) { return 0; } +static bool check(RBinFile *arch) { + const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; + ut64 sz = arch ? r_buf_size (arch->buf): 0; + return check_bytes (bytes, sz); +} + static RList* sections(RBinFile *arch) { RList *ret = NULL; RBinSection *ptr = NULL; - if (!(ret = r_list_new ())) + if (!(ret = r_list_new ())) { return NULL; + } ret->free = free; // program headers is another section - if (!(ptr = R_NEW0 (RBinSection))) + if (!(ptr = R_NEW0 (RBinSection))) { return ret; + } strcpy (ptr->name, "bootblk"); ptr->vsize = ptr->size = 0x10000; //printf ("SIZE %d\n", ptr->size); diff --git a/libr/bin/p/bin_bootimg.c b/libr/bin/p/bin_bootimg.c index d465830981..2a55e16f81 100644 --- a/libr/bin/p/bin_bootimg.c +++ b/libr/bin/p/bin_bootimg.c @@ -134,11 +134,11 @@ static RBinInfo* info(RBinFile *arch) { return ret; } -static int check_bytes(const ut8 *buf, ut64 length) { +static bool check_bytes(const ut8 *buf, ut64 length) { return (buf && length>12 && !strncmp ((const char *)buf, "ANDROID!", 8)); } -static int check(RBinFile *arch) { +static bool check(RBinFile *arch) { const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; ut64 sz = arch ? r_buf_size (arch->buf): 0; return check_bytes (bytes, sz); diff --git a/libr/bin/p/bin_cgc.c b/libr/bin/p/bin_cgc.c index b2122a51a8..d147e94525 100644 --- a/libr/bin/p/bin_cgc.c +++ b/libr/bin/p/bin_cgc.c @@ -12,12 +12,11 @@ extern struct r_bin_dbginfo_t r_bin_dbginfo_elf; extern struct r_bin_write_t r_bin_write_elf; -static int check_bytes(const ut8 *buf, ut64 length) { - return buf && length > 4 && memcmp (buf, CGCMAG, SCGCMAG) == 0 - && buf[4] != 2; +static bool check_bytes(const ut8 *buf, ut64 length) { + return buf && length > 4 && !memcmp (buf, CGCMAG, SCGCMAG) && buf[4] != 2; } -static int check(RBinFile *arch) { +static bool check(RBinFile *arch) { const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; ut64 sz = arch ? r_buf_size (arch->buf): 0; return check_bytes (bytes, sz); diff --git a/libr/bin/p/bin_coff.c b/libr/bin/p/bin_coff.c index 8fe4f8082f..c47c0a37f2 100644 --- a/libr/bin/p/bin_coff.c +++ b/libr/bin/p/bin_coff.c @@ -7,9 +7,6 @@ #include "coff/coff.h" -static int check(RBinFile *arch); -static int check_bytes(const ut8 *buf, ut64 length); - static Sdb* get_sdb(RBinObject *o) { if (!o) { return NULL; @@ -337,14 +334,7 @@ static ut64 size(RBinFile *arch) { return 0; } -static int check(RBinFile *arch) { - const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; - ut64 sz = arch ? r_buf_size (arch->buf): 0; - return check_bytes (bytes, sz); - -} - -static int check_bytes(const ut8 *buf, ut64 length) { +static bool check_bytes(const ut8 *buf, ut64 length) { #if 0 TODO: do more checks here to avoid false positives @@ -362,6 +352,13 @@ ut16 CHARACTERISTICS return false; } +static bool check(RBinFile *arch) { + const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; + ut64 sz = arch ? r_buf_size (arch->buf): 0; + return check_bytes (bytes, sz); + +} + RBinPlugin r_bin_plugin_coff = { .name = "coff", .desc = "COFF format r_bin plugin", diff --git a/libr/bin/p/bin_dex.c b/libr/bin/p/bin_dex.c index 74d6934976..c724d3b086 100644 --- a/libr/bin/p/bin_dex.c +++ b/libr/bin/p/bin_dex.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2011-2016 - pancake */ +/* radare - LGPL - Copyright 2011-2017 - pancake, h4ng3r */ #include #include @@ -11,22 +11,14 @@ extern struct r_bin_dbginfo_t r_bin_dbginfo_dex; -#define DEBUG_PRINTF 0 - -#if DEBUG_PRINTF -#define dprintf eprintf -#else -#define dprintf if (0)eprintf -#endif - static bool dexdump = false; static Sdb *mdb = NULL; -static Sdb *cdb = NULL; // TODO: remove if it is not used static char *getstr(RBinDexObj *bin, int idx) { ut8 buf[6]; ut64 len; int uleblen; + char c = 0; if (!bin || idx < 0 || idx >= bin->header.strings_size || !bin->strings) { return NULL; @@ -45,16 +37,14 @@ static char *getstr(RBinDexObj *bin, int idx) { if (!len || len >= bin->size) { return NULL; } - // TODO: improve this ugly fix - char c = 'a'; - while (c) { + do { ut64 offset = bin->strings[idx] + uleblen + len; if (offset >= bin->size || offset < len) { return NULL; } r_buf_read_at (bin->b, offset, (ut8*)&c, 1); len++; - } + } while (c); if ((int)len > 0 && len < R_BIN_SIZEOF_STRINGS) { char *str = calloc (1, len + 1); if (str) { @@ -149,22 +139,22 @@ static char *createAccessFlagStr(ut32 flags, AccessFor forWhat) { "?", /* 0x20000 */ }, }; + int i, count = countOnes (flags); const int kLongest = 21; - int i, count; - char* str; - char* cp; - count = countOnes(flags); - // XXX check if this allocation is safe what if all the arithmetic + char* str, *cp; // produces a huge number???? - cp = str = (char*) malloc (count * (kLongest + 1) + 1); + cp = str = (char*) calloc (count + 1, (kLongest + 1)); + if (!str) { + return NULL; + } for (i = 0; i < NUM_FLAGS; i++) { if (flags & 0x01) { - const char* accessStr = kAccessStrings[forWhat][i]; - int len = strlen(accessStr); + const char *accessStr = kAccessStrings[forWhat][i]; + int len = strlen (accessStr); if (cp != str) { *cp++ = ' '; } - memcpy(cp, accessStr, len); + memcpy (cp, accessStr, len); cp += len; } flags >>= 1; @@ -230,7 +220,13 @@ static char *dex_method_signature(RBinDexObj *bin, int method_idx) { } buff_len = strlen (buff); size += buff_len + 1; - signature = realloc (signature, size); + char *newsig = realloc (signature, size); + if (!newsig) { + eprintf ("Cannot realloc to %d\n", size); + free (signature); + break; + } + signature = newsig; strcpy (signature + pos, buff); pos += buff_len; signature[pos] = '\0'; @@ -374,10 +370,10 @@ static void dex_parse_debug_item(RBinFile *binfile, RBinDexObj *bin, debug_locals[reg].startAddress = address; debug_locals[reg].live = true; } - --parameters_size; + parameters_size--; } - if (p4 <= 0) { + if (p4 < 1) { return; } ut8 opcode = *(p4++) & 0xff; @@ -442,10 +438,7 @@ static void dex_parse_debug_item(RBinFile *binfile, RBinDexObj *bin, break; case 0x4: //DBG_START_LOCAL_EXTENDED { - ut64 register_num; - ut64 name_idx; - ut64 type_idx; - ut64 sig_idx; + ut64 register_num, name_idx, type_idx, sig_idx; p4 = r_uleb128 (p4, p4_end - p4, ®ister_num); p4 = r_uleb128 (p4, p4_end - p4, &name_idx); name_idx -= 1; @@ -531,7 +524,7 @@ static void dex_parse_debug_item(RBinFile *binfile, RBinDexObj *bin, break; default: { - int adjusted_opcode = opcode - 0x0a; + int adjusted_opcode = opcode - 10; address += (adjusted_opcode / 15); line += -4 + (adjusted_opcode % 15); struct dex_debug_position_t *position = @@ -627,9 +620,6 @@ static void dex_parse_debug_item(RBinFile *binfile, RBinDexObj *bin, r_list_free (params); } -static int check (RBinFile *arch); -static int check_bytes (const ut8 *buf, ut64 length); - static Sdb *get_sdb (RBinObject *o) { if (!o || !o->bin_obj) { return NULL; @@ -672,13 +662,7 @@ static ut64 baddr(RBinFile *arch) { return 0; } -static int check(RBinFile *arch) { - const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; - ut64 sz = arch ? r_buf_size (arch->buf): 0; - return check_bytes (bytes, sz); -} - -static int check_bytes(const ut8 *buf, ut64 length) { +static bool check_bytes(const ut8 *buf, ut64 length) { if (!buf || length < 8) { return false; } @@ -705,6 +689,12 @@ static int check_bytes(const ut8 *buf, ut64 length) { return false; } +static bool check(RBinFile *arch) { + const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; + ut64 sz = arch ? r_buf_size (arch->buf): 0; + return check_bytes (bytes, sz); +} + static RBinInfo *info(RBinFile *arch) { RBinHash *h; RBinInfo *ret = R_NEW0 (RBinInfo); @@ -1240,6 +1230,7 @@ static const ut8 *parse_dex_class_method(RBinFile *binfile, RBinDexObj *bin, // ----------------- // WORK IN PROGRESS // ----------------- +#if 0 if (0) { if (MA & 0x10000) { //ACC_CONSTRUCTOR if (!cdb) { @@ -1248,6 +1239,7 @@ static const ut8 *parse_dex_class_method(RBinFile *binfile, RBinDexObj *bin, sdb_num_set (cdb, sdb_fmt (0, "%d", c->class_id), sym->paddr, 0); } } +#endif } else { sym->size = 0; r_list_append (bin->methods_list, sym); @@ -1476,10 +1468,9 @@ static int dex_loadcode(RBinFile *arch, RBinDexObj *bin) { } methods = calloc (1, amount + 1); for (i = 0; i < bin->header.class_size; i++) { - char *super_name, *class_name; struct dex_class_t *c = &bin->classes[i]; - class_name = dex_class_name (bin, c); - super_name = dex_class_super_name (bin, c); + char *class_name = dex_class_name (bin, c); + char *super_name = dex_class_super_name (bin, c); if (dexdump) { rbin->cb_printf ("Class #%d -\n", i); } @@ -1681,7 +1672,6 @@ static int getoffset(RBinFile *arch, int type, int idx) { return dex_get_type_offset (arch, idx); case 'c': // class return dex_get_type_offset (arch, idx); - //return sdb_num_get (cdb, sdb_fmt (0, "%d", idx), 0); } return -1; } @@ -1764,7 +1754,6 @@ static RList *sections(RBinFile *arch) { } else { ptr->size = ptr->vsize = arch->buf->length - ptr->vaddr; // hacky workaround - //dprintf ("Hack\n"); //ptr->size = ptr->vsize = 1024; } ptr->srwx = R_BIN_SCN_READABLE | R_BIN_SCN_MAP; //|2; @@ -1832,10 +1821,10 @@ RBinPlugin r_bin_plugin_dex = { .license = "LGPL3", .get_sdb = &get_sdb, .load = &load, - .load_bytes = &load_bytes, - .check = &check, - .check_bytes = &check_bytes, - .baddr = &baddr, + .load_bytes = load_bytes, + .check = check, + .check_bytes = check_bytes, + .baddr = baddr, .entries = entries, .classes = classes, .sections = sections, diff --git a/libr/bin/p/bin_dol.c b/libr/bin/p/bin_dol.c index f75946d157..318f9dafdf 100644 --- a/libr/bin/p/bin_dol.c +++ b/libr/bin/p/bin_dol.c @@ -37,20 +37,19 @@ typedef struct __attribute__((packed)) { // 0x100 -- start of data section } DolHeader; -static int check(RBinFile *arch); -static int check_bytes(const ut8 *buf, ut64 length); +static bool check_bytes(const ut8 *buf, ut64 length) { + if (!buf || length < 6) { + return false; + } + return (!memcmp (buf, "\x00\x00\x01\x00\x00\x00", 6)); +} -static int check(RBinFile *arch) { +static bool check(RBinFile *arch) { const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; ut64 sz = arch ? r_buf_size (arch->buf): 0; return check_bytes (bytes, sz); } -static int check_bytes(const ut8 *buf, ut64 length) { - if (!buf || length < 6) return false; - return (!memcmp (buf, "\x00\x00\x01\x00\x00\x00", 6)); -} - static void * load_bytes(RBinFile *arch, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb) { bool has_dol_extension = false; DolHeader * dol; diff --git a/libr/bin/p/bin_dyldcache.c b/libr/bin/p/bin_dyldcache.c index 99918f592e..6f4ca7a056 100644 --- a/libr/bin/p/bin_dyldcache.c +++ b/libr/bin/p/bin_dyldcache.c @@ -5,18 +5,9 @@ #include #include -static int check(RBinFile *arch); -static int check_bytes(const ut8 *buf, ut64 length); - -static int check(RBinFile *arch) { - const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; - ut64 sz = arch ? r_buf_size (arch->buf): 0; - return check_bytes (bytes, sz); -} - static bool dyld64 = false; -static int check_bytes(const ut8 *buf, ut64 length) { +static bool check_bytes(const ut8 *buf, ut64 length) { bool rc = false; if (buf && length >= 32) { char arch[9] = {0}; @@ -32,6 +23,12 @@ static int check_bytes(const ut8 *buf, ut64 length) { return rc; } +static bool check(RBinFile *arch) { + const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; + ut64 sz = arch ? r_buf_size (arch->buf): 0; + return check_bytes (bytes, sz); +} + static void * load_bytes(RBinFile *arch, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb){ return (void*)(size_t)check_bytes (buf, sz); } diff --git a/libr/bin/p/bin_elf.c b/libr/bin/p/bin_elf.c index 8b49e3282d..7ccc302f9e 100644 --- a/libr/bin/p/bin_elf.c +++ b/libr/bin/p/bin_elf.c @@ -886,12 +886,12 @@ static void headers32(RBinFile *arch) { p ("0x00000014 ShOff 0x%08x\n", r_read_le32 (buf + 20)); } -static int check_bytes(const ut8 *buf, ut64 length) { +static bool check_bytes(const ut8 *buf, ut64 length) { return buf && length > 4 && memcmp (buf, ELFMAG, SELFMAG) == 0 && buf[4] != 2; } -static int check(RBinFile *arch) { +static bool check(RBinFile *arch) { const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; ut64 sz = arch ? r_buf_size (arch->buf): 0; return check_bytes (bytes, sz); diff --git a/libr/bin/p/bin_elf64.c b/libr/bin/p/bin_elf64.c index d01cce63ea..573607ac64 100644 --- a/libr/bin/p/bin_elf64.c +++ b/libr/bin/p/bin_elf64.c @@ -3,19 +3,18 @@ #define R_BIN_ELF64 1 #include "bin_elf.c" -static int check(RBinFile *arch); -static int check_bytes(const ut8 *buf, ut64 length); - -static int check(RBinFile *arch) { +static bool check(RBinFile *arch) { const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; ut64 sz = arch ? r_buf_size (arch->buf): 0; return check_bytes (bytes, sz); } -static int check_bytes(const ut8 *buf, ut64 length) { - if (buf && length >= 5) - if (!memcmp (buf, "\x7F\x45\x4c\x46\x02", 5)) +static bool check_bytes(const ut8 *buf, ut64 length) { + if (buf && length >= 5) { + if (!memcmp (buf, "\x7F\x45\x4c\x46\x02", 5)) { return true; + } + } return false; } diff --git a/libr/bin/p/bin_fs.c b/libr/bin/p/bin_fs.c index d41f887e2c..9902b43527 100644 --- a/libr/bin/p/bin_fs.c +++ b/libr/bin/p/bin_fs.c @@ -6,9 +6,6 @@ #include #include "../../fs/types.h" -static int check(RBinFile *arch); -static int check_bytes(const ut8 *buf, ut64 length); - //static char *fsname(RBinFile *arch) { static char *fsname(const ut8* buf, ut64 length) { ut8 fs_lbuf[1024]; @@ -106,22 +103,24 @@ static RBinInfo* info(RBinFile *arch) { return ret; } -static int check(RBinFile *arch) { - const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; - ut64 sz = arch ? r_buf_size (arch->buf): 0; - return check_bytes (bytes, sz); -} - -static int check_bytes(const ut8 *buf, ut64 length) { +static bool check_bytes(const ut8 *buf, ut64 length) { if (!buf || (st64)length <1) return false; char *p = fsname (buf, length); free (p); return p != NULL; } +static bool check(RBinFile *arch) { + const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; + ut64 sz = arch ? r_buf_size (arch->buf): 0; + return check_bytes (bytes, sz); +} + RBinPlugin r_bin_plugin_fs = { .name = "fs", .desc = "filesystem bin plugin", + .author = "pancake", + .version = "1.0", .license = "LGPL3", .get_sdb = &get_sdb, .load = &load, @@ -135,7 +134,7 @@ RBinPlugin r_bin_plugin_fs = { }; #ifndef CORELIB -struct r_lib_struct_t radare_plugin = { +RLiBStruct radare_plugin = { .type = R_LIB_TYPE_BIN, .data = &r_bin_plugin_fs, .version = R2_VERSION diff --git a/libr/bin/p/bin_java.c b/libr/bin/p/bin_java.c index 07cc0c9561..8021cf7840 100644 --- a/libr/bin/p/bin_java.c +++ b/libr/bin/p/bin_java.c @@ -11,8 +11,6 @@ #define IFDBG_BIN_JAVA if(0) static Sdb *DB = NULL; -static int check(RBinFile *arch); -static int check_bytes(const ut8 *buf, ut64 length); static void add_bin_obj_to_sdb(RBinJavaObj *bin); static int add_sdb_bin_obj(const char *key, RBinJavaObj *bin_obj); @@ -168,14 +166,7 @@ static RBinInfo* info(RBinFile *arch) { return ret; } -static int check(RBinFile *arch) { - const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; - ut64 sz = arch ? r_buf_size (arch->buf): 0; - return check_bytes (bytes, sz); - -} - -static int check_bytes(const ut8 *buf, ut64 length) { +static bool check_bytes(const ut8 *buf, ut64 length) { bool ret = false; int off, version = 0; if (buf && length>32 && !memcmp (buf, "\xca\xfe\xba\xbe", 4)) { @@ -191,6 +182,13 @@ static int check_bytes(const ut8 *buf, ut64 length) { return ret; } +static bool check(RBinFile *arch) { + const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; + ut64 sz = arch ? r_buf_size (arch->buf): 0; + return check_bytes (bytes, sz); + +} + static int retdemangle(const char *str) { return R_BIN_NM_JAVA; } diff --git a/libr/bin/p/bin_mach0.c b/libr/bin/p/bin_mach0.c index f0962546f5..233390f278 100644 --- a/libr/bin/p/bin_mach0.c +++ b/libr/bin/p/bin_mach0.c @@ -9,8 +9,6 @@ extern RBinWrite r_bin_write_mach0; -static int check(RBinFile *arch); -static int check_bytes(const ut8 *buf, ut64 length); static RBinInfo* info(RBinFile *arch); static Sdb* get_sdb (RBinObject *o) { @@ -424,13 +422,7 @@ static RBinInfo* info(RBinFile *arch) { } #if !R_BIN_MACH064 -static int check(RBinFile *arch) { - const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; - ut64 sz = arch ? r_buf_size (arch->buf): 0; - return check_bytes (bytes, sz); -} - -static int check_bytes(const ut8 *buf, ut64 length) { +static bool check_bytes(const ut8 *buf, ut64 length) { if (buf && length >= 4) { if (!memcmp (buf, "\xce\xfa\xed\xfe", 4) || !memcmp (buf, "\xfe\xed\xfa\xce", 4)) @@ -439,6 +431,12 @@ static int check_bytes(const ut8 *buf, ut64 length) { return false; } +static bool check(RBinFile *arch) { + const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; + ut64 sz = arch ? r_buf_size (arch->buf): 0; + return check_bytes (bytes, sz); +} + #if 0 typedef struct r_bin_create_t { int arch; diff --git a/libr/bin/p/bin_mach064.c b/libr/bin/p/bin_mach064.c index f72a43c386..2540252edd 100644 --- a/libr/bin/p/bin_mach064.c +++ b/libr/bin/p/bin_mach064.c @@ -5,16 +5,7 @@ #include "objc/mach064_classes.h" -static int check(RBinFile *arch); -static int check_bytes(const ut8 *buf, ut64 length); - -static int check(RBinFile *arch) { - const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; - ut64 sz = arch ? r_buf_size (arch->buf): 0; - return check_bytes (bytes, sz); -} - -static int check_bytes(const ut8 *buf, ut64 length) { +static bool check_bytes(const ut8 *buf, ut64 length) { if (buf && length > 4) if (!memcmp (buf, "\xfe\xed\xfa\xcf", 4) || !memcmp (buf, "\xcf\xfa\xed\xfe", 4)) @@ -22,6 +13,12 @@ static int check_bytes(const ut8 *buf, ut64 length) { return false; } +static bool check(RBinFile *arch) { + const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; + ut64 sz = arch ? r_buf_size (arch->buf): 0; + return check_bytes (bytes, sz); +} + static RBuffer* create(RBin* bin, const ut8 *code, int codelen, const ut8 *data, int datalen) { const bool use_pagezero = true; const bool use_main = true; diff --git a/libr/bin/p/bin_mbn.c b/libr/bin/p/bin_mbn.c index cacc19efc8..67006e4f45 100644 --- a/libr/bin/p/bin_mbn.c +++ b/libr/bin/p/bin_mbn.c @@ -18,22 +18,10 @@ typedef struct sbl_header { ut32 cert_sz; } SBLHDR; -static int check(RBinFile *arch); -static int check_bytes(const ut8 *buf, ut64 length); - // TODO avoid globals static SBLHDR sb = {0}; -static int check(RBinFile *arch) { - if (arch && arch->buf) { - const ut8 *bytes = r_buf_buffer (arch->buf); - ut64 sz = r_buf_size (arch->buf); - return check_bytes (bytes, sz); - } - return false; -} - -static int check_bytes(const ut8 *buf, ut64 bufsz) { +static bool check_bytes(const ut8 *buf, ut64 bufsz) { if (buf && bufsz >= sizeof (SBLHDR)) { RBuffer *b = r_buf_new_with_pointers (buf, bufsz); int ret = r_buf_fread_at (b, 0, (ut8*)&sb, "10i", 1); @@ -73,6 +61,15 @@ static int check_bytes(const ut8 *buf, ut64 bufsz) { return false; } +static bool check(RBinFile *arch) { + if (arch && arch->buf) { + const ut8 *bytes = r_buf_buffer (arch->buf); + ut64 sz = r_buf_size (arch->buf); + return check_bytes (bytes, sz); + } + return false; +} + static void * load_bytes(RBinFile *arch, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb){ return (void*)(size_t)check_bytes (buf, sz); } diff --git a/libr/bin/p/bin_menuet.c b/libr/bin/p/bin_menuet.c index 110a9ce2a2..eef9b45c29 100644 --- a/libr/bin/p/bin_menuet.c +++ b/libr/bin/p/bin_menuet.c @@ -7,9 +7,6 @@ #define MENUET_VERSION(x) x[7] -static int check(RBinFile *arch); -static int check_bytes(const ut8 *buf, ut64 length); - #if 0 db 'MENUET00' ; 8 byte id dd 38 ; required os @@ -52,13 +49,7 @@ static int check_bytes(const ut8 *buf, ut64 length); #endif -static int check(RBinFile *arch) { - const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; - ut64 sz = arch ? r_buf_size (arch->buf): 0; - return check_bytes (bytes, sz); -} - -static int check_bytes(const ut8 *buf, ut64 length) { +static bool check_bytes(const ut8 *buf, ut64 length) { if (buf && length >= 32 && !memcmp (buf, "MENUET0", 7)) { switch (buf[7]) { case '0': @@ -71,6 +62,12 @@ static int check_bytes(const ut8 *buf, ut64 length) { return false; } +static bool check(RBinFile *arch) { + const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; + ut64 sz = arch ? r_buf_size (arch->buf): 0; + return check_bytes (bytes, sz); +} + static void * load_bytes(RBinFile *arch, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb){ return (void*)(size_t)check_bytes (buf, sz); } diff --git a/libr/bin/p/bin_mz.c b/libr/bin/p/bin_mz.c index de2cd95a79..5a1a7d4e5f 100644 --- a/libr/bin/p/bin_mz.c +++ b/libr/bin/p/bin_mz.c @@ -26,7 +26,7 @@ static bool checkEntrypoint(const ut8 *buf, ut64 length) { return false; } -static int check_bytes(const ut8 *buf, ut64 length) { +static bool check_bytes(const ut8 *buf, ut64 length) { unsigned int exth_offset; int ret = false; if (!buf || length <= 0x3d) { diff --git a/libr/bin/p/bin_nes.c b/libr/bin/p/bin_nes.c index a26b90ead5..082da4528b 100644 --- a/libr/bin/p/bin_nes.c +++ b/libr/bin/p/bin_nes.c @@ -4,25 +4,22 @@ #include #include "nes/nes_specs.h" -static int check(RBinFile *arch); -static int check_bytes(const ut8 *buf, ut64 length); - static void * load_bytes(RBinFile *arch, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb){ check_bytes (buf, sz); return R_NOTNULL; } -static int check(RBinFile *arch) { +static bool check_bytes(const ut8 *buf, ut64 length) { + if (!buf || length < 4) return false; + return (!memcmp (buf, INES_MAGIC, 4)); +} + +static bool check(RBinFile *arch) { const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; ut64 sz = arch ? r_buf_size (arch->buf): 0; return check_bytes (bytes, sz); } -static int check_bytes(const ut8 *buf, ut64 length) { - if (!buf || length < 4) return false; - return (!memcmp (buf, INES_MAGIC, 4)); -} - static RBinInfo* info(RBinFile *arch) { RBinInfo *ret = NULL; ines_hdr ihdr; diff --git a/libr/bin/p/bin_nin3ds.c b/libr/bin/p/bin_nin3ds.c index 4d10b10002..fa37e00f98 100644 --- a/libr/bin/p/bin_nin3ds.c +++ b/libr/bin/p/bin_nin3ds.c @@ -8,24 +8,23 @@ #include "nin/n3ds.h" -static int check(RBinFile *arch); -static int check_bytes(const ut8 *buf, ut64 length); - static struct n3ds_firm_hdr loaded_header; -static int check(RBinFile *arch) { +static bool check(RBinFile *arch) { const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; ut64 sz = arch ? r_buf_size (arch->buf): 0; return check_bytes (bytes, sz); } -static int check_bytes(const ut8 *buf, ut64 length) { - if (!buf || length < sizeof(struct n3ds_firm_hdr)) return false; - return (!memcmp(buf, "FIRM", 4)); +static bool check_bytes(const ut8 *buf, ut64 length) { + if (!buf || length < sizeof (struct n3ds_firm_hdr)) { + return false; + } + return (!memcmp (buf, "FIRM", 4)); } static void * load_bytes(RBinFile *arch, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb) { - return memcpy (&loaded_header, buf, sizeof(struct n3ds_firm_hdr)); + return memcpy (&loaded_header, buf, sizeof (struct n3ds_firm_hdr)); } static int load(RBinFile *arch) { diff --git a/libr/bin/p/bin_ninds.c b/libr/bin/p/bin_ninds.c index 2297297df7..26a85db040 100644 --- a/libr/bin/p/bin_ninds.c +++ b/libr/bin/p/bin_ninds.c @@ -8,18 +8,9 @@ #include "../format/nin/nds.h" -static int check(RBinFile *arch); -static int check_bytes(const ut8 *buf, ut64 length); - static struct nds_hdr loaded_header; -static int check(RBinFile *arch) { - const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; - ut64 sz = arch ? r_buf_size (arch->buf): 0; - return check_bytes (bytes, sz); -} - -static int check_bytes(const ut8 *buf, ut64 length) { +static bool check_bytes(const ut8 *buf, ut64 length) { ut8 ninlogohead[6]; if (!buf || length < sizeof(struct nds_hdr)) /* header size */ return false; @@ -28,6 +19,12 @@ static int check_bytes(const ut8 *buf, ut64 length) { return (!memcmp (ninlogohead, "\x24\xff\xae\x51\x69\x9a", 6))? true : false; } +static bool check(RBinFile *arch) { + const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; + ut64 sz = arch ? r_buf_size (arch->buf): 0; + return check_bytes (bytes, sz); +} + static void * load_bytes(RBinFile *arch, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb) { return memcpy (&loaded_header, buf, sizeof(struct nds_hdr)); } diff --git a/libr/bin/p/bin_ningb.c b/libr/bin/p/bin_ningb.c index 6745428465..f1a20f2644 100644 --- a/libr/bin/p/bin_ningb.c +++ b/libr/bin/p/bin_ningb.c @@ -18,7 +18,7 @@ static void * load_bytes(RBinFile *arch, const ut8 *buf, ut64 sz, ut64 loadaddr, return R_NOTNULL; } -static int check_bytes(const ut8 *buf, ut64 length) { +static bool check_bytes(const ut8 *buf, ut64 length) { ut8 lict[48]; if (!buf || length < (0x104+48)) return 0; @@ -26,13 +26,13 @@ static int check_bytes(const ut8 *buf, ut64 length) { return (!memcmp (lict, lic, 48))? 1: 0; } -static int check(RBinFile *arch) { +static bool check(RBinFile *arch) { const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; ut64 sz = arch ? r_buf_size (arch->buf): 0; return check_bytes (bytes, sz); } -static int load(RBinFile *arch) { +static bool load(RBinFile *arch) { const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; ut64 sz = arch ? r_buf_size (arch->buf): 0; if (!arch || !arch->o) return false; diff --git a/libr/bin/p/bin_ningba.c b/libr/bin/p/bin_ningba.c index 59b529a8f6..3bded035fc 100644 --- a/libr/bin/p/bin_ningba.c +++ b/libr/bin/p/bin_ningba.c @@ -7,17 +7,7 @@ #include #include "../format/nin/gba.h" -static int check(RBinFile *arch); -int check_bytes(const ut8 *buf, ut64 length); - -static int check(RBinFile *arch) { - const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; - ut64 sz = arch ? r_buf_size (arch->buf): 0; - return check_bytes (bytes, sz); - -} - -int check_bytes(const ut8 *buf, ut64 length) { +static bool check_bytes(const ut8 *buf, ut64 length) { ut8 lict[156]; if (!buf || length < 160) return 0; @@ -25,6 +15,13 @@ int check_bytes(const ut8 *buf, ut64 length) { return (!memcmp (lict, lic_gba, 156))? 1: 0; } +static bool check(RBinFile *arch) { + const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; + ut64 sz = arch ? r_buf_size (arch->buf): 0; + return check_bytes (bytes, sz); + +} + static Sdb* get_sdb (RBinObject *o) { if (!o) return NULL; //struct r_bin_[NAME]_obj_t *bin = (struct r_bin_r_bin_[NAME]_obj_t *) o->bin_obj; diff --git a/libr/bin/p/bin_omf.c b/libr/bin/p/bin_omf.c index 0bb0334fa4..e0756270eb 100644 --- a/libr/bin/p/bin_omf.c +++ b/libr/bin/p/bin_omf.c @@ -29,7 +29,7 @@ static int destroy(RBinFile *arch) { return true; } -static int check_bytes(const ut8 *buf, ut64 length) { +static bool check_bytes(const ut8 *buf, ut64 length) { int i; if (!buf || length < 4) { return false; @@ -51,7 +51,7 @@ static int check_bytes(const ut8 *buf, ut64 length) { return r_bin_checksum_omf_ok ((char *)buf, length); } -static int check(RBinFile *arch) { +static bool check(RBinFile *arch) { const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; ut64 sz = arch ? r_buf_size (arch->buf): 0; return check_bytes (bytes, sz); diff --git a/libr/bin/p/bin_p9.c b/libr/bin/p/bin_p9.c index 8fcd07b65d..b5af1138c8 100644 --- a/libr/bin/p/bin_p9.c +++ b/libr/bin/p/bin_p9.c @@ -6,21 +6,18 @@ #include #include "../format/p9/p9bin.h" -static int check(RBinFile *arch); -static int check_bytes(const ut8 *buf, ut64 length); - -static int check(RBinFile *arch) { - const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; - ut64 sz = arch ? r_buf_size (arch->buf): 0; - return check_bytes (bytes, sz); -} - -static int check_bytes(const ut8 *buf, ut64 length) { +static bool check_bytes(const ut8 *buf, ut64 length) { if (buf && length >= 4) return (r_bin_p9_get_arch (buf, NULL, NULL)); return false; } +static bool check(RBinFile *arch) { + const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; + ut64 sz = arch ? r_buf_size (arch->buf): 0; + return check_bytes (bytes, sz); +} + static Sdb* get_sdb (RBinObject *o) { if (!o) return NULL; //struct r_bin_[NAME]_obj_t *bin = (struct r_bin_r_bin_[NAME]_obj_t *) o->bin_obj; diff --git a/libr/bin/p/bin_pe.c b/libr/bin/p/bin_pe.c index 3c8c18a080..7415ef2190 100644 --- a/libr/bin/p/bin_pe.c +++ b/libr/bin/p/bin_pe.c @@ -6,9 +6,6 @@ #include #include "pe/pe.h" -static int check(RBinFile *arch); -static int check_bytes(const ut8 *buf, ut64 length); - static Sdb* get_sdb (RBinObject *o) { struct PE_(r_bin_pe_obj_t) *bin; if (!o || !o->bin_obj) { @@ -528,14 +525,7 @@ static ut64 get_vaddr (RBinFile *arch, ut64 baddr, ut64 paddr, ut64 vaddr) { } #if !R_BIN_PE64 -static int check(RBinFile *arch) { - const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; - ut64 sz = arch ? r_buf_size (arch->buf): 0; - return check_bytes (bytes, sz); - -} - -static int check_bytes(const ut8 *buf, ut64 length) { +static bool check_bytes(const ut8 *buf, ut64 length) { unsigned int idx; if (!buf) { return false; @@ -554,6 +544,13 @@ static int check_bytes(const ut8 *buf, ut64 length) { return false; } +static bool check(RBinFile *arch) { + const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; + ut64 sz = arch ? r_buf_size (arch->buf): 0; + return check_bytes (bytes, sz); + +} + /* inspired in http://www.phreedom.org/solar/code/tinype/tiny.97/tiny.asm */ static RBuffer* create(RBin* bin, const ut8 *code, int codelen, const ut8 *data, int datalen) { ut32 hdrsize, p_start, p_opthdr, p_sections, p_lsrlc, n; diff --git a/libr/bin/p/bin_pe64.c b/libr/bin/p/bin_pe64.c index 8ae34a2fe7..3cbc00d0d4 100644 --- a/libr/bin/p/bin_pe64.c +++ b/libr/bin/p/bin_pe64.c @@ -3,16 +3,7 @@ #define R_BIN_PE64 1 #include "bin_pe.c" -static int check(RBinFile *arch); -static int check_bytes(const ut8 *buf, ut64 length); - -static int check(RBinFile *arch) { - const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; - ut64 sz = arch ? r_buf_size (arch->buf): 0; - return check_bytes (bytes, sz); -} - -static int check_bytes(const ut8 *buf, ut64 length) { +static bool check_bytes(const ut8 *buf, ut64 length) { int idx, ret = false; if (!buf || length <= 0x3d) return false; @@ -25,7 +16,13 @@ static int check_bytes(const ut8 *buf, ut64 length) { return ret; } -struct r_bin_plugin_t r_bin_plugin_pe64 = { +static bool check(RBinFile *arch) { + const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; + ut64 sz = arch ? r_buf_size (arch->buf): 0; + return check_bytes (bytes, sz); +} + +RBinPlugin r_bin_plugin_pe64 = { .name = "pe64", .desc = "PE64 (PE32+) bin plugin", .license = "LGPL3", @@ -48,7 +45,7 @@ struct r_bin_plugin_t r_bin_plugin_pe64 = { }; #ifndef CORELIB -struct r_lib_struct_t radare_plugin = { +RLibStruct radare_plugin = { .type = R_LIB_TYPE_BIN, .data = &r_bin_plugin_pe64, .version = R2_VERSION diff --git a/libr/bin/p/bin_pebble.c b/libr/bin/p/bin_pebble.c index 4439136ff5..f5853e7fb7 100644 --- a/libr/bin/p/bin_pebble.c +++ b/libr/bin/p/bin_pebble.c @@ -39,15 +39,11 @@ static Sdb* get_sdb (RBinObject *o) { return NULL; } -static int check_bytes(const ut8 *buf, ut64 length) { - if (length<8) - return 0; - if (!memcmp (buf, "PBLAPP\x00\x00", 8)) - return 1; - return 0; +static bool check_bytes(const ut8 *buf, ut64 length) { + return (length > 7 && !memcmp (buf, "PBLAPP\x00\x00", 8)); } -static int check(RBinFile *arch) { +static bool check(RBinFile *arch) { const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; ut64 sz = arch ? r_buf_size (arch->buf): 0; return check_bytes (bytes, sz); diff --git a/libr/bin/p/bin_psxexe.c b/libr/bin/p/bin_psxexe.c index f6b9eba14a..e8eac3753b 100644 --- a/libr/bin/p/bin_psxexe.c +++ b/libr/bin/p/bin_psxexe.c @@ -6,21 +6,19 @@ #include #include "psxexe/psxexe.h" -static int check_bytes(const ut8 *buf, ut64 length); - static void* load_bytes(RBinFile *arch, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb) { check_bytes (buf, sz); return R_NOTNULL; } -static int check_bytes(const ut8 *buf, ut64 length) { +static bool check_bytes(const ut8 *buf, ut64 length) { if (!buf || (length < PSXEXE_ID_LEN)) { return false; } return !memcmp (buf, PSXEXE_ID, PSXEXE_ID_LEN); } -static int check(RBinFile *arch) { +static bool check(RBinFile *arch) { if (!arch || !arch->buf) { return false; } diff --git a/libr/bin/p/bin_rar.c b/libr/bin/p/bin_rar.c index f99baabdd0..8730d3ba0d 100644 --- a/libr/bin/p/bin_rar.c +++ b/libr/bin/p/bin_rar.c @@ -14,22 +14,19 @@ typedef struct r_bin_obj_rar_t { Sdb *kv; } RRarBinObj; -static int check(RBinFile *arch); -static int check_bytes(const ut8 *buf, ut64 length); - -static int check(RBinFile *arch) { - const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; - ut64 sz = arch ? r_buf_size (arch->buf): 0; - return check_bytes (bytes, sz); -} - -static int check_bytes(const ut8 *buf, ut64 length) { +static bool check_bytes(const ut8 *buf, ut64 length) { if (buf && length > 16) if (!memcmp (buf, RARVMHDR, 16)) return true; return false; } +static bool check(RBinFile *arch) { + const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; + ut64 sz = arch ? r_buf_size (arch->buf): 0; + return check_bytes (bytes, sz); +} + static Sdb* get_sdb (RBinObject *o) { if (!o) return NULL; struct r_bin_obj_rar_t *bin = (struct r_bin_obj_rar_t *) o->bin_obj; diff --git a/libr/bin/p/bin_smd.c b/libr/bin/p/bin_smd.c index 9e6dd2aa0d..1de70aed8d 100644 --- a/libr/bin/p/bin_smd.c +++ b/libr/bin/p/bin_smd.c @@ -93,28 +93,24 @@ typedef struct gen_vect { -static int check(RBinFile *arch); -static int check_bytes(const ut8 *buf, ut64 length); - static void * load_bytes(RBinFile *arch, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb){ check_bytes (buf, sz); return R_NOTNULL; } -static int check(RBinFile *arch) { +static bool check_bytes(const ut8 *buf, ut64 length) { + if (length > 0x190 && !memcmp (buf+0x100, "SEGA", 4)) { + return true; + } + return false; +} + +static bool check(RBinFile *arch) { const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; ut64 sz = arch ? r_buf_size (arch->buf): 0; return check_bytes (bytes, sz); } -static int check_bytes(const ut8 *buf, ut64 length) { - if (length > 0x190) { - if (!memcmp (buf+0x100, "SEGA", 4)) - return true; - } - return false; -} - static RBinInfo* info(RBinFile *arch) { RBinInfo *ret = NULL; if (!(ret = R_NEW0 (RBinInfo))) diff --git a/libr/bin/p/bin_sms.c b/libr/bin/p/bin_sms.c index f3f3b0daad..732003492c 100644 --- a/libr/bin/p/bin_sms.c +++ b/libr/bin/p/bin_sms.c @@ -11,23 +11,14 @@ typedef struct gen_hdr { ut8 RegionRomSize; //Low 4 bits RomSize, Top 4 bits Region } SMS_Header; -static int check(RBinFile *arch); -static int check_bytes(const ut8 *buf, ut64 length); - static void * load_bytes(RBinFile *arch, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb){ check_bytes (buf, sz); return R_NOTNULL; } -static int check(RBinFile *arch) { - const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; - ut64 sz = arch ? r_buf_size (arch->buf): 0; - return check_bytes (bytes, sz); -} - #define CMP8(o,x) strncmp((const char*)bs+o,x,8) #define CMP4(o,x) strncmp((const char*)bs+o,x,4) -static int check_bytes(const ut8 *bs, ut64 length) { +static bool check_bytes(const ut8 *bs, ut64 length) { if (length > 0x2000 && !CMP8(0x1ff0, "TMR SEGA")) { return true; } @@ -46,6 +37,12 @@ static int check_bytes(const ut8 *bs, ut64 length) { return false; } +static bool check(RBinFile *arch) { + const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; + ut64 sz = arch ? r_buf_size (arch->buf): 0; + return check_bytes (bytes, sz); +} + static RBinInfo* info(RBinFile *arch) { const char *bs; SMS_Header *hdr = NULL; @@ -120,20 +117,20 @@ static RBinInfo* info(RBinFile *arch) { } -struct r_bin_plugin_t r_bin_plugin_sms = { +RBinPlugin r_bin_plugin_sms = { .name = "sms", .desc = "SEGA MasterSystem/GameGear", .license = "LGPL3", .load_bytes = &load_bytes, - .check = &check, - .check_bytes = &check_bytes, + .check = check, + .check_bytes = check_bytes, .info = &info, .minstrlen = 10, .strfilter = 'U' }; #ifndef CORELIB -struct r_lib_struct_t radare_plugin = { +RLibStruct radare_plugin = { .type = R_LIB_TYPE_BIN, .data = &r_bin_plugin_sms, .version = R2_VERSION diff --git a/libr/bin/p/bin_spc700.c b/libr/bin/p/bin_spc700.c index d443753be2..5611c5959c 100644 --- a/libr/bin/p/bin_spc700.c +++ b/libr/bin/p/bin_spc700.c @@ -4,27 +4,24 @@ #include #include "../format/spc700/spc_specs.h" -static int check(RBinFile *arch); -static int check_bytes(const ut8 *buf, ut64 length); - static void * load_bytes(RBinFile *arch, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb){ check_bytes (buf, sz); return R_NOTNULL; } -static int check(RBinFile *arch) { - const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; - ut64 sz = arch ? r_buf_size (arch->buf): 0; - return check_bytes (bytes, sz); -} - -static int check_bytes(const ut8 *buf, ut64 length) { +static bool check_bytes(const ut8 *buf, ut64 length) { if (!buf || length < 27) { return false; } return !memcmp (buf, SPC_MAGIC, 27); } +static bool check(RBinFile *arch) { + const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; + ut64 sz = arch ? r_buf_size (arch->buf): 0; + return check_bytes (bytes, sz); +} + static RBinInfo* info(RBinFile *arch) { RBinInfo *ret = NULL; spc_hdr spchdr; diff --git a/libr/bin/p/bin_te.c b/libr/bin/p/bin_te.c index cce662eca5..dada3874bc 100644 --- a/libr/bin/p/bin_te.c +++ b/libr/bin/p/bin_te.c @@ -7,9 +7,6 @@ #include "te/te_specs.h" #include "te/te.h" -static int check(RBinFile *arch); -static int check_bytes(const ut8 *buf, ut64 length); - static Sdb* get_sdb (RBinObject *o) { if (!o) return NULL; struct r_bin_te_obj_t *bin = (struct r_bin_te_obj_t *) o->bin_obj; @@ -147,18 +144,15 @@ static RBinInfo* info(RBinFile *arch) { return ret; } -static int check(RBinFile *arch) { +static bool check(RBinFile *arch) { const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; ut64 sz = arch ? r_buf_size (arch->buf): 0; return check_bytes (bytes, sz); } -static int check_bytes(const ut8 *buf, ut64 length) { - if (buf && length > 2) - if (!memcmp (buf, "\x56\x5a", 2)) - return true; - return false; +static bool check_bytes(const ut8 *buf, ut64 length) { + return (buf && length > 2 && !memcmp (buf, "\x56\x5a", 2)); } RBinPlugin r_bin_plugin_te = { @@ -180,7 +174,7 @@ RBinPlugin r_bin_plugin_te = { }; #ifndef CORELIB -struct r_lib_struct_t radare_plugin = { +RLibStruct radare_plugin = { .type = R_LIB_TYPE_BIN, .data = &r_bin_plugin_te, .version = R2_VERSION diff --git a/libr/bin/p/bin_vsf.c b/libr/bin/p/bin_vsf.c index 41d984feba..7cf310787a 100644 --- a/libr/bin/p/bin_vsf.c +++ b/libr/bin/p/bin_vsf.c @@ -24,9 +24,6 @@ static const struct { }; static const int MACHINES_MAX = sizeof(_machines) / sizeof(_machines[0]); -static int check(RBinFile *arch); -static int check_bytes(const ut8 *buf, ut64 length); - static Sdb* get_sdb (RBinObject *o) { if (!o || !o->bin_obj) { return NULL; @@ -38,6 +35,19 @@ static Sdb* get_sdb (RBinObject *o) { return NULL; } +static bool check_bytes(const ut8 *buf, ut64 length) { + if (!buf || length < VICE_MAGIC_LEN) { + return false; + } + return (!memcmp (buf, VICE_MAGIC, VICE_MAGIC_LEN)); +} + +static bool check(RBinFile *arch) { + const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; + ut64 sz = arch ? r_buf_size (arch->buf): 0; + return check_bytes (bytes, sz); +} + static void * load_bytes(RBinFile *arch, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb) { ut64 offset = 0; struct r_bin_vsf_obj* res = NULL; @@ -101,19 +111,6 @@ static void * load_bytes(RBinFile *arch, const ut8 *buf, ut64 sz, ut64 loadaddr, return res; } -static int check(RBinFile *arch) { - const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; - ut64 sz = arch ? r_buf_size (arch->buf): 0; - return check_bytes (bytes, sz); -} - -static int check_bytes(const ut8 *buf, ut64 length) { - if (!buf || length < VICE_MAGIC_LEN) { - return false; - } - return (!memcmp (buf, VICE_MAGIC, VICE_MAGIC_LEN)); -} - static RList *mem(RBinFile *arch) { // FIXME: What does Mem do? Should I remove it ? struct r_bin_vsf_obj* vsf_obj = (struct r_bin_vsf_obj*) arch->o->bin_obj; diff --git a/libr/bin/p/bin_wasm.c b/libr/bin/p/bin_wasm.c index 93ab49ad84..ce91f06748 100644 --- a/libr/bin/p/bin_wasm.c +++ b/libr/bin/p/bin_wasm.c @@ -7,13 +7,13 @@ #include #include -static int check_bytes(const ut8 *buf, ut64 length) { +static bool check_bytes(const ut8 *buf, ut64 length) { return (buf && length >= 4 && !memcmp (buf, "\x00" "asm", 4)); } static ut64 entrypoint = UT64_MAX; -static int check(RBinFile *arch) { +static bool check(RBinFile *arch) { const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; ut64 sz = arch ? r_buf_size (arch->buf): 0; return check_bytes (bytes, sz); diff --git a/libr/bin/p/bin_xbe.c b/libr/bin/p/bin_xbe.c index 42c71bd059..0d92fb6a78 100644 --- a/libr/bin/p/bin_xbe.c +++ b/libr/bin/p/bin_xbe.c @@ -15,18 +15,18 @@ static Sdb* get_sdb (RBinObject *o) { return NULL; } -static int check_bytes(const ut8 *buf, ut64 size) { +static bool check_bytes(const ut8 *buf, ut64 size) { xbe_header *header = (xbe_header *)buf; return (size > sizeof(xbe_header) && header->magic == XBE_MAGIC); } -static int check(RBinFile *arch) { +static bool check(RBinFile *arch) { const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; const ut64 size = arch ? r_buf_size (arch->buf) : 0; - if (!arch || !arch->o || !bytes) + if (!arch || !arch->o || !bytes) { return false; - + } return check_bytes(bytes, size); } @@ -337,7 +337,7 @@ static ut64 baddr(RBinFile *arch) { return obj->header->base; } -struct r_bin_plugin_t r_bin_plugin_xbe = { +RBinPlugin r_bin_plugin_xbe = { .name = "xbe", .desc = "Microsoft Xbox xbe format r_bin plugin", .license = "LGPL3", @@ -356,7 +356,7 @@ struct r_bin_plugin_t r_bin_plugin_xbe = { }; #ifndef CORELIB -struct r_lib_struct_t radare_plugin = { +RLibStruct radare_plugin = { .type = R_LIB_TYPE_BIN, .data = &r_bin_plugin_xbe, .version = R2_VERSION diff --git a/libr/bin/p/bin_xtr_dyldcache.c b/libr/bin/p/bin_xtr_dyldcache.c index 3681eb9bc6..215653ea92 100644 --- a/libr/bin/p/bin_xtr_dyldcache.c +++ b/libr/bin/p/bin_xtr_dyldcache.c @@ -10,11 +10,14 @@ static RBinXtrData * extract(RBin *bin, int idx); static RList * extractall(RBin *bin); static RBinXtrData * oneshot(RBin *bin, const ut8 *buf, ut64 size, int idx); -static int check_bytes(const ut8* bytes, ut64 sz); static RList * oneshotall(RBin *bin, const ut8 *buf, ut64 size); static int free_xtr (void *xtr_obj); -static int check(RBin *bin) { +static bool check_bytes(const ut8* buf, ut64 sz) { + return (buf && sz > 3 && !memcmp (buf, "\x64\x79\x6c\x64", 4)); +} + +static bool check(RBin *bin) { int size = 0, ret = false; ut8 *filebuf = (ut8*)r_file_slurp_range (bin->file, 0, 4, &size); ret = check_bytes (filebuf, size); @@ -22,10 +25,6 @@ static int check(RBin *bin) { return ret; } -static int check_bytes(const ut8* buf, ut64 sz) { - return (buf && sz > 3 && !memcmp (buf, "\x64\x79\x6c\x64", 4)); -} - // TODO: destroy must be void? static int destroy(RBin *bin) { return free_xtr (bin->cur->xtr_obj); diff --git a/libr/bin/p/bin_xtr_fatmach0.c b/libr/bin/p/bin_xtr_fatmach0.c index 3e9746c587..ac3b236c06 100644 --- a/libr/bin/p/bin_xtr_fatmach0.c +++ b/libr/bin/p/bin_xtr_fatmach0.c @@ -13,7 +13,7 @@ static RBinXtrData * oneshot(RBin *bin, const ut8 *buf, ut64 size, int idx); static RList * oneshotall(RBin *bin, const ut8 *buf, ut64 size ); static int free_xtr (void *xtr_obj) ; -static int checkHeader(const ut8 *h, int sz) { +static bool checkHeader(const ut8 *h, int sz) { ut8 buf[4]; if (sz >= 0x300 && !memcmp (h, "\xca\xfe\xba\xbe", 4)) { // XXX assuming BE @@ -31,7 +31,7 @@ static int checkHeader(const ut8 *h, int sz) { return false; } -static int check(RBin *bin) { +static bool check(RBin *bin) { int ret = false; RMmap *m = r_file_mmap (bin->file, false, 0); if (!m || !m->buf) { @@ -43,7 +43,7 @@ static int check(RBin *bin) { return ret; } -static int check_bytes(const ut8* bytes, ut64 sz) { +static bool check_bytes(const ut8* bytes, ut64 sz) { if (!bytes || sz < 0x300) { return false; } diff --git a/libr/bin/p/bin_zimg.c b/libr/bin/p/bin_zimg.c index b88b68d28d..8e81e94832 100644 --- a/libr/bin/p/bin_zimg.c +++ b/libr/bin/p/bin_zimg.c @@ -14,9 +14,6 @@ #define dprintf if (0)eprintf #endif -static int check(RBinFile *arch); -static int check_bytes(const ut8 *buf, ut64 length); - static Sdb* get_sdb (RBinObject *o) { if (!o) return NULL; struct r_bin_zimg_obj_t *bin = (struct r_bin_zimg_obj_t *) o->bin_obj; @@ -47,13 +44,13 @@ static ut64 baddr(RBinFile *arch) { return 0; } -static int check(RBinFile *arch) { +static bool check(RBinFile *arch) { const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; ut64 sz = arch ? r_buf_size (arch->buf): 0; return check_bytes (bytes, sz); } -static int check_bytes(const ut8 *buf, ut64 length) { +static bool check_bytes(const ut8 *buf, ut64 length) { if (buf && length >= 8) { // Checking ARM zImage kernel if (!memcmp (buf, "\x00\x00\xa0\xe1\x00\x00\xa0\xe1", 8)) { diff --git a/libr/include/r_bin.h b/libr/include/r_bin.h index 5ea857a3fe..38844227cf 100644 --- a/libr/include/r_bin.h +++ b/libr/include/r_bin.h @@ -285,9 +285,9 @@ typedef struct r_bin_xtr_plugin_t { char *license; int (*init)(void *user); int (*fini)(void *user); - int (*check)(RBin *bin); + bool (*check)(RBin *bin); // XXX: ut64 for size is maybe too much, what about st64? signed sizes are useful for detecting errors - int (*check_bytes)(const ut8 *bytes, ut64 sz); + bool (*check_bytes)(const ut8 *bytes, ut64 sz); RBinXtrData * (*extract_from_bytes)(RBin *bin, const ut8 *buf, ut64 size, int idx); RList * (*extractall_from_bytes)(RBin *bin, const ut8 *buf, ut64 size); RBinXtrData * (*extract)(RBin *bin, int idx); @@ -311,8 +311,8 @@ typedef struct r_bin_plugin_t { void *(*load_bytes)(RBinFile *arch, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb); ut64 (*size)(RBinFile *bin); // return ut64 maybe? meh int (*destroy)(RBinFile *arch); - int (*check)(RBinFile *arch); - int (*check_bytes)(const ut8 *buf, ut64 length); + bool (*check)(RBinFile *arch); + bool (*check_bytes)(const ut8 *buf, ut64 length); ut64 (*baddr)(RBinFile *arch); ut64 (*boffset)(RBinFile *arch); RBinAddr* (*binsym)(RBinFile *arch, int num);