Ejemplo n.º 1
0
int send_game_start(int sd) {
    int size = 4;
    byte buf[size];
    int_to_bytes(size, buf);
    buf[2] = GAME_START;
    buf[3] = 0;
    if(write(sd, buf, size) != -1)
        return 0;
    else return -1;
}
Ejemplo n.º 2
0
int send_score_package(int sd, score *score) {
    int size = strlen(score->player_name) + 1 + 12;

    byte buf[size];

    byte converted_int[2];
    //total msg size
    int_to_bytes(size, converted_int);
    buf[0] = converted_int[0];
    buf[1] = converted_int[1];

    buf[2] = SCORE_PACKAGE; //msg type
    //game type
    buf[3] = score->game_type;
    //score
    int_to_bytes(score->score, converted_int);
    buf[4] = converted_int[0];
    buf[5] = converted_int[1];
    //number of partners
    buf[6] = score->partners;
    //number of opponents
    buf[7] = score->opponents;

    //result, takeouts, remaining, free, and reserved
    char tmp[5];
    int i = generate_last_line(score->result, score->takeouts, score->remaining,
                               0, tmp);
    if(i != 0) {
        perror("generate_last_line in highscoreprotocol returned non-zero");
        exit(-1);
    }
    buf[8] = tmp[0];
    buf[9] = tmp[1];
    buf[10] = tmp[2];
    buf[11] = tmp[3];
    //add player name at the end.
    strcpy(buf + 12, score->player_name);

    //write to sd
    return write(sd, buf, size);
}
Ejemplo n.º 3
0
int send_game_end(int sd) {
    int size = 5;
    byte buf[size];
    int_to_bytes(size, buf);
    buf[2] = GAME_END;
    buf[3] = 0;
    buf[4] = 0;
    if(write(sd, buf, size) != -1)
        return 0;
    else
        return -1;
}
Ejemplo n.º 4
0
int send_game_update(int sd, int x, int y) {
    int size = 5;
    byte buf[size];
    int_to_bytes(size, buf);
    buf[2] = GAME_UPDATE;
    buf[3] = x;
    buf[4] = y;
    if(write(sd, buf, size) != -1)
        return 0;
    else
        return -1;
}
Ejemplo n.º 5
0
pair<char*, size_t>  User::toBinary(bool rescale) {
  const int record_size = (sizeof(int) + 1);
  pair<float, float> m_m = min_max();

  char * record = new char[record_size*size() + 2 * sizeof(int)];
  char * rec_ptr = record;
  *(rec_ptr++) = char(record_size);
  if(m_m.first == 0 && m_m.second == 0) {
    rec_ptr += int_to_bytes(0, rec_ptr);
  }
  else {
    float scale = rescale ? max(-m_m.first, m_m.second) : 1.;
    vector<pair<int, float> > vec = sort();
    rec_ptr += int_to_bytes(vec.size(), rec_ptr);
    for(vector<pair<int, float> >::iterator it = vec.begin();
        it != vec.end(); it++) {
      entry_to_bytes((*it), rec_ptr, scale);
      rec_ptr += record_size;
    }
    vec.clear();
  }
  return pair<char*, size_t>(record, rec_ptr - record);
}
Ejemplo n.º 6
0
int send_score_list(int sd, score **s, int count) {
    int total_size = 4; //size of score_list (0x00)
    int i;
    byte following;
    if(count > 127 || count < -128) {
        perror("send_score_list: 'count' is bigger than 126");
        perror("or smaller than -127 and will cause overflow\n");
        exit(-1);
    }

    if(count > 0) {
        for(i = 0; i < count; i++) {
            total_size += strlen(s[i]->player_name) + 12;
        }
    }

    byte buf[4];
    memset(buf, 0, 4);

    following = itotc(count);
    byte converted_int[2];
    memset(converted_int, 0, 2);

    //total msg size
    int_to_bytes(total_size, converted_int);
    buf[0] = converted_int[0];
    buf[1] = converted_int[1];
    //msg type
    buf[2] = SCORE_LIST; //poengliste type
    //number of following score packages
    buf[3] = following;
    //write score_list package
    if(write(sd, buf, 4) == -1) {
        perror("Error writing score_list.\n");
        return -1;
    }
    for(i = 0; i < count; i++) {
        if(send_score_package(sd, s[i]) <= 0) {
            perror("Error writing score_package.\n");
            return -1;
        }
    }
    return 0;
}
Ejemplo n.º 7
0
int send_game_init(int sd, byte game_type, int opponents,
                   int x, int y, int ox, int oy, char *buf) {
    int size = 9 + strlen(buf) + 1;
    byte b[size];
    int_to_bytes(size, b);
    b[2] = GAME_INIT;
    b[3] = game_type;
    b[4] = opponents;
    b[5] = x;
    b[6] = y;
    b[7] = ox;
    b[8] = oy;

    strcpy(b + 9, buf);

    if(write(sd, b, size) != -1) {
        return 0;
    } else
        return -1;
}
Ejemplo n.º 8
0
void ajouter_nuplet () {
    printf("\nÀ quelle relation voulez-vous ajouter un n-uplet?\n");
    Meta_relation *relation = proposer_relation();
    Buffer *buffer = creer_buffer(0, 64);
    for (int ind_att = 0; ind_att<relation->nb_attr; ind_att++) {
        Meta_attribut *attribut = relation->attributs[ind_att];
        
        printf("saisissez la valeur de l'attribut %s ", attribut->id);
        if (attribut->domaine->type == 0) {
            printf("de type INT\n");
            printf ("Votre saisie : ");
            scanf("%d", &choix_utilisateur);
            
            // Copie des octets dans le buffer
            unsigned char * octets = int_to_bytes(choix_utilisateur);
            memcpy(buffer->donnees+buffer->taille, octets, sizeof(unsigned char) * 4);
            
            // Mise à jour de la taille du buffer
            buffer->taille += 4;
            afficher_buffer(buffer);
        }
        else {
            printf("de type VARCHAR (%d)\n", attribut->domaine->taille);
            char *varchar = (char *) malloc (sizeof(attribut->domaine->taille));
            printf("Votre saisie : ");
            scanf("%s", varchar);
            unsigned long taille_varchar = strlen(varchar);
            
            //  AJOUT DE LA TAILLE REELLE DU VARCHAR
            buffer->donnees[buffer->taille] = int_to_char((int) taille_varchar);
            buffer->taille++;
            
            //  AJOUT DU VARCHAR
            strcpy(buffer->donnees + buffer->taille, varchar);
            buffer->taille+= taille_varchar;
            afficher_buffer(buffer);
        }
    }
    // ENREGISTREMENT DU NUPLET EN MEMOIRE VIVE
    enregistrer_nuplet(buffer, relation);
}
Ejemplo n.º 9
0
int send_shutdown_notification(int sd, char *msg) {
    int msg_size = 4 + strlen(msg) + 1;
    char *buf = malloc(msg_size);
    memset(buf, 0, msg_size);
    if(buf == NULL) {
        perror("Error allocating memory in generate_shutdown_notification()\n");
        exit(-1);
    }
    byte b[2];
    int_to_bytes(msg_size, b);
    buf[0] = b[0];
    buf[1] = b[1];
    buf[2] = SHUTDOWN_NOTIFICATION;
    buf[3] = 0; //reserved

    strcpy(buf + 4, msg);

    int result = write(sd, buf, msg_size);
    free(buf);
    return result;
}
Ejemplo n.º 10
0
size_t entry_to_bytes(pair<int, float>& vals, char * dest, float norm = 1.) {
  int i = int_to_bytes(vals.first, dest);
  dest[i] = (char)(signed char)max(-127, min(127, int(vals.second/norm*128.)));
  int (size_t)(i+1);
}
Ejemplo n.º 11
0
void enregistrer_nuplet (Buffer *buffer, Meta_relation *relation) {
    int ajout = 0;
    int index = -1;
    Assoc_rel_page *assoc = NULL;
    for (int cpt=0; cpt<annuaire->nb_rel_pages; cpt++) {
        
        if ((assoc = annuaire->rel_pages[cpt])->id_rel[0] != '\0') {
            if (strcmp(assoc->id_rel, relation->id) == 0) {
                Page *page = donnees_BD->pages[assoc->index_page];
                int nb_nuplet = (int) page->enregistrement[63];
                
                // RECHERCHE DU DERNIER DESCRIPTEUR
                int limite_zg = limite_zone_de_gestion(62-2*(nb_nuplet), page);
                
                for (int i = 62; i > limite_zg; i=i-2) {
                    
                    // PLACE EN PLEIN MILLIEU
                    if (page->enregistrement[i-1] == '0') {
                        
                        //VERIFICATION TAILLE LIBRE
                        int taille_libre = ((int) (page->enregistrement[i])) - ((int)(page->enregistrement[i+2]));
                        
                        //TAILLE LIBRE = TAILLE A INSERER
                        if (taille_libre == buffer->taille) {
                            page->enregistrement[i-1] = '1';
                            inserer_donnees(page, ((int) page->enregistrement[i+2]), buffer);
                            ajout = 1;
                            break;
                        }
                        
                        //TAILLE LIBRE > TAILLE A INSERER
                        else if (taille_libre > buffer->taille) {
                            
                            //VERIFICATION TAILLE DISPONIBLE POUR ZONE DE GESTION
                            if (((int) page->enregistrement[limite_zg+2]) < limite_zg) {
                                decaler_gauche_zone_gestion(page, i, buffer->taille, limite_zg);
                                inserer_donnees(page, ((int) page->enregistrement[i+2]), buffer);
                                ajout = 1;
                                break;
                            }
                        }
                    }
                }
                
                // NUPLET NON AJOUTE AU MILIEU DE LA PAGE
                if (ajout == 0) {
                    
                    // AJOUT A LA SUITE DE LA PAGE
                    int prochaine_position = ((int) page->enregistrement[limite_zg + 2]) + buffer->taille;
                    
                    if (prochaine_position + 2 <= limite_zg) {
                        page->enregistrement[limite_zg - 1] = '1';
                        page->enregistrement[limite_zg] = int_to_bytes(prochaine_position)[3];
                        inserer_donnees(page, ((int) page->enregistrement[limite_zg+2]), buffer);
                        ajout=1;
                        break;
                    }
                }
                else {
                    // ON SORT DE LA RECHERCHE
                    break;
                }
            }
        }
        else {
            if (index == -1) {
                index = assoc->index_page;
            }
        }
    }
    if (ajout == 0) {
        //INSERTION DANS UNE NOUVELLE PAGE
        assoc=annuaire->rel_pages[index];
        strcpy (annuaire->rel_pages[index]->id_rel, relation->id);
        annuaire->rel_pages[index]->index_page=index;
        Page *page = donnees_BD->pages[index];
        page->enregistrement[61] = '1';
        page->enregistrement[62] = int_to_bytes(buffer->taille)[3];
        inserer_donnees(page, 0, buffer);
        
    }
    printf("\nLe nuplet a été enregistré dans la page n°%d\n", assoc->index_page);
}
Ejemplo n.º 12
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);
            }
        }
    }
}
Ejemplo n.º 13
0
//start_index从1开始,不算0
int BCEGenPrivateKeys(byte *sys_params_path, byte *sys_priv_key, int num_user, int start_index, int length, byte *user_priv_keys_out, int check_array)
{
    global_broadcast_params_t gbs;
    broadcast_system_t sys;
    int priv_key_read, i;
    //struct single_priv_key_s user_keys[length];  // Problematic Memory Allocation

    struct single_priv_key_s *user_keys;

    char *recip;
    // char recip[num_user / NUM_USER_DIVISOR];

    if (sys_params_path == NULL)
        return 1;

    if (sys_priv_key == NULL)
        return 2;

    if (num_user % NUM_USER_DIVISOR != 0)
        return 3;

    if (start_index % NUM_USER_DIVISOR != 1)
        return 4;

    if (user_priv_keys_out == NULL)
        return 6;

    if (length > check_array)
        return -2;

    LoadParams((char *) sys_params_path, &gbs, &sys);

    priv_key_read = element_from_bytes(sys->priv_key, sys_priv_key);

    if (priv_key_read != BCE_ZR_LENGTH)
        return -1;

    recip = (char *) malloc(num_user / NUM_USER_DIVISOR);

    memset(recip, BIT_VECTOR_UNIT_VALUE, num_user / NUM_USER_DIVISOR);

    user_keys = (priv_key_t) malloc(length * sizeof(struct single_priv_key_s)); //**

    for (i = 0; i < length; i++)
    {
        Get_priv_key(gbs, sys, start_index + i, &user_keys[i]);
        Gen_decr_prod_from_bitvec(gbs, start_index + i, recip, &user_keys[i]);
    }

    for (i = 0; i < length; i++)
    {
        int_to_bytes(user_keys[i].index, user_priv_keys_out);
        user_priv_keys_out += 4;
        user_priv_keys_out += element_to_bytes(user_priv_keys_out, user_keys[i].g_i_gamma);
        user_priv_keys_out += element_to_bytes(user_priv_keys_out, user_keys[i].g_i);
        user_priv_keys_out += element_to_bytes(user_priv_keys_out, user_keys[i].h_i);
        user_priv_keys_out += element_to_bytes(user_priv_keys_out, user_keys[i].decr_prod);
    }

    memset(recip, BIT_VECTOR_CLEAR_UNIT_VALUE, num_user / NUM_USER_DIVISOR);
    free(recip);
    for (i = 0; i < length; i++)
    {
        FreePK(&user_keys[i]);
    }
    free(user_keys);
    FreeBCS(sys);
    pbc_free(sys);
    FreeGBP(gbs);
    pbc_free(gbs);

    return 0;
}