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

1.1       bertrand    1: /*
                      2: ================================================================================
1.3     ! bertrand    3:   RPL/2 (R) version 4.0.13
1.2       bertrand    4:   Copyright (C) 1989-2010 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: 
                     23: #include "rpl.conv.h"
                     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: {
                     46:    EVP_MD_CTX          contexte;
                     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;
                     59:    unsigned char       somme[EVP_MAX_MD_SIZE];
                     60:    unsigned char       somme_hexadecimale[2 * EVP_MAX_MD_SIZE];
                     61: 
                     62:    unsigned int        i;
                     63:    unsigned int        longueur_somme;
                     64: 
1.2       bertrand   65:    if (stat(fichier, &stat_buf) != 0)
                     66:    {
                     67:        (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
                     68:        return(d_faux);
                     69:    }
                     70: 
1.1       bertrand   71:    taille_fichier = stat_buf.st_size;
                     72: 
                     73:    if ((chaine = malloc(taille_fichier)) == NULL)
                     74:    {
                     75:        (*s_etat_processus).erreur_systeme = d_es_allocation_memoire;
                     76:        return(d_faux);
                     77:    }
                     78: 
                     79:    if ((in_fd = open(fichier, 0, O_RDONLY)) < 0)
                     80:    {
                     81:        free(chaine);
                     82: 
                     83:        (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
                     84:        return(d_faux);
                     85:    }
                     86: 
                     87:    if ((octets_lus = read(in_fd, chaine, taille_fichier)) != taille_fichier)
                     88:    {
                     89:        close(in_fd);
                     90:        free(chaine);
                     91: 
                     92:        (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
                     93:        return(d_faux);
                     94:    }
                     95: 
                     96:    close(in_fd);
                     97: 
                     98:    if (strcmp(type, "md5") == 0)
                     99:    {
                    100:        if (EVP_DigestInit(&contexte, EVP_md5()) != 1)
                    101:        {
                    102:            close(in_fd);
                    103:            free(chaine);
                    104: 
                    105:            (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
                    106:            return(d_faux);
                    107:        }
                    108: 
                    109:        if (EVP_DigestUpdate(&contexte, chaine, taille_fichier) != 1)
                    110:        {
                    111:            close(in_fd);
                    112:            free(chaine);
                    113: 
                    114:            (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
                    115:            return(d_faux);
                    116:        }
                    117: 
                    118:        if (EVP_DigestFinal_ex(&contexte, somme, &longueur_somme) != 1)
                    119:        {
                    120:            close(in_fd);
                    121:            free(chaine);
                    122: 
                    123:            (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
                    124:            return(d_faux);
                    125:        }
                    126:    }
                    127:    else if (strcmp(type, "sha1") == 0)
                    128:    {
                    129:        if (EVP_DigestInit(&contexte, EVP_sha1()) != 1)
                    130:        {
                    131:            close(in_fd);
                    132:            free(chaine);
                    133: 
                    134:            (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
                    135:            return(d_faux);
                    136:        }
                    137: 
                    138:        if (EVP_DigestUpdate(&contexte, chaine, taille_fichier) != 1)
                    139:        {
                    140:            close(in_fd);
                    141:            free(chaine);
                    142: 
                    143:            (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
                    144:            return(d_faux);
                    145:        }
                    146: 
                    147:        if (EVP_DigestFinal_ex(&contexte, somme, &longueur_somme) != 1)
                    148:        {
                    149:            close(in_fd);
                    150:            free(chaine);
                    151: 
                    152:            (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
                    153:            return(d_faux);
                    154:        }
                    155:    }
1.2       bertrand  156:    else
                    157:    {
                    158:        return(d_faux);
                    159:    }
1.1       bertrand  160: 
                    161:    EVP_MD_CTX_cleanup(&contexte);
                    162:    free(chaine);
                    163: 
                    164:    for(i = 0; i < longueur_somme; i++)
                    165:    {
                    166:        sprintf(&(somme_hexadecimale[2 * i]), "%02x", somme[i]);
                    167:    }
                    168: 
                    169:    if (strcmp(somme_candidate, somme_hexadecimale) == 0)
                    170:    {
                    171:        drapeau = d_vrai;
                    172:    }
                    173:    else
                    174:    {
                    175:        drapeau = d_faux;
                    176:    }
                    177: 
                    178:    return(drapeau);
                    179: }
                    180: 
                    181: // vim: ts=4

CVSweb interface <joel.bertrand@systella.fr>