Skip to content

Commit

Permalink
Fix #19729 - Make pswj consistent with psw output ##print
Browse files Browse the repository at this point in the history
  • Loading branch information
radare committed Feb 21, 2022
1 parent 6691d80 commit 4fa2303
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
24 changes: 22 additions & 2 deletions libr/core/cmd_print.c
Expand Up @@ -4701,25 +4701,45 @@ static void print_json_string(RCore *core, const char* block, int len, const cha
default: type = "unknown"; break;
}
}
bool is_wide = !strcmp (type, "wide");
size_t slen = r_str_nlen (block, len);
char *tblock = (char *)block;
if (is_wide) {
int i;
// dewide
tblock = r_mem_dup (block, len);
for (i = 0; i < len; i++) {
if (tblock[i] && !tblock[i + 1]) {
memmove (tblock + i + 1, tblock + i + 2, len - i - 1);
} else {
tblock[i] = 0;
break;
}
}
slen = strlen (tblock);
}
PJ *pj = r_core_pj_new (core);
if (!pj) {
return;
}
pj_o (pj);
pj_k (pj, "string");
// TODO: add pj_kd for data to pass key(string) and value(data,len) instead of pj_ks which null terminates
char *str = r_str_utf16_encode (block, len); // XXX just block + len should be fine, pj takes care of this
char *str = r_str_utf16_encode (tblock, slen); // XXX just block + len should be fine, pj takes care of this
pj_raw (pj, "\"");
pj_raw (pj, str);
free (str);
pj_raw (pj, "\"");
pj_kn (pj, "offset", core->offset);
pj_ks (pj, "section", section_name);
pj_ki (pj, "length", len);
pj_ki (pj, "length", slen);
pj_ks (pj, "type", type);
pj_end (pj);
r_cons_println (pj_string (pj));
pj_free (pj);
if (tblock != block) {
free (tblock);
}
}

static char *__op_refs(RCore *core, RAnalOp *op, int n) {
Expand Down
13 changes: 13 additions & 0 deletions test/db/cmd/cmd_ps
Expand Up @@ -102,3 +102,16 @@ EXPECT_ERR=<<EOF
Error: bitness of 16 not supported
EOF
RUN

NAME=pswj
FILE=-
CMDS=<<EOF
wx 680065006c006c006f000000
psw
pswj
EOF
EXPECT=<<EOF
hello
{"string":"hello","offset":0,"section":"unknown","length":5,"type":"wide"}
EOF
RUN
2 changes: 1 addition & 1 deletion test/db/cmd/cmd_psj
Expand Up @@ -27,7 +27,7 @@ s 0x08049600
psj 4
EOF
EXPECT=<<EOF
{"string":"\u000b\u0000\u0000\u0000","offset":134518272,"section":".dynamic","length":4,"type":"wide"}
{"string":"\u000b","offset":134518272,"section":".dynamic","length":1,"type":"wide"}
EOF
RUN

Expand Down

0 comments on commit 4fa2303

Please sign in to comment.