OSDN Git Service

pre-pTeX: tex0, tex1.c, tex2.c.
[putex/putex.git] / src / texsourc / md5file.c
1 /* Copyright 2014 Clerk Ma
2
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License as published by
5    the Free Software Foundation; either version 2 of the License, or
6    (at your option) any later version.
7
8    This program is distributed in the hope that it will be useful, but
9    WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11    General Public License for more details.
12
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16    02110-1301 USA.  */
17
18 #pragma warning(disable:4996)
19
20 #include <stdio.h>
21 #include "libmd5/md5.h"
22
23 char * md5_file(const char * file_name)
24 {
25   md5_state_t md5_ship;
26   md5_byte_t  md5_data[1024];
27   md5_byte_t  md5_digest[16];
28   static char md5_hex[33];
29   int         md5_len;
30   FILE * ship_file;
31   int i;
32
33   ship_file = fopen(file_name, "rb");
34
35   md5_init(&md5_ship);
36
37   while ((md5_len = fread(md5_data, 1, 1024, ship_file)) != 0)
38     md5_append(&md5_ship, md5_data, md5_len);
39
40   md5_finish(&md5_ship, md5_digest);
41
42   fclose(ship_file);
43
44   for (i = 0; i < 16; ++i)
45     sprintf(md5_hex + i * 2, "%02X", md5_digest[i]);
46
47   return md5_hex;
48 }
49
50 #ifdef MD5TEST
51 int main(void)
52 {
53   printf("%s", md5_file("md5.c"));
54
55   return 0;
56 }
57 #endif