Ejemplo n.º 1
0
/**
 * Muath documentation:
 * returns 1 (true) if there is a transition out of state 'state' not equal to lambda
 * end Muath documentation
 */
int isOtherLambdaOut(DFA* M, char* lambda, int state, int var){

	char* symbol;
	paths state_paths, pp;
	trace_descr tp;
	int j, sink;

    sink = find_sink(M);
    assert(sink >= 0);
    
	symbol = (char *) malloc((var + 1) * sizeof(char));
	state_paths = pp = make_paths(M->bddm, M->q[state]);

	while (pp) {
		if (pp->to != sink) {
			for (j = 0, tp = pp->trace; j < var && tp; j++, tp = tp->next) {
				if (tp) {
					if (tp->value)
						symbol[j] = '1';
					else
						symbol[j] = '0';
				} else
					symbol[j] = 'X';
			}
			symbol[j] = '\0';
			if (isNotExactEqual2Lambda(symbol, lambda, var))
				return 1;
		}
		pp = pp->next;
	} //end while
	return 0;
}
Ejemplo n.º 2
0
struct int_list_type *reachable_states_bounded_steps(DFA *M, int c1, int c2){

  paths state_paths, pp;
  int current;
  int steps;
  int sink = find_sink(M);

  struct int_list_type *worklist=NULL;
  struct int_list_type *nextlist=NULL;
  struct int_list_type *finallist = new_ilt();

  worklist = enqueue(worklist, M->s);

  for(steps=1; steps <=c2; steps++){
    while(worklist->count>0){
      current = dequeue(worklist); //dequeue returns the int value instead of the struct
      state_paths = pp = make_paths(M->bddm, M->q[current]);
      while (pp) {
	if(pp->to != sink){
	  nextlist=enqueue(nextlist, pp->to);
	  if(steps >= c1) finallist = enqueue(finallist, pp->to);
	}
	pp = pp->next;
      }
    }
    if(!nextlist) break;
    free(worklist);
    worklist = nextlist;
    nextlist = NULL;
  }

  print_ilt(finallist);
  return finallist;
}
Ejemplo n.º 3
0
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_domain_x500_decode(krb5_context context,
			krb5_data tr, char ***realms, unsigned int *num_realms,
			const char *client_realm, const char *server_realm)
{
    struct tr_realm *r = NULL;
    struct tr_realm *p, **q;
    int ret;

    if(tr.length == 0) {
	*realms = NULL;
	*num_realms = 0;
	return 0;
    }

    /* split string in components */
    ret = decode_realms(context, tr.data, tr.length, &r);
    if(ret)
	return ret;

    /* apply prefix rule */
    ret = expand_realms(context, r, client_realm);
    if(ret)
	return ret;

    ret = make_paths(context, r, client_realm, server_realm);
    if(ret)
	return ret;

    /* remove empty components and count realms */
    *num_realms = 0;
    for(q = &r; *q; ){
	if((*q)->realm[0] == '\0'){
	    p = *q;
	    *q = (*q)->next;
	    free(p->realm);
	    free(p);
	}else{
	    q = &(*q)->next;
	    (*num_realms)++;
	}
    }
    if (*num_realms + 1 > UINT_MAX/sizeof(**realms))
	return ERANGE;

    {
	char **R;
	R = malloc((*num_realms + 1) * sizeof(*R));
	if (R == NULL)
	    return krb5_enomem(context);
	*realms = R;
	while(r){
	    *R++ = r->realm;
	    p = r->next;
	    free(r);
	    r = p;
	}
    }
    return 0;
}
Ejemplo n.º 4
0
unsigned dfaGetDegree(DFA *M, unsigned state){
    int ssink = find_sink(M);
    unsigned sink;
    if (ssink == -1)
        sink = UINT_MAX;
    else
        sink = ssink;
    assert(state != sink);
    
    paths state_paths, pp;
    unsigned nextStatesSize = 16, nextStatesIndex = 0, degree = 0;
    unsigned *nextStates = (unsigned*) malloc((size_t) (nextStatesSize) * sizeof(unsigned));
    mem_zero(nextStates, (size_t) (nextStatesSize) * sizeof(unsigned));
    /*******************  find node degree *********************/
    state_paths = pp = make_paths(M->bddm, M->q[state]);
    while (pp) {
        if (pp->to != sink){
            bool found = false;
            for (nextStatesIndex = 0; nextStatesIndex < degree; nextStatesIndex++) {
                if (pp->to == nextStates[nextStatesIndex]){
                    found = true;
                    break;
                }
            }
            if (!found){
                if (degree < nextStatesSize){
                    nextStates[degree] = pp->to;
                }
                else {
                    unsigned oldSize = nextStatesSize;
                    nextStatesSize *= 2;
                    nextStates = (unsigned*) mem_resize(nextStates, (size_t)(nextStatesSize) * sizeof(unsigned));
                    mem_zero(nextStates + oldSize, (size_t) (nextStatesSize - oldSize) * sizeof(unsigned));
                    nextStates[degree] = pp->to;
                }
                degree++;
            }
        }
        pp = pp->next;
    }
    kill_paths(state_paths);
    free(nextStates);
    return degree;
}
Ejemplo n.º 5
0
/**
 * Muath documentation:
 * returns a list of states containing each state s that has at least one transition on lambda
 * into it and one transition on non-lambda out of it (except for sink state which is ignored)
 * end Muath documentation
 */
struct int_list_type *reachable_states_lambda_in_nout(DFA *M, char *lambda, int var){

	paths state_paths, pp;
	trace_descr tp;
	int j, current;
	int sink = find_sink(M);
	char *symbol;
	struct int_list_type *finallist=NULL;
	if(_FANG_DFA_DEBUG)dfaPrintVerbose(M);
	symbol=(char *)malloc((var+1)*sizeof(char));
	for(current=0; current<M->ns; current++){
		state_paths = pp = make_paths(M->bddm, M->q[current]);
		while (pp) {
			if(pp->to != sink){
				// construct transition from current to pp->to and store it in symbol
				for (j = 0, tp = pp->trace; j < var && tp; j++, tp = tp->next) {
					if (tp) {
						if (tp->value) symbol[j]='1';
						else symbol[j]='0';
					}
					else
						symbol[j]='X';
				}
				symbol[j]='\0';
				// if transition from current state to pp->to state is on labmda
				if(isEqual2Lambda(symbol, lambda, var)){
					// if there is a transition out of pp->to state on non-lambda then add pp->to to returned list
					if(isOtherLambdaOut(M, lambda, (pp->to), var)) finallist = enqueue(finallist, pp->to);
				}
			}
			pp = pp->next;
		}
	}

	if(_FANG_DFA_DEBUG) print_ilt(finallist);
	free(symbol);
	return finallist;
}
Ejemplo n.º 6
0
/*
 TODO: should return transition relation with the sink in it then add a function to
 remove the sink.
 This will break Tarjan length algorithm so it should be changed to take relation
 after removing the sink.
 it will also break pre_add_slashes which should not do any shifting
 */
pTransitionRelation dfaGetTransitionRelation(DFA *M){
    unsigned state, degree, nextState, i;
    state = degree = nextState = 0;
    paths state_paths, pp;
    
    int sink = find_sink(M);
    assert(sink >= 0);//assert that there is a sink
    
    pTransitionRelation p_transitionRelation = (pTransitionRelation) malloc(sizeof(transitionRelation));
    p_transitionRelation->reverse = false;
    p_transitionRelation->selfCycles = false;
    p_transitionRelation->sink = (unsigned)sink;
    p_transitionRelation->num_of_nodes = M->ns - 1;
    p_transitionRelation->num_of_edges = 0;
    p_transitionRelation->adjList = (unsigned**) malloc((size_t)
                                                        (p_transitionRelation->num_of_nodes) * sizeof(unsigned*));
    mem_zero(p_transitionRelation->adjList, (size_t) p_transitionRelation->num_of_nodes * sizeof(unsigned*) );
    p_transitionRelation->degrees = (t_st_word*) malloc((size_t)
                                                             (p_transitionRelation->num_of_nodes) * sizeof(t_st_word));
    mem_zero(p_transitionRelation->degrees, (size_t) p_transitionRelation->num_of_nodes * sizeof(t_st_word));
    bool *nextStates = (bool*) malloc((size_t) (p_transitionRelation->num_of_nodes) * sizeof(bool));
    
    unsigned numOfAcceptStates = 0;
    // a heuristic assuming 1/20th of states are accepting
    unsigned acceptSizeTemp = (M->ns < 100)? 4 : roundToNextPow2(M->ns / 20);
    p_transitionRelation->acceptsSize = (p_transitionRelation->num_of_nodes < acceptSizeTemp)? p_transitionRelation->num_of_nodes : acceptSizeTemp;
    p_transitionRelation->accepts = (unsigned *) mem_alloc((size_t) p_transitionRelation->acceptsSize * sizeof(unsigned));
    mem_zero(p_transitionRelation->accepts, (size_t) p_transitionRelation->acceptsSize * sizeof(unsigned));
    
    for (i = 0; i < M->ns; i++){
        if (M->f[i] == 1){
            p_transitionRelation->accepts[numOfAcceptStates++] = (i < sink)? i : i - 1;
            if (numOfAcceptStates == p_transitionRelation->acceptsSize){
                unsigned acceptSizeTemp = p_transitionRelation->acceptsSize * 2;
                p_transitionRelation->acceptsSize = (p_transitionRelation->num_of_nodes < acceptSizeTemp)? p_transitionRelation->num_of_nodes : acceptSizeTemp;
                p_transitionRelation->accepts = (unsigned*) mem_resize(p_transitionRelation->accepts, (size_t) p_transitionRelation->acceptsSize * sizeof(unsigned));
                mem_zero(p_transitionRelation->accepts + numOfAcceptStates, (size_t) (p_transitionRelation->acceptsSize - numOfAcceptStates) * sizeof(unsigned));
            }
        }
        
        if (sink == i)
            continue;
        else if (sink < i)
            state = i - 1;//shift state
        else
            state = i;
//        printf("i = %d for state = %u\n", i, state);
        /*******************  find node degree *********************/
        memset(nextStates, false, sizeof(bool) * (p_transitionRelation->num_of_nodes));
        state_paths = pp = make_paths(M->bddm, M->q[i]);
        while (pp) {
            if (pp->to == i)
                p_transitionRelation->selfCycles = true;
            unsigned to = (sink < pp->to)? pp->to - 1 : pp->to;
            if (pp->to != sink){
                if (nextStates[to] == false){
                    nextStates[to] = true;
                    p_transitionRelation->degrees[state]++;
                    p_transitionRelation->num_of_edges++;
                }
            }
            pp = pp->next;
        }
        kill_paths(state_paths);
        /*******************  allocate node's adjacency list and fill it up *********************/
        if (p_transitionRelation->degrees[state] == 0) {
            p_transitionRelation->adjList[state]  = NULL;
        }
        else {
            p_transitionRelation->adjList[state] = (unsigned *) malloc((size_t) (p_transitionRelation->degrees[state]) * sizeof(unsigned) );
            mem_zero(p_transitionRelation->adjList[state],(size_t) (p_transitionRelation->degrees[state]) * sizeof(unsigned));
            for (nextState = 0, degree = 0; nextState < p_transitionRelation->num_of_nodes; nextState++) {
                if (nextStates[nextState] == true){
                    assert(degree < p_transitionRelation->degrees[state]);
                    p_transitionRelation->adjList[state][degree] = nextState;
                    degree++;
                }
            }
        }
    }
    
    p_transitionRelation->acceptsSize = numOfAcceptStates;
    p_transitionRelation->accepts = (unsigned*) mem_resize((p_transitionRelation->accepts), ((p_transitionRelation->acceptsSize) * sizeof(unsigned)));

    qsort(p_transitionRelation->accepts, p_transitionRelation->acceptsSize, sizeof(unsigned), intcmpfunc);
    
    free(nextStates);
    return p_transitionRelation;
}
Ejemplo n.º 7
0
DFA *dfa_Suffix(DFA *M, int c1, int c2, int var, int *oldindices)
{
  DFA *result = NULL;
  DFA *tmpM = NULL;
  int aux=0;
  struct int_list_type *states=NULL;
  struct int_type *tmpState=NULL;

  int maxCount = 0;

  int *indices = oldindices; //indices is updated if you need to add auxiliary bits

  paths state_paths, pp;
  trace_descr tp;

  int i, j, z, k;

  char *exeps;
  int *to_states;
  long max_exeps;
  char *statuces;
  int len=var;
  int sink;

  char *auxbit=NULL;


  //	char *apath =bintostr(a, var);

  states = reachable_states_bounded_steps(M, c1, c2);
  maxCount = states->count;

  if(maxCount>0){ //Need auxiliary bits when there exist some outgoing edges
    aux = get_hsig(maxCount);
    if(_FANG_DFA_DEBUG) printf("\n There are %d reachable states, need to add %d auxiliary bits\n", maxCount, aux);
    auxbit = (char *) malloc(aux*sizeof(char));
    len = var+aux; // extra aux bits
    indices = allocateArbitraryIndex(len);
  }



  max_exeps=1<<len; //maybe exponential
  sink=find_sink(M);
  assert(sink >-1);

  //pairs[i] is the list of all reachable states by \sharp1 \bar \sharp0 from i


  dfaSetup(M->ns+1, len, indices); //add one new initial state
  exeps=(char *)malloc(max_exeps*(len+1)*sizeof(char)); //plus 1 for \0 end of the string
  to_states=(int *)malloc(max_exeps*sizeof(int));
  statuces=(char *)malloc((M->ns+2)*sizeof(char));

  //printf("Before Replace Char\n");
  //dfaPrintVerbose(M);

  k=0;
  //setup for the initial state
  tmpState = states->head;
  for (z=1; z<=states->count; z++) {
    state_paths = pp = make_paths(M->bddm, M->q[tmpState->value]);
    while (pp) {
      if(pp->to!=sink){
	to_states[k]=pp->to+1; //insert itself as the initial state
	for (j = 0; j < var; j++) {
	  //the following for loop can be avoided if the indices are in order
	  for (tp = pp->trace; tp && (tp->index != indices[j]); tp =tp->next);
	  if (tp) {
	    if (tp->value) exeps[k*(len+1)+j]='1';
	    else exeps[k*(len+1)+j]='0';
	  }else{
	    exeps[k*(len+1)+j]='X';
	  }
	}
	set_bitvalue(auxbit, aux, z); // aux = 3, z=4, auxbit 001
	for (j = var; j < len; j++) { //set to xxxxxxxx100
	  exeps[k*(len+1)+j]=auxbit[len-j-1];
	}
	exeps[k*(len+1)+len]='\0';
	k++;
      }
      pp = pp->next;
    }//end while
    kill_paths(state_paths);
    tmpState = tmpState->next;
  } //end for

  dfaAllocExceptions(k);
  for(k--;k>=0;k--)
    dfaStoreException(to_states[k],exeps+k*(len+1));
  dfaStoreState(sink+1);

  if(check_accept(M, states))	statuces[0]='+';
  else 	statuces[0]='0';



  //for the rest of states (shift one state)
  for (i = 0; i < M->ns; i++) {

    state_paths = pp = make_paths(M->bddm, M->q[i]);
    k=0;

    while (pp) {
      if(pp->to!=sink){
	for (tp = pp->trace; tp && (tp->index != indices[var]); tp =tp->next); //find the bar value
	if (!tp || !(tp->value)) {
	  to_states[k]=pp->to+1;
	  for (j = 0; j < var; j++) {
	    //the following for loop can be avoided if the indices are in order
	    for (tp = pp->trace; tp && (tp->index != indices[j]); tp =tp->next);

	    if (tp) {
	      if (tp->value) exeps[k*(len+1)+j]='1';
	      else exeps[k*(len+1)+j]='0';
	    }
	    else
	      exeps[k*(len+1)+j]='X';
	  }
	  for (j = var; j < len; j++) {
	    exeps[k*(len+1)+j]='0';
	  }
	  exeps[k*(len+1)+len]='\0';
	  k++;
	}
      }
      pp = pp->next;
    }//end while

    dfaAllocExceptions(k);
    for(k--;k>=0;k--)
      dfaStoreException(to_states[k],exeps+k*(len+1));
    dfaStoreState(sink+1);

    if(M->f[i]==1)
      statuces[i+1]='+';
    else if(M->f[i]==-1)
      statuces[i+1]='-';
    else
      statuces[i+1]='0';

    kill_paths(state_paths);
  }

  statuces[M->ns+1]='\0';
  result=dfaBuild(statuces);
  //	dfaPrintVerbose(result);
  for(i=0; i<aux; i++){
    j=len-i-1;
    tmpM =dfaProject(result, (unsigned) j);
    dfaFree(result);
    result = dfaMinimize(tmpM);
    dfaFree(tmpM);
    //		printf("\n After projecting away %d bits", j);
    //		dfaPrintVerbose(result);
  }
  free(exeps);
  //printf("FREE ToState\n");
  free(to_states);
  //printf("FREE STATUCES\n");
  free(statuces);

  if(maxCount>0) free(auxbit);

  free_ilt(states);

  return dfaMinimize(result);

}
Ejemplo n.º 8
0
static void
merge_spec_from_root (xmlNodePtr root, const gchar *provider, xmlDocPtr pdoc)
{
	/* make list of paths */
	GSList *paths, *list;

	paths = make_paths (xmlDocGetRootElement (pdoc), "", NULL);
	for (list = paths; list; list = list->next) {
		Path *path = (Path *) list->data;

		/* find node for this path ID, and create it if not yet present */
		xmlNodePtr node = NULL;
		for (node = root->children; node; node = node->next) {
			if (!strcmp ((gchar*) node->name, "path")) {
				xmlChar *pid;
				pid = xmlGetProp (node, BAD_CAST "id");
				if (pid) {
					if (!strcmp ((gchar*) pid, path->path)) {
						xmlFree (pid);
						break;
					}
					xmlFree (pid);
				}
			}
			    
		}
		if (!node) {
			node = xmlNewChild (root, NULL, BAD_CAST "path", NULL);
			xmlSetProp (node, BAD_CAST "id", BAD_CAST path->path);
			xmlSetProp (node, BAD_CAST "node_type", BAD_CAST node_type_to_string (path->node_type));
			if (path->type && *path->type)
				xmlSetProp (node, BAD_CAST "gdatype", BAD_CAST path->type);	
		}

		/* add this provider's specific information */
		xmlNodePtr pnode;
		if (!prov) {
			pnode = xmlNewChild (node, NULL, BAD_CAST "prov", NULL);
			xmlSetProp (pnode, BAD_CAST "prov_name", BAD_CAST provider);
		}
		else
			pnode = node;
		xmlSetProp (pnode, BAD_CAST "name", BAD_CAST path->name);
		if (path->descr && *path->descr)
			xmlSetProp (pnode, BAD_CAST "descr", BAD_CAST path->descr);

		if (!prov) {
			/* check node type is similar to "node->node_type" */
			xmlChar *node_type;
			node_type = xmlGetProp (node, BAD_CAST "node_type");
			g_assert (node_type);
			if (strcmp ((gchar*) node_type, 
				    node_type_to_string (path->node_type)))
				xmlSetProp (pnode, BAD_CAST "node_type", 
					    BAD_CAST node_type_to_string (path->node_type));
			xmlFree (node_type);
			
			/* check gdatype is similar to "node->gdatype" */
			node_type = xmlGetProp (node, BAD_CAST "gdatype");
			if ((node_type && path->type && 
			     strcmp ((gchar*) node_type, path->type)) ||
			    (!node_type && path->type) ||
			    (node_type && *node_type && !path->type))
				xmlSetProp (pnode, BAD_CAST "gdatype", BAD_CAST path->type);
			if (node_type) 
				xmlFree (node_type);
		}
	}
	
	/* mem free */
	g_slist_foreach (paths, (GFunc) path_free, NULL);
	g_slist_free (paths);
}
Ejemplo n.º 9
0
/* create a list of Path structures */
static GSList *
make_paths (xmlNodePtr node, const gchar *parent_path, GSList *exist_list)
{
	xmlChar *id;
	GSList *retlist = exist_list;
	id = xmlGetProp (node, BAD_CAST "id");
	if (id) {
		Path *path;
		gchar *pstr = g_strdup_printf ("%s/%s", parent_path, id);

		path = g_new0 (Path, 1);
		path->path = pstr;
		retlist = g_slist_append (retlist, path);

		if (!strcmp ((gchar*) node->name, "parameters"))
			path->node_type = GDA_SERVER_OPERATION_NODE_PARAMLIST;
		else if (!strcmp ((gchar*) node->name, "parameter"))
			path->node_type = GDA_SERVER_OPERATION_NODE_PARAM;
		else if (!strcmp ((gchar*) node->name, "sequence"))
			path->node_type = GDA_SERVER_OPERATION_NODE_SEQUENCE;
		else if (!strcmp ((gchar*) node->name, "gda_array"))
			path->node_type = GDA_SERVER_OPERATION_NODE_DATA_MODEL;
		else if (!strcmp ((gchar*) node->name, "gda_array_field")) {
			path->node_type = GDA_SERVER_OPERATION_NODE_DATA_MODEL_COLUMN;
			g_free (path->path);
			pstr = g_strdup_printf ("%s/@%s", parent_path, id);
			path->path = pstr;
		}
		else
			path->node_type = GDA_SERVER_OPERATION_NODE_UNKNOWN;

		xmlChar *prop;
		prop = xmlGetProp (node, BAD_CAST "name");
		if (prop) {
			path->name = g_strdup ((gchar*) prop);
			xmlFree (prop);
		}
		prop = xmlGetProp (node, BAD_CAST "descr");
		if (prop) {
			path->descr = g_strdup ((gchar*) prop);
			xmlFree (prop);
		}
		prop = xmlGetProp (node, BAD_CAST "gdatype");
		if (prop) {
			path->type = g_strdup ((gchar*) prop);
			xmlFree (prop);
		}

		/* children */
		xmlNodePtr child;
		for (child = node->children; child; child = child->next) 
			retlist = make_paths (child, pstr, retlist);

		xmlFree (id);
	}
	else {
		/* children */
		xmlNodePtr child;
		for (child = node->children; child; child = child->next) 
			retlist = make_paths (child, parent_path, retlist);
	}

	return retlist;
}