btcs

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

commit a18b8ff6296e12a113153bb88879136178880be5
parent 72a6288a53037721c22af1255eca2e95cb0e28b2
Author: William Casarin <jb55@jb55.com>
Date:   Sun, 12 Nov 2017 23:42:45 -0800

small tweaks

Diffstat:
Mlexer.l | 2+-
Mmisc.h | 6+++---
Mop.c | 9+++++----
Mscript.c | 2+-
Mscript_num.c | 10++++++++--
Mtest.c | 23++++++++++++++++++++---
Mval.c | 1+
7 files changed, 39 insertions(+), 14 deletions(-)

diff --git a/lexer.l b/lexer.l @@ -23,7 +23,7 @@ [oO][pP]_ {} --?[0-9] { +-?[0-9]+ { s64 i = strtoll(yytext, NULL, 10); yylval.val = val_from_int(i); return T_VAL; diff --git a/misc.h b/misc.h @@ -47,12 +47,12 @@ readle32(const u8* ptr) { } void static inline -print_bytes(u8 *bytes, size_t size) { +print_bytes(u8 *bytes, size_t size, int with_space) { size_t i; for (i = 0; i < size; i++) { - printf("%02x ", bytes[i]); + printf("%02x", bytes[i]); + if (with_space) printf(" "); } - printf("\n"); } diff --git a/op.c b/op.c @@ -5,6 +5,7 @@ #include "alloc.h" #include "misc.h" +#include <inttypes.h> #include <stdio.h> #include <string.h> #include <ctype.h> @@ -360,19 +361,19 @@ val_print(struct val val) { assert(val.ind != -1); n = num_pool_get(val.ind); assert(n); - printf("%lu", n->val); + printf("%" PRId64, n->val); break; case VT_OP: printf("OP_%s", op_name(val.ind)); break; case VT_SMALLINT: - printf("si:%d", val.ind); + printf("%d", val.ind); break; case VT_DATA: { u16 len; u8 *data = byte_pool_get(val.ind, &len); - printf("data (%d): ", len); - print_bytes(data, len); + printf("0x", len); + print_bytes(data, len, 0); break; } default: diff --git a/script.c b/script.c @@ -506,7 +506,7 @@ script_eval(u8 *script, size_t script_size, struct stack *stack) { if (res != SN_SUCCESS) { sprintf(tmpbuf, "invalid scriptnum %d", res); - SCRIPTERR(tmpbuf); + return SCRIPTERR(tmpbuf); } switch (opcode) diff --git a/script_num.c b/script_num.c @@ -3,6 +3,7 @@ #include "alloc.h" #include "val.h" #include <limits.h> +#include <inttypes.h> /** * Numeric opcodes (OP_1ADD, etc) are restricted to operating on 4-byte @@ -119,6 +120,7 @@ enum sn_result sn_from_val(struct val val, struct num ** sn, int require_minimal) { switch (val.type) { + case VT_SMALLINT: case VT_SCRIPTNUM: *sn = num_pool_get(val.ind); assert((*sn)->ind == val.ind); @@ -131,7 +133,10 @@ sn_from_val(struct val val, struct num ** sn, int require_minimal) { return SN_ERR_OVERFLOWED_INT; } - break; + if (val.type == VT_SMALLINT) + (*sn)->val = val.ind; + + return SN_SUCCESS; case VT_DATA: { u8 *data; u16 size; @@ -142,7 +147,8 @@ sn_from_val(struct val val, struct num ** sn, int require_minimal) { } } - assert("!sn_from_val: unhandled"); + printf("type: %d\n", val.type); + assert(!"sn_from_val: unhandled"); return SN_SUCCESS; } diff --git a/test.c b/test.c @@ -29,10 +29,10 @@ cmp_data(const u8 *a, const u8 *b, int alen, int blen, const char *msg) { printf("%02x ", b[i] & 0xff); printf("\n#\n"); - fail("%s: data should match", msg); + fail("%s", msg); } else - pass("%s: data should match", msg); + pass("%s", msg); } /* static void */ /* test_nip(struct stack *stack, struct stack *expected) { */ @@ -129,6 +129,22 @@ TEST(big_int_serializes_ok) { "big int output serializes ok"); } +TEST(test_small_int) { + int len; + static u8 buf[6]; + static u8 expected_in[] = { 0x01, 0x7f }; + + script_push_int(script, 127); + script_serialize(script, buf, ARRAY_SIZE(buf), &len); + cmp_data(buf, expected_in, len, ARRAY_SIZE(expected_in), + "small integer input serializes ok"); + + script_eval(buf, ARRAY_SIZE(expected_in), stack); + script_serialize(stack, buf, ARRAY_SIZE(buf), &len); + cmp_data(buf, expected_in, len, ARRAY_SIZE(expected_in), + "small integer output serializes ok"); +} + // TODO test scriptnum overflows // TODO test scriptnum negative zero boolean logic // TODO test scriptnum add into overflow + hash @@ -168,7 +184,7 @@ main(int argc, char *argv[]) { alloc_arenas(); - plan(8); + plan(10); stack_init(script); stack_init(stack); @@ -180,6 +196,7 @@ main(int argc, char *argv[]) { RUNTEST(negative_integer); RUNTEST(add_negative_two); RUNTEST(big_int_serializes_ok); + RUNTEST(test_small_int); stack_free(script); stack_free(expected); diff --git a/val.c b/val.c @@ -80,6 +80,7 @@ val_serialize(struct val val, u16 *len, u8 *buf, int bufsize) { else { assert(!"non-small VT_SMALLINT"); } + return; } assert(!"val_serialize missing implementation");