1: /*
2: ================================================================================
3: RPL/2 (R) version 4.0.12
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:
43: logical1
44: controle(struct_processus *s_etat_processus, unsigned char *fichier,
45: unsigned char *type, unsigned char *somme_candidate)
46: {
47: EVP_MD_CTX contexte;
48:
49: int i_rval = 0;
50: int in_fd = -1;
51:
52: logical1 drapeau;
53:
54: off_t taille_fichier;
55:
56: ssize_t octets_lus;
57:
58: struct stat stat_buf;
59:
60: unsigned char *chaine;
61: unsigned char somme[EVP_MAX_MD_SIZE];
62: unsigned char somme_hexadecimale[2 * EVP_MAX_MD_SIZE];
63:
64: unsigned int i;
65: unsigned int longueur_somme;
66:
67: i_rval = stat(fichier, &stat_buf);
68: taille_fichier = stat_buf.st_size;
69:
70: if ((chaine = malloc(taille_fichier)) == NULL)
71: {
72: (*s_etat_processus).erreur_systeme = d_es_allocation_memoire;
73: return(d_faux);
74: }
75:
76: if ((in_fd = open(fichier, 0, O_RDONLY)) < 0)
77: {
78: free(chaine);
79:
80: (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
81: return(d_faux);
82: }
83:
84: if ((octets_lus = read(in_fd, chaine, taille_fichier)) != taille_fichier)
85: {
86: close(in_fd);
87: free(chaine);
88:
89: (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
90: return(d_faux);
91: }
92:
93: close(in_fd);
94:
95: if (strcmp(type, "md5") == 0)
96: {
97: if (EVP_DigestInit(&contexte, EVP_md5()) != 1)
98: {
99: close(in_fd);
100: free(chaine);
101:
102: (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
103: return(d_faux);
104: }
105:
106: if (EVP_DigestUpdate(&contexte, chaine, taille_fichier) != 1)
107: {
108: close(in_fd);
109: free(chaine);
110:
111: (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
112: return(d_faux);
113: }
114:
115: if (EVP_DigestFinal_ex(&contexte, somme, &longueur_somme) != 1)
116: {
117: close(in_fd);
118: free(chaine);
119:
120: (*s_etat_processus).erreur_systeme = d_es_erreur_fichier;
121: return(d_faux);
122: }
123: }
124: else if (strcmp(type, "sha1") == 0)
125: {
126: if (EVP_DigestInit(&contexte, EVP_sha1()) != 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_DigestUpdate(&contexte, chaine, taille_fichier) != 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_DigestFinal_ex(&contexte, somme, &longueur_somme) != 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:
154: EVP_MD_CTX_cleanup(&contexte);
155: free(chaine);
156:
157: for(i = 0; i < longueur_somme; i++)
158: {
159: sprintf(&(somme_hexadecimale[2 * i]), "%02x", somme[i]);
160: }
161:
162: if (strcmp(somme_candidate, somme_hexadecimale) == 0)
163: {
164: drapeau = d_vrai;
165: }
166: else
167: {
168: drapeau = d_faux;
169: }
170:
171: return(drapeau);
172: }
173:
174: // vim: ts=4
CVSweb interface <joel.bertrand@systella.fr>