/*----------------------------------------------------------------------------*/ /** @file fits_md5.c @author N. Devillard @date May 2001 @version $Revision: 1.8 $ @brief FITS data block MD5 computation routine. This module offers MD5 computation over all data areas of a FITS file. */ /*----------------------------------------------------------------------------*/ /* $Id: fits_md5.c,v 1.8 2003/01/23 09:13:18 yjung Exp $ $Author: yjung $ $Date: 2003/01/23 09:13:18 $ $Revision: 1.8 $ */ /*----------------------------------------------------------------------------- Includes -----------------------------------------------------------------------------*/ #include #include #include #include #include #include "md5.h" #include "fits_std.h" #include "qerror.h" /*----------------------------------------------------------------------------- Defines -----------------------------------------------------------------------------*/ /** Size of an MD5 hash in bytes (32 bytes are 128 bits) */ #define MD5HASHSZ 32 /*----------------------------------------------------------------------------- Function code -----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/ /** @brief Compute the MD5 hash of data zones in a FITS file. @param filename Name of the FITS file to examine. @return 1 statically allocated character string, or NULL. This function expects the name of a FITS file. It will compute the MD5 hash on all data blocks in the main data section and possibly extensions (including zero-padding blocks if necessary) and return it as a string suitable for inclusion into a FITS keyword. The returned string is statically allocated inside this function, so do not free it or modify it. This function returns NULL in case of error. */ /*----------------------------------------------------------------------------*/ char * qfits_datamd5(char * filename) { static char datamd5[MD5HASHSZ+1] ; struct MD5Context ctx ; unsigned char digest[16] ; FILE * in ; char buf[FITS_BLOCK_SIZE]; char * buf_c ; int i ; int in_header ; int check_fits ; /* Check entries */ if (filename==NULL) return NULL ; /* Open input file */ if ((in=fopen(filename, "r"))==NULL) { qfits_error("cannot open file %s", filename); return NULL ; } /* Initialize all variables */ MD5Init(&ctx); in_header=1 ; check_fits=0 ; /* Loop over input file */ while (fread(buf, 1, FITS_BLOCK_SIZE, in)==FITS_BLOCK_SIZE) { /* First time in the loop: check the file is FITS */ if (check_fits==0) { /* Examine first characters in block */ if (buf[0]!='S' || buf[1]!='I' || buf[2]!='M' || buf[3]!='P' || buf[4]!='L' || buf[5]!='E' || buf[6]!=' ' || buf[7]!=' ' || buf[8]!='=') { qfits_error("file [%s] is not FITS\n", filename); fclose(in); return NULL ; } else { check_fits=1 ; } } if (in_header) { buf_c = buf ; for (i=0 ; i