コード例 #1
0
ファイル: logdb_tests.c プロジェクト: libbtc/libbtc
void test_examples()
{
    cstring *value;
    cstring *key;
    logdb_log_db *db;
    enum logdb_error error = 0;
    int create_database = 1;

    /* create a new database object */
    db = logdb_new();

    /* create a database file (will overwrite existing) */
    logdb_load(db, "/tmp/test.logdb", create_database, &error);

    /* append a record */
    value = cstr_new("testkey");
    key = cstr_new("somevalue");
    /* second parameter NULL means we don't use a transaction */
    logdb_append(db, NULL, key, value);

    /* flush database (write its state to disk) */
    logdb_flush(db);

    /* cleanup */
    logdb_free(db);
    cstr_free(key, true);
    cstr_free(value, true);
}
コード例 #2
0
ファイル: cstring.c プロジェクト: bouhssini/vectorC
void cstr_split_l(cstring*c,char*data,int size,vector *v)
{
    int n,len=0;
    cstring *st,*tx;

    tx = cstr_new();
    cstr_set_c(tx,c);

    while(1){
        n = cstr_find_l(tx,0,data,size);
/*
         if (n == 0){
            len = n+size;
            cstr_set_l(tx,tx->data+len,tx->size-len);
            continue;
        }else if (n > -1){
*/
        if (n > -1){
            st = cstr_new();
            cstr_add_l(st,tx->data,n);
            vector_add(v,st);
            len = n+size;
            cstr_set_l(tx,tx->data+len,tx->size-len);
        }else{
            st = cstr_new();
            cstr_add_l(st,tx->data,tx->size);
            vector_add(v,st);
            break;
        }
    }
    cstr_free(tx);
}
コード例 #3
0
ファイル: srp.c プロジェクト: shossain035/srp-2.1.2
SRP_new(SRP_METHOD * meth)
{
  SRP * srp = (SRP *) malloc(sizeof(SRP));

  if(srp == NULL)
    return NULL;

  srp->flags = 0;
  srp->username = cstr_new();
  srp->bctx = BigIntegerCtxNew();
  srp->modulus = NULL;
  srp->accel = NULL;
  srp->generator = NULL;
  srp->salt = NULL;
  srp->verifier = NULL;
  srp->password = NULL;
  srp->pubkey = NULL;
  srp->secret = NULL;
  srp->u = NULL;
  srp->key = NULL;
  srp->ex_data = cstr_new();
  srp->param_cb = NULL;
  srp->meth = meth;
  srp->meth_data = NULL;
  srp->slu = NULL;
  if(srp->meth->init == NULL || (*srp->meth->init)(srp) == SRP_SUCCESS)
    return srp;
  free(srp);
  return NULL;
}
コード例 #4
0
ファイル: socket_libevent.c プロジェクト: wuhong1986/cutils
static void cb_conn_read_cli(struct bufferevent *bev, void *user_data)
{
    void*    buffer  = NULL;
    uint32_t buf_len = 0;
    struct evbuffer *buf_in = bufferevent_get_input(bev);
    cli_cmd_t cmd;
    cstr *json = cstr_new();

    cli_cmd_init(&cmd);

    /* bufferevent_lock(bev); */

    /* read data frome buffer in */
    buf_len = evbuffer_get_length(buf_in);
    buffer = calloc(1, buf_len);
    bufferevent_read(bev, buffer, buf_len);

    /* bufferevent_unlock(bev); */

    log_dbg("recv command: %s", (char*)buffer);
    cli_parse(&cmd, (char*)buffer);
    cli_cmd_to_json(&cmd, json);
    log_dbg("cli response: %s", cstr_body(json));

    bufferevent_send(bev, cstr_body(json), cstr_len(json) + 1);

    /* put data to addr recv buffer, and translate to command format */
    cli_cmd_release(&cmd);
    cstr_free(json);
    free(buffer);
    /* bufferevent_free(bev); */
}
コード例 #5
0
ファイル: bignum.c プロジェクト: libbitc/libbitc
cstring *bn_getvch(const mpz_t v)
{
	/* get MPI format size */
	size_t sz;
	char *buf = mpz_export(NULL, &sz, -1, 1, 1, 0, v);

	if (sz == 0) {
		free(buf);
		return cstr_new(NULL);
	}

	cstring *s_le = cstr_new_buf(buf, sz);
	free(buf);

	/* check if sign bit is available */
	unsigned int msb = mpz_sizeinbase(v, 2);
	if ((!(msb & 0x07) && msb > 0)) {
		cstr_append_c(s_le, '\0');
		sz++;
	}
	cstr_resize(s_le, sz);

	/* set sign bit */
	if (mpz_sgn(v) == -1) {
		s_le->str[sz - 1] = (s_le->str[sz - 1] & 0xff) | 0x80;
	}

	return s_le;
}
コード例 #6
0
ファイル: srp.c プロジェクト: shossain035/srp-2.1.2
SRP_set_params(SRP * srp, const unsigned char * modulus, int modlen,
	       const unsigned char * generator, int genlen,
	       const unsigned char * salt, int saltlen)
{
  SRP_RESULT rc;

  if(modulus == NULL || generator == NULL || salt == NULL)
    return SRP_ERROR;

  /* Set fields in SRP context */
  srp->modulus = BigIntegerFromBytes(modulus, modlen);
  if(srp->flags & SRP_FLAG_MOD_ACCEL)
    srp->accel = BigIntegerModAccelNew(srp->modulus, srp->bctx);
  srp->generator = BigIntegerFromBytes(generator, genlen);
  if(srp->salt == NULL)
    srp->salt = cstr_new();
  cstr_setn(srp->salt, salt, saltlen);

  /* Now attempt to validate parameters */
  if(BigIntegerBitLen(srp->modulus) < SRP_get_modulus_min_bits())
    return SRP_ERROR;

  if(srp->param_cb) {
    rc = (*srp->param_cb)(srp, modulus, modlen, generator, genlen);
    if(!SRP_OK(rc))
      return rc;
  }

  return (*srp->meth->params)(srp, modulus, modlen, generator, genlen,
			      salt, saltlen);
}
コード例 #7
0
ファイル: message.c プロジェクト: haraldh/picocoin
cstring *ser_msg_ping(unsigned int protover, const struct msg_ping *mp)
{
	cstring *s = cstr_new(NULL);

	if (mp && (protover > BIP0031_VERSION))
		ser_u64(s, mp->nonce);

	return s;
}
コード例 #8
0
ファイル: cstr.c プロジェクト: Joinhack/json_parser
cstr cstr_ncat(cstr s, const char *b, size_t l) {
  cstrhdr *csh;
  if(s == NULL)
    return cstr_new(b, l);
  s = cstr_extend(s, l);
  csh = CSTR_HDR(s);
  memcpy(csh->buf + CSH_USED(csh), b, l);
  csh->len += l;
  s[CSTR_HDR_USED(csh)] = '\0';
  return (cstr)csh->buf;
}
コード例 #9
0
ファイル: cstr.c プロジェクト: Joinhack/json_parser
cstr* cstr_split(char *s, size_t len, const char *b, size_t slen, size_t *l) {
  setting *setting = get_setting();
  cstr *array = NULL;
  size_t i, cap = 0, size = 0, beg = 0;
  array = setting->malloc(sizeof(cstr)*cap);
  for(i = 0; i < len - (slen - 1); i++) {
    if(size + 1 >= cap ) {
      cap += 5;
      array = setting->realloc(array, sizeof(cstr)*cap);
    }
    if(s[i] == b[0] && memcmp(s + i, b, slen) == 0) {
      array[size] = cstr_new(s + beg, i - beg);
      beg = i + slen;
      size++;
    }
  }
  array[size++] = cstr_new(s + beg, len - beg);
  *l = size;
  return array;
}
コード例 #10
0
ファイル: db_test.c プロジェクト: Joinhack/chat
int main() {
	size_t i;
	obj *op;
	db* db = db_create(16);
	cstr k = cstr_new("1", 1);	
	obj* obj = cstr_obj_create("2");
	db_set(db, 0, k, obj);
	for(i = 0; i < 1; i++) {
		op = db_get(db, 0, k);
		obj_decr(op);
		op = db_get(db, 0, k);
		obj_decr(op);
		op = db_get(db, 0, k);
		obj_decr(op);
	}
	return 0;
}
コード例 #11
0
ファイル: cstring.c プロジェクト: bouhssini/vectorC
int cstr_split_count_l(cstring*c,char*data,int size)
{
    int n,len=0,cnt=0;
    cstring *tx;

    tx = cstr_new();
    cstr_set_c(tx,c);

    while(1){
        cnt++;
        n = cstr_find_l(tx,0,data,size);
        if (n > -1){
            len = n+size;
            cstr_set_l(tx,tx->data+len,tx->size-len);
        }else
            break;
    }
    cstr_free(tx);
    return cnt;
}
コード例 #12
0
ファイル: message.c プロジェクト: haraldh/picocoin
cstring *ser_msg_addr(unsigned int protover, const struct msg_addr *ma)
{
	cstring *s = cstr_new(NULL);

	if (!ma || !ma->addrs || !ma->addrs->len) {
		ser_varlen(s, 0);
		return s;
	}

	ser_varlen(s, ma->addrs->len);

	unsigned int i;
	for (i = 0; i < ma->addrs->len; i++) {
		struct bp_address *addr;

		addr = parr_idx(ma->addrs, i);

		ser_bp_addr(s, protover, addr);
	}

	return s;
}
コード例 #13
0
ファイル: message.c プロジェクト: haraldh/picocoin
cstring *ser_msg_vinv(const struct msg_vinv *mv)
{
	cstring *s = cstr_new(NULL);

	if (!mv || !mv->invs || !mv->invs->len) {
		ser_varlen(s, 0);
		return s;
	}

	ser_varlen(s, mv->invs->len);

	unsigned int i;
	for (i = 0; i < mv->invs->len; i++) {
		struct bp_inv *inv;

		inv = parr_idx(mv->invs, i);

		ser_bp_inv(s, inv);
	}

	return s;
}
コード例 #14
0
ファイル: message.c プロジェクト: haraldh/picocoin
cstring *ser_msg_headers(const struct msg_headers *mh)
{
	cstring *s = cstr_new(NULL);

	if (!mh || !mh->headers || !mh->headers->len) {
		ser_varlen(s, 0);
		return s;
	}

	ser_varlen(s, mh->headers->len);

	unsigned int i;
	for (i = 0; i < mh->headers->len; i++) {
		struct bp_block *block;

		block = parr_idx(mh->headers, i);

		ser_bp_block(s, block);
	}

	return s;
}
コード例 #15
0
ファイル: libtcc.c プロジェクト: aronsky/radare2
static void tcc_split_path(TCCState *s, void ***p_ary, int *p_nb_ary, const char *in)
{
	const char *p;
	do {
		int c;
		CString str;

		cstr_new (&str);
		for (p = in; c = *p, c != '\0' && c != PATHSEP; ++p) {
			if (c == '{' && p[1] && p[2] == '}') {
				c = p[1], p += 2;
				if (c == 'B') {
					cstr_cat (&str, s->tcc_lib_path);
				}
			} else {
				cstr_ccat (&str, c);
			}
		}
		cstr_ccat (&str, '\0');
		dynarray_add (p_ary, p_nb_ary, str.data);
		in = p + 1;
	} while (*p);
}
コード例 #16
0
ファイル: rawtx.c プロジェクト: favioflamingo/picocoin
static void append_input(char *txid_str, char *vout_str)
{
	bu256_t txid;
	if (!hex_bu256(&txid, txid_str)) {
		fprintf(stderr, "invalid txid hex\n");
		exit(1);
	}

	unsigned int vout = atoi(vout_str);

	struct bp_txin *txin = calloc(1, sizeof(struct bp_txin));
	if (!txin) {
		fprintf(stderr, "OOM\n");
		exit(1);
	}
	bp_txin_init(txin);

	bu256_copy(&txin->prevout.hash, &txid);
	txin->prevout.n = vout;
	txin->scriptSig = cstr_new(NULL);
	txin->nSequence = SEQUENCE_FINAL;

	parr_add(tx.vin, txin);
}
コード例 #17
0
ファイル: cstr_tests.c プロジェクト: libbtc/libbtc
void test_cstr()
{
    cstring* s1 = cstr_new("foo");
    cstring* s2 = cstr_new("foo");
    cstring* s3 = cstr_new("bar");
    cstring* s4 = cstr_new("bar1");
    
    cstring* s = cstr_new("foo");
    assert(s != NULL);
    assert(s->len == 3);
    assert(strcmp(s->str, "foo") == 0);

    cstr_free(s, true);

    s = cstr_new_sz(200);
    assert(s != NULL);
    assert(s->alloc > 200);
    assert(s->len == 0);

    cstr_free(s, true);

    s = cstr_new_buf("foo", 2);
    assert(s != NULL);
    assert(s->len == 2);
    assert(strcmp(s->str, "fo") == 0);

    cstr_free(s, true);

    s = cstr_new(NULL);
    assert(s != NULL);
    cstr_append_buf(s, "f", 1);
    cstr_append_buf(s, "o", 1);
    cstr_append_buf(s, "o", 1);
    assert(s->len == 3);
    assert(strcmp(s->str, "foo") == 0);

    cstr_free(s, true);

    s = cstr_new("foo");
    assert(s != NULL);

    cstr_resize(s, 2);
    cstr_resize(s, 2);
    cstr_alloc_minsize(s, 2);
    cstr_alloc_minsize(s, 1);

    assert(s->len == 2);
    assert(strcmp(s->str, "fo") == 0);

    cstr_resize(s, 4);
    assert(s->len == 4);
    assert(s->alloc > 4);
    memcpy(s->str, "food", 4);
    assert(strcmp(s->str, "food") == 0);

    cstr_free(s, true);

    assert(cstr_compare(s1, s2) ==  0);
    assert(cstr_compare(s1, s3) ==  1);
    assert(cstr_compare(s3, s1) ==  -1);
    assert(cstr_compare(s3, s4) ==  -1);
    assert(cstr_compare(s4, s3) ==  1);

    assert(cstr_equal(s1, s2) == true);
    assert(cstr_equal(s1, s3) == false);
    assert(cstr_equal(s1, NULL) == false);
    assert(cstr_equal(s2, s3) == false);
    assert(cstr_equal(s3, s3) == true);
    assert(cstr_equal(s3, s4) == false);
    cstr_erase(s4, 0, 3);
    cstr_erase(s4, 110, 3);
    cstr_erase(s4, s4->len, 0);
    cstr_erase(s4, 0, 100);
    assert(strcmp(s4->str, "1") == 0);

    cstr_free(s1, true);
    cstr_free(s2, true);
    cstr_free(s3, true);
    cstr_free(s4, true);
}
コード例 #18
0
ファイル: char.c プロジェクト: gitpan/C-sparse
/* free string and reset it to NULL */
void cstr_free(SCTX_ CString *cstr)
{
	free(cstr->data_allocated);
	cstr_new(sctx_ cstr);
}
コード例 #19
0
ファイル: util.c プロジェクト: HarryR/sanos
void cstr_free(CString *cstr) {
  tcc_free(cstr->data_allocated);
  cstr_new(cstr);
}
コード例 #20
0
ファイル: test_main.c プロジェクト: wuhong1986/cutils
int main(int argc, char *argv[])
{
    cli_init();
    log_init();
    thread_init();
    cmd_init();
    dev_addr_mgr_init();
    addr_sock_init();
    socket_init();
    dev_router_init();
    cli_commands_init();

    broadcast_msg_t msg;
    dev_addr_mgr_add_support_dev_type(1);
    dev_addr_mgr_set_network_type(NETWORK_TYPE_CENTER);
    dev_addr_mgr_set_addr_mac(0x89ABCDE0);

    dev_router_set_mac_local(0x89ABCDE0);

#ifdef TEST_CLIENT
    ex_memzero_one(&msg);
    strcpy(msg.dev_name, "1004");
    msg.router_list_lens[0] = 3;
    msg.router_list_lens[1] = 3;
    msg.router_list_lens[2] = 0;
    msg.router_list_lens[3] = 0;
    msg.router_cnt = 7;
    msg.router_list_cnt = 2;
    msg.network_nodes[0].dev_type = 1;
    msg.network_nodes[0].addr_mac = 0x89ABCDE0;
    msg.network_nodes[0].subnet_cnt = 4;
    msg.network_nodes[0].network_type = NETWORK_TYPE_ROUTER;
    strcpy(msg.network_nodes[0].dev_name, "1004");

    msg.network_nodes[1].dev_type = 1;
    msg.network_nodes[1].addr_mac = 0x89ABCDE1;
    msg.network_nodes[1].subnet_cnt = 4;
    msg.network_nodes[1].network_type = NETWORK_TYPE_ROUTER;
    strcpy(msg.network_nodes[1].dev_name, "1003");

    msg.network_nodes[2].dev_type = 1;
    msg.network_nodes[2].addr_mac = 0x89ABCDE2;
    msg.network_nodes[2].subnet_cnt = 4;
    msg.network_nodes[2].network_type = NETWORK_TYPE_ROUTER;
    strcpy(msg.network_nodes[2].dev_name, "1002");

    msg.network_nodes[3].dev_type = 1;
    msg.network_nodes[3].addr_mac = 0x89ABCDE3;
    msg.network_nodes[3].subnet_cnt = 4;
    msg.network_nodes[3].network_type = NETWORK_TYPE_ROUTER;
    strcpy(msg.network_nodes[3].dev_name, "1001");

    msg.network_nodes[4].dev_type = 1;
    msg.network_nodes[4].addr_mac = 0x89ABCDE4;
    msg.network_nodes[4].subnet_cnt = 4;
    msg.network_nodes[4].network_type = NETWORK_TYPE_ROUTER;
    strcpy(msg.network_nodes[4].dev_name, "1005");

    msg.network_nodes[5].dev_type = 1;
    msg.network_nodes[5].addr_mac = 0x89ABCDE5;
    msg.network_nodes[5].subnet_cnt = 4;
    msg.network_nodes[5].network_type = NETWORK_TYPE_ROUTER;
    strcpy(msg.network_nodes[5].dev_name, "1006");

    msg.network_nodes[6].dev_type = 1;
    msg.network_nodes[6].addr_mac = 0x89ABCDE6;
    msg.network_nodes[6].subnet_cnt = 4;
    msg.network_nodes[6].network_type = NETWORK_TYPE_ROUTER;
    strcpy(msg.network_nodes[6].dev_name, "1007");
#endif

    socket_listen_async(50002);
    socket_listen_cli(49999);
    socket_recv_start();
#ifdef TEST_SERVER
    socket_bc_tx_start("test", 50000, 50001, 50002);
#endif
#ifdef TEST_CLIENT
    socket_bc_rx_start("test", 50000, 50001, &msg);
#endif

#if 0
    cstr *json = cstr_new();
    int fd = 0;
    fd = socket_cli_send_request("127.0.0.1", 49999, "test");
    socket_cli_recv_response(fd, json);
    cstr_clear(json);
    fd = socket_cli_send_request("127.0.0.1", 49999, "test f**k");
    socket_cli_recv_response(fd, json);
    cstr_clear(json);
    fd = socket_cli_send_request("127.0.0.1", 49999, "f**k f**k f**k you test f**k");
    socket_cli_recv_response(fd, json);
    cstr_free(json);
#endif

    cli_loop();

    dev_addr_mgr_release();
    socket_release();
    cmd_release();
    thread_release();
    log_release();
    cli_release();
#if 0
  cli_cmd_t cmd;
  int i = 0;

  command_init(&cmd, argv[0], "0.0.1");
  command_option(&cmd, "-v", "--verbose", "enable verbose stuff", verbose);
  command_option(&cmd, "-r", "--required <arg>", "required arg", required);
  command_option(&cmd, "-o", "--optional [arg]", "optional arg", optional);
  command_parse(&cmd, argc, argv);
  printf("additional args:\n");
  for (i = 0; i < cmd.argc; ++i) {
    printf("  - '%s'\n", cmd.argv[i]);
  }
  command_free(&cmd);
#endif
    return 0;
}
コード例 #21
0
ファイル: script_eval.c プロジェクト: aido/picocoin
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;
}
コード例 #22
0
ファイル: logdb_tests.c プロジェクト: libbtc/libbtc
void test_logdb(logdb_log_db* (*new_func)())
{
    logdb_log_db *db;
    enum logdb_error error = 0;
    cstring *key;// key= {"key0", 4};
    cstring *value;// = {"val0", 4};
    cstring *key1;
    cstring *value1;
    cstring *outtest;
    cstring *value_test;
    unsigned char testbin[4] = {0x00, 0x10, 0x20, 0x30};
    cstring *value0;// = {"dumb", 4};
    cstring *key2;// = {"pkey", 4};
    cstring *value2;
    cstring *smp_value;
    cstring *smp_key;
    uint8_t txbin[10240];
    uint8_t txbin_rev[10240];
    char hexrev[98];
    int outlenrev;
    long fsize;
    char *buf;
    char *wrk_buf;
    FILE *f;
    unsigned int i;
    char bufs[300][65];
    rb_red_blk_node *nodetest;
    unsigned int cnt = 0;
    logdb_record* rec;

    key = cstr_new("key0");
    value = cstr_new("val0");

    value0 = cstr_new("dumb");
    value1 = cstr_new_sz(10);
    value2 = cstr_new_sz(10);
    key1 = cstr_new_sz(10);
    key2 = cstr_new("key2");

    cstr_append_buf(value2, testbin, sizeof(testbin));
    cstr_append_buf(value2, testbin, sizeof(testbin));
    cstr_append_buf(key1, key1str, strlen(key1str));
    cstr_append_buf(value1, value1str, strlen(value1str));

    unlink(dbtmpfile);
    db = new_func();
    u_assert_int_eq(logdb_load(db, "file_that_should_not_exists.dat", false, NULL), false);
    u_assert_int_eq(logdb_load(db, dbtmpfile, true, NULL), true);

    logdb_append(db, NULL, key, value);
    logdb_append(db, NULL, key1, value1);

    u_assert_int_eq(logdb_cache_size(db), 2);
    outtest = logdb_find_cache(db, key1);
    u_assert_int_eq(strcmp(outtest->str, value1str),0);
    logdb_flush(db);
    logdb_free(db);

    db = new_func();
    u_assert_int_eq(logdb_load(db, dbtmpfile, false, NULL), true);
    u_assert_int_eq(logdb_count_keys(db), 2);

    value_test = logdb_find(db, key1);
    u_assert_int_eq(strcmp(value_test->str, value1str), 0);
    value_test = logdb_find(db, key);
    u_assert_int_eq(memcmp(value_test->str, value->str, value->len), 0);
    logdb_free(db);

    db = new_func();
    u_assert_int_eq(logdb_load(db, dbtmpfile, false, NULL), true);



    logdb_append(db, NULL, key2, value2);
    logdb_flush(db);
    logdb_free(db);

    /* check if private key is available */
    db = new_func();
    u_assert_int_eq(logdb_load(db, dbtmpfile, false, NULL), true);

    value_test = logdb_find(db, key2);
    u_assert_int_eq(memcmp(value_test->str, value2->str, value2->len), 0);
    value_test = logdb_find(db, key);
    u_assert_int_eq(memcmp(value_test->str, value->str, value->len), 0);

    /* delete a record */
    logdb_delete(db, NULL, key2);
    logdb_flush(db);
    logdb_free(db);

    /* find and check the deleted record */
    db = new_func();
    u_assert_int_eq(logdb_load(db, dbtmpfile, false, NULL), true);

    value_test = logdb_find(db, key);
    u_assert_int_eq(memcmp(value_test->str, value->str, value->len), 0);

    value_test = logdb_find(db, key2);
    u_assert_int_eq((int)value_test, 0); /* should be null */

    /* overwrite a key */
    logdb_append(db, NULL, key, value0);

    value_test = logdb_find(db, key);
    u_assert_int_eq(memcmp(value_test->str, value0->str, value0->len), 0);

    logdb_flush(db);
    logdb_free(db);

    db = new_func();
    u_assert_int_eq(logdb_load(db, dbtmpfile, false, NULL), true);
    value_test = logdb_find(db, key);
    u_assert_int_eq(memcmp(value_test->str, value0->str, value0->len), 0);

    logdb_flush(db);
    logdb_free(db);




    /* simulate corruption */
    f = fopen(dbtmpfile, "rb");
    fseek(f, 0, SEEK_END);
    fsize = ftell(f);
    fseek(f, 0, SEEK_SET);

    buf = malloc(fsize + 1);
    fread(buf, fsize, 1, f);
    fclose(f);

    /* ---------------------------------------------------- */
    wrk_buf = safe_malloc(fsize + 1);
    memcpy(wrk_buf, buf, fsize);
    wrk_buf[0] = 0x88; /* wrong header */

    unlink(dbtmpfile);
    f = fopen(dbtmpfile, "wb");
    fwrite(wrk_buf, 1, fsize, f);
    fclose(f);

    db = new_func();
    u_assert_int_eq(logdb_load(db, dbtmpfile, false, &error), false);
    u_assert_int_eq(error, LOGDB_ERROR_WRONG_FILE_FORMAT);
    logdb_free(db);

    /* ---------------------------------------------------- */
    memcpy(wrk_buf, buf, fsize);
    wrk_buf[66] = 0x00; /* wrong checksum hash */

    unlink(dbtmpfile);
    f = fopen(dbtmpfile, "wb");
    fwrite(wrk_buf, 1, fsize, f);
    fclose(f);

    db = new_func();
    u_assert_int_eq(logdb_load(db, dbtmpfile, false, &error), false);
    u_assert_int_eq(error, LOGDB_ERROR_CHECKSUM);
    logdb_free(db);

    /* ---------------------------------------------------- */
    memcpy(wrk_buf, buf, fsize);
    wrk_buf[42] = 0xFF; /* wrong value length */

    unlink(dbtmpfile);
    f = fopen(dbtmpfile, "wb");
    fwrite(wrk_buf, 1, fsize, f);
    fclose(f);

    db = new_func();
    u_assert_int_eq(logdb_load(db, dbtmpfile, false, &error), false);
    u_assert_int_eq(error, LOGDB_ERROR_DATASTREAM_ERROR);
    logdb_free(db);

    free(buf);
    free(wrk_buf);


    /* --- large db test */
    unlink(dbtmpfile);

    db = new_func();
    u_assert_int_eq(logdb_load(db, dbtmpfile, true, NULL), true);

    smp_key = cstr_new_sz(100);
    smp_value = cstr_new_sz(100);
    for (i = 0; i < (sizeof(sampledata) / sizeof(sampledata[0])); i++) {
        const struct txtest *tx = &sampledata[i];

        uint8_t hashbin[sizeof(tx->txhash) / 2];
        int outlen = sizeof(tx->txhash) / 2;
        utils_hex_to_bin(tx->txhash, hashbin, strlen(tx->txhash), &outlen);

        cstr_erase(smp_key, 0, smp_key->len);
        cstr_append_buf(smp_key, hashbin, outlen);

        outlen = sizeof(tx->hextx) / 2;
        utils_hex_to_bin(tx->hextx, txbin, strlen(tx->hextx), &outlen);

        cstr_erase(smp_value, 0, smp_value->len);
        cstr_append_buf(smp_value, txbin, outlen);

        logdb_append(db, NULL, smp_key, smp_value);
    }

    u_assert_int_eq(logdb_count_keys(db), (sizeof(sampledata) / sizeof(sampledata[0])));

    /* check all records */
    for (i = 0; i < (sizeof(sampledata) / sizeof(sampledata[0])); i++) {
        const struct txtest *tx = &sampledata[i];

        uint8_t hashbin[sizeof(tx->txhash) / 2];
        int outlen = sizeof(tx->txhash) / 2;
        utils_hex_to_bin(tx->txhash, hashbin, strlen(tx->txhash), &outlen);

        cstr_erase(smp_key, 0, smp_key->len);
        cstr_append_buf(smp_key, hashbin, outlen);
        outtest = logdb_find(db, smp_key);

        outlen = sizeof(tx->hextx) / 2;
        utils_hex_to_bin(tx->hextx, txbin, strlen(tx->hextx), &outlen);

        u_assert_int_eq(outlen, outtest->len);
    }

    logdb_flush(db);
    logdb_free(db);

    db = new_func();
    error = 0;
    u_assert_int_eq(logdb_load(db, dbtmpfile, false, &error), true);
    u_assert_int_eq(logdb_count_keys(db), (sizeof(sampledata) / sizeof(sampledata[0])));

    /* check all records */
    for (i = 0; i < (sizeof(sampledata) / sizeof(sampledata[0])); i++) {
        const struct txtest *tx = &sampledata[i];

        uint8_t hashbin[sizeof(tx->txhash) / 2];
        int outlen = sizeof(tx->txhash) / 2;
        utils_hex_to_bin(tx->txhash, hashbin, strlen(tx->txhash), &outlen);

        memcpy(hexrev, tx->txhash, sizeof(tx->txhash));
        utils_reverse_hex(hexrev, strlen(tx->txhash));
        outlenrev = sizeof(tx->txhash) / 2;
        utils_hex_to_bin(hexrev, txbin_rev, strlen(hexrev), &outlenrev);

        cstr_erase(smp_key, 0, smp_key->len);
        cstr_append_buf(smp_key, hashbin, outlen);
        outtest = logdb_find(db, smp_key);

        outlen = strlen(tx->hextx) / 2;
        utils_hex_to_bin(tx->hextx, txbin, strlen(tx->hextx), &outlen);
        u_assert_int_eq(outlen, outtest->len);

        /*  hash transaction data and check hashes */
        if (strlen(tx->hextx) > 2)
        {
            uint8_t tx_hash_check[SHA256_DIGEST_LENGTH];
            sha256_Raw(txbin, outlen, tx_hash_check);
            sha256_Raw(tx_hash_check, 32, tx_hash_check);
            u_assert_int_eq(memcmp(tx_hash_check, txbin_rev, SHA256_DIGEST_LENGTH), 0);
        }

    }

    /* check all records */
    for (i = 0; i < (sizeof(sampledata) / sizeof(sampledata[0])); i++) {
        const struct txtest *tx = &sampledata[i];

        uint8_t hashbin[sizeof(tx->txhash) / 2];
        int outlen = sizeof(tx->txhash) / 2;
        utils_hex_to_bin(tx->txhash, hashbin, strlen(tx->txhash), &outlen);

        cstr_erase(smp_key, 0, smp_key->len);
        cstr_append_buf(smp_key, hashbin, outlen);
        logdb_delete(db, NULL, smp_key);
    }
    u_assert_int_eq(logdb_count_keys(db), 0);

    logdb_flush(db);
    logdb_free(db);

    db = new_func();
    error = 0;
    u_assert_int_eq(logdb_load(db, dbtmpfile, false, &error), true);
    u_assert_int_eq(error, LOGDB_SUCCESS);
    u_assert_int_eq(logdb_count_keys(db), 0);

    for (i = 0; i < (sizeof(sampledata) / sizeof(sampledata[0])); i++) {
        const struct txtest *tx = &sampledata[i];

        uint8_t hashbin[sizeof(tx->txhash) / 2];
        int outlen = sizeof(tx->txhash) / 2;
        utils_hex_to_bin(tx->txhash, hashbin, strlen(tx->txhash), &outlen);

        cstr_erase(smp_key, 0, smp_key->len);
        cstr_append_buf(smp_key, hashbin, outlen);

        outlen = sizeof(tx->hextx) / 2;
        utils_hex_to_bin(tx->hextx, txbin, strlen(tx->hextx), &outlen);

        cstr_erase(smp_value, 0, smp_value->len);
        cstr_append_buf(smp_value, txbin, outlen);

        logdb_append(db, NULL, smp_key, smp_value);
    }

    logdb_flush(db);
    logdb_free(db);

    db = new_func();
    error = 0;
    u_assert_int_eq(logdb_load(db, dbtmpfile, false, &error), true);
    u_assert_int_eq(error, LOGDB_SUCCESS);
    u_assert_int_eq(logdb_count_keys(db), (sizeof(sampledata) / sizeof(sampledata[0])));

    logdb_flush(db);
    logdb_free(db);

    db = new_func();
    error = 0;
    u_assert_int_eq(logdb_load(db, dbtmpfile, false, &error), true);
    u_assert_int_eq(error, LOGDB_SUCCESS);
    u_assert_int_eq(logdb_count_keys(db), (sizeof(sampledata) / sizeof(sampledata[0])));

    if(new_func == logdb_rbtree_new)
    {
        logdb_rbtree_db* handle = (logdb_rbtree_db *)db->cb_ctx;
        size_t size = rbtree_count(handle->tree);

        nodetest = NULL;
        while ((nodetest = rbtree_enumerate_next(handle->tree)))
        {
            rec = (logdb_record *)nodetest->info;
            utils_bin_to_hex((unsigned char *)rec->key->str, rec->key->len, bufs[cnt]);

            for(i = 0; i < cnt; i++)
            {
                u_assert_int_eq(strcmp(bufs[i], bufs[cnt]) != 0, 1);
            }
            cnt++;
        }
        u_assert_int_eq(size, cnt);
    }

    for (i = 0; i < (sizeof(sampledata) / sizeof(sampledata[0])); i++) {
        const struct txtest *tx = &sampledata[i];

        uint8_t hashbin[sizeof(tx->txhash) / 2];
        int outlen = sizeof(tx->txhash) / 2;
        utils_hex_to_bin(tx->txhash, hashbin, strlen(tx->txhash), &outlen);

        cstr_erase(smp_key, 0, smp_key->len);
        cstr_append_buf(smp_key, hashbin, outlen);

        outlen = sizeof(tx->hextx) / 2;
        utils_hex_to_bin(tx->hextx, txbin, strlen(tx->hextx), &outlen);

        cstr_erase(smp_value, 0, smp_value->len);
        cstr_append_buf(smp_value, txbin, outlen);

        logdb_append(db, NULL, smp_key, smp_value);
    }

    logdb_flush(db);
    logdb_free(db);

    /* test switch mem mapper after initialitaion. */
    db = logdb_new();
    logdb_set_memmapper(db, &logdb_rbtree_mapper, NULL);
    logdb_flush(db);
    logdb_free(db);


    unlink(dbtmpfile);

    db = new_func();
    u_assert_int_eq(logdb_load(db, dbtmpfile, true, NULL), true);

    // create transaction, don't store
    logdb_txn* txn = logdb_txn_new();
    logdb_append(db, txn, key, value);
    logdb_append(db, txn, key1, value1);
    u_assert_int_eq(logdb_cache_size(db), 0);
    logdb_txn_free(txn);

    logdb_flush(db);
    logdb_free(db);

    db = new_func();
    u_assert_int_eq(logdb_load(db, dbtmpfile, false, NULL), true);
    // db should still be empty
    u_assert_int_eq(logdb_count_keys(db), 0);

    // create transaction, store it this time
    txn = logdb_txn_new();
    logdb_append(db, txn, key, value);
    logdb_append(db, txn, key1, value1);
    logdb_txn_commit(db, txn);
    u_assert_int_eq(logdb_cache_size(db), 2);
    logdb_txn_free(txn);

    logdb_flush(db);
    logdb_free(db);

    db = new_func();
    u_assert_int_eq(logdb_load(db, dbtmpfile, false, NULL), true);
    // now we should have the two persisted items from the txn
    u_assert_int_eq(logdb_count_keys(db), 2);
    logdb_flush(db);
    logdb_free(db);

    cstr_free(key, true);
    cstr_free(value, true);
    cstr_free(value0, true);
    cstr_free(value1, true);
    cstr_free(value2, true);
    cstr_free(key1, true);
    cstr_free(key2, true);
    cstr_free(smp_key, true);
    cstr_free(smp_value, true);
}
コード例 #23
0
ファイル: serialize_tests.c プロジェクト: laanwj/dbb-app
void test_serialize()
{
    char hex0[] = "28969cdfa74a12c82f3bad960b0b000aca2ac329deea5c2328ebc6f2ba9802c1";
    char hex1[] = "28969cdfa74a12c82f3bad960b0b000aca2ac329deea5c2328ebc6f2ba9802c2";
    char hex2[] = "28969cdfa74a12c82f3bad960b0b000aca2ac329deea5c2328ebc6f2ba9802c3";
    uint8_t* hash0 = malloc(32);
    uint8_t* hash1 = malloc(32);
    uint8_t* hash2 = malloc(32);

    memcpy(hash0, utils_hex_to_uint8(hex0), 32);
    memcpy(hash1, utils_hex_to_uint8(hex1), 32);
    memcpy(hash2, utils_hex_to_uint8(hex2), 32);
    vector* vec = vector_new(5, free);
    vector_add(vec, hash0);
    vector_add(vec, hash1);
    vector_add(vec, hash2);


    cstring* s = cstr_new_sz(200);
    ser_u256_vector(s, vec);
    vector_free(vec, true);

    vector* vec2 = vector_new(0, NULL);
    struct const_buffer buf = {s->str, s->len};
    deser_u256_vector(&vec2, &buf);

    vector_free(vec2, true);
    cstr_free(s, true);


    cstring* s2 = cstr_new_sz(200);
    ser_u16(s2, 0xAAFF);
    ser_u32(s2, 0xDDBBAAFF);
    ser_u64(s2, 0x99FF99FFDDBBAAFF);
    ser_varlen(s2, 10);
    ser_varlen(s2, 1000);
    ser_varlen(s2, 100000000);
    ser_str(s2, "test", 4);
    cstring* s3 = cstr_new("foo");
    ser_varstr(s2, s3);
    cstr_free(s3, true);
    // ser_varlen(s2, (uint64_t)0x9999999999999999); // uint64 varlen is not supported right now

    struct const_buffer buf2 = {s2->str, s2->len};
    uint16_t num0;
    deser_u16(&num0, &buf2);
    assert(num0 == 43775); //0xAAFF
    uint32_t num1;
    deser_u32(&num1, &buf2);
    assert(num1 == 3720063743); //0xDDBBAAFF
    uint64_t num2;
    deser_u64(&num2, &buf2);
    assert(num2 == 0x99FF99FFDDBBAAFF); //0x99FF99FFDDBBAAFF
    uint32_t num3;
    deser_varlen(&num3, &buf2);
    assert(num3 == 10);
    deser_varlen(&num3, &buf2);
    assert(num3 == 1000);
    deser_varlen(&num3, &buf2);
    assert(num3 == 100000000);


    char strbuf[255];
    deser_str(strbuf, &buf2, 255);
    assert(strncmp(strbuf, "test", 4) == 0);

    cstring* deser_test = cstr_new_sz(0);
    deser_varstr(&deser_test, &buf2);
    assert(strncmp(deser_test->str, "foo", 3) == 0);

    cstr_free(deser_test, true);

    cstr_free(s2, true);
}
コード例 #24
0
ファイル: cstr.c プロジェクト: hsk81/picocoin
static void test_basic(void)
{
	cstring *s = cstr_new("foo");
	assert(s != NULL);
	assert(s->len == 3);
	assert(strcmp(s->str, "foo") == 0);

	cstr_free(s, true);

	s = cstr_new_sz(200);
	assert(s != NULL);
	assert(s->alloc > 200);
	assert(s->len == 0);

	cstr_free(s, true);

	s = cstr_new_buf("foo", 2);
	assert(s != NULL);
	assert(s->len == 2);
	assert(strcmp(s->str, "fo") == 0);

	cstr_free(s, true);

	s = cstr_new(NULL);
	assert(s != NULL);
	cstr_append_buf(s, "f", 1);
	cstr_append_buf(s, "o", 1);
	cstr_append_buf(s, "o", 1);
	assert(s->len == 3);
	assert(strcmp(s->str, "foo") == 0);

	cstr_free(s, true);

	s = cstr_new("foo");
	assert(s != NULL);

	cstr_resize(s, 2);
	assert(s->len == 2);
	assert(strcmp(s->str, "fo") == 0);

	cstr_resize(s, 4);
	assert(s->len == 4);
	assert(s->alloc > 4);
	memcpy(s->str, "food", 4);
	assert(strcmp(s->str, "food") == 0);

	cstr_free(s, true);

	cstring *s1 = cstr_new("foo");
	cstring *s2 = cstr_new("foo");
	cstring *s3 = cstr_new("bar");

	assert(cstr_equal(s1, s2) == true);
	assert(cstr_equal(s1, s3) == false);
	assert(cstr_equal(s2, s3) == false);
	assert(cstr_equal(s3, s3) == true);

	cstr_free(s1, true);
	cstr_free(s2, true);
	cstr_free(s3, true);
}