Exemplo n.º 1
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();
	}
}
Exemplo 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;
    }
}
Exemplo n.º 3
0
void writePrivKey(const char *host, const char *username, RSA *my_key){
    char *file_path = NULL;
    const char *user_home = 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_write_priv(my_key, file_path) != 0)
	report_error_q("Unable to write private key to file.", __FILE__, __LINE__, 0);
}
Exemplo n.º 4
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;
}
Exemplo n.º 5
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;
}
Exemplo n.º 6
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;
}
Exemplo n.º 7
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;
}
Exemplo n.º 8
0
static void search_create_tree(search_configuration *conf, position *pos, search_node *node, unsigned int depth)
{
	move *m;
	movelist ml;
	unsigned int i;


	/* check parameters */
	ASSERT(conf != (search_configuration *)0);
	ASSERT(pos  != (position *)0);
	ASSERT(node != (search_node *)0);

	/* update node counter */
	conf->result->numnodes++;

	if (depth < conf->precalcdepth)
	{
		switch (move_generate(pos, &ml))
		{
			case MOVESLEFT:
			{
				for (i = 0; i < ML_SIZE(ml); i++)
				{
					m = &ML_ENTRY(ml, i);
					move_apply(pos, m);
					node->children[i] = (search_node *)w_malloc(sizeof(search_node));
					memcpy(&(node->children[i]->m), m, sizeof(move));
					search_create_tree(conf, pos, node->children[i], depth + 1);
					move_rollback(pos, m);
				}
				node->numchildren = ML_SIZE(ml);
				break;
			}
			case STALEMATE:
			{
				node->rating      = RATING_INIT_INF;
				node->numchildren = 0;
				break;
			}
			case CHECKMATE:
			{
				if (pos->player == WHITE)
				{
					node->rating = RATING_INIT_BLACK;
				}
				else
				{
					node->rating = RATING_INIT_WHITE;
				}
				node->numchildren = 0;
				break;
			}
			default:
			{
				/* invalid return value from move generator */
				ASSERT(0);
			}
		}
	}
	else
	{
		switch (move_movesleft(pos))
		{
			case MOVESLEFT:
			{
				memcpy(&(node->pos), pos, sizeof(position));
				node->rating   = RATING_INIT_INF;
				node->nextleaf = conf->nextleaf;
				conf->nextleaf = node;
				conf->numleafnodes++;
				break;
			}
			case STALEMATE:
			{
				node->rating = RATING_INIT_INF;
				break;
			}
			case CHECKMATE:
			{
				if (pos->player == WHITE)
				{
					node->rating = RATING_INIT_BLACK;
				}
				else
				{
					node->rating = RATING_INIT_WHITE;
				}
				break;
			}
			default:
			{
				/* invalid return value from move generator */
				ASSERT(0);
			}
		}
		node->numchildren = 0;
	}
}
Exemplo n.º 9
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;
}
Exemplo n.º 10
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;
}
Exemplo n.º 11
0
void init_mboot(multiboot_header *mboot) {
	// make sure multiboot header is in high memory (after kernel)
	head = reinterpret_cast<multiboot_header *>(w_malloc(mboot->size, 8));
	if(head == nullptr) {
		PANIC("head is null");
	}
	memcpy((void *)head, (void *)mboot, mboot->size);
	// get count of multiboot tags
	pointer_t temp_ptr = reinterpret_cast<pointer_t>(head) + sizeof(multiboot_header);
	while(true) {
		multiboot_tag *tag = reinterpret_cast<multiboot_tag *>(temp_ptr);
		if(tag->type == t_end_marker && tag->size == 8) {
			break;
		} else {
			tag_count++;
			temp_ptr += tag->size;
			// multiboot 1.6 spec requires tags aligned to 8 bytes
			if((uintptr_t)temp_ptr % 8) {
				temp_ptr = (pointer_t)(((uintptr_t)temp_ptr & ~0x7) + 8);
			}
		}
	}
	temp_ptr = reinterpret_cast<pointer_t>(head) + sizeof(multiboot_header);
	tags = reinterpret_cast<multiboot_tag **>(w_malloc(sizeof(multiboot_tag *) * tag_count));
	if(tags == nullptr) {
		PANIC("tags is null");
	}
	// copy tags into the tags array
	for(int i = 0; i < tag_count; i++) {
		tags[i] = reinterpret_cast<multiboot_tag *>(temp_ptr);
		temp_ptr += tags[i]->size;
		// change 32 bit addresses into correct type
		if(tags[i]->type == t_module) {
			multiboot_module *tmp =
			    reinterpret_cast<multiboot_module *>(w_malloc(sizeof(multiboot_module)));
			multiboot_module_int *mmi = reinterpret_cast<multiboot_module_int *>(tags[i]);
			tmp->head.type = t_module;
			tmp->head.size = mmi->head.size;
			tmp->mod_start = static_cast<uintptr_t>(mmi->mod_start);
			tmp->mod_end = static_cast<uintptr_t>(mmi->mod_end);
			tags[i] = reinterpret_cast<multiboot_tag *>(tmp);
		}
		if((uintptr_t)temp_ptr % 8) {
			temp_ptr = (pointer_t)(((uintptr_t)temp_ptr & ~0x7) + 8);
		}
	}
	// copy the modules that we can into higher memory
	// too big ones should already be in the higher memory
	for(int i = 0; i < tag_count; i++) {
		if(get_tag(i)->type == t_module) {
			multiboot_module *module = reinterpret_cast<multiboot_module *>(get_tag(i));
			size_t module_size = module->mod_end - module->mod_start;
			// cannot be safely copied into the workspace
			if(module_size >= 0x19000) {
				continue;
			}
			// be safe, enforce 16 byte alignment
			void *start = w_malloc(module_size, 16);
			if(!start) {
				PANIC("could not allocate memory for module");
			}
			memmove(start, reinterpret_cast<void *>(module->mod_start), module_size);
			module->mod_start = reinterpret_cast<uintptr_t>(start);
			module->mod_end = module->mod_start + module_size;
		}
	}
}
Exemplo n.º 12
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
}