예제 #1
0
void Test_dictionary_dump(CuTest *tc)
{
    int i, j;
    char sec_name[32];
    char key_name[64];
    dictionary *dic;
    char *dump_buff;
    const char dump_real[] = "\
                sec1\t[]\n\
           sec1:key1\t[dummy_value]\n\
           sec1:key2\t[dummy_value]\n\
           sec1:key3\t[dummy_value]\n\
           sec1:key4\t[dummy_value]\n\
                sec2\t[]\n\
           sec2:key1\t[dummy_value]\n\
           sec2:key2\t[dummy_value]\n\
           sec2:key3\t[dummy_value]\n\
           sec2:key4\t[dummy_value]\n\
";

    dic = dictionary_new(DICTMINSZ);
    CuAssertPtrNotNull(tc, dic);

    /* Try dummy values */
    dictionary_dump(NULL, NULL);
    dictionary_dump(dic, NULL);

    /* Try with empty dictionary first */
    dump_buff = get_dump(dic);
    CuAssertStrEquals(tc, "empty dictionary\n", dump_buff);
    free(dump_buff);

    /* Populate the dictionary */
    for (i = 1 ; i < 3; ++i) {
        sprintf(sec_name, "sec%d", i);
        dictionary_set(dic, sec_name, "");
        for (j = 1 ; j < 5; ++j) {
            sprintf(key_name, "%s:key%d", sec_name, j);
            dictionary_set(dic, key_name, "dummy_value");
        }
    }

    /* Check the dump file */
    dump_buff = get_dump(dic);
    CuAssertStrEquals(tc, dump_real, dump_buff);
    free(dump_buff);

    dictionary_del(dic);
}
예제 #2
0
static char *get_dump(dictionary *d)
{
    FILE *fd;
    char *dump_buff;
    int dump_size;

    /* Dump the dictionary in temporary file */
    fd = tmpfile();
    if (fd == NULL)
        return NULL;
    dictionary_dump(d, fd);

    /* Retrieve the dump file */
    dump_size = ftell(fd);
    if (dump_size == -1) {
        fclose(fd);
        return NULL;
    }
    rewind(fd);

    dump_buff = (char*) calloc(1, dump_size + 1);
    if (dump_buff == NULL) {
        fclose(fd);
        return NULL;
    }
    fread(dump_buff, 1, dump_size, fd);

    fclose(fd);
    return dump_buff;
}
예제 #3
0
static void grow(Process *p)
{
    unsigned int i,j;
    unsigned int steps = p->dictionary->homeSize / 5;
    Eterm l1,l2;
    Eterm l;
    Eterm *hp;
    unsigned int pos;
    unsigned int homeSize;
    int needed = 0;
    ProcDict *pd;
#ifdef DEBUG
    Eterm *hp_limit;
#endif

    HDEBUGF(("grow: steps = %d", steps));
    if (steps == 0)
	steps = 1;
    /* Dont grow over MAX_HASH */
    if ((MAX_HASH - steps) <= HASH_RANGE(p->dictionary)) {
	return;
    }

    /*
     * Calculate total number of heap words needed, and garbage collect
     * if necessary.
     */

    pd = p->dictionary;
    pos = pd->splitPosition;
    homeSize = pd->homeSize;
    for (i = 0; i < steps; ++i) {
	if (pos == homeSize) {
	    homeSize *= 2;
	    pos = 0;
	}
	l = ARRAY_GET(pd, pos);
	pos++;
	if (is_not_tuple(l)) {
	    while (l != NIL) {
		needed += 2;
		l = TCDR(l);
	    }
	}
    }
    if (HeapWordsLeft(p) < needed) {
	BUMP_REDS(p, erts_garbage_collect(p, needed, 0, 0));
    }
#ifdef DEBUG
    hp_limit = p->htop + needed;
#endif

    /*
     * Now grow.
     */

    for (i = 0; i < steps; ++i) {
	ProcDict *pd = p->dictionary;
	if (pd->splitPosition == pd->homeSize) {
	    pd->homeSize *= 2;
	    pd->splitPosition = 0;
	}
	pos = pd->splitPosition;
	++pd->splitPosition; /* For the hashes */
	l = ARRAY_GET(pd, pos);
	if (is_tuple(l)) {
	    if (pd_hash_value(pd, tuple_val(l)[1]) != pos) {
		array_put(&(p->dictionary), pos + 
			  p->dictionary->homeSize, l);
		array_put(&(p->dictionary), pos, NIL);
	    }
	} else {
	    l2 = NIL;
	    l1 = l;
	    for (j = 0; l1 != NIL; l1 = TCDR(l1))
		j += 2;
	    hp = HeapOnlyAlloc(p, j);
	
	    while (l != NIL) {
		if (pd_hash_value(pd, tuple_val(TCAR(l))[1]) == pos) 
		    l1 = CONS(hp, TCAR(l), l1);
		else
		    l2 = CONS(hp, TCAR(l), l2);
		hp += 2;
		l = TCDR(l);
	    }
	    if (l1 != NIL && TCDR(l1) == NIL)
		l1 = TCAR(l1);
	    if (l2 != NIL && TCDR(l2) == NIL)
		l2 = TCAR(l2);
	    ASSERT(hp <= hp_limit);
	    /* After array_put pd is no longer valid */
	    array_put(&(p->dictionary), pos, l1);
	    array_put(&(p->dictionary), pos + 
		      p->dictionary->homeSize, l2);
	}
    }

#ifdef HARDDEBUG
    dictionary_dump(p->dictionary,CERR);
#endif
}
예제 #4
0
파일: kb-common.c 프로젝트: kbase/auth_lite
int validate_token(bstring token)
{
    int i;

    OpenSSL_add_all_algorithms();

    struct bstrList *parts = bsplit(token, '|');

    dictionary *dict = dictionary_new(10);

    bstring sig = bfromcstr("sig");
    bstring to_sign = bfromcstr("");
    
    for (i = 0; i < parts->qty; i++)
    {
	printf("%d: %s\n", i, parts->entry[i]->data);

	struct bstrList *x = bsplit(parts->entry[i], '=');
	if (x->qty == 2)
	{
	    if (bstrcmp(x->entry[0], sig) != 0)
	    {
		if (blength(to_sign) > 0)
		    bconchar(to_sign, '|');
		bconcat(to_sign, parts->entry[i]);
	    }
	    dictionary_set(dict, bdata(x->entry[0]), bdata(x->entry[1]));
	}
	bstrListDestroy(x);
    }
    bstrListDestroy(parts);
    parts = 0;
    bdestroy(sig);
    
    dictionary_dump(dict, stdout);
    printf("to sign: '%s'\n", bdata(to_sign));

    // Check signing subject (need to know the valid values)

    char *subj = dictionary_get(dict, "SigningSubject", 0);
    if (!subj)
    {
	fprintf(stderr, "could not get signing subject\n");
	return 0;
    }

    char *sigstr = dictionary_get(dict, "sig", 0);
    printf("sig to verify is %s\n", sigstr);

    bstring binsig = bfromcstralloc(strlen(sigstr) / 2, "");
    char *s, *e;
    for (s = sigstr, e = sigstr + strlen(sigstr); s < e; s += 2)
    {
	char n[3];
	n[0] = s[0];
	n[1] = s[1];
	n[2] = 0;
	long int v = strtol(n, 0, 16);
//	printf("n=%s v=%ld %lx\n", n, v, v);
	bconchar(binsig, (char) v);
    }

    unsigned char *sha = SHA1((const unsigned char *) to_sign->data, to_sign->slen, 0);

    bdestroy(to_sign);

    bstring bsubj = bfromcstr(subj);
    bstring pubkey = get_signer_pubkey(bsubj);

    BIO *bio = BIO_new(BIO_s_mem());
    BIO_puts(bio, bdata(pubkey));

    RSA *rsa = PEM_read_bio_RSAPublicKey(bio, 0, 0, 0);

    int rc = RSA_verify(NID_sha1, sha, SHA_DIGEST_LENGTH, binsig->data, binsig->slen, rsa);
    printf("rc=%d\n", rc);

    bdestroy(bsubj);
    bdestroy(binsig);
    bdestroy(pubkey);
    BIO_free(bio);
    RSA_free(rsa);
    dictionary_del(dict);
    EVP_cleanup();
    
    return rc;
}
예제 #5
0
/*--------------------------------------------------------------------------*/
void iniparser_dump(dictionary * d, FILE * f)
{
    dictionary_dump(d,f);
}