btcs

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

commit 9149dbdc4e837b7a28a5cece20ca79efbbb44735
parent c831574fcf8541edaacc451bf482a9a5bad36ddf
Author: William Casarin <jb55@jb55.com>
Date:   Mon,  3 Jun 2019 19:26:44 -0700

cleanups

Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
MMakefile | 2+-
Mlexer.l | 1+
Mmain.c | 5+++--
Mmisc.h | 6+++---
Mparser.y | 5++++-
Mscript.c | 23++++++++++++-----------
Mscript_num.c | 3+--
Mscript_num.h | 11++++-------
Mval.c | 4++--
Mval.h | 2+-
10 files changed, 32 insertions(+), 30 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,5 +1,5 @@ -CFLAGS=-g -DHAVE_LITTLE_ENDIAN -O0 -Ideps -std=c99 -Wall -Wno-unused-variable -Wno-unused-function -Wunreachable-code +CFLAGS=-g -DHAVE_LITTLE_ENDIAN -O2 -Ideps -std=c99 -Wall -Wextra -Werror -Wno-unused-variable -Wno-unused-function -Wunreachable-code DEPS=script.c \ oplookup.c \ diff --git a/lexer.l b/lexer.l @@ -5,6 +5,7 @@ #include "script.h" #include "op.h" +int fileno(FILE *stream); #define YY_DECL int yylex() #include "parser.tab.h" diff --git a/main.c b/main.c @@ -12,12 +12,13 @@ char * g_reader_buf; char * g_reader_buf_top; struct stack g_reader_stack; u32 g_reader_buf_cap; +void yy_scan_string(const char *); + void yyerror(const char* s); -void -parse_error(char* err) { +void parse_error(char* err) { fprintf(stderr, "[btcs] parse error: %s\n", err); exit(1); } diff --git a/misc.h b/misc.h @@ -32,21 +32,21 @@ typedef uint64_t u64; #define le16toh(x) (x) #define le32toh(x) (x) -u16 static inline +inline static u16 readle16(const u8* ptr) { u16 x; memcpy((char*)&x, ptr, 2); return le16toh(x); } -u32 static inline +inline static u32 readle32(const u8* ptr) { u32 x; memcpy((char*)&x, ptr, 4); return le32toh(x); } -void static inline +inline static void print_bytes(u8 *bytes, size_t size, int with_space) { size_t i; for (i = 0; i < size; i++) { diff --git a/parser.y b/parser.y @@ -8,7 +8,10 @@ extern int yylex(); extern struct stack g_reader_stack; -void yyerror(const char* s); +void yyerror(const char *s); + +void script_handle_input(struct stack *stack, const char *str); +void parse_error(const char *err); %} diff --git a/script.c b/script.c @@ -5,6 +5,7 @@ #include "stack.h" #include "alloc.h" #include "valstack.h" +#include "compiler.h" #include "sha256.h" #include "ripemd160.h" #include <stdio.h> @@ -428,7 +429,7 @@ script_eval(const u8 *script, size_t script_size, struct stack *stack, struct num *n; enum sn_result res = - sn_from_val(stack_top_val(stack, -1), &n, require_minimal); + sn_from_val(stack_top_val(stack, -1), &n); if (res != SN_SUCCESS) { sprintf(tmpbuf, "invalid scriptnum %d", res); @@ -503,7 +504,7 @@ script_eval(const u8 *script, size_t script_size, struct stack *stack, SCRIPTERR("INVALID_STACK_OPERATION"); struct val v1 = stack_top_val(stack, -2); struct val v2 = stack_top_val(stack, -1); - int equal = val_eq(v1, v2, require_minimal); + int equal = val_eq(v1, v2); // OP_NOTEQUAL is disabled because it would be too easy to say // something like n != 1 and have some wiseguy pass in 1 with extra // zero bytes after it (numerically, 0x01 == 0x0001 == 0x000001) @@ -538,7 +539,7 @@ script_eval(const u8 *script, size_t script_size, struct stack *stack, SCRIPTERR("INVALID_STACK_OPERATION"); struct num *bn; enum sn_result res = - sn_from_val(stack_top_val(stack, -1), &bn, require_minimal); + sn_from_val(stack_top_val(stack, -1), &bn); if (res != SN_SUCCESS) { sprintf(tmpbuf, "invalid scriptnum %d", res); @@ -583,10 +584,10 @@ script_eval(const u8 *script, size_t script_size, struct stack *stack, struct num *bn1, *bn2, bn; enum sn_result res; bn.ind = -1; - res = sn_from_val(stack_top_val(stack, -2), &bn1, require_minimal); + res = sn_from_val(stack_top_val(stack, -2), &bn1); if (res == SN_ERR_OVERFLOWED_INT) SCRIPTERR("SCRIPT_INT_OVERFLOW"); - res = sn_from_val(stack_top_val(stack, -1), &bn2, require_minimal); + res = sn_from_val(stack_top_val(stack, -1), &bn2); if (res == SN_ERR_OVERFLOWED_INT) SCRIPTERR("SCRIPT_INT_OVERFLOW"); /* struct num bn(0); */ @@ -640,9 +641,9 @@ script_eval(const u8 *script, size_t script_size, struct stack *stack, if (stack_size(stack) < 3) SCRIPTERR("INVALID_STACK_OPERATION"); struct num *bn1, *bn2, *bn3; - sn_from_val(stack_top_val(stack, -3), &bn1, require_minimal); - sn_from_val(stack_top_val(stack, -2), &bn2, require_minimal); - sn_from_val(stack_top_val(stack, -1), &bn3, require_minimal); + sn_from_val(stack_top_val(stack, -3), &bn1); + sn_from_val(stack_top_val(stack, -2), &bn2); + sn_from_val(stack_top_val(stack, -1), &bn3); int fval = bn2->val <= bn1->val && bn1->val < bn3->val; stack_pop(stack); stack_pop(stack); @@ -802,7 +803,7 @@ 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) { +void script_serialize_data(struct val val, u32 *len, u8 *buf) { u8 *p; p = byte_pool_get(val.ind, len); if (*len < OP_PUSHDATA1) { @@ -875,7 +876,7 @@ void script_serialize(struct stack *stack, u8 *buf, int buflen, int* len) break; } case VT_DATA: - script_serialize_data(*valp, &valsize, p, buflen-(p-buf)); + script_serialize_data(*valp, &valsize, p); break; default: val_serialize(*valp, &valsize, p, buflen-(p-buf)); @@ -912,5 +913,5 @@ void stack_serialize(struct stack *stack, u8 *buf, int buflen, int *len) { } void -script_handle_input(struct stack *stack, const char *str) { +script_handle_input(struct stack *stack UNUSED, const char *str UNUSED) { } diff --git a/script_num.c b/script_num.c @@ -114,8 +114,7 @@ sn_from_data(u8 *data, u16 size, struct num **num) { // Return a script num only if it's still a 4-byte integer -enum sn_result -sn_from_val(struct val val, struct num ** sn, int require_minimal) { +enum sn_result sn_from_val(struct val val, struct num ** sn) { switch (val.type) { case VT_SMALLINT: diff --git a/script_num.h b/script_num.h @@ -22,7 +22,7 @@ int sn_overflowed(struct num *num); enum sn_result -sn_from_val(struct val val, struct num ** sn, int require_minimal); +sn_from_val(struct val val, struct num ** sn); struct val sn_to_val(struct num *sn); @@ -34,18 +34,15 @@ void sn_serialize(struct num *sn, u8 *buf, int bufsize, u16 *len); -static void inline -sn_add(struct num *a, struct num *b, struct num *c) { +inline static void sn_add(struct num *a, struct num *b, struct num *c) { c->val = a->val + b->val; } -static void inline -sn_sub(struct num *a, struct num *b, struct num *c) { +inline static void sn_sub(struct num *a, struct num *b, struct num *c) { c->val = a->val - b->val; } -static void inline -sn_neg(struct num *a, struct num *b) { +inline static void sn_neg(struct num *a, struct num *b) { b->val = -(a->val); } diff --git a/val.c b/val.c @@ -77,8 +77,8 @@ void val_serialize(struct val val, u32 *len, u8 *buf, int bufsize) { assert(!"val_serialize missing implementation"); } -int -val_eq(struct val a, struct val b, int require_minimal) { + +int val_eq(struct val a, struct val b) { u32 alen = 0, blen = 0; int eq = 0; diff --git a/val.h b/val.h @@ -36,7 +36,7 @@ struct val val_copy(struct val a); u32 val_size(struct val val); int -val_eq(struct val a, struct val b, int require_minimal); +val_eq(struct val a, struct val b); void val_serialize(struct val val, u32 *len, u8 *buf, int bufsize); void val_bytes(struct val val, u32 *len, u8 *buf, int bufsize);