So here's my problem (my questions are at the end),
I'm trying to initialize a list of "Personne" from a file. Here's the struct of "Personne" and the code i'm using :
struct :
struct s_personne {
char name[STRING];
unsigned char password[SHA256_DIGEST_LENGTH];
char *salt;
char email[LSTRING];
};
code :
int parseMyJsonPersonne(Plist l){
FILE* fichier = NULL;
char buffer[1024];
char temp[502];
struct json_object *parsed_json;
struct json_object *name;
struct json_object *password;
struct json_object *salt;
struct json_object *email;
Personne p = (Personne)malloc(sizeof(Personne));
fichier = fopen("../../data/Personne.json", "r");
if (fichier != NULL) {
while (fgets(temp, 502, fichier)!=NULL) {
//printf("temp : %s", temp);
strncat(buffer, temp, strlen(temp));
if (strcmp(temp, "}\n")==0) {
//printf("Object find\n");
//printf("buffer : \n%s\n\n", buffer);
parsed_json = json_tokener_parse(buffer);
json_object_object_get_ex(parsed_json, "name", &name);
json_object_object_get_ex(parsed_json, "password", &password);
json_object_object_get_ex(parsed_json, "salt", &salt);
json_object_object_get_ex(parsed_json, "email", &email);
if (setPAll(p, (char *)json_object_get_string(name), (char *)json_object_get_string(password), (char *)json_object_get_string(salt), (char *)json_object_get_string(email))!=0) {
puts("Une ERREUR est survenue dans l'assemblage d'un objet (type=Personne)\n");
return 1;
}
if (addPersonne(p, l)!=0){
puts("Une ERREUR est survenue dans l'assemblage d'un objet (type=PList)\n");
return 1;
}
memset(buffer, '\0', 1024);
}
}
}
else {
puts("Une ERREUR est survenue lors du chargement des différents comptes\n\n");
return 1;
}
puts("Checkpoint 1 !!!");
fclose(fichier);
puts("Checkpoint 2 !!!");
free(p);
free(name);
free(password);
free(salt);
free(email);
return 0;
}
The error occurs when I try to make the fclose(fichier); however I already know that the cause of the problem is when calling the setPAll function. This one calls the setPPassword funtion which has the following code:
int setPPassword(Personne p, char * password) {
char stock[2];
long val;
printf("length of password (=64 ?) : %ld\n", strlen(password));
printf("lenght p->password (=32 ?) : %d\n\n", SHA256_DIGEST_LENGTH);
if (strlen(password) != 64) {
puts("ERROR in setPPassword");
return 1;
}
for(size_t i=0; i<SHA256_DIGEST_LENGTH; i++) {
stock[0]=password[2*i];
stock[1]=password[2*i+1];
printf("stock : %s\n", stock);
val = strtol(stock, NULL, 16);
p->password[i]=(unsigned char)val;
printf("value in p->password at %ld : %02x\n",i, p->password[i]);
}
return 0;
}
the problem is due to the p->password[i]=(unsigned char)val;
The problem here is that I really need my p->password to contain hex values. So is there a way to fix this error while initializing my p->password or is there a way to work around it by changing my setPPassword function?
(l->List)[l->size] = r; l->size++; return 0; } – magjico Apr 10 '20 at 11:14