Skip to content

Commit 2154e92

Browse files
authored
Remove global variable from the ninds plugin ##bin
1 parent a27bdc3 commit 2154e92

File tree

1 file changed

+41
-29
lines changed

1 file changed

+41
-29
lines changed

libr/bin/p/bin_ninds.c

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
/* radare - LGPL - 2015-2023 - a0rtega */
1+
/* radare - LGPL - 2015-2025 - a0rtega */
22

3-
#include <r_lib.h>
43
#include <r_bin.h>
5-
64
#include "../format/nin/nds.h"
75

8-
static R_TH_LOCAL struct nds_hdr loaded_header = {0};
9-
106
static bool check(RBinFile *bf, RBuffer *b) {
117
ut8 ninlogohead[6];
128
if (r_buf_read_at (b, 0xc0, ninlogohead, sizeof (ninlogohead)) == 6) {
@@ -23,34 +19,47 @@ static bool check(RBinFile *bf, RBuffer *b) {
2319
}
2420

2521
static bool load(RBinFile *bf, RBuffer *b, ut64 loadaddr) {
26-
r_buf_read_at (b, 0, (ut8*)&loaded_header, sizeof (loaded_header));
27-
bf->bo->bin_obj = &loaded_header;
28-
return bf->bo->bin_obj != NULL;
22+
struct nds_hdr *lh = R_NEW0 (struct nds_hdr);
23+
if (r_buf_read_at (b, 0, (ut8 *)lh, sizeof (*lh)) == sizeof (*lh)) {
24+
bf->bo->bin_obj = lh;
25+
return true;
26+
}
27+
free (lh);
28+
bf->bo->bin_obj = NULL;
29+
return false;
2930
}
3031

3132
static ut64 baddr(RBinFile *bf) {
32-
return (ut64) loaded_header.arm9_ram_address;
33+
struct nds_hdr *lh = (void *)bf->bo->bin_obj;
34+
if (!lh) {
35+
return 0;
36+
}
37+
return (ut64)lh->arm9_ram_address;
3338
}
3439

3540
static RList *sections(RBinFile *bf) {
3641
RList *ret = r_list_new ();
42+
struct nds_hdr *lh = (void *)bf->bo->bin_obj;
43+
if (!lh) {
44+
return ret;
45+
}
3746
RBinSection *ptr9 = R_NEW0 (RBinSection);
38-
RBinSection *ptr7 = R_NEW0 (RBinSection);
3947

4048
ptr9->name = strdup ("arm9");
41-
ptr9->size = loaded_header.arm9_size;
42-
ptr9->vsize = loaded_header.arm9_size;
43-
ptr9->paddr = loaded_header.arm9_rom_offset;
44-
ptr9->vaddr = loaded_header.arm9_ram_address;
49+
ptr9->size = lh->arm9_size;
50+
ptr9->vsize = lh->arm9_size;
51+
ptr9->paddr = lh->arm9_rom_offset;
52+
ptr9->vaddr = lh->arm9_ram_address;
4553
ptr9->perm = r_str_rwx ("rwx");
4654
ptr9->add = true;
4755
r_list_append (ret, ptr9);
4856

57+
RBinSection *ptr7 = R_NEW0 (RBinSection);
4958
ptr7->name = strdup ("arm7");
50-
ptr7->size = loaded_header.arm7_size;
51-
ptr7->vsize = loaded_header.arm7_size;
52-
ptr7->paddr = loaded_header.arm7_rom_offset;
53-
ptr7->vaddr = loaded_header.arm7_ram_address;
59+
ptr7->size = lh->arm7_size;
60+
ptr7->vsize = lh->arm7_size;
61+
ptr7->paddr = lh->arm7_rom_offset;
62+
ptr7->vaddr = lh->arm7_ram_address;
5463
ptr7->perm = r_str_rwx ("rwx");
5564
ptr7->add = true;
5665
r_list_append (ret, ptr7);
@@ -60,19 +69,20 @@ static RList *sections(RBinFile *bf) {
6069

6170
static RList *entries(RBinFile *bf) {
6271
RList *ret = r_list_new ();
63-
if (bf && bf->buf) {
72+
struct nds_hdr *lh = (void *)bf->bo->bin_obj;
73+
if (bf && bf->buf && lh) {
6474
ret->free = free;
65-
RBinAddr *ptr9 = R_NEW0 (RBinAddr);
66-
RBinAddr *ptr7 = R_NEW0 (RBinAddr);
6775

6876
/* ARM9 entry point */
69-
ptr9->vaddr = loaded_header.arm9_entry_address;
70-
// ptr9->paddr = loaded_header.arm9_entry_address;
77+
RBinAddr *ptr9 = R_NEW0 (RBinAddr);
78+
ptr9->vaddr = lh->arm9_entry_address;
79+
// ptr9->paddr = lh.arm9_entry_address;
7180
r_list_append (ret, ptr9);
7281

7382
/* ARM7 entry point */
74-
ptr7->vaddr = loaded_header.arm7_entry_address;
75-
// ptr7->paddr = loaded_header.arm7_entry_address;
83+
RBinAddr *ptr7 = R_NEW0 (RBinAddr);
84+
ptr7->vaddr = lh->arm7_entry_address;
85+
// ptr7->paddr = lh.arm7_entry_address;
7686
r_list_append (ret, ptr7);
7787
}
7888
return ret;
@@ -81,8 +91,10 @@ static RList *entries(RBinFile *bf) {
8191
static RBinInfo *info(RBinFile *bf) {
8292
R_RETURN_VAL_IF_FAIL (bf && bf->buf, NULL);
8393
RBinInfo *ret = R_NEW0 (RBinInfo);
84-
char *filepath = r_str_newf ("%.12s - %.4s",
85-
loaded_header.title, loaded_header.gamecode);
94+
struct nds_hdr *lh = (void *)bf->bo->bin_obj;
95+
char *filepath = lh
96+
? r_str_newf ("%.12s - %.4s", (const char *)lh->title, (const char *)lh->gamecode)
97+
: strdup (" - ");
8698
ret->file = filepath;
8799
ret->type = strdup ("ROM");
88100
ret->machine = strdup ("Nintendo DS");
@@ -96,8 +108,8 @@ static RBinInfo *info(RBinFile *bf) {
96108
RBinPlugin r_bin_plugin_ninds = {
97109
.meta = {
98110
.name = "ninds",
99-
.author = "pancake",
100-
.desc = "Nintendo DS ROMs",
111+
.author = "a0rtega",
112+
.desc = "Nintendo DS ROM loader",
101113
.license = "LGPL-3.0-only",
102114
},
103115
.load = &load,

0 commit comments

Comments
 (0)