Annotation of rpl/src/controle.c, revision 1.69

1.1       bertrand    1: /*
                      2: ================================================================================
1.68      bertrand    3:   RPL/2 (R) version 4.1.32
1.69    ! bertrand    4:   Copyright (C) 1989-2020 Dr. BERTRAND Joël
1.1       bertrand    5: 
                      6:   This file is part of RPL/2.
                      7: 
                      8:   RPL/2 is free software; you can redistribute it and/or modify it
                      9:   under the terms of the CeCILL V2 License as published by the french
                     10:   CEA, CNRS and INRIA.
                     11:  
                     12:   RPL/2 is distributed in the hope that it will be useful, but WITHOUT
                     13:   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
                     14:   FITNESS FOR A PARTICULAR PURPOSE.  See the CeCILL V2 License
                     15:   for more details.
                     16:  
                     17:   You should have received a copy of the CeCILL License
                     18:   along with RPL/2. If not, write to info@cecill.info.
                     19: ================================================================================
                     20: */
                     21: 
                     22: 
1.9       bertrand   23: #include "rpl-conv.h"
1.1       bertrand   24: 
                     25: 
                     26: /*
                     27: ================================================================================
1.2       bertrand   28:   Calcul des sommes de contrôle avant le lancement d'un exécutable
1.1       bertrand   29:   de la famille RPL/2 (rpliconv, rplconvert, rplfile et rplpp).
                     30: ================================================================================
1.2       bertrand   31:   Entrée :
                     32:    - chaîne de caractères sur le fichier à contrôler
                     33:    - chaîne de caractères contenant la somme à contrôler
                     34:    - type de somme de contrôle
1.1       bertrand   35: --------------------------------------------------------------------------------
                     36:   Sortie : drapeau
                     37: --------------------------------------------------------------------------------
1.2       bertrand   38:   Effets de bord : néant
1.1       bertrand   39: ================================================================================
                     40: */
                     41: 
                     42: logical1
                     43: controle(struct_processus *s_etat_processus, unsigned char *fichier,
                     44:        unsigned char *type, unsigned char *somme_candidate)
                     45: {
1.59      bertrand   46:    EVP_MD_CTX          *contexte;
1.1       bertrand   47: 
1.2       bertrand   48:    int                 in_fd;
1.1       bertrand   49: 
                     50:    logical1            drapeau;
                     51: 
                     52:    off_t               taille_fichier;
                     53: 
                     54:    ssize_t             octets_lus;
                     55: 
                     56:    struct stat         stat_buf;
                     57: 
                     58:    unsigned char       *chaine;
1.14      bertrand   59:    unsigned char       *registre;
1.1       bertrand   60:    unsigned char       somme[EVP_MAX_MD_SIZE];
                     61:    unsigned char       somme_hexadecimale[2 * EVP_MAX_MD_SIZE];
                     62: 
                     63:    unsigned int        i;
                     64:    unsigned int        longueur_somme;
                     65: 
1.14      bertrand   66:    registre = fichier;
                     67: 
1.11      bertrand   68: #  ifdef OS2
                     69:    unsigned char       *tampon;
                     70: 
                     71:    if ((tampon = malloc((strlen(fichier) + 5) * sizeof(unsigned char)))
                     72:            == NULL)
                     73:    {
                     74:        (*s_etat_processus).erreur_systeme = d_es_allocation_memoire;
                     75:        return(d_faux);
                     76:    }
                     77: 
                     78:    sprintf(tampon, "%s.exe", fichier);
                     79:    fichier = tampon;
                     80: #  endif
                     81: 
1.2       bertrand   82:    if (stat(fichier, &stat_buf) != 0)
                     83:    {
1.12      bertrand   84: #      ifdef OS2
                     85:        free(fichier);
                     86: #      endif
                     87: 
1.2       bertrand   88:        (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
                     89:        return(d_faux);
                     90:    }
                     91: 
1.1       bertrand   92:    taille_fichier = stat_buf.st_size;
                     93: 
1.44      bertrand   94:    if ((chaine = malloc((size_t) taille_fichier)) == NULL)
1.1       bertrand   95:    {
1.12      bertrand   96: #      ifdef OS2
                     97:        free(fichier);
                     98: #      endif
                     99: 
1.1       bertrand  100:        (*s_etat_processus).erreur_systeme = d_es_allocation_memoire;
                    101:        return(d_faux);
                    102:    }
                    103: 
                    104:    if ((in_fd = open(fichier, 0, O_RDONLY)) < 0)
                    105:    {
1.12      bertrand  106: #      ifdef OS2
                    107:        free(fichier);
                    108: #      endif
                    109: 
1.1       bertrand  110:        free(chaine);
                    111: 
                    112:        (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
                    113:        return(d_faux);
                    114:    }
                    115: 
1.12      bertrand  116: #  ifdef OS2
                    117:    free(fichier);
                    118: #  endif
                    119: 
1.44      bertrand  120:    if ((octets_lus = read(in_fd, chaine, (size_t) taille_fichier))
                    121:            != (ssize_t) taille_fichier)
1.1       bertrand  122:    {
1.9       bertrand  123: #      ifndef OS2
1.1       bertrand  124:        close(in_fd);
                    125:        free(chaine);
                    126: 
                    127:        (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
                    128:        return(d_faux);
1.9       bertrand  129: #      endif
1.1       bertrand  130:    }
                    131: 
                    132:    close(in_fd);
                    133: 
1.59      bertrand  134:    if ((contexte = EVP_MD_CTX_new()) == NULL)
                    135:    {
                    136:        close(in_fd);
                    137:        return(d_faux);
                    138:    }
                    139: 
1.1       bertrand  140:    if (strcmp(type, "md5") == 0)
                    141:    {
1.59      bertrand  142:        if (EVP_DigestInit(contexte, EVP_md5()) != 1)
1.1       bertrand  143:        {
1.59      bertrand  144:            EVP_MD_CTX_free(contexte);
1.1       bertrand  145:            close(in_fd);
                    146:            free(chaine);
                    147: 
                    148:            (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
                    149:            return(d_faux);
                    150:        }
                    151: 
1.59      bertrand  152:        if (EVP_DigestUpdate(contexte, chaine, (size_t) taille_fichier) != 1)
1.1       bertrand  153:        {
1.59      bertrand  154:            EVP_MD_CTX_free(contexte);
1.1       bertrand  155:            close(in_fd);
                    156:            free(chaine);
                    157: 
                    158:            (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
                    159:            return(d_faux);
                    160:        }
                    161: 
1.59      bertrand  162:        if (EVP_DigestFinal_ex(contexte, somme, &longueur_somme) != 1)
1.1       bertrand  163:        {
1.59      bertrand  164:            EVP_MD_CTX_free(contexte);
1.1       bertrand  165:            close(in_fd);
                    166:            free(chaine);
                    167: 
                    168:            (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
                    169:            return(d_faux);
                    170:        }
                    171:    }
                    172:    else if (strcmp(type, "sha1") == 0)
                    173:    {
1.59      bertrand  174:        if (EVP_DigestInit(contexte, EVP_sha1()) != 1)
1.1       bertrand  175:        {
1.59      bertrand  176:            EVP_MD_CTX_free(contexte);
1.1       bertrand  177:            close(in_fd);
                    178:            free(chaine);
                    179: 
                    180:            (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
                    181:            return(d_faux);
                    182:        }
                    183: 
1.59      bertrand  184:        if (EVP_DigestUpdate(contexte, chaine, (size_t) taille_fichier) != 1)
1.1       bertrand  185:        {
1.59      bertrand  186:            EVP_MD_CTX_free(contexte);
1.1       bertrand  187:            close(in_fd);
                    188:            free(chaine);
                    189: 
                    190:            (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
                    191:            return(d_faux);
                    192:        }
                    193: 
1.59      bertrand  194:        if (EVP_DigestFinal_ex(contexte, somme, &longueur_somme) != 1)
1.1       bertrand  195:        {
1.59      bertrand  196:            EVP_MD_CTX_free(contexte);
1.1       bertrand  197:            close(in_fd);
                    198:            free(chaine);
                    199: 
                    200:            (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
                    201:            return(d_faux);
                    202:        }
                    203:    }
1.2       bertrand  204:    else
                    205:    {
                    206:        return(d_faux);
                    207:    }
1.1       bertrand  208: 
1.59      bertrand  209:    EVP_MD_CTX_free(contexte);
1.1       bertrand  210:    free(chaine);
                    211: 
                    212:    for(i = 0; i < longueur_somme; i++)
                    213:    {
                    214:        sprintf(&(somme_hexadecimale[2 * i]), "%02x", somme[i]);
                    215:    }
                    216: 
                    217:    if (strcmp(somme_candidate, somme_hexadecimale) == 0)
                    218:    {
                    219:        drapeau = d_vrai;
                    220:    }
                    221:    else
                    222:    {
1.14      bertrand  223:        if ((*s_etat_processus).langue == 'F')
                    224:        {
                    225:            printf("+++Fatal : Somme de contrôle invalide\n");
1.15      bertrand  226:            printf("Fonction %s(%s)\n", type, registre);
1.14      bertrand  227:            printf("Résultat obtenu  : %s\n", somme_hexadecimale);
                    228:            printf("Résultat attendu : %s\n", somme_candidate);
                    229:        }
                    230:        else
                    231:        {
                    232:            printf("+++Fatal : Hash code mismatch\n");
1.15      bertrand  233:            printf("Function %s(%s)\n", type, registre);
1.14      bertrand  234:            printf("Computed hash code : %s\n", somme_hexadecimale);
1.19      bertrand  235:            printf("Expected hash code : %s\n", somme_candidate);
1.14      bertrand  236:        }
                    237: 
1.1       bertrand  238:        drapeau = d_faux;
                    239:    }
                    240: 
                    241:    return(drapeau);
                    242: }
                    243: 
                    244: // vim: ts=4

CVSweb interface <joel.bertrand@systella.fr>