void wallet_free(struct wallet *wlt) { if (!wlt) return; cstr_free(wlt->def_acct, true); parr_free(wlt->keys, true); parr_free(wlt->hdmaster, true); parr_free(wlt->accounts, true); memset(wlt, 0, sizeof(*wlt)); }
void msg_addr_free(struct msg_addr *ma) { if (ma->addrs) { parr_free(ma->addrs, true); ma->addrs = NULL; } }
static void shutdown_nci(struct net_child_info *nci) { peerman_free(nci->peers); nc_conns_gc(nci, true); assert(nci->conns->len == 0); parr_free(nci->conns, true); event_base_free(nci->eb); }
void msg_vinv_free(struct msg_vinv *mv) { if (!mv) return; if (mv->invs) { parr_free(mv->invs, true); mv->invs = NULL; } }
void msg_headers_free(struct msg_headers *mh) { if (!mh) return; if (mh->headers) { unsigned int i; for (i = 0; i < mh->headers->len; i++) { struct bp_block *block; block = parr_idx(mh->headers, i); bp_block_free(block); } parr_free(mh->headers, true); mh->headers = NULL; } }
parr *bp_block_match(const struct bp_block *block, const struct bp_keyset *ks) { if (!block || !block->vtx || !ks) return NULL; parr *arr = parr_new(block->vtx->len, bbm_free); if (!arr) return NULL; BIGNUM tmp_mask; BN_init(&tmp_mask); unsigned int n; for (n = 0; n < block->vtx->len; n++) { struct bp_tx *tx; tx = parr_idx(block->vtx, n); if (!bp_tx_match_mask(&tmp_mask, tx, ks)) goto err_out; if (!BN_is_zero(&tmp_mask)) { struct bp_block_match *match; match = bbm_new(); match->n = n; BN_copy(&match->mask, &tmp_mask); parr_add(arr, match); } } BN_clear_free(&tmp_mask); return arr; err_out: BN_clear_free(&tmp_mask); parr_free(arr, true); return NULL; }
static void runtest(const char *json_base_fn, const char *ser_in_fn, const char *block_in_hash, const char *tx_in_hash) { /* read wallet data */ char *json_fn = test_filename(json_base_fn); json_t *wallet = read_json(json_fn); assert(wallet != NULL); /* read block data containing incoming payment */ char *fn = test_filename(ser_in_fn); void *data; size_t data_len; assert(bu_read_file(fn, &data, &data_len, 1 * 1024 * 1024) == true); struct bp_block block_in; bp_block_init(&block_in); struct const_buffer buf = { data, data_len }; assert(deser_bp_block(&block_in, &buf) == true); bp_block_calc_sha256(&block_in); /* verify block-in data matches expected block-in hash */ bu256_t check_hash; assert(hex_bu256(&check_hash, block_in_hash) == true); assert(bu256_equal(&block_in.sha256, &check_hash) == true); /* load key that has received an incoming payment */ struct bp_key key; assert(bp_key_init(&key) == true); load_json_key(wallet, &key); /* load key into keyset */ struct bp_keyset ks; bpks_init(&ks); assert(bpks_add(&ks, &key) == true); /* find key matches in block */ parr *matches; matches = bp_block_match(&block_in, &ks); assert(matches != NULL); assert(matches->len == 1); struct bp_block_match *match = parr_idx(matches, 0); assert(match->n == 1); /* match 2nd tx, index 1 */ /* get matching transaction */ struct bp_tx *tx = parr_idx(block_in.vtx, match->n); bp_tx_calc_sha256(tx); /* verify txid matches expected */ char tx_hexstr[BU256_STRSZ]; bu256_hex(tx_hexstr, &tx->sha256); assert(strcmp(tx_hexstr, tx_in_hash) == 0); /* verify mask matches 2nd txout (1 << 1) */ BIGNUM tmp_mask; BN_init(&tmp_mask); BN_one(&tmp_mask); BN_lshift(&tmp_mask, &tmp_mask, 1); assert(BN_cmp(&tmp_mask, &match->mask) == 0); /* build merkle tree, tx's branch */ parr *mtree = bp_block_merkle_tree(&block_in); assert(mtree != NULL); parr *mbranch = bp_block_merkle_branch(&block_in, mtree, match->n); assert(mbranch != NULL); /* verify merkle branch for tx matches expected */ bu256_t mrk_check; bp_check_merkle_branch(&mrk_check, &tx->sha256, mbranch, match->n); assert(bu256_equal(&mrk_check, &block_in.hashMerkleRoot) == true); /* release resources */ parr_free(mtree, true); parr_free(mbranch, true); BN_clear_free(&tmp_mask); parr_free(matches, true); bpks_free(&ks); bp_key_free(&key); bp_block_free(&block_in); json_decref(wallet); free(data); free(fn); free(json_fn); }
static void runtest(bool is_valid, const char *basefn) { char *fn = test_filename(basefn); json_t *tests = read_json(fn); assert(json_is_array(tests)); struct bp_hashtab *input_map = bp_hashtab_new_ext( input_hash, input_equal, free, input_value_free); comments = parr_new(8, free); unsigned int idx; for (idx = 0; idx < json_array_size(tests); idx++) { json_t *test = json_array_get(tests, idx); if (!json_is_array(json_array_get(test, 0))) { const char *cmt = json_string_value(json_array_get(test, 0)); if (cmt) parr_add(comments, strdup(cmt)); continue; /* comments */ } assert(json_is_array(test)); assert(json_array_size(test) == 3); assert(json_is_string(json_array_get(test, 1))); assert(json_is_boolean(json_array_get(test, 2))); json_t *inputs = json_array_get(test, 0); assert(json_is_array(inputs)); bp_hashtab_clear(input_map); unsigned int i; for (i = 0; i < json_array_size(inputs); i++) { json_t *input = json_array_get(inputs, i); assert(json_is_array(input)); const char *prev_hashstr = json_string_value(json_array_get(input, 0)); int prev_n = json_integer_value(json_array_get(input, 1)); const char *prev_pubkey_enc = json_string_value(json_array_get(input, 2)); assert(prev_hashstr != NULL); assert(json_is_integer(json_array_get(input, 1))); assert(prev_pubkey_enc != NULL); struct bp_outpt *outpt; outpt = malloc(sizeof(*outpt)); hex_bu256(&outpt->hash, prev_hashstr); outpt->n = prev_n; cstring *script = parse_script_str(prev_pubkey_enc); assert(script != NULL); bp_hashtab_put(input_map, outpt, script); } const char *tx_hexser = json_string_value(json_array_get(test, 1)); assert(tx_hexser != NULL); bool enforce_p2sh = json_is_true(json_array_get(test, 2)); cstring *tx_ser = hex2str(tx_hexser); assert(tx_ser != NULL); test_tx_valid(is_valid, input_map, tx_ser, enforce_p2sh); cstr_free(tx_ser, true); if (comments->len > 0) { parr_free(comments, true); comments = parr_new(8, free); } } parr_free(comments, true); comments = NULL; bp_hashtab_unref(input_map); json_decref(tests); free(fn); }
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; }
bool bp_script_verify(const cstring *scriptSig, const cstring *scriptPubKey, const struct bp_tx *txTo, unsigned int nIn, unsigned int flags, int nHashType) { bool rc = false; parr *stack = parr_new(0, buffer_freep); parr *stackCopy = NULL; struct const_buffer sigbuf = { scriptSig->str, scriptSig->len }; if ((flags & SCRIPT_VERIFY_SIGPUSHONLY) != 0 && !is_bsp_pushonly(&sigbuf)) goto out; if (!bp_script_eval(stack, scriptSig, txTo, nIn, flags, nHashType)) goto out; if (flags & SCRIPT_VERIFY_P2SH) { stackCopy = parr_new(stack->len, buffer_freep); stack_copy(stackCopy, stack); } if (!bp_script_eval(stack, scriptPubKey, txTo, nIn, flags, nHashType)) goto out; if (stack->len == 0) goto out; if (CastToBool(stacktop(stack, -1)) == false) goto out; if ((flags & SCRIPT_VERIFY_P2SH) && is_bsp_p2sh_str(scriptPubKey)) { // scriptSig must be literals-only or validation fails if (!is_bsp_pushonly(&sigbuf)) goto out; // stack cannot be empty here, because if it was the // P2SH HASH <> EQUAL scriptPubKey would be evaluated with // an empty stack and the script_eval above would return false. if (stackCopy->len < 1) goto out; struct buffer *pubKeySerialized = stack_take(stackCopy, -1); popstack(stackCopy); cstring *pubkey2 = cstr_new_buf(pubKeySerialized->p, pubKeySerialized->len); buffer_freep(pubKeySerialized); bool rc2 = bp_script_eval(stackCopy, pubkey2, txTo, nIn, flags, nHashType); cstr_free(pubkey2, true); if (!rc2) goto out; if (stackCopy->len == 0) goto out; if (CastToBool(stacktop(stackCopy, -1)) == false) goto out; } // The CLEANSTACK check is only performed after potential P2SH evaluation, // as the non-P2SH evaluation of a P2SH script will obviously not result in // a clean stack (the P2SH inputs remain). if ((flags & SCRIPT_VERIFY_CLEANSTACK) != 0) { // Disallow CLEANSTACK without P2SH, as otherwise a switch CLEANSTACK->P2SH+CLEANSTACK // would be possible, which is not a softfork (and P2SH should be one). assert((flags & SCRIPT_VERIFY_P2SH) != 0); if (stackCopy->len != 1) goto out; } rc = true; out: parr_free(stack, true); if (stackCopy) parr_free(stackCopy, true); return rc; }
/* markdown • parses the input buffer and renders it into the output buffer */ void markdown(struct buf *ob, struct buf *ib, const struct mkd_renderer *rndrer) { struct link_ref *lr; struct buf *text = bufnew(TEXT_UNIT); size_t i, beg, end; struct render rndr; /* filling the render structure */ if (!rndrer) return; rndr.make = *rndrer; if (rndr.make.max_work_stack < 1) rndr.make.max_work_stack = 1; arr_init(&rndr.refs, sizeof (struct link_ref)); parr_init(&rndr.work); for (i = 0; i < 256; i += 1) rndr.active_char[i] = 0; if ((rndr.make.emphasis || rndr.make.double_emphasis || rndr.make.triple_emphasis) && rndr.make.emph_chars) for (i = 0; rndr.make.emph_chars[i]; i += 1) rndr.active_char[(unsigned char)rndr.make.emph_chars[i]] = char_emphasis; if (rndr.make.codespan) rndr.active_char['`'] = char_codespan; if (rndr.make.linebreak) rndr.active_char['\n'] = char_linebreak; if (rndr.make.image || rndr.make.link) rndr.active_char['['] = char_link; rndr.active_char['<'] = char_langle_tag; rndr.active_char['\\'] = char_escape; rndr.active_char['&'] = char_entity; /* first pass: looking for references, copying everything else */ beg = 0; while (beg < ib->size) /* iterating over lines */ if (is_ref(ib->data, beg, ib->size, &end, &rndr.refs)) beg = end; else { /* skipping to the next line */ end = beg; while (end < ib->size && ib->data[end] != '\n' && ib->data[end] != '\r') end += 1; /* adding the line body if present */ if (end > beg) bufput(text, ib->data + beg, end - beg); while (end < ib->size && (ib->data[end] == '\n' || ib->data[end] == '\r')) { /* add one \n per newline */ if (ib->data[end] == '\n' || (end + 1 < ib->size && ib->data[end + 1] != '\n')) bufputc(text, '\n'); end += 1; } beg = end; } /* sorting the reference array */ if (rndr.refs.size) qsort(rndr.refs.base, rndr.refs.size, rndr.refs.unit, cmp_link_ref_sort); /* adding a final newline if not already present */ if (text->size && text->data[text->size - 1] != '\n' && text->data[text->size - 1] != '\r') bufputc(text, '\n'); /* second pass: actual rendering */ if (rndr.make.prolog) rndr.make.prolog(ob, rndr.make.opaque); parse_block(ob, &rndr, text->data, text->size); if (rndr.make.epilog) rndr.make.epilog(ob, rndr.make.opaque); /* clean-up */ bufrelease(text); lr = rndr.refs.base; for (i = 0; i < rndr.refs.size; i += 1) { bufrelease(lr[i].id); bufrelease(lr[i].link); bufrelease(lr[i].title); } arr_free(&rndr.refs); assert(rndr.work.size == 0); for (i = 0; i < rndr.work.asize; i += 1) bufrelease(rndr.work.item[i]); parr_free(&rndr.work); }
/* markdown • parses the input buffer and renders it into the output buffer */ void ups_markdown(struct buf *ob, struct buf *ib, const struct mkd_renderer *rndrer, unsigned int extensions) { struct link_ref *lr; struct buf *text; size_t i, beg, end; struct render rndr; /* filling the render structure */ if (!rndrer) return; text = bufnew(TEXT_UNIT); if (!text) return; rndr.make = *rndrer; arr_init(&rndr.refs, sizeof (struct link_ref)); parr_init(&rndr.work); for (i = 0; i < 256; i += 1) rndr.active_char[i] = 0; if (rndr.make.emphasis || rndr.make.double_emphasis || rndr.make.triple_emphasis) { rndr.active_char['*'] = char_emphasis; rndr.active_char['_'] = char_emphasis; if (extensions & MKDEXT_STRIKETHROUGH) rndr.active_char['~'] = char_emphasis; } if (rndr.make.codespan) rndr.active_char['`'] = char_codespan; if (rndr.make.linebreak) rndr.active_char['\n'] = char_linebreak; if (rndr.make.image || rndr.make.link) rndr.active_char['['] = char_link; rndr.active_char['<'] = char_langle_tag; rndr.active_char['\\'] = char_escape; rndr.active_char['&'] = char_entity; if (extensions & MKDEXT_AUTOLINK) { rndr.active_char['h'] = char_autolink; // http, https rndr.active_char['H'] = char_autolink; rndr.active_char['f'] = char_autolink; // ftp rndr.active_char['F'] = char_autolink; rndr.active_char['m'] = char_autolink; // mailto rndr.active_char['M'] = char_autolink; } /* Extension data */ rndr.ext_flags = extensions; rndr.max_nesting = 16; /* first pass: looking for references, copying everything else */ beg = 0; while (beg < ib->size) /* iterating over lines */ if (is_ref(ib->data, beg, ib->size, &end, &rndr.refs)) beg = end; else { /* skipping to the next line */ end = beg; while (end < ib->size && ib->data[end] != '\n' && ib->data[end] != '\r') end += 1; /* adding the line body if present */ if (end > beg) expand_tabs(text, ib->data + beg, end - beg); while (end < ib->size && (ib->data[end] == '\n' || ib->data[end] == '\r')) { /* add one \n per newline */ if (ib->data[end] == '\n' || (end + 1 < ib->size && ib->data[end + 1] != '\n')) bufputc(text, '\n'); end += 1; } beg = end; } /* sorting the reference array */ if (rndr.refs.size) qsort(rndr.refs.base, rndr.refs.size, rndr.refs.unit, cmp_link_ref_sort); /* adding a final newline if not already present */ if (!text->size) goto cleanup; if (text->data[text->size - 1] != '\n' && text->data[text->size - 1] != '\r') bufputc(text, '\n'); /* second pass: actual rendering */ if (rndr.make.doc_header) rndr.make.doc_header(ob, rndr.make.opaque); parse_block(ob, &rndr, text->data, text->size); if (rndr.make.doc_footer) rndr.make.doc_footer(ob, rndr.make.opaque); /* clean-up */ cleanup: bufrelease(text); lr = rndr.refs.base; for (i = 0; i < (size_t)rndr.refs.size; i += 1) { bufrelease(lr[i].id); bufrelease(lr[i].link); bufrelease(lr[i].title); } arr_free(&rndr.refs); assert(rndr.work.size == 0); for (i = 0; i < (size_t)rndr.work.asize; i += 1) bufrelease(rndr.work.item[i]); parr_free(&rndr.work); }