Skip to content

Commit

Permalink
Fix izzzj json output ##json (radareorg#15739)
Browse files Browse the repository at this point in the history
  • Loading branch information
kazarmy authored and radare committed Jan 4, 2020
1 parent a5b41b9 commit 961dfec
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
10 changes: 7 additions & 3 deletions libr/bin/bfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static ut64 binobj_a2b(RBinObject *o, ut64 addr) {
return o ? addr + o->baddr_shift : addr;
}

static void print_string(RBinFile *bf, RBinString *string, int raw) {
static void print_string(RBinFile *bf, RBinString *string, int raw, bool first) {
r_return_if_fail (bf && string);

int mode = bf->strmode;
Expand Down Expand Up @@ -62,7 +62,7 @@ static void print_string(RBinFile *bf, RBinString *string, int raw) {
pj_o (pj);
pj_ks (pj, "string", string->string);
pj_end (pj);
io->cb_printf ("%s\n", pj_string (pj));
io->cb_printf ("%s%s\n", first ? "" : ",", pj_string (pj));
pj_free (pj);
}
}
Expand Down Expand Up @@ -138,6 +138,7 @@ static int string_scan_range(RList *list, RBinFile *bf, int min,
st64 vdelta = 0, pdelta = 0;
RBinSection *s = NULL;
bool ascii_only = false;
bool first = true;
r_buf_read_at (bf->buf, from, buf, len);
// may oobread
while (needle < to) {
Expand Down Expand Up @@ -329,8 +330,11 @@ static int string_scan_range(RList *list, RBinFile *bf, int min,
ht_up_insert (bf->o->strings_db, bs->vaddr, bs);
}
} else {
print_string (bf, bs, raw);
print_string (bf, bs, raw, first);
r_bin_string_free (bs);
if (first) {
first = false;
}
}
if (from == 0 && to == bf->size) {
/* force lookup section at the next one */
Expand Down
7 changes: 4 additions & 3 deletions libr/core/cmd_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ static int cmd_info(void *data, const char *input) {
int mode = 0; //R_MODE_SIMPLE;
bool rdump = false;
int is_array = 0;
bool is_zz = false;
Sdb *db;

for (i = 0; input[i] && input[i] != ' '; i++)
Expand All @@ -462,15 +463,15 @@ static int cmd_info(void *data, const char *input) {
if (mode == R_MODE_JSON) {
int suffix_shift = 0;
if (!strncmp (input, "SS", 2) || !strncmp (input, "ee", 2)
|| !strncmp (input, "zz", 2)) {
|| (is_zz = !strncmp (input, "zz", 2))) {
suffix_shift = 1;
}
if (strlen (input + 1 + suffix_shift) > 1) {
is_array = 1;
}
}
if (is_array) {
r_cons_printf ("{");
r_cons_printf (is_zz ? "[" : "{");
}
if (!*input) {
cmd_info_bin (core, va, mode);
Expand Down Expand Up @@ -1234,7 +1235,7 @@ static int cmd_info(void *data, const char *input) {
}
done:
if (is_array) {
r_cons_printf ("}\n");
r_cons_printf ("%s\n", is_zz ? "]" : "}");
}
if (newline) {
r_cons_newline ();
Expand Down
21 changes: 21 additions & 0 deletions test/new/db/cmd/cmd_i
Original file line number Diff line number Diff line change
Expand Up @@ -3464,6 +3464,27 @@ izzj
EXPECT='[{"vaddr":4195066,"paddr":762,"ordinal":4,"size":5,"length":4,"section":".dynstr","type":"ascii","string":"puts"},{"vaddr":347,"paddr":6483,"ordinal":65,"size":18,"length":17,"section":".strtab","type":"ascii","string":"puts@@GLIBC_2.2.5"}]'
RUN

NAME=izzzj
FILE=-
CMDS=<<EOF
w abcd\00012345\0efghi\0
izzzj~{}
EOF
EXPECT=<<EOF
[
{
"string": "abcd"
},
{
"string": "12345"
},
{
"string": "efghi"
}
]
EOF
RUN

NAME=i (no rbin - file x86_64)
FILE=../bins/elf/analysis/hello-linux-x86_64
EXPECT=<<EOF
Expand Down

0 comments on commit 961dfec

Please sign in to comment.