btcs

bitcoin script parser/evaluator/compiler/decompiler
git clone git://jb55.com/btcs
Log | Files | Refs | README | LICENSE

commit 86883716a2d58f6c27dfa472efd095323c28d750
parent 3b84538fd0bcc3a54c797b600f3160b69ffee6f4
Author: William Casarin <jb55@jb55.com>
Date:   Thu, 13 Jun 2019 01:43:35 -0700

btcs: --abbreviate-data option

makes script histograms a bit nicer

Diffstat:
Mmain.c | 9++++++---
Mscript.c | 8+++++---
Mscript.h | 2+-
3 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/main.c b/main.c @@ -125,7 +125,7 @@ static void fail(int err, const char *msg) } -static int decompile(const char *str, int strlen) +static int decompile(const char *str, int strlen, bool abbrev_data) { static u8 buf[10000]; @@ -135,7 +135,7 @@ static int decompile(const char *str, int strlen) hex_decode(str, strlen, buf, sizeof(buf)); size_t nbytes = strlen / 2; - script_print(buf, nbytes); + script_print(buf, nbytes, abbrev_data); return 1; } @@ -166,6 +166,7 @@ int main(int argc, const char *argv[]) bool is_decompile = false; const char *input = NULL; size_t written; + bool abbrev_data = false; int compile_options = 0; int last_opt = 0; int hide_labels = -1; @@ -190,6 +191,8 @@ int main(int argc, const char *argv[]) hide_labels = 1; else if (streq(argv[i], "+l") || streq(argv[i], "--show-labels")) hide_labels = 0; + else if (streq(argv[i], "--abbreviate-data")) + abbrev_data = true; else { last_opt = i-1; input = argv[i]; @@ -206,7 +209,7 @@ int main(int argc, const char *argv[]) if (input == NULL) { while ((len = getline(&line, &n, stdin)) != -1) { - ok = decompile(line, len-1); + ok = decompile(line, len-1, abbrev_data); if (!ok) fail(5, "failed to decompile"); diff --git a/script.c b/script.c @@ -750,7 +750,7 @@ static bool is_push_data(enum opcode op) op == OP_PUSHDATA4; } -void script_print(const u8 *script, size_t script_size) { +void script_print(const u8 *script, size_t script_size, int abbrev_data) { u32 len; static u8 tmpbuf[4096]; const u8 *p = script; @@ -761,8 +761,10 @@ void script_print(const u8 *script, size_t script_size) { script_getop(&p, top, &opcode, tmpbuf, sizeof(tmpbuf), &len); if (is_push_data(opcode)) { - hex_print(tmpbuf, len); - printf(" "); + if (abbrev_data) + printf("data(%d) ", len); + else + hex_print(tmpbuf, len); } else printf("%s ", op_name(opcode)); diff --git a/script.h b/script.h @@ -24,7 +24,7 @@ void script_push_datastr(struct stack *, const char *str, int israw); void script_serialize(struct stack *stack, u8 *buf, int buflen, int* len); void stack_serialize(struct stack *stack, u8 *buf, int buflen, int* len); -void script_print(const u8 *script, size_t script_size); +void script_print(const u8 *script, size_t script_size, int abbrev_data); int script_getop(const u8 **p, const u8 *end, enum opcode *popcode, u8 *buf, int bufsize, u32 *outlen); extern int g_silence_script_err;