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
0024 #define BLOCKSIZE 4096
0025 md5_state_t state;
0026 char buffer[BLOCKSIZE + 72];
0027 size_t sum;
0028
0029
0030 md5_init(&state);
0031
0032
0033 *filesize = 0;
0034 while (1)
0035 {
0036
0037
0038
0039 size_t n;
0040 sum = 0;
0041
0042
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
0055 if (n == 0)
0056 break;
0057
0058
0059
0060
0061 md5_append(&state, (const md5_byte_t *)buffer,BLOCKSIZE );
0062
0063 }
0064
0065
0066 if (sum > 0) md5_append(&state, (const md5_byte_t *)buffer,sum );
0067
0068
0069
0070 md5_finish(&state, (md5_byte_t *) digest);
0071
0072 return 0;
0073 }
0074