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

1.1       bertrand    1: /*
                      2: ================================================================================
1.13    ! bertrand    3:   RPL/2 (R) version 4.0.19
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: 
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: {
                     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.11      bertrand   65: #  ifdef OS2
                     66:    unsigned char       *tampon;
                     67: 
                     68:    if ((tampon = malloc((strlen(fichier) + 5) * sizeof(unsigned char)))
                     69:            == NULL)
                     70:    {
                     71:        (*s_etat_processus).erreur_systeme = d_es_allocation_memoire;
                     72:        return(d_faux);
                     73:    }
                     74: 
                     75:    sprintf(tampon, "%s.exe", fichier);
                     76:    fichier = tampon;
                     77: #  endif
                     78: 
1.2       bertrand   79:    if (stat(fichier, &stat_buf) != 0)
                     80:    {
1.12      bertrand   81: #      ifdef OS2
                     82:        free(fichier);
                     83: #      endif
                     84: 
1.2       bertrand   85:        (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
                     86:        return(d_faux);
                     87:    }
                     88: 
1.1       bertrand   89:    taille_fichier = stat_buf.st_size;
                     90: 
                     91:    if ((chaine = malloc(taille_fichier)) == NULL)
                     92:    {
1.12      bertrand   93: #      ifdef OS2
                     94:        free(fichier);
                     95: #      endif
                     96: 
1.1       bertrand   97:        (*s_etat_processus).erreur_systeme = d_es_allocation_memoire;
                     98:        return(d_faux);
                     99:    }
                    100: 
                    101:    if ((in_fd = open(fichier, 0, O_RDONLY)) < 0)
                    102:    {
1.12      bertrand  103: #      ifdef OS2
                    104:        free(fichier);
                    105: #      endif
                    106: 
1.1       bertrand  107:        free(chaine);
                    108: 
                    109:        (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
                    110:        return(d_faux);
                    111:    }
                    112: 
1.12      bertrand  113: #  ifdef OS2
                    114:    free(fichier);
                    115: #  endif
                    116: 
1.1       bertrand  117:    if ((octets_lus = read(in_fd, chaine, taille_fichier)) != taille_fichier)
                    118:    {
1.9       bertrand  119: #      ifndef OS2
1.1       bertrand  120:        close(in_fd);
                    121:        free(chaine);
                    122: 
                    123:        (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
                    124:        return(d_faux);
1.9       bertrand  125: #      endif
1.1       bertrand  126:    }
                    127: 
                    128:    close(in_fd);
                    129: 
                    130:    if (strcmp(type, "md5") == 0)
                    131:    {
                    132:        if (EVP_DigestInit(&contexte, EVP_md5()) != 1)
                    133:        {
                    134:            close(in_fd);
                    135:            free(chaine);
                    136: 
                    137:            (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
                    138:            return(d_faux);
                    139:        }
                    140: 
                    141:        if (EVP_DigestUpdate(&contexte, chaine, taille_fichier) != 1)
                    142:        {
                    143:            close(in_fd);
                    144:            free(chaine);
                    145: 
                    146:            (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
                    147:            return(d_faux);
                    148:        }
                    149: 
                    150:        if (EVP_DigestFinal_ex(&contexte, somme, &longueur_somme) != 1)
                    151:        {
                    152:            close(in_fd);
                    153:            free(chaine);
                    154: 
                    155:            (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
                    156:            return(d_faux);
                    157:        }
                    158:    }
                    159:    else if (strcmp(type, "sha1") == 0)
                    160:    {
                    161:        if (EVP_DigestInit(&contexte, EVP_sha1()) != 1)
                    162:        {
                    163:            close(in_fd);
                    164:            free(chaine);
                    165: 
                    166:            (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
                    167:            return(d_faux);
                    168:        }
                    169: 
                    170:        if (EVP_DigestUpdate(&contexte, chaine, taille_fichier) != 1)
                    171:        {
                    172:            close(in_fd);
                    173:            free(chaine);
                    174: 
                    175:            (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
                    176:            return(d_faux);
                    177:        }
                    178: 
                    179:        if (EVP_DigestFinal_ex(&contexte, somme, &longueur_somme) != 1)
                    180:        {
                    181:            close(in_fd);
                    182:            free(chaine);
                    183: 
                    184:            (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
                    185:            return(d_faux);
                    186:        }
                    187:    }
1.2       bertrand  188:    else
                    189:    {
                    190:        return(d_faux);
                    191:    }
1.1       bertrand  192: 
                    193:    EVP_MD_CTX_cleanup(&contexte);
                    194:    free(chaine);
                    195: 
                    196:    for(i = 0; i < longueur_somme; i++)
                    197:    {
                    198:        sprintf(&(somme_hexadecimale[2 * i]), "%02x", somme[i]);
                    199:    }
                    200: 
                    201:    if (strcmp(somme_candidate, somme_hexadecimale) == 0)
                    202:    {
                    203:        drapeau = d_vrai;
                    204:    }
                    205:    else
                    206:    {
                    207:        drapeau = d_faux;
                    208:    }
                    209: 
                    210:    return(drapeau);
                    211: }
                    212: 
                    213: // vim: ts=4

CVSweb interface <joel.bertrand@systella.fr>