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

1.1       bertrand    1: /*
                      2: ================================================================================
                      3:   RPL/2 (R) version 4.0.12
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: 
                     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: {
                     33:    EVP_MD_CTX          contexte;
                     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:    {
                     61:        exit(EXIT_FAILURE);
                     62:    }
                     63: 
                     64:    taille_fichier = stat_buf.st_size;
                     65: 
                     66:    if ((chaine = malloc(taille_fichier)) == NULL)
                     67:    {
                     68:        exit(EXIT_FAILURE);
                     69:    }
                     70: 
                     71:    if ((in_fd = open(fichier, 0, O_RDONLY)) < 0)
                     72:    {
                     73:        free(chaine);
                     74:        exit(EXIT_FAILURE);
                     75:    }
                     76: 
                     77:    if ((octets_lus = read(in_fd, chaine, taille_fichier)) != taille_fichier)
                     78:    {
                     79:        close(in_fd);
                     80:        free(chaine);
                     81:        exit(EXIT_FAILURE);
                     82:    }
                     83: 
                     84:    close(in_fd);
                     85: 
                     86:    if (EVP_DigestInit(&contexte, EVP_sum()) != 1)
                     87:    {
                     88:        close(in_fd);
                     89:        free(chaine);
                     90:        exit(EXIT_FAILURE);
                     91:    }
                     92: 
                     93:    if (EVP_DigestUpdate(&contexte, chaine, taille_fichier) != 1)
                     94:    {
                     95:        close(in_fd);
                     96:        free(chaine);
                     97:        exit(EXIT_FAILURE);
                     98:    }
                     99: 
                    100:    if (EVP_DigestFinal_ex(&contexte, somme, &longueur_somme) != 1)
                    101:    {
                    102:        close(in_fd);
                    103:        free(chaine);
                    104:        exit(EXIT_FAILURE);
                    105:    }
                    106: 
                    107:    EVP_MD_CTX_cleanup(&contexte);
                    108:    free(chaine);
                    109: 
                    110:    for(i = 0; i < longueur_somme; i++)
                    111:    {
                    112:        sprintf(&(somme_hexadecimale[2 * i]), "%02x", somme[i]);
                    113:    }
                    114: 
                    115:    printf("%s\n", somme_hexadecimale);
                    116: 
                    117:    return(EXIT_SUCCESS);
                    118: }
                    119: 
                    120: // vim: ts=4

CVSweb interface <joel.bertrand@systella.fr>