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

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

CVSweb interface <joel.bertrand@systella.fr>