#include <stdio.h> #include <stdlib.h> #include <windows.h> char copyright[] = "\n" " Comparaison de fichier\n" " Ecrit par LEVEUGLE Damien\n" " Elguevel software (c) 2010\n\n"; char syntaxe[] = "\n Usage : compare <fichierA> <fichierB>\n\n"; /* * Compare deux fichiers octet à octet * * Return : -1 -> Erreur programme * 0 -> Identique * 1 -> Non identique */ int compareFile( const char *fichierA, const char *fichierB ) { // handles fichier HANDLE hFileA = NULL; HANDLE hFileB = NULL; // mapping HANDLE hMapA = NULL; HANDLE hMapB = NULL; // buffers char *pFileBaseA; char *pFileBaseB; // taille fichier DWORD dwFileSizeA, hwFileSizeA; DWORD dwFileSizeB, hwFileSizeB; // code retour int retour = 0; // pointeur caractere unsigned int cpt; /* Ouverture des fichiers */ hFileA = CreateFile( fichierA, GENERIC_READ + GENERIC_WRITE, FILE_SHARE_READ + FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); if( hFileA == INVALID_HANDLE_VALUE ) { printf("\t* Impossible d'ouvrir le fichier A !\n"); CloseHandle( hFileA ); return -1; } hFileB = CreateFile( fichierB, GENERIC_READ + GENERIC_WRITE, FILE_SHARE_READ + FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); if( hFileB == INVALID_HANDLE_VALUE ) { printf("\t* Impossible d'ouvrir le fichier B !\n"); CloseHandle( hFileB ); return -1; } /* Mapping */ hMapA = CreateFileMapping( hFileA, NULL, PAGE_WRITECOPY, 0, 0, NULL ); hMapB = CreateFileMapping( hFileB, NULL, PAGE_WRITECOPY, 0, 0, NULL ); if( hMapA == NULL || hMapB == NULL ) { printf("\t* Erreur CreateFileMapping !\n"); CloseHandle( hFileA ); CloseHandle( hFileB ); return -1; } /* Buffers */ pFileBaseA = (char *)MapViewOfFile( hMapA, FILE_MAP_COPY, 0, 0, 0 ); pFileBaseB = (char *)MapViewOfFile( hMapB, FILE_MAP_COPY, 0, 0, 0 ); if( pFileBaseA == NULL || pFileBaseB == NULL ) { printf("\t* Erreur MapViewOfFile !\n"); CloseHandle( hMapA ); CloseHandle( hMapB ); CloseHandle( hFileA ); CloseHandle( hFileB ); return -1; } /* Tailles fichiers */ dwFileSizeA = GetFileSize( hFileA, &hwFileSizeA ); dwFileSizeA += hwFileSizeA * MAXDWORD; dwFileSizeB = GetFileSize( hFileB, &hwFileSizeB ); dwFileSizeB += hwFileSizeA * MAXDWORD; /* Comparaison */ if ( ( dwFileSizeA && dwFileSizeB ) && ( dwFileSizeB == dwFileSizeA ) ) { // boucle avec dwFileSizeA (= dwFileSizeB) for( cpt = 0; cpt < dwFileSizeA; cpt++ ) { if ( pFileBaseA[cpt] != pFileBaseB[cpt] ) { // Si différence, stop retour = 1; break; } } } /* Release the kracken ... :-) */ UnmapViewOfFile( pFileBaseA ); UnmapViewOfFile( pFileBaseB ); CloseHandle( hMapA ); CloseHandle( hMapB ); CloseHandle( hFileA ); CloseHandle( hFileB ); return retour; } /* * Entry point (demo) */ int main(int argc, char *argv[]) { printf("%s", copyright); if ( argc != 3 ) { printf("%s\n\t* Nombre d'arguments incorrect !\n", syntaxe); return -1; } else { int iresult = compareFile( argv[1], argv[2] ); switch ( iresult ) { case 0 : { printf("\t* Les fichiers sont identiques\n"); break; } case 1 : { printf("\t* Les fichiers sont differents !\n"); break; } default : printf("\t* Erreur de la fonction compareFile()\n"); } } return 0; }