Exemple #1
0
int receive_score(int sd, int score_packages, score **scores) {
    if(score_packages < 0) {
        return -1;
    } else {
        score *s = init_score();
        if(s == NULL || scores == NULL)
            return -1;

        int i;
        for(i = 0; i < score_packages; i++) {
            memset(s, 0, sizeof(struct score));

            byte buf[12];
            if(read(sd, buf, 12) <= 0) {
                perror("Error reading from client.\n");
                free(s);
                return -1;
            }
            int size = bytes_to_int(buf);

            s->game_type = buf[3];
            byte b[2];
            b[0] = buf[4];
            b[1] = buf[5];
            s->score = bytes_to_int(b);
            s->partners = (byte) buf[6];
            s->opponents = (byte) buf[7];
            byte result = buf[8];
            result = result >> 4;
            s->result = result;

            byte t = buf[8];
            t = t << 4;
            byte tmp = buf[9];
            tmp = tmp >> 4;
            t = t | tmp;
            s->takeouts = t;

            t = buf[9];
            t = t << 4;
            tmp = buf[10];
            tmp = tmp >> 4;
            t = t | tmp;
            s->remaining = t;

            size = size - 12;
            char name[size];
            if(read(sd, name, size) == -1) {
                perror("Error reading from client.\n");
                free(s);
                return -1;
            }
            strncpy(s->player_name, name, size);
            memcpy(scores[i], s, sizeof(score));
        }
        free(s);
        return 0;
    }
    return 0;
}
Exemple #2
0
void User::fromBinary(char * record, float scale) {
  //  int record_size = (int)(record++);
  int num_records = bytes_to_int(++record);
  record += sizeof(int);
  for(int i = 0; i < num_records; i++) {
    int key = bytes_to_int(record);
    record += sizeof(int);
    float val = scale * float((record++)[0])/128.;
    if(val > 1) val -= 2;
    add(key, val);
  }
}
Exemple #3
0
int receive_game_init(int sd, byte *game_type, int *opponents,
                      int *x, int *y, int *ox, int *oy, char *buf) {
    int size = 9;
    byte bu[size];
    if(read(sd, bu, size) != -1) {
        if(bu[2] == GAME_INIT) {
            size = bytes_to_int(bu);
            *game_type = bu[3];
            *opponents = bu[4];
            *x = bu[5];
            *y = bu[6];
            *ox = bu[7];
            *oy = bu[8];

            size = size - 9;
            char name[size];
            if(read(sd, name, size) != -1) {
                strcpy(buf, name);

                return 0;
            } else
                return -1;
        } else
            return -1;
    } else
        return -1;
}
Exemple #4
0
void afficher_relation () {
    printf("\nQuelle relation voulez-vous afficher ?\n");
    Meta_relation *relation = proposer_relation();
    Meta_attribut ** attributs = relation->attributs;
    printf("\n");
    for (int ind_att = 0; ind_att<relation->nb_attr; ind_att++) {   //  PARCOURS DE CHAQUE ATTRIBUT
        printf("%s\t\t", attributs[ind_att]->id);
    }
    printf("\n\n");
    Assoc_rel_page *assoc;
    for (int cpt=0; cpt<annuaire->nb_rel_pages; cpt++) {    // PARCOURS DES PAGES
        
        if ((assoc = annuaire->rel_pages[cpt])->id_rel[0] != '\0') {    //SELECTION DES PAGES NON VIDES
            if (strcmp(assoc->id_rel, relation->id) == 0) {
                int debut_attribut = 0;
                Page *page = donnees_BD->pages[assoc->index_page];
                
                int nb_nuplet = (int) page->enregistrement[63];
                int limite_zg = limite_zone_de_gestion(62-2*(nb_nuplet), page);
                
                for (int i = 62; i > limite_zg; i=i-2) {    // PARCOURS DE LA ZONE DE GESTION
                    
                    if (page->enregistrement[i-1] == '1') { // SELECTION DES NUPLETS NON EFFACEES
                        if (i != 62) {
                            debut_attribut = (int) page->enregistrement[i+2];
                        }
                        
                        // PARCOURS DES ATTRIBUTS DE CHAQUE NUPLET
                        for (int j=0; j<relation->nb_attr; j++) {
                            
                            // DOMAINE = INT
                            if (relation->attributs[j]->domaine->type == 0) {
                                unsigned char octets[4];
                                memcpy(octets, page->enregistrement + debut_attribut, sizeof(char) * 4);
                                int n = bytes_to_int(octets);
                                printf("%d", n);
                                printf("\t\t");
                                debut_attribut+=4;
                                
                            }
                            // DOMAINE = VARCHAR
                            else {
                                int taille_varchar = (int) page->enregistrement[debut_attribut];
                                debut_attribut++;
                                char *varchar = (char *) malloc(sizeof(char) * taille_varchar);
                                memcpy(varchar, page->enregistrement + debut_attribut, sizeof(char) * taille_varchar);
                                printf("%s", varchar);
                                printf("\t\t");
                                debut_attribut+=taille_varchar;
                            }
                        }
                        printf("\n");
                    }
                }
                
            }
        }
    }
}
Exemple #5
0
// *** nice-pass属性情報(2)
// 乗車日時
time_t nicepass_attr_in_time(const nicepass_attr2_t *attr) {
	int day = nicepass_day(attr);
	if (day == 0) return 0;

	struct tm tm = {
		//.tm_sec = nicepass_sec(attr->in_time),
		.tm_min = nicepass_min(attr->in_time),
		.tm_hour = nicepass_hour(attr->in_time),
		.tm_mday = day,
		.tm_mon = nicepass_month(attr) - 1,
		.tm_year = nicepass_year(attr) + 2000 - 1900,
	};

	return mktime(&tm);
}

// 降車日時
time_t nicepass_attr_out_time(const nicepass_attr2_t *attr) {
	int day = nicepass_day(attr);
	if (day == 0) return 0;

	struct tm tm = {
		//.tm_sec = nicepass_sec(attr->out_time),
		.tm_min = nicepass_min(attr->out_time),
		.tm_hour = nicepass_hour(attr->out_time),
		.tm_mday = day,
		.tm_mon = nicepass_month(attr) - 1,
		.tm_year = nicepass_year(attr) + 2000 - 1900,
	};

	return mktime(&tm);
}

// 使用装置
int nicepass_attr_type(const nicepass_attr2_t *attr) {
	return nicepass_type(attr);
}

// 処理種別
int nicepass_attr_proc(const nicepass_attr2_t *attr) {
	return nicepass_proc(attr);
}

// 直近利用金額
int nicepass_attr_use(const nicepass_attr2_t *attr) {
	return bytes_to_int(attr->use, sizeof(attr->use)) * 10;
}

// 直近残額
int nicepass_attr_balance(const nicepass_attr2_t *attr) {
	return nicepass_balance(attr);
}

// *** nice-pass属性情報(3)
// 乗車駅
int nicepass_attr_in_station(const nicepass_attr3_t *attr) {
	return nicepass_in_station(attr);
}

// 降車駅
int nicepass_attr_out_station(const nicepass_attr3_t *attr) {
	return nicepass_out_station(attr);
}

// 取引通番
int nicepass_attr_no(const nicepass_attr3_t *attr) {
	return bytes_to_int(attr->no, sizeof(attr->no));
}

// *** nice-pass利用履歴
// 処理日時
time_t nicepass_value_datetime(const nicepass_value_t *value) {
	int day = nicepass_day(value);
	if (day == 0) return 0;

	int min= nicepass_out_time(value);

	struct tm tm = {
		.tm_min = min % 60,
		.tm_hour = min / 60,
		.tm_mday = day,
		.tm_mon = nicepass_month(value) - 1,
		.tm_year = nicepass_year(value) + 2000 - 1900,
	};

	return mktime(&tm);
}

// 装置番号
int nicepass_value_train(const nicepass_value_t *value) {
	return nicepass_train(value);
}

// 乗車駅
int nicepass_value_in_station(const nicepass_value_t *value) {
	return nicepass_in_station(value);
}

// 降車駅
int nicepass_value_out_station(const nicepass_value_t *value) {
	return nicepass_out_station(value);
}

// 使用装置
int nicepass_value_type(const nicepass_value_t *value) {
	return nicepass_type(value);
}

// 処理種別
int nicepass_value_proc(const nicepass_value_t *value) {
	return nicepass_type(value);
}

// 利用金額種別
int nicepass_value_use_kind(const nicepass_value_t *value) {
	return nicepass_use_kind(value);
}

// 利用金額
int nicepass_value_use(const nicepass_value_t *value) {
	int16_t use = nicepass_use(value);

	if (use & 0x400) {
		use |= 0xf000;
	}

	return use * 10;
}
Exemple #6
0
// *** nice-pass残額
// チャージ金額残額
int nicepass_amount_charge(const nicepass_amount_t *amount) {
	return bytes_to_int(amount->charge, sizeof(amount->charge));
}
int conv32_8(FILE *arq_entrada, FILE *arq_saida){

	char ordem;
	unsigned char b_utf32[4];
	int contador_erro=0, n_bytes, n_leitura;
	unsigned int utf32;

	ordem = checa_bom( arq_entrada, &contador_erro );
	if(ordem == -1)
		return -1;
	

	if(ordem == 'L'){

		n_leitura = fscanf(arq_entrada, "%c%c%c%c", &b_utf32[3], &b_utf32[2], &b_utf32[1], &b_utf32[0]);	

		while( n_leitura == 4){

			utf32 = bytes_to_int( b_utf32 , contador_erro);
			if(utf32 == -1)
				return -1;
			n_bytes = determina_n(utf32);

			if(n_bytes == 1)
				separa_bytes_6( b_utf32, utf32, 4, 0x7F);
			else
				separa_bytes_6( b_utf32, utf32, 4, 0x3F);

			imprime_primeiro(arq_saida, b_utf32[4 - n_bytes], n_bytes);

			if(n_bytes > 1)
				imprime_continuacao(arq_saida, b_utf32, n_bytes);

			contador_erro += 4;
			n_leitura = fscanf(arq_entrada, "%c%c%c%c", &b_utf32[3], &b_utf32[2], &b_utf32[1], &b_utf32[0]);

		}
		if(	n_leitura > 0){
			fprintf(stderr, "O numero de bytes do arquivo excede em %d bytes o devido para a leitura correta do arquivo", n_leitura);
			return -1;
		}
	}




	else
	{	
		n_leitura = fscanf(arq_entrada, "%c%c%c%c", &b_utf32[0], &b_utf32[1], &b_utf32[2], &b_utf32[3]);

		while( n_leitura == 4){

			utf32 = bytes_to_int( b_utf32 , contador_erro);
			if(utf32 == -1)
				return -1;

			n_bytes = determina_n(utf32);

			if(n_bytes == 1)
				separa_bytes_6( b_utf32, utf32, 4, 0x7F);
			else
				separa_bytes_6( b_utf32, utf32, 4, 0x3F);
		
			imprime_primeiro(arq_saida, b_utf32[4 - n_bytes], n_bytes);

			if(n_bytes > 1)
				imprime_continuacao(arq_saida, b_utf32, n_bytes);
			
			contador_erro += 4;
			n_leitura = fscanf(arq_entrada, "%c%c%c%c", &b_utf32[0], &b_utf32[1], &b_utf32[2], &b_utf32[3]);

		}

		if(	n_leitura > 0){
			fprintf(stderr, "O numero de bytes do arquivo excede em %d bytes o devido para a leitura correta do arquivo", n_leitura);
			return -1;
		}
	}



	return 0;
}
Exemple #8
0
int dexe_run_function(Executable* exe)
{
    //set up the stack frame
    Stack_Frame* sf = (Stack_Frame*)stack_peek(&exe->call_stack);

    if (exe->functions[sf->function_id].local_count != 0)
    {
        sf->local_memory = (int*)malloc(exe->functions[sf->function_id].local_count * sizeof(int));

        if (!sf->local_memory)
            error(exe, ALLOCATION_ERROR_IN_EXECUTER, "Could not allocate %d bytes of memory for locals", exe->functions[sf->function_id].local_count * sizeof(int));

        memset(sf->local_memory,'\0',exe->functions[sf->function_id].local_count * sizeof(int));
    }
    else
        sf->local_memory = NULL;

    //additional variables for the sake of increasing readability (and hopefully, optimization purposes)
    char* code_ptr = exe->functions[sf->function_id].instructions;
    int size = exe->functions[sf->function_id].size_of_instructions;
    stack* stack_ptr = &sf->stack;


    for(sf->pc = 0; sf->pc < size; sf->pc++)
    {
        unsigned char opcode = code_ptr[sf->pc];

        switch(opcode)
        {
        case Nop:
        {
            //do nothing by definition
            break;
        }
        case Break:
        {
            if(exe->flags & DEXE_FLAGS_DEBUG && exe->info->commandline & COMMANDLINE_DEBUG)
                breakpoint(exe);
        }
        case Load:
        {
            sf->pc++;
            if(opcode > exe->functions[sf->function_id].local_count)
                error(exe, VARIABLE_INDEX_OUT_OF_RANGE, "Index recieved: %d. This occured in function %d.",opcode, sf->function_id);

            stack_push(stack_ptr, sf->local_memory[(unsigned char)opcode]);

            break;
        }
        case Push:
        {
            sf->pc++;
            if(sf->pc < size - 4)
                error(exe, ABRUPT_END_OF_FUNCTION, "Ran out of executable code while attempting to push a literal onto the stack.");

            stack_push(stack_ptr, bytes_to_int((char*)(code_ptr + sf->pc)));

            break;
        }
        case Store:
        {
            sf->pc++;
            if(opcode > exe->functions[sf->function_id].local_count)
                error(exe, VARIABLE_INDEX_OUT_OF_RANGE, "Index recieved: %d. This occured in function %d.",opcode, sf->function_id);
            if(stack_ptr->stack_pointer < 1)
                error(exe, MANIPULATED_EMPTY_STACK, "A store instruction was encountered that requires at least 1 item on the stack. Found %d items", stack_ptr->stack_pointer);

            sf->local_memory[opcode] = stack_pop(stack_ptr);

            break;
        }
        case Dup:
        {
            test_stack_size(exe, &DUP);

            stack_push(stack_ptr, stack_peek(stack_ptr));

            break;
        }
        case Pop:
        {
            test_stack_size(exe, &POP);

            stack_pop(stack_ptr);

            break;
        }
        case Inc:
        {
            test_stack_size(exe, &INC);

            stack_push(stack_ptr,stack_pop(stack_ptr)+1);

            break;
        }
        case Dec:
        {
            test_stack_size(exe, &DEC);

            stack_push(stack_ptr,stack_pop(stack_ptr)-1);

            break;
        }
        case Add:
        {
            test_stack_size(exe, &ADD);

            stack_push(stack_ptr, stack_pop(stack_ptr) + stack_pop(stack_ptr));

            break;
        }
        case Sub:
        {
            test_stack_size(exe, &SUB);

            register int value1 = stack_pop(stack_ptr);
            register int value2 = stack_pop(stack_ptr);
            stack_push(stack_ptr, value2 - value1);

            break;
        }
        case Mul:
        {
            test_stack_size(exe, &MUL);

            stack_push(stack_ptr, stack_pop(stack_ptr) * stack_pop(stack_ptr));

            break;
        }
        case Div:
        {
            test_stack_size(exe, &DIV);

            register int value1 = stack_pop(stack_ptr);
            register int value2 = stack_pop(stack_ptr);
            if(value2 == 0)
                error(exe,DIVISION_BY_ZERO, "A division by zero was encountered while trying to divide %d by %d", value2, value1);

            stack_push(stack_ptr, value2 / value1);

            break;
        }
        case Rem:
        {
            test_stack_size(exe, &REM);

            register int value1 = stack_pop(stack_ptr);
            register int value2 = stack_pop(stack_ptr);
            if(value2 == 0)
                error(exe,DIVISION_BY_ZERO, "A division by zero was encountered trying to divide %d by %d", value2, value1);

            stack_push(stack_ptr, value2 % value1);

            break;
        }
        case And:
        {
            test_stack_size(exe, &AND);

            stack_push(stack_ptr, stack_pop(stack_ptr) & stack_pop(stack_ptr));

            break;
        }
        case Or:
        {
            test_stack_size(exe, &OR);

            stack_push(stack_ptr, stack_pop(stack_ptr) | stack_pop(stack_ptr));

            break;
        }
        case Xor:
        {
            test_stack_size(exe, &XOR);

            stack_push(stack_ptr, stack_pop(stack_ptr) ^ stack_pop(stack_ptr));

            break;
        }
        case Not:
        {
            test_stack_size(exe, &NOT);

            stack_push(stack_ptr, ~stack_peek(stack_ptr));

            break;
        }
        case Neg:
        {
            test_stack_size(exe, &NEG);

            stack_push(stack_ptr, -stack_peek(stack_ptr));

            break;
        }
        case Shl:
        {
            test_stack_size(exe, &SHL);

            register int value1 = stack_pop(stack_ptr);
            register int value2 = stack_pop(stack_ptr);

            stack_push(stack_ptr, value2 << value1);

            break;
        }
        case Shr:
        {
            test_stack_size(exe, &SHR);

            register int value1 = stack_pop(stack_ptr);
            register int value2 = stack_pop(stack_ptr);

            stack_push(stack_ptr, value2 >> value1);

            break;
        }
        case Cmp:
        {
            test_stack_size(exe, &CMP);

            register int value1 = stack_pop(stack_ptr);
            register int value2 = stack_pop(stack_ptr);

            sf->flags = (value1 == value2 ? JUMP_EQUAL : JUMP_NOT_EQUAL) |
                        (value2 > value1 ? JUMP_GREATER : 0) |
                        (value2 < value1 ? JUMP_LESS : 0);

            break;
        }
        case Jmp:
        {
            sf->pc += bytes_to_int(code_ptr + sf->pc + 1);
            if(sf->pc < 0 || sf->pc > size)
                error(exe, INVALID_JUMP_POSITION, "Range expected: 0 - %d. Recieved %d", size, sf->pc);
            break;
        }
        case Je:
        {
            if (sf->flags & JUMP_EQUAL)
            {
                sf->pc += code_ptr[sf->pc + 1];
                if(sf->pc < 0 || sf->pc > size)
                    error(exe, INVALID_JUMP_POSITION, "Range expected: 0 - %d. Recieved %d", size, sf->pc);
            }
            break;
        }
        case Jne:
        {
            if (sf->flags & JUMP_NOT_EQUAL)
            {
                sf->pc += code_ptr[sf->pc + 1];
                if(sf->pc < 0 || sf->pc > size)
                    error(exe, INVALID_JUMP_POSITION, "Range expected: 0 - %d. Recieved %d", size, sf->pc);
            }
            break;
        }
        case Jg:
        {
            if(sf->flags & JUMP_GREATER)
            {
                sf->pc += code_ptr[sf->pc + 1];
                if(sf->pc < 0 || sf->pc > size)
                    error(exe, INVALID_JUMP_POSITION, "Range expected: 0 - %d. Recieved %d", size, sf->pc);
            }
            break;
        }
        case Jge:
        {
            if(sf->flags & (JUMP_GREATER | JUMP_EQUAL))
            {
                sf->pc += code_ptr[sf->pc + 1];
                if(sf->pc < 0 || sf->pc > size)
                    error(exe, INVALID_JUMP_POSITION, "Range expected: 0 - %d. Recieved %d", size, sf->pc);
            }
            break;
        }
        case Jl:
        {
            if(sf->flags & JUMP_LESS)
            {
                sf->pc += code_ptr[sf->pc + 1];
                if(sf->pc < 0 || sf->pc > size)
                    error(exe, INVALID_JUMP_POSITION, "Range expected: 0 - %d. Recieved %d", size, sf->pc);
            }
            break;
        }
        case Jle:
        {
            if(sf->flags & (JUMP_LESS | JUMP_EQUAL))
            {
                sf->pc += code_ptr[sf->pc + 1];
                if(sf->pc < 0 || sf->pc > size)
                    error(exe, INVALID_JUMP_POSITION, "Range expected: 0 - %d. Recieved %d", size, sf->pc);
            }
            break;
        }
        case In:
        {
            stack_push(stack_ptr, getc(stdin));

            break;
        }
        case Out:
        {
            test_stack_size(exe, &OUT);

            putc(stack_pop(stack_ptr), stdout);

            break;
        }
        case Call:
        {
            Stack_Frame frame;
            frame.function_id = bytes_to_int(code_ptr + sf->pc + 1);
            frame.pc = 0;
            frame.flags = 0;

            if(frame.function_id < 0 || frame.function_id > exe->number_of_functions)
                error(exe, FUNCTION_DOES_NOT_EXIST, "The function specified by a call does not exist. There are %d functions. Valid function ids are 0-%d. The value specified by the entry point is: %d", exe->number_of_functions, exe->number_of_functions, frame.function_id);

            if(stack_ptr->stack_pointer < exe->functions[frame.function_id].arg_count)
                error(exe, NOT_ENOUGH_ARGUMENTS, "Arguments required %d. Recieved %d", exe->functions[frame.function_id].arg_count, stack_ptr->stack_pointer + 1);

            stack_init(&frame.stack);
            for(int x = 0; x < exe->functions[frame.function_id].arg_count; x++)
                stack_push(&frame.stack, stack_pop(stack_ptr));

            stack_push(&exe->call_stack, (long)&frame);

            stack_push(stack_ptr, dexe_run_function(exe));

            stack_pop(&exe->call_stack);

            sf->pc += 4;

            break;
        }
        case Ret:
        {
            //Is there anything remaining on the stack? That's our return value. If not, maybe this is a void function? return 0
            int ret_value = stack_ptr->stack_pointer < 1 ? 0 : stack_pop(stack_ptr);
            stack_free(stack_ptr);
            free(sf->local_memory);

            return ret_value;
        }
        default:
            error(exe, INVALID_OPCODE, "Invalid opcode recieved: 0x%X", opcode);
        }
    }

    error(exe, ABRUPT_END_OF_FUNCTION, "This occured in function %d", sf->function_id);
}
Exemple #9
0
int listen_for_connections(void) {
    struct sockaddr_in serveraddr, clientaddr;
    int request_sd;
    int numsocks = 0;
    int maxsocks = 10;
    int sock[maxsocks];
    int receive;
    int clientaddrlen = sizeof(struct sockaddr);
    fd_set readfds, fds;
    struct timeval time;
    time.tv_sec = 20;
    time.tv_usec = 0;

    //request-socket, lytter på innkommende forbindelser
    request_sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

    //adresse struct
    memset((void*)  &serveraddr, 0, sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_addr.s_addr = INADDR_ANY;

    if(listening_port != 0) {
        serveraddr.sin_port = htons(listening_port);
        printf("Listening for connections on port '%d'\n", listening_port);
    } else {
        serveraddr.sin_port = htons(9090); //default if no port is provided
        printf("Listening for connections on default port '9090'\n");
    }
    //bind adressen til socketen
    bind(request_sd, (struct sockaddr *) &serveraddr, sizeof(serveraddr));

    //start lytting
    listen(request_sd, SOMAXCONN);

    FD_ZERO(&readfds);
    FD_ZERO(&fds);
    FD_SET(request_sd, &fds);
    FD_SET(0, &fds);
    numsocks++;

    for(;;) {
        readfds = fds;
        receive = select(maxsocks + 1, &readfds, NULL, NULL, &time);

        if(receive < 0) {
            perror("select encountered an error.\n");
            return -1;
        } else if(receive == 0) {
            //timeout, do nothing
        } else {
            int i;
            for(i = 0; i < FD_SETSIZE; i++) {
                if(FD_ISSET(i, &readfds)) {
                    if(i == 0) { //stdin look for ctrl-d
                        if(read(i, NULL, 10) == 0) {
                            for(i = 0; i < numsocks; i++) {
                                close(sock[i]);
                                FD_CLR(sock[i], &fds);
                            }
                            exit_server();
                        }
                    }
                    if(i == request_sd) {
                        if(numsocks < maxsocks) {
                            sock[numsocks] = accept(request_sd, (struct sockaddr *) &clientaddr,
                                                    (socklen_t *) &clientaddrlen);
                            FD_SET(sock[numsocks], &fds);
                            numsocks++;
                        } else {
                            //No more space for sockets
                            perror("Ran out of socket space.\n");
                            for(i = 0; i < numsocks; i++) {
                                close(sock[i]);
                                FD_CLR(sock[i], &fds);
                            }
                            exit_server();
                        }
                    } else {
                        //read first 4 bytes, and determine what kind of package is coming
                        char buf[4];
                        memset(buf, 0, 4);
                        if(read(i, buf, 4) == -1) {
                            perror("Error reading from client.\n");
                            return -1;
                        }
                        int size = bytes_to_int(buf);
                        byte msg_type = buf[2];

                        if(msg_type == SCORE_LIST) {
                            int score_packages = btotci(buf[3]);
                            if(score_packages > 0) {
                                score *scores[score_packages];
                                int count;
                                for(count = 0; count < score_packages; count++) {
                                    scores[count] = init_score();
                                }
                                int result = receive_score(i, score_packages, scores);
                                if(result != -1) {
                                    for(count = 0; count < score_packages; count++) {
                                        add_score(scores[count]);
                                    }
                                }
                                for(count = 0; count < score_packages; count++) {
                                    free(scores[count]);
                                }
                            } else if(score_packages < 0) {
                                //reply with abs(score_packages);
                                int num = abs(score_packages);
                                score *s[num];
                                int count;

                                count = get_scores(num, s);
                                if(send_score_list(i, s, count) < 0) {
                                    perror("Closing connection.\n");
                                    close(i);
                                    FD_CLR(i, &fds);
                                }
                            }
                        } else if(msg_type == SHUTDOWN_NOTIFICATION) {
                            //receive shutdown msg
                            char buf[size - 4]; //First 4 bytes allready read.
                            int result = receive_shutdown_notification(i, buf, size - 4);
                            if(result == 0) {
                                printf("Received shutdown notification: %s\n", buf);
                                close(i);
                                FD_CLR(i, &fds);
                            }
                        } else {
                            //error, unknown type, reply with shutdown notification
                            perror("Sending shutdown_notification.\n");
                            send_shutdown_notification(i, "Unknown msg_type, got: " + msg_type);
                            //close and remove socket criptor
                            close(i);
                            FD_CLR(i, &fds);
                        }
                    }
                }
            }
        }
    }
    int i;
    for(i = 0; i < maxsocks; i++) {
        close(sock[i]);
    }
    close(request_sd);
    return 0;
}
Exemple #10
0
void supprimer_nuplet () {
    Meta_relation *relation = proposer_relation();
    printf("\nSur quel attribut voulez-vous rechercher le n-uplet à supprimer ? ");
    Meta_attribut *attribut = proposer_attribut(relation);
    Meta_attribut ** attributs = relation->attributs;
    
    int cpt_suppr = 0;
    char critere_suppr[9];
    
    printf("\nVeuillez saisir votre critère de suppression (type ");
    if (attribut->domaine->type == 0) {
        printf("INT): ");
    }
    else {
        printf("VARCHAR(%d): ", attribut->domaine->taille);
    }
    scanf("%s", critere_suppr);
    
    Assoc_rel_page *assoc;
    for (int cpt=0; cpt<annuaire->nb_rel_pages; cpt++) {    // PARCOURS DES PAGES
        
        if ((assoc = annuaire->rel_pages[cpt])->id_rel[0] != '\0') {    //SELECTION DES PAGES NON VIDES
            Page *page = donnees_BD->pages[assoc->index_page];
            
            if (strcmp(assoc->id_rel, relation->id) == 0) {
                
                int nb_nuplet = (int) page->enregistrement[63];
                int limite_zg = limite_zone_de_gestion(62-2*(nb_nuplet), page);
                int debut_attribut = 0;
                
                for (int i = 62; i > limite_zg; i=i-2) {        // PARCOURS DE LA ZONE DE GESTION
                    if (page->enregistrement[i-1] == '1') {   // SELECTION DES NUPLETS NON EFFACES
                        if (i != 62) {
                            debut_attribut = (int) page->enregistrement[i+2];
                        }
                        for (int ind_att = 0; ind_att<relation->nb_attr; ind_att++) {   //  PARCOURS DE CHAQUE ATTRIBUT
                            if (ind_att == attribut->rang) {    // SELECTION DE L'ATTRIBUT A RECHERCHER
                                
                                
                                int comparaison = 0; // BOOLEAN RESULTANT DE LA COMPARAISON
                                
                                // ATTRIBUT DE TYPE INT
                                if (attributs[ind_att]->domaine->type == 0) {
                                    unsigned char octets[4];
                                    memcpy(octets, page->enregistrement + debut_attribut, sizeof(char) * 4);
                                    int res = bytes_to_int(octets);
                                    int critere_de_recherche = (int) strtol(critere_suppr, NULL, 10);
                                    
                                    // COMPARAISON SELON LE CRITERE DE RECHERCHE (POUR UN INT)
                                    if (res == critere_de_recherche)
                                        comparaison = 1;
                                }
                                // ATTRIBUT DE TYPE VARCHAR
                                else {
                                    int taille_varchar = char_to_int (page->enregistrement[debut_attribut]);
                                    char *varchar = (char *) malloc(sizeof(char) * taille_varchar);
                                    memcpy(varchar, page->enregistrement + debut_attribut + 1, sizeof(char) * taille_varchar);
                                    
                                    // COMPARAISON
                                    if (strcmp(critere_suppr, varchar) == 0)
                                        comparaison = 1;
                                }
                                
                                // SUPRESSION DU NUPLET SI COMPARAISON POSITIVE
                                if (comparaison == 1) {
                                    page->enregistrement[i-1]='0';
                                    page->enregistrement[63] = int_to_bytes (((int) page->enregistrement[63]) - 1)[3];
                                    cpt_suppr++;
                                    nettoyer_espace_vide (page, limite_zg, i-1);
                                }
                            }
                            // PASSAGE A L'ATTRIBUT SUIVANT
                            if (attributs[ind_att]->domaine->type == 0) {
                                debut_attribut += 4;
                            }
                            else {
                                int taille_varchar = char_to_int(page->enregistrement[debut_attribut]);
                                debut_attribut = debut_attribut + taille_varchar + 1;
                            }
                            
                        }
                    }
                }
                desallocation_si_page_vide(page, assoc);
            }
        }
    }
}
Exemple #11
0
int BCEDecrypt(byte *global_params_path, byte *priv_key_block, byte *CT_C0, byte *CT_C1, byte *symmetric_key_out)
{
    global_broadcast_params_t gbs;
    priv_key_t priv_key;
    ct_t shared_CT;
    element_t symmetric_key;
    int suffix = 0, retlen;

    if (global_params_path == NULL)
        return 1;
    if (priv_key_block == NULL)
        return 2;
    if (CT_C0 == NULL)
        return 3;
    if (CT_C1 == NULL)
        return 4;
    if (symmetric_key_out == NULL)
        return 5;

    LoadGlobalParams((char *) global_params_path, &gbs);

    priv_key = (priv_key_t) pbc_malloc(sizeof(struct single_priv_key_s));
    // restore index
    priv_key->index = bytes_to_int(priv_key_block);
    suffix += PRK_INDEX_LENGTH;

    // restore g_i_gamma
    element_init(priv_key->g_i_gamma, gbs->pairing->G1);
    retlen = element_from_bytes(priv_key->g_i_gamma, priv_key_block + suffix);
    if (retlen != PRK_G_I_GAMMA_LENGTH)
        return 6;
    suffix += PRK_G_I_GAMMA_LENGTH;

    // restore g_i
    element_init(priv_key->g_i, gbs->pairing->G1);
    retlen = element_from_bytes(priv_key->g_i, priv_key_block + suffix);
    if (retlen != PRK_G_I_LENGTH)
        return 7;
    suffix += PRK_G_I_LENGTH;

    // restore h_i
    element_init(priv_key->h_i, gbs->pairing->G2);
    retlen = element_from_bytes(priv_key->h_i, priv_key_block + suffix);
    if (retlen != PRK_H_I_LENGTH)
        return 8;
    suffix += PRK_H_I_LENGTH;

    // restore decr_prod
    element_init(priv_key->decr_prod, gbs->pairing->G1);
    retlen = element_from_bytes(priv_key->decr_prod, priv_key_block + suffix);
    if (retlen != PRK_DECR_PROD_LENGTH)
        return 9;

    shared_CT = (ct_t) pbc_malloc(sizeof(struct ciphertext_s));
    element_init(shared_CT->C0, gbs->pairing->G2);
    element_init(shared_CT->C1, gbs->pairing->G1);
    element_from_bytes(shared_CT->C0, CT_C0);
    element_from_bytes(shared_CT->C1, CT_C1);

    DecryptKEM_using_product(gbs, priv_key, symmetric_key, shared_CT);

    retlen = element_to_bytes(symmetric_key_out, symmetric_key);
    if (retlen != SYMMETRIC_KEY_LENGTH)
        return 10;

    element_random(symmetric_key);
    element_clear(symmetric_key);
    FreeCT(shared_CT);
    pbc_free(shared_CT);
    FreePK(priv_key);
    pbc_free(priv_key);
    FreeGBP(gbs);
    pbc_free(gbs);

    return 0;
}