Ejemplo n.º 1
0
static void
w_init_maybe(int m, int n)
{
    int i;

    if (m > n) {
	i = n; n = m; m = i;
    }
    if (w && (m > allocated_m || n > allocated_n))
	w_free(allocated_m, allocated_n); /* zeroes w */

    if (!w) { /* initialize w[][] */
	m = imax2(m, WILCOX_MAX);
	n = imax2(n, WILCOX_MAX);
	w = (double ***) calloc((size_t) m + 1, sizeof(double **));
#ifdef MATHLIB_STANDALONE
	if (!w) MATHLIB_ERROR(_("wilcox allocation error %d"), 1);
#endif
	for (i = 0; i <= m; i++) {
	    w[i] = (double **) calloc((size_t) n + 1, sizeof(double *));
#ifdef MATHLIB_STANDALONE
	    /* the apparent leak here in the in-R case should be
	       swept up by the on.exit action */
	    if (!w[i]) {
		/* first free all earlier allocations */
		w_free(i-1, n);
		MATHLIB_ERROR(_("wilcox allocation error %d"), 2);
	    }
#endif
	}
	allocated_m = m; allocated_n = n;
    }
}
Ejemplo n.º 2
0
int haveServerKey(const char *host, const char *username){
    char *file_path = NULL;
    const char *user_home = NULL;
    FILE *key_file = NULL;
    
    if ((user_home = getUserhome(username)) == NULL)
	report_error_q("Unable to find user's home directory", __FILE__, __LINE__, 0);
    
    file_path = (char *)w_malloc(strlen(host) + strlen(user_home) + 15);
    
    strncpy(file_path, user_home, strlen(user_home));
    strncat(file_path, "/.", 2);
    strncat(file_path, host, strlen(host));
    strncat(file_path, ".priv", strlen(".priv"));
    
    if ((key_file = fopen(file_path, "r")) == NULL){
	w_free(file_path);
	return -1;
    }
    else {
	fclose(key_file);
	w_free(file_path);
	return 0;
    }
}
Ejemplo n.º 3
0
void WaveEnvelope::link(WaveFile const& source)
{
	// If instance have WaveFile already, then free and malloc again.
	if(rootFile != NULL)
	{
		w_free();
	}
	
	// Memory Protection
	if(w_malloc(source.length, source.channel))
	{
		if(WENV_DEBUG)
		{
			printf("[WaveEnvelope : General] Wave link done!\n");
		}
		
		rootFile    = &source;
	
		length      = rootFile->length;
		channel     = rootFile->header.getChannel();
		sample_rate = rootFile->header.getSampleRate();
	}
	else
	{
		if(WENV_DEBUG)
		{
			printf("[WaveEnvelope : Error] Wave link fail! : Not enough memory\n");
		}
		
		init();
	}
}
Ejemplo n.º 4
0
void WaveEnvelope::init(void)
{
	// If instance have WaveFile already, then free the WaveContainer.
	if(rootFile != NULL)
	{
		w_free();
	}
	rootFile    = NULL;
	
	length      = 0;
	channel     = 0;
	sample_rate = 0;
}
Ejemplo n.º 5
0
static void search_destroy_tree(search_node *node)
{
	unsigned int i;


	/* check parameters */
	ASSERT(node != (search_node *)0);

	for (i = 0; i < node->numchildren; i++)
	{
		search_destroy_tree(node->children[i]);
		w_free(node->children[i]);
	}
}
Ejemplo n.º 6
0
SSL *ssl_client_connect(const char *host, const char *port){
    SSL_METHOD *my_ssl_method;
    SSL_CTX *my_ssl_ctx;
    SSL *my_ssl;
    BIO *my_bio;
    char *host_port;
    
    host_port = w_malloc(strlen(host) + strlen(port) + 2);
    sprintf(host_port, "%s: %s", port, port);
    my_ssl_method = TLSv1_client_method();
    
    if ((my_ssl_ctx = SSL_CTX_new(my_ssl_method)) == NULL)
	return NULL;
    if ((my_ssl = SSL_new(my_ssl_ctx)) == NULL){
	SSL_CTX_free(my_ssl_ctx);
	return NULL;
    }
    if ((my_bio = BIO_new_connect(host_port)) == NULL){
	SSL_free(my_ssl);
	w_free(host_port);
	return NULL;
    }
    if (BIO_do_connect(my_bio) <= 0){
	SSL_free(my_ssl);
	BIO_free(my_bio);
	w_free(host_port);
	return NULL;
    }
    SSL_set_bio(my_ssl, my_bio, my_bio);
    if (SSL_connect(my_ssl) <= 0){
	SSL_free(my_ssl);
	w_free(host_port);
	return NULL;
    }
    w_free(host_port);
    return my_ssl;
}
Ejemplo n.º 7
0
RSA *getServerKey(const char *host, const char *username){
    char *file_path = NULL;
    const char *user_home = NULL;
    RSA *my_key = NULL;
    
    if ((user_home = getUserhome(username)) == NULL)
	report_error_q("Unable to find user's name directory.", __FILE__, __LINE__, 0);
    
    file_path = (char *)w_malloc(strlen(host) + strlen(user_home) + 15);
    
    strncpy(file_path, user_home, strlen(user_home));
    strncat(file_path, "/.", 2);
    strncat(file_path, host, strlen(host));
    strncat(file_path, ".priv", strlen(".priv"));
    my_key = key_read_priv(file_path);
    w_free(file_path);
    return my_key;
}
Ejemplo n.º 8
0
const char *getUserhome(const char *username){
    static char *home_buffer = NULL;
    struct passwd *current_pwent = NULL;
    
    current_pwent = getpwent();
    if (home_buffer != NULL){
	w_free(home_buffer);
	home_buffer = NULL;
    }

    while (current_pwent){
	if (strcasecmp(username, current_pwent -> pw_name) == 0){
	    home_buffer = (char *)w_malloc(strlen(current_pwent -> pw_dir) + 1);
	    strncpy(home_buffer, current_pwent -> pw_dir, strlen(current_pwent -> pw_dir) + 1);
	}
	current_pwent = getpwent();
    }
    endpwent();
    return home_buffer;
}
Ejemplo n.º 9
0
static void
w_init_maybe(int n)
{
    int u, c;

    u = n * (n + 1) / 2;
    c = (u / 2);

    if (w) {
        if(n != allocated_n) {
	    w_free();
	}
	else return;
    }

    if(!w) {
	w = (double *) calloc((size_t) c + 1, sizeof(double));
#ifdef MATHLIB_STANDALONE
	if (!w) MATHLIB_ERROR("%s", _("signrank allocation error"));
#endif
	allocated_n = n;
    }
}
Ejemplo n.º 10
0
const char *getUsername(void){
    static char *username_buffer = NULL;
    uid_t my_uid;
    struct passwd *current_pwent = NULL;
    
    my_uid = getuid();
    current_pwent = getpwent();
    
    if (username_buffer != NULL){
	w_free(username_buffer);
	username_buffer = NULL;
    }
    
    while (current_pwent && !username_buffer){
	if (current_pwent -> pw_uid == my_uid){
	    username_buffer = (char *)w_malloc(strlen(current_pwent -> pw_name) + 1);
	    strncpy(username_buffer, current_pwent -> pw_name, strlen(current_pwent -> pw_name) + 1);
	}
	current_pwent = getpwent();
    }
    endpwent();
    return username_buffer;
}
Ejemplo n.º 11
0
static void
_w_opt_parse_file (w_parse_t *p, void *ctx)
{
    w_opt_status_t status;
    const w_opt_t *options = (w_opt_t*) ctx;
    const w_opt_t *opt;
    char *token;
    char **args;
    unsigned maxarg = 0;
    unsigned narg = 0;

    w_assert (p != NULL);
    w_assert (options != NULL);

    while (w_likely (p->look != W_IO_EOF)) {
        if ((token = w_parse_word (p)) == NULL) {
            w_parse_error (p, "Identifier expected");
        }

        opt = _opt_lookup_long (options, token);

        if (opt == NULL || CLI_LETTER (opt->letter)) {
            w_parse_ferror (p, "No such option '$s'", token);
            w_free (token);
            w_parse_rerror (p);
        }

        if (opt->narg > 0) {
            narg   = 0;
            maxarg = W_OPT_PARSE_ARG_CHUNK;
            args   = w_alloc (char*, maxarg);

            while (narg < opt->narg && p->look != W_IO_EOF) {
                if (narg >= maxarg) {
                    maxarg += W_OPT_PARSE_ARG_CHUNK;
                    args    = w_resize (args, char*, maxarg);
                }
                args[narg++] = (p->look == '"')
                    ? w_parse_string (p)
                    : w_parse_word (p);
            }

            /* Not enough arguments given */
            if (p->look != W_IO_EOF && narg < opt->narg) {
                unsigned i;
                for (i = 0; i < narg; i++)
                    w_free (args[i]);
                w_free (args);

                w_parse_ferror (p, "Insufficient arguments to '$s'", token);
                w_free (token);
                w_parse_rerror (p);
            }

            {
                /* Ok, arguments gathered, now let's try to invoke the action */
                w_opt_context_t oc = { narg, args, opt, NULL, args };
                status = (*opt->action) (&oc);
            }

            /* Clean up after ourselves */
            for (narg = 0; narg < opt->narg; narg++)
                w_free (args[narg]);
            w_free (args);

        }
Ejemplo n.º 12
0
static void search_basic(search_configuration *conf)
{
	thread *workers;
	rating_t rating;
	unsigned int i;
	time_t start, stop;


	/* check parameters */
	ASSERT(conf   != (search_configuration *)0);

	/* get start time */
	start = time(NULL);

	/* determine search depths */
	conf->precalcdepth = MIN(3, conf->problem->depth);
	printf("(%i/%i) ", conf->precalcdepth, conf->problem->depth);

	/* initialize search configuration */
	conf->numleafnodes		= 0;
	conf->numleafnodesdone	= 0;
	conf->numleafnodesleft	= 0;
	conf->nextleaf			= (search_node *)0;

	/* initalize search result */
	conf->result->rating = RATING_INIT_INF;
	ML_CLEAR(conf->result->solution);
	ML_CLEAR(conf->result->bestmoves);
	conf->result->numnodes = 0;
	conf->result->time     = 0;

	/* create search tree */
	conf->rootnode = (search_node *)w_malloc(sizeof(search_node));
	search_create_tree(conf, &(conf->problem->pos), conf->rootnode, 0);

	/* check if basic search tree contains the solution */
	rating = search_update_ratings(conf->rootnode, conf->problem->pos.player, 0);
	if (rating != RATING_INIT_INF)
	{
		conf->result->rating = rating;
		search_collect_result(conf);
		printf("result already found in precalculated tree");
	}
	/* check if no further search necessary */
	else if (conf->problem->depth <= conf->precalcdepth)
	{
		printf("no result found in precalculated tree, no main search necessary");
	}
	/* if not, start the main search */
	else
	{
		/* all leaf nodes are searched again in main search: no not count them twice */
		conf->result->numnodes -= conf->numleafnodes;
		/* prepare stats bar */
		search_init_statsbar(stdout);
		/* prepare threads */
		conf->numleafnodesleft = conf->numleafnodes;
		workers = (thread *)w_malloc(conf->numthreads * sizeof(thread));
		mutex_create(&(conf->workerlock));
		/* create and start all working threads */
		for (i = 0; i < conf->numthreads; i++)
		{
			thread_create(&(workers[i]), search_mainsearch_thread, (void *)conf);
		}
		/* supend main thread until all jobs have been finished */
		for (i = 0; i < conf->numthreads; i++)
		{
			thread_join(&(workers[i]));
		}
		/* workers should have finished all jobs */
		ASSERT(conf->numleafnodesleft == 0);
		ASSERT(conf->numleafnodes     == conf->numleafnodesdone);
		ASSERT(conf->nextleaf         == (search_node *)0);
		/* clean up */
		mutex_destroy(&(conf->workerlock));
		w_free(workers);
		/* extract result from search tree */
		rating = search_update_ratings(conf->rootnode, conf->problem->pos.player, 0);
		if (rating != RATING_INIT_INF)
		{
			conf->result->rating = rating;
			search_collect_result(conf);
		}
	}
	printf(" (");
	si_print(stdout, conf->result->numnodes);
	printf("N)\n");

	/* clean up */
	search_destroy_tree(conf->rootnode);
	w_free(conf->rootnode);

	/* get total time */
	stop = time(NULL);
	conf->result->time = stop - start;
}
Ejemplo n.º 13
0
void signrank_free(void)
{
    w_free();
}
Ejemplo n.º 14
0
int main(int argc, char* argv[]){
    SSL *ssl_connection = NULL;
    const char *host = NULL, *port = NULL;
    const char *username = NULL;
    char *response = NULL;
    char *signed_data_buffer = NULL;
    unsigned int signed_data_buffer_size = 0;
    RSA *my_rsa_key = NULL;
    
    if (argc != 3){
	fprintf(stderr, "Usage: %s host port.\n", argv[0]);
	exit(EXIT_FAILURE);
    }

    w_memory_init();
    openssl_init();
    
    host = argv[1];
    port = argv[2];
    username = getUsername();
    
    if (username == NULL)
	report_error_q("Unable to determine the username of this process.", __FILE__, __LINE__, 0);
 
    if((ssl_connection = ssl_client_connect(host, port)) == NULL)
	report_error_q(ERR_error_string(ERR_get_error(), NULL), __FILE__, __LINE__, 0);
    
    if (haveServerKey(host, username) == 0){
	ssl_write_uint(ssl_connection, REQUEST_KEY_AUTH);
	ssl_write_string(ssl_connection, username);
	my_rsa_key = getServerKey(host, username);
	if (my_rsa_key == NULL)
	    report_error_q("Key file exists, but data is invalid", __FILE__, __LINE__, 0);
    
    
	signed_data_buffer = (char *)w_malloc(key_buffer_size(my_rsa_key));
	signed_data_buffer_size = key_sign_data(my_rsa_key, username, strlen(username), signed_data_buffer, key_buffer_size(my_rsa_key));
	ssl_write_uint(ssl_connection, signed_data_buffer_size);
    
	ssl_write_bytes(ssl_connection, signed_data_buffer, signed_data_buffer_size);
    
	if (ssl_read_uint(ssl_connection) == SERVER_AUTH_SUCCESS){
	    printf("Server responded with SERVER_AUTH_SUCCESS.\n");
	}
	else {
	    printf("Server responded with SERVER_AUTH_SUCCESS.\n");
	}
	w_free(response);
    }
    else {
	ssl_write_uint(ssl_connection, REQUEST_PASS_AUTH);
	ssl_write_string(ssl_connection, username);
	ssl_write_string(ssl_connection, getUserPassword());
	
	if (ssl_read_uint(ssl_connection) == SERVER_AUTH_SUCCESS){
	    printf("Server reponded with SERVER_AUTH_SUCCESS, sending PKI Key.\n");
	    my_rsa_key = key_create_key();
	    if (!my_rsa_key){
		report_error("Error creating RSA key.", __FILE__, __LINE__, 0);
	    }
	    key_net_write_pub(my_rsa_key, ssl_connection);
	    writePrivKey(host, username, my_rsa_key);
	}
	else printf("Server responded with SERVER_AUTH_FAILURE.\n");
    }

    SSL_shutdown(ssl_connection);
    SSL_free(ssl_connection);
    return 0;
}
Ejemplo n.º 15
0
static void
w_free_maybe(int m, int n)
{
    if (m > WILCOX_MAX || n > WILCOX_MAX)
	w_free(m, n);
}
Ejemplo n.º 16
0
void le_soa_step_y(le_task *t)
{
	assert(t->stype == ST_SOA);
	
	int i, j;
	const real k1 = t->dt * t->mat.c1 / t->h.y;
	const real k2 = t->dt * t->mat.c2 / t->h.y;
	
	real *w1[5], *w2[5], *w3[5], *w4[5];
#define w_malloc(w)\
	w[0] = (real*)malloc(sizeof(real) * t->n.x);\
	w[1] = (real*)malloc(sizeof(real) * t->n.x);\
	w[2] = (real*)malloc(sizeof(real) * t->n.x);\
	w[3] = (real*)malloc(sizeof(real) * t->n.x);\
	w[4] = (real*)malloc(sizeof(real) * t->n.x);
	
	w_malloc(w1);
	w_malloc(w2);
	w_malloc(w3);
	w_malloc(w4);
#undef w_malloc

#define soa_omega_y(i, j, k) \
	{ \
	const real nv = soa_vy(i, j); \
	const real N00T = soa_syy(i, j) * t->mat.irhoc1; \
	const real n1v = soa_vx(i, j); \
	const real N01T = soa_sxy(i, j) * t->mat.irhoc2; \
	\
	w1[k + 2][i] = nv  - N00T; \
	w2[k + 2][i] = nv  + N00T; \
	w3[k + 2][i] = n1v - N01T; \
	w4[k + 2][i] = n1v + N01T; \
	}

	for (i = 0; i < t->n.x; i++) {
		soa_omega_y(i, 0, 0);
		soa_omega_y(i, 1, 1);
		soa_omega_y(i, 2, 2);
	}
#define w_init(w)\
	for (i = 0; i < t->n.x; i++) {\
		w[0][i] = w[1][i] = w[2][i];\
	}
	
	w_init(w1);
	w_init(w2);
	w_init(w3);
	w_init(w4);
#undef w_init

	for (j = 0; j < t->n.y; j++) {
		for (i = 0; i < t->n.x; i++) {
			real d1 = tvd2(k1, w1[0][i], w1[1][i], w1[2][i], w1[3][i]) - w1[2][i];
			real d2 = tvd2(k1, w2[4][i], w2[3][i], w2[2][i], w2[1][i]) - w2[2][i];
			real d3 = tvd2(k2, w3[0][i], w3[1][i], w3[2][i], w3[3][i]) - w3[2][i];
			real d4 = tvd2(k2, w4[4][i], w4[3][i], w4[2][i], w4[1][i]) - w4[2][i];
			d1 *= 0.5;
			d2 *= 0.5;
			d3 *= 0.5;
			d4 *= 0.5;

			soa_vy(i, j) += d1 + d2;
			soa_vx(i, j) += d3 + d4;

			soa_syy(i, j) += (d2 - d1) * t->mat.rhoc1;
			soa_sxx(i, j) += (d2 - d1) * t->mat.rhoc3;
			soa_sxy(i, j) += t->mat.rhoc2 * (d4 - d3);
		}
		
#define w_copy(w)\
		{\
		real *t = w[0];\
		w[0] = w[1];\
		w[1] = w[2];\
		w[2] = w[3];\
		w[3] = w[4];\
		w[4] = t;\
		}
		
		w_copy(w1);
		w_copy(w2);
		w_copy(w3);
		w_copy(w4);
#undef w_copy
		
		if (j < t->n.y - 3) {
			for (i = 0; i < t->n.x; i++) {
				soa_omega_y(i, j + 3, 2);
			}
		}
	}

#define w_free(w)\
	free(w[0]);\
	free(w[1]);\
	free(w[2]);\
	free(w[3]);\
	free(w[4]);
	
	w_free(w1);
	w_free(w2);
	w_free(w3);
	w_free(w4);
#undef w_free
}