/home/dko/projects/mobilec/trunk/src/security/xyssl-0.7/programs/hash/md5sum.c

Go to the documentation of this file.
00001 /* SVN FILE INFO
00002  * $Revision: 174 $ : Last Committed Revision
00003  * $Date: 2008-06-24 10:50:29 -0700 (Tue, 24 Jun 2008) $ : Last Committed Date */
00004 /*
00005  *  md5sum demonstration program
00006  *
00007  *  Copyright (C) 2006-2007  Christophe Devine
00008  *
00009  *  This library is free software; you can redistribute it and/or
00010  *  modify it under the terms of the GNU Lesser General Public
00011  *  License, version 2.1 as published by the Free Software Foundation.
00012  *
00013  *  This library is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  *  Lesser General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU Lesser General Public
00019  *  License along with this library; if not, write to the Free Software
00020  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00021  *  MA  02110-1301  USA
00022  */
00023 
00024 #ifndef _CRT_SECURE_NO_DEPRECATE
00025 #define _CRT_SECURE_NO_DEPRECATE 1
00026 #endif
00027 
00028 #include <string.h>
00029 #include <stdio.h>
00030 
00031 #include "xyssl/md5.h"
00032 
00033 int md5_wrapper( char *filename, unsigned char *sum )
00034 {
00035     int ret = md5_file( filename, sum );
00036 
00037     if( ret == 1 )
00038         fprintf( stderr, "failed to open: %s\n", filename );
00039 
00040     if( ret == 2 )
00041         fprintf( stderr, "failed to read: %s\n", filename );
00042 
00043     return( ret );
00044 }
00045 
00046 int md5_print( char *filename )
00047 {
00048     int i;
00049     unsigned char sum[16];
00050 
00051     if( md5_wrapper( filename, sum ) != 0 )
00052         return( 1 );
00053 
00054     for( i = 0; i < 16; i++ )
00055         printf( "%02x", sum[i] );
00056 
00057     printf( "  %s\n", filename );
00058     return( 0 );
00059 }
00060 
00061 int md5_check( char *filename )
00062 {
00063     int i;
00064     size_t n;
00065     FILE *f;
00066     int nb_err1, nb_err2;
00067     int nb_tot1, nb_tot2;
00068     unsigned char sum[16];
00069     char buf[33], line[1024];
00070 
00071     if( ( f = fopen( filename, "rb" ) ) == NULL )
00072     {
00073         printf( "failed to open: %s\n", filename );
00074         return( 1 );
00075     }
00076 
00077     nb_err1 = nb_err2 = 0;
00078     nb_tot1 = nb_tot2 = 0;
00079 
00080     memset( line, 0, sizeof( line ) );
00081 
00082     n = sizeof( line );
00083 
00084     while( fgets( line, n - 1, f ) != NULL )
00085     {
00086         n = strlen( line );
00087 
00088         if( n < 36 )
00089             continue;
00090 
00091         if( line[32] != ' ' || line[33] != ' ' )
00092             continue;
00093 
00094         if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
00095         if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
00096 
00097         nb_tot1++;
00098 
00099         if( md5_wrapper( line + 34, sum ) != 0 )
00100         {
00101             nb_err1++;
00102             continue;
00103         }
00104 
00105         nb_tot2++;
00106 
00107         for( i = 0; i < 16; i++ )
00108             sprintf( buf + i * 2, "%02x", sum[i] );
00109 
00110         if( memcmp( line, buf, 32 ) != 0 )
00111         {
00112             nb_err2++;
00113             fprintf( stderr, "wrong checksum: %s\n", line + 34 );
00114         }
00115 
00116         n = sizeof( line );
00117     }
00118 
00119     if( nb_err1 != 0 )
00120     {
00121         printf( "WARNING: %d (out of %d) input files could "
00122                 "not be read\n", nb_err1, nb_tot1 );
00123     }
00124 
00125     if( nb_err2 != 0 )
00126     {
00127         printf( "WARNING: %d (out of %d) computed checksums did "
00128                 "not match\n", nb_err2, nb_tot2 );
00129     }
00130 
00131     return( nb_err1 != 0 || nb_err2 != 0 );
00132 }
00133 
00134 int main( int argc, char *argv[] )
00135 {
00136     int ret, i;
00137 
00138     if( argc == 1 )
00139     {
00140         printf( "print mode:  md5sum <file> <file> ...\n" );
00141         printf( "check mode:  md5sum -c <checksum file>\n" );
00142 
00143 #ifdef WIN32
00144         printf( "\n  Press Enter to exit this program.\n" );
00145         fflush( stdout ); getchar();
00146 #endif
00147 
00148         return( 1 );
00149     }
00150 
00151     if( argc == 3 && strcmp( "-c", argv[1] ) == 0 )
00152         return( md5_check( argv[2] ) );
00153 
00154     ret = 0;
00155     for( i = 1; i < argc; i++ )
00156         ret |= md5_print( argv[i] );
00157 
00158     return( ret );
00159 }

Generated on Tue Jul 1 15:29:59 2008 for Mobile-C by  doxygen 1.5.4