File:  [local] / rpl / src / controle.c
Revision 1.10: download - view: text, annotated - select for diffs - revision graph
Fri Aug 6 15:32:57 2010 UTC (13 years, 9 months ago) by bertrand
Branches: MAIN
CVS tags: HEAD
Cohérence

    1: /*
    2: ================================================================================
    3:   RPL/2 (R) version 4.0.18
    4:   Copyright (C) 1989-2010 Dr. BERTRAND Joël
    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: ================================================================================
   28:   Calcul des sommes de contrôle avant le lancement d'un exécutable
   29:   de la famille RPL/2 (rpliconv, rplconvert, rplfile et rplpp).
   30: ================================================================================
   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
   35: --------------------------------------------------------------------------------
   36:   Sortie : drapeau
   37: --------------------------------------------------------------------------------
   38:   Effets de bord : néant
   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: 
   48:     int                 in_fd;
   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: 
   65:     if (stat(fichier, &stat_buf) != 0)
   66:     {
   67:         (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
   68:         return(d_faux);
   69:     }
   70: 
   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: #       ifndef OS2
   90:         close(in_fd);
   91:         free(chaine);
   92: 
   93:         (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
   94:         return(d_faux);
   95: #       endif
   96:     }
   97: 
   98:     close(in_fd);
   99: 
  100:     if (strcmp(type, "md5") == 0)
  101:     {
  102:         if (EVP_DigestInit(&contexte, EVP_md5()) != 1)
  103:         {
  104:             close(in_fd);
  105:             free(chaine);
  106: 
  107:             (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
  108:             return(d_faux);
  109:         }
  110: 
  111:         if (EVP_DigestUpdate(&contexte, chaine, taille_fichier) != 1)
  112:         {
  113:             close(in_fd);
  114:             free(chaine);
  115: 
  116:             (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
  117:             return(d_faux);
  118:         }
  119: 
  120:         if (EVP_DigestFinal_ex(&contexte, somme, &longueur_somme) != 1)
  121:         {
  122:             close(in_fd);
  123:             free(chaine);
  124: 
  125:             (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
  126:             return(d_faux);
  127:         }
  128:     }
  129:     else if (strcmp(type, "sha1") == 0)
  130:     {
  131:         if (EVP_DigestInit(&contexte, EVP_sha1()) != 1)
  132:         {
  133:             close(in_fd);
  134:             free(chaine);
  135: 
  136:             (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
  137:             return(d_faux);
  138:         }
  139: 
  140:         if (EVP_DigestUpdate(&contexte, chaine, taille_fichier) != 1)
  141:         {
  142:             close(in_fd);
  143:             free(chaine);
  144: 
  145:             (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
  146:             return(d_faux);
  147:         }
  148: 
  149:         if (EVP_DigestFinal_ex(&contexte, somme, &longueur_somme) != 1)
  150:         {
  151:             close(in_fd);
  152:             free(chaine);
  153: 
  154:             (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
  155:             return(d_faux);
  156:         }
  157:     }
  158:     else
  159:     {
  160:         return(d_faux);
  161:     }
  162: 
  163:     EVP_MD_CTX_cleanup(&contexte);
  164:     free(chaine);
  165: 
  166:     for(i = 0; i < longueur_somme; i++)
  167:     {
  168:         sprintf(&(somme_hexadecimale[2 * i]), "%02x", somme[i]);
  169:     }
  170: 
  171:     if (strcmp(somme_candidate, somme_hexadecimale) == 0)
  172:     {
  173:         drapeau = d_vrai;
  174:     }
  175:     else
  176:     {
  177:         drapeau = d_faux;
  178:     }
  179: 
  180:     return(drapeau);
  181: }
  182: 
  183: // vim: ts=4

CVSweb interface <joel.bertrand@systella.fr>