예제 #1
0
파일: parser.cpp 프로젝트: Arelius/ploy
pointer parse_number_or_pair(parser* parse)
{
    char next = (*(parse->curr+1));

    pointer ret_car;
    pointer ret_cdr;

    if(is_whitespace(next))
    {
        parse->curr++;
        return parse_expr(parse);
    }
    else if(next >= '0' && next <= '9')
    {
        ret_car = parse_number(parse);
        ret_cdr = parse_expr(parse);
        return create_pair(ret_car, ret_cdr);
    }
    else
    {
        ret_car = parse_symbol(parse);
        ret_cdr = parse_expr(parse);
        return create_pair(ret_car, ret_cdr);
    }
}
예제 #2
0
void send_pair_composed(struct network_status *net_stat)
  /*@ requires [?f0]world(?pub, ?key_clsfy) &*&
               proof_obligations(pub) &*&
               network_status(net_stat) &*&
               principal(?principal, ?count1) &*&
               true == bad(principal); @*/
  /*@ ensures  [f0]world(pub, key_clsfy) &*&
               proof_obligations(pub) &*&
               network_status(net_stat) &*&
               principal(principal, ?count2); @*/
{
  struct item *first = network_receive(net_stat);
  struct item *second = network_receive(net_stat);
  struct item * pair = create_pair(first, second);
  //@ assert item(first, ?f, pub);
  //@ assert item(second, ?s, pub);
  //@ assert item(pair, pair_item(f, s), pub);

  //@ open proof_obligations(pub);
  //@ assert is_public_pair_compose(?proof, pub);
  //@ proof(f, s);
  //@ close proof_obligations(pub);
  network_send(net_stat, pair);
  item_free(pair);
  item_free(first);
  item_free(second);
}
예제 #3
0
void app_send(struct item *key, struct item *message)
  /*@ requires [?f0]world(ss_pub) &*&
               item(key, symmetric_key_item(?creator, ?id), ss_pub) &*& 
               item(message, ?msg, ss_pub) &*& [_]ss_pub(msg) &*&
               app_send_event(creator, msg) == true;
  @*/
  /*@ ensures  [f0]world(ss_pub) &*&
               item(key, symmetric_key_item(creator, id), ss_pub) &*&
               item(message, msg, ss_pub);
  @*/
{
    struct network_status *net_stat = 
                                 network_connect("localhost", APP_RECEIVE_PORT);
    
    struct item *hash = create_hmac(key, message);
    //@ assert item(hash, ?h, ss_pub);
    //@ get_info_for_item(h);
    //@ close ss_pub(h);
    //@ leak ss_pub(h);
    struct item *m = create_pair(hash, message);
    //@ assert item(m, ?pmessage, ss_pub);
    //@ get_info_for_item(pmessage);
    //@ close ss_pub(pmessage);
    //@ leak ss_pub(pmessage);
    network_send(net_stat, m);
    item_free(hash);
    item_free(m);
    
    network_disconnect(net_stat);
}
예제 #4
0
int test_datastructure_rbtree_map(void)
{
	int ret = 0;
	rbtree_map_pos_t it,next,end;
	rbtree_map_t *map;
	allocator_t *allocator = allocator_get_default_alloc();
	pair_t *pair;
	struct rbtree_map_node *mnode;
	int key_len = 2;

	struct A t1 = {1,2};
	struct A t2 = {2,2};
	struct A t3 = {3,2};
	struct A t4 = {4,2};
	struct A t5 = {5,2};

	dbg_str(DBG_DETAIL,"rbtree allocator addr:%p",allocator);
	map = rbtree_map_create(allocator,0);

	rbtree_map_init(map,key_len,sizeof(struct A) + key_len,NULL); 

	dbg_str(DBG_DETAIL,"create_pair");
	pair = create_pair(key_len,sizeof(struct A));

	make_pair(pair,"44",&t4);
	rbtree_map_insert(map,pair->data);
	make_pair(pair,"33",&t3);
	rbtree_map_insert(map,pair->data);
	make_pair(pair,"22",&t2);
	rbtree_map_insert(map,pair->data);
	make_pair(pair,"11",&t1);
	rbtree_map_insert(map,pair->data);


	dbg_str(DBG_DETAIL,"foreach ordinal print");
	for(	rbtree_map_begin(map,&it),rbtree_map_pos_next(&it,&next); 
			!rbtree_map_pos_equal(&it,rbtree_map_end(map,&end));
			it = next,rbtree_map_pos_next(&it,&next))
	{
		mnode = rb_entry(it.rb_node_p,struct rbtree_map_node,node);
		dbg_buf(DBG_DETAIL,"key:",mnode->key,mnode->value_pos);
		print_A((struct A*)rbtree_map_pos_get_pointer(&it));
	}

	dbg_str(DBG_DETAIL,"search node key = 22");
	rbtree_map_search(map, (void *)"22",&it);
	print_A((struct A*)rbtree_map_pos_get_pointer(&it));

	/*
	 *dbg_str(DBG_DETAIL,"delte node key = 11");
	 *container_delete(map,&it);
	 */

	rbtree_map_destroy(map);
	allocator_destroy(allocator);
	dbg_str(DBG_DETAIL,"map addr =%p",map);

	return ret;
}
예제 #5
0
파일: parser.cpp 프로젝트: Arelius/ploy
pointer parse_expr(parser* parse)
{
    eat_whitespace(parse);
    pointer ret_car;
    pointer ret_cdr;
    switch(*parse->curr)
    {
    case '(':
        parse->curr++;
        ret_car = parse_expr(parse);
        ret_cdr = parse_expr(parse);
        return create_pair(ret_car, ret_cdr);
    case '"':
        ret_car = parse_string(parse);
        ret_cdr = parse_expr(parse);
        return create_pair(ret_car, ret_cdr);
    case '\'':
        parse->curr++;
        ret_car = parse_quote(parse);
        ret_cdr = parse_expr(parse);
        return create_pair(ret_car, ret_cdr);
    case ')':
        parse->curr++;
        return NIL;
    case '+': case '-': case 'b':
        ret_car = parse_number_or_symbol(parse);
        ret_cdr = parse_expr(parse);
        return create_pair(ret_car, ret_cdr);
    case '.':
        return parse_number_or_pair(parse);
    case '\\':
        parse->curr++;
        ret_car = create_char(*(parse->curr++));
        ret_cdr = parse_expr(parse);
        return create_pair(ret_car, ret_cdr);
    case ';':
        while(!is_newline(*parse->curr) && *parse->curr != '\0')
            parse->curr++;
        return parse_expr(parse);
    case 0:
        return NIL;
    default:
        if(is_number_char(*parse->curr))
        {
            ret_car = parse_number(parse);
            ret_cdr = parse_expr(parse);
            return create_pair(ret_car, ret_cdr);
        }
        else if(is_symbol_char(*parse->curr))
        {
            ret_car = parse_symbol(parse);
            ret_cdr = parse_expr(parse);
            return create_pair(ret_car, ret_cdr);
        }
        else
            return parser_error(parse, "Unexpected char in expression.");

    }
    parse->curr++;
}
예제 #6
0
파일: parser.cpp 프로젝트: Arelius/ploy
pointer parse_quote(parser* parse)
{
    if(is_whitespace(*parse->curr))
        return parser_error(parse, "unexpected whitespace after quote.");

    switch(*parse->curr)
    {
    case '(':
        return create_pair(create_symbol(parse->symbols, "QUOTE"), parse_expr(parse));
    default:
        if(is_symbol_char(*parse->curr) && !is_number_char(*parse->curr))
            return create_pair(create_symbol(parse->symbols, "QUOTE"), parse_symbol(parse));
        else
            return parser_error(parse, "Unexpected token after quote.");

    }

}
struct item *asymmetric_authenticated_encryption(char recipient,
                                                 struct item *public_key,
                                                 struct item *private_key, 
                                                 struct item *payload)
  /*@ requires [?f]world(?pub) &*&
               generated_values(?principal1, ?count1) &*&
               item(public_key, ?pub_k, pub) &*& 
                 pub_k == public_key_item(?principal2, ?count2) &*&
               item(private_key, ?priv_k, pub) &*& 
                 priv_k == private_key_item(?principal3, ?count3) &*&
               item(payload, ?pay, pub); @*/
  /*@ ensures  [f]world(pub) &*&
               generated_values(principal1, count1 + 2) &*&
               item(public_key, pub_k, pub) &*&
               item(private_key, priv_k, pub) &*&
               item(payload, pay, pub) &*&
               item(result, ?msg, pub) &*& 
               collision_in_run() ?
                 true
               :
                 msg == pair_item(?enc, ?sig) &*&
                 enc == asymmetric_encrypted_item(principal2, count2, 
                                                  some(pay), _) &*&
                 sig == asymmetric_signature_item(principal3, count3, 
                                                  some(?msg_id), _) &*&
                 msg_id == pair_item(data_item(cons(recipient, nil)), 
                                     hash_item(some(enc))); @*/
{
  struct item* encrypted = asymmetric_encryption(public_key, payload);
  struct item* hash = create_hash(encrypted);
  struct item* rcp = create_data_item_from_char(recipient);
  struct item* msg_id = create_pair(rcp, hash);
  struct item* signature = asymmetric_signature(private_key, msg_id);
  struct item* result = create_pair(encrypted, signature);
  item_free(encrypted);
  item_free(hash);
  item_free(rcp);
  item_free(msg_id);
  item_free(signature);
  return result;
}
struct item *asymmetric_authenticated_decryption(char recipient,
                                                 struct item *public_key,
                                                 struct item *private_key, 
                                                 struct item *message)
  /*@ requires [?f]world(?pub) &*&
               generated_values(?principal1, ?count1) &*&
               item(public_key, ?pub_k, pub) &*& 
                 pub_k == public_key_item(?principal2, ?count2) &*&
               item(private_key, ?priv_k, pub) &*& 
                 priv_k == private_key_item(?principal3, ?count3) &*&
               item(message, ?msg, pub); @*/
  /*@ ensures  [f]world(pub) &*&
               generated_values(principal1, count1 + 1) &*&
               item(public_key, pub_k, pub) &*&
               item(private_key, priv_k, pub) &*&
               item(message, msg, pub) &*&
               item(result, ?decrypted, pub) &*& 
               collision_in_run() ?
                 true
               :
                 msg == pair_item(?enc, ?sig) &*& 
                 enc == asymmetric_encrypted_item(?principal4, ?count4, 
                                                  ?pay, _) &*&
                 sig == asymmetric_signature_item(principal2, count2, 
                                                  some(?msg_id), _) &*&
                 msg_id == pair_item(data_item(cons(recipient, nil)), 
                                     hash_item(some(enc))) &*&
                 principal4 == principal3 && count4 == count3 ?
                   pay == some(decrypted)
                 :
                   [_]pub(decrypted)
               ; @*/
{
  check_is_pair(message);
  struct item* encrypted = pair_get_first(message);
  check_is_asymmetric_encrypted(encrypted);
  struct item* signature = pair_get_second(message);
  struct item* rcp = create_data_item_from_char(recipient);  
  struct item* hash = create_hash(encrypted);
  struct item* pair = create_pair(rcp, hash);
  asymmetric_signature_verify(public_key, pair, signature);
  struct item *result = asymmetric_decryption(private_key, encrypted);
  item_free(encrypted);
  item_free(rcp);
  item_free(pair);
  item_free(hash);
  item_free(signature);
  
  return result;
}
예제 #9
0
pair *map_set(map *m, void *key, void *value) {
	map_node *y, *z; /* Top node to update balance factor, and parent. */
	map_node *p, *q; /* Iterator, and parent. */
	map_node *n;     /* Newly inserted node. */
	map_node *w;     /* New root of rebalanced subtree. */
	unsigned char dir = 0;			/* Direction to descend. */
	unsigned char ds[MAX_HEIGHT];	/* Cached comparison results. */
	int i = 0;						/* Number of cached results. */

	z = (map_node*)&m->x;
	y = m->x;
	for (q = z, p = y; p != NULL; q = p, p = p->children[dir]) {
		int c = m->_cmp(key, p->x->a);
		if (c == 0)
			return p->x;
		if (p->balance != 0) {
			z = q;
			y = p;
			i = 0;
		}
		ds[i++] = dir = c > 0;
	}

	n = q->children[dir] = malloc(sizeof(map_node));
	if (n == NULL)
		return NULL;

	m->size++;
	n->x = create_pair(key, value);
	n->left = n->right = NULL;
	n->balance = 0;
	if (y == NULL)
		return n->x;

	for (p = y, i = 0; p != n; p = p->children[ds[i]], i++) {
		if (ds[i] == 0)
			p->balance--;
		else
			p->balance++;
	}

	if (y->balance == -2) {
		w = (y->left->balance == -1) ? rotate_c(y->left, y) : rotate_a(y->left, y);
	} else if (y->balance == +2) {
		w = (y->right->balance == +1) ? rotate_d(y->right, y) : rotate_b(y->right, y);
	} else
		return n->x;
	z->children[y != z->left] = w;
	return n->x;
}
예제 #10
0
bool ai_start(Player *player, Board *board) {
    int x, y;

    while (1) {
        x = rand() % board->width;
        y = rand() % board->height;

        if (board->fields[x][y].owner == 0) {
            if (!has_neighbour(x, y, ENEMY, board) || board->width * board->height <= 30) {
                player_start(player, pair_to_field(create_pair(x, y), board));
                return true;
            }
        }
    }
}
예제 #11
0
pair_t _multimap_equal_range_varg(
    const multimap_t* cpt_multimap, va_list val_elemlist)
{
    multimap_iterator_t t_first;
    multimap_iterator_t t_second;
    pair_t              t_pair;
#ifdef CSTL_MULTIMAP_AVL_TREE
    avl_tree_result_pair_t t_avlresult;
#else
    rb_tree_result_pair_t  t_rbresult;
#endif

    assert(cpt_multimap != NULL);

    _get_varg_value(
        cpt_multimap->_t_pair.first, 
        val_elemlist, 
        cpt_multimap->_t_pair._t_firsttypesize,
        cpt_multimap->_t_pair._sz_firsttypename);
#ifdef CSTL_MULTIMAP_AVL_TREE
    t_avlresult = _avl_tree_equal_range(
        _GET_MULTIMAP_AVL_TREE(cpt_multimap), &cpt_multimap->_t_pair);
    t_first = t_avlresult._t_first;
    t_second = t_avlresult._t_second._t_iterator;
#else
    t_rbresult = _rb_tree_equal_range(
        _GET_MULTIMAP_RB_TREE(cpt_multimap), &cpt_multimap->_t_pair);
    t_first = t_rbresult._t_first;
    t_second = t_rbresult._t_second._t_iterator;
#endif

    _GET_CONTAINER(&t_first) = (multimap_t*)cpt_multimap;
    _GET_MULTIMAP_CONTAINER_TYPE(&t_first) = _MULTIMAP_CONTAINER;
    _GET_MULTIMAP_ITERATOR_TYPE(&t_first) = _BIDIRECTIONAL_ITERATOR;
    _GET_CONTAINER(&t_second) = (multimap_t*)cpt_multimap;
    _GET_MULTIMAP_CONTAINER_TYPE(&t_second) = _MULTIMAP_CONTAINER;
    _GET_MULTIMAP_ITERATOR_TYPE(&t_second) = _BIDIRECTIONAL_ITERATOR;

    t_pair = create_pair(multimap_iterator_t, multimap_iterator_t);
    pair_init(&t_pair);
    memcpy(t_pair.first, &t_first, t_pair._t_firsttypesize);
    memcpy(t_pair.second, &t_second, t_pair._t_secondtypesize);

    return t_pair;
}
예제 #12
0
bool ai_move(Player *player, Board *board, enum State *state, Players *players, PairStack *fields_adjacent_enemies,
             PairStack *fields_adjacent_neutrals) {
    PairStack *actionable_neutral_fields = create_pair_stack();
    PairStack *actionable_enemy_fields = create_pair_stack();

    Field *field;

    for (int x = 0; x < board->width; x++) {
        for (int y = 0; y < board->height; y++) {
            if (is_actionable(board, x, y, player, *state)) {
                if (board->fields[x][y].owner == 0) {
                    push(actionable_neutral_fields, create_pair(x, y));
                }
                else if (board->fields[x][y].owner != player->id) {
                    push(actionable_enemy_fields, create_pair(x, y));
                }
            }
        }
    }

    if ((rand() / (double) RAND_MAX) < 0.75 || actionable_enemy_fields->size == 0) {
        if (actionable_enemy_fields->size > 0 &&
            ((rand() / (double) RAND_MAX) < 0.35 || actionable_neutral_fields->size == 0)) {
            Triple actionable_enemy_fields_tab[actionable_enemy_fields->size];

            int index = 0;
            PairItem *pair_item = actionable_enemy_fields->top;
            while (pair_item != NULL) {
                actionable_enemy_fields_tab[index] = create_triple(pair_item->pair.x, pair_item->pair.y,
                                                                   board->fields[pair_item->pair.x][pair_item->pair.y].force);
                pair_item = pair_item->prev;
                index++;
            }

            qsort(actionable_enemy_fields_tab, (size_t) actionable_enemy_fields->size, sizeof(Triple),
                  actionable_enemy_fields_cmp);

            if ((rand() / (double) RAND_MAX) < 0.5) {
                field = pair_to_field(create_pair(actionable_enemy_fields_tab[0].x, actionable_enemy_fields_tab[0].y),
                                      board);
            }
            else {
                index = rand() % actionable_enemy_fields->size;
                field = pair_to_field(
                        create_pair(actionable_enemy_fields_tab[index].x, actionable_enemy_fields_tab[index].y), board);
            }
        }
        else if (actionable_neutral_fields->size > 0) {
            field = random_field(actionable_neutral_fields, board);
        }
        else {
            field = random_field(player->fields_stack, board);
        }

        player_move(player, field, board, state, players);
    }
    else {
        if (fields_adjacent_enemies->size > 0) {
            field = random_field(fields_adjacent_enemies, board);
        }
        else if (fields_adjacent_neutrals->size > 0) {
            field = random_field(fields_adjacent_neutrals, board);
        }
        else {
            field = random_field(player->fields_stack, board);
        }

        player_move(player, field, board, state, players);
    }

    delete_pair_stack(actionable_enemy_fields);
    delete_pair_stack(actionable_neutral_fields);

    return true;
}
예제 #13
0
파일: rpc.c 프로젝트: amintimany/verifast
void server(char server, struct item *key)
  /*@ requires [?f0]world(rpc_pub, rpc_key_clsfy) &*&
               principal(server, ?count) &*&
               item(key, symmetric_key_item(?creator, ?id), rpc_pub) &*&  
               shared_with(creator, id) == server;
  @*/
  /*@ ensures  [f0]world(rpc_pub, rpc_key_clsfy) &*&
               principal(server, count + 1) &*&
               item(key, symmetric_key_item(creator, id), rpc_pub);
  @*/
{
    struct network_status *net_stat = network_bind_and_accept(SERVER_PORT);
    
    struct item *request = 0;
    {
        struct item *r = network_receive(net_stat);
        check_is_pair(r);
        //@ assert item(r, pair_item(?h0, ?p0), rpc_pub);
        struct item *hmac1 = pair_get_first(r);
        //@ assert item(hmac1, ?h, rpc_pub);
        struct item *payload = pair_get_second(r);
        //@ assert item(payload, ?p, rpc_pub);
        
        /*@ if (!col)
            {
              assert h0 == h;
              assert p0 == p;
              open [_]rpc_pub(pair_item(h, p));
              open [_]rpc_pub(h);
              open [_]rpc_pub(p);
            }
        @*/
        struct item *hmac2 = create_hmac(key, payload);
        item_check_equal(hmac1, hmac2);
        item_free(hmac1);
        item_free(hmac2);
        item_free(r);
        //@ assert col || h == hmac_item(creator, id, some(p));
        struct item *tag = pair_get_first(payload);
        check_is_data(tag);
        int tagValue = item_get_data_as_int(tag);
        if (tagValue != 0) abort();
        //@ item d = data_item(chars_of_int(0));
        request = pair_get_second(payload);
        
        /*@ if (!col) 
            {  
              assert item(tag, d, rpc_pub);
              assert item(request, ?req, rpc_pub);
              assert p == pair_item(d, req); 
            }
        @*/
        item_free(tag);
        item_free(payload);
    }
    
    //@ assert item(request, ?req, rpc_pub);
    struct item *response = compute_response(server, request);
    //@ assert item(response, ?resp, rpc_pub);
    
    {
        struct item *reqresp = create_pair(request, response);
        //@ item p = pair_item(req, resp);
        //@ assert item(reqresp, p, rpc_pub);
        //@ close rpc_pub(p);
        //@ leak rpc_pub(p);
        item_free(response);
        item_free(request);
        struct item *tag = create_data_item_from_int(1);
        struct item *payload = create_pair(tag, reqresp);
        //@ item d = data_item(chars_of_int(1));
        //@ close rpc_pub(d);
        //@ leak rpc_pub(d);
        //@ assert item(payload, pair_item(d, p), rpc_pub);
        //@ close rpc_pub(pair_item(d, p));
        //@ leak rpc_pub(pair_item(d, p));
        item_free(tag);
        item_free(reqresp);
        struct item *hash = create_hmac(key, payload);
        //@ item h = hmac_item(creator, id, some(pair_item(d, p)));
        //@ if (!col) assert item(hash, h, rpc_pub);
        //@ close rpc_pub(h);
        //@ leak rpc_pub(h);
        struct item *m = create_pair(hash, payload);
        //@ assert item(m, ?msg, rpc_pub);
        //@ if (!col) msg == pair_item(h, pair_item(d, p));
        //@ if (!col) assert item(m, msg, rpc_pub);
        //@ close rpc_pub(msg);
        //@ leak rpc_pub(msg);
        item_free(hash);
        item_free(payload);
        network_send(net_stat, m);
        item_free(m);
    }
    
    network_disconnect(net_stat);
}
예제 #14
0
파일: pl_ini.c 프로젝트: nworkers/smsplus
int pl_ini_load(pl_ini_file *file,
                const char *path)
{ 
  file->head = NULL;

  FILE *stream;
  if (!(stream = fopen(path, "r"))) 
    return 0;

  pl_ini_section *current_section = NULL;
  pl_ini_pair *tail;

  char string[PL_MAX_LINE_LENGTH],
       name[PL_MAX_LINE_LENGTH];
  char *ptr;
  int len;

  /* Create unnamed section */
  current_section = NULL;
  tail = NULL;

  while(!feof(stream) && fgets(string, sizeof(string), stream))
  {
    /* TODO: Skip whitespace */
    /* New section */
    if (string[0] == '[')
    {
      if ((ptr = strrchr(string, ']')))
      {
        len = ptr - string - 1;
        strncpy(name, string + 1, len);
        name[len] = '\0';

        if (!current_section)
          current_section = file->head = create_section(name);
        else
        {
          current_section->next = create_section(name);
          current_section = current_section->next;
        }

        tail = NULL;
      }
    }
    else if (string[0] =='#'); /* Do nothing - comment */
    else
    {
      /* No section defined - create empty section */
      if (!current_section)
      {
        current_section = create_section(strdup(""));
        file->head = current_section;
        tail = NULL;
      }

      pl_ini_pair *pair = create_pair(string);
      if (pair)
      {
        if (tail) tail->next = pair;
        else current_section->head = pair;
        tail = pair;
      }
    }
  }

  fclose(stream);
  return 1;
}
예제 #15
0
파일: baf.c 프로젝트: svn2github/staden
int parse_baf(GapIO *io, char *fn, tg_args *a) {
    int nseqs = 0, nobj = 0, ntags = 0, ncontigs = 0;
    struct stat sb;
    zfp *fp;
    off_t pos;
    contig_t *c = NULL;
    tg_pair_t *pair = NULL;
    baf_block *b, *co = NULL;
    int last_obj_type = 0;
    int last_obj_pos = 0;
    tg_rec last_obj_rec = 0;
    tg_rec last_cnt_rec = 0;
    int last_cnt_pos = 0;
    int last_obj_orient = 0;
    
    
	
    printf("Loading %s...\n", fn);
    if (-1 == stat(fn, &sb) ||
	NULL == (fp = zfopen(fn, "r"))) {
	perror(fn);
	return -1;
    }

    if (a->pair_reads) {
	pair = create_pair(a->pair_queue);
    }

    /* Loop:
     * Read 1 block of data.
     * If contig, create contig
     * If read, insert it, insert to index.
     * Anything else - reject for now
     */
    pos = 0;
    while (b = baf_next_block(fp)) {
	int delay_destroy = 0;

	switch (b->type) {
	case CO: {
	    char *contig = baf_block_value(b, CO);

	    if (co)
		baf_block_destroy(co);

	    co = b;
	    delay_destroy = 1;

	    ncontigs++;
	    
	    create_new_contig(io, &c, contig, a->merge_contigs);

	    /* For anno */
	    last_obj_type = GT_Contig;
	    last_obj_rec = c->rec;
	    last_obj_pos = c->start + 1;
	    last_cnt_rec = c->rec;
	    last_cnt_pos = c->start + 1;
	    last_obj_orient = 0;

	    break;
	}

	case RD: {
	    seq_t seq;
	    int flags;
	    char *tname;
	    tg_rec recno;
	    int is_pair = 0;

	    /* Construct seq struct */
	    if (-1 == construct_seq_from_block(a, &seq, b, &tname)) {
		fprintf(stderr, "Failed to parse read block for seq %d\n",
			nseqs);
		break;
	    }

	    /* Create range, save sequence */
	    flags = GRANGE_FLAG_TYPE_SINGLE;
	    
	    if (seq.flags & SEQ_END_REV)
		flags |= GRANGE_FLAG_END_REV;
	    else
		flags |= GRANGE_FLAG_END_FWD;
	    if (seq.len < 0)
		flags |= GRANGE_FLAG_COMP1;
		
	    if (pair) is_pair = 1;
		
	    recno = save_range_sequence(io, &seq, seq.mapping_qual, pair,
					is_pair, tname, c, a, flags, NULL);

	    /* For anno */
	    last_obj_type = GT_Seq;
	    last_obj_rec = recno;
	    if (seq.len >= 0) {
		last_obj_pos = seq.pos;
		last_obj_orient = 0;
	    } else {
		last_obj_pos = seq.pos - seq.len - 1;
		last_obj_orient = 1;
	    }

	    nseqs++;
	    
	    break;
	}

	case AN: {
	    range_t r;
	    anno_ele_t *e;
	    char *typ = baf_block_value(b, AN);
	    char *loc = baf_block_value(b, LO);
	    char *len = baf_block_value(b, LL);
	    char *txt = baf_block_value(b, TX);
	    char *at  = baf_block_value(b, AT);
	    int an_pos;
	    bin_index_t *bin;
	    int anno_obj_type;

	    if (!(a->data_type & DATA_ANNO))
		break;

	    if (txt)
		unescape_line(txt);

	    if (last_obj_type == GT_Contig || (at && *at == 'C'))
		anno_obj_type = GT_Contig;
	    else
		anno_obj_type = GT_Seq;

	    if (!loc) {
		an_pos = last_obj_pos;
	    } else {
		if (*loc == '@') {
		    an_pos = atoi(loc+1);
		} else {
		    if (anno_obj_type == GT_Contig) {
			if (last_obj_orient == 0)
			    an_pos = last_cnt_pos + atoi(loc)-1;
			else
			    an_pos = last_cnt_pos - (atoi(loc)-1)
				- (len ? atoi(len)-1 : 0);
		    } else {
			if (last_obj_orient == 0)
			    an_pos = last_obj_pos + atoi(loc)-1;
			else
			    an_pos = last_obj_pos - (atoi(loc)-1)
				- (len ? atoi(len)-1 : 0);
		    }
		}
	    }

	    r.start = an_pos;
	    r.end = an_pos + (len ? atoi(len)-1 : 0);

	    r.mqual = str2type(typ);
	    r.pair_rec = (anno_obj_type == GT_Contig)
		? last_cnt_rec
		: last_obj_rec;

	    r.flags = GRANGE_FLAG_ISANNO;
	    if (GT_Seq == anno_obj_type)
		r.flags |= GRANGE_FLAG_TAG_SEQ;
	    r.rec = anno_ele_new(io, 0, anno_obj_type, r.pair_rec, 0,
				 str2type(typ), txt);
	    e = (anno_ele_t *)cache_search(io, GT_AnnoEle, r.rec);
	    e = cache_rw(io, e);
	
	    bin = bin_add_range(io, &c, &r, NULL, NULL, 0);
	    e->bin = bin->rec;

	    ntags++;
	    break;
	}

	case 0:
	    /* blank line */
	    break;

	default:
	    printf("Unsupported block type '%s'\n",
		   linetype2str(b->type));
	}

	if (!delay_destroy)
	    baf_block_destroy(b);

	if ((++nobj & 0xfff) == 0) {
	    int perc = 0;

	    pos = zftello(fp);
	    perc = 100.0 * pos / sb.st_size;
	    printf("\r%d%c", perc, (nobj & 0x3fff) ? '%' : '*');
	    fflush(stdout);
	    if ((nobj & 0x3fff) == 0)
		cache_flush(io);
	}

#if 1
	if ((nobj & 0x3fff) == 0) {
	    static int perc = 0;
	    if (perc < 100.0 * pos / sb.st_size) {
		perc = 100.0 * pos / sb.st_size;
		printf("\r%d%%", perc);
		//HacheTableStats(io->cache, stdout);
		//HacheTableStats(((GDB **)io->dbh)[0]->gfile->idx_hash, stdout);
		{
		    static struct timeval last, curr;
		    static int first = 1;
		    static int last_obj = 0;
		    static int last_contigs = 0;
		    long delta;

		    gettimeofday(&curr, NULL);
		    if (first) {
			last = curr;
			first = 0;
		    }

		    delta = (curr.tv_sec - last.tv_sec) * 1000000
			+ (curr.tv_usec - last.tv_usec);
		    printf(" - %g sec %d obj (%d contigs)\n", delta/1000000.0,
			   nobj - last_obj, ncontigs - last_contigs);
		    last = curr;
		    last_obj = nobj;
		    last_contigs = ncontigs;
		}
		fflush(stdout);
	    }
	}
#endif
    }
    
    if (pair && !a->fast_mode) {  
	finish_pairs(io, pair);
    }
    
    if (co)
	baf_block_destroy(co);

    cache_flush(io);
    zfclose(fp);

    printf("\nLoaded %12d contigs\n",     ncontigs);
    printf("       %12d sequences\n",   nseqs);
    printf("       %12d annotations\n", ntags);

    if (pair) delete_pair(pair);

    if (c)
	cache_decr(io, c);

    return 0;
}
예제 #16
0
파일: rpc.c 프로젝트: amintimany/verifast
struct item *client(char server, struct item *key, struct item *request)
  /*@ requires [?f0]world(rpc_pub, rpc_key_clsfy) &*& 
               principal(?client, ?count) &*&
               item(key, symmetric_key_item(?creator, ?id), rpc_pub) &*&
               item(request, ?req, rpc_pub) &*& [_]rpc_pub(req) &*&
               true == request(creator, server, req) &*&
               shared_with(creator, id) == server;
  @*/
  /*@ ensures  [f0]world(rpc_pub, rpc_key_clsfy) &*& 
               principal(client, count) &*&
               item(key, symmetric_key_item(creator, id), rpc_pub) &*&
               item(request, req, rpc_pub) &*& item(result, ?resp, rpc_pub) &*& 
               (
                 col || bad(creator) || bad(server) ||
                 response(creator, server, req, resp)
               );
  @*/
{
    struct network_status *net_stat = network_connect("localhost", SERVER_PORT);
    
    {
        struct item *tag = create_data_item_from_int(0);
        //@ item d = data_item(chars_of_int(0));
        //@ assert item(tag, d, rpc_pub);
        //@ close rpc_pub(d);
        //@ leak rpc_pub(d);
        struct item *payload = create_pair(tag, request);
        //@ item p = pair_item(d, req);
        //@ assert item(payload, p, rpc_pub);
        //@ close rpc_pub(p);
        //@ leak rpc_pub(p);
        item_free(tag);
        struct item *hash = create_hmac(key, payload);
        //@ item h = hmac_item(creator, id, some(p));
        //@ if (!col) assert item(hash, h, rpc_pub);
        //@ close rpc_pub(h);
        //@ leak rpc_pub(h);
        struct item *m = create_pair(hash, payload);
        //@ assert item(m, ?msg, rpc_pub);
        //@ item msg0 = pair_item(h, p);
        //@ if (!col) msg == msg0;
        //@ close rpc_pub(msg);
        //@ leak rpc_pub(msg);
        item_free(hash);
        item_free(payload);
        network_send(net_stat, m);
        item_free(m);
    }
    
    struct item *response;
    {
        struct item *r = network_receive(net_stat);
        check_is_pair(r);
        //@ assert item(r, pair_item(?h0, ?p0), rpc_pub);
        struct item *hmac1 = pair_get_first(r);
        //@ assert item(hmac1, ?h, rpc_pub);
        struct item *payload = pair_get_second(r);
        //@ assert item(payload, ?p, rpc_pub);
        
        /*@ if (!col)
            {
              assert h0 == h;
              assert p0 == p;
              open [_]rpc_pub(pair_item(h, p));
              open [_]rpc_pub(h);
              open [_]rpc_pub(p);
            }
        @*/
        struct item *hmac2 = create_hmac(key, payload);
        item_check_equal(hmac1, hmac2);
        item_free(hmac1);
        item_free(hmac2);
        item_free(r);
        //@ assert col || h == hmac_item(creator, id, some(p));
       
        struct item *tag = pair_get_first(payload);
        check_is_data(tag);
        int tagValue = item_get_data_as_int(tag);
        if (tagValue != 1) abort();
        //@ item d = data_item(chars_of_int(1));
        //@ assert item(tag, ?d0, rpc_pub);
        //@ assert col || d == d0;
        item_free(tag);
        struct item *reqresp = pair_get_second(payload);
        struct item *request1 = pair_get_first(reqresp);
        response = pair_get_second(reqresp);
        //@ assert item(request1, ?req1, rpc_pub);
        //@ assert item(response, ?resp, rpc_pub);
        //@ if (!col) assert p == pair_item(d, pair_item(req1, resp));
        item_free(payload);
        item_free(reqresp);
        item_check_equal(request, request1);
        //@ assert col || req1 == req;
        item_free(request1);
    }
    network_disconnect(net_stat);
    return response;
}
예제 #17
0
/**
 * Builds the Chariot test.
 *  
 * @param i_testHandle   Test object to be configured.
 */
static void
build_test(
    CHR_TEST_HANDLE i_testHandle)
{
    CHR_CHANNEL_HANDLE channel1;
    CHR_CHANNEL_HANDLE channel2;
    CHR_CHANNEL_HANDLE channel3;
    CHR_RECEIVER_HANDLE receiver1;
    CHR_RUNOPTS_HANDLE runOptionsHandle;
    CHR_API_RC rc;

    rc = CHR_test_set_filename(
            i_testHandle,
            lc_testFile,
            strlen(lc_testFile));
    if (rc != CHR_OK)
        show_error(i_testHandle, rc, "test_set_filename");

    /*
     * Create channel objects.
     */
    channel1 = create_channel(&lc_channelSpec1);
    channel2 = create_channel(&lc_channelSpec2);
    channel3 = create_channel(&lc_channelSpec3);

    /*
     * Add channels to test.
     */
    rc = CHR_test_add_channel(i_testHandle, channel1);
    if (rc != CHR_OK)
        show_error(i_testHandle, rc, "test_add_channel");

    rc = CHR_test_add_channel(i_testHandle, channel2);
    if (rc != CHR_OK)
        show_error(i_testHandle, rc, "test_add_channel");

    rc = CHR_test_add_channel(i_testHandle, channel3);
    if (rc != CHR_OK)
        show_error(i_testHandle, rc, "test_add_channel");

    /*
     * Create receiver objects.
     */
    receiver1 = create_receiver(&lc_receiverSpec1);

    /*
     * Create IPTV pairs.
     */
    create_pair(receiver1, channel1);
    create_pair(receiver1, channel2);
    create_pair(receiver1, channel3);

    /*
     * Add receivers to test.
     */
    rc = CHR_test_add_receiver(i_testHandle, receiver1);
    if (rc != CHR_OK)
        show_error(i_testHandle, rc, "test_add_receiver");

    /*
     * Set a time limit on the test.
     */
    rc = CHR_test_get_runopts(i_testHandle, &runOptionsHandle);
    if (rc != CHR_OK)
        show_error(i_testHandle, rc, "test_get_runopts");

    rc = CHR_runopts_set_test_duration(
            runOptionsHandle,
            TEST_DURATION);
    if (rc != CHR_OK)
        show_error(i_testHandle, rc, "runopts_set_test_duration");

    rc = CHR_runopts_set_test_end(
            runOptionsHandle,
            CHR_TEST_END_AFTER_FIXED_DURATION);
    if (rc != CHR_OK)
        show_error(i_testHandle, rc, "runopts_set_test_end");

} /* build_test */