File:  [local] / rpl / src / controle.c
Revision 1.19.2.2: download - view: text, annotated - select for diffs - revision graph
Thu Apr 14 08:46:36 2011 UTC (13 years ago) by bertrand
Branches: rpl-4_0
CVS tags: rpl-4_0_24
Diff to: branchpoint 1.19: preferred, colored
En route pour la 4.0.23.

    1: /*
    2: ================================================================================
    3:   RPL/2 (R) version 4.0.23
    4:   Copyright (C) 1989-2011 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       *registre;
   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: 
   66:     registre = fichier;
   67: 
   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: 
   82:     if (stat(fichier, &stat_buf) != 0)
   83:     {
   84: #       ifdef OS2
   85:         free(fichier);
   86: #       endif
   87: 
   88:         (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
   89:         return(d_faux);
   90:     }
   91: 
   92:     taille_fichier = stat_buf.st_size;
   93: 
   94:     if ((chaine = malloc(taille_fichier)) == NULL)
   95:     {
   96: #       ifdef OS2
   97:         free(fichier);
   98: #       endif
   99: 
  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:     {
  106: #       ifdef OS2
  107:         free(fichier);
  108: #       endif
  109: 
  110:         free(chaine);
  111: 
  112:         (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
  113:         return(d_faux);
  114:     }
  115: 
  116: #   ifdef OS2
  117:     free(fichier);
  118: #   endif
  119: 
  120:     if ((octets_lus = read(in_fd, chaine, taille_fichier)) != taille_fichier)
  121:     {
  122: #       ifndef OS2
  123:         close(in_fd);
  124:         free(chaine);
  125: 
  126:         (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
  127:         return(d_faux);
  128: #       endif
  129:     }
  130: 
  131:     close(in_fd);
  132: 
  133:     if (strcmp(type, "md5") == 0)
  134:     {
  135:         if (EVP_DigestInit(&contexte, EVP_md5()) != 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:         if (EVP_DigestUpdate(&contexte, chaine, taille_fichier) != 1)
  145:         {
  146:             close(in_fd);
  147:             free(chaine);
  148: 
  149:             (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
  150:             return(d_faux);
  151:         }
  152: 
  153:         if (EVP_DigestFinal_ex(&contexte, somme, &longueur_somme) != 1)
  154:         {
  155:             close(in_fd);
  156:             free(chaine);
  157: 
  158:             (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
  159:             return(d_faux);
  160:         }
  161:     }
  162:     else if (strcmp(type, "sha1") == 0)
  163:     {
  164:         if (EVP_DigestInit(&contexte, EVP_sha1()) != 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: 
  173:         if (EVP_DigestUpdate(&contexte, chaine, taille_fichier) != 1)
  174:         {
  175:             close(in_fd);
  176:             free(chaine);
  177: 
  178:             (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
  179:             return(d_faux);
  180:         }
  181: 
  182:         if (EVP_DigestFinal_ex(&contexte, somme, &longueur_somme) != 1)
  183:         {
  184:             close(in_fd);
  185:             free(chaine);
  186: 
  187:             (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
  188:             return(d_faux);
  189:         }
  190:     }
  191:     else
  192:     {
  193:         return(d_faux);
  194:     }
  195: 
  196:     EVP_MD_CTX_cleanup(&contexte);
  197:     free(chaine);
  198: 
  199:     for(i = 0; i < longueur_somme; i++)
  200:     {
  201:         sprintf(&(somme_hexadecimale[2 * i]), "%02x", somme[i]);
  202:     }
  203: 
  204:     if (strcmp(somme_candidate, somme_hexadecimale) == 0)
  205:     {
  206:         drapeau = d_vrai;
  207:     }
  208:     else
  209:     {
  210:         if ((*s_etat_processus).langue == 'F')
  211:         {
  212:             printf("+++Fatal : Somme de contrôle invalide\n");
  213:             printf("Fonction %s(%s)\n", type, registre);
  214:             printf("Résultat obtenu  : %s\n", somme_hexadecimale);
  215:             printf("Résultat attendu : %s\n", somme_candidate);
  216:         }
  217:         else
  218:         {
  219:             printf("+++Fatal : Hash code mismatch\n");
  220:             printf("Function %s(%s)\n", type, registre);
  221:             printf("Computed hash code : %s\n", somme_hexadecimale);
  222:             printf("Expected hash code : %s\n", somme_candidate);
  223:         }
  224: 
  225:         drapeau = d_faux;
  226:     }
  227: 
  228:     return(drapeau);
  229: }
  230: 
  231: // vim: ts=4

CVSweb interface <joel.bertrand@systella.fr>