Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-03 08:20:41

0001 
0002 #include "stdio.h"
0003 
0004 #include "PHmd5Utils.h"
0005 #include "md5.h"
0006 
0007 int PHmd5File(const char * filename,  unsigned char *digest, int *filesize)
0008 {
0009   int status;
0010   FILE *fp;
0011   fp = fopen (filename, "r");
0012 
0013   if (!fp) return 1; 
0014 
0015   status = PHmd5Stream(fp, digest, filesize);
0016   fclose(fp);
0017   return status;
0018 }
0019 
0020 
0021 int PHmd5Stream(FILE *stream,  unsigned char *digest, int *filesize)
0022 {
0023   /* Important: BLOCKSIZE must be a multiple of 64.  */
0024 #define BLOCKSIZE 4096
0025   md5_state_t state;
0026   char buffer[BLOCKSIZE + 72];
0027   size_t sum;
0028 
0029   /* Initialize the computation context.  */
0030   md5_init(&state);
0031 
0032   /* Iterate over full file contents.  */
0033   *filesize = 0;
0034   while (1)
0035     {
0036       /* We read the file in blocks of BLOCKSIZE bytes.  One call of the
0037      computation function processes the whole buffer so that with the
0038      next round of the loop another block can be read.  */
0039       size_t n;
0040       sum = 0;
0041 
0042       /* Read block.  Take care for partial reads.  */
0043       do
0044     {
0045       n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
0046 
0047       sum += n;
0048       *filesize += n;
0049     }
0050       while (sum < BLOCKSIZE && n != 0);
0051       if (n == 0 && ferror (stream))
0052         return 1;
0053       
0054       /* If end of file is reached, end the loop.  */
0055       if (n == 0)
0056     break;
0057       
0058       /* Process buffer with BLOCKSIZE bytes.  Note that
0059      BLOCKSIZE % 64 == 0
0060        */
0061       md5_append(&state, (const md5_byte_t *)buffer,BLOCKSIZE );
0062     /*      md5_process_block (buffer, BLOCKSIZE, &ctx); */
0063     }
0064 
0065   /* Add the last bytes if necessary.  */
0066   if (sum > 0) md5_append(&state, (const md5_byte_t *)buffer,sum );
0067   /*   md5_process_bytes (buffer, sum, &ctx); */
0068 
0069   /* Construct result in desired memory.  */
0070   md5_finish(&state,  (md5_byte_t *) digest);
0071   /*  md5_finish_ctx (&ctx, resblock); */
0072   return 0;
0073 }
0074