btcs

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

commit df415b7bd38a7ec65632b0e4a27e26b1264e0d6a
parent fc6a77941c7ea088c77843ceb66daa8a4f0e93b0
Author: William Casarin <jb55@jb55.com>
Date:   Mon,  3 Jun 2019 14:19:15 -0700

fix more stack issues

Diffstat:
Mscript.c | 46++++++++++++++++++++++++++++++++++++++++++----
Mtest.c | 4++--
Mval.c | 35+----------------------------------
3 files changed, 45 insertions(+), 40 deletions(-)

diff --git a/script.c b/script.c @@ -802,6 +802,41 @@ script_push_raw(struct stack *stack, const char *data) { script_push_datastr(stack, data, 1); } +void script_serialize_data(struct val val, u32 *len, u8 *buf, int bufsize) { + u8 *p; + p = byte_pool_get(val.ind, len); + if (*len < OP_PUSHDATA1) { + *buf++ = *len; + memcpy(buf, p, *len); + *len += 1; + } + else if (*len <= 0xFF) { + *buf++ = OP_PUSHDATA1; + *buf++ = *len; + memcpy(buf, p, *len); + *len += 2; + } + else if (*len <= 0xFFFF) { + *buf++ = OP_PUSHDATA2; + u16 *sp = (u16*)buf; + // TODO: writele16 + *sp = *len; + buf += 2; + memcpy(buf, p, *len); + *len += 3; + } + else { + *buf++ = OP_PUSHDATA4; + u32 *ip = (u32*)buf; + // TODO: writele32 + *ip = *len; + buf += 4; + memcpy(buf, p, *len); + *len += 5; + } + return; +} + void script_serialize(struct stack *stack, u8 *buf, int buflen, int* len) { struct val *valp; @@ -816,7 +851,8 @@ void script_serialize(struct stack *stack, u8 *buf, int buflen, int* len) /* printf("%d %d\n", val.type, val.ind); */ valp = (struct val*)sp; - if (valp->type == VT_SCRIPTNUM) { + switch (valp->type) { + case VT_SCRIPTNUM: { struct num *sn = num_pool_get(valp->ind); if (sn->val == -1) { @@ -833,16 +869,18 @@ void script_serialize(struct stack *stack, u8 *buf, int buflen, int* len) } else { val_serialize(*valp, &valsize, p+1, buflen-((p+1)-buf)); - assert(valsize <= 0xFF); *p = (u8)valsize; valsize++; } + break; } - else { + case VT_DATA: + script_serialize_data(*valp, &valsize, p, buflen-(p-buf)); + break; + default: val_serialize(*valp, &valsize, p, buflen-(p-buf)); } - p += valsize; *len += valsize; assert(p-buf <= buflen); diff --git a/test.c b/test.c @@ -129,8 +129,8 @@ TEST(big_int_serializes_ok) { cmp_data(buf, expected_in, len, ARRAY_SIZE(expected_in), "big int input serializes ok"); - script_eval(buf, ARRAY_SIZE(expected_in), stack, result); - stack_serialize(stack, buf, ARRAY_SIZE(buf), &len); + script_eval(buf, sizeof(expected_in), stack, result); + stack_serialize(stack, buf, sizeof(buf), &len); cmp_data(buf, expected_out, len, ARRAY_SIZE(expected_out), "big int output serializes ok"); diff --git a/val.c b/val.c @@ -52,46 +52,13 @@ void val_serialize(struct val val, u32 *len, u8 *buf, int bufsize) { *len = 1; *buf = val.ind & 0xFF; return; + case VT_DATA: case VT_RAW: { u8 *p; p = byte_pool_get(val.ind, len); memcpy(buf, p, *len); return; } - case VT_DATA: { - u8 *p; - p = byte_pool_get(val.ind, len); - if (*len < OP_PUSHDATA1) { - *buf++ = *len; - memcpy(buf, p, *len); - *len += 1; - } - else if (*len <= 0xFF) { - *buf++ = OP_PUSHDATA1; - *buf++ = *len; - memcpy(buf, p, *len); - *len += 2; - } - else if (*len <= 0xFFFF) { - *buf++ = OP_PUSHDATA2; - u16 *sp = (u16*)buf; - // TODO: writele16 - *sp = *len; - buf += 2; - memcpy(buf, p, *len); - *len += 3; - } - else { - *buf++ = OP_PUSHDATA4; - u32 *ip = (u32*)buf; - // TODO: writele32 - *ip = *len; - buf += 4; - memcpy(buf, p, *len); - *len += 5; - } - return; - } case VT_SMALLINT: assert(0); *len = 1;