btcs

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

commit 8c5edcb07f56102922ce11898d55e82080495ad0
parent f356e6217ece3187666cc957b3b7ee2a94ad27c7
Author: William Casarin <jb55@jb55.com>
Date:   Sun, 12 Nov 2017 21:54:22 -0800

tests pass!

for now

Diffstat:
Mscript.c | 1-
Mscript_num.c | 27++++++++++++++++++---------
Mval.c | 15++++++++++++---
Mvalstack.h | 1+
4 files changed, 31 insertions(+), 13 deletions(-)

diff --git a/script.c b/script.c @@ -557,7 +557,6 @@ script_eval(u8 *script, size_t script_size, struct stack *stack) { switch (opcode) { case OP_ADD: - printf("%d + %d\n", bn1->val, bn2->val); bn.val = bn1->val + bn2->val; break; diff --git a/script_num.c b/script_num.c @@ -153,16 +153,25 @@ sn_to_val(struct num *sn) { struct num *snref; int ind; - //TODO: implement return overflowed scriptnums - assert(!sn_overflowed(sn)); - - val.type = VT_SCRIPTNUM; - if (sn->ind > -1) - val.ind = sn->ind; - else { - snref = num_pool_new(&ind); - snref->val = sn->val; + if (sn_overflowed(sn)) { + u16 ind, len; + static u8 tmp[8]; + sn_serialize(sn, tmp, 8, &len); + u8 *data = byte_pool_new(len, &ind); + memcpy(data, tmp, len); + val.type = VT_DATA; val.ind = ind; } + else + { + val.type = VT_SCRIPTNUM; + if (sn->ind > -1) + val.ind = sn->ind; + else { + snref = num_pool_new(&ind); + snref->val = sn->val; + val.ind = ind; + } + } return val; } diff --git a/val.c b/val.c @@ -45,10 +45,19 @@ val_serialize(struct val val, u16 *len, u8 *buf, int bufsize) { *len = 1; *buf = val.ind & 0xFF; return; - case VT_DATA: - assert("!implement val_serialize VT_DATA"); - byte_pool_get(val.ind, len); + case VT_DATA: { + u8 *p; + p = byte_pool_get(val.ind, len); + if (*len <= 0xFF) { + *buf++ = *len & 0xFF; + memcpy(buf, p, *len); + *len = *len + 1; + } + else { + assert(!"serialize bigger pushdatas"); + } return; + } case VT_SMALLINT: *len = 1; n = val.ind; diff --git a/valstack.h b/valstack.h @@ -4,6 +4,7 @@ #include "op.h" #include "val.h" +#include "alloc.h" static inline struct val stack_top_val(struct stack *stack, int ind) {