示例#1
0
static void test_txout(const struct bitc_txout *txout)
{
	struct const_buffer buf = { txout->scriptPubKey->str,
				    txout->scriptPubKey->len };

	struct bscript_parser bsp;
	struct bscript_op op;
	clist *ops = NULL;

	/*
	 * parse script
	 */

	bsp_start(&bsp, &buf);

	while (bsp_getop(&op, &bsp)) {
		struct bscript_op *op_p;

		op_p = memdup(&op, sizeof(op));
		ops = clist_append(ops, op_p);
	}

	assert(!bsp.error);

	/*
	 * build script
	 */

	clist *tmp = ops;
	cstring *s = cstr_new_sz(256);
	while (tmp) {
		struct bscript_op *op_p;

		op_p = tmp->data;
		tmp = tmp->next;

		if (is_bsp_pushdata(op_p->op)) {
			bsp_push_data(s, op_p->data.p, op_p->data.len);
		} else {
			bsp_push_op(s, op_p->op);
		}
	}

	clist_free_ext(ops, free);

	/* byte-compare original and newly created scripts */
	assert(cstr_equal(s, txout->scriptPubKey));

	cstr_free(s, true);
}
示例#2
0
文件: script.c 项目: gasteve/picocoin
bool is_bsp_pushonly(struct const_buffer *buf)
{
    struct bscript_parser bp;
    struct bscript_op op;

    bsp_start(&bp, buf);

    while (bsp_getop(&op, &bp))
        if (!is_bsp_pushdata(op.op))
            return false;
    if (bp.error)
        return false;

    return true;
}
示例#3
0
文件: main.c 项目: AlexShiLucky/rtems
int main(
  int   argc,
  char **argv,
  char **environp
)
{
  extern void bsp_start( int, char**, char ** );

  bsp_start( argc, argv, environp );

  /*
   *  May be able to return to the "crt/start.s" code but also
   *  may not be able to.  Do something here which is board dependent.
   */

  rtems_fatal_error_occurred( 0 );

  return 0; /* just to satisfy the native compiler */
}
示例#4
0
文件: script.c 项目: gasteve/picocoin
GPtrArray *bsp_parse_all(const void *data_, size_t data_len)
{
    struct const_buffer buf = { data_, data_len };
    struct bscript_parser bp;
    struct bscript_op op;
    GPtrArray *arr = g_ptr_array_new_full(16, g_free);

    bsp_start(&bp, &buf);

    while (bsp_getop(&op, &bp))
        g_ptr_array_add(arr, g_memdup(&op, sizeof(op)));
    if (bp.error)
        goto err_out;

    return arr;

err_out:
    g_ptr_array_free(arr, TRUE);
    return NULL;
}
示例#5
0
文件: bootcard.c 项目: phipse/L4RTEMS
/*
 *  This is the initialization framework routine that weaves together
 *  calls to RTEMS and the BSP in the proper sequence to initialize
 *  the system while maximizing shared code and keeping BSP code in C
 *  as much as possible.
 */
int boot_card(
    const char *cmdline
)
{
    rtems_interrupt_level  bsp_isr_level;
    void                  *work_area_start = NULL;
    uintptr_t              work_area_size = 0;
    void                  *heap_start = NULL;
    uintptr_t              heap_size = 0;

    /*
     * Special case for PowerPC: The interrupt disable mask is stored in SPRG0.
     * It must be valid before we can use rtems_interrupt_disable().
     */
#ifdef PPC_INTERRUPT_DISABLE_MASK_DEFAULT
    ppc_interrupt_set_disable_mask( PPC_INTERRUPT_DISABLE_MASK_DEFAULT );
#endif /* PPC_INTERRUPT_DISABLE_MASK_DEFAULT */

    /*
     *  Make sure interrupts are disabled.
     */
    rtems_interrupt_disable( bsp_isr_level );

    bsp_boot_cmdline = cmdline;

    /*
     * Invoke Board Support Package initialization routine written in C.
     */
    bsp_start();

    /*
     *  Find out where the block of memory the BSP will use for
     *  the RTEMS Workspace and the C Program Heap is.
     */
    bsp_get_work_area(&work_area_start, &work_area_size,
                      &heap_start, &heap_size);

    if ( work_area_size <= Configuration.work_space_size ) {
        printk(
            "bootcard: work space too big for work area: %p > %p\n",
            (void *) Configuration.work_space_size,
            (void *) work_area_size
        );
        bsp_cleanup();
        return -1;
    }

    if ( rtems_unified_work_area ) {
        Configuration.work_space_start = work_area_start;
        Configuration.work_space_size  = work_area_size;
    } else {
        Configuration.work_space_start = work_area_start;
    }

#if (BSP_DIRTY_MEMORY == 1)
    memset( work_area_start, 0xCF,  work_area_size );
#endif

    /*
     *  Initialize RTEMS data structures
     */
    rtems_initialize_data_structures();

    /*
     *  Initialize the C library for those BSPs using the shared
     *  framework.
     */
    bootcard_bsp_libc_helper(
        work_area_start,
        work_area_size,
        heap_start,
        heap_size
    );

    /*
     *  All BSP to do any required initialization now that RTEMS
     *  data structures are initialized.  In older BSPs or those
     *  which do not use the shared framework, this is the typical
     *  time when the C Library is initialized so malloc()
     *  can be called by device drivers.  For BSPs using the shared
     *  framework, this routine can be empty.
     */
    bsp_pretasking_hook();

    /*
     *  If debug is enabled, then enable all dynamic RTEMS debug
     *  capabilities.
     *
     *  NOTE: Most debug features are conditionally compiled in
     *        or enabled via configure time plugins.
     */
#ifdef RTEMS_DEBUG
    rtems_debug_enable( RTEMS_DEBUG_ALL_MASK );
#endif

    /*
     *  Let RTEMS perform initialization it requires before drivers
     *  are allowed to be initialized.
     */
    rtems_initialize_before_drivers();

    /*
     *  Execute BSP specific pre-driver hook. Drivers haven't gotten
     *  to initialize yet so this is a good chance to initialize
     *  buses, spurious interrupt handlers, etc..
     *
     *  NOTE: Many BSPs do not require this handler and use the
     *        shared stub.
     */
    bsp_predriver_hook();

    /*
     *  Initialize all device drivers.
     */
    rtems_initialize_device_drivers();

    /*
     *  Invoke the postdriver hook.  This normally opens /dev/console
     *  for use as stdin, stdout, and stderr.
     */
    bsp_postdriver_hook();

    /*
     *  Complete initialization of RTEMS and switch to the first task.
     *  Global C++ constructors will be executed in the context of that task.
     */
    rtems_initialize_start_multitasking();

    /***************************************************************
     ***************************************************************
     *  APPLICATION RUNS HERE!!!  When it shuts down, we return!!! *
     ***************************************************************
     ***************************************************************
     */

    /*
     *  Perform any BSP specific shutdown actions which are written in C.
     */
    bsp_cleanup();

    /*
     *  Now return to the start code.
     */
    return 0;
}
示例#6
0
static bool bp_script_eval(parr *stack, const cstring *script,
			   const struct bp_tx *txTo, unsigned int nIn,
			   unsigned int flags, int nHashType)
{
	struct const_buffer pc = { script->str, script->len };
	struct const_buffer pend = { script->str + script->len, 0 };
	struct const_buffer pbegincodehash = { script->str, script->len };
	struct bscript_op op;
	bool rc = false;
	cstring *vfExec = cstr_new(NULL);
	parr *altstack = parr_new(0, buffer_freep);
	mpz_t bn, bn_Zero, bn_One;
	mpz_init(bn);
	mpz_init_set_ui(bn_Zero, 0);
	mpz_init_set_ui(bn_One,1);

	if (script->len > MAX_SCRIPT_SIZE)
		goto out;

	unsigned int nOpCount = 0;
	bool fRequireMinimal = (flags & SCRIPT_VERIFY_MINIMALDATA) != 0;

	struct bscript_parser bp;
	bsp_start(&bp, &pc);

	while (pc.p < pend.p) {
		bool fExec = !count_false(vfExec);

		if (!bsp_getop(&op, &bp))
			goto out;
		enum opcodetype opcode = op.op;

		if (op.data.len > MAX_SCRIPT_ELEMENT_SIZE)
			goto out;
		if (opcode > OP_16 && ++nOpCount > MAX_OPS_PER_SCRIPT)
			goto out;
		if (disabled_op[opcode])
			goto out;

		if (fExec && 0 <= opcode && opcode <= OP_PUSHDATA4) {
			if (fRequireMinimal && !CheckMinimalPush(&op.data, opcode))
				goto out;
			stack_push(stack, (struct buffer *) &op.data);
		} else if (fExec || (OP_IF <= opcode && opcode <= OP_ENDIF))
		switch (opcode) {

		//
		// Push value
		//
		case OP_1NEGATE:
		case OP_1:
		case OP_2:
		case OP_3:
		case OP_4:
		case OP_5:
		case OP_6:
		case OP_7:
		case OP_8:
		case OP_9:
		case OP_10:
		case OP_11:
		case OP_12:
		case OP_13:
		case OP_14:
		case OP_15:
		case OP_16:
			mpz_set_si(bn, (int)opcode - (int)(OP_1 - 1));
			stack_push_str(stack, bn_getvch(bn));
			break;

		//
		// Control
		//
		case OP_NOP:
			break;

		case OP_CHECKLOCKTIMEVERIFY: {
			if (!(flags & SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY)) {
				// not enabled; treat as a NOP2
				if (flags & SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS)
					goto out;
				break;
			}

			if (stack->len < 1)
				goto out;

			// Note that elsewhere numeric opcodes are limited to
			// operands in the range -2**31+1 to 2**31-1, however it is
			// legal for opcodes to produce results exceeding that
			// range. This limitation is implemented by CastToBigNum's
			// default 4-byte limit.
			//
			// If we kept to that limit we'd have a year 2038 problem,
			// even though the nLockTime field in transactions
			// themselves is uint32 which only becomes meaningless
			// after the year 2106.
			//
			// Thus as a special case we tell CastToBigNum to accept up
			// to 5-byte bignums, which are good until 2**39-1, well
			// beyond the 2**32-1 limit of the nLockTime field itself.

			if (!CastToBigNum(bn, stacktop(stack, -1), fRequireMinimal, 5))
				goto out;

			// In the rare event that the argument may be < 0 due to
			// some arithmetic being done first, you can always use
			// 0 MAX CHECKLOCKTIMEVERIFY.
			if (mpz_sgn(bn) < 0)
				goto out;

			uint64_t nLockTime = mpz_get_ui(bn);

			// Actually compare the specified lock time with the transaction.
			if (!CheckLockTime(nLockTime, txTo, nIn))
				goto out;

			break;
		}

		case OP_CHECKSEQUENCEVERIFY:
		{
			if (!(flags & SCRIPT_VERIFY_CHECKSEQUENCEVERIFY)) {
				// not enabled; treat as a NOP3
				if (flags & SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS)
					goto out;
				break;
			}

			if (stack->len < 1)
				goto out;

			// nSequence, like nLockTime, is a 32-bit unsigned integer
			// field. See the comment in CHECKLOCKTIMEVERIFY regarding
			// 5-byte numeric operands.
			if (!CastToBigNum(bn, stacktop(stack, -1), fRequireMinimal, 5))
				goto out;

			// In the rare event that the argument may be < 0 due to
			// some arithmetic being done first, you can always use
			// 0 MAX CHECKSEQUENCEVERIFY.
			if (mpz_sgn(bn) < 0)
				goto out;

			uint32_t nSequence = mpz_get_ui(bn);

			// To provide for future soft-fork extensibility, if the
			// operand has the disabled lock-time flag set,
			// CHECKSEQUENCEVERIFY behaves as a NOP.
			if ((nSequence & SEQUENCE_LOCKTIME_DISABLE_FLAG) != 0)
				break;

			// Compare the specified sequence number with the input.
			if (!CheckSequence(nSequence, txTo, nIn))
				goto out;

			break;
		}

		case OP_NOP1: case OP_NOP4: case OP_NOP5:
		case OP_NOP6: case OP_NOP7: case OP_NOP8: case OP_NOP9: case OP_NOP10:
			if (flags & SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS)
				goto out;
			break;

		case OP_IF:
		case OP_NOTIF: {
			// <expression> if [statements] [else [statements]] endif
			bool fValue = false;
			if (fExec) {
				if (stack->len < 1)
					goto out;
				struct buffer *vch = stacktop(stack, -1);
				fValue = CastToBool(vch);
				if (opcode == OP_NOTIF)
					fValue = !fValue;
				popstack(stack);
			}
			uint8_t vc = (uint8_t) fValue;
			cstr_append_c(vfExec, vc);
			break;
		}

		case OP_ELSE: {
			if (vfExec->len == 0)
				goto out;
			uint8_t *v = (uint8_t *) &vfExec->str[vfExec->len - 1];
			*v = !(*v);
			break;
		}

		case OP_ENDIF:
			if (vfExec->len == 0)
				goto out;
			cstr_erase(vfExec, vfExec->len - 1, 1);
			break;

		case OP_VERIFY: {
			if (stack->len < 1)
				goto out;
			bool fValue = CastToBool(stacktop(stack, -1));
			if (fValue)
				popstack(stack);
			else
				goto out;
			break;
		}

		case OP_RETURN:
			goto out;

		//
		// Stack ops
		//
		case OP_TOALTSTACK:
			if (stack->len < 1)
				goto out;
			stack_push(altstack, stacktop(stack, -1));
			popstack(stack);
			break;

		case OP_FROMALTSTACK:
			if (altstack->len < 1)
				goto out;
			stack_push(stack, stacktop(altstack, -1));
			popstack(altstack);
			break;

		case OP_2DROP:
			// (x1 x2 -- )
			if (stack->len < 2)
				goto out;
			popstack(stack);
			popstack(stack);
			break;

		case OP_2DUP: {
			// (x1 x2 -- x1 x2 x1 x2)
			if (stack->len < 2)
				goto out;
			struct buffer *vch1 = stacktop(stack, -2);
			struct buffer *vch2 = stacktop(stack, -1);
			stack_push(stack, vch1);
			stack_push(stack, vch2);
			break;
		}

		case OP_3DUP: {
			// (x1 x2 x3 -- x1 x2 x3 x1 x2 x3)
			if (stack->len < 3)
				goto out;
			struct buffer *vch1 = stacktop(stack, -3);
			struct buffer *vch2 = stacktop(stack, -2);
			struct buffer *vch3 = stacktop(stack, -1);
			stack_push(stack, vch1);
			stack_push(stack, vch2);
			stack_push(stack, vch3);
			break;
		}

		case OP_2OVER: {
			// (x1 x2 x3 x4 -- x1 x2 x3 x4 x1 x2)
			if (stack->len < 4)
				goto out;
			struct buffer *vch1 = stacktop(stack, -4);
			struct buffer *vch2 = stacktop(stack, -3);
			stack_push(stack, vch1);
			stack_push(stack, vch2);
			break;
		}

		case OP_2ROT: {
			// (x1 x2 x3 x4 x5 x6 -- x3 x4 x5 x6 x1 x2)
			if (stack->len < 6)
				goto out;
			struct buffer *vch1 = stack_take(stack, -6);
			struct buffer *vch2 = stack_take(stack, -5);
			parr_remove_range(stack, stack->len - 6, 2);
			stack_push_nocopy(stack, vch1);
			stack_push_nocopy(stack, vch2);
			break;
		}

		case OP_2SWAP:
			// (x1 x2 x3 x4 -- x3 x4 x1 x2)
			if (stack->len < 4)
				goto out;
			stack_swap(stack, -4, -2);
			stack_swap(stack, -3, -1);
			break;

		case OP_IFDUP: {
			// (x - 0 | x x)
			if (stack->len < 1)
				goto out;
			struct buffer *vch = stacktop(stack, -1);
			if (CastToBool(vch))
				stack_push(stack, vch);
			break;
		}

		case OP_DEPTH:
			// -- stacksize
			mpz_set_ui(bn, stack->len);
			stack_push_str(stack, bn_getvch(bn));
			break;

		case OP_DROP:
			// (x -- )
			if (stack->len < 1)
				goto out;
			popstack(stack);
			break;

		case OP_DUP: {
			// (x -- x x)
			if (stack->len < 1)
				goto out;
			struct buffer *vch = stacktop(stack, -1);
			stack_push(stack, vch);
			break;
		}

		case OP_NIP:
			// (x1 x2 -- x2)
			if (stack->len < 2)
				goto out;
			parr_remove_idx(stack, stack->len - 2);
			break;

		case OP_OVER: {
			// (x1 x2 -- x1 x2 x1)
			if (stack->len < 2)
				goto out;
			struct buffer *vch = stacktop(stack, -2);
			stack_push(stack, vch);
			break;
		}

		case OP_PICK:
		case OP_ROLL: {
			// (xn ... x2 x1 x0 n - xn ... x2 x1 x0 xn)
			// (xn ... x2 x1 x0 n - ... x2 x1 x0 xn)
			if (stack->len < 2)
				goto out;

			int n = stackint(stack, -1, fRequireMinimal);
			popstack(stack);
			if (n < 0 || n >= (int)stack->len)
				goto out;
			struct buffer *vch = stacktop(stack, -n-1);
			if (opcode == OP_ROLL) {
				vch = buffer_copy(vch->p, vch->len);
				parr_remove_idx(stack,
							 stack->len - n - 1);
				stack_push_nocopy(stack, vch);
			} else
				stack_push(stack, vch);
			break;
		}

		case OP_ROT: {
			// (x1 x2 x3 -- x2 x3 x1)
			//  x2 x1 x3  after first swap
			//  x2 x3 x1  after second swap
			if (stack->len < 3)
				goto out;
			stack_swap(stack, -3, -2);
			stack_swap(stack, -2, -1);
			break;
		}

		case OP_SWAP: {
			// (x1 x2 -- x2 x1)
			if (stack->len < 2)
				goto out;
			stack_swap(stack, -2, -1);
			break;
		}

		case OP_TUCK: {
			// (x1 x2 -- x2 x1 x2)
			if (stack->len < 2)
				goto out;
			struct buffer *vch = stacktop(stack, -1);
			stack_insert(stack, vch, -2);
			break;
		}

		case OP_SIZE: {
			// (in -- in size)
			if (stack->len < 1)
				goto out;
			struct buffer *vch = stacktop(stack, -1);
			mpz_set_ui(bn, vch->len);
			stack_push_str(stack, bn_getvch(bn));
			break;
		}


		case OP_EQUAL:
		case OP_EQUALVERIFY: {
			// (x1 x2 - bool)
			if (stack->len < 2)
				goto out;
			struct buffer *vch1 = stacktop(stack, -2);
			struct buffer *vch2 = stacktop(stack, -1);
			bool fEqual = buffer_equal(vch1, vch2);
			// 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)
			//if (opcode == OP_NOTEQUAL)
			//	fEqual = !fEqual;
			popstack(stack);
			popstack(stack);
			stack_push_str(stack, fEqual ? bn_getvch(bn_One) : bn_getvch(bn_Zero));
			if (opcode == OP_EQUALVERIFY) {
				if (fEqual)
					popstack(stack);
				else
					goto out;
			}
			break;
		}

		//
		// Numeric
		//
		case OP_1ADD:
		case OP_1SUB:
		case OP_NEGATE:
		case OP_ABS:
		case OP_NOT:
		case OP_0NOTEQUAL: {
			// (in -- out)
			if (stack->len < 1)
				goto out;
			if (!CastToBigNum(bn, stacktop(stack, -1), fRequireMinimal, nDefaultMaxNumSize))
				goto out;
			switch (opcode)
			{
			case OP_1ADD:
				mpz_add_ui(bn, bn, 1);
				break;
			case OP_1SUB:
				mpz_sub_ui(bn, bn, 1);
				break;
			case OP_NEGATE:
				mpz_neg(bn, bn);
				break;
			case OP_ABS:
				mpz_abs(bn, bn);
				break;
			case OP_NOT:
				mpz_set_ui(bn, mpz_sgn(bn) == 0 ? 1 : 0);
				break;
			case OP_0NOTEQUAL:
				mpz_set_ui(bn, mpz_sgn(bn) == 0 ? 0 : 1);
				break;
			default:
				// impossible
				goto out;
			}
			popstack(stack);
			stack_push_str(stack, bn_getvch(bn));
			break;
		}

		case OP_ADD:
		case OP_SUB:
		case OP_BOOLAND:
		case OP_BOOLOR:
		case OP_NUMEQUAL:
		case OP_NUMEQUALVERIFY:
		case OP_NUMNOTEQUAL:
		case OP_LESSTHAN:
		case OP_GREATERTHAN:
		case OP_LESSTHANOREQUAL:
		case OP_GREATERTHANOREQUAL:
		case OP_MIN:
		case OP_MAX: {
			// (x1 x2 -- out)
			if (stack->len < 2)
				goto out;

			mpz_t bn1, bn2;
			mpz_init(bn1);
			mpz_init(bn2);
			if (!CastToBigNum(bn1, stacktop(stack, -2), fRequireMinimal, nDefaultMaxNumSize) ||
			    !CastToBigNum(bn2, stacktop(stack, -1), fRequireMinimal, nDefaultMaxNumSize)) {
				mpz_clear(bn1);
				mpz_clear(bn2);
				goto out;
			}

			switch (opcode)
			{
			case OP_ADD:
				mpz_add(bn, bn1, bn2);
				break;
			case OP_SUB:
				mpz_sub(bn, bn1, bn2);
				break;
			case OP_BOOLAND:
				mpz_set_ui(bn,
				    !(mpz_sgn(bn1) == 0) && !(mpz_sgn(bn2) == 0) ?
				    1 : 0);
				break;
			case OP_BOOLOR:
				mpz_set_ui(bn,
				    !(mpz_sgn(bn1) == 0) || !(mpz_sgn(bn2) == 0) ?
				    1 : 0);
				break;
			case OP_NUMEQUAL:
			case OP_NUMEQUALVERIFY:
				mpz_set_ui(bn,
				    mpz_cmp(bn1, bn2) == 0 ?  1 : 0);
				break;
			case OP_NUMNOTEQUAL:
				mpz_set_ui(bn,
				    mpz_cmp(bn1, bn2) != 0 ?  1 : 0);
				break;
			case OP_LESSTHAN:
				mpz_set_ui(bn,
				    mpz_cmp(bn1, bn2) < 0 ?  1 : 0);
				break;
			case OP_GREATERTHAN:
				mpz_set_ui(bn,
				    mpz_cmp(bn1, bn2) > 0 ?  1 : 0);
				break;
			case OP_LESSTHANOREQUAL:
				mpz_set_ui(bn,
				    mpz_cmp(bn1, bn2) <= 0 ?  1 : 0);
				break;
			case OP_GREATERTHANOREQUAL:
				mpz_set_ui(bn,
				    mpz_cmp(bn1, bn2) >= 0 ?  1 : 0);
				break;
			case OP_MIN:
				if (mpz_cmp(bn1, bn2) < 0)
					mpz_set(bn, bn1);
				else
					mpz_set(bn, bn2);
				break;
			case OP_MAX:
				if (mpz_cmp(bn1, bn2) > 0)
					mpz_set(bn, bn1);
				else
					mpz_set(bn, bn2);
				break;
			default:
				// impossible
				break;
			}
			popstack(stack);
			popstack(stack);
			stack_push_str(stack, bn_getvch(bn));
			mpz_clear(bn1);
			mpz_clear(bn2);

			if (opcode == OP_NUMEQUALVERIFY)
			{
				if (CastToBool(stacktop(stack, -1)))
					popstack(stack);
				else
					goto out;
			}
			break;
		}

		case OP_WITHIN: {
			// (x min max -- out)
			if (stack->len < 3)
				goto out;
			mpz_t bn1, bn2, bn3;
			mpz_init(bn1);
			mpz_init(bn2);
			mpz_init(bn3);
			bool rc1 = CastToBigNum(bn1, stacktop(stack, -3), fRequireMinimal, nDefaultMaxNumSize);
			bool rc2 = CastToBigNum(bn2, stacktop(stack, -2), fRequireMinimal, nDefaultMaxNumSize);
			bool rc3 = CastToBigNum(bn3, stacktop(stack, -1), fRequireMinimal, nDefaultMaxNumSize);
			bool fValue = (mpz_cmp(bn2, bn1) <= 0 &&
				       mpz_cmp(bn1, bn3) < 0);
			popstack(stack);
			popstack(stack);
			popstack(stack);
			stack_push_str(stack, fValue ? bn_getvch(bn_One) : bn_getvch(bn_Zero));
			mpz_clear(bn1);
			mpz_clear(bn2);
			mpz_clear(bn3);
			if (!rc1 || !rc2 || !rc3)
				goto out;
			break;
		}

		//
		// Crypto
		//
		case OP_RIPEMD160:
		case OP_SHA1:
		case OP_SHA256:
		case OP_HASH160:
		case OP_HASH256: {
			// (in -- hash)
			if (stack->len < 1)
				goto out;
			struct buffer *vch = stacktop(stack, -1);
			unsigned int hashlen;
			unsigned char md[32];

			switch (opcode) {
			case OP_RIPEMD160:
				hashlen = 20;
				ripemd160(vch->p, vch->len, md);
				break;
			case OP_SHA1:
				hashlen = 20;
				sha1_Raw(vch->p, vch->len, md);
				break;
			case OP_SHA256:
				hashlen = 32;
				sha256_Raw(vch->p, vch->len, md);
				break;
			case OP_HASH160:
				hashlen = 20;
				bu_Hash160(md, vch->p, vch->len);
				break;
			case OP_HASH256:
				hashlen = 32;
				bu_Hash(md, vch->p, vch->len);
				break;
			default:
				// impossible
				goto out;
			}

			popstack(stack);
			struct buffer buf = { md, hashlen };
			stack_push(stack, &buf);
			break;
		}

		case OP_CODESEPARATOR:
			// Hash starts after the code separator
			memcpy(&pbegincodehash, &pc, sizeof(pc));
			break;

		case OP_CHECKSIG:
		case OP_CHECKSIGVERIFY: {
			// (sig pubkey -- bool)
			if (stack->len < 2)
				goto out;

			struct buffer *vchSig	= stacktop(stack, -2);
			struct buffer *vchPubKey = stacktop(stack, -1);

			// Subset of script starting at the most recent codeseparator
			cstring *scriptCode = cstr_new_buf(pbegincodehash.p,
                                                pbegincodehash.len);

			// Drop the signature, since there's no way for
			// a signature to sign itself
			string_find_del(scriptCode, vchSig);

			if (!CheckSignatureEncoding(vchSig, flags) || !CheckPubKeyEncoding(vchPubKey, flags)) {
				cstr_free(scriptCode, true);
				goto out;
			}

			bool fSuccess = bp_checksig(vchSig, vchPubKey,
						       scriptCode,
						       txTo, nIn);

			cstr_free(scriptCode, true);

			popstack(stack);
			popstack(stack);
			stack_push_str(stack, fSuccess ? bn_getvch(bn_One) : bn_getvch(bn_Zero));
			if (opcode == OP_CHECKSIGVERIFY)
			{
				if (fSuccess)
					popstack(stack);
				else
					goto out;
			}
			break;
		}

		case OP_CHECKMULTISIG:
		case OP_CHECKMULTISIGVERIFY: {
			// ([sig ...] num_of_signatures [pubkey ...] num_of_pubkeys -- bool)

			int i = 1;
			if ((int)stack->len < i)
				goto out;

			int nKeysCount = stackint(stack, -i, fRequireMinimal);
			if (nKeysCount < 0 || nKeysCount > MAX_PUBKEYS_PER_MULTISIG)
				goto out;
			nOpCount += nKeysCount;
			if (nOpCount > MAX_OPS_PER_SCRIPT)
				goto out;
			int ikey = ++i;
			i += nKeysCount;
			if ((int)stack->len < i)
				goto out;

			int nSigsCount = stackint(stack, -i, fRequireMinimal);
			if (nSigsCount < 0 || nSigsCount > nKeysCount)
				goto out;
			int isig = ++i;
			i += nSigsCount;
			if ((int)stack->len < i)
				goto out;

			// Subset of script starting at the most recent codeseparator
			cstring *scriptCode = cstr_new_buf(pbegincodehash.p,
                                                pbegincodehash.len);

			// Drop the signatures, since there's no way for
			// a signature to sign itself
			int k;
			for (k = 0; k < nSigsCount; k++)
			{
				struct buffer *vchSig =stacktop(stack, -isig-k);
				string_find_del(scriptCode, vchSig);
			}

			bool fSuccess = true;
			while (fSuccess && nSigsCount > 0)
			{
				struct buffer *vchSig	= stacktop(stack, -isig);
				struct buffer *vchPubKey = stacktop(stack, -ikey);

				// Note how this makes the exact order of pubkey/signature evaluation
				// distinguishable by CHECKMULTISIG NOT if the STRICTENC flag is set.
				// See the script_(in)valid tests for details.
				if (!CheckSignatureEncoding(vchSig, flags) || !CheckPubKeyEncoding(vchPubKey, flags)) {
					cstr_free(scriptCode, true);
					goto out;
				}

				// Check signature
				bool fOk = bp_checksig(vchSig, vchPubKey,
							  scriptCode, txTo, nIn);

				if (fOk) {
					isig++;
					nSigsCount--;
				}
				ikey++;
				nKeysCount--;

				// If there are more signatures left than keys left,
				// then too many signatures have failed
				if (nSigsCount > nKeysCount)
					fSuccess = false;
			}

			cstr_free(scriptCode, true);

			// Clean up stack of actual arguments
			while (i-- > 1)
				popstack(stack);

			// A bug causes CHECKMULTISIG to consume one extra argument
			// whose contents were not checked in any way.
			//
			// Unfortunately this is a potential source of mutability,
			// so optionally verify it is exactly equal to zero prior
			// to removing it from the stack.
			if ((int)stack->len < 1)
				goto out;
			if ((flags & SCRIPT_VERIFY_NULLDUMMY) && stacktop(stack, -1)->len)
				goto out;
			popstack(stack);

			stack_push_str(stack, fSuccess ? bn_getvch(bn_One) : bn_getvch(bn_Zero));

			if (opcode == OP_CHECKMULTISIGVERIFY)
			{
				if (fSuccess)
					popstack(stack);
				else
					goto out;
			}
			break;
		}

		default:
			goto out;
		}

		// Size limits
		if (stack->len + altstack->len > 1000)
			goto out;
	}

	rc = (vfExec->len == 0 && bp.error == false);

out:
	mpz_clears(bn, bn_Zero, bn_One, NULL);
	parr_free(altstack, true);
	cstr_free(vfExec, true);
	return rc;
}
示例#7
0
void bp_tx_sigserializer(cstring *s, const cstring *scriptCode,
			const struct bp_tx *txTo, unsigned int nIn,
			int nHashType)
{
    const bool fAnyoneCanPay = (!!(nHashType & SIGHASH_ANYONECANPAY));
    const bool fHashSingle = ((nHashType & 0x1f) == SIGHASH_SINGLE);
    const bool fHashNone = ((nHashType & 0x1f) == SIGHASH_NONE);

    /** Serialize txTo */
    // Serialize nVersion
    ser_u32(s, txTo->nVersion);

    // Serialize vin
    unsigned int nInputs = fAnyoneCanPay ? 1 : txTo->vin->len;
    ser_varlen(s, nInputs);

	unsigned int nInput;
	for (nInput = 0; nInput < nInputs; nInput++) {
	    /** Serialize an input of txTo */
	    // In case of SIGHASH_ANYONECANPAY, only the input being signed is serialized
		if (fAnyoneCanPay)
			nInput = nIn;

	    struct bp_txin *txin = parr_idx(txTo->vin, nInput);

	    // Serialize the prevout
	    ser_bp_outpt(s, &txin->prevout);

		// Serialize the script
		if (nInput != nIn)
			// Blank out other inputs' signatures
			ser_varlen(s, (int)0);
		else if (scriptCode == NULL)
		    cstr_append_c(s, 0);
		else {
			/** Serialize the passed scriptCode, skipping OP_CODESEPARATORs */
			struct const_buffer it = { scriptCode->str, scriptCode->len };
			struct const_buffer itBegin = it;
			struct bscript_op op;
			unsigned int nCodeSeparators = 0;

			struct bscript_parser bp;
			bsp_start(&bp, &it);

			while (bsp_getop(&op, &bp)) {
				if (op.op == OP_CODESEPARATOR)
				    nCodeSeparators++;
			}
			ser_varlen(s, scriptCode->len - nCodeSeparators);

			it = itBegin;
			bsp_start(&bp, &it);

			while (bsp_getop(&op, &bp)) {
			    if (op.op == OP_CODESEPARATOR) {
					ser_bytes(s, itBegin.p, it.p - itBegin.p - 1);
					itBegin  = it;
			    }
			}

			if (itBegin.p != scriptCode->str + scriptCode->len)
			    ser_bytes(s, itBegin.p, it.p - itBegin.p);
		}

		// Serialize the nSequence
		if ((nInput != nIn) && (fHashSingle || fHashNone))
			// let the others update at will
			ser_u32(s, (int)0);
		else
			ser_u32(s, txin->nSequence);
	}

    // Serialize vout
    unsigned int nOutputs = fHashNone ? 0 : (fHashSingle ? (nIn + 1) : txTo->vout->len);
    ser_varlen(s, nOutputs);

	unsigned int nOutput;
    for (nOutput = 0; nOutput < nOutputs; nOutput++) {
		struct bp_txout *txout = parr_idx(txTo->vout, nOutput);
		if (fHashSingle && (nOutput != nIn))
			// Do not lock-in the txout payee at other indices as txin;
			bp_txout_set_null(txout);

		ser_bp_txout(s, txout);
    }
    // Serialize nLockTime
    ser_u32(s, txTo->nLockTime);
}
示例#8
0
文件: bootcard.c 项目: fsmd/RTEMS
/*
 *  This is the initialization framework routine that weaves together
 *  calls to RTEMS and the BSP in the proper sequence to initialize
 *  the system while maximizing shared code and keeping BSP code in C
 *  as much as possible.
 */
void boot_card(
  const char *cmdline
)
{
  rtems_interrupt_level  bsp_isr_level;

  /*
   *  Make sure interrupts are disabled.
   */
  (void) bsp_isr_level;
  rtems_interrupt_local_disable( bsp_isr_level );

  bsp_boot_cmdline = cmdline;

  /*
   * Invoke Board Support Package initialization routine written in C.
   */
  bsp_start();

  /*
   *  Initialize the RTEMS Workspace and the C Program Heap.
   */
  bsp_work_area_initialize();

  /*
   *  Initialize RTEMS data structures
   */
  rtems_initialize_data_structures();

  /*
   *  Initialize the C library for those BSPs using the shared
   *  framework.
   */
  bsp_libc_init();

  /*
   *  Let the BSP do any required initialization now that RTEMS
   *  data structures are initialized.  In older BSPs or those
   *  which do not use the shared framework, this is the typical
   *  time when the C Library is initialized so malloc()
   *  can be called by device drivers.  For BSPs using the shared
   *  framework, this routine can be empty.
   */
  bsp_pretasking_hook();

  /*
   *  If debug is enabled, then enable all dynamic RTEMS debug
   *  capabilities.
   *
   *  NOTE: Most debug features are conditionally compiled in
   *        or enabled via configure time plugins.
   */
  #ifdef RTEMS_DEBUG
    rtems_debug_enable( RTEMS_DEBUG_ALL_MASK );
  #endif

  /*
   *  Let RTEMS perform initialization it requires before drivers
   *  are allowed to be initialized.
   */
  rtems_initialize_before_drivers();

  /*
   *  Execute BSP specific pre-driver hook. Drivers haven't gotten
   *  to initialize yet so this is a good chance to initialize
   *  buses, spurious interrupt handlers, etc..
   *
   *  NOTE: Many BSPs do not require this handler and use the
   *        shared stub.
   */
  bsp_predriver_hook();

  /*
   *  Initialize all device drivers.
   */
  rtems_initialize_device_drivers();

  /*
   *  Invoke the postdriver hook.  This normally opens /dev/console
   *  for use as stdin, stdout, and stderr.
   */
  bsp_postdriver_hook();

  /*
   *  Complete initialization of RTEMS and switch to the first task.
   *  Global C++ constructors will be executed in the context of that task.
   */
  rtems_initialize_start_multitasking();

  /***************************************************************
   ***************************************************************
   *  APPLICATION RUNS NOW!!!  We will not return to here!!!     *
   ***************************************************************
   ***************************************************************/
}
示例#9
0
static bool bp_script_eval(GPtrArray *stack, const GString *script,
			   const struct bp_tx *txTo, unsigned int nIn,
			   unsigned int flags, int nHashType)
{
	struct const_buffer pc = { script->str, script->len };
	struct const_buffer pend = { script->str + script->len, 0 };
	struct const_buffer pbegincodehash = { script->str, script->len };
	struct bscript_op op;
	bool rc = false;
	GByteArray *vfExec = g_byte_array_new();
	GPtrArray *altstack = g_ptr_array_new_with_free_func(
						(GDestroyNotify) buffer_free);
	BIGNUM bn;
	BN_init(&bn);

	if (script->len > 10000)
		goto out;
	
	bool fStrictEncodings = flags & SCRIPT_VERIFY_STRICTENC;
	unsigned int nOpCount = 0;

	struct bscript_parser bp;
	bsp_start(&bp, &pc);

	while (pc.p < pend.p) {
		bool fExec = !count_false(vfExec);

		if (!bsp_getop(&op, &bp))
			goto out;
		enum opcodetype opcode = op.op;

		if (op.data.len > 520)
			goto out;
		if (opcode > OP_16 && ++nOpCount > 201)
			goto out;
		if (disabled_op[opcode])
			goto out;

		if (fExec && is_bsp_pushdata(opcode))
			stack_push(stack, (struct buffer *) &op.data);
		else if (fExec || (OP_IF <= opcode && opcode <= OP_ENDIF))
		switch (opcode) {

		//
		// Push value
		//
		case OP_1NEGATE:
		case OP_1:
		case OP_2:
		case OP_3:
		case OP_4:
		case OP_5:
		case OP_6:
		case OP_7:
		case OP_8:
		case OP_9:
		case OP_10:
		case OP_11:
		case OP_12:
		case OP_13:
		case OP_14:
		case OP_15:
		case OP_16:
			bn_set_int(&bn, (int)opcode - (int)(OP_1 - 1));
			stack_push_str(stack, bn_getvch(&bn));
			break;

		//
		// Control
		//
		case OP_NOP:
		case OP_NOP1: case OP_NOP2: case OP_NOP3: case OP_NOP4: case
OP_NOP5:
		case OP_NOP6: case OP_NOP7: case OP_NOP8: case OP_NOP9: case
OP_NOP10:
			break;

		case OP_IF:
		case OP_NOTIF: {
			// <expression> if [statements] [else [statements]] endif
			bool fValue = false;
			if (fExec) {
				if (stack->len < 1)
					goto out;
				struct buffer *vch = stacktop(stack, -1);
				fValue = CastToBool(vch);
				if (opcode == OP_NOTIF)
					fValue = !fValue;
				popstack(stack);
			}
			guint8 vc = (guint8) fValue;
			g_byte_array_append(vfExec, &vc, 1);
			break;
		}

		case OP_ELSE: {
			if (vfExec->len == 0)
				goto out;
			guint8 *v = &vfExec->data[vfExec->len - 1];
			*v = !(*v);
			break;
		}

		case OP_ENDIF:
			if (vfExec->len == 0)
				goto out;
			g_byte_array_remove_index(vfExec, vfExec->len - 1);
			break;

		case OP_VERIFY: {
			if (stack->len < 1)
				goto out;
			bool fValue = CastToBool(stacktop(stack, -1));
			if (fValue)
				popstack(stack);
			else
				goto out;
			break;
		}

		case OP_RETURN:
			goto out;

		//
		// Stack ops
		//
		case OP_TOALTSTACK:
			if (stack->len < 1)
				goto out;
			stack_push(altstack, stacktop(stack, -1));
			popstack(stack);
			break;

		case OP_FROMALTSTACK:
			if (altstack->len < 1)
				goto out;
			stack_push(stack, stacktop(altstack, -1));
			popstack(altstack);
			break;

		case OP_2DROP:
			// (x1 x2 -- )
			if (stack->len < 2)
				goto out;
			popstack(stack);
			popstack(stack);
			break;

		case OP_2DUP: {
			// (x1 x2 -- x1 x2 x1 x2)
			if (stack->len < 2)
				goto out;
			struct buffer *vch1 = stacktop(stack, -2);
			struct buffer *vch2 = stacktop(stack, -1);
			stack_push(stack, vch1);
			stack_push(stack, vch2);
			break;
		}

		case OP_3DUP: {
			// (x1 x2 x3 -- x1 x2 x3 x1 x2 x3)
			if (stack->len < 3)
				goto out;
			struct buffer *vch1 = stacktop(stack, -3);
			struct buffer *vch2 = stacktop(stack, -2);
			struct buffer *vch3 = stacktop(stack, -1);
			stack_push(stack, vch1);
			stack_push(stack, vch2);
			stack_push(stack, vch3);
			break;
		}

		case OP_2OVER: {
			// (x1 x2 x3 x4 -- x1 x2 x3 x4 x1 x2)
			if (stack->len < 4)
				goto out;
			struct buffer *vch1 = stacktop(stack, -4);
			struct buffer *vch2 = stacktop(stack, -3);
			stack_push(stack, vch1);
			stack_push(stack, vch2);
			break;
		}

		case OP_2ROT: {
			// (x1 x2 x3 x4 x5 x6 -- x3 x4 x5 x6 x1 x2)
			if (stack->len < 6)
				goto out;
			struct buffer *vch1 = stack_take(stack, -6);
			struct buffer *vch2 = stack_take(stack, -5);
			g_ptr_array_remove_range(stack, stack->len - 6, 2);
			stack_push(stack, vch1);
			stack_push(stack, vch2);
			break;
		}

		case OP_2SWAP:
			// (x1 x2 x3 x4 -- x3 x4 x1 x2)
			if (stack->len < 4)
				goto out;
			stack_swap(stack, -4, -2);
			stack_swap(stack, -3, -1);
			break;

		case OP_IFDUP: {
			// (x - 0 | x x)
			if (stack->len < 1)
				goto out;
			struct buffer *vch = stacktop(stack, -1);
			if (CastToBool(vch))
				stack_push(stack, vch);
			break;
		}

		case OP_DEPTH:
			// -- stacksize
			BN_set_word(&bn, stack->len);
			stack_push_str(stack, bn_getvch(&bn));
			break;

		case OP_DROP:
			// (x -- )
			if (stack->len < 1)
				goto out;
			popstack(stack);
			break;

		case OP_DUP: {
			// (x -- x x)
			if (stack->len < 1)
				goto out;
			struct buffer *vch = stacktop(stack, -1);
			stack_push(stack, vch);
			break;
		}

		case OP_NIP:
			// (x1 x2 -- x2)
			if (stack->len < 2)
				goto out;
			g_ptr_array_remove_index(stack, stack->len - 2);
			break;

		case OP_OVER: {
			// (x1 x2 -- x1 x2 x1)
			if (stack->len < 2)
				goto out;
			struct buffer *vch = stacktop(stack, -2);
			stack_push(stack, vch);
			break;
		}

		case OP_PICK:
		case OP_ROLL: {
			// (xn ... x2 x1 x0 n - xn ... x2 x1 x0 xn)
			// (xn ... x2 x1 x0 n - ... x2 x1 x0 xn)
			if (stack->len < 2)
				goto out;
			int n = stackint(stack, -1);
			popstack(stack);
			if (n < 0 || n >= (int)stack->len)
				goto out;
			struct buffer *vch = stacktop(stack, -n-1);
			if (opcode == OP_ROLL) {
				vch = buffer_copy(vch->p, vch->len);
				g_ptr_array_remove_index(stack,
							 stack->len - n - 1);
				stack_push_nocopy(stack, vch);
			} else
				stack_push(stack, vch);
			break;
		}

		case OP_ROT: {
			// (x1 x2 x3 -- x2 x3 x1)
			//  x2 x1 x3  after first swap
			//  x2 x3 x1  after second swap
			if (stack->len < 3)
				goto out;
			stack_swap(stack, -3, -2);
			stack_swap(stack, -2, -1);
			break;
		}

		case OP_SWAP: {
			// (x1 x2 -- x2 x1)
			if (stack->len < 2)
				goto out;
			stack_swap(stack, -2, -1);
			break;
		}

		case OP_TUCK: {
			// (x1 x2 -- x2 x1 x2)
			if (stack->len < 2)
				goto out;
			struct buffer *vch = stacktop(stack, -1);
			stack_insert(stack, vch, -2);
			break;
		}

		case OP_SIZE: {
			// (in -- in size)
			if (stack->len < 1)
				goto out;
			struct buffer *vch = stacktop(stack, -1);
			BN_set_word(&bn, vch->len);
			stack_push_str(stack, bn_getvch(&bn));
			break;
		}


		case OP_EQUAL:
		case OP_EQUALVERIFY: {
			// (x1 x2 - bool)
			if (stack->len < 2)
				goto out;
			struct buffer *vch1 = stacktop(stack, -2);
			struct buffer *vch2 = stacktop(stack, -1);
			bool fEqual = ((vch1->len == vch2->len) &&
				      memcmp(vch1->p, vch2->p, vch1->len) == 0);
			// 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)
			//if (opcode == OP_NOTEQUAL)
			//	fEqual = !fEqual;
			popstack(stack);
			popstack(stack);
			stack_push_char(stack, fEqual ? 1 : 0);
			if (opcode == OP_EQUALVERIFY) {
				if (fEqual)
					popstack(stack);
				else
					goto out;
			}
			break;
		}

		//
		// Numeric
		//
		case OP_1ADD:
		case OP_1SUB:
		case OP_NEGATE:
		case OP_ABS:
		case OP_NOT:
		case OP_0NOTEQUAL: {
			// (in -- out)
			if (stack->len < 1)
				goto out;
			if (!CastToBigNum(&bn, stacktop(stack, -1)))
				goto out;
			switch (opcode)
			{
			case OP_1ADD:
				BN_add_word(&bn, 1);
				break;
			case OP_1SUB:
				BN_sub_word(&bn, 1);
				break;
			case OP_NEGATE:
				BN_set_negative(&bn, !BN_is_negative(&bn));
				break;
			case OP_ABS:
				if (BN_is_negative(&bn))
					BN_set_negative(&bn, 0);
				break;
			case OP_NOT:
				BN_set_word(&bn, BN_is_zero(&bn) ? 1 : 0);
				break;
			case OP_0NOTEQUAL:
				BN_set_word(&bn, BN_is_zero(&bn) ? 0 : 1);
				break;
			default:
				// impossible
				goto out;
			}
			popstack(stack);
			stack_push_str(stack, bn_getvch(&bn));
			break;
		}

		case OP_ADD:
		case OP_SUB:
		case OP_BOOLAND:
		case OP_BOOLOR:
		case OP_NUMEQUAL:
		case OP_NUMEQUALVERIFY:
		case OP_NUMNOTEQUAL:
		case OP_LESSTHAN:
		case OP_GREATERTHAN:
		case OP_LESSTHANOREQUAL:
		case OP_GREATERTHANOREQUAL:
		case OP_MIN:
		case OP_MAX: {
			// (x1 x2 -- out)
			if (stack->len < 2)
				goto out;

			BIGNUM bn1, bn2;
			BN_init(&bn1);
			BN_init(&bn2);
			if (!CastToBigNum(&bn1, stacktop(stack, -2)) ||
			    !CastToBigNum(&bn2, stacktop(stack, -1))) {
				BN_clear_free(&bn1);
				BN_clear_free(&bn2);
				goto out;
			}

			switch (opcode)
			{
			case OP_ADD:
				BN_add(&bn, &bn1, &bn2);
				break;
			case OP_SUB:
				BN_sub(&bn, &bn1, &bn2);
				break;
			case OP_BOOLAND:
				BN_set_word(&bn,
				    (!BN_is_zero(&bn1) && !BN_is_zero(&bn2)) ?
				    1 : 0);
				break;
			case OP_BOOLOR:
				BN_set_word(&bn,
				    (!BN_is_zero(&bn1) || !BN_is_zero(&bn2)) ?
				    1 : 0);
				break;
			case OP_NUMEQUAL:
			case OP_NUMEQUALVERIFY:
				BN_set_word(&bn,
				    (BN_cmp(&bn1, &bn2) == 0) ?  1 : 0);
				break;
			case OP_NUMNOTEQUAL:
				BN_set_word(&bn,
				    (BN_cmp(&bn1, &bn2) != 0) ?  1 : 0);
				break;
			case OP_LESSTHAN:
				BN_set_word(&bn,
				    (BN_cmp(&bn1, &bn2) < 0) ?  1 : 0);
				break;
			case OP_GREATERTHAN:
				BN_set_word(&bn,
				    (BN_cmp(&bn1, &bn2) > 0) ?  1 : 0);
				break;
			case OP_LESSTHANOREQUAL:
				BN_set_word(&bn,
				    (BN_cmp(&bn1, &bn2) <= 0) ?  1 : 0);
				break;
			case OP_GREATERTHANOREQUAL:
				BN_set_word(&bn,
				    (BN_cmp(&bn1, &bn2) >= 0) ?  1 : 0);
				break;
			case OP_MIN:
				if (BN_cmp(&bn1, &bn2) < 0)
					BN_copy(&bn, &bn1);
				else
					BN_copy(&bn, &bn2);
				break;
			case OP_MAX:
				if (BN_cmp(&bn1, &bn2) > 0)
					BN_copy(&bn, &bn1);
				else
					BN_copy(&bn, &bn2);
				break;
			default:
				// impossible
				break;
			}
			popstack(stack);
			popstack(stack);
			stack_push_str(stack, bn_getvch(&bn));
			BN_clear_free(&bn1);
			BN_clear_free(&bn2);

			if (opcode == OP_NUMEQUALVERIFY)
			{
				if (CastToBool(stacktop(stack, -1)))
					popstack(stack);
				else
					goto out;
			}
			break;
		}

		case OP_WITHIN: {
			// (x min max -- out)
			if (stack->len < 3)
				goto out;
			BIGNUM bn1, bn2, bn3;
			BN_init(&bn1);
			BN_init(&bn2);
			BN_init(&bn3);
			bool rc1 = CastToBigNum(&bn1, stacktop(stack, -3));
			bool rc2 = CastToBigNum(&bn2, stacktop(stack, -2));
			bool rc3 = CastToBigNum(&bn3, stacktop(stack, -1));
			bool fValue = (BN_cmp(&bn2, &bn1) <= 0 &&
				       BN_cmp(&bn1, &bn3) < 0);
			popstack(stack);
			popstack(stack);
			popstack(stack);
			stack_push_char(stack, fValue ? 1 : 0);
			BN_clear_free(&bn1);
			BN_clear_free(&bn2);
			BN_clear_free(&bn3);
			if (!rc1 || !rc2 || !rc3)
				goto out;
			break;
		}

		//
		// Crypto
		//
		case OP_RIPEMD160:
		case OP_SHA1:
		case OP_SHA256:
		case OP_HASH160:
		case OP_HASH256: {
			// (in -- hash)
			if (stack->len < 1)
				goto out;
			struct buffer *vch = stacktop(stack, -1);
			unsigned int hashlen;
			unsigned char md[32];

			switch (opcode) {
			case OP_RIPEMD160:
				hashlen = 20;
				RIPEMD160(vch->p, vch->len, md);
				break;
			case OP_SHA1:
				hashlen = 20;
				SHA1(vch->p, vch->len, md);
				break;
			case OP_SHA256:
				hashlen = 32;
				SHA256(vch->p, vch->len, md);
				break;
			case OP_HASH160:
				hashlen = 20;
				bu_Hash160(md, vch->p, vch->len);
				break;
			case OP_HASH256:
				hashlen = 32;
				bu_Hash(md, vch->p, vch->len);
				break;
			default:
				// impossible
				goto out;
			}

			popstack(stack);
			struct buffer buf = { md, hashlen };
			stack_push(stack, &buf);
			break;
		}

		case OP_CODESEPARATOR:
			// Hash starts after the code separator
			memcpy(&pbegincodehash, &pc, sizeof(pc));
			break;

		case OP_CHECKSIG:
		case OP_CHECKSIGVERIFY: {
			// (sig pubkey -- bool)
			if (stack->len < 2)
				goto out;

			struct buffer *vchSig	= stacktop(stack, -2);
			struct buffer *vchPubKey = stacktop(stack, -1);

			////// debug print
			//PrintHex(vchSig.begin(), vchSig.end(), "sig: %s\n");
			//PrintHex(vchPubKey.begin(), vchPubKey.end(), "pubkey: %s\n");

			// Subset of script starting at the most recent codeseparator
			GString *scriptCode = g_string_sized_new(pbegincodehash.len);
			g_string_append_len(scriptCode,
					    pbegincodehash.p,
					    pbegincodehash.len);

			// Drop the signature, since there's no way for
			// a signature to sign itself
			string_find_del(scriptCode, vchSig);

			bool fSuccess =
				(!fStrictEncodings ||
				 (IsCanonicalSignature(vchSig) &&
				  IsCanonicalPubKey(vchPubKey)));
			if (fSuccess)
				fSuccess = bp_checksig(vchSig, vchPubKey,
						       scriptCode,
						       txTo, nIn, nHashType);

			g_string_free(scriptCode, TRUE);

			popstack(stack);
			popstack(stack);
			stack_push_char(stack, fSuccess ? 1 : 0);
			if (opcode == OP_CHECKSIGVERIFY)
			{
				if (fSuccess)
					popstack(stack);
				else
					goto out;
			}
			break;
		}

		case OP_CHECKMULTISIG:
		case OP_CHECKMULTISIGVERIFY: {
			// ([sig ...] num_of_signatures [pubkey ...] num_of_pubkeys -- bool)

			int i = 1;
			if ((int)stack->len < i)
				goto out;

			int nKeysCount = stackint(stack, -i);
			if (nKeysCount < 0 || nKeysCount > 20)
				goto out;
			nOpCount += nKeysCount;
			if (nOpCount > 201)
				goto out;
			int ikey = ++i;
			i += nKeysCount;
			if ((int)stack->len < i)
				goto out;

			int nSigsCount = stackint(stack, -i);
			if (nSigsCount < 0 || nSigsCount > nKeysCount)
				goto out;
			int isig = ++i;
			i += nSigsCount;
			if ((int)stack->len < i)
				goto out;

			// Subset of script starting at the most recent codeseparator
			GString *scriptCode = g_string_sized_new(pbegincodehash.len);
			g_string_append_len(scriptCode,
					    pbegincodehash.p,
					    pbegincodehash.len);

			// Drop the signatures, since there's no way for
			// a signature to sign itself
			int k;
			for (k = 0; k < nSigsCount; k++)
			{
				struct buffer *vchSig =stacktop(stack, -isig-k);
				string_find_del(scriptCode, vchSig);
			}

			bool fSuccess = true;
			while (fSuccess && nSigsCount > 0)
			{
				struct buffer *vchSig	= stacktop(stack, -isig);
				struct buffer *vchPubKey = stacktop(stack, -ikey);

				// Check signature
				bool fOk =
					(!fStrictEncodings ||
					 (IsCanonicalSignature(vchSig) &&
					  IsCanonicalPubKey(vchPubKey)));
				if (fOk)
					fOk = bp_checksig(vchSig, vchPubKey,
							  scriptCode, txTo, nIn,
							  nHashType);

				if (fOk) {
					isig++;
					nSigsCount--;
				}
				ikey++;
				nKeysCount--;

				// If there are more signatures left than keys left,
				// then too many signatures have failed
				if (nSigsCount > nKeysCount)
					fSuccess = false;
			}

			g_string_free(scriptCode, TRUE);

			while (i-- > 0)
				popstack(stack);
			stack_push_char(stack, fSuccess ? 1 : 0);

			if (opcode == OP_CHECKMULTISIGVERIFY)
			{
				if (fSuccess)
					popstack(stack);
				else
					goto out;
			}
			break;
		}

		default:
			goto out;
		}

		if (stack->len + altstack->len > 1000)
			goto out;
	}

	rc = (vfExec->len == 0 && bp.error == false);

out:
	BN_clear_free(&bn);
	g_ptr_array_free(altstack, TRUE);
	g_byte_array_unref(vfExec);
	return rc;
}