Annotation of rpl/rplsums/rplsum.c, revision 1.19

1.1       bertrand    1: /*
                      2: ================================================================================
1.18      bertrand    3:   RPL/2 (R) version 4.1.32
1.19    ! bertrand    4:   Copyright (C) 1989-2020 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: 
                     23: #include <stdio.h>
                     24: #include <stdlib.h>
                     25: #include <unistd.h>
                     26: #include "openssl/evp.h"
                     27: #include <sys/stat.h>
                     28: #include <fcntl.h>
                     29: 
                     30: int
                     31: main(int argc, char *argv[])
                     32: {
1.9       bertrand   33:    EVP_MD_CTX          *contexte;
1.1       bertrand   34: 
                     35:    int                 in_fd;
                     36: 
                     37:    off_t               taille_fichier;
                     38: 
                     39:    ssize_t             octets_lus;
                     40: 
                     41:    struct stat         stat_buf;
                     42: 
                     43:    unsigned char       *chaine;
                     44:    unsigned char       *fichier;
                     45:    unsigned char       somme[EVP_MAX_MD_SIZE];
                     46:    unsigned char       somme_hexadecimale[2 * EVP_MAX_MD_SIZE];
                     47: 
                     48:    unsigned int        i;
                     49:    unsigned int        longueur_somme;
                     50: 
                     51:    if (argc != 2)
                     52:    {
                     53:        printf("Usage: rplmd5sum filename\n");
                     54:        exit(EXIT_FAILURE);
                     55:    }
                     56: 
                     57:    fichier = argv[1];
                     58: 
                     59:    if (stat(fichier, &stat_buf) != 0)
                     60:    {
1.4       bertrand   61:        fprintf(stderr, "stat() failed\n");
                     62:        perror("stat");
1.1       bertrand   63:        exit(EXIT_FAILURE);
                     64:    }
                     65: 
                     66:    taille_fichier = stat_buf.st_size;
                     67: 
                     68:    if ((chaine = malloc(taille_fichier)) == NULL)
                     69:    {
1.4       bertrand   70:        fprintf(stderr, "malloc() failed\n");
1.1       bertrand   71:        exit(EXIT_FAILURE);
                     72:    }
                     73: 
                     74:    if ((in_fd = open(fichier, 0, O_RDONLY)) < 0)
                     75:    {
1.4       bertrand   76:        fprintf(stderr, "open() failed\n");
                     77:        perror("open");
1.1       bertrand   78:        free(chaine);
                     79:        exit(EXIT_FAILURE);
                     80:    }
                     81: 
                     82:    if ((octets_lus = read(in_fd, chaine, taille_fichier)) != taille_fichier)
                     83:    {
1.4       bertrand   84: #      ifndef OS2
                     85:        // Sur des fichiers binaires et sous OS/2, le nombre d'octets lus peut
1.8       bertrand   86:        // être inférieur à la taille du fichier sur le disque.
1.4       bertrand   87: 
                     88:        fprintf(stderr, "read() failed\n");
1.6       bertrand   89:        fprintf(stderr, "Meditation: %d < %d\n", (int) octets_lus,
                     90:                (int) taille_fichier);
1.4       bertrand   91:        perror("read");
1.1       bertrand   92:        close(in_fd);
                     93:        free(chaine);
                     94:        exit(EXIT_FAILURE);
1.4       bertrand   95: #      endif
1.1       bertrand   96:    }
                     97: 
                     98:    close(in_fd);
                     99: 
1.9       bertrand  100:    if ((contexte = EVP_MD_CTX_new()) == NULL)
                    101:    {
                    102:        fprintf(stderr, "EVP_MD_CTX_new() failed\n");
                    103:        close(in_fd);
                    104:        free(chaine);
                    105:        exit(EXIT_FAILURE);
                    106:    }
                    107: 
                    108:    if (EVP_DigestInit(contexte, EVP_sum()) != 1)
1.1       bertrand  109:    {
1.4       bertrand  110:        fprintf(stderr, "EVP_DigestInit() failed\n");
1.9       bertrand  111:        EVP_MD_CTX_free(contexte);
1.1       bertrand  112:        close(in_fd);
                    113:        free(chaine);
                    114:        exit(EXIT_FAILURE);
                    115:    }
                    116: 
1.9       bertrand  117:    if (EVP_DigestUpdate(contexte, chaine, taille_fichier) != 1)
1.1       bertrand  118:    {
1.4       bertrand  119:        fprintf(stderr, "EVP_DigestUpdate() failed\n");
1.9       bertrand  120:        EVP_MD_CTX_free(contexte);
1.1       bertrand  121:        close(in_fd);
                    122:        free(chaine);
                    123:        exit(EXIT_FAILURE);
                    124:    }
                    125: 
1.9       bertrand  126:    if (EVP_DigestFinal_ex(contexte, somme, &longueur_somme) != 1)
1.1       bertrand  127:    {
1.4       bertrand  128:        fprintf(stderr, "EVP_DigestFinal_ex() failed\n");
1.9       bertrand  129:        EVP_MD_CTX_free(contexte);
1.1       bertrand  130:        close(in_fd);
                    131:        free(chaine);
                    132:        exit(EXIT_FAILURE);
                    133:    }
                    134: 
1.9       bertrand  135:    EVP_MD_CTX_free(contexte);
1.1       bertrand  136:    free(chaine);
                    137: 
                    138:    for(i = 0; i < longueur_somme; i++)
                    139:    {
                    140:        sprintf(&(somme_hexadecimale[2 * i]), "%02x", somme[i]);
                    141:    }
                    142: 
                    143:    printf("%s\n", somme_hexadecimale);
                    144: 
                    145:    return(EXIT_SUCCESS);
                    146: }
                    147: 
                    148: // vim: ts=4

CVSweb interface <joel.bertrand@systella.fr>